]> arthur.barton.de Git - netdata.git/blob - src/rrdhost.c
localhost is now dynamic
[netdata.git] / src / rrdhost.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 // ----------------------------------------------------------------------------
5 // RRDHOST index
6
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);
11 }
12
13 avl_tree_lock rrdhost_root_index = {
14         .avl_tree = { NULL, rrdhost_compare },
15         .rwlock = AVL_LOCK_INITIALIZER
16 };
17
18 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
19     RRDHOST tmp;
20     strncpyz(tmp.machine_guid, guid, GUID_LEN);
21     tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
22
23     return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
24 }
25
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))
28
29
30 // ----------------------------------------------------------------------------
31 // RRDHOST - internal helpers
32
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);
37 }
38
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);
43 }
44
45 // ----------------------------------------------------------------------------
46 // RRDHOST - add a host
47
48 RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
49     RRDHOST *host = callocz(1, sizeof(RRDHOST));
50
51     pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
52
53     rrdhost_init_hostname(host, hostname);
54     rrdhost_init_machine_guid(host, guid);
55
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);
60
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);
67
68     if(rrdhost_index_add(host) != host)
69         fatal("Cannot add host '%s' to index. It already exists.", hostname);
70
71     debug(D_RRDHOST, "Added host '%s'", host->hostname);
72     return host;
73 }
74
75 // ----------------------------------------------------------------------------
76 // RRDHOST global / startup initialization
77
78 RRDHOST *localhost = NULL;
79
80 void rrd_init(char *hostname) {
81     localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
82 }
83
84 // ----------------------------------------------------------------------------
85 // RRDHOST - locks
86
87 void rrdhost_rwlock(RRDHOST *host) {
88     pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
89 }
90
91 void rrdhost_rdlock(RRDHOST *host) {
92     pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
93 }
94
95 void rrdhost_unlock(RRDHOST *host) {
96     pthread_rwlock_unlock(&host->rrdset_root_rwlock);
97 }
98
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);
101
102     if(ret == 0)
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);
104 }
105
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);
108
109     if(ret == 0)
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);
111 }
112
113 void rrdhost_free(RRDHOST *host) {
114     if(!host) return;
115
116     info("Freeing all memory for host '%s'...", host->hostname);
117
118     rrdhost_rwlock(host);
119
120     RRDSET *st;
121     for(st = host->rrdset_root; st ;) {
122         RRDSET *next = st->next;
123
124         pthread_rwlock_wrlock(&st->rwlock);
125
126         while(st->variables)
127             rrdsetvar_free(st->variables);
128
129         while(st->alarms)
130             rrdsetcalc_unlink(st->alarms);
131
132         while(st->dimensions)
133             rrddim_free(st, st->dimensions);
134
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);
137
138         rrdset_index_del_name(host, st);
139
140         st->rrdfamily->use_count--;
141         if(!st->rrdfamily->use_count)
142             rrdfamily_free(host, st->rrdfamily);
143
144         pthread_rwlock_unlock(&st->rwlock);
145
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);
149         }
150         else
151             freez(st);
152
153         st = next;
154     }
155     host->rrdset_root = NULL;
156
157     freez(host->hostname);
158     rrdhost_unlock(host);
159     freez(host);
160
161     info("Host memory cleanup completed...");
162 }
163
164 void rrdhost_save(RRDHOST *host) {
165     if(!host) return;
166
167     info("Saving host '%s' database...", host->hostname);
168
169     RRDSET *st;
170     RRDDIM *rd;
171
172     // we get an write lock
173     // to ensure only one thread is saving the database
174     rrdhost_rwlock(host);
175
176     for(st = host->rrdset_root; st ; st = st->next) {
177         pthread_rwlock_rdlock(&st->rwlock);
178
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);
182         }
183
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);
188             }
189         }
190
191         pthread_rwlock_unlock(&st->rwlock);
192     }
193
194     rrdhost_unlock(host);
195 }
196
197 void rrdhost_free_all(void) {
198     RRDHOST *host = localhost;
199
200     // FIXME: lock all hosts
201
202     while(host) {
203         RRDHOST *next = host = host->next;
204         rrdhost_free(host);
205         host = next;
206     }
207
208     localhost = NULL;
209
210     // FIXME: unlock all hosts
211 }
212
213 void rrdhost_save_all(void) {
214     RRDHOST *host;
215     for(host = localhost; host ; host = host->next)
216         rrdhost_save(host);
217 }