b1e405b953f49a68caa0933908590349dd234c8c
[platform/upstream/linaro-glibc.git] / malloc / memusage.sh
1 #! @BASH@
2 # Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
4 # Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
5
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.
10
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.
15
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.
20
21 memusageso=@SLIBDIR@/libmemusage.so
22 memusagestat=@BINDIR@/memusagestat
23 TEXTDOMAIN=libc
24
25 # Print usage message.
26 do_usage() {
27   echo >&2 $"Try \`memusage --help' for more information."
28   exit 1
29 }
30
31 # Message for missing argument.
32 do_missing_arg() {
33   echo >&2 $"memusage: option \`$1' requires an argument"
34   do_usage
35 }
36
37 # Print help message
38 do_help() {
39   echo $"Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
40 Profile memory usage of PROGRAM.
41
42    -n,--progname=NAME     Name of the program file to profile
43    -p,--png=FILE          Generate PNG graphic and store it in FILE
44    -d,--data=FILE         Generate binary data file and store it in FILE
45    -u,--unbuffered        Don't buffer output
46    -b,--buffer=SIZE       Collect SIZE entries before writing them out
47       --no-timer          Don't collect additional information though timer
48    -m,--mmap              Also trace mmap & friends
49
50    -?,--help              Print this help and exit
51       --usage             Give a short usage message
52    -V,--version           Print version information and exit
53
54  The following options only apply when generating graphical output:
55    -t,--time-based        Make graph linear in time
56    -T,--total             Also draw graph of total memory use
57       --title=STRING      Use STRING as title of the graph
58    -x,--x-size=SIZE       Make graphic SIZE pixels wide
59    -y,--y-size=SIZE       Make graphic SIZE pixels high
60
61 Mandatory arguments to long options are also mandatory for any corresponding
62 short options.
63
64 Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
65   exit 0
66 }
67
68 do_version() {
69   echo 'memusage (GNU libc) @VERSION@'
70   printf $"Copyright (C) %s Free Software Foundation, Inc.
71 This is free software; see the source for copying conditions.  There is NO
72 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
73 " "2004"
74   printf $"Written by %s.
75 " "Ulrich Drepper"
76   exit 0
77 }
78
79 # Process arguments.  But stop as soon as the program name is found.
80 while test $# -gt 0; do
81   case "$1" in
82   -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
83     do_version
84     ;;
85   -\? | --h | --he | --hel | --help)
86     do_help
87     ;;
88   --us | --usa | --usag | --usage)
89     echo $"Syntax: memusage [--data=FILE] [--progname=NAME] [--png=FILE] [--unbuffered]
90             [--buffer=SIZE] [--no-timer] [--time-based] [--total]
91             [--title=STRING] [--x-size=SIZE] [--y-size=SIZE]
92             PROGRAM [PROGRAMOPTION]..."
93     exit 0
94     ;;
95   -n | --pr | --pro | --prog | --progn | --progna | --prognam | --progname)
96     if test $# -eq 1; then
97       do_missing_arg $1
98     fi
99     shift
100     progname="$1"
101     ;;
102   --pr=* | --pro=* | --prog=* | --progn=* | --progna=* | --prognam=* | --progname=*)
103     progname=${1##*=}
104     ;;
105   -p | --pn | --png)
106     if test $# -eq 1; then
107       do_missing_arg $1
108     fi
109     shift
110     png="$1"
111     ;;
112   --pn=* | --png=*)
113     png=${1##*=}
114     ;;
115   -d | --d | --da | --dat | --data)
116     if test $# -eq 1; then
117       do_missing_arg $1
118     fi
119     shift
120     data="$1"
121     ;;
122   --d=* | --da=* | --dat=* | --data=*)
123     data=${1##*=}
124     ;;
125   -u | --un | --unb | --unbu | --unbuf | --unbuff | --unbuffe | --unbuffer | --unbuffere | --unbuffered)
126     buffer=1
127     ;;
128   -b | --b | --bu | --buf | --buff | --buffe | --buffer)
129     if test $# -eq 1; then
130       do_missing_arg $1
131     fi
132     shift
133     buffer="$1"
134     ;;
135   --b=* | --bu=* | --buf=* | --buff=* | --buffe=* | --buffer=*)
136     buffer=${1##*=}
137     ;;
138   --n | --no | --no- | --no-t | --no-ti | --no-tim | --no-time | --no-timer)
139     notimer=yes
140     ;;
141   -m | --m | --mm | --mma | --mmap)
142     tracemmap=yes
143     ;;
144   -t | --tim | --time | --time- | --time-b | --time-ba | --time-bas | --time-base | --time-based)
145     memusagestat_args="$memusagestat_args -t"
146     ;;
147   -T | --to | --tot | --tota | --total)
148     memusagestat_args="$memusagestat_args -T"
149     ;;
150   --tit | --titl | --title)
151     if test $# -eq 1; then
152       do_missing_arg $1
153     fi
154     shift
155     memusagestat_args="$memusagestat_args -s $1"
156     ;;
157   --tit=* | --titl=* | --title=*)
158     memusagestat_args="$memusagestat_args -s ${1##*=}"
159     ;;
160   -x | --x | --x- | --x-s | --x-si | --x-siz | --x-size)
161     if test $# -eq 1; then
162       do_missing_arg $1
163     fi
164     shift
165     memusagestat_args="$memusagestat_args -x $1"
166     ;;
167   --x=* | --x-=* | --x-s=* | --x-si=* | --x-siz=* | --x-size=*)
168     memusagestat_args="$memusagestat_args -x ${1##*=}"
169     ;;
170   -y | --y | --y- | --y-s | --y-si | --y-siz | --y-size)
171     if test $# -eq 1; then
172       do_missing_arg $1
173     fi
174     shift
175     memusagestat_args="$memusagestat_args -y $1"
176     ;;
177   --y=* | --y-=* | --y-s=* | --y-si=* | --y-siz=* | --y-size=*)
178     memusagestat_args="$memusagestat_args -y ${1##*=}"
179     ;;
180   --p | --p=* | --t | --t=* | --ti | --ti=* | --u)
181     echo >&2 $"memusage: option \`${1##*=}' is ambiguous"
182     do_usage
183     ;;
184   --)
185     # Stop processing arguments.
186     shift
187     break
188     ;;
189   --*)
190     echo >&2 $"memusage: unrecognized option \`$1'"
191     do_usage
192     ;;
193   *)
194     # Unknown option.  This means the rest is the program name and parameters.
195     break
196     ;;
197   esac
198   shift
199 done
200
201 # See whether any arguments are left.
202 if test $# -eq 0; then
203   echo >&2 $"No program name given"
204   do_usage
205 fi
206
207 # This will be in the environment.
208 add_env="LD_PRELOAD=$memusageso"
209
210 # Generate data file name.
211 datafile=
212 if test -n "$data"; then
213   datafile="$data"
214 elif test -n "$png"; then
215   datafile=$(mktemp ${TMPDIR:-/tmp}/memusage.XXXXXX 2> /dev/null)
216   if test $? -ne 0; then
217     # Lame, but if there is no `mktemp' program the user cannot expect more.
218     if test "$RANDOM" != "$RANDOM"; then
219       datafile=${TMPDIR:-/tmp}/memusage.$RANDOM
220     else
221       datafile=${TMPDIR:-/tmp}/memusage.$$
222     fi
223   fi
224 fi
225 if test -n "$datafile"; then
226   add_env="$add_env MEMUSAGE_OUTPUT=$datafile"
227 fi
228
229 # Set program name.
230 if test -n "$progname"; then
231   add_env="$add_env MEMUSAGE_PROG_NAME=$progname"
232 fi
233
234 # Set buffer size.
235 if test -n "$buffer"; then
236   add_env="$add_env MEMUSAGE_BUFFER_SIZE=$buffer"
237 fi
238
239 # Disable timers.
240 if test -n "$notimer"; then
241   add_env="$add_env MEMUSAGE_NO_TIMER=yes"
242 fi
243
244 # Trace mmap.
245 if test -n "$tracemmap"; then
246   add_env="$add_env MEMUSAGE_TRACE_MMAP=yes"
247 fi
248
249 # Execute the program itself.
250 eval $add_env '"$@"'
251 result=$?
252
253 # Generate the PNG data file if wanted and there is something to generate
254 # it from.
255 if test -n "$png" -a -n "$datafile" -a -s "$datafile"; then
256   # Append extension .png if it isn't already there.
257   case $png in
258   *.png) ;;
259   *) png="$png.png" ;;
260   esac
261   $memusagestat $memusagestat_args "$datafile" "$png"
262 fi
263
264 if test -z "$data" -a -n "$datafile"; then
265   rm -f "$datafile"
266 fi
267
268 exit $result
269 # Local Variables:
270 #  mode:ksh
271 # End: