]> arthur.barton.de Git - netdata.git/blob - src/rrd.h
added libavl for supporting balanced binary trees - this improves search performance...
[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_DEFAULT_GAP_INTERPOLATIONS 10
19
20 typedef long long total_number;
21 #define TOTAL_NUMBER_FORMAT "%lld"
22
23 #define RRD_ID_LENGTH_MAX 1024
24
25 #define RRDSET_MAGIC            "NETDATA CACHE STATS FILE V011"
26 #define RRDDIMENSION_MAGIC      "NETDATA CACHE DIMENSION FILE V011"
27
28 // ----------------------------------------------------------------------------
29 // chart types
30
31 #define RRDSET_TYPE_LINE_NAME "line"
32 #define RRDSET_TYPE_AREA_NAME "area"
33 #define RRDSET_TYPE_STACKED_NAME "stacked"
34
35 #define RRDSET_TYPE_LINE        0
36 #define RRDSET_TYPE_AREA        1
37 #define RRDSET_TYPE_STACKED 2
38
39 int rrdset_type_id(const char *name);
40 const char *rrdset_type_name(int chart_type);
41
42
43 // ----------------------------------------------------------------------------
44 // memory mode
45
46 #define RRD_MEMORY_MODE_RAM_NAME "ram"
47 #define RRD_MEMORY_MODE_MAP_NAME "map"
48 #define RRD_MEMORY_MODE_SAVE_NAME "save"
49
50 #define RRD_MEMORY_MODE_RAM 0
51 #define RRD_MEMORY_MODE_MAP 1
52 #define RRD_MEMORY_MODE_SAVE 2
53
54 extern int rrd_memory_mode;
55
56 extern const char *rrd_memory_mode_name(int id);
57 extern int rrd_memory_mode_id(const char *name);
58
59
60 // ----------------------------------------------------------------------------
61 // algorithms types
62
63 #define RRDDIM_ABSOLUTE_NAME                            "absolute"
64 #define RRDDIM_INCREMENTAL_NAME                         "incremental"
65 #define RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME       "percentage-of-incremental-row"
66 #define RRDDIM_PCENT_OVER_ROW_TOTAL_NAME        "percentage-of-absolute-row"
67
68 #define RRDDIM_ABSOLUTE                                 0
69 #define RRDDIM_INCREMENTAL                              1
70 #define RRDDIM_PCENT_OVER_DIFF_TOTAL    2
71 #define RRDDIM_PCENT_OVER_ROW_TOTAL             3
72
73 extern int rrddim_algorithm_id(const char *name);
74 extern const char *rrddim_algorithm_name(int chart_type);
75
76
77 // ----------------------------------------------------------------------------
78 // RRD DIMENSION
79
80 struct rrddim {
81         avl avl;
82
83         char magic[sizeof(RRDDIMENSION_MAGIC) + 1];             // our magic
84         char id[RRD_ID_LENGTH_MAX + 1];                                         // the id of this dimension (for internal identification)
85         const char *name;                                                               // the name of this dimension (as presented to user)
86         char cache_filename[FILENAME_MAX+1];
87         
88         unsigned long hash;                                                             // a simple hash on the id, to speed up searching
89                                                                                                         // we first compare hashes, and only if the hashes are equal we do string comparisons
90
91         long entries;                                                                   // how many entries this dimension has
92                                                                                                         // this should be the same to the entries of the data set
93
94         int update_every;                                                               // every how many seconds is this updated?
95         int updated;                                                                    // set to 0 after each calculation, to 1 after each collected value
96
97         int hidden;                                                                             // if set to non zero, this dimension will not be sent to the client
98         int mapped;                                                                             // 1 if the file is mapped
99         unsigned long memsize;                                                  // the memory allocated for this dimension
100
101         int algorithm;
102         long multiplier;
103         long divisor;
104
105         struct timeval last_collected_time;                             // when was this dimension last updated
106                                                                                                         // this is actual date time we updated the last_collected_value
107                                                                                                         // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRD_STATS
108
109         calculated_number calculated_value;
110         calculated_number last_calculated_value;
111
112         collected_number collected_value;                               // the value collected at this round
113         collected_number last_collected_value;                  // the value that was collected at the last round
114
115         calculated_number collected_volume;
116         calculated_number stored_volume;
117
118         struct rrddim *next;                                            // linking of dimensions within the same data set
119
120         storage_number values[];                                                // the array of values - THIS HAS TO BE THE LAST MEMBER
121 };
122 typedef struct rrddim RRDDIM;
123
124
125 // ----------------------------------------------------------------------------
126 // RRDSET
127
128 struct rrdset {
129         avl avl;
130
131         char magic[sizeof(RRDSET_MAGIC) + 1];                   // our magic
132
133         char id[RRD_ID_LENGTH_MAX + 1];                                         // id of the data set
134         const char *name;                                                               // name of the data set
135         char *cache_dir;                                                                // the directory to store dimension maps
136         char cache_filename[FILENAME_MAX+1];
137
138         char *type;                                                                             // the type of graph RRD_TYPE_* (a category, for determining graphing options)
139         char *family;                                                                   // the family of this data set (for grouping them together)
140         char *title;                                                                    // title shown to user
141         char *units;                                                                    // units of measurement
142
143         pthread_rwlock_t rwlock;
144         unsigned long counter;                                                  // the number of times we added values to this rrd
145         unsigned long counter_done;                                             // the number of times we added values to this rrd
146
147         int mapped;                                                                             // if set to 1, this is memory mapped
148         unsigned long memsize;                                                  // how much mem we have allocated for this (without dimensions)
149
150         unsigned long hash_name;                                                // a simple hash on the name
151         unsigned long hash;                                                             // a simple hash on the id, to speed up searching
152                                                                                                         // we first compare hashes, and only if the hashes are equal we do string comparisons
153
154         int gap_when_lost_iterations_above;                             // after how many lost iterations a gap should be stored
155                                                                                                         // netdata will interpolate values for gaps lower than this
156
157         long priority;
158
159         long entries;                                                                   // total number of entries in the data set
160         long current_entry;                                                             // the entry that is currently being updated
161                                                                                                         // it goes around in a round-robin fashion
162
163         int update_every;                                                               // every how many seconds is this updated?
164         unsigned long long first_entry_t;                               // the timestamp (in microseconds) of the oldest entry in the db
165         struct timeval last_updated;                                    // when this data set was last updated (updated every time the rrd_stats_done() function)
166         struct timeval last_collected_time;                             // 
167         unsigned long long usec_since_last_update;
168
169         total_number collected_total;
170         total_number last_collected_total;
171
172         int chart_type;
173         int debug;
174         int enabled;
175         int isdetail;                                                                   // if set, the data set should be considered as a detail of another
176                                                                                                         // (the master data set should be the one that has the same family and is not detail)
177
178         RRDDIM *dimensions;                                                             // the actual data for every dimension
179
180         avl_tree dimensions_index;
181
182         struct rrdset *next;                                                    // linking of rrdsets
183 };
184 typedef struct rrdset RRDSET;
185
186 extern RRDSET *rrdset_root;
187 extern pthread_rwlock_t rrdset_root_rwlock;
188
189 // ----------------------------------------------------------------------------
190 // RRD SET functions
191
192 extern char *rrdset_strncpy_name(char *to, const char *from, int length);
193 extern void rrdset_set_name(RRDSET *st, const char *name);
194
195 extern char *rrdset_cache_dir(const char *id);
196
197 extern void rrdset_reset(RRDSET *st);
198
199 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);
200
201 extern void rrdset_free_all(void);
202 extern void rrdset_save_all(void);
203
204 extern RRDSET *rrdset_find(const char *id);
205 extern RRDSET *rrdset_find_bytype(const char *type, const char *id);
206 extern RRDSET *rrdset_find_byname(const char *name);
207
208 extern void rrdset_next_usec(RRDSET *st, unsigned long long microseconds);
209 extern void rrdset_next(RRDSET *st);
210 extern void rrdset_next_plugins(RRDSET *st);
211
212 extern unsigned long long rrdset_done(RRDSET *st);
213
214 extern time_t rrdset_first_entry_t(RRDSET *st);
215
216
217 // ----------------------------------------------------------------------------
218 // RRD DIMENSION functions
219
220 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm);
221
222 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
223 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
224
225 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
226
227 extern int rrddim_hide(RRDSET *st, const char *id);
228
229 extern void rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
230 extern int rrddim_set(RRDSET *st, const char *id, collected_number value);
231
232 #endif /* NETDATA_RRD_H */