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->id, 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->rrdhost->rrd_memory_mode != RRD_MEMORY_MODE_RAM) {
75 rd = (RRDDIM *)mymmap(fullfilename, size, ((st->rrdhost->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1);
77 // we have a file mapped for rd
81 rd->cache_filename = NULL;
82 rd->memory_mode = st->rrdhost->rrd_memory_mode;
83 rd->flags = 0x00000000;
87 memset(&rd->avl, 0, sizeof(avl));
90 now_realtime_timeval(&now);
92 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
94 info("Initializing file %s.", fullfilename);
97 else if(rd->memsize != size) {
99 error("File %s does not have the desired size. Clearing it.", fullfilename);
102 else if(rd->multiplier != multiplier) {
104 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
107 else if(rd->divisor != divisor) {
109 error("File %s does not have the same divisor. Clearing it.", fullfilename);
112 else if(rd->update_every != st->update_every) {
114 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
117 else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
119 error("File %s is too old. Clearing it.", fullfilename);
123 if(rd->algorithm && rd->algorithm != algorithm)
124 error("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong."
125 , fullfilename, algorithm, rrd_algorithm_name(algorithm), rd->algorithm,
126 rrd_algorithm_name(rd->algorithm));
131 // if we didn't manage to get a mmap'd dimension, just create one
132 rd = callocz(1, size);
133 rd->memory_mode = RRD_MEMORY_MODE_RAM;
138 strcpy(rd->magic, RRDDIMENSION_MAGIC);
140 rd->id = strdupz(id);
141 rd->hash = simple_hash(rd->id);
143 rd->cache_filename = strdupz(fullfilename);
145 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
146 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
147 rd->hash_name = simple_hash(rd->name);
149 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
150 rd->algorithm = rrd_algorithm_id(config_get(st->id, varname, rrd_algorithm_name(algorithm)));
152 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
153 rd->multiplier = config_get_number(st->id, varname, multiplier);
155 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
156 rd->divisor = config_get_number(st->id, varname, divisor);
157 if(!rd->divisor) rd->divisor = 1;
159 rd->entries = st->entries;
160 rd->update_every = st->update_every;
162 // prevent incremental calculation spikes
164 rrddim_flag_clear(rd, RRDDIM_FLAG_UPDATED);
165 rd->calculated_value = 0;
166 rd->last_calculated_value = 0;
167 rd->collected_value = 0;
168 rd->last_collected_value = 0;
169 rd->collected_volume = 0;
170 rd->stored_volume = 0;
171 rd->last_stored_value = 0;
172 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
173 rd->last_collected_time.tv_sec = 0;
174 rd->last_collected_time.tv_usec = 0;
177 // append this dimension
178 pthread_rwlock_wrlock(&st->rwlock);
182 RRDDIM *td = st->dimensions;
183 for(; td->next; td = td->next) ;
187 if(st->rrdhost->health_enabled) {
188 rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
189 rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
190 rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
193 pthread_rwlock_unlock(&st->rwlock);
195 if(unlikely(rrddim_index_add(st, rd) != rd))
196 error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
202 // ----------------------------------------------------------------------------
203 // RRDDIM remove / free a dimension
205 void rrddim_free(RRDSET *st, RRDDIM *rd)
207 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
209 if(rd == st->dimensions)
210 st->dimensions = rd->next;
213 for (i = st->dimensions; i && i->next != rd; i = i->next) ;
215 if (i && i->next == rd)
218 error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
223 rrddimvar_free(rd->variables);
225 if(unlikely(rrddim_index_del(st, rd) != rd))
226 error("RRDDIM: INTERNAL ERROR: attempt to remove from index dimension '%s' on chart '%s', removed a different dimension.", rd->id, st->id);
228 // free(rd->annotations);
230 switch(rd->memory_mode) {
231 case RRD_MEMORY_MODE_SAVE:
232 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
233 savememory(rd->cache_filename, rd, rd->memsize);
234 // continue to map mode - no break;
236 case RRD_MEMORY_MODE_MAP:
237 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
238 freez((void *)rd->id);
239 freez(rd->cache_filename);
240 munmap(rd, rd->memsize);
243 case RRD_MEMORY_MODE_RAM:
244 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
245 freez((void *)rd->id);
246 freez(rd->cache_filename);
253 // ----------------------------------------------------------------------------
254 // RRDDIM - set dimension options
256 int rrddim_hide(RRDSET *st, const char *id) {
257 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
259 RRDDIM *rd = rrddim_find(st, id);
261 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
265 rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
269 int rrddim_unhide(RRDSET *st, const char *id) {
270 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
272 RRDDIM *rd = rrddim_find(st, id);
274 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
278 rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
283 // ----------------------------------------------------------------------------
284 // RRDDIM - collect values for a dimension
286 inline collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value) {
287 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
289 now_realtime_timeval(&rd->last_collected_time);
290 rd->collected_value = value;
291 rrddim_flag_set(rd, RRDDIM_FLAG_UPDATED);
294 // 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));
296 return rd->last_collected_value;
299 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
300 RRDDIM *rd = rrddim_find(st, id);
302 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
306 return rrddim_set_by_pointer(st, rd, value);