3 # Copyright (c) 2005, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
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
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.
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.
33 # Author: Craig Silverstein
35 # Runs the heap-profiler unittest and makes sure the profile looks appropriate.
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> [...]
41 # This is because libtool sometimes turns the 'executable' into a
42 # shell script which runs an actual binary somewhere else.
44 # We expect BINDIR and PPROF_PATH to be set in the environment.
45 # If not, we set them to some reasonable values
47 PPROF_PATH
="${PPROF_PATH:-$BINDIR/src/pprof}"
49 if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
50 echo "USAGE: $0 [unittest dir] [path to pprof]"
51 echo " By default, unittest_dir=$BINDIR, pprof_path=$PPROF_PATH"
55 HEAP_PROFILER
="${1:-$BINDIR}/heap-profiler_unittest"
56 PPROF
="${2:-$PPROF_PATH}"
57 TEST_TMPDIR
=/tmp
/heap_profile_info
59 # It's meaningful to the profiler, so make sure we know its state
63 mkdir
"$TEST_TMPDIR" ||
exit 2
67 # Given one profile (to check the contents of that profile) or two
68 # profiles (to check the diff between the profiles), and a function
69 # name, verify that the function name takes up at least 90% of the
70 # allocated memory. The function name is actually specified first.
75 # get program name. Note we have to unset HEAPPROFILE so running
76 # help doesn't overwrite existing profiles.
77 exec=`unset HEAPPROFILE; $HEAP_PROFILER --help | awk '{print $2; exit;}'`
80 [ -f "$1" ] ||
{ echo "Profile not found: $1"; exit 1; }
81 [ -f "$2" ] ||
{ echo "Profile not found: $2"; exit 1; }
82 $PPROF --base="$1" $exec "$2" >"$TEST_TMPDIR/output.pprof" 2>&1
84 [ -f "$1" ] ||
{ echo "Profile not found: $1"; exit 1; }
85 $PPROF $exec "$1" >"$TEST_TMPDIR/output.pprof" 2>&1
88 cat "$TEST_TMPDIR/output.pprof" \
89 |
tr -d % |
awk '$6 ~ /^'$function'$/ && $2 > 90 {exit 1;}'
92 echo "--- Test failed for $function: didn't account for 90% of executable memory"
93 echo "--- Program output:"
94 cat "$TEST_TMPDIR/output"
95 echo "--- pprof output:"
96 cat "$TEST_TMPDIR/output.pprof"
98 num_failures
=`expr $num_failures + 1`
102 VerifyOutputContains
() {
105 if ! grep "$text" "$TEST_TMPDIR/output" >/dev
/null
2>&1; then
106 echo "--- Test failed: output does not contain '$text'"
107 echo "--- Program output:"
108 cat "$TEST_TMPDIR/output"
110 num_failures
=`expr $num_failures + 1`
114 HEAPPROFILE
="$TEST_TMPDIR/test"
115 HEAP_PROFILE_INUSE_INTERVAL
="10240" # need this to be 10Kb
116 HEAP_PROFILE_ALLOCATION_INTERVAL
="$HEAP_PROFILE_INUSE_INTERVAL"
117 HEAP_PROFILE_DEALLOCATION_INTERVAL
="$HEAP_PROFILE_INUSE_INTERVAL"
119 export HEAP_PROFILE_INUSE_INTERVAL
120 export HEAP_PROFILE_ALLOCATION_INTERVAL
121 export HEAP_PROFILE_DEALLOCATION_INTERVAL
123 # We make the unittest run a child process, to test that the child
124 # process doesn't try to write a heap profile as well and step on the
125 # parent's toes. If it does, we expect the parent-test to fail.
126 $HEAP_PROFILER 1 >$TEST_TMPDIR/output
2>&1 # run program, with 1 child proc
128 VerifyMemFunction Allocate2
"$HEAPPROFILE.1329.heap"
129 VerifyMemFunction Allocate
"$HEAPPROFILE.1448.heap" "$HEAPPROFILE.1548.heap"
131 # Check the child process got to emit its own profile as well.
132 VerifyMemFunction Allocate2
"$HEAPPROFILE"_
*.1329.heap
133 VerifyMemFunction Allocate
"$HEAPPROFILE"_
*.1448.heap
"$HEAPPROFILE"_
*.1548.heap
135 # Make sure we logged both about allocating and deallocating memory
136 VerifyOutputContains
"62 MB allocated"
137 VerifyOutputContains
"62 MB freed"
139 # Now try running without --heap_profile specified, to allow
140 # testing of the HeapProfileStart/Stop functionality.
141 $HEAP_PROFILER >"$TEST_TMPDIR/output2" 2>&1
143 rm -rf $TMPDIR # clean up
145 if [ $num_failures = 0 ]; then
148 echo "Tests finished with $num_failures failures"