X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Fplugins_d.c;h=71d1fb2defefa5ea6d3ccbdba744365ff8e56839;hb=6372ed837298a34f6d16e0cde929822879863195;hp=2fe5c56baf6bed014f96e0d2a8048f5a866ad9b1;hpb=ccb396d17f169e1bf57bcb50af89701eccad0e56;p=netdata.git diff --git a/src/plugins_d.c b/src/plugins_d.c index 2fe5c56b..71d1fb2d 100755 --- a/src/plugins_d.c +++ b/src/plugins_d.c @@ -1,3 +1,6 @@ +#ifdef HAVE_CONFIG_H +#include +#endif #include #include #include @@ -6,8 +9,9 @@ #include #include +#include "main.h" #include "common.h" -#include "config.h" +#include "appconfig.h" #include "log.h" #include "rrd.h" #include "popen.h" @@ -36,10 +40,10 @@ static void pluginsd_split_words(char *str, char **words, int max_words) { int i = 0; // skip all white space - while(pluginsd_space(*s)) s++; + while(unlikely(pluginsd_space(*s))) s++; // check for quote - if(*s == '\'' || *s == '"') { + if(unlikely(*s == '\'' || *s == '"')) { quote = *s; // remember the quote s++; // skip the quote } @@ -48,40 +52,40 @@ static void pluginsd_split_words(char *str, char **words, int max_words) { words[i++] = s; // while we have something - while(*s) { + while(likely(*s)) { // if it is escape - if(*s == '\\' && s[1]) { + if(unlikely(*s == '\\' && s[1])) { s += 2; continue; } // if it is quote - else if(*s == quote) { + else if(unlikely(*s == quote)) { quote = 0; *s = ' '; continue; } // if it is a space - else if(quote == 0 && pluginsd_space(*s)) { + else if(unlikely(quote == 0 && pluginsd_space(*s))) { // terminate the word *s++ = '\0'; // skip all white space - while(pluginsd_space(*s)) s++; + while(likely(pluginsd_space(*s))) s++; // check for quote - if(*s == '\'' || *s == '"') { + if(unlikely(*s == '\'' || *s == '"')) { quote = *s; // remember the quote s++; // skip the quote } // if we reached the end, stop - if(!*s) break; + if(unlikely(!*s)) break; // store the next word - if(i < max_words) words[i++] = s; + if(likely(i < max_words)) words[i++] = s; else break; } @@ -90,7 +94,7 @@ static void pluginsd_split_words(char *str, char **words, int max_words) { } // terminate the words - while(i < max_words) words[i++] = NULL; + while(likely(i < max_words)) words[i++] = NULL; } @@ -117,26 +121,28 @@ void *pluginsd_worker_thread(void *arg) uint32_t STOPPING_WAKE_ME_UP_PLEASE_HASH = simple_hash("STOPPING_WAKE_ME_UP_PLEASE"); #endif - while(1) { + while(likely(1)) { FILE *fp = mypopen(cd->cmd, &cd->pid); - if(!fp) { + if(unlikely(!fp)) { error("Cannot popen(\"%s\", \"r\").", cd->cmd); break; } + info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid); + RRDSET *st = NULL; unsigned long long count = 0; char *s; uint32_t hash; - while(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL) { + while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) { line[PLUGINSD_LINE_MAX] = '\0'; // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line); pluginsd_split_words(line, words, MAX_WORDS); s = words[0]; - if(!s || !*s) { + if(unlikely(!s || !*s)) { // debug(D_PLUGINSD, "PLUGINSD: empty line"); continue; } @@ -145,87 +151,82 @@ void *pluginsd_worker_thread(void *arg) hash = simple_hash(s); - if(hash == SET_HASH && !strcmp(s, "SET")) { + if(likely(hash == SET_HASH && !strcmp(s, "SET"))) { char *dimension = words[1]; char *value = words[2]; - if(!dimension || !*dimension) { + if(unlikely(!dimension || !*dimension)) { error("PLUGINSD: '%s' is requesting a SET on chart '%s', like this: 'SET %s = %s'. Disabling it.", cd->fullfilename, st->id, dimension?dimension:"", value?value:""); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } - if(!value || !*value) value = "0"; + if(unlikely(!value || !*value)) value = "0"; - if(!st) { + if(unlikely(!st)) { error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } - if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value); + if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value); rrddim_set(st, dimension, atoll(value)); count++; } - else if(hash == BEGIN_HASH && !strcmp(s, "BEGIN")) { + else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) { char *id = words[1]; char *microseconds_txt = words[2]; - if(!id) { + if(unlikely(!id)) { error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } st = rrdset_find(id); - if(!st) { + if(unlikely(!st)) { error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } - if(st->counter_done) { + if(likely(st->counter_done)) { unsigned long long microseconds = 0; if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10); if(microseconds) rrdset_next_usec(st, microseconds); else rrdset_next_plugins(st); } } - else if(hash == END_HASH && !strcmp(s, "END")) { - if(!st) { + else if(likely(hash == END_HASH && !strcmp(s, "END"))) { + if(unlikely(!st)) { error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } - if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id); + if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id); rrdset_done(st); st = NULL; } - else if(hash == FLUSH_HASH && !strcmp(s, "FLUSH")) { + else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) { debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename); st = NULL; } - else if(hash == CHART_HASH && !strcmp(s, "CHART")) { + else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) { st = NULL; char *type = words[1]; char *id = NULL; - if(type) { + if(likely(type)) { id = strchr(type, '.'); - if(id) { *id = '\0'; id++; } + if(likely(id)) { *id = '\0'; id++; } } char *name = words[2]; char *title = words[3]; @@ -236,30 +237,29 @@ void *pluginsd_worker_thread(void *arg) char *priority_s = words[8]; char *update_every_s = words[9]; - if(!type || !*type || !id || !*id) { + if(unlikely(!type || !*type || !id || !*id)) { error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } int priority = 1000; - if(priority_s) priority = atoi(priority_s); + if(likely(priority_s)) priority = atoi(priority_s); int update_every = cd->update_every; - if(update_every_s) update_every = atoi(update_every_s); - if(!update_every) update_every = cd->update_every; + if(likely(update_every_s)) update_every = atoi(update_every_s); + if(unlikely(!update_every)) update_every = cd->update_every; int chart_type = RRDSET_TYPE_LINE; - if(chart) chart_type = rrdset_type_id(chart); + if(unlikely(chart)) chart_type = rrdset_type_id(chart); - if(!name || !*name) name = NULL; - if(!family || !*family) family = id; - if(!category || !*category) category = type; + if(unlikely(!name || !*name)) name = NULL; + if(unlikely(!family || !*family)) family = id; + if(unlikely(!category || !*category)) category = type; st = rrdset_find_bytype(type, id); - if(!st) { + if(unlikely(!st)) { debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d" , type, id , name?name:"" @@ -273,11 +273,11 @@ void *pluginsd_worker_thread(void *arg) st = rrdset_create(type, id, name, family, title, units, priority, update_every, chart_type); cd->update_every = update_every; - if(strcmp(category, "none") == 0) st->isdetail = 1; + if(unlikely(strcmp(category, "none") == 0)) st->isdetail = 1; } else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id); } - else if(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION")) { + else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) { char *id = words[1]; char *name = words[2]; char *algorithm = words[3]; @@ -285,33 +285,31 @@ void *pluginsd_worker_thread(void *arg) char *divisor_s = words[5]; char *hidden = words[6]; - if(!id || !*id) { + if(unlikely(!id || !*id)) { error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } - if(!st) { + if(unlikely(!st)) { error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } long multiplier = 1; if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s); - if(!multiplier) multiplier = 1; + if(unlikely(!multiplier)) multiplier = 1; long divisor = 1; - if(divisor_s && *divisor_s) divisor = atol(divisor_s); - if(!divisor) divisor = 1; + if(likely(divisor_s && *divisor_s)) divisor = atol(divisor_s); + if(unlikely(!divisor)) divisor = 1; - if(!algorithm || !*algorithm) algorithm = "absolute"; + if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute"; - if(st->debug) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'" + if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'" , st->id , id , name?name:"" @@ -322,32 +320,31 @@ void *pluginsd_worker_thread(void *arg) ); RRDDIM *rd = rrddim_find(st, id); - if(!rd) { + if(unlikely(!rd)) { rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm)); - if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1; + if(unlikely(hidden && strcmp(hidden, "hidden") == 0)) rd->hidden = 1; } - else if(st->debug) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id); + else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id); } - else if(hash == DISABLE_HASH && !strcmp(s, "DISABLE")) { + else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) { error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } #ifdef DETACH_PLUGINS_FROM_NETDATA - else if(hash == MYPID_HASH && !strcmp(s, "MYPID")) { + else if(likely(hash == MYPID_HASH && !strcmp(s, "MYPID"))) { char *pid_s = words[1]; pid_t pid = atol(pid_s); - if(pid) cd->pid = pid; + if(likely(pid)) cd->pid = pid; debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid); } - else if(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE")) { + else if(likely(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE"))) { error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid); gettimeofday(&now, NULL); - if(!usec && !susec) { + if(unlikely(!usec && !susec)) { // our first run susec = cd->rrd_update_every * 1000000ULL; } @@ -355,14 +352,13 @@ void *pluginsd_worker_thread(void *arg) // second+ run usec = usecdiff(&now, &last) - susec; error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec); - if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec; + if(unlikely(usec < (rrd_update_every * 1000000ULL / 2ULL))) susec = (rrd_update_every * 1000000ULL) - usec; else susec = rrd_update_every * 1000000ULL / 2ULL; } error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid); usleep(susec); - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGCONT); + killpid(cd->pid, SIGCONT); bcopy(&now, &last, sizeof(struct timeval)); break; } @@ -370,24 +366,24 @@ void *pluginsd_worker_thread(void *arg) else { error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s); cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + killpid(cd->pid, SIGTERM); break; } } + info("PLUGINSD: '%s' on pid %d stopped.", cd->fullfilename, cd->pid); + // fgets() failed or loop broke mypclose(fp, cd->pid); - cd->pid = 0; - if(!count && cd->enabled) { - error("PLUGINSD: '%s' does not generate usefull output. Disabling it.", cd->fullfilename); - cd->enabled = 0; - // test: killing an exited child kills us - lets check it exists before killing it - if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM); + if(unlikely(!count && cd->enabled)) { + error("PLUGINSD: '%s' (pid %d) does not generate usefull output. Waiting a bit before starting it again.", cd->fullfilename, cd->pid); + sleep(cd->update_every * 10); } - if(cd->enabled) sleep(cd->update_every); + cd->pid = 0; + + if(likely(cd->enabled)) sleep(cd->update_every); else break; } @@ -399,6 +395,8 @@ void *pluginsd_main(void *ptr) { if(ptr) { ; } + info("PLUGINS.D thread created with task id %d", gettid()); + if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0) error("Cannot set pthread cancel type to DEFERRED."); @@ -417,21 +415,21 @@ void *pluginsd_main(void *ptr) if(scan_frequency < 1) scan_frequency = 1; - while(1) { + while(likely(1)) { dir = opendir(dir_name); - if(!dir) { + if(unlikely(!dir)) { error("Cannot open directory '%s'.", dir_name); return NULL; } - while((file = readdir(dir))) { + while(likely((file = readdir(dir)))) { debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name); - if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) continue; + if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue; int len = strlen(file->d_name); - if(len <= (int)PLUGINSD_FILE_SUFFIX_LEN) continue; - if(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0) { + if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue; + if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) { debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX); continue; } @@ -440,25 +438,25 @@ void *pluginsd_main(void *ptr) snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name); int enabled = config_get_boolean("plugins", pluginname, automatic_run); - if(!enabled) { + if(unlikely(!enabled)) { debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name); continue; } // check if it runs already - for(cd = pluginsd_root ; cd ; cd = cd->next) { - if(strcmp(cd->filename, file->d_name) == 0) break; + for(cd = pluginsd_root ; likely(cd) ; cd = cd->next) { + if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break; } - if(cd && !cd->obsolete) { + if(likely(cd && !cd->obsolete)) { debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename); continue; } // it is not running // allocate a new one, or use the obsolete one - if(!cd) { + if(unlikely(!cd)) { cd = calloc(sizeof(struct plugind), 1); - if(!cd) fatal("Cannot allocate memory for plugin."); + if(unlikely(!cd)) fatal("Cannot allocate memory for plugin."); snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname); @@ -473,19 +471,19 @@ void *pluginsd_main(void *ptr) snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def)); // link it - if(pluginsd_root) cd->next = pluginsd_root; + if(likely(pluginsd_root)) cd->next = pluginsd_root; pluginsd_root = cd; } cd->obsolete = 0; - if(!cd->enabled) continue; + if(unlikely(!cd->enabled)) continue; // spawn a new thread for it - if(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0) { + if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0)) { error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename); cd->obsolete = 1; } - else if(pthread_detach(cd->thread) != 0) + else if(unlikely(pthread_detach(cd->thread) != 0)) error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename); }