This is the mail archive of the
mailing list for the glibc project.
Re: [PATCH] Add malloc micro benchmark
On 12/18/2017 07:18 AM, Wilco Dijkstra wrote:
> Carlos O'Donell wrote:
> Thanks for the review!
Thank you for the detailed follow up.
>> This test is a long time coming and is a great idea.
>> My "big" question here is: What are we trying to model?
>> Do we want to prove that the single threaded optimizations you
>> added are helping a given size class of allocations?
> Yes that is the main goal of the benchmark. It models the allocation
> pattern of a few benchmarks which were reported as being slow
> despite the new tcache (which didn't show any gains).
> When the tcache was configured to be larger there was a major
> speedup, suggesting that the tcache doesn't work on patterns with
> a high number of (de)allocations of similar sized blocks. Since DJ
> didn't seem keen on increasing the tcache size despite it showing
> major gains across a wide range of benchmarks, I decided to fix
> the performance for the single-threaded case at least. It's now 2.5x
> faster on a few sever benchmarks (of course the next question is
> whether tcache is actually useful in its current form).
If you have a pattern of malloc/free of *similar* sized blocks, then
it overflows the sized bin in the tcache, with other size bins remaining
empty. The cache itself does not dynamically reconfigure itself to consume
X MiB or Y % of RSS, instead it uses a simple data structure to contain
a fixed number of fixed size blocks.
Therefore I agree, that enhancing the core data structure in tcache may
result in better overall performance, particularly if we got rid of the
fixed bin sizes and instead found a way to be performant *and* keep a
running total of consumption.
This is not a trivial goal though.
Likewise *all* of malloc needs to be moved to a better data structure than
just linked lists. I would like to see glibc's malloc offer a cacheing
footprint of no more than Y % of RSS available, and let the user tweak that.
Currently we just consume RSS without much regard for overhead. Though this
is a different case than than what you are talking about, the changes are
related via data-structure enhancements that would benefit both cases IMO.
>> You are currently modeling a workload that has increasing
>> memory size requests and in some ways this is an odd workload
>> that has high external fragmentation characteristics. For example
>> after allocating lots of 256 byte blocks we move on to 1024 byte
>> blocks, with the latter being unusable unless we coalesce.
> I'm assuming coalescing works as expected. If it doesn't, it would
> be a nasty bug.
You are probably right.
>> I *wish* we could test main_arena vs. threaded arena, since they
>> have different code and behave differently e.g. sbrk vs. mmap'd
> I'd have to check how easy it is to force it to use the thread arena.
> The whole thing is just crazily weird, with too many different code
> paths and possibilities. It seems much easier just to always use
> thread arenas, and perhaps use sbrk only if there is some serious
> advantage over mmap. Also it appears all the values are set to
> what was perhaps reasonable 10-20 years ago, not today. When
> a small server has 128GB, there is absolutely no reason to worry
> about returning 128KB to the OS as quickly as possible...
(a) Returning memory based on a limit of memory cached.
The decision to return memory to the operating system should be based
on a desire to run within the bounds of a certain amount of cached
memory in the user process.
This should be the goal IMO. We should not return 128KB to the OS unless
we are within our bounds of Y % of RSS cache, or X MiB of RSS cache.
This bounded behaviour is more and more important for (b).
So I argue that this has nothing to do with how much memory the server
has but how much the user wants as cache in the process. This gets back
to your point about tcache size needing to be bigger; if you had Y % RSS
allocated to tcache it would solve your needs.
(b) Packing density matters, or rather consistent RSS usage matters.
Yes, and no. We are facing a lot of downstream request for container,
and VM packing efficiency. This means that your 128GB is split into
32 servers each with 4GB, or 64 servers each with 2GB running smaller
services. In these cases we *do* care a lot about packing density.
(b) Maintenance costs of the existing weird cases and harmonizing threaded
and main_arena paths.
As I suggested in bug 15321:
We need to merge the main_arena and threaded code together, and stop
treating them as different things. Right now the main_arena, if you
look at the code, is a *pretend* heap with a partial data structure
layered in place. This needs to go away. We need to treat all heaps
as identical, with identical code paths, with just different backing
I think people still expect that thread 0 allocates from the sbrk
heap in a single-threaded application, and we can do that by ensuring
sbrk is used to provide the backing store for the main thread. This way
we can jump the pointer 64MB like we normally do for mmap'd heaps, but
then on page touch there the kernel just extends the heap normally.
No difference (except VMA usage).
Once that is in place we can experiment with other strategies like never
>> You need to make this robust against env vars changing malloc
>> behaviour. You should use mallopt to change some parameters.
> You mean setting the tcache size explicitly (maybe even switching off)?
You have several options:
* Add a wrapper script that clear all mallopt related env vars.
* Adjust the Makefile to clear all mallopt related env vars before starting
* Set tcache sizes explicitly *if* that is what you want, but likely you
don't want this and want to run the test with just the defaults to see
how the defaults are performing.
>>> Note something very bad happens for the larger allocations, there
>>> is a 25x slowdown from 25 to 400 allocations of 4KB blocks...
>> Keep in mind you are testing the performance of sbrk here. In a threaded
>> arena, the non-main_arena mmap's a 64MiB heap (on 64-bit) and then
>> draws allocations from it. So in some ways main_arena is expenseive,
>> but both have to pay a page-touch cost...
>> For each 4KiB block you touch the block to write the co-located metadata
>> and that forces the kernel to give you a blank page, which you then do
>> nothing with. Then you repeat the above again.
>> For all other sizes you amortize the cost of the new page among
>> several allocations.
>> Do you have any other explanation?
> Well that looks like a reasonable explanation, but it shows a serious
> performance bug - I think we use MADV_DONTNEED which doesn't
> work on Linux and will cause all pages to be deallocated, reallocated
> and zero-filled... This is the sort of case where you need to be very
> careful to amortize over many allocations or long elapsed time, if at
> all (many other allocators never give pages back).
We need to move to MADV_FREE, which was designed for memory allocators.
The semantics of MADV_DONTNEED have the problem that one has to consider:
* Is the data destructively lost in that page?
* Is the data flushed to the underlying store before being not-needed?
All of which lead to MADV_DONTNEED doing a lot of teardown work to ensure
that users don't corrupt the data in their backing stores.
I think that detection of MADV_FREE, and usage, would help performance,
but only on > Linux 4.5, and that might be OK for you.
>> At some point you will hit the mmap threshold and the cost of the
>> allocation will skyrocket as you have to call mmap.
> That only happens on huge allocations (much larger than 4KB), or when
> you run out of sbrk space (unlikely).
It happens at the mmap threshold, which is variable :-)
Please consider the implementation as a fluid set of parameters that
model application behaviour.
We can run out of sbrk space *immediately* if you have an interposing
low-address mmap that means sbrk can't grow (again see swbz#15321).
Right now the mmap threshold is 128KiB though, so you're right, for
the default. I don't know if that size is a good idea or not.
>> In glibc we have:
>> tcache -> fastbins -> smallbins -> largbing -> unordered -> mmap
>> If you proceed through from small allocations to larger allocations
>> you will create chunks that cannot be used by future allocations.
>> In many cases this is a worst case performance bottleneck. The
>> heap will contain many 256 byte allocations but these cannot service
>> the 1024 bytes, that is unless consolidation has been run. So this
>> tests the consolidation as much as anything else, which might not
>> trigger because of the free thresholds required.
> If consolidation doesn't work that's a serious bug. However allocation
> performance should not be affected either way - in a real application
> those small blocks might still be allocated. As long as consolidation
> runs quickly (generally it's a small percentage in profiles), it won't
> affect the results.
>> So what are we trying to model here?
>> If we want to look at the cost of independent size class allocations
>> then we need a clean process and allocate only a given size, and look
>> at performance across the number of allocations.
> That's certainly feasible if we keep the number of sizes small (less
> than the list below). It should be easy to reuse the bench-malloc-thread.c
> makefile magic to run the same binary with multiple sizes.
>> I would also have much finer grained allocations by powers of 2.
>> 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4092 etc. You want
>> to see what happens for the allocations which are:
>> Would this serve better to show that your single threaded malloc
>> changes were helpful for a given size class?
> Well I can easily add some of the above sizes, it's highly configurable.
> I don't think there will be much difference with the existing sizes though.
Perhaps, but I don't know the answer to that.
>> You need to use mallopt to make sure the user's environment
>> did not set MALLOC_MMAP_THRESHOLD_ to a value lower than your
>> maximum allocation size.
> I don't think that is possible given the largest allocation size is 4KB.
We carry out the allocation with mmap regardless, rounding up the size to
that of a page.