OSDN Git Service

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