Detect number of CPUs in .NET 1.1

  • Thread starter Thread starter Kevin Jackson
  • Start date Start date
K

Kevin Jackson

Hello,

How does one go about determining the number of processors a server has in
..NET???

Thanks
 
Hi,

Use WMI extensions (Remember to add reference to the System.Management.dll)

-- BEGIN CODE --

System.Management.SelectQuery query =
new System.Management.SelectQuery("SELECT NumberOfProcessors FROM
Win32_ComputerSystem");

System.Management.ManagementObjectSearcher searcher =
new System.Management.ManagementObjectSearcher(query);

System.Management.ManagementObjectCollection results = searcher.Get();

foreach (System.Management.ManagementBaseObject obj in results)
{
foreach (System.Management.PropertyData data in obj.Properties)
{
if (data.Value == null)
listBox1.Items.Add(data.Name + "=");
else
listBox1.Items.Add(data.Name + "=" + data.Value.ToString());
}
}

-- END CODE --

Hope this helps
 
You can also use WINAPI

add
using System.Runtime.InteropServices;
in using section
-- BEGIN CODE --

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}

[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

private uint GetNuberOfProcessors()
{
SYSTEM_INFO info = new SYSTEM_INFO();
GetSystemInfo(ref info);
return info.dwNumberOfProcessors;
}

-- END CODE --

hope this helps
 
Excellent, that was exactly what I was looking for.

Thanks


Milosz Skalecki said:
You can also use WINAPI

add
using System.Runtime.InteropServices;
in using section
-- BEGIN CODE --

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}

[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

private uint GetNuberOfProcessors()
{
SYSTEM_INFO info = new SYSTEM_INFO();
GetSystemInfo(ref info);
return info.dwNumberOfProcessors;
}

-- END CODE --

hope this helps
--
Milosz Skalecki
MCP, MCAD


Kevin Jackson said:
Hello,

How does one go about determining the number of processors a server has
in
..NET???

Thanks
 
Well, this count includes hyperthreaded procs.

Anyone know of code that can distinguish between real and hyperthreaded
procs?

Thanks

Milosz Skalecki said:
You can also use WINAPI

add
using System.Runtime.InteropServices;
in using section
-- BEGIN CODE --

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}

[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

private uint GetNuberOfProcessors()
{
SYSTEM_INFO info = new SYSTEM_INFO();
GetSystemInfo(ref info);
return info.dwNumberOfProcessors;
}

-- END CODE --

hope this helps
--
Milosz Skalecki
MCP, MCAD


Kevin Jackson said:
Hello,

How does one go about determining the number of processors a server has
in
..NET???

Thanks
 
Kevin said:
Well, this count includes hyperthreaded procs.

Anyone know of code that can distinguish between real and hyperthreaded
procs?

Thanks

Milosz Skalecki said:
You can also use WINAPI

add
using System.Runtime.InteropServices;
in using section
-- BEGIN CODE --

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}

[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

private uint GetNuberOfProcessors()
{
SYSTEM_INFO info = new SYSTEM_INFO();
GetSystemInfo(ref info);
return info.dwNumberOfProcessors;
}

-- END CODE --

hope this helps
--
Milosz Skalecki
MCP, MCAD


Kevin Jackson said:
Hello,

How does one go about determining the number of processors a server has
in
..NET???

Thanks
Not just hyperthreaded CPUs (or virtual processors; and obviously two
physical CPUs too) but also dual core CPUs. Same story if you'd use WMI
to get the data. The only ways I know isn't exactly quick or simple (low
level stuff - rolling your own fancy class using various CPUID calls and
checks depending on VendorID/model; but there are also functions like
GetLogicalProcessorInformation). There's only so many ways to get the
information... I wish WMI's support improves in this area but I doubt
we'll see that unfortunately.
 
Yep you're right. I found some code snipets on Intel's site. Looks quite
involved. SQL Server is one application that knows the difference. Looks
like a challenge for someone to wrap this in a .NET class and then give it
back to the public domain. If I weren't so busy I would tackle it.

I'll keep looking to see if someone has already done it.


john smith said:
Kevin said:
Well, this count includes hyperthreaded procs.

Anyone know of code that can distinguish between real and hyperthreaded
procs?

Thanks

Milosz Skalecki said:
You can also use WINAPI

add
using System.Runtime.InteropServices;
in using section
-- BEGIN CODE --

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}

[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

private uint GetNuberOfProcessors()
{
SYSTEM_INFO info = new SYSTEM_INFO();
GetSystemInfo(ref info);
return info.dwNumberOfProcessors;
}

-- END CODE --

hope this helps
--
Milosz Skalecki
MCP, MCAD


:

Hello,

How does one go about determining the number of processors a server has
in
..NET???

Thanks
Not just hyperthreaded CPUs (or virtual processors; and obviously two
physical CPUs too) but also dual core CPUs. Same story if you'd use WMI to
get the data. The only ways I know isn't exactly quick or simple (low
level stuff - rolling your own fancy class using various CPUID calls and
checks depending on VendorID/model; but there are also functions like
GetLogicalProcessorInformation). There's only so many ways to get the
information... I wish WMI's support improves in this area but I doubt
we'll see that unfortunately.
 
You can use WMI for this, you just have to interpret the ProcessorId
attribute (from win32_processor) returned from a call to cpuid.

For instance this is returned on my box.

ProcessorId = "178BFBFF00000F7A";

This represents the contents of the CpuInfo array index 3 and 0 for infotype
1.

The first four bytes are the index 3 DWORD values and the last four bytes
are the index 0 DWORD values.
So the first DWORD (index 3) are the feature bits.
the highest byte with value 17 (00010111) means: multicore, SSE2, SSE and
FXSAVE/FXRSTOR
Check MSDN for details on __cpuid() C runtime built-in and a sample.


The v2.0 CLR also knows the difference as it calls the (v2.0) C runtime
built-in __cpuid(). So if you realy needs the index 1 and 2 DWORDS you can
wrapp the __cpuid() call using PInvoke on V2.0


Willy.

| Yep you're right. I found some code snipets on Intel's site. Looks quite
| involved. SQL Server is one application that knows the difference. Looks
| like a challenge for someone to wrap this in a .NET class and then give it
| back to the public domain. If I weren't so busy I would tackle it.
|
| I'll keep looking to see if someone has already done it.
|
|
| | > Kevin Jackson wrote:
| >> Well, this count includes hyperthreaded procs.
| >>
| >> Anyone know of code that can distinguish between real and hyperthreaded
| >> procs?
| >>
| >> Thanks
| >>
| >> | >>> You can also use WINAPI
| >>>
| >>> add
| >>> using System.Runtime.InteropServices;
| >>> in using section
| >>> -- BEGIN CODE --
| >>>
| >>> [StructLayout(LayoutKind.Sequential)]
| >>> public struct SYSTEM_INFO
| >>> {
| >>> public uint dwOemId;
| >>> public uint dwPageSize;
| >>> public uint lpMinimumApplicationAddress;
| >>> public uint lpMaximumApplicationAddress;
| >>> public uint dwActiveProcessorMask;
| >>> public uint dwNumberOfProcessors;
| >>> public uint dwProcessorType;
| >>> public uint dwAllocationGranularity;
| >>> public uint dwProcessorLevel;
| >>> public uint dwProcessorRevision;
| >>> }
| >>>
| >>> [DllImport("kernel32")]
| >>> static extern void GetSystemInfo(ref SYSTEM_INFO pSI);
| >>>
| >>> private uint GetNuberOfProcessors()
| >>> {
| >>> SYSTEM_INFO info = new SYSTEM_INFO();
| >>> GetSystemInfo(ref info);
| >>> return info.dwNumberOfProcessors;
| >>> }
| >>>
| >>> -- END CODE --
| >>>
| >>> hope this helps
| >>> --
| >>> Milosz Skalecki
| >>> MCP, MCAD
| >>>
| >>>
| >>> "Kevin Jackson" wrote:
| >>>
| >>>> Hello,
| >>>>
| >>>> How does one go about determining the number of processors a server
has
| >>>> in
| >>>> ..NET???
| >>>>
| >>>> Thanks
| >>>>
| >>>>
| >>>>
| >>
| >>
| > Not just hyperthreaded CPUs (or virtual processors; and obviously two
| > physical CPUs too) but also dual core CPUs. Same story if you'd use WMI
to
| > get the data. The only ways I know isn't exactly quick or simple (low
| > level stuff - rolling your own fancy class using various CPUID calls and
| > checks depending on VendorID/model; but there are also functions like
| > GetLogicalProcessorInformation). There's only so many ways to get the
| > information... I wish WMI's support improves in this area but I doubt
| > we'll see that unfortunately.
|
|
 
WMI returns the (part of) cpuid stuff in the ProcessorId property of the
Win32_Process instance, this makes it possible to check if the processor is
a HT/Multicore.
See my other reply for details.

Willy.


| Kevin Jackson wrote:
| > Well, this count includes hyperthreaded procs.
| >
| > Anyone know of code that can distinguish between real and hyperthreaded
| > procs?
| >
| > Thanks
| >
| > | >> You can also use WINAPI
| >>
| >> add
| >> using System.Runtime.InteropServices;
| >> in using section
| >> -- BEGIN CODE --
| >>
| >> [StructLayout(LayoutKind.Sequential)]
| >> public struct SYSTEM_INFO
| >> {
| >> public uint dwOemId;
| >> public uint dwPageSize;
| >> public uint lpMinimumApplicationAddress;
| >> public uint lpMaximumApplicationAddress;
| >> public uint dwActiveProcessorMask;
| >> public uint dwNumberOfProcessors;
| >> public uint dwProcessorType;
| >> public uint dwAllocationGranularity;
| >> public uint dwProcessorLevel;
| >> public uint dwProcessorRevision;
| >> }
| >>
| >> [DllImport("kernel32")]
| >> static extern void GetSystemInfo(ref SYSTEM_INFO pSI);
| >>
| >> private uint GetNuberOfProcessors()
| >> {
| >> SYSTEM_INFO info = new SYSTEM_INFO();
| >> GetSystemInfo(ref info);
| >> return info.dwNumberOfProcessors;
| >> }
| >>
| >> -- END CODE --
| >>
| >> hope this helps
| >> --
| >> Milosz Skalecki
| >> MCP, MCAD
| >>
| >>
| >> "Kevin Jackson" wrote:
| >>
| >>> Hello,
| >>>
| >>> How does one go about determining the number of processors a server
has
| >>> in
| >>> ..NET???
| >>>
| >>> Thanks
| >>>
| >>>
| >>>
| >
| >
| Not just hyperthreaded CPUs (or virtual processors; and obviously two
| physical CPUs too) but also dual core CPUs. Same story if you'd use WMI
| to get the data. The only ways I know isn't exactly quick or simple (low
| level stuff - rolling your own fancy class using various CPUID calls and
| checks depending on VendorID/model; but there are also functions like
| GetLogicalProcessorInformation). There's only so many ways to get the
| information... I wish WMI's support improves in this area but I doubt
| we'll see that unfortunately.
 
Back
Top