简体   繁体   English

如何判断我的程序所运行的计算机是否使用超线程? (C#)

[英]How do I tell if the machine my program is running on uses hyperthreading? (C#)

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#? 有没有办法在C#中做到这一点?

Update: The machines are a mix of XP and Vista 更新:这些机器是XP和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) 更新:访问“ Win32_Processor.NumberOfCores”或“ Win32_Processor.NumberOfLogicalProcessors”会在其中一台计算机(但不是全部)上引发异常(带有消息“未找到”的ManagmentException)

On Vista and higher you can use GetLogicalProcessorInformation via PInvoke to get the number of logical processor units. 在Vista或更高版本上,可以通过PInvoke使用GetLogicalProcessorInformation来获取逻辑处理器单元的数量。

On Windows XP there's no way via C# to reliably differentiate hyper-threading from other multi-processor/core configurations. 在Windows XP上,无法通过C#可靠地将超线程与其他多处理器/核心配置区分开。 The WMI solution that someone posted will class multi-core processors as hyper-threaded. 有人发布的WMI解决方案会将多核处理器归类为超线程。

Prior to Vista the only reliable means is to check the CPUID of the processor. 在Vista之前,唯一可靠的方法是检查处理器的CPUID。 To use this you could create a native DLL that can be called from your managed code. 要使用此功能,您可以创建可以从托管代码中调用的本机DLL。 The following Intel code sample would be a good starting point. 以下英特尔代码示例将是一个很好的起点。

Simple answer to the first question at least: Environment.ProcessorCount should return the number of cores on the machine. 至少第一个问题的简单答案: Environment.ProcessorCount应该返回计算机上的内核数。

Edit : Here 'sa non-WMI-based method of checking for whether Hyperthreading is enabled (not that it's any nicer necessarily). 编辑是一种非基于WMI的方法,用于检查是否启用了超线程(不是一定更好)。 Also see this article. 另请参阅文章。

System.Environment.ProcessorCount将告诉您运行代码的计算机上存在多少个内核。

检查Environment.ProcessorCount属性,它会返回一个整数,至于HyperThreading,我不确定。

StackOverflow question 188503 has the information you need ... StackOverflow问题188503具有您需要的信息...

Quoting the top answer on that question: 引用该问题的最高答案:

System.Environment.ProcessorCount

returns the number of logical processors (see MSDN ) 返回逻辑处理器的数量(请参阅MSDN

To distinguish between Hyperthreaded and separate cores, sounds as though you need a bit of WMI. 要区分超线程内核和单独的内核,听起来好像需要一点WMI。

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) GetLogicalProcessorInformation对于HT方面已足够,但可悲的是,它仅在XP SP3、64位XP / Vista / Server 2003中可用(并且我相信在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. 乔·达菲(Joe Duffy)将其包装在c#中,但尚未发布源代码,尽管马克·鲁西诺维奇(Mark Russinovich)已发布了他使用它创建的工具(Coreinfo),可能您可以对其进行反编译以查看代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM