Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C++

CPU Load Control

Rate me:
Please Sign up or sign in to vote.
3.90/5 (13 votes)
22 Feb 2007CPOL1 min read 77.4K   2.5K   33   16
Demonstration to control the amount of CPU load used by your computer
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiongood Pin
bleachli9-Nov-11 5:13
bleachli9-Nov-11 5:13 
GeneralXP is showing two processors Pin
Lokanatha Reddy15-May-07 23:38
Lokanatha Reddy15-May-07 23:38 
GeneralCPU Load Ported on Visual C++ 6.0 Pin
cristitomi15-Mar-07 23:54
cristitomi15-Mar-07 23:54 
GeneralNot work in one core PC with Windows2000 Professional Pin
AleksMain27-Feb-07 2:24
AleksMain27-Feb-07 2:24 
GeneralRe: Not work in one core PC with Windows2000 Professional Pin
jkhax0r2-Apr-07 6:38
jkhax0r2-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.
GeneralMulticore Pin
Jörgen Sigvardsson22-Feb-07 11:00
Jörgen Sigvardsson22-Feb-07 11:00 
GeneralRe: Multicore Pin
Vasudevan Deepak Kumar22-Feb-07 18:08
Vasudevan Deepak Kumar22-Feb-07 18:08 
QuestionRe: Multicore Pin
jkhax0r26-Feb-07 5:16
jkhax0r26-Feb-07 5:16 
AnswerRe: Multicore Pin
Jörgen Sigvardsson26-Feb-07 7:29
Jörgen Sigvardsson26-Feb-07 7:29 
GeneralRe: Multicore Pin
tobywf26-Feb-07 23:54
tobywf26-Feb-07 23:54 
GeneralRe: Multicore Pin
Jörgen Sigvardsson27-Feb-07 2:36
Jörgen Sigvardsson27-Feb-07 2:36 
GeneralRe: Multicore [modified] Pin
tobywf27-Feb-07 8:56
tobywf27-Feb-07 8:56 
GeneralRe: Multicore Pin
jkhax0r28-Feb-07 16:46
jkhax0r28-Feb-07 16:46 
QuestionMaybe this would work? Pin
Ivo Beltchev22-Feb-07 10:59
Ivo Beltchev22-Feb-07 10:59 
AnswerRe: Maybe this would work? Pin
Vasudevan Deepak Kumar22-Feb-07 18:09
Vasudevan Deepak Kumar22-Feb-07 18:09 
GeneralRe: Maybe this would work? Pin
dongbaek19-Jul-09 22:21
dongbaek19-Jul-09 22:21 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.