OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man3 / malloc_info.3
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH MALLOC_INFO 3  2013-04-19 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 malloc_info \- export malloc state to a stream
28 .SH SYNOPSIS
29 .nf
30 .B #include <malloc.h>
31 .sp
32 .BI "int malloc_info(int " options ", FILE *" fp );
33 .fi
34 .SH DESCRIPTION
35 The
36 .BR malloc_info ()
37 function exports an XML string that describes the current state
38 of the memory-allocation
39 implementation in the caller.
40 The string is printed on the file stream
41 .IR fp .
42 The exported string includes information about all arenas (see
43 .BR malloc (3)).
44
45 As currently implemented,
46 .I options
47 must be zero.
48 .SH RETURN VALUE
49 On success,
50 .BR malloc_info ()
51 returns 0;
52 on error, it returns \-1, with
53 .I errno
54 set to indicate the cause.
55 .SH ERRORS
56 .TP
57 .B EINVAL
58 .I options
59 was nonzero.
60 .SH VERSIONS
61 .BR malloc_info ()
62 was added to glibc in version 2.10.
63 .SH CONFORMING TO
64 This function is a GNU extension.
65 .SH NOTES
66 The memory-allocation information is provided as an XML string
67 (rather than a C structure)
68 because the information may change over time
69 (according to changes in the underlying implementation).
70 The output XML string includes a version field.
71
72 The
73 .BR open_memstream (3)
74 function can be used to send the output of
75 .BR malloc_info ()
76 directly into a buffer in memory, rather than to a file.
77
78 The
79 .BR malloc_info ()
80 function is designed to address deficiencies in
81 .BR malloc_stats (3)
82 and
83 .BR mallinfo (3).
84 .SH EXAMPLE
85 The program below takes up to four command-line arguments,
86 of which the first three are mandatory.
87 The first argument specifies the number of threads that
88 the program should create.
89 All of the threads, including the main thread,
90 allocate the number of blocks of memory specified by the second argument.
91 The third argument controls the size of the blocks to be allocated.
92 The main thread creates blocks of this size,
93 the second thread created by the program allocates blocks of twice this size,
94 the third thread allocates blocks of three times this size, and so on.
95
96 The program calls
97 .BR malloc_info ()
98 twice to display the memory-allocation state.
99 The first call takes place before any threads
100 are created or memory allocated.
101 The second call is performed after all threads have allocated memory.
102
103 In the following example,
104 the command-line arguments specify the creation of one additional thread,
105 and both the main thread and the additional thread
106 allocate 10000 blocks of memory.
107 After the blocks of memory have been allocated,
108 .BR malloc_info ()
109 shows the state of two allocation arenas.
110 .in +4
111 .nf
112
113 .RB "$ " "getconf GNU_LIBC_VERSION"
114 glibc 2.13
115 .RB "$ " "./a.out 1 10000 100"
116 ============ Before allocating blocks ============
117 <malloc version="1">
118 <heap nr="0">
119 <sizes>
120 </sizes>
121 <total type="fast" count="0" size="0"/>
122 <total type="rest" count="0" size="0"/>
123 <system type="current" size="135168"/>
124 <system type="max" size="135168"/>
125 <aspace type="total" size="135168"/>
126 <aspace type="mprotect" size="135168"/>
127 </heap>
128 <total type="fast" count="0" size="0"/>
129 <total type="rest" count="0" size="0"/>
130 <system type="current" size="135168"/>
131 <system type="max" size="135168"/>
132 <aspace type="total" size="135168"/>
133 <aspace type="mprotect" size="135168"/>
134 </malloc>
135
136 ============ After allocating blocks ============
137 <malloc version="1">
138 <heap nr="0">
139 <sizes>
140 </sizes>
141 <total type="fast" count="0" size="0"/>
142 <total type="rest" count="0" size="0"/>
143 <system type="current" size="1081344"/>
144 <system type="max" size="1081344"/>
145 <aspace type="total" size="1081344"/>
146 <aspace type="mprotect" size="1081344"/>
147 </heap>
148 <heap nr="1">
149 <sizes>
150 </sizes>
151 <total type="fast" count="0" size="0"/>
152 <total type="rest" count="0" size="0"/>
153 <system type="current" size="1032192"/>
154 <system type="max" size="1032192"/>
155 <aspace type="total" size="1032192"/>
156 <aspace type="mprotect" size="1032192"/>
157 </heap>
158 <total type="fast" count="0" size="0"/>
159 <total type="rest" count="0" size="0"/>
160 <system type="current" size="2113536"/>
161 <system type="max" size="2113536"/>
162 <aspace type="total" size="2113536"/>
163 <aspace type="mprotect" size="2113536"/>
164 </malloc>
165 .fi
166 .in
167 .SS Program source
168 .nf
169
170 #include <unistd.h>
171 #include <stdlib.h>
172 #include <pthread.h>
173 #include <malloc.h>
174 #include <errno.h>
175
176 static size_t blockSize;
177 static int numThreads, numBlocks;
178
179 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
180                         } while (0)
181
182 static void *
183 thread_func(void *arg)
184 {
185     int j;
186     int tn = (int) arg;
187
188     /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
189        the main thread) allocates a different amount of memory */
190
191     for (j = 0; j < numBlocks; j++)
192         if (malloc(blockSize * (2 + tn)) == NULL)
193             errExit("malloc\-thread");
194
195     sleep(100);         /* Sleep until main thread terminates */
196     return NULL;
197 }
198
199 int
200 main(int argc, char *argv[])
201 {
202     int j, tn, sleepTime;
203     pthread_t *thr;
204
205     if (argc < 4) {
206         fprintf(stderr,
207                 "%s num\-threads num\-blocks block\-size [sleep\-time]\\n",
208                 argv[0]);
209         exit(EXIT_FAILURE);
210     }
211
212     numThreads = atoi(argv[1]);
213     numBlocks = atoi(argv[2]);
214     blockSize = atoi(argv[3]);
215     sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
216
217     thr = calloc(numThreads, sizeof(pthread_t));
218     if (thr == NULL)
219         errExit("calloc");
220
221     printf("============ Before allocating blocks ============\\n");
222     malloc_info(0, stdout);
223
224     /* Create threads that allocate different amounts of memory */
225
226     for (tn = 0; tn < numThreads; tn++) {
227         errno = pthread_create(&thr[tn], NULL, thread_func,
228                                (void *) tn);
229         if (errno != 0)
230             errExit("pthread_create");
231
232         /* If we add a sleep interval after the start\-up of each
233            thread, the threads likely won\(aqt contend for malloc
234            mutexes, and therefore additional arenas won\(aqt be
235            allocated (see malloc(3)). */
236
237         if (sleepTime > 0)
238             sleep(sleepTime);
239     }
240
241     /* The main thread also allocates some memory */
242
243     for (j = 0; j < numBlocks; j++)
244         if (malloc(blockSize) == NULL)
245             errExit("malloc");
246
247     sleep(2);           /* Give all threads a chance to
248                            complete allocations */
249
250     printf("\\n============ After allocating blocks ============\\n");
251     malloc_info(0, stdout);
252
253     exit(EXIT_SUCCESS);
254 }
255 .fi
256 .SH SEE ALSO
257 .BR mallinfo (3),
258 .BR malloc (3),
259 .BR malloc_stats (3),
260 .BR mallopt (3),
261 .BR open_memstream (3)
262 .SH COLOPHON
263 This page is part of release 3.68 of the Linux
264 .I man-pages
265 project.
266 A description of the project,
267 information about reporting bugs,
268 and the latest version of this page,
269 can be found at
270 \%http://www.kernel.org/doc/man\-pages/.