@cryptotaxi247 / netdata-1 / commits / 57a38770f

health typedefs are now structured

Costa Tsaousis (ktsaou) committed Oct 15, 2017 at 14:35 UTC 57a38770fe2ddb93587fe9c2023163ff6117c869
16 files changed +130 -71
src/common.h
+13 -2
@@ -203,10 +203,21 @@
203 #define NETDATA_OS_TYPE "linux"
204 #endif /* __FreeBSD__, __APPLE__*/
205
206 -#include "statistical.h"
207 -#include "socket.h"
206 +typedef enum rrdcalc_status {
207 + RRDCALC_STATUS_REMOVED = -2,
208 + RRDCALC_STATUS_UNDEFINED = -1,
209 + RRDCALC_STATUS_UNINITIALIZED = 0,
210 + RRDCALC_STATUS_CLEAR = 1,
211 + RRDCALC_STATUS_RAISED = 2,
212 + RRDCALC_STATUS_WARNING = 3,
213 + RRDCALC_STATUS_CRITICAL = 4
214 +} RRDCALC_STATUS;
215 +
216 #include "eval.h"
217 #include "health.h"
218 +
219 +#include "statistical.h"
220 +#include "socket.h"
221 #include "rrd.h"
222 #include "plugin_tc.h"
223 #include "plugins_d.h"
src/eval.c
+1 -1
@@ -232,7 +232,7 @@ calculated_number eval_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
232 if(isinf(n1) && isinf(n2)) return 1;
233 if(isnan(n1) || isnan(n2)) return 0;
234 if(isinf(n1) || isinf(n2)) return 0;
235 - return n1 == n2;
235 + return calculated_number_equal(n1, n2);
236 }
237 calculated_number eval_not_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
238 return !eval_equal(exp, op, error);
src/eval.h
+1 -1
@@ -14,7 +14,7 @@ typedef struct eval_expression {
14 const char *source;
15 const char *parsed_as;
16
17 - int *status;
17 + RRDCALC_STATUS *status;
18 calculated_number *this;
19 time_t *after;
20 time_t *before;
src/health.c
+4 -5
@@ -84,7 +84,7 @@ void health_reload(void) {
84 // ----------------------------------------------------------------------------
85 // health main thread and friends
86
87 -static inline int rrdcalc_value2status(calculated_number n) {
87 +static inline RRDCALC_STATUS rrdcalc_value2status(calculated_number n) {
88 if(isnan(n) || isinf(n)) return RRDCALC_STATUS_UNDEFINED;
89 if(n) return RRDCALC_STATUS_RAISED;
90 return RRDCALC_STATUS_CLEAR;
@@ -189,7 +189,6 @@ static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
189
190 done:
191 health_alarm_log_save(host, ae);
192 - return;
192 }
193
194 static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
@@ -537,8 +536,8 @@ void *health_main(void *ptr) {
536 if(unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_RUNNABLE)))
537 continue;
538
540 - int warning_status = RRDCALC_STATUS_UNDEFINED;
541 - int critical_status = RRDCALC_STATUS_UNDEFINED;
539 + RRDCALC_STATUS warning_status = RRDCALC_STATUS_UNDEFINED;
540 + RRDCALC_STATUS critical_status = RRDCALC_STATUS_UNDEFINED;
541
542 // --------------------------------------------------------
543 // check the warning expression
@@ -605,7 +604,7 @@ void *health_main(void *ptr) {
604 // --------------------------------------------------------
605 // decide the final alarm status
606
608 - int status = RRDCALC_STATUS_UNDEFINED;
607 + RRDCALC_STATUS status = RRDCALC_STATUS_UNDEFINED;
608
609 switch(warning_status) {
610 case RRDCALC_STATUS_CLEAR:
src/health.h
+30 -31
@@ -5,13 +5,14 @@ extern int default_health_enabled;
5
6 extern int rrdvar_compare(void *a, void *b);
7
8 -#define RRDVAR_TYPE_CALCULATED 1
9 -#define RRDVAR_TYPE_TIME_T 2
10 -#define RRDVAR_TYPE_COLLECTED 3
11 -#define RRDVAR_TYPE_TOTAL 4
12 -#define RRDVAR_TYPE_INT 5
13 -#define RRDVAR_TYPE_CALCULATED_ALLOCATED 6
14 -
8 +typedef enum rrdvar_type {
9 + RRDVAR_TYPE_CALCULATED = 1,
10 + RRDVAR_TYPE_TIME_T = 2,
11 + RRDVAR_TYPE_COLLECTED = 3,
12 + RRDVAR_TYPE_TOTAL = 4,
13 + RRDVAR_TYPE_INT = 5,
14 + RRDVAR_TYPE_CALCULATED_ALLOCATED = 6
15 +} RRDVAR_TYPE;
16
17 // the variables as stored in the variables indexes
18 // there are 3 indexes:
@@ -24,7 +25,7 @@ typedef struct rrdvar {
25 char *name;
26 uint32_t hash;
27
27 - int type;
28 + RRDVAR_TYPE type;
29 void *value;
30
31 time_t last_updated;
@@ -35,15 +36,21 @@ typedef struct rrdvar {
36 // calculated / processed by the normal data collection process
37 // This means, there will be no speed penalty for using
38 // these variables
39 +
40 +typedef enum rrdvar_options {
41 + RRDVAR_OPTION_DEFAULT = (0 << 0)
42 + // future use
43 +} RRDVAR_OPTIONS;
44 +
45 typedef struct rrdsetvar {
46 char *key_fullid; // chart type.chart id.variable
47 char *key_fullname; // chart type.chart name.variable
41 - char *variable; // variable
48 + char *variable; // variable
49
43 - int type;
50 + RRDVAR_TYPE type;
51 void *value;
52
46 - uint32_t options;
53 + RRDVAR_OPTIONS options;
54
55 RRDVAR *var_local;
56 RRDVAR *var_family;
@@ -75,10 +82,10 @@ typedef struct rrddimvar {
82 char *key_fullnameid; // chart type.chart name + dimension id
83 char *key_fullnamename; // chart type.chart name + dimension name
84
78 - int type;
85 + RRDVAR_TYPE type;
86 void *value;
87
81 - uint32_t options;
88 + RRDVAR_OPTIONS options;
89
90 RRDVAR *var_local_id;
91 RRDVAR *var_local_name;
@@ -101,7 +108,7 @@ typedef struct rrddimvar {
108 // calculated variables (defined in health configuration)
109 // These aggregate time-series data at fixed intervals
110 // (defined in their update_every member below)
104 -// These increase the overhead of netdata.
111 +// They increase the overhead of netdata.
112 //
113 // These calculations are allocated and linked (->next)
114 // under RRDHOST.
@@ -111,14 +118,6 @@ typedef struct rrddimvar {
118 // having as RRDSET.calculations the RRDCALC to be processed
119 // next.
120
114 -#define RRDCALC_STATUS_REMOVED -2
115 -#define RRDCALC_STATUS_UNDEFINED -1
116 -#define RRDCALC_STATUS_UNINITIALIZED 0
117 -#define RRDCALC_STATUS_CLEAR 1
118 -#define RRDCALC_STATUS_RAISED 2
119 -#define RRDCALC_STATUS_WARNING 3
120 -#define RRDCALC_STATUS_CRITICAL 4
121 -
121 #define RRDCALC_FLAG_DB_ERROR 0x00000001
122 #define RRDCALC_FLAG_DB_NAN 0x00000002
123 /* #define RRDCALC_FLAG_DB_STALE 0x00000004 */
@@ -179,7 +178,7 @@ typedef struct rrdcalc {
178 // ------------------------------------------------------------------------
179 // runtime information
180
182 - int status; // the current status of the alarm
181 + RRDCALC_STATUS status; // the current status of the alarm
182
183 calculated_number value; // the current value of the alarm
184 calculated_number old_value; // the previous value of the alarm
@@ -314,8 +313,8 @@ typedef struct alarm_entry {
313 char *old_value_string;
314 char *new_value_string;
315
317 - int old_status;
318 - int new_status;
316 + RRDCALC_STATUS old_status;
317 + RRDCALC_STATUS new_status;
318
319 uint32_t flags;
320
@@ -340,11 +339,11 @@ typedef struct alarm_log {
339 #include "rrd.h"
340
341 extern void rrdsetvar_rename_all(RRDSET *st);
343 -extern RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options);
342 +extern RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, RRDVAR_TYPE type, void *value, RRDVAR_OPTIONS options);
343 extern void rrdsetvar_free(RRDSETVAR *rs);
344
345 extern void rrddimvar_rename_all(RRDDIM *rd);
347 -extern RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options);
346 +extern RRDDIMVAR *rrddimvar_create(RRDDIM *rd, RRDVAR_TYPE type, const char *prefix, const char *suffix, void *value, RRDVAR_OPTIONS options);
347 extern void rrddimvar_free(RRDDIMVAR *rs);
348
349 extern void rrdsetcalc_link_matching(RRDSET *st);
@@ -367,7 +366,7 @@ extern RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *nam
366 extern void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name);
367 extern void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value);
368
370 -extern const char *rrdcalc_status2string(int status);
369 +extern const char *rrdcalc_status2string(RRDCALC_STATUS status);
370
371
372 extern int health_alarm_log_open(RRDHOST *host);
@@ -389,8 +388,8 @@ extern void health_alarm_log(
388 time_t duration,
389 calculated_number old_value,
390 calculated_number new_value,
392 - int old_status,
393 - int new_status,
391 + RRDCALC_STATUS old_status,
392 + RRDCALC_STATUS new_status,
393 const char *source,
394 const char *units,
395 const char *info,
@@ -419,7 +418,7 @@ extern int rrdvar_fix_name(char *variable);
418 extern RRDCALC *rrdcalc_create(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart);
419 extern void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc);
420
422 -extern RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, void *value);
421 +extern RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, RRDVAR_TYPE type, void *value);
422 extern void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv);
423
424 extern void health_alarm_log_free_one_nochecks_nounlink(ALARM_ENTRY *ae);
src/health_log.c
+2 -2
@@ -351,8 +351,8 @@ inline void health_alarm_log(
351 time_t duration,
352 calculated_number old_value,
353 calculated_number new_value,
354 - int old_status,
355 - int new_status,
354 + RRDCALC_STATUS old_status,
355 + RRDCALC_STATUS new_status,
356 const char *source,
357 const char *units,
358 const char *info,
src/main.c
+10 -10
@@ -228,7 +228,7 @@ void kill_childs()
228 info("All threads/childs stopped.");
229 }
230
231 -struct option_def options[] = {
231 +struct option_def option_definitions[] = {
232 // opt description arg name default value
233 { 'c', "Configuration file to load.", "filename", CONFIG_DIR "/" CONFIG_FILENAME},
234 { 'D', "Do not fork. Run in the foreground.", NULL, "run in the background"},
@@ -251,14 +251,14 @@ int help(int exitcode) {
251 else
252 stream = stderr;
253
254 - int num_opts = sizeof(options) / sizeof(struct option_def);
254 + int num_opts = sizeof(option_definitions) / sizeof(struct option_def);
255 int i;
256 int max_len_arg = 0;
257
258 // Compute maximum argument length
259 for( i = 0; i < num_opts; i++ ) {
260 - if(options[i].arg_name) {
261 - int len_arg = (int)strlen(options[i].arg_name);
260 + if(option_definitions[i].arg_name) {
261 + int len_arg = (int)strlen(option_definitions[i].arg_name);
262 if(len_arg > max_len_arg) max_len_arg = len_arg;
263 }
264 }
@@ -296,9 +296,9 @@ int help(int exitcode) {
296
297 // Output options description.
298 for( i = 0; i < num_opts; i++ ) {
299 - fprintf(stream, " -%c %-*s %s", options[i].val, max_len_arg, options[i].arg_name ? options[i].arg_name : "", options[i].description);
300 - if(options[i].default_value) {
301 - fprintf(stream, "\n %c %-*s Default: %s\n", ' ', max_len_arg, "", options[i].default_value);
299 + fprintf(stream, " -%c %-*s %s", option_definitions[i].val, max_len_arg, option_definitions[i].arg_name ? option_definitions[i].arg_name : "", option_definitions[i].description);
300 + if(option_definitions[i].default_value) {
301 + fprintf(stream, "\n %c %-*s Default: %s\n", ' ', max_len_arg, "", option_definitions[i].default_value);
302 } else {
303 fprintf(stream, "\n");
304 }
@@ -656,14 +656,14 @@ int main(int argc, char **argv) {
656
657 // parse options
658 {
659 - int num_opts = sizeof(options) / sizeof(struct option_def);
659 + int num_opts = sizeof(option_definitions) / sizeof(struct option_def);
660 char optstring[(num_opts * 2) + 1];
661
662 int string_i = 0;
663 for( i = 0; i < num_opts; i++ ) {
664 - optstring[string_i] = options[i].val;
664 + optstring[string_i] = option_definitions[i].val;
665 string_i++;
666 - if(options[i].arg_name) {
666 + if(option_definitions[i].arg_name) {
667 optstring[string_i] = ':';
668 string_i++;
669 }
src/main.h
-6
@@ -16,12 +16,6 @@ struct option_def {
16 const char *default_value;
17 };
18
19 -/**
20 - * List of command line options.
21 - * This can be used to compute manpage, help messages, ect.
22 - */
23 -extern struct option_def options[];
24 -
19 struct netdata_static_thread {
20 char *name;
21
src/rrdcalc.c
+1 -1
@@ -4,7 +4,7 @@
4 // ----------------------------------------------------------------------------
5 // RRDCALC management
6
7 -inline const char *rrdcalc_status2string(int status) {
7 +inline const char *rrdcalc_status2string(RRDCALC_STATUS status) {
8 switch(status) {
9 case RRDCALC_STATUS_REMOVED:
10 return "REMOVED";
src/rrddim.c
+3 -3
@@ -262,9 +262,9 @@ RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collecte
262 }
263
264 if(st->rrdhost->health_enabled) {
265 - rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
266 - rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
267 - rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
265 + rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, RRDVAR_OPTION_DEFAULT);
266 + rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, RRDVAR_OPTION_DEFAULT);
267 + rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
268 }
269
270 rrdset_unlock(st);
src/rrddimvar.c
+1 -1
@@ -147,7 +147,7 @@ static inline void rrddimvar_create_variables(RRDDIMVAR *rs) {
147 rs->var_host_chartnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->key_fullnamename, rs->type, rs->value);
148 }
149
150 -RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
150 +RRDDIMVAR *rrddimvar_create(RRDDIM *rd, RRDVAR_TYPE type, const char *prefix, const char *suffix, void *value, RRDVAR_OPTIONS options) {
151 RRDSET *st = rd->rrdset;
152
153 debug(D_VARIABLES, "RRDDIMSET create for chart id '%s' name '%s', dimension id '%s', name '%s%s%s'", st->id, st->name, rd->id, (prefix)?prefix:"", rd->name, (suffix)?suffix:"");
src/rrdset.c
+5 -5
@@ -660,11 +660,11 @@ RRDSET *rrdset_create_custom(
660 host->rrdset_root = st;
661
662 if(host->health_enabled) {
663 - rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
664 - rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
665 - rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
666 - rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
667 - rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
663 + rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
664 + rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, RRDVAR_OPTION_DEFAULT);
665 + rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, RRDVAR_OPTION_DEFAULT);
666 + rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, RRDVAR_OPTION_DEFAULT);
667 + rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, RRDVAR_OPTION_DEFAULT);
668 }
669
670 if(unlikely(rrdset_index_add(host, st) != st))
src/rrdsetvar.c
+1 -1
@@ -68,7 +68,7 @@ static inline void rrdsetvar_create_variables(RRDSETVAR *rs) {
68
69 }
70
71 -RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
71 +RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, RRDVAR_TYPE type, void *value, RRDVAR_OPTIONS options) {
72 debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
73 RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
74
src/rrdvar.c
+2 -2
@@ -63,7 +63,7 @@ inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
63 freez(rv);
64 }
65
66 -inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, void *value) {
66 +inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, RRDVAR_TYPE type, void *value) {
67 char *variable = strdupz(name);
68 rrdvar_fix_name(variable);
69 uint32_t hash = simple_hash(variable);
@@ -191,7 +191,7 @@ static calculated_number rrdvar2number(RRDVAR *rv) {
191 }
192
193 default:
194 - error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
194 + error("I don't know how to convert RRDVAR type %u to calculated_number", rv->type);
195 return NAN;
196 }
197 }
src/storage_number.h
+3
@@ -17,6 +17,9 @@ typedef long double collected_number;
17 #define calculated_number_llrint(x) llrintl(x)
18 #define calculated_number_round(x) roundl(x)
19 #define calculated_number_fabs(x) fabsl(x)
20 +#define calculated_number_epsilon (calculated_number)0.0000001
21 +
22 +#define calculated_number_equal(a, b) (calculated_number_fabs((a) - (b)) < calculated_number_epsilon)
23
24 typedef uint32_t storage_number;
25 #define STORAGE_NUMBER_FORMAT "%u"
src/unit_test.c
+53
@@ -1,5 +1,55 @@
1 #include "common.h"
2
3 +static int check_rrdcalc_comparisons(void) {
4 + RRDCALC_STATUS a, b;
5 +
6 + a = RRDCALC_STATUS_REMOVED;
7 + b = RRDCALC_STATUS_UNDEFINED;
8 + if(!(a < b)) {
9 + fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
10 + return 1;
11 + }
12 +
13 + a = RRDCALC_STATUS_UNDEFINED;
14 + b = RRDCALC_STATUS_UNINITIALIZED;
15 + if(!(a < b)) {
16 + fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
17 + return 1;
18 + }
19 +
20 + a = RRDCALC_STATUS_UNINITIALIZED;
21 + b = RRDCALC_STATUS_CLEAR;
22 + if(!(a < b)) {
23 + fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
24 + return 1;
25 + }
26 +
27 + a = RRDCALC_STATUS_CLEAR;
28 + b = RRDCALC_STATUS_RAISED;
29 + if(!(a < b)) {
30 + fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
31 + return 1;
32 + }
33 +
34 + a = RRDCALC_STATUS_RAISED;
35 + b = RRDCALC_STATUS_WARNING;
36 + if(!(a < b)) {
37 + fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
38 + return 1;
39 + }
40 +
41 + a = RRDCALC_STATUS_WARNING;
42 + b = RRDCALC_STATUS_CRITICAL;
43 + if(!(a < b)) {
44 + fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
45 + return 1;
46 + }
47 +
48 + fprintf(stderr, "RRDCALC_STATUSes are sortable.\n");
49 +
50 + return 0;
51 +}
52 +
53 int check_storage_number(calculated_number n, int debug) {
54 char buffer[100];
55 uint32_t flags = SN_EXISTS;
@@ -1106,6 +1156,9 @@ static int test_variable_renames(void) {
1156
1157 int run_all_mockup_tests(void)
1158 {
1159 + if(check_rrdcalc_comparisons())
1160 + return 1;
1161 +
1162 if(!test_variable_renames())
1163 return 1;
1164