From e69f9e902b354a94a37672296af41bcb4a0e5ef4 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Thu, 29 Jan 2026 16:08:24 +0800 Subject: [PATCH] sched/hrtimer: Fix test in QEMU. This commit fixed the hrtimer test. In a QEMU environment, the hrtimer latency can be arbitrary because vCPUs can be preempted. We can not assert the latency. we can only issue alerts for excessively high latency. Signed-off-by: ouyangxiangzhen --- testing/ostest/hrtimer.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/testing/ostest/hrtimer.c b/testing/ostest/hrtimer.c index 585c6f30a..d82cf171a 100644 --- a/testing/ostest/hrtimer.c +++ b/testing/ostest/hrtimer.c @@ -100,9 +100,9 @@ static uint64_t test_hrtimer_callback(FAR const hrtimer_t *hrtimer, uint64_t expired) { struct timespec ts; - uint32_t diff; + int64_t diff; uint64_t now; - int ret; + int ret; FAR struct hrtimer_test_s *test = (FAR struct hrtimer_test_s *)hrtimer; @@ -116,18 +116,20 @@ test_hrtimer_callback(FAR const hrtimer_t *hrtimer, uint64_t expired) clock_systime_timespec(&ts); now = clock_time2nsec(&ts); - /* Skip comparison for first two invocations */ + /* Verify the timer interval is exactly + * 500ms with nsec resolution + */ - if (test->count > 2) + diff = now - expired; + + /* Ensure the time diff is valid. */ + + ASSERT(diff >= 0); + + if (diff > HRTIMER_TEST_MARGIN) { - /* Verify the timer interval is exactly - * 500ms with nsec resolution - */ - - diff = (uint32_t)(now - expired); - - ASSERT(NSEC_PER_50MS < diff + HRTIMER_TEST_MARGIN); - ASSERT(NSEC_PER_50MS > diff - HRTIMER_TEST_MARGIN); + printf("hrtimer_test: warning diff=%" PRIu64 " > %" PRIu64 "\n", + diff, HRTIMER_TEST_MARGIN); } test->timestamp = now;