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