Skip to content

Commit 80d315e

Browse files
committed
testing/ostest: add hrtimer API functional tests
Add functional tests for the newly added hrtimer APIs, including hrtimer_init(), hrtimer_start(), and hrtimer_cancel(). Signed-off-by: Chengdong Wang <[email protected]>
1 parent 60f5a68 commit 80d315e

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

testing/ostest/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ if(CONFIG_TESTING_OSTEST)
153153
list(APPEND SRCS nxevent.c)
154154
endif()
155155

156+
if(CONFIG_HRTIMER AND CONFIG_BUILD_FLAT)
157+
list(APPEND SRCS hrtimer.c)
158+
endif()
159+
156160
if(CONFIG_BUILD_FLAT)
157161
list(APPEND SRCS wdog.c)
158162
endif()

testing/ostest/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ CSRCS += nxevent.c
150150
endif
151151
endif
152152

153+
ifeq ($(CONFIG_HRTIMER),y)
154+
ifeq ($(CONFIG_BUILD_FLAT),y)
155+
CSRCS += hrtimer.c
156+
endif
157+
endif
158+
153159
ifeq ($(CONFIG_BUILD_FLAT),y)
154160
CSRCS += wdog.c
155161
endif

testing/ostest/hrtimer.c

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/****************************************************************************
2+
* apps/testing/ostest/hrtimer.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <nuttx/hrtimer.h>
29+
30+
#include <stdio.h>
31+
#include <sched.h>
32+
#include <pthread.h>
33+
34+
#include "ostest.h"
35+
36+
/****************************************************************************
37+
* Pre-processor Definitions
38+
****************************************************************************/
39+
40+
#define NSEC_PER_500MS (500 * NSEC_PER_MSEC)
41+
42+
/* Simple assertion macro for hrtimer test cases */
43+
44+
#define HRTIMER_TEST(expr, value) \
45+
do \
46+
{ \
47+
ret = (expr); \
48+
if (ret != (value)) \
49+
{ \
50+
printf("ERROR: hrtimer test failed, line=%d ret=%d\n", \
51+
__LINE__, ret); \
52+
ASSERT(false); \
53+
} \
54+
} \
55+
while (0)
56+
57+
/****************************************************************************
58+
* Private Data
59+
****************************************************************************/
60+
61+
/* High resolution timer instance used for 500ms periodic test */
62+
63+
static hrtimer_t test_hrtimer_500ms;
64+
65+
/* Indicate whether the hrtimer test is still running */
66+
67+
static bool hrtimer_test_running = false;
68+
69+
/****************************************************************************
70+
* Private Functions
71+
****************************************************************************/
72+
73+
/****************************************************************************
74+
* Name: test_hrtimer_callback
75+
*
76+
* Description:
77+
* High resolution timer callback function. This callback verifies
78+
* that the actual expiration interval is exactly 500ms (nanosecond
79+
* precision) and re-arms the timer in absolute mode.
80+
*
81+
* The timer will be cancelled after it fires 15 times.
82+
*
83+
* Input Parameters:
84+
* hrtimer - Pointer to the expired high resolution timer instance
85+
*
86+
* Returned Value:
87+
* None
88+
*
89+
****************************************************************************/
90+
91+
static void test_hrtimer_callback(FAR hrtimer_t *hrtimer)
92+
{
93+
static uint64_t previous = 0;
94+
static uint64_t now = 0;
95+
static uint32_t count = 0;
96+
97+
struct timespec ts;
98+
int ret;
99+
100+
count++;
101+
102+
/* Get current system time */
103+
104+
clock_systime_timespec(&ts);
105+
now = clock_time2nsec(&ts);
106+
107+
/* Skip comparison for the first invocation */
108+
109+
if (previous != 0 && count > 2)
110+
{
111+
/* Verify the timer interval is exactly
112+
* 500ms with nsec resolution
113+
*/
114+
115+
HRTIMER_TEST(now - previous, NSEC_PER_500MS);
116+
}
117+
118+
previous = now;
119+
120+
/* Re-start the timer using absolute expiration time */
121+
122+
ret = hrtimer_start(hrtimer,
123+
hrtimer->expired + NSEC_PER_500MS,
124+
HRTIMER_MODE_ABS);
125+
126+
HRTIMER_TEST(ret, OK);
127+
128+
/* Stop the test after 15 expirations */
129+
130+
if (count >= 10)
131+
{
132+
ret = hrtimer_cancel(&test_hrtimer_500ms);
133+
HRTIMER_TEST(ret, OK);
134+
135+
hrtimer_test_running = false;
136+
}
137+
}
138+
139+
/****************************************************************************
140+
* Public Functions
141+
****************************************************************************/
142+
143+
/****************************************************************************
144+
* Name: hrtimer_test
145+
*
146+
* Description:
147+
* Entry point for high resolution timer functional test.
148+
*
149+
* This test initializes a high resolution timer, starts it with
150+
* a 500ms relative timeout, and verifies that subsequent expirations
151+
* occur precisely at 500ms intervals using absolute re-arming.
152+
*
153+
* Input Parameters:
154+
* None
155+
*
156+
* Returned Value:
157+
* None
158+
*
159+
****************************************************************************/
160+
161+
void hrtimer_test(void)
162+
{
163+
int ret;
164+
165+
hrtimer_test_running = true;
166+
167+
/* Initialize the high resolution timer */
168+
169+
hrtimer_init(&test_hrtimer_500ms,
170+
test_hrtimer_callback,
171+
NULL);
172+
173+
/* Start the timer with an initial 500ms relative timeout */
174+
175+
ret = hrtimer_start(&test_hrtimer_500ms,
176+
NSEC_PER_500MS,
177+
HRTIMER_MODE_REL);
178+
HRTIMER_TEST(ret, OK);
179+
180+
/* Wait until the test completes */
181+
182+
while (hrtimer_test_running)
183+
{
184+
usleep(500 * USEC_PER_MSEC);
185+
}
186+
}

testing/ostest/ostest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,8 @@ int sem_nfreeholders(void);
304304
void nxevent_test(void);
305305
#endif
306306

307+
#if defined(CONFIG_SCHED_EVENTS) && defined(CONFIG_BUILD_FLAT)
308+
void hrtimer_test(void);
309+
#endif
310+
307311
#endif /* __APPS_TESTING_OSTEST_OSTEST_H */

testing/ostest/ostest_main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ static int user_main(int argc, char *argv[])
625625
check_test_memory_usage();
626626
#endif
627627

628+
#if defined(CONFIG_HRTIMER) && defined(CONFIG_BUILD_FLAT)
629+
/* Verify hrtimer */
630+
631+
printf("\nuser_main: hrtimer test\n");
632+
hrtimer_test();
633+
check_test_memory_usage();
634+
#endif
635+
628636
/* Compare memory usage at time ostest_main started until
629637
* user_main exits. These should not be identical, but should
630638
* be similar enough that we can detect any serious OS memory

0 commit comments

Comments
 (0)