mirror of
https://github.com/Pithikos/C-Thread-Pool
synced 2024-11-22 05:31:18 +03:00
147 lines
3.3 KiB
Bash
Executable File
147 lines
3.3 KiB
Bash
Executable File
#! /bin/bash
|
|
|
|
#
|
|
# This file has several tests to check for memory leaks.
|
|
# valgrind is used so make sure you have it installed
|
|
#
|
|
|
|
|
|
|
|
# --------------------------- Generic ----------------------------------
|
|
|
|
function needle { #needle #haystack
|
|
python -c "import re; print(re.search(r'$1', '$2').group(0))"
|
|
if (( $? != 0 )); then
|
|
err "Python script error"
|
|
fi
|
|
}
|
|
function extract_num { #needle with number #haystack
|
|
string=$(needle "$1" "$2")
|
|
needle "[0-9]*" "$string"
|
|
if (( $? != 0 )); then
|
|
err "Python script error"
|
|
fi
|
|
}
|
|
function err { #string #log
|
|
echo "------------------- ERROR ------------------------"
|
|
echo "$1"
|
|
echo "$2" >> memleaks.log
|
|
exit 1
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------- Tests -----------------------------------
|
|
|
|
|
|
function test_single_thread_alloc {
|
|
|
|
read -d '' code <<"EOF"
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <time.h>
|
|
#include "../thpool.h"
|
|
|
|
int main(){
|
|
|
|
thpool_t* threadpool;
|
|
threadpool = thpool_init(1);
|
|
thpool_destroy(threadpool);
|
|
|
|
return 0;
|
|
}
|
|
EOF
|
|
|
|
echo "Testing single thread creation and destruction in pool"
|
|
echo "$code" > _test_.c
|
|
gcc _test_.c ../thpool.c -pthread -o test
|
|
output=$(valgrind --leak-check=full --track-origins=yes ./test 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" == "$frees" ]; then
|
|
return
|
|
fi
|
|
err "Allocated $allocs times but freed only $frees" "$output"
|
|
}
|
|
|
|
|
|
function test_many_thread_allocs {
|
|
|
|
read -d '' code <<"EOF"
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <time.h>
|
|
#include "../thpool.h"
|
|
|
|
int main(){
|
|
|
|
thpool_t* threadpool;
|
|
threadpool = thpool_init(4); // VALGRIND SPENDS 1 SEC TO KILL EACH THREAD
|
|
// SO KEEP THIS LOW
|
|
thpool_destroy(threadpool);
|
|
|
|
return 0;
|
|
}
|
|
EOF
|
|
|
|
echo "Testing multiple threads creation and destruction in pool (est ~4secs)"
|
|
echo "$code" > _test_.c
|
|
gcc _test_.c ../thpool.c -pthread -o test
|
|
output=$(valgrind --leak-check=full --track-origins=yes ./test 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" == "$frees" ]; then
|
|
return
|
|
fi
|
|
err "Allocated $allocs times but freed only $frees" "$output"
|
|
}
|
|
|
|
|
|
# This is the same with test_many_thread_allocs but multiplied
|
|
function test_many_thread_allocs_multi {
|
|
|
|
read -d '' code <<"EOF"
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <time.h>
|
|
#include "../thpool.h"
|
|
|
|
int main(){
|
|
|
|
thpool_t* threadpool;
|
|
threadpool = thpool_init(4); // VALGRIND SPENDS 1 SEC TO KILL EACH THREAD
|
|
// SO KEEP THIS LOW
|
|
thpool_destroy(threadpool);
|
|
|
|
return 0;
|
|
}
|
|
EOF
|
|
|
|
echo "Testing multiple threads creation and destruction in pool (est ~40secs)"
|
|
echo "$code" > _test_.c
|
|
gcc _test_.c ../thpool.c -pthread -o test
|
|
for i in {1..10}; do
|
|
output=$(valgrind --leak-check=full --track-origins=yes ./test 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" != "$frees" ]; then
|
|
err "Allocated $allocs times but freed only $frees" "$output"
|
|
fi
|
|
done
|
|
}
|
|
|
|
|
|
|
|
# Run tests
|
|
test_single_thread_alloc
|
|
test_many_thread_allocs
|
|
test_many_thread_allocs_multi
|
|
|
|
echo "No memory leaks"
|