[PATCH] Use madvise in glibc

Thorsten Kukuk kukuk@suse.de
Fri Jul 23 13:53:00 GMT 2004


Hi,

One of our gcc developers made a patch for glibc to use madvise to
tell the kernel that the loaded libraries are expected to be needed
in the near future.
After this, I got a lot of requests from people, who think that this
feature needs to go upstream.

I have attached the patch for discussion.

  Thorsten
-- 
Thorsten Kukuk       http://www.suse.de/~kukuk/        kukuk@suse.de
SuSE Linux AG        Maxfeldstr. 5                 D-90409 Nuernberg
--------------------------------------------------------------------    
Key fingerprint = A368 676B 5E1B 3E46 CFCE  2D97 F8FD 4E23 56C6 FB4B
-------------- next part --------------
2004-07-23  Thorsten Kukuk  <kukuk@suse.de>

	* glibc-2.3/sysdeps/unix/sysv/linux/syscalls.list: Also
	define __madvise.

	* glibc-2.3/sysdeps/generic/ldsodefs.h: Add _dl_madvise
	prototpye.

	* glibc-2.3/include/sys/mman.h: Add __madvise prototype.

	* glibc-2.3/elf/dl-support.c: Define _dl_madvise.
	(_dl_non_dynamic_init): Check for LD_NOMADVISE env variable.
	* glibc-2.3/elf/rtld.c (process_envvars): Likewise.

	* glibc-2.3/elf/dl-load.c (_dl_map_object_from_fd): Call
	__madvise to tell the kernel that the pages will be needed
	in the near future.

	Patch by Michael Matz.

--- glibc-2.3/elf/dl-load.c	2004-07-07 10:07:35.000000000 +0200
+++ glibc-2.3-madvise/elf/dl-load.c	2004-07-23 14:53:38.000000000 +0200
@@ -1108,6 +1108,9 @@
 	    goto call_lose_errno;
 	  }
 
+	if (GLRO(dl_madvise))
+	  __madvise ((void *) l->l_map_start, maplength, MADV_WILLNEED);
+
 	l->l_map_end = l->l_map_start + maplength;
 	l->l_addr = l->l_map_start - c->mapstart;
 
--- glibc-2.3/elf/dl-support.c	2004-07-06 08:30:28.000000000 +0200
+++ glibc-2.3-madvise/elf/dl-support.c	2004-07-23 14:56:20.000000000 +0200
@@ -41,6 +41,7 @@
 
 int _dl_debug_mask;
 int _dl_lazy;
+int _dl_madvise;
 ElfW(Addr) _dl_use_load_bias = -2;
 int _dl_dynamic_weak;
 
@@ -246,6 +247,8 @@
 
   _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
 
+  _dl_madvise = *(getenv ("LD_NOMADVISE") ?: "") == '\0';
+
   _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
 
   _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
--- glibc-2.3/elf/rtld.c	2004-07-12 16:03:26.000000000 +0200
+++ glibc-2.3-madvise/elf/rtld.c	2004-07-23 15:00:15.000000000 +0200
@@ -124,6 +124,7 @@
     ._dl_hwcap_mask = HWCAP_IMPORTANT,
     ._dl_lazy = 1,
     ._dl_fpu_control = _FPU_DEFAULT,
+    ._dl_madvise = 1,
 
     /* Function pointers.  */
     ._dl_get_origin = _dl_get_origin,
@@ -2089,6 +2090,14 @@
 	  break;
 
 	case 9:
+	  /* Test whether we should not advise the kernel
+	     about memory usage.  */
+	  if (memcmp (envline, "NOMADVISE", 9) == 0)
+	    {
+	      GLRO(dl_madvise) = envline[10] == '\0';
+	      break;
+	    }
+
 	  /* Test whether we want to see the content of the auxiliary
 	     array passed up from the kernel.  */
 	  if (memcmp (envline, "SHOW_AUXV", 9) == 0)
--- glibc-2.3/include/sys/mman.h	1999-11-26 09:16:29.000000000 +0100
+++ glibc-2.3-madvise/include/sys/mman.h	2004-07-23 15:03:23.000000000 +0200
@@ -8,6 +8,7 @@
 		       int __flags, int __fd, __off64_t __offset);
 extern int __munmap (void *__addr, size_t __len);
 extern int __mprotect (void *__addr, size_t __len, int __prot);
+extern int __madvise (void *__addr, size_t __len, int __advise);
 
 /* This one is Linux specific.  */
 extern void *__mremap (void *__addr, size_t __old_len,
--- glibc-2.3/sysdeps/generic/ldsodefs.h	2004-07-12 16:03:35.000000000 +0200
+++ glibc-2.3-madvise/sysdeps/generic/ldsodefs.h	2004-07-23 15:04:33.000000000 +0200
@@ -403,6 +403,9 @@
   /* Do we do lazy relocations?  */
   EXTERN int _dl_lazy;
 
+  /* Should we advise kernel about memory usage? */
+  EXTERN int _dl_madvise;
+
   /* Nonzero if runtime lookups should not update the .got/.plt.  */
   EXTERN int _dl_bind_not;
 
--- glibc-2.3/sysdeps/unix/sysv/linux/syscalls.list	2004-04-13 06:43:06.000000000 +0200
+++ glibc-2.3-madvise/sysdeps/unix/sysv/linux/syscalls.list	2004-07-23 15:07:39.000000000 +0200
@@ -29,7 +29,7 @@
 klogctl		EXTRA	syslog		i:isi	klogctl
 lchown		-	lchown		i:sii	__lchown	lchown
 posix_madvise	-	madvise		Vi:pii	posix_madvise
-madvise		-	madvise		i:pii	madvise
+madvise		-	madvise		i:pii	__madvise	madvise
 mincore		-	mincore		i:anV	mincore
 mlock		-	mlock		i:bn	mlock
 mlockall	-	mlockall	i:i	mlockall


More information about the Libc-alpha mailing list