Skip to content

Instantly share code, notes, and snippets.

@dasjoe
Created October 31, 2014 15:13
Show Gist options
  • Save dasjoe/bb48b548361a61b16a2e to your computer and use it in GitHub Desktop.
Save dasjoe/bb48b548361a61b16a2e to your computer and use it in GitHub Desktop.
Efficient, multithreaded RAM stress tester, using md5sum
#!/bin/bash
# source: https://groups.google.com/a/zfsonlinux.org/d/msg/zfs-discuss/i09_VBXAyig/UWjPa23cQ0YJ
pids=""
tdir=/tmp/memtest.$$
function cleanup()
{
[[ -n "${pids}" ]] && kill -9 ${pids} &> /dev/null || true
# sleep a little!
sleep 2
umount ${tdir} &> /dev/null || true
[[ -d ${tdir} ]] && /bin/rmdir ${tdir}
}
function run_md5sum()
{
set +xv
# in MiB
msize=${1}
# seconds to run for
ttime=${2}
# file to create
file=${3}
echo
echo "Creating file ${file} of size ${msize} MiB..."
echo
dd if=/dev/urandom of=${file} count=${msize} bs=1M
orgmd5=$(md5sum ${file} | awk '{print $1}')
cdt=$(date +%s)
cdt=$((cdt+ttime))
# tight CPU loop
while true
do
nmd5=$(md5sum ${file} | awk '{print $1}')
# trust that memory used by $orgmd5 and $nmd5 is sane...:-)
if [[ "${nmd5}" != "${orgmd5}" ]]
then
echo
echo "Failed md5sum on file ${file}, new=${nmd5}, org=${orgmd5} ..." | tee -a /tmp/md5sum.err
echo
exit 1
fi
dt=$(date +%s)
if [[ ${dt} -gt ${cdt} ]]
then
echo "Thread ran all the way" | tee -a /tmp/md5sum.out
exit 0
fi
done
}
# run verbose
set -xv
# cleanup on exit
trap cleanup 1 2 3 6 15
# how long to run?
ttime=${1}
if [[ -z "${ttime}" ]]
then
# replace 1 with 3600 later to run for 24 hours by default
ttime=$((24*1))
fi
# create a tmpfs with as much RAM you can spare. Drop caches first.
echo 3 > /proc/sys/vm/drop_caches
tmem=$(grep MemFree /proc/meminfo | awk '{print $2}')
tmem=$((tmem/1024))
# spare 0.5GB for the system in single-user mode and for the programs
# spawned by this script etc.
# change 5120 to 512 for real run.
tmem=$((tmem-5120))
echo
echo "Will create tmpfs of size ${tmem} MiB, and each thread will run for ${ttime} seconds"
echo
mkdir ${tdir}
mount -t tmpfs -o size=$((tmem*1024*1024)) none ${tdir}
# spawn as many copies as the CPUs
ncpus=$(cat /proc/cpuinfo | grep processor | wc -l)
pcpumem=$((tmem/ncpus))
for i in $(seq 1 ${ncpus})
do
run_md5sum ${pcpumem} ${ttime} "${tdir}/${i}" &
pids="${pids} $!"
done
echo
echo "Waiting for PIDs ${pids}..."
echo
wait ${pids}
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment