如何判断运行我的程序的机器是否使用超线程? (C#)
我有一些代码需要知道我的特定机器上有多少实际可用的核心,以及是否启用了超线程。
有没有办法在 C# 中做到这一点?
更新:这些机器是 XP 和 Vista 的混合体
更新:在其中一台机器(但不是所有机器)上访问“Win32_Processor.NumberOfCores”或“Win32_Processor.NumberOfLogicalProcessors”会引发异常(带有消息“Not Found”的 ManagmentException) )
I have some code that needs to know how many actual cores are available on my particular machine, and whether or not Hyperthreading is enabled.
Is there a way to do this in C#?
Update: The machines are a mix of XP and Vista
Update: Accessing 'Win32_Processor.NumberOfCores' or 'Win32_Processor.NumberOfLogicalProcessors' throws an exception (a ManagmentException with the message "Not Found") on one of the machines (but not all of them)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Vista 及更高版本上,您可以通过 PInvoke 使用 GetLogicalProcessorInformation 来获取逻辑处理器单元的数量。
在 Windows XP 上,无法通过 C# 可靠地区分超线程与其他多处理器/核心配置。 有人发布的 WMI 解决方案将多核处理器归类为超线程。
在 Vista 之前,唯一可靠的方法是检查处理器的 CPUID。 要使用它,您可以创建一个可以从托管代码中调用的本机 DLL。 以下英特尔代码 示例 将是良好的起点。
On Vista and higher you can use GetLogicalProcessorInformation via PInvoke to get the number of logical processor units.
On Windows XP there's no way via C# to reliably differentiate hyper-threading from other multi-processor/core configurations. The WMI solution that someone posted will class multi-core processors as hyper-threaded.
Prior to Vista the only reliable means is to check the CPUID of the processor. To use this you could create a native DLL that can be called from your managed code. The following Intel code sample would be a good starting point.
至少对第一个问题进行简单回答:Environment.ProcessorCount 应该返回计算机上的核心数量。
编辑:此处 是一种非基于 WMI 的方法,用于检查是否启用了超线程(并不是说它一定更好)。 另请参阅此< /a> 文章。
Simple answer to the first question at least: Environment.ProcessorCount should return the number of cores on the machine.
Edit: Here's a non-WMI-based method of checking for whether Hyperthreading is enabled (not that it's any nicer necessarily). Also see this article.
System.Environment.ProcessorCount 将告诉您运行代码的计算机上有多少个内核。
System.Environment.ProcessorCount will tell you how many cores exist on the machine the code is running on.
检查Environment.ProcessorCount属性,它会返回一个整数,至于超线程,我不确定。
Check the Environment.ProcessorCount property, it will return an integer, as far as HyperThreading, I'm not sure.
StackOverflow 问题 188503 有您需要的信息...
引用该问题的最佳答案:
返回逻辑处理器的数量(请参阅 MSDN )
为了区分超线程和独立核心,听起来好像您需要一些 WMI。
StackOverflow question 188503 has the information you need ...
Quoting the top answer on that question:
returns the number of logical processors (see MSDN)
To distinguish between Hyperthreaded and separate cores, sounds as though you need a bit of WMI.
GetLogicalProcessorInformation 对于 HT 方面来说已经足够了,但遗憾的是它仅适用于XP SP3,64 位 XP/Vista/Server 2003(我相信在 Vista 之前略有损坏)
Joe Duffy 将其封装在 c# 中,但尚未发布源代码,尽管 Mark Russinovich 已经发布了他用它创建的工具 (Coreinfo),您可能可以将其反编译为请参阅代码。
GetLogicalProcessorInformation is sufficient for the HT aspect but sadly it is only available in XP SP3, 64bit XP/Vista/Server 2003 (and I believe is is slightly broken pre vista)
Joe Duffy wrapped this in c# but has not yet released the source, though Mark Russinovich has released the tool (Coreinfo) he created with it, likely you can decompile that to see the code.