]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
tc now supports hierarchical qdiscs too
[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         //debug(D_TC_LOOP, "TC: In device '%s', %s '%s'  has leafid: '%s' and parentid '%s'.",
271         //    d->id,
272         //    c->isqdisc?"qdisc":"class",
273         //    c->id,
274         //    c->leafid?c->leafid:"NULL",
275         //    c->parentid?c->parentid:"NULL");
276
277         // find if c is leaf or not
278         for(x = d->classes; x; x = x->next) {
279             if(unlikely(!x->updated || c == x || !x->parentid)) continue;
280
281             if( (c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
282                 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0)) {
283                 // debug(D_TC_LOOP, "TC: In device '%s', %s '%s' (leafid: '%s') has as leaf %s '%s' (parentid: '%s').", d->name?d->name:d->id, c->isqdisc?"qdisc":"class", c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->isqdisc?"qdisc":"class", x->name?x->name:x->id, x->parentid?x->parentid:x->id);
284                 c->isleaf = 0;
285                 x->hasparent = 1;
286             }
287         }
288     }
289
290     // debugging only
291     /*
292     if(unlikely(debug_flags & D_TC_LOOP)) {
293         for(c = d->classes ; c ; c = c->next) {
294             if((c->isleaf && c->hasparent) || d->enabled_all_classes_qdiscs) debug(D_TC_LOOP, "TC: final nodes dump for '%s': class %s, OK", d->name, c->id);
295             else debug(D_TC_LOOP, "TC: final nodes dump for '%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)");
296         }
297     }
298     */
299
300     for(c = d->classes ; c ; c = c->next) {
301         if(unlikely(!c->updated)) continue;
302
303         // debug(D_TC_LOOP, "TC: device '%s', %s '%s' isleaf=%d, hasparent=%d", d->id, (c->isqdisc)?"qdisc":"class", c->id, c->isleaf, c->hasparent);
304
305         if(unlikely((c->isleaf && c->hasparent) || d->enabled_all_classes_qdiscs)) {
306             c->render = 1;
307             active_nodes++;
308             bytes_sum += c->bytes;
309             packets_sum += c->packets;
310             dropped_sum += c->dropped;
311             tokens_sum += c->tokens;
312             ctokens_sum += c->ctokens;
313         }
314
315         //if(unlikely(!c->hasparent)) {
316         //    if(root) error("TC: multiple root class/qdisc for device '%s' (old: '%s', new: '%s')", d->id, root->id, c->id);
317         //    root = c;
318         //    debug(D_TC_LOOP, "TC: found root class/qdisc '%s'", root->id);
319         //}
320     }
321
322     if(unlikely(!active_nodes)) {
323         debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No useful classes/qdiscs.", d->name?d->name:d->id);
324         tc_device_classes_cleanup(d);
325         return;
326     }
327
328     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).",
329         d->name?d->name:d->id,
330         d->enabled, enable_new_interfaces,
331         d->enabled_bytes, enable_bytes,
332         d->enabled_packets, enable_packets,
333         d->enabled_dropped, enable_dropped,
334         d->enabled_tokens, enable_tokens,
335         d->enabled_ctokens, enable_ctokens,
336         d->enabled_all_classes_qdiscs, enabled_all_classes_qdiscs,
337         bytes_sum,
338         packets_sum,
339         dropped_sum,
340         tokens_sum,
341         ctokens_sum
342         );
343
344     // --------------------------------------------------------------------
345     // bytes
346
347     if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
348         d->enabled_bytes = CONFIG_ONDEMAND_YES;
349
350         if(unlikely(!d->st_bytes))
351             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);
352
353         else {
354             rrdset_next(d->st_bytes);
355             if(unlikely(d->name_updated)) rrdset_set_name(d->st_bytes, d->name);
356
357             // FIXME
358             // update the family
359         }
360
361         for(c = d->classes ; c ; c = c->next) {
362             if(unlikely(!c->render)) continue;
363
364             if(unlikely(!c->rd_bytes))
365                 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
366             else if(unlikely(c->name_updated))
367                 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
368
369             rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
370         }
371         rrdset_done(d->st_bytes);
372     }
373
374     // --------------------------------------------------------------------
375     // packets
376
377     if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
378         d->enabled_packets = CONFIG_ONDEMAND_YES;
379
380         if(unlikely(!d->st_packets)) {
381             char id[RRD_ID_LENGTH_MAX + 1];
382             char name[RRD_ID_LENGTH_MAX + 1];
383             snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
384             snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
385
386             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);
387         }
388         else {
389             rrdset_next(d->st_packets);
390
391             if(unlikely(d->name_updated)) {
392                 char name[RRD_ID_LENGTH_MAX + 1];
393                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
394                 rrdset_set_name(d->st_packets, name);
395             }
396
397             // FIXME
398             // update the family
399         }
400
401         for(c = d->classes ; c ; c = c->next) {
402             if(unlikely(!c->render)) continue;
403
404             if(unlikely(!c->rd_packets))
405                 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
406             else if(unlikely(c->name_updated))
407                 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
408
409             rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
410         }
411         rrdset_done(d->st_packets);
412     }
413
414     // --------------------------------------------------------------------
415     // dropped
416
417     if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
418         d->enabled_dropped = CONFIG_ONDEMAND_YES;
419
420         if(unlikely(!d->st_dropped)) {
421             char id[RRD_ID_LENGTH_MAX + 1];
422             char name[RRD_ID_LENGTH_MAX + 1];
423             snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
424             snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
425
426             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);
427         }
428         else {
429             rrdset_next(d->st_dropped);
430
431             if(unlikely(d->name_updated)) {
432                 char name[RRD_ID_LENGTH_MAX + 1];
433                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
434                 rrdset_set_name(d->st_dropped, name);
435             }
436
437             // FIXME
438             // update the family
439         }
440
441         for(c = d->classes ; c ; c = c->next) {
442             if(unlikely(!c->render)) continue;
443
444             if(unlikely(!c->rd_dropped))
445                 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
446             else if(unlikely(c->name_updated))
447                 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
448
449             rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
450         }
451         rrdset_done(d->st_dropped);
452     }
453
454     // --------------------------------------------------------------------
455     // tokens
456
457     if(d->enabled_tokens == CONFIG_ONDEMAND_YES || (d->enabled_tokens == CONFIG_ONDEMAND_ONDEMAND && tokens_sum)) {
458         d->enabled_tokens = CONFIG_ONDEMAND_YES;
459
460         if(unlikely(!d->st_tokens)) {
461             char id[RRD_ID_LENGTH_MAX + 1];
462             char name[RRD_ID_LENGTH_MAX + 1];
463             snprintfz(id, RRD_ID_LENGTH_MAX, "%s_tokens", d->id);
464             snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
465
466             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);
467         }
468         else {
469             rrdset_next(d->st_tokens);
470
471             if(unlikely(d->name_updated)) {
472                 char name[RRD_ID_LENGTH_MAX + 1];
473                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
474                 rrdset_set_name(d->st_tokens, name);
475             }
476
477             // FIXME
478             // update the family
479         }
480
481         for(c = d->classes ; c ; c = c->next) {
482             if(unlikely(!c->render)) continue;
483
484             if(unlikely(!c->rd_tokens)) {
485                 c->rd_tokens = rrddim_add(d->st_tokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
486             }
487             else if(unlikely(c->name_updated))
488                 rrddim_set_name(d->st_tokens, c->rd_tokens, c->name);
489
490             rrddim_set_by_pointer(d->st_tokens, c->rd_tokens, c->tokens);
491         }
492         rrdset_done(d->st_tokens);
493     }
494
495     // --------------------------------------------------------------------
496     // ctokens
497
498     if(d->enabled_ctokens == CONFIG_ONDEMAND_YES || (d->enabled_ctokens == CONFIG_ONDEMAND_ONDEMAND && ctokens_sum)) {
499         d->enabled_ctokens = CONFIG_ONDEMAND_YES;
500
501         if(unlikely(!d->st_ctokens)) {
502             char id[RRD_ID_LENGTH_MAX + 1];
503             char name[RRD_ID_LENGTH_MAX + 1];
504             snprintfz(id, RRD_ID_LENGTH_MAX, "%s_ctokens", d->id);
505             snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
506
507             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);
508         }
509         else {
510             debug(D_TC_LOOP, "TC: Updating _ctokens chart for device '%s'", d->name?d->name:d->id);
511             rrdset_next(d->st_ctokens);
512
513             if(unlikely(d->name_updated)) {
514                 char name[RRD_ID_LENGTH_MAX + 1];
515                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
516                 rrdset_set_name(d->st_ctokens, name);
517             }
518
519             // FIXME
520             // update the family
521         }
522
523         for(c = d->classes ; c ; c = c->next) {
524             if(unlikely(!c->render)) continue;
525
526             if(unlikely(!c->rd_ctokens))
527                 c->rd_ctokens = rrddim_add(d->st_ctokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
528             else if(unlikely(c->name_updated))
529                 rrddim_set_name(d->st_ctokens, c->rd_ctokens, c->name);
530
531             rrddim_set_by_pointer(d->st_ctokens, c->rd_ctokens, c->ctokens);
532         }
533         rrdset_done(d->st_ctokens);
534     }
535
536     tc_device_classes_cleanup(d);
537 }
538
539 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name) {
540     if(unlikely(!name || !*name)) return;
541
542     struct tc_class *c = tc_class_index_find(d, id, 0);
543     if(likely(c)) {
544         if(likely(c->name)) {
545             if(!strcmp(c->name, name)) return;
546             freez(c->name);
547             c->name = NULL;
548         }
549
550         if(likely(name && *name && strcmp(c->id, name) != 0)) {
551             debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
552             c->name = strdupz(name);
553             c->name_updated = 1;
554         }
555     }
556 }
557
558 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
559     if(unlikely(!name || !*name)) return;
560
561     if(d->name) {
562         if(!strcmp(d->name, name)) return;
563         freez(d->name);
564         d->name = NULL;
565     }
566
567     if(likely(name && *name && strcmp(d->id, name))) {
568         debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
569         d->name = strdupz(name);
570         d->name_updated = 1;
571     }
572 }
573
574 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
575     freez(d->family);
576     d->family = NULL;
577
578     if(likely(family && *family && strcmp(d->id, family) != 0)) {
579         debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
580         d->family = strdupz(family);
581         d->family_updated = 1;
582     }
583     // no need for null termination - it is already null
584 }
585
586 static inline struct tc_device *tc_device_create(char *id)
587 {
588     struct tc_device *d = tc_device_index_find(id, 0);
589
590     if(!d) {
591         debug(D_TC_LOOP, "TC: Creating device '%s'", id);
592
593         d = callocz(1, sizeof(struct tc_device));
594
595         d->id = strdupz(id);
596         d->hash = simple_hash(d->id);
597         d->enabled = (char)-1;
598
599         avl_init(&d->classes_index, tc_class_compare);
600         if(unlikely(tc_device_index_add(d) != d))
601             error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", d->id);
602
603         if(!tc_device_root) {
604             tc_device_root = d;
605         }
606         else {
607             d->next = tc_device_root;
608             tc_device_root->prev = d;
609             tc_device_root = d;
610         }
611     }
612
613     return(d);
614 }
615
616 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char qdisc, char *parentid, char *leafid)
617 {
618     struct tc_class *c = tc_class_index_find(n, id, 0);
619
620     if(!c) {
621         debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
622
623         c = callocz(1, sizeof(struct tc_class));
624
625         if(n->classes) n->classes->prev = c;
626         c->next = n->classes;
627         n->classes = c;
628
629         c->id = strdupz(id);
630         c->hash = simple_hash(c->id);
631
632         c->isqdisc = qdisc;
633         if(parentid && *parentid) {
634             c->parentid = strdupz(parentid);
635             c->parent_hash = simple_hash(c->parentid);
636         }
637
638         if(leafid && *leafid) {
639             c->leafid = strdupz(leafid);
640             c->leaf_hash = simple_hash(c->leafid);
641         }
642
643         if(unlikely(tc_class_index_add(n, c) != c))
644             error("plugin_tc: INTERNAL ERROR: attempt index class '%s' on device '%s': already exists", c->id, n->id);
645     }
646     return(c);
647 }
648
649 static inline void tc_device_free(struct tc_device *n)
650 {
651     if(n->next) n->next->prev = n->prev;
652     if(n->prev) n->prev->next = n->next;
653     if(tc_device_root == n) {
654         if(n->next) tc_device_root = n->next;
655         else tc_device_root = n->prev;
656     }
657
658     if(unlikely(tc_device_index_del(n) != n))
659         error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", n->id);
660
661     while(n->classes) tc_class_free(n, n->classes);
662
663     freez(n->id);
664     freez(n->name);
665     freez(n->family);
666     freez(n);
667 }
668
669 static inline void tc_device_free_all()
670 {
671     while(tc_device_root)
672         tc_device_free(tc_device_root);
673 }
674
675 #define MAX_WORDS 20
676
677 static inline int tc_space(char c) {
678     switch(c) {
679     case ' ':
680     case '\t':
681     case '\r':
682     case '\n':
683         return 1;
684
685     default:
686         return 0;
687     }
688 }
689
690 static inline void tc_split_words(char *str, char **words, int max_words) {
691     char *s = str;
692     int i = 0;
693
694     // skip all white space
695     while(tc_space(*s)) s++;
696
697     // store the first word
698     words[i++] = s;
699
700     // while we have something
701     while(*s) {
702         // if it is a space
703         if(unlikely(tc_space(*s))) {
704
705             // terminate the word
706             *s++ = '\0';
707
708             // skip all white space
709             while(tc_space(*s)) s++;
710
711             // if we reached the end, stop
712             if(!*s) break;
713
714             // store the next word
715             if(i < max_words) words[i++] = s;
716             else break;
717         }
718         else s++;
719     }
720
721     // terminate the words
722     while(i < max_words) words[i++] = NULL;
723 }
724
725 volatile pid_t tc_child_pid = 0;
726 void *tc_main(void *ptr) {
727     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
728
729     info("TC thread created with task id %d", gettid());
730
731     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
732         error("Cannot set pthread cancel type to DEFERRED.");
733
734     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
735         error("Cannot set pthread cancel state to ENABLE.");
736
737     struct rusage thread;
738     RRDSET *stcpu = NULL, *sttime = NULL;
739
740     char buffer[TC_LINE_MAX+1] = "";
741     char *words[MAX_WORDS] = { NULL };
742
743     uint32_t BEGIN_HASH = simple_hash("BEGIN");
744     uint32_t END_HASH = simple_hash("END");
745     uint32_t QDISC_HASH = simple_hash("qdisc");
746     uint32_t CLASS_HASH = simple_hash("class");
747     uint32_t SENT_HASH = simple_hash("Sent");
748     uint32_t LENDED_HASH = simple_hash("lended:");
749     uint32_t TOKENS_HASH = simple_hash("tokens:");
750     uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
751     uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
752     uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
753     uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
754 #ifdef DETACH_PLUGINS_FROM_NETDATA
755     uint32_t MYPID_HASH = simple_hash("MYPID");
756 #endif
757     uint32_t first_hash;
758
759     snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", config_get("plugins", "plugins directory", PLUGINS_DIR));
760     char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
761     
762     for(;1;) {
763         if(unlikely(netdata_exit)) break;
764
765         FILE *fp;
766         struct tc_device *device = NULL;
767         struct tc_class *class = NULL;
768
769         snprintfz(buffer, TC_LINE_MAX, "exec %s %d", tc_script, rrd_update_every);
770         debug(D_TC_LOOP, "executing '%s'", buffer);
771
772         fp = mypopen(buffer, (pid_t *)&tc_child_pid);
773         if(unlikely(!fp)) {
774             error("TC: Cannot popen(\"%s\", \"r\").", buffer);
775             goto cleanup;
776         }
777
778         while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
779             if(unlikely(netdata_exit)) break;
780
781             buffer[TC_LINE_MAX] = '\0';
782             // debug(D_TC_LOOP, "TC: read '%s'", buffer);
783
784             tc_split_words(buffer, words, MAX_WORDS);
785
786             if(unlikely(!words[0] || !*words[0])) {
787                 // debug(D_TC_LOOP, "empty line");
788                 continue;
789             }
790             // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
791
792             first_hash = simple_hash(words[0]);
793
794             if(unlikely(device && ((first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) ||  (first_hash == QDISC_HASH && strcmp(words[0], "qdisc") == 0)))) {
795                 // 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]);
796
797                 char *type     = words[1];  // the class/qdisc type: htb, fq_codel, etc
798                 char *id       = words[2];  // the class/qdisc major:minor
799                 char *parent   = words[3];  // the word 'parent' or 'root'
800                 char *parentid = words[4];  // parentid
801                 char *leaf     = words[5];  // the word 'leaf'
802                 char *leafid   = words[6];  // leafid
803
804                 int parent_is_root = 0;
805                 int parent_is_parent = 0;
806                 if(likely(parent)) {
807                     parent_is_parent = !strcmp(parent, "parent");
808
809                     if(!parent_is_parent)
810                         parent_is_root = !strcmp(parent, "root");
811                 }
812
813                 if(likely(type && id && (parent_is_root || parent_is_parent))) {
814                     char qdisc = 0;
815
816                     if(first_hash == QDISC_HASH) {
817                         qdisc = 1;
818
819                         if(!strcmp(type, "ingress")) {
820                             // we don't want to get the ingress qdisc
821                             // there should be an IFB interface for this
822
823                             class = NULL;
824                             continue;
825                         }
826
827                         if(parent_is_parent && parentid) {
828                             // eliminate the minor number from parentid
829                             char *s = parentid;
830                             while(*s && *s != ':') s++;
831                             if(*s == ':') s[1] = '\0';
832                         }
833                     }
834
835                     if(parent_is_root) {
836                         parentid = NULL;
837                         leafid = NULL;
838                     }
839                     else if(!leaf || strcmp(leaf, "leaf") != 0)
840                         leafid = NULL;
841
842                     char leafbuf[20 + 1] = "";
843                     if(leafid && leafid[strlen(leafid) - 1] == ':') {
844                         strncpyz(leafbuf, leafid, 20 - 1);
845                         strcat(leafbuf, "1");
846                         leafid = leafbuf;
847                     }
848
849                     class = tc_class_add(device, id, qdisc, parentid, leafid);
850                 }
851                 else {
852                     // clear the last class
853                     class = NULL;
854                 }
855             }
856             else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
857                 // debug(D_TC_LOOP, "END line");
858
859                 if(likely(device)) {
860                     if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
861                         error("Cannot set pthread cancel state to DISABLE.");
862
863                     tc_device_commit(device);
864                     // tc_device_free(device);
865
866                     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
867                         error("Cannot set pthread cancel state to ENABLE.");
868                 }
869
870                 device = NULL;
871                 class = NULL;
872             }
873             else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
874                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
875
876                 if(likely(words[1] && *words[1])) {
877                     device = tc_device_create(words[1]);
878                 }
879                 else {
880                     // tc_device_free(device);
881                     device = NULL;
882                 }
883
884                 class = NULL;
885             }
886             else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
887                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
888                 if(likely(words[1] && *words[1])) {
889                     class->bytes = str2ull(words[1]);
890                     class->updated = 1;
891                 }
892                 else {
893                     class->updated = 0;
894                 }
895
896                 if(likely(words[3] && *words[3]))
897                     class->packets = str2ull(words[3]);
898
899                 if(likely(words[6] && *words[6]))
900                     class->dropped = str2ull(words[6]);
901
902                 if(likely(words[8] && *words[8]))
903                     class->overlimits = str2ull(words[8]);
904
905                 if(likely(words[10] && *words[10]))
906                     class->requeues = str2ull(words[8]);
907             }
908             else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
909                 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
910                 if(likely(words[1] && *words[1]))
911                     class->lended = str2ull(words[1]);
912
913                 if(likely(words[3] && *words[3]))
914                     class->borrowed = str2ull(words[3]);
915
916                 if(likely(words[5] && *words[5]))
917                     class->giants = str2ull(words[5]);
918             }
919             else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
920                 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
921                 if(likely(words[1] && *words[1]))
922                     class->tokens = str2ull(words[1]);
923
924                 if(likely(words[3] && *words[3]))
925                     class->ctokens = str2ull(words[3]);
926             }
927             else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
928                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
929                 if(likely(words[1] && *words[1]))
930                     tc_device_set_device_name(device, words[1]);
931             }
932             else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
933                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
934                 if(likely(words[1] && *words[1]))
935                     tc_device_set_device_family(device, words[1]);
936             }
937             else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
938                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
939                 char *id    = words[1];
940                 char *path  = words[2];
941                 if(likely(id && *id && path && *path))
942                     tc_device_set_class_name(device, id, path);
943             }
944             else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
945                 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
946                 getrusage(RUSAGE_THREAD, &thread);
947
948                 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
949                 if(unlikely(!stcpu)) {
950                     stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
951                     rrddim_add(stcpu, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
952                     rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
953                 }
954                 else rrdset_next(stcpu);
955
956                 rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
957                 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
958                 rrdset_done(stcpu);
959
960                 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
961                 if(unlikely(!sttime)) {
962                     sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
963                     rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
964                 }
965                 else rrdset_next(sttime);
966
967                 rrddim_set(sttime, "run_time", atoll(words[1]));
968                 rrdset_done(sttime);
969
970             }
971 #ifdef DETACH_PLUGINS_FROM_NETDATA
972             else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
973                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
974                 char *id = words[1];
975                 pid_t pid = atol(id);
976
977                 if(likely(pid)) tc_child_pid = pid;
978
979                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
980             }
981 #endif
982             //else {
983             //  debug(D_TC_LOOP, "IGNORED line");
984             //}
985         }
986
987         // fgets() failed or loop broke
988         int code = mypclose(fp, (pid_t)tc_child_pid);
989         tc_child_pid = 0;
990
991         if(unlikely(device)) {
992             // tc_device_free(device);
993             device = NULL;
994             class = NULL;
995         }
996
997         if(unlikely(netdata_exit)) {
998             tc_device_free_all();
999             goto cleanup;
1000         }
1001
1002         if(code == 1 || code == 127) {
1003             // 1 = DISABLE
1004             // 127 = cannot even run it
1005             error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
1006
1007             tc_device_free_all();
1008             goto cleanup;
1009         }
1010
1011         sleep((unsigned int) rrd_update_every);
1012     }
1013
1014 cleanup:
1015     info("TC thread exiting");
1016
1017     static_thread->enabled = 0;
1018     pthread_exit(NULL);
1019     return NULL;
1020 }