I'm trying to write a program that will stress test ALL logical cores on a system in C# (Up to 72 logical cores) and no matter what, System.Environment.ProcessorCount()
only returns 32, no matter what. I ran it on a system with 40 cores and 72 cores and it only sees 32 cores.
I even tried running it on core 32 (index 0), for example, and it wraps around and stress tests CPU0 Node0 again.
Anyone have any ideas how to stress test ALL cores? I'm using the logic I found in this article (http://omegacoder.com/?p=94)
Tried compiling as 64 bit and still nothing.
Edit: Added Code Sample Below: Usage: (Pass in the CPU number -> 31 = CPU12 Node1) System has 20 logical cores per physical processor)
public static void Usage(int cpuToStart) {
// set cpu
int cpu = cpuToStart;
ThreadProcessor tp = new ThreadProcessor();
// Spikes CPU 1
Console.WriteLine("Spike CPU 1");
tp.SpikeCPU(cpu);
// ouput error
if (tp._ex != null) {
Console.WriteLine(tp._ex.Message);
}
// No error
else {
// if multiple processors (logical cores)
if (Environment.ProcessorCount > 1) {
while (++cpu < Environment.ProcessorCount) {
Thread.Sleep(1000);
// Spike each CPU
Console.WriteLine("Spike CPU " + (cpu + 1).ToString());
tp.SpikeCPU(cpu);
if (tp._ex != null) {
Console.WriteLine(tp._ex.Message);
break;
}
}
}
else // Either a single CPU or hyperthreading not enabled in the OS or the BIOS.
{
Console.WriteLine("This PC does not have two processors available.");
}
}
}
Spike CPU:
public void SpikeCPU(int targetCPU) {
// Create a worker thread for the work.
_worker = new Thread(DoBusyWork);
// Background is set so not to not prevent the
// mainprocess from terminating if someone closes it.
_worker.IsBackground = true;
_worker.Start((object)targetCPU);
_worker.Join(); // Wait for it to be done.
}
DoWork
public void DoBusyWork(object target) {
try {
int processor = (int)target;
Thread tr = Thread.CurrentThread;
if (Environment.ProcessorCount > 1) {
SetThreadAffinityMask(GetCurrentThread(),
new IntPtr(1 << processor));
}
CalculatePI.Process(PiSignificantDigits);
}
catch (Exception ex) {
_ex = ex;
}
}
Maybe this will help everyone understand. I've labeled every CPU when I spike them up to 39 (40th CPU):