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