mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Manfred Spraul <manfred@colorfullife.com>
To: Arjan van de Ven <arjanv@redhat.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH,RFC] faster kmalloc lookup
Date: Fri, 01 Nov 2002 00:03:44 +0100	[thread overview]
Message-ID: <3DC1B6D0.8050202@colorfullife.com> (raw)
In-Reply-To: <1036056917.2872.0.camel@dhcp59-228.rdu.redhat.com>

[-- Attachment #1: Type: text/plain, Size: 516 bytes --]

Arjan van de Ven wrote:

>On Sat, 2002-10-26 at 21:22, Manfred Spraul wrote:
>  
>
>>kmalloc spends a large part of the total execution time trying to find 
>>the cache for the passed in size.
>>    
>>
>
>would it be possible for fixed size kmalloc's to have the compiler
>precalculate this ?
>
>
>  
>
My patch is attached.
Question to the preprocessor experts:
is there a simpler way to achieve that? Right now the list of kmalloc 
caches exists in 3 copies, which could easily get out of sync.

--
    Manfred



[-- Attachment #2: patch-kmalloc-fast --]
[-- Type: text/plain, Size: 3396 bytes --]

// $Header$
// Kernel Version:
//  VERSION = 2
//  PATCHLEVEL = 5
//  SUBLEVEL = 45
//  EXTRAVERSION =
--- 2.5/include/linux/slab.h	Sat Oct 26 21:02:12 2002
+++ build-2.5/include/linux/slab.h	Thu Oct 31 23:58:26 2002
@@ -58,7 +58,80 @@
 extern void kmem_cache_free(kmem_cache_t *, void *);
 extern unsigned int kmem_cache_size(kmem_cache_t *);
 
-extern void *kmalloc(size_t, int);
+/* Size description struct for general caches. */
+struct cache_sizes {
+	size_t		 cs_size;
+	kmem_cache_t	*cs_cachep;
+	kmem_cache_t	*cs_dmacachep;
+};
+extern struct cache_sizes malloc_sizes[];
+extern void __you_cannot_kmalloc_more_than_128_kilo_bytes(void);
+
+enum malloc_numbers
+{
+#if PAGE_SIZE == 4096
+	kmalloc_size_32 = 0,
+#endif
+	kmalloc_size_64,
+#if L1_CACHE_BYTES < 64
+	kmalloc_size_96,
+#endif
+	kmalloc_size_128,
+#if L1_CACHE_BYTES < 128
+	kmalloc_size_192,
+#endif
+	kmalloc_size_256,
+	kmalloc_size_512,
+	kmalloc_size_1024,
+	kmalloc_size_2048,
+	kmalloc_size_4096,
+	kmalloc_size_8192,
+	kmalloc_size_16384,
+	kmalloc_size_32768,
+	kmalloc_size_65536,
+	kmalloc_size_131072
+};
+
+static inline void* __constant_kmalloc(size_t size, int flags)
+{
+#define FIXED_ALLOC(len) \
+	if(size <= len) \
+		return kmem_cache_alloc( (flags & GFP_DMA)? \
+				malloc_sizes[kmalloc_size_##len].cs_dmacachep \
+				: malloc_sizes[kmalloc_size_##len].cs_cachep, \
+				flags)
+#if PAGE_SIZE == 4096
+	FIXED_ALLOC(32);
+#endif
+	FIXED_ALLOC(64);
+#if L1_CACHE_BYTES < 64
+	FIXED_ALLOC(96);
+#endif
+	FIXED_ALLOC(128);
+#if L1_CACHE_BYTES < 128
+	FIXED_ALLOC(192);
+#endif
+	FIXED_ALLOC(256);
+	FIXED_ALLOC(512);
+	FIXED_ALLOC(1024);
+	FIXED_ALLOC(2048);
+	FIXED_ALLOC(4096);
+	FIXED_ALLOC(8192);
+	FIXED_ALLOC(16384);
+	FIXED_ALLOC(32768);
+	FIXED_ALLOC(65536);
+	FIXED_ALLOC(131072);
+#undef FIXED_ALLOC
+	__you_cannot_kmalloc_more_than_128_kilo_bytes();
+	return NULL;
+}
+extern void *__kmalloc(size_t, int);
+
+#define kmalloc(size, flags)				\
+	(__builtin_constant_p(size) ?			\
+	 __constant_kmalloc((size),(flags)) :		\
+	 __kmalloc(size,flags))
+
 extern void kfree(const void *);
 
 extern int FASTCALL(kmem_cache_reap(int));
--- 2.5/mm/slab.c	Thu Oct 31 18:48:19 2002
+++ build-2.5/mm/slab.c	Thu Oct 31 23:58:26 2002
@@ -369,15 +369,8 @@
 #define	SET_PAGE_SLAB(pg,x)   ((pg)->list.prev = (struct list_head *)(x))
 #define	GET_PAGE_SLAB(pg)     ((struct slab *)(pg)->list.prev)
 
-/* Size description struct for general caches. */
-struct cache_sizes {
-	size_t		 cs_size;
-	kmem_cache_t	*cs_cachep;
-	kmem_cache_t	*cs_dmacachep;
-};
-
 /* These are the default caches for kmalloc. Custom caches can have other sizes. */
-static struct cache_sizes malloc_sizes[] = {
+struct cache_sizes malloc_sizes[] = {
 #if PAGE_SIZE == 4096
 	{    32,	NULL, NULL},
 #endif
@@ -1804,7 +1797,7 @@
  * platforms.  For example, on i386, it means that the memory must come
  * from the first 16MB.
  */
-void * kmalloc (size_t size, int flags)
+void * __kmalloc (size_t size, int flags)
 {
 	struct cache_sizes *csizep = malloc_sizes;
 
--- 2.5/kernel/ksyms.c	Thu Oct 31 18:48:18 2002
+++ build-2.5/kernel/ksyms.c	Thu Oct 31 23:44:54 2002
@@ -102,7 +102,8 @@
 EXPORT_SYMBOL(kmem_cache_size);
 EXPORT_SYMBOL(set_shrinker);
 EXPORT_SYMBOL(remove_shrinker);
-EXPORT_SYMBOL(kmalloc);
+EXPORT_SYMBOL(malloc_sizes);
+EXPORT_SYMBOL(__kmalloc);
 EXPORT_SYMBOL(kfree);
 EXPORT_SYMBOL(vfree);
 EXPORT_SYMBOL(__vmalloc);

  reply	other threads:[~2002-10-31 22:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-26 19:22 Manfred Spraul
2002-10-26 22:30 ` Alan Cox
2002-10-27 10:08   ` Manfred Spraul
2002-10-27 13:29     ` Manfred Spraul
2002-10-28 13:05       ` Nikita Danilov
2002-10-28 13:18         ` Marcus Alanen
2002-10-28 13:26           ` Nikita Danilov
2002-10-28 16:03             ` Manfred Spraul
2002-10-31  9:35 ` Arjan van de Ven
2002-10-31 23:03   ` Manfred Spraul [this message]
     [not found] <3DBAEB64.1090109@colorfullife.com.suse.lists.linux.kernel>
     [not found] ` <1036056917.2872.0.camel@dhcp59-228.rdu.redhat.com.suse.lists.linux.kernel>
     [not found]   ` <3DC1B6D0.8050202@colorfullife.com.suse.lists.linux.kernel>
2002-11-01  8:03     ` Andi Kleen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3DC1B6D0.8050202@colorfullife.com \
    --to=manfred@colorfullife.com \
    --cc=arjanv@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome