| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | |
| 3 | # Copyright (c) 2005, Google Inc. |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are |
| 8 | # met: |
| 9 | # |
| 10 | # * Redistributions of source code must retain the above copyright |
| 11 | # notice, this list of conditions and the following disclaimer. |
| 12 | # * Redistributions in binary form must reproduce the above |
| 13 | # copyright notice, this list of conditions and the following disclaimer |
| 14 | # in the documentation and/or other materials provided with the |
| 15 | # distribution. |
| 16 | # * Neither the name of Google Inc. nor the names of its |
| 17 | # contributors may be used to endorse or promote products derived from |
| 18 | # this software without specific prior written permission. |
| 19 | # |
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | # --- |
| 33 | # Author: Craig Silverstein |
| 34 | # |
| 35 | # Runs the heap-profiler unittest and makes sure the profile looks appropriate. |
| 36 | # |
| 37 | # We run under the assumption that if $HEAP_PROFILER is run with --help, |
| 38 | # it prints a usage line of the form |
| 39 | # USAGE: <actual executable being run> [...] |
| 40 | # |
| 41 | # This is because libtool sometimes turns the 'executable' into a |
| 42 | # shell script which runs an actual binary somewhere else. |
| 43 | |
| 44 | # We expect BINDIR and PPROF_PATH to be set in the environment. |
| 45 | # If not, we set them to some reasonable values |
| 46 | BINDIR="${BINDIR:-.}" |
| 47 | PPROF_PATH="${PPROF_PATH:-$BINDIR/src/pprof}" |
| 48 | |
| 49 | if [scip-shell system . . `[`. "x$1" = "x-h" -o "x$1" = "x--help" ]; then |
| 50 | echoscip-shell system . . echo. "USAGE: $0 [unittest dir] [path to pprof]" |
| 51 | echoscip-shell system . . echo. " By default, unittest_dir=$BINDIR, pprof_path=$PPROF_PATH" |
| 52 | exit 1 |
| 53 | fi |
| 54 | |
| 55 | HEAP_PROFILER="${1:-$BINDIR/heap-profiler_unittest}" |
| 56 | PPROF="${2:-$PPROF_PATH}" |
| 57 | TEST_TMPDIR=`mktempscip-shell system . . mktemp. -d /tmp/heap-profiler_unittest.XXXXXXscip-shell filesystem . . `/tmp/heap-profiler_unittest.XXXXXX`.` |
| 58 | |
| 59 | # It's meaningful to the profiler, so make sure we know its state |
| 60 | unset HEAPPROFILE |
| 61 | |
| 62 | num_failures=0 |
| 63 | |
| 64 | # Given one profile (to check the contents of that profile) or two |
| 65 | # profiles (to check the diff between the profiles), and a function |
| 66 | # name, verify that the function name takes up at least 90% of the |
| 67 | # allocated memory. The function name is actually specified first. |
| 68 | VerifyMemFunctionscip-shell shell shell-project 0.0.0 VerifyMemFunction().() { |
| 69 | function="$1" |
| 70 | shift |
| 71 | |
| 72 | # get program name. Note we have to unset HEAPPROFILE so running |
| 73 | # help doesn't overwrite existing profiles. |
| 74 | exec=`unset HEAPPROFILE; $HEAP_PROFILER --help | awkscip-shell system . . awk. '{print $2; exit;}'` |
| 75 | |
| 76 | if [scip-shell system . . `[`. $# = 2 ]; then |
| 77 | [scip-shell system . . `[`. -f "$1" ] || { echoscip-shell system . . echo. "Profile not found: $1"; exit 1; } |
| 78 | [scip-shell system . . `[`. -f "$2" ] || { echoscip-shell system . . echo. "Profile not found: $2"; exit 1; } |
| 79 | $PPROF --base="$1" $exec "$2" >"$TEST_TMPDIR/output.pprof" 2>&1 |
| 80 | else |
| 81 | [scip-shell system . . `[`. -f "$1" ] || { echoscip-shell system . . echo. "Profile not found: $1"; exit 1; } |
| 82 | $PPROF $exec "$1" >"$TEST_TMPDIR/output.pprof" 2>&1 |
| 83 | fi |
| 84 | |
| 85 | catscip-shell system . . cat. "$TEST_TMPDIR/output.pprof" \ |
| 86 | | trscip-shell system . . tr. -d % | awkscip-shell system . . awk. '$6 ~ /^'$function'$/ && ($2+0) > 90 {exit 1;}' |
| 87 | if [scip-shell system . . `[`. $? != 1 ]; then |
| 88 | echoscip-shell system . . echo. |
| 89 | echoscip-shell system . . echo. "--- Test failed for $function: didn't account for 90% of executable memory" |
| 90 | echoscip-shell system . . echo. "--- Program output:" |
| 91 | catscip-shell system . . cat. "$TEST_TMPDIR/output" |
| 92 | echoscip-shell system . . echo. "--- pprof output:" |
| 93 | catscip-shell system . . cat. "$TEST_TMPDIR/output.pprof" |
| 94 | echoscip-shell system . . echo. "---" |
| 95 | num_failures=`exprscip-shell system . . expr. $num_failures + 1` |
| 96 | fi |
| 97 | } |
| 98 | |
| 99 | VerifyOutputContainsscip-shell shell shell-project 0.0.0 VerifyOutputContains().() { |
| 100 | text="$1" |
| 101 | |
| 102 | if ! grepscip-shell system . . grep. "$text" "$TEST_TMPDIR/output" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 103 | echoscip-shell system . . echo. "--- Test failed: output does not contain '$text'" |
| 104 | echoscip-shell system . . echo. "--- Program output:" |
| 105 | catscip-shell system . . cat. "$TEST_TMPDIR/output" |
| 106 | echoscip-shell system . . echo. "---" |
| 107 | num_failures=`exprscip-shell system . . expr. $num_failures + 1` |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | HEAPPROFILE="$TEST_TMPDIR/test" |
| 112 | HEAP_PROFILE_INUSE_INTERVAL="10240" # need this to be 10Kb |
| 113 | HEAP_PROFILE_ALLOCATION_INTERVAL="$HEAP_PROFILE_INUSE_INTERVAL" |
| 114 | HEAP_PROFILE_DEALLOCATION_INTERVAL="$HEAP_PROFILE_INUSE_INTERVAL" |
| 115 | export HEAPPROFILE |
| 116 | export HEAP_PROFILE_INUSE_INTERVAL |
| 117 | export HEAP_PROFILE_ALLOCATION_INTERVAL |
| 118 | export HEAP_PROFILE_DEALLOCATION_INTERVAL |
| 119 | |
| 120 | # We make the unittest run a child process, to test that the child |
| 121 | # process doesn't try to write a heap profile as well and step on the |
| 122 | # parent's toes. If it does, we expect the parent-test to fail. |
| 123 | $HEAP_PROFILER 1 >$TEST_TMPDIR/output 2>&1 # run program, with 1 child proc |
| 124 | |
| 125 | VerifyMemFunctionscip-shell shell shell-project 0.0.0 VerifyMemFunction(). Allocate2 "$HEAPPROFILE.1329.heap" |
| 126 | VerifyMemFunctionscip-shell shell shell-project 0.0.0 VerifyMemFunction(). Allocate "$HEAPPROFILE.1448.heap" "$HEAPPROFILE.1548.heap" |
| 127 | |
| 128 | # Check the child process got to emit its own profile as well. |
| 129 | VerifyMemFunctionscip-shell shell shell-project 0.0.0 VerifyMemFunction(). Allocate2 "$HEAPPROFILE"_*.1329.heap |
| 130 | VerifyMemFunctionscip-shell shell shell-project 0.0.0 VerifyMemFunction(). Allocate "$HEAPPROFILE"_*.1448.heap "$HEAPPROFILE"_*.1548.heap |
| 131 | |
| 132 | # Make sure we logged both about allocating and deallocating memory |
| 133 | VerifyOutputContainsscip-shell shell shell-project 0.0.0 VerifyOutputContains(). "62 MB allocated" |
| 134 | VerifyOutputContainsscip-shell shell shell-project 0.0.0 VerifyOutputContains(). "62 MB freed" |
| 135 | |
| 136 | # Now try running without --heap_profile specified, to allow |
| 137 | # testing of the HeapProfileStart/Stop functionality. |
| 138 | $HEAP_PROFILER >"$TEST_TMPDIR/output2" 2>&1 |
| 139 | |
| 140 | rmscip-shell system . . rm. -rf $TEST_TMPDIR # clean up |
| 141 | |
| 142 | if [scip-shell system . . `[`. $num_failures = 0 ]; then |
| 143 | echoscip-shell system . . echo. "PASS" |
| 144 | else |
| 145 | echoscip-shell system . . echo. "Tests finished with $num_failures failures" |
| 146 | fi |
| 147 | exit $num_failures |