]> arthur.barton.de Git - netdata.git/blobdiff - src/plugins_d.c
lower the memory footprint of dimensions by 1.5KB
[netdata.git] / src / plugins_d.c
index 8036b632dc8fbf1ea00e390122bca6447e0fa397..4ddcc20d036adcb3262406298dad2e8508e83ed7 100644 (file)
@@ -87,6 +87,8 @@ static int pluginsd_split_words(char *str, char **words, int max_words) {
 void *pluginsd_worker_thread(void *arg)
 {
     struct plugind *cd = (struct plugind *)arg;
+    cd->obsolete = 0;
+
     char line[PLUGINSD_LINE_MAX + 1];
 
 #ifdef DETACH_PLUGINS_FROM_NETDATA
@@ -95,7 +97,6 @@ void *pluginsd_worker_thread(void *arg)
 #endif
 
     char *words[MAX_WORDS] = { NULL };
-    uint32_t SET_HASH = simple_hash("SET");
     uint32_t BEGIN_HASH = simple_hash("BEGIN");
     uint32_t END_HASH = simple_hash("END");
     uint32_t FLUSH_HASH = simple_hash("FLUSH");
@@ -139,9 +140,7 @@ void *pluginsd_worker_thread(void *arg)
 
             // debug(D_PLUGINSD, "PLUGINSD: words 0='%s' 1='%s' 2='%s' 3='%s' 4='%s' 5='%s' 6='%s' 7='%s' 8='%s' 9='%s'", words[0], words[1], words[2], words[3], words[4], words[5], words[6], words[7], words[8], words[9]);
 
-            hash = simple_hash(s);
-
-            if(likely(hash == SET_HASH && !strcmp(s, "SET"))) {
+            if(likely(!simple_hash_strcmp(s, "SET", &hash))) {
                 char *dimension = words[1];
                 char *value = words[2];
 
@@ -186,7 +185,7 @@ void *pluginsd_worker_thread(void *arg)
 
                 if(likely(st->counter_done)) {
                     usec_t microseconds = 0;
-                    if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
+                    if(microseconds_txt && *microseconds_txt) microseconds = str2ull(microseconds_txt);
                     if(microseconds) rrdset_next_usec(st, microseconds);
                     else rrdset_next(st);
                 }
@@ -240,10 +239,10 @@ void *pluginsd_worker_thread(void *arg)
                 }
 
                 int priority = 1000;
-                if(likely(priority_s)) priority = atoi(priority_s);
+                if(likely(priority_s)) priority = str2i(priority_s);
 
                 int update_every = cd->update_every;
-                if(likely(update_every_s)) update_every = atoi(update_every_s);
+                if(likely(update_every_s)) update_every = str2i(update_every_s);
                 if(unlikely(!update_every)) update_every = cd->update_every;
 
                 int chart_type = RRDSET_TYPE_LINE;
@@ -317,15 +316,15 @@ void *pluginsd_worker_thread(void *arg)
                     rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
                     rd->flags = 0x00000000;
                     if(options && *options) {
-                        if(strstr(options, "hidden") != NULL) rd->flags |= RRDDIM_FLAG_HIDDEN;
-                        if(strstr(options, "noreset") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
-                        if(strstr(options, "nooverflow") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
+                        if(strstr(options, "hidden") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
+                        if(strstr(options, "noreset") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
+                        if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
                     }
                 }
                 else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
             }
             else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
-                error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
+                info("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
                 cd->enabled = 0;
                 killpid(cd->pid, SIGTERM);
                 break;
@@ -433,7 +432,7 @@ void *pluginsd_worker_thread(void *arg)
 }
 
 void *pluginsd_main(void *ptr) {
-    (void)ptr;
+    struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
 
     info("PLUGINS.D thread created with task id %d", gettid());
 
@@ -443,7 +442,6 @@ void *pluginsd_main(void *ptr) {
     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
         error("Cannot set pthread cancel state to ENABLE.");
 
-    char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
     int automatic_run = config_get_boolean("plugins", "enable running new plugins", 1);
     int scan_frequency = (int) config_get_number("plugins", "check for new plugins every", 60);
     DIR *dir = NULL;
@@ -458,11 +456,10 @@ void *pluginsd_main(void *ptr) {
     for(;;) {
         if(unlikely(netdata_exit)) break;
 
-        dir = opendir(dir_name);
+        dir = opendir(netdata_configured_plugins_dir);
         if(unlikely(!dir)) {
-            error("Cannot open directory '%s'.", dir_name);
-            pthread_exit(NULL);
-            return NULL;
+            error("Cannot open directory '%s'.", netdata_configured_plugins_dir);
+            goto cleanup;
         }
 
         while(likely((file = readdir(dir)))) {
@@ -489,9 +486,9 @@ void *pluginsd_main(void *ptr) {
             }
 
             // check if it runs already
-            for(cd = pluginsd_root ; likely(cd) ; cd = cd->next) {
+            for(cd = pluginsd_root ; cd ; cd = cd->next)
                 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
-            }
+
             if(likely(cd && !cd->obsolete)) {
                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
                 continue;
@@ -505,7 +502,7 @@ void *pluginsd_main(void *ptr) {
                 snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
 
                 strncpyz(cd->filename, file->d_name, FILENAME_MAX);
-                snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
+                snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", netdata_configured_plugins_dir, cd->filename);
 
                 cd->enabled = enabled;
                 cd->update_every = (int) config_get_number(cd->id, "update every", rrd_update_every);
@@ -517,26 +514,29 @@ void *pluginsd_main(void *ptr) {
                 // link it
                 if(likely(pluginsd_root)) cd->next = pluginsd_root;
                 pluginsd_root = cd;
-            }
-            cd->obsolete = 0;
 
-            if(unlikely(!cd->enabled)) continue;
-
-            // spawn a new thread for it
-            if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0)) {
-                error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
+                // it is not currently running
                 cd->obsolete = 1;
+
+                if(cd->enabled) {
+                    // spawn a new thread for it
+                    if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0))
+                        error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
+
+                    else if(unlikely(pthread_detach(cd->thread) != 0))
+                        error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
+                }
             }
-            else if(unlikely(pthread_detach(cd->thread) != 0))
-                error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
         }
 
         closedir(dir);
         sleep((unsigned int) scan_frequency);
     }
 
+cleanup:
     info("PLUGINS.D thread exiting");
 
+    static_thread->enabled = 0;
     pthread_exit(NULL);
     return NULL;
 }