sampling_test.sh 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/sh
  2. # Copyright (c) 2008, 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. # ---
  32. # Author: Craig Silverstein
  33. #
  34. # This is a test that tcmalloc creates, and pprof reads, sampling data
  35. # correctly: both for the heap profile (ReadStackTraces) and for
  36. # growth in the heap sized (ReadGrowthStackTraces).
  37. BINDIR="${BINDIR:-.}"
  38. PPROF_PATH="${PPROF_PATH:-$BINDIR/src/pprof}"
  39. if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
  40. echo "USAGE: $0 [unittest dir] [path to pprof]"
  41. echo " By default, unittest_dir=$BINDIR, pprof_path=$PPROF_PATH"
  42. exit 1
  43. fi
  44. SAMPLING_TEST="${1:-$BINDIR/sampling_test}"
  45. PPROF="${2:-$PPROF_PATH}"
  46. OUTDIR="/tmp/sampling_test_dir"
  47. # libtool is annoying, and puts the actual executable in a different
  48. # directory, replacing the seeming-executable with a shell script.
  49. # We use the error output of sampling_test to indicate its real location
  50. SAMPLING_TEST_BINARY=`"$SAMPLING_TEST" 2>&1 | awk '/USAGE/ {print $2; exit;}'`
  51. # A kludge for cygwin. Unfortunately, 'test -f' says that 'foo' exists
  52. # even when it doesn't, and only foo.exe exists. Other unix utilities
  53. # (like nm) need you to say 'foo.exe'. We use one such utility, cat, to
  54. # see what the *real* binary name is.
  55. if ! cat "$SAMPLING_TEST_BINARY" >/dev/null 2>&1; then
  56. SAMPLING_TEST_BINARY="$SAMPLING_TEST_BINARY".exe
  57. fi
  58. die() { # runs the command given as arguments, and then dies.
  59. echo "FAILED. Output from $@"
  60. echo "----"
  61. "$@"
  62. echo "----"
  63. exit 1
  64. }
  65. rm -rf "$OUTDIR" || die "Unable to delete $OUTDIR"
  66. mkdir "$OUTDIR" || die "Unable to create $OUTDIR"
  67. # This puts the output into out.heap and out.growth. It allocates
  68. # 8*10^7 bytes of memory, which is 76M. Because we sample, the
  69. # estimate may be a bit high or a bit low: we accept anything from
  70. # 50M to 99M.
  71. "$SAMPLING_TEST" "$OUTDIR/out"
  72. echo "Testing heap output..."
  73. "$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.heap" \
  74. | grep '[5-9][0-9]\.[0-9][ 0-9.%]*_*AllocateAllocate' >/dev/null \
  75. || die "$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.heap"
  76. echo "OK"
  77. echo "Testing growth output..."
  78. "$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.growth" \
  79. | grep '[5-9][0-9]\.[0-9][ 0-9.%]*_*AllocateAllocate' >/dev/null \
  80. || die "$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.growth"
  81. echo "OK"
  82. echo "PASS"