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 4 profiler unittests and makes sure their profiles look
36 # appropriate. We expect two commandline args, as described below.
38 # We run under the assumption that if $PROFILER1 is run with no
39 # arguments, it prints a usage line of the form
40 # USAGE: <actual executable being run> [...]
42 # This is because libtool sometimes turns the 'executable' into a
43 # shell script which runs an actual binary somewhere else.
45 # We expect BINDIR and PPROF_PATH to be set in the environment.
46 # If not, we set them to some reasonable values
48 PPROF_PATH
="${PPROF_PATH:-$BINDIR/src/pprof}"
50 if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
51 echo "USAGE: $0 [unittest dir] [path to pprof]"
52 echo " By default, unittest_dir=$BINDIR, pprof_path=$PPROF_PATH"
56 TMPDIR
=/tmp
/profile_info
58 UNITTEST_DIR
=${1:-$BINDIR}
59 PPROF
=${2:-$PPROF_PATH}
61 # We test the sliding-window functionality of the cpu-profile reader
62 # by using a small stride, forcing lots of reads.
63 PPROF_FLAGS
="--test_stride=128"
65 PROFILER1
="$UNITTEST_DIR/profiler1_unittest"
66 PROFILER2
="$UNITTEST_DIR/profiler2_unittest"
67 PROFILER3
="$UNITTEST_DIR/profiler3_unittest"
68 PROFILER4
="$UNITTEST_DIR/profiler4_unittest"
70 # Unfortunately, for us, libtool can replace executables with a shell
71 # script that does some work before calling the 'real' executable
72 # under a different name. We need the 'real' executable name to run
73 # pprof on it. We've constructed all the binaries used in this
74 # unittest so when they are called with no arguments, they report
75 # their argv[0], which is the real binary name.
77 "$1" 2>&1 |
awk '{print $2; exit;}'
80 PROFILER1_REALNAME
=`Realname "$PROFILER1"`
81 PROFILER2_REALNAME
=`Realname "$PROFILER2"`
82 PROFILER3_REALNAME
=`Realname "$PROFILER3"`
83 PROFILER4_REALNAME
=`Realname "$PROFILER4"`
85 # It's meaningful to the profiler, so make sure we know its state
89 mkdir
"$TMPDIR" ||
exit 2
94 num_failures
=`expr $num_failures + 1`
97 # Takes two filenames representing profiles, with their executable scripts,
98 # and a multiplier, and verifies that the 'contentful' functions in
99 # each profile take the same time (possibly scaled by the given
100 # multiplier). It used to be "same" meant within 50%, after adding an
101 # noise-reducing X units to each value. But even that would often
102 # spuriously fail, so now it's "both non-zero". We're pretty forgiving.
110 # We are careful not to put exec1 and exec2 in quotes, because if
111 # they are the empty string, it means we want to use the 1-arg
113 mthread1
=`"$PPROF" $PPROF_FLAGS $exec1 "$prof1" | grep test_main_thread | awk '{print $1}'`
114 mthread2
=`"$PPROF" $PPROF_FLAGS $exec2 "$prof2" | grep test_main_thread | awk '{print $1}'`
115 mthread1_plus
=`expr $mthread1 + 5`
116 mthread2_plus
=`expr $mthread2 + 5`
117 if [ -z "$mthread1" ] ||
[ -z "$mthread2" ] || \
118 [ "$mthread1" -le 0 -o "$mthread2" -le 0 ]
119 # || [ `expr $mthread1_plus \* $mult` -gt `expr $mthread2_plus \* 2` -o \
120 # `expr $mthread1_plus \* $mult \* 2` -lt `expr $mthread2_plus` ]
123 echo ">>> profile on $exec1 vs $exec2 with multiplier $mult failed:"
124 echo "Actual times (in profiling units) were '$mthread1' vs. '$mthread2'"
130 # Takes two filenames representing profiles, and optionally their
131 # executable scripts (these may be empty if the profiles include
132 # symbols), and verifies that the two profiles are identical.
139 # We are careful not to put exec1 and exec2 in quotes, because if
140 # they are the empty string, it means we want to use the 1-arg
142 "$PPROF" $PPROF_FLAGS $exec1 "$prof1" > "$TMPDIR/out1"
143 "$PPROF" $PPROF_FLAGS $exec2 "$prof2" > "$TMPDIR/out2"
144 diff=`diff "$TMPDIR/out1" "$TMPDIR/out2"`
146 if [ ! -z "$diff" ]; then
148 echo ">>> profile doesn't match, args: $exec1 $prof1 vs. $exec2 $prof2"
156 # Takes a filename representing a profile, with its executable,
157 # and a multiplier, and verifies that the main-thread function takes
158 # the same amount of time as the other-threads function (possibly scaled
159 # by the given multiplier). Figuring out the multiplier can be tricky,
160 # since by design the main thread runs twice as long as each of the
161 # 'other' threads! It used to be "same" meant within 50%, after adding an
162 # noise-reducing X units to each value. But even that would often
163 # spuriously fail, so now it's "both non-zero". We're pretty forgiving.
164 VerifyAcrossThreads
() {
166 # We need to run the script with no args to get the actual exe name
170 # We are careful not to put exec1 in quotes, because if it is the
171 # empty string, it means we want to use the 1-arg version of pprof.
172 mthread
=`$PPROF $PPROF_FLAGS $exec1 "$prof1" | grep test_main_thread | awk '{print $1}'`
173 othread
=`$PPROF $PPROF_FLAGS $exec1 "$prof1" | grep test_other_thread | awk '{print $1}'`
174 if [ -z "$mthread" ] ||
[ -z "$othread" ] || \
175 [ "$mthread" -le 0 -o "$othread" -le 0 ]
176 # || [ `expr $mthread \* $mult \* 3` -gt `expr $othread \* 10` -o \
177 # `expr $mthread \* $mult \* 10` -lt `expr $othread \* 3` ]
180 echo ">>> profile on $exec1 (main vs thread) with multiplier $mult failed:"
181 echo "Actual times (in profiling units) were '$mthread' vs. '$othread'"
188 echo ">>> WARNING <<<"
189 echo "This test looks at timing information to determine correctness."
190 echo "If your system is loaded, the test may spuriously fail."
191 echo "If the test does fail with an 'Actual times' error, try running again."
194 # profiler1 is a non-threaded version
195 "$PROFILER1" 50 1 "$TMPDIR/p1" || RegisterFailure
196 "$PROFILER1" 100 1 "$TMPDIR/p2" || RegisterFailure
197 VerifySimilar p1
"$PROFILER1_REALNAME" p2
"$PROFILER1_REALNAME" 2
199 # Verify the same thing works if we statically link
200 "$PROFILER2" 50 1 "$TMPDIR/p3" || RegisterFailure
201 "$PROFILER2" 100 1 "$TMPDIR/p4" || RegisterFailure
202 VerifySimilar p3
"$PROFILER2_REALNAME" p4
"$PROFILER2_REALNAME" 2
204 # Verify the same thing works if we specify via CPUPROFILE
205 CPUPROFILE
="$TMPDIR/p5" "$PROFILER2" 50 || RegisterFailure
206 CPUPROFILE
="$TMPDIR/p6" "$PROFILER2" 100 || RegisterFailure
207 VerifySimilar p5
"$PROFILER2_REALNAME" p6
"$PROFILER2_REALNAME" 2
209 CPUPROFILE
="$TMPDIR/p5b" "$PROFILER3" 30 || RegisterFailure
210 CPUPROFILE
="$TMPDIR/p5c" "$PROFILER3" 60 || RegisterFailure
211 VerifySimilar p5b
"$PROFILER3_REALNAME" p5c
"$PROFILER3_REALNAME" 2
213 # Now try what happens when we use threads
214 "$PROFILER3" 30 2 "$TMPDIR/p7" || RegisterFailure
215 "$PROFILER3" 60 2 "$TMPDIR/p8" || RegisterFailure
216 VerifySimilar p7
"$PROFILER3_REALNAME" p8
"$PROFILER3_REALNAME" 2
218 "$PROFILER4" 30 2 "$TMPDIR/p9" || RegisterFailure
219 "$PROFILER4" 60 2 "$TMPDIR/p10" || RegisterFailure
220 VerifySimilar p9
"$PROFILER4_REALNAME" p10
"$PROFILER4_REALNAME" 2
223 "$PROFILER4" 25 3 "$TMPDIR/p9" || RegisterFailure
224 "$PROFILER4" 50 3 "$TMPDIR/p10" || RegisterFailure
225 VerifySimilar p9
"$PROFILER4_REALNAME" p10
"$PROFILER4_REALNAME" 2
227 # Compare how much time the main thread takes compared to the other threads
228 # Recall the main thread runs twice as long as the other threads, by design.
229 "$PROFILER4" 20 4 "$TMPDIR/p11" || RegisterFailure
230 VerifyAcrossThreads p11
"$PROFILER4_REALNAME" 2
232 # Test symbol save and restore
233 "$PROFILER1" 50 1 "$TMPDIR/p12" || RegisterFailure
234 "$PPROF" $PPROF_FLAGS "$PROFILER1_REALNAME" "$TMPDIR/p12" --raw \
235 >"$TMPDIR/p13" 2>/dev
/null || RegisterFailure
236 VerifyIdentical p12
"$PROFILER1_REALNAME" p13
"" || RegisterFailure
238 "$PROFILER3" 30 2 "$TMPDIR/p14" || RegisterFailure
239 "$PPROF" $PPROF_FLAGS "$PROFILER3_REALNAME" "$TMPDIR/p14" --raw \
240 >"$TMPDIR/p15" 2>/dev
/null || RegisterFailure
241 VerifyIdentical p14
"$PROFILER3_REALNAME" p15
"" || RegisterFailure
243 # Test using ITIMER_REAL instead of ITIMER_PROF.
244 env CPUPROFILE_REALTIME
=1 "$PROFILER3" 30 2 "$TMPDIR/p16" || RegisterFailure
245 env CPUPROFILE_REALTIME
=1 "$PROFILER3" 60 2 "$TMPDIR/p17" || RegisterFailure
246 VerifySimilar p16
"$PROFILER3_REALNAME" p17
"$PROFILER3_REALNAME" 2
249 # Make sure that when we have a process with a fork, the profiles don't
251 CPUPROFILE
="$TMPDIR/pfork" "$PROFILER1" 1 -2 || RegisterFailure
252 n
=`ls $TMPDIR/pfork* | wc -l`
254 echo "FORK test FAILED: expected 3 profiles (for main + 2 children), found $n"
255 num_failures
=`expr $num_failures + 1`
258 rm -rf "$TMPDIR" # clean up
260 echo "Tests finished with $num_failures failures"