ClickHouse uses jemalloc as its global allocator. Jemalloc comes with some tools for allocation sampling and profiling.Documentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
To make allocation profiling more convenient,
SYSTEM commands are provided along with four letter word (4LW) commands in Keeper.
Sampling allocations and flushing heap profiles
If you want to sample and profile allocations injemalloc, you need to start ClickHouse/Keeper with profiling enabled using the environment variable MALLOC_CONF:
jemalloc will sample allocations and store the information internally.
You can tell jemalloc to flush the current profile by running:
- ClickHouse
- Keeper
/tmp/jemalloc_clickhouse._pid_._seqnum_.heap where _pid_ is the PID of ClickHouse and _seqnum_ is the global sequence number for the current heap profile.For Keeper, the default file is
/tmp/jemalloc_keeper._pid_._seqnum_.heap, and follows the same rules.
A different location can be defined by appending the MALLOC_CONF environment variable with the prof_prefix option.For example, if you want to generate profiles in the
/data folder where the filename prefix will be my_current_profile, you can run ClickHouse/Keeper with the following environment variable:
Analyzing heap profiles
After heap profiles have been generated, they need to be analyzed.For that,
jemalloc’s tool called jeprof can be used. It can be installed in multiple ways:
- Using the system’s package manager
- Cloning the jemalloc repo and running
autogen.shfrom the root folder. This will provide you with thejeprofscript inside thebinfolder
jeprof uses addr2line to generate stacktraces which can be really slow.If that’s the case, it is recommended to install an alternative implementation of the tool.
jeprof.
It is recommended to run jeprof --help for information on the usage and the various options the tool provides.
In general, the jeprof command is used as:
base argument:
Examples
- if you want to generate a text file with each procedure written per line:
- if you want to generate a PDF file with a call-graph:
Generating a flame graph
jeprof allows you to generate collapsed stacks for building flame graphs.
You need to use the --collapsed argument:
flamegraph.pl:
Controlling allocation profiler during runtime
If ClickHouse/Keeper is started with the profiler enabled, additional commands for disabling/enabling allocation profiling during runtime are supported. Using those commands, it’s easier to profile only specific intervals. To disable the profiler:- ClickHouse
- Keeper
- ClickHouse
- Keeper
prof_active option which is enabled by default.For example, if you don’t want to sample allocations during startup but only after, you can enable the profiler. You can start ClickHouse/Keeper with the following environment variable:
Additional options for the profiler
jemalloc has many different options available, which are related to the profiler. They can be controlled by modifying the MALLOC_CONF environment variable.
For example, the interval between allocation samples can be controlled with lg_prof_sample.If you want to dump the heap profile every N bytes you can enable it using
lg_prof_interval.
It is recommended to check jemallocs reference page for a complete list of options.
Other resources
ClickHouse/Keeper exposejemalloc related metrics in many different ways.
System table asynchronous_metrics
System table jemalloc_bins
Contains information about memory allocations done via the jemalloc allocator in different size classes (bins) aggregated from all arenas.
Reference
Prometheus
Alljemalloc related metrics from asynchronous_metrics are also exposed using the Prometheus endpoint in both ClickHouse and Keeper.
Reference
jmst 4LW command in Keeper
Keeper supports the jmst 4LW command which returns basic allocator statistics: