Skip to content

Commit 7d8ea99

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): use get_host_us_time() from native_sim timer_model.c
All previous approaches (clock_gettime, syscall, k_uptime, nsi_host_*) returned 0 because native_sim intercepts them all at the binary level and maps them to the simulated Zephyr clock (which doesn't advance during CPU-bound loops). get_host_us_time() is defined in timer_model.c (the native_sim hardware timer model, compiled into the same binary) and calls CLOCK_MONOTONIC_RAW directly from the OS timer model layer, BEFORE any Zephyr-level intercept. It is the function native_sim itself uses to obtain real host time for real-time mode synchronization -- it cannot be intercepted. Returns µs * 1000 for nanosecond output. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent e3b40ba commit 7d8ea99

2 files changed

Lines changed: 8 additions & 19 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,15 @@
1919
* Zephyr kernel ticks which do NOT advance during CPU-bound loops.
2020
*/
2121
#ifdef CONFIG_NATIVE_SIMULATOR
22-
/* native_sim replaces clock_gettime() AND syscall() with versions that
23-
* return simulated Zephyr time (stuck at 0 during CPU-bound loops).
24-
* nsi_host_clock_gettime() is native_sim's own escape hatch to the real
25-
* host OS clock, bypassing all simulated-time interception.
22+
/* native_sim's get_host_us_time() (defined in timer_model.c, compiled into
23+
* the same native_sim binary) calls CLOCK_MONOTONIC_RAW directly from the
24+
* OS timer model — it is the real host time in µs and is not intercepted
25+
* by any simulated-time override.
2626
*/
27-
#include <time.h>
28-
/* nsi_host_clock_gettime maps CLOCK_MONOTONIC to simulated time.
29-
* CLOCK_PROCESS_CPUTIME_ID measures actual CPU time consumed by this
30-
* process — it is OS-maintained, not interceptable by user-space time
31-
* simulation, and gives the true benchmark execution time.
32-
*/
33-
extern int nsi_host_clock_gettime(int clk_id, struct timespec *tp);
27+
extern uint64_t get_host_us_time(void);
3428
static inline uint64_t bench_ns(void)
3529
{
36-
struct timespec ts;
37-
nsi_host_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
38-
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
30+
return get_host_us_time() * 1000ULL;
3931
}
4032
#else
4133
/* On real hardware fall back to Zephyr timing API */

tests/benchmarks/pid_benchmark/src/main.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@
2222
#include "arbiter_model.h"
2323

2424
#ifdef CONFIG_NATIVE_SIMULATOR
25-
#include <time.h>
26-
extern int nsi_host_clock_gettime(int clk_id, struct timespec *tp);
25+
extern uint64_t get_host_us_time(void);
2726
static inline uint64_t bench_ns(void)
2827
{
29-
struct timespec ts;
30-
nsi_host_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
31-
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
28+
return get_host_us_time() * 1000ULL;
3229
}
3330
#else
3431
#include <zephyr/timing/timing.h>

0 commit comments

Comments
 (0)