Skip to content

std: count all processor groups in available_parallelism on Windows - #159511

Open
valentynkit wants to merge 3 commits into
rust-lang:mainfrom
valentynkit:fix/available-parallelism-windows-groups
Open

valentynkit wants to merge 3 commits into
rust-lang:mainfrom
valentynkit:fix/available-parallelism-windows-groups

Conversation

@valentynkit

@valentynkit valentynkit commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

available_parallelism calls GetSystemInfo, which only reports the process's primary processor group, so a 128-CPU Windows machine looks like 64.

Since Windows 11 and Server 2022 a process can run on every group by default, but before that it could only use one, so always calling GetActiveProcessorCount(ALL_PROCESSOR_GROUPS) would overcount there.

GetProcessGroupAffinity tells the two apart with no version check, and exists since Windows 7. If the process is in more than one group, count all of them; otherwise keep GetSystemInfo, which stays correct on Windows 10. #114856 proposed the same call without the gate.

On a machine with three or more groups, a process that runs on some but not all of them now gets the system total, an overcount, where before it was an undercount. Counting only the CPUs a process is allowed to use is #143709.

Miri doesn't model processor groups, so both calls get std-only shims: GetProcessGroupAffinity reports two groups, GetActiveProcessorCount returns num_cpus. The -Zmiri-num-cpus=1024 test then hits the multi-group branch. The >64-CPU path needs real hardware, so nothing covers it here.

Two things I'm unsure about:

  • I use ALL_PROCESSOR_GROUPS instead of adding up GetActiveProcessorCount for each group the process is in. That would avoid the overcount above, at the cost of a buffer and a loop, and it still wouldn't handle affinity inside a group. Happy to do it that way if you prefer.
  • I ignore the return value, assuming any failure other than ERROR_INSUFFICIENT_BUFFER leaves group_count at 1. Docs don't say what happens to it when the call fails for any other reason, so I'm not sure that's safe?

Fixes #152389

On Windows 11 and Server 2022 a process spans every processor group by
default, so GetSystemInfo undercounts a machine with more than 64 logical
CPUs because it only reports the process's primary group. When the process
is in more than one group, sum the active CPUs across all of them;
otherwise keep using GetSystemInfo. The group count tells the two apart
without an OS-version check.

Add the matching Miri shims for the two new calls.
@rustbot

rustbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

@rustbot rustbot added 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 Jul 18, 2026
@rustbot

rustbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

@RalfJung RalfJung left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand this processor group stuff, but it sounds like Miri is doing something wrong by putting a number bigger than 64 into dwNumberOfProcessors in GetSystemInfo?

View changes since this review

let group_count = this.deref_pointer_as(group_count, this.machine.layouts.u16)?;
this.write_scalar(Scalar::from_u16(2), &group_count)?;
this.write_scalar(Scalar::from_i32(1), dest)?; // TRUE
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Each shim here should either

  • be correct, in the sense that it checks all arguments for for every possible value either does the right thing or throws an "unsupported error"
  • or be restricted to std like the shims at the bottom of this file (e.g. SetThreadStackGuarantee)

These shims here ignore some of their arguments so they satisfy neither of these criteria.

)?;
// Report more than one group so `available_parallelism` takes the
// `GetActiveProcessorCount(ALL_PROCESSOR_GROUPS)` path; a single-group
// affinity mask cannot hold the up-to-1024 CPUs the num-cpus test uses.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could also just limit the number of CPUs we support on Windows to 64. It's entirely virtual anyway, Miri never really runs stuff in parallel.

@RalfJung

Copy link
Copy Markdown
Member

I can't build the Windows target or Miri on macOS

FWIW, ./x test miri --target x86_64-pc-windows-msvc is supposed to work on macOS, and also builds the standard library for the Windows target. So if that does not work then please ask on Zulip or file an issue.

@RalfJung

Copy link
Copy Markdown
Member

Cc @ChrisDenton

GetActiveProcessorCount and GetProcessGroupAffinity ignore some of their
arguments (the group number, the group array, the return value), so they
are only correct for the way std calls them. Gate both on frame_in_std so
any other caller gets an unsupported error instead.
Move GetActiveProcessorCount and GetProcessGroupAffinity down to the other
std-only shims, and make GetActiveProcessorCount reject anything but
ALL_PROCESSOR_GROUPS instead of ignoring the argument.
@RalfJung

Copy link
Copy Markdown
Member

Thanks :) Miri changes LGTM.

@valentynkit

Copy link
Copy Markdown
Contributor Author

Thanks :) Miri changes LGTM.

Thanks, any actions remain from my side, or just waiting for other review?

@RalfJung

RalfJung commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Yeah the libs part still needs to be reviewed.

You picked a specific reviewer so this may take longer. Usually we use random reviewer assignment which keeps review load more evenly balanced.

Also Cc @ChrisDenton because Windows.
EDIT: Oops I already did that, sorry for the double-ping.

@workingjubilee workingjubilee left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apologies for the delay. I'm not sure this PR is correct, and in any case it could use some modest cleanup.

View changes since this review

Comment on lines +86 to +88
// A process in more than one group fails this call with ERROR_INSUFFICIENT_BUFFER
// and writes the real group count into `group_count`; every other case leaves the
// initial `1`, so the return value can be ignored.

@workingjubilee workingjubilee Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It was not immediately obvious to me that group_count and group formed a sort of &mut [u16]. This is one rewording that clarifies that by connecting the dots between each reference to "one", and makes clear why GetProcessGroupAffinity works here. Restating that we can ignore the return value, when we are obviously ignoring the return value, and have already stated what we actually are looking for, is fruitless.

Suggested change
// A process in more than one group fails this call with ERROR_INSUFFICIENT_BUFFER
// and writes the real group count into `group_count`; every other case leaves the
// initial `1`, so the return value can be ignored.
// If there are more than `group_count` processor groups, this fails with ERROR_INSUFFICIENT_BUFFER
// and writes the real group count into `group_count`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

( If you want to specifically draw attention to "this value is being discarded after evaluation", let _ = expr; works too. )

Comment on lines +82 to +83
// group by default; since then it spans every group. The number of groups the
// process is assigned to tells the two apart without an OS-version check.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// group by default; since then it spans every group. The number of groups the
// process is assigned to tells the two apart without an OS-version check.
// group by default; since then it spans every group.

Current wording is ambiguous and suggests this can identify the OS version without actually checking the OS version, but we have no knowledge of version if there are only 64 logical cores. This is irrelevant and can simply be cut down.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alternatively this might be useful information.

Suggested change
// group by default; since then it spans every group. The number of groups the
// process is assigned to tells the two apart without an OS-version check.
// group by default; since then it spans every group.
// Fortunately processtopologyapi.h itself dates back to Windows 7.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also see later comment where I determine that a process before Win11 can in fact span multiple groups.

// The process spans every group, so count the active CPUs across all of them.
c::GetActiveProcessorCount(c::ALL_PROCESSOR_GROUPS) as usize
} else {
// A single group holds every CPU the process can use by default.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// A single group holds every CPU the process can use by default.
// The process is confined to a single process group.

Comment on lines +91 to +99
if group_count > 1 {
// The process spans every group, so count the active CPUs across all of them.
c::GetActiveProcessorCount(c::ALL_PROCESSOR_GROUPS) as usize
} else {
// A single group holds every CPU the process can use by default.
let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed();
c::GetSystemInfo(&mut sysinfo);
sysinfo.dwNumberOfProcessors as usize
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That said, can't this instead be

    c::GetActiveProcessorCount(if group_count > 1 { c::ALL_PROCESSOR_GROUPS } else { group }) as usize

Erm, except probably wrapped...

Except now I'm reading this and I'm not sure this assumption about how the handling works for pre-Win11 is correct. It seems that you can in fact use more than 1 group, you just needed to explicitly configure the process affinity masks and such, so we presumably would want to identify the amount available to the process in its current configuration? https://learn.microsoft.com/en-us/windows/win32/procthread/processor-groups

c::GetProcessGroupAffinity(c::GetCurrentProcess(), &mut group_count, &mut group);

if group_count > 1 {
// The process spans every group, so count the active CPUs across all of them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah this comment is just wrong, we need a better handling here unless we assume Windows 11 or later, which we currently aren't.

@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 Oct 30, 2026
@rustbot

rustbot commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-windows Operating system: Windows 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.

std::thread::available_parallelism() is wrong about Windows 11/Server 2022

4 participants