-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add jemalloc heap profiling infrastructure #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eudelins-zama
wants to merge
11
commits into
main
Choose a base branch
from
eudelins/feat/2927/memory-profiling-setup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5ff202
fix: add missing field in config
eudelins-zama e663733
feat: add jemalloc heap profiling infrastructure
eudelins-zama 2ccf39c
chore: simplify analysis script
eudelins-zama ba79db2
fix: trivy action bump
eudelins-zama 298351a
chore: rename jemalloc-stats to heap-profiling
eudelins-zama b5b2623
fix: use std::ffi::CString
eudelins-zama 44b0f8a
docs: portable sed doc clarification
eudelins-zama 16386f0
docs: memory profiling doc clarification
eudelins-zama 2b69f54
chore: fix profiler script for macOS
kc1212 fa1e321
fix: revert config update
eudelins-zama f971458
fix: heap dump diff analysis fix
eudelins-zama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,4 +42,4 @@ user_decrypt = 1 | |
| crsgen = 100 | ||
| preproc = 25000 | ||
| keygen = 1000 | ||
| new_epoch = 1 | ||
| new_epoch = 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //! Heap profiling support using jemalloc. | ||
| //! | ||
| //! When the `heap-profiling` feature is enabled and `MALLOC_CONF` includes | ||
| //! `prof:true`, this module provides on-demand heap dumps. | ||
| //! | ||
| //! # Quick Start | ||
| //! | ||
| //! For the full Docker-based workflow (handles PIE/ASLR, symbol resolution, | ||
| //! and diff analysis automatically), see `profiling/README.md`. | ||
| //! | ||
| //! Manual (non-PIE binary) usage: | ||
| //! | ||
| //! 1. Build with: `cargo build -p kms --bin kms-server --profile heap-profiling -F heap-profiling` | ||
| //! 2. Run with env: `MALLOC_CONF=prof:true,lg_prof_sample:12 kms-server ...` | ||
| //! (use `lg_prof_sample:19` for lower overhead — see `profiling/README.md`) | ||
| //! 3. Dump heap: `kill -USR1 <pid>` | ||
| //! 4. Analyze: `jeprof --svg kms-server /tmp/kms-heap/prof.0001.heap > heap.svg` | ||
| //! 5. Diff two dumps: `jeprof --base=prof.0001.heap --svg kms-server prof.0010.heap > diff.svg` | ||
|
|
||
| use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
|
||
| const HEAP_DUMP_DIR: &str = "/tmp/kms-heap"; | ||
|
|
||
| static DUMP_SEQ: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| /// Dump a heap profile to `/tmp/kms-heap/prof.NNNN.heap`. | ||
| /// | ||
| /// Creates the output directory if it does not already exist. | ||
| pub fn dump_heap_profile() -> Result<String, String> { | ||
| // Ensure the output directory exists (idempotent) | ||
| if let Err(e) = std::fs::create_dir_all(HEAP_DUMP_DIR) { | ||
| eprintln!("[heap-profiling] WARNING: failed to create {HEAP_DUMP_DIR}: {e}"); | ||
| } | ||
|
|
||
| let seq = DUMP_SEQ.fetch_add(1, Ordering::Relaxed); | ||
| let path_str = format!("{HEAP_DUMP_DIR}/prof.{seq:04}.heap"); | ||
| let path_c = | ||
| std::ffi::CString::new(path_str.clone()).map_err(|e| format!("invalid path: {e}"))?; | ||
|
|
||
| // jemalloc mallctl expects a pointer to the filename string | ||
| let ptr = path_c.as_ptr(); | ||
| // SAFETY: `ptr` points to a valid null-terminated C string (`path_c`) that | ||
| // outlives this call. jemalloc's `prof.dump` mallctl expects a `const char *` | ||
| // and `raw::write` passes `&ptr` as `newp`, matching the expected ABI. | ||
| let result = unsafe { tikv_jemalloc_ctl::raw::write(b"prof.dump\0", ptr) }; | ||
|
|
||
| match result { | ||
| Ok(()) => { | ||
| eprintln!("[heap-profiling] Dumped to {path_str}"); | ||
| Ok(path_str) | ||
| } | ||
| Err(e) => { | ||
| let msg = format!("jemalloc prof.dump failed: {e}. Is MALLOC_CONF=prof:true set?"); | ||
| eprintln!("[heap-profiling] ERROR: {msg}"); | ||
| Err(msg) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Install a SIGUSR1 handler that triggers heap profile dumps. | ||
| /// | ||
| /// Call this once at startup. Then `kill -USR1 <pid>` to dump. | ||
| pub fn install_sigusr1_handler() { | ||
| if let Err(e) = std::fs::create_dir_all(HEAP_DUMP_DIR) { | ||
| eprintln!("[heap-profiling] WARNING: failed to create {HEAP_DUMP_DIR}: {e}"); | ||
| } | ||
|
|
||
| // Spawn a background tokio task to listen for SIGUSR1 | ||
| tokio::spawn(async { | ||
| let mut sig = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) | ||
| .expect("Failed to register SIGUSR1 handler"); | ||
|
|
||
| eprintln!("[heap-profiling] Ready — send SIGUSR1 to dump heap profile to {HEAP_DUMP_DIR}/"); | ||
|
|
||
| loop { | ||
| sig.recv().await; | ||
| let _ = dump_heap_profile(); | ||
| } | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.