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