summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/clocks
diff options
context:
space:
mode:
Diffstat (limited to 'libc-bottom-half/clocks')
-rw-r--r--libc-bottom-half/clocks/clock.c35
-rw-r--r--libc-bottom-half/clocks/getrusage.c28
-rw-r--r--libc-bottom-half/clocks/times.c26
3 files changed, 89 insertions, 0 deletions
diff --git a/libc-bottom-half/clocks/clock.c b/libc-bottom-half/clocks/clock.c
new file mode 100644
index 0000000..6767d73
--- /dev/null
+++ b/libc-bottom-half/clocks/clock.c
@@ -0,0 +1,35 @@
+#define _WASI_EMULATED_PROCESS_CLOCKS
+#include <time.h>
+#include <wasi/api.h>
+#include <common/time.h>
+
+_Static_assert(
+ CLOCKS_PER_SEC == NSEC_PER_SEC,
+ "This implementation assumes that `clock` is in nanoseconds"
+);
+
+// Snapshot of the monotonic clock at the start of the program.
+static __wasi_timestamp_t start;
+
+// Use a priority of 10 to run fairly early in the implementation-reserved
+// constructor priority range.
+__attribute__((constructor(10)))
+static void init(void) {
+ (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &start);
+}
+
+// Define the libc symbol as `__clock` so that we can reliably call it
+// from elsewhere in libc.
+clock_t __clock(void) {
+ // Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have
+ // an inherent concept of a process. Note that this means we'll incorrectly
+ // include time from other processes, so this function is only declared by
+ // the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
+ __wasi_timestamp_t now = 0;
+ (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
+ return now - start;
+}
+
+// Define a user-visible alias as a weak symbol.
+__attribute__((__weak__, __alias__("__clock")))
+clock_t clock(void);
diff --git a/libc-bottom-half/clocks/getrusage.c b/libc-bottom-half/clocks/getrusage.c
new file mode 100644
index 0000000..d0113c5
--- /dev/null
+++ b/libc-bottom-half/clocks/getrusage.c
@@ -0,0 +1,28 @@
+#define _WASI_EMULATED_PROCESS_CLOCKS
+#include <sys/resource.h>
+#include <errno.h>
+#include <time.h>
+#include <wasi/api.h>
+#include <common/time.h>
+
+// `clock` is a weak symbol so that application code can override it.
+// We want to use the function in libc, so use the libc-internal name.
+clock_t __clock(void);
+
+int getrusage(int who, struct rusage *r_usage) {
+ switch (who) {
+ case RUSAGE_SELF: {
+ __wasi_timestamp_t usertime = __clock();
+ *r_usage = (struct rusage) {
+ .ru_utime = timestamp_to_timeval(usertime)
+ };
+ return 0;
+ }
+ case RUSAGE_CHILDREN:
+ *r_usage = (struct rusage) {};
+ return 0;
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+}
diff --git a/libc-bottom-half/clocks/times.c b/libc-bottom-half/clocks/times.c
new file mode 100644
index 0000000..e245569
--- /dev/null
+++ b/libc-bottom-half/clocks/times.c
@@ -0,0 +1,26 @@
+#define _WASI_EMULATED_PROCESS_CLOCKS
+#include <time.h>
+#include <sys/times.h>
+#include <wasi/api.h>
+#include <common/time.h>
+
+_Static_assert(
+ CLOCKS_PER_SEC == NSEC_PER_SEC,
+ "This implementation assumes that `clock` is in nanoseconds"
+);
+
+// `clock` is a weak symbol so that application code can override it.
+// We want to use the function in libc, so use the libc-internal name.
+clock_t __clock(void);
+
+clock_t times(struct tms *buffer) {
+ __wasi_timestamp_t user = __clock();
+ *buffer = (struct tms){
+ .tms_utime = user,
+ .tms_cutime = user
+ };
+
+ __wasi_timestamp_t realtime = 0;
+ (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
+ return realtime;
+}