How to Profile Databend
CPU profiling
go tool pprof -http="0.0.0.0:8081" http://localhost:8080/debug/pprof/profile?seconds=30
Open <your-ip>:8081 and select Flame Graph from the VIEW menus in the site header:
Query Level CPU Profiling
EXPLAIN PERF <statement> can also be used for CPU profiling, which focuses on a specific query and gathers CPU profiling data from other nodes to help analyze query performance. Check EXPLAIN PERF for more details.
Profile an Already Running Query
Use the Query Admin API to collect CPU samples after a slow query starts. You do not need to rerun the query with EXPLAIN PERF. Use a trusted admin connection. Do not expose the admin port to public networks. These endpoints are not on the SQL HTTP port.
- Open
/v1/processliston the Query Admin API address, such ashttp://127.0.0.1:8080. - Copy the running query's
current_query_id. - Open
/v1/queries/<query_id>/perfon the same address. - Start sampling in the viewer.
- Stop sampling when you have enough data.
The viewer refreshes the cumulative flamegraph every 30 seconds by default. Each cluster node has a separate flamegraph. You can also use the API directly:
ADMIN_URL='http://127.0.0.1:8080'
QUERY_ID='replace-with-current-query-id'
PERF_URL="$ADMIN_URL/v1/queries/$QUERY_ID/perf"
curl -sS -X POST "$PERF_URL/start?frequency=99&seconds=300"
curl -sS "$PERF_URL/status"
# Allow time to collect samples, then stop and download them.
curl -sS -X POST "$PERF_URL/stop"
curl -fS "$PERF_URL/flamegraph?format=html" -o query-flamegraph.html
Open query-flamegraph.html in a browser. To export folded stacks for other profiling tools, use GET .../flamegraph?format=folded.
| Target | Base path | Default sampling | Maximum duration |
|---|---|---|---|
| One running query | /v1/queries/<query_id>/perf | 99 Hz for 300 seconds | 3,600 seconds |
| All threads on all cluster nodes | /v1/perf | 49 Hz for 60 seconds | 600 seconds |
Both base paths support the viewer, POST /start, POST /stop, GET /status, and GET /flamegraph. Whole-process sampling can cost more than query sampling. Use a short interval first. Its overhead on a large Linux host has not been measured.
Interpret Results and Limits
- Check each entry in the response's
nodesobject. A200 OKresponse does not mean every node started sampling. Read each node'sstateandmessage. - If the query runs nowhere and no profile remains,
nodescan be empty. A missing query does not by itself cause404. The HTML flamegraph returns404when no samples exist. Folded output can be empty. - Each node permits one active CPU profiler. Query sampling, whole-process sampling,
EXPLAIN PERF, and/debug/pprof/profilecompete for it. Finish the active session before you start another. - Samples accumulate from the start. Stop and restart to inspect a recent interval. Restarting replaces the previous samples.
- Query fragments that start on a node after sampling begins are not included there. A short query can finish before enough samples are collected.
Memory profiling
databend-query and databend-meta can be built optionally with jemalloc,
which provides various memory profiling features.
Currently, it does not work on Mac, with either intel or Arm.
Enable memory profiling
- Build
databend-querywithmemory-profilingfeature enabled:
cargo build --bin databend-query --release --features memory-profiling
- Fire up
databend, using environment variableMALLOC_CONFto enable memory profiling:
MALLOC_CONF=prof:true,lg_prof_interval:30 ./target/release/databend-query
Generate heap profile
Generate a call graph in pdf illustrating memory allocation during this interval:
jeprof --pdf ./target/release/databend-query heap.prof > heap.pdf
Fast jeprof
jeprof is very slow for large heap analysis, the bottleneck is addr2line, if you want to speed up from 30 minutes to 3s, please use :
git clone https://github.com/gimli-rs/addr2line
cd addr2line
cargo b --examples -r
cp ./target/release/examples/addr2line <your-addr2line-find-with-whereis-addr2line>