6

I have a RHEL box that I need to put under a moderate and variable amount of CPU load (50%-75%).

What is the best way to go about this? Is there a program that can do this that I am not aware of? I am happy to write some C code to make this happen, I just don't know what system calls will help.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
salt.racer
  • 21,903
  • 14
  • 44
  • 51

10 Answers10

15

This is exactly what you need (internet archive link): https://web.archive.org/web/20120512025754/http://weather.ou.edu/~apw/projects/stress/stress-1.0.4.tar.gz

From the homepage: "stress is a simple workload generator for POSIX systems. It imposes a configurable amount of CPU, memory, I/O, and disk stress on the system. It is written in C, and is free software licensed under the GPL."

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Tom Feiner
  • 20,656
  • 20
  • 48
  • 51
  • Above link is not working. Found it here: http://people.seas.harvard.edu/~apw/stress/ – Gautam Somani Nov 11 '16 at 23:13
  • Both links aren't working. Here's the internet archive link: https://web.archive.org/web/20120512025754/http://weather.ou.edu/~apw/projects/stress/stress-1.0.4.tar.gz – Vincent Guttmann May 12 '21 at 06:44
2

Find a simple prime number search program that has source code. Modify the source code to add a nanosleep call to the main loop with whichever delay gives you the desired CPU load.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
1

One common way to get some load on a system is to compile a large software package over and over again. Something like the Linux kernel.

Get a copy of the source code, extract the tar.bz2, go into the top level source directory, copy your kernel config from /boot to .config or zcat /proc/config.gz > .config, the do make oldconfig, then while true; do make clean && make bzImage; done

If you have an SMP system, then make -j bzImage is fun, it will spawn make tasks in parallel.

One problem with this is adjusting the CPU load. It will be a maximum CPU load except for when waiting on disk I/O.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
1

It really depends what you're trying to test. If you're just testing CPU load, simple scripts to eat empty CPU cycles will work fine. I personally had to test the performance of a RAID array recently and I relied on Bonnie++ and IOZone. IOZone will put a decent load on the box, particularly if you set the file size higher than the RAM.

You may also be interested in this Article.

sethbc
  • 3,441
  • 1
  • 16
  • 9
1

You could possibly do this using a Bash script. Use " ps -o pcpu | grep -v CPU" to get the CPU Usage of all the processes. Add all those values together to get the current usage. Then have a busy while loop that basically keeps on checking those values, figuring out the current CPU usage, and waiting a calculated amount of time to keep the processor at a certain threshhold. More detail is need, but hopefully this will give you a good starting point.

Take a look at this CPU Monitor script I found and try to get some other ideas on how you can accomplish this.

Kibbee
  • 65,369
  • 27
  • 142
  • 182
1

Lookbusy enables set value of CPU load. Project site

lookbusy -c util[-high_util], --cpu-util util[-high_util]

i.e. 60% load
lookbusy -c 60
mat48
  • 11
  • 1
0

You can probably use some load-generating tool to accomplish this, or run a script to take all the CPU cycles and then use nice and renice on the process to vary the percentage of cycles that the process gets.

Here is a sample bash script that will occupy all the free CPU cycles:

#!/bin/bash
while true ; do
true
done
takrl
  • 6,356
  • 3
  • 60
  • 69
ya8282
  • 9
  • 1
  • Or straight from a terminal you could type: while true; do true; done – Jesse Jun 14 '10 at 23:51
  • This will load a CPU to 100% CPU but nice and renice won't allow to tune this load, only to weight it with competing processes, if any. – jlliagre Jun 28 '12 at 06:13
0

Use the "nice" command.

a) Highest priority: $ nice -n -20 my_command

or

b) Lowest priority: $ nice -n 20 my_command

Gravstar
  • 1,071
  • 6
  • 10
0

A Simple script to load & hammer the CPU using awk. The script does mathematical calculations and thus CPU load peaks up on higher values passwd to loadserver.sh .

checkout the script @ http://unixfoo.blogspot.com/2008/11/linux-cpu-hammer-script.html

-1

Not sure what your goal is here. I believe glxgears will use 100% CPU.

So find any process that you know will max out the CPU to 100%.

If you have four CPU cores(0 1 2 3), you could use "taskset" to bind this process to say CPUs 0 and 1. That should load your box 50%. To load it 75% bind the process to 0 1 2 CPUs.

Disclaimer: Haven't tested this. Please let us know your results. Even if this works, I'm not sure what you will achieve out of this?

Bash
  • 4,591
  • 2
  • 18
  • 12