From 1bb5c34f98b5717bf482bf6b45acf4b345886e26 Mon Sep 17 00:00:00 2001 From: Akaiwa Wataru Date: Thu, 28 Nov 2024 23:23:37 +0900 Subject: [PATCH] tests: kernel/sched: 32-bit tick wraparound during k_sleep() execution This test reproduce the issue: https://github.com/zephyrproject-rtos/zephyr/issues/79863 In this test, instead of waiting the 32-bit tick wraparound for several days, patching the kernel internal tick with the test API. Signed-off-by: Akaiwa Wataru --- tests/kernel/sched/wraparound/CMakeLists.txt | 8 ++++ tests/kernel/sched/wraparound/prj.conf | 1 + tests/kernel/sched/wraparound/src/main.c | 41 ++++++++++++++++++++ tests/kernel/sched/wraparound/testcase.yaml | 3 ++ 4 files changed, 53 insertions(+) create mode 100644 tests/kernel/sched/wraparound/CMakeLists.txt create mode 100644 tests/kernel/sched/wraparound/prj.conf create mode 100644 tests/kernel/sched/wraparound/src/main.c create mode 100644 tests/kernel/sched/wraparound/testcase.yaml diff --git a/tests/kernel/sched/wraparound/CMakeLists.txt b/tests/kernel/sched/wraparound/CMakeLists.txt new file mode 100644 index 00000000000..13e7852267c --- /dev/null +++ b/tests/kernel/sched/wraparound/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sleep) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/kernel/sched/wraparound/prj.conf b/tests/kernel/sched/wraparound/prj.conf new file mode 100644 index 00000000000..9467c292689 --- /dev/null +++ b/tests/kernel/sched/wraparound/prj.conf @@ -0,0 +1 @@ +CONFIG_ZTEST=y diff --git a/tests/kernel/sched/wraparound/src/main.c b/tests/kernel/sched/wraparound/src/main.c new file mode 100644 index 00000000000..e31bbd18cd3 --- /dev/null +++ b/tests/kernel/sched/wraparound/src/main.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 Akaiwa Wataru + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +static k_tid_t thread_id; + +static void alarm_callback(struct k_timer *timer) +{ + k_wakeup(thread_id); +} + +K_TIMER_DEFINE(alarm, alarm_callback, NULL); + +/** + * @brief Test 32-bit tick wraparound during k_sleep() execution + */ +ZTEST(wraparound, test_tick_wraparound_in_sleep) +{ + static const uint32_t start_ticks = 0xffffff00; /* It wraps around after 256 ticks! */ + static const uint32_t timeout_ticks = 300; /* 3 seconds @ 100Hz tick */ + static const uint32_t wakeup_ticks = 10; /* 100 ms @ 100Hz tick */ + + sys_clock_tick_set(start_ticks); + + /* Wake up myself by alarm */ + thread_id = k_current_get(); + k_timer_start(&alarm, K_TICKS(wakeup_ticks), K_FOREVER); + + /* Waiting alarm's k_wakeup() call */ + int32_t left_ms = k_sleep(K_TICKS(timeout_ticks)); + + zassert(left_ms > 0, "k_sleep() timed out"); + + k_timer_stop(&alarm); +} + +ZTEST_SUITE(wraparound, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/kernel/sched/wraparound/testcase.yaml b/tests/kernel/sched/wraparound/testcase.yaml new file mode 100644 index 00000000000..7b32524586b --- /dev/null +++ b/tests/kernel/sched/wraparound/testcase.yaml @@ -0,0 +1,3 @@ +tests: + kernel.scheduler.wraparound: + tags: kernel