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