2 # Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
5 # The GNU C Library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # The GNU C Library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <https://www.gnu.org/licenses/>.
19 memusageso
='@SLIBDIR@/libmemusage.so'
20 memusagestat
='@BINDIR@/memusagestat'
23 # Print usage message.
25 printf >&2 $
"Try \`%s --help' or \`%s --usage' for more information.\n" memusage memusage
29 # Message for missing argument.
31 printf >&2 $
"%s: option '%s' requires an argument\n" memusage
"$1"
37 echo $
"Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
38 Profile memory usage of PROGRAM.
40 -n,--progname=NAME Name of the program file to profile
41 -p,--png=FILE Generate PNG graphic and store it in FILE
42 -d,--data=FILE Generate binary data file and store it in FILE
43 -u,--unbuffered Don't buffer output
44 -b,--buffer=SIZE Collect SIZE entries before writing them out
45 --no-timer Don't collect additional information through timer
46 -m,--mmap Also trace mmap & friends
48 -?,--help Print this help and exit
49 --usage Give a short usage message
50 -V,--version Print version information and exit
52 The following options only apply when generating graphical output:
53 -t,--time-based Make graph linear in time
54 -T,--total Also draw graph of total memory use
55 --title=STRING Use STRING as title of the graph
56 -x,--x-size=SIZE Make graphic SIZE pixels wide
57 -y,--y-size=SIZE Make graphic SIZE pixels high
59 Mandatory arguments to long options are also mandatory for any corresponding
63 printf $
"For bug reporting instructions, please see:\\n%s.\\n" \
69 echo 'memusage @PKGVERSION@@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.
74 printf $
"Written by %s.
79 # These variables are local
88 # Process arguments. But stop as soon as the program name is found.
89 while test $# -gt 0; do
91 -V |
--v |
--ve |
--ver |
--vers |
--versi |
--versio |
--version)
94 -\? |
--h |
--he |
--hel |
--help)
97 --us |
--usa |
--usag |
--usage)
98 echo $
"Syntax: memusage [--data=FILE] [--progname=NAME] [--png=FILE] [--unbuffered]
99 [--buffer=SIZE] [--no-timer] [--time-based] [--total]
100 [--title=STRING] [--x-size=SIZE] [--y-size=SIZE]
101 PROGRAM [PROGRAMOPTION]..."
104 -n |
--pr |
--pro |
--prog |
--progn |
--progna |
--prognam |
--progname)
105 if test $# -eq 1; then
111 --pr=* |
--pro=* |
--prog=* |
--progn=* |
--progna=* |
--prognam=* |
--progname=*)
115 if test $# -eq 1; then
124 -d |
--d |
--da |
--dat |
--data)
125 if test $# -eq 1; then
131 --d=* |
--da=* |
--dat=* |
--data=*)
134 -u |
--un |
--unb |
--unbu |
--unbuf |
--unbuff |
--unbuffe |
--unbuffer |
--unbuffere |
--unbuffered)
137 -b |
--b |
--bu |
--buf |
--buff |
--buffe |
--buffer)
138 if test $# -eq 1; then
144 --b=* |
--bu=* |
--buf=* |
--buff=* |
--buffe=* |
--buffer=*)
147 --n |
--no |
--no- |
--no-t |
--no-ti |
--no-tim |
--no-time |
--no-timer)
150 -m |
--m |
--mm |
--mma |
--mmap)
153 -t |
--tim |
--time |
--time- |
--time-b |
--time-ba |
--time-bas |
--time-base |
--time-based)
154 memusagestat_args
="$memusagestat_args -t"
156 -T |
--to |
--tot |
--tota |
--total)
157 memusagestat_args
="$memusagestat_args -T"
159 --tit |
--titl |
--title)
160 if test $# -eq 1; then
164 memusagestat_args
="$memusagestat_args -s $1"
166 --tit=* |
--titl=* |
--title=*)
167 memusagestat_args
="$memusagestat_args -s ${1##*=}"
169 -x |
--x |
--x- |
--x-s |
--x-si |
--x-siz |
--x-size)
170 if test $# -eq 1; then
174 memusagestat_args
="$memusagestat_args -x $1"
176 --x=* |
--x-=* |
--x-s=* |
--x-si=* |
--x-siz=* |
--x-size=*)
177 memusagestat_args
="$memusagestat_args -x ${1##*=}"
179 -y |
--y |
--y- |
--y-s |
--y-si |
--y-siz |
--y-size)
180 if test $# -eq 1; then
184 memusagestat_args
="$memusagestat_args -y $1"
186 --y=* |
--y-=* |
--y-s=* |
--y-si=* |
--y-siz=* |
--y-size=*)
187 memusagestat_args
="$memusagestat_args -y ${1##*=}"
189 --p |
--p=* |
--t |
--t=* |
--ti |
--ti=* |
--u)
190 echo >&2 $
"memusage: option \`${1##*=}' is ambiguous"
194 # Stop processing arguments.
199 echo >&2 $
"memusage: unrecognized option \`$1'"
203 # Unknown option. This means the rest is the program name and parameters.
210 # See whether any arguments are left.
211 if test $# -eq 0; then
212 echo >&2 $
"No program name given"
216 # This will be in the environment.
217 add_env
="LD_PRELOAD=$memusageso"
219 # Generate data file name.
221 if test -n "$data"; then
223 elif test -n "$png"; then
224 datafile
=$
(mktemp
-t memusage.XXXXXX
) ||
exit
225 trap 'rm -f "$datafile"; exit 1' HUP INT QUIT TERM PIPE
227 if test -n "$datafile"; then
228 add_env
="$add_env MEMUSAGE_OUTPUT=$datafile"
232 if test -n "$progname"; then
233 add_env
="$add_env MEMUSAGE_PROG_NAME=$progname"
237 if test -n "$buffer"; then
238 add_env
="$add_env MEMUSAGE_BUFFER_SIZE=$buffer"
242 if test -n "$notimer"; then
243 add_env
="$add_env MEMUSAGE_NO_TIMER=yes"
247 if test -n "$tracemmap"; then
248 add_env
="$add_env MEMUSAGE_TRACE_MMAP=yes"
251 # Execute the program itself.
255 # Generate the PNG data file if wanted and there is something to generate
257 if test -n "$png" -a -n "$datafile" -a -s "$datafile"; then
258 # Append extension .png if it isn't already there.
263 $memusagestat $memusagestat_args "$datafile" "$png"
266 if test -z "$data" -a -n "$datafile"; then