Skip to content

Commit 75b9292

Browse files
cfriedtcarlescufi
authored andcommitted
tests: posix: nanosleep: round up to the nearest microsecond
Here, we include some addtional tests for durations that have sub-microsecond components. 1ns => k_busy_wait(0). Round to 1us. 1us + 1ns => k_busy_wait(1us). Round to 2us. 1s + 1ns => k_busy_wait(1000000us). Round to 1000001us. 1s + 1us + 1ns => k_busy_wait(1000001us). Round to 1000002us. Fixes #28483 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 3cdf718 commit 75b9292

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

tests/posix/common/src/main.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ extern void test_nanosleep_req_is_rem(void);
2727
extern void test_nanosleep_n1_0(void);
2828
extern void test_nanosleep_0_n1(void);
2929
extern void test_nanosleep_n1_n1(void);
30-
extern void test_nanosleep_0_1000000000(void);
3130
extern void test_nanosleep_0_1(void);
31+
extern void test_nanosleep_0_1001(void);
32+
extern void test_nanosleep_0_1000000000(void);
3233
extern void test_nanosleep_0_500000000(void);
3334
extern void test_nanosleep_1_0(void);
35+
extern void test_nanosleep_1_1(void);
36+
extern void test_nanosleep_1_1001(void);
3437

3538
void test_main(void)
3639
{
@@ -55,9 +58,12 @@ void test_main(void)
5558
ztest_unit_test(test_nanosleep_n1_0),
5659
ztest_unit_test(test_nanosleep_0_n1),
5760
ztest_unit_test(test_nanosleep_n1_n1),
58-
ztest_unit_test(test_nanosleep_0_1000000000),
61+
ztest_unit_test(test_nanosleep_0_1),
62+
ztest_unit_test(test_nanosleep_0_1001),
5963
ztest_unit_test(test_nanosleep_0_500000000),
60-
ztest_unit_test(test_nanosleep_1_0)
64+
ztest_unit_test(test_nanosleep_1_0),
65+
ztest_unit_test(test_nanosleep_1_1),
66+
ztest_unit_test(test_nanosleep_1_1001)
6167
);
6268
ztest_run_test_suite(posix_apis);
6369
}

tests/posix/common/src/nanosleep.c

+31-33
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,6 @@
1010
#include <stdint.h>
1111
#include <sys_clock.h>
1212

13-
/* TODO: Upper bounds check when hr timers are available */
14-
#define NSEC_PER_TICK \
15-
(NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
16-
#define NSEC_PER_CYCLE \
17-
(NSEC_PER_SEC / CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC)
18-
19-
/* Specify accepted tolerance. On some Zephyr platforms (e.g. nRF5x) the busy
20-
* wait loop and the system timer are based on different mechanisms and may not
21-
* align perfectly. 1 percent base intolerance is to cover CPU processing in the
22-
* test.
23-
*/
24-
#if CONFIG_NRF_RTC_TIMER
25-
/* High frequency clock used for k_busy_wait may have up to 8% tolerance.
26-
* Additionally, if RC is used for low frequency clock then it has 5% tolerance.
27-
*/
28-
#define TOLERANCE_PPC \
29-
(1 + 8 + (IS_ENABLED(CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC) ? 5 : 0))
30-
#else
31-
#define TOLERANCE_PPC 1
32-
#endif
33-
3413
/** req and rem are both NULL */
3514
void test_nanosleep_NULL_NULL(void)
3615
{
@@ -199,23 +178,30 @@ static void common(const uint32_t s, uint32_t ns)
199178
zassert_equal(rem.tv_nsec, 0, "actual: %d expected: %d",
200179
rem.tv_nsec, 0);
201180

181+
uint64_t actual_ns = k_cyc_to_ns_ceil64((now - then));
202182
uint64_t exp_ns = (uint64_t)s * NSEC_PER_SEC + ns;
203-
uint64_t tolerance_ns = MAX(NSEC_PER_CYCLE,
204-
(TOLERANCE_PPC * exp_ns) / 100U);
205-
uint64_t tck_ns = k_cyc_to_ns_ceil64((now == then)
206-
? 1U
207-
: (now - then));
208-
int64_t delta_ns = (int64_t)(exp_ns - tck_ns);
209-
210-
zassert_true((delta_ns < 0)
211-
? ((uint64_t)-delta_ns <= tolerance_ns)
212-
: ((uint64_t)delta_ns <= tolerance_ns),
213-
"error %lld beyond tolerance %llu for %llu vs %llu",
214-
delta_ns, tolerance_ns, exp_ns, tck_ns);
183+
/* round up to the nearest microsecond for k_busy_wait() */
184+
exp_ns = ceiling_fraction(exp_ns, NSEC_PER_USEC) * NSEC_PER_USEC;
185+
186+
/* lower bounds check */
187+
zassert_true(actual_ns >= exp_ns,
188+
"actual: %llu expected: %llu", actual_ns, exp_ns);
215189

216190
/* TODO: Upper bounds check when hr timers are available */
217191
}
218192

193+
/** sleep for 1ns */
194+
void test_nanosleep_0_1(void)
195+
{
196+
common(0, 1);
197+
}
198+
199+
/** sleep for 1us + 1ns */
200+
void test_nanosleep_0_1001(void)
201+
{
202+
common(0, 1001);
203+
}
204+
219205
/** sleep for 500000000ns */
220206
void test_nanosleep_0_500000000(void)
221207
{
@@ -227,3 +213,15 @@ void test_nanosleep_1_0(void)
227213
{
228214
common(1, 0);
229215
}
216+
217+
/** sleep for 1s + 1ns */
218+
void test_nanosleep_1_1(void)
219+
{
220+
common(1, 1);
221+
}
222+
223+
/** sleep for 1s + 1us + 1ns */
224+
void test_nanosleep_1_1001(void)
225+
{
226+
common(1, 1001);
227+
}

0 commit comments

Comments
 (0)