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

windows: provide more accurate result for available_parallelism if cores > 64 #114856

Closed
wants to merge 2 commits into from

Conversation

klensy
Copy link
Contributor

@klensy klensy commented Aug 15, 2023

This should give more accurate result if number of cores > 64

see:
https://devblogs.microsoft.com/oldnewthing/20200824-00/?p=104116
ninja-build/ninja#1604

Could probably implement using GetLogicalProcessorInformationEx
see: ninja-build/ninja#1674

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getactiveprocessorcount >= Win 7

crates that probably want to fix it too:
sysinfo: but maybe GetLogicalProcessorInformationEx instead?
num_cpus: obviously

@rustbot
Copy link
Collaborator

rustbot commented Aug 15, 2023

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added 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 Aug 15, 2023
@klensy
Copy link
Contributor Author

klensy commented Aug 15, 2023

Anyone with > 64 cores test please?

Copy link
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

Do the docs also need updating with Windows specific information?

library/std/src/sys/windows/thread.rs Outdated Show resolved Hide resolved
library/std/src/sys/windows/thread.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@klensy
Copy link
Contributor Author

klensy commented Aug 15, 2023

Do the docs also need updating with Windows specific information?

https://doc.rust-lang.org/nightly/std/thread/fn.available_parallelism.html
Docs already mention that this can be not accurate, so i don't know.

It may undercount the amount of parallelism available on systems with more than 64 logical CPUs

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Aug 15, 2023

The Miri subtree was changed

cc @rust-lang/miri

Comment on lines +550 to +554
"GetActiveProcessorCount" => {
let [groupnumber] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(groupnumber)?.to_u16()?;
this.write_scalar(Scalar::from_u32(this.machine.num_cpus), dest)?;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This probably wrong, need advice.

Copy link
Member

Choose a reason for hiding this comment

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

cc @rust-lang/miri

Copy link
Member

Choose a reason for hiding this comment

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

We were already summoned by rustbot automatically :)

Why do you think this is wrong?

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 further up the match, down here is the section for if this.frame_in_std() shims. Probably putting it with GetSystemInfo makes sense.

Other than that -- without knowing GetActiveProcessorCount, this seems reasonable? A brief comment explaining why we can ignore the groupnumber might be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved and added comment.

@rust-log-analyzer

This comment has been minimized.

@klensy
Copy link
Contributor Author

klensy commented Aug 15, 2023

Uhh, can't this be more human-friendly message?
{"message":"the place in the program where the ICE was triggered",...

@joshtriplett
Copy link
Member

Given that Windows 11 appears to lift the 64-CPU limitation by default without requiring any special handling from a process, this seems reasonable.

r=me once CI passes, and once someone from miri signs off on the miri changes.

@joshtriplett
Copy link
Member

@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 Aug 15, 2023
@RalfJung
Copy link
Member

Uhh, can't this be more human-friendly message? {"message":"the place in the program where the ICE was triggered",...

Yeah we're working on that. Or at least I hope this has the side-effect of making things better. I don't know why it uses JSON, it is some strange interaction of x.py test and the Miri test suite.

@@ -3875,47 +3875,6 @@ pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: SYMBOLIC_LINK_FLAGS = 2u
pub const SYMBOLIC_LINK_FLAG_DIRECTORY: SYMBOLIC_LINK_FLAGS = 1u32;
pub const SYMLINK_FLAG_RELATIVE: u32 = 1u32;
pub const SYNCHRONIZE: FILE_ACCESS_RIGHTS = 1048576u32;
#[repr(C)]
pub struct SYSTEM_INFO {
Copy link
Member

Choose a reason for hiding this comment

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

Miri was relying on this type declaration for its GetSystemInfo shim, that's why we are getting ICEs now.

The alternative is to change Miri's GetSystemInfo to use hard-coded offsets for the two fields it cares about (dwPageSize, dwNumberOfProcessors).

Copy link
Member

Choose a reason for hiding this comment

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

I think it's ok to keep the old definitions around even though they're no longer used by std. I don't think there's yet a pressing need to keep windows_sys.rs definitions strictly minimal,. Plus keeping them reduces churn should we decide to use them for something else in the future.

Copy link
Member

Choose a reason for hiding this comment

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

I can also disentangle Miri from that definition, if you'd prefer us not to rely on it. That can happen asynchronously with this PR, so eventually when the Miri patch lands you can do the cleanup.

Copy link
Member

Choose a reason for hiding this comment

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

I mean, it might be better overall if miri wasn't entangled with the std this way but I don't feel particularly strongly about it.

Copy link
Member

@RalfJung RalfJung Aug 15, 2023

Choose a reason for hiding this comment

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

On Unix we rely on libc for tons of constants and types. That's just a lot more maintainable than hard-coding them all in the Miri sources. For Windows sadly there is no libc-equivalent (that is present in the sysroot) so we can only use the few things we can find in std...

Copy link
Member

Choose a reason for hiding this comment

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

Can you not use the windows-sys crate directly? The definitions in std are essentially just a vendored version of it.

Copy link
Member

Choose a reason for hiding this comment

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

We need a crate that's in the sysroot. (Miri might be running a Windows target on a Linux host.) So... we can do that once std depends on it. ;)

@the8472
Copy link
Member

the8472 commented Aug 15, 2023

Do the docs also need updating with Windows specific information?

https://doc.rust-lang.org/nightly/std/thread/fn.available_parallelism.html Docs already mention that this can be not accurate, so i don't know.

It may undercount the amount of parallelism available on systems with more than 64 logical CPUs

Yes, this should be updated if that restriction is lifted. Based on #114856 (comment) it should now mention that may be overreporting on windows 10 because processes won't use more than 64 cores by default.

// Number of cores, but more accurate vaulue, than GetSystemInfo.dwNumberOfProcessors
// if number of cores > 64. If called with ALL_PROCESSOR_GROUPS as groupnumber,
// returns total number of cores, instead of number in group, see
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getactiveprocessorcount
Copy link
Member

Choose a reason for hiding this comment

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

This sounds like we should do throw_unsup_format! if the groupnumber is not ALL_PROCESSOR_GROUPS, since we are not correctly handling that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

vaulue typo.

let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed();
c::GetSystemInfo(&mut sysinfo);
sysinfo.dwNumberOfProcessors as usize
// FIXME: windows::Win32::System::SystemServices::ALL_PROCESSOR_GROUPS should be u16, not u32
Copy link
Contributor Author

Choose a reason for hiding this comment

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

microsoft/win32metadata@8f70180
will be changed to Windows.Win32.System.Threading.Apis.ALL_PROCESSOR_GROUPS

Copy link
Member

Choose a reason for hiding this comment

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

Hm... I've been thinking about this some more. Could you move the correct definition of ALL_PROCESSOR_GROUPS into c.rs? That way we can keep all the FIXMEs into one file so they're easier to find and fix when the bindings are next updated. Otherwise it might be easier to forget about it.

@JohnCSimon
Copy link
Member

@klensy
ping from triage - can you post your status on this PR? There hasn't been an update in a few months. Thanks!

FYI: when a PR is ready for review, send a message containing
@rustbot ready to switch to S-waiting-on-review so the PR is in the reviewer's backlog.

@bors
Copy link
Contributor

bors commented Nov 24, 2023

☔ The latest upstream changes (presumably #118232) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

@klensy
Ping from triage: I'm closing this due to inactivity because it hasn't been touched in months.
Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Dec 17, 2023
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.