]> arthur.barton.de Git - netdata.git/blob - src/rrd.h
dbaf986503675fde06923ddc66d01024e08c5b23
[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 // time in seconds to delete unupdated dimensions
13 // set to zero to disable this feature
14 extern int rrd_delete_unupdated_dimensions;
15
16 #define RRD_ID_LENGTH_MAX 400
17
18 #define RRDSET_MAGIC        "NETDATA RRD SET FILE V018"
19 #define RRDDIMENSION_MAGIC  "NETDATA RRD DIMENSION FILE V018"
20
21 typedef long long total_number;
22 #define TOTAL_NUMBER_FORMAT "%lld"
23
24 // ----------------------------------------------------------------------------
25 // chart types
26
27 #define RRDSET_TYPE_LINE_NAME "line"
28 #define RRDSET_TYPE_AREA_NAME "area"
29 #define RRDSET_TYPE_STACKED_NAME "stacked"
30
31 #define RRDSET_TYPE_LINE    0
32 #define RRDSET_TYPE_AREA    1
33 #define RRDSET_TYPE_STACKED 2
34
35 int rrdset_type_id(const char *name);
36 const char *rrdset_type_name(int chart_type);
37
38
39 // ----------------------------------------------------------------------------
40 // memory mode
41
42 #define RRD_MEMORY_MODE_RAM_NAME "ram"
43 #define RRD_MEMORY_MODE_MAP_NAME "map"
44 #define RRD_MEMORY_MODE_SAVE_NAME "save"
45
46 #define RRD_MEMORY_MODE_RAM 0
47 #define RRD_MEMORY_MODE_MAP 1
48 #define RRD_MEMORY_MODE_SAVE 2
49
50 extern int rrd_memory_mode;
51
52 extern const char *rrd_memory_mode_name(int id);
53 extern int rrd_memory_mode_id(const char *name);
54
55
56 // ----------------------------------------------------------------------------
57 // algorithms types
58
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"
63
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
68
69 extern int rrddim_algorithm_id(const char *name);
70 extern const char *rrddim_algorithm_name(int chart_type);
71
72 // ----------------------------------------------------------------------------
73 // flags
74
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
77
78 // ----------------------------------------------------------------------------
79 // RRD CONTEXT
80
81 struct rrdfamily {
82     avl avl;
83
84     const char *family;
85     uint32_t hash_family;
86
87     size_t use_count;
88
89     avl_tree_lock variables_root_index;
90 };
91 typedef struct rrdfamily RRDFAMILY;
92
93 // ----------------------------------------------------------------------------
94 // RRD DIMENSION
95
96 struct rrddim {
97     // ------------------------------------------------------------------------
98     // binary indexing structures
99
100     avl avl;                                        // the binary index - this has to be first member!
101
102     // ------------------------------------------------------------------------
103     // the dimension definition
104
105     char id[RRD_ID_LENGTH_MAX + 1];                 // the id of this dimension (for internal identification)
106
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)
111
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
115
116     int mapped;                                     // if set to non zero, this dimension is mapped to a file
117
118     // ------------------------------------------------------------------------
119     // members for temporary data we need for calculations
120
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
124
125     // FIXME
126     // we need the hash_name too!
127
128     uint32_t flags;
129
130     char cache_filename[FILENAME_MAX+1];            // the filename we load/save from/to this set
131
132     unsigned long counter;                          // the number of times we added values to this rrdim
133
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
136
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
140
141     calculated_number calculated_value;             // the current calculated value, after applying the algorithm - resets to zero after being used
142     calculated_number last_calculated_value;        // the last calculated value processed
143
144     calculated_number last_stored_value;            // the last value as stored in the database (after interpolation)
145
146     collected_number collected_value;               // the current value, as collected - resets to 0 after being used
147     collected_number last_collected_value;          // the last value that was collected, after being processed
148
149     // the *_volume members are used to calculate the accuracy of the rounding done by the
150     // storage number - they are printed to debug.log when debug is enabled for a set.
151     calculated_number collected_volume;             // the sum of all collected values so far
152     calculated_number stored_volume;                // the sum of all stored values so far
153
154     struct rrddim *next;                            // linking of dimensions within the same data set
155     struct rrdset *rrdset;
156
157     // ------------------------------------------------------------------------
158     // members for checking the data when loading from disk
159
160     long entries;                                   // how many entries this dimension has in ram
161                                                     // this is the same to the entries of the data set
162                                                     // we set it here, to check the data when we load it from disk.
163
164     int update_every;                               // every how many seconds is this updated
165
166     unsigned long memsize;                          // the memory allocated for this dimension
167
168     char magic[sizeof(RRDDIMENSION_MAGIC) + 1];     // a string to be saved, used to identify our data file
169
170     struct rrddimvar *variables;
171
172     // ------------------------------------------------------------------------
173     // the values stored in this dimension, using our floating point numbers
174
175     storage_number values[];                        // the array of values - THIS HAS TO BE THE LAST MEMBER
176 };
177 typedef struct rrddim RRDDIM;
178
179
180 // ----------------------------------------------------------------------------
181 // RRDSET
182
183 struct rrdset {
184     // ------------------------------------------------------------------------
185     // binary indexing structures
186
187     avl avl;                                        // the index, with key the id - this has to be first!
188     avl avlname;                                    // the index, with key the name
189
190     // ------------------------------------------------------------------------
191     // the set configuration
192
193     char id[RRD_ID_LENGTH_MAX + 1];                 // id of the data set
194
195     const char *name;                               // the name of this dimension (as presented to user)
196                                                     // this is a pointer to the config structure
197                                                     // since the config always has a higher priority
198                                                     // (the user overwrites the name of the charts)
199
200     char *type;                                     // the type of graph RRD_TYPE_* (a category, for determining graphing options)
201     char *family;                                   // grouping sets under the same family
202     char *title;                                    // title shown to user
203     char *units;                                    // units of measurement
204
205     char *context;                                  // the template of this data set
206     uint32_t hash_context;
207
208     int chart_type;
209
210     int update_every;                               // every how many seconds is this updated?
211
212     long entries;                                   // total number of entries in the data set
213
214     long current_entry;                             // the entry that is currently being updated
215                                                     // it goes around in a round-robin fashion
216
217     int enabled;
218
219     int gap_when_lost_iterations_above;             // after how many lost iterations a gap should be stored
220                                                     // netdata will interpolate values for gaps lower than this
221
222     long priority;
223
224     int isdetail;                                   // if set, the data set should be considered as a detail of another
225                                                     // (the master data set should be the one that has the same family and is not detail)
226
227     // ------------------------------------------------------------------------
228     // members for temporary data we need for calculations
229
230     int mapped;                                     // if set to 1, this is memory mapped
231
232     int debug;
233
234     char *cache_dir;                                // the directory to store dimensions
235     char cache_filename[FILENAME_MAX+1];            // the filename to store this set
236
237     pthread_rwlock_t rwlock;
238
239     unsigned long counter;                          // the number of times we added values to this rrd
240     unsigned long counter_done;                     // the number of times we added values to this rrd
241
242     uint32_t hash;                                  // a simple hash on the id, to speed up searching
243                                                     // we first compare hashes, and only if the hashes are equal we do string comparisons
244
245     uint32_t hash_name;                             // a simple hash on the name
246
247     usec_t usec_since_last_update;                  // the time in microseconds since the last collection of data
248
249     struct timeval last_updated;                    // when this data set was last updated (updated every time the rrd_stats_done() function)
250     struct timeval last_collected_time;             // when did this data set last collected values
251
252     total_number collected_total;                   // used internally to calculate percentages
253     total_number last_collected_total;              // used internally to calculate percentages
254
255     RRDFAMILY *rrdfamily;
256     struct rrdhost *rrdhost;
257
258     struct rrdset *next;                            // linking of rrdsets
259
260     // ------------------------------------------------------------------------
261     // local variables
262
263     calculated_number green;
264     calculated_number red;
265
266     avl_tree_lock variables_root_index;
267     RRDSETVAR *variables;
268     RRDCALC *alarms;
269
270     // ------------------------------------------------------------------------
271     // members for checking the data when loading from disk
272
273     unsigned long memsize;                          // how much mem we have allocated for this (without dimensions)
274
275     char magic[sizeof(RRDSET_MAGIC) + 1];           // our magic
276
277     // ------------------------------------------------------------------------
278     // the dimensions
279
280     avl_tree_lock dimensions_index;                 // the root of the dimensions index
281     RRDDIM *dimensions;                             // the actual data for every dimension
282
283 };
284 typedef struct rrdset RRDSET;
285
286 // ----------------------------------------------------------------------------
287 // RRD HOST
288
289 struct rrdhost {
290     avl avl;
291
292     char *hostname;
293
294     RRDSET *rrdset_root;
295     pthread_rwlock_t rrdset_root_rwlock;
296
297     avl_tree_lock rrdset_root_index;
298     avl_tree_lock rrdset_root_index_name;
299
300     avl_tree_lock rrdfamily_root_index;
301     avl_tree_lock variables_root_index;
302
303     // all RRDCALCs are primarily allocated and linked here
304     // RRDCALCs may be linked to charts at any point
305     // (charts may or may not exist when these are loaded)
306     RRDCALC *alarms;
307     ALARM_LOG health_log;
308
309     RRDCALCTEMPLATE *templates;
310 };
311 typedef struct rrdhost RRDHOST;
312 extern RRDHOST localhost;
313 extern void rrdhost_init(char *hostname);
314
315 #ifdef NETDATA_INTERNAL_CHECKS
316 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
317 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
318 #else
319 #define rrdhost_check_rdlock(host) (void)0
320 #define rrdhost_check_wrlock(host) (void)0
321 #endif
322
323 extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
324 extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
325
326 extern void rrdhost_rwlock(RRDHOST *host);
327 extern void rrdhost_rdlock(RRDHOST *host);
328 extern void rrdhost_unlock(RRDHOST *host);
329
330 // ----------------------------------------------------------------------------
331 // RRD SET functions
332
333 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
334 extern void rrdset_set_name(RRDSET *st, const char *name);
335
336 extern char *rrdset_cache_dir(const char *id);
337
338 extern void rrdset_reset(RRDSET *st);
339
340 extern RRDSET *rrdset_create(const char *type
341         , const char *id
342         , const char *name
343         , const char *family
344         , const char *context
345         , const char *title
346         , const char *units
347         , long priority
348         , int update_every
349         , int chart_type);
350
351 extern void rrdset_free_all(void);
352 extern void rrdset_save_all(void);
353
354 extern RRDSET *rrdset_find(const char *id);
355 extern RRDSET *rrdset_find_bytype(const char *type, const char *id);
356 extern RRDSET *rrdset_find_byname(const char *name);
357
358 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
359 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
360 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
361
362 extern usec_t rrdset_done(RRDSET *st);
363
364 // get the total duration in seconds of the round robin database
365 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
366
367 // get the timestamp of the last entry in the round robin database
368 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
369
370 // get the timestamp of first entry in the round robin database
371 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
372
373 // get the last slot updated in the round robin database
374 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
375
376 // get the first / oldest slot updated in the round robin database
377 #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) ))
378
379 // get the slot of the round robin database, for the given timestamp (t)
380 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
381 #define rrdset_time2slot(st, t) ( \
382         (  (time_t)(t) >= rrdset_last_entry_t(st))  ? ( rrdset_last_slot(st) ) : \
383         ( ((time_t)(t) <= rrdset_first_entry_t(st)) ?   rrdset_first_slot(st) : \
384         ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
385           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
386           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
387         )))
388
389 // get the timestamp of a specific slot in the round robin database
390 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
391         ((unsigned long)(st)->update_every * ( \
392                 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
393                 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
394                 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
395         ))
396
397 // ----------------------------------------------------------------------------
398 // RRD DIMENSION functions
399
400 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm);
401
402 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
403 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
404
405 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
406
407 extern int rrddim_hide(RRDSET *st, const char *id);
408 extern int rrddim_unhide(RRDSET *st, const char *id);
409
410 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
411 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
412
413 #endif /* NETDATA_RRD_H */