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