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