Skip to content

Commit b50c987

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): use nsi_host_clock_gettime to bypass all native_sim time interception
native_sim intercepts both clock_gettime() AND the syscall() C function to return simulated Zephyr time. The raw syscall approach also failed because native_sim overrides the syscall() C function itself. nsi_host_clock_gettime() is native_sim's own built-in escape hatch: it calls the real host OS clock_gettime() via the original libc function that was saved before any intercept. This is the correct and documented way to get real host time from within native_sim firmware code. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent 69ac0dc commit b50c987

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
* Zephyr kernel ticks which do NOT advance during CPU-bound loops.
2020
*/
2121
#ifdef CONFIG_NATIVE_SIMULATOR
22-
/* native_sim replaces the C-library's clock_gettime() with one that returns
23-
* simulated Zephyr time (stuck at 0 during CPU-bound loops). A raw Linux
24-
* syscall bypasses the intercept and reads the real CLOCK_MONOTONIC directly
25-
* from the kernel, giving true nanosecond-resolution wall-clock timing.
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.
2626
*/
2727
#include <time.h>
28-
#include <sys/syscall.h>
28+
extern int nsi_host_clock_gettime(int clk_id, struct timespec *tp);
2929
static inline uint64_t bench_ns(void)
3030
{
3131
struct timespec ts;
32-
syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts);
32+
nsi_host_clock_gettime(CLOCK_MONOTONIC, &ts);
3333
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3434
}
3535
#else

tests/benchmarks/pid_benchmark/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
#ifdef CONFIG_NATIVE_SIMULATOR
2525
#include <time.h>
26-
#include <sys/syscall.h>
26+
extern int nsi_host_clock_gettime(int clk_id, struct timespec *tp);
2727
static inline uint64_t bench_ns(void)
2828
{
2929
struct timespec ts;
30-
syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts);
30+
nsi_host_clock_gettime(CLOCK_MONOTONIC, &ts);
3131
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3232
}
3333
#else

0 commit comments

Comments
 (0)