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 15 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

r? @cuviper

@rustbot rustbot assigned cuviper and unassigned tgross35 May 31, 2025
@workingjubilee workingjubilee changed the title Specialize sleep_until implementation for unix (expect mac) Specialize sleep_until implementation for unix (except 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
@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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be done.

  • I'm a little unsure about the libc-time.rs test as I've added a use std::time::{Duration, Instant}; while the test hardly had any imports before.
  • I've added a test for nanosleep since there was none there.
  • All tests where locally tested by sabotaging them.

@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.

@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") {

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 7c039f4 to 2645080 Compare July 4, 2025 18:41
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-miri failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tests/pass-dep/libc/pthread-sync.rs ... ok
tests/pass-dep/libc/mmap.rs ... ok

FAILED TEST: tests/pass-dep/libc/libc-time.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-mL2ljm" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/miri" "--error-format=json" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/pass-dep/libc" "tests/pass-dep/libc/libc-time.rs" "-Zmiri-disable-isolation" "--extern" "cfg_if=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libcfg_if-21ee9281a45bad5a.rlib" "--extern" "cfg_if=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libcfg_if-21ee9281a45bad5a.rmeta" "--extern" "getrandom_01=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libgetrandom-7f3b946e20715eef.rlib" "--extern" "getrandom_01=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libgetrandom-7f3b946e20715eef.rmeta" "--extern" "getrandom_02=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libgetrandom-bd5c0888d94a3e06.rlib" "--extern" "getrandom_02=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libgetrandom-bd5c0888d94a3e06.rmeta" "--extern" "getrandom_03=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libgetrandom-5f83e5e26635837a.rlib" "--extern" "getrandom_03=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libgetrandom-5f83e5e26635837a.rmeta" "--extern" "libc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/liblibc-d596bfe702a73e06.rlib" "--extern" "libc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/liblibc-d596bfe702a73e06.rmeta" "--extern" "num_cpus=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libnum_cpus-325cbfe60dab5fe3.rlib" "--extern" "num_cpus=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libnum_cpus-325cbfe60dab5fe3.rmeta" "--extern" "page_size=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libpage_size-55a78d54650635f3.rlib" "--extern" "page_size=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libpage_size-55a78d54650635f3.rmeta" "--extern" "tempfile=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libtempfile-7becc496b63f407b.rlib" "--extern" "tempfile=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libtempfile-7becc496b63f407b.rmeta" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libtokio-6ee44d08c36215c7.rlib" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps/libtokio-6ee44d08c36215c7.rmeta" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/aarch64-apple-darwin/debug/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/debug/deps" "--edition" "2021" "--target" "aarch64-apple-darwin"

error: test got exit status: 1, but expected 0
 = note: compilation failed, but was expected to succeed

error: no output was expected
Execute `./miri test --bless` to update `tests/pass-dep/libc/libc-time.stderr` to the actual output
+++ <stderr output>
error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:106:15
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, libc::TIMER_ABSTIME, &before_start, remainder)
   |               ^^^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find value `TIMER_ABSTIME` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:106:60
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, libc::TIMER_ABSTIME, &before_start, remainder)
   |                                                            ^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:119:15
   |
---

error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:137:15
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, NO_FLAGS, &duration_zero, remainder)
   |               ^^^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:146:15
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, NO_FLAGS, &duration_100_millis, remainder)
   |               ^^^^^^^^^^^^^^^ not found in `libc`

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0425`.


full stderr:
error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:106:15
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, libc::TIMER_ABSTIME, &before_start, remainder)
   |               ^^^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find value `TIMER_ABSTIME` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:106:60
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, libc::TIMER_ABSTIME, &before_start, remainder)
   |                                                            ^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:119:15
   |
---

error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:137:15
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, NO_FLAGS, &duration_zero, remainder)
   |               ^^^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find function `clock_nanosleep` in crate `libc`
##[error]  --> tests/pass-dep/libc/libc-time.rs:146:15
   |
LL |         libc::clock_nanosleep(libc::CLOCK_MONOTONIC, NO_FLAGS, &duration_100_millis, remainder)
   |               ^^^^^^^^^^^^^^^ not found in `libc`

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0425`.
---

Location:
   /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ui_test-0.29.2/src/lib.rs:369

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
error: test failed, to rerun pass `--test ui`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/release/deps/ui-bf27d3ce51670675` (exit status: 1)
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:02:10
  local time: Fri Jul  4 21:57:25 UTC 2025
  network time: Fri, 04 Jul 2025 21:57:25 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.