268

I’m currently in the process of debugging a Cacti installation and want to create CPU load to debug my CPU utilization graphs.

I tried to simply run cat /dev/zero > /dev/null, which works great but only utilizes 1 core:

enter image description here

Is there a better method of testing/maxing-out system resources under load?

Related: How can I produce high CPU load on Windows?

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
  • 1
    is it possible to run multiple instances of `cat` simultaneously? – Nate Koppenhaver Jun 30 '12 at 17:16
  • @NateKoppenhaver: Yes, that seems to be possible when wrapping them in `screen` sessions. But I would prefer a more sophisticated solution if possible. – Oliver Salzburg Jun 30 '12 at 17:18
  • 2
    Heh, I always used `cat /dev/random > /dev/null`. Guess `/dev/zero` works too. :-) – oKtosiTe Jul 01 '12 at 07:25
  • 9
    @oKtosiTe cat /dev/random has the side effect of depleting the entropy in /dev/random. There are times you need to conserve entropy, I wouldn't have this as my go to CPU hog. – Rich Homolka Jul 25 '12 at 21:41
  • 5
    @oKtosiTe What Rich Homolka said is right, but it's not just that it's a bad thing to do, it's also useless because it'll block almost immediately and stop consuming cpu. – Luc Jul 22 '14 at 18:11
  • @Luc This isn't always true. With `haveged` you can create enough entropy. – Stefan Aug 12 '14 at 12:05
  • @Stefan Interesting, I didn't know that tool existed. Still, it isn't installed by default on any Debian (based) distribution that I know of. – Luc Aug 12 '14 at 13:12
  • I've been using `matho-primes all >/dev/null &` but it's undoubtedly not the BEST answer for everyone. – Wildcard Dec 19 '15 at 03:00

17 Answers17

295

Try stress It's pretty much an equivalent of the Windows consume.exe:

oliver$ ./stress --cpu 3
stress: info: [18472] dispatching hogs: 3 cpu, 0 io, 0 vm, 0 hdd
Mitesh Shah
  • 2,966
  • 1
  • 14
  • 3
144

No need to install any extra package, your good old shell is able to do it alone.

This one-liner will load your four cores1 at 100%:

for i in 1 2 3 4; do while : ; do : ; done & done

How it works is quite simple, it starts four endless loops. Each of them is repeating the null instruction (:). Each loop is able to load a CPU core at 100%.

If you use bash, ksh93 and other shells supporting ranges, (i.e. not dash or older ksh), you can use this non portable syntax:

for i in {1..4}; do ...

Replace 4 with the number of CPUs you'd like to load if different from 4.

Assuming you had no background job already running when you launched one of these loops, you can stop the load generation with that command:

for i in 1 2 3 4; do kill %$i; done

Answering @underscore_d's comment, here is an enhanced version that simplify a lot stopping the load and that also allow specifying a timeout (default 60 seconds.) A Control-C will kill all the runaway loops too. This shell function works at least under bash and ksh.

# Usage: lc [number_of_cpus_to_load [number_of_seconds] ]
lc() {
  (
    pids=""
    cpus=${1:-1}
    seconds=${2:-60}
    echo loading $cpus CPUs for $seconds seconds
    trap 'for p in $pids; do kill $p; done' 0
    for ((i=0;i<cpus;i++)); do while : ; do : ; done & pids="$pids $!"; done
    sleep $seconds
  )
}

1Note that with CPUs supporting more than one thread per core (Hyper-threading), the OS will dispatch the load to all virtual CPUs. In that case, the load behavior is implementation dependent (each thread might be reported as 100% busy or not)..

jlliagre
  • 13,899
  • 4
  • 31
  • 48
46

One alternative way would be

openssl speed -multi $(grep -ci processor /proc/cpuinfo)

or (if nproc is present)

openssl speed -multi $(nproc --all)

OpenSSL is almost always present on nowadays distros, so no extra packages needed.

rkosegi
  • 624
  • 6
  • 9
  • 1
    imho this should be the accepted answer because it doesn't require any additional packages and it takes care of all cores. – lsu_guy May 24 '23 at 05:03
24

I made a simple python script which does the same. You can control the number of cpu cores you want to load. The good thing about this is that it won't consume any other resource besides the cpu. (I think mark johnson's idea would consume a lot of I/O resources, which is undesired here.)

from multiprocessing import Pool

def f(x):
    # Put any cpu (only) consuming operation here. I have given 1 below -
    while True:
        x * x

# decide how many cpus you need to load with.
no_of_cpu_to_be_consumed = 3

p = Pool(processes=no_of_cpu_to_be_consumed)
p.map(f, range(no_of_cpu_to_be_consumed))

Just run this script from the terminal $ python temp1.py. You need to kill the script when you are done.

Here, is my cpu consumption output when I load 3 of my cores.

Script temp1.py creates three processes (PIDs - 9377, 9378, 9379) which load 3 of my cores

Pushpak Dagade
  • 373
  • 1
  • 9
20

Start two

sha1sum /dev/zero &

commands for every core in your system.

To stop

killall sha1sum

or

kill sha1sum
ecabuk
  • 351
  • 2
  • 7
11

I've been developing stress-ng, an updated stress tool that can stress a wide range of aspects of a Linux system. For more information, see http://kernel.ubuntu.com/~cking/stress-ng/

Usage is similar to stress

$ stress-ng --cpu 4 --vm 2 --fork 8 --switch 4 --timeout 1m
stress-ng: info:  [32254] dispatching hogs: 4 cpu, 8 fork, 4 switch, 2 vm
stress-ng: info:  [32254] cache allocate: default cache size: 8192K

Install with

sudo apt-get install stress-ng
Matt
  • 153
  • 7
Colin King
  • 121
  • 1
  • 2
  • 8
    Please read [How do I recommend software](https://meta.superuser.com/questions/5329/how-do-i-recommend-software-in-my-answers/5330#5330) for some tips as to how you should go about recommending software. At the very least you should provide more than just/at least a link, for example some additional information about the software itself, and how it can be used to solve the problem in the question. – DavidPostill Sep 06 '15 at 20:33
7

I usually take the cpuburn suite:

sudo apt-get install cpuburn
for i in {1..4}; do burnK7 & done

Replace 4 with the number of cores / HT-threads you have or want to stress.

Note: This stresses as much chip area as possible at the same time, it's programmed to generate maximum power dissipation. I had to write this post a second time, somehow my machine didn't like it :-(

You could also do cpuburn in sequences:

burnP6 & burnP6 & burnP6 & burnP6 & 
[1] 28520
[2] 28521
[3] 28522
[4] 28523

And when you want to stop them:

killall burnP6

You could also multiply burnP6 & to match the number of CPU cores on your system.

ce4
  • 923
  • 8
  • 11
3

You can run that command as many times as you want, and it will take up a different core each time:

$ CORES=1
$ for i in `seq 1 $CORES`; do cat /dev/zero > /dev/null &
> done
[1] 8388
Christian Mann
  • 691
  • 8
  • 21
2

I combined both +jlliagre and +ecabuk.

#!/bin/bash
lc() {
    nowMs=$(date +%s)
    (
        pids=""
        cpus=${1:-1}
        seconds=${2:-60}
        echo "[$(date)] loading $cpus CPUs for $seconds seconds"
        echo "[$(date)] Expected completion: [$(date --date=@$(expr $nowMs + $seconds))]"
        trap 'for p in $pids; do kill $p; done' 0
        for ((i=0;i<cpus;i++)); do
            sha1sum /dev/zero &
            pids="$pids $!";
        done
        sleep $seconds
    )
    echo "[$(date)] Done"
}

lc $@
TJR
  • 183
  • 9
2

Here's the way I use and there's no need to install anything extra.

For example to start with 4 processes,

nproc | xargs seq | xargs -n1 -P4 md5sum /dev/zero

You can change the number of processes by the option "-P" above.

yichun
  • 21
  • 2
2

https://github.com/GaetanoCarlucci/CPULoadGenerator

pretty simple and scientific solution.

Here you can see dynamics example in which 50% load is generated on CPU core 0:

enter image description here

You can run the process on other cores at the same time.

user
  • 163
  • 1
  • 9
1

pxz is a parallel implementation of xz.

pxz -9e /dev/zero --stdout >/dev/null should do the trick, as this is quite cpu intensive.

If /dev/zero is not fast enough (you notice that pxz gets I/O throttled) you can do pxz -9e /dev/zero --stdout | pxz -9e --stdout >/dev/null

Newer versions of xz have the --threads option which is a substitute for pxz.

styrofoam fly
  • 1,836
  • 3
  • 20
  • 22
1

I wanted to add this to @jlliagre's comment, but I don't have enough reputation. If you're going to use this code on multiple servers and the CPU count will vary, you can use the following command:

for ((i=1; i<=`nproc --all`; i++)); do while : ; do : ; done & done

This will utilize all of the cores on your server regardless of how many you have. The command nproc is part of coreutils so should be on most Linux installations.

AndreasKralj
  • 137
  • 7
1

Dockerized solution, using stress. Can be deployed using k8s to cover multiple machines.

https://hub.docker.com/r/jfusterm/stress

Sida Zhou
  • 121
  • 4
1

You can use:

fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd

Repeat dd if=/dev/zero of=/dev/null for your CPU cores.

Hit any key to stop the test!

Lyma Lyma
  • 11
  • 2
0

Run the program stress specifying all the cores you have on your CPU:

stress -c `nproc`

You can get it on Debian/Ubuntu: sudo apt install stress

Smeterlink
  • 652
  • 2
  • 11
  • 20
0

A simple command line does it too:

x="x" ; while : ; do x=$x$x ; echo -n "." ; done
ott--
  • 2,201
  • 1
  • 15
  • 15
  • 1
    This would be simpler: `while : ; do : ; done` – jlliagre Jul 01 '12 at 06:30
  • @jlliagre Yours wön't go above loadavg 1. – ott-- Jul 01 '12 at 11:06
  • Your loop isn't primarily loading the CPU but more filling the memory. It will eventually crash with an out of memory error. – jlliagre Jul 01 '12 at 12:31
  • @jlliagre Mine fills memory and swap (if present), thus producing a load of 3 before it killed because it runs out of memory. – ott-- Jul 01 '12 at 14:14
  • 4
    That's the problem. You aren't answering the question asked which is how to produce a high CPU load on a server. Your script is quickly making a system unresponsive and then crashes. There are much more reliable ways to get a loadavg of 3. eg: `for i in 1 2 3; do while : ; do : ; done & ; done` – jlliagre Jul 01 '12 at 14:52