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