3

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.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • If you're thinking about loading the CPU, you should look into heavy mathematics based applications (matrix multipliers etc etc). – Florin Stingaciu Oct 23 '12 at 15:56
  • you should try using renice, as the process is probably getting preempted by the scheduler. – iabdalkader Oct 23 '12 at 15:57
  • @FlorinStingaciu - Looping them? Why would that do a better job than a `while(1);` loop? Could you explain? – Mike Oct 23 '12 at 15:58
  • @mux - Can that be set programmatically? I would think the application will need root permissions to bump the priority – Mike Oct 23 '12 at 16:00
  • yes, setpriority, not sure if that needs root too. – iabdalkader Oct 23 '12 at 16:05
  • What is your real goal? Measuring max power consumption? – nibot Oct 23 '12 at 16:05
  • 2
    @nibot - Heat. I want to see the CPU catch on fire. >:) seriously though, I want to get the CPU's core temp up just by running it "full blast" non-stop. – Mike Oct 23 '12 at 16:07
  • Years ago, on a cold winter's night, I was doing something on my laptop in my recliner next to the window, and I was *cold*. So I opened another window (er, a window on the laptop, that is), and typed a number like 78234782348732783276326732716761234784328723478238723376127612756136753217848741 into it, and had a not-very-sophisticated program of mine try to discover its prime factorization, and in just a minute or so, I had a nice lapwarmer in my lap. :-) – Steve Summit Dec 14 '22 at 23:09

4 Answers4

3

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.
CAFxX
  • 28,060
  • 6
  • 41
  • 66
  • Is there a way to realize #1 in a HW independent manner? (We can assume a constant Linux OS) – Mike Oct 23 '12 at 16:02
  • There isn't a software control for the clock-gating of individual execution units; you have to run code that exercises them. A max power load will often involve max-throughput FMA, with the full width of whatever SIMD capability it has. e.g. Prime95, or code like [How do I achieve the theoretical maximum of 4 FLOPs per cycle?](https://stackoverflow.com/q/8389648) (that's only 128-bit non-FMA). – Peter Cordes Dec 14 '22 at 22:20
1

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.

Community
  • 1
  • 1
Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
1

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.

Art
  • 19,807
  • 1
  • 34
  • 60
  • On CPUs (not tiny microcontrollers), the OS's idle loop won't actually be a loop, it'll put the CPU into a sleep state that halts the clock (until the next interrupt), like the x86 `hlt` instruction. And depending on heuristics and expected time until next wake, maybe into a deeper sleep state that powers down that core, maybe even flushing its caches. e.g. [Uses of the monitor/mwait instructions](https://stackoverflow.com/q/57471862) – Peter Cordes Dec 14 '22 at 22:23
0

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.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123