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 rrddim_algorithm {
60 RRDDIM_ALGORITHM_ABSOLUTE = 0,
61 RRDDIM_ALGORITHM_INCREMENTAL = 1,
62 RRDDIM_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
63 RRDDIM_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3
66 #define RRDDIM_ALGORITHM_ABSOLUTE_NAME "absolute"
67 #define RRDDIM_ALGORITHM_INCREMENTAL_NAME "incremental"
68 #define RRDDIM_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
69 #define RRDDIM_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
71 extern RRDDIM_ALGORITHM rrddim_algorithm_id(const char *name);
72 extern const char *rrddim_algorithm_name(RRDDIM_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 RRDDIM_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 char machine_guid[GUID_LEN + 1];
303 pthread_rwlock_t rrdset_root_rwlock;
305 avl_tree_lock rrdset_root_index;
306 avl_tree_lock rrdset_root_index_name;
308 avl_tree_lock rrdfamily_root_index;
309 avl_tree_lock variables_root_index;
311 // all RRDCALCs are primarily allocated and linked here
312 // RRDCALCs may be linked to charts at any point
313 // (charts may or may not exist when these are loaded)
315 ALARM_LOG health_log;
317 RRDCALCTEMPLATE *templates;
321 struct rrdhost *next;
323 typedef struct rrdhost RRDHOST;
324 extern RRDHOST localhost;
325 extern void rrdhost_init(char *hostname);
327 #ifdef NETDATA_INTERNAL_CHECKS
328 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
329 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
331 #define rrdhost_check_rdlock(host) (void)0
332 #define rrdhost_check_wrlock(host) (void)0
335 extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
336 extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
338 extern void rrdhost_rwlock(RRDHOST *host);
339 extern void rrdhost_rdlock(RRDHOST *host);
340 extern void rrdhost_unlock(RRDHOST *host);
342 // ----------------------------------------------------------------------------
345 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
346 extern void rrdset_set_name(RRDSET *st, const char *name);
348 extern char *rrdset_cache_dir(const char *id);
350 extern void rrdset_reset(RRDSET *st);
352 extern RRDSET *rrdset_create(const char *type
356 , const char *context
363 extern void rrdset_free_all(void);
364 extern void rrdset_save_all(void);
366 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
367 #define rrdset_find_localhost(id) rrdset_find(&localhost, id)
369 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
370 #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(&localhost, type, id)
372 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
373 #define rrdset_find_byname_localhost(name) rrdset_find_byname(&localhost, name)
375 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
376 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
377 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
379 extern usec_t rrdset_done(RRDSET *st);
381 // get the total duration in seconds of the round robin database
382 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
384 // get the timestamp of the last entry in the round robin database
385 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
387 // get the timestamp of first entry in the round robin database
388 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
390 // get the last slot updated in the round robin database
391 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
393 // get the first / oldest slot updated in the round robin database
394 #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) ))
396 // get the slot of the round robin database, for the given timestamp (t)
397 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
398 #define rrdset_time2slot(st, t) ( \
399 ( (time_t)(t) >= rrdset_last_entry_t(st)) ? ( rrdset_last_slot(st) ) : \
400 ( ((time_t)(t) <= rrdset_first_entry_t(st)) ? rrdset_first_slot(st) : \
401 ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
402 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
403 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
406 // get the timestamp of a specific slot in the round robin database
407 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
408 ((unsigned long)(st)->update_every * ( \
409 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
410 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
411 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
414 // ----------------------------------------------------------------------------
415 // RRD DIMENSION functions
417 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRDDIM_ALGORITHM algorithm);
419 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
420 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
422 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
424 extern int rrddim_hide(RRDSET *st, const char *id);
425 extern int rrddim_unhide(RRDSET *st, const char *id);
427 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
428 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
430 #endif /* NETDATA_RRD_H */