Skip to content

Commit 59e41ef

Browse files
ycsinnashif
authored andcommitted
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 <[email protected]> Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 4f4cc4d commit 59e41ef

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

tests/benchmarks/latency_measure/src/timing_sc.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ BENCH_BMEM uint64_t timestamp_overhead;
1919
BENCH_BMEM uint64_t user_timestamp_overhead;
2020
#endif
2121

22+
#define OVERHEAD_CALC_ITER 10
23+
2224
timing_t z_impl_timing_timestamp_get(void)
2325
{
2426
return timing_counter_get();
@@ -37,17 +39,23 @@ static void start_thread_entry(void *p1, void *p2, void *p3)
3739
uint32_t num_iterations = (uint32_t)(uintptr_t)p1;
3840
timing_t start;
3941
timing_t finish;
42+
uint64_t min_cycles = UINT64_MAX;
4043

4144
ARG_UNUSED(p2);
4245
ARG_UNUSED(p3);
4346

44-
start = timing_timestamp_get();
45-
for (uint32_t i = 0; i < num_iterations; i++) {
46-
timing_timestamp_get();
47+
/* Repeat the overhead measurements for a few times to obtain the minimum overhead */
48+
for (int n = 0; n < OVERHEAD_CALC_ITER; n++) {
49+
start = timing_timestamp_get();
50+
for (uint32_t i = 0; i < num_iterations; i++) {
51+
timing_timestamp_get();
52+
}
53+
finish = timing_timestamp_get();
54+
55+
min_cycles = MIN(min_cycles, timing_cycles_get(&start, &finish));
4756
}
48-
finish = timing_timestamp_get();
4957

50-
timestamp.cycles = timing_cycles_get(&start, &finish);
58+
timestamp.cycles = min_cycles;
5159
}
5260

5361
void timestamp_overhead_init(uint32_t num_iterations)

0 commit comments

Comments
 (0)