Skip to content

Commit 78d72bd

Browse files
tbitcsoz-agent
andcommitted
fix(benchmarks): use bench_ts struct to avoid header dependency issues
Avoid any include of <time.h> or system headers to sidestep Zephyr's wrapper which doesn't define CLOCK_MONOTONIC_RAW on native_sim without CONFIG_POSIX_CLOCK_SELECTION=y. Declare a plain struct {long,long} that matches glibc's 32-bit struct timespec layout and extern-declare clock_gettime. Hardcode clockid 4 (CLOCK_MONOTONIC_RAW). Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent e4b394f commit 78d72bd

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

  • tests/benchmarks

tests/benchmarks/kalman_benchmark/src/main.c

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

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.
16+
/* Real host time via glibc clock_gettime bypassing Zephyr's POSIX override.
17+
* Define our own timespec-compatible struct (Linux 32-bit: long,long = 8 bytes)
18+
* and declare clock_gettime with it. glibc's clock_gettime(4, ...) returns
19+
* CLOCK_MONOTONIC_RAW with nanosecond resolution.
2020
*/
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
21+
struct bench_ts { long tv_sec; long tv_nsec; };
22+
extern int clock_gettime(int, struct bench_ts *);
2723

2824
static inline uint64_t bench_ns(void)
2925
{
30-
struct timespec ts;
26+
struct bench_ts ts;
3127

32-
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
33-
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
28+
clock_gettime(4 /* CLOCK_MONOTONIC_RAW */, &ts);
29+
return (uint64_t)(unsigned long)ts.tv_sec * 1000000000ULL +
30+
(uint64_t)(unsigned long)ts.tv_nsec;
3431
}
3532

3633
LOG_MODULE_REGISTER(kf_bench, LOG_LEVEL_INF);

tests/benchmarks/pid_benchmark/src/main.c

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

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
24+
struct bench_ts { long tv_sec; long tv_nsec; };
25+
extern int clock_gettime(int, struct bench_ts *);
2926

3027
static inline uint64_t bench_ns(void)
3128
{
32-
struct timespec ts;
29+
struct bench_ts ts;
3330

34-
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
35-
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
31+
clock_gettime(4 /* CLOCK_MONOTONIC_RAW */, &ts);
32+
return (uint64_t)(unsigned long)ts.tv_sec * 1000000000ULL +
33+
(uint64_t)(unsigned long)ts.tv_nsec;
3634
}
3735

3836
LOG_MODULE_REGISTER(pid_bench, LOG_LEVEL_INF);

0 commit comments

Comments
 (0)