Skip to content

Commit 2a6baf8

Browse files
committed
add thread event test code
1 parent 2f91485 commit 2a6baf8

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-07-03 rcitach test case for event
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rthw.h>
13+
#include <rtdevice.h>
14+
#include <utest.h>
15+
#include <utest_assert.h>
16+
17+
#define PERF_COUNT 1000
18+
#define TIME_OUT_TIME 10
19+
#define OUTTIME_TICK (RT_TICK_PER_SECOND * TIME_OUT_TIME)
20+
#define EVENT_FLAG (1 << 0)
21+
static rt_event_t perf_thread_event = RT_NULL;
22+
static rt_sem_t sem1 = RT_NULL;
23+
volatile static uint16_t thread_event_count = 0;
24+
25+
#define THREAD_STACK_SIZE 1024
26+
#define THREAD_PRIORITY 10
27+
#define THREAD_TIMESLICE 5
28+
29+
struct perf_time
30+
{
31+
rt_uint32_t start_time;
32+
rt_uint32_t end_time;
33+
rt_uint32_t use_time;
34+
};
35+
static struct perf_time use_time[PERF_COUNT];
36+
37+
#ifdef USING_HWTIME_AS_TIME_REF
38+
static rt_device_t hw_dev = RT_NULL;
39+
static rt_hwtimerval_t timer_val;
40+
41+
static rt_uint32_t get_timer_us(void)
42+
{
43+
if (hw_dev && rt_device_read(hw_dev, 0, &timer_val, sizeof(rt_hwtimerval_t)))
44+
{
45+
return (rt_uint32_t)(timer_val.sec * 1000000 + timer_val.usec);
46+
}
47+
return 0;
48+
}
49+
#endif
50+
51+
static void perf_thread_event1(void *parameter)
52+
{
53+
rt_err_t ret = RT_EOK;
54+
rt_uint32_t recv = 0;
55+
rt_uint32_t total_time = 0;
56+
57+
while (1)
58+
{
59+
ret= rt_event_recv(perf_thread_event, EVENT_FLAG,
60+
(RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR),
61+
RT_WAITING_FOREVER, &recv);
62+
#ifdef USING_HWTIME_AS_TIME_REF
63+
use_time[thread_event_count].end_time = get_timer_us();
64+
use_time[thread_event_count].use_time = use_time[thread_event_count].end_time - use_time[thread_event_count].start_time;
65+
#else
66+
use_time[thread_event_count].end_time = rt_tick_get();
67+
use_time[thread_event_count].use_time = (use_time[thread_event_count].end_time - use_time[thread_event_count].start_time) / RT_TICK_PER_SECOND;
68+
use_time[thread_event_count].use_time += ((use_time[thread_event_count].end_time - use_time[thread_event_count].start_time) % RT_TICK_PER_SECOND) * (1000000u / RT_TICK_PER_SECOND);
69+
#endif
70+
if (ret != RT_EOK)
71+
{
72+
LOG_E("event recv error!");
73+
rt_event_delete(perf_thread_event);
74+
return;
75+
}
76+
total_time += use_time[thread_event_count].use_time;
77+
thread_event_count ++;
78+
if (thread_event_count >= PERF_COUNT)
79+
{
80+
LOG_I("Event test completed: %d iterations, total time = %lu us, average = %lu us per event",
81+
PERF_COUNT, total_time, total_time / PERF_COUNT);
82+
83+
rt_event_delete(perf_thread_event);
84+
rt_sem_delete(sem1);
85+
return;
86+
}
87+
rt_sem_release(sem1);
88+
}
89+
}
90+
91+
static void perf_thread_event2(void *parameter)
92+
{
93+
rt_err_t ret = RT_EOK;
94+
while (1)
95+
{
96+
if (thread_event_count >= PERF_COUNT)
97+
{
98+
return;
99+
}
100+
ret = rt_sem_take(sem1, OUTTIME_TICK);
101+
if(ret == -RT_ETIMEOUT)
102+
{
103+
LOG_W("Timeout when waiting for sem1");
104+
rt_sem_delete(sem1);
105+
return;
106+
}
107+
#ifdef USING_HWTIME_AS_TIME_REF
108+
use_time[thread_event_count].start_time = get_timer_us();
109+
#else
110+
use_time[thread_event_count].start_time = rt_tick_get();
111+
#endif
112+
rt_event_send(perf_thread_event, EVENT_FLAG);
113+
}
114+
}
115+
116+
static void rt_perf_thread_event(void)
117+
{
118+
rt_thread_t thread1 = RT_NULL;
119+
rt_thread_t thread2 = RT_NULL;
120+
121+
perf_thread_event = rt_event_create("perf_thread_event", RT_IPC_FLAG_PRIO);
122+
if (perf_thread_event == RT_NULL)
123+
{
124+
LOG_E("perf_thread_event create failed.");
125+
return;
126+
}
127+
sem1 = rt_sem_create("sem1", 1, RT_IPC_FLAG_FIFO);
128+
129+
thread1 = rt_thread_create("perf_thread_event1", perf_thread_event1, RT_NULL,
130+
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
131+
if (thread1 == RT_NULL)
132+
{
133+
LOG_E("perf_thread_event1 create failed.");
134+
return;
135+
}
136+
137+
thread2 = rt_thread_create("perf_thread_event2", perf_thread_event2, RT_NULL,
138+
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
139+
if (thread2 == RT_NULL)
140+
{
141+
LOG_E("perf_thread_event2 create failed.");
142+
return;
143+
}
144+
145+
rt_thread_startup(thread1);
146+
rt_thread_startup(thread2);
147+
}
148+
149+
static rt_err_t utest_tc_init(void)
150+
{
151+
#ifdef USING_HWTIME_AS_TIME_REF
152+
rt_hwtimerval_t timeout_s;
153+
int ret = RT_EOK;
154+
hw_dev = rt_device_find(UTEST_HWTIMER_DEV_NAME);
155+
if (hw_dev == RT_NULL)
156+
{
157+
ret = RT_ERROR;
158+
LOG_E("hwtimer sample run failed! can't find %s device!", UTEST_HWTIMER_DEV_NAME);
159+
return ret;
160+
}
161+
162+
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
163+
if (ret != RT_EOK)
164+
{
165+
LOG_E("open %s device failed!", UTEST_HWTIMER_DEV_NAME);
166+
return ret;
167+
}
168+
169+
timeout_s.sec = TIME_OUT_TIME; /* s */
170+
timeout_s.usec = 0; /* us */
171+
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(rt_hwtimerval_t)) != sizeof(rt_hwtimerval_t))
172+
{
173+
ret = RT_ERROR;
174+
LOG_E("set timeout value failed");
175+
return ret;
176+
}
177+
#endif
178+
179+
return RT_EOK;
180+
}
181+
182+
static rt_err_t utest_tc_cleanup(void)
183+
{
184+
#ifdef USING_HWTIME_AS_TIME_REF
185+
if (hw_dev) rt_device_close(hw_dev);
186+
#endif
187+
return RT_EOK;
188+
}
189+
190+
static void testcase(void)
191+
{
192+
UTEST_UNIT_RUN(rt_perf_thread_event);
193+
}
194+
195+
UTEST_TC_EXPORT(testcase, "testcase.pref.event", utest_tc_init, utest_tc_cleanup, 10);
196+

0 commit comments

Comments
 (0)