All articles
Performance & memoryEngineering record

Database Kernel Optimization, Part 2: Optimizing CPU Usage

Start with the interval that actually saturates

CPU utilization is an interval measurement rather than a fixed-capacity number like memory. If process X runs on one core for 200 ms during a one-second interval, it used 20% of that core over the interval. On a host with 500 available CPU time slices, the runnable work cannot consume more than 500 slices unless the host is oversubscribed. It will not fail in the same way as an out-of-memory condition. For a complex system, CPU optimization therefore starts with the interval in which CPUs are actually busy; after the peak has passed, the useful evidence is gone. This differs from memory analysis, where an in-use snapshot can remain informative after the event.

This article follows an index-loading optimization in Milvus. Indexes must be loaded before they can serve search traffic. A slow load path delays recovery and can consume enough CPU for long enough to affect foreground queries.

Establish the CPU profile

On a 1 CU, 2-vCPU, 8-GiB AWS instance, loading 8 million 768-dimensional vectors took 59.9 seconds end to end, while CPU was nearly saturated for most of the run. I captured a perf profile to find where the CPU time went.

Perf tips:
1. Capturing perf inside a Kubernetes pod requires host permission for
   /proc/sys/kernel/perf_event_paranoid. Its default value is 2; use -1.
2. Install perf with: sudo apt install -y linux-tools-common linux-tools-generic
3. A basic CPU capture command: perf record -F 99 -p [pid] -g -- sleep 20
4. FlameGraph: https://github.com/brendangregg/FlameGraph.git

Figure 1View full-size image

The flame graph attributed 10.42% of CPU time to ZSTD decompression, 30.73% to Arrow batch reads, and 3.77% to Parquet batch reads. One stack, arrow::util::RleDecoder::GetBatchWithDict<int>, warranted a closer look.

Remove a redundant encoding path

The index shard was written into an arrow::Int8Builder, which meant it had to be deserialized as an INT8 Arrow array when loading. The same Arrow array was also compressed with ZSTD-3, explaining the decompression cost in the flame graph.

Figure 2View full-size image

That representation was unnecessarily expensive. An index shard was already a serialized, sliced binary payload; encoding it again as INT8 values and compressing it did not add useful meaning, but it did add decoding and decompression work.

Figure 3View full-size image

After removing the redundant encoding and compression, the same 8-million-vector load fell from 59.9 seconds to 44.5 seconds. An isolated C++ deserialization test over 100 iterations measured 4,759 ms without the encoding and 14,931 ms with it.

Follow the remaining cost to page faults

The 4,759 ms result was still surprising: without the encoding or ZSTD compression, deserialization itself should have been close to negligible. A perf snapshot of the C++ unit test showed many exc_page_fault events below __memmove_avx_unaligned_erm.

Figure 4View full-size image

The load path did not retain the downloaded buffer. Instead, it allocated a new uint8_t[] and copied the data into it. Because new and malloc allocate virtual memory, a single index shard as large as 16 MiB can fault in 4,096 pages during the copy. Removing the unnecessary allocation and memcpy reduced the 100-iteration test to 1 ms without encoding, versus 14,855 ms with it. The 8-million-vector end-to-end load then fell from 44.5 seconds to 38 seconds.

For page-fault sources that are not apparent from a code review, collect the relevant counters and stack traces directly:

perf stat -e page-faults,minor-faults,major-faults ./your_program

The subsequent load profile no longer showed large Parquet, Arrow, ZSTD, or __memmove stacks.

Figure 5View full-size image

A remaining SDK cost

The next profile still showed substantial AWS SDK C++ work: MemRelease accounted for 12% and copy operations for 29%.

Figure 6View full-size image

Reading the AWS SDK source traced the copying to two operations. One appends each received frame to the response body:

Figure 7View full-size image

The other writes the completed response into the result stream:

Figure 8View full-size image

Both are hard-coded in the SDK and cannot be bypassed through its public interface. The issue was raised in a public discussion: aws/aws-sdk-cpp#3384.

Takeaway

The useful loop for CPU optimization is straightforward: capture a profile while the workload is CPU-bound, trace the expensive stack to its data representation or memory behavior, make one focused change, and measure the same workload again. In this case, removing redundant serialization work and a needless memory copy reduced load time from 59.9 seconds to 38 seconds.

— MrPresent-Han