summaryrefslogtreecommitdiffstats
path: root/database/rrdvar.c
diff options
context:
space:
mode:
Diffstat (limited to 'database/rrdvar.c')
-rw-r--r--database/rrdvar.c272
1 files changed, 155 insertions, 117 deletions
diff --git a/database/rrdvar.c b/database/rrdvar.c
index 43976951ac..28be4f6a1e 100644
--- a/database/rrdvar.c
+++ b/database/rrdvar.c
@@ -1,8 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-or-later
-#define NETDATA_HEALTH_INTERNALS
#include "rrd.h"
+// the variables as stored in the variables indexes
+// there are 3 indexes:
+// 1. at each chart (RRDSET.rrdvar_root_index)
+// 2. at each context (RRDFAMILY.rrdvar_root_index)
+// 3. at each host (RRDHOST.rrdvar_root_index)
+typedef struct rrdvar {
+ STRING *name;
+ void *value;
+ RRDVAR_FLAGS flags:24;
+ RRDVAR_TYPE type:8;
+} RRDVAR;
+
// ----------------------------------------------------------------------------
// RRDVAR management
@@ -20,142 +31,153 @@ inline int rrdvar_fix_name(char *variable) {
return fixed;
}
-static inline RRDVAR *rrdvar_index_add(DICTIONARY *dict, RRDVAR *rv) {
- return dictionary_set(dict, rrdvar_name(rv), rv, sizeof(RRDVAR));
+inline STRING *rrdvar_name_to_string(const char *name) {
+ char *variable = strdupz(name);
+ rrdvar_fix_name(variable);
+ STRING *name_string = string_strdupz(variable);
+ freez(variable);
+ return name_string;
}
-static inline RRDVAR *rrdvar_index_del(DICTIONARY *dict, RRDVAR *rv) {
- if(dictionary_del(dict, rrdvar_name(rv)) != 0) {
- error("Request to remove RRDVAR '%s' from index failed. Not Found.", rrdvar_name(rv));
- return NULL;
- }
+struct rrdvar_constructor {
+ STRING *name;
+ void *value;
+ RRDVAR_FLAGS options:16;
+ RRDVAR_TYPE type:8;
- return rv;
-}
+ enum {
+ RRDVAR_REACT_NONE = 0,
+ RRDVAR_REACT_NEW = (1 << 0),
+ } react_action;
+};
-static inline RRDVAR *rrdvar_index_find(DICTIONARY *dict, STRING *name) {
- return dictionary_get(dict, string2str(name));
-}
+static void rrdvar_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdvar, void *constructor_data) {
+ RRDVAR *rv = rrdvar;
+ struct rrdvar_constructor *ctr = constructor_data;
-inline void rrdvar_free(RRDHOST *host, DICTIONARY *dict, RRDVAR *rv) {
- (void)host;
+ ctr->options &= ~RRDVAR_OPTIONS_REMOVED_ON_NEW_OBJECTS;
- if(!rv) return;
+ rv->name = string_dup(ctr->name);
+ rv->type = ctr->type;
+ rv->flags = ctr->options;
- if(dict) {
- debug(D_VARIABLES, "Deleting variable '%s'", rrdvar_name(rv));
- if(unlikely(!rrdvar_index_del(dict, rv)))
- error("RRDVAR: Attempted to delete variable '%s' from host '%s', but it is not found.", rrdvar_name(rv), rrdhost_hostname(host));
+ if(!ctr->value) {
+ NETDATA_DOUBLE *v = mallocz(sizeof(NETDATA_DOUBLE));
+ *v = NAN;
+ rv->value = v;
+ rv->flags |= RRDVAR_FLAG_ALLOCATED;
}
+ else
+ rv->value = ctr->value;
- if(rv->options & RRDVAR_OPTION_ALLOCATED)
+ ctr->react_action = RRDVAR_REACT_NEW;
+}
+
+static void rrdvar_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdvar, void *nothing __maybe_unused) {
+ RRDVAR *rv = rrdvar;
+
+ if(rv->flags & RRDVAR_FLAG_ALLOCATED)
freez(rv->value);
string_freez(rv->name);
- freez(rv);
+ rv->name = NULL;
}
-inline STRING *rrdvar_name_to_string(const char *name) {
- char *variable = strdupz(name);
- rrdvar_fix_name(variable);
- STRING *name_string = string_strdupz(variable);
- freez(variable);
- return name_string;
+DICTIONARY *rrdvariables_create(void) {
+ DICTIONARY *dict = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
+
+ dictionary_register_insert_callback(dict, rrdvar_insert_callback, NULL);
+ dictionary_register_delete_callback(dict, rrdvar_delete_callback, NULL);
+
+ return dict;
}
-inline RRDVAR *rrdvar_create_and_index(const char *scope __maybe_unused, DICTIONARY *dict, STRING *name,
- RRDVAR_TYPE type, RRDVAR_OPTIONS options, void *value) {
-
- RRDVAR *rv = rrdvar_index_find(dict, name);
- if(unlikely(!rv)) {
- debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", string2str(name), scope);
-
- rv = callocz(1, sizeof(RRDVAR));
- rv->name = string_dup(name);
- rv->type = type;
- rv->options = options;
- rv->value = value;
- rv->last_updated = now_realtime_sec();
-
- RRDVAR *ret = rrdvar_index_add(dict, rv);
- if(unlikely(ret != rv)) {
- debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", string2str(name), scope);
- freez(rv);
- rv = NULL;
- }
- else
- debug(D_VARIABLES, "Variable '%s' created in scope '%s'", string2str(name), scope);
- }
- else {
- debug(D_VARIABLES, "Variable '%s' is already found in scope '%s'.", string2str(name), scope);
+void rrdvariables_destroy(DICTIONARY *dict) {
+ dictionary_destroy(dict);
+}
- // this is important
- // it must return NULL - not the existing variable - or double-free will happen
- rv = NULL;
- }
+static inline const RRDVAR_ACQUIRED *rrdvar_get_and_acquire(DICTIONARY *dict, STRING *name) {
+ return (const RRDVAR_ACQUIRED *)dictionary_get_and_acquire_item_advanced(dict, string2str(name), (ssize_t)string_strlen(name) + 1);
+}
- return rv;
+inline void rrdvar_release_and_del(DICTIONARY *dict, const RRDVAR_ACQUIRED *rva) {
+ if(unlikely(!dict || !rva)) return;
+
+ RRDVAR *rv = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rva);
+
+ dictionary_del_advanced(dict, string2str(rv->name), (ssize_t)string_strlen(rv->name) + 1);
+
+ dictionary_acquired_item_release(dict, (const DICTIONARY_ITEM *)rva);
}
-void rrdvar_free_remaining_variables(RRDHOST *host, DICTIONARY *dict) {
- RRDVAR *rv;
- dfe_start_reentrant(dict, rv) {
- rrdvar_free(host, dict, rv);
- }
- dfe_done(rv);
+inline const RRDVAR_ACQUIRED *rrdvar_add_and_acquire(const char *scope __maybe_unused, DICTIONARY *dict, STRING *name, RRDVAR_TYPE type, RRDVAR_FLAGS options, void *value) {
+ if(unlikely(!dict || !name)) return NULL;
+
+ struct rrdvar_constructor tmp = {
+ .name = name,
+ .value = value,
+ .type = type,
+ .options = options,
+ .react_action = RRDVAR_REACT_NONE,
+ };
+ return (const RRDVAR_ACQUIRED *)dictionary_set_and_acquire_item_advanced(dict, string2str(name), (ssize_t)string_strlen(name) + 1, NULL, sizeof(RRDVAR), &tmp);
+}
+
+void rrdvar_delete_all(DICTIONARY *dict) {
+ dictionary_flush(dict);
}
+
// ----------------------------------------------------------------------------
// CUSTOM HOST VARIABLES
-inline int rrdvar_walkthrough_read(DICTIONARY *dict, int (*callback)(const char *name, void *rrdvar, void *data), void *data) {
+inline int rrdvar_walkthrough_read(DICTIONARY *dict, int (*callback)(const DICTIONARY_ITEM *item, void *rrdvar, void *data), void *data) {
+ if(unlikely(!dict)) return 0; // when health is not enabled
return dictionary_walkthrough_read(dict, callback, data);
}
-static RRDVAR *rrdvar_custom_variable_create(const char *scope, DICTIONARY *dict, const char *name) {
- NETDATA_DOUBLE *v = callocz(1, sizeof(NETDATA_DOUBLE));
- *v = NAN;
+const RRDVAR_ACQUIRED *rrdvar_custom_host_variable_add_and_acquire(RRDHOST *host, const char *name) {
+ DICTIONARY *dict = host->rrdvars;
+ if(unlikely(!dict)) return NULL; // when health is not enabled
STRING *name_string = rrdvar_name_to_string(name);
- RRDVAR *rv = rrdvar_create_and_index(scope, dict, name_string, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_CUSTOM_HOST_VAR|RRDVAR_OPTION_ALLOCATED, v);
- if(unlikely(!rv)) {
- freez(v);
- debug(D_VARIABLES, "Requested variable '%s' already exists - possibly 2 plugins are updating it at the same time.", string2str(name_string));
-
- // find the existing one to return it
- rv = rrdvar_index_find(dict, name_string);
- }
+ const RRDVAR_ACQUIRED *rva = rrdvar_add_and_acquire("host", dict, name_string, RRDVAR_TYPE_CALCULATED, RRDVAR_FLAG_CUSTOM_HOST_VAR, NULL);
string_freez(name_string);
-
- return rv;
+ return rva;
}
-RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
- return rrdvar_custom_variable_create("host", host->rrdvar_root_index, name);
-}
+void rrdvar_custom_host_variable_set(RRDHOST *host, const RRDVAR_ACQUIRED *rva, NETDATA_DOUBLE value) {
+ if(unlikely(!host->rrdvars || !rva)) return; // when health is not enabled
-void rrdvar_custom_host_variable_set(RRDHOST *host, RRDVAR *rv, NETDATA_DOUBLE value) {
- if(rv->type != RRDVAR_TYPE_CALCULATED || !(rv->options & RRDVAR_OPTION_CUSTOM_HOST_VAR) || !(rv->options & RRDVAR_OPTION_ALLOCATED))
- error("requested to set variable '%s' to value " NETDATA_DOUBLE_FORMAT " but the variable is not a custom one.", rrdvar_name(rv), value);
+ if(rrdvar_type(rva) != RRDVAR_TYPE_CALCULATED || !(rrdvar_flags(rva) & (RRDVAR_FLAG_CUSTOM_HOST_VAR | RRDVAR_FLAG_ALLOCATED)))
+ error("requested to set variable '%s' to value " NETDATA_DOUBLE_FORMAT " but the variable is not a custom one.", rrdvar_name(rva), value);
else {
+ RRDVAR *rv = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rva);
NETDATA_DOUBLE *v = rv->value;
if(*v != value) {
*v = value;
- rv->last_updated = now_realtime_sec();
-
// if the host is streaming, send this variable upstream immediately
- rrdpush_sender_send_this_host_variable_now(host, rv);
+ rrdpush_sender_send_this_host_variable_now(host, rva);
}
}
}
+void rrdvar_release(DICTIONARY *dict, const RRDVAR_ACQUIRED *rva) {
+ if(unlikely(!dict || !rva)) return; // when health is not enabled
+ dictionary_acquired_item_release(dict, (const DICTIONARY_ITEM *)rva);
+}
+
// ----------------------------------------------------------------------------
// RRDVAR lookup
-NETDATA_DOUBLE rrdvar2number(RRDVAR *rv) {
+NETDATA_DOUBLE rrdvar2number(const RRDVAR_ACQUIRED *rva) {
+ if(unlikely(!rva)) return NAN;
+
+ RRDVAR *rv = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rva);
+
switch(rv->type) {
case RRDVAR_TYPE_CALCULATED: {
NETDATA_DOUBLE *n = (NETDATA_DOUBLE *)rv->value;
@@ -164,17 +186,17 @@ NETDATA_DOUBLE rrdvar2number(RRDVAR *rv) {
case RRDVAR_TYPE_TIME_T: {
time_t *n = (time_t *)rv->value;
- return *n;
+ return (NETDATA_DOUBLE)*n;
}
case RRDVAR_TYPE_COLLECTED: {
collected_number *n = (collected_number *)rv->value;
- return *n;
+ return (NETDATA_DOUBLE)*n;
}
case RRDVAR_TYPE_TOTAL: {
total_number *n = (total_number *)rv->value;
- return *n;
+ return (NETDATA_DOUBLE)*n;
}
case RRDVAR_TYPE_INT: {
@@ -193,23 +215,26 @@ int health_variable_lookup(STRING *variable, RRDCALC *rc, NETDATA_DOUBLE *result
if(!st) return 0;
RRDHOST *host = st->rrdhost;
- RRDVAR *rv;
+ const RRDVAR_ACQUIRED *rva;
- rv = rrdvar_index_find(st->rrdvar_root_index, variable);
- if(rv) {
- *result = rrdvar2number(rv);
+ rva = rrdvar_get_and_acquire(st->rrdvars, variable);
+ if(rva) {
+ *result = rrdvar2number(rva);
+ dictionary_acquired_item_release(st->rrdvars, (const DICTIONARY_ITEM *)rva);
return 1;
}
- rv = rrdvar_index_find(st->rrdfamily->rrdvar_root_index, variable);
- if(rv) {
- *result = rrdvar2number(rv);
+ rva = rrdvar_get_and_acquire(rrdfamily_rrdvars_dict(st->rrdfamily), variable);
+ if(rva) {
+ *result = rrdvar2number(rva);
+ dictionary_acquired_item_release(rrdfamily_rrdvars_dict(st->rrdfamily), (const DICTIONARY_ITEM *)rva);
return 1;
}
- rv = rrdvar_index_find(host->rrdvar_root_index, variable);
- if(rv) {
- *result = rrdvar2number(rv);
+ rva = rrdvar_get_and_acquire(host->rrdvars, variable);
+ if(rva) {
+ *result = rrdvar2number(rva);
+ dictionary_acquired_item_release(host->rrdvars, (const DICTIONARY_ITEM *)rva);
return 1;
}
@@ -222,19 +247,19 @@ int health_variable_lookup(STRING *variable, RRDCALC *rc, NETDATA_DOUBLE *result
struct variable2json_helper {
BUFFER *buf;
size_t counter;
- RRDVAR_OPTIONS options;
+ RRDVAR_FLAGS options;
};
-static int single_variable2json(const char *name __maybe_unused, void *entry, void *data) {
- struct variable2json_helper *helper = (struct variable2json_helper *)data;
- RRDVAR *rv = (RRDVAR *)entry;
- NETDATA_DOUBLE value = rrdvar2number(rv);
+static int single_variable2json_callback(const DICTIONARY_ITEM *item __maybe_unused, void *entry __maybe_unused, void *helper_data) {
+ struct variable2json_helper *helper = (struct variable2json_helper *)helper_data;
+ const RRDVAR_ACQUIRED *rva = (const RRDVAR_ACQUIRED *)item;
+ NETDATA_DOUBLE value = rrdvar2number(rva);
- if (helper->options == RRDVAR_OPTION_DEFAULT || rv->options & helper->options) {
+ if (helper->options == RRDVAR_FLAG_NONE || rrdvar_flags(rva) & helper->options) {
if(unlikely(isnan(value) || isinf(value)))
- buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rrdvar_name(rv));
+ buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rrdvar_name(rva));
else
- buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" NETDATA_DOUBLE_MODIFIER, helper->counter?",":"", rrdvar_name(rv), (NETDATA_DOUBLE)value);
+ buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" NETDATA_DOUBLE_MODIFIER, helper->counter?",":"", rrdvar_name(rva), (NETDATA_DOUBLE)value);
helper->counter++;
}
@@ -246,11 +271,10 @@ void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf) {
struct variable2json_helper helper = {
.buf = buf,
.counter = 0,
- .options = RRDVAR_OPTION_CUSTOM_CHART_VAR
- };
+ .options = RRDVAR_FLAG_CUSTOM_CHART_VAR};
buffer_sprintf(buf, "{");
- rrdvar_walkthrough_read(st->rrdvar_root_index, single_variable2json, &helper);
+ rrdvar_walkthrough_read(st->rrdvars, single_variable2json_callback, &helper);
buffer_strcat(buf, "\n\t\t\t}");
}
@@ -260,20 +284,34 @@ void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf) {
struct variable2json_helper helper = {
.buf = buf,
.counter = 0,
- .options = RRDVAR_OPTION_DEFAULT
- };
+ .options = RRDVAR_FLAG_NONE};
buffer_sprintf(buf, "{\n\t\"chart\": \"%s\",\n\t\"chart_name\": \"%s\",\n\t\"chart_context\": \"%s\",\n\t\"chart_variables\": {", rrdset_id(st), rrdset_name(st), rrdset_context(st));
- rrdvar_walkthrough_read(st->rrdvar_root_index, single_variable2json, &helper);
+ rrdvar_walkthrough_read(st->rrdvars, single_variable2json_callback, &helper);
buffer_sprintf(buf, "\n\t},\n\t\"family\": \"%s\",\n\t\"family_variables\": {", rrdset_family(st));
helper.counter = 0;
- rrdvar_walkthrough_read(st->rrdfamily->rrdvar_root_index, single_variable2json, &helper);
+ rrdvar_walkthrough_read(rrdfamily_rrdvars_dict(st->rrdfamily), single_variable2json_callback, &helper);
buffer_sprintf(buf, "\n\t},\n\t\"host\": \"%s\",\n\t\"host_variables\": {", rrdhost_hostname(host));
helper.counter = 0;
- rrdvar_walkthrough_read(host->rrdvar_root_index, single_variable2json, &helper);
+ rrdvar_walkthrough_read(host->rrdvars, single_variable2json_callback, &helper);
buffer_strcat(buf, "\n\t}\n}\n");
}
+// ----------------------------------------------------------------------------
+// RRDVAR private members examination
+
+const char *rrdvar_name(const RRDVAR_ACQUIRED *rva) {
+ return dictionary_acquired_item_name((const DICTIONARY_ITEM *)rva);
+}
+
+RRDVAR_FLAGS rrdvar_flags(const RRDVAR_ACQUIRED *rva) {
+ RRDVAR *rv = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rva);
+ return rv->flags;
+}
+RRDVAR_TYPE rrdvar_type(const RRDVAR_ACQUIRED *rva) {
+ RRDVAR *rv = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rva);
+ return rv->type;
+}