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