1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/pm/pm.h>
9
10 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
11 #define NUM_THREAD 4
12 static K_THREAD_STACK_ARRAY_DEFINE(tstack, NUM_THREAD, STACK_SIZE);
13 static struct k_thread tdata[NUM_THREAD];
14
15 #define IDLE_THRESH 20
16
17 /*sleep duration tickless*/
18 #define SLEEP_TICKLESS k_ticks_to_ms_floor64(IDLE_THRESH)
19
20 /*sleep duration with tick*/
21 #define SLEEP_TICKFUL k_ticks_to_ms_floor64(IDLE_THRESH - 1)
22
23 /*slice size is set as half of the sleep duration*/
24 #define SLICE_SIZE k_ticks_to_ms_floor64(IDLE_THRESH >> 1)
25
26 /*maximum slice duration accepted by the test*/
27 #define SLICE_SIZE_LIMIT k_ticks_to_ms_floor64((IDLE_THRESH >> 1) + 1)
28
29 /*align to millisecond boundary*/
30 #define ALIGN_MS_BOUNDARY() \
31 do { \
32 uint32_t t = k_uptime_get_32(); \
33 while (t == k_uptime_get_32()) \
34 Z_SPIN_DELAY(50); \
35 } while (0)
36
37 K_SEM_DEFINE(sema, 0, NUM_THREAD);
38 static int64_t elapsed_slice;
39
thread_tslice(void * p1,void * p2,void * p3)40 static void thread_tslice(void *p1, void *p2, void *p3)
41 {
42 int64_t t = k_uptime_delta(&elapsed_slice);
43
44 TC_PRINT("elapsed slice %" PRId64 ", expected: <%" PRId64 ", %" PRId64 ">\n",
45 t, SLICE_SIZE, SLICE_SIZE_LIMIT);
46
47 /**TESTPOINT: verify slicing scheduler behaves as expected*/
48 zassert_true(t >= SLICE_SIZE);
49 /*less than one tick delay*/
50 zassert_true(t <= SLICE_SIZE_LIMIT);
51
52 /*keep the current thread busy for more than one slice*/
53 k_busy_wait(1000 * SLEEP_TICKLESS);
54 k_sem_give(&sema);
55 }
56 /**
57 * @defgroup kernel_tickless_tests Tickless
58 * @ingroup all_tests
59 * @{
60 */
61
62
63 /**
64 * @brief Verify system clock with and without tickless idle
65 *
66 * @details Check if system clock recovers and works as expected
67 * when tickless idle is enabled and disabled.
68 */
ZTEST(tickless_concept,test_tickless_sysclock)69 ZTEST(tickless_concept, test_tickless_sysclock)
70 {
71 volatile uint32_t t0, t1;
72
73 ALIGN_MS_BOUNDARY();
74 t0 = k_uptime_get_32();
75 k_msleep(SLEEP_TICKLESS);
76 t1 = k_uptime_get_32();
77 TC_PRINT("time %d, %d\n", t0, t1);
78 /**TESTPOINT: verify system clock recovery after exiting tickless idle*/
79 zassert_true((t1 - t0) >= SLEEP_TICKLESS);
80
81 ALIGN_MS_BOUNDARY();
82 t0 = k_uptime_get_32();
83 k_sem_take(&sema, K_MSEC(SLEEP_TICKFUL));
84 t1 = k_uptime_get_32();
85 TC_PRINT("time %d, %d\n", t0, t1);
86 /**TESTPOINT: verify system clock recovery after exiting tickful idle*/
87 zassert_true((t1 - t0) >= SLEEP_TICKFUL);
88 }
89
90 /**
91 * @brief Verify tickless functionality with time slice
92 *
93 * @details Create threads of equal priority and enable time
94 * slice. Check if the threads execute more than a tick.
95 */
ZTEST(tickless_concept,test_tickless_slice)96 ZTEST(tickless_concept, test_tickless_slice)
97 {
98 k_tid_t tid[NUM_THREAD];
99
100 k_sem_reset(&sema);
101 /*enable time slice*/
102 k_sched_time_slice_set(SLICE_SIZE, K_PRIO_PREEMPT(0));
103
104 /*create delayed threads with equal preemptive priority*/
105 for (int i = 0; i < NUM_THREAD; i++) {
106 tid[i] = k_thread_create(&tdata[i], tstack[i], STACK_SIZE,
107 thread_tslice, NULL, NULL, NULL,
108 K_PRIO_PREEMPT(0), 0,
109 K_MSEC(SLICE_SIZE));
110 }
111 k_uptime_delta(&elapsed_slice);
112 /*relinquish CPU and wait for each thread to complete*/
113 for (int i = 0; i < NUM_THREAD; i++) {
114 k_sem_take(&sema, K_FOREVER);
115 }
116
117 /*test case teardown*/
118 for (int i = 0; i < NUM_THREAD; i++) {
119 k_thread_abort(tid[i]);
120 }
121 /*disable time slice*/
122 k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
123 }
124
125 /**
126 * @}
127 */
128
129 ZTEST_SUITE(tickless_concept, NULL, NULL,
130 ztest_simple_1cpu_before, ztest_simple_1cpu_after, NULL);
131