From 59e41ef830d49816d4a3900215a0fb55da1228d0 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Wed, 18 Sep 2024 13:35:11 +0800 Subject: [PATCH] tests: latency_measure: reduce the chance of cycles underflow Sometimes there's an unusually large cycles for tests that are known to complete with just a few cycles. Upon some testing, I found that it was because the overhead cycles was larger than the cycles taken by tests, causing the cycles to underflow. To workaround that, make sure that the overhead measurement thread runs uninterrupted, repeat the measurement for a few times, and take the minimum value. Signed-off-by: Yong Cong Sin Signed-off-by: Yong Cong Sin --- .../latency_measure/src/timing_sc.c | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/benchmarks/latency_measure/src/timing_sc.c b/tests/benchmarks/latency_measure/src/timing_sc.c index 6b160e97bd3..60d6e9a7204 100644 --- a/tests/benchmarks/latency_measure/src/timing_sc.c +++ b/tests/benchmarks/latency_measure/src/timing_sc.c @@ -19,6 +19,8 @@ BENCH_BMEM uint64_t timestamp_overhead; BENCH_BMEM uint64_t user_timestamp_overhead; #endif +#define OVERHEAD_CALC_ITER 10 + timing_t z_impl_timing_timestamp_get(void) { return timing_counter_get(); @@ -37,17 +39,23 @@ static void start_thread_entry(void *p1, void *p2, void *p3) uint32_t num_iterations = (uint32_t)(uintptr_t)p1; timing_t start; timing_t finish; + uint64_t min_cycles = UINT64_MAX; ARG_UNUSED(p2); ARG_UNUSED(p3); - start = timing_timestamp_get(); - for (uint32_t i = 0; i < num_iterations; i++) { - timing_timestamp_get(); - } - finish = timing_timestamp_get(); + /* Repeat the overhead measurements for a few times to obtain the minimum overhead */ + for (int n = 0; n < OVERHEAD_CALC_ITER; n++) { + start = timing_timestamp_get(); + for (uint32_t i = 0; i < num_iterations; i++) { + timing_timestamp_get(); + } + finish = timing_timestamp_get(); - timestamp.cycles = timing_cycles_get(&start, &finish); + min_cycles = MIN(min_cycles, timing_cycles_get(&start, &finish)); + } + + timestamp.cycles = min_cycles; } void timestamp_overhead_init(uint32_t num_iterations)