Blame malloc/memusage.sh

Packit 6c4009
#! @BASH@
Packit 6c4009
# Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
# This file is part of the GNU C Library.
Packit 6c4009
# Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
Packit 6c4009
Packit 6c4009
# The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
# modify it under the terms of the GNU Lesser General Public
Packit 6c4009
# License as published by the Free Software Foundation; either
Packit 6c4009
# version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
# The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
# Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
# You should have received a copy of the GNU Lesser General Public
Packit 6c4009
# License along with the GNU C Library; if not, see
Packit 6c4009
# <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
memusageso='@SLIBDIR@/libmemusage.so'
Packit 6c4009
memusagestat='@BINDIR@/memusagestat'
Packit 6c4009
TEXTDOMAIN=libc
Packit 6c4009
Packit 6c4009
# Print usage message.
Packit 6c4009
do_usage() {
Packit 6c4009
  printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" memusage memusage
Packit 6c4009
  exit 1
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# Message for missing argument.
Packit 6c4009
do_missing_arg() {
Packit 6c4009
  printf >&2 $"%s: option '%s' requires an argument\n" memusage "$1"
Packit 6c4009
  do_usage
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# Print help message
Packit 6c4009
do_help() {
Packit 6c4009
  echo $"Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
Packit 6c4009
Profile memory usage of PROGRAM.
Packit 6c4009
Packit 6c4009
   -n,--progname=NAME     Name of the program file to profile
Packit 6c4009
   -p,--png=FILE          Generate PNG graphic and store it in FILE
Packit 6c4009
   -d,--data=FILE         Generate binary data file and store it in FILE
Packit 6c4009
   -u,--unbuffered        Don't buffer output
Packit 6c4009
   -b,--buffer=SIZE       Collect SIZE entries before writing them out
Packit 6c4009
      --no-timer          Don't collect additional information through timer
Packit 6c4009
   -m,--mmap              Also trace mmap & friends
Packit 6c4009
Packit 6c4009
   -?,--help              Print this help and exit
Packit 6c4009
      --usage             Give a short usage message
Packit 6c4009
   -V,--version           Print version information and exit
Packit 6c4009
Packit 6c4009
 The following options only apply when generating graphical output:
Packit 6c4009
   -t,--time-based        Make graph linear in time
Packit 6c4009
   -T,--total             Also draw graph of total memory use
Packit 6c4009
      --title=STRING      Use STRING as title of the graph
Packit 6c4009
   -x,--x-size=SIZE       Make graphic SIZE pixels wide
Packit 6c4009
   -y,--y-size=SIZE       Make graphic SIZE pixels high
Packit 6c4009
Packit 6c4009
Mandatory arguments to long options are also mandatory for any corresponding
Packit 6c4009
short options.
Packit 6c4009
Packit 6c4009
"
Packit 6c4009
  printf $"For bug reporting instructions, please see:\\n%s.\\n" \
Packit 6c4009
    "@REPORT_BUGS_TO@"
Packit 6c4009
  exit 0
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
do_version() {
Packit 6c4009
  echo 'memusage @PKGVERSION@@VERSION@'
Packit 6c4009
  printf $"Copyright (C) %s Free Software Foundation, Inc.
Packit 6c4009
This is free software; see the source for copying conditions.  There is NO
Packit 6c4009
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit 6c4009
" "2018"
Packit 6c4009
  printf $"Written by %s.
Packit 6c4009
" "Ulrich Drepper"
Packit 6c4009
  exit 0
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# These variables are local
Packit 6c4009
buffer=
Packit 6c4009
data=
Packit 6c4009
memusagestat_args=
Packit 6c4009
notimer=
Packit 6c4009
png=
Packit 6c4009
progname=
Packit 6c4009
tracemmap=
Packit 6c4009
Packit 6c4009
# Process arguments.  But stop as soon as the program name is found.
Packit 6c4009
while test $# -gt 0; do
Packit 6c4009
  case "$1" in
Packit 6c4009
  -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
Packit 6c4009
    do_version
Packit 6c4009
    ;;
Packit 6c4009
  -\? | --h | --he | --hel | --help)
Packit 6c4009
    do_help
Packit 6c4009
    ;;
Packit 6c4009
  --us | --usa | --usag | --usage)
Packit 6c4009
    echo $"Syntax: memusage [--data=FILE] [--progname=NAME] [--png=FILE] [--unbuffered]
Packit 6c4009
	    [--buffer=SIZE] [--no-timer] [--time-based] [--total]
Packit 6c4009
	    [--title=STRING] [--x-size=SIZE] [--y-size=SIZE]
Packit 6c4009
	    PROGRAM [PROGRAMOPTION]..."
Packit 6c4009
    exit 0
Packit 6c4009
    ;;
Packit 6c4009
  -n | --pr | --pro | --prog | --progn | --progna | --prognam | --progname)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    progname="$1"
Packit 6c4009
    ;;
Packit 6c4009
  --pr=* | --pro=* | --prog=* | --progn=* | --progna=* | --prognam=* | --progname=*)
Packit 6c4009
    progname=${1##*=}
Packit 6c4009
    ;;
Packit 6c4009
  -p | --pn | --png)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    png="$1"
Packit 6c4009
    ;;
Packit 6c4009
  --pn=* | --png=*)
Packit 6c4009
    png=${1##*=}
Packit 6c4009
    ;;
Packit 6c4009
  -d | --d | --da | --dat | --data)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    data="$1"
Packit 6c4009
    ;;
Packit 6c4009
  --d=* | --da=* | --dat=* | --data=*)
Packit 6c4009
    data=${1##*=}
Packit 6c4009
    ;;
Packit 6c4009
  -u | --un | --unb | --unbu | --unbuf | --unbuff | --unbuffe | --unbuffer | --unbuffere | --unbuffered)
Packit 6c4009
    buffer=1
Packit 6c4009
    ;;
Packit 6c4009
  -b | --b | --bu | --buf | --buff | --buffe | --buffer)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    buffer="$1"
Packit 6c4009
    ;;
Packit 6c4009
  --b=* | --bu=* | --buf=* | --buff=* | --buffe=* | --buffer=*)
Packit 6c4009
    buffer=${1##*=}
Packit 6c4009
    ;;
Packit 6c4009
  --n | --no | --no- | --no-t | --no-ti | --no-tim | --no-time | --no-timer)
Packit 6c4009
    notimer=yes
Packit 6c4009
    ;;
Packit 6c4009
  -m | --m | --mm | --mma | --mmap)
Packit 6c4009
    tracemmap=yes
Packit 6c4009
    ;;
Packit 6c4009
  -t | --tim | --time | --time- | --time-b | --time-ba | --time-bas | --time-base | --time-based)
Packit 6c4009
    memusagestat_args="$memusagestat_args -t"
Packit 6c4009
    ;;
Packit 6c4009
  -T | --to | --tot | --tota | --total)
Packit 6c4009
    memusagestat_args="$memusagestat_args -T"
Packit 6c4009
    ;;
Packit 6c4009
  --tit | --titl | --title)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    memusagestat_args="$memusagestat_args -s $1"
Packit 6c4009
    ;;
Packit 6c4009
  --tit=* | --titl=* | --title=*)
Packit 6c4009
    memusagestat_args="$memusagestat_args -s ${1##*=}"
Packit 6c4009
    ;;
Packit 6c4009
  -x | --x | --x- | --x-s | --x-si | --x-siz | --x-size)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    memusagestat_args="$memusagestat_args -x $1"
Packit 6c4009
    ;;
Packit 6c4009
  --x=* | --x-=* | --x-s=* | --x-si=* | --x-siz=* | --x-size=*)
Packit 6c4009
    memusagestat_args="$memusagestat_args -x ${1##*=}"
Packit 6c4009
    ;;
Packit 6c4009
  -y | --y | --y- | --y-s | --y-si | --y-siz | --y-size)
Packit 6c4009
    if test $# -eq 1; then
Packit 6c4009
      do_missing_arg $1
Packit 6c4009
    fi
Packit 6c4009
    shift
Packit 6c4009
    memusagestat_args="$memusagestat_args -y $1"
Packit 6c4009
    ;;
Packit 6c4009
  --y=* | --y-=* | --y-s=* | --y-si=* | --y-siz=* | --y-size=*)
Packit 6c4009
    memusagestat_args="$memusagestat_args -y ${1##*=}"
Packit 6c4009
    ;;
Packit 6c4009
  --p | --p=* | --t | --t=* | --ti | --ti=* | --u)
Packit 6c4009
    echo >&2 $"memusage: option \`${1##*=}' is ambiguous"
Packit 6c4009
    do_usage
Packit 6c4009
    ;;
Packit 6c4009
  --)
Packit 6c4009
    # Stop processing arguments.
Packit 6c4009
    shift
Packit 6c4009
    break
Packit 6c4009
    ;;
Packit 6c4009
  --*)
Packit 6c4009
    echo >&2 $"memusage: unrecognized option \`$1'"
Packit 6c4009
    do_usage
Packit 6c4009
    ;;
Packit 6c4009
  *)
Packit 6c4009
    # Unknown option.  This means the rest is the program name and parameters.
Packit 6c4009
    break
Packit 6c4009
    ;;
Packit 6c4009
  esac
Packit 6c4009
  shift
Packit 6c4009
done
Packit 6c4009
Packit 6c4009
# See whether any arguments are left.
Packit 6c4009
if test $# -eq 0; then
Packit 6c4009
  echo >&2 $"No program name given"
Packit 6c4009
  do_usage
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
# This will be in the environment.
Packit 6c4009
add_env="LD_PRELOAD=$memusageso"
Packit 6c4009
Packit 6c4009
# Generate data file name.
Packit 6c4009
datafile=
Packit 6c4009
if test -n "$data"; then
Packit 6c4009
  datafile="$data"
Packit 6c4009
elif test -n "$png"; then
Packit 6c4009
  datafile=$(mktemp -t memusage.XXXXXX) || exit
Packit 6c4009
  trap 'rm -f "$datafile"; exit 1' HUP INT QUIT TERM PIPE
Packit 6c4009
fi
Packit 6c4009
if test -n "$datafile"; then
Packit 6c4009
  add_env="$add_env MEMUSAGE_OUTPUT=$datafile"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
# Set program name.
Packit 6c4009
if test -n "$progname"; then
Packit 6c4009
  add_env="$add_env MEMUSAGE_PROG_NAME=$progname"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
# Set buffer size.
Packit 6c4009
if test -n "$buffer"; then
Packit 6c4009
  add_env="$add_env MEMUSAGE_BUFFER_SIZE=$buffer"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
# Disable timers.
Packit 6c4009
if test -n "$notimer"; then
Packit 6c4009
  add_env="$add_env MEMUSAGE_NO_TIMER=yes"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
# Trace mmap.
Packit 6c4009
if test -n "$tracemmap"; then
Packit 6c4009
  add_env="$add_env MEMUSAGE_TRACE_MMAP=yes"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
# Execute the program itself.
Packit 6c4009
eval $add_env '"$@"'
Packit 6c4009
result=$?
Packit 6c4009
Packit 6c4009
# Generate the PNG data file if wanted and there is something to generate
Packit 6c4009
# it from.
Packit 6c4009
if test -n "$png" -a -n "$datafile" -a -s "$datafile"; then
Packit 6c4009
  # Append extension .png if it isn't already there.
Packit 6c4009
  case $png in
Packit 6c4009
  *.png) ;;
Packit 6c4009
  *) png="$png.png" ;;
Packit 6c4009
  esac
Packit 6c4009
  $memusagestat $memusagestat_args "$datafile" "$png"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
if test -z "$data" -a -n "$datafile"; then
Packit 6c4009
  rm -f "$datafile"
Packit 6c4009
fi
Packit 6c4009
Packit 6c4009
exit $result
Packit 6c4009
# Local Variables:
Packit 6c4009
#  mode:ksh
Packit 6c4009
# End: