rrd.h 73 KB

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