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