Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create thread with >70% CPU utilization

Tags:

c++

windows

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??

like image 485
anand Avatar asked Dec 03 '22 16:12

anand


2 Answers

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.)

like image 80
Jon Skeet Avatar answered Dec 09 '22 15:12

Jon Skeet


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.
like image 32
Suma Avatar answered Dec 09 '22 15:12

Suma