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