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