Max CPU: Another one for your developer toolbox...

Saturday, October 22, 2005

For various reasons, mostly related to my day job, I often find it useful to simulate excessive CPU usage. I’m sure every developer that has been coding for a while has at one point written the following program.

int main(){ while (true);}

(For the C# programmers out there, this is in fact a perfectly valid C++ program.) Of course today it is common to have multi-processor computers, or at least multi-core or hyper-threaded processors in which case the program above will only max out one of the logical processors on the computer.

For this reason I wrote the Max CPU program to provide a simpler and more convenient way of maxing out my processors.

Single 64-bit dual-core Intel Pentium

Max CPU will determine how many logical processors are currently available and allow you to stress any number of them.

Dual Intel Xeon with Hyper-Threading

The program creates one thread for every processor and each thread will automatically spin or sleep depending on the position of the slider control.

Of course you can set the processor affinity thus limiting the processors that the threads can get executed on but that would be cheating.

:)

And just in case you accidentally raise the process priority, I have lowered the relative priority of the “processor threads” so that they won’t starve your computer.

It doesn’t minimize and defaults to being a top-most window to avoid you forgetting to stop it since you don’t want to be running this little guy in the background just for fun.

Anyway, I find this little tool useful. I have no idea if it will interest anyone else but I thought I’d share it anyway.

Download the executable and save it to a local drive before running it. It requires the .NET Framework 2.0 and will run natively as either a 32-bit or 64-bit process depending on your operating system.

5 Comments

&gt; int main()

&gt; {

&gt; while (true);

&gt; }

But that's inefficient. It has to compute the value of true and compare to true every time it goes through the loop, before never getting around to returning an undefined value to its caller. The following is more efficient:

int main()

{

for (;;);

}

An optimizing compiler would optimize out all of the code that's never reached, so it wouldn't execute any of those useless loops and wouldn't waste any CPU time before not proceeding to never get around to returning a random value to its caller. I guess it would optimize to something like this:

int main()

{

Sleep(INFINITY);

}

But you shouldn't depend on optimizations. You should always check to make sure your program will perform the way you wanted. Like, put profiling options on your compilations and come back after the next big bang to see which parts of your program were executed more infinitely many times than which others.

Oops, sorry I have to add a serious comment after the above. You really do have to set processor affinity to control which processors get affected by what. For example if two separate cores each have one hyperthreading partial CPU wiped out, effects will be different from just one core having both of its hyperthreading partial CPUs wiped out.