Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions light-client-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ rocksdb = { package = "ckb-rocksdb", version = "=0.21.1", features = [
], default-features = false }
env_logger = "0.11"
anyhow = "1.0.56"
tikv-jemallocator = { version = "0.6.0", features = ["profiling", "unprefixed_malloc_on_supported_platforms"] }
tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"] }

[dev-dependencies]
rand = "0.8"
Expand Down
10 changes: 10 additions & 0 deletions light-client-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ mod tests;
use cli::AppConfig;
use env_logger::{Builder, Env, Target};

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[allow(non_upper_case_globals)]
#[export_name = "malloc_conf"]
pub static malloc_conf: &[u8] =
b"prof:true,prof_active:true,lg_prof_sample:19,prof_prefix:/tmp/jeprof\0";

fn main() -> anyhow::Result<()> {
let mut builder = Builder::from_env(Env::default());
builder.target(Target::Stdout);
Expand Down
53 changes: 53 additions & 0 deletions light-client-bin/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ pub trait NetRpc {
fn get_peers(&self) -> Result<Vec<RemoteNode>>;
}

#[rpc(server)]
pub trait DebugRpc {
/// Dumps jemalloc profiling data to a file
/// Returns the path to the generated profile file
#[rpc(name = "jemalloc_profiling_dump")]
fn jemalloc_profiling_dump(&self) -> Result<String>;
}

pub struct BlockFilterRpcImpl {
pub(crate) swc: StorageWithChainData,
}
Expand All @@ -129,6 +137,8 @@ pub struct NetRpcImpl {
peers: Arc<Peers>,
}

pub struct DebugRpcImpl;

impl BlockFilterRpc for BlockFilterRpcImpl {
fn set_scripts(
&self,
Expand Down Expand Up @@ -802,6 +812,47 @@ impl NetRpc for NetRpcImpl {
}
}

impl DebugRpc for DebugRpcImpl {
fn jemalloc_profiling_dump(&self) -> Result<String> {
#[cfg(not(target_env = "msvc"))]
{
use std::ffi::CString;
use tikv_jemalloc_ctl::{epoch, raw};

// Trigger an epoch update to ensure current statistics
epoch::mib()
.map_err(|_| Error::invalid_params("Failed to get epoch mib"))?
.advance()
.map_err(|_| Error::invalid_params("Failed to advance epoch"))?;

// Generate a unique filename with timestamp
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
let filename = format!("/tmp/jeprof.{}.{}.heap", std::process::id(), timestamp);

// Dump the heap profile using prof.dump
let c_filename = CString::new(filename.clone())
.map_err(|_| Error::invalid_params("Invalid filename"))?;

let prof_dump_name = b"prof.dump\0";
unsafe {
raw::write(prof_dump_name, c_filename.as_ptr() as *const _)
.map_err(|_| Error::invalid_params("Failed to dump heap profile"))?;
}

Ok(filename)
}
#[cfg(target_env = "msvc")]
{
Err(Error::invalid_params(
"Jemalloc profiling is not available on MSVC builds",
))
}
}
}

impl TransactionRpc for TransactionRpcImpl {
fn send_transaction(&self, tx: Transaction) -> Result<H256> {
let tx: packed::Transaction = tx.into();
Expand Down Expand Up @@ -983,10 +1034,12 @@ impl Service {
network_controller,
peers,
};
let debug_rpc_impl = DebugRpcImpl;
io_handler.extend_with(block_filter_rpc_impl.to_delegate());
io_handler.extend_with(chain_rpc_impl.to_delegate());
io_handler.extend_with(transaction_rpc_impl.to_delegate());
io_handler.extend_with(net_rpc_impl.to_delegate());
io_handler.extend_with(debug_rpc_impl.to_delegate());

ServerBuilder::new(io_handler)
.cors(DomainsValidation::AllowOnly(vec![
Expand Down
Loading