Skip to content

Instantly share code, notes, and snippets.

@elizarov
Created November 18, 2020 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elizarov/5a65d9e1e2f8b0bcdc8d45c145ddbf29 to your computer and use it in GitHub Desktop.
Save elizarov/5a65d9e1e2f8b0bcdc8d45c145ddbf29 to your computer and use it in GitHub Desktop.
Bisect leaks in K/N code
#!/bin/bash
kexe="$1"
if [ "$1" == "" ]; then
echo "Usage bisect_leaks.sh <path-to-test.kexe>"
exit
fi
echo "Bisecting leaks for $kexe"
log="$kexe.log"
err="$kexe.err"
leaks="$kexe.leaks"
rm $leaks
function run {
$($kexe "$@" >$log 2>$err)
# $kexe "$@" | tee $log
if [ `grep FAILED < $log | wc -l` != 0 ]; then
echo "ABORT: There were failing tests:"
grep FAILED < $log
exit
fi
leak_line=`grep "Memory leaks detected" $err`
# leak_line=`grep "Memory leaks detected" $log`
if [ "$leak_line" == "" ]; then
leak_count=0
else
leak_count=`echo "$leak_line" | sed -E -n 's/[^0-9]*([0-9]+).*/\1/p'`
fi
ok_tests=(`sed -E -n 's/\[ OK \] ([a-zA-Z.0-9_]*).*/\1/p' $log | tr "\n" " "`)
}
function bisect {
local mode=$1 # run | norun
shift
local tests=("$@")
local n=${#tests[@]}
if (( n==0 )); then
return
fi
local joined=$(printf ":%s" "${tests[@]}")
local joined=${joined:1}
let last=$n-1
if [ "$mode" == "run" ]; then
echo " .. bisecting $n tests from ${tests[0]} to ${tests[$last]}"
run "--ktest_filter=$joined"
fi
if (( $leak_count == 0 )); then
return
fi
if (( $n == 1 )); then
echo "LEAKING: $joined $leak_count"
echo "$joined $leak_count" >> $leaks
return
fi
local local_leak_count=$leak_count
local tests_a=()
local tests_b=()
for (( i=0; i<$n; i++)); do
if (( $i<$n/2 )); then
tests_a+=(${tests[$i]})
else
tests_b+=(${tests[$i]})
fi
done
local retry=0
while (( $retry < 10 )); do
# Run first half
bisect run "${tests_a[@]}"
local a_leak_count=$leak_count
# Run second half
bisect run "${tests_b[@]}"
local b_leak_count=$leak_count
if (( $a_leak_count == 0 && $b_leak_count == 0 )); then
let retry=$retry+1
echo "!!! WARNING: Flaky leaks in $n tests from ${tests[0]} to ${tests[$last]} -- retry #$retry"
else
local retry=100 # quit
fi
done
# resulting leak count
leak_count=$local_leak_count
}
run
test_count="${#ok_tests[@]}"
echo "-- Total number of memory leaks : $leak_count"
echo "-- Total number of tests : $test_count"
if (( $leak_count != 0 )); then
bisect norun "${ok_tests[@]}"
fi
echo "DONE. See $leaks"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment