Skip to content
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

implement clock_gettime, clock_getres, clock_settime #1100

Closed

Conversation

kevinwern
Copy link
Contributor

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.

@kevinwern kevinwern force-pushed the feature/implement-time-funcs branch 3 times, most recently from 14fce4c to 0b16e15 Compare July 24, 2019 13:50
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};
Copy link
Member

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.

Copy link
Member

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.

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();
Copy link
Member

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]
pub fn test_clock_settime() {
require_capability!(CAP_SYS_TIME);
Copy link
Member

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.

@kevinwern
Copy link
Contributor Author

Was making some of the changes but getting on a flight now...will follow up with the rest

src/time.rs Outdated
@@ -3,7 +3,8 @@ use {Result, Errno};
use libc;

libc_enum! {
#[repr(i32)]
#[cfg_attr(target_os = "darwin", repr(i32))]
#[cfg_attr(not(target_os = "darwin"), repr(u32))]
Copy link
Member

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};
Copy link
Member

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.

@kevinwern
Copy link
Contributor Author

kevinwern commented Aug 27, 2019

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 libc and used MaybeUninit where we need to create and pass a timespec ourselves instead of expecting one from the caller.

Also decided to just ditch the set_* tests. Don't think there's much value in that, and I couldn't find a consistent, safe thing to use for all platforms (my computer uses NTP so I didn't catch that I had changed the clock ID to CLOCK_REALTIME in my stupid tinkering to create a "successful" test). I think I've been overtesting in my PRs anyway.

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))]
Copy link
Member

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"),
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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",
Copy link
Member

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.

Copy link
Contributor Author

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")).

kevinwern added a commit to kevinwern/nix that referenced this pull request Sep 17, 2019
@asomers
Copy link
Member

asomers commented Sep 20, 2019

You should be able to fix the test failures by rebasing now.

kevinwern added a commit to kevinwern/nix that referenced this pull request Sep 22, 2019
@asomers
Copy link
Member

asomers commented Sep 28, 2019

Ok, I think this is good, but you need to squash before we can merge.

@kevinwern kevinwern force-pushed the feature/implement-time-funcs branch 2 times, most recently from d2a1c8d to e84c05b Compare December 2, 2019 22:04
@kevinwern
Copy link
Contributor Author

Squashed a while ago and just rebased again for the changelog. Hopefully good to merge now.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors r+

bors bot added a commit that referenced this pull request Dec 22, 2019
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>
@bors
Copy link
Contributor

bors bot commented Dec 22, 2019

Build failed

@asomers
Copy link
Member

asomers commented Dec 22, 2019

I've seen this before. I think it's a bug in glibc. It's certainly nothing to do with your PR.

bors retry

bors bot added a commit that referenced this pull request Dec 22, 2019
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>
@bors
Copy link
Contributor

bors bot commented Dec 22, 2019

Build failed

@asomers
Copy link
Member

asomers commented Dec 23, 2019

Please rebase to fix the musl failure.

@xonatius
Copy link
Contributor

I'm interested in some of these functions too for #1275. @kevinwern do you mind if I pick up this PR to implement them?

bors bot added a commit that referenced this pull request Oct 3, 2020
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>
@cemeyer
Copy link
Contributor

cemeyer commented Sep 7, 2021

Can we close this one after #1281?

@asomers
Copy link
Member

asomers commented Sep 7, 2021

Yep

@asomers asomers closed this Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants