2 #define NETDATA_RRD_H 1
5 #define UPDATE_EVERY_MAX 3600
7 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
8 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
10 extern int default_rrd_update_every;
11 extern int default_rrd_history_entries;
13 #define RRD_ID_LENGTH_MAX 200
15 #define RRDSET_MAGIC "NETDATA RRD SET FILE V019"
16 #define RRDDIMENSION_MAGIC "NETDATA RRD DIMENSION FILE V019"
18 typedef long long total_number;
19 #define TOTAL_NUMBER_FORMAT "%lld"
21 // ----------------------------------------------------------------------------
24 typedef enum rrdset_type {
27 RRDSET_TYPE_STACKED = 2
30 #define RRDSET_TYPE_LINE_NAME "line"
31 #define RRDSET_TYPE_AREA_NAME "area"
32 #define RRDSET_TYPE_STACKED_NAME "stacked"
34 RRDSET_TYPE rrdset_type_id(const char *name);
35 const char *rrdset_type_name(RRDSET_TYPE chart_type);
38 // ----------------------------------------------------------------------------
41 typedef enum rrd_memory_mode {
42 RRD_MEMORY_MODE_RAM = 0,
43 RRD_MEMORY_MODE_MAP = 1,
44 RRD_MEMORY_MODE_SAVE = 2
47 #define RRD_MEMORY_MODE_RAM_NAME "ram"
48 #define RRD_MEMORY_MODE_MAP_NAME "map"
49 #define RRD_MEMORY_MODE_SAVE_NAME "save"
51 extern RRD_MEMORY_MODE default_rrd_memory_mode;
53 extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
54 extern RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
57 // ----------------------------------------------------------------------------
60 typedef enum rrd_algorithm {
61 RRD_ALGORITHM_ABSOLUTE = 0,
62 RRD_ALGORITHM_INCREMENTAL = 1,
63 RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
64 RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3
67 #define RRD_ALGORITHM_ABSOLUTE_NAME "absolute"
68 #define RRD_ALGORITHM_INCREMENTAL_NAME "incremental"
69 #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
70 #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
72 extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
73 extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
75 // ----------------------------------------------------------------------------
78 typedef enum rrddim_flags {
79 RRDDIM_FLAG_HIDDEN = 1 << 0, // this dimension will not be offered to callers
80 RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = 1 << 1, // do not offer RESET or OVERFLOW info to callers
81 RRDDIM_FLAG_UPDATED = 1 << 2, // the dimension has been updated since the last processing
82 RRDDIM_FLAG_EXPOSED = 1 << 3 // when set what have sent this dimension to the central netdata
85 #define rrddim_flag_check(rd, flag) ((rd)->flags & flag)
86 #define rrddim_flag_set(rd, flag) (rd)->flags |= flag
87 #define rrddim_flag_clear(rd, flag) (rd)->flags &= ~flag
90 // ----------------------------------------------------------------------------
101 avl_tree_lock variables_root_index;
103 typedef struct rrdfamily RRDFAMILY;
106 // ----------------------------------------------------------------------------
107 // RRD DIMENSION - this is a metric
110 // ------------------------------------------------------------------------
111 // binary indexing structures
113 avl avl; // the binary index - this has to be first member!
115 // ------------------------------------------------------------------------
116 // the dimension definition
118 const char *id; // the id of this dimension (for internal identification)
119 const char *name; // the name of this dimension (as presented to user)
120 // this is a pointer to the config structure
121 // since the config always has a higher priority
122 // (the user overwrites the name of the charts)
123 // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
125 RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
126 RRD_MEMORY_MODE rrd_memory_mode; // the memory mode for this dimension
128 collected_number multiplier; // the multiplier of the collected values
129 collected_number divisor; // the divider of the collected values
131 uint32_t flags; // options and status options for the dimension
133 // ------------------------------------------------------------------------
134 // members for temporary data we need for calculations
136 uint32_t hash; // a simple hash of the id, to speed up searching / indexing
137 // instead of strcmp() every item in the binary index
138 // we first compare the hashes
140 uint32_t hash_name; // a simple hash of the name
142 char *cache_filename; // the filename we load/save from/to this set
144 size_t counter; // the number of times we added values to this rrdim
146 struct timeval last_collected_time; // when was this dimension last updated
147 // this is actual date time we updated the last_collected_value
148 // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
150 calculated_number calculated_value; // the current calculated value, after applying the algorithm - resets to zero after being used
151 calculated_number last_calculated_value; // the last calculated value processed
153 calculated_number last_stored_value; // the last value as stored in the database (after interpolation)
155 collected_number collected_value; // the current value, as collected - resets to 0 after being used
156 collected_number last_collected_value; // the last value that was collected, after being processed
158 // the *_volume members are used to calculate the accuracy of the rounding done by the
159 // storage number - they are printed to debug.log when debug is enabled for a set.
160 calculated_number collected_volume; // the sum of all collected values so far
161 calculated_number stored_volume; // the sum of all stored values so far
163 struct rrddim *next; // linking of dimensions within the same data set
164 struct rrdset *rrdset;
166 // ------------------------------------------------------------------------
167 // members for checking the data when loading from disk
169 long entries; // how many entries this dimension has in ram
170 // this is the same to the entries of the data set
171 // we set it here, to check the data when we load it from disk.
173 int update_every; // every how many seconds is this updated
175 size_t memsize; // the memory allocated for this dimension
177 char magic[sizeof(RRDDIMENSION_MAGIC) + 1]; // a string to be saved, used to identify our data file
179 struct rrddimvar *variables;
181 // ------------------------------------------------------------------------
182 // the values stored in this dimension, using our floating point numbers
184 storage_number values[]; // the array of values - THIS HAS TO BE THE LAST MEMBER
186 typedef struct rrddim RRDDIM;
188 // ----------------------------------------------------------------------------
189 // these loop macros make sure the linked list is accessed with the right lock
191 #define rrddim_foreach_read(rd, st) \
192 for(rd = st->dimensions, rrdset_check_rdlock(st); rd ; rd = rd->next)
194 #define rrddim_foreach_write(rd, st) \
195 for(rd = st->dimensions, rrdset_check_wrlock(st); rd ; rd = rd->next)
198 // ----------------------------------------------------------------------------
199 // RRDSET - this is a chart
201 typedef enum rrdset_flags {
202 RRDSET_FLAG_ENABLED = 1 << 0, // enables or disables a chart
203 RRDSET_FLAG_DETAIL = 1 << 1, // if set, the data set should be considered as a detail of another
204 // (the master data set should be the one that has the same family and is not detail)
205 RRDSET_FLAG_DEBUG = 1 << 2 // enables or disables debugging for a chart
208 #define rrdset_flag_check(st, flag) ((st)->flags & flag)
209 #define rrdset_flag_set(st, flag) (st)->flags |= flag
210 #define rrdset_flag_clear(st, flag) (st)->flags &= ~flag
213 // ------------------------------------------------------------------------
214 // binary indexing structures
216 avl avl; // the index, with key the id - this has to be first!
217 avl avlname; // the index, with key the name
219 // ------------------------------------------------------------------------
220 // the set configuration
222 char id[RRD_ID_LENGTH_MAX + 1]; // id of the data set
224 const char *name; // the name of this dimension (as presented to user)
225 // this is a pointer to the config structure
226 // since the config always has a higher priority
227 // (the user overwrites the name of the charts)
229 char *config_section; // the config section for the chart
231 char *type; // the type of graph RRD_TYPE_* (a category, for determining graphing options)
232 char *family; // grouping sets under the same family
233 char *title; // title shown to user
234 char *units; // units of measurement
236 char *context; // the template of this data set
237 uint32_t hash_context;
239 RRDSET_TYPE chart_type;
241 int update_every; // every how many seconds is this updated?
243 long entries; // total number of entries in the data set
245 long current_entry; // the entry that is currently being updated
246 // it goes around in a round-robin fashion
250 int gap_when_lost_iterations_above; // after how many lost iterations a gap should be stored
251 // netdata will interpolate values for gaps lower than this
256 // ------------------------------------------------------------------------
257 // members for temporary data we need for calculations
259 RRD_MEMORY_MODE rrd_memory_mode; // if set to 1, this is memory mapped
261 char *cache_dir; // the directory to store dimensions
262 char cache_filename[FILENAME_MAX+1]; // the filename to store this set
264 pthread_rwlock_t rrdset_rwlock; // protects dimensions linked list
266 unsigned long counter; // the number of times we added values to this rrd
267 unsigned long counter_done; // the number of times we added values to this rrd
269 uint32_t hash; // a simple hash on the id, to speed up searching
270 // we first compare hashes, and only if the hashes are equal we do string comparisons
272 uint32_t hash_name; // a simple hash on the name
274 usec_t usec_since_last_update; // the time in microseconds since the last collection of data
276 struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
277 struct timeval last_collected_time; // when did this data set last collected values
279 total_number collected_total; // used internally to calculate percentages
280 total_number last_collected_total; // used internally to calculate percentages
282 RRDFAMILY *rrdfamily;
283 struct rrdhost *rrdhost;
285 struct rrdset *next; // linking of rrdsets
287 // ------------------------------------------------------------------------
290 calculated_number green;
291 calculated_number red;
293 avl_tree_lock variables_root_index;
294 RRDSETVAR *variables;
297 // ------------------------------------------------------------------------
298 // members for checking the data when loading from disk
300 unsigned long memsize; // how much mem we have allocated for this (without dimensions)
302 char magic[sizeof(RRDSET_MAGIC) + 1]; // our magic
304 // ------------------------------------------------------------------------
307 avl_tree_lock dimensions_index; // the root of the dimensions index
308 RRDDIM *dimensions; // the actual data for every dimension
311 typedef struct rrdset RRDSET;
313 #define rrdset_rdlock(st) pthread_rwlock_rdlock(&((st)->rrdset_rwlock))
314 #define rrdset_wrlock(st) pthread_rwlock_wrlock(&((st)->rrdset_rwlock))
315 #define rrdset_unlock(st) pthread_rwlock_unlock(&((st)->rrdset_rwlock))
317 // ----------------------------------------------------------------------------
318 // these loop macros make sure the linked list is accessed with the right lock
320 #define rrdset_foreach_read(st, host) \
321 for(st = host->rrdset_root, rrdhost_check_rdlock(host); st ; st = st->next)
323 #define rrdset_foreach_write(st, host) \
324 for(st = host->rrdset_root, rrdhost_check_wrlock(host); st ; st = st->next)
327 // ----------------------------------------------------------------------------
331 avl avl; // the index of hosts
333 char *hostname; // the hostname of this host
334 uint32_t hash_hostname; // the hostname hash
336 char machine_guid[GUID_LEN + 1]; // the unique ID of this host
337 uint32_t hash_machine_guid; // the hash of the unique ID
339 int rrd_update_every; // the update frequency of the host
340 int rrd_history_entries; // the number of history entries for the host's charts
342 int health_enabled; // 1 when this host has health enabled
343 RRD_MEMORY_MODE rrd_memory_mode; // the memory more for the charts of this host
345 RRDSET *rrdset_root; // the host charts
347 pthread_rwlock_t rrdhost_rwlock; // lock for this RRDHOST (protects rrdset_root linked list)
349 avl_tree_lock rrdset_root_index; // the host's charts index (by id)
350 avl_tree_lock rrdset_root_index_name; // the host's charts index (by name)
352 avl_tree_lock rrdfamily_root_index; // the host's chart families index
353 avl_tree_lock variables_root_index; // the host's chart variables index
355 char *cache_dir; // the directory to save RRD cache files
356 char *varlib_dir; // the directory to save health log
358 // all RRDCALCs are primarily allocated and linked here
359 // RRDCALCs may be linked to charts at any point
360 // (charts may or may not exist when these are loaded)
363 ALARM_LOG health_log; // alarms historical events (event log)
365 // templates of alarms
366 // these are used to create alarms when charts
367 // are created or renamed, that match them
368 RRDCALCTEMPLATE *templates;
370 // health / alarm settings
371 char *health_default_exec;
372 char *health_default_recipient;
373 char *health_log_filename;
374 size_t health_log_entries_written;
377 struct rrdhost *next;
379 typedef struct rrdhost RRDHOST;
380 extern RRDHOST *localhost;
382 #define rrdhost_rdlock(h) pthread_rwlock_rdlock(&((h)->rrdhost_rwlock))
383 #define rrdhost_wrlock(h) pthread_rwlock_wrlock(&((h)->rrdhost_rwlock))
384 #define rrdhost_unlock(h) pthread_rwlock_unlock(&((h)->rrdhost_rwlock))
386 // ----------------------------------------------------------------------------
387 // these loop macros make sure the linked list is accessed with the right lock
389 #define rrdhost_foreach_read(var) \
390 for(var = localhost, rrd_check_rdlock(); var ; var = var->next)
392 #define rrdhost_foreach_write(var) \
393 for(var = localhost, rrd_check_wrlock(); var ; var = var->next)
396 // ----------------------------------------------------------------------------
397 // global lock for all RRDHOSTs
399 extern pthread_rwlock_t rrd_rwlock;
400 #define rrd_rdlock() pthread_rwlock_rdlock(&rrd_rwlock)
401 #define rrd_wrlock() pthread_rwlock_wrlock(&rrd_rwlock)
402 #define rrd_unlock() pthread_rwlock_unlock(&rrd_rwlock)
404 // ----------------------------------------------------------------------------
406 extern void rrd_init(char *hostname);
408 extern RRDHOST *rrdhost_find(const char *guid, uint32_t hash);
409 extern RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid);
411 #ifdef NETDATA_INTERNAL_CHECKS
412 extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
413 extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
414 extern void rrdset_check_rdlock_int(RRDSET *st, const char *file, const char *function, const unsigned long line);
415 extern void rrdset_check_wrlock_int(RRDSET *st, const char *file, const char *function, const unsigned long line);
416 extern void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line);
417 extern void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line);
419 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
420 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
421 #define rrdset_check_rdlock(st) rrdset_check_rdlock_int(st, __FILE__, __FUNCTION__, __LINE__)
422 #define rrdset_check_wrlock(st) rrdset_check_wrlock_int(st, __FILE__, __FUNCTION__, __LINE__)
423 #define rrd_check_rdlock() rrd_check_rdlock_int(__FILE__, __FUNCTION__, __LINE__)
424 #define rrd_check_wrlock() rrd_check_wrlock_int(__FILE__, __FUNCTION__, __LINE__)
427 #define rrdhost_check_rdlock(host) (void)0
428 #define rrdhost_check_wrlock(host) (void)0
429 #define rrdset_check_rdlock(host) (void)0
430 #define rrdset_check_wrlock(host) (void)0
431 #define rrd_check_rdlock() (void)0
432 #define rrd_check_wrlock() (void)0
435 // ----------------------------------------------------------------------------
438 extern void rrdset_set_name(RRDSET *st, const char *name);
440 extern RRDSET *rrdset_create(RRDHOST *host
445 , const char *context
450 , RRDSET_TYPE chart_type);
452 #define rrdset_create_localhost(type, id, name, family, context, title, units, priority, update_every, chart_type) rrdset_create(localhost, type, id, name, family, context, title, units, priority, update_every, chart_type)
454 extern void rrdhost_free_all(void);
455 extern void rrdhost_save_all(void);
457 extern void rrdhost_free(RRDHOST *host);
458 extern void rrdhost_save(RRDHOST *host);
460 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
461 #define rrdset_find_localhost(id) rrdset_find(localhost, id)
463 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
464 #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
466 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
467 #define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
469 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
470 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
471 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
473 extern void rrdset_done(RRDSET *st);
475 // get the total duration in seconds of the round robin database
476 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
478 // get the timestamp of the last entry in the round robin database
479 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
481 // get the timestamp of first entry in the round robin database
482 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
484 // get the last slot updated in the round robin database
485 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
487 // get the first / oldest slot updated in the round robin database
488 #define rrdset_first_slot(st) ((unsigned long)( (((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) ))
490 // get the slot of the round robin database, for the given timestamp (t)
491 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
492 #define rrdset_time2slot(st, t) ( \
493 ( (time_t)(t) >= rrdset_last_entry_t(st)) ? ( rrdset_last_slot(st) ) : \
494 ( ((time_t)(t) <= rrdset_first_entry_t(st)) ? rrdset_first_slot(st) : \
495 ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
496 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
497 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
500 // get the timestamp of a specific slot in the round robin database
501 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
502 ((unsigned long)(st)->update_every * ( \
503 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
504 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
505 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
508 // ----------------------------------------------------------------------------
509 // RRD DIMENSION functions
511 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm);
513 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
514 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
516 extern int rrddim_hide(RRDSET *st, const char *id);
517 extern int rrddim_unhide(RRDSET *st, const char *id);
519 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
520 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
522 extern long align_entries_to_pagesize(long entries);
525 // ----------------------------------------------------------------------------
526 // RRD internal functions
528 #ifdef NETDATA_RRD_INTERNALS
530 extern void rrdset_free(RRDSET *st);
531 extern avl_tree_lock rrdhost_root_index;
533 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
534 extern char *rrdset_cache_dir(RRDHOST *host, const char *id, const char *config_section);
536 extern void rrdset_reset(RRDSET *st);
538 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
540 extern int rrddim_compare(void* a, void* b);
541 extern int rrdset_compare(void* a, void* b);
542 extern int rrdset_compare_name(void* a, void* b);
543 extern int rrdfamily_compare(void *a, void *b);
545 extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
546 extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
548 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
549 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
550 extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
552 #endif /* NETDATA_RRD_INTERNALS */
555 #endif /* NETDATA_RRD_H */