Program to eat CPU and Memory on Linux

Program to eat CPU and Memory on Linux

C program to eat CPU and memory

I always found tough to find a right tool to perform CPU and memory load test on servers or on my test setup. Note that this one just puts load on 1 CPU core, I will also write one for multi core  CPU, mem load test.

So, I decide to write my own and this became my favourite one.

Note: Since this a C program, this can be compiled and used on any platform like Windows etc.,

Also, see Multi core (parallel) CPU and Memory load test (eat multiple CPU cores)

This programs puts 100% load on CPU and allocates the memory you requested. It eats both CPU and memory.

I am just running a math function in an inifite while loop to put CPU load and allocationg memory using calloc to fill up the RAM. Then, I am using alram fucntion to exit the program once it exceeds requested time.

#Usage

eat_cpu_mem.o <num-of-minutes-to-run> <mem-in-gb>

# Compiling C program using gcc

[root@electronproton ~]# gcc -lm eat_cpu_mem.c -o eat_cpu_mem.o

# Running the load test

[root@electronproton ~]# ./eat_cpu_mem.o 2 1
This program will run for approximately 2 minutes and consumes 1 GB of RAM 
Please note that this exists with SIGALRM (14) signal
Alarm clock
[root@electronproton ~]#

 

While the program was running, notice CPU and Memory usage.

1
2
3
4
5
6
7
8
9
top - 17:45:19 up 3 days,  4:59,  8 users,  load average: 0.88, 0.63, 0.46
Tasks: 201 total,   4 running, 196 sleeping,   0 stopped,   1 zombie
%Cpu0  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  6.3 us,  1.6 sy,  0.0 ni, 88.9 id,  3.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1532440 total,    73908 free,  1338320 used,   120212 buff/cache
KiB Swap:  2097148 total,  1229876 free,   867272 used.    37792 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                      
21306 root      20   0 1055820 1.000g     20 R 100.0 68.4   0:48.55 eat_cpu_mem.o                                                                

 

Source code of eat_cpu_mem.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>


// This program can be used for simple CPU and MEM load tests
// This program puts almost 100% load on one cpu core and consumes specified amount of RAM (main memory)
// Please note that the timing behaviour changes based on CPU clock etc., so tune the constant accordingly
// Usage a.out <num-of-minutes-to-run> <mem-in-gb>

int main( int argc, char *argv[] )  {

        long * holder, rand_val=989978777;
        int i;
        long long j, timed, long_type_mult_factor=134217728, mem_in_gb;

        double x=945346346464.3453453453,y=7899.345345345,z=2343523523.242342342;

        if( argc == 3 ) {
                // printf("The argument supplied is %s\n", argv[1]);
                timed = atoi(argv[1]) ;
                mem_in_gb = atoi(argv[2]) * long_type_mult_factor ;

                printf("This program will run for approximately %s minutes and consumes %s GB of RAM \n", argv[1], argv[2]);
                printf("Please note that this exists with SIGALRM (14) signal\n");
        }
        else if( argc > 3 ) {
                printf("Usage a.out <num-of-minutes-to-run> <mem-in-gb>\n");
				exit(0);
        }
        else {
                printf("Usage a.out <num-of-minutes-to-run> <mem-in-gb>\n");
                exit(0);
        }

        // Allocate memory
        holder = calloc(mem_in_gb, sizeof(long));
        memcpy(holder,&rand_val,sizeof(long));

        alarm(timed*60);

	// Burst CPU

	while(1)
	{
                y = sqrt(x) * sin(z) * pow(25,y);
        }
        //free(holder);
return 0;

}

 

Warning: Please do not run this on production systems unless it is a necessary or planned activity. The system’s response time will come down drastically. It  may also endup using all memory in server (if you specify non optimal mem value to program) and might lead to invocation of oom_killer. This can cause server to crash.

 

Please comment with your inputs or feedback.

2 Replies to “Program to eat CPU and Memory on Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *