]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man1/memusage.1
ldd.1, execve.2, fanotify_init.2, fanotify_mark.2, getrlimit.2, open.2, readlink...
[thirdparty/man-pages.git] / man1 / memusage.1
CommitLineData
26ae4cd9 1.\" Copyright (c) 2013, Peter Schiffer <pschiffe@redhat.com>
93aed984 2.\" and Copyright (C) 2014, Michael Kerrisk <mtk.manpages@gmail.com>
5018c197
PS
3.\"
4.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5.\" This is free documentation; you can redistribute it and/or
6.\" modify it under the terms of the GNU General Public License as
7.\" published by the Free Software Foundation; either version 2 of
8.\" the License, or (at your option) any later version.
9.\"
10.\" The GNU General Public License's references to "object code"
11.\" and "executables" are to be interpreted as the output of any
12.\" document formatting or typesetting system, including
13.\" intermediate and printed output.
14.\"
15.\" This manual is distributed in the hope that it will be useful,
16.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18.\" GNU General Public License for more details.
19.\"
20.\" You should have received a copy of the GNU General Public
21.\" License along with this manual; if not, see
22.\" <http://www.gnu.org/licenses/>.
23.\" %%%LICENSE_END
62c76ace 24.TH MEMUSAGE 1 2014-09-06 "GNU" "Linux user manual"
5018c197
PS
25.SH NAME
26memusage \- profile memory usage of a program
27.SH SYNOPSIS
28.BR memusage " [\fIoption\fR]... \fIprogram\fR [\fIprogramoption\fR]..."
29.SH DESCRIPTION
30.B memusage
26ae4cd9
MK
31is a bash script which profiles memory usage of the program,
32.IR program .
33It preloads the
5018c197 34.B libmemusage.so
26ae4cd9 35library into the caller's environment (via the
5018c197 36.B LD_PRELOAD
26ae4cd9
MK
37environment variable; see
38.BR ld.so (8)).
39The
5018c197
PS
40.B libmemusage.so
41library traces memory allocation by intercepting calls to
42.BR malloc (3),
26ae4cd9 43.BR calloc (3),
5018c197 44.BR free (3),
26ae4cd9
MK
45and
46.BR realloc (3);
47optionally, calls to
48.BR mmap (2),
49.BR mremap (2),
50and
51.BR munmap (2)
52can also be intercepted.
5018c197
PS
53.PP
54.B memusage
26ae4cd9 55can output the collected data in textual form, or it can use
5018c197 56.BR memusagestat (1)
26ae4cd9 57(see the
5018c197 58.B -p
26ae4cd9 59option, below)
5018c197
PS
60to create a PNG file containing graphical representation
61of the collected data.
26ae4cd9
MK
62.SS Memory usage summary
63The "Memory usage summary" line output by
64.BR memusage
65contains three fields:
66.RS 4
67.TP
68\fBheap total\fR
69Sum of \fIsize\fR arguments of all
70.BR malloc (3)
71calls,
72products of arguments (\fInmemb\fR*\fIsize\fR) of all
73.BR calloc (3)
74calls,
75and sum of \fIlength\fR arguments of all
76.BR mmap (2)
77calls.
41deb515
MK
78In the case of
79.BR realloc (3)
80and
81.BR mremap (2),
82if the new size of an allocation is larger than the previous size,
83the sum of all such differences (new size minus old size) is added.
26ae4cd9
MK
84.TP
85.B "heap peak"
86Maximum of all \fIsize\fR arguments of
87.BR malloc (3),
88all products of \fInmemb\fR*\fIsize\fR of
89.BR calloc (3),
90all \fIsize\fR arguments of
91.BR realloc (3),
92.I length
93arguments of
94.BR mmap (2),
95and
96\fInew_size\fR arguments of
97.BR mremap (2).
98.TP
99.B "stack peak"
5018c197 100Before the first call to any monitored function,
26ae4cd9
MK
101the stack pointer address (base stack pointer) is saved.
102After each function call, the actual stack pointer address is read and
103the difference from the base stack pointer computed.
104The maximum of these differences is then the stack peak.
5018c197 105.RE
26ae4cd9 106.PP
41deb515 107Immediately following this summary line, a table shows the number calls,
3fd38c0c 108total memory allocated or deallocated,
41deb515 109and number of failed calls for each intercepted function.
983700ef
MK
110For
111.BR realloc (3)
112and
113.BR mremap (2),
114the additional field "nomove" shows reallocations that
115changed the address of a block,
116and the additional "dec" field shows reallocations that
117decreased the size of the block.
118For
119.BR realloc (3),
120the additional field "free" shows reallocations that
121caused a block to be freed (i.e., the reallocated size was 0).
122
41deb515
MK
123
124The "realloc/total memory" of the table output by
125.B memusage
126does not reflect cases where
26ae4cd9
MK
127.BR realloc (3)
128is used to reallocate a block of memory
41deb515
MK
129to have a smaller size than previously.
130This can cause sum of all "total memory" cells (excluding "free")
131to be larger than the "free/total memory" cell.
26ae4cd9
MK
132.SS Histogram for block sizes
133The "Histogram for block sizes" provides a breakdown of memory
134allocations into various bucket sizes.
5018c197
PS
135.SH OPTIONS
136.TP
137.BI \-n\ name \fR,\ \fB\-\-progname= name
138Name of the program file to profile.
139.TP
140.BI \-p\ file \fR,\ \fB\-\-png= file
26ae4cd9
MK
141Generate PNG graphic and store it in
142.IR file .
5018c197
PS
143.TP
144.BI \-d\ file \fR,\ \fB\-\-data= file
26ae4cd9
MK
145Generate binary data file and store it in
146.IR file .
5018c197
PS
147.TP
148.BI \-u\fR,\ \fB\-\-unbuffered
149Do not buffer output.
150.TP
151.BI \-b\ size \fR,\ \fB\-\-buffer= size
26ae4cd9
MK
152Collect
153.I size
154entries before writing them out.
5018c197
PS
155.TP
156.BI \fB\-\-no-timer
26ae4cd9
MK
157Disable timer-based
158.RB ( SIGPROF )
159sampling of stack pointer value.
5018c197
PS
160.TP
161.BI \-m\fR,\ \fB\-\-mmap
26ae4cd9
MK
162Also trace
163.BR mmap (2),
164.BR mremap (2),
165and
166.BR mmap (2).
5018c197
PS
167.TP
168.BI \-?\fR,\ \fB\-\-help
169Print help and exit.
170.TP
171.BI \fB\-\-usage
172Print a short usage message and exit.
173.TP
174.BI \-V\fR,\ \fB\-\-version
175Print version information and exit.
176.TP
177The following options only apply when generating graphical output:
178.TP
179.BI \-t\fR,\ \fB\-\-time\-based
26ae4cd9 180Use time (rather than number of function calls) as the scale for the X axis.
5018c197
PS
181.TP
182.BI \-T\fR,\ \fB\-\-total
26ae4cd9 183Also draw a graph of total memory use.
5018c197
PS
184.TP
185.BI \fB\-\-title= name
26ae4cd9
MK
186Use
187.I name
188as the title of the graph.
5018c197
PS
189.TP
190.BI \-x\ size \fR,\ \fB\-\-x\-size= size
26ae4cd9
MK
191Make the graph
192.I size
193pixels wide.
5018c197
PS
194.TP
195.BI \-y\ size \fR,\ \fB\-\-y\-size= size
26ae4cd9
MK
196Make the graph
197.I size
198pixels high.
5018c197
PS
199.SH EXIT STATUS
200Exit status is equal to the exit status of profiled program.
26ae4cd9 201.SH EXAMPLE
3fd38c0c 202Below is a simple program that reallocates a block of
93aed984
MK
203memory in cycles that rise to a peak before then cyclically
204reallocating the memory in smaller blocks that return to zero.
205After compiling the program and running the following commands,
206a graph of the memory usage of the program can be found in the file
207.IR memusage.png :
208
209.in +4n
210.nf
211$ \fBmemusage --data=memusage.dat ./a.out\fP
212\&...
213Memory usage summary: heap total: 45200, heap peak: 6440, stack peak: 224
214 total calls total memory failed calls
215 malloc| 1 400 0
216realloc| 40 44800 0 (nomove:40, dec:19, free:0)
217 calloc| 0 0 0
218 free| 1 440
219Histogram for block sizes:
220 192-207 1 2% ================
221\&...
222 2192-2207 1 2% ================
223 2240-2255 2 4% =================================
224 2832-2847 2 4% =================================
225 3440-3455 2 4% =================================
226 4032-4047 2 4% =================================
227 4640-4655 2 4% =================================
228 5232-5247 2 4% =================================
229 5840-5855 2 4% =================================
230 6432-6447 1 2% ================
231$ \fBmemusagestat memusage.dat memusage.png\fP
232.fi
233.in
234.SS Program source
235.nf
236#include <stdio.h>
237#include <stdlib.h>
238
239#define CYCLES 20
240
241int
242main(int argc, char *argv[])
243{
244 int i, j;
245 int *p;
246
247 printf("malloc: %zd\\n", sizeof(int) * 100);
248 p = malloc(sizeof(int) * 100);
249
250 for (i = 0; i < CYCLES; i++) {
251 if (i < CYCLES / 2)
252 j = i;
253 else
254 j--;
255
256 printf("realloc: %zd\\n", sizeof(int) * (j * 50 + 110));
257 p = realloc(p, sizeof(int) * (j * 50 + 100));
258
259 printf("realloc: %zd\\n", sizeof(int) * ((j+1) * 150 + 110));
260 p = realloc(p, sizeof(int) * ((j + 1) * 150 + 110));
261 }
262
263 free(p);
264 exit(EXIT_SUCCESS);
265}
266.fi
5018c197 267.SH BUGS
26ae4cd9
MK
268To report bugs, see
269.UR http://www.gnu.org/software/libc/bugs.html
270.UE
5018c197
PS
271.SH SEE ALSO
272.BR memusagestat (1),
26ae4cd9 273.BR mtrace (1)
5018c197 274.BR ld.so (8)