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