From aee8dd1d33bf0ca1088288a36bc1ab08f138f156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Tue, 26 Mar 2024 16:33:06 +0100 Subject: [PATCH] kernel: timeout: Optimize setting next alarm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Next timeout was set unconditionally at the end of sys_clock_announce. However, if one of the current expired timeouts was setting a new timeout which is the first to execute then system clock was configured twice. Lets configure system clock only once in the isr at the and of sys_clock_announce. If timeouts are frequent this optimization can reduce CPU load. In many cases setting the new sys_clock timeout is the most time consuming operation in the sys_clock isr handler. As an example, on the target I used setting new sys_clock timeout is taking 6 uS of 9 uS spent in the isr and it takes 16 uS with the redundant call. Signed-off-by: Krzysztof Chruściński --- kernel/timeout.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/timeout.c b/kernel/timeout.c index 3d5cb51c0f0..8c727e204f9 100644 --- a/kernel/timeout.c +++ b/kernel/timeout.c @@ -135,7 +135,7 @@ void z_add_timeout(struct _timeout *to, _timeout_func_t fn, sys_dlist_append(&timeout_list, &to->node); } - if (to == first()) { + if (to == first() && announce_remaining == 0) { sys_clock_set_timeout(next_timeout(), false); } }