Runtime: [Proposal] Expose cpuid

Created on 12 Aug 2020  ·  3Comments  ·  Source: dotnet/runtime

Background and Motivation

For some specific scenarios it's important to know the CPU Vendor / CPUID. The .NET Runtime itself also checks the CPUID already:
https://github.com/dotnet/runtime/blob/96f178d32b7ba62485917ac46ef1edcfd3c2d10d/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/X86/X86Base.cs#L71-L81
https://github.com/dotnet/runtime/blob/96f178d32b7ba62485917ac46ef1edcfd3c2d10d/src/tests/JIT/HardwareIntrinsics/X86/X86Base/CpuId.cs#L27-L32

Because of that, it would be a small afford to expose this information. Specific scenarios where it matters:

  • Dealing with hardware intrinsic code, the performance can be different whether the CPU is intel or amd.
  • Disassembling and assembling / patching code via detours it is absolutely necessary to know if the CPU supports a vendor specific instruction set or not
  • AOT (future, i.e. patching its own code)

There might be other scenarios. When I searched for a way I found a couple of StackOverflow discussion about this.

The current "official" pure C# way for retrieving the CPUID is via using ManagementClass. The downside of this method is that its slow and only works on Windows (and is because of that not supported in .NET Core, as far as I know) //edit: Turns out its supported via nuget package, still limited to Windows only. So I'm proposing the expose a property which uses the same existing internal way of retrieving the cpuid as shown above.

Proposed API

I'm not quite sure where such a property fits into, so I've put them into Intrinsics namespace, if you have a better idea please feel free to let me know so I can edit it.

namespace System.Runtime.Intrinsics.X86
{
+    public static class CpuInfo
+    {
+        public enum CpuVendor 
+        {
+            AMD,
+            Intel
+            Unknown
+        }
+
+        public CpuVendor Id { }
+    }
}

Usage Examples

``` C#
if (CpuInfo.Id == CpuVendor.AMD)
{

}
else if (CpuInfo.Id == CpuVendor.Intel)
{

}
else
throw new NotSupportedException();
```

Alternative Designs

None

Risks

None

api-suggestion untriaged

Most helpful comment

That first method that you linked is a public API 😅 See #785, which was implemented last week.

All 3 comments

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

That first method that you linked is a public API 😅 See #785, which was implemented last week.

That first method that you linked is a public API 😅 See #785, which was implemented last week.

I wondered about that, couldn't find it in .NET 5 Preview 7, explains why 🤦‍♂️

Was this page helpful?
0 / 5 - 0 ratings