Where to start First steps track · 15 stages
Linux Labs

stress-ng — timed load on the CPU, memory and disk

9 min stress-ng 0.17 · Ubuntu 24.04

stress-ng puts load on the CPU, memory and disk for a set time — for experiments with load, OOM and disk I/O.

Cheat sheet

9 commands Printable version
  • stress-ng --cpu 2 --timeout 60s 2 processes load the CPU
  • stress-ng --cpu 1 --cpu-load 30 -t 30s one process at 30 %
  • stress-ng --vm 1 --vm-bytes 128M -t 30s take 128 MiB of memory
  • stress-ng --hdd 1 --hdd-bytes 64M -t 30s --temp-path /var/tmp
  • stress-ng --io 1 -t 30s sync() in a loop
  • stress-ng --cpu 1 -t 10s --metrics-brief with a summary table
  • nice -n 19 stress-ng --cpu 1 -t 60s & background, yielding
  • sudo systemd-run --unit=lab-vm -p MemoryMax=96M \
  • stress-ng --vm 1 --vm-bytes 160M -t 20s in its own cgroup

Overview

stress-ng starts worker processes called stressors. Each loads its own part of the system: --cpu computes, --vm allocates and walks through memory, --hdd writes files, --io calls sync(). The number after a key is how many workers to start. The training machine has version 0.17.

--timeout (-t) limits the run time. Without it stress-ng runs for a day:

text
student@ubuntu:~$ stress-ng --cpu 1
stress-ng: info:  [1994] defaulting to a 1 day, 0 secs run per stressor

In ps and top the main process is stress-ng, the workers are stress-ng-cpu, stress-ng-vm and so on. The main one only hands out work: CPU and memory are used by the workers.

Output and exit code

text
student@ubuntu:~$ stress-ng --cpu 2 --timeout 70s --metrics-brief
stress-ng: info:  [972] setting to a 1 min, 10 secs run per stressor
stress-ng: info:  [972] dispatching hogs: 2 cpu
…
stress-ng: info:  [972] passed: 2: cpu (2)
stress-ng: info:  [972] failed: 0
stress-ng: info:  [972] successful run completed in 1 min, 10.01 secs

--metrics-brief added a line for the cpu stressor:

bogo ops real time usr time sys time bogo ops/s (real)
87462 70.00 68.68 0.02 1249.39

Bogo ops are the stressor's nominal operations: compare them only between runs of the same stressor on the same machine. Two processes ran for 70 seconds on one CPU and got 68.68 seconds of usr together — half each.

Code Meaning
0 success
1 an error in the options or the temporary directory
2 a stressor failed
3 a stressor lacked resources — memory, space
4 a stressor is not supported on this system
5 a stressor was killed by an unexpected signal

Stressors

Key What it does Where it shows
--cpu N N processes compute us in top, load average
--cpu-load P each --cpu loads the CPU by P % the process's %CPU
--vm N --vm-bytes S N processes take S of memory each and walk through it free, RSS
--hdd N --hdd-bytes S N processes write and delete files of S wa, iostat
--io N N processes call sync() bo in vmstat

--cpu 0 starts one process per CPU. --vm-bytes takes a percentage of available memory too: --vm-bytes 25%. --vm-keep holds the same memory without freeing it between passes.

stress-ng and OOM

stress-ng gives its workers oom_score_adj 1000: when memory runs out, the oom-killer picks them first. The main process restarts a killed worker until the time is up. The --oomable key turns restarts off: the stressor ends early, and stress-ng prints vm: WARNING: finished prematurely after just 0.56 secs.

Options

16

The main keys are in the table below. The full list of stressors is stress-ng --stressors, the keys are in stress-ng --help.

Option What it does Example
-c --cpu N Start N processes that compute and load the CPU; 0 means one per CPU. stress-ng --cpu 2 --timeout 60s
-m --vm N Start N processes that allocate memory and walk through it. stress-ng --vm 1 --vm-bytes 128M --timeout 30s
--vm-bytes SIZE How much memory each --vm process takes (256M by default); a percentage of available memory such as 25% works too. stress-ng --vm 1 --vm-bytes 25% --timeout 30s
-t --timeout T Stop after T (seconds or 30s, 2m); without it stress-ng runs for a day. stress-ng --cpu 1 --timeout 2m
-l --cpu-load P Load the CPU by P percent per --cpu process instead of full load. stress-ng --cpu 1 --cpu-load 30 --timeout 30s
--vm-keep Keep the same memory the whole time instead of freeing and allocating it again. stress-ng --vm 1 --vm-bytes 64M --vm-keep --timeout 30s
-d --hdd N Start N processes that write and delete files in a temporary directory. stress-ng --hdd 1 --hdd-bytes 64M --temp-path /var/tmp -t 30s
--hdd-bytes SIZE How much data each --hdd process writes (1G by default). stress-ng --hdd 1 --hdd-bytes 64M --temp-path /var/tmp -t 30s
-i --io N Start N processes that call sync() in a loop, flushing the cache to disk. stress-ng --io 1 --timeout 30s
--metrics-brief Print a table of bogo ops and times per stressor at the end. stress-ng --cpu 1 --timeout 10s --metrics-brief
--temp-path DIR Directory for temporary files of --hdd and other stressors; the current one by default. stress-ng --hdd 1 --temp-path /var/tmp --timeout 30s
--ionice-class CLASS Disk priority class for worker processes — idle, besteffort, realtime. stress-ng --hdd 1 --ionice-class idle --timeout 30s
-q --quiet Do not print info messages. stress-ng --cpu 1 --timeout 30s -q
--oomable Do not restart a worker process killed by the OOM killer. stress-ng --vm 1 --vm-bytes 160M --oomable --timeout 10s
-v --verbose Print everything, including debug messages. stress-ng --vm 1 --timeout 5s -v
--aggressive Enable the heaviest stressor modes — do not run on a shared machine. dangerous stress-ng --cpu 1 --aggressive --timeout 10s

Nothing found. Try a shorter query.

Recipes

4

Load that does not hurt the neighbours

bash
nice -n 19 stress-ng --cpu 1 --timeout 60s &       # CPU
stress-ng --hdd 1 --hdd-bytes 64M --temp-path /var/tmp \
  --ionice-class idle --timeout 30s                # disk
sudo systemd-run --unit=lab-vm -p MemoryMax=96M \
  stress-ng --vm 1 --vm-bytes 160M --timeout 20s   # memory
text
student@ubuntu:~$ ps -o pid,ni,stat,pcpu,comm -C stress-ng-cpu
    PID  NI STAT %CPU COMMAND
   1636  19 RN   97.4 stress-ng-cpu

With nice -n 19 the process takes all the free CPU (97.4 %) and yields it to any process with the normal nice value. Memory in its own cgroup leaves the rest of the machine alone: OOM happens inside the lab-vm group (see oom-killer).

Raise the load average

bash
stress-ng --cpu 3 --timeout 120s &
uptime                    # after one minute, after two

Three workers on one CPU push the load towards 3. The 1-minute number reaches about 1.9 in a minute and almost 3 in three (see load-average).

Get an OOM in a cgroup

text
student@ubuntu:~$ sudo systemd-run --unit=lab-vm -p MemoryMax=96M \
  --wait stress-ng --vm 1 --vm-bytes 160M --timeout 10s
Running as unit: lab-vm.service; invocation ID: 95a09070a73e46a6…
Finished with result: oom-kill
Main processes terminated with: code=exited/status=0
Service runtime: 129ms

systemd stopped the whole service at the very first OOM (OOMPolicy=stop). With -p OOMPolicy=continue the service lives until --timeout, and stress-ng restarts the killed worker.

In the background, under control

bash
stress-ng --cpu 2 --timeout 90s -q &   # a shell job
jobs; kill %1                          # stop it early
sudo systemd-run --unit=lab-cpu stress-ng --cpu 1 -t 300s
sudo systemctl stop lab-cpu            # stop the unit

A shell job dies with the session. A systemd-run unit lives apart from the terminal and shows in systemctl status lab-cpu. Ctrl+C stops stress-ng with a summary: the main process collects the workers and prints successful run completed.

Troubleshooting

4

temp-path '.' must be readable and writeable

text
student@ubuntu:/$ stress-ng --hdd 1 --timeout 3s
aborting: temp-path '.' must be readable and writeable

--hdd writes files to the current directory, and the student cannot write to /. Pass --temp-path /var/tmp or go to your home directory.

gave up trying to mmap, no available memory

The full line is vm: gave up trying to mmap, no available memory, skipping stressor. --vm-bytes is more than the machine can give (520M with 458 MiB in total). The stressor is skipped, the exit code is 3. Make the size smaller or give it as a percentage: --vm-bytes 50%.

stress-ng runs and never ends

--timeout is missing: by default a run lasts a day. Stop it with Ctrl+C, kill %1 or sudo systemctl stop <unit> and start again with -t.

note and cpu-method messages in the output

note: /proc/sys/kernel/sched_autogroup_enabled is 1… and for stable load results, select a specific cpu stress method… are hints about the accuracy of bogo ops. They do not change the load.

Danger zone

1

Load without a timer or limits on a shared machine

--vm without MemoryMax drives the machine into OOM, and neighbours may suffer along with the stressor. --all, --aggressive and large N load the machine so hard that everything slows down, your terminal included. On a shared server always use --timeout, nice, ionice and a cgroup of its own for memory.

See also

Loading…