-
Notifications
You must be signed in to change notification settings - Fork 718
implement clock_gettime, clock_getres, clock_settime #1100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget ios
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the special case for target_arch on newlib? I don't see that in libc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be all(target_os = "linux", not(target_env = "newlib")) and likewise for android and emscripten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
asomers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bors r+
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
|
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.