C-Thread-Pool/tests/memleaks.sh

97 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2015-03-06 19:18:15 +03:00
#! /bin/bash
2015-01-03 17:18:07 +03:00
#
# This file has several tests to check for memory leaks.
# valgrind is used so make sure you have it installed
#
2015-03-06 19:26:05 +03:00
. funcs.sh
2015-01-06 22:10:30 +03:00
# ---------------------------- Tests -----------------------------------
2015-01-09 22:30:29 +03:00
function test_thread_free { #threads
echo "Testing creation and destruction of threads(=$1)"
compile src/no_work.c
2015-01-09 22:30:29 +03:00
output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null)
2015-01-03 17:18:07 +03:00
heap_usage=$(echo "$output" | grep "total heap usage")
allocs=$(extract_num "[0-9]* allocs" "$heap_usage")
frees=$(extract_num "[0-9]* frees" "$heap_usage")
2015-01-09 22:30:29 +03:00
if (( "$allocs" == 0 )); then
err "Allocated 0 times. Something is wrong.." "$output"
2015-01-03 17:18:07 +03:00
fi
2015-01-09 22:30:29 +03:00
if (( "$allocs" == "$frees" )); then
2015-01-06 22:10:30 +03:00
return
fi
err "Allocated $allocs times but freed only $frees" "$output"
2015-01-17 23:27:28 +03:00
exit 1
2015-01-06 22:10:30 +03:00
}
function _test_thread_free_multi { #threads
output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null)
heap_usage=$(echo "$output" | grep "total heap usage")
allocs=$(extract_num "[0-9]* allocs" "$heap_usage")
frees=$(extract_num "[0-9]* frees" "$heap_usage")
if (( "$allocs" == 0 )); then
err "Allocated 0 times. Something is wrong.." "$output"
return 1
fi
if (( "$allocs" != "$frees" )); then
err "Allocated $allocs times but freed only $frees" "$output"
return 1
fi
#echo "Allocs: $allocs Frees: $frees"
}
2015-01-06 22:10:30 +03:00
# This is the same with test_many_thread_allocs but multiplied
function test_thread_free_multi { #threads #times #nparallel
2015-01-13 12:42:20 +03:00
echo "Testing multiple threads creation and destruction in pool(threads=$1 times=$2)"
compile src/no_work.c
pids=()
nparallel="${3:-10}"
# Run tests in p
for (( i = 1; i <= "$2"; i++ )); do
# Run test in background
2015-01-13 13:10:06 +03:00
python -c "import sys; sys.stdout.write('$i/$2\r')"
_test_thread_free_multi "$1" &
pids+=($!)
# Wait for 10 background jobs to finish
if (( "$i" % 10 == 0 )); then
for pid in ${pids[@]}; do
wait $pid
if (( $? != 0 )); then
err "Test failed" "Test failed"
fi
done
pids=()
fi
2015-01-06 22:10:30 +03:00
done
2015-01-13 13:10:06 +03:00
echo
2015-01-06 22:10:30 +03:00
}
2015-01-03 17:18:07 +03:00
# Run tests
assure_installed_valgrind
2015-01-09 22:30:29 +03:00
test_thread_free 1
test_thread_free 2
test_thread_free 4
test_thread_free 8
test_thread_free 1
2015-01-13 12:42:20 +03:00
test_thread_free 20
2015-01-09 22:30:29 +03:00
test_thread_free_multi 4 20
2020-07-21 02:54:13 +03:00
# test_thread_free_multi 3 1000 # Takes way too long
test_thread_free_multi 3 200
# test_thread_free_multi 100 100 # Takes way too long
test_thread_free_multi 100 20 1
2020-07-21 02:54:13 +03:00
2015-01-03 17:18:07 +03:00
2015-01-06 22:10:30 +03:00
echo "No memory leaks"