[PATCH] optimization for fixed size kmalloc()

From: Manfred Spraul (manfreds@colorfullife.com)
Date: Mon Apr 24 2000 - 13:25:43 EST


Most kmalloc calls are fixed size - we could use the same optimization
that's used by put_user()/get_user().

Which benchmarks are useful for testing such a patch?

--
	Manfred

// $Header$ // Kernel Version: // VERSION = 2 // PATCHLEVEL = 3 // SUBLEVEL = 99 // EXTRAVERSION = -pre5 --- 2.3/kernel/ksyms.c Wed Apr 12 15:00:33 2000 +++ build-2.3/kernel/ksyms.c Mon Apr 24 19:42:22 2000 @@ -107,7 +107,8 @@ EXPORT_SYMBOL(kmem_cache_shrink); EXPORT_SYMBOL(kmem_cache_alloc); EXPORT_SYMBOL(kmem_cache_free); -EXPORT_SYMBOL(kmalloc); +EXPORT_SYMBOL(cache_sizes); +EXPORT_SYMBOL(__kmalloc); EXPORT_SYMBOL(kfree); EXPORT_SYMBOL(kfree_s); EXPORT_SYMBOL(vmalloc); --- 2.3/mm/slab.c Wed Apr 12 15:00:33 2000 +++ build-2.3/mm/slab.c Mon Apr 24 19:38:59 2000 @@ -324,13 +324,8 @@ #define SLAB_SET_PAGE_SLAB(pg,x) ((pg)->list.prev = (struct list_head *)(x)) #define SLAB_GET_PAGE_SLAB(pg) ((kmem_slab_t *)(pg)->list.prev) -/* Size description struct for general caches. */ -typedef struct cache_sizes { - size_t cs_size; - kmem_cache_t *cs_cachep; -} cache_sizes_t; - -static cache_sizes_t cache_sizes[] = { +/* these values are hardcoded in <linux/kmalloc.h> */ +cache_sizes_t cache_sizes[] = { #if PAGE_SIZE == 4096 { 32, NULL}, #endif @@ -1680,7 +1675,7 @@ } void * -kmalloc(size_t size, int flags) +__kmalloc(size_t size, int flags) { cache_sizes_t *csizep = cache_sizes; diff -urN --exclude autoconf.h --exclude wait.h --exclude gdb.h --exclude modversions.h --exclude version.h --exclude compile.h 2.3/include/linux/kmalloc.h build-2.3/include/linux/kmalloc.h --- 2.3/include/linux/kmalloc.h Thu Jan 1 01:00:00 1970 +++ build-2.3/include/linux/kmalloc.h Mon Apr 24 20:07:05 2000 @@ -0,0 +1,64 @@ +/* + * linux/mm/slab.h + * Written by Manfred Spraul, 2000 + * (manfreds@colorfullife.com) + */ + +#ifndef _LINUX_KMALLOC_H +#define _LINUX_KMALLOC_H + +#ifdef __KERNEL__ + +#include <asm/page.h> +extern void *__kmalloc(size_t, int); +extern void kfree(const void *); +extern void kfree_s(const void *, size_t); + +/* Size description struct for general caches. */ +typedef struct cache_sizes { + size_t cs_size; + kmem_cache_t *cs_cachep; +} cache_sizes_t; + +extern cache_sizes_t cache_sizes[]; +extern void __you_cannot_kmalloc_more_than_128_kilo_bytes(void); + +static inline void* __constant_kmalloc(size_t size, int flags) +{ +#if PAGE_SIZE == 4096 + if(size < 32) + return kmem_cache_alloc(cache_sizes[0].cs_cachep, flags); +#define KSHIFT 0 +#else +#define KSHIFT 1 +#endif +#define FIXED_ALLOC(len,off) \ + if(size < len) \ + return kmem_cache_alloc(cache_sizes[off-KSHIFT].cs_cachep, flags) + FIXED_ALLOC(64,1); + FIXED_ALLOC(128,2); + FIXED_ALLOC(256,3); + FIXED_ALLOC(512,4); + FIXED_ALLOC(1024,5); + FIXED_ALLOC(2048,6); + FIXED_ALLOC(4096,7); + FIXED_ALLOC(8192,8); + FIXED_ALLOC(16384,9); + FIXED_ALLOC(32768,10); + FIXED_ALLOC(65536,11); + FIXED_ALLOC(131072,12); +#undef FIXED_ALLOC +#undef KSHIFT + __you_cannot_kmalloc_more_than_128_kilo_bytes(); + return NULL; +} + +extern void *kmem_cache_alloc(kmem_cache_t *, int); + +#define kmalloc(size, flags) \ + (__builtin_constant_p(size) ? \ + __constant_kmalloc((size),(flags)) : \ + __kmalloc(size,flags)) + +#endif /* __KERNEL__ */ +#endif /* _LINUX_KMALLOC_H */ diff -urN --exclude autoconf.h --exclude wait.h --exclude gdb.h --exclude modversions.h --exclude version.h --exclude compile.h 2.3/include/linux/slab.h build-2.3/include/linux/slab.h --- 2.3/include/linux/slab.h Sat Feb 12 20:42:24 2000 +++ build-2.3/include/linux/slab.h Mon Apr 24 20:10:20 2000 @@ -56,9 +56,7 @@ extern void *kmem_cache_alloc(kmem_cache_t *, int); extern void kmem_cache_free(kmem_cache_t *, void *); -extern void *kmalloc(size_t, int); -extern void kfree(const void *); -extern void kfree_s(const void *, size_t); +#include <linux/kmalloc.h> extern void kmem_cache_reap(int); extern int get_slabinfo(char *);

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun Apr 30 2000 - 21:00:08 EST