2 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH malloc_info 3 (date) "Linux man-pages (unreleased)"
8 malloc_info \- export malloc state to a stream
11 .RI ( libc ", " \-lc )
14 .B #include <malloc.h>
16 .BI "int malloc_info(int " options ", FILE *" stream );
21 function exports an XML string that describes the current state
22 of the memory-allocation
23 implementation in the caller.
24 The string is printed on the file stream
26 The exported string includes information about all arenas (see
29 As currently implemented,
36 On failure, it returns \-1, and
38 is set to indicate the error.
45 For an explanation of the terms used in this section, see
51 Interface Attribute Value
56 T} Thread safety MT-Safe
63 The memory-allocation information is provided as an XML string
64 (rather than a C structure)
65 because the information may change over time
66 (according to changes in the underlying implementation).
67 The output XML string includes a version field.
70 .BR open_memstream (3)
71 function can be used to send the output of
73 directly into a buffer in memory, rather than to a file.
77 function is designed to address deficiencies in
82 The program below takes up to four command-line arguments,
83 of which the first three are mandatory.
84 The first argument specifies the number of threads that
85 the program should create.
86 All of the threads, including the main thread,
87 allocate the number of blocks of memory specified by the second argument.
88 The third argument controls the size of the blocks to be allocated.
89 The main thread creates blocks of this size,
90 the second thread created by the program allocates blocks of twice this size,
91 the third thread allocates blocks of three times this size, and so on.
95 twice to display the memory-allocation state.
96 The first call takes place before any threads
97 are created or memory allocated.
98 The second call is performed after all threads have allocated memory.
100 In the following example,
101 the command-line arguments specify the creation of one additional thread,
102 and both the main thread and the additional thread
103 allocate 10000 blocks of memory.
104 After the blocks of memory have been allocated,
106 shows the state of two allocation arenas.
110 .RB "$ " "getconf GNU_LIBC_VERSION"
112 .RB "$ " "./a.out 1 10000 100"
113 ============ Before allocating blocks ============
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"/>
125 <total type="fast" count="0" size="0"/>
126 <total type="rest" count="0" size="0"/>
127 <system type="current" size="135168"/>
128 <system type="max" size="135168"/>
129 <aspace type="total" size="135168"/>
130 <aspace type="mprotect" size="135168"/>
133 ============ After allocating blocks ============
138 <total type="fast" count="0" size="0"/>
139 <total type="rest" count="0" size="0"/>
140 <system type="current" size="1081344"/>
141 <system type="max" size="1081344"/>
142 <aspace type="total" size="1081344"/>
143 <aspace type="mprotect" size="1081344"/>
148 <total type="fast" count="0" size="0"/>
149 <total type="rest" count="0" size="0"/>
150 <system type="current" size="1032192"/>
151 <system type="max" size="1032192"/>
152 <aspace type="total" size="1032192"/>
153 <aspace type="mprotect" size="1032192"/>
155 <total type="fast" count="0" size="0"/>
156 <total type="rest" count="0" size="0"/>
157 <system type="current" size="2113536"/>
158 <system type="max" size="2113536"/>
159 <aspace type="total" size="2113536"/>
160 <aspace type="mprotect" size="2113536"/>
165 .\" SRC BEGIN (malloc_info.c)
174 static size_t blockSize;
175 static size_t numThreads;
176 static unsigned int numBlocks;
179 thread_func(void *arg)
183 /* The multiplier \[aq](2 + tn)\[aq] ensures that each thread (including
184 the main thread) allocates a different amount of memory. */
186 for (unsigned int j = 0; j < numBlocks; j++)
187 if (malloc(blockSize * (2 + tn)) == NULL)
188 err(EXIT_FAILURE, "malloc\-thread");
190 sleep(100); /* Sleep until main thread terminates. */
195 main(int argc, char *argv[])
202 "%s num\-threads num\-blocks block\-size [sleep\-time]\en",
207 numThreads = atoi(argv[1]);
208 numBlocks = atoi(argv[2]);
209 blockSize = atoi(argv[3]);
210 sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
212 thr = calloc(numThreads, sizeof(*thr));
214 err(EXIT_FAILURE, "calloc");
216 printf("============ Before allocating blocks ============\en");
217 malloc_info(0, stdout);
219 /* Create threads that allocate different amounts of memory. */
221 for (size_t tn = 0; tn < numThreads; tn++) {
222 errno = pthread_create(&thr[tn], NULL, thread_func,
225 err(EXIT_FAILURE, "pthread_create");
227 /* If we add a sleep interval after the start\-up of each
228 thread, the threads likely won\[aq]t contend for malloc
229 mutexes, and therefore additional arenas won\[aq]t be
230 allocated (see malloc(3)). */
236 /* The main thread also allocates some memory. */
238 for (unsigned int j = 0; j < numBlocks; j++)
239 if (malloc(blockSize) == NULL)
240 err(EXIT_FAILURE, "malloc");
242 sleep(2); /* Give all threads a chance to
243 complete allocations. */
245 printf("\en============ After allocating blocks ============\en");
246 malloc_info(0, stdout);
255 .BR malloc_stats (3),
257 .BR open_memstream (3)