libbacktrace patch committed: Fixes for large binaries

Ian Lance Taylor iant@google.com
Fri May 9 05:01:00 GMT 2014


While testing on a large Google binary, I noticed that libbacktrace is
allocating an inordinate amount of memory.  The binary winds up with
377,944 entries in the unit_addrs vector.  Each entry is 24 bytes, so
this is 9,070,656 bytes, which is not too terrible.  Unfortunately, for
some reason I thought that when a libbacktrace vector is larger than a
page the code should only allocate one additional page at a time.  This
vector requires 2215 4096-byte pages.  Growing the vector one page at a
time allocates a total of something like (2215 * 2214) / 2 pages, which
turns out to be nearly 1.5G.  Allocating 1.5G to represent a vector of
size 9M is not desirable.

It's true that when the vector grows, the old memory can be reused.  But
there is nothing in libbacktrace that is going to reuse that much
memory.  And even worse, there was a bug in the vector_grow routine that
caused it to fail to correctly report the size of the old vector, so the
memory had no chance of being reused anyhow.

This patch fixes vector growth to double the number of pages requested
each time.  It fixes vector growth to record the correct size of the old
vector being freed.

The patch also adds some code to simply munmap large blocks of allocated
memory.  It's unlikely in practice that libbacktrace will ever be able
to reuse a large block, so it's probably better to hand the memory back
rather than hold onto it for no purpose.

Bootstrapped and tested on x86_64-unknown-linux-gnu.  Committed to 4.9
branch and mainline.

Ian


2014-05-08  Ian Lance Taylor  <iant@google.com>

	* mmap.c (backtrace_free): If freeing a large aligned block of
	memory, call munmap rather than holding onto it.
	(backtrace_vector_grow): When growing a vector, double the number
	of pages requested.  When releasing the old version of a grown
	vector, pass the correct size to backtrace_free.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.patch
Type: text/x-diff
Size: 2320 bytes
Desc: patch
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20140509/4ce77893/attachment.bin>


More information about the Gcc-patches mailing list