OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[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  2012-04-28 "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.
53 .SH ERRORS
54 .TP
55 .B EINVAL
56 .I options
57 was nonzero.
58 .SH VERSIONS
59 .BR malloc_info ()
60 was added to glibc in version 2.10.
61 .SH CONFORMING TO
62 This function is a GNU extension.
63 .SH NOTES
64 The memory-allocation information is provided as an XML string
65 (rather than a C structure)
66 because the information may change over time
67 (according to changes in the underlying implementation).
68 The output XML string includes a version field.
69
70 The
71 .BR open_memstream (3)
72 function can be used to send the output of
73 .BR malloc_info ()
74 directly into a buffer in memory, rather than to a file.
75
76 The
77 .BR malloc_info ()
78 function is designed to address deficiencies in
79 .BR malloc_stats (3)
80 and
81 .BR mallinfo (3).
82 .SH EXAMPLE
83 The program below takes up to four command-line arguments,
84 of which the first three are mandatory.
85 The first argument specifies the number of threads that
86 the program should create.
87 All of the threads, including the main thread,
88 allocate the number of blocks of memory specified by the second argument.
89 The third argument controls the size of the blocks to be allocated.
90 The main thread creates blocks of this size,
91 the second thread created by the program allocates blocks of twice this size,
92 the third thread allocates blocks of three times this size, and so on.
93
94 The program calls
95 .BR malloc_info ()
96 twice to display the memory-allocation state.
97 The first call takes place before any threads
98 are created or memory allocated.
99 The second call is performed after all threads have allocated memory.
100
101 In the following example,
102 the command-line arguments specify the creation of one additional thread,
103 and both the main thread and the additional thread
104 allocate 10000 blocks of memory.
105 After the blocks of memory have been allocated,
106 .BR malloc_info ()
107 shows the state of two allocation arenas.
108 .in +4
109 .nf
110
111 .RB "$ " "getconf GNU_LIBC_VERSION"
112 glibc 2.13
113 .RB "$ " "./a.out 1 10000 100"
114 ============ Before allocating blocks ============
115 <malloc version="1">
116 <heap nr="0">
117 <sizes>
118 </sizes>
119 <total type="fast" count="0" size="0"/>
120 <total type="rest" count="0" size="0"/>
121 <system type="current" size="135168"/>
122 <system type="max" size="135168"/>
123 <aspace type="total" size="135168"/>
124 <aspace type="mprotect" size="135168"/>
125 </heap>
126 <total type="fast" count="0" size="0"/>
127 <total type="rest" count="0" size="0"/>
128 <system type="current" size="135168"/>
129 <system type="max" size="135168"/>
130 <aspace type="total" size="135168"/>
131 <aspace type="mprotect" size="135168"/>
132 </malloc>
133
134 ============ After allocating blocks ============
135 <malloc version="1">
136 <heap nr="0">
137 <sizes>
138 </sizes>
139 <total type="fast" count="0" size="0"/>
140 <total type="rest" count="0" size="0"/>
141 <system type="current" size="1081344"/>
142 <system type="max" size="1081344"/>
143 <aspace type="total" size="1081344"/>
144 <aspace type="mprotect" size="1081344"/>
145 </heap>
146 <heap nr="1">
147 <sizes>
148 </sizes>
149 <total type="fast" count="0" size="0"/>
150 <total type="rest" count="0" size="0"/>
151 <system type="current" size="1032192"/>
152 <system type="max" size="1032192"/>
153 <aspace type="total" size="1032192"/>
154 <aspace type="mprotect" size="1032192"/>
155 </heap>
156 <total type="fast" count="0" size="0"/>
157 <total type="rest" count="0" size="0"/>
158 <system type="current" size="2113536"/>
159 <system type="max" size="2113536"/>
160 <aspace type="total" size="2113536"/>
161 <aspace type="mprotect" size="2113536"/>
162 </malloc>
163 .fi
164 .in
165 .SS Program source
166 .nf
167
168 #include <unistd.h>
169 #include <stdlib.h>
170 #include <pthread.h>
171 #include <malloc.h>
172 #include <errno.h>
173
174 static size_t blockSize;
175 static int numThreads, numBlocks;
176
177 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
178                         } while (0)
179
180 static void *
181 thread_func(void *arg)
182 {
183     int j;
184     int tn = (int) arg;
185
186     /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
187        the main thread) allocates a different amount of memory */
188
189     for (j = 0; j < numBlocks; j++)
190         if (malloc(blockSize * (2 + tn)) == NULL)
191             errExit("malloc\-thread");
192
193     sleep(100);         /* Sleep until main thread terminates */
194     return NULL;
195 }
196
197 int
198 main(int argc, char *argv[])
199 {
200     int j, tn, sleepTime;
201     pthread_t *thr;
202
203     if (argc < 4) {
204         fprintf(stderr,
205                 "%s num\-threads num\-blocks block\-size [sleep\-time]\\n",
206                 argv[0]);
207         exit(EXIT_FAILURE);
208     }
209
210     numThreads = atoi(argv[1]);
211     numBlocks = atoi(argv[2]);
212     blockSize = atoi(argv[3]);
213     sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
214
215     thr = calloc(numThreads, sizeof(pthread_t));
216     if (thr == NULL)
217         errExit("calloc");
218
219     printf("============ Before allocating blocks ============\\n");
220     malloc_info(0, stdout);
221
222     /* Create threads that allocate different amounts of memory */
223
224     for (tn = 0; tn < numThreads; tn++) {
225         errno = pthread_create(&thr[tn], NULL, thread_func,
226                                (void *) tn);
227         if (errno != 0)
228             errExit("pthread_create");
229
230         /* If we add a sleep interval after the start\-up of each
231            thread, the threads likely won\(aqt contend for malloc
232            mutexes, and therefore additional arenas won\(aqt be
233            allocated (see malloc(3)). */
234
235         if (sleepTime > 0)
236             sleep(sleepTime);
237     }
238
239     /* The main thread also allocates some memory */
240
241     for (j = 0; j < numBlocks; j++)
242         if (malloc(blockSize) == NULL)
243             errExit("malloc");
244
245     sleep(2);           /* Give all threads a chance to
246                            complete allocations */
247
248     printf("\\n============ After allocating blocks ============\\n");
249     malloc_info(0, stdout);
250
251     exit(EXIT_SUCCESS);
252 }
253 .fi
254 .SH SEE ALSO
255 .BR mallinfo (3),
256 .BR malloc (3),
257 .BR malloc_stats (3),
258 .BR mallopt (3),
259 .BR open_memstream (3)