profiler_unittest.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/sh
  2. # Copyright (c) 2005, Google Inc.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. # ---
  31. # Author: Craig Silverstein
  32. #
  33. # Runs the 4 profiler unittests and makes sure their profiles look
  34. # appropriate. We expect two commandline args, as described below.
  35. #
  36. # We run under the assumption that if $PROFILER1 is run with no
  37. # arguments, it prints a usage line of the form
  38. # USAGE: <actual executable being run> [...]
  39. #
  40. # This is because libtool sometimes turns the 'executable' into a
  41. # shell script which runs an actual binary somewhere else.
  42. # We expect BINDIR and PPROF_PATH to be set in the environment.
  43. # If not, we set them to some reasonable values
  44. BINDIR="${BINDIR:-.}"
  45. PPROF_PATH="${PPROF_PATH:-$BINDIR/src/pprof}"
  46. if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
  47. echo "USAGE: $0 [unittest dir] [path to pprof]"
  48. echo " By default, unittest_dir=$BINDIR, pprof_path=$PPROF_PATH"
  49. exit 1
  50. fi
  51. TMPDIR=/tmp/profile_info
  52. UNITTEST_DIR=${1:-$BINDIR}
  53. PPROF=${2:-$PPROF_PATH}
  54. # We test the sliding-window functionality of the cpu-profile reader
  55. # by using a small stride, forcing lots of reads.
  56. PPROF_FLAGS="--test_stride=128"
  57. PROFILER1="$UNITTEST_DIR/profiler1_unittest"
  58. PROFILER2="$UNITTEST_DIR/profiler2_unittest"
  59. PROFILER3="$UNITTEST_DIR/profiler3_unittest"
  60. PROFILER4="$UNITTEST_DIR/profiler4_unittest"
  61. # Unfortunately, for us, libtool can replace executables with a shell
  62. # script that does some work before calling the 'real' executable
  63. # under a different name. We need the 'real' executable name to run
  64. # pprof on it. We've constructed all the binaries used in this
  65. # unittest so when they are called with no arguments, they report
  66. # their argv[0], which is the real binary name.
  67. Realname() {
  68. "$1" 2>&1 | awk '{print $2; exit;}'
  69. }
  70. PROFILER1_REALNAME=`Realname "$PROFILER1"`
  71. PROFILER2_REALNAME=`Realname "$PROFILER2"`
  72. PROFILER3_REALNAME=`Realname "$PROFILER3"`
  73. PROFILER4_REALNAME=`Realname "$PROFILER4"`
  74. # It's meaningful to the profiler, so make sure we know its state
  75. unset CPUPROFILE
  76. # Some output/logging in the profiler can cause issues when running the unit
  77. # tests. For example, logging a warning when the profiler is detected as being
  78. # present but no CPUPROFILE is specified in the environment. Especially when
  79. # we are checking for a silent run or specific timing constraints are being
  80. # checked. So set the env variable signifying that we are running in a unit
  81. # test environment.
  82. PERFTOOLS_UNITTEST=1
  83. rm -rf "$TMPDIR"
  84. mkdir "$TMPDIR" || exit 2
  85. num_failures=0
  86. RegisterFailure() {
  87. num_failures=`expr $num_failures + 1`
  88. }
  89. # Takes two filenames representing profiles, with their executable scripts,
  90. # and a multiplier, and verifies that the 'contentful' functions in each
  91. # profile take the same time (possibly scaled by the given multiplier). It
  92. # used to be "same" meant within 50%, after adding an noise-reducing X units
  93. # to each value. But even that would often spuriously fail, so now it's
  94. # "both non-zero". We're pretty forgiving.
  95. VerifySimilar() {
  96. prof1="$TMPDIR/$1"
  97. exec1="$2"
  98. prof2="$TMPDIR/$3"
  99. exec2="$4"
  100. mult="$5"
  101. # We are careful not to put exec1 and exec2 in quotes, because if
  102. # they are the empty string, it means we want to use the 1-arg
  103. # version of pprof.
  104. mthread1=`"$PPROF" $PPROF_FLAGS $exec1 "$prof1" | grep test_main_thread | awk '{print $1}'`
  105. mthread2=`"$PPROF" $PPROF_FLAGS $exec2 "$prof2" | grep test_main_thread | awk '{print $1}'`
  106. mthread1_plus=`expr $mthread1 + 5`
  107. mthread2_plus=`expr $mthread2 + 5`
  108. if [ -z "$mthread1" ] || [ -z "$mthread2" ] || \
  109. [ "$mthread1" -le 0 -o "$mthread2" -le 0 ]
  110. # || [ `expr $mthread1_plus \* $mult` -gt `expr $mthread2_plus \* 2` -o \
  111. # `expr $mthread1_plus \* $mult \* 2` -lt `expr $mthread2_plus` ]
  112. then
  113. echo
  114. echo ">>> profile on $exec1 vs $exec2 with multiplier $mult failed:"
  115. echo "Actual times (in profiling units) were '$mthread1' vs. '$mthread2'"
  116. echo
  117. RegisterFailure
  118. fi
  119. }
  120. # Takes two filenames representing profiles, and optionally their
  121. # executable scripts (these may be empty if the profiles include
  122. # symbols), and verifies that the two profiles are identical.
  123. VerifyIdentical() {
  124. prof1="$TMPDIR/$1"
  125. exec1="$2"
  126. prof2="$TMPDIR/$3"
  127. exec2="$4"
  128. # We are careful not to put exec1 and exec2 in quotes, because if
  129. # they are the empty string, it means we want to use the 1-arg
  130. # version of pprof.
  131. "$PPROF" $PPROF_FLAGS $exec1 "$prof1" > "$TMPDIR/out1"
  132. "$PPROF" $PPROF_FLAGS $exec2 "$prof2" > "$TMPDIR/out2"
  133. diff=`diff "$TMPDIR/out1" "$TMPDIR/out2"`
  134. if [ ! -z "$diff" ]; then
  135. echo
  136. echo ">>> profile doesn't match, args: $exec1 $prof1 vs. $exec2 $prof2"
  137. echo ">>> Diff:"
  138. echo "$diff"
  139. echo
  140. RegisterFailure
  141. fi
  142. }
  143. # Takes a filename representing a profile, with its executable,
  144. # and a multiplier, and verifies that the main-thread function takes
  145. # the same amount of time as the other-threads function (possibly scaled
  146. # by the given multiplier). Figuring out the multiplier can be tricky,
  147. # since by design the main thread runs twice as long as each of the
  148. # 'other' threads! It used to be "same" meant within 50%, after adding an
  149. # noise-reducing X units to each value. But even that would often
  150. # spuriously fail, so now it's "both non-zero". We're pretty forgiving.
  151. VerifyAcrossThreads() {
  152. prof1="$TMPDIR/$1"
  153. # We need to run the script with no args to get the actual exe name
  154. exec1="$2"
  155. mult="$3"
  156. # We are careful not to put exec1 in quotes, because if it is the
  157. # empty string, it means we want to use the 1-arg version of pprof.
  158. mthread=`$PPROF $PPROF_FLAGS $exec1 "$prof1" | grep test_main_thread | awk '{print $1}'`
  159. othread=`$PPROF $PPROF_FLAGS $exec1 "$prof1" | grep test_other_thread | awk '{print $1}'`
  160. if [ -z "$mthread" ] || [ -z "$othread" ] || \
  161. [ "$mthread" -le 0 -o "$othread" -le 0 ]
  162. # || [ `expr $mthread \* $mult \* 3` -gt `expr $othread \* 10` -o \
  163. # `expr $mthread \* $mult \* 10` -lt `expr $othread \* 3` ]
  164. then
  165. echo
  166. echo ">>> profile on $exec1 (main vs thread) with multiplier $mult failed:"
  167. echo "Actual times (in profiling units) were '$mthread' vs. '$othread'"
  168. echo
  169. RegisterFailure
  170. fi
  171. }
  172. echo
  173. echo ">>> WARNING <<<"
  174. echo "This test looks at timing information to determine correctness."
  175. echo "If your system is loaded, the test may spuriously fail."
  176. echo "If the test does fail with an 'Actual times' error, try running again."
  177. echo
  178. # profiler1 is a non-threaded version
  179. "$PROFILER1" 50 1 "$TMPDIR/p1" || RegisterFailure
  180. "$PROFILER1" 100 1 "$TMPDIR/p2" || RegisterFailure
  181. VerifySimilar p1 "$PROFILER1_REALNAME" p2 "$PROFILER1_REALNAME" 2
  182. # Verify the same thing works if we statically link
  183. "$PROFILER2" 50 1 "$TMPDIR/p3" || RegisterFailure
  184. "$PROFILER2" 100 1 "$TMPDIR/p4" || RegisterFailure
  185. VerifySimilar p3 "$PROFILER2_REALNAME" p4 "$PROFILER2_REALNAME" 2
  186. # Verify the same thing works if we specify via CPUPROFILE
  187. CPUPROFILE="$TMPDIR/p5" "$PROFILER2" 50 || RegisterFailure
  188. CPUPROFILE="$TMPDIR/p6" "$PROFILER2" 100 || RegisterFailure
  189. VerifySimilar p5 "$PROFILER2_REALNAME" p6 "$PROFILER2_REALNAME" 2
  190. CPUPROFILE="$TMPDIR/p5b" "$PROFILER3" 30 || RegisterFailure
  191. CPUPROFILE="$TMPDIR/p5c" "$PROFILER3" 60 || RegisterFailure
  192. VerifySimilar p5b "$PROFILER3_REALNAME" p5c "$PROFILER3_REALNAME" 2
  193. # Now try what happens when we use threads
  194. "$PROFILER3" 30 2 "$TMPDIR/p7" || RegisterFailure
  195. "$PROFILER3" 60 2 "$TMPDIR/p8" || RegisterFailure
  196. VerifySimilar p7 "$PROFILER3_REALNAME" p8 "$PROFILER3_REALNAME" 2
  197. "$PROFILER4" 30 2 "$TMPDIR/p9" || RegisterFailure
  198. "$PROFILER4" 60 2 "$TMPDIR/p10" || RegisterFailure
  199. VerifySimilar p9 "$PROFILER4_REALNAME" p10 "$PROFILER4_REALNAME" 2
  200. # More threads!
  201. "$PROFILER4" 25 3 "$TMPDIR/p9" || RegisterFailure
  202. "$PROFILER4" 50 3 "$TMPDIR/p10" || RegisterFailure
  203. VerifySimilar p9 "$PROFILER4_REALNAME" p10 "$PROFILER4_REALNAME" 2
  204. # Compare how much time the main thread takes compared to the other threads
  205. # Recall the main thread runs twice as long as the other threads, by design.
  206. "$PROFILER4" 20 4 "$TMPDIR/p11" || RegisterFailure
  207. VerifyAcrossThreads p11 "$PROFILER4_REALNAME" 2
  208. # Test symbol save and restore
  209. "$PROFILER1" 50 1 "$TMPDIR/p12" || RegisterFailure
  210. "$PPROF" $PPROF_FLAGS "$PROFILER1_REALNAME" "$TMPDIR/p12" --raw \
  211. >"$TMPDIR/p13" 2>/dev/null || RegisterFailure
  212. VerifyIdentical p12 "$PROFILER1_REALNAME" p13 "" || RegisterFailure
  213. "$PROFILER3" 30 2 "$TMPDIR/p14" || RegisterFailure
  214. "$PPROF" $PPROF_FLAGS "$PROFILER3_REALNAME" "$TMPDIR/p14" --raw \
  215. >"$TMPDIR/p15" 2>/dev/null || RegisterFailure
  216. VerifyIdentical p14 "$PROFILER3_REALNAME" p15 "" || RegisterFailure
  217. # Test using ITIMER_REAL instead of ITIMER_PROF.
  218. env CPUPROFILE_REALTIME=1 "$PROFILER3" 30 2 "$TMPDIR/p16" || RegisterFailure
  219. env CPUPROFILE_REALTIME=1 "$PROFILER3" 60 2 "$TMPDIR/p17" || RegisterFailure
  220. VerifySimilar p16 "$PROFILER3_REALNAME" p17 "$PROFILER3_REALNAME" 2
  221. # Make sure that when we have a process with a fork, the profiles don't
  222. # clobber each other
  223. CPUPROFILE="$TMPDIR/pfork" "$PROFILER1" 1 -2 || RegisterFailure
  224. n=`ls $TMPDIR/pfork* | wc -l`
  225. if [ $n != 3 ]; then
  226. echo "FORK test FAILED: expected 3 profiles (for main + 2 children), found $n"
  227. num_failures=`expr $num_failures + 1`
  228. fi
  229. rm -rf "$TMPDIR" # clean up
  230. echo "Tests finished with $num_failures failures"
  231. exit $num_failures