tags:

views:

1019

answers:

7

I am creating a test program to test the functionality of program which calcultes CPU Utilization.

Now I want to test that program at different times when CPU utilization is 100%, 50% 0% etc.

My question how to make CPU to utilize to 100% or may be > 80%.

I think creating a while loop like will suffice

while(i++< 2000)
{
   cout<<" in while "<< endl;
   Sleep(10); // sleep for 10 ms.
}

After running this I dont get high CPU utilization. What would be the possible solutions to make high cpu intensive??

A: 

Get hold of a copy of CPU Burn-In.

Roger Lipscombe
This looks like an executable. I need to integrate in my code.
Alien01
Your question doesn't state that. You should include this constraint. Also: why?
Roger Lipscombe
Perhaps homework?
kgiannakakis
+1  A: 

If you call Sleep in your loop then most of the the loop's time will be spent doing nothing (sleeping). This is why your CPU utilization is low - because that 10mS sleep is huge compared to the time the CPU will spend executing the rest of the code in each loop iteration. It is a non-trivial task to write code to accurately waste CPU time. Roger's suggestion of using CPU Burn-In is a good one.

Stu Mackellar
A: 

To make a thread use a lot of CPU, make sure it doesn't block/wait. Your Sleep call will suspend the thread and not schedule it for at least the number of ms the Sleep call indicates, during which it will not use the CPU.

Brian Rasmussen
Why the down vote? Please explain so I have a chance of improving my reply. Thanks.
Brian Rasmussen
+6  A: 

You're right to use a loop, but:

  • You've got IO
  • You've got a sleep

Basically nothing in that loop is going to take very much CPU time compared with the time it's sleeping or waiting for IO.

To kill a CPU you need to give it just CPU stuff. The only tricky bit really is making sure the C++ compiler doesn't optimise away the loop. Something like this should probably be okay:

// A bit like generating a hashcode. Pretty arbitrary choice,
// but simple code which would be hard for the compiler to
// optimise away.
int running_total = 23;
for (int i=0; i < some_large_number; i++)
{
    running_total = 37 * running_total + i;
}
return running_total;

Note the fact that I'm returning the value out of the loop. That should stop the C++ compiler from noticing that the loop is useless (if you never used the value anywhere, the loop would have no purpose). You may want to disable inlining too, as otherwise I guess there's a possibility that a smart compiler would notice you calling the function without using the return value, and inline it to nothing. (As Suma points out in the answer, using volatile when calling the function should disable inlining.)

Jon Skeet
Sometimes (with some opmization settings) returning the value out is not enough. Compiler sometimes inlines any short function, not only those marked inline, and with constant input it can optimize the function out. Use volatile when calling the function to prevent this.
Suma
Hence the "you may want to disable inlining too" :)
Jon Skeet
(Added the "volatile" part to the answer though.)
Jon Skeet
+3  A: 

Your loop mostly sleeps, which means it has very light CPU load. Besides of Sleep, be sure to include some loop performing any computations, like this (Factorial implementation is left as an exercise to reader, you may replace it with any other non-trivial function).

while(i++< 2000)
{
   int sleepBalance = 10; // increase this to reduce the CPU load
   int computeBalance = 1000; // increase this to increase the CPU load
   for (int i=0; i<computeBalance; i++)
   {
     /* both volatiles are important to prevent compiler */
     /* optimizing out the function */
     volatile int n = 30;
     volatile int pretendWeNeedTheResult = Factorial(n);
   }
   Sleep(sleepBalance);
}

By adjusting sleepBalance / computeBalance you may adjust how much CPU this program takes. If you want to this as a CPU load simulation, you might want to take a few addtional steps:

  • on a multicore system be sure to either spawn the loop like this in multiple threads (one for each CPU), or execute the process multiple times, and to make the scheduling predictable assign thread/process affinity explicitly
  • sometimes you may also want to increase the thread/process priority to simulate the environment where CPU is heavily loaded with high priority applications.
Suma
+3  A: 

Use consume.exe in the Windows SDK.

Don't roll your own when someone else has already done the work and will give it to you for free.

Rob K
I can't choose the CPU percentage with my consume.exe (v0.1.0), it consumes all the CPU.
uvts_cvs
+1  A: 

I know the "yes" command on UNIX systems, when routed to /dev/null will eat up 100% CPU on a single core (it doesn't thread). You can launch multiple instances of it to utilize each core. You could probably compile the "yes" code in your application and call it directly. You don't specify what C++ compiler you are using for Windows, but I am going to assume it has POSIX compatibility of some kind (ala Cygwin). If that's the case, "yes" should work fine.

Nolte Burke