]> arthur.barton.de Git - netdata.git/commitdiff
config now uses less memory by allocating only the memory it needs for storing the...
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 28 Mar 2015 03:43:34 +0000 (05:43 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 28 Mar 2015 03:43:34 +0000 (05:43 +0200)
src/config.c
src/config.h
src/log.c
src/plugins.d/apps_plugin.c
src/rrd.h

index c55e7aed206d524d98859608302bd28062b04df2..3c8e041ff23093826ee4db5cfb24d5075b0d32fd 100755 (executable)
@@ -8,7 +8,7 @@
 #include "config.h"
 #include "log.h"
 
-#define CONFIG_FILE_LINE_MAX 4096
+#define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
 
 pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
 
@@ -26,8 +26,8 @@ struct config_value {
        unsigned long hash;             // a simple hash to speed up searching
                                                        // we first compare hashes, and only if the hashes are equal we do string comparisons
 
-       char name[CONFIG_MAX_NAME + 1];
-       char value[CONFIG_MAX_VALUE + 1];
+       char *name;
+       char *value;
 
        uint8_t flags;
 
@@ -40,7 +40,7 @@ struct config {
        unsigned long hash;             // a simple hash to speed up searching
                                                        // we first compare hashes, and only if the hashes are equal we do string comparisons
 
-       char name[CONFIG_MAX_NAME + 1];
+       char *name;
 
        struct config_value *values;
        avl_tree values_index;
@@ -66,8 +66,7 @@ static int config_value_compare(void* a, void* b) {
 static struct config_value *config_value_index_find(struct config *co, const char *name, unsigned long hash) {
        struct config_value *result = NULL, tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
-       strncpy(tmp.name, name, CONFIG_MAX_NAME);
-       tmp.name[CONFIG_MAX_NAME] = '\0';
+       tmp.name = (char *)name;
 
        avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
        return result;
@@ -95,8 +94,7 @@ avl_tree config_root_index = {
 static struct config *config_index_find(const char *name, unsigned long hash) {
        struct config *result = NULL, tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
-       strncpy(tmp.name, name, CONFIG_MAX_NAME);
-       tmp.name[CONFIG_MAX_NAME] = '\0';
+       tmp.name = (char *)name;
 
        avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
        return result;
@@ -109,10 +107,13 @@ struct config_value *config_value_create(struct config *co, const char *name, co
        struct config_value *cv = calloc(1, sizeof(struct config_value));
        if(!cv) fatal("Cannot allocate config_value");
 
-       strncpy(cv->name,  name,  CONFIG_MAX_NAME);
-       strncpy(cv->value, value, CONFIG_MAX_VALUE);
+       cv->name = strdup(name);
+       if(!cv->name) fatal("Cannot allocate config.name");
        cv->hash = simple_hash(cv->name);
 
+       cv->value = strdup(value);
+       if(!cv->value) fatal("Cannot allocate config.value");
+
        config_value_index_add(co, cv);
 
        // no need for string termination, due to calloc()
@@ -134,8 +135,10 @@ struct config *config_create(const char *section)
        struct config *co = calloc(1, sizeof(struct config));
        if(!co) fatal("Cannot allocate config");
 
-       strncpy(co->name, section, CONFIG_MAX_NAME);
+       co->name = strdup(section);
+       if(!co->name) fatal("Cannot allocate config.name");
        co->hash = simple_hash(co->name);
+
        co->values_index.compar = config_value_compare;
 
        config_index_add(co);
@@ -229,8 +232,9 @@ int load_config(char *filename, int overwrite_used)
                else {
                        if((cv->flags & CONFIG_VALUE_USED && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
                                debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
-                               strncpy(cv->value, value, CONFIG_MAX_VALUE);
-                               // termination is already there
+                               free(cv->value);
+                               cv->value = strdup(value);
+                               if(!cv->value) fatal("Cannot allocate config.value");
                        }
                        else
                                debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
@@ -295,14 +299,8 @@ int config_get_boolean(const char *section, const char *name, int value)
        s = config_get(section, name, s);
        if(!s) return 0;
 
-       if(strcmp(s, "yes") == 0 || strcmp(s, "true") == 0 || strcmp(s, "1") == 0) {
-               strcpy(s, "yes");
-               return 1;
-       }
-       else {
-               strcpy(s, "no");
-               return 0;
-       }
+       if(!strcmp(s, "yes")) return 1;
+       else return 0;
 }
 
 const char *config_set(const char *section, const char *name, const char *value)
@@ -322,8 +320,9 @@ const char *config_set(const char *section, const char *name, const char *value)
 
        if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
 
-       strncpy(cv->value, value, CONFIG_MAX_VALUE);
-       // termination is already there
+       free(cv->value);
+       cv->value = strdup(value);
+       if(!cv->value) fatal("Cannot allocate config.value");
 
        pthread_rwlock_unlock(&config_rwlock);
 
@@ -400,16 +399,16 @@ void generate_config(struct web_buffer *wb, int only_changed)
                                        web_buffer_printf(wb, "\n# node '%s' is not used.", co->name);
                                }
 
-                               web_buffer_increase(wb, CONFIG_MAX_NAME + 4);
+                               web_buffer_increase(wb, CONFIG_FILE_LINE_MAX+1);
                                web_buffer_printf(wb, "\n[%s]\n", co->name);
 
                                for(cv = co->values; cv ; cv = cv->next) {
 
                                        if(used && !(cv->flags & CONFIG_VALUE_USED)) {
-                                               web_buffer_increase(wb, CONFIG_MAX_NAME + 200);
+                                               web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
                                                web_buffer_printf(wb, "\n\t# option '%s' is not used.\n", cv->name);
                                        }
-                                       web_buffer_increase(wb, CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 5);
+                                       web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
                                        web_buffer_printf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
                                }
                        }
index 218655eb6cec4a0863b63ceefaceea26c80287ff..79c22388e0c787beb9f0ed7df57c2c9e1eecd0ef 100755 (executable)
@@ -3,10 +3,13 @@
 #ifndef NETDATA_CONFIG_H
 #define NETDATA_CONFIG_H 1
 
-#define CONFIG_MAX_NAME 100
-#define CONFIG_MAX_VALUE 1024
 #define CONFIG_FILENAME "netdata.conf"
 
+// these are used to limit the configuration names and values lengths
+// they are not enforced by config.c functions (they will strdup() all strings, no matter of their length)
+#define CONFIG_MAX_NAME 1024
+#define CONFIG_MAX_VALUE 2048
+
 extern int load_config(char *filename, int overwrite_used);
 
 extern char *config_get(const char *section, const char *name, const char *default_value);
index 0ba8a228de423335893489269b0586873ad20423..a2a7eb0e2e0af5f4ef116f1608196adce4818d8a 100755 (executable)
--- a/src/log.c
+++ b/src/log.c
@@ -39,12 +39,11 @@ void log_date(FILE *out)
 
 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
 {
-       if(file) { ; }
        va_list args;
 
        log_date(stdout);
        va_start( args, fmt );
-       fprintf(stdout, "DEBUG (%04lu@%-15.15s): ", line, function);
+       fprintf(stdout, "DEBUG (%04lu@%-10.10s:%-15.15s): ", line, file, function);
        vfprintf( stdout, fmt, args );
        va_end( args );
        fprintf(stdout, "\n");
@@ -58,13 +57,12 @@ void debug_int( const char *file, const char *function, const unsigned long line
 
 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
 {
-       if(file) { ; }
        va_list args;
 
        log_date(stderr);
 
        va_start( args, fmt );
-       if(debug_flags) fprintf(stderr, "INFO (%04lu@%-15.15s): ", line, function);
+       if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): ", line, file, function);
        else            fprintf(stderr, "INFO: ");
        vfprintf( stderr, fmt, args );
        va_end( args );
@@ -80,13 +78,12 @@ void info_int( const char *file, const char *function, const unsigned long line,
 
 void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
 {
-       if(file) { ; }
        va_list args;
 
        log_date(stderr);
 
        va_start( args, fmt );
-       if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-15.15s): ", line, function);
+       if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-10.10s:%-15.15s): ", line, file, function);
        else            fprintf(stderr, "ERROR: ");
        vfprintf( stderr, fmt, args );
        va_end( args );
@@ -106,13 +103,12 @@ void error_int( const char *file, const char *function, const unsigned long line
 
 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
 {
-       if(file) { ; }
        va_list args;
 
        log_date(stderr);
 
        va_start( args, fmt );
-       if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-15.15s): ", line, function);
+       if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): ", line, file, function);
        else            fprintf(stderr, "FATAL: ");
        vfprintf( stderr, fmt, args );
        va_end( args );
index f8835dbf97fba327f4f449c28cdbdb6392085a3a..053ad35c6dcd2d2a9dd434a84fb2d6f6b05d5808 100755 (executable)
@@ -50,7 +50,7 @@ unsigned long long file_counter = 0;
 long get_processors(void) {
        int processors = 0;
 
-       procfile *ff = procfile_open("/proc/stat", O_RDONLY);
+       procfile *ff = procfile_open("/proc/stat", "");
        if(!ff) return 1;
 
        ff = procfile_readall(ff);
@@ -75,7 +75,7 @@ long get_processors(void) {
 long get_pid_max(void) {
        long mpid = 32768;
 
-       procfile *ff = procfile_open("/proc/sys/kernel/pid_max", O_RDONLY);
+       procfile *ff = procfile_open("/proc/sys/kernel/pid_max", "");
        if(!ff) return mpid;
 
        ff = procfile_readall(ff);
index a3e0177229ada50f2d205c2113ef082afd4a3672..5a02a77b32c7f412b5f166c10363103490428b1c 100755 (executable)
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -112,7 +112,7 @@ struct rrddim {
 
        struct timeval last_collected_time;                             // when was this dimension last updated
                                                                                                        // this is actual date time we updated the last_collected_value
-                                                                                                       // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRD_STATS
+                                                                                                       // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
 
        calculated_number calculated_value;                             // the current calculated value, after applying the algorithm
        calculated_number last_calculated_value;                // the last calculated value