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 <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
This commit is contained in:
Yong Cong Sin 2024-09-18 13:35:11 +08:00 committed by Anas Nashif
parent 4f4cc4de08
commit 59e41ef830
1 changed files with 14 additions and 6 deletions

View File

@ -19,6 +19,8 @@ BENCH_BMEM uint64_t timestamp_overhead;
BENCH_BMEM uint64_t user_timestamp_overhead; BENCH_BMEM uint64_t user_timestamp_overhead;
#endif #endif
#define OVERHEAD_CALC_ITER 10
timing_t z_impl_timing_timestamp_get(void) timing_t z_impl_timing_timestamp_get(void)
{ {
return timing_counter_get(); 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; uint32_t num_iterations = (uint32_t)(uintptr_t)p1;
timing_t start; timing_t start;
timing_t finish; timing_t finish;
uint64_t min_cycles = UINT64_MAX;
ARG_UNUSED(p2); ARG_UNUSED(p2);
ARG_UNUSED(p3); ARG_UNUSED(p3);
/* 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(); start = timing_timestamp_get();
for (uint32_t i = 0; i < num_iterations; i++) { for (uint32_t i = 0; i < num_iterations; i++) {
timing_timestamp_get(); timing_timestamp_get();
} }
finish = 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) void timestamp_overhead_init(uint32_t num_iterations)