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