Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

64-bit-capable alternative to mallinfo?

On Linux, we have this (GNU C library) function named mallinfo() which gives you some numbers relating to memory allocation:

struct mallinfo {
       int arena;     /* Non-mmapped space allocated (bytes) */
       int ordblks;   /* Number of free chunks */
       int smblks;    /* Number of free fastbin blocks */
       int hblks;     /* Number of mmapped regions */
       int hblkhd;    /* Space allocated in mmapped regions (bytes) */
       int usmblks;   /* Maximum total allocated space (bytes) */
       int fsmblks;   /* Space in freed fastbin blocks (bytes) */
       int uordblks;  /* Total allocated space (bytes) */
       int fordblks;  /* Total free space (bytes) */
       int keepcost;  /* Top-most, releasable space (bytes) */
};

Strangely enough, those values are typically 32-bit integers (!); well, that really won't do, especially for the values given in number of bytes (e.g. fordblks).

I would guess this is deprecated somehow, and that some other facility is available to get the same information. What is it that alternative facility?

like image 755
einpoklum Avatar asked Nov 30 '16 00:11

einpoklum


2 Answers

Use malloc_info(). You need to parse it's xml output.
From the malloc_info man page:

The malloc_info() function is designed to address deficiencies in malloc_stats(3) and mallinfo(3).

Source code for malloc_info is e.g. available here. All variables are stored using size_t and printed out accordingly, it should work on any-bit machines.

E.g. on my system (glibc version 2.26) malloc_info(0, stdout) prints out the following:

<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"/>
<total type="mmap" 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>
like image 189
KamilCuk Avatar answered Oct 19 '22 10:10

KamilCuk


In glibc 2.33 and later there is a mallinfo2() function that has 64-bit field sizes:

https://sourceware.org/git/?p=glibc.git;a=commit;h=e3960d1c57e57f33e0e846d615788f4ede73b945

/* SVID2/XPG mallinfo2 structure which can handle allocations
   bigger than 4GB.  */

struct mallinfo2
{
  size_t arena;    /* non-mmapped space allocated from system */
  size_t ordblks;  /* number of free chunks */
  size_t smblks;   /* number of fastbin blocks */
  size_t hblks;    /* number of mmapped regions */
  size_t hblkhd;   /* space in mmapped regions */
  size_t usmblks;  /* always 0, preserved for backwards compatibility */
  size_t fsmblks;  /* space available in freed fastbin blocks */
  size_t uordblks; /* total allocated space */
  size_t fordblks; /* total free space */
  size_t keepcost; /* top-most, releasable (via malloc_trim) space */
};

Fixed only 15 years after being reported to glibc maintainers, but better late than never.

like image 1
LustreOne Avatar answered Oct 19 '22 09:10

LustreOne