]> arthur.barton.de Git - netdata.git/blob - src/rrd.h
d09fe6283669e79f3f549179a20db0ed1ee7def0
[netdata.git] / src / rrd.h
1 #ifndef NETDATA_RRD_H
2 #define NETDATA_RRD_H 1
3
4 #define UPDATE_EVERY 1
5 #define UPDATE_EVERY_MAX 3600
6 extern int rrd_update_every;
7
8 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
9 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
10 extern int rrd_default_history_entries;
11
12 #define RRD_ID_LENGTH_MAX 200
13
14 #define RRDSET_MAGIC        "NETDATA RRD SET FILE V019"
15 #define RRDDIMENSION_MAGIC  "NETDATA RRD DIMENSION FILE V019"
16
17 typedef long long total_number;
18 #define TOTAL_NUMBER_FORMAT "%lld"
19
20 // ----------------------------------------------------------------------------
21 // chart types
22
23 typedef enum rrdset_type {
24     RRDSET_TYPE_LINE    = 0,
25     RRDSET_TYPE_AREA    = 1,
26     RRDSET_TYPE_STACKED = 2
27 } RRDSET_TYPE;
28
29 #define RRDSET_TYPE_LINE_NAME "line"
30 #define RRDSET_TYPE_AREA_NAME "area"
31 #define RRDSET_TYPE_STACKED_NAME "stacked"
32
33 RRDSET_TYPE rrdset_type_id(const char *name);
34 const char *rrdset_type_name(RRDSET_TYPE chart_type);
35
36
37 // ----------------------------------------------------------------------------
38 // memory mode
39
40 typedef enum rrd_memory_mode {
41     RRD_MEMORY_MODE_RAM  = 0,
42     RRD_MEMORY_MODE_MAP  = 1,
43     RRD_MEMORY_MODE_SAVE = 2
44 } RRD_MEMORY_MODE;
45
46 #define RRD_MEMORY_MODE_RAM_NAME "ram"
47 #define RRD_MEMORY_MODE_MAP_NAME "map"
48 #define RRD_MEMORY_MODE_SAVE_NAME "save"
49
50 RRD_MEMORY_MODE rrd_memory_mode;
51
52 extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
53 extern int rrd_memory_mode_id(const char *name);
54
55
56 // ----------------------------------------------------------------------------
57 // algorithms types
58
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
64 } RRD_ALGORITHM;
65
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"
70
71 extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
72 extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
73
74 // ----------------------------------------------------------------------------
75 // flags
76
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
81 } RRDDIM_FLAGS;
82
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
86
87
88 // ----------------------------------------------------------------------------
89 // RRD FAMILY
90
91 struct rrdfamily {
92     avl avl;
93
94     const char *family;
95     uint32_t hash_family;
96
97     size_t use_count;
98
99     avl_tree_lock variables_root_index;
100 };
101 typedef struct rrdfamily RRDFAMILY;
102
103
104 // ----------------------------------------------------------------------------
105 // RRD DIMENSION - this is a metric
106
107 struct rrddim {
108     // ------------------------------------------------------------------------
109     // binary indexing structures
110
111     avl avl;                                        // the binary index - this has to be first member!
112
113     // ------------------------------------------------------------------------
114     // the dimension definition
115
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
122
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
125
126     collected_number multiplier;                    // the multiplier of the collected values
127     collected_number divisor;                       // the divider of the collected values
128
129     uint32_t flags;                                 // options and status options for the dimension
130
131     // ------------------------------------------------------------------------
132     // members for temporary data we need for calculations
133
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
137
138     uint32_t hash_name;                             // a simple hash of the name
139
140     char *cache_filename;                           // the filename we load/save from/to this set
141
142     size_t counter;                                 // the number of times we added values to this rrdim
143
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
147
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
150
151     calculated_number last_stored_value;            // the last value as stored in the database (after interpolation)
152
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
155
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
160
161     struct rrddim *next;                            // linking of dimensions within the same data set
162     struct rrdset *rrdset;
163
164     // ------------------------------------------------------------------------
165     // members for checking the data when loading from disk
166
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.
170
171     int update_every;                               // every how many seconds is this updated
172
173     size_t memsize;                                 // the memory allocated for this dimension
174
175     char magic[sizeof(RRDDIMENSION_MAGIC) + 1];     // a string to be saved, used to identify our data file
176
177     struct rrddimvar *variables;
178
179     // ------------------------------------------------------------------------
180     // the values stored in this dimension, using our floating point numbers
181
182     storage_number values[];                        // the array of values - THIS HAS TO BE THE LAST MEMBER
183 };
184 typedef struct rrddim RRDDIM;
185
186
187 // ----------------------------------------------------------------------------
188 // RRDSET - this is a chart
189
190 struct rrdset {
191     // ------------------------------------------------------------------------
192     // binary indexing structures
193
194     avl avl;                                        // the index, with key the id - this has to be first!
195     avl avlname;                                    // the index, with key the name
196
197     // ------------------------------------------------------------------------
198     // the set configuration
199
200     char id[RRD_ID_LENGTH_MAX + 1];                 // id of the data set
201
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)
206
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
211
212     char *context;                                  // the template of this data set
213     uint32_t hash_context;
214
215     RRDSET_TYPE chart_type;
216
217     int update_every;                               // every how many seconds is this updated?
218
219     long entries;                                   // total number of entries in the data set
220
221     long current_entry;                             // the entry that is currently being updated
222                                                     // it goes around in a round-robin fashion
223
224     int enabled;
225
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
228
229     long priority;
230
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)
233
234     // ------------------------------------------------------------------------
235     // members for temporary data we need for calculations
236
237     int mapped;                                     // if set to 1, this is memory mapped
238
239     int debug;
240
241     char *cache_dir;                                // the directory to store dimensions
242     char cache_filename[FILENAME_MAX+1];            // the filename to store this set
243
244     pthread_rwlock_t rwlock;
245
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
248
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
251
252     uint32_t hash_name;                             // a simple hash on the name
253
254     usec_t usec_since_last_update;                  // the time in microseconds since the last collection of data
255
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
258
259     total_number collected_total;                   // used internally to calculate percentages
260     total_number last_collected_total;              // used internally to calculate percentages
261
262     RRDFAMILY *rrdfamily;
263     struct rrdhost *rrdhost;
264
265     struct rrdset *next;                            // linking of rrdsets
266
267     // ------------------------------------------------------------------------
268     // local variables
269
270     calculated_number green;
271     calculated_number red;
272
273     avl_tree_lock variables_root_index;
274     RRDSETVAR *variables;
275     RRDCALC *alarms;
276
277     // ------------------------------------------------------------------------
278     // members for checking the data when loading from disk
279
280     unsigned long memsize;                          // how much mem we have allocated for this (without dimensions)
281
282     char magic[sizeof(RRDSET_MAGIC) + 1];           // our magic
283
284     // ------------------------------------------------------------------------
285     // the dimensions
286
287     avl_tree_lock dimensions_index;                 // the root of the dimensions index
288     RRDDIM *dimensions;                             // the actual data for every dimension
289
290 };
291 typedef struct rrdset RRDSET;
292
293 // ----------------------------------------------------------------------------
294 // RRD HOST
295
296 struct rrdhost {
297     avl avl;
298
299     char *hostname;
300     char machine_guid[GUID_LEN + 1];
301
302     RRDSET *rrdset_root;
303     pthread_rwlock_t rrdset_root_rwlock;
304
305     avl_tree_lock rrdset_root_index;
306     avl_tree_lock rrdset_root_index_name;
307
308     avl_tree_lock rrdfamily_root_index;
309     avl_tree_lock variables_root_index;
310
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)
314     RRDCALC *alarms;
315
316     // alarms historical events
317     ALARM_LOG health_log;
318
319     // templates of alarms
320     // these are used to create alarms when charts
321     // are created or renamed, that match them
322     RRDCALCTEMPLATE *templates;
323
324     uint32_t flags;
325
326     struct rrdhost *next;
327 };
328 typedef struct rrdhost RRDHOST;
329 extern RRDHOST localhost;
330 extern void rrdhost_init(char *hostname);
331
332 #ifdef NETDATA_INTERNAL_CHECKS
333 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
334 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
335 #else
336 #define rrdhost_check_rdlock(host) (void)0
337 #define rrdhost_check_wrlock(host) (void)0
338 #endif
339
340 extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
341 extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
342
343 extern void rrdhost_rwlock(RRDHOST *host);
344 extern void rrdhost_rdlock(RRDHOST *host);
345 extern void rrdhost_unlock(RRDHOST *host);
346
347 // ----------------------------------------------------------------------------
348 // RRD SET functions
349
350 extern void rrdset_set_name(RRDSET *st, const char *name);
351
352 extern RRDSET *rrdset_create(const char *type
353         , const char *id
354         , const char *name
355         , const char *family
356         , const char *context
357         , const char *title
358         , const char *units
359         , long priority
360         , int update_every
361         , int chart_type);
362
363 extern void rrdhost_free_all(void);
364 extern void rrdhost_save_all(void);
365
366 extern void rrdhost_free(RRDHOST *host);
367 extern void rrdhost_save(RRDHOST *host);
368
369 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
370 #define rrdset_find_localhost(id) rrdset_find(&localhost, id)
371
372 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
373 #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(&localhost, type, id)
374
375 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
376 #define rrdset_find_byname_localhost(name)  rrdset_find_byname(&localhost, name)
377
378 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
379 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
380 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
381
382 extern usec_t rrdset_done(RRDSET *st);
383
384 // get the total duration in seconds of the round robin database
385 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
386
387 // get the timestamp of the last entry in the round robin database
388 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
389
390 // get the timestamp of first entry in the round robin database
391 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
392
393 // get the last slot updated in the round robin database
394 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
395
396 // get the first / oldest slot updated in the round robin database
397 #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) ))
398
399 // get the slot of the round robin database, for the given timestamp (t)
400 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
401 #define rrdset_time2slot(st, t) ( \
402         (  (time_t)(t) >= rrdset_last_entry_t(st))  ? ( rrdset_last_slot(st) ) : \
403         ( ((time_t)(t) <= rrdset_first_entry_t(st)) ?   rrdset_first_slot(st) : \
404         ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
405           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
406           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
407         )))
408
409 // get the timestamp of a specific slot in the round robin database
410 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
411         ((unsigned long)(st)->update_every * ( \
412                 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
413                 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
414                 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
415         ))
416
417 // ----------------------------------------------------------------------------
418 // RRD DIMENSION functions
419
420 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm);
421
422 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
423 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
424
425 extern int rrddim_hide(RRDSET *st, const char *id);
426 extern int rrddim_unhide(RRDSET *st, const char *id);
427
428 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
429 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
430
431
432 // ----------------------------------------------------------------------------
433 // RRD internal functions
434
435 #ifdef NETDATA_RRD_INTERNALS
436
437 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
438 extern char *rrdset_cache_dir(const char *id);
439
440 extern void rrdset_reset(RRDSET *st);
441
442 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
443
444 extern int rrddim_compare(void* a, void* b);
445 extern int rrdset_compare(void* a, void* b);
446 extern int rrdset_compare_name(void* a, void* b);
447 extern int rrdfamily_compare(void *a, void *b);
448
449 extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
450 extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
451
452 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
453 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
454 extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
455
456 #endif /* NETDATA_RRD_INTERNALS */
457
458
459 #endif /* NETDATA_RRD_H */