Click here to Skip to main content
Click here to Skip to main content

CPU Load Control

By , 22 Feb 2007
 
Screenshot - img.png

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.

History

  • 22nd February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jkhax0r
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiongoodmemberbleachli9 Nov '11 - 5:13 
think you ,
wonderful
GeneralXP is showing two processorsmemberLokanatha Reddy15 May '07 - 23:38 
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.
 
Thanks,
Loka

GeneralCPU Load Ported on Visual C++ 6.0membercristitomi15 Mar '07 - 23:54 
Hi!
I have ported this application in Visual C++ 6.0.
If anyone is interested e-mail me and I will send the zip file containing the application.
It wasn't an easy thing to do.
Best wishes,
Christian.
 
I am in love with VC++
GeneralNot work in one core PC with Windows2000 ProfessionalmemberAleksMain27 Feb '07 - 2:24 
I tested your demo execution file and didn't see any blue bar or percent of loading for processor. Any percent entered by me manually equally didn't work.
 

 
AleksMain

GeneralRe: Not work in one core PC with Windows2000 Professionalmemberjkhax0r2 Apr '07 - 6:38 
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.
GeneralMulticorememberJoergen Sigvardsson22 Feb '07 - 11:00 
Have you tried setting the processor affinity for each thread? Please see SetThreadAffinityMask() in the MSDN documentation. It lets you divert the execution of threads to specific CPUs.
 

GeneralRe: MulticorememberVasudevan Deepak Kumar22 Feb '07 - 18:08 
Yep. That would achieve almost.
 
Vasudevan Deepak Kumar
Personal Homepage
Tech Gossips

QuestionRe: Multicorememberjkhax0r26 Feb '07 - 5:16 
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?
 

//Update to get number of processors
DWORD procmask = 0;
DWORD systemmask = 0;
 
//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;
}
 
if (systemmask != procmask)
SetProcessAffinityMask(proc, systemmask);
 

m_desiredLoad = 0;
HANDLE thread;
DWORD threadaffinity = 0x00000001; //first thread runs on proc1
 
//now create a thread for each processor and set it to run on a unique proc
for (int i = 0; i < numprocs; ++i)
{
thread = CreateThread(NULL, 0, &ThreadProc, this, 0, NULL);
DWORD res = SetThreadAffinityMask(thread, threadaffinity);
threadaffinity <<= 1;
}

AnswerRe: MulticorememberJoergen Sigvardsson26 Feb '07 - 7:29 
Do the new duo-chips have hyperthreading enabled? Windows reports each "hyper unit" as a processor of its own (at least XP does). Maybe you have to adjust for that.
 
Also, what does SetThreadAffinityMask() return? Anything of significance?
 
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören

GeneralRe: MulticorememberGreat-er26 Feb '07 - 23:54 
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.
GeneralRe: MulticorememberJoergen Sigvardsson27 Feb '07 - 2:36 
Great-er wrote:
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.

 
Why so when you direct specific threads to specific CPUs?
 
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören

GeneralRe: Multicore [modified]memberGreat-er27 Feb '07 - 8:56 
hmm, you are right, i couldn't test the program (wasn't at home), so i didn't know it was threaded/had to threads. but i hope i helped, nevertheless.
 
EDIT: If you download the original program, you have to run it twice, because it only starts one thread.
 
-- modified at 15:03 Tuesday 27th February, 2007
GeneralRe: Multicorememberjkhax0r28 Feb '07 - 16:46 
SetThreadAffinity returned 0x03. Based on the documentation, the return value was the previous thread affinity which means the thread was already setup to run on either CPU.
QuestionMaybe this would work?memberIvo Beltchev22 Feb '07 - 10:59 
Haven't tried it myself, but maybe you can use SetProcessAffinityMask and SetThreadAffinityMask to force each of the threads on a specific core.
AnswerRe: Maybe this would work?memberVasudevan Deepak Kumar22 Feb '07 - 18:09 
It would as I have posted here: http://www.codeproject.com/useritems/CPU_Load_Control.asp?forumid=388784&select=1908183&df=100#xx1908183xx[^]
 
Vasudevan Deepak Kumar
Personal Homepage
Tech Gossips

GeneralRe: Maybe this would work?memberdongbaek19 Jul '09 - 22:21 
why need this?
don't understand
!!!
 
welcome

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 22 Feb 2007
Article Copyright 2007 by jkhax0r
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid