Commit 93a49594 authored by Tollef Fog Heen's avatar Tollef Fog Heen

Import jemalloc


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@3215 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 38368054
CFLAGS := -O3 -g
# See source code comments to avoid memory leaks when enabling MALLOC_MAG.
#CPPFLAGS := -DMALLOC_PRODUCTION -DMALLOC_MAG
CPPFLAGS := -DMALLOC_PRODUCTION
all: libjemalloc.so.0 libjemalloc_mt.so.0
jemalloc_linux_mt.o: jemalloc_linux.c
gcc $(CFLAGS) -c -DPIC -fPIC $(CPPFLAGS) -D__isthreaded=true -o $@ $+
jemalloc_linux.o: jemalloc_linux.c
gcc $(CFLAGS) -c -DPIC -fPIC $(CPPFLAGS) -D__isthreaded=false -o $@ $+
libjemalloc_mt.so.0: jemalloc_linux_mt.o
gcc -shared -lpthread -o $@ $+
ln -sf $@ libjemalloc_mt.so
libjemalloc.so.0: jemalloc_linux.o
gcc -shared -lpthread -o $@ $+
ln -sf $@ libjemalloc.so
clean:
rm -f *.o *.so.0 *.so
This is a minimal-effort stand-alone jemalloc distribution for Linux. The main
rough spots are:
* __isthreaded must be hard-coded, since the pthreads library really needs to
be involved in order to toggle it at run time. Therefore, this distribution
builds two separate libraries:
+ libjemalloc_mt.so.0 : Use for multi-threaded applications.
+ libjemalloc.so.0 : Use for single-threaded applications.
Both libraries link against libpthread, though with a bit more code hacking,
this dependency could be removed for the single-threaded version.
* MALLOC_MAG (thread-specific caching, using magazines) is disabled, because
special effort is required to avoid memory leaks when it is enabled. To make
cleanup automatic, we would need help from the pthreads library. If you
enable MALLOC_MAG, be sure to call _malloc_thread_cleanup() in each thread
just before it exits.
* The code that determines the number of CPUs is sketchy. The trouble is that
we must avoid any memory allocation during early initialization.
In order to build:
make
This generates two shared libraries, which you can either link against, or
pre-load.
Linking and running, where /path/to is the path to libjemalloc (-lpthread
required even for libjemalloc.so):
gcc app.o -o app -L/path/to -ljemalloc_mt -lpthread
LD_LIBRARY_PATH=/path/to app
Pre-loading:
LD_PRELOAD=/path/to/libjemalloc_mt.so.0 app
jemalloc has a lot of run-time tuning options. See the man page for details:
nroff -man malloc.3 | less
In particular, take a look at the B, F, and N options. If you enable
MALLOC_MAG, look at the G and R options.
If your application is crashing, or performance seems to be lacking, enable
assertions and statistics gathering by removing MALLOC_PRODUCTION from CPPFLAGS
in the Makefile. In order to print a statistics summary at program exit, run
your application like:
LD_PRELOAD=/path/to/libjemalloc_mt.so.0 MALLOC_OPTIONS=P app
Please contact Jason Evans <jasone@canonware.com> with questions, comments, bug
reports, etc.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment