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
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-07-03 rcitach test case for irq latency
* 2025-11-30 westcity-YOLO Add standardized utest documentation block
*/
/**
* Test Case Name: Kernel Core Performance Benchmark Suite
*
* Test Objectives:
* - Measures key kernel performance metrics using high-resolution hardware timer
* - Validates context switch and IPC mechanism latency
* - Tests interrupt-to-thread response time (IRQ latency)
* - Provides quantitative results in microseconds (us)
*
* Test Scenarios:
* - **Context Switch Overhead**: Thread-to-thread switch time
* - **Semaphore Performance**: rt_sem_take/rt_sem_release round-trip latency
* - **Event Performance**: rt_event_recv/rt_event_send latency
* - **Message Queue Performance**: rt_mq_send/rt_mq_recv latency
* - **Mailbox Performance**: rt_mb_send/rt_mb_recv latency
* - **IRQ Latency**: Time from hardware timer interrupt to thread wakeup
*
* Verification Metrics:
* - All tests complete without crash or timeout
* - Measured times are within reasonable system limits (e.g., < 1000 us for IRQ)
* - Performance data is printed in structured table format
* - No memory leaks during test execution
*
* Dependencies:
* - RT_USING_PERF_TEST must be enabled
* - RT_USING_UTEST framework enabled
* - Hardware timer device named "hwtimer0" (or defined by RT_UTEST_HWTIMER_DEV_NAME)
* - System must support rt_device_find/open/close for HWTIMER
* - Sufficient heap memory for dynamic allocation in test runner
*
* Expected Results:
* - Console output shows a formatted table with:
* Test No | Test Name | Count | Total/Max/Min/Avg Time (us)
* - Final line: "=== Performance Test Results End ==="
* - utest framework reports: [ PASSED ] [ result ] testcase (core.perf_test)
* - Test runs via: `utest_run core.perf_test` in msh
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rtservice.h>
#include <utest.h>
#include <utest_assert.h>
#include <perf_tc.h>
#define RET_INT 0
#define RET_DECIMALS 1
#define GET_INT(num) split_double(num, RET_INT)
#define GET_DECIMALS(num) split_double(num, RET_DECIMALS)
static rt_device_t hw_dev = RT_NULL;
static rt_clock_timerval_t timeout_s = { 0 };
typedef rt_err_t (*testcase_function)(rt_perf_t *perf);
testcase_function test_func_ptrs[] = {
context_switch_test,
rt_perf_thread_sem,
rt_perf_thread_event,
rt_perf_thread_mq,
rt_perf_thread_mbox,
rt_perf_irq_latency, /* Timer Interrupt Source */
RT_NULL
};
static rt_uint32_t rt_perf_get_timer_us(void)
{
rt_clock_timerval_t timer_val = { 0 };
if (hw_dev && rt_device_read(hw_dev, 0, &timer_val, sizeof(rt_clock_timerval_t)))
{
return (rt_uint32_t)(timer_val.sec * 1000000u + timer_val.usec); /* return us */
}
return 0;
}
void rt_perf_start_impl(rt_perf_t *perf, rt_clock_timerval_t *timeout)
{
if (hw_dev)
{
if (timeout == RT_NULL)
timeout = &timeout_s;
rt_device_write(hw_dev, 0, timeout, sizeof(rt_clock_timerval_t));
}
perf->begin_time = rt_perf_get_timer_us();
}
void rt_perf_stop(rt_perf_t *perf)
{
perf->real_time = rt_perf_get_timer_us() - perf->begin_time;
if (perf->local_modify)
perf->local_modify(perf);
if (perf->real_time > perf->max_time)
{
perf->max_time = perf->real_time;
}
if (perf->real_time < perf->min_time)
{
perf->min_time = perf->real_time;
}
perf->count++;
perf->tot_time += perf->real_time;
if (hw_dev)
rt_device_control(hw_dev, CLOCK_TIMER_CTRL_STOP, NULL);
}
static rt_int32_t split_double(double num, rt_uint32_t type)
{
if (type == RET_INT)
{
return (rt_int32_t)num;
}
else if (type == RET_DECIMALS)
{
return (rt_int32_t)((num - (rt_int32_t)num) * 10000);
}
else
{
return (-1);
}
return (-1);
}
void rt_perf_dump(rt_perf_t *perf)
{
static rt_uint32_t test_index = 1;
char avg_str[10] = { 0 };
if (perf->dump_head)
{
rt_kprintf("Test No | Test Name | Count | Total Time (us) | Max Time (us) | Min Time (us) | Avg Time (us)\n");
rt_kprintf("--------|----------------------|-------|-----------------|---------------|---------------|--------------\n");
perf->dump_head = RT_FALSE;
}
if (perf->count)
perf->avg_time = (double)perf->tot_time / perf->count;
else
perf->avg_time = 0.0;
rt_sprintf(avg_str, "%u.%04u", GET_INT(perf->avg_time), GET_DECIMALS(perf->avg_time));
rt_kprintf("%7u | %-20s | %5u | %15u | %13u | %13u | %12s\n",
test_index++,
perf->name,
perf->count,
perf->tot_time,
perf->max_time,
perf->min_time,
avg_str);
}
static void rt_perf_clear(rt_perf_t *perf)
{
perf->local_modify = NULL;
perf->begin_time = 0;
perf->real_time = 0;
perf->tot_time = 0;
perf->max_time = 0;
perf->min_time = RT_UINT32_MAX;
perf->count = 0;
perf->avg_time = 0;
perf->tmp_time = 0;
}
static void rt_perf_all_test(void)
{
rt_perf_t *perf_data = rt_malloc(sizeof(rt_perf_t));
if (perf_data == RT_NULL)
{
return;
}
perf_data->lock = rt_mutex_create("perf", RT_IPC_FLAG_PRIO);
perf_data->dump_head = RT_TRUE;
rt_kprintf("\n === Performance Test Results Start ===\n");
for (int i = 0; test_func_ptrs[i] != RT_NULL; i++)
{
rt_perf_clear(perf_data);
if (test_func_ptrs[i](perf_data) != RT_EOK)
{
LOG_E("%s test fail", perf_data->name);
continue;
}
}
rt_kprintf("\n === Performance Test Results End ===\n");
rt_mutex_delete(perf_data->lock);
rt_free(perf_data);
}
static rt_err_t utest_tc_init(void)
{
int ret = RT_EOK;
hw_dev = rt_device_find(RT_UTEST_CLOCK_TIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
ret = RT_ERROR;
LOG_E("clock_timer sample run failed! can't find %s device!", RT_UTEST_CLOCK_TIMER_DEV_NAME);
return ret;
}
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
if (ret != RT_EOK)
{
LOG_E("open %s device failed!", RT_UTEST_CLOCK_TIMER_DEV_NAME);
return ret;
}
timeout_s.sec = 10; /* No modification is necessary here, use the fixed value */
timeout_s.usec = 0;
return ret;
}
static rt_err_t utest_tc_cleanup(void)
{
if (hw_dev)
rt_device_close(hw_dev);
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(rt_perf_all_test);
}
UTEST_TC_EXPORT(testcase, "core.perf_test", utest_tc_init, utest_tc_cleanup, 10);
|