rrd.h 73 KB

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