Skip to content

Commit 69ac0dc

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): bypass native_sim clock intercept via raw syscall
native_sim replaces libc's clock_gettime() to return simulated Zephyr time. Using syscall(__NR_clock_gettime, ...) bypasses the intercept entirely and reads CLOCK_MONOTONIC directly from the Linux kernel, giving real ns-precision wall-clock timing even during CPU-bound loops with no timer interrupts. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent aa885cd commit 69ac0dc

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +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.
26+
*/
2227
#include <time.h>
28+
#include <sys/syscall.h>
2329
static inline uint64_t bench_ns(void)
2430
{
2531
struct timespec ts;
26-
clock_gettime(CLOCK_MONOTONIC, &ts);
32+
syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts);
2733
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
2834
}
2935
#else

tests/benchmarks/pid_benchmark/src/main.c

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

2424
#ifdef CONFIG_NATIVE_SIMULATOR
2525
#include <time.h>
26+
#include <sys/syscall.h>
2627
static inline uint64_t bench_ns(void)
2728
{
2829
struct timespec ts;
29-
clock_gettime(CLOCK_MONOTONIC, &ts);
30+
syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts);
3031
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3132
}
3233
#else

0 commit comments

Comments
 (0)