Skip to content

Commit e4b394f

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): forward-declare clock_gettime, hardcode CLOCK_MONOTONIC_RAW=4
Avoid Zephyr's time.h wrapper which doesn't expose CLOCK_MONOTONIC_RAW without CONFIG_POSIX_CLOCK_SELECTION=y. Declare the glibc clock_gettime prototype manually and hardcode the Linux clockid (4) so the call resolves to glibc at link time and returns real host time. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent 7fa3959 commit e4b394f

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

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

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.
16+
/* Real host time via glibc clock_gettime (not Zephyr's simulated clock).
17+
* Zephyr's POSIX wrapper doesn't define CLOCK_MONOTONIC_RAW without
18+
* CONFIG_POSIX_CLOCK_SELECTION=y, so hardcode the Linux clockid values.
19+
* CLOCK_MONOTONIC_RAW (4) gives nanosecond-resolution real host time.
2120
*/
22-
#define _POSIX_C_SOURCE 200809L
23-
#include <time.h>
21+
struct timespec; /* forward declare for glibc signature */
22+
extern int clock_gettime(int clk_id, struct timespec *tp);
23+
24+
#ifndef CLOCK_MONOTONIC_RAW
25+
#define CLOCK_MONOTONIC_RAW 4
26+
#endif
27+
2428
static inline uint64_t bench_ns(void)
2529
{
2630
struct timespec ts;
2731

28-
#if defined(CLOCK_MONOTONIC_RAW)
2932
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
30-
#else
31-
clock_gettime(CLOCK_MONOTONIC, &ts);
32-
#endif
3333
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3434
}
3535

tests/benchmarks/pid_benchmark/src/main.c

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

24-
#define _POSIX_C_SOURCE 200809L
25-
#include <time.h>
24+
struct timespec;
25+
extern int clock_gettime(int clk_id, struct timespec *tp);
26+
#ifndef CLOCK_MONOTONIC_RAW
27+
#define CLOCK_MONOTONIC_RAW 4
28+
#endif
29+
2630
static inline uint64_t bench_ns(void)
2731
{
2832
struct timespec ts;
2933

30-
#if defined(CLOCK_MONOTONIC_RAW)
3134
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
32-
#else
33-
clock_gettime(CLOCK_MONOTONIC, &ts);
34-
#endif
3535
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
3636
}
3737

0 commit comments

Comments
 (0)