From 0ce05fa621ba95ba3ce98f99cb14fec549409069 Mon Sep 17 00:00:00 2001 From: Shritesh Bhattarai Date: Tue, 30 Apr 2019 20:06:39 -0500 Subject: [PATCH 1/2] wasi: import clock and timestamp function/types --- std/os/wasi/core.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/std/os/wasi/core.zig b/std/os/wasi/core.zig index b296b83c70..77fd831f81 100644 --- a/std/os/wasi/core.zig +++ b/std/os/wasi/core.zig @@ -1,18 +1,28 @@ +pub const clockid_t = u32; pub const errno_t = u16; pub const exitcode_t = u32; pub const fd_t = u32; pub const signal_t = u8; +pub const timestamp_t = u64; pub const ciovec_t = extern struct { buf: [*]const u8, buf_len: usize, }; +pub const CLOCK_REALTIME = 0; +pub const CLOCK_MONOTONIC = 1; +pub const CLOCK_PROCESS_CPUTIME_ID = 2; +pub const CLOCK_THREAD_CPUTIME_ID = 3; + pub const SIGABRT: signal_t = 6; pub extern "wasi_unstable" fn args_get(argv: [*][*]u8, argv_buf: [*]u8) errno_t; pub extern "wasi_unstable" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t; +pub extern "wasi_unstable" fn clock_res_get(clock_id: clockid_t, resolution: *timestamp_t) errno_t; +pub extern "wasi_unstable" fn clock_time_get(clock_id: clockid_t, precision: timestamp_t, timestamp: *timestamp_t) errno_t; + pub extern "wasi_unstable" fn environ_get(environ: [*]?[*]u8, environ_buf: [*]u8) errno_t; pub extern "wasi_unstable" fn environ_sizes_get(environ_count: *usize, environ_buf_size: *usize) errno_t; From d395ed2d403aa0ff57c493052e3675de23292424 Mon Sep 17 00:00:00 2001 From: Shritesh Bhattarai Date: Tue, 30 Apr 2019 20:43:43 -0500 Subject: [PATCH 2/2] wasi: implement timestamp --- std/os/time.zig | 13 +++++++++++++ std/os/wasi/core.zig | 8 ++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/std/os/time.zig b/std/os/time.zig index 66ceedb1b6..d742c5d03b 100644 --- a/std/os/time.zig +++ b/std/os/time.zig @@ -7,6 +7,7 @@ const testing = std.testing; const windows = std.os.windows; const linux = std.os.linux; const darwin = std.os.darwin; +const wasi = std.os.wasi; const posix = std.os.posix; pub const epoch = @import("epoch.zig"); @@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) { Os.windows => milliTimestampWindows, Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix, Os.macosx, Os.ios => milliTimestampDarwin, + Os.wasi => milliTimestampWasi, else => @compileError("Unsupported OS"), }; +fn milliTimestampWasi() u64 { + var ns: wasi.timestamp_t = undefined; + + // TODO: Verify that precision is ignored + const err = wasi.clock_time_get(wasi.CLOCK_REALTIME, 1, &ns); + debug.assert(err == wasi.ESUCCESS); + + const ns_per_ms = 1000; + return @divFloor(ns, ns_per_ms); +} + fn milliTimestampWindows() u64 { //FileTime has a granularity of 100 nanoseconds // and uses the NTFS/Windows epoch diff --git a/std/os/wasi/core.zig b/std/os/wasi/core.zig index 77fd831f81..23a0c7ba65 100644 --- a/std/os/wasi/core.zig +++ b/std/os/wasi/core.zig @@ -10,10 +10,10 @@ pub const ciovec_t = extern struct { buf_len: usize, }; -pub const CLOCK_REALTIME = 0; -pub const CLOCK_MONOTONIC = 1; -pub const CLOCK_PROCESS_CPUTIME_ID = 2; -pub const CLOCK_THREAD_CPUTIME_ID = 3; +pub const CLOCK_REALTIME: clockid_t = 0; +pub const CLOCK_MONOTONIC: clockid_t = 1; +pub const CLOCK_PROCESS_CPUTIME_ID: clockid_t = 2; +pub const CLOCK_THREAD_CPUTIME_ID: clockid_t = 3; pub const SIGABRT: signal_t = 6;