5 #include "storage_number.h"
8 #define NETDATA_RRD_H 1
10 #define UPDATE_EVERY 1
11 #define UPDATE_EVERY_MAX 3600
12 extern int rrd_update_every;
14 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
15 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
16 extern int rrd_default_history_entries;
18 #define RRD_ID_LENGTH_MAX 1024
20 #define RRDSET_MAGIC "NETDATA RRD SET FILE V012"
21 #define RRDDIMENSION_MAGIC "NETDATA RRD DIMENSION FILE V012"
23 typedef long long total_number;
24 #define TOTAL_NUMBER_FORMAT "%lld"
26 // ----------------------------------------------------------------------------
29 #define RRDSET_TYPE_LINE_NAME "line"
30 #define RRDSET_TYPE_AREA_NAME "area"
31 #define RRDSET_TYPE_STACKED_NAME "stacked"
33 #define RRDSET_TYPE_LINE 0
34 #define RRDSET_TYPE_AREA 1
35 #define RRDSET_TYPE_STACKED 2
37 int rrdset_type_id(const char *name);
38 const char *rrdset_type_name(int chart_type);
41 // ----------------------------------------------------------------------------
44 #define RRD_MEMORY_MODE_RAM_NAME "ram"
45 #define RRD_MEMORY_MODE_MAP_NAME "map"
46 #define RRD_MEMORY_MODE_SAVE_NAME "save"
48 #define RRD_MEMORY_MODE_RAM 0
49 #define RRD_MEMORY_MODE_MAP 1
50 #define RRD_MEMORY_MODE_SAVE 2
52 extern int rrd_memory_mode;
54 extern const char *rrd_memory_mode_name(int id);
55 extern int rrd_memory_mode_id(const char *name);
58 // ----------------------------------------------------------------------------
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"
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
71 extern int rrddim_algorithm_id(const char *name);
72 extern const char *rrddim_algorithm_name(int chart_type);
75 // ----------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
80 // binary indexing structures
82 avl avl; // the binary index - this has to be first member!
84 // ------------------------------------------------------------------------
85 // the dimension definition
87 char id[RRD_ID_LENGTH_MAX + 1]; // the id of this dimension (for internal identification)
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)
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
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
101 // ------------------------------------------------------------------------
102 // members for temporary data we need for calculations
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
108 char cache_filename[FILENAME_MAX+1]; // the filename we load/save from/to this set
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
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
117 calculated_number calculated_value; // the current calculated value, after applying the algorithm
118 calculated_number last_calculated_value; // the last calculated value
120 collected_number collected_value; // the current value, as collected
121 collected_number last_collected_value; // the last value that was collected
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
128 struct rrddim *next; // linking of dimensions within the same data set
130 // ------------------------------------------------------------------------
131 // members for checking the data when loading from disk
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.
137 int update_every; // every how many seconds is this updated
139 unsigned long memsize; // the memory allocated for this dimension
141 char magic[sizeof(RRDDIMENSION_MAGIC) + 1]; // a string to be saved, used to identify our data file
143 // ------------------------------------------------------------------------
144 // the values stored in this dimension, using our floating point numbers
146 storage_number values[]; // the array of values - THIS HAS TO BE THE LAST MEMBER
148 typedef struct rrddim RRDDIM;
151 // ----------------------------------------------------------------------------
155 // ------------------------------------------------------------------------
156 // binary indexing structures
158 avl avl; // the index, with key the id - this has to be first!
159 avl avlname; // the index, with key the name
161 // ------------------------------------------------------------------------
162 // the set configuration
164 char id[RRD_ID_LENGTH_MAX + 1]; // id of the data set
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)
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
178 int update_every; // every how many seconds is this updated?
180 long entries; // total number of entries in the data set
182 long current_entry; // the entry that is currently being updated
183 // it goes around in a round-robin fashion
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
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)
195 // ------------------------------------------------------------------------
196 // members for temporary data we need for calculations
198 int mapped; // if set to 1, this is memory mapped
202 char *cache_dir; // the directory to store dimensions
203 char cache_filename[FILENAME_MAX+1]; // the filename to store this set
205 pthread_rwlock_t rwlock;
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
210 unsigned long long first_entry_t; // the timestamp (in microseconds) of the oldest entry in the db
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
215 unsigned long hash_name; // a simple hash on the name
217 unsigned long long usec_since_last_update; // the time in microseconds since the last collection of data
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
222 total_number collected_total; // used internally to calculate percentages
223 total_number last_collected_total; // used internally to calculate percentages
225 struct rrdset *next; // linking of rrdsets
227 // ------------------------------------------------------------------------
228 // members for checking the data when loading from disk
230 unsigned long memsize; // how much mem we have allocated for this (without dimensions)
232 char magic[sizeof(RRDSET_MAGIC) + 1]; // our magic
234 // ------------------------------------------------------------------------
237 avl_tree dimensions_index; // the root of the dimensions index
238 RRDDIM *dimensions; // the actual data for every dimension
240 typedef struct rrdset RRDSET;
242 extern RRDSET *rrdset_root;
243 extern pthread_rwlock_t rrdset_root_rwlock;
245 // ----------------------------------------------------------------------------
248 extern char *rrdset_strncpy_name(char *to, const char *from, int length);
249 extern void rrdset_set_name(RRDSET *st, const char *name);
251 extern char *rrdset_cache_dir(const char *id);
253 extern void rrdset_reset(RRDSET *st);
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);
257 extern void rrdset_free_all(void);
258 extern void rrdset_save_all(void);
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);
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);
268 extern unsigned long long rrdset_done(RRDSET *st);
270 extern time_t rrdset_first_entry_t(RRDSET *st);
273 // ----------------------------------------------------------------------------
274 // RRD DIMENSION functions
276 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm);
278 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
279 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
281 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
283 extern int rrddim_hide(RRDSET *st, const char *id);
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);
288 #endif /* NETDATA_RRD_H */