How do you detect the number of physical processors/cores in .net?
You can use PowerShell to access comprehensive processor information. For example, you can run the following command to get the number of CPU cores:
Get-WmiObject -namespace root\CIMV2 -class Win32_Processor -Property NumberOfCores
It's much easier to research WMI when using some kind of explorer tool. So, I can suggest using WMI browsing tool (e.g. WMIExplorer or WMI CIM Studio) to explore WMI classes, properties and methods.
While Environment.ProcessorCount
will indeed get you the number of virtual processors in the system, that may not be the number of processors available to your process. I whipped up a quick little static class/property to get exactly that:
using System;
using System.Diagnostics;
/// <summary>
/// Provides a single property which gets the number of processor threads
/// available to the currently executing process.
/// </summary>
internal static class ProcessInfo
{
/// <summary>
/// Gets the number of processors.
/// </summary>
/// <value>The number of processors.</value>
internal static uint NumberOfProcessorThreads
{
get
{
uint processAffinityMask;
using (var currentProcess = Process.GetCurrentProcess())
{
processAffinityMask = (uint)currentProcess.ProcessorAffinity;
}
const uint BitsPerByte = 8;
var loop = BitsPerByte * sizeof(uint);
uint result = 0;
while (--loop > 0)
{
result += processAffinityMask & 1;
processAffinityMask >>= 1;
}
return (result == 0) ? 1 : result;
}
}
}
System.Environment.ProcessorCount is what you need
This actually varies quite a bit depending on the target platform. Stephbu's answer will work great on XP SP3 and newer.
If you are targeting older platforms, you may want to check out this article. I wrote it about half a year ago and in it I discuss several different ways to do this as well as the individual pros and cons of each method.
You may also want to check out this code project article if you are interested in differentiating shadow cores from hyperthreading from real ones.
System.Environment.ProcessorCount
returns the number of logical processors
http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx
For physical processor count you'd probably need to use WMI - the following metadata is supported in XP/Win2k3 upwards (Functionality enabled in SP's prior to Vista/Win2k8).
Win32_ComputerSystem.NumberOfProcessors returns physical count
Win32_ComputerSystem.NumberOfLogicalProcessors returns logical (duh!)
Be cautious that HyperThreaded CPUs appear identical to multicore'd CPU's yet the performance characteristics are very different.
To check for HT-enabled CPUs examine each instance of Win32_Processor and compare these two properties.
Win32_Processor.NumberOfLogicalProcessors
Win32_Processor.NumberOfCores
On multicore systems these are typically the same the value.
Also, be aware of systems that may have multiple Processor Groups, which is often seen on computers with a large number of processors. By default .Net will only using the first processor group - which means that by default, threads will utilize only CPUs from the first processor group, and Environment.ProcessorCount
will return only the number of CPUs in this group. According to Alastair Maw's answer, this behavior can be changed by altering the app.config as follows:
<configuration>
<runtime>
<Thread_UseAllCpuGroups enabled="true"/>
<GCCpuGroup enabled="true"/>
<gcServer enabled="true"/>
</runtime>
</configuration>
Environment.ProcessorCount will also include any hyperthreaded processors.
There is no way (at least up through Windows 2003) to distinguish a hyperthreaded processor from one with two cores.