353 lines
13 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
malloc_info - export malloc state to a stream
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/malloc.h">malloc.h</A>></B>
|
|
|
|
<B>int malloc_info(int </B><I>options</I><B>, FILE *</B><I>stream</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </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"> </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"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="1"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>options</I>
|
|
|
|
was nonzero.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>malloc_info</B>()
|
|
|
|
was added to glibc in version 2.10.
|
|
<A NAME="lbAH"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
This function is a GNU extension.
|
|
<A NAME="lbAJ"> </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"> </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 ============
|
|
<malloc version="1">
|
|
<heap nr="0">
|
|
<sizes>
|
|
</sizes>
|
|
<total type="fast" count="0" size="0"/>
|
|
<total type="rest" count="0" size="0"/>
|
|
<system type="current" size="135168"/>
|
|
<system type="max" size="135168"/>
|
|
<aspace type="total" size="135168"/>
|
|
<aspace type="mprotect" size="135168"/>
|
|
</heap>
|
|
<total type="fast" count="0" size="0"/>
|
|
<total type="rest" count="0" size="0"/>
|
|
<system type="current" size="135168"/>
|
|
<system type="max" size="135168"/>
|
|
<aspace type="total" size="135168"/>
|
|
<aspace type="mprotect" size="135168"/>
|
|
</malloc>
|
|
<P>
|
|
============ After allocating blocks ============
|
|
<malloc version="1">
|
|
<heap nr="0">
|
|
<sizes>
|
|
</sizes>
|
|
<total type="fast" count="0" size="0"/>
|
|
<total type="rest" count="0" size="0"/>
|
|
<system type="current" size="1081344"/>
|
|
<system type="max" size="1081344"/>
|
|
<aspace type="total" size="1081344"/>
|
|
<aspace type="mprotect" size="1081344"/>
|
|
</heap>
|
|
<heap nr="1">
|
|
<sizes>
|
|
</sizes>
|
|
<total type="fast" count="0" size="0"/>
|
|
<total type="rest" count="0" size="0"/>
|
|
<system type="current" size="1032192"/>
|
|
<system type="max" size="1032192"/>
|
|
<aspace type="total" size="1032192"/>
|
|
<aspace type="mprotect" size="1032192"/>
|
|
</heap>
|
|
<total type="fast" count="0" size="0"/>
|
|
<total type="rest" count="0" size="0"/>
|
|
<system type="current" size="2113536"/>
|
|
<system type="max" size="2113536"/>
|
|
<aspace type="total" size="2113536"/>
|
|
<aspace type="mprotect" size="2113536"/>
|
|
</malloc>
|
|
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/malloc.h">malloc.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
static size_t blockSize;
|
|
static int numThreads, numBlocks;
|
|
<P>
|
|
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
|
|
<BR> } while (0)
|
|
<P>
|
|
static void *
|
|
thread_func(void *arg)
|
|
{
|
|
<BR> int j;
|
|
<BR> int tn = (int) arg;
|
|
<P>
|
|
<BR> /* The multiplier '(2 + tn)' ensures that each thread (including
|
|
<BR> the main thread) allocates a different amount of memory */
|
|
<P>
|
|
<BR> for (j = 0; j < numBlocks; j++)
|
|
<BR> if (malloc(blockSize * (2 + tn)) == NULL)
|
|
<BR> errExit("malloc-thread");
|
|
<P>
|
|
<BR> sleep(100); /* Sleep until main thread terminates */
|
|
<BR> return NULL;
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int j, tn, sleepTime;
|
|
<BR> pthread_t *thr;
|
|
<P>
|
|
<BR> if (argc < 4) {
|
|
<BR> fprintf(stderr,
|
|
<BR> "%s num-threads num-blocks block-size [sleep-time]\n",
|
|
<BR> argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> numThreads = atoi(argv[1]);
|
|
<BR> numBlocks = atoi(argv[2]);
|
|
<BR> blockSize = atoi(argv[3]);
|
|
<BR> sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
|
|
<P>
|
|
<BR> thr = calloc(numThreads, sizeof(pthread_t));
|
|
<BR> if (thr == NULL)
|
|
<BR> errExit("calloc");
|
|
<P>
|
|
<BR> printf("============ Before allocating blocks ============\n");
|
|
<BR> malloc_info(0, stdout);
|
|
<P>
|
|
<BR> /* Create threads that allocate different amounts of memory */
|
|
<P>
|
|
<BR> for (tn = 0; tn < numThreads; tn++) {
|
|
<BR> errno = pthread_create(&thr[tn], NULL, thread_func,
|
|
<BR> (void *) tn);
|
|
<BR> if (errno != 0)
|
|
<BR> errExit("pthread_create");
|
|
<P>
|
|
<BR> /* If we add a sleep interval after the start-up of each
|
|
<BR> thread, the threads likely won't contend for malloc
|
|
<BR> mutexes, and therefore additional arenas won't be
|
|
<BR> allocated (see <A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A>(3)). */
|
|
<P>
|
|
<BR> if (sleepTime > 0)
|
|
<BR> sleep(sleepTime);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* The main thread also allocates some memory */
|
|
<P>
|
|
<BR> for (j = 0; j < numBlocks; j++)
|
|
<BR> if (malloc(blockSize) == NULL)
|
|
<BR> errExit("malloc");
|
|
<P>
|
|
<BR> <A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2); /* Give all threads a chance to
|
|
<BR> complete allocations */
|
|
<P>
|
|
<BR> printf("\n============ After allocating blocks ============\n");
|
|
<BR> malloc_info(0, stdout);
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAM"> </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"> </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"> </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>
|