Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Management;
using System.Runtime.InteropServices;

namespace RetrievalOfCpuCores;

Expand All @@ -7,16 +8,17 @@ public class CpuInformation
const string wmiQuery = "Select * from Win32_ComputerSystem";

/// <summary>
/// Retrieves the number of logical processors of the CPU by using Environment.ProcessorCount.
/// Retrieves the number of processors available to the current process by using Environment.ProcessorCount.
/// </summary>
/// <returns>number of logical processors</returns>
/// <returns>number of processors available to the process</returns>
public int GetProcessorsCount()
{
return Environment.ProcessorCount;
}

/// <summary>
/// Retrieves the number of logical processors of the CPU.
/// Retrieves the number of logical processors of the machine.
/// This is only supported on Windows.
/// </summary>
/// <returns>number of logical processors</returns>
public int GetLogicalProcessors()
Expand All @@ -26,17 +28,18 @@ public int GetLogicalProcessors()
throw new PlatformNotSupportedException();
}

foreach (var item in new System.Management.ManagementObjectSearcher(wmiQuery).Get())
using var searcher = new ManagementObjectSearcher(wmiQuery);
foreach (var item in searcher.Get())
{
return int.Parse(item["NumberOfLogicalProcessors"].ToString());
return Convert.ToInt32(item["NumberOfLogicalProcessors"]);
}

return 0;
}

/// <summary>
/// Retrieves the number of cores of the CPU.
/// This is only supported on windows.
/// This is only supported on Windows.
/// </summary>
/// <returns>number of cores of the CPU</returns>
public int GetNumberOfCores()
Expand All @@ -47,18 +50,18 @@ public int GetNumberOfCores()
}

var coreCount = 0;
const string wmiQuery = "Select * from Win32_Processor";
foreach (var item in new System.Management.ManagementObjectSearcher(wmiQuery).Get())
using var searcher = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (var item in searcher.Get())
{
coreCount += int.Parse(item["NumberOfCores"].ToString());
coreCount += Convert.ToInt32(item["NumberOfCores"]);
}

return coreCount;
}

/// <summary>
/// Retrieves the number of physical processors of the CPU.
/// This is only supported on windows.
/// This is only supported on Windows.
/// </summary>
/// <returns>number of physical processors</returns>
public int GetPhysicalProcessors()
Expand All @@ -68,19 +71,21 @@ public int GetPhysicalProcessors()
throw new PlatformNotSupportedException();
}

foreach (var item in new System.Management.ManagementObjectSearcher(wmiQuery).Get())
using var searcher = new ManagementObjectSearcher(wmiQuery);
foreach (var item in searcher.Get())
{
return int.Parse(item["NumberOfProcessors"].ToString());
return Convert.ToInt32(item["NumberOfProcessors"]);
}

return 0;
}

/// <summary>
/// Retrieves the number of logical processors of the CPU, including also the excluded processors.
/// Retrieves the number of logical processors of the CPU, including the excluded processors.
/// This is only supported on Windows.
/// </summary>
/// <returns></returns>
public int GetExcludedProcessors()
/// <returns>number of logical processors, including the excluded ones</returns>
public int GetLogicalProcessorsIncludingExcluded()
{
if (!OperatingSystem.IsWindows())
{
Expand Down Expand Up @@ -114,6 +119,16 @@ public int GetExcludedProcessors()
return deviceCount;
}

/// <summary>
/// Retrieves the number of logical processors that are excluded from Windows.
/// This is only supported on Windows.
/// </summary>
/// <returns>number of excluded processors</returns>
public int GetExcludedProcessors()
{
return GetLogicalProcessorsIncludingExcluded() - GetLogicalProcessors();
}

[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPStr)] string enumerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

CpuInformation cpuInformation = new();

Console.WriteLine("Processors Count: {0}", cpuInformation.GetProcessorsCount());
Console.WriteLine("Processors Count: {0}", Environment.ProcessorCount);

Console.WriteLine("Logical Processors: {0}", cpuInformation.GetLogicalProcessors());

Console.WriteLine("Number Of Cores: {0}", cpuInformation.GetNumberOfCores());

Console.WriteLine("Physical Processors: {0}", cpuInformation.GetPhysicalProcessors());

Console.WriteLine("Logical Processors (including excluded Processors): {0}", cpuInformation.GetExcludedProcessors());
Console.WriteLine("Logical Processors (including excluded Processors): {0}", cpuInformation.GetLogicalProcessorsIncludingExcluded());

Console.WriteLine("Excluded Processors: {0}", cpuInformation.GetExcludedProcessors());

var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
Parallel.For(0, 4, options, i => Console.WriteLine("Work item {0} done", i));
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Management" Version="9.0.0-preview.1.24080.9" />
<PackageReference Include="System.Management" Version="11.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void WhenGetProcessorCount_ResultIsGreaterThanZero()
}

[TestMethod]
public void WhenGetLogicalProcessors_LogicalProcessorEqualToEnvironmentProcessorCount()
public void WhenGetLogicalProcessors_ResultIsAtLeastEnvironmentProcessorCount_Live()
{
if (!OperatingSystem.IsWindows())
{
Expand All @@ -24,11 +24,11 @@ public void WhenGetLogicalProcessors_LogicalProcessorEqualToEnvironmentProcessor
var logicalProcessors = cpuInformation.GetLogicalProcessors();
var processorsCount = cpuInformation.GetProcessorsCount();

Assert.AreEqual(logicalProcessors, processorsCount);
Assert.IsTrue(processorsCount <= logicalProcessors);
}

[TestMethod]
public void WhenGetLogicalProcessors_ResultIsGreaterThanZero()
public void WhenGetLogicalProcessors_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
Expand All @@ -43,7 +43,7 @@ public void WhenGetLogicalProcessors_ResultIsGreaterThanZero()


[TestMethod]
public void WhenGetPhysicalProcessors_ResultIsGreaterThanZero()
public void WhenGetPhysicalProcessors_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
Expand All @@ -57,7 +57,7 @@ public void WhenGetPhysicalProcessors_ResultIsGreaterThanZero()
}

[TestMethod]
public void WhenGetProcessorsCount_ResultIsGreaterThanZero()
public void WhenGetProcessorsCount_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
Expand All @@ -71,17 +71,31 @@ public void WhenGetProcessorsCount_ResultIsGreaterThanZero()
}

[TestMethod]
public void WhenGetProcessorsCountWithExcludedProcessors_ResultIsGreaterThanZero()
public void WhenGetProcessorsCountWithExcludedProcessors_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var cpuInformation = new CpuInformation();
var processorsCount = cpuInformation.GetExcludedProcessors();
var processorsCount = cpuInformation.GetLogicalProcessorsIncludingExcluded();

Assert.IsTrue(processorsCount > 0);
}

[TestMethod]
public void WhenGetExcludedProcessors_ResultIsNotNegative_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var cpuInformation = new CpuInformation();
var excludedProcessors = cpuInformation.GetExcludedProcessors();

Assert.IsTrue(excludedProcessors >= 0);
}
}
}
10 changes: 5 additions & 5 deletions dotnet-platform-specific/RetrievalOfCpuCores/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading