tools/trace-malloc/histogram-diff.sh

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/trace-malloc/histogram-diff.sh	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +#   histogram-diff.sh [-c <count>] <base> <incr>
    1.11 +#
    1.12 +# Compute incremental memory growth from histogram in file <base> to
    1.13 +# histogram in file <incr>, displaying at most <count> rows.
    1.14 +
    1.15 +# How many rows are we gonna show?
    1.16 +COUNT=20
    1.17 +
    1.18 +# Read arguments
    1.19 +while [ $# -gt 0 ]; do
    1.20 +    case "$1" in
    1.21 +    -c) COUNT=$2
    1.22 +        shift 2
    1.23 +        ;;
    1.24 +    *)  break
    1.25 +        ;;
    1.26 +    esac
    1.27 +done
    1.28 +
    1.29 +BASE=$1
    1.30 +INCR=$2
    1.31 +
    1.32 +# Sort the base and incremental files so that we can `join' them on
    1.33 +# the type name
    1.34 +sort $BASE > /tmp/$$.left
    1.35 +sort $INCR > /tmp/$$.right
    1.36 +
    1.37 +# Do the join. The `awk' script computes the difference between
    1.38 +# the base and the incremental files.
    1.39 +join /tmp/$$.left /tmp/$$.right \
    1.40 +    | awk '{ print $1, $2, $3, $4, $5, $4 - $2, $5 - $3; }' \
    1.41 +    > /tmp/$$.joined
    1.42 +
    1.43 +rm -f /tmp/$$.left /tmp/$$.right
    1.44 +
    1.45 +# Now compute a `TOTAL' row.
    1.46 +awk '{ tobj1 += $2; tbytes1 += $3; tobj2 += $4; tbytes2 += $5; tdobj += $6; tdbytes += $7; } END { print "TOTAL", tobj1, tbytes1, tobj2, tbytes2, tdobj, tdbytes; }' /tmp/$$.joined \
    1.47 +    > /tmp/$$.sorted
    1.48 +
    1.49 +# Then, we sort by the largest delta in bytes.
    1.50 +sort -nr +6 /tmp/$$.joined >> /tmp/$$.sorted
    1.51 +
    1.52 +rm -f /tmp/$$.joined
    1.53 +
    1.54 +# Pretty-print, including percentages
    1.55 +cat <<EOF > /tmp/$$.awk
    1.56 +BEGIN {
    1.57 +  print "                        ---- Base ----   ---- Incr ----   ----- Difference ----";
    1.58 +  print "Type                    Count    Bytes   Count    Bytes   Count    Bytes %Total";
    1.59 +  }
    1.60 +\$1 == "TOTAL" {
    1.61 +  tbytes = \$7;
    1.62 +  }
    1.63 +NR <= $COUNT {
    1.64 +  printf "%-22s %6d %8d  %6d %8d  %6d %8d %6.2lf\n", \$1, \$2, \$3, \$4, \$5, \$6, \$7, 100.0 * \$7 / tbytes;
    1.65 +  }
    1.66 +NR > $COUNT {
    1.67 +  oobjs1 += \$2;  obytes1 += \$3;
    1.68 +  oobjs2 += \$4;  obytes2 += \$5;
    1.69 +  odobjs += \$6;  odbytes += \$7;
    1.70 +  }
    1.71 +END {
    1.72 +  printf "%-22s %6d %8d  %6d %8d  %6d %8d %6.2lf\n", "OTHER", oobjs1, obytes1, oobjs2, obytes2, odobjs, odbytes, odbytes * 100.0 / tbytes;
    1.73 +  }
    1.74 +EOF
    1.75 +
    1.76 +# Now pretty print the file, and spit it out on stdout.
    1.77 +awk -f /tmp/$$.awk /tmp/$$.sorted
    1.78 +
    1.79 +rm -f /tmp/$$.awk /tmp/$$.sorted

mercurial