Questions

Forum Navigation
Please to create posts and topics.

How to simulate CPU activity on a Linux system?

For a project, I need to simulate CPU activity for some time on a test system. How to do that on a Linux system?

A quick way to simulate CPU activity on a Linux-based system is to use dd and /dev/zero and /dev/null, all together. The command that will let to simulate this activity is the following:

dd if=/dev/zero of=/dev/null &

/dev/zero provides an endless stream of zero bytes when read. This function is provided by the kernel and does not require allocating memory. All writes to /dev/null are dropped silently.

The final ampersand (&) will put the command in background. Usually, issuing 3-4 times the above command will load the CPU almost totally, near to 100%.

As a result, when you perform the dd, the system generates an infinite amount of zero bytes that simply get discarded. Except for a temporary buffer, no data are stored before, during, or after this operation.

The speed of the transfer from /dev/zero to /dev/null is determined primarily by the speed of the processor and relevant system calls.

A similar command is the following, in which it is possible, using dd, to specify the block size of data to generate and how many times (useful if it is needed to simulate CPU activity for a limit period of time):

dd if=/dev/zero of=/dev/null bs=500M count=x

In general the above command will: duplicate data (dd) from input file (if) of /dev/zero (virtual limitless supply of 0's) into output file (of) of /dev/null (virtual sinkhole) using blocks of 500M size (bs = block size) and repeat this (count) just once (1).

An alternative way to stress the CPU is by using the stress utility. It imposes a configurable amount of CPU, memory, I/O, and disk stress, using workers, on the system. You can install the stress utility on different platform using the following approaches:

CentOS/RHEL/Fedora: sudo yum install stress

Ubuntu/Debian: sudo apt-get install stress

Arch: sudo pacman -S stress

MacOSX: brew install stress

For example, to force activity on 4 CPU cores:

stress --cpu 4