Skip to content

Commit e3b40ba

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): use CLOCK_PROCESS_CPUTIME_ID for actual CPU time
nsi_host_clock_gettime maps CLOCK_MONOTONIC to native_sim's simulated time (which starts at 0 and doesn't advance during CPU-bound loops). CLOCK_PROCESS_CPUTIME_ID measures actual CPU time consumed by the process. It is maintained by the OS kernel per-process clock and should not be redirected by native_sim's user-space time simulation. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent b50c987 commit e3b40ba

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@
2525
* host OS clock, bypassing all simulated-time interception.
2626
*/
2727
#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+
*/
2833
extern int nsi_host_clock_gettime(int clk_id, struct timespec *tp);
2934
static inline uint64_t bench_ns(void)
3035
{
3136
struct timespec ts;
32-
nsi_host_clock_gettime(CLOCK_MONOTONIC, &ts);
37+
nsi_host_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
3338
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3439
}
3540
#else

tests/benchmarks/pid_benchmark/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ 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-
nsi_host_clock_gettime(CLOCK_MONOTONIC, &ts);
30+
nsi_host_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
3131
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3232
}
3333
#else

0 commit comments

Comments
 (0)