提交 fbaccacf 编写于 作者: S Steven Rostedt 提交者: Linus Torvalds

[PATCH] slab: cache_estimate cleanup

Clean up cache_estimate() in mm/slab.c and improves the algorithm from O(n) to
O(1).  We first calculate the maximum number of objects a slab can hold after
struct slab and kmem_bufctl_t for each object has been given enough space.
After that, to respect alignment rules, we decrease the number of objects if
necessary.  As required padding is at most align-1 and memory of obj_size is
at least align, it is always enough to decrease number of objects by one.

The optimization was originally made by Balbir Singh with more improvements
from Steven Rostedt.  Manfred Spraul provider further modifications: no loop
at all for the off-slab case and added comments to explain the background.
Acked-by: NBalbir Singh <bsingharora@gmail.com>
Signed-off-by: NManfred Spraul <manfred@colorfullife.com>
Signed-off-by: NSteven Rostedt <rostedt@goodmis.org>
Signed-off-by: NPekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 5ec8a847
...@@ -702,32 +702,69 @@ kmem_cache_t *kmem_find_general_cachep(size_t size, gfp_t gfpflags) ...@@ -702,32 +702,69 @@ kmem_cache_t *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
} }
EXPORT_SYMBOL(kmem_find_general_cachep); EXPORT_SYMBOL(kmem_find_general_cachep);
/* Cal the num objs, wastage, and bytes left over for a given slab size. */ static size_t slab_mgmt_size(size_t nr_objs, size_t align)
static void cache_estimate(unsigned long gfporder, size_t size, size_t align,
int flags, size_t *left_over, unsigned int *num)
{ {
int i; return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
size_t wastage = PAGE_SIZE << gfporder; }
size_t extra = 0;
size_t base = 0;
if (!(flags & CFLGS_OFF_SLAB)) { /* Calculate the number of objects and left-over bytes for a given
base = sizeof(struct slab); buffer size. */
extra = sizeof(kmem_bufctl_t); static void cache_estimate(unsigned long gfporder, size_t buffer_size,
} size_t align, int flags, size_t *left_over,
i = 0; unsigned int *num)
while (i * size + ALIGN(base + i * extra, align) <= wastage) {
i++; int nr_objs;
if (i > 0) size_t mgmt_size;
i--; size_t slab_size = PAGE_SIZE << gfporder;
if (i > SLAB_LIMIT) /*
i = SLAB_LIMIT; * The slab management structure can be either off the slab or
* on it. For the latter case, the memory allocated for a
* slab is used for:
*
* - The struct slab
* - One kmem_bufctl_t for each object
* - Padding to respect alignment of @align
* - @buffer_size bytes for each object
*
* If the slab management structure is off the slab, then the
* alignment will already be calculated into the size. Because
* the slabs are all pages aligned, the objects will be at the
* correct alignment when allocated.
*/
if (flags & CFLGS_OFF_SLAB) {
mgmt_size = 0;
nr_objs = slab_size / buffer_size;
*num = i; if (nr_objs > SLAB_LIMIT)
wastage -= i * size; nr_objs = SLAB_LIMIT;
wastage -= ALIGN(base + i * extra, align); } else {
*left_over = wastage; /*
* Ignore padding for the initial guess. The padding
* is at most @align-1 bytes, and @buffer_size is at
* least @align. In the worst case, this result will
* be one greater than the number of objects that fit
* into the memory allocation when taking the padding
* into account.
*/
nr_objs = (slab_size - sizeof(struct slab)) /
(buffer_size + sizeof(kmem_bufctl_t));
/*
* This calculated number will be either the right
* amount, or one greater than what we want.
*/
if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
> slab_size)
nr_objs--;
if (nr_objs > SLAB_LIMIT)
nr_objs = SLAB_LIMIT;
mgmt_size = slab_mgmt_size(nr_objs, align);
}
*num = nr_objs;
*left_over = slab_size - nr_objs*buffer_size - mgmt_size;
} }
#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg) #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册