Using madvise for ld.so

Michael Matz matz at suse.de
Tue Mar 16 12:34:09 CET 2004


Hi,

following up Lubos' hack in kdeinit and friends to use madvise to speed up 
loading DSOs and his observation that this better belongs inside ld.so I 
did so (thanks to Lubos for fixing a silly error in it).

The patch is for glibc CVS.  With older glibc's the GLRO has to be 
replaced with GL, and the order of the fields in rtld.c has changed a bit.  
I'm sure you'll figure out.  Brings a bit of speed up, ask Lubos for 
details ;)


Ciao,
Michael.
-- 
diff -upr sysdeps.orig/generic/ldsodefs.h sysdeps/generic/ldsodefs.h
--- sysdeps.orig/generic/ldsodefs.h	2004-03-09 17:51:44.000000000 +0100
+++ sysdeps/generic/ldsodefs.h	2004-03-09 16:31:45.000000000 +0100
@@ -282,6 +282,8 @@ struct rtld_global
   /* Do we do lazy relocations?  */
   EXTERN int _dl_lazy;
 
+  EXTERN int _dl_madvise;
+
   /* Nonzero if runtime lookups should not update the .got/.plt.  */
   EXTERN int _dl_bind_not;
 
diff -upr sysdeps.orig/unix/sysv/linux/syscalls.list sysdeps/unix/sysv/linux/syscalls.list
--- sysdeps.orig/unix/sysv/linux/syscalls.list	2003-09-08 08:42:40.000000000 +0200
+++ sysdeps/unix/sysv/linux/syscalls.list	2004-03-09 17:19:46.000000000 +0100
@@ -29,7 +29,7 @@ iopl		-	iopl		i:i	iopl
 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
diff -upr elf.orig/dl-load.c elf/dl-load.c
--- elf.orig/dl-load.c	2004-02-22 20:33:36.000000000 +0100
+++ elf/dl-load.c	2004-03-09 17:19:07.000000000 +0100
@@ -1148,6 +1148,11 @@ cannot allocate TLS data structures for 
 		== MAP_FAILED))
 	  goto map_error;
 
+	if (GLRO(dl_madvise)
+	    && c->mapend > c->mapstart)
+	  __madvise ((void *) (l->l_addr + c->mapstart),
+		     c->mapend - c->mapstart, MADV_WILLNEED);
+
       postmap:
 	if (l->l_phdr == 0
 	    && (ElfW(Off)) c->mapoff <= header->e_phoff
diff -upr elf.orig/dl-support.c elf/dl-support.c
--- elf.orig/dl-support.c	2004-01-28 07:53:55.000000000 +0100
+++ elf/dl-support.c	2004-03-09 17:47:53.000000000 +0100
@@ -41,6 +41,7 @@ size_t _dl_platformlen;
 
 int _dl_debug_mask;
 int _dl_lazy;
+int _dl_madvise;
 ElfW(Addr) _dl_use_load_bias = -2;
 int _dl_dynamic_weak;
 
@@ -243,6 +244,8 @@ _dl_non_dynamic_init (void)
 
   _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';
diff -upr elf.orig/rtld.c elf/rtld.c
--- elf.orig/rtld.c	2004-03-09 11:35:17.000000000 +0100
+++ elf/rtld.c	2004-03-09 17:47:24.000000000 +0100
@@ -122,6 +122,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,
@@ -2086,6 +2087,12 @@ process_envvars (enum mode *modep)
 	  break;
 
 	case 9:
+	  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)
diff -upr include.orig/sys/mman.h include/sys/mman.h
--- include.orig/sys/mman.h	1999-11-26 09:16:29.000000000 +0100
+++ include/sys/mman.h	2004-03-09 17:18:50.000000000 +0100
@@ -8,6 +8,7 @@ extern void *__mmap64 (void *__addr, siz
 		       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,


More information about the Kde-optimize mailing list