rrd.h 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRD_H
  3. #define NETDATA_RRD_H 1
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. // non-existing structs instead of voids
  8. // to enable type checking at compile time
  9. typedef struct storage_instance STORAGE_INSTANCE;
  10. typedef struct storage_metric_handle STORAGE_METRIC_HANDLE;
  11. typedef struct storage_alignment STORAGE_METRICS_GROUP;
  12. // forward typedefs
  13. typedef struct rrdhost RRDHOST;
  14. typedef struct rrddim RRDDIM;
  15. typedef struct rrdset RRDSET;
  16. typedef struct rrdcalc RRDCALC;
  17. typedef struct rrdcalctemplate RRDCALCTEMPLATE;
  18. typedef struct alarm_entry ALARM_ENTRY;
  19. typedef struct rrdfamily_acquired RRDFAMILY_ACQUIRED;
  20. typedef struct rrdvar_acquired RRDVAR_ACQUIRED;
  21. typedef struct rrdsetvar_acquired RRDSETVAR_ACQUIRED;
  22. typedef struct rrdcalc_acquired RRDCALC_ACQUIRED;
  23. typedef struct rrdhost_acquired RRDHOST_ACQUIRED;
  24. typedef struct rrdset_acquired RRDSET_ACQUIRED;
  25. typedef struct rrddim_acquired RRDDIM_ACQUIRED;
  26. typedef struct ml_host rrd_ml_host_t;
  27. typedef struct ml_chart rrd_ml_chart_t;
  28. typedef struct ml_dimension rrd_ml_dimension_t;
  29. typedef enum __attribute__ ((__packed__)) {
  30. QUERY_SOURCE_UNKNOWN = 0,
  31. QUERY_SOURCE_API_DATA,
  32. QUERY_SOURCE_API_BADGE,
  33. QUERY_SOURCE_API_WEIGHTS,
  34. QUERY_SOURCE_HEALTH,
  35. QUERY_SOURCE_ML,
  36. QUERY_SOURCE_UNITTEST,
  37. } QUERY_SOURCE;
  38. typedef enum __attribute__ ((__packed__)) storage_priority {
  39. STORAGE_PRIORITY_INTERNAL_DBENGINE = 0,
  40. STORAGE_PRIORITY_INTERNAL_QUERY_PREP,
  41. // query priorities
  42. STORAGE_PRIORITY_HIGH,
  43. STORAGE_PRIORITY_NORMAL,
  44. STORAGE_PRIORITY_LOW,
  45. STORAGE_PRIORITY_BEST_EFFORT,
  46. // synchronous query, not to be dispatched to workers or queued
  47. STORAGE_PRIORITY_SYNCHRONOUS,
  48. STORAGE_PRIORITY_INTERNAL_MAX_DONT_USE,
  49. } STORAGE_PRIORITY;
  50. // forward declarations
  51. struct rrddim_tier;
  52. #ifdef ENABLE_DBENGINE
  53. struct rrdeng_page_descr;
  54. struct rrdengine_instance;
  55. struct pg_cache_page_index;
  56. #endif
  57. // ----------------------------------------------------------------------------
  58. // memory mode
  59. typedef enum __attribute__ ((__packed__)) rrd_memory_mode {
  60. RRD_MEMORY_MODE_NONE = 0,
  61. RRD_MEMORY_MODE_RAM = 1,
  62. RRD_MEMORY_MODE_MAP = 2,
  63. RRD_MEMORY_MODE_SAVE = 3,
  64. RRD_MEMORY_MODE_ALLOC = 4,
  65. RRD_MEMORY_MODE_DBENGINE = 5,
  66. // this is 8-bit
  67. } RRD_MEMORY_MODE;
  68. #define RRD_MEMORY_MODE_NONE_NAME "none"
  69. #define RRD_MEMORY_MODE_RAM_NAME "ram"
  70. #define RRD_MEMORY_MODE_MAP_NAME "map"
  71. #define RRD_MEMORY_MODE_SAVE_NAME "save"
  72. #define RRD_MEMORY_MODE_ALLOC_NAME "alloc"
  73. #define RRD_MEMORY_MODE_DBENGINE_NAME "dbengine"
  74. extern RRD_MEMORY_MODE default_rrd_memory_mode;
  75. const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
  76. RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
  77. #include "daemon/common.h"
  78. #include "web/api/queries/query.h"
  79. #include "web/api/queries/rrdr.h"
  80. #include "rrdvar.h"
  81. #include "rrdsetvar.h"
  82. #include "rrddimvar.h"
  83. #include "rrdcalc.h"
  84. #include "rrdcalctemplate.h"
  85. #include "streaming/rrdpush.h"
  86. #include "aclk/aclk_rrdhost_state.h"
  87. #include "sqlite/sqlite_health.h"
  88. typedef struct storage_query_handle STORAGE_QUERY_HANDLE;
  89. typedef enum __attribute__ ((__packed__)) {
  90. STORAGE_ENGINE_BACKEND_RRDDIM = 1,
  91. STORAGE_ENGINE_BACKEND_DBENGINE = 2,
  92. } STORAGE_ENGINE_BACKEND;
  93. #define is_valid_backend(backend) ((backend) >= STORAGE_ENGINE_BACKEND_RRDDIM && (backend) <= STORAGE_ENGINE_BACKEND_DBENGINE)
  94. // iterator state for RRD dimension data queries
  95. struct storage_engine_query_handle {
  96. time_t start_time_s;
  97. time_t end_time_s;
  98. STORAGE_PRIORITY priority;
  99. STORAGE_ENGINE_BACKEND backend;
  100. STORAGE_QUERY_HANDLE *handle;
  101. };
  102. // ----------------------------------------------------------------------------
  103. // chart types
  104. typedef enum __attribute__ ((__packed__)) rrdset_type {
  105. RRDSET_TYPE_LINE = 0,
  106. RRDSET_TYPE_AREA = 1,
  107. RRDSET_TYPE_STACKED = 2,
  108. } RRDSET_TYPE;
  109. #define RRDSET_TYPE_LINE_NAME "line"
  110. #define RRDSET_TYPE_AREA_NAME "area"
  111. #define RRDSET_TYPE_STACKED_NAME "stacked"
  112. RRDSET_TYPE rrdset_type_id(const char *name);
  113. const char *rrdset_type_name(RRDSET_TYPE chart_type);
  114. #include "contexts/rrdcontext.h"
  115. extern bool unittest_running;
  116. extern bool dbengine_enabled;
  117. extern size_t storage_tiers;
  118. extern bool use_direct_io;
  119. extern size_t storage_tiers_grouping_iterations[RRD_STORAGE_TIERS];
  120. typedef enum __attribute__ ((__packed__)) {
  121. RRD_BACKFILL_NONE = 0,
  122. RRD_BACKFILL_FULL,
  123. RRD_BACKFILL_NEW
  124. } RRD_BACKFILL;
  125. extern RRD_BACKFILL storage_tiers_backfill[RRD_STORAGE_TIERS];
  126. #define UPDATE_EVERY 1
  127. #define UPDATE_EVERY_MAX 3600
  128. #define RRD_DEFAULT_HISTORY_ENTRIES 3600
  129. #define RRD_HISTORY_ENTRIES_MAX (86400*365)
  130. extern int default_rrd_update_every;
  131. extern int default_rrd_history_entries;
  132. extern int gap_when_lost_iterations_above;
  133. extern time_t rrdset_free_obsolete_time_s;
  134. #if defined(ENV32BIT)
  135. #define MIN_LIBUV_WORKER_THREADS 8
  136. #define MAX_LIBUV_WORKER_THREADS 128
  137. #define RESERVED_LIBUV_WORKER_THREADS 3
  138. #else
  139. #define MIN_LIBUV_WORKER_THREADS 16
  140. #define MAX_LIBUV_WORKER_THREADS 1024
  141. #define RESERVED_LIBUV_WORKER_THREADS 6
  142. #endif
  143. extern int libuv_worker_threads;
  144. extern bool ieee754_doubles;
  145. #define RRD_ID_LENGTH_MAX 1000
  146. typedef long long total_number;
  147. #define TOTAL_NUMBER_FORMAT "%lld"
  148. // ----------------------------------------------------------------------------
  149. // algorithms types
  150. typedef enum __attribute__ ((__packed__)) rrd_algorithm {
  151. RRD_ALGORITHM_ABSOLUTE = 0,
  152. RRD_ALGORITHM_INCREMENTAL = 1,
  153. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
  154. RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3,
  155. // this is 8-bit
  156. } RRD_ALGORITHM;
  157. #define RRD_ALGORITHM_ABSOLUTE_NAME "absolute"
  158. #define RRD_ALGORITHM_INCREMENTAL_NAME "incremental"
  159. #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
  160. #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
  161. RRD_ALGORITHM rrd_algorithm_id(const char *name);
  162. const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
  163. // ----------------------------------------------------------------------------
  164. // RRD FAMILY
  165. const RRDFAMILY_ACQUIRED *rrdfamily_add_and_acquire(RRDHOST *host, const char *id);
  166. void rrdfamily_release(RRDHOST *host, const RRDFAMILY_ACQUIRED *rfa);
  167. void rrdfamily_index_init(RRDHOST *host);
  168. void rrdfamily_index_destroy(RRDHOST *host);
  169. DICTIONARY *rrdfamily_rrdvars_dict(const RRDFAMILY_ACQUIRED *rf);
  170. // ----------------------------------------------------------------------------
  171. // flags & options
  172. // options are permanent configuration options (no atomics to alter/access them)
  173. typedef enum __attribute__ ((__packed__)) rrddim_options {
  174. RRDDIM_OPTION_NONE = 0,
  175. RRDDIM_OPTION_HIDDEN = (1 << 0), // this dimension will not be offered to callers
  176. RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS = (1 << 1), // do not offer RESET or OVERFLOW info to callers
  177. RRDDIM_OPTION_BACKFILLED_HIGH_TIERS = (1 << 2), // when set, we have backfilled higher tiers
  178. // this is 8-bit
  179. } RRDDIM_OPTIONS;
  180. #define rrddim_option_check(rd, option) ((rd)->options & (option))
  181. #define rrddim_option_set(rd, option) (rd)->options |= (option)
  182. #define rrddim_option_clear(rd, option) (rd)->options &= ~(option)
  183. // flags are runtime changing status flags (atomics are required to alter/access them)
  184. typedef enum __attribute__ ((__packed__)) rrddim_flags {
  185. RRDDIM_FLAG_NONE = 0,
  186. RRDDIM_FLAG_PENDING_HEALTH_INITIALIZATION = (1 << 0),
  187. RRDDIM_FLAG_OBSOLETE = (1 << 2), // this is marked by the collector/module as obsolete
  188. // No new values have been collected for this dimension since agent start, or it was marked RRDDIM_FLAG_OBSOLETE at
  189. // least rrdset_free_obsolete_time seconds ago.
  190. RRDDIM_FLAG_ARCHIVED = (1 << 3),
  191. RRDDIM_FLAG_METADATA_UPDATE = (1 << 4), // Metadata needs to go to the database
  192. RRDDIM_FLAG_META_HIDDEN = (1 << 6), // Status of hidden option in the metadata database
  193. // this is 8 bit
  194. } RRDDIM_FLAGS;
  195. #define rrddim_flag_check(rd, flag) (__atomic_load_n(&((rd)->flags), __ATOMIC_SEQ_CST) & (flag))
  196. #define rrddim_flag_set(rd, flag) __atomic_or_fetch(&((rd)->flags), (flag), __ATOMIC_SEQ_CST)
  197. #define rrddim_flag_clear(rd, flag) __atomic_and_fetch(&((rd)->flags), ~(flag), __ATOMIC_SEQ_CST)
  198. typedef enum __attribute__ ((__packed__)) rrdlabel_source {
  199. RRDLABEL_SRC_AUTO = (1 << 0), // set when Netdata found the label by some automation
  200. RRDLABEL_SRC_CONFIG = (1 << 1), // set when the user configured the label
  201. RRDLABEL_SRC_K8S = (1 << 2), // set when this label is found from k8s (RRDLABEL_SRC_AUTO should also be set)
  202. RRDLABEL_SRC_ACLK = (1 << 3), // set when this label is found from ACLK (RRDLABEL_SRC_AUTO should also be set)
  203. // more sources can be added here
  204. RRDLABEL_FLAG_PERMANENT = (1 << 29), // set when this label should never be removed (can be overwritten though)
  205. RRDLABEL_FLAG_OLD = (1 << 30), // marks for rrdlabels internal use - they are not exposed outside rrdlabels
  206. RRDLABEL_FLAG_NEW = (1 << 31) // marks for rrdlabels internal use - they are not exposed outside rrdlabels
  207. } RRDLABEL_SRC;
  208. #define RRDLABEL_FLAG_INTERNAL (RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_PERMANENT)
  209. size_t text_sanitize(unsigned char *dst, const unsigned char *src, size_t dst_size, unsigned char *char_map, bool utf, const char *empty, size_t *multibyte_length);
  210. DICTIONARY *rrdlabels_create(void);
  211. void rrdlabels_destroy(DICTIONARY *labels_dict);
  212. void rrdlabels_add(DICTIONARY *dict, const char *name, const char *value, RRDLABEL_SRC ls);
  213. void rrdlabels_add_pair(DICTIONARY *dict, const char *string, RRDLABEL_SRC ls);
  214. void rrdlabels_get_value_to_buffer_or_null(DICTIONARY *labels, BUFFER *wb, const char *key, const char *quote, const char *null);
  215. void rrdlabels_value_to_buffer_array_item_or_null(DICTIONARY *labels, BUFFER *wb, const char *key);
  216. void rrdlabels_get_value_strdup_or_null(DICTIONARY *labels, char **value, const char *key);
  217. void rrdlabels_get_value_strcpyz(DICTIONARY *labels, char *dst, size_t dst_len, const char *key);
  218. STRING *rrdlabels_get_value_string_dup(DICTIONARY *labels, const char *key);
  219. STRING *rrdlabels_get_value_to_buffer_or_unset(DICTIONARY *labels, BUFFER *wb, const char *key, const char *unset);
  220. void rrdlabels_flush(DICTIONARY *labels_dict);
  221. void rrdlabels_unmark_all(DICTIONARY *labels);
  222. void rrdlabels_remove_all_unmarked(DICTIONARY *labels);
  223. int rrdlabels_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data);
  224. int rrdlabels_sorted_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data);
  225. void rrdlabels_log_to_buffer(DICTIONARY *labels, BUFFER *wb);
  226. bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_pattern_txt);
  227. bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal, size_t *searches);
  228. int rrdlabels_to_buffer(DICTIONARY *labels, BUFFER *wb, const char *before_each, const char *equal, const char *quote, const char *between_them, bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *filter_data, void (*name_sanitizer)(char *dst, const char *src, size_t dst_size), void (*value_sanitizer)(char *dst, const char *src, size_t dst_size));
  229. void rrdlabels_to_buffer_json_members(DICTIONARY *labels, BUFFER *wb);
  230. void rrdlabels_migrate_to_these(DICTIONARY *dst, DICTIONARY *src);
  231. void rrdlabels_copy(DICTIONARY *dst, DICTIONARY *src);
  232. void reload_host_labels(void);
  233. void rrdset_update_rrdlabels(RRDSET *st, DICTIONARY *new_rrdlabels);
  234. void rrdset_save_rrdlabels_to_sql(RRDSET *st);
  235. void rrdhost_set_is_parent_label(int count);
  236. int rrdlabels_unittest(void);
  237. // unfortunately this break when defined in exporting_engine.h
  238. bool exporting_labels_filter_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  239. // ----------------------------------------------------------------------------
  240. // engine-specific iterator state for dimension data collection
  241. typedef struct storage_collect_handle {
  242. STORAGE_ENGINE_BACKEND backend;
  243. } STORAGE_COLLECT_HANDLE;
  244. // ----------------------------------------------------------------------------
  245. // Storage tier data for every dimension
  246. struct rrddim_tier {
  247. STORAGE_POINT virtual_point;
  248. STORAGE_ENGINE_BACKEND backend;
  249. uint32_t tier_grouping;
  250. time_t next_point_end_time_s;
  251. STORAGE_METRIC_HANDLE *db_metric_handle; // the metric handle inside the database
  252. STORAGE_COLLECT_HANDLE *db_collection_handle; // the data collection handle
  253. };
  254. void rrdr_fill_tier_gap_from_smaller_tiers(RRDDIM *rd, size_t tier, time_t now_s);
  255. // ----------------------------------------------------------------------------
  256. // RRD DIMENSION - this is a metric
  257. struct rrddim {
  258. uuid_t metric_uuid; // global UUID for this metric (unique_across hosts)
  259. // ------------------------------------------------------------------------
  260. // dimension definition
  261. STRING *id; // the id of this dimension (for internal identification)
  262. STRING *name; // the name of this dimension (as presented to user)
  263. RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
  264. RRDDIM_OPTIONS options; // permanent configuration options
  265. RRD_MEMORY_MODE rrd_memory_mode; // the memory mode for this dimension
  266. RRDDIM_FLAGS flags; // run time changing status flags
  267. bool updated; // 1 when the dimension has been updated since the last processing
  268. bool exposed; // 1 when set what have sent this dimension to the central netdata
  269. collected_number multiplier; // the multiplier of the collected values
  270. collected_number divisor; // the divider of the collected values
  271. int update_every; // every how many seconds is this updated
  272. // TODO - remove update_every from rrddim
  273. // it is always the same in rrdset
  274. // ------------------------------------------------------------------------
  275. // operational state members
  276. rrd_ml_dimension_t *ml_dimension; // machine learning data about this dimension
  277. // ------------------------------------------------------------------------
  278. // linking to siblings and parents
  279. struct rrdset *rrdset;
  280. RRDMETRIC_ACQUIRED *rrdmetric; // the rrdmetric of this dimension
  281. // ------------------------------------------------------------------------
  282. // data collection members
  283. struct rrddim_tier tiers[RRD_STORAGE_TIERS]; // our tiers of databases
  284. struct timeval last_collected_time; // when was this dimension last updated
  285. // this is actual date time we updated the last_collected_value
  286. // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
  287. size_t collections_counter; // the number of times we added values to this rrddim
  288. collected_number collected_value_max; // the absolute maximum of the collected value
  289. NETDATA_DOUBLE calculated_value; // the current calculated value, after applying the algorithm - resets to zero after being used
  290. NETDATA_DOUBLE last_calculated_value; // the last calculated value processed
  291. NETDATA_DOUBLE last_stored_value; // the last value as stored in the database (after interpolation)
  292. collected_number collected_value; // the current value, as collected - resets to 0 after being used
  293. collected_number last_collected_value; // the last value that was collected, after being processed
  294. #ifdef NETDATA_LOG_COLLECTION_ERRORS
  295. usec_t rrddim_store_metric_last_ut; // the timestamp we last called rrddim_store_metric()
  296. size_t rrddim_store_metric_count; // the rrddim_store_metric() counter
  297. const char *rrddim_store_metric_last_caller; // the name of the function that last called rrddim_store_metric()
  298. #endif
  299. // ------------------------------------------------------------------------
  300. // db mode RAM, SAVE, MAP, ALLOC, NONE specifics
  301. // TODO - they should be managed by storage engine
  302. // (RRDDIM_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  303. size_t memsize; // the memory allocated for this dimension (without RRDDIM)
  304. void *rd_on_file; // pointer to the header written on disk
  305. storage_number *db; // the array of values
  306. };
  307. #define rrddim_id(rd) string2str((rd)->id)
  308. #define rrddim_name(rd) string2str((rd) ->name)
  309. // returns the RRDDIM cache filename, or NULL if it does not exist
  310. const char *rrddim_cache_filename(RRDDIM *rd);
  311. // updated the header with the latest RRDDIM value, for memory mode MAP and SAVE
  312. void rrddim_memory_file_update(RRDDIM *rd);
  313. // free the memory file structures for memory mode MAP and SAVE
  314. void rrddim_memory_file_free(RRDDIM *rd);
  315. bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode);
  316. // return the v019 header size of RRDDIM files
  317. size_t rrddim_memory_file_header_size(void);
  318. void rrddim_memory_file_save(RRDDIM *rd);
  319. // ------------------------------------------------------------------------
  320. // DATA COLLECTION STORAGE OPS
  321. STORAGE_METRICS_GROUP *rrdeng_metrics_group_get(STORAGE_INSTANCE *db_instance, uuid_t *uuid);
  322. STORAGE_METRICS_GROUP *rrddim_metrics_group_get(STORAGE_INSTANCE *db_instance, uuid_t *uuid);
  323. static inline STORAGE_METRICS_GROUP *storage_engine_metrics_group_get(STORAGE_ENGINE_BACKEND backend __maybe_unused, STORAGE_INSTANCE *db_instance, uuid_t *uuid) {
  324. internal_fatal(!is_valid_backend(backend), "STORAGE: invalid backend");
  325. #ifdef ENABLE_DBENGINE
  326. if(likely(backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  327. return rrdeng_metrics_group_get(db_instance, uuid);
  328. #endif
  329. return rrddim_metrics_group_get(db_instance, uuid);
  330. }
  331. void rrdeng_metrics_group_release(STORAGE_INSTANCE *db_instance, STORAGE_METRICS_GROUP *smg);
  332. void rrddim_metrics_group_release(STORAGE_INSTANCE *db_instance, STORAGE_METRICS_GROUP *smg);
  333. static inline void storage_engine_metrics_group_release(STORAGE_ENGINE_BACKEND backend __maybe_unused, STORAGE_INSTANCE *db_instance, STORAGE_METRICS_GROUP *smg) {
  334. internal_fatal(!is_valid_backend(backend), "STORAGE: invalid backend");
  335. #ifdef ENABLE_DBENGINE
  336. if(likely(backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  337. rrdeng_metrics_group_release(db_instance, smg);
  338. else
  339. #endif
  340. rrddim_metrics_group_release(db_instance, smg);
  341. }
  342. STORAGE_COLLECT_HANDLE *rrdeng_store_metric_init(STORAGE_METRIC_HANDLE *db_metric_handle, uint32_t update_every, STORAGE_METRICS_GROUP *smg);
  343. STORAGE_COLLECT_HANDLE *rrddim_collect_init(STORAGE_METRIC_HANDLE *db_metric_handle, uint32_t update_every, STORAGE_METRICS_GROUP *smg);
  344. static inline STORAGE_COLLECT_HANDLE *storage_metric_store_init(STORAGE_ENGINE_BACKEND backend __maybe_unused, STORAGE_METRIC_HANDLE *db_metric_handle, uint32_t update_every, STORAGE_METRICS_GROUP *smg) {
  345. internal_fatal(!is_valid_backend(backend), "STORAGE: invalid backend");
  346. #ifdef ENABLE_DBENGINE
  347. if(likely(backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  348. return rrdeng_store_metric_init(db_metric_handle, update_every, smg);
  349. #endif
  350. return rrddim_collect_init(db_metric_handle, update_every, smg);
  351. }
  352. void rrdeng_store_metric_next(
  353. STORAGE_COLLECT_HANDLE *collection_handle, usec_t point_in_time_ut,
  354. NETDATA_DOUBLE n, NETDATA_DOUBLE min_value, NETDATA_DOUBLE max_value,
  355. uint16_t count, uint16_t anomaly_count, SN_FLAGS flags);
  356. void rrddim_collect_store_metric(
  357. STORAGE_COLLECT_HANDLE *collection_handle, usec_t point_in_time_ut,
  358. NETDATA_DOUBLE n, NETDATA_DOUBLE min_value, NETDATA_DOUBLE max_value,
  359. uint16_t count, uint16_t anomaly_count, SN_FLAGS flags);
  360. static inline void storage_engine_store_metric(
  361. STORAGE_COLLECT_HANDLE *collection_handle, usec_t point_in_time_ut,
  362. NETDATA_DOUBLE n, NETDATA_DOUBLE min_value, NETDATA_DOUBLE max_value,
  363. uint16_t count, uint16_t anomaly_count, SN_FLAGS flags) {
  364. internal_fatal(!is_valid_backend(collection_handle->backend), "STORAGE: invalid backend");
  365. #ifdef ENABLE_DBENGINE
  366. if(likely(collection_handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  367. return rrdeng_store_metric_next(collection_handle, point_in_time_ut,
  368. n, min_value, max_value,
  369. count, anomaly_count, flags);
  370. #endif
  371. return rrddim_collect_store_metric(collection_handle, point_in_time_ut,
  372. n, min_value, max_value,
  373. count, anomaly_count, flags);
  374. }
  375. void rrdeng_store_metric_flush_current_page(STORAGE_COLLECT_HANDLE *collection_handle);
  376. void rrddim_store_metric_flush(STORAGE_COLLECT_HANDLE *collection_handle);
  377. static inline void storage_engine_store_flush(STORAGE_COLLECT_HANDLE *collection_handle) {
  378. if(unlikely(!collection_handle))
  379. return;
  380. internal_fatal(!is_valid_backend(collection_handle->backend), "STORAGE: invalid backend");
  381. #ifdef ENABLE_DBENGINE
  382. if(likely(collection_handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  383. rrdeng_store_metric_flush_current_page(collection_handle);
  384. else
  385. #endif
  386. rrddim_store_metric_flush(collection_handle);
  387. }
  388. int rrdeng_store_metric_finalize(STORAGE_COLLECT_HANDLE *collection_handle);
  389. int rrddim_collect_finalize(STORAGE_COLLECT_HANDLE *collection_handle);
  390. // a finalization function to run after collection is over
  391. // returns 1 if it's safe to delete the dimension
  392. static inline int storage_engine_store_finalize(STORAGE_COLLECT_HANDLE *collection_handle) {
  393. internal_fatal(!is_valid_backend(collection_handle->backend), "STORAGE: invalid backend");
  394. #ifdef ENABLE_DBENGINE
  395. if(likely(collection_handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  396. return rrdeng_store_metric_finalize(collection_handle);
  397. #endif
  398. return rrddim_collect_finalize(collection_handle);
  399. }
  400. void rrdeng_store_metric_change_collection_frequency(STORAGE_COLLECT_HANDLE *collection_handle, int update_every);
  401. void rrddim_store_metric_change_collection_frequency(STORAGE_COLLECT_HANDLE *collection_handle, int update_every);
  402. static inline void storage_engine_store_change_collection_frequency(STORAGE_COLLECT_HANDLE *collection_handle, int update_every) {
  403. internal_fatal(!is_valid_backend(collection_handle->backend), "STORAGE: invalid backend");
  404. #ifdef ENABLE_DBENGINE
  405. if(likely(collection_handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  406. rrdeng_store_metric_change_collection_frequency(collection_handle, update_every);
  407. else
  408. #endif
  409. rrddim_store_metric_change_collection_frequency(collection_handle, update_every);
  410. }
  411. // ----------------------------------------------------------------------------
  412. // STORAGE ENGINE QUERY OPS
  413. time_t rrdeng_metric_oldest_time(STORAGE_METRIC_HANDLE *db_metric_handle);
  414. time_t rrddim_query_oldest_time_s(STORAGE_METRIC_HANDLE *db_metric_handle);
  415. static inline time_t storage_engine_oldest_time_s(STORAGE_ENGINE_BACKEND backend __maybe_unused, STORAGE_METRIC_HANDLE *db_metric_handle) {
  416. internal_fatal(!is_valid_backend(backend), "STORAGE: invalid backend");
  417. #ifdef ENABLE_DBENGINE
  418. if(likely(backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  419. return rrdeng_metric_oldest_time(db_metric_handle);
  420. #endif
  421. return rrddim_query_oldest_time_s(db_metric_handle);
  422. }
  423. time_t rrdeng_metric_latest_time(STORAGE_METRIC_HANDLE *db_metric_handle);
  424. time_t rrddim_query_latest_time_s(STORAGE_METRIC_HANDLE *db_metric_handle);
  425. static inline time_t storage_engine_latest_time_s(STORAGE_ENGINE_BACKEND backend __maybe_unused, STORAGE_METRIC_HANDLE *db_metric_handle) {
  426. internal_fatal(!is_valid_backend(backend), "STORAGE: invalid backend");
  427. #ifdef ENABLE_DBENGINE
  428. if(likely(backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  429. return rrdeng_metric_latest_time(db_metric_handle);
  430. #endif
  431. return rrddim_query_latest_time_s(db_metric_handle);
  432. }
  433. void rrdeng_load_metric_init(
  434. STORAGE_METRIC_HANDLE *db_metric_handle, struct storage_engine_query_handle *rrddim_handle,
  435. time_t start_time_s, time_t end_time_s, STORAGE_PRIORITY priority);
  436. void rrddim_query_init(
  437. STORAGE_METRIC_HANDLE *db_metric_handle, struct storage_engine_query_handle *handle,
  438. time_t start_time_s, time_t end_time_s, STORAGE_PRIORITY priority);
  439. static inline void storage_engine_query_init(
  440. STORAGE_ENGINE_BACKEND backend __maybe_unused,
  441. STORAGE_METRIC_HANDLE *db_metric_handle, struct storage_engine_query_handle *handle,
  442. time_t start_time_s, time_t end_time_s, STORAGE_PRIORITY priority) {
  443. internal_fatal(!is_valid_backend(backend), "STORAGE: invalid backend");
  444. #ifdef ENABLE_DBENGINE
  445. if(likely(backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  446. rrdeng_load_metric_init(db_metric_handle, handle, start_time_s, end_time_s, priority);
  447. else
  448. #endif
  449. rrddim_query_init(db_metric_handle, handle, start_time_s, end_time_s, priority);
  450. }
  451. STORAGE_POINT rrdeng_load_metric_next(struct storage_engine_query_handle *rrddim_handle);
  452. STORAGE_POINT rrddim_query_next_metric(struct storage_engine_query_handle *handle);
  453. static inline STORAGE_POINT storage_engine_query_next_metric(struct storage_engine_query_handle *handle) {
  454. internal_fatal(!is_valid_backend(handle->backend), "STORAGE: invalid backend");
  455. #ifdef ENABLE_DBENGINE
  456. if(likely(handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  457. return rrdeng_load_metric_next(handle);
  458. #endif
  459. return rrddim_query_next_metric(handle);
  460. }
  461. int rrdeng_load_metric_is_finished(struct storage_engine_query_handle *rrddim_handle);
  462. int rrddim_query_is_finished(struct storage_engine_query_handle *handle);
  463. static inline int storage_engine_query_is_finished(struct storage_engine_query_handle *handle) {
  464. internal_fatal(!is_valid_backend(handle->backend), "STORAGE: invalid backend");
  465. #ifdef ENABLE_DBENGINE
  466. if(likely(handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  467. return rrdeng_load_metric_is_finished(handle);
  468. #endif
  469. return rrddim_query_is_finished(handle);
  470. }
  471. void rrdeng_load_metric_finalize(struct storage_engine_query_handle *rrddim_handle);
  472. void rrddim_query_finalize(struct storage_engine_query_handle *handle);
  473. static inline void storage_engine_query_finalize(struct storage_engine_query_handle *handle) {
  474. internal_fatal(!is_valid_backend(handle->backend), "STORAGE: invalid backend");
  475. #ifdef ENABLE_DBENGINE
  476. if(likely(handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  477. rrdeng_load_metric_finalize(handle);
  478. else
  479. #endif
  480. rrddim_query_finalize(handle);
  481. }
  482. time_t rrdeng_load_align_to_optimal_before(struct storage_engine_query_handle *rrddim_handle);
  483. time_t rrddim_query_align_to_optimal_before(struct storage_engine_query_handle *rrddim_handle);
  484. static inline time_t storage_engine_align_to_optimal_before(struct storage_engine_query_handle *handle) {
  485. internal_fatal(!is_valid_backend(handle->backend), "STORAGE: invalid backend");
  486. #ifdef ENABLE_DBENGINE
  487. if(likely(handle->backend == STORAGE_ENGINE_BACKEND_DBENGINE))
  488. return rrdeng_load_align_to_optimal_before(handle);
  489. #endif
  490. return rrddim_query_align_to_optimal_before(handle);
  491. }
  492. // ------------------------------------------------------------------------
  493. // function pointers for all APIs provided by a storage engine
  494. typedef struct storage_engine_api {
  495. // metric management
  496. STORAGE_METRIC_HANDLE *(*metric_get)(STORAGE_INSTANCE *instance, uuid_t *uuid);
  497. STORAGE_METRIC_HANDLE *(*metric_get_or_create)(RRDDIM *rd, STORAGE_INSTANCE *instance);
  498. void (*metric_release)(STORAGE_METRIC_HANDLE *);
  499. STORAGE_METRIC_HANDLE *(*metric_dup)(STORAGE_METRIC_HANDLE *);
  500. bool (*metric_retention_by_uuid)(STORAGE_INSTANCE *db_instance, uuid_t *uuid, time_t *first_entry_s, time_t *last_entry_s);
  501. } STORAGE_ENGINE_API;
  502. typedef struct storage_engine {
  503. STORAGE_ENGINE_BACKEND backend;
  504. RRD_MEMORY_MODE id;
  505. const char* name;
  506. STORAGE_ENGINE_API api;
  507. } STORAGE_ENGINE;
  508. STORAGE_ENGINE* storage_engine_get(RRD_MEMORY_MODE mmode);
  509. STORAGE_ENGINE* storage_engine_find(const char* name);
  510. // ----------------------------------------------------------------------------
  511. // these loop macros make sure the linked list is accessed with the right lock
  512. #define rrddim_foreach_read(rd, st) \
  513. dfe_start_read((st)->rrddim_root_index, rd)
  514. #define rrddim_foreach_write(rd, st) \
  515. dfe_start_write((st)->rrddim_root_index, rd)
  516. #define rrddim_foreach_reentrant(rd, st) \
  517. dfe_start_reentrant((st)->rrddim_root_index, rd)
  518. #define rrddim_foreach_done(rd) \
  519. dfe_done(rd)
  520. // ----------------------------------------------------------------------------
  521. // RRDSET - this is a chart
  522. // use this for configuration flags, not for state control
  523. // flags are set/unset in a manner that is not thread safe
  524. // and may lead to missing information.
  525. typedef enum __attribute__ ((__packed__)) rrdset_flags {
  526. RRDSET_FLAG_DETAIL = (1 << 1), // if set, the data set should be considered as a detail of another
  527. // (the master data set should be the one that has the same family and is not detail)
  528. RRDSET_FLAG_DEBUG = (1 << 2), // enables or disables debugging for a chart
  529. RRDSET_FLAG_OBSOLETE = (1 << 3), // this is marked by the collector/module as obsolete
  530. RRDSET_FLAG_EXPORTING_SEND = (1 << 4), // if set, this chart should be sent to Prometheus web API and external databases
  531. RRDSET_FLAG_EXPORTING_IGNORE = (1 << 5), // if set, this chart should not be sent to Prometheus web API and external databases
  532. RRDSET_FLAG_UPSTREAM_SEND = (1 << 6), // if set, this chart should be sent upstream (streaming)
  533. RRDSET_FLAG_UPSTREAM_IGNORE = (1 << 7), // if set, this chart should not be sent upstream (streaming)
  534. RRDSET_FLAG_UPSTREAM_EXPOSED = (1 << 8), // if set, we have sent this chart definition to netdata parent (streaming)
  535. RRDSET_FLAG_STORE_FIRST = (1 << 9), // if set, do not eliminate the first collection during interpolation
  536. RRDSET_FLAG_HETEROGENEOUS = (1 << 10), // if set, the chart is not homogeneous (dimensions in it have multiple algorithms, multipliers or dividers)
  537. RRDSET_FLAG_HOMOGENEOUS_CHECK = (1 << 11), // if set, the chart should be checked to determine if the dimensions are homogeneous
  538. RRDSET_FLAG_HIDDEN = (1 << 12), // if set, do not show this chart on the dashboard, but use it for exporting
  539. RRDSET_FLAG_SYNC_CLOCK = (1 << 13), // if set, microseconds on next data collection will be ignored (the chart will be synced to now)
  540. RRDSET_FLAG_OBSOLETE_DIMENSIONS = (1 << 14), // this is marked by the collector/module when a chart has obsolete dimensions
  541. // No new values have been collected for this chart since agent start, or it was marked RRDSET_FLAG_OBSOLETE at
  542. // least rrdset_free_obsolete_time seconds ago.
  543. RRDSET_FLAG_ARCHIVED = (1 << 15),
  544. RRDSET_FLAG_METADATA_UPDATE = (1 << 16), // Mark that metadata needs to be stored
  545. RRDSET_FLAG_ANOMALY_DETECTION = (1 << 18), // flag to identify anomaly detection charts.
  546. RRDSET_FLAG_INDEXED_ID = (1 << 19), // the rrdset is indexed by its id
  547. RRDSET_FLAG_INDEXED_NAME = (1 << 20), // the rrdset is indexed by its name
  548. RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION = (1 << 21),
  549. RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS = (1 << 22), // the sending side has replication in progress
  550. RRDSET_FLAG_SENDER_REPLICATION_FINISHED = (1 << 23), // the sending side has completed replication
  551. RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS = (1 << 24), // the receiving side has replication in progress
  552. RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED = (1 << 25), // the receiving side has completed replication
  553. RRDSET_FLAG_UPSTREAM_SEND_VARIABLES = (1 << 26), // a custom variable has been updated and needs to be exposed to parent
  554. RRDSET_FLAG_COLLECTION_FINISHED = (1 << 27), // when set, data collection is not available for this chart
  555. } RRDSET_FLAGS;
  556. #define rrdset_flag_check(st, flag) (__atomic_load_n(&((st)->flags), __ATOMIC_SEQ_CST) & (flag))
  557. #define rrdset_flag_set(st, flag) __atomic_or_fetch(&((st)->flags), flag, __ATOMIC_SEQ_CST)
  558. #define rrdset_flag_clear(st, flag) __atomic_and_fetch(&((st)->flags), ~(flag), __ATOMIC_SEQ_CST)
  559. #define rrdset_is_replicating(st) (rrdset_flag_check(st, RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS|RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS) \
  560. && !rrdset_flag_check(st, RRDSET_FLAG_SENDER_REPLICATION_FINISHED|RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED))
  561. struct rrdset {
  562. uuid_t chart_uuid; // the global UUID for this chart
  563. // ------------------------------------------------------------------------
  564. // chart configuration
  565. struct {
  566. STRING *type; // the type of {type}.{id}
  567. STRING *id; // the id of {type}.{id}
  568. STRING *name; // the name of {type}.{name}
  569. } parts;
  570. STRING *id; // the unique ID of the rrdset as {type}.{id}
  571. STRING *name; // the unique name of the rrdset as {type}.{name}
  572. STRING *family; // grouping sets under the same family
  573. STRING *title; // title shown to user
  574. STRING *units; // units of measurement
  575. STRING *context; // the template of this data set
  576. STRING *plugin_name; // the name of the plugin that generated this
  577. STRING *module_name; // the name of the plugin module that generated this
  578. RRDSET_TYPE chart_type; // line, area, stacked
  579. long priority; // the sorting priority of this chart
  580. int update_every; // data collection frequency
  581. DICTIONARY *rrdlabels; // chart labels
  582. DICTIONARY *rrdsetvar_root_index; // chart variables
  583. DICTIONARY *rrddimvar_root_index; // dimension variables
  584. // we use this dictionary to manage their allocation
  585. rrd_ml_chart_t *ml_chart;
  586. // ------------------------------------------------------------------------
  587. // operational state members
  588. RRDSET_FLAGS flags; // flags
  589. RRD_MEMORY_MODE rrd_memory_mode; // the db mode of this rrdset
  590. DICTIONARY *rrddim_root_index; // dimensions index
  591. STORAGE_METRICS_GROUP *storage_metrics_groups[RRD_STORAGE_TIERS];
  592. // ------------------------------------------------------------------------
  593. // linking to siblings and parents
  594. RRDHOST *rrdhost; // pointer to RRDHOST this chart belongs to
  595. RRDINSTANCE_ACQUIRED *rrdinstance; // the rrdinstance of this chart
  596. RRDCONTEXT_ACQUIRED *rrdcontext; // the rrdcontext this chart belongs to
  597. // ------------------------------------------------------------------------
  598. // data collection members
  599. SPINLOCK data_collection_lock;
  600. size_t counter; // the number of times we added values to this database
  601. size_t counter_done; // the number of times rrdset_done() has been called
  602. time_t last_accessed_time_s; // the last time this RRDSET has been accessed
  603. usec_t usec_since_last_update; // the time in microseconds since the last collection of data
  604. struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
  605. struct timeval last_collected_time; // when did this data set last collected values
  606. size_t rrdlabels_last_saved_version;
  607. DICTIONARY *functions_view; // collector functions this rrdset supports, can be NULL
  608. // ------------------------------------------------------------------------
  609. // data collection - streaming to parents, temp variables
  610. time_t upstream_resync_time_s; // the timestamp up to which we should resync clock upstream
  611. // ------------------------------------------------------------------------
  612. // db mode SAVE, MAP specifics
  613. // TODO - they should be managed by storage engine
  614. // (RRDSET_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  615. char *cache_dir; // the directory to store dimensions
  616. void *st_on_file; // compatibility with V019 RRDSET files
  617. // ------------------------------------------------------------------------
  618. // db mode RAM, SAVE, MAP, ALLOC, NONE specifics
  619. // TODO - they should be managed by storage engine
  620. // (RRDSET_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  621. long entries; // total number of entries in the data set
  622. long current_entry; // the entry that is currently being updated
  623. // it goes around in a round-robin fashion
  624. // ------------------------------------------------------------------------
  625. // exporting to 3rd party time-series members
  626. // TODO - they should be managed by exporting engine
  627. // (RRDSET_EXPORTING_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  628. RRDSET_FLAGS *exporting_flags; // array of flags for exporting connector instances
  629. // ------------------------------------------------------------------------
  630. // health monitoring members
  631. // TODO - they should be managed by health
  632. // (RRDSET_HEALTH_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  633. NETDATA_DOUBLE green; // green threshold for this chart
  634. NETDATA_DOUBLE red; // red threshold for this chart
  635. DICTIONARY *rrdvars; // RRDVAR index for this chart
  636. const RRDFAMILY_ACQUIRED *rrdfamily; // pointer to RRDFAMILY dictionary item, this chart belongs to
  637. struct {
  638. netdata_rwlock_t rwlock; // protection for RRDCALC *base
  639. RRDCALC *base; // double linked list of RRDCALC related to this RRDSET
  640. } alerts;
  641. struct {
  642. size_t pos;
  643. size_t size;
  644. size_t used;
  645. RRDDIM_ACQUIRED **rda;
  646. } pluginsd;
  647. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  648. struct {
  649. bool log_next_data_collection;
  650. bool start_streaming;
  651. time_t after;
  652. time_t before;
  653. } replay;
  654. #endif // NETDATA_LOG_REPLICATION_REQUESTS
  655. };
  656. #define rrdset_plugin_name(st) string2str((st)->plugin_name)
  657. #define rrdset_module_name(st) string2str((st)->module_name)
  658. #define rrdset_units(st) string2str((st)->units)
  659. #define rrdset_parts_type(st) string2str((st)->parts.type)
  660. #define rrdset_family(st) string2str((st)->family)
  661. #define rrdset_title(st) string2str((st)->title)
  662. #define rrdset_context(st) string2str((st)->context)
  663. #define rrdset_name(st) string2str((st)->name)
  664. #define rrdset_id(st) string2str((st)->id)
  665. STRING *rrd_string_strdupz(const char *s);
  666. // ----------------------------------------------------------------------------
  667. // these loop macros make sure the linked list is accessed with the right lock
  668. #define rrdset_foreach_read(st, host) \
  669. dfe_start_read((host)->rrdset_root_index, st)
  670. #define rrdset_foreach_write(st, host) \
  671. dfe_start_write((host)->rrdset_root_index, st)
  672. #define rrdset_foreach_reentrant(st, host) \
  673. dfe_start_reentrant((host)->rrdset_root_index, st)
  674. #define rrdset_foreach_done(st) \
  675. dfe_done(st)
  676. #define rrdset_number_of_dimensions(st) \
  677. dictionary_entries((st)->rrddim_root_index)
  678. void rrdset_memory_file_save(RRDSET *st);
  679. void rrdset_memory_file_free(RRDSET *st);
  680. void rrdset_memory_file_update(RRDSET *st);
  681. const char *rrdset_cache_filename(RRDSET *st);
  682. bool rrdset_memory_load_or_create_map_save(RRDSET *st_on_file, RRD_MEMORY_MODE memory_mode);
  683. #include "rrdfunctions.h"
  684. // ----------------------------------------------------------------------------
  685. // RRDHOST flags
  686. // use this for configuration flags, not for state control
  687. // flags are set/unset in a manner that is not thread safe
  688. // and may lead to missing information.
  689. typedef enum __attribute__ ((__packed__)) rrdhost_flags {
  690. // Careful not to overlap with rrdhost_options to avoid bugs if
  691. // rrdhost_flags_xxx is used instead of rrdhost_option_xxx or vice-versa
  692. // Orphan, Archived and Obsolete flags
  693. RRDHOST_FLAG_ORPHAN = (1 << 8), // this host is orphan (not receiving data)
  694. RRDHOST_FLAG_ARCHIVED = (1 << 9), // The host is archived, no collected charts yet
  695. RRDHOST_FLAG_PENDING_OBSOLETE_CHARTS = (1 << 10), // the host has pending chart obsoletions
  696. RRDHOST_FLAG_PENDING_OBSOLETE_DIMENSIONS = (1 << 11), // the host has pending dimension obsoletions
  697. // Streaming sender
  698. RRDHOST_FLAG_RRDPUSH_SENDER_INITIALIZED = (1 << 12), // the host has initialized rrdpush structures
  699. RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN = (1 << 13), // When set, the sender thread is running
  700. RRDHOST_FLAG_RRDPUSH_SENDER_CONNECTED = (1 << 14), // When set, the host is connected to a parent
  701. RRDHOST_FLAG_RRDPUSH_SENDER_READY_4_METRICS = (1 << 15), // when set, rrdset_done() should push metrics to parent
  702. RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS = (1 << 16), // when set, we have logged the status of metrics streaming
  703. // Health
  704. RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION = (1 << 17), // contains charts and dims with uninitialized variables
  705. RRDHOST_FLAG_INITIALIZED_HEALTH = (1 << 18), // the host has initialized health structures
  706. // Exporting
  707. RRDHOST_FLAG_EXPORTING_SEND = (1 << 19), // send it to external databases
  708. RRDHOST_FLAG_EXPORTING_DONT_SEND = (1 << 20), // don't send it to external databases
  709. // ACLK
  710. RRDHOST_FLAG_ACLK_STREAM_CONTEXTS = (1 << 21), // when set, we should send ACLK stream context updates
  711. RRDHOST_FLAG_ACLK_STREAM_ALERTS = (1 << 22), // set when the receiver part is disconnected
  712. // Metadata
  713. RRDHOST_FLAG_METADATA_UPDATE = (1 << 23), // metadata needs to be stored in the database
  714. RRDHOST_FLAG_METADATA_LABELS = (1 << 24), // metadata needs to be stored in the database
  715. RRDHOST_FLAG_METADATA_INFO = (1 << 25), // metadata needs to be stored in the database
  716. RRDHOST_FLAG_PENDING_CONTEXT_LOAD = (1 << 26), // metadata needs to be stored in the database
  717. RRDHOST_FLAG_CONTEXT_LOAD_IN_PROGRESS = (1 << 27), // metadata needs to be stored in the database
  718. RRDHOST_FLAG_METADATA_CLAIMID = (1 << 28), // metadata needs to be stored in the database
  719. RRDHOST_FLAG_RRDPUSH_RECEIVER_DISCONNECTED = (1 << 29), // set when the receiver part is disconnected
  720. } RRDHOST_FLAGS;
  721. #define rrdhost_flag_check(host, flag) (__atomic_load_n(&((host)->flags), __ATOMIC_SEQ_CST) & (flag))
  722. #define rrdhost_flag_set(host, flag) __atomic_or_fetch(&((host)->flags), flag, __ATOMIC_SEQ_CST)
  723. #define rrdhost_flag_clear(host, flag) __atomic_and_fetch(&((host)->flags), ~(flag), __ATOMIC_SEQ_CST)
  724. #ifdef NETDATA_INTERNAL_CHECKS
  725. #define rrdset_debug(st, fmt, args...) do { if(unlikely(debug_flags & D_RRD_STATS && rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) \
  726. debug_int(__FILE__, __FUNCTION__, __LINE__, "%s: " fmt, rrdset_name(st), ##args); } while(0)
  727. #else
  728. #define rrdset_debug(st, fmt, args...) debug_dummy()
  729. #endif
  730. typedef enum __attribute__ ((__packed__)) {
  731. // Indexing
  732. RRDHOST_OPTION_INDEXED_MACHINE_GUID = (1 << 0), // when set, we have indexed its machine guid
  733. RRDHOST_OPTION_INDEXED_HOSTNAME = (1 << 1), // when set, we have indexed its hostname
  734. // Streaming configuration
  735. RRDHOST_OPTION_SENDER_ENABLED = (1 << 2), // set when the host is configured to send metrics to a parent
  736. // Configuration options
  737. RRDHOST_OPTION_DELETE_OBSOLETE_CHARTS = (1 << 3), // delete files of obsolete charts
  738. RRDHOST_OPTION_DELETE_ORPHAN_HOST = (1 << 4), // delete the entire host when orphan
  739. RRDHOST_OPTION_REPLICATION = (1 << 5), // when set, we support replication for this host
  740. } RRDHOST_OPTIONS;
  741. #define rrdhost_option_check(host, flag) ((host)->options & (flag))
  742. #define rrdhost_option_set(host, flag) (host)->options |= flag
  743. #define rrdhost_option_clear(host, flag) (host)->options &= ~(flag)
  744. #define rrdhost_has_rrdpush_sender_enabled(host) (rrdhost_option_check(host, RRDHOST_OPTION_SENDER_ENABLED) && (host)->sender)
  745. #define rrdhost_can_send_definitions_to_parent(host) (rrdhost_has_rrdpush_sender_enabled(host) && rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_CONNECTED))
  746. // ----------------------------------------------------------------------------
  747. // Health data
  748. struct alarm_entry {
  749. uint32_t unique_id;
  750. uint32_t alarm_id;
  751. uint32_t alarm_event_id;
  752. uuid_t config_hash_id;
  753. uuid_t transition_id;
  754. time_t when;
  755. time_t duration;
  756. time_t non_clear_duration;
  757. STRING *name;
  758. STRING *chart;
  759. STRING *chart_context;
  760. STRING *family;
  761. STRING *classification;
  762. STRING *component;
  763. STRING *type;
  764. STRING *exec;
  765. STRING *recipient;
  766. time_t exec_run_timestamp;
  767. int exec_code;
  768. uint64_t exec_spawn_serial;
  769. STRING *source;
  770. STRING *units;
  771. STRING *info;
  772. NETDATA_DOUBLE old_value;
  773. NETDATA_DOUBLE new_value;
  774. STRING *old_value_string;
  775. STRING *new_value_string;
  776. RRDCALC_STATUS old_status;
  777. RRDCALC_STATUS new_status;
  778. uint32_t flags;
  779. int delay;
  780. time_t delay_up_to_timestamp;
  781. uint32_t updated_by_id;
  782. uint32_t updates_id;
  783. time_t last_repeat;
  784. struct alarm_entry *next;
  785. struct alarm_entry *next_in_progress;
  786. struct alarm_entry *prev_in_progress;
  787. };
  788. #define ae_name(ae) string2str((ae)->name)
  789. #define ae_chart_name(ae) string2str((ae)->chart)
  790. #define ae_chart_context(ae) string2str((ae)->chart_context)
  791. #define ae_family(ae) string2str((ae)->family)
  792. #define ae_classification(ae) string2str((ae)->classification)
  793. #define ae_component(ae) string2str((ae)->component)
  794. #define ae_type(ae) string2str((ae)->type)
  795. #define ae_exec(ae) string2str((ae)->exec)
  796. #define ae_recipient(ae) string2str((ae)->recipient)
  797. #define ae_source(ae) string2str((ae)->source)
  798. #define ae_units(ae) string2str((ae)->units)
  799. #define ae_info(ae) string2str((ae)->info)
  800. #define ae_old_value_string(ae) string2str((ae)->old_value_string)
  801. #define ae_new_value_string(ae) string2str((ae)->new_value_string)
  802. typedef struct alarm_log {
  803. uint32_t next_log_id;
  804. uint32_t next_alarm_id;
  805. unsigned int count;
  806. unsigned int max;
  807. ALARM_ENTRY *alarms;
  808. netdata_rwlock_t alarm_log_rwlock;
  809. } ALARM_LOG;
  810. typedef struct health {
  811. unsigned int health_enabled; // 1 when this host has health enabled
  812. time_t health_delay_up_to; // a timestamp to delay alarms processing up to
  813. STRING *health_default_exec; // the full path of the alarms notifications program
  814. STRING *health_default_recipient; // the default recipient for all alarms
  815. size_t health_log_entries_written; // the number of alarm events written to the alarms event log
  816. uint32_t health_default_warn_repeat_every; // the default value for the interval between repeating warning notifications
  817. uint32_t health_default_crit_repeat_every; // the default value for the interval between repeating critical notifications
  818. } HEALTH;
  819. // ----------------------------------------------------------------------------
  820. // RRD HOST
  821. struct rrdhost_system_info {
  822. char *cloud_provider_type;
  823. char *cloud_instance_type;
  824. char *cloud_instance_region;
  825. char *host_os_name;
  826. char *host_os_id;
  827. char *host_os_id_like;
  828. char *host_os_version;
  829. char *host_os_version_id;
  830. char *host_os_detection;
  831. char *host_cores;
  832. char *host_cpu_freq;
  833. char *host_ram_total;
  834. char *host_disk_space;
  835. char *container_os_name;
  836. char *container_os_id;
  837. char *container_os_id_like;
  838. char *container_os_version;
  839. char *container_os_version_id;
  840. char *container_os_detection;
  841. char *kernel_name;
  842. char *kernel_version;
  843. char *architecture;
  844. char *virtualization;
  845. char *virt_detection;
  846. char *container;
  847. char *container_detection;
  848. char *is_k8s_node;
  849. uint16_t hops;
  850. bool ml_capable;
  851. bool ml_enabled;
  852. char *install_type;
  853. char *prebuilt_arch;
  854. char *prebuilt_dist;
  855. int mc_version;
  856. };
  857. struct rrdhost_system_info *rrdhost_labels_to_system_info(DICTIONARY *labels);
  858. struct rrdhost {
  859. char machine_guid[GUID_LEN + 1]; // the unique ID of this host
  860. // ------------------------------------------------------------------------
  861. // host information
  862. STRING *hostname; // the hostname of this host
  863. STRING *registry_hostname; // the registry hostname for this host
  864. STRING *os; // the O/S type of the host
  865. STRING *tags; // tags for this host
  866. STRING *timezone; // the timezone of the host
  867. STRING *abbrev_timezone; // the abbriviated timezone of the host
  868. STRING *program_name; // the program name that collects metrics for this host
  869. STRING *program_version; // the program version that collects metrics for this host
  870. int32_t utc_offset; // the offset in seconds from utc
  871. RRDHOST_OPTIONS options; // configuration option for this RRDHOST (no atomics on this)
  872. RRDHOST_FLAGS flags; // runtime flags about this RRDHOST (atomics on this)
  873. RRDHOST_FLAGS *exporting_flags; // array of flags for exporting connector instances
  874. int rrd_update_every; // the update frequency of the host
  875. long rrd_history_entries; // the number of history entries for the host's charts
  876. RRD_MEMORY_MODE rrd_memory_mode; // the configured memory more for the charts of this host
  877. // the actual per tier is at .db[tier].mode
  878. char *cache_dir; // the directory to save RRD cache files
  879. struct {
  880. RRD_MEMORY_MODE mode; // the db mode for this tier
  881. STORAGE_ENGINE *eng; // the storage engine API for this tier
  882. STORAGE_INSTANCE *instance; // the db instance for this tier
  883. uint32_t tier_grouping; // tier 0 iterations aggregated on this tier
  884. } db[RRD_STORAGE_TIERS];
  885. struct rrdhost_system_info *system_info; // information collected from the host environment
  886. // ------------------------------------------------------------------------
  887. // streaming of data to remote hosts - rrdpush sender
  888. char *rrdpush_send_destination; // where to send metrics to
  889. char *rrdpush_send_api_key; // the api key at the receiving netdata
  890. struct rrdpush_destinations *destinations; // a linked list of possible destinations
  891. struct rrdpush_destinations *destination; // the current destination from the above list
  892. SIMPLE_PATTERN *rrdpush_send_charts_matching; // pattern to match the charts to be sent
  893. const char *rrdpush_last_receiver_exit_reason;
  894. time_t rrdpush_seconds_to_replicate; // max time we want to replicate from the child
  895. time_t rrdpush_replication_step; // seconds per replication step
  896. size_t rrdpush_receiver_replicating_charts; // the number of charts currently being replicated from a child
  897. NETDATA_DOUBLE rrdpush_receiver_replication_percent; // the % of replication completion
  898. // the following are state information for the threading
  899. // streaming metrics from this netdata to an upstream netdata
  900. struct sender_state *sender;
  901. netdata_thread_t rrdpush_sender_thread; // the sender thread
  902. size_t rrdpush_sender_replicating_charts; // the number of charts currently being replicated to a parent
  903. void *aclk_sync_host_config;
  904. // ------------------------------------------------------------------------
  905. // streaming of data from remote hosts - rrdpush receiver
  906. time_t child_connect_time; // the time the last sender was connected
  907. time_t child_last_chart_command; // the time of the last CHART streaming command
  908. time_t child_disconnected_time; // the time the last sender was disconnected
  909. int connected_children_count; // number of senders currently streaming
  910. struct receiver_state *receiver;
  911. netdata_mutex_t receiver_lock;
  912. int trigger_chart_obsoletion_check; // set when child connects, will instruct parent to
  913. // trigger a check for obsoleted charts since previous connect
  914. // ------------------------------------------------------------------------
  915. // health monitoring options
  916. // health variables
  917. HEALTH health;
  918. // all RRDCALCs are primarily allocated and linked here
  919. DICTIONARY *rrdcalc_root_index;
  920. // templates of alarms
  921. DICTIONARY *rrdcalctemplate_root_index;
  922. ALARM_LOG health_log; // alarms historical events (event log)
  923. uint32_t health_last_processed_id; // the last processed health id from the log
  924. uint32_t health_max_unique_id; // the max alarm log unique id given for the host
  925. uint32_t health_max_alarm_id; // the max alarm id given for the host
  926. size_t health_transitions; // the number of times an alert changed state
  927. // ------------------------------------------------------------------------
  928. // locks
  929. SPINLOCK rrdhost_update_lock;
  930. // ------------------------------------------------------------------------
  931. // ML handle
  932. rrd_ml_host_t *ml_host;
  933. // ------------------------------------------------------------------------
  934. // Support for host-level labels
  935. DICTIONARY *rrdlabels;
  936. // ------------------------------------------------------------------------
  937. // Support for functions
  938. DICTIONARY *functions; // collector functions this rrdset supports, can be NULL
  939. // ------------------------------------------------------------------------
  940. // indexes
  941. DICTIONARY *rrdset_root_index; // the host's charts index (by id)
  942. DICTIONARY *rrdset_root_index_name; // the host's charts index (by name)
  943. DICTIONARY *rrdfamily_root_index; // the host's chart families index
  944. DICTIONARY *rrdvars; // the host's chart variables index
  945. // this includes custom host variables
  946. struct {
  947. DICTIONARY *contexts;
  948. DICTIONARY *hub_queue;
  949. DICTIONARY *pp_queue;
  950. } rrdctx;
  951. uuid_t host_uuid; // Global GUID for this host
  952. uuid_t *node_id; // Cloud node_id
  953. netdata_mutex_t aclk_state_lock;
  954. aclk_rrdhost_state aclk_state;
  955. struct rrdhost *next;
  956. struct rrdhost *prev;
  957. };
  958. extern RRDHOST *localhost;
  959. #define rrdhost_hostname(host) string2str((host)->hostname)
  960. #define rrdhost_registry_hostname(host) string2str((host)->registry_hostname)
  961. #define rrdhost_os(host) string2str((host)->os)
  962. #define rrdhost_tags(host) string2str((host)->tags)
  963. #define rrdhost_timezone(host) string2str((host)->timezone)
  964. #define rrdhost_abbrev_timezone(host) string2str((host)->abbrev_timezone)
  965. #define rrdhost_program_name(host) string2str((host)->program_name)
  966. #define rrdhost_program_version(host) string2str((host)->program_version)
  967. #define rrdhost_aclk_state_lock(host) netdata_mutex_lock(&((host)->aclk_state_lock))
  968. #define rrdhost_aclk_state_unlock(host) netdata_mutex_unlock(&((host)->aclk_state_lock))
  969. #define rrdhost_receiver_replicating_charts(host) (__atomic_load_n(&((host)->rrdpush_receiver_replicating_charts), __ATOMIC_RELAXED))
  970. #define rrdhost_receiver_replicating_charts_plus_one(host) (__atomic_add_fetch(&((host)->rrdpush_receiver_replicating_charts), 1, __ATOMIC_RELAXED))
  971. #define rrdhost_receiver_replicating_charts_minus_one(host) (__atomic_sub_fetch(&((host)->rrdpush_receiver_replicating_charts), 1, __ATOMIC_RELAXED))
  972. #define rrdhost_receiver_replicating_charts_zero(host) (__atomic_store_n(&((host)->rrdpush_receiver_replicating_charts), 0, __ATOMIC_RELAXED))
  973. #define rrdhost_sender_replicating_charts(host) (__atomic_load_n(&((host)->rrdpush_sender_replicating_charts), __ATOMIC_RELAXED))
  974. #define rrdhost_sender_replicating_charts_plus_one(host) (__atomic_add_fetch(&((host)->rrdpush_sender_replicating_charts), 1, __ATOMIC_RELAXED))
  975. #define rrdhost_sender_replicating_charts_minus_one(host) (__atomic_sub_fetch(&((host)->rrdpush_sender_replicating_charts), 1, __ATOMIC_RELAXED))
  976. #define rrdhost_sender_replicating_charts_zero(host) (__atomic_store_n(&((host)->rrdpush_sender_replicating_charts), 0, __ATOMIC_RELAXED))
  977. extern DICTIONARY *rrdhost_root_index;
  978. size_t rrdhost_hosts_available(void);
  979. RRDHOST_ACQUIRED *rrdhost_find_and_acquire(const char *machine_guid);
  980. RRDHOST *rrdhost_acquired_to_rrdhost(RRDHOST_ACQUIRED *rha);
  981. void rrdhost_acquired_release(RRDHOST_ACQUIRED *rha);
  982. // ----------------------------------------------------------------------------
  983. #define rrdhost_foreach_read(var) \
  984. for((var) = localhost; var ; (var) = (var)->next)
  985. #define rrdhost_foreach_write(var) \
  986. for((var) = localhost; var ; (var) = (var)->next)
  987. // ----------------------------------------------------------------------------
  988. // global lock for all RRDHOSTs
  989. extern netdata_rwlock_t rrd_rwlock;
  990. #define rrd_rdlock() netdata_rwlock_rdlock(&rrd_rwlock)
  991. #define rrd_wrlock() netdata_rwlock_wrlock(&rrd_rwlock)
  992. #define rrd_unlock() netdata_rwlock_unlock(&rrd_rwlock)
  993. // ----------------------------------------------------------------------------
  994. bool is_storage_engine_shared(STORAGE_INSTANCE *engine);
  995. void rrdset_index_init(RRDHOST *host);
  996. void rrdset_index_destroy(RRDHOST *host);
  997. void rrddim_index_init(RRDSET *st);
  998. void rrddim_index_destroy(RRDSET *st);
  999. // ----------------------------------------------------------------------------
  1000. extern time_t rrdhost_free_orphan_time_s;
  1001. int rrd_init(char *hostname, struct rrdhost_system_info *system_info, bool unittest);
  1002. RRDHOST *rrdhost_find_by_hostname(const char *hostname);
  1003. RRDHOST *rrdhost_find_by_guid(const char *guid);
  1004. RRDHOST *find_host_by_node_id(char *node_id);
  1005. RRDHOST *rrdhost_find_or_create(
  1006. const char *hostname
  1007. , const char *registry_hostname
  1008. , const char *guid
  1009. , const char *os
  1010. , const char *timezone
  1011. , const char *abbrev_timezone
  1012. , int32_t utc_offset
  1013. , const char *tags
  1014. , const char *program_name
  1015. , const char *program_version
  1016. , int update_every
  1017. , long history
  1018. , RRD_MEMORY_MODE mode
  1019. , unsigned int health_enabled
  1020. , unsigned int rrdpush_enabled
  1021. , char *rrdpush_destination
  1022. , char *rrdpush_api_key
  1023. , char *rrdpush_send_charts_matching
  1024. , bool rrdpush_enable_replication
  1025. , time_t rrdpush_seconds_to_replicate
  1026. , time_t rrdpush_replication_step
  1027. , struct rrdhost_system_info *system_info
  1028. , bool is_archived
  1029. );
  1030. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value);
  1031. // ----------------------------------------------------------------------------
  1032. // RRDSET functions
  1033. int rrdset_reset_name(RRDSET *st, const char *name);
  1034. RRDSET *rrdset_create_custom(RRDHOST *host
  1035. , const char *type
  1036. , const char *id
  1037. , const char *name
  1038. , const char *family
  1039. , const char *context
  1040. , const char *title
  1041. , const char *units
  1042. , const char *plugin
  1043. , const char *module
  1044. , long priority
  1045. , int update_every
  1046. , RRDSET_TYPE chart_type
  1047. , RRD_MEMORY_MODE memory_mode
  1048. , long history_entries);
  1049. #define rrdset_create(host, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  1050. rrdset_create_custom(host, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type, (host)->rrd_memory_mode, (host)->rrd_history_entries)
  1051. #define rrdset_create_localhost(type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  1052. rrdset_create(localhost, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type)
  1053. void rrdhost_free_all(void);
  1054. void rrdhost_save_all(void);
  1055. void rrdhost_cleanup_all(void);
  1056. void rrdhost_system_info_free(struct rrdhost_system_info *system_info);
  1057. void rrdhost_free___while_having_rrd_wrlock(RRDHOST *host, bool force);
  1058. void rrdhost_save_charts(RRDHOST *host);
  1059. void rrdhost_delete_charts(RRDHOST *host);
  1060. int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now_s);
  1061. void rrdset_update_heterogeneous_flag(RRDSET *st);
  1062. time_t rrdset_set_update_every_s(RRDSET *st, time_t update_every_s);
  1063. RRDSET *rrdset_find(RRDHOST *host, const char *id);
  1064. RRDSET_ACQUIRED *rrdset_find_and_acquire(RRDHOST *host, const char *id);
  1065. RRDSET *rrdset_acquired_to_rrdset(RRDSET_ACQUIRED *rsa);
  1066. void rrdset_acquired_release(RRDSET_ACQUIRED *rsa);
  1067. #define rrdset_find_localhost(id) rrdset_find(localhost, id)
  1068. /* This will not return charts that are archived */
  1069. static inline RRDSET *rrdset_find_active_localhost(const char *id)
  1070. {
  1071. RRDSET *st = rrdset_find_localhost(id);
  1072. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  1073. return NULL;
  1074. return st;
  1075. }
  1076. RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
  1077. #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
  1078. /* This will not return charts that are archived */
  1079. static inline RRDSET *rrdset_find_active_bytype_localhost(const char *type, const char *id)
  1080. {
  1081. RRDSET *st = rrdset_find_bytype_localhost(type, id);
  1082. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  1083. return NULL;
  1084. return st;
  1085. }
  1086. RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
  1087. #define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
  1088. /* This will not return charts that are archived */
  1089. static inline RRDSET *rrdset_find_active_byname_localhost(const char *name)
  1090. {
  1091. RRDSET *st = rrdset_find_byname_localhost(name);
  1092. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  1093. return NULL;
  1094. return st;
  1095. }
  1096. void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
  1097. void rrdset_next_usec(RRDSET *st, usec_t microseconds);
  1098. void rrdset_timed_next(RRDSET *st, struct timeval now, usec_t microseconds);
  1099. #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
  1100. void rrdset_timed_done(RRDSET *st, struct timeval now, bool pending_rrdset_next);
  1101. void rrdset_done(RRDSET *st);
  1102. void rrdset_is_obsolete(RRDSET *st);
  1103. void rrdset_isnot_obsolete(RRDSET *st);
  1104. // checks if the RRDSET should be offered to viewers
  1105. #define rrdset_is_available_for_viewers(st) (!rrdset_flag_check(st, RRDSET_FLAG_HIDDEN) && !rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && rrdset_number_of_dimensions(st) && (st)->rrd_memory_mode != RRD_MEMORY_MODE_NONE)
  1106. #define rrdset_is_available_for_exporting_and_alarms(st) (!rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && rrdset_number_of_dimensions(st))
  1107. #define rrdset_is_archived(st) (rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && rrdset_number_of_dimensions(st))
  1108. time_t rrddim_first_entry_s(RRDDIM *rd);
  1109. time_t rrddim_first_entry_s_of_tier(RRDDIM *rd, size_t tier);
  1110. time_t rrddim_last_entry_s(RRDDIM *rd);
  1111. time_t rrddim_last_entry_s_of_tier(RRDDIM *rd, size_t tier);
  1112. time_t rrdset_first_entry_s(RRDSET *st);
  1113. time_t rrdset_first_entry_s_of_tier(RRDSET *st, size_t tier);
  1114. time_t rrdset_last_entry_s(RRDSET *st);
  1115. time_t rrdset_last_entry_s_of_tier(RRDSET *st, size_t tier);
  1116. void rrdset_get_retention_of_tier_for_collected_chart(RRDSET *st, time_t *first_time_s, time_t *last_time_s, time_t now_s, size_t tier);
  1117. // ----------------------------------------------------------------------------
  1118. // RRD DIMENSION functions
  1119. RRDDIM *rrddim_add_custom(RRDSET *st
  1120. , const char *id
  1121. , const char *name
  1122. , collected_number multiplier
  1123. , collected_number divisor
  1124. , RRD_ALGORITHM algorithm
  1125. , RRD_MEMORY_MODE memory_mode
  1126. );
  1127. #define rrddim_add(st, id, name, multiplier, divisor, algorithm) \
  1128. rrddim_add_custom(st, id, name, multiplier, divisor, algorithm, (st)->rrd_memory_mode)
  1129. int rrddim_reset_name(RRDSET *st, RRDDIM *rd, const char *name);
  1130. int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm);
  1131. int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier);
  1132. int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor);
  1133. RRDDIM *rrddim_find(RRDSET *st, const char *id);
  1134. RRDDIM_ACQUIRED *rrddim_find_and_acquire(RRDSET *st, const char *id);
  1135. RRDDIM *rrddim_acquired_to_rrddim(RRDDIM_ACQUIRED *rda);
  1136. void rrddim_acquired_release(RRDDIM_ACQUIRED *rda);
  1137. RRDDIM *rrddim_find_active(RRDSET *st, const char *id);
  1138. int rrddim_hide(RRDSET *st, const char *id);
  1139. int rrddim_unhide(RRDSET *st, const char *id);
  1140. void rrddim_is_obsolete(RRDSET *st, RRDDIM *rd);
  1141. void rrddim_isnot_obsolete(RRDSET *st, RRDDIM *rd);
  1142. collected_number rrddim_timed_set_by_pointer(RRDSET *st, RRDDIM *rd, struct timeval collected_time, collected_number value);
  1143. collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
  1144. collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
  1145. bool rrddim_finalize_collection_and_check_retention(RRDDIM *rd);
  1146. void rrdset_finalize_collection(RRDSET *st, bool dimensions_too);
  1147. void rrdhost_finalize_collection(RRDHOST *host);
  1148. void rrd_finalize_collection_for_all_hosts(void);
  1149. long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries);
  1150. #ifdef NETDATA_LOG_COLLECTION_ERRORS
  1151. #define rrddim_store_metric(rd, point_end_time_ut, n, flags) rrddim_store_metric_with_trace(rd, point_end_time_ut, n, flags, __FUNCTION__)
  1152. void rrddim_store_metric_with_trace(RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags, const char *function);
  1153. #else
  1154. void rrddim_store_metric(RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags);
  1155. #endif
  1156. // ----------------------------------------------------------------------------
  1157. // Miscellaneous functions
  1158. char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
  1159. // ----------------------------------------------------------------------------
  1160. // RRD internal functions
  1161. void rrdset_delete_files(RRDSET *st);
  1162. void rrdset_save(RRDSET *st);
  1163. void rrdset_free(RRDSET *st);
  1164. void rrddim_free(RRDSET *st, RRDDIM *rd);
  1165. #ifdef NETDATA_RRD_INTERNALS
  1166. char *rrdhost_cache_dir_for_rrdset_alloc(RRDHOST *host, const char *id);
  1167. const char *rrdset_cache_dir(RRDSET *st);
  1168. void rrdset_reset(RRDSET *st);
  1169. void rrdset_delete_obsolete_dimensions(RRDSET *st);
  1170. #endif /* NETDATA_RRD_INTERNALS */
  1171. void set_host_properties(
  1172. RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *registry_hostname,
  1173. const char *os, const char *tags, const char *tzone, const char *abbrev_tzone, int32_t utc_offset,
  1174. const char *program_name, const char *program_version);
  1175. size_t get_tier_grouping(size_t tier);
  1176. void store_metric_collection_completed(void);
  1177. // ----------------------------------------------------------------------------
  1178. // RRD DB engine declarations
  1179. #ifdef ENABLE_DBENGINE
  1180. #include "database/engine/rrdengineapi.h"
  1181. #endif
  1182. #include "sqlite/sqlite_functions.h"
  1183. #include "sqlite/sqlite_context.h"
  1184. #include "sqlite/sqlite_metadata.h"
  1185. #include "sqlite/sqlite_aclk.h"
  1186. #include "sqlite/sqlite_aclk_alert.h"
  1187. #include "sqlite/sqlite_aclk_node.h"
  1188. #include "sqlite/sqlite_health.h"
  1189. #ifdef __cplusplus
  1190. }
  1191. #endif
  1192. #endif /* NETDATA_RRD_H */