Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Last active June 10, 2020 21:51
Show Gist options
  • Select an option

  • Save rr-codes/22bd20e4d0949bc9201a010f4baeed33 to your computer and use it in GitHub Desktop.

Select an option

Save rr-codes/22bd20e4d0949bc9201a010f4baeed33 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Given a folder of source *.cpp files, compiles each file and profiles heap and stack usage with massif.
# Results are added to a "massif" directory in the pwd
# a) create two subdirectories titled 'generated' and 'massif' in the cwd
# b) iterate over each file inside the directory specified by the first argument
# c) for each file, run `g++` on it and store the executable in /generated
# d) for each file in /generated, run `massif` on it and store the output in \massif
# e) for each file in /massif, extract the relevant info via regex and store everything in $2
# f) done
generated="${PWD}/generated"
massif="${PWD}/massif"
rm -rf ${generated}; mkdir -v ${generated}
rm -rf ${massif}; mkdir -v ${massif}
echo ""
rm -rf $2
touch $2
for file in $1/*.cpp
do
echo "Building ${file}..."
name_ext=${file##*/}
name=${name_ext%.*}
executable="${generated}/${name}"
g++ ${file} -std=c++17 -isystem ~/etl-18.1.3/include -o ${executable}
chmod u+x ${executable}
done
echo ""
for file in ${generated}/*
do
echo "Running massif on ${file}..."
name_ext=${file##*/}
name=${name_ext%.*}
valgrind --tool=massif --massif-out-file="${massif}/${name}.txt" -q --time-unit=B --stacks=yes ${file}
done
regex=$'time=([0-9]+)
mem_heap_B=([0-9]+)
mem_heap_extra_B=([0-9]+)
mem_stacks_B=([0-9]+)'
format_row() {
local s=$1 regex=$2
while [[ $s =~ $regex ]]
do
t="${BASH_REMATCH[1]}"
heap="${BASH_REMATCH[2]}"
stack="${BASH_REMATCH[4]}"
echo "${t}, ${heap}, ${stack}"
s=${s#*"${BASH_REMATCH[4]}"}
done
}
for file in ${massif}/*
do
echo "Parsing ${file}..."
echo $file >> $2
echo "time, heap, stack" >> $2
text=$(<${file})
format_row "$text" "$regex" >> "$2"
echo "" >> $2
done
echo ""
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment