]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
more exit cleanup actions
[netdata.git] / src / plugin_tc.c
1 #include "common.h"
2
3 #define RRD_TYPE_TC                 "tc"
4 #define RRD_TYPE_TC_LEN             strlen(RRD_TYPE_TC)
5
6 // ----------------------------------------------------------------------------
7 // /sbin/tc processor
8 // this requires the script plugins.d/tc-qos-helper.sh
9
10 #define TC_LINE_MAX 1024
11
12 struct tc_class {
13     avl avl;
14
15     char *id;
16     uint32_t hash;
17
18     char *name;
19
20     char *leafid;
21     uint32_t leaf_hash;
22
23     char *parentid;
24     uint32_t parent_hash;
25
26     char hasparent;
27     char isleaf;
28     unsigned long long bytes;
29     unsigned long long packets;
30     unsigned long long dropped;
31     unsigned long long overlimits;
32     unsigned long long requeues;
33     unsigned long long lended;
34     unsigned long long borrowed;
35     unsigned long long giants;
36     unsigned long long tokens;
37     unsigned long long ctokens;
38
39     RRDDIM *rd_bytes;
40     RRDDIM *rd_packets;
41     RRDDIM *rd_dropped;
42     RRDDIM *rd_tokens;
43     RRDDIM *rd_ctokens;
44
45     char name_updated;
46     char updated;   // updated bytes
47     int seen;       // seen in the tc list (even without bytes)
48
49     struct tc_class *next;
50     struct tc_class *prev;
51 };
52
53 struct tc_device {
54     avl avl;
55
56     char *id;
57     uint32_t hash;
58
59     char *name;
60     char *family;
61
62     char name_updated;
63     char family_updated;
64
65     char enabled;
66     char enabled_bytes;
67     char enabled_packets;
68     char enabled_dropped;
69     char enabled_tokens;
70     char enabled_ctokens;
71
72     RRDSET *st_bytes;
73     RRDSET *st_packets;
74     RRDSET *st_dropped;
75     RRDSET *st_tokens;
76     RRDSET *st_ctokens;
77
78     avl_tree classes_index;
79
80     struct tc_class *classes;
81
82     struct tc_device *next;
83     struct tc_device *prev;
84 };
85
86
87 struct tc_device *tc_device_root = NULL;
88
89 // ----------------------------------------------------------------------------
90 // tc_device index
91
92 static int tc_device_compare(void* a, void* b) {
93     if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
94     else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
95     else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
96 }
97
98 avl_tree tc_device_root_index = {
99         NULL,
100         tc_device_compare
101 };
102
103 #define tc_device_index_add(st) (struct tc_device *)avl_insert(&tc_device_root_index, (avl *)(st))
104 #define tc_device_index_del(st) (struct tc_device *)avl_remove(&tc_device_root_index, (avl *)(st))
105
106 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
107     struct tc_device tmp;
108     tmp.id = (char *)id;
109     tmp.hash = (hash)?hash:simple_hash(tmp.id);
110
111     return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
112 }
113
114
115 // ----------------------------------------------------------------------------
116 // tc_class index
117
118 static int tc_class_compare(void* a, void* b) {
119     if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
120     else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
121     else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
122 }
123
124 #define tc_class_index_add(st, rd) (struct tc_class *)avl_insert(&((st)->classes_index), (avl *)(rd))
125 #define tc_class_index_del(st, rd) (struct tc_class *)avl_remove(&((st)->classes_index), (avl *)(rd))
126
127 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
128     struct tc_class tmp;
129     tmp.id = (char *)id;
130     tmp.hash = (hash)?hash:simple_hash(tmp.id);
131
132     return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
133 }
134
135 // ----------------------------------------------------------------------------
136
137 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
138     if(c == n->classes) {
139         if(c->next)
140             n->classes = c->next;
141         else
142             n->classes = c->prev;
143     }
144     if(c->next) c->next->prev = c->prev;
145     if(c->prev) c->prev->next = c->next;
146
147     debug(D_TC_LOOP, "Removing from device '%s' class '%s', parentid '%s', leafid '%s', seen=%d", n->id, c->id, c->parentid?c->parentid:"", c->leafid?c->leafid:"", c->seen);
148
149     if(unlikely(tc_class_index_del(n, c) != c))
150         error("plugin_tc: INTERNAL ERROR: attempt remove class '%s' from device '%s': removed a different calls", c->id, n->id);
151
152     freez(c->id);
153     freez(c->name);
154     freez(c->leafid);
155     freez(c->parentid);
156     freez(c);
157 }
158
159 static inline void tc_device_classes_cleanup(struct tc_device *d) {
160     static int cleanup_every = 999;
161
162     if(unlikely(cleanup_every > 0)) {
163         cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
164         if(cleanup_every < 0) cleanup_every = -cleanup_every;
165     }
166
167     d->name_updated = 0;
168     d->family_updated = 0;
169
170     struct tc_class *c = d->classes;
171     while(c) {
172         if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
173             struct tc_class *nc = c->next;
174             tc_class_free(d, c);
175             c = nc;
176         }
177         else {
178             c->updated = 0;
179             c->name_updated = 0;
180
181             c = c->next;
182         }
183     }
184 }
185
186 static inline void tc_device_commit(struct tc_device *d) {
187     static int enable_new_interfaces = -1, enable_bytes = -1, enable_packets = -1, enable_dropped = -1, enable_tokens = -1, enable_ctokens = -1;
188
189     if(unlikely(enable_new_interfaces == -1)) {
190         enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
191         enable_bytes          = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
192         enable_packets        = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
193         enable_dropped        = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
194         enable_tokens         = config_get_boolean_ondemand("plugin:tc", "enable tokens charts for all interfaces", CONFIG_ONDEMAND_NO);
195         enable_ctokens        = config_get_boolean_ondemand("plugin:tc", "enable ctokens charts for all interfaces", CONFIG_ONDEMAND_NO);
196     }
197
198     // we only need to add leaf classes
199     struct tc_class *c, *x;
200     unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0, tokens_sum = 0, ctokens_sum = 0;
201     int active_classes = 0;
202
203     // set all classes
204     for(c = d->classes ; c ; c = c->next) {
205         c->isleaf = 1;
206         c->hasparent = 0;
207     }
208
209     // mark the classes as leafs and parents
210     for(c = d->classes ; c ; c = c->next) {
211         if(unlikely(!c->updated)) continue;
212
213         for(x = d->classes ; x ; x = x->next) {
214             if(unlikely(!x->updated)) continue;
215
216             if(unlikely(c == x)) continue;
217
218             if(x->parentid && (
219                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
220                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
221                 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has as leaf class '%s' (parentid: '%s').", d->name?d->name:d->id, c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->name?x->name:x->id, x->parentid?x->parentid:x->id);
222                 c->isleaf = 0;
223                 x->hasparent = 1;
224             }
225         }
226     }
227
228     // debugging only
229     /*
230     if(unlikely(debug_flags & D_TC_LOOP)) {
231         for(c = d->classes ; c ; c = c->next) {
232             if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device '%s', class %s, OK", d->name, c->id);
233             else debug(D_TC_LOOP, "TC: Device '%s', class %s, IGNORE (isleaf: %d, hasparent: %d, parent: %s)", d->name?d->name:d->id, c->id, c->isleaf, c->hasparent, c->parentid?c->parentid:"(unset)");
234         }
235     }
236     */
237
238     // we need at least a class
239     for(c = d->classes ; c ; c = c->next) {
240         // debug(D_TC_LOOP, "TC: Device '%s', class '%s', isLeaf=%d, HasParent=%d, Seen=%d", d->name?d->name:d->id, c->name?c->name:c->id, c->isleaf, c->hasparent, c->seen);
241         if(unlikely(c->updated && c->isleaf && c->hasparent)) {
242             active_classes++;
243             bytes_sum += c->bytes;
244             packets_sum += c->packets;
245             dropped_sum += c->dropped;
246             tokens_sum += c->tokens;
247             ctokens_sum += c->ctokens;
248         }
249     }
250
251     if(unlikely(!active_classes)) {
252         debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
253         tc_device_classes_cleanup(d);
254         return;
255     }
256
257     if(unlikely(d->enabled == (char)-1)) {
258         char var_name[CONFIG_MAX_NAME + 1];
259         snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
260         d->enabled         = config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
261
262         snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
263         d->enabled_bytes   = config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
264
265         snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
266         d->enabled_packets = config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
267
268         snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
269         d->enabled_dropped = config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
270
271         snprintfz(var_name, CONFIG_MAX_NAME, "tokens chart for %s", d->id);
272         d->enabled_tokens = config_get_boolean_ondemand("plugin:tc", var_name, enable_tokens);
273
274         snprintfz(var_name, CONFIG_MAX_NAME, "ctokens chart for %s", d->id);
275         d->enabled_ctokens = config_get_boolean_ondemand("plugin:tc", var_name, enable_ctokens);
276     }
277
278     debug(D_TC_LOOP, "TC: evaluating TC device '%s'. enabled = %d/%d (bytes: %d/%d, packets: %d/%d, dropped: %d/%d, tokens: %d/%d, ctokens: %d/%d), classes = %d (bytes = %llu, packets = %llu, dropped = %llu, tokens = %llu, ctokens = %llu).",
279         d->name?d->name:d->id,
280         d->enabled, enable_new_interfaces,
281         d->enabled_bytes, enable_bytes,
282         d->enabled_packets, enable_packets,
283         d->enabled_dropped, enable_dropped,
284         d->enabled_tokens, enable_tokens,
285         d->enabled_ctokens, enable_ctokens,
286         active_classes,
287         bytes_sum,
288         packets_sum,
289         dropped_sum,
290         tokens_sum,
291         ctokens_sum
292         );
293
294     if(likely(d->enabled)) {
295         // --------------------------------------------------------------------
296         // bytes
297
298         if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
299             d->enabled_bytes = CONFIG_ONDEMAND_YES;
300
301             if(unlikely(!d->st_bytes)) {
302                 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
303                 if(unlikely(!d->st_bytes)) {
304                     debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
305                     d->st_bytes = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, RRD_TYPE_TC ".qos", "Class Usage", "kilobits/s", 7000, rrd_update_every, RRDSET_TYPE_STACKED);
306                 }
307             }
308             else {
309                 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
310                 rrdset_next(d->st_bytes);
311
312                 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
313                     rrdset_set_name(d->st_bytes, d->name);
314                     d->name_updated = 0;
315                 }
316
317                 // FIXME
318                 // update the family
319             }
320
321             for(c = d->classes ; c ; c = c->next) {
322                 if(unlikely(!c->updated)) continue;
323
324                 if(c->isleaf && c->hasparent) {
325                     c->seen++;
326
327                     if(unlikely(!c->rd_bytes)) {
328                         c->rd_bytes = rrddim_find(d->st_bytes, c->id);
329                         if(unlikely(!c->rd_bytes)) {
330                             debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
331
332                             // new class, we have to add it
333                             c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
334                         }
335                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
336                     }
337
338                     rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
339
340                     // if it has a name, different to the id
341                     if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
342                         // update the rrd dimension with the new name
343                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
344                         rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
345                     }
346                 }
347             }
348             rrdset_done(d->st_bytes);
349         }
350
351         // --------------------------------------------------------------------
352         // packets
353         
354         if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
355             d->enabled_packets = CONFIG_ONDEMAND_YES;
356
357             if(unlikely(!d->st_packets)) {
358                 char id[RRD_ID_LENGTH_MAX + 1];
359                 char name[RRD_ID_LENGTH_MAX + 1];
360                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
361                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
362
363                 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
364                 if(unlikely(!d->st_packets)) {
365                     debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
366                     d->st_packets = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_packets", "Class Packets", "packets/s", 7010, rrd_update_every, RRDSET_TYPE_STACKED);
367                 }
368             }
369             else {
370                 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
371                 rrdset_next(d->st_packets);
372
373                 // FIXME
374                 // update the family
375             }
376
377             for(c = d->classes ; c ; c = c->next) {
378                 if(unlikely(!c->updated)) continue;
379
380                 if(c->isleaf && c->hasparent) {
381                     if(unlikely(!c->rd_packets)) {
382                         c->rd_packets = rrddim_find(d->st_packets, c->id);
383                         if(unlikely(!c->rd_packets)) {
384                             debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
385
386                             // new class, we have to add it
387                             c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
388                         }
389                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
390                     }
391
392                     rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
393
394                     // if it has a name, different to the id
395                     if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
396                         // update the rrd dimension with the new name
397                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
398                         rrddim_set_name(d->st_packets, c->rd_packets, c->name);
399                     }
400                 }
401             }
402             rrdset_done(d->st_packets);
403         }
404
405         // --------------------------------------------------------------------
406         // dropped
407         
408         if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
409             d->enabled_dropped = CONFIG_ONDEMAND_YES;
410             
411             if(unlikely(!d->st_dropped)) {
412                 char id[RRD_ID_LENGTH_MAX + 1];
413                 char name[RRD_ID_LENGTH_MAX + 1];
414                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
415                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
416
417                 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
418                 if(unlikely(!d->st_dropped)) {
419                     debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
420                     d->st_dropped = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_dropped", "Class Dropped Packets", "packets/s", 7020, rrd_update_every, RRDSET_TYPE_STACKED);
421                 }
422             }
423             else {
424                 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
425                 rrdset_next(d->st_dropped);
426
427                 // FIXME
428                 // update the family
429             }
430
431             for(c = d->classes ; c ; c = c->next) {
432                 if(unlikely(!c->updated)) continue;
433
434                 if(c->isleaf && c->hasparent) {
435                     if(unlikely(!c->rd_dropped)) {
436                         c->rd_dropped = rrddim_find(d->st_dropped, c->id);
437                         if(unlikely(!c->rd_dropped)) {
438                             debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
439
440                             // new class, we have to add it
441                             c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
442                         }
443                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
444                     }
445
446                     rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
447
448                     // if it has a name, different to the id
449                     if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
450                         // update the rrd dimension with the new name
451                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
452                         rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
453                     }
454                 }
455             }
456             rrdset_done(d->st_dropped);
457         }
458
459         // --------------------------------------------------------------------
460         // tokens
461         
462         if(d->enabled_tokens == CONFIG_ONDEMAND_YES || (d->enabled_tokens == CONFIG_ONDEMAND_ONDEMAND && tokens_sum)) {
463             d->enabled_tokens = CONFIG_ONDEMAND_YES;
464             
465             if(unlikely(!d->st_tokens)) {
466                 char id[RRD_ID_LENGTH_MAX + 1];
467                 char name[RRD_ID_LENGTH_MAX + 1];
468                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_tokens", d->id);
469                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
470
471                 d->st_tokens = rrdset_find_bytype(RRD_TYPE_TC, id);
472                 if(unlikely(!d->st_tokens)) {
473                     debug(D_TC_LOOP, "TC: Creating new _tokens chart for device '%s'", d->name?d->name:d->id);
474                     d->st_tokens = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_tokens", "Class Tokens", "tokens", 7030, rrd_update_every, RRDSET_TYPE_LINE);
475                 }
476             }
477             else {
478                 debug(D_TC_LOOP, "TC: Updating _tokens chart for device '%s'", d->name?d->name:d->id);
479                 rrdset_next(d->st_tokens);
480
481                 // FIXME
482                 // update the family
483             }
484
485             for(c = d->classes ; c ; c = c->next) {
486                 if(unlikely(!c->updated)) continue;
487
488                 if(c->isleaf && c->hasparent) {
489                     if(unlikely(!c->rd_tokens)) {
490                         c->rd_tokens = rrddim_find(d->st_tokens, c->id);
491                         if(unlikely(!c->rd_tokens)) {
492                             debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_tokens->id, c->id, c->name);
493
494                             // new class, we have to add it
495                             c->rd_tokens = rrddim_add(d->st_tokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
496                         }
497                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_tokens->id, c->id);
498                     }
499
500                     rrddim_set_by_pointer(d->st_tokens, c->rd_tokens, c->tokens);
501
502                     // if it has a name, different to the id
503                     if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
504                         // update the rrd dimension with the new name
505                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_tokens->id, c->rd_tokens->id, c->name);
506                         rrddim_set_name(d->st_tokens, c->rd_tokens, c->name);
507                     }
508                 }
509             }
510             rrdset_done(d->st_tokens);
511         }
512
513         // --------------------------------------------------------------------
514         // ctokens
515         
516         if(d->enabled_ctokens == CONFIG_ONDEMAND_YES || (d->enabled_ctokens == CONFIG_ONDEMAND_ONDEMAND && ctokens_sum)) {
517             d->enabled_ctokens = CONFIG_ONDEMAND_YES;
518             
519             if(unlikely(!d->st_ctokens)) {
520                 char id[RRD_ID_LENGTH_MAX + 1];
521                 char name[RRD_ID_LENGTH_MAX + 1];
522                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_ctokens", d->id);
523                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
524
525                 d->st_ctokens = rrdset_find_bytype(RRD_TYPE_TC, id);
526                 if(unlikely(!d->st_ctokens)) {
527                     debug(D_TC_LOOP, "TC: Creating new _ctokens chart for device '%s'", d->name?d->name:d->id);
528                     d->st_ctokens = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_ctokens", "Class cTokens", "ctokens", 7040, rrd_update_every, RRDSET_TYPE_LINE);
529                 }
530             }
531             else {
532                 debug(D_TC_LOOP, "TC: Updating _ctokens chart for device '%s'", d->name?d->name:d->id);
533                 rrdset_next(d->st_ctokens);
534
535                 // FIXME
536                 // update the family
537             }
538
539             for(c = d->classes ; c ; c = c->next) {
540                 if(unlikely(!c->updated)) continue;
541
542                 if(c->isleaf && c->hasparent) {
543                     if(unlikely(!c->rd_ctokens)) {
544                         c->rd_ctokens = rrddim_find(d->st_ctokens, c->id);
545                         if(unlikely(!c->rd_ctokens)) {
546                             debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_ctokens->id, c->id, c->name);
547
548                             // new class, we have to add it
549                             c->rd_ctokens = rrddim_add(d->st_ctokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
550                         }
551                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_ctokens->id, c->id);
552                     }
553
554                     rrddim_set_by_pointer(d->st_ctokens, c->rd_ctokens, c->ctokens);
555
556                     // if it has a name, different to the id
557                     if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
558                         // update the rrd dimension with the new name
559                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_ctokens->id, c->rd_ctokens->id, c->name);
560                         rrddim_set_name(d->st_ctokens, c->rd_ctokens, c->name);
561                     }
562                 }
563             }
564             rrdset_done(d->st_ctokens);
565         }
566     }
567
568     tc_device_classes_cleanup(d);
569 }
570
571 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
572 {
573     struct tc_class *c = tc_class_index_find(d, id, 0);
574     if(likely(c)) {
575         freez(c->name);
576         c->name = NULL;
577
578         if(likely(name && *name && strcmp(c->id, name) != 0)) {
579             debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
580             c->name = strdupz(name);
581             c->name_updated = 1;
582         }
583     }
584 }
585
586 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
587     freez(d->name);
588     d->name = NULL;
589
590     if(likely(name && *name && strcmp(d->id, name) != 0)) {
591         debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
592         d->name = strdupz(name);
593         d->name_updated = 1;
594     }
595 }
596
597 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
598     freez(d->family);
599     d->family = NULL;
600
601     if(likely(family && *family && strcmp(d->id, family) != 0)) {
602         debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
603         d->family = strdupz(family);
604         d->family_updated = 1;
605     }
606     // no need for null termination - it is already null
607 }
608
609 static inline struct tc_device *tc_device_create(char *id)
610 {
611     struct tc_device *d = tc_device_index_find(id, 0);
612
613     if(!d) {
614         debug(D_TC_LOOP, "TC: Creating device '%s'", id);
615
616         d = callocz(1, sizeof(struct tc_device));
617
618         d->id = strdupz(id);
619         d->hash = simple_hash(d->id);
620         d->enabled = (char)-1;
621
622         avl_init(&d->classes_index, tc_class_compare);
623         if(unlikely(tc_device_index_add(d) != d))
624             error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", d->id);
625
626         if(!tc_device_root) {
627             tc_device_root = d;
628         }
629         else {
630             d->next = tc_device_root;
631             tc_device_root->prev = d;
632             tc_device_root = d;
633         }
634     }
635
636     return(d);
637 }
638
639 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
640 {
641     struct tc_class *c = tc_class_index_find(n, id, 0);
642
643     if(!c) {
644         debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
645
646         c = callocz(1, sizeof(struct tc_class));
647
648         if(n->classes) n->classes->prev = c;
649         c->next = n->classes;
650         n->classes = c;
651
652         c->id = strdupz(id);
653         c->hash = simple_hash(c->id);
654
655         if(parentid && *parentid) {
656             c->parentid = strdupz(parentid);
657             c->parent_hash = simple_hash(c->parentid);
658         }
659
660         if(leafid && *leafid) {
661             c->leafid = strdupz(leafid);
662             c->leaf_hash = simple_hash(c->leafid);
663         }
664
665         if(unlikely(tc_class_index_add(n, c) != c))
666             error("plugin_tc: INTERNAL ERROR: attempt index class '%s' on device '%s': already exists", c->id, n->id);
667     }
668
669     c->seen = 1;
670
671     return(c);
672 }
673
674 static inline void tc_device_free(struct tc_device *n)
675 {
676     if(n->next) n->next->prev = n->prev;
677     if(n->prev) n->prev->next = n->next;
678     if(tc_device_root == n) {
679         if(n->next) tc_device_root = n->next;
680         else tc_device_root = n->prev;
681     }
682
683     if(unlikely(tc_device_index_del(n) != n))
684         error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", n->id);
685
686     while(n->classes) tc_class_free(n, n->classes);
687
688     freez(n->id);
689     freez(n->name);
690     freez(n->family);
691     freez(n);
692 }
693
694 static inline void tc_device_free_all()
695 {
696     while(tc_device_root)
697         tc_device_free(tc_device_root);
698 }
699
700 #define MAX_WORDS 20
701
702 static inline int tc_space(char c) {
703     switch(c) {
704     case ' ':
705     case '\t':
706     case '\r':
707     case '\n':
708         return 1;
709
710     default:
711         return 0;
712     }
713 }
714
715 static inline void tc_split_words(char *str, char **words, int max_words) {
716     char *s = str;
717     int i = 0;
718
719     // skip all white space
720     while(tc_space(*s)) s++;
721
722     // store the first word
723     words[i++] = s;
724
725     // while we have something
726     while(*s) {
727         // if it is a space
728         if(unlikely(tc_space(*s))) {
729
730             // terminate the word
731             *s++ = '\0';
732
733             // skip all white space
734             while(tc_space(*s)) s++;
735
736             // if we reached the end, stop
737             if(!*s) break;
738
739             // store the next word
740             if(i < max_words) words[i++] = s;
741             else break;
742         }
743         else s++;
744     }
745
746     // terminate the words
747     while(i < max_words) words[i++] = NULL;
748 }
749
750 volatile pid_t tc_child_pid = 0;
751 void *tc_main(void *ptr) {
752     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
753
754     info("TC thread created with task id %d", gettid());
755
756     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
757         error("Cannot set pthread cancel type to DEFERRED.");
758
759     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
760         error("Cannot set pthread cancel state to ENABLE.");
761
762     struct rusage thread;
763     RRDSET *stcpu = NULL, *sttime = NULL;
764
765     char buffer[TC_LINE_MAX+1] = "";
766     char *words[MAX_WORDS] = { NULL };
767
768     uint32_t BEGIN_HASH = simple_hash("BEGIN");
769     uint32_t END_HASH = simple_hash("END");
770     uint32_t CLASS_HASH = simple_hash("class");
771     uint32_t SENT_HASH = simple_hash("Sent");
772     uint32_t LENDED_HASH = simple_hash("lended:");
773     uint32_t TOKENS_HASH = simple_hash("tokens:");
774     uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
775     uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
776     uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
777     uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
778 #ifdef DETACH_PLUGINS_FROM_NETDATA
779     uint32_t MYPID_HASH = simple_hash("MYPID");
780 #endif
781     uint32_t first_hash;
782
783     snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", config_get("plugins", "plugins directory", PLUGINS_DIR));
784     char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
785     
786     for(;1;) {
787         if(unlikely(netdata_exit)) break;
788
789         FILE *fp;
790         struct tc_device *device = NULL;
791         struct tc_class *class = NULL;
792
793         snprintfz(buffer, TC_LINE_MAX, "exec %s %d", tc_script, rrd_update_every);
794         debug(D_TC_LOOP, "executing '%s'", buffer);
795
796         fp = mypopen(buffer, (pid_t *)&tc_child_pid);
797         if(unlikely(!fp)) {
798             error("TC: Cannot popen(\"%s\", \"r\").", buffer);
799             goto cleanup;
800         }
801
802         while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
803             if(unlikely(netdata_exit)) break;
804
805             buffer[TC_LINE_MAX] = '\0';
806             // debug(D_TC_LOOP, "TC: read '%s'", buffer);
807
808             tc_split_words(buffer, words, MAX_WORDS);
809
810             if(unlikely(!words[0] || !*words[0])) {
811                 // debug(D_TC_LOOP, "empty line");
812                 continue;
813             }
814             // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
815
816             first_hash = simple_hash(words[0]);
817
818             if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
819                 // debug(D_TC_LOOP, "CLASS line on class id='%s', parent='%s', parentid='%s', leaf='%s', leafid='%s'", words[2], words[3], words[4], words[5], words[6]);
820
821                 // words[1] : class type
822                 // words[2] : N:XX
823                 // words[3] : parent or root
824                 if(likely(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0))) {
825                     //char *type     = words[1];  // the class: htb, fq_codel, etc
826
827                     // we are only interested for HTB classes
828                     //if(strcmp(type, "htb") != 0) continue;
829
830                     char *id       = words[2];  // the class major:minor
831                     char *parent   = words[3];  // 'parent' or 'root'
832                     char *parentid = words[4];  // the parent's id
833                     char *leaf     = words[5];  // 'leaf'
834                     char *leafid   = words[6];  // leafid
835
836                     if(strcmp(parent, "root") == 0) {
837                         parentid = NULL;
838                         leafid = NULL;
839                     }
840                     else if(!leaf || strcmp(leaf, "leaf") != 0)
841                         leafid = NULL;
842
843                     char leafbuf[20 + 1] = "";
844                     if(leafid && leafid[strlen(leafid) - 1] == ':') {
845                         strncpyz(leafbuf, leafid, 20 - 1);
846                         strcat(leafbuf, "1");
847                         leafid = leafbuf;
848                     }
849
850                     class = tc_class_add(device, id, parentid, leafid);
851                 }
852                 else {
853                     // clear the last class
854                     class = NULL;
855                 }
856             }
857             else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
858                 // debug(D_TC_LOOP, "END line");
859
860                 if(likely(device)) {
861                     if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
862                         error("Cannot set pthread cancel state to DISABLE.");
863
864                     tc_device_commit(device);
865                     // tc_device_free(device);
866
867                     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
868                         error("Cannot set pthread cancel state to ENABLE.");
869                 }
870
871                 device = NULL;
872                 class = NULL;
873             }
874             else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
875                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
876
877                 if(likely(words[1] && *words[1])) {
878                     device = tc_device_create(words[1]);
879                 }
880                 else {
881                     // tc_device_free(device);
882                     device = NULL;
883                 }
884
885                 class = NULL;
886             }
887             else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
888                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
889                 if(likely(words[1] && *words[1])) {
890                     class->bytes = strtoull(words[1], NULL, 10);
891                     class->updated = 1;
892                 }
893                 else {
894                     class->updated = 0;
895                 }
896
897                 if(likely(words[3] && *words[3]))
898                     class->packets = strtoull(words[3], NULL, 10);
899
900                 if(likely(words[6] && *words[6]))
901                     class->dropped = strtoull(words[6], NULL, 10);
902
903                 if(likely(words[8] && *words[8]))
904                     class->overlimits = strtoull(words[8], NULL, 10);
905
906                 if(likely(words[10] && *words[10]))
907                     class->requeues = strtoull(words[8], NULL, 10);
908             }
909             else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
910                 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
911                 if(likely(words[1] && *words[1]))
912                     class->lended = strtoull(words[1], NULL, 10);
913
914                 if(likely(words[3] && *words[3]))
915                     class->borrowed = strtoull(words[3], NULL, 10);
916
917                 if(likely(words[5] && *words[5]))
918                     class->giants = strtoull(words[5], NULL, 10);
919             }
920             else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
921                 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
922                 if(likely(words[1] && *words[1]))
923                     class->tokens = strtoull(words[1], NULL, 10);
924
925                 if(likely(words[3] && *words[3]))
926                     class->ctokens = strtoull(words[3], NULL, 10);
927             }
928             else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
929                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
930                 if(likely(words[1] && *words[1]))
931                     tc_device_set_device_name(device, words[1]);
932             }
933             else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
934                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
935                 if(likely(words[1] && *words[1]))
936                     tc_device_set_device_family(device, words[1]);
937             }
938             else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
939                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
940                 char *id    = words[1];
941                 char *path  = words[2];
942                 if(likely(id && *id && path && *path))
943                     tc_device_set_class_name(device, id, path);
944             }
945             else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
946                 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
947                 getrusage(RUSAGE_THREAD, &thread);
948
949                 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
950                 if(unlikely(!stcpu)) {
951                     stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
952                     rrddim_add(stcpu, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
953                     rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
954                 }
955                 else rrdset_next(stcpu);
956
957                 rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
958                 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
959                 rrdset_done(stcpu);
960
961                 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
962                 if(unlikely(!sttime)) {
963                     sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
964                     rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
965                 }
966                 else rrdset_next(sttime);
967
968                 rrddim_set(sttime, "run_time", atoll(words[1]));
969                 rrdset_done(sttime);
970
971             }
972 #ifdef DETACH_PLUGINS_FROM_NETDATA
973             else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
974                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
975                 char *id = words[1];
976                 pid_t pid = atol(id);
977
978                 if(likely(pid)) tc_child_pid = pid;
979
980                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
981             }
982 #endif
983             //else {
984             //  debug(D_TC_LOOP, "IGNORED line");
985             //}
986         }
987
988         // fgets() failed or loop broke
989         int code = mypclose(fp, (pid_t)tc_child_pid);
990         tc_child_pid = 0;
991
992         if(unlikely(device)) {
993             // tc_device_free(device);
994             device = NULL;
995             class = NULL;
996         }
997
998         if(unlikely(netdata_exit)) {
999             tc_device_free_all();
1000             goto cleanup;
1001         }
1002
1003         if(code == 1 || code == 127) {
1004             // 1 = DISABLE
1005             // 127 = cannot even run it
1006             error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
1007
1008             tc_device_free_all();
1009             goto cleanup;
1010         }
1011
1012         sleep((unsigned int) rrd_update_every);
1013     }
1014
1015 cleanup:
1016     info("TC thread exiting");
1017
1018     static_thread->enabled = 0;
1019     static_thread->thread = NULL;
1020     pthread_exit(NULL);
1021     return NULL;
1022 }