]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man1/memusage.1
rename.2: SEE ALSO: add rename(1)
[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
9ba01802 24.TH MEMUSAGE 1 2019-03-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
a28b73cd 64.B memusage
26ae4cd9
MK
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).
2a86152e 122.PP
41deb515
MK
123The "realloc/total memory" of the table output by
124.B memusage
125does not reflect cases where
26ae4cd9
MK
126.BR realloc (3)
127is used to reallocate a block of memory
41deb515
MK
128to have a smaller size than previously.
129This can cause sum of all "total memory" cells (excluding "free")
130to be larger than the "free/total memory" cell.
26ae4cd9
MK
131.SS Histogram for block sizes
132The "Histogram for block sizes" provides a breakdown of memory
133allocations into various bucket sizes.
5018c197
PS
134.SH OPTIONS
135.TP
136.BI \-n\ name \fR,\ \fB\-\-progname= name
137Name of the program file to profile.
138.TP
139.BI \-p\ file \fR,\ \fB\-\-png= file
26ae4cd9
MK
140Generate PNG graphic and store it in
141.IR file .
5018c197
PS
142.TP
143.BI \-d\ file \fR,\ \fB\-\-data= file
26ae4cd9
MK
144Generate binary data file and store it in
145.IR file .
5018c197 146.TP
a28b73cd 147.B \-u\fR,\ \fB\-\-unbuffered
5018c197
PS
148Do not buffer output.
149.TP
150.BI \-b\ size \fR,\ \fB\-\-buffer= size
26ae4cd9
MK
151Collect
152.I size
153entries before writing them out.
5018c197 154.TP
a28b73cd 155.B \fB\-\-no-timer
26ae4cd9
MK
156Disable timer-based
157.RB ( SIGPROF )
158sampling of stack pointer value.
5018c197 159.TP
a28b73cd 160.B \-m\fR,\ \fB\-\-mmap
26ae4cd9
MK
161Also trace
162.BR mmap (2),
163.BR mremap (2),
164and
ff3e5500 165.BR munmap (2).
5018c197 166.TP
a28b73cd 167.B \-?\fR,\ \fB\-\-help
5018c197
PS
168Print help and exit.
169.TP
a28b73cd 170.B \-\-usage
5018c197
PS
171Print a short usage message and exit.
172.TP
a28b73cd 173.B \-V\fR,\ \fB\-\-version
5018c197
PS
174Print version information and exit.
175.TP
531fee2f 176The following options apply only when generating graphical output:
5018c197 177.TP
a28b73cd 178.B \-t\fR,\ \fB\-\-time\-based
26ae4cd9 179Use time (rather than number of function calls) as the scale for the X axis.
5018c197 180.TP
a28b73cd 181.B \-T\fR,\ \fB\-\-total
26ae4cd9 182Also draw a graph of total memory use.
5018c197
PS
183.TP
184.BI \fB\-\-title= name
26ae4cd9
MK
185Use
186.I name
187as the title of the graph.
5018c197
PS
188.TP
189.BI \-x\ size \fR,\ \fB\-\-x\-size= size
26ae4cd9
MK
190Make the graph
191.I size
192pixels wide.
5018c197
PS
193.TP
194.BI \-y\ size \fR,\ \fB\-\-y\-size= size
26ae4cd9
MK
195Make the graph
196.I size
197pixels high.
5018c197
PS
198.SH EXIT STATUS
199Exit status is equal to the exit status of profiled program.
1b3e9b9c
MK
200.SH BUGS
201To report bugs, see
202.UR http://www.gnu.org/software/libc/bugs.html
203.UE
26ae4cd9 204.SH EXAMPLE
3fd38c0c 205Below is a simple program that reallocates a block of
93aed984
MK
206memory in cycles that rise to a peak before then cyclically
207reallocating the memory in smaller blocks that return to zero.
208After compiling the program and running the following commands,
209a graph of the memory usage of the program can be found in the file
210.IR memusage.png :
2a86152e 211.PP
93aed984 212.in +4n
60d3774e 213.EX
d2fdb1e3 214$ \fBmemusage \-\-data=memusage.dat ./a.out\fP
93aed984
MK
215\&...
216Memory usage summary: heap total: 45200, heap peak: 6440, stack peak: 224
217 total calls total memory failed calls
218 malloc| 1 400 0
219realloc| 40 44800 0 (nomove:40, dec:19, free:0)
220 calloc| 0 0 0
221 free| 1 440
222Histogram for block sizes:
d2fdb1e3 223 192\-207 1 2% ================
93aed984 224\&...
d2fdb1e3
MK
225 2192\-2207 1 2% ================
226 2240\-2255 2 4% =================================
227 2832\-2847 2 4% =================================
228 3440\-3455 2 4% =================================
229 4032\-4047 2 4% =================================
230 4640\-4655 2 4% =================================
231 5232\-5247 2 4% =================================
232 5840\-5855 2 4% =================================
233 6432\-6447 1 2% ================
93aed984 234$ \fBmemusagestat memusage.dat memusage.png\fP
60d3774e 235.EE
93aed984
MK
236.in
237.SS Program source
e7d0bb47 238.EX
93aed984
MK
239#include <stdio.h>
240#include <stdlib.h>
241
242#define CYCLES 20
243
244int
245main(int argc, char *argv[])
246{
247 int i, j;
248 int *p;
249
d1a71985 250 printf("malloc: %zd\en", sizeof(int) * 100);
93aed984
MK
251 p = malloc(sizeof(int) * 100);
252
253 for (i = 0; i < CYCLES; i++) {
254 if (i < CYCLES / 2)
255 j = i;
256 else
257 j--;
258
d1a71985 259 printf("realloc: %zd\en", sizeof(int) * (j * 50 + 110));
93aed984
MK
260 p = realloc(p, sizeof(int) * (j * 50 + 100));
261
d1a71985 262 printf("realloc: %zd\en", sizeof(int) * ((j+1) * 150 + 110));
93aed984
MK
263 p = realloc(p, sizeof(int) * ((j + 1) * 150 + 110));
264 }
265
266 free(p);
267 exit(EXIT_SUCCESS);
268}
e7d0bb47 269.EE
5018c197
PS
270.SH SEE ALSO
271.BR memusagestat (1),
bba4bbbd 272.BR mtrace (1),
5018c197 273.BR ld.so (8)