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