@cryptotaxi247 / netdata-1 / commits / 71a743b41

config now uses less memory by allocating only the memory it needs for storing the strings; fixed a bug in apps_plugin

Costa Tsaousis (ktsaou) committed Mar 28, 2015 at 05:43 UTC 71a743b419f6363fd0f3176e2d606cd9b8300815
5 files changed +37 -39
src/config.c
+25 -26
@@ -8,7 +8,7 @@
8 #include "config.h"
9 #include "log.h"
10
11 -#define CONFIG_FILE_LINE_MAX 4096
11 +#define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
12
13 pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
14
@@ -26,8 +26,8 @@ struct config_value {
26 unsigned long hash; // a simple hash to speed up searching
27 // we first compare hashes, and only if the hashes are equal we do string comparisons
28
29 - char name[CONFIG_MAX_NAME + 1];
30 - char value[CONFIG_MAX_VALUE + 1];
29 + char *name;
30 + char *value;
31
32 uint8_t flags;
33
@@ -40,7 +40,7 @@ struct config {
40 unsigned long hash; // a simple hash to speed up searching
41 // we first compare hashes, and only if the hashes are equal we do string comparisons
42
43 - char name[CONFIG_MAX_NAME + 1];
43 + char *name;
44
45 struct config_value *values;
46 avl_tree values_index;
@@ -66,8 +66,7 @@ static int config_value_compare(void* a, void* b) {
66 static struct config_value *config_value_index_find(struct config *co, const char *name, unsigned long hash) {
67 struct config_value *result = NULL, tmp;
68 tmp.hash = (hash)?hash:simple_hash(name);
69 - strncpy(tmp.name, name, CONFIG_MAX_NAME);
70 - tmp.name[CONFIG_MAX_NAME] = '\0';
69 + tmp.name = (char *)name;
70
71 avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
72 return result;
@@ -95,8 +94,7 @@ avl_tree config_root_index = {
94 static struct config *config_index_find(const char *name, unsigned long hash) {
95 struct config *result = NULL, tmp;
96 tmp.hash = (hash)?hash:simple_hash(name);
98 - strncpy(tmp.name, name, CONFIG_MAX_NAME);
99 - tmp.name[CONFIG_MAX_NAME] = '\0';
97 + tmp.name = (char *)name;
98
99 avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
100 return result;
@@ -109,10 +107,13 @@ struct config_value *config_value_create(struct config *co, const char *name, co
107 struct config_value *cv = calloc(1, sizeof(struct config_value));
108 if(!cv) fatal("Cannot allocate config_value");
109
112 - strncpy(cv->name, name, CONFIG_MAX_NAME);
113 - strncpy(cv->value, value, CONFIG_MAX_VALUE);
110 + cv->name = strdup(name);
111 + if(!cv->name) fatal("Cannot allocate config.name");
112 cv->hash = simple_hash(cv->name);
113
114 + cv->value = strdup(value);
115 + if(!cv->value) fatal("Cannot allocate config.value");
116 +
117 config_value_index_add(co, cv);
118
119 // no need for string termination, due to calloc()
@@ -134,8 +135,10 @@ struct config *config_create(const char *section)
135 struct config *co = calloc(1, sizeof(struct config));
136 if(!co) fatal("Cannot allocate config");
137
137 - strncpy(co->name, section, CONFIG_MAX_NAME);
138 + co->name = strdup(section);
139 + if(!co->name) fatal("Cannot allocate config.name");
140 co->hash = simple_hash(co->name);
141 +
142 co->values_index.compar = config_value_compare;
143
144 config_index_add(co);
@@ -229,8 +232,9 @@ int load_config(char *filename, int overwrite_used)
232 else {
233 if((cv->flags & CONFIG_VALUE_USED && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
234 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
232 - strncpy(cv->value, value, CONFIG_MAX_VALUE);
233 - // termination is already there
235 + free(cv->value);
236 + cv->value = strdup(value);
237 + if(!cv->value) fatal("Cannot allocate config.value");
238 }
239 else
240 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)
299 s = config_get(section, name, s);
300 if(!s) return 0;
301
298 - if(strcmp(s, "yes") == 0 || strcmp(s, "true") == 0 || strcmp(s, "1") == 0) {
299 - strcpy(s, "yes");
300 - return 1;
301 - }
302 - else {
303 - strcpy(s, "no");
304 - return 0;
305 - }
302 + if(!strcmp(s, "yes")) return 1;
303 + else return 0;
304 }
305
306 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)
320
321 if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
322
325 - strncpy(cv->value, value, CONFIG_MAX_VALUE);
326 - // termination is already there
323 + free(cv->value);
324 + cv->value = strdup(value);
325 + if(!cv->value) fatal("Cannot allocate config.value");
326
327 pthread_rwlock_unlock(&config_rwlock);
328
@@ -400,16 +399,16 @@ void generate_config(struct web_buffer *wb, int only_changed)
399 web_buffer_printf(wb, "\n# node '%s' is not used.", co->name);
400 }
401
403 - web_buffer_increase(wb, CONFIG_MAX_NAME + 4);
402 + web_buffer_increase(wb, CONFIG_FILE_LINE_MAX+1);
403 web_buffer_printf(wb, "\n[%s]\n", co->name);
404
405 for(cv = co->values; cv ; cv = cv->next) {
406
407 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
409 - web_buffer_increase(wb, CONFIG_MAX_NAME + 200);
408 + web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
409 web_buffer_printf(wb, "\n\t# option '%s' is not used.\n", cv->name);
410 }
412 - web_buffer_increase(wb, CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 5);
411 + web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
412 web_buffer_printf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
413 }
414 }
src/config.h
+5 -2
@@ -3,10 +3,13 @@
3 #ifndef NETDATA_CONFIG_H
4 #define NETDATA_CONFIG_H 1
5
6 -#define CONFIG_MAX_NAME 100
7 -#define CONFIG_MAX_VALUE 1024
6 #define CONFIG_FILENAME "netdata.conf"
7
8 +// these are used to limit the configuration names and values lengths
9 +// they are not enforced by config.c functions (they will strdup() all strings, no matter of their length)
10 +#define CONFIG_MAX_NAME 1024
11 +#define CONFIG_MAX_VALUE 2048
12 +
13 extern int load_config(char *filename, int overwrite_used);
14
15 extern char *config_get(const char *section, const char *name, const char *default_value);
src/log.c
+4 -8
@@ -39,12 +39,11 @@ void log_date(FILE *out)
39
40 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
41 {
42 - if(file) { ; }
42 va_list args;
43
44 log_date(stdout);
45 va_start( args, fmt );
47 - fprintf(stdout, "DEBUG (%04lu@%-15.15s): ", line, function);
46 + fprintf(stdout, "DEBUG (%04lu@%-10.10s:%-15.15s): ", line, file, function);
47 vfprintf( stdout, fmt, args );
48 va_end( args );
49 fprintf(stdout, "\n");
@@ -58,13 +57,12 @@ void debug_int( const char *file, const char *function, const unsigned long line
57
58 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
59 {
61 - if(file) { ; }
60 va_list args;
61
62 log_date(stderr);
63
64 va_start( args, fmt );
67 - if(debug_flags) fprintf(stderr, "INFO (%04lu@%-15.15s): ", line, function);
65 + if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): ", line, file, function);
66 else fprintf(stderr, "INFO: ");
67 vfprintf( stderr, fmt, args );
68 va_end( args );
@@ -80,13 +78,12 @@ void info_int( const char *file, const char *function, const unsigned long line,
78
79 void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
80 {
83 - if(file) { ; }
81 va_list args;
82
83 log_date(stderr);
84
85 va_start( args, fmt );
89 - if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-15.15s): ", line, function);
86 + if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-10.10s:%-15.15s): ", line, file, function);
87 else fprintf(stderr, "ERROR: ");
88 vfprintf( stderr, fmt, args );
89 va_end( args );
@@ -106,13 +103,12 @@ void error_int( const char *file, const char *function, const unsigned long line
103
104 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
105 {
109 - if(file) { ; }
106 va_list args;
107
108 log_date(stderr);
109
110 va_start( args, fmt );
115 - if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-15.15s): ", line, function);
111 + if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): ", line, file, function);
112 else fprintf(stderr, "FATAL: ");
113 vfprintf( stderr, fmt, args );
114 va_end( args );
src/plugins.d/apps_plugin.c
+2 -2
@@ -50,7 +50,7 @@ unsigned long long file_counter = 0;
50 long get_processors(void) {
51 int processors = 0;
52
53 - procfile *ff = procfile_open("/proc/stat", O_RDONLY);
53 + procfile *ff = procfile_open("/proc/stat", "");
54 if(!ff) return 1;
55
56 ff = procfile_readall(ff);
@@ -75,7 +75,7 @@ long get_processors(void) {
75 long get_pid_max(void) {
76 long mpid = 32768;
77
78 - procfile *ff = procfile_open("/proc/sys/kernel/pid_max", O_RDONLY);
78 + procfile *ff = procfile_open("/proc/sys/kernel/pid_max", "");
79 if(!ff) return mpid;
80
81 ff = procfile_readall(ff);
src/rrd.h
+1 -1
@@ -112,7 +112,7 @@ struct rrddim {
112
113 struct timeval last_collected_time; // when was this dimension last updated
114 // this is actual date time we updated the last_collected_value
115 - // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRD_STATS
115 + // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
116
117 calculated_number calculated_value; // the current calculated value, after applying the algorithm
118 calculated_number last_calculated_value; // the last calculated value