From 2f6b045fb13b6a49e5185fa0f4006bef1dacd0d7 Mon Sep 17 00:00:00 2001 From: data-man Date: Fri, 6 Dec 2019 06:29:23 +0500 Subject: [PATCH 1/2] Add std.os.getrusage --- lib/std/os.zig | 11 +++++++++++ lib/std/os/bits/linux.zig | 23 +++++++++++++++++++++++ lib/std/os/linux.zig | 4 ++++ 3 files changed, 38 insertions(+) diff --git a/lib/std/os.zig b/lib/std/os.zig index 878d1a57e7..17945b9ce4 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3325,3 +3325,14 @@ pub fn memfd_create(name: []const u8, flags: u32) !fd_t { const name_t = try toMemFdPath(name); return memfd_createC(&name_t, flags); } + +pub fn getrusage(who: i32) rusage { + var result: rusage = undefined; + const rc = system.getrusage(who, &result); + switch (errno(rc)) { + 0 => return result, + EINVAL => unreachable, + EFAULT => unreachable, + else => unreachable, + } +} diff --git a/lib/std/os/bits/linux.zig b/lib/std/os/bits/linux.zig index 3fb160e3ec..6fa8bc1f36 100644 --- a/lib/std/os/bits/linux.zig +++ b/lib/std/os/bits/linux.zig @@ -1453,3 +1453,26 @@ pub const MFD_HUGE_512MB = HUGETLB_FLAG_ENCODE_512MB; pub const MFD_HUGE_1GB = HUGETLB_FLAG_ENCODE_1GB; pub const MFD_HUGE_2GB = HUGETLB_FLAG_ENCODE_2GB; pub const MFD_HUGE_16GB = HUGETLB_FLAG_ENCODE_16GB; + +pub const RUSAGE_SELF = 0; +pub const RUSAGE_CHILDREN = -1; +pub const RUSAGE_THREAD = 1; + +pub const rusage = extern struct { + utime: timeval, + stime: timeval, + maxrss: isize, + ix_rss: isize, + idrss: isize, + isrss: isize, + minflt: isize, + majflt: isize, + nswap: isize, + inblock: isize, + oublock: isize, + msgsnd: isize, + msgrcv: isize, + nsignals: isize, + nvcsw: isize, + nivcsw: isize, +}; diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index b6030fe2fe..efba40bb8d 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -1114,6 +1114,10 @@ pub fn memfd_create(name: [*:0]const u8, flags: u32) usize { return syscall2(SYS_memfd_create, @ptrToInt(name), flags); } +pub fn getrusage(who: i32, usage: *rusage) usize { + return syscall2(SYS_getrusage, @bitCast(usize, @as(isize, who)), @ptrToInt(usage)); +} + test "" { if (builtin.os == .linux) { _ = @import("linux/test.zig"); From fee9318b1756054ff7050bae72c87a1b63a6f968 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 5 Jan 2020 16:57:14 -0500 Subject: [PATCH 2/2] std.os.getrusage: add C extern fn and reserved field * add reserved field to match musl struct definition so that it will work with musl libc. * add libc getrusage so that it will work with libc what's not done in this branch is: * test coverage. See #1629, which should also aim to provide general test coverage for the std lib. * rusage struct bits for non-linux operating systems --- lib/std/c.zig | 1 + lib/std/os/bits/linux.zig | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/std/c.zig b/lib/std/c.zig index 684758286b..45d0b4db03 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -109,6 +109,7 @@ pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int; pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int; pub extern "c" fn rmdir(path: [*:0]const u8) c_int; pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8; +pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int; pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; diff --git a/lib/std/os/bits/linux.zig b/lib/std/os/bits/linux.zig index 6fa8bc1f36..678868a2c3 100644 --- a/lib/std/os/bits/linux.zig +++ b/lib/std/os/bits/linux.zig @@ -1475,4 +1475,5 @@ pub const rusage = extern struct { nsignals: isize, nvcsw: isize, nivcsw: isize, + __reserved: [16]isize = [1]isize{0} ** 16, };