Skip to content

Commit 9aa63c3

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): switch to k_uptime_get_32 for real timing on native_sim
timing_counter_get() on native_sim measures simulated Zephyr clock ticks which do NOT advance during CPU-bound loops — giving 0 ns for all timings. k_uptime_get_32() uses the real host CLOCK_MONOTONIC and returns real milliseconds. Total = (t1_ms - t0_ms) * 1000000 ns gives ms-resolution results. For 100k iterations this is sufficient: 10ms total = 100 ns/tick. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent 18ababb commit 9aa63c3

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ int main(void)
117117
LOG_INF("Iterations: %d (warmup: %d)", BENCH_ITERATIONS,
118118
WARMUP_ITERATIONS);
119119

120-
timing_init();
121-
timing_start();
120+
/* Use k_uptime_get_32() (real-time ms counter) instead of the
121+
* timing API: native_sim's timing_counter_get() measures simulated
122+
* Zephyr clock ticks which do NOT advance during CPU-bound loops.
123+
*/
122124

123125
/* ----- Hand-coded Kalman benchmark ----- */
124126
struct hand_kf hkf;
@@ -140,7 +142,7 @@ int main(void)
140142
true_val = 0;
141143
prng_seed = 42;
142144

143-
timing_t t0 = timing_counter_get();
145+
uint32_t t0_ms = k_uptime_get_32();
144146

145147
for (int i = 0; i < BENCH_ITERATIONS; i++) {
146148
if (i < 600) {
@@ -149,8 +151,8 @@ int main(void)
149151
hand_kf_tick(&hkf, true_val + prng_noise(3000));
150152
}
151153

152-
timing_t t1 = timing_counter_get();
153-
uint64_t hand_ns = timing_cycles_to_ns(timing_cycles_get(&t0, &t1));
154+
uint32_t t1_ms = k_uptime_get_32();
155+
uint64_t hand_ns = (uint64_t)(t1_ms - t0_ms) * 1000000ULL;
154156
uint64_t hand_per_tick = hand_ns / BENCH_ITERATIONS;
155157

156158
LOG_INF("--- Hand-coded Kalman ---");
@@ -197,7 +199,7 @@ int main(void)
197199
true_val = 0;
198200
prng_seed = 42;
199201

200-
timing_t t2 = timing_counter_get();
202+
uint32_t t2_ms = k_uptime_get_32();
201203

202204
for (int i = 0; i < BENCH_ITERATIONS; i++) {
203205
if (i < 600) {
@@ -217,8 +219,8 @@ int main(void)
217219
ARBITER_eval(&ARBITER_generated_model, &snap, &result, NULL);
218220
}
219221

220-
timing_t t3 = timing_counter_get();
221-
uint64_t ARBITER_ns = timing_cycles_to_ns(timing_cycles_get(&t2, &t3));
222+
uint32_t t3_ms = k_uptime_get_32();
223+
uint64_t ARBITER_ns = (uint64_t)(t3_ms - t2_ms) * 1000000ULL;
222224
uint64_t ARBITER_per_tick = ARBITER_ns / BENCH_ITERATIONS;
223225

224226
LOG_INF("--- arbiter Engine Kalman ---");
@@ -247,7 +249,7 @@ int main(void)
247249
(unsigned)(sizeof(struct ARBITER_ctx) - sizeof(struct hand_kf)));
248250
LOG_INF(" ROM: compare .elf sizes (see README)");
249251

250-
timing_stop();
252+
/* timing_stop() not needed — we're using k_uptime, not timing API */
251253

252254
return 0;
253255
}

tests/benchmarks/pid_benchmark/src/main.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ int main(void)
125125
LOG_INF("Iterations: %d (warmup: %d)", BENCH_ITERATIONS,
126126
WARMUP_ITERATIONS);
127127

128-
timing_init();
129-
timing_start();
130-
131128
/* ----- Hand-coded PID benchmark ----- */
132129
struct hand_pid hpid;
133130

@@ -144,15 +141,15 @@ int main(void)
144141
hand_pid_init(&hpid);
145142
plant_h = 0;
146143

147-
timing_t t0 = timing_counter_get();
144+
uint32_t t0_ms = k_uptime_get_32();
148145

149146
for (int i = 0; i < BENCH_ITERATIONS; i++) {
150147
hand_pid_tick(&hpid, 10000, plant_h);
151148
plant_h += hpid.output / 10;
152149
}
153150

154-
timing_t t1 = timing_counter_get();
155-
uint64_t hand_ns = timing_cycles_to_ns(timing_cycles_get(&t0, &t1));
151+
uint32_t t1_ms = k_uptime_get_32();
152+
uint64_t hand_ns = (uint64_t)(t1_ms - t0_ms) * 1000000ULL;
156153
uint64_t hand_per_tick_ns = hand_ns / BENCH_ITERATIONS;
157154

158155
LOG_INF("--- Hand-coded PID ---");
@@ -201,7 +198,7 @@ int main(void)
201198
ARBITER_set_u32(&ARBITER_ctx, F_DT_MS, 10);
202199
plant_z = 0;
203200

204-
timing_t t2 = timing_counter_get();
201+
uint32_t t2_ms = k_uptime_get_32();
205202

206203
for (int i = 0; i < BENCH_ITERATIONS; i++) {
207204
ARBITER_set_i32(&ARBITER_ctx, F_SETPOINT, 10000);
@@ -217,8 +214,8 @@ int main(void)
217214
plant_z += ARBITER_ctx.fact_values[F_OUTPUT].value / 10;
218215
}
219216

220-
timing_t t3 = timing_counter_get();
221-
uint64_t ARBITER_ns = timing_cycles_to_ns(timing_cycles_get(&t2, &t3));
217+
uint32_t t3_ms = k_uptime_get_32();
218+
uint64_t ARBITER_ns = (uint64_t)(t3_ms - t2_ms) * 1000000ULL;
222219
uint64_t ARBITER_per_tick_ns = ARBITER_ns / BENCH_ITERATIONS;
223220

224221
LOG_INF("--- arbiter Engine PID ---");
@@ -254,7 +251,7 @@ int main(void)
254251
LOG_INF(" shell inspection, and runtime watchdog integration.");
255252
LOG_INF(" Hand-coded adds: nothing — it's just math.");
256253

257-
timing_stop();
254+
/* timing_stop() not needed — using k_uptime, not timing API */
258255

259256
return 0;
260257
}

0 commit comments

Comments
 (0)