Linux: how to load the load on system memory?

I am working on a small feature that gives my users an idea of ​​how busy the processor is.

I use cat/proc/loadavg , which returns the well-known 3 numbers.

My problem is that the processor is not doing anything right now while I am working.

Is there a good way to generate some CPU load, I was thinking something like makecpudosomething 30 , for a load of 0.3 or similar. Is there such an application?

Also, is there a way to use RAM in controlled mode?

+5
source share
13 answers

I didn’t understand very well if you want to create arbitrary CPU load or processor load . Yes, these are really different things. I will try to address both issues.

First of all: load - the average number of processes in starting, starting, or waiting for the CPU scheduler queues for a certain period of time, “so that your processor” has it.

So, if you want to generate an arbitrary load (say, 0.3), you must start the process within 30% of the time, and then remove it from the execution queue within 70% of the time, for example, moving it to a sleeping queue or killing it.

You can try this script to do this:

 export LOAD=0.3 while true do yes > /dev/null & sleep $LOAD killall yes sleep `echo "1 - $LOAD" | bc` done 

Please note that you need to wait some time (1, 10 and 15 minutes) to get the corresponding numbers, and other processes in your system will influence it. The more busy your system, the more numbers will float. The last number (interval of 15 minutes) tends to be the most accurate.

CPU usage is the time that the processor has been used to process computer program instructions.

So, if you want to generate arbitrary CPU usage (say, 30%), you must start a process that is associated with the processor in 30% of cases and is 70% of it.

I wrote an example to show you that:

 #include <stdlib.h> #include <unistd.h> #include <err.h> #include <math.h> #include <sys/time.h> #include <stdarg.h> #include <sys/wait.h> #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 <= SLEEPTIME) usSleep = SLEEPTIME - usSleepDelay; else usSleep = SLEEPTIME; gettime (&tSleepStart); usleep (usSleep); gettime (&tSleepStop); usSleepDelay = timediff (&tSleepStop, &tSleepStart) - usSleep; } 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 a fixed amount of RAM, you can use the program in cgkanchi's answer.

+7
source
 while true; do openssl speed; done 

Also, the stress program allows you to load the processor / memory / disk to the levels you want to simulate:

stress is an intentionally simple workload generator for POSIX systems. It imposes a configurable amount of CPU, memory, I / O and disk voltage on the system. It is written in C and is free software licensed under GPLv2.

to maintain a certain level of CPU usage, say 30%, try cpulimit:

he will adapt to the current system environment and configure for any other activity in the system.

there is also a kernel fix for cpu speed limits: http://lwn.net/Articles/185489/

+14
source

To eat a fixed amount of RAM, you can simply:

 #include <stdlib.h> #include <string.h> #define UNIX 1 //remove the above line if running under Windows #ifdef UNIX #include <unistd.h> #else #include <windows.h> #endif int main(int argc, char** argv) { unsigned long mem; if(argc==1) mem = 1024*1024*512; //512 mb else if(argc==2) mem = (unsigned) atol(argv[1]); else { printf("Usage: loadmem <memory in bytes>"); exit(1); } char* ptr = malloc(mem); while(1) { memset(ptr, 0, mem); #ifdef UNIX sleep(120); #else Sleep(120*1000); #endif } } 

It seems that a memset () call is required because at least on OS X, the actual memory does not seem to be used until it is initialized.

EDIT: fixed in response to comment

+3
source

Use "memtester" to run memory regression tests on Linux.

+2
source

I took this program and changed the line: mem = 1024 * 1024 * 512; // 512 MB say: mem = 1 * 1024 * 1024 * 1024; // 1 GB and compile it.

$ gcc iss_mem.c -o iss_mem

And write a bash wrapper around the compiled version of the C program above. This helps me generate a lot of memory load on my server.

 #!/bin/bash # Author: Mamadou Lamine Diatta # Senior Principal Consultant / Infrastructure Architect # Email: diatta at post dot harvard dot edu # -------------------------------------------------------------------------------------- # ************************************************************************************* memsize_kb=`grep -i MemTotal /proc/meminfo | awk '{print $2}'` MemTotal=$(($memsize_kb*1024)) for i in `seq 1 50` do echo "`date +"%F:%H:%M:%S"` ----------------- Running [ $i ] iteration(s)" MemToAlloc=$((1*1024*1204*1204)) # 1Gb of memory per iss_mem call TRESHOLD=$(($MemTotal/$MemToAlloc)) # We are not supposed to make the system # run out of memory rand=1000 # High enough to force a new one while (( $rand > $TRESHOLD )) do rand=$(($RANDOM/1000)) done if [ $rand -eq 0 ] then rand=1 fi echo `date +"%F:%H:%M:%S"` Running $rand iss_mem in parallel ... for j in `seq 1 $rand` do ${ISSHOME}/bin/iss_mem > /dev/null & # NOTE: gcc iss_mem.c -o iss_mem done sleep 180 jobs -p kill `jobs -p` sleep 30 done # ------------------------------------------------------------------------------------- # ************************************************************************************* 
+1
source

Very simple: install stress and do:

 stress --vm X --vm-bytes YM 
  • replace X with the number of workers you want to create, and " malloc() " is your RAM
  • replace Y with the amount of memory that each worker should allocate

Example:

 stress --vm 2 --vm-bytes 128M 
+1
source

You can emphasize the usefulness, as it is a workload generator tool designed to provide your system with a custom measure of CPU, memory, I / O and disk voltage.

To start 1 vm stressor using 1 GB of virtual memory for 60 seconds, enter:

stress --vm 1 --vm-bytes 1G --vm-keep -t 60s

+1
source

Do you find using prime95 ?

I'm not sure you can limit it to that percentage ...

0
source
 mar k@localhost $ time pi 1048576 | egrep '.*total$' 

This is a simple benchmarking command that will give your processor a routine, post your time: D

0
source

The easiest way I found to download RAM (and SWAP) is to use Perl:

 my $allocation = "A" x (1024 * 1024 * $ARGV[0]); print "\nAllocated " . length($allocation) . "\n"; 
0
source

We hope that this application will be useful:

https://www.devin.com/lookbusy/

Installation and use steps are in a github project that uses lookbusy.

https://github.com/beloglazov/cpu-load-generator

Excerpt from the Github page:

To generate a sequence of 20%, 90% and 50% processor load for 20 seconds each on 2 cores, using the test.data file,

 python cpu-load-generator.py -n 2 20 test.data 
0
source

1G memory

 python -c 'a="a"*1024**3;raw_input()' 
0
source

All Articles