implement clock_gettime, clock_getres, clock_settime#1100
implement clock_gettime, clock_getres, clock_settime#1100kevinwern wants to merge 1 commit intonix-rust:masterfrom
Conversation
14fce4c to
0b16e15
Compare
src/time.rs
Outdated
| } | ||
|
|
||
| pub fn clock_getres(clk_id: ClockId) -> Result<TimeSpec> { | ||
| let mut c_time = libc::timespec {tv_sec: 0, tv_nsec: 0}; |
There was a problem hiding this comment.
Please use MaybeUninit instead of zero-initializing. That will cause the test to fail for now, but they'll pass as soon as #1096 is fixed.
There was a problem hiding this comment.
MaybeUninit is the correct thing to do here, not zero-initialization.
test/test_time.rs
Outdated
| pub fn test_clock_settime() { | ||
| require_capability!(CAP_SYS_TIME); | ||
| let ts = TimeSpec::from(timespec{tv_sec: 10000000, tv_nsec: 100}); | ||
| let res = clock_settime(ClockId::CLOCK_REALTIME, ts).unwrap(); |
There was a problem hiding this comment.
Lol, this will set the system time back to April 26, 1970! That's not a safe test. There's probably little you can do to test clock_settime. Perhaps it would be ok to set it to the current time.
test/test_time.rs
Outdated
|
|
||
| #[test] | ||
| pub fn test_clock_settime() { | ||
| require_capability!(CAP_SYS_TIME); |
There was a problem hiding this comment.
require_capability is a Linux-only macro.
|
Was making some of the changes but getting on a flight now...will follow up with the rest |
src/time.rs
Outdated
| libc_enum! { | ||
| #[repr(i32)] | ||
| #[cfg_attr(target_os = "darwin", repr(i32))] | ||
| #[cfg_attr(not(target_os = "darwin"), repr(u32))] |
There was a problem hiding this comment.
This isn't correct. Check the libc crate to see what the actual size is on various platforms:
> rg type.clockid_t
linux_like/mod.rs
4:pub type clockid_t = ::c_int;
newlib/mod.rs
4:pub type clockid_t = ::c_ulong;
uclibc/mod.rs
6:pub type clockid_t = ::c_int;
solarish/mod.rs
5:pub type clockid_t = ::c_int;
haiku/mod.rs
9:pub type clockid_t = i32;
redox/mod.rs
9:pub type clockid_t = ::c_int;
hermit/mod.rs
39:pub type clockid_t = c_ulong;
bsd/netbsdlike/mod.rs
10:pub type clockid_t = ::c_int;
bsd/apple/mod.rs
16:pub type clockid_t = ::c_uint;
bsd/freebsdlike/dragonfly/mod.rs
8:pub type clockid_t = ::c_ulong;
bsd/freebsdlike/freebsd/mod.rs
6:pub type clockid_t = ::c_int;
src/time.rs
Outdated
| } | ||
|
|
||
| pub fn clock_getres(clk_id: ClockId) -> Result<TimeSpec> { | ||
| let mut c_time = libc::timespec {tv_sec: 0, tv_nsec: 0}; |
There was a problem hiding this comment.
MaybeUninit is the correct thing to do here, not zero-initialization.
|
Sorry, ended up not having time for a while after pushing up some of the changes...will mark things as a WIP in that situation in the future. I should be around now, though. Anyway, I updated the conditional compilation to reflect what is in Also decided to just ditch the Let me know what you think. Sorry again for falling off for a bit. |
src/time.rs
Outdated
| target_os = "illumos", | ||
| all(target_env = "newlib", target_arch = "arm"), | ||
| ), repr(i32))] | ||
| #[cfg_attr(target_os = "macos", repr(u32))] |
src/time.rs
Outdated
| target_os = "emscripten", | ||
| target_os = "solaris", | ||
| target_os = "illumos", | ||
| all(target_env = "newlib", target_arch = "arm"), |
There was a problem hiding this comment.
Why the special case for target_arch on newlib? I don't see that in libc.
There was a problem hiding this comment.
For newlib, the supported target_archs are only arm and aarch64. While the type clockid_t is defined here:
https://github.com/rust-lang/libc/blob/49c1d138e3b4719e21ee7cbce7dd903543cda80d/src/unix/newlib/mod.rs#L4
c_ulong is defined for each arch in their respective modules:
https://github.com/rust-lang/libc/blob/49c1d138e3b4719e21ee7cbce7dd903543cda80d/src/unix/newlib/arm/mod.rs#L5
https://github.com/rust-lang/libc/blob/49c1d138e3b4719e21ee7cbce7dd903543cda80d/src/unix/newlib/aarch64/mod.rs#L5
There was a problem hiding this comment.
Just realized that I had put the "arm" case under i32 rather than u32. Hopefully makes more sense now.
src/time.rs
Outdated
| target_os = "netbsd", | ||
| target_os = "openbsd", | ||
| target_os = "haiku", | ||
| target_os = "linux", |
There was a problem hiding this comment.
This should be all(target_os = "linux", not(target_env = "newlib")) and likewise for android and emscripten.
There was a problem hiding this comment.
Ah, that's very tricky. Added that in the case of everything that appears after newlib/uclibc. In some cases it had to be all(target_os = "<x>", not(target_env = "newlib", target_env = "uclibc")).
e1b481f to
e480d1e
Compare
|
You should be able to fix the test failures by rebasing now. |
19f57d4 to
7a10cae
Compare
|
Ok, I think this is good, but you need to squash before we can merge. |
7a10cae to
8051751
Compare
d2a1c8d to
e84c05b
Compare
e84c05b to
c9b4a36
Compare
|
Squashed a while ago and just rebased again for the changelog. Hopefully good to merge now. |
1100: implement clock_gettime, clock_getres, clock_settime r=asomers a=kevinwern See: https://pubs.opengroup.org/onlinepubs/007908799/xsh/clock_settime.html Again related to: https://github.com/CraneStation/wasi-common/issues/16 Conditional inclusion of clock IDs based on looking through libc. Co-authored-by: Kevin Wern <kevin.m.wern@gmail.com>
Build failed |
|
I've seen this before. I think it's a bug in glibc. It's certainly nothing to do with your PR. bors retry |
1100: implement clock_gettime, clock_getres, clock_settime r=asomers a=kevinwern See: https://pubs.opengroup.org/onlinepubs/007908799/xsh/clock_settime.html Again related to: https://github.com/CraneStation/wasi-common/issues/16 Conditional inclusion of clock IDs based on looking through libc. Co-authored-by: Kevin Wern <kevin.m.wern@gmail.com>
Build failed |
|
Please rebase to fix the musl failure. |
|
I'm interested in some of these functions too for #1275. @kevinwern do you mind if I pick up this PR to implement them? |
1281: Added clock_gettime, clock_getres, clock_settime, clock_getcpuclockid r=asomers a=xonatius Picked up #1100 and added `clock_getcpuclockid` call as well. Credits to @kevinwern for the initial version. https://www.man7.org/linux/man-pages/man2/clock_gettime.2.html https://www.man7.org/linux/man-pages/man3/clock_getcpuclockid.3.html Closes #1275 Co-authored-by: Daniil Bondarev <xonatius@gmail.com>
|
Can we close this one after #1281? |
|
Yep |
See: https://pubs.opengroup.org/onlinepubs/007908799/xsh/clock_settime.html
Again related to: https://github.com/CraneStation/wasi-common/issues/16
Conditional inclusion of clock IDs based on looking through libc.