Linux教程

memtester测试——Linux内存测试工具

本文主要是介绍memtester测试——Linux内存测试工具,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、Memtester简单介绍
Memtester主要是捕获内存错误和一直处于很高或者很低的坏位, 其测试的主要项目有随机值,异或比较,减法,乘法,除法,与或运算等等. 通过给定测试内存的大小和次数, 可以对系统现有的内存进行上面项目的测试。

二、安装

  1. 在http://pyropus.ca/software/memtester/ 下载memtester-4.3.0.tar.gz;
  2. 执行以下命令,安装Memtester。
    tar zxvf memtester-4.2.2.tar.gz
    cd memtester-4.2.2
    make && make install
    三、 参数介绍
    memtester [-p PHYSADDR] [ITERATIONS]
    参数说明:
    MEMORY 申请测试内存的数量,单位默认是megabytes(兆),也可以是B K M G。
    ITERATIONS 测试的次数,默认是无限
    四、测试实例
    开始测试,申请28G内存,测试一次。如下图:

可使用free -h命令查看内存使用情况。

===================================

运行memteste非常简单,作为root ./ memtester <memory> [runs] <memory>:内存大小,单位M [runs]:运行次数 这样我们就可以对单颗CPU的机器进行测试,查看内存是否足够稳定。 但如果我有多颗CPU和较大的memory呢?最简单的方法是多开几个终端,同时运行memtester,每个CPU分配同样大小的内存。 还有一种方法可以使用Baif编写的一个脚本memtester-multi.sh,位置放在memtester-4.0.7下就可以了。 这个脚本只在linux下有效!
#!/bin/sh
# Memory Tester Scripts

# by Baif
# version = date
VERSION="Fri Oct 19 11:56:57 CST 2007"

# trap for irruptions
MEMTESTER=${PWD}/memtester
PPIDKILL=$$
SIDKILL=$$
trap "pkill -9 -P ${PPIDKILL};kill -9 $$" INT
trap "pkill -9 -P ${PPIDKILL};kill -9 $$" KILL


cat <<-EOF_vpps >&2

Version: ${VERSION}
PID: $$
PPIDKILL: ${PPIDKILL}
SIDKILL: ${PPIDKILL}

EOF_vpps

CORE_NUM=$(grep -i ^processor /proc/cpuinfo|wc -l)
MEMTESTERCOPY=${CORE_NUM}
MEM_TOTAL_K=$(awk '/^MemTotal/{print $2}'  /proc/meminfo)
MEM_RESERVE_PERCENTAGE=$((1000*50/1024)) # 95%

MEM_RESERVED=$((MEM_TOTAL_K/1024*MEM_RESERVE_PERCENTAGE/1000))
MEM_TOTAL_TOBETESTED=$((MEM_TOTAL_K/1024-MEM_RESERVED))
MEM_PER_COPY=$((MEM_TOTAL_TOBETESTED/MEMTESTERCOPY))

RUN_DURATION_TIME=0
RUN_LOOPS=-1
RUN_DURATION_TIME_FLAG=0
RUN_LOOPS_FLAG=0
DDPERCOPY_TIME=6s

LOGDIR=/tmp/memtester-log-${$}
mkdir -p ${LOGDIR}

show_help ()
{
        cat <<-EOFshow_HELP >&2

Version: ${VERSION}

        Usage: $(basename ${0})
         -r Directory: the root location of memtester binary file
         -c NUMBER: the copies of memtester should be run
         -m NUMBER: how many memory should be tested totally (in MB)
         -t TIME:   duration mode, how long will the tests go
         -l NUMBER: loops mode,how many loops will each memtester should go

         The option -t and -l are exclusive, which means tests could work 
         only with   1. duration mode or 2. loops mode

         RUN 4 copies memtester with in 24 hours, to test total 4000 MB memory:

         $(basename ${0}) -t 24h -c 4 -m 4000

         RUN 2 copies memtester with in 1 hours, to test total 4000 MB memory:

         $(basename ${0}) -t 1h -c 4 -m 4000

         RUN 4 copies memtester with in 2 loops, to test total 3600 MB memory:

         $(basename ${0}) -l 2 -c 4 -m 3600

         -V/-h/-H: show this info.

        EOFshow_HELP

        exit 0
}

while getopts :c:m:t:l:r:p:hHVvx OPTION
do
 case ${OPTION} in
 c)
        #echo "-c ${OPTARG}"
        MEMTESTERCOPY=${OPTARG}
 ;;
 m)
        #echo "-m ${OPTARG} MB"
        MEM_TOTAL_TOBETESTED=${OPTARG}
        MEM_RESERVED=$((MEM_TOTAL_K/1024-MEM_TOTAL_TOBETESTED))
 ;;
 t)
        #echo "-t ${OPTARG}"
        [ 0 -ne ${RUN_LOOPS_FLAG} ] && echo "-t and -l are exclusive." && exit 222
        RUN_DURATION_TIME=${OPTARG}
        RUN_DURATION_TIME_FLAG=1
 ;;
 l)
        #echo "-l ${OPTARG}"
        [ 0 -ne ${RUN_DURATION_TIME_FLAG} ] && echo && echo "-t and -l are exclusive." && show_help && echo && exit 223
        RUN_LOOPS=${OPTARG};
        RUN_LOOPS_FLAG=1
 ;;
 d)
        #echo "-r ${OPTARG}"
        MEMTESTER=${OPTARG}/memtester
 ;;
 p)
        #echo "-p ${OPTARG}"
        MEMTESTER=${OPTARG}
 ;;
 V|h|H)
        show_help
 ;;
 v)
        set -v
 ;;
 x)
        set -x
 ;;
 ?)     echo "Error...";
        echo "?Unknown args..."
        exit 224
 ;;
 *)     #echo "*Unknown args..."
 esac
done
#exit
[ 0 -eq ${RUN_DURATION_TIME_FLAG} ] && [ 0 -eq ${RUN_LOOPS_FLAG} ] && echo && echo "Please specified which mode should we run... -t or -l" && show_help && echo && exit 225

MEM_PER_COPY=$((MEM_TOTAL_TOBETESTED/MEMTESTERCOPY))

echo "Mem total: " $((MEM_TOTAL_K/1024)) MB
echo "Core total: "${CORE_NUM}
echo "Memtester copys: " ${MEMTESTERCOPY}
echo "Mem per copy: "${MEM_PER_COPY}
echo "Mem total to used: "${MEM_TOTAL_TOBETESTED} MB
if [ ${MEM_RESERVED} -lt 1 ];
then
 echo "Mem reserved: -- No more memory reserved..."
else
 echo "Mem reserved: "${MEM_RESERVED} MB
fi
#exit


# GOGOGO
if [ 0 -ne ${RUN_DURATION_TIME_FLAG} ]; then
echo "Run within a duration: ${RUN_DURATION_TIME}"
elif [ 0 -ne ${RUN_LOOPS_FLAG} ];
then
echo "Run within a loop: ${RUN_LOOPS}"
fi
echo "Working directory: " $PWD
echo "Memtester: " ${MEMTESTER}
echo "LOGs directory: " $LOGDIR
echo
echo -n "Jobs started at date: "
date #+%Y/%m/%d\ %H:%M
echo
#exit

########################
# Run testing within a duration time.
if [ 0 -ne ${RUN_DURATION_TIME_FLAG} ]
then
# prepareing the sleeping killers
 sleep ${RUN_DURATION_TIME} 
    echo -n "End of testing(TIMEOUT)... "
 echo "KILL CHILD" && kill -9 $(pgrep -P ${PPIDKILL} memtester) && echo "Childen processes - KILLED."
 # attention to how the memtesters are forked...
 echo "KILL PARENT" && kill $$  && echo "KILLED." &
 echo "Finished the memtester"
 echo -n "Jobs finished at date: " 
 date #+%Y/%m/%d\ %H:%M 

fi &

echo -n "Waiting (PID: $$) for ${MEMTESTERCOPY} memtesters(${MEM_PER_COPY}MB for each). "
if [ 0 -ne ${RUN_DURATION_TIME_FLAG} ];
then
 echo -n "For time: ${RUN_DURATION_TIME} "
fi
if [ 0 -ne ${RUN_LOOPS_FLAG} ];
then
 echo -n "For loops: ${RUN_LOOPS} " 
fi
echo "..."


while true
do
 MEMTESTER_NUM=0

 echo -n "{"
 while [ ${MEMTESTER_NUM} -lt ${MEMTESTERCOPY} ]
 do
        echo -n " ${MEMTESTER_NUM} "

        if [ 0 -ne ${RUN_DURATION_TIME_FLAG} ]
        then
                        RUN_LOOPS=0
        fi

        ${MEMTESTER} ${MEM_PER_COPY} ${RUN_LOOPS} 2>&1 >> ${LOGDIR}/${MEMTESTER_NUM}.log &
# set loops = 0 to make memtester run loop infinitely...
# .pogo version will run only one loop by default

        sleep ${DDPERCOPY_TIME}
        MEMTESTER_NUM=$(expr $MEMTESTER_NUM + 1)
 done
 echo -n "}"
 wait

 [ 0 -ne ${RUN_LOOPS_FLAG} ] && break
 # memtesters' loops...
done

########################

echo
echo -n "End of testing(Excution ended)... "
pkill -9 -P ${PPIDKILL}
kill $$
echo "Finished the memtester"
echo -n "Jobs finished at date: " 
date #+%Y/%m/%d\ %H:%M
运行这个脚本
sh memtester-multi.sh –c <number> -l <number> -m <memory>
-c:运行几个memtester -l: 运行几次 -m:测试多大的内存,直接填测试总数即可,程序会自动分配个平均值给每个memtester,这样省去我们自己的计算,也不用多开几个终端了,单位MB
例如我有一个4颗CPU,32G内存的机器,我就可以这样运行

sh memtester-multi.sh –c 4 –l 1 –m 3100

大家可以看这个脚本中还可以限定时间,例如我想运行24个小时

sh memtester-multi.sh –t  24h  -c 4 –m 3100
这篇关于memtester测试——Linux内存测试工具的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!