Skip to content

Commit

Permalink
jemalloc: Enable background thread to reduce tail latency (#16780) (#…
Browse files Browse the repository at this point in the history
…17007)

ref #15927

Per jemalloc tuning guide, enable background thread of jemalloc to improve tail latency

Signed-off-by: ti-chi-bot <[email protected]>

Co-authored-by: Connor <[email protected]>
  • Loading branch information
ti-chi-bot and Connor1996 committed May 15, 2024
1 parent 0e49bf9 commit 53048f9
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions components/tikv_alloc/src/jemalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ mod profiling {
const OPT_PROF: &[u8] = b"opt.prof\0";
const ARENAS_CREATE: &[u8] = b"arenas.create\0";
const THREAD_ARENA: &[u8] = b"thread.arena\0";
const BACKGROUND_THREAD: &[u8] = b"background_thread\0";

pub fn set_thread_exclusive_arena(enable: bool) {
ENABLE_THREAD_EXCLUSIVE_ARENA.store(enable, Ordering::Relaxed);
Expand Down Expand Up @@ -387,6 +388,18 @@ mod profiling {
Ok(())
}

fn enable_background_thread() -> ProfResult<()> {
unsafe {
if let Err(e) = tikv_jemalloc_ctl::raw::write(BACKGROUND_THREAD, true) {
return Err(ProfError::JemallocError(format!(
"failed to enable background thread: {}",
e
)));
}
}
Ok(())
}

pub fn fetch_arena_stats(index: usize) -> (u64, u64, u64) {
let resident = unsafe {
tikv_jemalloc_ctl::raw::read(format!("stats.arenas.{}.resident\0", index).as_bytes())
Expand Down Expand Up @@ -434,6 +447,7 @@ mod profiling {
)));
}
}
enable_background_thread()?;
Ok(())
}

Expand Down

0 comments on commit 53048f9

Please sign in to comment.