Re: FreeBSD malloc.c, -lmalloc, and squid.

From: Poul-Henning Kamp <phk@dont-contact.us>
Date: Tue, 27 Aug 1996 20:42:47 +0200

Here, try these changes, and see if it makes a difference when
you set 'H' or one or more '<' or '>' in the options.

You can do it system wide:
        ln -s 'H<<' /etc/default/malloc
or per process:
        setenv MALLOC_OPTIONS 'H<<'

Feedback most welcome!

Poul-Henning

Index: malloc.3
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdlib/malloc.3,v
retrieving revision 1.5
diff -u -r1.5 malloc.3
--- malloc.3 1996/02/11 22:34:02 1.5
+++ malloc.3 1996/08/27 18:37:24
@@ -34,10 +34,11 @@
 .\" SUCH DAMAGE.
 .\"
 .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
+.\" $Id$
 .\"
-.Dd June 4, 1993
+.Dd August 27, 1996
 .Dt MALLOC 3
-.Os BSD 4
+.Os FreeBSD 2
 .Sh NAME
 .Nm malloc ,
 .Nd general memory allocation function
@@ -112,9 +113,11 @@
 .Pp
 
 .Sh ENVIRONMENT
-This malloc will check the environment for a variable called
-.Em MALLOC_OPTIONS
-and scan it for flags.
+Malloc will first look for a symbolic link called
+.Pa /etc/default/malloc
+and next check the environment for a variable called
+.Ev MALLOC_OPTIONS
+and scan them for flags in that order.
 Flags are single letters, uppercase means on, lowercase means off.
 .Bl -tag -width indent
 .It A
@@ -130,6 +133,10 @@
 ``junk'' fill some junk into the area allocated.
 Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-)
 
+.It H
+``hint'' pass a hint to the kernel about pages we don't use. If the
+machine is paging a lot this may help a bit.
+
 .It R
 ``realloc'' always reallocate when
 .Fn realloc
@@ -140,11 +147,22 @@
 ``zero'' fill some junk into the area allocated (see ``J''),
 except for the exact length the user asked for, which is zeroed.
 
+.It <
+``Half the cache size'' Reduce the size of the cache by a factor of two.
+
+.It >
+``Double the cache size'' Double the size of the cache by a factor of two.
 .El
 .Pp
+So to set a systemwide reduction of cache size and coredumps on problems
+one would:
+.Li ln -s 'A<' /etc/default/malloc
+.Pp
 The ``J'' and ``Z'' is mostly for testing and debugging,
 if a program changes behavior if either of these options are used,
 it is buggy.
+.Pp
+The default cache size is 32 pages.
 .Sh RETURN VALUES
 The
 .Fn malloc
@@ -166,6 +184,7 @@
 .Xr calloc 3 ,
 .Xr getpagesize 3 ,
 .Xr memory 3
+.Pa /usr/share/doc/papers/malloc.ascii.gz
 .Sh STANDARDS
 The
 .Fn malloc
Index: malloc.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.11
diff -u -r1.11 malloc.c
--- malloc.c 1996/07/03 05:03:07 1.11
+++ malloc.c 1996/08/27 18:25:10
@@ -188,9 +188,7 @@
 /*
  * The minimum size (in bytes) of the free page cache.
  */
-#ifndef malloc_cache
-static unsigned malloc_cache;
-#endif /* malloc_cache */
+static unsigned malloc_cache = 32 << malloc_pageshift;
 
 /*
  * The offset from pagenumber to index into the page directory
@@ -241,6 +239,11 @@
 static int malloc_realloc;
 
 /*
+ * pass the kernel a hint on free pages ?
+ */
+static int malloc_hint;
+
+/*
  * zero fill ?
  */
 static int malloc_zero;
@@ -496,30 +499,46 @@
 static void
 malloc_init ()
 {
- char *p;
+ char *p, b[64];
+ int i,j;
 
 #ifdef EXTRA_SANITY
     malloc_junk = 1;
 #endif /* EXTRA_SANITY */
 
- for (p=getenv("MALLOC_OPTIONS"); p && *p; p++) {
- switch (*p) {
- case 'a': malloc_abort = 0; break;
- case 'A': malloc_abort = 1; break;
+ for (i = 0; i < 2; i++) {
+ if (i == 0) {
+ j = readlink("/etc/default/malloc", b, sizeof b - 1);
+ if (j <= 0)
+ continue;
+ b[j] = '\0';
+ p = b;
+ } else if (i == 1) {
+ p = getenv("MALLOC_OPTIONS");
+ }
+ for (; p && *p; p++) {
+ switch (*p) {
+ case '>': malloc_cache <<= 1; break;
+ case '<': malloc_cache >>= 1; break;
+ case 'a': malloc_abort = 0; break;
+ case 'A': malloc_abort = 1; break;
 #ifdef MALLOC_STATS
- case 'd': malloc_stats = 0; break;
- case 'D': malloc_stats = 1; break;
+ case 'd': malloc_stats = 0; break;
+ case 'D': malloc_stats = 1; break;
 #endif /* MALLOC_STATS */
- case 'r': malloc_realloc = 0; break;
- case 'R': malloc_realloc = 1; break;
- case 'j': malloc_junk = 0; break;
- case 'J': malloc_junk = 1; break;
- case 'z': malloc_zero = 0; break;
- case 'Z': malloc_zero = 1; break;
- default:
- wrtwarning("(Init): Unknown char in MALLOC_OPTIONS\n");
- p = 0;
- break;
+ case 'h': malloc_hint = 0; break;
+ case 'H': malloc_hint = 1; break;
+ case 'r': malloc_realloc = 0; break;
+ case 'R': malloc_realloc = 1; break;
+ case 'j': malloc_junk = 0; break;
+ case 'J': malloc_junk = 1; break;
+ case 'z': malloc_zero = 0; break;
+ case 'Z': malloc_zero = 1; break;
+ default:
+ wrtwarning("(Init): Unknown char in MALLOC_OPTIONS\n");
+ p = 0;
+ break;
+ }
         }
     }
 
@@ -553,10 +572,6 @@
     }
 #endif /* malloc_pageshift */
 
-#ifndef malloc_cache
- malloc_cache = 100 << malloc_pageshift;
-#endif /* malloc_cache */
-
 #ifndef malloc_minsize
     {
     int i;
@@ -967,6 +982,9 @@
         page_dir[index + i] = MALLOC_FREE;
 
     l = i << malloc_pageshift;
+
+ if (malloc_hint)
+ madvise(ptr, l, MADV_FREE);
 
     tail = ptr+l;
 

--
Poul-Henning Kamp           | phk@FreeBSD.ORG       FreeBSD Core-team.
http://www.freebsd.org/~phk | phk@login.dknet.dk    Private mailbox.
whois: [PHK]                | phk@ref.tfs.com       TRW Financial Systems, Inc.
Future will arrive by its own means, progress not so.
Received on Tue Aug 27 1996 - 11:45:36 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:32:52 MST