From 59b9f0143a241ecc273b2a07529b45a97fdb7c9b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 2 Oct 2024 22:05:40 +0200 Subject: [PATCH 1/3] Outsource counter resetting into a dedicated function periodically run by the GC thread to make the average computation independet from external trigger sources. This solves the problem of artificially high QPS values when no queries are processed by FTL. Signed-off-by: DL6ER --- src/gc.c | 9 +++++++++ src/shmem.c | 32 +++++++++++++------------------- src/shmem.h | 8 +++----- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/gc.c b/src/gc.c index 1aaf036af..3d6514ca3 100644 --- a/src/gc.c +++ b/src/gc.c @@ -560,6 +560,15 @@ void *GC_thread(void *val) reread_config(); } + // Intermediate cancellation-point + if(killed) + break; + + // Reset the queries-per-second counter + lock_shm(); + reset_qps(now); + unlock_shm(); + thread_sleepms(GC, 1000); } diff --git a/src/shmem.c b/src/shmem.c index e5569a5da..7a343de1b 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -1210,29 +1210,23 @@ int __attribute__((pure)) is_shm_fd(const int fd) // Update queries per second (qps) value // This is done in shared memory to allow for both UDP and TCP workers to // contribute. -void update_qps(const double timestamp) +void update_qps(const time_t timestamp) { // Get the timeslot for the current timestamp - const unsigned int slot = (unsigned int)timestamp % QPS_AVGLEN; + const unsigned int slot = timestamp % QPS_AVGLEN; - // Check if the timestamp is in the same slot as the last one - if(shmSettings->qps.last != slot) - { - // Reset all the slots in between - // This is relevant if less than one query per second is - // received and the intermediate slots are not updated - for(unsigned int i = (shmSettings->qps.last + 1) % QPS_AVGLEN; i != slot; i = (i + 1) % QPS_AVGLEN) - shmSettings->qps.buf[i] = 0; - - // Reset the current slot - shmSettings->qps.buf[slot] = 0; + // Add the query + shmSettings->qps[slot]++; +} - // Update the last slot index - shmSettings->qps.last = slot; - } +// Reset queries per second (qps) value for a given timeslot +void reset_qps(const time_t timestamp) +{ + // Get the timeslot for the current timestamp + const unsigned int slot = timestamp % QPS_AVGLEN; - // Add the query - shmSettings->qps.buf[slot]++; + // Reset the query count + shmSettings->qps[slot] = 0; } // Compute queries per second (qps) value @@ -1245,7 +1239,7 @@ double __attribute__((pure)) get_qps(void) // double qps = 0.0; for(unsigned int i = 0; i < QPS_AVGLEN; i++) - qps += shmSettings->qps.buf[i]; + qps += shmSettings->qps[i]; return qps / QPS_AVGLEN; } diff --git a/src/shmem.h b/src/shmem.h index f717445ee..5f37a56b8 100644 --- a/src/shmem.h +++ b/src/shmem.h @@ -31,10 +31,7 @@ typedef struct { pid_t pid; unsigned int global_shm_counter; unsigned int next_str_pos; - struct { - unsigned int last; - unsigned int buf[QPS_AVGLEN]; - } qps; + unsigned int qps[QPS_AVGLEN]; } ShmSettings; typedef struct { @@ -155,7 +152,8 @@ void set_per_client_regex(const int clientID, const int regexID, const bool valu // Used in dnsmasq/utils.c int is_shm_fd(const int fd); -void update_qps(const double timestamp); +void update_qps(const time_t timestamp); +void reset_qps(const time_t timestamp); double get_qps(void) __attribute__((pure)); #endif //SHARED_MEMORY_SERVER_H From 7ec1478018a46a90a5b280fd1064f8229aeb4ac9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 2 Oct 2024 22:15:08 +0200 Subject: [PATCH 2/3] Adaptively adjust GC sleeping time (if necessary) Signed-off-by: DL6ER --- src/gc.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/gc.c b/src/gc.c index 3d6514ca3..af315d449 100644 --- a/src/gc.c +++ b/src/gc.c @@ -504,6 +504,7 @@ void *GC_thread(void *val) while(!killed) { const time_t now = time(NULL); + const double time_start = double_time(); if(config.dns.rateLimit.interval.v.ui > 0 && (unsigned int)(now - lastRateLimitCleaner) >= config.dns.rateLimit.interval.v.ui) { @@ -569,7 +570,17 @@ void *GC_thread(void *val) reset_qps(now); unlock_shm(); - thread_sleepms(GC, 1000); + // Intermediate cancellation-point + if(killed) + break; + + // Sleep for the remaining time of the interval (if any) + const double time_end = double_time(); + const double time_diff = time_end - time_start; + const double sleep_time = 1.0 - time_diff; // 1.0 second interval + + if(sleep_time > 0) + thread_sleepms(GC, (unsigned int)(sleep_time*1000)); } // Close inotify watcher From 7e12e40840da8bea155a833532c48962d5ce3bdd Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 3 Oct 2024 19:22:58 +0200 Subject: [PATCH 3/3] Reset the following timestamp slot to avoid possible race-collisions (GC thread is not aligned to wall clock) Signed-off-by: DL6ER --- src/shmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shmem.c b/src/shmem.c index 7a343de1b..7568345bb 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -1219,11 +1219,12 @@ void update_qps(const time_t timestamp) shmSettings->qps[slot]++; } -// Reset queries per second (qps) value for a given timeslot +// Reset queries per second (qps) value for the timeslot following the current +// one void reset_qps(const time_t timestamp) { // Get the timeslot for the current timestamp - const unsigned int slot = timestamp % QPS_AVGLEN; + const unsigned int slot = (timestamp + 1) % QPS_AVGLEN; // Reset the query count shmSettings->qps[slot] = 0; @@ -1241,5 +1242,6 @@ double __attribute__((pure)) get_qps(void) for(unsigned int i = 0; i < QPS_AVGLEN; i++) qps += shmSettings->qps[i]; + // Return the computed value divided by N (the number of slots) return qps / QPS_AVGLEN; }