-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a feature to enable jemalloc profiling
- Loading branch information
1 parent
6019bc0
commit 8b6263a
Showing
15 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,62 @@ | ||
# CKB Debugging | ||
|
||
## Memory | ||
|
||
**Only linux versions supported.** | ||
|
||
### Tracking Memory Usage in Logs | ||
|
||
Add the follow configuration into `ckb.toml`: | ||
|
||
```toml | ||
[logger] | ||
filter = "error,ckb-memory-tracker=trace" | ||
|
||
[memory_tracker] | ||
# Seconds between checking the process, 0 is disable, default is 0. | ||
interval = 600 | ||
``` | ||
|
||
### Memory Profiling | ||
|
||
- Compile `ckb` with feature `profiling`. | ||
|
||
```sh | ||
make build-for-profiling` | ||
``` | ||
|
||
After compiling, a script named `jeprof` will be generated in `target` direcotry. | ||
|
||
```sh | ||
find target/ -name "jeprof" | ||
``` | ||
|
||
- Enable RPC module `Debug` in `ckb.toml`. | ||
|
||
```toml | ||
[rpc] | ||
modules = ["Debug"] | ||
``` | ||
|
||
- Run `ckb`. | ||
|
||
- Dump memory usage to a file via call RPC `jemalloc_profiling_dump`. | ||
|
||
```sh | ||
curl -H 'content-type: application/json' -d '{ "id": 2, "jsonrpc": "2.0", "method": "jemalloc_profiling_dump", "params": [] }' http://localhost:8114 | ||
``` | ||
|
||
Then, a file named `ckb-jeprof.$TIMESTAMP.heap` will be generated in the working directory of the running `ckb`. | ||
|
||
- Generate a PDF of the call graph. | ||
|
||
**Required**: graphviz and ghostscript | ||
|
||
```sh | ||
jeprof --show_bytes --pdf target/debug/ckb ckb-jeprof.$TIMESTAMP.heap > call-graph.pdf | ||
``` | ||
|
||
## References: | ||
|
||
- [JEMALLOC: Use Case: Leak Checking](https://github.com/jemalloc/jemalloc/wiki/Use-Case%3A-Leak-Checking) | ||
- [JEMALLOC: Use Case: Heap Profiling](https://github.com/jemalloc/jemalloc/wiki/Use-Case%3A-Heap-Profiling) |
This file contains 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 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 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 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,23 @@ | ||
use jsonrpc_core::Result; | ||
use jsonrpc_derive::rpc; | ||
use std::time; | ||
|
||
#[rpc(server)] | ||
pub trait DebugRpc { | ||
#[rpc(name = "jemalloc_profiling_dump")] | ||
fn jemalloc_profiling_dump(&self) -> Result<()>; | ||
} | ||
|
||
pub(crate) struct DebugRpcImpl {} | ||
|
||
impl DebugRpc for DebugRpcImpl { | ||
fn jemalloc_profiling_dump(&self) -> Result<()> { | ||
let timestamp = time::SystemTime::now() | ||
.duration_since(time::SystemTime::UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs(); | ||
let filename = format!("ckb-jeprof.{}.heap\0", timestamp); | ||
ckb_memory_tracker::jemalloc_profiling_dump(filename); | ||
Ok(()) | ||
} | ||
} |
This file contains 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 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 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 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,5 @@ | ||
use ckb_logger::warn; | ||
|
||
pub fn jemalloc_profiling_dump(_: String) { | ||
warn!("jemalloc profiling dump: unsupported"); | ||
} |
This file contains 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,15 @@ | ||
use std::{ffi, mem, ptr}; | ||
|
||
pub fn jemalloc_profiling_dump(mut filename: String) { | ||
let opt_name = "prof.dump"; | ||
let opt_c_name = ffi::CString::new(opt_name).unwrap(); | ||
unsafe { | ||
jemalloc_sys::mallctl( | ||
opt_c_name.as_ptr(), | ||
ptr::null_mut(), | ||
ptr::null_mut(), | ||
&mut filename as *mut _ as *mut _, | ||
mem::size_of::<*mut ffi::c_void>(), | ||
); | ||
} | ||
} |
This file contains 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