zig/std/os/time.zig

262 lines
8.5 KiB
Zig
Raw Normal View History

const std = @import("../index.zig");
const builtin = @import("builtin");
const Os = builtin.Os;
const debug = std.debug;
const windows = std.os.windows;
const darwin = std.os.darwin;
const posix = std.os.posix;
pub const epoch = @import("epoch.zig");
/// Sleep for the specified duration
pub fn sleep(seconds: usize, nanoseconds: usize) void {
switch(builtin.os) {
Os.linux, Os.macosx, Os.ios => {
posixSleep(u63(seconds), u63(nanoseconds));
},
Os.windows => {
const milliseconds = seconds * ms_per_s + nanoseconds / (ns_per_s / ms_per_s);
windows.Sleep(windows.DWORD(milliseconds));
},
else => @compileError("Unsupported OS"),
}
}
const u63 = @IntType(false, 63);
pub fn posixSleep(seconds: u63, nanoseconds: u63) void {
var req = posix.timespec {
.tv_sec = seconds,
.tv_nsec = nanoseconds,
};
var rem: posix.timespec = undefined;
while (true) {
const ret_val = posix.nanosleep(&req, &rem);
const err = posix.getErrno(ret_val);
if (err == 0) return;
switch (err) {
posix.EFAULT => unreachable,
posix.EINVAL => {
// Sometimes Darwin returns EINVAL for no reason.
// We treat it as a spurious wakeup.
return;
},
posix.EINTR => {
req = rem;
continue;
},
else => return,
}
}
}
/// Get the posix timestamp, UTC, in seconds
pub fn timestamp() u64 {
return @divFloor(miliTimestamp(), ms_per_s);
}
/// Get the posix timestamp, UTC, in nanoseconds
pub const miliTimestamp = switch(builtin.os) {
Os.windows => miliTimestampWindows,
Os.linux => miliTimestampPosix,
Os.macosx, Os.ios => miliTimestampDarwin,
else => @compileError("Unsupported OS"),
};
fn miliTimestampWindows() u64 {
//FileTime has a granularity of 100 nanoseconds
// and uses the NTFS/Windows epoch
var ft: i64 = undefined;
windows.GetSystemTimeAsFileTime(&ft);
const hns_per_ms = (ns_per_s / 100) / ms_per_s;
const epoch_adj = epoch.windows * ms_per_s;
return u64(@divFloor(ft, hns_per_ms) + epoch_adj);
}
fn miliTimestampDarwin() u64 {
//Sources suggest MacOS 10.12 has support for
// posix clock_gettime.
var tv: darwin.timeval = undefined;
var err = darwin.gettimeofday(&tv, null);
debug.assert(err == 0);
return tv.tv_sec * ms_per_s + ts.tv_usec
* (us_per_s / ms_per_s);
}
fn miliTimestampPosix() u64 {
//From what I can tell there's no reason clock_gettime
// should ever fail for us with CLOCK_REALTIME
var ts: posix.timespec = undefined;
const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts);
debug.assert(err == 0);
const sec_ms = ts.tv_sec * ms_per_s;
const nsec_ms = @divFloor(ts.tv_nsec, ns_per_s / ms_per_s);
return u64(sec_ms) + u64(nsec_ms);
}
/// Divisions of a second
pub const ns_per_s = 1000000000;
pub const us_per_s = 1000000;
pub const ms_per_s = 1000;
pub const cs_per_s = 100;
/// Common time divisions
pub const s_per_min = 60;
pub const s_per_hour = s_per_min * 60;
pub const s_per_day = s_per_hour * 24;
pub const s_per_week = s_per_day * 7;
/// A monotonic high-performance timer.
/// Timer.start() must be called to initialize the struct, which captures
/// the counter frequency on windows and darwin, records the resolution,
/// and gives the user an oportunity to check for the existnece of
/// monotonic clocks without forcing them to check for error on each read.
/// .resolution is in nanoseconds on all platforms but .start_time's meaning
/// depends on the OS. On Windows and Darwin it is a hardware counter
/// value that requires calculation to convert to a meaninful unit.
pub const Timer = struct {
//if we used resolution's value when performing the
// performance counter calc on windows, it would be
// less precise
frequency: switch(builtin.os) {
Os.windows => u64,
Os.macosx, Os.ios => darwin.mach_timebase_info_data,
else => void,
},
resolution: u64,
start_time: u64,
//Initialize the timer structure.
//This gives us an oportunity to grab the counter frequency in windows.
//On Windows: QueryPerformanceCounter will succeed on anything > XP.
//On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
// supported, or if the timespec pointer is out of bounds, which should be
// impossible here barring cosmic rays or other such occurances of
// incredibly bad luck.
//On Darwin: This cannot fail, as far as I am able to tell.
pub fn start() !Timer {
var self: Timer = undefined;
switch(builtin.os) {
Os.windows => {
var freq: i64 = undefined;
var err = windows.QueryPerformanceFrequency(&freq);
if(err == 0) return error.TimerUnsupported;
self.frequency = u64(freq);
self.resolution = @divFloor(ns_per_s, self.frequency);
var start_time: i64 = undefined;
_ = windows.QueryPerformanceCounter(&start_time);
self.start_time = u64(start_time);
},
Os.linux => {
var ts: posix.timespec = undefined;
var result = posix.clock_getres(posix.CLOCK_MONOTONIC, &ts);
switch(posix.getErrno(result)) {
0 => {},
posix.EINVAL => return error.TimerUnsupported,
else => unreachable,
}
self.resolution = u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
_ = posix.clock_gettime(posix.CLOCK_MONOTONIC, &ts);
self.start_time = u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
},
Os.macosx, Os.ios => {
darwin.c.mach_timebase_info(&self.frequency);
self.resolution = @divFloor(self.frequency.numer, self.denom);
self.start_time = darwin.c.mach_absolute_time();
},
else => @compileError("Unsupported OS"),
}
return self;
}
/// Reads the timer value since start or the last reset in nanoseconds
pub fn read(self: &Timer) u64 {
var clock = clockNative() - self.start_time;
return switch(builtin.os) {
Os.windows => @divFloor(clock * ns_per_s, self.frequency),
Os.linux => clock,
Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom),
else => @compileError("Unsupported OS"),
};
}
/// Resets the timer value to 0/now.
pub fn reset(self: &Timer) void
{
self.start_time = clockNative();
}
/// Returns the current value of the timer in nanoseconds, then resets it
pub fn lap(self: &Timer) u64 {
var now = clockNative();
var lap_time = self.read();
self.start_time = now;
return lap_time;
}
const clockNative = switch(builtin.os) {
Os.windows => clockWindows,
Os.linux => clockLinux,
Os.macosx, Os.ios => clockDarwin,
else => @compileError("Unsupported OS"),
};
fn clockWindows() u64 {
var result: i64 = undefined;
var err = windows.QueryPerformanceCounter(&result);
debug.assert(err != 0);
return u64(result);
}
fn clockDarwin() u64 {
var result: u64 = undefined;
darwin.c.mach_absolute_time(&result);
return result;
}
fn clockLinux() u64 {
var ts: posix.timespec = undefined;
var result = posix.clock_gettime(posix.CLOCK_MONOTONIC, &ts);
debug.assert(posix.getErrno(result) == 0);
return u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
}
};
test "os.time.sleep" {
sleep(0, 1);
}
test "os.time.timestamp" {
const ns_per_ms = (ns_per_s / ms_per_s);
const margin = 50;
const time_0 = miliTimestamp();
sleep(0, ns_per_ms);
const time_1 = miliTimestamp();
const interval = time_1 - time_0;
debug.assert(interval > 0 and interval < margin);
}
test "os.time.Timer" {
const ns_per_ms = (ns_per_s / ms_per_s);
const margin = ns_per_ms * 50;
var timer = try Timer.start();
sleep(0, 10 * ns_per_ms);
const time_0 = timer.read();
debug.assert(time_0 > 0 and time_0 < margin);
const time_1 = timer.lap();
debug.assert(time_1 > time_0);
timer.reset();
debug.assert(timer.read() < time_1);
}