* how expensive are mallocs?
@ 2009-11-24 17:57 Andrew Grover
2009-11-25 23:18 ` David Miller
2009-11-26 22:25 ` Andi Kleen
0 siblings, 2 replies; 3+ messages in thread
From: Andrew Grover @ 2009-11-24 17:57 UTC (permalink / raw)
To: netdev
How much effort generally makes sense to avoid mallocs? For example,
rds has a function that does a kmalloc for an array of scatterlist
entries, which is freed at the bottom of the function. We couldn't
allocate off the stack in all cases (too big), but we could allocate
an array of say 8 scatterlists, and use it if that's big enough,
falling back to kmalloc if it's not.
Is this a good idea?
Also, RDS has its own per-cpu page remainder allocator (see
net/rds/page.c) for kernel send buffers. Would cutting this code and
just using kmalloc be recommended? Doesn't SL?B already do per-cpu
pools?
Does this stuff even matter enough to rise above the noise in benchmarks?
Thanks -- Regards -- Andy
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: how expensive are mallocs?
2009-11-24 17:57 how expensive are mallocs? Andrew Grover
@ 2009-11-25 23:18 ` David Miller
2009-11-26 22:25 ` Andi Kleen
1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2009-11-25 23:18 UTC (permalink / raw)
To: andy.grover; +Cc: netdev
From: Andrew Grover <andy.grover@gmail.com>
Date: Tue, 24 Nov 2009 09:57:34 -0800
> How much effort generally makes sense to avoid mallocs? For example,
> rds has a function that does a kmalloc for an array of scatterlist
> entries, which is freed at the bottom of the function. We couldn't
> allocate off the stack in all cases (too big), but we could allocate
> an array of say 8 scatterlists, and use it if that's big enough,
> falling back to kmalloc if it's not.
>
> Is this a good idea?
This is a poor idea, especially if this function executes frequently.
Better to have a per-socket or per-cpu (softirq protected, if
necessary) work area to do such things if you really can't fit it on
the stack.
> Also, RDS has its own per-cpu page remainder allocator (see
> net/rds/page.c) for kernel send buffers. Would cutting this code and
> just using kmalloc be recommended? Doesn't SL?B already do per-cpu
> pools?
>
> Does this stuff even matter enough to rise above the noise in benchmarks?
We already have a per-socket page allocation scheme for sendmsg handling.
In general adding more and more specialized allocators is heavily
discouraged. If the generic kernel facilities don't fit the bill,
fix them.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: how expensive are mallocs?
2009-11-24 17:57 how expensive are mallocs? Andrew Grover
2009-11-25 23:18 ` David Miller
@ 2009-11-26 22:25 ` Andi Kleen
1 sibling, 0 replies; 3+ messages in thread
From: Andi Kleen @ 2009-11-26 22:25 UTC (permalink / raw)
To: Andrew Grover; +Cc: netdev
Andrew Grover <andy.grover@gmail.com> writes:
> How much effort generally makes sense to avoid mallocs? For example,
The slab allocators are very optimized, with a fast path for
allocation and freeing that's essentially "disable interrupts ;
unlink object from a list ; reenable". That's not expensive
(unless you're running on a CPU where disabling interrupts is)
The main costs in them come when you free objects on a different CPU
(or worse node) than where you allocate them. In that case you can end
up with some bounced cache lines, which are slow. If you can avoid
that you're good. If you can't even then you would need to make major
effort to do better.
> Also, RDS has its own per-cpu page remainder allocator (see
> net/rds/page.c) for kernel send buffers. Would cutting this code and
> just using kmalloc be recommended? Doesn't SL?B already do per-cpu
> pools?
Slab is all per cpu in the fast path, but see above.
> Does this stuff even matter enough to rise above the noise in benchmarks?
Yes it does in some circumstances, but it's hard to do better.
One example of doing better for special circumstances would be Eric's
rps work, but doing these things is not easy and only worth it
for really critical cases.
-Andi
--
ak@linux.intel.com -- Speaking for myself only.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-11-26 22:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-24 17:57 how expensive are mallocs? Andrew Grover
2009-11-25 23:18 ` David Miller
2009-11-26 22:25 ` Andi Kleen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).