Skip to content

Specialize sleep_until implementation for unix (except mac) #141829

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

dvdsk
Copy link
Contributor

@dvdsk dvdsk commented May 31, 2025

related tracking issue: #113752
Supersedes #118480 for the reasons see: #113752 (comment)

Replaces the generic catch all implementation with target_os specific ones for: linux/netbsd/freebsd/android/solaris/illumos etc. Other platforms like wasi, macos/ios/tvos/watchos and windows will follow in later separate PR's (once this is merged).

@rustbot
Copy link
Collaborator

rustbot commented May 31, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-hermit Operating System: Hermit O-itron Operating System: ITRON O-SGX Target: SGX O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 31, 2025
@dvdsk
Copy link
Contributor Author

dvdsk commented May 31, 2025

@rustbot rustbot assigned cuviper and unassigned tgross35 May 31, 2025
@workingjubilee workingjubilee changed the title Specialize sleep_until implementation for unix (expect mac) May 31, 2025
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 8010ec8 to 295e8d1 Compare May 31, 2025 19:54
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 295e8d1 to 1e7d958 Compare May 31, 2025 20:04
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 1e7d958 to 406e32b Compare May 31, 2025 21:31
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk marked this pull request as draft May 31, 2025 22:37
@dvdsk
Copy link
Contributor Author

dvdsk commented May 31, 2025

This can be done without touching all pal's, ill be doing that tomorrow, now its bed time 😴

edit: I was mistaken, tidy does not allow #[cfg(...)] in strc/thread/mod.rs probably for a good reason. Therefore we need a trivial not platform specific sleep_until in every pal that does not have a specialized one.

@rustbot rustbot added the O-wasm Target: WASM (WebAssembly), http://webassembly.org/ label Jun 1, 2025
@dvdsk dvdsk force-pushed the sleep_until_linux branch from 4bcfd27 to f657ec1 Compare June 1, 2025 07:08
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from f657ec1 to 1127a50 Compare June 1, 2025 07:26
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 1127a50 to 60edd5a Compare June 1, 2025 07:35
@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2025
@bors
Copy link
Collaborator

bors commented Jul 3, 2025

⌛ Testing commit dd0a1e6 with merge 94817f8...

bors added a commit that referenced this pull request Jul 3, 2025
Specialize sleep_until implementation for unix (except mac)

related tracking issue: #113752
Supersedes #118480 for the reasons see: #113752 (comment)

Replaces the generic catch all implementation with target_os specific ones for: linux/netbsd/freebsd/android/solaris/illumos etc. Other platforms like wasi, macos/ios/tvos/watchos and windows will follow in later separate PR's (once this is merged).
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 3, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 3, 2025
@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 3, 2025

The job x86_64-gnu-aux failed! Check out the build log: (web) (plain enhanced) (plain)
Click to see the possible cause of the failure (guessed by this bot)

Miri does not understand clock_nanosleep. To fix that I think I need to add a shim for it to miri/src/shims/time.rs. Seems doable, I'll see if I can adjust whats done for nanosleep. I'll get on that now.

@rustbot
Copy link
Collaborator

rustbot commented Jul 3, 2025

The Miri subtree was changed

cc @rust-lang/miri

@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 3, 2025

First time adding anything to MIRI.

  • It does not make sense to me to support all argument combinations of clock_nanosleep. Instead I have implemented those needed for sleep_until and those trivial to add.
  • Is it correct to panic when an unimplemented argument combination is passed?
  • I am not sure about my usage of callback!, I just copied what nanosleep had without fully understanding it. I've added a comment to reflect that. Please review that carefully.

It was fun to see some MIRI code but I understand if the additional code to MIRI for supporting clock_nanosleep is not worth it at this stage.

Let me also thank you all again for your reviews and mentoring me on how to get this PR up to spec.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Thanks for working on the Miri shims! However, they don't quite match what we'd usually expect. If you need more Miri-specific help, please ask on Zulip.

Copy link
Member

Choose a reason for hiding this comment

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

Please also add a test in tests/pass-dep/libc/libc-time.rs for the new shim. In particular, it seems there are two codepaths (relative and absolute?), they should both be tested.

Also, please add a call to sleep_until in tests/pass/shims/time.rs.

&mut self,
clock_id: &OpTy<'tcx>,
flags: &OpTy<'tcx>,
req_op: &OpTy<'tcx>,
Copy link
Member

Choose a reason for hiding this comment

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

Why do you call this req_op? It seems to be the timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree the name could be better. I've mirrored what was there for nanosleep as the argument for clock_nanosleep has a similar function and they are called similar in the man pages.

nanosleep:

int nanosleep(const struct timespec *req, struct timespec *rem);

clock_nanosleep:

int clock_nanosleep(clockid_t clockid, int flags,
                           const struct timespec *request,
                           struct timespec *remain);

I don't know why they call it req/request. Timeout doesn't quite work as its either a deadline or a timeout depending on if the TIMER_ABSTIME flag is set.

Maybe deadline_or_timeout would be a better name.

Copy link
Member

Choose a reason for hiding this comment

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

and they are called similar in the man pages.

Hm, odd, in my manpages it's

       int nanosleep(const struct timespec *duration,
                     struct timespec *_Nullable rem);
       int clock_nanosleep(clockid_t clockid, int flags,
                           const struct timespec *t,
                           struct timespec *_Nullable remain);
Copy link
Contributor Author

@dvdsk dvdsk Jul 3, 2025

Choose a reason for hiding this comment

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

My OS release is kinda old (Pop!_OS seems to be stuck on 22.04 until they finish cosmic).

I'm going to rename the the argument to instant_or_timeout, though those words are not used in the man pages it best describes what the argument is for.

edit: let me know if you disagree

Copy link
Member

Choose a reason for hiding this comment

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

I would suggest naming it after the type, timespec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thats better 👍

Copy link
Member

Choose a reason for hiding this comment

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

(unresolved since that rename didn't happen yet)

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Jul 3, 2025

(The build fails on Windows because you're using libc.)

@dvdsk dvdsk force-pushed the sleep_until_linux branch from a7c539f to f65530d Compare July 3, 2025 19:02
@rust-log-analyzer

This comment has been minimized.

dvdsk and others added 6 commits July 3, 2025 21:20
Co-authored-by: Jonas Böttiger <jonasboettiger@icloud.com>

- use the internal platform specific Instant::CLOCK_ID
- skip allow(unused) on on platform that uses it such that it
can not become dead code
- sleep_until: remove leftover links
- In contradiction to nanosleep clock_nanosleep returns the error
  directly and does not require a call to `os::errno()`.
- The last argument to clock_nanosleep can be NULL removing the need
  for mutating the timespec.
- Missed an `allow(unused)` that could be made conditional.
This is intended to support the std's new sleep_until. Only
the clocks REALTIME and MONOTONIC are supported. The first because it
was trivial to add the second as its needed for sleep_until. Only
passing no flags or passing only TIMER_ABSTIME is supported.

If an unsupported flags or clocks are passed this implementation panics.
@dvdsk dvdsk force-pushed the sleep_until_linux branch from d91e6a9 to dcb3593 Compare July 3, 2025 19:20
@rust-log-analyzer

This comment has been minimized.

Comment on lines +372 to +374
let this = self.eval_context_mut();

let clockid_t_size = this.libc_ty_layout("clockid_t").size;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let this = self.eval_context_mut();
let clockid_t_size = this.libc_ty_layout("clockid_t").size;
let this = self.eval_context_mut();
let clockid_t_size = this.libc_ty_layout("clockid_t").size;

making code more readable by having empty lines between semantically relevant blocks, like paragraphs in a text

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 want to make sure I understand correctly. You would like two empty lines between semantically relevant blocks?

That is not what I see for other shims in time.rs.

Copy link
Member

Choose a reason for hiding this comment

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

That's not what the diff does. What it does is it moves the clockid_t_size out of the block that should consist of exactly one line per argument, converting that argument into a more typed representation.

Copy link
Contributor Author

@dvdsk dvdsk Jul 3, 2025

Choose a reason for hiding this comment

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

Oh I read the diff wrong.

Its getting late, thank you very much for the quick and extensive feedback! I'm gonna call it a day here and add the tests tomorrow when I've got some more sleep 💤

Have great day!

@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 3, 2025

rebased to:

  • fix commit formatting unrelated MIRI code.
  • fix spell errors for months ago without adding new useless commit

edit: I just realized rebasing all the time makes this rather hard to review. I'll rebase/squash at the end, for now I'll focus on short descriptive commits.

@@ -396,14 +393,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// No flags set, the timespec should be interperted as a duration
// to sleep for
TimeoutAnchor::Relative
} else if flag == this.eval_libc("TIMER_ABSTIME").to_int(int_size) {
} else if flags == this.eval_libc("TIMER_ABSTIME").to_i32()? {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else if flags == this.eval_libc("TIMER_ABSTIME").to_i32()? {
} else if flags == this.eval_libc_i32("TIMER_ABSTIME") {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-hermit Operating System: Hermit O-itron Operating System: ITRON O-SGX Target: SGX O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-wasm Target: WASM (WebAssembly), http://webassembly.org/ O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.