From 4108e1474038bf877185d1cb5e791d2f5bef38e5 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Thu, 15 Dec 2022 10:14:43 -0800 Subject: [PATCH] ztest: provide sys_clock_tick_set syscall Accurate timekeeping is something that is often taken for granted. However, reliability of timekeeping code is critical for most core and subsystem code. Furthermore, Many higher-level timekeeping utilities in Zephyr work off of ticks but there is no way to modify ticks directly which would require either unnecessary delays in test code or non-ideal compromises in test coverage. Since timekeeping is so critical, there should be as few barriers to testing timekeeping code as possible, while preserving integrity of the kernel's public interface. With this, we expose `sys_clock_tick_set()` as a system call only when `CONFIG_ZTEST` is set, declared within the ztest framework. Signed-off-by: Chris Friedt --- kernel/timeout.c | 12 ++++++++++++ subsys/testsuite/ztest/include/zephyr/ztest_test.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/kernel/timeout.c b/kernel/timeout.c index 98c38a0908f..7e0daee0641 100644 --- a/kernel/timeout.c +++ b/kernel/timeout.c @@ -379,3 +379,15 @@ uint64_t sys_clock_timeout_end_calc(k_timeout_t timeout) return sys_clock_tick_get() + MAX(1, dt); } } + +#ifdef CONFIG_ZTEST +void z_impl_sys_clock_tick_set(uint64_t tick) +{ + curr_tick = tick; +} + +void z_vrfy_sys_clock_tick_set(uint64_t tick) +{ + z_impl_sys_clock_tick_set(tick); +} +#endif diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_test.h b/subsys/testsuite/ztest/include/zephyr/ztest_test.h index 693d139dc92..b9635560c02 100644 --- a/subsys/testsuite/ztest/include/zephyr/ztest_test.h +++ b/subsys/testsuite/ztest/include/zephyr/ztest_test.h @@ -21,6 +21,8 @@ extern "C" { __syscall void z_test_1cpu_start(void); __syscall void z_test_1cpu_stop(void); +__syscall void sys_clock_tick_set(uint64_t tick); + #ifdef __cplusplus } #endif