rrd.h 52 KB

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