- Linux: How to put a load on system memory?
- 14 Answers 14
- Simulate System Loads
- Saved searches
- Use saved searches to filter your results more quickly
- License
- ptitiano/cpuloadgen
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- How can I produce high CPU load on a Linux server?
- 17 Answers 17
Linux: How to put a load on system memory?
I’m working on a small function, that gives my users a picture of how occupied the CPU is. I’m using cat /proc/loadavg , which returns the well known 3 numbers. My problem is that the CPU doesn’t do anything, right now, while I’m developing. Is there a good way to generate some load on the CPU, I was thinking something like makecpudosomething 30 , for a load of 0.3 or similar. Does an application like this exist? Also, are there any way to eat up RAM in a controlled fashion?
@Paul — He said «eat up RAM in a controlled fasion». Java would just chew through whatever it wanted. -P
14 Answers 14
while true; do openssl speed; done
also the stress program will let you load the cpu/mem/disk to the levels you want to simulate:
stress is a deliberately 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 GPLv2.
to maintain a particular level of cpu utilization, say 30%, try cpulimit:
it will adapt to the current system environment and adjust for any other activity on the system.
there’s also a patch to the kernel for native cpu rate limits here: http://lwn.net/Articles/185489/
You mistaken what Ingo said. What he meant was «nice +19 tasks gets 1.5% in case of 100% CPU usage», obviously. Why on earth an OS would limit the CPU usage of a process if the CPU is completely idle ? I did a little test on a 2 core CPU: 1 yes process with nice 19 and two yes processes with nice 0. Indeed, the 1.5% Ingo was talking about. gist.github.com/265744
This is 3 years late, but here’s a page — searched google for the description of the project «stress is a deliberately simple. &c.» people.seas.harvard.edu/~apw/stress
Also stress-ng is an update/fork of stress that allows cpu load to be specified in percentage rather than «number of entire CPUs to max at 100%» kernel.ubuntu.com/~cking/stress-ng
I didn’t understand very well if you want to generate arbitrary CPU load or CPU utilization. Yes, they are different things indeed. I’ll try to cover both problems.
First of all: load is the average number of processes in the running, runnable or waiting for CPU scheduler queues in a given amount of time, «the one that wants your CPU» so to speak.
So, if you want to generate arbitrary load (say 0.3) you have to run a process for 30% of the time and then remove it from the run queue for 70% of the time, moving it to the sleeping queue or killing it, for example.
You can try this script to do that:
export LOAD=0.3 while true do yes > /dev/null & sleep $LOAD killall yes sleep `echo "1 - $LOAD" | bc` done
Note that you have to wait some time (1, 10 and 15 minutes) to get the respective numbers to come up, and it will be influenced by other processes in your system. The more busy your system is the more this numbers will float. The last number (15 minutes interval) tends to be the most accurate.
CPU usage is, instead, the amount of time for which CPU was used for processing instructions of a computer program.
So, if you want to generate arbitrary CPU usage (say 30%) you have to run a process that is CPU bound 30% of the time and sleeps 70% of it.
I wrote an example to show you that:
#include #include #include #include #include #include #include #define CPUUSAGE 0.3 /* set it to a 0 < float < 1 */ #define PROCESSES 1 /* number of child worker processes */ #define CYCLETIME 50000 /* total cycle interval in microseconds */ #define WORKTIME (CYCLETIME * CPUUSAGE) #define SLEEPTIME (CYCLETIME - WORKTIME) /* returns t1-t2 in microseconds */ static inline long timediff(const struct timeval *t1, const struct timeval *t2) < return (t1->tv_sec - t2->tv_sec) * 1000000 + (t1->tv_usec - t2->tv_usec); > static inline void gettime (struct timeval *t) < if (gettimeofday(t, NULL) < 0) < err(1, "failed to acquire time"); >> int hogcpu (void) < struct timeval tWorkStart, tWorkCur, tSleepStart, tSleepStop; long usSleep, usWork, usWorkDelay = 0, usSleepDelay = 0; do < usWork = WORKTIME - usWorkDelay; gettime (&tWorkStart); do < sqrt (rand ()); gettime (&tWorkCur); >while ((usWorkDelay = (timediff (&tWorkCur, &tWorkStart) - usWork)) < 0); if (usSleepDelay while (1); return 0; > int main (int argc, char const *argv[]) < pid_t pid; int i; for (i = 0; i < PROCESSES; i++) < switch (pid = fork ()) < case 0: _exit (hogcpu ()); case -1: err (1, "fork failed"); break; default: warnx ("worker [%d] forked", pid); >> wait(NULL); return 0; >
If you want to eat up a fixed amount of RAM you can use the program in the cgkanchi’s answer.
Simulate System Loads
Sysadmins often need to discover how the performance of an application is affected when the system is under certain types of load. This means that an artificial load must be re-created. It is, of course, possible to install dedicated tools to do this but this option isn’t always desirable or possible.
Every Linux distribution comes with all the tools needed to create load. They are not as configurable as dedicated tools but they will always be present and you already know how to use them.
CPU
The following command will generate a CPU load by compressing a stream of random data and then sending it to /dev/null :
cat /dev/urandom | gzip -9 > /dev/null
If you require a greater load or have a multi-core system simply keep compressing and decompressing the data as many times as you need e.g.:
cat /dev/urandom | gzip -9 | gzip -d | gzip -9 | gzip -d > /dev/null
RAM
The following process will reduce the amount of free RAM. It does this by creating a file system in RAM and then writing files to it. You can use up as much RAM as you need to by simply writing more files. First, create a mount point then mount a ramfs filesystem there:
mkdir z mount -t ramfs ramfs z/
dd if=/dev/zero of=z/file bs=1M count=128
- bs= Block Size. This can be set to any number followed B for bytes, K for kilobytes, M for megabytes or G for gigabytes.
- count= The number of blocks to write.
Disk
We will create disk I/O by firstly creating a file, and then use a for loop to repeatedly copy it.
This command uses dd to generate a 1GB file of zeros:
dd if=/dev/zero of=loadfile bs=1M count=1024
The following command starts a for loop that runs 10 times. Each time it runs it will copy loadfile over loadfile1 :
for i in ; do cp loadfile loadfile1; done
If you want it to run for a longer or shorter time change the second number in .
If you prefer the process to run forever until you kill it with CTRL+C use the following command:
while true; do cp loadfile loadfile1; done
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
License
ptitiano/cpuloadgen
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Signed-off-by: Patrick Titiano
Git stats
Files
Failed to load latest commit information.
README.md
A Programmable CPU Load Generator, based on Dhrystone loops
CPULOADGEN is a Linux user-space standalone application designed to provide a quick’n easy way to generate programmable load on CPU core(s).
To generate load, it reuses DHRYSTONE loops, applying on top of it sort of PWM (Pulse Width Modulation) principle: by generating active (100% load) and idle (0% load) with programmable duty cycle, it generates an average CPU load of 100%.
CPULOADGEN is a generic application, it can run on any CPU architecture.
THIS SOFTWARE IS PROVIDED AS IS, WITH NO SUPPORT OR MAINTENANCE COMMITMENT FROM TEXAS INSTRUMENTS INCORPORATED.
To only build the output binary file:
NB: CROSS_COMPILE variable must be set to point to the correct compiler.
# export CROSS_COMPILE=arm-none-linux-gnueabi-
# make CROSS_COMPILE=arm-none-linux-gnueabi- cpuloadgen
To build and install cpuloadgen:
Where «YOUR_DIR» is a destination directory where cpuloadgen output binary file will be installed (copied, e.g. ubuntu/android filesystem).
Load is a percentage which may be any integer value between 1 and 100.
Duration time unit is seconds. If duration is omitted, generate load(s) until CTRL+C is pressed.
Arguments may be provided in any order.
If no argument is given, generate 100% load on all online CPU cores indefinitely.
E.g.: Generate 100% load on all online CPU cores until CTRL+C is pressed:
Generate 100% load on all online CPU cores during 10 seconds:
Generate 50% load on CPU1 and 100% load on CPU3 during 10 seconds:
# cpuloadgen cpu3=100 cpu1=50 duration=5
How can I produce high CPU load on a Linux server?
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: Is there a better method of testing/maxing-out system resources under load?
@NateKoppenhaver: Yes, that seems to be possible when wrapping them in screen sessions. But I would prefer a more sophisticated solution if possible.
@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.
@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.
17 Answers 17
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
No need to install any extra package, your good old shell is able to do it alone.
This one-liner will load your four cores 1 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:
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=$seconds=$ echo loading $cpus CPUs for $seconds seconds trap 'for p in $pids; do kill $p; done' 0 for ((i=0;i
1 Note 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)..