Skip to content

Commit c4fae7c

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): call clock_gettime directly without ifdef guard
All previous timing attempts used wrong CONFIG guards that were never true, causing the code to fall through to k_uptime_get() (simulated clock = 0). Zephyr only overrides clock_gettime with CONFIG_POSIX_CLOCK_SELECTION=y. The benchmark prj.conf does NOT set this, so clock_gettime() resolves to glibc which returns real host time with ns precision. Also restoring %llu for PID benchmark (it was already using it). Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent 5049ef4 commit c4fae7c

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@
1313
#include <arbiter/arbiter.h>
1414
#include "arbiter_model.h"
1515

16-
/* Use k_uptime_get() which returns simulated time in ms.
17-
* With CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=y the simulated clock
18-
* is tied to the real host clock, so k_uptime_get() gives true elapsed ms.
19-
* Multiply by 1,000,000 to convert to nanoseconds.
16+
/* Use CLOCK_MONOTONIC_RAW via the real glibc clock_gettime.
17+
* On native_sim, Zephyr only overrides clock_gettime when
18+
* CONFIG_POSIX_CLOCK_SELECTION=y. With our minimal prj.conf that
19+
* flag is off, so clock_gettime resolves to glibc and returns real
20+
* host time with nanosecond resolution.
2021
*/
22+
#include <time.h>
2123
static inline uint64_t bench_ns(void)
2224
{
23-
return (uint64_t)k_uptime_get() * 1000000ULL;
25+
struct timespec ts;
26+
27+
#if defined(CLOCK_MONOTONIC_RAW)
28+
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
29+
#else
30+
clock_gettime(CLOCK_MONOTONIC, &ts);
31+
#endif
32+
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
2433
}
2534

2635
LOG_MODULE_REGISTER(kf_bench, LOG_LEVEL_INF);
@@ -160,9 +169,8 @@ int main(void)
160169
uint64_t hand_per_tick = hand_ns / BENCH_ITERATIONS;
161170

162171
LOG_INF("--- Hand-coded Kalman ---");
163-
/* Print in ms (uint32) to avoid any %llu 32-bit printing issues */
164172
LOG_INF(" Total: %u ms (%u ns/tick)",
165-
(uint32_t)(hand_ns / 1000000ULL), (uint32_t)hand_per_tick);
173+
(unsigned)(hand_ns / 1000000ULL), (unsigned)hand_per_tick);
166174
LOG_INF(" RAM (struct): %u bytes", (unsigned)sizeof(struct hand_kf));
167175
LOG_INF(" Final estimate: %d (true: %d)", hkf.x_est, true_val);
168176

@@ -231,7 +239,7 @@ int main(void)
231239

232240
LOG_INF("--- arbiter Engine Kalman ---");
233241
LOG_INF(" Total: %u ms (%u ns/tick)",
234-
(uint32_t)(ARBITER_ns / 1000000ULL), (uint32_t)ARBITER_per_tick);
242+
(unsigned)(ARBITER_ns / 1000000ULL), (unsigned)ARBITER_per_tick);
235243
LOG_INF(" RAM (ctx): %u bytes", (unsigned)sizeof(struct ARBITER_ctx));
236244
LOG_INF(" Final estimate: %d (true: %d)",
237245
zctx.fact_values[F_X_EST].value, true_val);

tests/benchmarks/pid_benchmark/src/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@
2121
#include <arbiter/arbiter.h>
2222
#include "arbiter_model.h"
2323

24+
#include <time.h>
2425
static inline uint64_t bench_ns(void)
2526
{
26-
return (uint64_t)k_uptime_get() * 1000000ULL;
27+
struct timespec ts;
28+
29+
#if defined(CLOCK_MONOTONIC_RAW)
30+
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
31+
#else
32+
clock_gettime(CLOCK_MONOTONIC, &ts);
33+
#endif
34+
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
2735
}
2836

2937
LOG_MODULE_REGISTER(pid_bench, LOG_LEVEL_INF);

0 commit comments

Comments
 (0)