Multi core CPU and memory load test
Multi core CPU and memory load test using OpenMP on Linux
A normal C program is serial, which means it can run only on one single core even though your processor has many cores.
We have seen how to run CPU and memory load test on single core (eat CPU and Memory on single core).
In order run a multi core cpu and memory load test we have to use parallel libraries such as OpenMP
In this program I have used OpenMP and created a parallel for loop that can run any number of CPU cores that exist in your system or server.
# Compiling the parallel OpenMP program needs the flag “-fopenmp“. See below for steps.
[root@electronproton ~]# gcc -lm -fopenmp eat_cpu_mem_parallel.c -o eat_cpu_mem_p.o
# Running parallel CPU and Memory load test
[root@electronproton ~]# ./eat_cpu_mem_p.o 2 0.1 This program will run for approximately 2 minutes and consumes 0.1 GB of RAM Please note that this exists with SIGALRM (14) signal Alarm clock [root@electronproton ~]#
#How to view threads of a program in the top command
By default “top” command in Linux hides threads view. To see the threads do as follow
inside top, press “H” (capital H) key to turn on threads view.
Notice that the following program is running 2 threads on 2 cores and is putting 100% load on both.
1 2 3 4 5 6 7 8 9 |
Threads: 406 total, 9 running, 396 sleeping, 0 stopped, 1 zombie %Cpu0 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st %Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1532440 total, 639372 free, 507480 used, 385588 buff/cache KiB Swap: 2097148 total, 1336792 free, 760356 used. 833496 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 22973 root 20 0 19932 668 544 R 99.0 0.0 0:20.34 eat_cpu_mem_p.o 22972 root 20 0 19932 668 544 R 98.7 0.0 0:20.53 eat_cpu_mem_p.o |
#Controlling number of threads (on how many cores to run)
You can control your OpenMP program to run on desired number of cores. By default it runs on all cores in your system. Variable “OMP_NUM_THREADS” controls number of threads to be launched.
In the below example, I would like to restrict the program to one thread.
[root@electronproton ~]# export OMP_NUM_THREADS=1 [root@electronproton ~]# ./eat_cpu_mem_p.o 2 0.1 This program will run for approximately 2 minutes and consumes 0.1 GB of RAM Please note that this exits with SIGALRM (14) signal Alarm clock [root@electronproton ~]#
Source code of C OPenMP (parallel) CPU and Memory load test program.
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 54 55 |
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<omp.h> // This program can be used for simple parallel (mulri core) CPU and MEM load tests // This program puts almost 100% load on any or all CPU cores and consumes specified amount of RAM (main memory) // Usage a.out <num-of-minutes-to-run> <mem-in-gb> int main( int argc, char *argv[] ) { long * holder, rand_val=989978777; long i,m; 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 exits 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); m=9223372036854775807; // Burst CPU // Parallel For loop #pragma omp parallel for for(i=0;i<m;i++) { y = sqrt(x) * sin(z) * pow(25,y); } //free(holder); return 0; } |
Notice that the program is linked with libgomp library.
[root@electronproton ~]# ldd eat_cpu_mem_p.o linux-vdso.so.1 => (0x00007ffe50fd6000) libm.so.6 => /lib64/libm.so.6 (0x00007fd1ac99d000) libgomp.so.1 => /lib64/libgomp.so.1 (0x00007fd1ac777000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd1ac55a000) libc.so.6 => /lib64/libc.so.6 (0x00007fd1ac199000) /lib64/ld-linux-x86-64.so.2 (0x00007fd1accb3000) [root@electronproton ~]#
Notice that the OpenMP library is packaged in libgomp rpm package.
[root@electronproton ~]# rpm -qf /lib64/libgomp.so.1 libgomp-4.8.5-11.el7.x86_64 [root@electronproton ~]#
The sourcecode can also be found on GitHub
https://github.com/Electronprotonweb/cpumemloadtest
2 Replies to “Multi core CPU and memory load test”