]> arthur.barton.de Git - netdata.git/blob - src/rrd.h
Merge branch 'master' into ab-debian
[netdata.git] / src / rrd.h
1 #ifndef NETDATA_RRD_H
2 #define NETDATA_RRD_H 1
3
4 #define UPDATE_EVERY 1
5 #define UPDATE_EVERY_MAX 3600
6
7 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
8 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
9
10 extern int default_rrd_update_every;
11 extern int default_rrd_history_entries;
12
13 #define RRD_ID_LENGTH_MAX 200
14
15 #define RRDSET_MAGIC        "NETDATA RRD SET FILE V019"
16 #define RRDDIMENSION_MAGIC  "NETDATA RRD DIMENSION FILE V019"
17
18 typedef long long total_number;
19 #define TOTAL_NUMBER_FORMAT "%lld"
20
21 // ----------------------------------------------------------------------------
22 // chart types
23
24 typedef enum rrdset_type {
25     RRDSET_TYPE_LINE    = 0,
26     RRDSET_TYPE_AREA    = 1,
27     RRDSET_TYPE_STACKED = 2
28 } RRDSET_TYPE;
29
30 #define RRDSET_TYPE_LINE_NAME "line"
31 #define RRDSET_TYPE_AREA_NAME "area"
32 #define RRDSET_TYPE_STACKED_NAME "stacked"
33
34 RRDSET_TYPE rrdset_type_id(const char *name);
35 const char *rrdset_type_name(RRDSET_TYPE chart_type);
36
37
38 // ----------------------------------------------------------------------------
39 // memory mode
40
41 typedef enum rrd_memory_mode {
42     RRD_MEMORY_MODE_NONE = 0,
43     RRD_MEMORY_MODE_RAM  = 1,
44     RRD_MEMORY_MODE_MAP  = 2,
45     RRD_MEMORY_MODE_SAVE = 3
46 } RRD_MEMORY_MODE;
47
48 #define RRD_MEMORY_MODE_NONE_NAME "none"
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 extern RRD_MEMORY_MODE default_rrd_memory_mode;
54
55 extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
56 extern RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
57
58
59 // ----------------------------------------------------------------------------
60 // algorithms types
61
62 typedef enum rrd_algorithm {
63     RRD_ALGORITHM_ABSOLUTE              = 0,
64     RRD_ALGORITHM_INCREMENTAL           = 1,
65     RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
66     RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL  = 3
67 } RRD_ALGORITHM;
68
69 #define RRD_ALGORITHM_ABSOLUTE_NAME                "absolute"
70 #define RRD_ALGORITHM_INCREMENTAL_NAME             "incremental"
71 #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME   "percentage-of-incremental-row"
72 #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME    "percentage-of-absolute-row"
73
74 extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
75 extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
76
77 // ----------------------------------------------------------------------------
78 // RRD FAMILY
79
80 struct rrdfamily {
81     avl avl;
82
83     const char *family;
84     uint32_t hash_family;
85
86     size_t use_count;
87
88     avl_tree_lock variables_root_index;
89 };
90 typedef struct rrdfamily RRDFAMILY;
91
92
93 // ----------------------------------------------------------------------------
94 // flags
95 // use this for configuration flags, not for state control
96 // flags are set/unset in a manner that is not thread safe
97 // and may lead to missing information.
98
99 typedef enum rrddim_flags {
100     RRDDIM_FLAG_HIDDEN                          = 1 << 0,  // this dimension will not be offered to callers
101     RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = 1 << 1   // do not offer RESET or OVERFLOW info to callers
102 } RRDDIM_FLAGS;
103
104 #define rrddim_flag_check(rd, flag) ((rd)->flags & flag)
105 #define rrddim_flag_set(rd, flag)   (rd)->flags |= flag
106 #define rrddim_flag_clear(rd, flag) (rd)->flags &= ~flag
107
108
109 // ----------------------------------------------------------------------------
110 // RRD DIMENSION - this is a metric
111
112 struct rrddim {
113     // ------------------------------------------------------------------------
114     // binary indexing structures
115
116     avl avl;                                        // the binary index - this has to be first member!
117
118     // ------------------------------------------------------------------------
119     // the dimension definition
120
121     const char *id;                                 // the id of this dimension (for internal identification)
122     const char *name;                               // the name of this dimension (as presented to user)
123                                                     // this is a pointer to the config structure
124                                                     // since the config always has a higher priority
125                                                     // (the user overwrites the name of the charts)
126                                                     // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
127
128     RRD_ALGORITHM algorithm;                        // the algorithm that is applied to add new collected values
129     RRD_MEMORY_MODE rrd_memory_mode;                // the memory mode for this dimension
130
131     collected_number multiplier;                    // the multiplier of the collected values
132     collected_number divisor;                       // the divider of the collected values
133
134     uint32_t flags;                                 // configuration flags for the dimension
135
136     // ------------------------------------------------------------------------
137     // members for temporary data we need for calculations
138
139     uint32_t hash;                                  // a simple hash of the id, to speed up searching / indexing
140                                                     // instead of strcmp() every item in the binary index
141                                                     // we first compare the hashes
142
143     uint32_t hash_name;                             // a simple hash of the name
144
145     char *cache_filename;                           // the filename we load/save from/to this set
146
147     size_t collections_counter;                     // the number of times we added values to this rrdim
148     size_t unused[10];
149
150     int updated:1;                                  // 1 when the dimension has been updated since the last processing
151     int exposed:1;                                  // 1 when set what have sent this dimension to the central netdata
152
153     struct timeval last_collected_time;             // when was this dimension last updated
154                                                     // this is actual date time we updated the last_collected_value
155                                                     // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
156
157     calculated_number calculated_value;             // the current calculated value, after applying the algorithm - resets to zero after being used
158     calculated_number last_calculated_value;        // the last calculated value processed
159
160     calculated_number last_stored_value;            // the last value as stored in the database (after interpolation)
161
162     collected_number collected_value;               // the current value, as collected - resets to 0 after being used
163     collected_number last_collected_value;          // the last value that was collected, after being processed
164
165     // the *_volume members are used to calculate the accuracy of the rounding done by the
166     // storage number - they are printed to debug.log when debug is enabled for a set.
167     calculated_number collected_volume;             // the sum of all collected values so far
168     calculated_number stored_volume;                // the sum of all stored values so far
169
170     struct rrddim *next;                            // linking of dimensions within the same data set
171     struct rrdset *rrdset;
172
173     // ------------------------------------------------------------------------
174     // members for checking the data when loading from disk
175
176     long entries;                                   // how many entries this dimension has in ram
177                                                     // this is the same to the entries of the data set
178                                                     // we set it here, to check the data when we load it from disk.
179
180     int update_every;                               // every how many seconds is this updated
181
182     size_t memsize;                                 // the memory allocated for this dimension
183
184     char magic[sizeof(RRDDIMENSION_MAGIC) + 1];     // a string to be saved, used to identify our data file
185
186     struct rrddimvar *variables;
187
188     // ------------------------------------------------------------------------
189     // the values stored in this dimension, using our floating point numbers
190
191     storage_number values[];                        // the array of values - THIS HAS TO BE THE LAST MEMBER
192 };
193 typedef struct rrddim RRDDIM;
194
195 // ----------------------------------------------------------------------------
196 // these loop macros make sure the linked list is accessed with the right lock
197
198 #define rrddim_foreach_read(rd, st) \
199     for(rd = st->dimensions, rrdset_check_rdlock(st); rd ; rd = rd->next)
200
201 #define rrddim_foreach_write(rd, st) \
202     for(rd = st->dimensions, rrdset_check_wrlock(st); rd ; rd = rd->next)
203
204
205 // ----------------------------------------------------------------------------
206 // RRDSET - this is a chart
207
208 // use this for configuration flags, not for state control
209 // flags are set/unset in a manner that is not thread safe
210 // and may lead to missing information.
211
212 typedef enum rrdset_flags {
213     RRDSET_FLAG_ENABLED  = 1 << 0, // enables or disables a chart
214     RRDSET_FLAG_DETAIL   = 1 << 1, // if set, the data set should be considered as a detail of another
215                                    // (the master data set should be the one that has the same family and is not detail)
216     RRDSET_FLAG_DEBUG    = 1 << 2, // enables or disables debugging for a chart
217     RRDSET_FLAG_OBSOLETE = 1 << 3  // this is marked by the collector/module as obsolete
218 } RRDSET_FLAGS;
219
220 #define rrdset_flag_check(st, flag) ((st)->flags & flag)
221 #define rrdset_flag_set(st, flag)   (st)->flags |= flag
222 #define rrdset_flag_clear(st, flag) (st)->flags &= ~flag
223
224 struct rrdset {
225     // ------------------------------------------------------------------------
226     // binary indexing structures
227
228     avl avl;                                        // the index, with key the id - this has to be first!
229     avl avlname;                                    // the index, with key the name
230
231     // ------------------------------------------------------------------------
232     // the set configuration
233
234     char id[RRD_ID_LENGTH_MAX + 1];                 // id of the data set
235
236     const char *name;                               // the name of this dimension (as presented to user)
237                                                     // this is a pointer to the config structure
238                                                     // since the config always has a higher priority
239                                                     // (the user overwrites the name of the charts)
240
241     char *config_section;                           // the config section for the chart
242
243     char *type;                                     // the type of graph RRD_TYPE_* (a category, for determining graphing options)
244     char *family;                                   // grouping sets under the same family
245     char *title;                                    // title shown to user
246     char *units;                                    // units of measurement
247
248     char *context;                                  // the template of this data set
249     uint32_t hash_context;
250
251     RRDSET_TYPE chart_type;
252
253     int update_every;                               // every how many seconds is this updated?
254
255     long entries;                                   // total number of entries in the data set
256
257     long current_entry;                             // the entry that is currently being updated
258                                                     // it goes around in a round-robin fashion
259
260     uint32_t flags;                                 // configuration flags
261
262     int gap_when_lost_iterations_above;             // after how many lost iterations a gap should be stored
263                                                     // netdata will interpolate values for gaps lower than this
264
265     long priority;
266
267
268     // ------------------------------------------------------------------------
269     // members for temporary data we need for calculations
270
271     RRD_MEMORY_MODE rrd_memory_mode;                // if set to 1, this is memory mapped
272
273     char *cache_dir;                                // the directory to store dimensions
274     char cache_filename[FILENAME_MAX+1];            // the filename to store this set
275
276     netdata_rwlock_t rrdset_rwlock;                 // protects dimensions linked list
277
278     size_t counter;                                 // the number of times we added values to this database
279     size_t counter_done;                            // the number of times rrdset_done() has been called
280
281     time_t last_accessed_time;                      // the last time this RRDSET has been accessed
282     size_t unused[9];
283
284     uint32_t hash;                                  // a simple hash on the id, to speed up searching
285                                                     // we first compare hashes, and only if the hashes are equal we do string comparisons
286
287     uint32_t hash_name;                             // a simple hash on the name
288
289     usec_t usec_since_last_update;                  // the time in microseconds since the last collection of data
290
291     struct timeval last_updated;                    // when this data set was last updated (updated every time the rrd_stats_done() function)
292     struct timeval last_collected_time;             // when did this data set last collected values
293
294     total_number collected_total;                   // used internally to calculate percentages
295     total_number last_collected_total;              // used internally to calculate percentages
296
297     RRDFAMILY *rrdfamily;
298     struct rrdhost *rrdhost;
299
300     struct rrdset *next;                            // linking of rrdsets
301
302     // ------------------------------------------------------------------------
303     // local variables
304
305     calculated_number green;
306     calculated_number red;
307
308     avl_tree_lock variables_root_index;
309     RRDSETVAR *variables;
310     RRDCALC *alarms;
311
312     // ------------------------------------------------------------------------
313     // members for checking the data when loading from disk
314
315     unsigned long memsize;                          // how much mem we have allocated for this (without dimensions)
316
317     char magic[sizeof(RRDSET_MAGIC) + 1];           // our magic
318
319     // ------------------------------------------------------------------------
320     // the dimensions
321
322     avl_tree_lock dimensions_index;                 // the root of the dimensions index
323     RRDDIM *dimensions;                             // the actual data for every dimension
324
325 };
326 typedef struct rrdset RRDSET;
327
328 #define rrdset_rdlock(st) netdata_rwlock_rdlock(&((st)->rrdset_rwlock))
329 #define rrdset_wrlock(st) netdata_rwlock_wrlock(&((st)->rrdset_rwlock))
330 #define rrdset_unlock(st) netdata_rwlock_unlock(&((st)->rrdset_rwlock))
331
332
333 // ----------------------------------------------------------------------------
334 // these loop macros make sure the linked list is accessed with the right lock
335
336 #define rrdset_foreach_read(st, host) \
337     for(st = host->rrdset_root, rrdhost_check_rdlock(host); st ; st = st->next)
338
339 #define rrdset_foreach_write(st, host) \
340     for(st = host->rrdset_root, rrdhost_check_wrlock(host); st ; st = st->next)
341
342
343 // ----------------------------------------------------------------------------
344 // RRDHOST flags
345 // use this for configuration flags, not for state control
346 // flags are set/unset in a manner that is not thread safe
347 // and may lead to missing information.
348
349 typedef enum rrdhost_flags {
350     RRDHOST_ORPHAN                 = 1 << 0, // this host is orphan
351     RRDHOST_DELETE_OBSOLETE_FILES  = 1 << 1, // delete files of obsolete charts
352     RRDHOST_DELETE_ORPHAN_FILES    = 1 << 2  // delete the entire host when orphan
353 } RRDHOST_FLAGS;
354
355 #define rrdhost_flag_check(host, flag) ((host)->flags & flag)
356 #define rrdhost_flag_set(host, flag)   (host)->flags |= flag
357 #define rrdhost_flag_clear(host, flag) (host)->flags &= ~flag
358
359 // ----------------------------------------------------------------------------
360 // RRD HOST
361
362 struct rrdhost {
363     avl avl;                                        // the index of hosts
364
365     // ------------------------------------------------------------------------
366     // host information
367
368     char *hostname;                                 // the hostname of this host
369     uint32_t hash_hostname;                         // the hostname hash
370
371     char machine_guid[GUID_LEN + 1];                // the unique ID of this host
372     uint32_t hash_machine_guid;                     // the hash of the unique ID
373
374     char *os;                                       // the O/S type of the host
375
376     uint32_t flags;                                 // flags about this RRDHOST
377
378     int rrd_update_every;                           // the update frequency of the host
379     long rrd_history_entries;                       // the number of history entries for the host's charts
380     RRD_MEMORY_MODE rrd_memory_mode;                // the memory more for the charts of this host
381
382     char *cache_dir;                                // the directory to save RRD cache files
383     char *varlib_dir;                               // the directory to save health log
384
385
386     // ------------------------------------------------------------------------
387     // streaming of data to remote hosts - rrdpush
388
389     int rrdpush_enabled:1;                          // 1 when this host sends metrics to another netdata
390     char *rrdpush_destination;                      // where to send metrics to
391     char *rrdpush_api_key;                          // the api key at the receiving netdata
392     volatile int rrdpush_connected:1;               // 1 when the sender is ready to push metrics
393     volatile int rrdpush_spawn:1;                   // 1 when the sender thread has been spawn
394     volatile int rrdpush_error_shown:1;             // 1 when we have logged a communication error
395     int rrdpush_socket;                             // the fd of the socket to the remote host, or -1
396     pthread_t rrdpush_thread;                       // the sender thread
397     netdata_mutex_t rrdpush_mutex;                  // exclusive access to rrdpush_buffer
398     int rrdpush_pipe[2];                            // collector to sender thread communication
399     BUFFER *rrdpush_buffer;                         // collector fills it, sender sends them
400
401
402     // ------------------------------------------------------------------------
403     // streaming of data from remote hosts - rrdpush
404
405     volatile size_t connected_senders;              // when remote hosts are streaming to this
406                                                     // host, this is the counter of connected clients
407
408     time_t senders_disconnected_time;               // the time the last sender was disconnected
409
410     // ------------------------------------------------------------------------
411     // health monitoring options
412
413     int health_enabled:1;                           // 1 when this host has health enabled
414     time_t health_delay_up_to;                      // a timestamp to delay alarms processing up to
415     char *health_default_exec;                      // the full path of the alarms notifications program
416     char *health_default_recipient;                 // the default recipient for all alarms
417     char *health_log_filename;                      // the alarms event log filename
418     size_t health_log_entries_written;              // the number of alarm events writtern to the alarms event log
419     FILE *health_log_fp;                            // the FILE pointer to the open alarms event log file
420
421     // all RRDCALCs are primarily allocated and linked here
422     // RRDCALCs may be linked to charts at any point
423     // (charts may or may not exist when these are loaded)
424     RRDCALC *alarms;
425
426     ALARM_LOG health_log;                           // alarms historical events (event log)
427
428     // templates of alarms
429     // these are used to create alarms when charts
430     // are created or renamed, that match them
431     RRDCALCTEMPLATE *templates;
432
433
434     // ------------------------------------------------------------------------
435     // the charts of the host
436
437     RRDSET *rrdset_root;                            // the host charts
438
439
440     // ------------------------------------------------------------------------
441     // locks
442
443     netdata_rwlock_t rrdhost_rwlock;                // lock for this RRDHOST (protects rrdset_root linked list)
444
445     avl_tree_lock rrdset_root_index;                // the host's charts index (by id)
446     avl_tree_lock rrdset_root_index_name;           // the host's charts index (by name)
447
448     avl_tree_lock rrdfamily_root_index;             // the host's chart families index
449     avl_tree_lock variables_root_index;             // the host's chart variables index
450
451     struct rrdhost *next;
452 };
453 typedef struct rrdhost RRDHOST;
454 extern RRDHOST *localhost;
455
456 #define rrdhost_rdlock(host) netdata_rwlock_rdlock(&((host)->rrdhost_rwlock))
457 #define rrdhost_wrlock(host) netdata_rwlock_wrlock(&((host)->rrdhost_rwlock))
458 #define rrdhost_unlock(host) netdata_rwlock_unlock(&((host)->rrdhost_rwlock))
459
460 // ----------------------------------------------------------------------------
461 // these loop macros make sure the linked list is accessed with the right lock
462
463 #define rrdhost_foreach_read(var) \
464     for(var = localhost, rrd_check_rdlock(); var ; var = var->next)
465
466 #define rrdhost_foreach_write(var) \
467     for(var = localhost, rrd_check_wrlock(); var ; var = var->next)
468
469
470 // ----------------------------------------------------------------------------
471 // global lock for all RRDHOSTs
472
473 extern netdata_rwlock_t rrd_rwlock;
474
475 #define rrd_rdlock() netdata_rwlock_rdlock(&rrd_rwlock)
476 #define rrd_wrlock() netdata_rwlock_wrlock(&rrd_rwlock)
477 #define rrd_unlock() netdata_rwlock_unlock(&rrd_rwlock)
478
479 // ----------------------------------------------------------------------------
480
481 extern size_t rrd_hosts_available;
482 extern time_t rrdhost_free_orphan_time;
483
484 extern void rrd_init(char *hostname);
485
486 extern RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash);
487 extern RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash);
488
489 extern RRDHOST *rrdhost_find_or_create(
490         const char *hostname
491         , const char *guid
492         , const char *os
493         , int update_every
494         , long history
495         , RRD_MEMORY_MODE mode
496         , int health_enabled
497         , int rrdpush_enabled
498         , char *rrdpush_destination
499         , char *rrdpush_api_key
500 );
501
502 #if defined(NETDATA_INTERNAL_CHECKS) && defined(NETDATA_VERIFY_LOCKS)
503 extern void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line);
504 extern void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line);
505 extern void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line);
506 extern void __rrdset_check_wrlock(RRDSET *st, const char *file, const char *function, const unsigned long line);
507 extern void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line);
508 extern void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line);
509
510 #define rrdhost_check_rdlock(host) __rrdhost_check_rdlock(host, __FILE__, __FUNCTION__, __LINE__)
511 #define rrdhost_check_wrlock(host) __rrdhost_check_wrlock(host, __FILE__, __FUNCTION__, __LINE__)
512 #define rrdset_check_rdlock(st) __rrdset_check_rdlock(st, __FILE__, __FUNCTION__, __LINE__)
513 #define rrdset_check_wrlock(st) __rrdset_check_wrlock(st, __FILE__, __FUNCTION__, __LINE__)
514 #define rrd_check_rdlock() __rrd_check_rdlock(__FILE__, __FUNCTION__, __LINE__)
515 #define rrd_check_wrlock() __rrd_check_wrlock(__FILE__, __FUNCTION__, __LINE__)
516
517 #else
518 #define rrdhost_check_rdlock(host) (void)0
519 #define rrdhost_check_wrlock(host) (void)0
520 #define rrdset_check_rdlock(host) (void)0
521 #define rrdset_check_wrlock(host) (void)0
522 #define rrd_check_rdlock() (void)0
523 #define rrd_check_wrlock() (void)0
524 #endif
525
526 // ----------------------------------------------------------------------------
527 // RRDSET functions
528
529 extern void rrdset_set_name(RRDSET *st, const char *name);
530
531 extern RRDSET *rrdset_create(RRDHOST *host
532                              , const char *type
533                              , const char *id
534                              , const char *name
535                              , const char *family
536                              , const char *context
537                              , const char *title
538                              , const char *units
539                              , long priority
540                              , int update_every
541                              , RRDSET_TYPE chart_type);
542
543 #define rrdset_create_localhost(type, id, name, family, context, title, units, priority, update_every, chart_type) rrdset_create(localhost, type, id, name, family, context, title, units, priority, update_every, chart_type)
544
545 extern void rrdhost_free_all(void);
546 extern void rrdhost_save_all(void);
547
548 extern void rrdhost_cleanup_orphan(RRDHOST *protected);
549 extern void rrdhost_free(RRDHOST *host);
550 extern void rrdhost_save(RRDHOST *host);
551 extern void rrdhost_delete(RRDHOST *host);
552
553 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
554 #define rrdset_find_localhost(id) rrdset_find(localhost, id)
555
556 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
557 #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
558
559 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
560 #define rrdset_find_byname_localhost(name)  rrdset_find_byname(localhost, name)
561
562 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
563 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
564 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
565
566 extern void rrdset_done(RRDSET *st);
567
568 // checks if the RRDSET should be offered to viewers
569 #define rrdset_is_available_for_viewers(st) (rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && !rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && (st)->dimensions)
570
571 // get the total duration in seconds of the round robin database
572 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
573
574 // get the timestamp of the last entry in the round robin database
575 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
576
577 // get the timestamp of first entry in the round robin database
578 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
579
580 // get the last slot updated in the round robin database
581 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
582
583 // get the first / oldest slot updated in the round robin database
584 #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) ))
585
586 // get the slot of the round robin database, for the given timestamp (t)
587 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
588 #define rrdset_time2slot(st, t) ( \
589         (  (time_t)(t) >= rrdset_last_entry_t(st))  ? ( rrdset_last_slot(st) ) : \
590         ( ((time_t)(t) <= rrdset_first_entry_t(st)) ?   rrdset_first_slot(st) : \
591         ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
592           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
593           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
594         )))
595
596 // get the timestamp of a specific slot in the round robin database
597 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
598         ((unsigned long)(st)->update_every * ( \
599                 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
600                 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
601                 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
602         ))
603
604 // ----------------------------------------------------------------------------
605 // RRD DIMENSION functions
606
607 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm);
608
609 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
610 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
611
612 extern int rrddim_hide(RRDSET *st, const char *id);
613 extern int rrddim_unhide(RRDSET *st, const char *id);
614
615 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
616 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
617
618 extern long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries);
619
620
621 // ----------------------------------------------------------------------------
622 // RRD internal functions
623
624 #ifdef NETDATA_RRD_INTERNALS
625
626 extern avl_tree_lock rrdhost_root_index;
627
628 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
629 extern char *rrdset_cache_dir(RRDHOST *host, const char *id, const char *config_section);
630
631 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
632
633 extern int rrddim_compare(void* a, void* b);
634 extern int rrdset_compare(void* a, void* b);
635 extern int rrdset_compare_name(void* a, void* b);
636 extern int rrdfamily_compare(void *a, void *b);
637
638 extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
639 extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
640
641 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
642 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
643 extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
644
645 extern void rrdset_free(RRDSET *st);
646 extern void rrdset_reset(RRDSET *st);
647 extern void rrdset_save(RRDSET *st);
648 extern void rrdset_delete(RRDSET *st);
649
650 extern void rrdhost_cleanup_obsolete(RRDHOST *host);
651
652 #endif /* NETDATA_RRD_INTERNALS */
653
654
655 #endif /* NETDATA_RRD_H */