[Mimedefang] [PATCH] FreeBSD 6.2 libpthread patch with jemalloc support for clamd

Martin Blapp mb at imp.ch
Tue Mar 13 15:36:47 EDT 2007


Hi all,

After the ugly odyssey here are finally the steps to make clamd working on 
FreeBSD without to burn a lot of CPU cycles and without the ugly lock 
contention syndroms with libpthread:

1.) Compile jemalloc.so
-----------------------

Get tree.h and malloc.c from CURRENT:

# fetch http://antispam.imp.ch/files/tree.h
# fetch http://antispam.imp.ch/files/malloc.c

# gcc -O -Wall -I/usr/src/lib/libc/include -shared \
   -o /usr/local/lib/jemalloc.so malloc.c

# rm tree.h malloc.c


2.) Then make a copy of libpthread:
-----------------------------------

cp /usr/src/lib/libpthread /usr/src/lib/libpthread-jemalloc

Then apply the patch below to /usr/src/lib/libpthread-jemalloc
and install it:

cd /usr/src/lib/libpthread-jemalloc
make && make install

mv /lib/libpthread-jemalloc.so.2 /usr/local/lib

rm -rfd /usr/src/lib/libpthread-jemalloc


3.) libmap.conf
---------------

And add this to /etc/libmap.conf (To be safe I've included
both libthr and libpthread)

[/usr/local/sbin/clamd]
libthr.so.2             libpthread-jemalloc.so.2
libthr.so               libpthread-jemalloc.so.2
libpthread.so.2		libpthread-jemalloc.so.2
libpthread.so		libpthread-jemalloc.so.2


4.) Preload and start
---------------------

Then preload the lib and start clamd ...

LD_PRELOAD=/usr/local/lib/jemalloc.so
/usr/local/sbin/clamd


5.) Enjoy :-)
-------------

Maybe we will add this procedure to the clamav port ...


--
Martin

----------------------------------------------------------------

--- libpthread-jemalloc/Makefile     Fri Sep 29 09:04:17 2006
+++ libpthread-jemalloc/Makefile Tue Mar 13 19:00:48 2007
@@ -10,7 +10,7 @@
  .if ${MACHINE_ARCH} == "alpha" || ${MACHINE_ARCH} == "sparc64"
  LIB=kse
  .else
-LIB=pthread
+LIB=pthread-jemalloc
  SHLIBDIR?= /lib
  .endif
  SHLIB_MAJOR= 2
--- lib/libpthread-jemalloc/thread/thr_fork.c    Sat Dec 18 19:07:37 2004
+++ lib/libpthread-jemalloc/thread/thr_fork.c    Tue Mar 13 18:24:42 2007
@@ -91,7 +91,7 @@

         /* Fork a new process: */
         if (_kse_isthreaded() != 0) {
-               _spinlock(__malloc_lock);
+               _malloc_prefork();
         }
         if ((ret = __sys_fork()) == 0) {
                 /* Child process */
@@ -107,8 +107,8 @@
                 }
                 _thr_mutex_reinit(&_thr_atfork_mutex);
         } else {
-               if ((_kse_isthreaded() != 0) && (__malloc_lock != NULL)) {
-                       _spinunlock(__malloc_lock);
+               if (_kse_isthreaded() != 0)  {
+                       _malloc_postfork();
                 }
                 errsave = errno;
                 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
--- lib/libpthread-jemalloc/thread/thr_spinlock.c        Sat Dec 18 19:07:37 
2004
+++ lib/libpthread-jemalloc/thread/thr_spinlock.c        Tue Mar 13 18:27:59 
2007
@@ -40,7 +40,7 @@
  #include "spinlock.h"
  #include "thr_private.h"

-#define        MAX_SPINLOCKS   5
+#define MAX_SPINLOCKS   72

  struct spinlock_extra {
         spinlock_t      *owner;



More information about the MIMEDefang mailing list