Contemporary Intel Pentium IV processors support the so called Hyper-Threading mode. With Hyper-Threading, a real physical processor presents itself to the operating system as two independent logical processors. Hyper-Threading technology lessens process switching overhead, increasing overall system throughput. But in some calculation intensive applications, this delusion of two logical processors may lead to the loss of effectiveness if the program will start two parallel threads, which will compete for one physical processor. Thus we have to determine how many physical processors we have in our computer to adjust the number of parallel threads to the number of physical processors. The latter utility shows how we can do it from a C# program.
The CPU Counting utility is based on the following Intel documents and code samples:
Using the code
The CPU Counting utility consists of two parts - the client part and the server part. The client part written in C# calls the server function and displays its results. The server part actually determines the number of processors. It uses ASM commands extensively and therefore is written in managed C++.
The key part of the client is the call to the server function
int LogicalNum = 0; int PhysicalNum = 0; CPUCountStatus HTStatusFlag = 0;
HTStatusFlag = CPUCount.GetCPUCount(ref LogicalNum, ref PhysicalNum);
Don't forget to add namespace usage statement.
Server part is implemented as a separate DLL, containing static function
GetCPUCount of the class
using namespace System;
public __value enum CPUCountStatus
public __value class CPUCount
static CPUCountStatus GetCPUCount(int __gc* LogicalNum,
int __gc* PhysicalNum);
unsigned int HTSupported();
unsigned int LogicalProcPerPhysicalProc();
unsigned int GetAPIC_ID();
CPUCountStatus DefineCPUCount(int __gc* LogicalNum,
int __gc* PhysicalNum);
GetCPUCount returns value of
enum type. Meaning of
CPUCountStatus values are:
|Hyper-threading technology doesn't exist in this system.|
|Hyper-threading technology is enabled in this system.|
|Hyper-threading technology is disabled in the hardware.|
|Hyper-threading technology is supported but disabled in the BIOS.|
|Hyper-threading technology cannot be detected|
To determine hyper-threading possibilities and the number of physical and logical processors in your computer, just copy the files CPUCounterClient.exe and CPUCounter.dll to some directory and run CPUCounterClient.exe.
This is the first version of the CPU counting utility.