Support cancellation in librt.
[glibc.git] / malloc / memusage.sh
blobd7b212465dbd89e86396c839596d8877dcb7b02e
1 #! @BASH@
2 # Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
4 # Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, write to the Free
18 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # 02111-1307 USA.
21 memusageso=@SLIBDIR@/libmemusage.so
22 memusagestat=@BINDIR@/memusagestat
24 # Print usage message.
25 do_usage() {
26 echo >&2 $"Try \`memusage --help' for more information."
27 exit 1
30 # Message for missing argument.
31 do_missing_arg() {
32 echo >&2 $"memusage: option \`$1' requires an argument"
33 do_usage
36 # Print help message
37 do_help() {
38 echo $"Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
39 Profile memory usage of PROGRAM.
41 -n,--progname=NAME Name of the program file to profile
42 -p,--png=FILE Generate PNG graphic and store it in FILE
43 -d,--data=FILE Generate binary data file and store it in FILE
44 -u,--unbuffered Don't buffer output
45 -b,--buffer=SIZE Collect SIZE entries before writing them out
46 --no-timer Don't collect additional information though timer
47 -m,--mmap Also trace mmap & friends
49 -?,--help Print this help and exit
50 --usage Give a short usage message
51 -V,--version Print version information and exit
53 The following options only apply when generating graphical output:
54 -t,--time-based Make graph linear in time
55 -T,--total Also draw graph of total memory use
56 --title=STRING Use STRING as title of the graph
57 -x,--x-size=SIZE Make graphic SIZE pixels wide
58 -y,--y-size=SIZE Make graphic SIZE pixels high
60 Mandatory arguments to long options are also mandatory for any corresponding
61 short options.
63 Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
64 exit 0
67 do_version() {
68 echo 'memusage (GNU libc) @VERSION@'
69 echo $"Copyright (C) 2003 Free Software Foundation, Inc.
70 This is free software; see the source for copying conditions. There is NO
71 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
72 Written by Ulrich Drepper."
73 exit 0
76 # Process arguments. But stop as soon as the program name is found.
77 while test $# -gt 0; do
78 case "$1" in
79 -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
80 do_version
82 -\? | --h | --he | --hel | --help)
83 do_help
85 --us | --usa | --usag | --usage)
86 echo $"Syntax: memusage [--data=FILE] [--progname=NAME] [--png=FILE] [--unbuffered]
87 [--buffer=SIZE] [--no-timer] [--time-based] [--total]
88 [--title=STRING] [--x-size=SIZE] [--y-size=SIZE]
89 PROGRAM [PROGRAMOPTION]..."
90 exit 0
92 -n | --pr | --pro | --prog | --progn | --progna | --prognam | --progname)
93 if test $# -eq 1; then
94 do_missing_arg $1
96 shift
97 progname="$1"
99 --pr=* | --pro=* | --prog=* | --progn=* | --progna=* | --prognam=* | --progname=*)
100 progname=${1##*=}
102 -p | --pn | --png)
103 if test $# -eq 1; then
104 do_missing_arg $1
106 shift
107 png="$1"
109 --pn=* | --png=*)
110 png=${1##*=}
112 -d | --d | --da | --dat | --data)
113 if test $# -eq 1; then
114 do_missing_arg $1
116 shift
117 data="$1"
119 --d=* | --da=* | --dat=* | --data=*)
120 data=${1##*=}
122 -u | --un | --unb | --unbu | --unbuf | --unbuff | --unbuffe | --unbuffer | --unbuffere | --unbuffered)
123 buffer=1
125 -b | --b | --bu | --buf | --buff | --buffe | --buffer)
126 if test $# -eq 1; then
127 do_missing_arg $1
129 shift
130 buffer="$1"
132 --b=* | --bu=* | --buf=* | --buff=* | --buffe=* | --buffer=*)
133 buffer=${1##*=}
135 --n | --no | --no- | --no-t | --no-ti | --no-tim | --no-time | --no-timer)
136 notimer=yes
138 -m | --m | --mm | --mma | --mmap)
139 tracemmap=yes
141 -t | --tim | --time | --time- | --time-b | --time-ba | --time-bas | --time-base | --time-based)
142 memusagestat_args="$memusagestat_args -t"
144 -T | --to | --tot | --tota | --total)
145 memusagestat_args="$memusagestat_args -T"
147 --tit | --titl | --title)
148 if test $# -eq 1; then
149 do_missing_arg $1
151 shift
152 memusagestat_args="$memusagestat_args -s $1"
154 --tit=* | --titl=* | --title=*)
155 memusagestat_args="$memusagestat_args -s ${1##*=}"
157 -x | --x | --x- | --x-s | --x-si | --x-siz | --x-size)
158 if test $# -eq 1; then
159 do_missing_arg $1
161 shift
162 memusagestat_args="$memusagestat_args -x $1"
164 --x=* | --x-=* | --x-s=* | --x-si=* | --x-siz=* | --x-size=*)
165 memusagestat_args="$memusagestat_args -x ${1##*=}"
167 -y | --y | --y- | --y-s | --y-si | --y-siz | --y-size)
168 if test $# -eq 1; then
169 do_missing_arg $1
171 shift
172 memusagestat_args="$memusagestat_args -y $1"
174 --y=* | --y-=* | --y-s=* | --y-si=* | --y-siz=* | --y-size=*)
175 memusagestat_args="$memusagestat_args -y ${1##*=}"
177 --p | --p=* | --t | --t=* | --ti | --ti=* | --u)
178 echo >&2 $"memusage: option \`${1##*=}' is ambiguous"
179 do_usage
182 # Stop processing arguments.
183 shift
184 break
186 --*)
187 echo >&2 $"memusage: unrecognized option \`$1'"
188 do_usage
191 # Unknown option. This means the rest is the program name and parameters.
192 break
194 esac
195 shift
196 done
198 # See whether any arguments are left.
199 if test $# -eq 0; then
200 echo >&2 $"No program name given"
201 do_usage
204 # This will be in the environment.
205 add_env="LD_PRELOAD=$memusageso"
207 # Generate data file name.
208 datafile=
209 if test -n "$data"; then
210 datafile="$data"
211 elif test -n "$png"; then
212 datafile=$(mktemp ${TMPDIR:-/tmp}/memusage.XXXXXX 2> /dev/null)
213 if test $? -ne 0; then
214 # Lame, but if there is no `mktemp' program the user cannot expect more.
215 if test "$RANDOM" != "$RANDOM"; then
216 datafile=${TMPDIR:-/tmp}/memusage.$RANDOM
217 else
218 datafile=${TMPDIR:-/tmp}/memusage.$$
222 if test -n "$datafile"; then
223 add_env="$add_env MEMUSAGE_OUTPUT=$datafile"
226 # Set program name.
227 if test -n "$progname"; then
228 add_env="$add_env MEMUSAGE_PROG_NAME=$progname"
231 # Set buffer size.
232 if test -n "$buffer"; then
233 add_env="$add_env MEMUSAGE_BUFFER_SIZE=$buffer"
236 # Disable timers.
237 if test -n "$notimer"; then
238 add_env="$add_env MEMUSAGE_NO_TIMER=yes"
241 # Trace mmap.
242 if test -n "$tracemmap"; then
243 add_env="$add_env MEMUSAGE_TRACE_MMAP=yes"
246 # Execute the program itself.
247 eval $add_env '"$@"'
248 result=$?
250 # Generate the PNG data file if wanted and there is something to generate
251 # it from.
252 if test -n "$png" -a -n "$datafile" -a -s "$datafile"; then
253 # Append extension .png if it isn't already there.
254 case $png in
255 *.png) ;;
256 *) png="$png.png" ;;
257 esac
258 $memusagestat $memusagestat_args "$datafile" "$png"
261 if test -z "$data" -a -n "$datafile"; then
262 rm -f "$datafile"
265 exit $result
266 # Local Variables:
267 # mode:ksh
268 # End: