2 #define NETDATA_RRD_H 1
5 #define UPDATE_EVERY_MAX 3600
6 extern int rrd_update_every;
8 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
9 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
10 extern int rrd_default_history_entries;
12 #define RRD_ID_LENGTH_MAX 200
14 #define RRDSET_MAGIC "NETDATA RRD SET FILE V019"
15 #define RRDDIMENSION_MAGIC "NETDATA RRD DIMENSION FILE V019"
17 typedef long long total_number;
18 #define TOTAL_NUMBER_FORMAT "%lld"
20 // ----------------------------------------------------------------------------
23 typedef enum rrdset_type {
26 RRDSET_TYPE_STACKED = 2
29 #define RRDSET_TYPE_LINE_NAME "line"
30 #define RRDSET_TYPE_AREA_NAME "area"
31 #define RRDSET_TYPE_STACKED_NAME "stacked"
33 RRDSET_TYPE rrdset_type_id(const char *name);
34 const char *rrdset_type_name(RRDSET_TYPE chart_type);
37 // ----------------------------------------------------------------------------
40 typedef enum rrd_memory_mode {
41 RRD_MEMORY_MODE_RAM = 0,
42 RRD_MEMORY_MODE_MAP = 1,
43 RRD_MEMORY_MODE_SAVE = 2
46 #define RRD_MEMORY_MODE_RAM_NAME "ram"
47 #define RRD_MEMORY_MODE_MAP_NAME "map"
48 #define RRD_MEMORY_MODE_SAVE_NAME "save"
50 RRD_MEMORY_MODE rrd_memory_mode;
52 extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
53 extern int rrd_memory_mode_id(const char *name);
56 // ----------------------------------------------------------------------------
59 typedef enum rrd_algorithm {
60 RRD_ALGORITHM_ABSOLUTE = 0,
61 RRD_ALGORITHM_INCREMENTAL = 1,
62 RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
63 RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3
66 #define RRD_ALGORITHM_ABSOLUTE_NAME "absolute"
67 #define RRD_ALGORITHM_INCREMENTAL_NAME "incremental"
68 #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
69 #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
71 extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
72 extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
74 // ----------------------------------------------------------------------------
77 typedef enum rrddim_flags {
78 RRDDIM_FLAG_HIDDEN = 1 << 0, // this dimension will not be offered to callers
79 RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = 1 << 1, // do not offer RESET or OVERFLOW info to callers
80 RRDDIM_FLAG_UPDATED = 1 << 31 // the dimension has been updated since the last processing
83 #define rrddim_flag_check(rd, flag) ((rd)->flags & flag)
84 #define rrddim_flag_set(rd, flag) (rd)->flags |= flag
85 #define rrddim_flag_clear(rd, flag) (rd)->flags &= ~flag
88 // ----------------------------------------------------------------------------
99 avl_tree_lock variables_root_index;
101 typedef struct rrdfamily RRDFAMILY;
104 // ----------------------------------------------------------------------------
105 // RRD DIMENSION - this is a metric
108 // ------------------------------------------------------------------------
109 // binary indexing structures
111 avl avl; // the binary index - this has to be first member!
113 // ------------------------------------------------------------------------
114 // the dimension definition
116 const char *id; // the id of this dimension (for internal identification)
117 const char *name; // the name of this dimension (as presented to user)
118 // this is a pointer to the config structure
119 // since the config always has a higher priority
120 // (the user overwrites the name of the charts)
121 // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
123 RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
124 RRD_MEMORY_MODE memory_mode; // the memory mode for this dimension
126 collected_number multiplier; // the multiplier of the collected values
127 collected_number divisor; // the divider of the collected values
129 uint32_t flags; // options and status options for the dimension
131 // ------------------------------------------------------------------------
132 // members for temporary data we need for calculations
134 uint32_t hash; // a simple hash of the id, to speed up searching / indexing
135 // instead of strcmp() every item in the binary index
136 // we first compare the hashes
138 uint32_t hash_name; // a simple hash of the name
140 char *cache_filename; // the filename we load/save from/to this set
142 size_t counter; // the number of times we added values to this rrdim
144 struct timeval last_collected_time; // when was this dimension last updated
145 // this is actual date time we updated the last_collected_value
146 // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
148 calculated_number calculated_value; // the current calculated value, after applying the algorithm - resets to zero after being used
149 calculated_number last_calculated_value; // the last calculated value processed
151 calculated_number last_stored_value; // the last value as stored in the database (after interpolation)
153 collected_number collected_value; // the current value, as collected - resets to 0 after being used
154 collected_number last_collected_value; // the last value that was collected, after being processed
156 // the *_volume members are used to calculate the accuracy of the rounding done by the
157 // storage number - they are printed to debug.log when debug is enabled for a set.
158 calculated_number collected_volume; // the sum of all collected values so far
159 calculated_number stored_volume; // the sum of all stored values so far
161 struct rrddim *next; // linking of dimensions within the same data set
162 struct rrdset *rrdset;
164 // ------------------------------------------------------------------------
165 // members for checking the data when loading from disk
167 long entries; // how many entries this dimension has in ram
168 // this is the same to the entries of the data set
169 // we set it here, to check the data when we load it from disk.
171 int update_every; // every how many seconds is this updated
173 size_t memsize; // the memory allocated for this dimension
175 char magic[sizeof(RRDDIMENSION_MAGIC) + 1]; // a string to be saved, used to identify our data file
177 struct rrddimvar *variables;
179 // ------------------------------------------------------------------------
180 // the values stored in this dimension, using our floating point numbers
182 storage_number values[]; // the array of values - THIS HAS TO BE THE LAST MEMBER
184 typedef struct rrddim RRDDIM;
187 // ----------------------------------------------------------------------------
188 // RRDSET - this is a chart
191 // ------------------------------------------------------------------------
192 // binary indexing structures
194 avl avl; // the index, with key the id - this has to be first!
195 avl avlname; // the index, with key the name
197 // ------------------------------------------------------------------------
198 // the set configuration
200 char id[RRD_ID_LENGTH_MAX + 1]; // id of the data set
202 const char *name; // the name of this dimension (as presented to user)
203 // this is a pointer to the config structure
204 // since the config always has a higher priority
205 // (the user overwrites the name of the charts)
207 char *type; // the type of graph RRD_TYPE_* (a category, for determining graphing options)
208 char *family; // grouping sets under the same family
209 char *title; // title shown to user
210 char *units; // units of measurement
212 char *context; // the template of this data set
213 uint32_t hash_context;
215 RRDSET_TYPE chart_type;
217 int update_every; // every how many seconds is this updated?
219 long entries; // total number of entries in the data set
221 long current_entry; // the entry that is currently being updated
222 // it goes around in a round-robin fashion
226 int gap_when_lost_iterations_above; // after how many lost iterations a gap should be stored
227 // netdata will interpolate values for gaps lower than this
231 int isdetail; // if set, the data set should be considered as a detail of another
232 // (the master data set should be the one that has the same family and is not detail)
234 // ------------------------------------------------------------------------
235 // members for temporary data we need for calculations
237 int mapped; // if set to 1, this is memory mapped
241 char *cache_dir; // the directory to store dimensions
242 char cache_filename[FILENAME_MAX+1]; // the filename to store this set
244 pthread_rwlock_t rwlock;
246 unsigned long counter; // the number of times we added values to this rrd
247 unsigned long counter_done; // the number of times we added values to this rrd
249 uint32_t hash; // a simple hash on the id, to speed up searching
250 // we first compare hashes, and only if the hashes are equal we do string comparisons
252 uint32_t hash_name; // a simple hash on the name
254 usec_t usec_since_last_update; // the time in microseconds since the last collection of data
256 struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
257 struct timeval last_collected_time; // when did this data set last collected values
259 total_number collected_total; // used internally to calculate percentages
260 total_number last_collected_total; // used internally to calculate percentages
262 RRDFAMILY *rrdfamily;
263 struct rrdhost *rrdhost;
265 struct rrdset *next; // linking of rrdsets
267 // ------------------------------------------------------------------------
270 calculated_number green;
271 calculated_number red;
273 avl_tree_lock variables_root_index;
274 RRDSETVAR *variables;
277 // ------------------------------------------------------------------------
278 // members for checking the data when loading from disk
280 unsigned long memsize; // how much mem we have allocated for this (without dimensions)
282 char magic[sizeof(RRDSET_MAGIC) + 1]; // our magic
284 // ------------------------------------------------------------------------
287 avl_tree_lock dimensions_index; // the root of the dimensions index
288 RRDDIM *dimensions; // the actual data for every dimension
291 typedef struct rrdset RRDSET;
293 // ----------------------------------------------------------------------------
300 uint32_t hash_hostname;
302 char machine_guid[GUID_LEN + 1];
303 uint32_t hash_machine_guid;
308 pthread_rwlock_t rrdset_root_rwlock;
310 avl_tree_lock rrdset_root_index;
311 avl_tree_lock rrdset_root_index_name;
313 avl_tree_lock rrdfamily_root_index;
314 avl_tree_lock variables_root_index;
316 // all RRDCALCs are primarily allocated and linked here
317 // RRDCALCs may be linked to charts at any point
318 // (charts may or may not exist when these are loaded)
321 // alarms historical events
322 ALARM_LOG health_log;
324 // templates of alarms
325 // these are used to create alarms when charts
326 // are created or renamed, that match them
327 RRDCALCTEMPLATE *templates;
329 // health / alarm settings
330 char *health_default_exec;
331 char *health_default_recipient;
332 char *health_log_filename;
333 size_t health_log_entries_written;
336 struct rrdhost *next;
338 typedef struct rrdhost RRDHOST;
340 extern RRDHOST *localhost;
342 extern void rrd_init(char *hostname);
344 extern RRDHOST *rrdhost_find(const char *guid, uint32_t hash);
345 extern RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid);
347 #ifdef NETDATA_INTERNAL_CHECKS
348 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
349 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
351 #define rrdhost_check_rdlock(host) (void)0
352 #define rrdhost_check_wrlock(host) (void)0
355 extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
356 extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
358 extern void rrdhost_rwlock(RRDHOST *host);
359 extern void rrdhost_rdlock(RRDHOST *host);
360 extern void rrdhost_unlock(RRDHOST *host);
362 // ----------------------------------------------------------------------------
365 extern void rrdset_set_name(RRDSET *st, const char *name);
367 extern RRDSET *rrdset_create(const char *type
371 , const char *context
378 extern void rrdhost_free_all(void);
379 extern void rrdhost_save_all(void);
381 extern void rrdhost_free(RRDHOST *host);
382 extern void rrdhost_save(RRDHOST *host);
384 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
385 #define rrdset_find_localhost(id) rrdset_find(localhost, id)
387 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
388 #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
390 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
391 #define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
393 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
394 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
395 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
397 extern usec_t rrdset_done(RRDSET *st);
399 // get the total duration in seconds of the round robin database
400 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
402 // get the timestamp of the last entry in the round robin database
403 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
405 // get the timestamp of first entry in the round robin database
406 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
408 // get the last slot updated in the round robin database
409 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
411 // get the first / oldest slot updated in the round robin database
412 #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) ))
414 // get the slot of the round robin database, for the given timestamp (t)
415 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
416 #define rrdset_time2slot(st, t) ( \
417 ( (time_t)(t) >= rrdset_last_entry_t(st)) ? ( rrdset_last_slot(st) ) : \
418 ( ((time_t)(t) <= rrdset_first_entry_t(st)) ? rrdset_first_slot(st) : \
419 ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
420 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
421 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
424 // get the timestamp of a specific slot in the round robin database
425 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
426 ((unsigned long)(st)->update_every * ( \
427 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
428 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
429 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
432 // ----------------------------------------------------------------------------
433 // RRD DIMENSION functions
435 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm);
437 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
438 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
440 extern int rrddim_hide(RRDSET *st, const char *id);
441 extern int rrddim_unhide(RRDSET *st, const char *id);
443 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
444 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
447 // ----------------------------------------------------------------------------
448 // RRD internal functions
450 #ifdef NETDATA_RRD_INTERNALS
452 extern avl_tree_lock rrdhost_root_index;
454 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
455 extern char *rrdset_cache_dir(const char *id);
457 extern void rrdset_reset(RRDSET *st);
459 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
461 extern int rrddim_compare(void* a, void* b);
462 extern int rrdset_compare(void* a, void* b);
463 extern int rrdset_compare_name(void* a, void* b);
464 extern int rrdfamily_compare(void *a, void *b);
466 extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
467 extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
469 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
470 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
471 extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
473 #endif /* NETDATA_RRD_INTERNALS */
476 #endif /* NETDATA_RRD_H */