1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH MALLOC_INFO 3 2012-04-28 "GNU" "Linux Programmer's Manual"
25 malloc_info \- export malloc state to a stream
28 .B #include <malloc.h>
30 .BI "int malloc_info(int " options ", FILE *" fp );
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
40 The exported string includes information about all arenas (see
43 As currently implemented,
50 on error, it returns \-1.
58 was added to glibc in version 2.10.
60 This function is a GNU extension.
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.
69 .BR open_memstream (3)
70 function can be used to send the output of
72 directly into a buffer in memory, rather than to a file.
76 function is designed to address deficiencies in
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.
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.
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,
105 shows the state of two allocation arenas.
109 .RB "$ " "getconf GNU_LIBC_VERSION"
111 .RB "$ " "./a.out 1 10000 100"
112 ============ Before allocating blocks ============
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"/>
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"/>
132 ============ After allocating blocks ============
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"/>
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"/>
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"/>
172 static size_t blockSize;
173 static int numThreads, numBlocks;
175 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
179 thread_func(void *arg)
184 /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
185 the main thread) allocates a different amount of memory */
187 for (j = 0; j < numBlocks; j++)
188 if (malloc(blockSize * (2 + tn)) == NULL)
189 errExit("malloc\-thread");
191 sleep(100); /* Sleep until main thread terminates */
196 main(int argc, char *argv[])
198 int j, tn, sleepTime;
203 "%s num\-threads num\-blocks block\-size [sleep\-time]\\n",
208 numThreads = atoi(argv[1]);
209 numBlocks = atoi(argv[2]);
210 blockSize = atoi(argv[3]);
211 sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
213 thr = calloc(numThreads, sizeof(pthread_t));
217 printf("============ Before allocating blocks ============\\n");
218 malloc_info(0, stdout);
220 /* Create threads that allocate different amounts of memory */
222 for (tn = 0; tn < numThreads; tn++) {
223 errno = pthread_create(&thr[tn], NULL, thread_func,
226 errExit("pthread_create");
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)). */
237 /* The main thread also allocates some memory */
239 for (j = 0; j < numBlocks; j++)
240 if (malloc(blockSize) == NULL)
243 sleep(2); /* Give all threads a chance to
244 complete allocations */
246 printf("\\n============ After allocating blocks ============\\n");
247 malloc_info(0, stdout);
255 .BR malloc_stats (3),
257 .BR open_memstream (3)