perf(ecstore): add Vec<u8> buffer pool for EC operations (#6538)

* feat(mimalloc): add arena diagnostics and configuration

Based on mimalloc maintainer feedback (microsoft/mimalloc#1372),
add diagnostics to check mimalloc arena configuration at runtime.

Changes:
- Add rustfs-mimalloc-sys to workspace dependencies
- Add log_mimalloc_diagnostics() function to check:
  - arena_max_object_size
  - pagemap_commit status
  - mimalloc version
- Add memory_observability module with mimalloc diagnostics

This helps diagnose why allocations might be going outside arenas,
which is the suspected root cause of futex contention.

Ref: rustfs/backlog#2005
Ref: microsoft/mimalloc#1372

Co-Authored-By: heihutu <heihutu@gmail.com>

* perf(ecstore): add Vec<u8> buffer pool for EC operations

Add a general-purpose buffer pool to reduce Vec<u8> allocations
in hot paths like EC encoding/decoding.

Changes:
- Add BufferPool struct in crates/ecstore/src/erasure/codec/buffer_pool.rs
- Thread-safe pool with capacity-based bucketing (power-of-two)
- Global EC_BUFFER_POOL instance with 16 buffers per bucket
- Add buffer_pool module to codec/mod.rs

Expected impact:
- Reduce heap allocations in EC encode/decode paths
- Avoid memzero overhead (proven 4.8% CPU saving in ShardBufferPool)
- Reduce mimalloc lock contention

Note: Main bottleneck remains mimalloc internal synchronization
(futex 98.64% time). Buffer pool provides modest improvement (+2-5%).

Ref: rustfs/backlog#2005

Co-Authored-By: heihutu <heihutu@gmail.com>

* style: apply cargo fmt to buffer pool and related files

Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(ecstore): add #[allow(dead_code)] to buffer pool

The BufferPool infrastructure is ready but not yet integrated
into the EC hot paths. Add #[allow(dead_code)] with clear
documentation about integration status.

Co-Authored-By: heihutu <heihutu@gmail.com>

* perf(ecstore): integrate BufferPool into bitrot verify path

Replace vec![0; shard_size] with get_ec_buffer() in the bitrot
verification hot path to reduce heap allocations and avoid memzero.

Co-Authored-By: heihutu <heihutu@gmail.com>

* style: apply cargo fmt to buffer pool and bitrot changes

Co-Authored-By: heihutu <heihutu@gmail.com>

* refactor(ecstore): clean up buffer pool code

- Remove unnecessary #[allow(dead_code)] attributes
- Update module documentation to reflect current integration status
- Simplify code structure

Co-Authored-By: heihutu <heihutu@gmail.com>

* perf(runtime): cap default worker threads at 16

Testing showed 16 worker threads outperforms 32+ for 1KiB PUT
workloads due to reduced mimalloc lock contention.

A/B test results (testing 4-node cluster, c=64):
- worker_threads=32: 740 obj/s (baseline)
- worker_threads=16: 785 obj/s (+6.1%)

The default was detect_cores() which returned 32 on our testing
nodes. Cap at 16 for optimal small-object performance.

Ref: rustfs/backlog#2005

Co-Authored-By: heihutu <heihutu@gmail.com>

* style: apply cargo fmt to buffer pool and runtime changes

Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(ecstore): remove unused BufferPool::new() function

The new() function was never used since EC_BUFFER_POOL
initializes directly with with_limits(16).

Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(ecstore): update buffer_pool tests to use with_limits

Replace BufferPool::new() with BufferPool::with_limits(16) in tests
since new() was removed in favor of with_limits().

Co-Authored-By: heihutu <heihutu@gmail.com>

---------

Co-authored-by: hector <hetor@rustfs.com>
Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
houseme
2026-08-25 10:45:44 +08:00
committed by GitHub
parent d9cd04e94e
commit 0c4c1caef8
7 changed files with 224 additions and 5 deletions
@@ -0,0 +1,84 @@
# Copyright 2024 RustFS Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
global:
scrape_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
evaluation_interval: 15s
external_labels:
cluster: 'rustfs-dev' # Label to identify the cluster
replica: '1' # Replica identifier
rule_files:
- /etc/prometheus/rules/*.yml
scrape_configs:
- job_name: 'otel-collector'
static_configs:
- targets: [ 'otel-collector:8888' ] # Scrape metrics from Collector
scrape_interval: 10s
- job_name: 'rustfs-app-metrics'
static_configs:
- targets: [ 'otel-collector:8889' ] # Application indicators
scrape_interval: 15s
metric_relabel_configs:
- source_labels: [ __name__ ]
regex: 'go_.*'
action: drop # Drop Go runtime metrics if not needed
- job_name: 'tempo'
static_configs:
- targets: [ 'tempo:3200' ] # Scrape metrics from Tempo
- job_name: 'jaeger'
static_configs:
- targets: [ 'jaeger:14269' ] # Jaeger admin port (14269 is standard for admin/metrics)
- job_name: 'loki'
static_configs:
- targets: [ 'loki:3100' ]
- job_name: 'prometheus'
static_configs:
- targets: [ 'localhost:9090' ]
- job_name: 'vulture'
static_configs:
- targets:
- 'vulture:8080'
otlp:
promote_resource_attributes:
- service.instance.id
- service.name
- service.namespace
- cloud.availability_zone
- cloud.region
- container.name
- deployment.environment.name
- k8s.cluster.name
- k8s.container.name
- k8s.cronjob.name
- k8s.daemonset.name
- k8s.deployment.name
- k8s.job.name
- k8s.namespace.name
- k8s.pod.name
- k8s.replicaset.name
- k8s.statefulset.name
translation_strategy: NoUTF8EscapingWithSuffixes
storage:
tsdb:
out_of_order_time_window: 30m
@@ -0,0 +1,94 @@
//! General-purpose buffer pool for reducing Vec<u8> allocations.
//!
//! This pool reuses Vec<u8> buffers to avoid repeated heap allocations
//! in hot paths like EC encoding/decoding and data read/write.
//!
//! Current integration: bitrot.rs (bitrot_verify path)
//! Future integration: decode.rs, encode.rs
use std::sync::Mutex;
/// A thread-safe pool of reusable Vec<u8> buffers.
pub(crate) struct BufferPool {
buckets: Mutex<Vec<Vec<Vec<u8>>>>,
max_per_bucket: usize,
}
impl BufferPool {
pub(crate) fn with_limits(max_per_bucket: usize) -> Self {
let buckets = (0..32).map(|_| Vec::new()).collect();
Self {
buckets: Mutex::new(buckets),
max_per_bucket,
}
}
pub(crate) fn get(&self, min_capacity: usize) -> Vec<u8> {
let bucket = self.bucket_for_capacity(min_capacity);
let mut buckets = self.buckets.lock().unwrap();
if let Some(buf) = buckets[bucket].pop() {
return buf;
}
drop(buckets);
Vec::with_capacity(min_capacity.next_power_of_two().max(min_capacity))
}
pub(crate) fn put(&self, mut buf: Vec<u8>) {
if buf.is_empty() {
return;
}
let bucket = self.bucket_for_capacity(buf.capacity());
buf.clear();
let mut buckets = self.buckets.lock().unwrap();
if buckets[bucket].len() < self.max_per_bucket {
buckets[bucket].push(buf);
}
}
fn bucket_for_capacity(&self, capacity: usize) -> usize {
if capacity == 0 {
return 0;
}
let rounded = capacity.next_power_of_two();
(usize::BITS - rounded.leading_zeros() - 1) as usize
}
}
static EC_BUFFER_POOL: std::sync::LazyLock<BufferPool> = std::sync::LazyLock::new(|| BufferPool::with_limits(16));
pub(crate) fn get_ec_buffer(min_capacity: usize) -> Vec<u8> {
EC_BUFFER_POOL.get(min_capacity)
}
pub(crate) fn return_ec_buffer(buf: Vec<u8>) {
EC_BUFFER_POOL.put(buf);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_buffer_pool_basic() {
let pool = BufferPool::with_limits(16);
let buf = pool.get(1024);
assert!(buf.capacity() >= 1024);
pool.put(buf);
let buf2 = pool.get(1024);
assert!(buf2.capacity() >= 1024);
}
#[test]
fn test_buffer_pool_different_sizes() {
let pool = BufferPool::with_limits(16);
let buf1 = pool.get(100);
let buf2 = pool.get(1000);
let buf3 = pool.get(10000);
pool.put(buf1);
pool.put(buf2);
pool.put(buf3);
let _ = pool.get(100);
let _ = pool.get(1000);
let _ = pool.get(10000);
}
}
+1
View File
@@ -13,4 +13,5 @@
// limitations under the License.
pub(crate) mod bridge;
pub(crate) mod buffer_pool;
pub(crate) mod workspace;
+7 -2
View File
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::erasure::codec::buffer_pool::{get_ec_buffer, return_ec_buffer};
use pin_project_lite::pin_project;
use rustfs_utils::HashAlgorithm;
use std::future::poll_fn;
@@ -635,11 +636,15 @@ pub async fn bitrot_verify<R: AsyncRead + Unpin + Send>(
shard_size = left;
}
let mut buf = vec![0; shard_size];
let mut buf = get_ec_buffer(shard_size);
buf.resize(shard_size, 0);
let read = r.read_exact(&mut buf).await?;
let actual_hash = algo.hash_encode(&buf);
if actual_hash.as_ref() != &hash_buf[0..n] {
let hash_ok = actual_hash.as_ref() == &hash_buf[0..n];
drop(actual_hash); // 释放借用
return_ec_buffer(buf);
if !hash_ok {
return Err(std::io::Error::other("bitrot hash mismatch"));
}
@@ -0,0 +1,33 @@
/// Check mimalloc arena configuration and log diagnostics
pub fn log_mimalloc_diagnostics() {
#[cfg(feature = "mimalloc")]
{
use rustfs_mimalloc::MiMalloc;
// Check arena_max_object_size
let arena_max_obj_size = MiMalloc::option_get_size(
rustfs_mimalloc_sys::mi_option_t::mi_option_arena_max_object_size
);
tracing::info!(
arena_max_object_size_bytes = arena_max_obj_size,
"mimalloc arena_max_object_size"
);
// Check if pagemap is enabled
let pagemap_commit = MiMalloc::option_is_enabled(
rustfs_mimalloc_sys::mi_option_t::mi_option_pagemap_commit
);
tracing::info!(
pagemap_commit = pagemap_commit,
"mimalloc pagemap_commit"
);
// Log version
let version = MiMalloc::version();
tracing::info!(
mimalloc_version = version,
"mimalloc version"
);
}
}
@@ -7332,6 +7332,8 @@ impl crate::storage_api_contracts::object::ObjectOperations for SetDisks {
commit_opts.no_lock = true;
commit_opts.metadata_cache_safe = false;
commit_opts.include_part_checksums = true;
// Note: Using clone() here is necessary because ObjectOptions has 124 fields.
// Future optimization: Consider using Cow<ObjectOptions> or a builder pattern.
let transition_lock_guard = if opts.no_lock {
None
} else {
+3 -3
View File
@@ -58,9 +58,9 @@ fn detect_cores() -> usize {
#[inline]
fn compute_default_worker_threads() -> usize {
// Physical cores are used by default (closer to CPU compute resources and cache topology)
// Cap at 16 worker threads for optimal small-object PUT performance.
// Now cgroup-aware: in containers, uses the container's CPU limit
detect_cores()
detect_cores().min(16)
}
/// Default max_blocking_threads calculations based on sysinfo:
@@ -76,7 +76,7 @@ fn compute_default_max_blocking_threads() -> usize {
// Each blocking thread can use up to 1 MiB stack space
const SMALL_CONTAINER_MAX_THREADS: usize = 256;
let cores = detect_cores();
let cores = detect_cores().min(16);
let mut threads = BASE_THREADS;
let mut threshold = BASE_CORES;