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