MariaDB and jemalloc on CentOS

No Comments

I felt like trying out jemalloc, an alternative malloc(3) implementation, which is promising improved performance for both, MySQL and MariaDB.

Being reluctant to compile MariaDB (which is quite time-consuming), I were looking for a way to facilitate jemalloc via RPM only. Luckily, jemalloc is available from the EPEL repository, so the compiled .so has only been a yum install jemalloc away. Now here's the hard part: How to get the MariaDB mysqld to actually load jemalloc? In the Linux world, this is usually accomplished through the LD_PRELOAD environmental variable. But with a service started through init.d? Hm … No luck finding a file in /etc/sysconfig that were of any help. A somewhat heavy-hitting solution were to add libjemalloc.so.1 to /etc/ld.so.preload. But: This would make every process start with jemalloc, which comes with quite a big memory penalty. In addition to that, only multi-threaded processes are truely benefiting from jemalloc, so this is no good solution.

So here's the trick: /usr/bin/mysqld_safe is actually a configurable shellscript, bootstrapping mysqld. Pretty close to the start, there is a variable manipulating LD_PRELOAD, called mysqld_ld_preload. Looking for this variable and changing it to the following did the trick:

mysqld_ld_preload=/usr/lib64/libjemalloc.so.1

After restarting MariaDB, running pmap `pidof mysqld` | grep jemalloc indeed confirmed the process were using jemalloc now.

This method should also work with tcmalloc, which is being favoured by Facebook and GitHub.

Be the first to write a comment!