]> arthur.barton.de Git - netdata.git/blobdiff - src/plugins_d.c
code optimizations; added temperature charts.d plugin
[netdata.git] / src / plugins_d.c
index 11f26edd259407cdd72c761fd0c020ec502cc3a1..a8baf6c73936848b6ad428469920351ef070f115 100755 (executable)
@@ -37,10 +37,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
        }
@@ -49,40 +49,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;
                }
 
@@ -91,7 +91,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;
 }
 
 
@@ -118,9 +118,9 @@ 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;
                }
@@ -132,14 +132,14 @@ void *pluginsd_worker_thread(void *arg)
                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;
                        }
@@ -148,36 +148,36 @@ 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:"<nothing>", value?value:"<nothing>");
                                        cd->enabled = 0;
                                        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;
                                        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;
                                        killpid(cd->pid, SIGTERM);
@@ -185,45 +185,45 @@ void *pluginsd_worker_thread(void *arg)
                                }
 
                                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;
                                        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;
                                        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];
@@ -234,7 +234,7 @@ 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;
                                        killpid(cd->pid, SIGTERM);
@@ -242,21 +242,21 @@ void *pluginsd_worker_thread(void *arg)
                                }
 
                                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:""
@@ -270,11 +270,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];
@@ -282,14 +282,14 @@ 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;
                                        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;
                                        killpid(cd->pid, SIGTERM);
@@ -298,15 +298,15 @@ void *pluginsd_worker_thread(void *arg)
 
                                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:""
@@ -317,31 +317,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;
                                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;
                                }
@@ -349,7 +349,7 @@ 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;
                                }
 
@@ -373,14 +373,14 @@ void *pluginsd_worker_thread(void *arg)
                // fgets() failed or loop broke
                mypclose(fp, cd->pid);
 
-               if(!count && cd->enabled) {
+               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);
                }
 
                cd->pid = 0;
 
-               if(cd->enabled) sleep(cd->update_every);
+               if(likely(cd->enabled)) sleep(cd->update_every);
                else break;
        }
 
@@ -410,21 +410,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;
                        }
@@ -433,25 +433,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);
                                
@@ -466,19 +466,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);
                }