-
Notifications
You must be signed in to change notification settings - Fork 40
Add docs on Rust jemalloc profiling #489
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,106 @@ | ||
| # Profiling Rust Memory Usage with Jemalloc | ||
|
|
||
| Out jemalloc integration makes it possible to profile heap usage of | ||
| Rust programs, as long as you are willing to use the jemalloc | ||
| allocator. | ||
|
|
||
| # Setup Instructions | ||
|
|
||
| ## Using jemalloc and rust-jemalloc-pprof | ||
|
|
||
| Add the | ||
| [jemalloc_pprof](https://crates.io/crates/jemalloc-pprof) | ||
| and [tikv-jemallocator](https://crates.io/crates/tikv-jemallocator) | ||
| packages to your project. Make sure the latter has the `profiling` and | ||
| `unprefixed_malloc_on_supported_platforms` features: | ||
|
|
||
| ``` bash | ||
| cargo add jemalloc_pprof | ||
| cargo add tikv-jemallocator --features profiling,unprefixed_malloc_on_supported_platforms | ||
| ``` | ||
|
|
||
| Then, in your program's `main.rs` set your global allocator to | ||
| jemalloc and configure it with the special `malloc_conf` symbol: | ||
|
|
||
| ``` rust | ||
| #[global_allocator] | ||
| static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; | ||
|
|
||
| #[unsafe(export_name = "malloc_conf")] | ||
| #[allow(non_upper_case_globals)] | ||
| pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:19\0"; | ||
| ``` | ||
|
|
||
| ## Exposing pprof profiles with http: | ||
|
|
||
| Call the `dump_pprof` method to dump profiles to memory. We | ||
| recommending exposing an HTTP interface for these profiles that can be | ||
| scraped by Parca. | ||
|
|
||
| Here is how to do so using Axum: | ||
|
|
||
| ``` rust | ||
| async fn handle_get_heap() -> Result<impl IntoResponse, (StatusCode, String)> { | ||
| let mut prof_ctl = jemalloc_pprof::PROF_CTL | ||
| .as_ref() | ||
| .ok_or(( | ||
| StatusCode::INTERNAL_SERVER_ERROR, | ||
| "Profiling not available".to_string(), | ||
| ))? | ||
| .lock() | ||
| .await; | ||
|
|
||
| let pprof = prof_ctl | ||
| .dump_pprof() | ||
| .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?; | ||
|
|
||
| Ok(pprof) | ||
| } | ||
|
|
||
| fn main() { | ||
| let app = Router::new().route("/debug/pprof/heap", get(handle_get_heap)); | ||
|
|
||
| let rt = Runtime::new().unwrap(); | ||
|
|
||
| rt.spawn(async { | ||
| let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") | ||
| .await | ||
| .expect("Failed to bind to port 3000"); | ||
| axum::serve(listener, app).await.expect("Server failed"); | ||
| }); | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Uploading symbols with `parca-debuginfo` (only if not using | ||
| `parca-agent`). | ||
|
|
||
| If you are already using `parca-agent`, all relevant symbols will be | ||
| found and uploaded to the backend automatically. Otherwise, you will | ||
| need to manually upload them using the `parca-debuginfo` CLI. For example, | ||
| assuming Parca is running on localhost: | ||
|
|
||
| ``` bash | ||
| parca-debuginfo upload --store-address=localhost:7070 --insecure path/to/your/binary | ||
| ``` | ||
|
|
||
| ## Scraping with Parca | ||
|
|
||
| In order to continually scrape the endpoint, add a stanza like the | ||
| following to your `parca.yaml`, assuming (as in the example above) the | ||
| profiles are being served via HTTP on `127.0.0.1:3000`: | ||
|
|
||
| ``` yaml | ||
| scrape_configs: | ||
| - job_name: "rjemp" | ||
| scrape_interval: "10s" | ||
| static_configs: | ||
| - targets: [ '127.0.0.1:3000' ] | ||
| profiling_config: | ||
| pprof_config: | ||
| heap: | ||
| enabled: true | ||
| path: /debug/pprof/heap | ||
| ``` | ||
|
|
||
| This should cause profiles to appear in the Parca UI. | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ourhere