Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | /* * Copyright (c) 2016 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 */ /* * @file * @brief Test early sleeping microkernel mechanism * * This test verifies that both fiber_sleep() and task_sleep() * can each be used to put the calling thread to sleep for a specified * number of ticks during system initialization (before k_server() starts) * as well as after the microkernel initializes (after k_server() starts). * * To ensure that the nanokernel timeout both operates correctly during * system initialization and that it allows fibers to sleep for a specified * number of ticks the test has a fiber invoke fiber_sleep() before the init * task invokes task_sleep(). The fiber sleep time is less than that of the * task sleep time so that the fiber will wake before the init task wakes. */ #include <zephyr.h> #include <tc_util.h> #include <init.h> #define FIBER_TICKS_TO_SLEEP 40 #define TASK_TICKS_TO_SLEEP 50 /* time that the task was actually sleeping */ static int task_actual_sleep_ticks; static int task_actual_sleep_nano_ticks; static int task_actual_sleep_micro_ticks; static int task_actual_sleep_app_ticks; /* time that the fiber was actually sleeping */ static volatile int fiber_actual_sleep_ticks; /* * Flag is changed by lower priority task to make sure * that sleeping did not go in a tight toop */ static bool alternate_task_run; /* test fiber synchronization semaphore */ static struct nano_sem test_fiber_sem; /** * * @brief Put task to sleep and measure time it really slept * * @param ticks_to_sleep number of ticks for a task to sleep * * @return number of ticks the task actually slept */ int test_task_sleep(int ticks_to_sleep) { uint32_t start_time; uint32_t stop_time; start_time = sys_cycle_get_32(); task_sleep(ticks_to_sleep); stop_time = sys_cycle_get_32(); return (stop_time - start_time) / sys_clock_hw_cycles_per_tick; } /** * * @brief Put fiber to sleep and measure time it really slept * * @param ticks_to_sleep number of ticks for a fiber to sleep * * @return number of ticks the fiber actually slept */ int test_fiber_sleep(int ticks_to_sleep) { uint32_t start_time; uint32_t stop_time; start_time = sys_cycle_get_32(); fiber_sleep(ticks_to_sleep); stop_time = sys_cycle_get_32(); return (stop_time - start_time) / sys_clock_hw_cycles_per_tick; } /** * * @brief Early task sleep test * * Note: it will be used to test the early sleep at SECONDARY level too * * Call task_sleep() and checks the time sleep actually * took to make sure that task actually slept * * @return 0 */ static int test_early_task_sleep(struct device *unused) { ARG_UNUSED(unused); task_actual_sleep_ticks = test_task_sleep(TASK_TICKS_TO_SLEEP); return 0; } SYS_INIT(test_early_task_sleep, SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); /** * * @brief Early task sleep test in NANOKERNEL level only * * Call task_sleep() and checks the time sleep actually * took to make sure that task actually slept * * @return 0 */ static int test_early_task_sleep_in_nanokernel_level(struct device *unused) { ARG_UNUSED(unused); task_actual_sleep_nano_ticks = test_task_sleep(TASK_TICKS_TO_SLEEP); return 0; } SYS_INIT(test_early_task_sleep_in_nanokernel_level, NANOKERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); /** * * @brief Early task sleep test in MICROKERNEL level only * * Call task_sleep() and checks the time sleep actually * took to make sure that task actually slept * * @return 0 */ static int test_early_task_sleep_in_microkernel_level(struct device *unused) { ARG_UNUSED(unused); task_actual_sleep_micro_ticks = test_task_sleep(TASK_TICKS_TO_SLEEP); return 0; } SYS_INIT(test_early_task_sleep_in_microkernel_level, MICROKERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); /** * * @brief Early task sleep test in APPLICATION level only * * Call task_sleep() and checks the time sleep actually * took to make sure that task actually slept * * @return 0 */ static int test_early_task_sleep_in_application_level(struct device *unused) { ARG_UNUSED(unused); task_actual_sleep_app_ticks = test_task_sleep(TASK_TICKS_TO_SLEEP); return 0; } SYS_INIT(test_early_task_sleep_in_application_level, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); /** * * @brief Fiber function that measures fiber sleep time * * @return N/A */ static void test_fiber(int ticks_to_sleep, int unused) { ARG_UNUSED(unused); while (1) { fiber_actual_sleep_ticks = test_fiber_sleep(ticks_to_sleep); fiber_sem_give(TEST_FIBER_SEM); nano_sem_take(&test_fiber_sem, TICKS_UNLIMITED); } } #define STACKSIZE 512 char __stack test_fiber_stack[STACKSIZE]; /** * * @brief Initialize test fiber data * * @return 0 */ static int test_fiber_start(struct device *unused) { ARG_UNUSED(unused); fiber_actual_sleep_ticks = 0; nano_sem_init(&test_fiber_sem); task_fiber_start(&test_fiber_stack[0], STACKSIZE, (nano_fiber_entry_t) test_fiber, FIBER_TICKS_TO_SLEEP, 0, 7, 0); return 0; } SYS_INIT(test_fiber_start, SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); /** * * @brief Lower priority task to make sure that main task really sleeps * * * * @return N/A */ void AlternateTask(void) { alternate_task_run = true; } /** * * @brief Regression task * * Checks the results of the early sleep * * @return N/A */ void RegressionTask(void) { TC_START("Test early and regular task and fiber sleep functionality\n"); alternate_task_run = false; TC_PRINT("Test fiber_sleep() call during the system initialization\n"); /* * Make sure that the fiber_sleep() called during the * initialization has returned. * fiber_sleep() invoked during the initialization for the * shorter period that task_sleep() should return by now. */ if (task_sem_take(TEST_FIBER_SEM, TICKS_NONE) != RC_OK) { TC_ERROR("fiber_sleep() has not returned while expected\n"); } /* * Check that the fiber_sleep() called during the system * initialization put the fiber to sleep for the specified * amount of time * * On heavily loaded systems QEMU may demonstrate a drift * of hardware clock ticks to system clock. Test verifies * that sleep took at least not less amount of time. * Allow up to 1 tick variance as the test may not have put * the task to sleep on a tick boundary. */ if ((fiber_actual_sleep_ticks + 1) < FIBER_TICKS_TO_SLEEP) { TC_ERROR("fiber_sleep() time is too small: %d\n", fiber_actual_sleep_ticks); goto error_out; } /* * Check that the task_sleep() called during the system * initialization puts the task to sleep for the specified * amount of time */ TC_PRINT("Test task_sleep() call during the system initialization\n"); TC_PRINT("- At SECONDARY level\n"); if ((task_actual_sleep_ticks + 1) < TASK_TICKS_TO_SLEEP) { TC_ERROR("task_sleep() time is is too small: %d\n", task_actual_sleep_ticks); goto error_out; } /* * Check that the task_sleep() called during the system * initialization at NANOKERNEL level puts the task to sleep for * the specified amount of time */ TC_PRINT("- At NANOKERNEL level\n"); if ((task_actual_sleep_nano_ticks + 1) < TASK_TICKS_TO_SLEEP) { TC_ERROR("task_sleep() time is is too small: %d\n", task_actual_sleep_nano_ticks); goto error_out; } /* * Check that the task_sleep() called during the system * initialization at MICROKERNEL level puts the task to sleep for * the specified amount of time */ TC_PRINT("- At MICROKERNEL level\n"); if ((task_actual_sleep_micro_ticks + 1) < TASK_TICKS_TO_SLEEP) { TC_ERROR("task_sleep() time is is too small: %d\n", task_actual_sleep_micro_ticks); goto error_out; } /* * Check that the task_sleep() called during the system * initialization at APPLICATION level puts the task to sleep for * the specified amount of time */ TC_PRINT("- At APPLICATION level\n"); if ((task_actual_sleep_app_ticks + 1) < TASK_TICKS_TO_SLEEP) { TC_ERROR("task_sleep() time is is too small: %d\n", task_actual_sleep_app_ticks); goto error_out; } /* * Check that the task_sleep() called during the normal * microkernel work put the task to sleep for the specified * amount of time */ TC_PRINT("Test task_sleep() call on a running system\n"); task_actual_sleep_ticks = test_task_sleep(TASK_TICKS_TO_SLEEP); if ((task_actual_sleep_ticks + 1) < TASK_TICKS_TO_SLEEP) { TC_ERROR("task_sleep() time is too small: %d\n", task_actual_sleep_ticks); goto error_out; } /* check that calling task_sleep() allowed the lower priority task run */ if (!alternate_task_run) { TC_ERROR("Lower priority task did not run during task_sleep()\n"); goto error_out; } /* * Check that the fiber_sleep() called during the normal * microkernel work put the fiber to sleep for the specified * amount of time */ TC_PRINT("Test fiber_sleep() call on a running system\n"); fiber_actual_sleep_ticks = 0; nano_sem_give(&test_fiber_sem); /* wait for the test fiber return from the sleep */ task_sem_take(TEST_FIBER_SEM, TICKS_UNLIMITED); if ((fiber_actual_sleep_ticks + 1) < FIBER_TICKS_TO_SLEEP) { TC_ERROR("fiber_sleep() time is too small: %d\n", fiber_actual_sleep_ticks); goto error_out; } TC_END_RESULT(TC_PASS); TC_END_REPORT(TC_PASS); return; error_out: TC_END_RESULT(TC_FAIL); TC_END_REPORT(TC_FAIL); } |