2 * util/shm_side/shm_main.c - SHM for statistics transport
4 * Copyright (c) 2017, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions for the SHM implementation.
54 #include "daemon/daemon.h"
55 #include "daemon/worker.h"
56 #include "daemon/stats.h"
57 #include "services/mesh.h"
58 #include "services/cache/rrset.h"
59 #include "services/cache/infra.h"
60 #include "validator/validator.h"
61 #include "util/config_file.h"
62 #include "util/fptr_wlist.h"
66 /** subtract timers and the values do not overflow or become negative */
68 stat_timeval_subtract(long long *d_sec, long long *d_usec, const struct timeval* end,
69 const struct timeval* start)
72 time_t end_usec = end->tv_usec;
73 *d_sec = end->tv_sec - start->tv_sec;
74 if(end_usec < start->tv_usec) {
78 *d_usec = end_usec - start->tv_usec;
81 #endif /* HAVE_SHMGET */
83 int shm_main_init(struct daemon* daemon)
86 struct ub_shm_stat_info *shm_stat;
92 if(!daemon->cfg->shm_enable)
94 if(daemon->cfg->stat_interval == 0)
95 log_warn("shm-enable is yes but statistics-interval is 0");
97 /* Statistics to maintain the number of thread + total */
98 shm_size = (sizeof(struct ub_stats_info) * (daemon->num + 1));
100 /* Allocation of needed memory */
101 daemon->shm_info = (struct shm_main_info*)calloc(1, shm_size);
104 if(!daemon->shm_info) {
105 log_err("shm fail: malloc failure");
109 daemon->shm_info->key = daemon->cfg->shm_key;
111 /* Check for previous create SHM */
112 daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(int), SHM_R);
113 daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, sizeof(int), SHM_R);
115 /* Destroy previous SHM */
116 if (daemon->shm_info->id_ctl >= 0)
117 shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
119 /* Destroy previous SHM */
120 if (daemon->shm_info->id_arr >= 0)
121 shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
123 /* SHM: Create the segment */
124 daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(struct ub_shm_stat_info), IPC_CREAT | 0644);
126 if (daemon->shm_info->id_ctl < 0)
128 log_err("SHM failed(id_ctl) cannot shmget(key %d) %s",
129 daemon->shm_info->key, strerror(errno));
131 /* Just release memory unused */
132 free(daemon->shm_info);
137 daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0644);
139 if (daemon->shm_info->id_arr < 0)
141 log_err("SHM failed(id_arr) cannot shmget(key %d + 1) %s",
142 daemon->shm_info->key, strerror(errno));
144 /* Just release memory unused */
145 free(daemon->shm_info);
150 /* SHM: attach the segment */
151 daemon->shm_info->ptr_ctl = (struct ub_shm_stat_info*)
152 shmat(daemon->shm_info->id_ctl, NULL, 0);
153 if(daemon->shm_info->ptr_ctl == (void *) -1) {
154 log_err("SHM failed(ctl) cannot shmat(%d) %s",
155 daemon->shm_info->id_ctl, strerror(errno));
157 /* Just release memory unused */
158 free(daemon->shm_info);
163 daemon->shm_info->ptr_arr = (struct ub_stats_info*)
164 shmat(daemon->shm_info->id_arr, NULL, 0);
166 if (daemon->shm_info->ptr_arr == (void *) -1)
168 log_err("SHM failed(arr) cannot shmat(%d) %s",
169 daemon->shm_info->id_arr, strerror(errno));
171 /* Just release memory unused */
172 free(daemon->shm_info);
177 /* Zero fill SHM to stand clean while is not filled by other events */
178 memset(daemon->shm_info->ptr_ctl, 0, sizeof(struct ub_shm_stat_info));
179 memset(daemon->shm_info->ptr_arr, 0, shm_size);
181 shm_stat = daemon->shm_info->ptr_ctl;
182 shm_stat->num_threads = daemon->num;
186 #endif /* HAVE_SHMGET */
190 void shm_main_shutdown(struct daemon* daemon)
193 /* web are OK, just disabled */
194 if(!daemon->cfg->shm_enable)
197 verbose(VERB_DETAIL, "SHM shutdown - KEY [%d] - ID CTL [%d] ARR [%d] - PTR CTL [%p] ARR [%p]",
198 daemon->shm_info->key, daemon->shm_info->id_ctl, daemon->shm_info->id_arr, daemon->shm_info->ptr_ctl, daemon->shm_info->ptr_arr);
200 /* Destroy previous SHM */
201 if (daemon->shm_info->id_ctl >= 0)
202 shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
204 if (daemon->shm_info->id_arr >= 0)
205 shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
207 if (daemon->shm_info->ptr_ctl)
208 shmdt(daemon->shm_info->ptr_ctl);
210 if (daemon->shm_info->ptr_arr)
211 shmdt(daemon->shm_info->ptr_arr);
215 #endif /* HAVE_SHMGET */
218 void shm_main_run(struct worker *worker)
221 struct ub_shm_stat_info *shm_stat;
222 struct ub_stats_info *stat_total;
223 struct ub_stats_info *stat_info;
227 verbose(VERB_DETAIL, "SHM run - worker [%d] - daemon [%p] - timenow(%u) - timeboot(%u)",
228 worker->thread_num, worker->daemon, (unsigned)worker->env.now_tv->tv_sec, (unsigned)worker->daemon->time_boot.tv_sec);
231 offset = worker->thread_num + 1;
232 stat_total = worker->daemon->shm_info->ptr_arr;
233 stat_info = worker->daemon->shm_info->ptr_arr + offset;
235 /* Copy data to the current position */
236 server_stats_compile(worker, stat_info, 0);
238 /* First thread, zero fill total, and copy general info */
239 if (worker->thread_num == 0) {
241 /* Copy data to the current position */
242 memset(stat_total, 0, sizeof(struct ub_stats_info));
244 /* Point to data into SHM */
246 shm_stat = worker->daemon->shm_info->ptr_ctl;
247 shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec;
248 shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec;
251 stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot);
252 stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat);
254 shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache);
255 shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table);
256 shm_stat->mem.dnscrypt_shared_secret = 0;
258 if(worker->daemon->dnscenv) {
259 shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem(
260 worker->daemon->dnscenv->shared_secrets_cache);
261 shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem(
262 worker->daemon->dnscenv->nonces_cache);
265 shm_stat->mem.val = (long long)mod_get_mem(&worker->env,
267 shm_stat->mem.iter = (long long)mod_get_mem(&worker->env,
269 shm_stat->mem.respip = (long long)mod_get_mem(&worker->env,
272 /* subnet mem value is available in shm, also when not enabled,
273 * to make the struct easier to memmap by other applications,
274 * independent of the configuration of unbound */
275 shm_stat->mem.subnet = 0;
277 shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env,
280 /* ipsecmod mem value is available in shm, also when not enabled,
281 * to make the struct easier to memmap by other applications,
282 * independent of the configuration of unbound */
283 shm_stat->mem.ipsecmod = 0;
285 shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env,
288 #ifdef WITH_DYNLIBMODULE
289 shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env,
294 server_stats_add(stat_total, stat_info);
296 /* print the thread statistics */
297 stat_total->mesh_time_median /= (double)worker->daemon->num;
301 #endif /* HAVE_SHMGET */