rrd.h 70 KB

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