jemalloc is a userspace memory management library which avoids memory fragmentation, performs well in multithreaded programs, and offers profiling. It has been integrated in the libc on FreeBSD. Whenever you call malloc() or free() in your program on FreeBSD, you talk to functions provided by the jemalloc library. Unfortunatelly, jemalloc as part of the libc on FreeBSD is compiled without support for memory profiling. You can proof this by starting any program with given MALLOC_CONF environment variable, e.g. from csh:
% env MALLOC_CONF="prof:true" ls /foobar <jemalloc>: Invalid conf pair: prof:true ls: /foobar: No such file or directoryThus, we need to build a custom version of jemalloc.
% pkg install git gmake autoconf automake
% git clone https://github.com/jemalloc/jemalloc.git % cd jemalloc
% git checkout 5.3.0
JEMALLOC_CPREFIX"MALLOC_CONF" #else "MALLOC_CONF"Replace both strings "MALLOC_CONF" by "MALLOX_CONF", e.g.
JEMALLOC_CPREFIX"MALLOX_CONF" #else "MALLOX_CONF"This enables us to use the environment variable "MALLOX_CONF" to pass any option to our custom-built library only.
% sh autogen.sh --enable-prof --disable-cxx --prefix=/usr/local/jemalloc530If you plan to link any program written in C++ against this library, remove the --disable-cxx option given to autogen.sh.
% gmake % gmake install
% echo "libjemalloc.so.2 /usr/local/jemalloc530/lib/libjemalloc.so.2" > /usr/local/etc/libmap.d/jemalloc.confOf course, if you do not take care, libmap.conf allows you to render your operating system to an unusable state, which will require you to reboot in rescue mode most likely.
This section describes how to build BIND which uses our custom version of jemalloc. This is normally not needed on FreeBSD, since jemalloc is part of libc.
% cd /usr/ports/dns/bind920 % make configI chose just these options:
% make configure
% env PKG_CONFIG_PATH=/usr/local/jemalloc530/lib/pkgconfig ./configure --enable-dnsrps --localstatedir=/var --sysconfdir=/usr/local/etc/namedb --with-openssl=/usr --with-readline=libedit --disable-tracing --disable-dnstap --disable-fixed-rrset --disable-geoip --without-maxminddb --without-gssapi --with-libidn2=/usr/local --disable-largefile --without-lmdb --disable-querytrace --without-json-c --with-libxml2 --enable-tcp-fastopen --prefix=/usr/local --mandir=/usr/local/share/man --disable-silent-rules --infodir=/usr/local/share/info/ --build=amd64-portbld-freebsd14.3 --with-jemallocOf course, any other option given to the configure script should reflect the options you set when calling make config for the port.
% cd ../../ % make % make install clean
% env MALLOX_CONF="prof:true,prof_prefix=/var/tmp/named_prof" /usr/local/sbin/named -t -u bind -c /usr/local/etc/namedb/named.conf