15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#!/bin/sh
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Copyright (c) 2008, Google Inc.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# All rights reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# modification, are permitted provided that the following conditions are
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# met:
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Redistributions of source code must retain the above copyright
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# notice, this list of conditions and the following disclaimer.
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Redistributions in binary form must reproduce the above
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# in the documentation and/or other materials provided with the
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# distribution.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# contributors may be used to endorse or promote products derived from
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# this software without specific prior written permission.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# ---
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Author: Craig Silverstein
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# This is a test that tcmalloc creates, and pprof reads, sampling data
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# correctly: both for the heap profile (ReadStackTraces) and for
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# growth in the heap sized (ReadGrowthStackTraces).
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BINDIR="${BINDIR:-.}"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PPROF_PATH="${PPROF_PATH:-$BINDIR/src/pprof}"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  echo "USAGE: $0 [unittest dir] [path to pprof]"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  echo "       By default, unittest_dir=$BINDIR, pprof_path=$PPROF_PATH"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit 1
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fi
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SAMPLING_TEST="${1:-$BINDIR/sampling_test}"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PPROF="${2:-$PPROF_PATH}"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)OUTDIR="/tmp/sampling_test_dir"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# libtool is annoying, and puts the actual executable in a different
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# directory, replacing the seeming-executable with a shell script.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# We use the error output of sampling_test to indicate its real location
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SAMPLING_TEST_BINARY=`"$SAMPLING_TEST" 2>&1 | awk '/USAGE/ {print $2; exit;}'`
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# A kludge for cygwin.  Unfortunately, 'test -f' says that 'foo' exists
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# even when it doesn't, and only foo.exe exists.  Other unix utilities
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# (like nm) need you to say 'foo.exe'.  We use one such utility, cat, to
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# see what the *real* binary name is.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if ! cat "$SAMPLING_TEST_BINARY" >/dev/null 2>&1; then
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SAMPLING_TEST_BINARY="$SAMPLING_TEST_BINARY".exe
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fi
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)die() {    # runs the command given as arguments, and then dies.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    echo "FAILED.  Output from $@"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    echo "----"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "$@"
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    echo "----"
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    exit 1
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)rm -rf "$OUTDIR" || die "Unable to delete $OUTDIR"
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)mkdir "$OUTDIR" || die "Unable to create $OUTDIR"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# This puts the output into out.heap and out.growth.  It allocates
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# 8*10^7 bytes of memory, which is 76M.  Because we sample, the
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# estimate may be a bit high or a bit low: we accept anything from
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# 50M to 99M.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"$SAMPLING_TEST" "$OUTDIR/out"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)echo "Testing heap output..."
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.heap" \
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   | grep '[5-9][0-9]\.[0-9][ 0-9.%]*_*AllocateAllocate' >/dev/null \
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   || die "$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.heap"
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)echo "OK"
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)echo "Testing growth output..."
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.growth" \
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   | grep '[5-9][0-9]\.[0-9][ 0-9.%]*_*AllocateAllocate' >/dev/null \
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   || die "$PPROF" --text "$SAMPLING_TEST_BINARY" "$OUTDIR/out.growth"
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)echo "OK"
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)echo "PASS"
95