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