man-pages/man3/malloc_info.3.html
2021-03-31 01:06:50 +01:00

353 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MALLOC_INFO</TITLE>
</HEAD><BODY>
<H1>MALLOC_INFO</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
malloc_info - export malloc state to a stream
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;</B>
<B>int malloc_info(int </B><I>options</I><B>, FILE *</B><I>stream</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>malloc_info</B>()
function exports an XML string that describes the current state
of the memory-allocation
implementation in the caller.
The string is printed on the file stream
<I>stream</I>.
The exported string includes information about all arenas (see
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)).
<P>
As currently implemented,
<I>options</I>
must be zero.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>malloc_info</B>()
returns 0;
on error, it returns -1, with
<I>errno</I>
set to indicate the cause.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EINVAL</B>
<DD>
<I>options</I>
was nonzero.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>malloc_info</B>()
was added to glibc in version 2.10.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>malloc_info</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
This function is a GNU extension.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
The memory-allocation information is provided as an XML string
(rather than a C structure)
because the information may change over time
(according to changes in the underlying implementation).
The output XML string includes a version field.
<P>
The
<B><A HREF="/cgi-bin/man/man2html?3+open_memstream">open_memstream</A></B>(3)
function can be used to send the output of
<B>malloc_info</B>()
directly into a buffer in memory, rather than to a file.
<P>
The
<B>malloc_info</B>()
function is designed to address deficiencies in
<B><A HREF="/cgi-bin/man/man2html?3+malloc_stats">malloc_stats</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+mallinfo">mallinfo</A></B>(3).
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below takes up to four command-line arguments,
of which the first three are mandatory.
The first argument specifies the number of threads that
the program should create.
All of the threads, including the main thread,
allocate the number of blocks of memory specified by the second argument.
The third argument controls the size of the blocks to be allocated.
The main thread creates blocks of this size,
the second thread created by the program allocates blocks of twice this size,
the third thread allocates blocks of three times this size, and so on.
<P>
The program calls
<B>malloc_info</B>()
twice to display the memory-allocation state.
The first call takes place before any threads
are created or memory allocated.
The second call is performed after all threads have allocated memory.
<P>
In the following example,
the command-line arguments specify the creation of one additional thread,
and both the main thread and the additional thread
allocate 10000 blocks of memory.
After the blocks of memory have been allocated,
<B>malloc_info</B>()
shows the state of two allocation arenas.
<P>
$ <B>getconf GNU_LIBC_VERSION</B>
glibc 2.13
$ <B>./a.out 1 10000 100</B>
============ Before allocating blocks ============
&lt;malloc version=&quot;1&quot;&gt;
&lt;heap nr=&quot;0&quot;&gt;
&lt;sizes&gt;
&lt;/sizes&gt;
&lt;total type=&quot;fast&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;total type=&quot;rest&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;system type=&quot;current&quot; size=&quot;135168&quot;/&gt;
&lt;system type=&quot;max&quot; size=&quot;135168&quot;/&gt;
&lt;aspace type=&quot;total&quot; size=&quot;135168&quot;/&gt;
&lt;aspace type=&quot;mprotect&quot; size=&quot;135168&quot;/&gt;
&lt;/heap&gt;
&lt;total type=&quot;fast&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;total type=&quot;rest&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;system type=&quot;current&quot; size=&quot;135168&quot;/&gt;
&lt;system type=&quot;max&quot; size=&quot;135168&quot;/&gt;
&lt;aspace type=&quot;total&quot; size=&quot;135168&quot;/&gt;
&lt;aspace type=&quot;mprotect&quot; size=&quot;135168&quot;/&gt;
&lt;/malloc&gt;
<P>
============ After allocating blocks ============
&lt;malloc version=&quot;1&quot;&gt;
&lt;heap nr=&quot;0&quot;&gt;
&lt;sizes&gt;
&lt;/sizes&gt;
&lt;total type=&quot;fast&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;total type=&quot;rest&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;system type=&quot;current&quot; size=&quot;1081344&quot;/&gt;
&lt;system type=&quot;max&quot; size=&quot;1081344&quot;/&gt;
&lt;aspace type=&quot;total&quot; size=&quot;1081344&quot;/&gt;
&lt;aspace type=&quot;mprotect&quot; size=&quot;1081344&quot;/&gt;
&lt;/heap&gt;
&lt;heap nr=&quot;1&quot;&gt;
&lt;sizes&gt;
&lt;/sizes&gt;
&lt;total type=&quot;fast&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;total type=&quot;rest&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;system type=&quot;current&quot; size=&quot;1032192&quot;/&gt;
&lt;system type=&quot;max&quot; size=&quot;1032192&quot;/&gt;
&lt;aspace type=&quot;total&quot; size=&quot;1032192&quot;/&gt;
&lt;aspace type=&quot;mprotect&quot; size=&quot;1032192&quot;/&gt;
&lt;/heap&gt;
&lt;total type=&quot;fast&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;total type=&quot;rest&quot; count=&quot;0&quot; size=&quot;0&quot;/&gt;
&lt;system type=&quot;current&quot; size=&quot;2113536&quot;/&gt;
&lt;system type=&quot;max&quot; size=&quot;2113536&quot;/&gt;
&lt;aspace type=&quot;total&quot; size=&quot;2113536&quot;/&gt;
&lt;aspace type=&quot;mprotect&quot; size=&quot;2113536&quot;/&gt;
&lt;/malloc&gt;
<A NAME="lbAL">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
<P>
static size_t blockSize;
static int numThreads, numBlocks;
<P>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(0)
<P>
static void *
thread_func(void *arg)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;tn&nbsp;=&nbsp;(int)&nbsp;arg;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;multiplier&nbsp;'(2&nbsp;+&nbsp;tn)'&nbsp;ensures&nbsp;that&nbsp;each&nbsp;thread&nbsp;(including
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;main&nbsp;thread)&nbsp;allocates&nbsp;a&nbsp;different&nbsp;amount&nbsp;of&nbsp;memory&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;numBlocks;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(malloc(blockSize&nbsp;*&nbsp;(2&nbsp;+&nbsp;tn))&nbsp;==&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;malloc-thread&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sleep(100);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Sleep&nbsp;until&nbsp;main&nbsp;thread&nbsp;terminates&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NULL;
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j,&nbsp;tn,&nbsp;sleepTime;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;*thr;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;4)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;%s&nbsp;num-threads&nbsp;num-blocks&nbsp;block-size&nbsp;[sleep-time]\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;numThreads&nbsp;=&nbsp;atoi(argv[1]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;numBlocks&nbsp;=&nbsp;atoi(argv[2]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;blockSize&nbsp;=&nbsp;atoi(argv[3]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sleepTime&nbsp;=&nbsp;(argc&nbsp;&gt;&nbsp;4)&nbsp;?&nbsp;atoi(argv[4])&nbsp;:&nbsp;0;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;thr&nbsp;=&nbsp;calloc(numThreads,&nbsp;sizeof(pthread_t));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(thr&nbsp;==&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;calloc&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;============&nbsp;Before&nbsp;allocating&nbsp;blocks&nbsp;============\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;malloc_info(0,&nbsp;stdout);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Create&nbsp;threads&nbsp;that&nbsp;allocate&nbsp;different&nbsp;amounts&nbsp;of&nbsp;memory&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(tn&nbsp;=&nbsp;0;&nbsp;tn&nbsp;&lt;&nbsp;numThreads;&nbsp;tn++)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno&nbsp;=&nbsp;pthread_create(&amp;thr[tn],&nbsp;NULL,&nbsp;thread_func,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;*)&nbsp;tn);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(errno&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;pthread_create&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;If&nbsp;we&nbsp;add&nbsp;a&nbsp;sleep&nbsp;interval&nbsp;after&nbsp;the&nbsp;start-up&nbsp;of&nbsp;each
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread,&nbsp;the&nbsp;threads&nbsp;likely&nbsp;won't&nbsp;contend&nbsp;for&nbsp;malloc
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutexes,&nbsp;and&nbsp;therefore&nbsp;additional&nbsp;arenas&nbsp;won't&nbsp;be
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allocated&nbsp;(see&nbsp;<A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A>(3)).&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(sleepTime&nbsp;&gt;&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(sleepTime);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;main&nbsp;thread&nbsp;also&nbsp;allocates&nbsp;some&nbsp;memory&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;numBlocks;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(malloc(blockSize)&nbsp;==&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;malloc&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Give&nbsp;all&nbsp;threads&nbsp;a&nbsp;chance&nbsp;to
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete&nbsp;allocations&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n============&nbsp;After&nbsp;allocating&nbsp;blocks&nbsp;============\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;malloc_info(0,&nbsp;stdout);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+mallinfo">mallinfo</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc_stats">malloc_stats</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+open_memstream">open_memstream</A></B>(3)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="5"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="6"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="7"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="8"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="9"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="10"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="11"><A HREF="#lbAK">EXAMPLE</A><DD>
<DL>
<DT id="12"><A HREF="#lbAL">Program source</A><DD>
</DL>
<DT id="13"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:47 GMT, March 31, 2021
</BODY>
</HTML>