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 // time in seconds to delete unupdated dimensions
13 // set to zero to disable this feature
14 extern int rrd_delete_unupdated_dimensions;
16 #define RRD_ID_LENGTH_MAX 1024
18 #define RRDSET_MAGIC "NETDATA RRD SET FILE V018"
19 #define RRDDIMENSION_MAGIC "NETDATA RRD DIMENSION FILE V018"
21 typedef long long total_number;
22 #define TOTAL_NUMBER_FORMAT "%lld"
24 // ----------------------------------------------------------------------------
27 #define RRDSET_TYPE_LINE_NAME "line"
28 #define RRDSET_TYPE_AREA_NAME "area"
29 #define RRDSET_TYPE_STACKED_NAME "stacked"
31 #define RRDSET_TYPE_LINE 0
32 #define RRDSET_TYPE_AREA 1
33 #define RRDSET_TYPE_STACKED 2
35 int rrdset_type_id(const char *name);
36 const char *rrdset_type_name(int chart_type);
39 // ----------------------------------------------------------------------------
42 #define RRD_MEMORY_MODE_RAM_NAME "ram"
43 #define RRD_MEMORY_MODE_MAP_NAME "map"
44 #define RRD_MEMORY_MODE_SAVE_NAME "save"
46 #define RRD_MEMORY_MODE_RAM 0
47 #define RRD_MEMORY_MODE_MAP 1
48 #define RRD_MEMORY_MODE_SAVE 2
50 extern int rrd_memory_mode;
52 extern const char *rrd_memory_mode_name(int id);
53 extern int rrd_memory_mode_id(const char *name);
56 // ----------------------------------------------------------------------------
59 #define RRDDIM_ABSOLUTE_NAME "absolute"
60 #define RRDDIM_INCREMENTAL_NAME "incremental"
61 #define RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
62 #define RRDDIM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
64 #define RRDDIM_ABSOLUTE 0
65 #define RRDDIM_INCREMENTAL 1
66 #define RRDDIM_PCENT_OVER_DIFF_TOTAL 2
67 #define RRDDIM_PCENT_OVER_ROW_TOTAL 3
69 extern int rrddim_algorithm_id(const char *name);
70 extern const char *rrddim_algorithm_name(int chart_type);
72 // ----------------------------------------------------------------------------
75 #define RRDDIM_FLAG_HIDDEN 0x00000001 // this dimension will not be offered to callers
76 #define RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS 0x00000002 // do not offer RESET or OVERFLOW info to callers
78 // ----------------------------------------------------------------------------
89 avl_tree_lock variables_root_index;
91 typedef struct rrdcontext RRDCONTEXT;
93 // ----------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
98 // binary indexing structures
100 avl avl; // the binary index - this has to be first member!
102 // ------------------------------------------------------------------------
103 // the dimension definition
105 char id[RRD_ID_LENGTH_MAX + 1]; // the id of this dimension (for internal identification)
107 const char *name; // the name of this dimension (as presented to user)
108 // this is a pointer to the config structure
109 // since the config always has a higher priority
110 // (the user overwrites the name of the charts)
112 int algorithm; // the algorithm that is applied to add new collected values
113 long multiplier; // the multiplier of the collected values
114 long divisor; // the divider of the collected values
116 int mapped; // if set to non zero, this dimension is mapped to a file
118 // ------------------------------------------------------------------------
119 // members for temporary data we need for calculations
121 uint32_t hash; // a simple hash of the id, to speed up searching / indexing
122 // instead of strcmp() every item in the binary index
123 // we first compare the hashes
126 // we need the hash_name too!
130 char cache_filename[FILENAME_MAX+1]; // the filename we load/save from/to this set
132 unsigned long counter; // the number of times we added values to this rrdim
134 int updated; // set to 0 after each calculation, to 1 after each collected value
135 // we use this to detect that a dimension is not updated
137 struct timeval last_collected_time; // when was this dimension last updated
138 // this is actual date time we updated the last_collected_value
139 // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
141 calculated_number calculated_value; // the current calculated value, after applying the algorithm
142 calculated_number last_calculated_value; // the last calculated value
144 collected_number collected_value; // the current value, as collected
145 collected_number last_collected_value; // the last value that was collected
147 // the *_volume members are used to calculate the accuracy of the rounding done by the
148 // storage number - they are printed to debug.log when debug is enabled for a set.
149 calculated_number collected_volume; // the sum of all collected values so far
150 calculated_number stored_volume; // the sum of all stored values so far
152 struct rrddim *next; // linking of dimensions within the same data set
153 struct rrdset *rrdset;
155 // ------------------------------------------------------------------------
156 // members for checking the data when loading from disk
158 long entries; // how many entries this dimension has in ram
159 // this is the same to the entries of the data set
160 // we set it here, to check the data when we load it from disk.
162 int update_every; // every how many seconds is this updated
164 unsigned long memsize; // the memory allocated for this dimension
166 char magic[sizeof(RRDDIMENSION_MAGIC) + 1]; // a string to be saved, used to identify our data file
168 struct rrddimvar *variables;
170 // ------------------------------------------------------------------------
171 // the values stored in this dimension, using our floating point numbers
173 storage_number values[]; // the array of values - THIS HAS TO BE THE LAST MEMBER
175 typedef struct rrddim RRDDIM;
178 // ----------------------------------------------------------------------------
182 // ------------------------------------------------------------------------
183 // binary indexing structures
185 avl avl; // the index, with key the id - this has to be first!
186 avl avlname; // the index, with key the name
188 // ------------------------------------------------------------------------
189 // the set configuration
191 char id[RRD_ID_LENGTH_MAX + 1]; // id of the data set
193 const char *name; // the name of this dimension (as presented to user)
194 // this is a pointer to the config structure
195 // since the config always has a higher priority
196 // (the user overwrites the name of the charts)
198 char *type; // the type of graph RRD_TYPE_* (a category, for determining graphing options)
199 char *family; // grouping sets under the same family
200 char *context; // the template of this data set
201 char *title; // title shown to user
202 char *units; // units of measurement
206 int update_every; // every how many seconds is this updated?
208 long entries; // total number of entries in the data set
210 long current_entry; // the entry that is currently being updated
211 // it goes around in a round-robin fashion
215 int gap_when_lost_iterations_above; // after how many lost iterations a gap should be stored
216 // netdata will interpolate values for gaps lower than this
220 int isdetail; // if set, the data set should be considered as a detail of another
221 // (the master data set should be the one that has the same family and is not detail)
223 // ------------------------------------------------------------------------
224 // members for temporary data we need for calculations
226 int mapped; // if set to 1, this is memory mapped
230 char *cache_dir; // the directory to store dimensions
231 char cache_filename[FILENAME_MAX+1]; // the filename to store this set
233 pthread_rwlock_t rwlock;
235 unsigned long counter; // the number of times we added values to this rrd
236 unsigned long counter_done; // the number of times we added values to this rrd
238 uint32_t hash; // a simple hash on the id, to speed up searching
239 // we first compare hashes, and only if the hashes are equal we do string comparisons
241 uint32_t hash_name; // a simple hash on the name
243 unsigned long long usec_since_last_update; // the time in microseconds since the last collection of data
245 struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
246 struct timeval last_collected_time; // when did this data set last collected values
248 total_number collected_total; // used internally to calculate percentages
249 total_number last_collected_total; // used internally to calculate percentages
251 RRDCONTEXT *rrdcontext;
252 struct rrdhost *rrdhost;
254 struct rrdset *next; // linking of rrdsets
256 // ------------------------------------------------------------------------
259 avl_tree_lock variables_root_index;
260 RRDSETVAR *variables;
261 RRDCALC *calculations;
263 // ------------------------------------------------------------------------
264 // members for checking the data when loading from disk
266 unsigned long memsize; // how much mem we have allocated for this (without dimensions)
268 char magic[sizeof(RRDSET_MAGIC) + 1]; // our magic
270 // ------------------------------------------------------------------------
273 avl_tree_lock dimensions_index; // the root of the dimensions index
274 RRDDIM *dimensions; // the actual data for every dimension
277 typedef struct rrdset RRDSET;
279 // ----------------------------------------------------------------------------
288 pthread_rwlock_t rrdset_root_rwlock;
290 avl_tree_lock rrdset_root_index;
291 avl_tree_lock rrdset_root_index_name;
293 avl_tree_lock rrdcontext_root_index;
294 avl_tree_lock variables_root_index;
296 // all RRDCALCs are primarily allocated and linked here
297 // RRDCALCs may be linked to charts at any point
298 // (charts may or may not exist when these are loaded)
299 RRDCALC *calculations;
301 // all variable references are linked here
302 // RRDVARs may be free'd, so every time this happens
303 // we need to find all their references and invalidate them
304 EVAL_VARIABLE *references;
306 typedef struct rrdhost RRDHOST;
308 extern RRDHOST localhost;
310 // ----------------------------------------------------------------------------
313 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
314 extern void rrdset_set_name(RRDSET *st, const char *name);
316 extern char *rrdset_cache_dir(const char *id);
318 extern void rrdset_reset(RRDSET *st);
320 extern RRDSET *rrdset_create(const char *type
324 , const char *context
331 extern void rrdset_free_all(void);
332 extern void rrdset_save_all(void);
334 extern RRDSET *rrdset_find(const char *id);
335 extern RRDSET *rrdset_find_bytype(const char *type, const char *id);
336 extern RRDSET *rrdset_find_byname(const char *name);
338 extern void rrdset_next_usec(RRDSET *st, unsigned long long microseconds);
339 extern void rrdset_next(RRDSET *st);
340 extern void rrdset_next_plugins(RRDSET *st);
342 extern unsigned long long rrdset_done(RRDSET *st);
344 // get the total duration in seconds of the round robin database
345 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
347 // get the timestamp of the last entry in the round robin database
348 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
350 // get the timestamp of first entry in the round robin database
351 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
353 // get the last slot updated in the round robin database
354 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
356 // get the first / oldest slot updated in the round robin database
357 #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) ))
359 // get the slot of the round robin database, for the given timestamp (t)
360 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
361 #define rrdset_time2slot(st, t) ( \
362 ( (time_t)(t) >= rrdset_last_entry_t(st)) ? ( rrdset_last_slot(st) ) : \
363 ( ((time_t)(t) <= rrdset_first_entry_t(st)) ? rrdset_first_slot(st) : \
364 ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
365 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
366 (rrdset_last_slot(st) - (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
369 // get the timestamp of a specific slot in the round robin database
370 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
371 ((unsigned long)(st)->update_every * ( \
372 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
373 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
374 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
377 // ----------------------------------------------------------------------------
378 // RRD DIMENSION functions
380 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm);
382 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
383 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
385 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
387 extern int rrddim_hide(RRDSET *st, const char *id);
388 extern int rrddim_unhide(RRDSET *st, const char *id);
390 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
391 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
393 #endif /* NETDATA_RRD_H */