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

Merged
merged 2 commits into from
Jul 7, 2025

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
fn add_100_millis(mut ts: libc::timespec) -> libc::timespec {
const SECOND: i64 = 1_000_000_000;
ts.tv_nsec += SECOND / 10;
ts.tv_sec = ts.tv_nsec / SECOND;
Copy link
Member

@RalfJung RalfJung Jul 5, 2025

Choose a reason for hiding this comment

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

Isn't this still wrong?

Suggested change
ts.tv_sec = ts.tv_nsec / SECOND;
// If this pushes tv_nsec to SECOND or higher, we need to overflow to tv_sec.
ts.tv_sec += ts.tv_nsec / SECOND;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be this now:

fn add_100_millis(mut ts: libc::timespec) -> libc::timespec {
    const SECOND: i64 = 1_000_000_000;
    ts.tv_nsec += SECOND / 10;
    ts.tv_sec = ts.tv_nsec / SECOND;
    ts.tv_nsec %= SECOND;
    ts
}

Copy link
Member

@RalfJung RalfJung Jul 5, 2025

Choose a reason for hiding this comment

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

But that's wrong, as I already said above. Why do you reset tv_sec to 0 or 1??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the overflow of tv_nsec is taken care of by:

ts.tv_nsec %= SECOND;

right?
I tested it in the playground and there it seems to work https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8c8dcb8b0f9c7238b747b61e073ddc14

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that's wrong, as I already said above. Why do you reset tv_sec to 0 or 1??

oh damn I get it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry I'm struggling with this, no idea why. Thanks for your patience Ralf.

Copy link
Member

Choose a reason for hiding this comment

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

Better take a bit more time and think things through twice next time. :)

My suggestion above has a comment, I think that's still a useful comment to explain the (clearly non-obvious!) logic. Please add that comment.

@RalfJung
Copy link
Member

RalfJung commented Jul 5, 2025

Miri code looks good once the last comment nit is resolved. :)

@RalfJung
Copy link
Member

RalfJung commented Jul 5, 2025

Regarding trying on more Unix platforms... this should cover various unix flavors
@bors2 try jobs=dist-x86_64-*

@rust-bors
Copy link

rust-bors bot commented Jul 5, 2025

⌛ Trying commit 6e212a3 with merge 99000fa

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 5, 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).
try-job: dist-x86_64-*
@rust-log-analyzer

This comment has been minimized.

@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 5, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 5, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 5, 2025

☀️ Try build successful (CI)
Build commit: 99000fa (99000fa1c60a6e398f1a0c78ad37c6d1956a1f3a, parent: f0b67dd97d74610ee4185cf01c775a563c2017a2)

@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 6, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 6, 2025
@RalfJung
Copy link
Member

RalfJung commented Jul 6, 2025

The non-Miri part was already approved before, so please clean up the history and then we can land this. :)

dvdsk added 2 commits July 6, 2025 17:36
Using clock nanosleep leads to more accurate sleep times on platforms
where it is supported.

To enable using clock_nanosleep this makes `sleep_until` platform
specific. That unfortunatly requires identical placeholder
implementations for the other platforms (windows/mac/wasm etc).

we will land platform specific implementations for those later. See the
`sleep_until` tracking issue.

This requires an accessors for the Instant type. As that accessor is only
used on the platforms that have clock_nanosleep it is marked as allow_unused.

32bit time_t targets do not use clock_nanosleep atm, they instead rely
on the same placeholder as the other platforms. We could make them
use clock_nanosleep too in the future using `__clock_nanosleep_time64`.

__clock_nanosleep_time64 is documented at:
https://www.gnu.org/software/libc/manual/html_node/64_002dbit-time-symbol-handling.html
The clock_nanosleep support is there to allow code using `sleep_until`
to run under Miri. Therefore the implementation is minimal.
- Only the clocks REALTIME and MONOTONIC are supported. The first is supported simply
because it was trivial to add not because it was needed for sleep_until.
- The only supported flag combinations are no flags or TIMER_ABSTIME only.
If an unsupported flag combination or clock is passed in this throws
unsupported.
@dvdsk dvdsk force-pushed the sleep_until_linux branch from 6631767 to 61cf174 Compare July 6, 2025 16:04
@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 6, 2025

The non-Miri part was already approved before, so please clean up the history and then we can land this. :)

Squashed to two commits

@RalfJung
Copy link
Member

RalfJung commented Jul 6, 2025

@bors r=cuviper,RalfJung

@bors
Copy link
Collaborator

bors commented Jul 6, 2025

📌 Commit 61cf174 has been approved by cuviper,RalfJung

It is now in the queue for this repository.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 6, 2025
@bors
Copy link
Collaborator

bors commented Jul 6, 2025

⌛ Testing commit 61cf174 with merge ca98d4d...

@bors
Copy link
Collaborator

bors commented Jul 7, 2025

☀️ Test successful - checks-actions
Approved by: cuviper,RalfJung
Pushing ca98d4d to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 7, 2025
@bors bors merged commit ca98d4d into rust-lang:master Jul 7, 2025
12 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 7, 2025
Copy link
Contributor

github-actions bot commented Jul 7, 2025

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing a84ab0c (parent) -> ca98d4d (this PR)

Test differences

Show 49 test diffs

Stage 1

  • sleep_until: [missing] -> pass (J0)

Additionally, 48 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard ca98d4d4b3114116203699c2734805547df86f9a --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-apple-1: 8191.0s -> 6182.1s (-24.5%)
  2. x86_64-apple-2: 3891.8s -> 3078.9s (-20.9%)
  3. dist-apple-various: 6340.5s -> 5395.4s (-14.9%)
  4. dist-x86_64-apple: 7683.6s -> 8534.0s (11.1%)
  5. tidy: 70.1s -> 64.4s (-8.1%)
  6. dist-various-1: 4014.2s -> 3734.3s (-7.0%)
  7. dist-x86_64-mingw: 7681.9s -> 7286.0s (-5.2%)
  8. dist-arm-linux-musl: 5334.7s -> 5609.2s (5.1%)
  9. x86_64-msvc-ext3: 6089.3s -> 5780.9s (-5.1%)
  10. i686-gnu-nopt-2: 8135.4s -> 7743.9s (-4.8%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ca98d4d): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (secondary -1.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) - - 0

Cycles

Results (secondary 2.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.9% [2.9%, 2.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 464.608s -> 463.852s (-0.16%)
Artifact size: 372.10 MiB -> 372.07 MiB (-0.01%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.