1 #define NETDATA_RRD_INTERNALS 1
4 // ----------------------------------------------------------------------------
7 int rrddim_compare(void* a, void* b) {
8 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
9 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
10 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
13 #define rrddim_index_add(st, rd) (RRDDIM *)avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
14 #define rrddim_index_del(st,rd ) (RRDDIM *)avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
16 static inline RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
19 .hash = (hash)?hash:simple_hash(id)
21 return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
25 // ----------------------------------------------------------------------------
26 // RRDDIM - find a dimension
28 inline RRDDIM *rrddim_find(RRDSET *st, const char *id) {
29 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
31 return rrddim_index_find(st, id, 0);
35 // ----------------------------------------------------------------------------
36 // RRDDIM rename a dimension
38 inline void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name) {
39 if(unlikely(!strcmp(rd->name, name)))
42 debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
44 char varname[CONFIG_MAX_NAME + 1];
45 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
46 rd->name = config_set_default(st->config_section, varname, name);
47 rd->hash_name = simple_hash(rd->name);
49 rrddimvar_rename_all(rd);
53 // ----------------------------------------------------------------------------
54 // RRDDIM create a dimension
56 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm) {
57 RRDDIM *rd = rrddim_find(st, id);
59 debug(D_RRD_CALLS, "Cannot create rrd dimension '%s/%s', it already exists.", st->id, name?name:"<NONAME>");
63 char filename[FILENAME_MAX + 1];
64 char fullfilename[FILENAME_MAX + 1];
66 char varname[CONFIG_MAX_NAME + 1];
67 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
69 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
71 rrdset_strncpyz_name(filename, id, FILENAME_MAX);
72 snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
74 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
75 rd = (RRDDIM *)mymmap(fullfilename, size, ((st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1);
77 // we have a file mapped for rd
79 memset(&rd->avl, 0, sizeof(avl));
82 rd->cache_filename = NULL;
88 now_realtime_timeval(&now);
90 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
92 info("Initializing file %s.", fullfilename);
95 else if(rd->memsize != size) {
97 error("File %s does not have the desired size. Clearing it.", fullfilename);
100 else if(rd->multiplier != multiplier) {
102 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
105 else if(rd->divisor != divisor) {
107 error("File %s does not have the same divisor. Clearing it.", fullfilename);
110 else if(rd->update_every != st->update_every) {
112 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
115 else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
117 error("File %s is too old. Clearing it.", fullfilename);
121 if(rd->algorithm && rd->algorithm != algorithm)
122 error("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong."
123 , fullfilename, algorithm, rrd_algorithm_name(algorithm), rd->algorithm,
124 rrd_algorithm_name(rd->algorithm));
126 // make sure we have the right memory mode
127 // even if we cleared the memory
128 rd->rrd_memory_mode = st->rrd_memory_mode;
133 // if we didn't manage to get a mmap'd dimension, just create one
134 rd = callocz(1, size);
135 rd->rrd_memory_mode = (st->rrd_memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_RAM;
140 strcpy(rd->magic, RRDDIMENSION_MAGIC);
142 rd->id = strdupz(id);
143 rd->hash = simple_hash(rd->id);
145 rd->cache_filename = strdupz(fullfilename);
147 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
148 rd->name = config_get(st->config_section, varname, (name && *name)?name:rd->id);
149 rd->hash_name = simple_hash(rd->name);
151 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
152 rd->algorithm = rrd_algorithm_id(config_get(st->config_section, varname, rrd_algorithm_name(algorithm)));
154 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
155 rd->multiplier = config_get_number(st->config_section, varname, multiplier);
157 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
158 rd->divisor = config_get_number(st->config_section, varname, divisor);
159 if(!rd->divisor) rd->divisor = 1;
161 rd->entries = st->entries;
162 rd->update_every = st->update_every;
164 // prevent incremental calculation spikes
165 rd->collections_counter = 0;
167 rd->flags = 0x00000000;
169 rd->calculated_value = 0;
170 rd->last_calculated_value = 0;
171 rd->collected_value = 0;
172 rd->last_collected_value = 0;
173 rd->collected_volume = 0;
174 rd->stored_volume = 0;
175 rd->last_stored_value = 0;
176 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
177 rd->last_collected_time.tv_sec = 0;
178 rd->last_collected_time.tv_usec = 0;
181 // append this dimension
186 RRDDIM *td = st->dimensions;
187 for(; td->next; td = td->next) ;
191 if(st->rrdhost->health_enabled) {
192 rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
193 rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
194 rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
199 if(unlikely(rrddim_index_add(st, rd) != rd))
200 error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
206 // ----------------------------------------------------------------------------
207 // RRDDIM remove / free a dimension
209 void rrddim_free(RRDSET *st, RRDDIM *rd)
211 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
213 if(rd == st->dimensions)
214 st->dimensions = rd->next;
217 for (i = st->dimensions; i && i->next != rd; i = i->next) ;
219 if (i && i->next == rd)
222 error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
227 rrddimvar_free(rd->variables);
229 if(unlikely(rrddim_index_del(st, rd) != rd))
230 error("RRDDIM: INTERNAL ERROR: attempt to remove from index dimension '%s' on chart '%s', removed a different dimension.", rd->id, st->id);
232 // free(rd->annotations);
234 switch(rd->rrd_memory_mode) {
235 case RRD_MEMORY_MODE_SAVE:
236 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
237 savememory(rd->cache_filename, rd, rd->memsize);
238 // continue to map mode - no break;
240 case RRD_MEMORY_MODE_MAP:
241 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
242 freez((void *)rd->id);
243 freez(rd->cache_filename);
244 munmap(rd, rd->memsize);
247 case RRD_MEMORY_MODE_NONE:
248 case RRD_MEMORY_MODE_RAM:
249 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
250 freez((void *)rd->id);
251 freez(rd->cache_filename);
258 // ----------------------------------------------------------------------------
259 // RRDDIM - set dimension options
261 int rrddim_hide(RRDSET *st, const char *id) {
262 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
264 RRDDIM *rd = rrddim_find(st, id);
266 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
270 rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
274 int rrddim_unhide(RRDSET *st, const char *id) {
275 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
277 RRDDIM *rd = rrddim_find(st, id);
279 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
283 rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
288 // ----------------------------------------------------------------------------
289 // RRDDIM - collect values for a dimension
291 inline collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value) {
292 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
294 now_realtime_timeval(&rd->last_collected_time);
295 rd->collected_value = value;
298 rd->collections_counter++;
300 // fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " CALCULATED_NUMBER_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (calculated_number)((value - rd->last_collected_value) * (calculated_number)rd->multiplier / (calculated_number)rd->divisor * 1000000.0 / (calculated_number)st->usec_since_last_update));
302 return rd->last_collected_value;
305 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
306 RRDDIM *rd = rrddim_find(st, id);
308 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
312 return rrddim_set_by_pointer(st, rd, value);