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 #include <zephyr/irq_offload.h>
10 #include <zephyr/debug/stack.h>
11
12 #define SLEEP_MS 100
13 #define NUM_OF_WORK 2
14 #define TEST_STRING "TEST"
15
16 static struct k_work work[NUM_OF_WORK];
17 static struct k_sem sync_sema;
18
19 /**TESTPOINT: stack analyze*/
tdata_dump_callback(const struct k_thread * thread,void * user_data)20 static void tdata_dump_callback(const struct k_thread *thread, void *user_data)
21 {
22 log_stack_usage(thread);
23 }
24
25 /*work handler*/
work_handler(struct k_work * w)26 static void work_handler(struct k_work *w)
27 {
28 k_thread_foreach(tdata_dump_callback, NULL);
29 k_sem_give(&sync_sema);
30 }
31
32 /**
33 * @brief Tests for kernel profiling
34 * @defgroup kernel_profiling_tests Profiling
35 * @ingroup all_tests
36 * @{
37 * @}
38 */
39
40 /**
41 * @brief Test stack usage through main thread
42 *
43 * This test prints the main, idle, interrupt and system workqueue
44 * stack usage through main thread.
45 *
46 * @ingroup kernel_profiling_tests
47 *
48 * @see k_thread_foreach(), log_stack_usage()
49 */
ZTEST(profiling_api,test_call_stacks_analyze_main)50 ZTEST(profiling_api, test_call_stacks_analyze_main)
51 {
52 TC_PRINT("from main thread:\n");
53 k_thread_foreach(tdata_dump_callback, NULL);
54 }
55
56 /**
57 * @brief Test stack usage through idle thread
58 *
59 * This test prints the main, idle, interrupt and system workqueue
60 * stack usage through idle thread.
61 *
62 * @ingroup kernel_profiling_tests
63 *
64 * @see k_thread_foreach(), pm_system_suspend(), pm_system_resume(),
65 * log_stack_usage()
66 */
ZTEST(profiling_api_1cpu,test_call_stacks_analyze_idle)67 ZTEST(profiling_api_1cpu, test_call_stacks_analyze_idle)
68 {
69 TC_PRINT("from idle thread:\n");
70 k_msleep(SLEEP_MS);
71 }
72
73 /**
74 * @brief Test stack usage through system workqueue
75 *
76 * This test prints the main, idle, interrupt and system workqueue
77 * stack usage through system workqueue.
78 *
79 * @ingroup kernel_profiling_tests
80 *
81 * @see k_thread_foreach(), k_work_init(), k_work_submit(),
82 * log_stack_usage()
83 */
ZTEST(profiling_api_1cpu,test_call_stacks_analyze_workq)84 ZTEST(profiling_api_1cpu, test_call_stacks_analyze_workq)
85 {
86 TC_PRINT("from workq:\n");
87 k_sem_init(&sync_sema, 0, NUM_OF_WORK);
88 for (int i = 0; i < NUM_OF_WORK; i++) {
89 k_work_init(&work[i], work_handler);
90 k_work_submit(&work[i]);
91 k_sem_take(&sync_sema, K_FOREVER);
92 }
93 }
94
95 /*TODO: add test case to capture the usage of interrupt call stack*/
96
97 ZTEST_SUITE(profiling_api, NULL, NULL, NULL, NULL, NULL);
98
99 ZTEST_SUITE(profiling_api_1cpu, NULL, NULL,
100 ztest_simple_1cpu_before, ztest_simple_1cpu_after, NULL);
101