]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
Merge pull request #539 from ktsaou/master
[netdata.git] / src / plugin_tc.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/resource.h>
9
10 #include "avl.h"
11 #include "log.h"
12 #include "common.h"
13 #include "appconfig.h"
14 #include "rrd.h"
15 #include "popen.h"
16 #include "plugin_tc.h"
17 #include "main.h"
18 #include "../config.h"
19
20 #define RRD_TYPE_TC                                     "tc"
21 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
22
23 // ----------------------------------------------------------------------------
24 // /sbin/tc processor
25 // this requires the script plugins.d/tc-qos-helper.sh
26
27 #define TC_LINE_MAX 1024
28
29 struct tc_class {
30         avl avl;
31
32         char *id;
33         uint32_t hash;
34
35         char *name;
36
37         char *leafid;
38         uint32_t leaf_hash;
39
40         char *parentid;
41         uint32_t parent_hash;
42
43         char hasparent;
44         char isleaf;
45         unsigned long long bytes;
46         unsigned long long packets;
47         unsigned long long dropped;
48         unsigned long long overlimits;
49         unsigned long long requeues;
50         unsigned long long lended;
51         unsigned long long borrowed;
52         unsigned long long giants;
53         unsigned long long tokens;
54         unsigned long long ctokens;
55
56         char updated;   // updated bytes
57         char seen;              // seen in the tc list (even without bytes)
58
59         struct tc_class *next;
60         struct tc_class *prev;
61 };
62
63 struct tc_device {
64         avl avl;
65
66         char *id;
67         uint32_t hash;
68
69         char *name;
70         char *family;
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         if(c->id) free(c->id);
144         if(c->name) free(c->name);
145         if(c->leafid) free(c->leafid);
146         if(c->parentid) free(c->parentid);
147
148         free(c);
149 }
150
151 static inline void tc_device_classes_cleanup(struct tc_device *d) {
152         static int cleanup_every = 999;
153
154         if(cleanup_every > 0) {
155                 cleanup_every = (int) -config_get_number("plugin:tc", "cleanup unused classes every", 60);
156                 if(cleanup_every > 0) cleanup_every = -cleanup_every;
157                 if(cleanup_every == 0) cleanup_every = -1;
158         }
159
160         struct tc_class *c = d->classes;
161         while(c) {
162                 if(c->seen < cleanup_every) {
163                         struct tc_class *nc = c->next;
164                         tc_class_free(d, c);
165                         c = nc;
166                 }
167                 else c = c->next;
168
169                 if(c) {
170                         c->updated = 0;
171                         c->seen--;
172                 }
173         }
174 }
175
176 static inline void tc_device_commit(struct tc_device *d)
177 {
178         static int enable_new_interfaces = -1;
179
180         if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
181
182         // we only need to add leaf classes
183         struct tc_class *c, *x;
184
185         // set all classes
186         for(c = d->classes ; c ; c = c->next) {
187                 c->isleaf = 1;
188                 c->hasparent = 0;
189         }
190
191         // mark the classes as leafs and parents
192         for(c = d->classes ; c ; c = c->next) {
193                 if(!c->updated) continue;
194
195                 for(x = d->classes ; x ; x = x->next) {
196                         if(!x->updated) continue;
197
198                         if(c == x) continue;
199
200                         if(x->parentid && (
201                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
202                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
203                                 // 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);
204                                 c->isleaf = 0;
205                                 x->hasparent = 1;
206                         }
207                 }
208         }
209
210         // debugging:
211         /*
212         for ( c = d->classes ; c ; c = c->next) {
213                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
214                 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);
215         }
216         */
217
218         // we need at least a class
219         for(c = d->classes ; c ; c = c->next) {
220                 // 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);
221                 if(!c->updated) continue;
222                 if(c->isleaf && c->hasparent) break;
223         }
224         if(!c) {
225                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
226                 tc_device_classes_cleanup(d);
227                 return;
228         }
229
230         char var_name[CONFIG_MAX_NAME + 1];
231         snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
232         if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
233                 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
234                 if(!st) {
235                         debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
236
237                         st = 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);
238
239                         for(c = d->classes ; c ; c = c->next) {
240                                 if(!c->updated) continue;
241
242                                 if(c->isleaf && c->hasparent)
243                                         rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
244                         }
245                 }
246                 else {
247                         debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
248                         rrdset_next_plugins(st);
249
250                         if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
251                 }
252
253                 for(c = d->classes ; c ; c = c->next) {
254                         if(!c->updated) continue;
255
256                         if(c->isleaf && c->hasparent) {
257                                 RRDDIM *rd = rrddim_find(st, c->id);
258
259                                 if(!rd) {
260                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", st->id, c->id, c->name);
261
262                                         // new class, we have to add it
263                                         rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
264                                 }
265                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
266
267                                 rrddim_set_by_pointer(st, rd, c->bytes);
268
269                                 // if it has a name, different to the id
270                                 if(c->name) {
271                                         // update the rrd dimension with the new name
272                                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
273                                         rrddim_set_name(st, rd, c->name);
274
275                                         free(c->name);
276                                         c->name = NULL;
277                                 }
278                         }
279                 }
280                 rrdset_done(st);
281         }
282
283         tc_device_classes_cleanup(d);
284 }
285
286 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
287 {
288         struct tc_class *c = tc_class_index_find(d, id, 0);
289         if(c) {
290                 if(c->name) free(c->name);
291                 c->name = NULL;
292
293                 if(name && *name && strcmp(c->id, name) != 0) {
294                         debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
295                         c->name = strdup(name);
296                 }
297         }
298 }
299
300 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
301         if(d->name) free(d->name);
302         d->name = NULL;
303
304         if(name && *name && strcmp(d->id, name) != 0) {
305                 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
306                 d->name = strdup(name);
307         }
308 }
309
310 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
311         if(d->family) free(d->family);
312         d->family = NULL;
313
314         if(family && *family && strcmp(d->id, family) != 0) {
315                 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
316                 d->family = strdup(family);
317         }
318         // no need for null termination - it is already null
319 }
320
321 static inline struct tc_device *tc_device_create(char *id)
322 {
323         struct tc_device *d = tc_device_index_find(id, 0);
324
325         if(!d) {
326                 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
327
328                 d = calloc(1, sizeof(struct tc_device));
329                 if(!d) {
330                         fatal("Cannot allocate memory for tc_device %s", id);
331                         return NULL;
332                 }
333
334                 d->id = strdup(id);
335                 d->hash = simple_hash(d->id);
336
337                 avl_init(&d->classes_index, tc_class_compare);
338                 tc_device_index_add(d);
339
340                 if(!tc_device_root) {
341                         tc_device_root = d;
342                 }
343                 else {
344                         d->next = tc_device_root;
345                         tc_device_root->prev = d;
346                         tc_device_root = d;
347                 }
348         }
349
350         return(d);
351 }
352
353 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
354 {
355         struct tc_class *c = tc_class_index_find(n, id, 0);
356
357         if(!c) {
358                 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
359
360                 c = calloc(1, sizeof(struct tc_class));
361                 if(!c) {
362                         fatal("Cannot allocate memory for tc class");
363                         return NULL;
364                 }
365
366                 if(n->classes) n->classes->prev = c;
367                 c->next = n->classes;
368                 n->classes = c;
369
370                 c->id = strdup(id);
371                 if(!c->id) {
372                         free(c);
373                         return NULL;
374                 }
375                 c->hash = simple_hash(c->id);
376
377                 if(parentid && *parentid) {
378                         c->parentid = strdup(parentid);
379                         c->parent_hash = simple_hash(c->parentid);
380                 }
381
382                 if(leafid && *leafid) {
383                         c->leafid = strdup(leafid);
384                         c->leaf_hash = simple_hash(c->leafid);
385                 }
386
387                 tc_class_index_add(n, c);
388         }
389
390         c->seen = 1;
391
392         return(c);
393 }
394
395 static inline void tc_device_free(struct tc_device *n)
396 {
397         if(n->next) n->next->prev = n->prev;
398         if(n->prev) n->prev->next = n->next;
399         if(tc_device_root == n) {
400                 if(n->next) tc_device_root = n->next;
401                 else tc_device_root = n->prev;
402         }
403
404         tc_device_index_del(n);
405
406         while(n->classes) tc_class_free(n, n->classes);
407
408         if(n->id) free(n->id);
409         if(n->name) free(n->name);
410         if(n->family) free(n->family);
411
412         free(n);
413 }
414
415 static inline void tc_device_free_all()
416 {
417         while(tc_device_root)
418                 tc_device_free(tc_device_root);
419 }
420
421 #define MAX_WORDS 20
422
423 static inline int tc_space(char c) {
424         switch(c) {
425         case ' ':
426         case '\t':
427         case '\r':
428         case '\n':
429                 return 1;
430
431         default:
432                 return 0;
433         }
434 }
435
436 static inline void tc_split_words(char *str, char **words, int max_words) {
437         char *s = str;
438         int i = 0;
439
440         // skip all white space
441         while(tc_space(*s)) s++;
442
443         // store the first word
444         words[i++] = s;
445
446         // while we have something
447         while(*s) {
448                 // if it is a space
449                 if(tc_space(*s)) {
450
451                         // terminate the word
452                         *s++ = '\0';
453
454                         // skip all white space
455                         while(tc_space(*s)) s++;
456
457                         // if we reached the end, stop
458                         if(!*s) break;
459
460                         // store the next word
461                         if(i < max_words) words[i++] = s;
462                         else break;
463                 }
464                 else s++;
465         }
466
467         // terminate the words
468         while(i < max_words) words[i++] = NULL;
469 }
470
471 pid_t tc_child_pid = 0;
472 void *tc_main(void *ptr)
473 {
474         if(ptr) { ; }
475
476         info("TC thread created with task id %d", gettid());
477
478         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
479                 error("Cannot set pthread cancel type to DEFERRED.");
480
481         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
482                 error("Cannot set pthread cancel state to ENABLE.");
483
484         struct rusage thread;
485         RRDSET *stcpu = NULL, *sttime = NULL;
486
487         char buffer[TC_LINE_MAX+1] = "";
488         char *words[MAX_WORDS] = { NULL };
489
490         uint32_t BEGIN_HASH = simple_hash("BEGIN");
491         uint32_t END_HASH = simple_hash("END");
492         uint32_t CLASS_HASH = simple_hash("class");
493         uint32_t SENT_HASH = simple_hash("Sent");
494         uint32_t LENDED_HASH = simple_hash("lended:");
495         uint32_t TOKENS_HASH = simple_hash("tokens:");
496         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
497         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
498         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
499         uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
500 #ifdef DETACH_PLUGINS_FROM_NETDATA
501         uint32_t MYPID_HASH = simple_hash("MYPID");
502 #endif
503         uint32_t first_hash;
504
505         for(;1;) {
506                 if(unlikely(netdata_exit)) break;
507
508                 FILE *fp;
509                 struct tc_device *device = NULL;
510                 struct tc_class *class = NULL;
511
512                 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", config_get("plugin:tc", "script to run to get tc values", PLUGINS_DIR "/tc-qos-helper.sh"), rrd_update_every);
513                 debug(D_TC_LOOP, "executing '%s'", buffer);
514                 // fp = popen(buffer, "r");
515                 fp = mypopen(buffer, &tc_child_pid);
516                 if(!fp) {
517                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
518                         pthread_exit(NULL);
519                         return NULL;
520                 }
521
522                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
523                         if(unlikely(netdata_exit)) break;
524
525                         buffer[TC_LINE_MAX] = '\0';
526                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
527
528                         tc_split_words(buffer, words, MAX_WORDS);
529                         if(!words[0] || !*words[0]) {
530                                 // debug(D_TC_LOOP, "empty line");
531                                 continue;
532                         }
533                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
534
535                         first_hash = simple_hash(words[0]);
536
537                         if(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) {
538                                 // 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]);
539
540                                 // clear the last class
541                                 class = NULL;
542
543                                 // words[1] : class type
544                                 // words[2] : N:XX
545                                 // words[3] : parent or root
546                                 if(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
547                                         //char *type     = words[1];  // the class: htb, fq_codel, etc
548
549                                         // we are only interested for HTB classes
550                                         //if(strcmp(type, "htb") != 0) continue;
551
552                                         char *id       = words[2];      // the class major:minor
553                                         char *parent   = words[3];      // 'parent' or 'root'
554                                         char *parentid = words[4];      // the parent's id
555                                         char *leaf     = words[5];      // 'leaf'
556                                         char *leafid   = words[6];      // leafid
557
558                                         if(strcmp(parent, "root") == 0) {
559                                                 parentid = NULL;
560                                                 leafid = NULL;
561                                         }
562                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
563                                                 leafid = NULL;
564
565                                         char leafbuf[20 + 1] = "";
566                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
567                                                 strncpyz(leafbuf, leafid, 20 - 1);
568                                                 strcat(leafbuf, "1");
569                                                 leafid = leafbuf;
570                                         }
571
572                                         class = tc_class_add(device, id, parentid, leafid);
573                                 }
574                         }
575                         else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
576                                 // debug(D_TC_LOOP, "END line");
577
578                                 if(device) {
579                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
580                                                 error("Cannot set pthread cancel state to DISABLE.");
581
582                                         tc_device_commit(device);
583                                         // tc_device_free(device);
584                                         device = NULL;
585                                         class = NULL;
586
587                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
588                                                 error("Cannot set pthread cancel state to ENABLE.");
589                                 }
590                         }
591                         else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
592                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
593
594                                 if(device) {
595                                         // tc_device_free(device);
596                                         device = NULL;
597                                         class = NULL;
598                                 }
599
600                                 if(words[1] && *words[1]) {
601                                         device = tc_device_create(words[1]);
602                                         class = NULL;
603                                 }
604                         }
605                         else if(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0) {
606                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
607                                 if(words[1] && *words[1]) {
608                                         class->bytes = strtoull(words[1], NULL, 10);
609                                         class->updated = 1;
610                                 }
611
612                                 if(words[3] && *words[3])
613                                         class->packets = strtoull(words[3], NULL, 10);
614
615                                 if(words[6] && *words[6])
616                                         class->dropped = strtoull(words[6], NULL, 10);
617
618                                 if(words[8] && *words[8])
619                                         class->overlimits = strtoull(words[8], NULL, 10);
620
621                                 if(words[10] && *words[10])
622                                         class->requeues = strtoull(words[8], NULL, 10);
623                         }
624                         else if(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0) {
625                                 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
626                                 if(words[1] && *words[1])
627                                         class->lended = strtoull(words[1], NULL, 10);
628
629                                 if(words[3] && *words[3])
630                                         class->borrowed = strtoull(words[3], NULL, 10);
631
632                                 if(words[5] && *words[5])
633                                         class->giants = strtoull(words[5], NULL, 10);
634                         }
635                         else if(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0) {
636                                 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
637                                 if(words[1] && *words[1])
638                                         class->tokens = strtoull(words[1], NULL, 10);
639
640                                 if(words[3] && *words[3])
641                                         class->ctokens = strtoull(words[3], NULL, 10);
642                         }
643                         else if(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0) {
644                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
645                                 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
646                         }
647                         else if(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0) {
648                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
649                                 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
650                         }
651                         else if(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0) {
652                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
653                                 char *id    = words[1];
654                                 char *path  = words[2];
655                                 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
656                         }
657                         else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
658                                 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
659                                 getrusage(RUSAGE_THREAD, &thread);
660
661                                 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
662                                 if(!stcpu) {
663                                         stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
664                                         rrddim_add(stcpu, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
665                                         rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
666                                 }
667                                 else rrdset_next(stcpu);
668
669                                 rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
670                                 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
671                                 rrdset_done(stcpu);
672
673                                 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
674                                 if(!sttime) {
675                                         sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
676                                         rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
677                                 }
678                                 else rrdset_next(sttime);
679
680                                 rrddim_set(sttime, "run_time", atoll(words[1]));
681                                 rrdset_done(sttime);
682
683                         }
684 #ifdef DETACH_PLUGINS_FROM_NETDATA
685                         else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
686                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
687                                 char *id = words[1];
688                                 pid_t pid = atol(id);
689
690                                 if(pid) tc_child_pid = pid;
691
692                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
693                         }
694 #endif
695                         //else {
696                         //      debug(D_TC_LOOP, "IGNORED line");
697                         //}
698                 }
699                 // fgets() failed or loop broke
700                 int code = mypclose(fp, tc_child_pid);
701                 tc_child_pid = 0;
702
703                 if(device) {
704                         // tc_device_free(device);
705                         device = NULL;
706                         class = NULL;
707                 }
708
709                 if(netdata_exit) {
710                         tc_device_free_all();
711                         pthread_exit(NULL);
712                         return NULL;
713                 }
714
715                 if(code == 1 || code == 127) {
716                         // 1 = DISABLE
717                         // 127 = cannot even run it
718                         error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
719
720                         tc_device_free_all();
721                         pthread_exit(NULL);
722                         return NULL;
723                 }
724
725                 sleep((unsigned int) rrd_update_every);
726         }
727
728         pthread_exit(NULL);
729         return NULL;
730 }