]> arthur.barton.de Git - netdata.git/blob - src/rrd.h
preparation for multi-host internal database
[netdata.git] / src / rrd.h
1 #include <sys/time.h>
2 #include <pthread.h>
3 #include <stdint.h>
4
5 #include "avl.h"
6 #include "storage_number.h"
7
8 #ifndef NETDATA_RRD_H
9 #define NETDATA_RRD_H 1
10
11 #define UPDATE_EVERY 1
12 #define UPDATE_EVERY_MAX 3600
13 extern int rrd_update_every;
14
15 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
16 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
17 extern int rrd_default_history_entries;
18
19 // time in seconds to delete unupdated dimensions
20 // set to zero to disable this feature
21 extern int rrd_delete_unupdated_dimensions;
22
23 #define RRD_ID_LENGTH_MAX 1024
24
25 #define RRDSET_MAGIC            "NETDATA RRD SET FILE V017"
26 #define RRDDIMENSION_MAGIC      "NETDATA RRD DIMENSION FILE V017"
27
28 typedef long long total_number;
29 #define TOTAL_NUMBER_FORMAT "%lld"
30
31 // ----------------------------------------------------------------------------
32 // chart types
33
34 #define RRDSET_TYPE_LINE_NAME "line"
35 #define RRDSET_TYPE_AREA_NAME "area"
36 #define RRDSET_TYPE_STACKED_NAME "stacked"
37
38 #define RRDSET_TYPE_LINE        0
39 #define RRDSET_TYPE_AREA        1
40 #define RRDSET_TYPE_STACKED 2
41
42 int rrdset_type_id(const char *name);
43 const char *rrdset_type_name(int chart_type);
44
45
46 // ----------------------------------------------------------------------------
47 // memory mode
48
49 #define RRD_MEMORY_MODE_RAM_NAME "ram"
50 #define RRD_MEMORY_MODE_MAP_NAME "map"
51 #define RRD_MEMORY_MODE_SAVE_NAME "save"
52
53 #define RRD_MEMORY_MODE_RAM 0
54 #define RRD_MEMORY_MODE_MAP 1
55 #define RRD_MEMORY_MODE_SAVE 2
56
57 extern int rrd_memory_mode;
58
59 extern const char *rrd_memory_mode_name(int id);
60 extern int rrd_memory_mode_id(const char *name);
61
62
63 // ----------------------------------------------------------------------------
64 // algorithms types
65
66 #define RRDDIM_ABSOLUTE_NAME                            "absolute"
67 #define RRDDIM_INCREMENTAL_NAME                         "incremental"
68 #define RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME       "percentage-of-incremental-row"
69 #define RRDDIM_PCENT_OVER_ROW_TOTAL_NAME        "percentage-of-absolute-row"
70
71 #define RRDDIM_ABSOLUTE                                 0
72 #define RRDDIM_INCREMENTAL                              1
73 #define RRDDIM_PCENT_OVER_DIFF_TOTAL    2
74 #define RRDDIM_PCENT_OVER_ROW_TOTAL             3
75
76 extern int rrddim_algorithm_id(const char *name);
77 extern const char *rrddim_algorithm_name(int chart_type);
78
79 // ----------------------------------------------------------------------------
80 // flags
81
82 #define RRDDIM_FLAG_HIDDEN 0x00000001 // this dimension will not be offered to callers
83 #define RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS 0x00000002 // do not offer RESET or OVERFLOW info to callers
84
85 // ----------------------------------------------------------------------------
86 // RRD DIMENSION
87
88 struct rrddim {
89         // ------------------------------------------------------------------------
90         // binary indexing structures
91
92         avl avl;                                                                                // the binary index - this has to be first member!
93
94         // ------------------------------------------------------------------------
95         // the dimension definition
96
97         char id[RRD_ID_LENGTH_MAX + 1];                                 // the id of this dimension (for internal identification)
98
99         const char *name;                                                               // the name of this dimension (as presented to user)
100                                                                                                         // this is a pointer to the config structure
101                                                                                                         // since the config always has a higher priority
102                                                                                                         // (the user overwrites the name of the charts)
103
104         int algorithm;                                                                  // the algorithm that is applied to add new collected values
105         long multiplier;                                                                // the multiplier of the collected values
106         long divisor;                                                                   // the divider of the collected values
107
108         int mapped;                                                                             // if set to non zero, this dimension is mapped to a file
109
110         // ------------------------------------------------------------------------
111         // members for temporary data we need for calculations
112
113         uint32_t hash;                                                                  // a simple hash of the id, to speed up searching / indexing
114                                                                                                         // instead of strcmp() every item in the binary index
115                                                                                                         // we first compare the hashes
116
117         // FIXME
118         // we need the hash_name too!
119
120         uint32_t flags;
121
122         char cache_filename[FILENAME_MAX+1];                    // the filename we load/save from/to this set
123
124         unsigned long counter;                                                  // the number of times we added values to this rrdim
125
126         int updated;                                                                    // set to 0 after each calculation, to 1 after each collected value
127                                                                                                         // we use this to detect that a dimension is not updated
128
129         struct timeval last_collected_time;                             // when was this dimension last updated
130                                                                                                         // this is actual date time we updated the last_collected_value
131                                                                                                         // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
132
133         calculated_number calculated_value;                             // the current calculated value, after applying the algorithm
134         calculated_number last_calculated_value;                // the last calculated value
135
136         collected_number collected_value;                               // the current value, as collected
137         collected_number last_collected_value;                  // the last value that was collected
138
139         // the *_volume members are used to calculate the accuracy of the rounding done by the
140         // storage number - they are printed to debug.log when debug is enabled for a set.
141         calculated_number collected_volume;                             // the sum of all collected values so far
142         calculated_number stored_volume;                                // the sum of all stored values so far
143
144         struct rrddim *next;                                                    // linking of dimensions within the same data set
145
146         // ------------------------------------------------------------------------
147         // members for checking the data when loading from disk
148
149         long entries;                                                                   // how many entries this dimension has in ram
150                                                                                                         // this is the same to the entries of the data set
151                                                                                                         // we set it here, to check the data when we load it from disk.
152
153         int update_every;                                                               // every how many seconds is this updated
154
155         unsigned long memsize;                                                  // the memory allocated for this dimension
156
157         char magic[sizeof(RRDDIMENSION_MAGIC) + 1];             // a string to be saved, used to identify our data file
158
159         // ------------------------------------------------------------------------
160         // the values stored in this dimension, using our floating point numbers
161
162         storage_number values[];                                                // the array of values - THIS HAS TO BE THE LAST MEMBER
163 };
164 typedef struct rrddim RRDDIM;
165
166
167 // ----------------------------------------------------------------------------
168 // RRDSET
169
170 struct rrdset {
171         // ------------------------------------------------------------------------
172         // binary indexing structures
173
174         avl avl;                                                                                // the index, with key the id - this has to be first!
175         avl avlname;                                                                    // the index, with key the name
176
177         // ------------------------------------------------------------------------
178         // the set configuration
179
180         char id[RRD_ID_LENGTH_MAX + 1];                                 // id of the data set
181
182         const char *name;                                                               // the name of this dimension (as presented to user)
183                                                                                                         // this is a pointer to the config structure
184                                                                                                         // since the config always has a higher priority
185                                                                                                         // (the user overwrites the name of the charts)
186
187         char *type;                                                                             // the type of graph RRD_TYPE_* (a category, for determining graphing options)
188         char *family;                                                                   // grouping sets under the same family
189         char *context;                                                                  // the template of this data set
190         char *title;                                                                    // title shown to user
191         char *units;                                                                    // units of measurement
192
193         int chart_type;
194
195         int update_every;                                                               // every how many seconds is this updated?
196
197         long entries;                                                                   // total number of entries in the data set
198
199         long current_entry;                                                             // the entry that is currently being updated
200                                                                                                         // it goes around in a round-robin fashion
201
202         int enabled;
203
204         int gap_when_lost_iterations_above;                             // after how many lost iterations a gap should be stored
205                                                                                                         // netdata will interpolate values for gaps lower than this
206
207         long priority;
208
209         int isdetail;                                                                   // if set, the data set should be considered as a detail of another
210                                                                                                         // (the master data set should be the one that has the same family and is not detail)
211
212         // ------------------------------------------------------------------------
213         // members for temporary data we need for calculations
214
215         int mapped;                                                                             // if set to 1, this is memory mapped
216
217         int debug;
218
219         char *cache_dir;                                                                // the directory to store dimensions
220         char cache_filename[FILENAME_MAX+1];                    // the filename to store this set
221
222         pthread_rwlock_t rwlock;
223
224         unsigned long counter;                                                  // the number of times we added values to this rrd
225         unsigned long counter_done;                                             // the number of times we added values to this rrd
226
227         uint32_t hash;                                                                  // a simple hash on the id, to speed up searching
228                                                                                                         // we first compare hashes, and only if the hashes are equal we do string comparisons
229
230         uint32_t hash_name;                                                             // a simple hash on the name
231
232         unsigned long long usec_since_last_update;              // the time in microseconds since the last collection of data
233
234         struct timeval last_updated;                                    // when this data set was last updated (updated every time the rrd_stats_done() function)
235         struct timeval last_collected_time;                             // when did this data set last collected values
236
237         total_number collected_total;                                   // used internally to calculate percentages
238         total_number last_collected_total;                              // used internally to calculate percentages
239
240         struct rrdset *next;                                                    // linking of rrdsets
241
242         // ------------------------------------------------------------------------
243         // members for checking the data when loading from disk
244
245         unsigned long memsize;                                                  // how much mem we have allocated for this (without dimensions)
246
247         char magic[sizeof(RRDSET_MAGIC) + 1];                   // our magic
248
249         // ------------------------------------------------------------------------
250         // the dimensions
251
252         avl_tree_lock dimensions_index;                                         // the root of the dimensions index
253         RRDDIM *dimensions;                                                             // the actual data for every dimension
254 };
255 typedef struct rrdset RRDSET;
256
257 // ----------------------------------------------------------------------------
258 // RRD CONTEXT
259
260 struct rrdcontext {
261     avl avl;
262
263     char *context;
264 };
265 typedef struct rrdcontext RRDCONTEXT;
266
267
268 // ----------------------------------------------------------------------------
269 // RRD HOST
270
271 struct rrdhost {
272     char *hostname;
273
274     RRDSET *rrdset_root;
275     pthread_rwlock_t rrdset_root_rwlock;
276
277     avl_tree_lock rrdset_root_index;
278     avl_tree_lock rrdset_root_index_name;
279 };
280 typedef struct rrdhost RRDHOST;
281
282 extern RRDHOST localhost;
283
284 // ----------------------------------------------------------------------------
285 // RRD SET functions
286
287 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
288 extern void rrdset_set_name(RRDSET *st, const char *name);
289
290 extern char *rrdset_cache_dir(const char *id);
291
292 extern void rrdset_reset(RRDSET *st);
293
294 extern RRDSET *rrdset_create(const char *type
295                 , const char *id
296                 , const char *name
297                 , const char *family
298                 , const char *context
299                 , const char *title
300                 , const char *units
301                 , long priority
302                 , int update_every
303                 , int chart_type);
304
305 extern void rrdset_free_all(void);
306 extern void rrdset_save_all(void);
307
308 extern RRDSET *rrdset_find(const char *id);
309 extern RRDSET *rrdset_find_bytype(const char *type, const char *id);
310 extern RRDSET *rrdset_find_byname(const char *name);
311
312 extern void rrdset_next_usec(RRDSET *st, unsigned long long microseconds);
313 extern void rrdset_next(RRDSET *st);
314 extern void rrdset_next_plugins(RRDSET *st);
315
316 extern unsigned long long rrdset_done(RRDSET *st);
317
318 // get the total duration in seconds of the round robin database
319 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
320
321 // get the timestamp of the last entry in the round robin database
322 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
323
324 // get the timestamp of first entry in the round robin database
325 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
326
327 // get the last slot updated in the round robin database
328 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
329
330 // get the first / oldest slot updated in the round robin database
331 #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) ))
332
333 // get the slot of the round robin database, for the given timestamp (t)
334 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
335 #define rrdset_time2slot(st, t) ( \
336                 (  (time_t)(t) >= rrdset_last_entry_t(st))  ? ( rrdset_last_slot(st) ) : \
337                 ( ((time_t)(t) <= rrdset_first_entry_t(st)) ?   rrdset_first_slot(st) : \
338                 ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
339                   (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
340                   (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
341                 )))
342
343 // get the timestamp of a specific slot in the round robin database
344 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
345                 ((unsigned long)(st)->update_every * ( \
346                                 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
347                                 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
348                                 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
349                 ))
350
351 // ----------------------------------------------------------------------------
352 // RRD DIMENSION functions
353
354 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm);
355
356 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
357 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
358
359 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
360
361 extern int rrddim_hide(RRDSET *st, const char *id);
362 extern int rrddim_unhide(RRDSET *st, const char *id);
363
364 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
365 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
366
367 #endif /* NETDATA_RRD_H */