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) {
19 debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
22 strncpyz(tmp.machine_guid, guid, GUID_LEN);
23 tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
25 return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
28 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
29 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
32 // ----------------------------------------------------------------------------
33 // RRDHOST - internal helpers
35 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
36 freez(host->hostname);
37 host->hostname = strdupz(hostname);
38 host->hash_hostname = simple_hash(host->hostname);
41 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
42 strncpy(host->machine_guid, machine_guid, GUID_LEN);
43 host->machine_guid[GUID_LEN] = '\0';
44 host->hash_machine_guid = simple_hash(host->machine_guid);
47 // ----------------------------------------------------------------------------
48 // RRDHOST - add a host
50 RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
51 debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
53 RRDHOST *host = callocz(1, sizeof(RRDHOST));
55 pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
57 rrdhost_init_hostname(host, hostname);
58 rrdhost_init_machine_guid(host, guid);
60 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
61 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
62 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
63 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
65 host->health_log.next_log_id = 1;
66 host->health_log.next_alarm_id = 1;
67 host->health_log.max = 1000;
68 host->health_log.next_log_id =
69 host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
70 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
72 if(rrdhost_index_add(host) != host)
73 fatal("Cannot add host '%s' to index. It already exists.", hostname);
75 debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
79 RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
80 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
82 RRDHOST *host = rrdhost_find(guid, 0);
84 host = rrdhost_create(hostname, guid);
89 // ----------------------------------------------------------------------------
90 // RRDHOST global / startup initialization
92 RRDHOST *localhost = NULL;
94 void rrd_init(char *hostname) {
95 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
96 localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
99 // ----------------------------------------------------------------------------
102 void rrdhost_rwlock(RRDHOST *host) {
103 debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
104 pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
107 void rrdhost_rdlock(RRDHOST *host) {
108 debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
109 pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
112 void rrdhost_unlock(RRDHOST *host) {
113 debug(D_RRDHOST, "Unlock host '%s'", host->hostname);
114 pthread_rwlock_unlock(&host->rrdset_root_rwlock);
117 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
118 debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
120 int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
122 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);
125 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
126 debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
128 int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
130 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);
133 void rrdhost_free(RRDHOST *host) {
136 info("Freeing all memory for host '%s'...", host->hostname);
138 rrdhost_rwlock(host);
141 for(st = host->rrdset_root; st ;) {
142 RRDSET *next = st->next;
144 pthread_rwlock_wrlock(&st->rwlock);
147 rrdsetvar_free(st->variables);
150 rrdsetcalc_unlink(st->alarms);
152 while(st->dimensions)
153 rrddim_free(st, st->dimensions);
155 if(unlikely(rrdset_index_del(host, st) != st))
156 error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
158 rrdset_index_del_name(host, st);
160 st->rrdfamily->use_count--;
161 if(!st->rrdfamily->use_count)
162 rrdfamily_free(host, st->rrdfamily);
164 pthread_rwlock_unlock(&st->rwlock);
166 if(st->mapped == RRD_MEMORY_MODE_SAVE || st->mapped == RRD_MEMORY_MODE_MAP) {
167 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
168 munmap(st, st->memsize);
175 host->rrdset_root = NULL;
177 freez(host->hostname);
178 rrdhost_unlock(host);
181 info("Host memory cleanup completed...");
184 void rrdhost_save(RRDHOST *host) {
187 info("Saving host '%s' database...", host->hostname);
192 // we get an write lock
193 // to ensure only one thread is saving the database
194 rrdhost_rwlock(host);
196 for(st = host->rrdset_root; st ; st = st->next) {
197 pthread_rwlock_rdlock(&st->rwlock);
199 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
200 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
201 savememory(st->cache_filename, st, st->memsize);
204 for(rd = st->dimensions; rd ; rd = rd->next) {
205 if(likely(rd->memory_mode == RRD_MEMORY_MODE_SAVE)) {
206 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
207 savememory(rd->cache_filename, rd, rd->memsize);
211 pthread_rwlock_unlock(&st->rwlock);
214 rrdhost_unlock(host);
217 void rrdhost_free_all(void) {
218 RRDHOST *host = localhost;
220 // FIXME: lock all hosts
223 RRDHOST *next = host = host->next;
230 // FIXME: unlock all hosts
233 void rrdhost_save_all(void) {
235 for(host = localhost; host ; host = host->next)