1 #define NETDATA_RRD_INTERNALS 1
4 // ----------------------------------------------------------------------------
7 int rrdhost_compare(void* a, void* b) {
8 if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
9 else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
10 else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
13 avl_tree_lock rrdhost_root_index = {
14 .avl_tree = { NULL, rrdhost_compare },
15 .rwlock = AVL_LOCK_INITIALIZER
18 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
20 strncpyz(tmp.machine_guid, guid, GUID_LEN);
21 tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
23 return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
26 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
27 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
30 // ----------------------------------------------------------------------------
31 // RRDHOST - internal helpers
33 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
34 freez(host->hostname);
35 host->hostname = strdupz(hostname);
36 host->hash_hostname = simple_hash(host->hostname);
39 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
40 strncpy(host->machine_guid, machine_guid, GUID_LEN);
41 host->machine_guid[GUID_LEN] = '\0';
42 host->hash_machine_guid = simple_hash(host->machine_guid);
45 // ----------------------------------------------------------------------------
46 // RRDHOST - add a host
48 RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
49 RRDHOST *host = callocz(1, sizeof(RRDHOST));
51 pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
53 rrdhost_init_hostname(host, hostname);
54 rrdhost_init_machine_guid(host, guid);
56 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
57 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
58 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
59 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
61 host->health_log.next_log_id = 1;
62 host->health_log.next_alarm_id = 1;
63 host->health_log.max = 1000;
64 host->health_log.next_log_id =
65 host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
66 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
68 if(rrdhost_index_add(host) != host)
69 fatal("Cannot add host '%s' to index. It already exists.", hostname);
71 debug(D_RRDHOST, "Added host '%s'", host->hostname);
75 // ----------------------------------------------------------------------------
76 // RRDHOST global / startup initialization
78 RRDHOST *localhost = NULL;
80 void rrd_init(char *hostname) {
81 localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
84 // ----------------------------------------------------------------------------
87 void rrdhost_rwlock(RRDHOST *host) {
88 pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
91 void rrdhost_rdlock(RRDHOST *host) {
92 pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
95 void rrdhost_unlock(RRDHOST *host) {
96 pthread_rwlock_unlock(&host->rrdset_root_rwlock);
99 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
100 int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
103 fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
106 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
107 int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
110 fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
113 void rrdhost_free(RRDHOST *host) {
116 info("Freeing all memory for host '%s'...", host->hostname);
118 rrdhost_rwlock(host);
121 for(st = host->rrdset_root; st ;) {
122 RRDSET *next = st->next;
124 pthread_rwlock_wrlock(&st->rwlock);
127 rrdsetvar_free(st->variables);
130 rrdsetcalc_unlink(st->alarms);
132 while(st->dimensions)
133 rrddim_free(st, st->dimensions);
135 if(unlikely(rrdset_index_del(host, st) != st))
136 error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
138 rrdset_index_del_name(host, st);
140 st->rrdfamily->use_count--;
141 if(!st->rrdfamily->use_count)
142 rrdfamily_free(host, st->rrdfamily);
144 pthread_rwlock_unlock(&st->rwlock);
146 if(st->mapped == RRD_MEMORY_MODE_SAVE || st->mapped == RRD_MEMORY_MODE_MAP) {
147 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
148 munmap(st, st->memsize);
155 host->rrdset_root = NULL;
157 freez(host->hostname);
158 rrdhost_unlock(host);
161 info("Host memory cleanup completed...");
164 void rrdhost_save(RRDHOST *host) {
167 info("Saving host '%s' database...", host->hostname);
172 // we get an write lock
173 // to ensure only one thread is saving the database
174 rrdhost_rwlock(host);
176 for(st = host->rrdset_root; st ; st = st->next) {
177 pthread_rwlock_rdlock(&st->rwlock);
179 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
180 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
181 savememory(st->cache_filename, st, st->memsize);
184 for(rd = st->dimensions; rd ; rd = rd->next) {
185 if(likely(rd->memory_mode == RRD_MEMORY_MODE_SAVE)) {
186 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
187 savememory(rd->cache_filename, rd, rd->memsize);
191 pthread_rwlock_unlock(&st->rwlock);
194 rrdhost_unlock(host);
197 void rrdhost_free_all(void) {
198 RRDHOST *host = localhost;
200 // FIXME: lock all hosts
203 RRDHOST *next = host = host->next;
210 // FIXME: unlock all hosts
213 void rrdhost_save_all(void) {
215 for(host = localhost; host ; host = host->next)