mirror of
https://github.com/netdata/netdata.git
synced 2025-04-13 01:08:11 +00:00

* rrdset - in progress * rrdset optimal constructor; rrdset conflict * rrdset final touches * re-organization of rrdset object members * prevent use-after-free * dictionary dfe supports also counting of iterations * rrddim managed by dictionary * rrd.h cleanup * DICTIONARY_ITEM now is referencing actual dictionary items in the code * removed rrdset linked list * Revert "removed rrdset linked list" This reverts commit 690d6a588b4b99619c2c5e10f84e8f868ae6def5. * removed rrdset linked list * added comments * Switch chart uuid to static allocation in rrdset Remove unused functions * rrdset_archive() and friends... * always create rrdfamily * enable ml_free_dimension * rrddim_foreach done with dfe * most custom rrddim loops replaced with rrddim_foreach * removed accesses to rrddim->dimensions * removed locks that are no longer needed * rrdsetvar is now managed by the dictionary * set rrdset is rrdsetvar, fixes https://github.com/netdata/netdata/pull/13646#issuecomment-1242574853 * conflict callback of rrdsetvar now properly checks if it has to reset the variable * dictionary registered callbacks accept as first parameter the DICTIONARY_ITEM * dictionary dfe now uses internal counter to report; avoided excess variables defined with dfe * dictionary walkthrough callbacks get dictionary acquired items * dictionary reference counters that can be dupped from zero * added advanced functions for get and del * rrdvar managed by dictionaries * thread safety for rrdsetvar * faster rrdvar initialization * rrdvar string lengths should match in all add, del, get functions * rrdvar internals hidden from the rest of the world * rrdvar is now acquired throughout netdata * hide the internal structures of rrdsetvar * rrdsetvar is now acquired through out netdata * rrddimvar managed by dictionary; rrddimvar linked list removed; rrddimvar structures hidden from the rest of netdata * better error handling * dont create variables if not initialized for health * dont create variables if not initialized for health again * rrdfamily is now managed by dictionaries; references of it are acquired dictionary items * type checking on acquired objects * rrdcalc renaming of functions * type checking for rrdfamily_acquired * rrdcalc managed by dictionaries * rrdcalc double free fix * host rrdvars is always needed * attempt to fix deadlock 1 * attempt to fix deadlock 2 * Remove unused variable * attempt to fix deadlock 3 * snprintfz * rrdcalc index in rrdset fix * Stop storing active charts and computing chart hashes * Remove store active chart function * Remove compute chart hash function * Remove sql_store_chart_hash function * Remove store_active_dimension function * dictionary delayed destruction * formatting and cleanup * zero dictionary base on rrdsetvar * added internal error to log delayed destructions of dictionaries * typo in rrddimvar * added debugging info to dictionary * debug info * fix for rrdcalc keys being empty * remove forgotten unlock * remove deadlock * Switch to metadata version 5 and drop chart_hash chart_hash_map chart_active dimension_active v_chart_hash * SQL cosmetic changes * do not busy wait while destroying a referenced dictionary * remove deadlock * code cleanup; re-organization; * fast cleanup and flushing of dictionaries * number formatting fixes * do not delete configured alerts when archiving a chart * rrddim obsolete linked list management outside dictionaries * removed duplicate contexts call * fix crash when rrdfamily is not initialized * dont keep rrddimvar referenced * properly cleanup rrdvar * removed some locks * Do not attempt to cleanup chart_hash / chart_hash_map * rrdcalctemplate managed by dictionary * register callbacks on the right dictionary * removed some more locks * rrdcalc secondary index replaced with linked-list; rrdcalc labels updates are now executed by health thread * when looking up for an alarm look using both chart id and chart name * host initialization a bit more modular * init rrdlabels on host update * preparation for dictionary views * improved comment * unused variables without internal checks * service threads isolation and worker info * more worker info in service thread * thread cancelability debugging with internal checks * strings data races addressed; fixes https://github.com/netdata/netdata/issues/13647 * dictionary modularization * Remove unused SQL statement definition * unit-tested thread safety of dictionaries; removed data race conditions on dictionaries and strings; dictionaries now can detect if the caller is holds a write lock and automatically all the calls become their unsafe versions; all direct calls to unsafe version is eliminated * remove worker_is_idle() from the exit of service functions, because we lose the lock time between loops * rewritten dictionary to have 2 separate locks, one for indexing and another for traversal * Update collectors/cgroups.plugin/sys_fs_cgroup.c Co-authored-by: Vladimir Kobal <vlad@prokk.net> * Update collectors/cgroups.plugin/sys_fs_cgroup.c Co-authored-by: Vladimir Kobal <vlad@prokk.net> * Update collectors/proc.plugin/proc_net_dev.c Co-authored-by: Vladimir Kobal <vlad@prokk.net> * fix memory leak in rrdset cache_dir * minor dictionary changes * dont use index locks in single threaded * obsolete dict option * rrddim options and flags separation; rrdset_done() optimization to keep array of reference pointers to rrddim; * fix jump on uninitialized value in dictionary; remove double free of cache_dir * addressed codacy findings * removed debugging code * use the private refcount on dictionaries * make dictionary item desctructors work on dictionary destruction; strictier control on dictionary API; proper cleanup sequence on rrddim; * more dictionary statistics * global statistics about dictionary operations, memory, items, callbacks * dictionary support for views - missing the public API * removed warning about unused parameter * chart and context name for cloud * chart and context name for cloud, again * dictionary statistics fixed; first implementation of dictionary views - not currently used * only the master can globally delete an item * context needs netdata prefix * fix context and chart it of spins * fix for host variables when health is not enabled * run garbage collector on item insert too * Fix info message; remove extra "using" * update dict unittest for new placement of garbage collector * we need RRDHOST->rrdvars for maintaining custom host variables * Health initialization needs the host->host_uuid * split STRING to its own files; no code changes other than that * initialize health unconditionally * unit tests do not pollute the global scope with their variables * Skip initialization when creating archived hosts on startup. When a child connects it will initialize properly Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com> Co-authored-by: Vladimir Kobal <vlad@prokk.net>
750 lines
28 KiB
C
750 lines
28 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#define NETDATA_RRD_INTERNALS
|
|
#include "rrd.h"
|
|
#ifdef ENABLE_DBENGINE
|
|
#include "database/engine/rrdengineapi.h"
|
|
#endif
|
|
#include "storage_engine.h"
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDDIM index
|
|
|
|
struct rrddim_constructor {
|
|
RRDSET *st;
|
|
const char *id;
|
|
const char *name;
|
|
collected_number multiplier;
|
|
collected_number divisor;
|
|
RRD_ALGORITHM algorithm;
|
|
RRD_MEMORY_MODE memory_mode;
|
|
|
|
enum {
|
|
RRDDIM_REACT_NONE = 0,
|
|
RRDDIM_REACT_NEW = (1 << 0),
|
|
RRDDIM_REACT_UPDATED = (1 << 2),
|
|
} react_action;
|
|
|
|
};
|
|
|
|
static void rrddim_update_rrddimvars_unsafe(RRDDIM *rd) {
|
|
RRDSET *st = rd->rrdset;
|
|
RRDHOST *host = st->rrdhost;
|
|
|
|
if(host->health_enabled && !rrdset_is_ar_chart(st)) {
|
|
rrddimvar_add_and_leave_released(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, RRDVAR_FLAG_NONE);
|
|
rrddimvar_add_and_leave_released(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, RRDVAR_FLAG_NONE);
|
|
rrddimvar_add_and_leave_released(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, RRDVAR_FLAG_NONE);
|
|
rrddim_flag_set(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARMS);
|
|
rrdset_flag_set(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS);
|
|
rrdhost_flag_set(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS);
|
|
}
|
|
}
|
|
|
|
static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *constructor_data) {
|
|
struct rrddim_constructor *ctr = constructor_data;
|
|
RRDDIM *rd = rrddim;
|
|
RRDSET *st = ctr->st;
|
|
RRDHOST *host = st->rrdhost;
|
|
|
|
rd->flags = RRDDIM_FLAG_NONE;
|
|
|
|
rd->id = string_strdupz(ctr->id);
|
|
rd->name = (ctr->name && *ctr->name)?rrd_string_strdupz(ctr->name):string_dup(rd->id);
|
|
|
|
rd->algorithm = ctr->algorithm;
|
|
rd->multiplier = ctr->multiplier;
|
|
rd->divisor = ctr->divisor;
|
|
if(!rd->divisor) rd->divisor = 1;
|
|
|
|
rd->update_every = st->update_every;
|
|
|
|
rd->rrdset = st;
|
|
|
|
if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
|
|
rd->collections_counter = 1;
|
|
|
|
if(ctr->memory_mode == RRD_MEMORY_MODE_MAP || ctr->memory_mode == RRD_MEMORY_MODE_SAVE) {
|
|
if(!rrddim_memory_load_or_create_map_save(st, rd, ctr->memory_mode)) {
|
|
info("Failed to use memory mode %s for chart '%s', dimension '%s', falling back to ram", (ctr->memory_mode == RRD_MEMORY_MODE_MAP)?"map":"save", rrdset_name(st), rrddim_name(rd));
|
|
ctr->memory_mode = RRD_MEMORY_MODE_RAM;
|
|
}
|
|
}
|
|
|
|
if(ctr->memory_mode == RRD_MEMORY_MODE_RAM) {
|
|
size_t entries = st->entries;
|
|
if(!entries) entries = 5;
|
|
|
|
rd->db = netdata_mmap(NULL, entries * sizeof(storage_number), MAP_PRIVATE, 1);
|
|
if(!rd->db) {
|
|
info("Failed to use memory mode ram for chart '%s', dimension '%s', falling back to alloc", rrdset_name(st), rrddim_name(rd));
|
|
ctr->memory_mode = RRD_MEMORY_MODE_ALLOC;
|
|
}
|
|
else rd->memsize = entries * sizeof(storage_number);
|
|
}
|
|
|
|
if(ctr->memory_mode == RRD_MEMORY_MODE_ALLOC || ctr->memory_mode == RRD_MEMORY_MODE_NONE) {
|
|
size_t entries = st->entries;
|
|
if(entries < 5) entries = 5;
|
|
|
|
rd->db = callocz(entries, sizeof(storage_number));
|
|
rd->memsize = entries * sizeof(storage_number);
|
|
}
|
|
|
|
rd->rrd_memory_mode = ctr->memory_mode;
|
|
|
|
#ifdef ENABLE_ACLK
|
|
rd->aclk_live_status = -1;
|
|
#endif
|
|
|
|
(void) find_dimension_uuid(st, rd, &(rd->metric_uuid));
|
|
|
|
// initialize the db tiers
|
|
{
|
|
size_t initialized = 0;
|
|
RRD_MEMORY_MODE wanted_mode = ctr->memory_mode;
|
|
for(int tier = 0; tier < storage_tiers ; tier++, wanted_mode = RRD_MEMORY_MODE_DBENGINE) {
|
|
STORAGE_ENGINE *eng = storage_engine_get(wanted_mode);
|
|
if(!eng) continue;
|
|
|
|
rd->tiers[tier] = callocz(1, sizeof(struct rrddim_tier));
|
|
rd->tiers[tier]->tier_grouping = get_tier_grouping(tier);
|
|
rd->tiers[tier]->mode = eng->id;
|
|
rd->tiers[tier]->collect_ops = eng->api.collect_ops;
|
|
rd->tiers[tier]->query_ops = eng->api.query_ops;
|
|
rd->tiers[tier]->db_metric_handle = eng->api.init(rd, host->storage_instance[tier]);
|
|
storage_point_unset(rd->tiers[tier]->virtual_point);
|
|
initialized++;
|
|
|
|
// internal_error(true, "TIER GROUPING of chart '%s', dimension '%s' for tier %d is set to %d", rd->rrdset->name, rd->name, tier, rd->tiers[tier]->tier_grouping);
|
|
}
|
|
|
|
if(!initialized)
|
|
error("Failed to initialize all db tiers for chart '%s', dimension '%s", rrdset_name(st), rrddim_name(rd));
|
|
|
|
if(!rd->tiers[0])
|
|
error("Failed to initialize the first db tier for chart '%s', dimension '%s", rrdset_name(st), rrddim_name(rd));
|
|
}
|
|
|
|
// initialize data collection for all tiers
|
|
{
|
|
size_t initialized = 0;
|
|
for (int tier = 0; tier < storage_tiers; tier++) {
|
|
if (rd->tiers[tier]) {
|
|
rd->tiers[tier]->db_collection_handle = rd->tiers[tier]->collect_ops.init(rd->tiers[tier]->db_metric_handle);
|
|
initialized++;
|
|
}
|
|
}
|
|
|
|
if(!initialized)
|
|
error("Failed to initialize data collection for all db tiers for chart '%s', dimension '%s", rrdset_name(st), rrddim_name(rd));
|
|
}
|
|
|
|
if(st->dimensions) {
|
|
RRDDIM *td = st->dimensions;
|
|
|
|
if(td->algorithm != rd->algorithm || ABS(td->multiplier) != ABS(rd->multiplier) || ABS(td->divisor) != ABS(rd->divisor)) {
|
|
if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
|
|
#ifdef NETDATA_INTERNAL_CHECKS
|
|
info("Dimension '%s' added on chart '%s' of host '%s' is not homogeneous to other dimensions already present (algorithm is '%s' vs '%s', multiplier is " COLLECTED_NUMBER_FORMAT " vs " COLLECTED_NUMBER_FORMAT ", divisor is " COLLECTED_NUMBER_FORMAT " vs " COLLECTED_NUMBER_FORMAT ").",
|
|
rrddim_name(rd),
|
|
rrdset_name(st),
|
|
rrdhost_hostname(host),
|
|
rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(td->algorithm),
|
|
rd->multiplier, td->multiplier,
|
|
rd->divisor, td->divisor
|
|
);
|
|
#endif
|
|
rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
|
|
}
|
|
}
|
|
}
|
|
|
|
rrddim_update_rrddimvars_unsafe(rd);
|
|
|
|
// let the chart resync
|
|
rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
|
|
rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
|
|
|
|
ml_new_dimension(rd);
|
|
|
|
ctr->react_action = RRDDIM_REACT_NEW;
|
|
|
|
internal_error(false, "RRDDIM: inserted dimension '%s' of chart '%s' of host '%s'",
|
|
rrddim_name(rd), rrdset_name(st), rrdhost_hostname(st->rrdhost));
|
|
|
|
}
|
|
|
|
static void rrddim_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *rrdset) {
|
|
RRDDIM *rd = rrddim;
|
|
RRDSET *st = rrdset; (void)st;
|
|
|
|
internal_error(false, "RRDDIM: deleting dimension '%s' of chart '%s' of host '%s'",
|
|
rrddim_name(rd), rrdset_name(st), rrdhost_hostname(st->rrdhost));
|
|
|
|
rrdcontext_removed_rrddim(rd);
|
|
|
|
ml_delete_dimension(rd);
|
|
|
|
debug(D_RRD_CALLS, "rrddim_free() %s.%s", rrdset_name(st), rrddim_name(rd));
|
|
|
|
if (!rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
|
|
|
|
size_t tiers_available = 0, tiers_said_yes = 0;
|
|
for(int tier = 0; tier < storage_tiers ;tier++) {
|
|
if(rd->tiers[tier]) {
|
|
tiers_available++;
|
|
|
|
if(rd->tiers[tier]->collect_ops.finalize(rd->tiers[tier]->db_collection_handle))
|
|
tiers_said_yes++;
|
|
|
|
rd->tiers[tier]->db_collection_handle = NULL;
|
|
}
|
|
}
|
|
|
|
if (tiers_available == tiers_said_yes && tiers_said_yes && rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
|
|
/* This metric has no data and no references */
|
|
delete_dimension_uuid(&rd->metric_uuid);
|
|
}
|
|
}
|
|
|
|
rrddimvar_delete_all(rd);
|
|
|
|
// free(rd->annotations);
|
|
//#ifdef ENABLE_ACLK
|
|
// if (!netdata_exit)
|
|
// aclk_send_dimension_update(rd);
|
|
//#endif
|
|
|
|
// this will free MEMORY_MODE_SAVE and MEMORY_MODE_MAP structures
|
|
rrddim_memory_file_free(rd);
|
|
|
|
for(int tier = 0; tier < storage_tiers ;tier++) {
|
|
if(!rd->tiers[tier]) continue;
|
|
|
|
STORAGE_ENGINE* eng = storage_engine_get(rd->tiers[tier]->mode);
|
|
if(eng)
|
|
eng->api.free(rd->tiers[tier]->db_metric_handle);
|
|
|
|
freez(rd->tiers[tier]);
|
|
rd->tiers[tier] = NULL;
|
|
}
|
|
|
|
if(rd->db) {
|
|
if(rd->rrd_memory_mode == RRD_MEMORY_MODE_RAM)
|
|
munmap(rd->db, rd->memsize);
|
|
else
|
|
freez(rd->db);
|
|
}
|
|
|
|
string_freez(rd->id);
|
|
string_freez(rd->name);
|
|
}
|
|
|
|
static bool rrddim_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *new_rrddim, void *constructor_data) {
|
|
(void)new_rrddim; // it is NULL
|
|
|
|
struct rrddim_constructor *ctr = constructor_data;
|
|
RRDDIM *rd = rrddim;
|
|
RRDSET *st = ctr->st;
|
|
|
|
ctr->react_action = RRDDIM_REACT_NONE;
|
|
|
|
int rc = rrddim_reset_name(st, rd, ctr->name);
|
|
rc += rrddim_set_algorithm(st, rd, ctr->algorithm);
|
|
rc += rrddim_set_multiplier(st, rd, ctr->multiplier);
|
|
rc += rrddim_set_divisor(st, rd, ctr->divisor);
|
|
|
|
if(rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
|
|
|
|
for(int tier = 0; tier < storage_tiers ;tier++) {
|
|
if (rd->tiers[tier])
|
|
rd->tiers[tier]->db_collection_handle =
|
|
rd->tiers[tier]->collect_ops.init(rd->tiers[tier]->db_metric_handle);
|
|
}
|
|
|
|
rrddim_flag_clear(rd, RRDDIM_FLAG_ARCHIVED);
|
|
rrddim_update_rrddimvars_unsafe(rd);
|
|
}
|
|
|
|
if(unlikely(rc))
|
|
ctr->react_action = RRDDIM_REACT_UPDATED;
|
|
|
|
return ctr->react_action == RRDDIM_REACT_UPDATED;
|
|
}
|
|
|
|
static void rrddim_react_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *constructor_data) {
|
|
struct rrddim_constructor *ctr = constructor_data;
|
|
RRDDIM *rd = rrddim;
|
|
RRDSET *st = ctr->st;
|
|
|
|
if(ctr->react_action == RRDDIM_REACT_UPDATED) {
|
|
debug(D_METADATALOG, "DIMENSION [%s] metadata updated", rrddim_id(rd));
|
|
(void)sql_store_dimension(&rd->metric_uuid, &rd->rrdset->chart_uuid, rrddim_id(rd), rrddim_name(rd), rd->multiplier, rd->divisor, rd->algorithm);
|
|
#ifdef ENABLE_ACLK
|
|
queue_dimension_to_aclk(rd, calc_dimension_liveness(rd, now_realtime_sec()));
|
|
#endif
|
|
|
|
// the chart needs to be updated to the parent
|
|
rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
|
|
rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
|
|
}
|
|
|
|
rrdcontext_updated_rrddim(rd);
|
|
}
|
|
|
|
void rrddim_index_init(RRDSET *st) {
|
|
if(!st->rrddim_root_index) {
|
|
st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
|
|
|
|
dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL);
|
|
dictionary_register_conflict_callback(st->rrddim_root_index, rrddim_conflict_callback, NULL);
|
|
dictionary_register_delete_callback(st->rrddim_root_index, rrddim_delete_callback, st);
|
|
dictionary_register_react_callback(st->rrddim_root_index, rrddim_react_callback, st);
|
|
}
|
|
}
|
|
|
|
void rrddim_index_destroy(RRDSET *st) {
|
|
dictionary_destroy(st->rrddim_root_index);
|
|
st->rrddim_root_index = NULL;
|
|
}
|
|
|
|
static inline RRDDIM *rrddim_index_find(RRDSET *st, const char *id) {
|
|
return dictionary_get(st->rrddim_root_index, id);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDDIM - find a dimension
|
|
|
|
inline RRDDIM *rrddim_find(RRDSET *st, const char *id) {
|
|
debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", rrdset_name(st), id);
|
|
|
|
return rrddim_index_find(st, id);
|
|
}
|
|
|
|
// This will not return dimensions that are archived
|
|
RRDDIM *rrddim_find_active(RRDSET *st, const char *id) {
|
|
RRDDIM *rd = rrddim_find(st, id);
|
|
|
|
if (unlikely(rd && rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)))
|
|
return NULL;
|
|
|
|
return rd;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDDIM rename a dimension
|
|
|
|
inline int rrddim_reset_name(RRDSET *st, RRDDIM *rd, const char *name) {
|
|
if(unlikely(!name || !*name || !strcmp(rrddim_name(rd), name)))
|
|
return 0;
|
|
|
|
debug(D_RRD_CALLS, "rrddim_reset_name() from %s.%s to %s.%s", rrdset_name(st), rrddim_name(rd), rrdset_name(st), name);
|
|
|
|
STRING *old = rd->name;
|
|
rd->name = rrd_string_strdupz(name);
|
|
string_freez(old);
|
|
|
|
if (!rrdset_is_ar_chart(st))
|
|
rrddimvar_rename_all(rd);
|
|
|
|
rd->exposed = 0;
|
|
rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
|
|
|
|
ml_dimension_update_name(st, rd, name);
|
|
|
|
return 1;
|
|
}
|
|
|
|
inline int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm) {
|
|
if(unlikely(rd->algorithm == algorithm))
|
|
return 0;
|
|
|
|
debug(D_RRD_CALLS, "Updating algorithm of dimension '%s/%s' from %s to %s", rrdset_id(st), rrddim_name(rd), rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(algorithm));
|
|
rd->algorithm = algorithm;
|
|
rd->exposed = 0;
|
|
rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
|
|
rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
|
|
rrdcontext_updated_rrddim_algorithm(rd);
|
|
return 1;
|
|
}
|
|
|
|
inline int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier) {
|
|
if(unlikely(rd->multiplier == multiplier))
|
|
return 0;
|
|
|
|
debug(D_RRD_CALLS, "Updating multiplier of dimension '%s/%s' from " COLLECTED_NUMBER_FORMAT " to " COLLECTED_NUMBER_FORMAT, rrdset_id(st), rrddim_name(rd), rd->multiplier, multiplier);
|
|
rd->multiplier = multiplier;
|
|
rd->exposed = 0;
|
|
rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
|
|
rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
|
|
rrdcontext_updated_rrddim_multiplier(rd);
|
|
return 1;
|
|
}
|
|
|
|
inline int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor) {
|
|
if(unlikely(rd->divisor == divisor))
|
|
return 0;
|
|
|
|
debug(D_RRD_CALLS, "Updating divisor of dimension '%s/%s' from " COLLECTED_NUMBER_FORMAT " to " COLLECTED_NUMBER_FORMAT, rrdset_id(st), rrddim_name(rd), rd->divisor, divisor);
|
|
rd->divisor = divisor;
|
|
rd->exposed = 0;
|
|
rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
|
|
rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
|
|
rrdcontext_updated_rrddim_divisor(rd);
|
|
return 1;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// get the timestamp of the last entry in the round-robin database
|
|
time_t rrddim_last_entry_t(RRDDIM *rd) {
|
|
time_t latest = rd->tiers[0]->query_ops.latest_time(rd->tiers[0]->db_metric_handle);
|
|
|
|
for(int tier = 1; tier < storage_tiers ;tier++) {
|
|
if(unlikely(!rd->tiers[tier])) continue;
|
|
|
|
time_t t = rd->tiers[tier]->query_ops.latest_time(rd->tiers[tier]->db_metric_handle);
|
|
if(t > latest)
|
|
latest = t;
|
|
}
|
|
|
|
return latest;
|
|
}
|
|
|
|
time_t rrddim_first_entry_t(RRDDIM *rd) {
|
|
time_t oldest = 0;
|
|
|
|
for(int tier = 0; tier < storage_tiers ;tier++) {
|
|
if(unlikely(!rd->tiers[tier])) continue;
|
|
|
|
time_t t = rd->tiers[tier]->query_ops.oldest_time(rd->tiers[tier]->db_metric_handle);
|
|
if(t != 0 && (oldest == 0 || t < oldest))
|
|
oldest = t;
|
|
}
|
|
|
|
return oldest;
|
|
}
|
|
|
|
// Return either
|
|
// 0 : Dimension is live
|
|
// last collected time : Dimension is not live
|
|
|
|
#ifdef ENABLE_ACLK
|
|
time_t calc_dimension_liveness(RRDDIM *rd, time_t now)
|
|
{
|
|
time_t last_updated = rd->last_collected_time.tv_sec;
|
|
int live;
|
|
|
|
if(rd->aclk_live_status == 1) {
|
|
int offline_time = RRDSET_MINIMUM_DIM_OFFLINE_MULTIPLIER * rd->update_every;
|
|
live = ((now - last_updated) < MIN(rrdset_free_obsolete_time, offline_time));
|
|
}
|
|
else
|
|
live = ((now - last_updated) < RRDSET_MINIMUM_DIM_LIVE_MULTIPLIER * rd->update_every);
|
|
|
|
return live ? 0 : last_updated;
|
|
}
|
|
#endif
|
|
|
|
RRDDIM *rrddim_add_custom(RRDSET *st
|
|
, const char *id
|
|
, const char *name
|
|
, collected_number multiplier
|
|
, collected_number divisor
|
|
, RRD_ALGORITHM algorithm
|
|
, RRD_MEMORY_MODE memory_mode
|
|
) {
|
|
struct rrddim_constructor tmp = {
|
|
.st = st,
|
|
.id = id,
|
|
.name = name,
|
|
.multiplier = multiplier,
|
|
.divisor = divisor,
|
|
.algorithm = algorithm,
|
|
.memory_mode = memory_mode,
|
|
};
|
|
|
|
RRDDIM *rd = dictionary_set_advanced(st->rrddim_root_index, tmp.id, -1, NULL, sizeof(RRDDIM), &tmp);
|
|
|
|
if(tmp.react_action == RRDDIM_REACT_NEW) {
|
|
// append this dimension
|
|
rrdset_wrlock(st);
|
|
DOUBLE_LINKED_LIST_APPEND_UNSAFE(st->dimensions, rd, prev, next);
|
|
rrdset_unlock(st);
|
|
}
|
|
|
|
return(rd);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDDIM remove / free a dimension
|
|
|
|
void rrddim_free(RRDSET *st, RRDDIM *rd) {
|
|
rrdset_wrlock(st);
|
|
DOUBLE_LINKED_LIST_REMOVE_UNSAFE(st->dimensions, rd, prev, next);
|
|
rrdset_unlock(st);
|
|
|
|
dictionary_del(st->rrddim_root_index, string2str(rd->id));
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDDIM - set dimension options
|
|
|
|
int rrddim_hide(RRDSET *st, const char *id) {
|
|
debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", rrdset_name(st), id);
|
|
|
|
RRDHOST *host = st->rrdhost;
|
|
|
|
RRDDIM *rd = rrddim_find(st, id);
|
|
if(unlikely(!rd)) {
|
|
error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, rrdset_name(st), rrdset_id(st), rrdhost_hostname(host));
|
|
return 1;
|
|
}
|
|
if (!rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN))
|
|
(void)sql_set_dimension_option(&rd->metric_uuid, "hidden");
|
|
|
|
rrddim_option_set(rd, RRDDIM_OPTION_HIDDEN);
|
|
rrddim_flag_set(rd, RRDDIM_FLAG_META_HIDDEN);
|
|
rrdcontext_updated_rrddim_flags(rd);
|
|
return 0;
|
|
}
|
|
|
|
int rrddim_unhide(RRDSET *st, const char *id) {
|
|
debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", rrdset_name(st), id);
|
|
|
|
RRDHOST *host = st->rrdhost;
|
|
RRDDIM *rd = rrddim_find(st, id);
|
|
if(unlikely(!rd)) {
|
|
error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, rrdset_name(st), rrdset_id(st), rrdhost_hostname(host));
|
|
return 1;
|
|
}
|
|
if (rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN))
|
|
(void)sql_set_dimension_option(&rd->metric_uuid, NULL);
|
|
|
|
rrddim_option_clear(rd, RRDDIM_OPTION_HIDDEN);
|
|
rrddim_flag_clear(rd, RRDDIM_FLAG_META_HIDDEN);
|
|
rrdcontext_updated_rrddim_flags(rd);
|
|
return 0;
|
|
}
|
|
|
|
inline void rrddim_is_obsolete(RRDSET *st, RRDDIM *rd) {
|
|
debug(D_RRD_CALLS, "rrddim_is_obsolete() for chart %s, dimension %s", rrdset_name(st), rrddim_name(rd));
|
|
|
|
if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))) {
|
|
info("Cannot obsolete already archived dimension %s from chart %s", rrddim_name(rd), rrdset_name(st));
|
|
return;
|
|
}
|
|
rrddim_flag_set(rd, RRDDIM_FLAG_OBSOLETE);
|
|
rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS);
|
|
rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_PENDING_OBSOLETE_DIMENSIONS);
|
|
rrdcontext_updated_rrddim_flags(rd);
|
|
}
|
|
|
|
inline void rrddim_isnot_obsolete(RRDSET *st __maybe_unused, RRDDIM *rd) {
|
|
debug(D_RRD_CALLS, "rrddim_isnot_obsolete() for chart %s, dimension %s", rrdset_name(st), rrddim_name(rd));
|
|
|
|
rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
|
|
rrdcontext_updated_rrddim_flags(rd);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDDIM - collect values for a dimension
|
|
|
|
inline collected_number rrddim_set_by_pointer(RRDSET *st __maybe_unused, RRDDIM *rd, collected_number value) {
|
|
debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, rrdset_name(st), rrddim_name(rd), value);
|
|
|
|
now_realtime_timeval(&rd->last_collected_time);
|
|
rd->collected_value = value;
|
|
rd->updated = 1;
|
|
|
|
rd->collections_counter++;
|
|
|
|
collected_number v = (value >= 0) ? value : -value;
|
|
if(unlikely(v > rd->collected_value_max)) rd->collected_value_max = v;
|
|
|
|
// fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " NETDATA_DOUBLE_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (NETDATA_DOUBLE)((value - rd->last_collected_value) * (NETDATA_DOUBLE)rd->multiplier / (NETDATA_DOUBLE)rd->divisor * 1000000.0 / (NETDATA_DOUBLE)st->usec_since_last_update));
|
|
|
|
return rd->last_collected_value;
|
|
}
|
|
|
|
collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
|
|
RRDHOST *host = st->rrdhost;
|
|
RRDDIM *rd = rrddim_find(st, id);
|
|
if(unlikely(!rd)) {
|
|
error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, rrdset_name(st), rrdset_id(st), rrdhost_hostname(host));
|
|
return 0;
|
|
}
|
|
|
|
return rrddim_set_by_pointer(st, rd, value);
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// compatibility layer for RRDDIM files v019
|
|
|
|
#define RRDDIMENSION_MAGIC_V019 "NETDATA RRD DIMENSION FILE V019"
|
|
|
|
struct avl_element_v019 {
|
|
void *avl_link[2];
|
|
signed char avl_balance;
|
|
};
|
|
|
|
struct rrddim_map_save_v019 {
|
|
struct avl_element_v019 avl; // ignored
|
|
void *id; // ignored
|
|
void *name; // ignored
|
|
uint32_t algorithm; // print warning on mismatch - update on load
|
|
uint32_t rrd_memory_mode; // ignored
|
|
long long multiplier; // print warning on mismatch - update on load
|
|
long long divisor; // print warning on mismatch - update on load
|
|
uint32_t flags; // ignored
|
|
uint32_t hash; // ignored
|
|
uint32_t hash_name; // ignored
|
|
void *cache_filename; // ignored - we use it to keep the filename to save back
|
|
size_t collections_counter; // ignored
|
|
void *state; // ignored
|
|
size_t unused[8]; // ignored
|
|
long long collected_value_max; // ignored
|
|
unsigned int updated:1; // ignored
|
|
unsigned int exposed:1; // ignored
|
|
struct timeval last_collected_time; // check to reset all - ignored after load
|
|
long double calculated_value; // ignored
|
|
long double last_calculated_value; // ignored
|
|
long double last_stored_value; // ignored
|
|
long long collected_value; // ignored
|
|
long long last_collected_value; // ignored
|
|
long double collected_volume; // ignored
|
|
long double stored_volume; // ignored
|
|
void *next; // ignored
|
|
void *rrdset; // ignored
|
|
long entries; // check to reset all - update on load
|
|
int update_every; // check to reset all - update on load
|
|
size_t memsize; // check to reset all - update on load
|
|
char magic[sizeof(RRDDIMENSION_MAGIC_V019) + 1];// check to reset all - update on load
|
|
void *variables; // ignored
|
|
storage_number values[]; // the array of values
|
|
};
|
|
|
|
size_t rrddim_memory_file_header_size(void) {
|
|
return sizeof(struct rrddim_map_save_v019);
|
|
}
|
|
|
|
void rrddim_memory_file_update(RRDDIM *rd) {
|
|
if(!rd || !rd->rd_on_file) return;
|
|
struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
|
|
|
|
rd_on_file->last_collected_time.tv_sec = rd->last_collected_time.tv_sec;
|
|
rd_on_file->last_collected_time.tv_usec = rd->last_collected_time.tv_usec;
|
|
}
|
|
|
|
void rrddim_memory_file_free(RRDDIM *rd) {
|
|
if(!rd || !rd->rd_on_file) return;
|
|
|
|
// needed for memory mode map, to save the latest state
|
|
rrddim_memory_file_update(rd);
|
|
|
|
struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
|
|
freez(rd_on_file->cache_filename);
|
|
munmap(rd_on_file, rd_on_file->memsize);
|
|
|
|
// remove the pointers from the RRDDIM
|
|
rd->rd_on_file = NULL;
|
|
rd->db = NULL;
|
|
}
|
|
|
|
const char *rrddim_cache_filename(RRDDIM *rd) {
|
|
if(!rd || !rd->rd_on_file) return NULL;
|
|
struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
|
|
return rd_on_file->cache_filename;
|
|
}
|
|
|
|
void rrddim_memory_file_save(RRDDIM *rd) {
|
|
if(!rd || !rd->rd_on_file) return;
|
|
|
|
rrddim_memory_file_update(rd);
|
|
|
|
struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
|
|
if(rd_on_file->rrd_memory_mode != RRD_MEMORY_MODE_SAVE) return;
|
|
|
|
memory_file_save(rd_on_file->cache_filename, rd_on_file, rd_on_file->memsize);
|
|
}
|
|
|
|
bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode) {
|
|
if(memory_mode != RRD_MEMORY_MODE_SAVE && memory_mode != RRD_MEMORY_MODE_MAP)
|
|
return false;
|
|
|
|
struct rrddim_map_save_v019 *rd_on_file = NULL;
|
|
|
|
unsigned long size = sizeof(struct rrddim_map_save_v019) + (st->entries * sizeof(storage_number));
|
|
|
|
char filename[FILENAME_MAX + 1];
|
|
char fullfilename[FILENAME_MAX + 1];
|
|
rrdset_strncpyz_name(filename, rrddim_id(rd), FILENAME_MAX);
|
|
snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
|
|
|
|
rd_on_file = (struct rrddim_map_save_v019 *)netdata_mmap(fullfilename, size,
|
|
((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1);
|
|
|
|
if(unlikely(!rd_on_file)) return false;
|
|
|
|
struct timeval now;
|
|
now_realtime_timeval(&now);
|
|
|
|
int reset = 0;
|
|
rd_on_file->magic[sizeof(RRDDIMENSION_MAGIC_V019)] = '\0';
|
|
if(strcmp(rd_on_file->magic, RRDDIMENSION_MAGIC_V019) != 0) {
|
|
info("Initializing file %s.", fullfilename);
|
|
memset(rd_on_file, 0, size);
|
|
reset = 1;
|
|
}
|
|
else if(rd_on_file->memsize != size) {
|
|
error("File %s does not have the desired size, expected %lu but found %lu. Clearing it.", fullfilename, size, rd_on_file->memsize);
|
|
memset(rd_on_file, 0, size);
|
|
reset = 1;
|
|
}
|
|
else if(rd_on_file->update_every != st->update_every) {
|
|
error("File %s does not have the same update frequency, expected %d but found %d. Clearing it.", fullfilename, st->update_every, rd_on_file->update_every);
|
|
memset(rd_on_file, 0, size);
|
|
reset = 1;
|
|
}
|
|
else if(dt_usec(&now, &rd_on_file->last_collected_time) > (rd_on_file->entries * rd_on_file->update_every * USEC_PER_SEC)) {
|
|
info("File %s is too old (last collected %llu seconds ago, but the database is %ld seconds). Clearing it.", fullfilename, dt_usec(&now, &rd_on_file->last_collected_time) / USEC_PER_SEC, rd_on_file->entries * rd_on_file->update_every);
|
|
memset(rd_on_file, 0, size);
|
|
reset = 1;
|
|
}
|
|
|
|
if(!reset) {
|
|
if(rd_on_file->algorithm != rd->algorithm)
|
|
info("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong.",
|
|
fullfilename, rd->algorithm, rrd_algorithm_name(rd->algorithm), rd_on_file->algorithm, rrd_algorithm_name(rd_on_file->algorithm));
|
|
|
|
if(rd_on_file->multiplier != rd->multiplier)
|
|
info("File %s does not have the expected multiplier (expected " COLLECTED_NUMBER_FORMAT ", found " COLLECTED_NUMBER_FORMAT "). Previous values may be wrong.", fullfilename, rd->multiplier, rd_on_file->multiplier);
|
|
|
|
if(rd_on_file->divisor != rd->divisor)
|
|
info("File %s does not have the expected divisor (expected " COLLECTED_NUMBER_FORMAT ", found " COLLECTED_NUMBER_FORMAT "). Previous values may be wrong.", fullfilename, rd->divisor, rd_on_file->divisor);
|
|
}
|
|
|
|
// zero the entire header
|
|
memset(rd_on_file, 0, sizeof(struct rrddim_map_save_v019));
|
|
|
|
// set the important fields
|
|
strcpy(rd_on_file->magic, RRDDIMENSION_MAGIC_V019);
|
|
rd_on_file->algorithm = rd->algorithm;
|
|
rd_on_file->multiplier = rd->multiplier;
|
|
rd_on_file->divisor = rd->divisor;
|
|
rd_on_file->entries = st->entries;
|
|
rd_on_file->update_every = rd->update_every;
|
|
rd_on_file->memsize = size;
|
|
rd_on_file->rrd_memory_mode = memory_mode;
|
|
rd_on_file->cache_filename = strdupz(fullfilename);
|
|
|
|
rd->db = &rd_on_file->values[0];
|
|
rd->rd_on_file = rd_on_file;
|
|
rd->memsize = size;
|
|
rrddim_memory_file_update(rd);
|
|
|
|
return true;
|
|
}
|