Introduction

This is a basic MFC program that demonstrates how to retrieve the current CPU load percentage and set it using a high priority thread control loop. I created this project simply because I needed to test another fairly time/buffer critical application and wanted to simulate heavy amounts of CPU load and/or a slower computer. By using this app, you can successfully "steal" away certain amounts of processor instructions to emulate slower or more heavily loaded machines.

It only fully works on single core systems. I personally have a dual core machine, and therefore the one thread that runs in this program can never utilize more than 50% of the "Total Processor Load." I tried launching multiple threads that would hopefully use both cores, but it didn't work - I'm guessing you would have to somehow instruct a 2nd thread to use your second core. Anyway, it wasn't worth it for me to implement, so if anyone wants to add full support for multi-core machines, please post how it's done to others.

Included is a project built using VS 2005. The code is very simple and could be easily added to another project. To retrieve the CPU load percentage, you must include the dependency pdh.lib in your Project->Linker settings.

About the Author

Comments and Discussions

Hi,
I am using xp. when i open task manager->Performance,View->CPU history->One graph per CPU, it is whoing two seperate graphs. But, i have only one processor in my machine. When i run this tool, the total load is going on 2nd processor, so maximum load can be 50%. I created two instances of this load tool, set affinity of 1st as CPU0 and 2nd as CPU1. Still the entire load is going on 2nd processor only.
How to put 90-100% load on my CPU.

Sorry for late reply. The calls to measure CPU load are somewhat black magic to me as it is merely a call to the Windows API.

The function OnTimer is called every ~1 second to update the CPU load percentage. This function calls an internal function GetCPUCycle() which then calls the windows API functions PdhCollectQueryData() and PdhGetRawCounterValue() to get the CPU load percentage. These Pdh functions are where all the magic happens and unfortunately I am not very familiar with them as I just used an example for how to measure CPU load for the sake of controlling it. Perhaps the implementation is wrong. I would suggest looking in MSDN documentation for an example of how to use these. It may be a problem in your version of MFC or possible of the DLL that the Pdh functions use. The person in the above post may have experienced this same problem when he or she had so much "trouble" porting to VC6.0.

I tried setting the thread affinity with no luck. Based on the documentation, I seem to have set everything up correctly - the following code detects my two processors, and starts two threads setting up each to run on a different processor, but the load still maxes out at 50%. I setup the load thread to run a while(1){} loop just to make sure and still can't go above 50%. The thread affinity on the process and the thread prior to calling SetThreadAffinity() was set to 0x03, which should have meant they would run on both processors anyway. Maybe I just missed something in the documentation... any suggestions from anyone with more experience w/ multicores?

//Get the current processor and system affinity to determine what processors are avaialble
//and which this process can use
HANDLE proc = GetCurrentProcess();
GetProcessAffinityMask(proc, &procmask, &systemmask);

//Count the number of bits set to get the total number of processors on the system
DWORD temp = systemmask;
int numprocs = 0;
while (temp)
{
if (temp & 0x00000001) //increment count for each bit (processor) set
numprocs++;
temp >>= 1;
}

Just use the task manager. You can set which programm should be run on which core (works with my core 2 duo). I don't think core 2 duos have hyperthreading, btw, but even if they did, you'd see in the task manager.

EDIT: Obviously, you'll need to start two versions of the programm, one for each core. Set one instance to core 1 and the other to core 2.