]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/clocks/clock.c
Emulate `clock`, `times`, and `getrusage` using the monotonic clock.
[wasi-libc.git] / libc-bottom-half / clocks / clock.c
1 #define _WASI_EMULATED_PROCESS_CLOCKS
2 #include <time.h>
3 #include <wasi/api.h>
4 #include <common/time.h>
5
6 _Static_assert(
7 CLOCKS_PER_SEC == NSEC_PER_SEC,
8 "This implementation assumes that `clock` is in nanoseconds"
9 );
10
11 // Snapshot of the monotonic clock at the start of the program.
12 static __wasi_timestamp_t start;
13
14 // Use a priority of 10 to run fairly early in the implementation-reserved
15 // constructor priority range.
16 __attribute__((constructor(10)))
17 static void init(void) {
18 (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &start);
19 }
20
21 clock_t __clock(void) {
22 // Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have
23 // an inherent concept of a process. Note that this means we'll incorrectly
24 // include time from other processes, so this function is only declared by
25 // the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
26 __wasi_timestamp_t now = 0;
27 (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
28 return now - start;
29 }
30
31 __attribute__((__weak__, __alias__("__clock")))
32 clock_t clock(void);