Create thread s> 70% CPU utilization

I am creating a test program to test the functionality of a program that calculates processor utilization.

Now I want to test this program at different times when the processor load is 100%, 50% 0%, etc.

My question is how to get the processor to use 100% or maybe> 80%.

I think creating a while loop will be enough

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

After running this, I don't get high CPU usage. What are the possible solutions for an intensive intensive processor?

+4
source share
7 answers

You are right to use a loop, but:

  • Do you have an IO
  • You slept

In principle, nothing in this cycle will take a lot of processor time compared to the time it sleeps or waits for I / O.

To kill a CPU, you just need to give it a processor. The only tricky bit really is that the C ++ compiler does not optimize the loop. Perhaps something like this should be fine:

 // 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 that I am returning a value from a loop. This should stop the C ++ compiler, noticing that the loop is useless (if you had never used a value anywhere, the loop would have no purpose). You can also disable inlining, since otherwise I would suggest that the smart compiler would notice that you are calling the function without using the return value, and inserting it into nothing. (As Suma points out in the answer, using volatile when calling a function should disable inlining.)

+7
source

Your loop is mostly sleeping, which means that it has a very light CPU load. In addition to Sleep, be sure to include some kind of cycle that performs any calculations, for example (The factorial implementation is left as an exercise for the reader, you can 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); } 

When setting the sleepBalance / computeBalance parameter, you can configure how much the processor runs this program. If you want this to be a simulation of CPU usage, you can take a few extra steps:

  • in a multi-core system, be sure to call a cycle like this in several threads (one for each CPU) or execute the process several times and for predictable planning to plan direct thread / process binding
  • sometimes you can also increase the priority of a thread / process to mimic the environment in which the CPU is loaded with high-priority applications.
+4
source

Use the consume.exe file in the Windows SDK.

Do not fold yourself when someone else has completed this work and will provide it to you for free.

+3
source

If you call Sleep in your loop, then most of the loop time will be spent on doing nothing (sleeping). That's why your processor load is low - because this 10 ms sleep is huge compared to the time the processor spends the rest of the code in each iteration of the loop. A nontrivial task is to write code to accurately remove processor time. Roger suggests using the CPU Burn-In.

+1
source

For a thread to use a lot of CPUs, make sure that it does not block / does not wait. Your sleep call suspends the thread and does not schedule it for at least the number of ms that indicates the sleep call during which it will not use the CPU.

+1
source

I know the yes command on UNIX systems when routing in / dev / null will consume a 100% processor on a single core (it doesn't work). You can run multiple instances to use each core. You could probably compile the yes code in your application and call it directly. You do not indicate which C ++ compiler you use for Windows, but I assume that it is compatible with POSIX (ala Cygwin). If so, yes should work fine.

+1
source

Get a copy of the processor .

0
source

All Articles