Mike
Mike

Reputation: 49363

How can I write a test application to fully load the CPU?

The CPU is designed to drop into low power modes whenever it can to save power and keep cool, I'd like to make a program to prevent that from happening.

I'm working on a few different embedded platforms (Freescale Coldfire 8052, TI Sitara AM3359, probably a few others in the pipeworks) and so I wanted to make an application that will just keep the CPU fully loaded for benchmarking. I want to write my own since it would be easier to cross-compile then to look for a solution per target.

My initial thought was just:

while(1);

Question 1:
But at I over simplifing this? top shows that program taking about 99.4% CPU usage, so I guess it's working, but it doesn't seem like it should be so simple. :) Anyone know if there should be more to it than that?

Question 2:
If I wanted to expand this to do different loads (say, 50%, 75%, or whatever) how could I do that? I managed to get a 18~20% CPU usage via:

while(1){usleep(1);}

Is there a more, scientific way rather than just guessing and checking at sleep values? I would think these would be different per target anyway.

Upvotes: 3

Views: 880

Answers (4)

CAFxX
CAFxX

Reputation: 30281

while(1); will eat up all your CPU cycles but won't exercise most parts of your CPU (let alone the GPU). Most modern CPUs have the ability to selectively switch off individual execution units if they're not used: the only way to prevent it is:

  1. tell the CPU/SoC driver to disable power saving
  2. exercise all control and execution units of your CPU/GPU/chipset/etc.

Upvotes: 3

Mark Lakata
Mark Lakata

Reputation: 20818

You can run the yes command to have a process than consumes 100% CPU.

yes > /dev/null &

Read the man page for yes to see what it does. It's a pretty stupid program, designed to just answer "yes" over and over again to programs that ask for user confirmation.

If you redirect to a file, you can also simulate heavy file IO too.

Upvotes: 0

Art
Art

Reputation: 20392

A while(1); loop will most likely be running all the time the operating system isn't doing other things like handling interrupts or running daemons. The problem you have is that top doesn't actually show how much time your program has been actually running, but a rather crude estimate that is used for internal scheduling calculations. On some systems you can end up with over 100% cpu usage just because the math is a little bit off.

When it comes to loading your cpu properly, it depends on what you want to do. Touch every part of the cpu? Maximal power usage? It's not an easy question, especially when you probably don't know what the question actually is.

Upvotes: 1

Florin Stingaciu
Florin Stingaciu

Reputation: 8275

So I'll try to post this as an answer then. If you look at what the specs for usleep are, you'll notice the following line:

The usleep() function will cause the calling thread to be suspended from execution...

This means that 18~20% CPU usage was actually time spent during context switching. The while(1) in your code will use CPU cycles because it gets scheduled but it wont use the CPU to its full capability. There are a lot of options there for C programs that will try to use 100% CPU. Most of them use multiple threads mixed with math-based applications.

See this thread for a number of examples.

Upvotes: 1

Related Questions