14 - jemalloc

最後更新: 2019-04-03

 

介紹

jemalloc 它的功能是 malloc

Homepage: http://jemalloc.net/

比 Linux Default 的 glibc 優點:

 * fragmentation avoidance(躲避)
 * scalable concurrency
 * heap profiling
 * Valgrind integration ( Valgrind is an instrumentation framework for building dynamic analysis tools )
 * monitoring / tuning hooks

Version

V3.X Last: https://github.com/jemalloc/jemalloc/releases/download/3.6.0/jemalloc-3....

V4.0 Last: https://github.com/jemalloc/jemalloc/releases/download/4.0.4/jemalloc-4....

目錄

  • jemalloc module
  • tengine with jemalloc

 


jemalloc module

 

DL Source:

cd /usr/src

wget https://github.com/jemalloc/jemalloc/archive/refs/heads/stable-4.zip -O jemalloc-4.zip

unzip jemalloc-4.zip

cd jemalloc-stable-4

Complie:

# 如果沒有 "./configure" 的話, 就要行一次 ./autogen.sh

# 建議用 ./configure --prefix /opt/jemalloc

./configure

...
install_suffix     :
malloc_conf        :
autogen            : 0
cc-silence         : 1
debug              : 0
code-coverage      : 0
stats              : 1
prof               : 0
prof-libunwind     : 0
prof-libgcc        : 0
prof-gcc           : 0
tcache             : 1
thp                : 1
fill               : 1
utrace             : 0
valgrind           : 1
xmalloc            : 0
munmap             : 0
lazy_lock          : 0
tls                : 1
cache-oblivious    : 1

--with-jemalloc-prefix=<prefix>

Prefix all public APIs with <prefix>. By default, the prefix is ""

For example: malloc()  --> prefix_malloc()

make -j

make install

/usr/bin/install -c -d /opt/jemalloc/bin
/usr/bin/install -c -m 755 bin/jemalloc-config /opt/jemalloc/bin
/usr/bin/install -c -m 755 bin/jemalloc.sh /opt/jemalloc/bin
/usr/bin/install -c -m 755 bin/jeprof /opt/jemalloc/bin
/usr/bin/install -c -d /opt/jemalloc/include/jemalloc
/usr/bin/install -c -m 644 include/jemalloc/jemalloc.h /opt/jemalloc/include/jemalloc
/usr/bin/install -c -d /opt/jemalloc/lib
/usr/bin/install -c -m 755 lib/libjemalloc.so.2 /opt/jemalloc/lib
ln -sf libjemalloc.so.2 /opt/jemalloc/lib/libjemalloc.so
/usr/bin/install -c -d /opt/jemalloc/lib
/usr/bin/install -c -m 755 lib/libjemalloc.a /opt/jemalloc/lib
/usr/bin/install -c -m 755 lib/libjemalloc_pic.a /opt/jemalloc/lib
/usr/bin/install -c -d /opt/jemalloc/lib/pkgconfig
/usr/bin/install -c -m 644 jemalloc.pc /opt/jemalloc/lib/pkgconfig
/usr/bin/install -c -d /opt/jemalloc/share/doc/jemalloc
/usr/bin/install -c -m 644 doc/jemalloc.html /opt/jemalloc/share/doc/jemalloc
/usr/bin/install: cannot stat 'doc/jemalloc.html': No such file or directory
make: *** [Makefile:372: install_doc_html] Error 1

Configure Library

echo "/opt/jemalloc/lib" > /etc/ld.so.conf.d/jemalloc.conf

ldconfig

Checking

ldconfig -v | grep jemalloc

/opt/jemalloc/lib: (from /etc/ld.so.conf.d/jemalloc.conf:1)
        libjemalloc.so.2 -> libjemalloc.so.2

Remark

# V3

/opt/jemalloc/lib: (from /etc/ld.so.conf.d/jemalloc.conf:1)
        libjemalloc.so.1 -> libjemalloc.so.1

 



tengine with jemalloc

 

Remark

tengine-2.3.3(nginx/1.18.0) support jemalloc-4 !!

安裝

cd /usr/src/tengine

./configure --with-jemalloc --with-jemalloc=/usr/src/tengine/jemalloc-stable-4 ...

Output

...
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library
  + using system jemalloc library
...

Remark

--with-ld-opt="-ljemalloc"

checking for --with-ld-opt="-ljemalloc" ... not found
./configure: error: the invalid value in --with-ld-opt="-ljemalloc"

--with-ld-opt="-L /opt/jemalloc/lib -ljemalloc"

make -j

Checking:

strings objs/nginx | grep jemalloc

libjemalloc.so.2
configure arguments: --prefix=/opt/tengine ...

OR

ldd objs/nginx | grep jemalloc

libjemalloc.so.2 => /opt/jemalloc/lib/libjemalloc.so.2 (0x00007fad7a6c8000)

Or

lsof -u tengine | grep jemalloc

nginx   124180 tengine  mem       REG                8,2  3851304   493200 /opt/jemalloc/lib/libjemalloc.so.2
nginx   124181 tengine  mem       REG                8,2  3851304   493200 /opt/jemalloc/lib/libjemalloc.so.2
...

Other Info.

 

Google's tcmalloc:

https://github.com/gperftools/gperftools

Scalable memory allocation using jemalloc:

http://www.facebook.com/notes/facebook-engineering/scalable-memory-allocation-using-jemalloc/480222803919

 


DOC

man jemalloc

 

Creative Commons license icon Creative Commons license icon