rrd.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRD_H
  3. #define NETDATA_RRD_H 1
  4. // forward typedefs
  5. typedef struct rrdhost RRDHOST;
  6. typedef struct rrddim RRDDIM;
  7. typedef struct rrdset RRDSET;
  8. typedef struct rrdvar RRDVAR;
  9. typedef struct rrdsetvar RRDSETVAR;
  10. typedef struct rrddimvar RRDDIMVAR;
  11. typedef struct rrdcalc RRDCALC;
  12. typedef struct rrdcalctemplate RRDCALCTEMPLATE;
  13. typedef struct alarm_entry ALARM_ENTRY;
  14. typedef struct context_param CONTEXT_PARAM;
  15. // forward declarations
  16. struct rrddim_volatile;
  17. struct rrdset_volatile;
  18. struct context_param;
  19. struct label;
  20. #ifdef ENABLE_DBENGINE
  21. struct rrdeng_page_descr;
  22. struct rrdengine_instance;
  23. struct pg_cache_page_index;
  24. #endif
  25. #include "../daemon/common.h"
  26. #include "web/api/queries/query.h"
  27. #include "rrdvar.h"
  28. #include "rrdsetvar.h"
  29. #include "rrddimvar.h"
  30. #include "rrdcalc.h"
  31. #include "rrdcalctemplate.h"
  32. #include "../streaming/rrdpush.h"
  33. #include "../aclk/legacy/aclk_rrdhost_state.h"
  34. struct context_param {
  35. RRDDIM *rd;
  36. time_t first_entry_t;
  37. time_t last_entry_t;
  38. };
  39. #define META_CHART_UPDATED 1
  40. #define META_PLUGIN_UPDATED 2
  41. #define META_MODULE_UPDATED 4
  42. #define META_CHART_ACTIVATED 8
  43. #define UPDATE_EVERY 1
  44. #define UPDATE_EVERY_MAX 3600
  45. #define RRD_DEFAULT_HISTORY_ENTRIES 3600
  46. #define RRD_HISTORY_ENTRIES_MAX (86400*365)
  47. extern int default_rrd_update_every;
  48. extern int default_rrd_history_entries;
  49. extern int gap_when_lost_iterations_above;
  50. extern time_t rrdset_free_obsolete_time;
  51. #define RRD_ID_LENGTH_MAX 200
  52. #define RRDSET_MAGIC "NETDATA RRD SET FILE V019"
  53. #define RRDDIMENSION_MAGIC "NETDATA RRD DIMENSION FILE V019"
  54. typedef long long total_number;
  55. #define TOTAL_NUMBER_FORMAT "%lld"
  56. // ----------------------------------------------------------------------------
  57. // chart types
  58. typedef enum rrdset_type {
  59. RRDSET_TYPE_LINE = 0,
  60. RRDSET_TYPE_AREA = 1,
  61. RRDSET_TYPE_STACKED = 2
  62. } RRDSET_TYPE;
  63. #define RRDSET_TYPE_LINE_NAME "line"
  64. #define RRDSET_TYPE_AREA_NAME "area"
  65. #define RRDSET_TYPE_STACKED_NAME "stacked"
  66. RRDSET_TYPE rrdset_type_id(const char *name);
  67. const char *rrdset_type_name(RRDSET_TYPE chart_type);
  68. // ----------------------------------------------------------------------------
  69. // memory mode
  70. typedef enum rrd_memory_mode {
  71. RRD_MEMORY_MODE_NONE = 0,
  72. RRD_MEMORY_MODE_RAM = 1,
  73. RRD_MEMORY_MODE_MAP = 2,
  74. RRD_MEMORY_MODE_SAVE = 3,
  75. RRD_MEMORY_MODE_ALLOC = 4,
  76. RRD_MEMORY_MODE_DBENGINE = 5
  77. } RRD_MEMORY_MODE;
  78. #define RRD_MEMORY_MODE_NONE_NAME "none"
  79. #define RRD_MEMORY_MODE_RAM_NAME "ram"
  80. #define RRD_MEMORY_MODE_MAP_NAME "map"
  81. #define RRD_MEMORY_MODE_SAVE_NAME "save"
  82. #define RRD_MEMORY_MODE_ALLOC_NAME "alloc"
  83. #define RRD_MEMORY_MODE_DBENGINE_NAME "dbengine"
  84. extern RRD_MEMORY_MODE default_rrd_memory_mode;
  85. extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
  86. extern RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
  87. // ----------------------------------------------------------------------------
  88. // algorithms types
  89. typedef enum rrd_algorithm {
  90. RRD_ALGORITHM_ABSOLUTE = 0,
  91. RRD_ALGORITHM_INCREMENTAL = 1,
  92. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
  93. RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3
  94. } RRD_ALGORITHM;
  95. #define RRD_ALGORITHM_ABSOLUTE_NAME "absolute"
  96. #define RRD_ALGORITHM_INCREMENTAL_NAME "incremental"
  97. #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
  98. #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
  99. extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
  100. extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
  101. // ----------------------------------------------------------------------------
  102. // RRD FAMILY
  103. struct rrdfamily {
  104. avl avl;
  105. const char *family;
  106. uint32_t hash_family;
  107. size_t use_count;
  108. avl_tree_lock rrdvar_root_index;
  109. };
  110. typedef struct rrdfamily RRDFAMILY;
  111. // ----------------------------------------------------------------------------
  112. // flags
  113. // use this for configuration flags, not for state control
  114. // flags are set/unset in a manner that is not thread safe
  115. // and may lead to missing information.
  116. typedef enum rrddim_flags {
  117. RRDDIM_FLAG_NONE = 0,
  118. RRDDIM_FLAG_HIDDEN = (1 << 0), // this dimension will not be offered to callers
  119. RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = (1 << 1), // do not offer RESET or OVERFLOW info to callers
  120. RRDDIM_FLAG_OBSOLETE = (1 << 2), // this is marked by the collector/module as obsolete
  121. // No new values have been collected for this dimension since agent start or it was marked RRDDIM_FLAG_OBSOLETE at
  122. // least rrdset_free_obsolete_time seconds ago.
  123. RRDDIM_FLAG_ARCHIVED = (1 << 3)
  124. } RRDDIM_FLAGS;
  125. #ifdef HAVE_C___ATOMIC
  126. #define rrddim_flag_check(rd, flag) (__atomic_load_n(&((rd)->flags), __ATOMIC_SEQ_CST) & (flag))
  127. #define rrddim_flag_set(rd, flag) __atomic_or_fetch(&((rd)->flags), (flag), __ATOMIC_SEQ_CST)
  128. #define rrddim_flag_clear(rd, flag) __atomic_and_fetch(&((rd)->flags), ~(flag), __ATOMIC_SEQ_CST)
  129. #else
  130. #define rrddim_flag_check(rd, flag) ((rd)->flags & (flag))
  131. #define rrddim_flag_set(rd, flag) (rd)->flags |= (flag)
  132. #define rrddim_flag_clear(rd, flag) (rd)->flags &= ~(flag)
  133. #endif
  134. typedef enum label_source {
  135. LABEL_SOURCE_AUTO = 0,
  136. LABEL_SOURCE_NETDATA_CONF = 1,
  137. LABEL_SOURCE_DOCKER = 2,
  138. LABEL_SOURCE_ENVIRONMENT = 3,
  139. LABEL_SOURCE_KUBERNETES = 4
  140. } LABEL_SOURCE;
  141. #define LABEL_FLAG_UPDATE_STREAM 1
  142. #define LABEL_FLAG_STOP_STREAM 2
  143. struct label {
  144. char *key, *value;
  145. uint32_t key_hash;
  146. LABEL_SOURCE label_source;
  147. struct label *next;
  148. };
  149. struct label_index {
  150. struct label *head; // Label list
  151. netdata_rwlock_t labels_rwlock; // lock for the label list
  152. uint32_t labels_flag; // Flags for labels
  153. };
  154. typedef enum strip_quotes {
  155. DO_NOT_STRIP_QUOTES,
  156. STRIP_QUOTES
  157. } STRIP_QUOTES_OPTION;
  158. typedef enum skip_escaped_characters {
  159. DO_NOT_SKIP_ESCAPED_CHARACTERS,
  160. SKIP_ESCAPED_CHARACTERS
  161. } SKIP_ESCAPED_CHARACTERS_OPTION;
  162. char *translate_label_source(LABEL_SOURCE l);
  163. struct label *create_label(char *key, char *value, LABEL_SOURCE label_source);
  164. extern struct label *add_label_to_list(struct label *l, char *key, char *value, LABEL_SOURCE label_source);
  165. extern void update_label_list(struct label **labels, struct label *new_labels);
  166. extern void replace_label_list(struct label_index *labels, struct label *new_labels);
  167. extern int is_valid_label_value(char *value);
  168. extern int is_valid_label_key(char *key);
  169. extern void free_label_list(struct label *labels);
  170. extern struct label *label_list_lookup_key(struct label *head, char *key, uint32_t key_hash);
  171. extern struct label *label_list_lookup_keylist(struct label *head, char *keylist);
  172. extern int label_list_contains_keylist(struct label *head, char *keylist);
  173. extern int label_list_contains_key(struct label *head, char *key, uint32_t key_hash);
  174. extern int label_list_contains(struct label *head, struct label *check);
  175. extern struct label *merge_label_lists(struct label *lo_pri, struct label *hi_pri);
  176. extern void strip_last_symbol(
  177. char *str,
  178. char symbol,
  179. SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters);
  180. extern char *strip_double_quotes(char *str, SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters);
  181. void reload_host_labels(void);
  182. extern void rrdset_add_label_to_new_list(RRDSET *st, char *key, char *value, LABEL_SOURCE source);
  183. extern void rrdset_finalize_labels(RRDSET *st);
  184. extern void rrdset_update_labels(RRDSET *st, struct label *labels);
  185. extern int rrdset_contains_label_keylist(RRDSET *st, char *key);
  186. extern struct label *rrdset_lookup_label_key(RRDSET *st, char *key, uint32_t key_hash);
  187. // ----------------------------------------------------------------------------
  188. // RRD DIMENSION - this is a metric
  189. struct rrddim {
  190. // ------------------------------------------------------------------------
  191. // binary indexing structures
  192. avl avl; // the binary index - this has to be first member!
  193. // ------------------------------------------------------------------------
  194. // the dimension definition
  195. const char *id; // the id of this dimension (for internal identification)
  196. const char *name; // the name of this dimension (as presented to user)
  197. // this is a pointer to the config structure
  198. // since the config always has a higher priority
  199. // (the user overwrites the name of the charts)
  200. // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
  201. RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
  202. RRD_MEMORY_MODE rrd_memory_mode; // the memory mode for this dimension
  203. collected_number multiplier; // the multiplier of the collected values
  204. collected_number divisor; // the divider of the collected values
  205. uint32_t flags; // configuration flags for the dimension
  206. // ------------------------------------------------------------------------
  207. // members for temporary data we need for calculations
  208. uint32_t hash; // a simple hash of the id, to speed up searching / indexing
  209. // instead of strcmp() every item in the binary index
  210. // we first compare the hashes
  211. uint32_t hash_name; // a simple hash of the name
  212. char *cache_filename; // the filename we load/save from/to this set
  213. size_t collections_counter; // the number of times we added values to this rrdim
  214. struct rrddim_volatile *state; // volatile state that is not persistently stored
  215. size_t unused[8];
  216. collected_number collected_value_max; // the absolute maximum of the collected value
  217. unsigned int updated:1; // 1 when the dimension has been updated since the last processing
  218. unsigned int exposed:1; // 1 when set what have sent this dimension to the central netdata
  219. struct timeval last_collected_time; // when was this dimension last updated
  220. // this is actual date time we updated the last_collected_value
  221. // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
  222. calculated_number calculated_value; // the current calculated value, after applying the algorithm - resets to zero after being used
  223. calculated_number last_calculated_value; // the last calculated value processed
  224. calculated_number last_stored_value; // the last value as stored in the database (after interpolation)
  225. collected_number collected_value; // the current value, as collected - resets to 0 after being used
  226. collected_number last_collected_value; // the last value that was collected, after being processed
  227. // the *_volume members are used to calculate the accuracy of the rounding done by the
  228. // storage number - they are printed to debug.log when debug is enabled for a set.
  229. calculated_number collected_volume; // the sum of all collected values so far
  230. calculated_number stored_volume; // the sum of all stored values so far
  231. struct rrddim *next; // linking of dimensions within the same data set
  232. struct rrdset *rrdset;
  233. // ------------------------------------------------------------------------
  234. // members for checking the data when loading from disk
  235. long entries; // how many entries this dimension has in ram
  236. // this is the same to the entries of the data set
  237. // we set it here, to check the data when we load it from disk.
  238. int update_every; // every how many seconds is this updated
  239. size_t memsize; // the memory allocated for this dimension
  240. char magic[sizeof(RRDDIMENSION_MAGIC) + 1]; // a string to be saved, used to identify our data file
  241. struct rrddimvar *variables;
  242. // ------------------------------------------------------------------------
  243. // the values stored in this dimension, using our floating point numbers
  244. storage_number values[]; // the array of values - THIS HAS TO BE THE LAST MEMBER
  245. };
  246. // ----------------------------------------------------------------------------
  247. // iterator state for RRD dimension data collection
  248. union rrddim_collect_handle {
  249. struct {
  250. long slot;
  251. long entries;
  252. } slotted; // state the legacy code uses
  253. #ifdef ENABLE_DBENGINE
  254. struct rrdeng_collect_handle {
  255. struct rrdeng_page_descr *descr, *prev_descr;
  256. unsigned long page_correlation_id;
  257. struct rrdengine_instance *ctx;
  258. // set to 1 when this dimension is not page aligned with the other dimensions in the chart
  259. uint8_t unaligned_page;
  260. } rrdeng; // state the database engine uses
  261. #endif
  262. };
  263. // ----------------------------------------------------------------------------
  264. // iterator state for RRD dimension data queries
  265. struct rrddim_query_handle {
  266. RRDDIM *rd;
  267. time_t start_time;
  268. time_t end_time;
  269. union {
  270. struct {
  271. long slot;
  272. long last_slot;
  273. uint8_t finished;
  274. } slotted; // state the legacy code uses
  275. #ifdef ENABLE_DBENGINE
  276. struct rrdeng_query_handle {
  277. struct rrdeng_page_descr *descr;
  278. struct rrdengine_instance *ctx;
  279. struct pg_cache_page_index *page_index;
  280. time_t next_page_time;
  281. time_t now;
  282. unsigned position;
  283. } rrdeng; // state the database engine uses
  284. #endif
  285. };
  286. };
  287. // ----------------------------------------------------------------------------
  288. // volatile state per RRD dimension
  289. struct rrddim_volatile {
  290. #ifdef ENABLE_DBENGINE
  291. uuid_t *rrdeng_uuid; // database engine metric UUID
  292. uuid_t *metric_uuid; // global UUID for this metric (unique_across hosts)
  293. struct pg_cache_page_index *page_index;
  294. #endif
  295. union rrddim_collect_handle handle;
  296. // ------------------------------------------------------------------------
  297. // function pointers that handle data collection
  298. struct rrddim_collect_ops {
  299. // an initialization function to run before starting collection
  300. void (*init)(RRDDIM *rd);
  301. // run this to store each metric into the database
  302. void (*store_metric)(RRDDIM *rd, usec_t point_in_time, storage_number number);
  303. // an finalization function to run after collection is over
  304. // returns 1 if it's safe to delete the dimension
  305. int (*finalize)(RRDDIM *rd);
  306. } collect_ops;
  307. // function pointers that handle database queries
  308. struct rrddim_query_ops {
  309. // run this before starting a series of next_metric() database queries
  310. void (*init)(RRDDIM *rd, struct rrddim_query_handle *handle, time_t start_time, time_t end_time);
  311. // run this to load each metric number from the database
  312. storage_number (*next_metric)(struct rrddim_query_handle *handle, time_t *current_time);
  313. // run this to test if the series of next_metric() database queries is finished
  314. int (*is_finished)(struct rrddim_query_handle *handle);
  315. // run this after finishing a series of load_metric() database queries
  316. void (*finalize)(struct rrddim_query_handle *handle);
  317. // get the timestamp of the last entry of this metric
  318. time_t (*latest_time)(RRDDIM *rd);
  319. // get the timestamp of the first entry of this metric
  320. time_t (*oldest_time)(RRDDIM *rd);
  321. } query_ops;
  322. };
  323. // ----------------------------------------------------------------------------
  324. // volatile state per chart
  325. struct rrdset_volatile {
  326. char *old_title;
  327. char *old_context;
  328. struct label *new_labels;
  329. struct label_index labels;
  330. };
  331. // ----------------------------------------------------------------------------
  332. // these loop macros make sure the linked list is accessed with the right lock
  333. #define rrddim_foreach_read(rd, st) \
  334. for((rd) = (st)->dimensions, rrdset_check_rdlock(st); (rd) ; (rd) = (rd)->next)
  335. #define rrddim_foreach_write(rd, st) \
  336. for((rd) = (st)->dimensions, rrdset_check_wrlock(st); (rd) ; (rd) = (rd)->next)
  337. // ----------------------------------------------------------------------------
  338. // RRDSET - this is a chart
  339. // use this for configuration flags, not for state control
  340. // flags are set/unset in a manner that is not thread safe
  341. // and may lead to missing information.
  342. typedef enum rrdset_flags {
  343. RRDSET_FLAG_ENABLED = 1 << 0, // enables or disables a chart
  344. RRDSET_FLAG_DETAIL = 1 << 1, // if set, the data set should be considered as a detail of another
  345. // (the master data set should be the one that has the same family and is not detail)
  346. RRDSET_FLAG_DEBUG = 1 << 2, // enables or disables debugging for a chart
  347. RRDSET_FLAG_OBSOLETE = 1 << 3, // this is marked by the collector/module as obsolete
  348. RRDSET_FLAG_BACKEND_SEND = 1 << 4, // if set, this chart should be sent to backends
  349. RRDSET_FLAG_BACKEND_IGNORE = 1 << 5, // if set, this chart should not be sent to backends
  350. RRDSET_FLAG_UPSTREAM_SEND = 1 << 6, // if set, this chart should be sent upstream (streaming)
  351. RRDSET_FLAG_UPSTREAM_IGNORE = 1 << 7, // if set, this chart should not be sent upstream (streaming)
  352. RRDSET_FLAG_UPSTREAM_EXPOSED = 1 << 8, // if set, we have sent this chart definition to netdata parent (streaming)
  353. RRDSET_FLAG_STORE_FIRST = 1 << 9, // if set, do not eliminate the first collection during interpolation
  354. RRDSET_FLAG_HETEROGENEOUS = 1 << 10, // if set, the chart is not homogeneous (dimensions in it have multiple algorithms, multipliers or dividers)
  355. RRDSET_FLAG_HOMOGENEOUS_CHECK = 1 << 11, // if set, the chart should be checked to determine if the dimensions are homogeneous
  356. RRDSET_FLAG_HIDDEN = 1 << 12, // if set, do not show this chart on the dashboard, but use it for backends
  357. RRDSET_FLAG_SYNC_CLOCK = 1 << 13, // if set, microseconds on next data collection will be ignored (the chart will be synced to now)
  358. RRDSET_FLAG_OBSOLETE_DIMENSIONS = 1 << 14, // this is marked by the collector/module when a chart has obsolete dimensions
  359. // No new values have been collected for this chart since agent start or it was marked RRDSET_FLAG_OBSOLETE at
  360. // least rrdset_free_obsolete_time seconds ago.
  361. RRDSET_FLAG_ARCHIVED = 1 << 15,
  362. RRDSET_FLAG_ACLK = 1 << 16
  363. } RRDSET_FLAGS;
  364. #ifdef HAVE_C___ATOMIC
  365. #define rrdset_flag_check(st, flag) (__atomic_load_n(&((st)->flags), __ATOMIC_SEQ_CST) & (flag))
  366. #define rrdset_flag_set(st, flag) __atomic_or_fetch(&((st)->flags), flag, __ATOMIC_SEQ_CST)
  367. #define rrdset_flag_clear(st, flag) __atomic_and_fetch(&((st)->flags), ~flag, __ATOMIC_SEQ_CST)
  368. #else
  369. #define rrdset_flag_check(st, flag) ((st)->flags & (flag))
  370. #define rrdset_flag_set(st, flag) (st)->flags |= (flag)
  371. #define rrdset_flag_clear(st, flag) (st)->flags &= ~(flag)
  372. #endif
  373. #define rrdset_flag_check_noatomic(st, flag) ((st)->flags & (flag))
  374. struct rrdset {
  375. // ------------------------------------------------------------------------
  376. // binary indexing structures
  377. avl avl; // the index, with key the id - this has to be first!
  378. avl avlname; // the index, with key the name
  379. // ------------------------------------------------------------------------
  380. // the set configuration
  381. char id[RRD_ID_LENGTH_MAX + 1]; // id of the data set
  382. const char *name; // the name of this dimension (as presented to user)
  383. // this is a pointer to the config structure
  384. // since the config always has a higher priority
  385. // (the user overwrites the name of the charts)
  386. char *config_section; // the config section for the chart
  387. char *type; // the type of graph RRD_TYPE_* (a category, for determining graphing options)
  388. char *family; // grouping sets under the same family
  389. char *title; // title shown to user
  390. char *units; // units of measurement
  391. char *context; // the template of this data set
  392. uint32_t hash_context; // the hash of the chart's context
  393. RRDSET_TYPE chart_type; // line, area, stacked
  394. int update_every; // every how many seconds is this updated?
  395. long entries; // total number of entries in the data set
  396. long current_entry; // the entry that is currently being updated
  397. // it goes around in a round-robin fashion
  398. RRDSET_FLAGS flags; // configuration flags
  399. RRDSET_FLAGS *exporting_flags; // array of flags for exporting connector instances
  400. int gap_when_lost_iterations_above; // after how many lost iterations a gap should be stored
  401. // netdata will interpolate values for gaps lower than this
  402. long priority; // the sorting priority of this chart
  403. // ------------------------------------------------------------------------
  404. // members for temporary data we need for calculations
  405. RRD_MEMORY_MODE rrd_memory_mode; // if set to 1, this is memory mapped
  406. char *cache_dir; // the directory to store dimensions
  407. char cache_filename[FILENAME_MAX+1]; // the filename to store this set
  408. netdata_rwlock_t rrdset_rwlock; // protects dimensions linked list
  409. size_t counter; // the number of times we added values to this database
  410. size_t counter_done; // the number of times rrdset_done() has been called
  411. time_t last_accessed_time; // the last time this RRDSET has been accessed
  412. time_t upstream_resync_time; // the timestamp up to which we should resync clock upstream
  413. char *plugin_name; // the name of the plugin that generated this
  414. char *module_name; // the name of the plugin module that generated this
  415. uuid_t *chart_uuid; // Store the global GUID for this chart
  416. // this object.
  417. struct rrdset_volatile *state; // volatile state that is not persistently stored
  418. size_t unused[3];
  419. size_t rrddim_page_alignment; // keeps metric pages in alignment when using dbengine
  420. uint32_t hash; // a simple hash on the id, to speed up searching
  421. // we first compare hashes, and only if the hashes are equal we do string comparisons
  422. uint32_t hash_name; // a simple hash on the name
  423. usec_t usec_since_last_update; // the time in microseconds since the last collection of data
  424. struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
  425. struct timeval last_collected_time; // when did this data set last collected values
  426. total_number collected_total; // used internally to calculate percentages
  427. total_number last_collected_total; // used internally to calculate percentages
  428. RRDFAMILY *rrdfamily; // pointer to RRDFAMILY this chart belongs to
  429. RRDHOST *rrdhost; // pointer to RRDHOST this chart belongs to
  430. struct rrdset *next; // linking of rrdsets
  431. // ------------------------------------------------------------------------
  432. // local variables
  433. calculated_number green; // green threshold for this chart
  434. calculated_number red; // red threshold for this chart
  435. avl_tree_lock rrdvar_root_index; // RRDVAR index for this chart
  436. RRDSETVAR *variables; // RRDSETVAR linked list for this chart (one RRDSETVAR, many RRDVARs)
  437. RRDCALC *alarms; // RRDCALC linked list for this chart
  438. // ------------------------------------------------------------------------
  439. // members for checking the data when loading from disk
  440. unsigned long memsize; // how much mem we have allocated for this (without dimensions)
  441. char magic[sizeof(RRDSET_MAGIC) + 1]; // our magic
  442. // ------------------------------------------------------------------------
  443. // the dimensions
  444. avl_tree_lock dimensions_index; // the root of the dimensions index
  445. RRDDIM *dimensions; // the actual data for every dimension
  446. };
  447. #define rrdset_rdlock(st) netdata_rwlock_rdlock(&((st)->rrdset_rwlock))
  448. #define rrdset_wrlock(st) netdata_rwlock_wrlock(&((st)->rrdset_rwlock))
  449. #define rrdset_unlock(st) netdata_rwlock_unlock(&((st)->rrdset_rwlock))
  450. // ----------------------------------------------------------------------------
  451. // these loop macros make sure the linked list is accessed with the right lock
  452. #define rrdset_foreach_read(st, host) \
  453. for((st) = (host)->rrdset_root, rrdhost_check_rdlock(host); st ; (st) = (st)->next)
  454. #define rrdset_foreach_write(st, host) \
  455. for((st) = (host)->rrdset_root, rrdhost_check_wrlock(host); st ; (st) = (st)->next)
  456. // ----------------------------------------------------------------------------
  457. // RRDHOST flags
  458. // use this for configuration flags, not for state control
  459. // flags are set/unset in a manner that is not thread safe
  460. // and may lead to missing information.
  461. typedef enum rrdhost_flags {
  462. RRDHOST_FLAG_ORPHAN = 1 << 0, // this host is orphan (not receiving data)
  463. RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS = 1 << 1, // delete files of obsolete charts
  464. RRDHOST_FLAG_DELETE_ORPHAN_HOST = 1 << 2, // delete the entire host when orphan
  465. RRDHOST_FLAG_BACKEND_SEND = 1 << 3, // send it to backends
  466. RRDHOST_FLAG_BACKEND_DONT_SEND = 1 << 4, // don't send it to backends
  467. RRDHOST_FLAG_ARCHIVED = 1 << 5, // The host is archived, no collected charts yet
  468. RRDHOST_FLAG_MULTIHOST = 1 << 6, // Host belongs to localhost/megadb
  469. } RRDHOST_FLAGS;
  470. #ifdef HAVE_C___ATOMIC
  471. #define rrdhost_flag_check(host, flag) (__atomic_load_n(&((host)->flags), __ATOMIC_SEQ_CST) & (flag))
  472. #define rrdhost_flag_set(host, flag) __atomic_or_fetch(&((host)->flags), flag, __ATOMIC_SEQ_CST)
  473. #define rrdhost_flag_clear(host, flag) __atomic_and_fetch(&((host)->flags), ~flag, __ATOMIC_SEQ_CST)
  474. #else
  475. #define rrdhost_flag_check(host, flag) ((host)->flags & (flag))
  476. #define rrdhost_flag_set(host, flag) (host)->flags |= (flag)
  477. #define rrdhost_flag_clear(host, flag) (host)->flags &= ~(flag)
  478. #endif
  479. #ifdef NETDATA_INTERNAL_CHECKS
  480. #define rrdset_debug(st, fmt, args...) do { if(unlikely(debug_flags & D_RRD_STATS && rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) \
  481. debug_int(__FILE__, __FUNCTION__, __LINE__, "%s: " fmt, st->name, ##args); } while(0)
  482. #else
  483. #define rrdset_debug(st, fmt, args...) debug_dummy()
  484. #endif
  485. // ----------------------------------------------------------------------------
  486. // Health data
  487. struct alarm_entry {
  488. uint32_t unique_id;
  489. uint32_t alarm_id;
  490. uint32_t alarm_event_id;
  491. time_t when;
  492. time_t duration;
  493. time_t non_clear_duration;
  494. char *name;
  495. uint32_t hash_name;
  496. char *chart;
  497. uint32_t hash_chart;
  498. char *family;
  499. char *exec;
  500. char *recipient;
  501. time_t exec_run_timestamp;
  502. int exec_code;
  503. uint64_t exec_spawn_serial;
  504. char *source;
  505. char *units;
  506. char *info;
  507. calculated_number old_value;
  508. calculated_number new_value;
  509. char *old_value_string;
  510. char *new_value_string;
  511. RRDCALC_STATUS old_status;
  512. RRDCALC_STATUS new_status;
  513. uint32_t flags;
  514. int delay;
  515. time_t delay_up_to_timestamp;
  516. uint32_t updated_by_id;
  517. uint32_t updates_id;
  518. time_t last_repeat;
  519. struct alarm_entry *next;
  520. struct alarm_entry *next_in_progress;
  521. struct alarm_entry *prev_in_progress;
  522. };
  523. typedef struct alarm_log {
  524. uint32_t next_log_id;
  525. uint32_t next_alarm_id;
  526. unsigned int count;
  527. unsigned int max;
  528. ALARM_ENTRY *alarms;
  529. netdata_rwlock_t alarm_log_rwlock;
  530. } ALARM_LOG;
  531. // ----------------------------------------------------------------------------
  532. // RRD HOST
  533. struct rrdhost_system_info {
  534. char *host_os_name;
  535. char *host_os_id;
  536. char *host_os_id_like;
  537. char *host_os_version;
  538. char *host_os_version_id;
  539. char *host_os_detection;
  540. char *host_cores;
  541. char *host_cpu_freq;
  542. char *host_ram_total;
  543. char *host_disk_space;
  544. char *container_os_name;
  545. char *container_os_id;
  546. char *container_os_id_like;
  547. char *container_os_version;
  548. char *container_os_version_id;
  549. char *container_os_detection;
  550. char *kernel_name;
  551. char *kernel_version;
  552. char *architecture;
  553. char *virtualization;
  554. char *virt_detection;
  555. char *container;
  556. char *container_detection;
  557. char *is_k8s_node;
  558. };
  559. struct rrdhost {
  560. avl avl; // the index of hosts
  561. // ------------------------------------------------------------------------
  562. // host information
  563. char *hostname; // the hostname of this host
  564. uint32_t hash_hostname; // the hostname hash
  565. char *registry_hostname; // the registry hostname for this host
  566. char machine_guid[GUID_LEN + 1]; // the unique ID of this host
  567. uint32_t hash_machine_guid; // the hash of the unique ID
  568. const char *os; // the O/S type of the host
  569. const char *tags; // tags for this host
  570. const char *timezone; // the timezone of the host
  571. RRDHOST_FLAGS flags; // flags about this RRDHOST
  572. RRDHOST_FLAGS *exporting_flags; // array of flags for exporting connector instances
  573. int rrd_update_every; // the update frequency of the host
  574. long rrd_history_entries; // the number of history entries for the host's charts
  575. RRD_MEMORY_MODE rrd_memory_mode; // the memory more for the charts of this host
  576. char *cache_dir; // the directory to save RRD cache files
  577. char *varlib_dir; // the directory to save health log
  578. char *program_name; // the program name that collects metrics for this host
  579. char *program_version; // the program version that collects metrics for this host
  580. struct rrdhost_system_info *system_info; // information collected from the host environment
  581. // ------------------------------------------------------------------------
  582. // streaming of data to remote hosts - rrdpush
  583. unsigned int rrdpush_send_enabled:1; // 1 when this host sends metrics to another netdata
  584. char *rrdpush_send_destination; // where to send metrics to
  585. char *rrdpush_send_api_key; // the api key at the receiving netdata
  586. // the following are state information for the threading
  587. // streaming metrics from this netdata to an upstream netdata
  588. struct sender_state *sender;
  589. volatile unsigned int rrdpush_sender_spawn:1; // 1 when the sender thread has been spawn
  590. netdata_thread_t rrdpush_sender_thread; // the sender thread
  591. volatile unsigned int rrdpush_sender_connected:1; // 1 when the sender is ready to push metrics
  592. int rrdpush_sender_socket; // the fd of the socket to the remote host, or -1
  593. volatile unsigned int rrdpush_sender_error_shown:1; // 1 when we have logged a communication error
  594. volatile unsigned int rrdpush_sender_join:1; // 1 when we have to join the sending thread
  595. SIMPLE_PATTERN *rrdpush_send_charts_matching; // pattern to match the charts to be sent
  596. int rrdpush_sender_pipe[2]; // collector to sender thread signaling
  597. //BUFFER *rrdpush_sender_buffer; // collector fills it, sender sends it
  598. //uint32_t stream_version; //Set the current version of the stream.
  599. // ------------------------------------------------------------------------
  600. // streaming of data from remote hosts - rrdpush
  601. volatile size_t connected_senders; // when remote hosts are streaming to this
  602. // host, this is the counter of connected clients
  603. time_t senders_disconnected_time; // the time the last sender was disconnected
  604. struct receiver_state *receiver;
  605. netdata_mutex_t receiver_lock;
  606. // ------------------------------------------------------------------------
  607. // health monitoring options
  608. unsigned int health_enabled:1; // 1 when this host has health enabled
  609. time_t health_delay_up_to; // a timestamp to delay alarms processing up to
  610. char *health_default_exec; // the full path of the alarms notifications program
  611. char *health_default_recipient; // the default recipient for all alarms
  612. char *health_log_filename; // the alarms event log filename
  613. size_t health_log_entries_written; // the number of alarm events writtern to the alarms event log
  614. FILE *health_log_fp; // the FILE pointer to the open alarms event log file
  615. uint32_t health_default_warn_repeat_every; // the default value for the interval between repeating warning notifications
  616. uint32_t health_default_crit_repeat_every; // the default value for the interval between repeating critical notifications
  617. // all RRDCALCs are primarily allocated and linked here
  618. // RRDCALCs may be linked to charts at any point
  619. // (charts may or may not exist when these are loaded)
  620. RRDCALC *alarms;
  621. RRDCALC *alarms_with_foreach;
  622. avl_tree_lock alarms_idx_health_log;
  623. avl_tree_lock alarms_idx_name;
  624. ALARM_LOG health_log; // alarms historical events (event log)
  625. uint32_t health_last_processed_id; // the last processed health id from the log
  626. uint32_t health_max_unique_id; // the max alarm log unique id given for the host
  627. uint32_t health_max_alarm_id; // the max alarm id given for the host
  628. // templates of alarms
  629. // these are used to create alarms when charts
  630. // are created or renamed, that match them
  631. RRDCALCTEMPLATE *templates;
  632. RRDCALCTEMPLATE *alarms_template_with_foreach;
  633. // ------------------------------------------------------------------------
  634. // the charts of the host
  635. RRDSET *rrdset_root; // the host charts
  636. // ------------------------------------------------------------------------
  637. // locks
  638. netdata_rwlock_t rrdhost_rwlock; // lock for this RRDHOST (protects rrdset_root linked list)
  639. // ------------------------------------------------------------------------
  640. // Support for host-level labels
  641. struct label_index labels;
  642. // ------------------------------------------------------------------------
  643. // indexes
  644. avl_tree_lock rrdset_root_index; // the host's charts index (by id)
  645. avl_tree_lock rrdset_root_index_name; // the host's charts index (by name)
  646. avl_tree_lock rrdfamily_root_index; // the host's chart families index
  647. avl_tree_lock rrdvar_root_index; // the host's chart variables index
  648. #ifdef ENABLE_DBENGINE
  649. struct rrdengine_instance *rrdeng_ctx; // DB engine instance for this host
  650. uuid_t host_uuid; // Global GUID for this host
  651. #endif
  652. #ifdef ENABLE_HTTPS
  653. struct netdata_ssl ssl; //Structure used to encrypt the connection
  654. struct netdata_ssl stream_ssl; //Structure used to encrypt the stream
  655. #endif
  656. netdata_mutex_t aclk_state_lock;
  657. aclk_rrdhost_state aclk_state;
  658. struct rrdhost *next;
  659. };
  660. extern RRDHOST *localhost;
  661. #define rrdhost_rdlock(host) netdata_rwlock_rdlock(&((host)->rrdhost_rwlock))
  662. #define rrdhost_wrlock(host) netdata_rwlock_wrlock(&((host)->rrdhost_rwlock))
  663. #define rrdhost_unlock(host) netdata_rwlock_unlock(&((host)->rrdhost_rwlock))
  664. #define rrdhost_aclk_state_lock(host) netdata_mutex_lock(&((host)->aclk_state_lock))
  665. #define rrdhost_aclk_state_unlock(host) netdata_mutex_unlock(&((host)->aclk_state_lock))
  666. // ----------------------------------------------------------------------------
  667. // these loop macros make sure the linked list is accessed with the right lock
  668. #define rrdhost_foreach_read(var) \
  669. for((var) = localhost, rrd_check_rdlock(); var ; (var) = (var)->next)
  670. #define rrdhost_foreach_write(var) \
  671. for((var) = localhost, rrd_check_wrlock(); var ; (var) = (var)->next)
  672. // ----------------------------------------------------------------------------
  673. // global lock for all RRDHOSTs
  674. extern netdata_rwlock_t rrd_rwlock;
  675. #define rrd_rdlock() netdata_rwlock_rdlock(&rrd_rwlock)
  676. #define rrd_wrlock() netdata_rwlock_wrlock(&rrd_rwlock)
  677. #define rrd_unlock() netdata_rwlock_unlock(&rrd_rwlock)
  678. // ----------------------------------------------------------------------------
  679. extern size_t rrd_hosts_available;
  680. extern time_t rrdhost_free_orphan_time;
  681. extern int rrd_init(char *hostname, struct rrdhost_system_info *system_info);
  682. extern RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash);
  683. extern RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash);
  684. extern RRDHOST *rrdhost_find_or_create(
  685. const char *hostname
  686. , const char *registry_hostname
  687. , const char *guid
  688. , const char *os
  689. , const char *timezone
  690. , const char *tags
  691. , const char *program_name
  692. , const char *program_version
  693. , int update_every
  694. , long history
  695. , RRD_MEMORY_MODE mode
  696. , unsigned int health_enabled
  697. , unsigned int rrdpush_enabled
  698. , char *rrdpush_destination
  699. , char *rrdpush_api_key
  700. , char *rrdpush_send_charts_matching
  701. , struct rrdhost_system_info *system_info
  702. );
  703. extern void rrdhost_update(RRDHOST *host
  704. , const char *hostname
  705. , const char *registry_hostname
  706. , const char *guid
  707. , const char *os
  708. , const char *timezone
  709. , const char *tags
  710. , const char *program_name
  711. , const char *program_version
  712. , int update_every
  713. , long history
  714. , RRD_MEMORY_MODE mode
  715. , unsigned int health_enabled
  716. , unsigned int rrdpush_enabled
  717. , char *rrdpush_destination
  718. , char *rrdpush_api_key
  719. , char *rrdpush_send_charts_matching
  720. , struct rrdhost_system_info *system_info
  721. );
  722. extern int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value);
  723. #if defined(NETDATA_INTERNAL_CHECKS) && defined(NETDATA_VERIFY_LOCKS)
  724. extern void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line);
  725. extern void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line);
  726. extern void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line);
  727. extern void __rrdset_check_wrlock(RRDSET *st, const char *file, const char *function, const unsigned long line);
  728. extern void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line);
  729. extern void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line);
  730. #define rrdhost_check_rdlock(host) __rrdhost_check_rdlock(host, __FILE__, __FUNCTION__, __LINE__)
  731. #define rrdhost_check_wrlock(host) __rrdhost_check_wrlock(host, __FILE__, __FUNCTION__, __LINE__)
  732. #define rrdset_check_rdlock(st) __rrdset_check_rdlock(st, __FILE__, __FUNCTION__, __LINE__)
  733. #define rrdset_check_wrlock(st) __rrdset_check_wrlock(st, __FILE__, __FUNCTION__, __LINE__)
  734. #define rrd_check_rdlock() __rrd_check_rdlock(__FILE__, __FUNCTION__, __LINE__)
  735. #define rrd_check_wrlock() __rrd_check_wrlock(__FILE__, __FUNCTION__, __LINE__)
  736. #else
  737. #define rrdhost_check_rdlock(host) (void)0
  738. #define rrdhost_check_wrlock(host) (void)0
  739. #define rrdset_check_rdlock(st) (void)0
  740. #define rrdset_check_wrlock(st) (void)0
  741. #define rrd_check_rdlock() (void)0
  742. #define rrd_check_wrlock() (void)0
  743. #endif
  744. // ----------------------------------------------------------------------------
  745. // RRDSET functions
  746. extern int rrdset_set_name(RRDSET *st, const char *name);
  747. extern RRDSET *rrdset_create_custom(RRDHOST *host
  748. , const char *type
  749. , const char *id
  750. , const char *name
  751. , const char *family
  752. , const char *context
  753. , const char *title
  754. , const char *units
  755. , const char *plugin
  756. , const char *module
  757. , long priority
  758. , int update_every
  759. , RRDSET_TYPE chart_type
  760. , RRD_MEMORY_MODE memory_mode
  761. , long history_entries);
  762. #define rrdset_create(host, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  763. 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)
  764. #define rrdset_create_localhost(type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  765. rrdset_create(localhost, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type)
  766. extern void rrdhost_free_all(void);
  767. extern void rrdhost_save_all(void);
  768. extern void rrdhost_cleanup_all(void);
  769. extern void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected);
  770. extern void rrdhost_system_info_free(struct rrdhost_system_info *system_info);
  771. extern void rrdhost_free(RRDHOST *host);
  772. extern void rrdhost_save_charts(RRDHOST *host);
  773. extern void rrdhost_delete_charts(RRDHOST *host);
  774. extern int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now);
  775. extern void rrdset_update_heterogeneous_flag(RRDSET *st);
  776. extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
  777. #define rrdset_find_localhost(id) rrdset_find(localhost, id)
  778. /* This will not return charts that are archived */
  779. static inline RRDSET *rrdset_find_active_localhost(const char *id)
  780. {
  781. RRDSET *st = rrdset_find_localhost(id);
  782. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  783. return NULL;
  784. return st;
  785. }
  786. extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
  787. #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
  788. /* This will not return charts that are archived */
  789. static inline RRDSET *rrdset_find_active_bytype_localhost(const char *type, const char *id)
  790. {
  791. RRDSET *st = rrdset_find_bytype_localhost(type, id);
  792. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  793. return NULL;
  794. return st;
  795. }
  796. extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
  797. #define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
  798. /* This will not return charts that are archived */
  799. static inline RRDSET *rrdset_find_active_byname_localhost(const char *name)
  800. {
  801. RRDSET *st = rrdset_find_byname_localhost(name);
  802. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  803. return NULL;
  804. return st;
  805. }
  806. extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
  807. extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
  808. #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
  809. extern void rrdset_done(RRDSET *st);
  810. extern void rrdset_is_obsolete(RRDSET *st);
  811. extern void rrdset_isnot_obsolete(RRDSET *st);
  812. // checks if the RRDSET should be offered to viewers
  813. #define rrdset_is_available_for_viewers(st) (rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && !rrdset_flag_check(st, RRDSET_FLAG_HIDDEN) && !rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && (st)->dimensions && (st)->rrd_memory_mode != RRD_MEMORY_MODE_NONE)
  814. #define rrdset_is_available_for_backends(st) (rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && !rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && (st)->dimensions)
  815. #define rrdset_is_archived(st) (rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && (st)->dimensions)
  816. // get the total duration in seconds of the round robin database
  817. #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
  818. // get the timestamp of the last entry in the round robin database
  819. static inline time_t rrdset_last_entry_t_nolock(RRDSET *st)
  820. {
  821. if (st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  822. RRDDIM *rd;
  823. time_t last_entry_t = 0;
  824. rrddim_foreach_read(rd, st) {
  825. last_entry_t = MAX(last_entry_t, rd->state->query_ops.latest_time(rd));
  826. }
  827. return last_entry_t;
  828. } else {
  829. return (time_t)st->last_updated.tv_sec;
  830. }
  831. }
  832. static inline time_t rrdset_last_entry_t(RRDSET *st)
  833. {
  834. time_t last_entry_t;
  835. netdata_rwlock_rdlock(&st->rrdset_rwlock);
  836. last_entry_t = rrdset_last_entry_t_nolock(st);
  837. netdata_rwlock_unlock(&st->rrdset_rwlock);
  838. return last_entry_t;
  839. }
  840. // get the timestamp of first entry in the round robin database
  841. static inline time_t rrdset_first_entry_t_nolock(RRDSET *st)
  842. {
  843. if (st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  844. RRDDIM *rd;
  845. time_t first_entry_t = LONG_MAX;
  846. rrddim_foreach_read(rd, st) {
  847. first_entry_t = MIN(first_entry_t, rd->state->query_ops.oldest_time(rd));
  848. }
  849. if (unlikely(LONG_MAX == first_entry_t)) return 0;
  850. return first_entry_t;
  851. } else {
  852. return (time_t)(rrdset_last_entry_t_nolock(st) - rrdset_duration(st));
  853. }
  854. }
  855. static inline time_t rrdset_first_entry_t(RRDSET *st)
  856. {
  857. time_t first_entry_t;
  858. netdata_rwlock_rdlock(&st->rrdset_rwlock);
  859. first_entry_t = rrdset_first_entry_t_nolock(st);
  860. netdata_rwlock_unlock(&st->rrdset_rwlock);
  861. return first_entry_t;
  862. }
  863. // get the timestamp of the last entry in the round robin database
  864. static inline time_t rrddim_last_entry_t(RRDDIM *rd) {
  865. if (rd->rrdset->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  866. return rd->state->query_ops.latest_time(rd);
  867. return (time_t)rd->rrdset->last_updated.tv_sec;
  868. }
  869. static inline time_t rrddim_first_entry_t(RRDDIM *rd) {
  870. if (rd->rrdset->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  871. return rd->state->query_ops.oldest_time(rd);
  872. return (time_t)(rd->rrdset->last_updated.tv_sec - rrdset_duration(rd->rrdset));
  873. }
  874. time_t rrdhost_last_entry_t(RRDHOST *h);
  875. // get the last slot updated in the round robin database
  876. #define rrdset_last_slot(st) ((size_t)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
  877. // get the first / oldest slot updated in the round robin database
  878. // #define rrdset_first_slot(st) ((size_t)( (((st)->counter >= ((unsigned long)(st)->entries)) ? (unsigned long)( ((unsigned long)(st)->current_entry > 0) ? ((unsigned long)(st)->current_entry) : ((unsigned long)(st)->entries) ) - 1 : 0) ))
  879. // return the slot that has the oldest value
  880. static inline size_t rrdset_first_slot(RRDSET *st) {
  881. if(st->counter >= (size_t)st->entries) {
  882. // the database has been rotated at least once
  883. // the oldest entry is the one that will be next
  884. // overwritten by data collection
  885. return (size_t)st->current_entry;
  886. }
  887. // we do not have rotated the db yet
  888. // so 0 is the first entry
  889. return 0;
  890. }
  891. // get the slot of the round robin database, for the given timestamp (t)
  892. // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
  893. // only valid when not using dbengine
  894. static inline size_t rrdset_time2slot(RRDSET *st, time_t t) {
  895. size_t ret = 0;
  896. time_t last_entry_t = rrdset_last_entry_t_nolock(st);
  897. time_t first_entry_t = rrdset_first_entry_t_nolock(st);
  898. if(t >= last_entry_t) {
  899. // the requested time is after the last entry we have
  900. ret = rrdset_last_slot(st);
  901. }
  902. else {
  903. if(t <= first_entry_t) {
  904. // the requested time is before the first entry we have
  905. ret = rrdset_first_slot(st);
  906. }
  907. else {
  908. if(rrdset_last_slot(st) >= ((last_entry_t - t) / (size_t)(st->update_every)))
  909. ret = rrdset_last_slot(st) - ((last_entry_t - t) / (size_t)(st->update_every));
  910. else
  911. ret = rrdset_last_slot(st) - ((last_entry_t - t) / (size_t)(st->update_every)) + (unsigned long)st->entries;
  912. }
  913. }
  914. if(unlikely(ret >= (size_t)st->entries)) {
  915. error("INTERNAL ERROR: rrdset_time2slot() on %s returns values outside entries", st->name);
  916. ret = (size_t)(st->entries - 1);
  917. }
  918. return ret;
  919. }
  920. // get the timestamp of a specific slot in the round robin database
  921. // only valid when not using dbengine
  922. static inline time_t rrdset_slot2time(RRDSET *st, size_t slot) {
  923. time_t ret;
  924. time_t last_entry_t = rrdset_last_entry_t_nolock(st);
  925. time_t first_entry_t = rrdset_first_entry_t_nolock(st);
  926. if(slot >= (size_t)st->entries) {
  927. error("INTERNAL ERROR: caller of rrdset_slot2time() gives invalid slot %zu", slot);
  928. slot = (size_t)st->entries - 1;
  929. }
  930. if(slot > rrdset_last_slot(st)) {
  931. ret = last_entry_t - (size_t)st->update_every * (rrdset_last_slot(st) - slot + (size_t)st->entries);
  932. }
  933. else {
  934. ret = last_entry_t - (size_t)st->update_every;
  935. }
  936. if(unlikely(ret < first_entry_t)) {
  937. error("INTERNAL ERROR: rrdset_slot2time() on %s returns time too far in the past", st->name);
  938. ret = first_entry_t;
  939. }
  940. if(unlikely(ret > last_entry_t)) {
  941. error("INTERNAL ERROR: rrdset_slot2time() on %s returns time into the future", st->name);
  942. ret = last_entry_t;
  943. }
  944. return ret;
  945. }
  946. // ----------------------------------------------------------------------------
  947. // RRD DIMENSION functions
  948. extern void rrdcalc_link_to_rrddim(RRDDIM *rd, RRDSET *st, RRDHOST *host);
  949. extern RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collected_number multiplier,
  950. collected_number divisor, RRD_ALGORITHM algorithm, RRD_MEMORY_MODE memory_mode);//,
  951. //int is_archived, uuid_t *dim_uuid);
  952. #define rrddim_add(st, id, name, multiplier, divisor, algorithm) rrddim_add_custom(st, id, name, multiplier, divisor, \
  953. algorithm, (st)->rrd_memory_mode)//, 0, NULL)
  954. extern int rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
  955. extern int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm);
  956. extern int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier);
  957. extern int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor);
  958. extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
  959. /* This will not return dimensions that are archived */
  960. static inline RRDDIM *rrddim_find_active(RRDSET *st, const char *id)
  961. {
  962. RRDDIM *rd = rrddim_find(st, id);
  963. if (unlikely(rd && rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)))
  964. return NULL;
  965. return rd;
  966. }
  967. extern int rrddim_hide(RRDSET *st, const char *id);
  968. extern int rrddim_unhide(RRDSET *st, const char *id);
  969. extern void rrddim_is_obsolete(RRDSET *st, RRDDIM *rd);
  970. extern void rrddim_isnot_obsolete(RRDSET *st, RRDDIM *rd);
  971. extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
  972. extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
  973. extern long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries);
  974. // ----------------------------------------------------------------------------
  975. // Miscellaneous functions
  976. extern int alarm_compare_id(void *a, void *b);
  977. extern int alarm_compare_name(void *a, void *b);
  978. // ----------------------------------------------------------------------------
  979. // RRD internal functions
  980. #ifdef NETDATA_RRD_INTERNALS
  981. extern avl_tree_lock rrdhost_root_index;
  982. extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
  983. extern char *rrdset_cache_dir(RRDHOST *host, const char *id, const char *config_section);
  984. #define rrddim_free(st, rd) rrddim_free_custom(st, rd, 0)
  985. extern void rrddim_free_custom(RRDSET *st, RRDDIM *rd, int db_rotated);
  986. extern int rrddim_compare(void* a, void* b);
  987. extern int rrdset_compare(void* a, void* b);
  988. extern int rrdset_compare_name(void* a, void* b);
  989. extern int rrdfamily_compare(void *a, void *b);
  990. extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
  991. extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
  992. #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
  993. #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
  994. extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
  995. extern void rrdset_free(RRDSET *st);
  996. extern void rrdset_reset(RRDSET *st);
  997. extern void rrdset_save(RRDSET *st);
  998. #define rrdset_delete(st) rrdset_delete_custom(st, 0)
  999. extern void rrdset_delete_custom(RRDSET *st, int db_rotated);
  1000. extern void rrdset_delete_obsolete_dimensions(RRDSET *st);
  1001. extern void rrdhost_cleanup_obsolete_charts(RRDHOST *host);
  1002. extern RRDHOST *rrdhost_create(
  1003. const char *hostname, const char *registry_hostname, const char *guid, const char *os, const char *timezone,
  1004. const char *tags, const char *program_name, const char *program_version, int update_every, long entries,
  1005. RRD_MEMORY_MODE memory_mode, unsigned int health_enabled, unsigned int rrdpush_enabled, char *rrdpush_destination,
  1006. char *rrdpush_api_key, char *rrdpush_send_charts_matching, struct rrdhost_system_info *system_info,
  1007. int is_localhost); //TODO: Remove , int is_archived);
  1008. #endif /* NETDATA_RRD_INTERNALS */
  1009. extern void set_host_properties(
  1010. RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname, const char *registry_hostname,
  1011. const char *guid, const char *os, const char *tags, const char *tzone, const char *program_name,
  1012. const char *program_version);
  1013. // ----------------------------------------------------------------------------
  1014. // RRD DB engine declarations
  1015. #ifdef ENABLE_DBENGINE
  1016. #include "database/engine/rrdengineapi.h"
  1017. #include "sqlite/sqlite_functions.h"
  1018. #endif
  1019. #endif /* NETDATA_RRD_H */