Linux: How to put a load on system memory?

后端 未结 12 2099
清酒与你
清酒与你 2021-02-10 07:54

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 num

12条回答
  •  爱一瞬间的悲伤
    2021-02-10 08:28

    I took this program and modify the line: mem = 1024*1024*512; //512 mb to say this: 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. It helps me generate a lot of memory load in 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
    # -------------------------------------------------------------------------------------
    # *************************************************************************************
    

提交回复
热议问题