From 082f3f91cf75aeda6889c0b61109ce1604f3b875 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 5 Mar 2025 12:46:05 -0400 Subject: [PATCH] MT#55283 add timer-accuracy= option Change-Id: I32eb05a5eaacccd2ef3e553a90a1eada33a9f50d --- daemon/main.c | 5 +++++ daemon/timerthread.c | 5 ++++- docs/rtpengine.md | 8 ++++++++ include/main.h | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/daemon/main.c b/daemon/main.c index 2a402aac2..b9d418a04 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -121,6 +121,7 @@ struct rtpengine_config rtpe_config = { }, .max_recv_iters = MAX_RECV_ITERS, .kernel_player_media = 128, + .timer_accuracy = 500, }; struct interface_config_callback_arg { @@ -738,6 +739,7 @@ static void options(int *argc, char ***argv, charp_ht templates) { { "http-threads", 0,0, G_OPTION_ARG_INT, &rtpe_config.http_threads,"Number of worker threads for HTTP and WS","INT"}, { "software-id", 0,0, G_OPTION_ARG_STRING, &rtpe_config.software_id,"Identification string of this software presented to external systems","STRING"}, { "poller-per-thread", 0,0, G_OPTION_ARG_NONE, &rtpe_config.poller_per_thread, "Use poller per thread", NULL }, + { "timer-accuracy", 0,0,G_OPTION_ARG_INT, &rtpe_config.timer_accuracy,"Minimum number of microseconds to sleep","INT"}, #ifdef WITH_TRANSCODING { "dtx-delay", 0,0, G_OPTION_ARG_INT, &rtpe_config.dtx_delay, "Delay in milliseconds to trigger DTX handling","INT"}, { "max-dtx", 0,0, G_OPTION_ARG_INT, &rtpe_config.max_dtx, "Maximum duration of DTX handling", "INT"}, @@ -1280,6 +1282,9 @@ static void options(int *argc, char ***argv, charp_ht templates) { die("Number of CPU cores is unknown, cannot auto-set socket CPU affinity"); } + if (rtpe_config.timer_accuracy < 0) + die("Invalid --timer-accuracy value (%d)", rtpe_config.timer_accuracy); + // everything OK, do post-processing } diff --git a/daemon/timerthread.c b/daemon/timerthread.c index 7e30b91c8..58f1139d4 100644 --- a/daemon/timerthread.c +++ b/daemon/timerthread.c @@ -3,6 +3,7 @@ #include "helpers.h" #include "log_funcs.h" #include "poller.h" +#include "main.h" static int tt_obj_cmp(const void *a, const void *b) { @@ -55,6 +56,8 @@ static void timerthread_run(void *p) { struct thread_waker waker = { .lock = &tt->lock, .cond = &tt->cond }; thread_waker_add(&waker); + long long accuracy = rtpe_config.timer_accuracy; + mutex_lock(&tt->lock); while (!rtpe_shutdown) { @@ -76,7 +79,7 @@ static void timerthread_run(void *p) { // scheduled to run? if not, then we remember this object/reference and go to sleep sleeptime = timeval_diff(&tt_obj->next_check, &rtpe_now); - if (sleeptime > 0) { + if (sleeptime > accuracy) { tt->obj = tt_obj; goto sleep; } diff --git a/docs/rtpengine.md b/docs/rtpengine.md index b23ade206..90f9560f9 100644 --- a/docs/rtpengine.md +++ b/docs/rtpengine.md @@ -1322,6 +1322,14 @@ call to inject-DTMF won't be sent to __\-\-dtmf-log-dest=__ or __\-\-listen-tcp- thus maintaining the order of the packets. Might help when having issues with DTMF packets (RFC 2833). +- __\-\-timer-accuracy=__*INT* + + Minimum number of microseconds that a timer thread is allowed to sleep. In + other words, if a timed event is scheduled sooner than this many + microseconds in the future, then the event will be executed immediately. + Set to zero for highest accuracy. Default is 500 microseconds (half a + millisecond). + - __\-\-io-uring__ Enable **experimental** support for `io_uring`. Requires Linux kernel 6.0 diff --git a/include/main.h b/include/main.h index 5636c4008..515255d24 100644 --- a/include/main.h +++ b/include/main.h @@ -99,6 +99,7 @@ enum endpoint_learning { X(media_expire) \ X(db_expire) \ X(cache_expire) \ + X(timer_accuracy) \ #define RTPE_CONFIG_UINT64_PARAMS \ X(bw_limit)