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
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.
71 .BR open_memstream (3)
72 function can be used to send the output of
74 directly into a buffer in memory, rather than to a file.
78 function is designed to address deficiencies in
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.
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.
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,
107 shows the state of two allocation arenas.
111 .RB "$ " "getconf GNU_LIBC_VERSION"
113 .RB "$ " "./a.out 1 10000 100"
114 ============ Before allocating blocks ============
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"/>
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"/>
134 ============ After allocating blocks ============
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"/>
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"/>
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"/>
166 .\" SRC BEGIN (malloc_info.c)
175 static size_t blockSize;
176 static size_t numThreads;
177 static unsigned int numBlocks;
180 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 (unsigned int j = 0; j < numBlocks; j++)
188 if (malloc(blockSize * (2 + tn)) == NULL)
189 err(EXIT_FAILURE, "malloc\-thread");
191 sleep(100); /* Sleep until main thread terminates. */
196 main(int argc, char *argv[])
203 "%s num\-threads num\-blocks block\-size [sleep\-time]\en",
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(*thr));
215 err(EXIT_FAILURE, "calloc");
217 printf("============ Before allocating blocks ============\en");
218 malloc_info(0, stdout);
220 /* Create threads that allocate different amounts of memory. */
222 for (size_t tn = 0; tn < numThreads; tn++) {
223 errno = pthread_create(&thr[tn], NULL, thread_func,
226 err(EXIT_FAILURE, "pthread_create");
228 /* If we add a sleep interval after the start\-up of each
229 thread, the threads likely won\[aq]t contend for malloc
230 mutexes, and therefore additional arenas won\[aq]t be
231 allocated (see malloc(3)). */
237 /* The main thread also allocates some memory. */
239 for (unsigned int j = 0; j < numBlocks; j++)
240 if (malloc(blockSize) == NULL)
241 err(EXIT_FAILURE, "malloc");
243 sleep(2); /* Give all threads a chance to
244 complete allocations. */
246 printf("\en============ After allocating blocks ============\en");
247 malloc_info(0, stdout);
256 .BR malloc_stats (3),
258 .BR open_memstream (3)