From 1f24a5b7af0ac8d7f8a221b3ce4a0b3b9bb6264b Mon Sep 17 00:00:00 2001 From: "W.C.A. Wijngaards" Date: Fri, 20 Feb 2026 11:53:55 +0100 Subject: [PATCH] - stats-shm-volley, stat totals in separate struct. The first thread zeroes it, and the last thread copies it. --- util/shm_side/shm_main.c | 187 ++++++++++++++++++++++++++------------- util/shm_side/shm_main.h | 13 +++ 2 files changed, 139 insertions(+), 61 deletions(-) diff --git a/util/shm_side/shm_main.c b/util/shm_side/shm_main.c index fa6d45b80..496093f22 100644 --- a/util/shm_side/shm_main.c +++ b/util/shm_side/shm_main.c @@ -185,6 +185,16 @@ int shm_main_init(struct daemon* daemon) shm_stat = daemon->shm_info->ptr_ctl; shm_stat->num_threads = daemon->num; + lock_basic_init(&daemon->shm_info->lock); + daemon->shm_info->volley_in_progress = 0; + daemon->shm_info->thread_volley = (int*)calloc( + daemon->num, sizeof(int)); + if(!daemon->shm_info->thread_volley) { + log_err("shm fail: malloc failure"); + free(daemon->shm_info); + daemon->shm_info = NULL; + return 0; + } #else (void)daemon; #endif /* HAVE_SHMGET */ @@ -214,6 +224,9 @@ void shm_main_shutdown(struct daemon* daemon) if (daemon->shm_info->ptr_arr) shmdt(daemon->shm_info->ptr_arr); + lock_basic_destroy(&daemon->shm_info->lock); + free(daemon->shm_info->thread_volley); + free(daemon->shm_info); daemon->shm_info = NULL; #else @@ -221,14 +234,98 @@ void shm_main_shutdown(struct daemon* daemon) #endif /* HAVE_SHMGET */ } +/** Copy general info into the stat structure. */ +static void +shm_general_info(struct worker* worker) +{ + struct ub_shm_stat_info *shm_stat; + /* Point to data into SHM */ +#ifndef S_SPLINT_S + shm_stat = worker->daemon->shm_info->ptr_ctl; + shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec; + shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec; +#endif + + stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot); + stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat); + + shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache); + shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table); + shm_stat->mem.dnscrypt_shared_secret = 0; +#ifdef USE_DNSCRYPT + if(worker->daemon->dnscenv) { + shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem( + worker->daemon->dnscenv->shared_secrets_cache); + shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem( + worker->daemon->dnscenv->nonces_cache); + } +#endif + shm_stat->mem.val = (long long)mod_get_mem(&worker->env, "validator"); + shm_stat->mem.iter = (long long)mod_get_mem(&worker->env, "iterator"); + shm_stat->mem.respip = (long long)mod_get_mem(&worker->env, "respip"); + + /* subnet mem value is available in shm, also when not enabled, + * to make the struct easier to memmap by other applications, + * independent of the configuration of unbound */ + shm_stat->mem.subnet = 0; +#ifdef CLIENT_SUBNET + shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env, + "subnetcache"); +#endif + /* ipsecmod mem value is available in shm, also when not enabled, + * to make the struct easier to memmap by other applications, + * independent of the configuration of unbound */ + shm_stat->mem.ipsecmod = 0; +#ifdef USE_IPSECMOD + shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env, + "ipsecmod"); +#endif +#ifdef WITH_DYNLIBMODULE + shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env, "dynlib"); +#endif +} + +/** See if the thread is first. Caller has lock. */ +static int +shm_thread_is_first(struct shm_main_info* shm_info, int thread_num) +{ + /* The usual method, all thread executed last time, and there + * is no statistics in progress. */ + if(!shm_info->volley_in_progress) + return 1; + /* See if we are already active, if so, the timer fired twice + * for this thread during the statistics in progress. + * Another thread is not active during the statistics process, + * so this thread must be the first of this new round without that + * other thread. */ + if(shm_info->thread_volley[thread_num] != 0) { + return 1; + } + return 0; +} + +/** See if the thread is last. Caller has lock. */ +static int +shm_thread_is_last(struct daemon* daemon) +{ + /* This means that all threads have been active and this thread + * is the last one. All the thread_volley values are true then. */ + int i; + for(i=0; inum; i++) { + if(!daemon->shm_info->thread_volley[i]) + return 0; + } + return 1; +} + void shm_main_run(struct worker *worker) { #ifdef HAVE_SHMGET - struct ub_shm_stat_info *shm_stat; struct ub_stats_info *stat_total; struct ub_stats_info *stat_info; int offset; double total_mesh_time_median; + struct shm_main_info* shm_info = worker->daemon->shm_info; #ifndef S_SPLINT_S verbose(VERB_DETAIL, "SHM run - worker [%d] - daemon [%p] - timenow(%u) - timeboot(%u)", @@ -236,75 +333,43 @@ void shm_main_run(struct worker *worker) #endif offset = worker->thread_num + 1; - stat_total = worker->daemon->shm_info->ptr_arr; - stat_info = worker->daemon->shm_info->ptr_arr + offset; + stat_total = shm_info->ptr_arr; + stat_info = shm_info->ptr_arr + offset; /* Copy data to the current position */ server_stats_compile(worker, stat_info, 0); - /* First thread, zero fill total, and copy general info */ - if (worker->thread_num == 0) { - - /* Copy data to the current position */ - memset(stat_total, 0, sizeof(struct ub_stats_info)); - - /* Point to data into SHM */ -#ifndef S_SPLINT_S - shm_stat = worker->daemon->shm_info->ptr_ctl; - shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec; - shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec; -#endif - - stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot); - stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat); - - shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache); - shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table); - shm_stat->mem.dnscrypt_shared_secret = 0; -#ifdef USE_DNSCRYPT - if(worker->daemon->dnscenv) { - shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem( - worker->daemon->dnscenv->shared_secrets_cache); - shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem( - worker->daemon->dnscenv->nonces_cache); - } -#endif - shm_stat->mem.val = (long long)mod_get_mem(&worker->env, - "validator"); - shm_stat->mem.iter = (long long)mod_get_mem(&worker->env, - "iterator"); - shm_stat->mem.respip = (long long)mod_get_mem(&worker->env, - "respip"); - - /* subnet mem value is available in shm, also when not enabled, - * to make the struct easier to memmap by other applications, - * independent of the configuration of unbound */ - shm_stat->mem.subnet = 0; -#ifdef CLIENT_SUBNET - shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env, - "subnetcache"); -#endif - /* ipsecmod mem value is available in shm, also when not enabled, - * to make the struct easier to memmap by other applications, - * independent of the configuration of unbound */ - shm_stat->mem.ipsecmod = 0; -#ifdef USE_IPSECMOD - shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env, - "ipsecmod"); -#endif -#ifdef WITH_DYNLIBMODULE - shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env, - "dynlib"); -#endif + /* Lock the lock and see if this thread is first or last of the + * stat threads. It can then zero value or sum up values. */ + lock_basic_lock(&shm_info->lock); + if(shm_thread_is_first(shm_info, worker->thread_num)) { + /* First thread, zero fill total. */ + memset(&shm_info->total_in_progress, 0, + sizeof(struct ub_stats_info)); + shm_info->volley_in_progress = 1; } - - total_mesh_time_median = stat_total->mesh_time_median; - server_stats_add(stat_total, stat_info); + shm_info->thread_volley[worker->thread_num] = 1; + if(worker->thread_num == 0) { + /* Thread 0, copy general info. */ + shm_general_info(worker); + } + /* Add thread data to the total */ + total_mesh_time_median = shm_info->total_in_progress.mesh_time_median; + server_stats_add(&shm_info->total_in_progress, stat_info); /* By adding the value/num per stat thread, for the median, * it is going to add up to the sum/num. */ - stat_total->mesh_time_median = total_mesh_time_median + + shm_info->total_in_progress.mesh_time_median = total_mesh_time_median + (stat_info->mesh_time_median/(double)worker->daemon->num); + if(shm_thread_is_last(worker->daemon)) { + /* Copy over the total */ + memcpy(stat_total, &shm_info->total_in_progress, + sizeof(struct ub_stats_info)); + shm_info->volley_in_progress = 0; + memset(shm_info->thread_volley, 0, + ((size_t)worker->daemon->num) * sizeof(int)); + } + lock_basic_unlock(&shm_info->lock); #else (void)worker; #endif /* HAVE_SHMGET */ diff --git a/util/shm_side/shm_main.h b/util/shm_side/shm_main.h index 76c60e484..245a1e617 100644 --- a/util/shm_side/shm_main.h +++ b/util/shm_side/shm_main.h @@ -47,6 +47,8 @@ struct worker; /* get struct ub_shm_stat_info */ #include "libunbound/unbound.h" +#include "util/locks.h" + /** * The SHM info. */ @@ -59,6 +61,17 @@ struct shm_main_info { int key; int id_ctl; int id_arr; + + /** This mutex is on the volley information. */ + lock_basic_type lock; + /** If there is a volley, a number of stat timer callbacks by the + * threads, in progress. If not, it was never started or has ended + * previously. */ + int volley_in_progress; + /** Per thread, if they have put in stats. 0 if not. */ + int* thread_volley; + /** The total stats of the thread stat timers, it is in progress */ + struct ub_stats_info total_in_progress; }; int shm_main_init(struct daemon* daemon);