|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Runs valgrind and reports results. |
| 4 | +# |
| 5 | +# Since jemalloc dropped support for valgrind |
| 6 | +# (https://github.com/jemalloc/jemalloc/issues/369), and both Rust and TensorFlow |
| 7 | +# use jemalloc by default, we need to compile both without it. Unfortunately, |
| 8 | +# compiling TensorFlow from source is expensive, so this script takes a long |
| 9 | +# time to run. |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +cd $(dirname $(readlink -f "$0")) |
| 14 | + |
| 15 | +tensorflow_version=1.1.0 |
| 16 | + |
| 17 | +valgrind_log=valgrind.log |
| 18 | +truncate --size=0 "$valgrind_log" |
| 19 | + |
| 20 | +# Disable jemalloc in TensorFlow. |
| 21 | +export TF_NEED_JEMALLOC=0 |
| 22 | + |
| 23 | +# Disable jemalloc in Rust. |
| 24 | +export TF_RUST_BUILD_FROM_SRC=true |
| 25 | + |
| 26 | +# Don't need to rebuild the world, and `cargo clean --package tensorflow-sys` doesn't seem to do the job. |
| 27 | +rm -rf tensorflow-sys/target |
| 28 | + |
| 29 | +# This is the very expensive step. |
| 30 | +cargo +nightly build --features=nightly -p tensorflow-sys -vvv |
| 31 | + |
| 32 | +# Run valgrind against all the things. |
| 33 | +export LD_LIBRARY_PATH="$(echo "$PWD"/target/debug/build/tensorflow-sys-*/out/lib-v$tensorflow_version)" |
| 34 | +echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH" |
| 35 | +for example in addition regression expressions; do |
| 36 | + cargo +nightly build --features='nightly tensorflow_unstable' --example="$example" |
| 37 | + valgrind --leak-check=full target/debug/examples/"$example" >> "$valgrind_log" 2>&1 |
| 38 | +done |
| 39 | + |
| 40 | +rel_log=$(readlink -f "$PWD"/"$valgrind_log") |
| 41 | +if grep -i 'error summary' < "$valgrind_log" > /dev/null; then |
| 42 | + echo "Error running valgrind. See $rel_log" |
| 43 | +fi |
| 44 | + |
| 45 | +# Aggregate results. |
| 46 | +lost_bytes=$(awk '/(definitely|indirectly) lost:/{sum+=gensub(",","","g",$4)}END{print sum}' < "$valgrind_log") |
| 47 | +echo "Lost bytes: $lost_bytes" |
| 48 | +echo "For details, see $rel_log" |
0 commit comments