Skip to content

Optimize thread parking on NetBSD #101482

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 1 commit into from
Sep 11, 2022
Merged

Optimize thread parking on NetBSD #101482

merged 1 commit into from
Sep 11, 2022

Conversation

joboet
Copy link
Member

@joboet joboet commented Sep 6, 2022

As the futex syscall is not present in the latest stable release, NetBSD cannot use the efficient thread parker and locks Linux uses. Currently, it therefore relies on a pthread-based parker, consisting of a mutex and semaphore which protect a state variable. NetBSD however has more efficient syscalls available: _lwp_park and _lwp_unpark. These already provide the exact semantics of thread::park and Thread::unpark, but work with thread ids. In std, this ID is here stored in an atomic state variable, which is also used to optimize cases were the parking token is already available at the time thread::park is called.

r? @m-ou-se

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 6, 2022
@rustbot
Copy link
Collaborator

rustbot commented Sep 6, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 6, 2022
Comment on lines 11 to 21
extern "C" {
fn _lwp_park(
clock_id: clockid_t,
flags: c_int,
ts: *mut timespec,
unpark: lwpid_t,
hint: *const c_void,
unparkhint: *const c_void,
) -> c_int;
fn _lwp_unpark(lwp: lwpid_t, hint: *const c_void) -> c_int;
}
Copy link
Member

Choose a reason for hiding this comment

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

It might be good to add these to the libc crate.

Copy link
Member Author

Choose a reason for hiding this comment

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

The syscall behaviour can change slightly with newer versions (e.g. the ts argument is only modified on NetBSD 9.0 and above, while it is still the same symbol), so it might not necessarily be a good idea to advertise these functions in libc.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think that matters much for inclusion in the libc crate. It contains many symbols that aren't fully "stable" or only exist on certain versions of the operating system or underlying libc.

Anyway, this is not a blocker for this PR.

@m-ou-se
Copy link
Member

m-ou-se commented Sep 6, 2022

If I understand the documentation correctly, it seems best to pass the address of state to the park and unpark syscalls as the hint argument?

@m-ou-se
Copy link
Member

m-ou-se commented Sep 6, 2022

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 6, 2022

📌 Commit e41555848ed70cc76d4f17387150a108bdea580d has been approved by m-ou-se

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 Sep 6, 2022
@joboet
Copy link
Member Author

joboet commented Sep 6, 2022

@bors r-
The symbol for _lwp_park is actually ___lwp_park60, so I'm not sure this even compiles properly. Anyways, this should definitely be tested; sorry for not clarifying that I have not yet done so.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 6, 2022
@joboet
Copy link
Member Author

joboet commented Sep 7, 2022

I've now confirmed that this works on NetBSD 9.3 (x86_64 with QEMU). ___lwp_park60 is indeed the right symbol, as the regular _lwp_park uses the old interface from NetBSD 5.0.

@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 Sep 7, 2022
@sanxiyn
Copy link
Member

sanxiyn commented Sep 10, 2022

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 10, 2022

📌 Commit adfe1b171e178e436a3f48ae9774461b5d929d3d has been approved by sanxiyn

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 Sep 10, 2022
@bors
Copy link
Collaborator

bors commented Sep 10, 2022

⌛ Testing commit adfe1b171e178e436a3f48ae9774461b5d929d3d with merge 9da77f41d2fc05613b5cb869d0ebf63c2b64d653...

@bors
Copy link
Collaborator

bors commented Sep 10, 2022

💔 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 Sep 10, 2022
@rust-log-analyzer

This comment has been minimized.

@sanxiyn
Copy link
Member

sanxiyn commented Sep 11, 2022

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 11, 2022

📌 Commit 81b11ed has been approved by sanxiyn

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 Sep 11, 2022
@bors
Copy link
Collaborator

bors commented Sep 11, 2022

⌛ Testing commit 81b11ed with merge 56b625b...

@bors
Copy link
Collaborator

bors commented Sep 11, 2022

☀️ Test successful - checks-actions
Approved by: sanxiyn
Pushing 56b625b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 11, 2022
@bors bors merged commit 56b625b into rust-lang:master Sep 11, 2022
@rustbot rustbot added this to the 1.65.0 milestone Sep 11, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (56b625b): 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

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 1
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
7.1% [7.1%, 7.1%] 1
Improvements ✅
(primary)
-1.6% [-1.6%, -1.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.6% [-1.6%, -1.6%] 1

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

@joboet joboet deleted the netbsd_parker branch February 18, 2023 00:28
@workingjubilee workingjubilee added the O-netbsd Operating system: NetBSD label Feb 26, 2023
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-netbsd Operating system: NetBSD 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.

9 participants