This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathEnvironment.ProcessorCount.cs
71 lines (61 loc) · 2.34 KB
/
Environment.ProcessorCount.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
using Xunit;
namespace System.Tests
{
public class EnvironmentProcessorCount
{
[PlatformSpecific(TestPlatforms.Windows)] // Uses P/Invokes to get processor information
[Fact]
public void Windows_ProcessorCountTest()
{
//arrange
SYSTEM_INFO sysInfo = new SYSTEM_INFO();
GetSystemInfo(ref sysInfo);
int expected = sysInfo.dwNumberOfProcessors;
//act
int actual = Environment.ProcessorCount;
//assert
Assert.True(actual > 0);
Assert.Equal(expected, actual);
}
#if Unix
[PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes to get processor information
[Fact]
public void Unix_ProcessorCountTest()
{
//arrange
int _SC_NPROCESSORS_ONLN =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? 84 :
RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")) ? 1002 :
58;
int expected = (int)sysconf(_SC_NPROCESSORS_ONLN);
//act
int actual = Environment.ProcessorCount;
//assert
Assert.True(actual > 0);
Assert.Equal(expected, actual);
}
[DllImport("libc")]
private static extern long sysconf(int name);
#endif
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
internal int dwOemId; // This is a union of a DWORD and a struct containing 2 WORDs.
internal int dwPageSize;
internal IntPtr lpMinimumApplicationAddress;
internal IntPtr lpMaximumApplicationAddress;
internal IntPtr dwActiveProcessorMask;
internal int dwNumberOfProcessors;
internal int dwProcessorType;
internal int dwAllocationGranularity;
internal short wProcessorLevel;
internal short wProcessorRevision;
}
}
}