Skip to content

Commit

Permalink
tests: posix: nanosleep: round up to the nearest microsecond
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cfriedt authored and carlescufi committed Oct 2, 2020
1 parent 3cdf718 commit 75b9292
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
12 changes: 9 additions & 3 deletions tests/posix/common/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ extern void test_nanosleep_req_is_rem(void);
extern void test_nanosleep_n1_0(void);
extern void test_nanosleep_0_n1(void);
extern void test_nanosleep_n1_n1(void);
extern void test_nanosleep_0_1000000000(void);
extern void test_nanosleep_0_1(void);
extern void test_nanosleep_0_1001(void);
extern void test_nanosleep_0_1000000000(void);
extern void test_nanosleep_0_500000000(void);
extern void test_nanosleep_1_0(void);
extern void test_nanosleep_1_1(void);
extern void test_nanosleep_1_1001(void);

void test_main(void)
{
Expand All @@ -55,9 +58,12 @@ void test_main(void)
ztest_unit_test(test_nanosleep_n1_0),
ztest_unit_test(test_nanosleep_0_n1),
ztest_unit_test(test_nanosleep_n1_n1),
ztest_unit_test(test_nanosleep_0_1000000000),
ztest_unit_test(test_nanosleep_0_1),
ztest_unit_test(test_nanosleep_0_1001),
ztest_unit_test(test_nanosleep_0_500000000),
ztest_unit_test(test_nanosleep_1_0)
ztest_unit_test(test_nanosleep_1_0),
ztest_unit_test(test_nanosleep_1_1),
ztest_unit_test(test_nanosleep_1_1001)
);
ztest_run_test_suite(posix_apis);
}
64 changes: 31 additions & 33 deletions tests/posix/common/src/nanosleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,6 @@
#include <stdint.h>
#include <sys_clock.h>

/* TODO: Upper bounds check when hr timers are available */
#define NSEC_PER_TICK \
(NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
#define NSEC_PER_CYCLE \
(NSEC_PER_SEC / CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC)

/* Specify accepted tolerance. On some Zephyr platforms (e.g. nRF5x) the busy
* wait loop and the system timer are based on different mechanisms and may not
* align perfectly. 1 percent base intolerance is to cover CPU processing in the
* test.
*/
#if CONFIG_NRF_RTC_TIMER
/* High frequency clock used for k_busy_wait may have up to 8% tolerance.
* Additionally, if RC is used for low frequency clock then it has 5% tolerance.
*/
#define TOLERANCE_PPC \
(1 + 8 + (IS_ENABLED(CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC) ? 5 : 0))
#else
#define TOLERANCE_PPC 1
#endif

/** req and rem are both NULL */
void test_nanosleep_NULL_NULL(void)
{
Expand Down Expand Up @@ -199,23 +178,30 @@ static void common(const uint32_t s, uint32_t ns)
zassert_equal(rem.tv_nsec, 0, "actual: %d expected: %d",
rem.tv_nsec, 0);

uint64_t actual_ns = k_cyc_to_ns_ceil64((now - then));
uint64_t exp_ns = (uint64_t)s * NSEC_PER_SEC + ns;
uint64_t tolerance_ns = MAX(NSEC_PER_CYCLE,
(TOLERANCE_PPC * exp_ns) / 100U);
uint64_t tck_ns = k_cyc_to_ns_ceil64((now == then)
? 1U
: (now - then));
int64_t delta_ns = (int64_t)(exp_ns - tck_ns);

zassert_true((delta_ns < 0)
? ((uint64_t)-delta_ns <= tolerance_ns)
: ((uint64_t)delta_ns <= tolerance_ns),
"error %lld beyond tolerance %llu for %llu vs %llu",
delta_ns, tolerance_ns, exp_ns, tck_ns);
/* round up to the nearest microsecond for k_busy_wait() */
exp_ns = ceiling_fraction(exp_ns, NSEC_PER_USEC) * NSEC_PER_USEC;

/* lower bounds check */
zassert_true(actual_ns >= exp_ns,
"actual: %llu expected: %llu", actual_ns, exp_ns);

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

/** sleep for 1ns */
void test_nanosleep_0_1(void)
{
common(0, 1);
}

/** sleep for 1us + 1ns */
void test_nanosleep_0_1001(void)
{
common(0, 1001);
}

/** sleep for 500000000ns */
void test_nanosleep_0_500000000(void)
{
Expand All @@ -227,3 +213,15 @@ void test_nanosleep_1_0(void)
{
common(1, 0);
}

/** sleep for 1s + 1ns */
void test_nanosleep_1_1(void)
{
common(1, 1);
}

/** sleep for 1s + 1us + 1ns */
void test_nanosleep_1_1001(void)
{
common(1, 1001);
}

0 comments on commit 75b9292

Please sign in to comment.