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