-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add Environment.TotalProcessorCount
or similar API, after semantics of Environment.ProcessorCount
has changed
#46283
Comments
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsBackground and MotivationWith #45943 the semantics of Knowing the active physical processor count can be critical: I've come across a problem with a CPU in an embedded system that (due to a BIOS bug) would occassionally only start up with a single core. In that case, the system performance was to low for the application to run reliably and we had to tell the user of this situation. Proposed APIAdd public int TotalProcessorCount
{
get;
} to Usage Examples// Ensure our application runs on all available cores:
int numCores = Environment.TotalProcessorCount;
int affinityMask = (int)(Math.Pow(2, numCores) - 1);
System.Diagnostics.Process.ProcessorAffinity = affinityMask; Alternative DesignsSee above, the feature could be added to other classes. RisksThe whole system of affinity masks is a bit problematic when looking forward, as computers with more than 32 cores are likely to be widely available soon. So this might also require a larger redesign.
|
Win32 has GetProcessAffinityMask and SetThreadAffinityMask but no GetThreadAffinityMask. No such information class in GetThreadInformation, either. |
At least SetThreadAffinityMask can be abused to get the desired information: If the function succeeds, the return value is the thread's previous affinity mask.. While the limitations of the Win32 API may explain the current situation, the C# system APIs should, as far as possible, no longer be designed to match that API, but be based on the requirements of programmers. .NET is no longer Windows-only. |
Related: #22948 |
Background and Motivation
With #45943 the semantics of
Environment.ProcessorCount
is (for all systems) changed to the number of active processors the application can run on, taking the thread affinity mask into account. With this change, there's no more a direct possibility (without using OS-dependent PIvoke calls) to get the number of physical (or logical) CPUs in the system. This also prevents comparing the active vs the available CPU cores (i.e. to check whether an affinity mask has been applied).Knowing the active physical processor count can be critical: I've come across a problem with a CPU in an embedded system that (due to a BIOS bug) would occassionally only start up with a single core. In that case, the system performance was to low for the application to run reliably and we had to tell the user of this situation.
Proposed API
Add
to
System.Environment
. An alternative would be theSystem.Diagnostics.Process
class, which also supports setting the affinity. (Note: How would I know the valid affinity masks if I don't know the number of CPUs?)Usage Examples
Alternative Designs
See above, the feature could be added to other classes.
System.Diagnostics.Process
orSystem.Diagnostics.ProcessThread
would be an option. (Why isSystem.Diagnostics.ProcessThread.ProcessorAffinity
write only, by the way?)Risks
The whole system of affinity masks is a bit problematic when looking forward, as computers with more than 32 cores are likely to be widely available soon. So this might also require a larger redesign.
The text was updated successfully, but these errors were encountered: