Skip to content
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

feat: prof command support specify execution path #2018

Merged
merged 1 commit into from
Apr 16, 2020
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions ckb-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ckb-build-info = { path = "../util/build-info" }
ckb-memory-tracker = { path = "../util/memory-tracker" }
ckb-verification = { path = "../verification" }
base64 = "0.10.1"
tempfile = "3.0"

[features]
deadlock_detection = ["ckb-util/deadlock_detection"]
79 changes: 51 additions & 28 deletions ckb-bin/src/subcommand/prof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ckb_logger::info;
use ckb_shared::shared::{Shared, SharedBuilder};
use ckb_store::ChainStore;
use std::sync::Arc;
use tempfile;

pub fn profile(args: ProfArgs) -> Result<(), ExitCode> {
let (shared, _table) = SharedBuilder::with_db_config(&args.config.db)
Expand All @@ -16,35 +17,57 @@ pub fn profile(args: ProfArgs) -> Result<(), ExitCode> {
ExitCode::Failure
})?;

let (tmp_shared, table) = SharedBuilder::default()
.consensus(args.consensus)
.tx_pool_config(args.config.tx_pool)
.build()
.map_err(|err| {
eprintln!("Prof error: {:?}", err);
ExitCode::Failure
})?;
if !args.tmp_target.is_dir() {
eprintln!(
"Prof error: {:?}",
"The specified path does not exist or not directory"
);
return Err(ExitCode::Failure);
}
let tmp_db_dir = tempfile::tempdir_in(args.tmp_target).map_err(|err| {
eprintln!("Prof error: {:?}", err);
ExitCode::Failure
})?;
{
let mut tmp_db_config = args.config.db.clone();
tmp_db_config.path = tmp_db_dir.path().to_path_buf();

let (tmp_shared, table) = SharedBuilder::with_db_config(&tmp_db_config)
.consensus(args.consensus)
.tx_pool_config(args.config.tx_pool)
.build()
.map_err(|err| {
eprintln!("Prof error: {:?}", err);
ExitCode::Failure
})?;

let from = std::cmp::max(1, args.from);
let to = std::cmp::min(shared.snapshot().tip_number(), args.to);
let chain = ChainService::new(tmp_shared, table);
let chain_controller = chain.start(Some("chain"));
profile_block_process(
shared.clone(),
chain_controller.clone(),
1,
std::cmp::max(1, from.saturating_sub(1)),
);
info!("start profling, re-process blocks {}..{}:", from, to);
let now = std::time::Instant::now();
let tx_count = profile_block_process(shared, chain_controller, from, to);
let duration = now.elapsed();
info!(
"end profling, duration {:?} txs {} tps {}",
duration,
tx_count,
tx_count as u64 / duration.as_secs()
);
}

tmp_db_dir.close().map_err(|err| {
eprintln!("Prof error: {:?}", err);
ExitCode::Failure
})?;

let from = std::cmp::max(1, args.from);
let to = std::cmp::min(shared.snapshot().tip_number(), args.to);
let chain = ChainService::new(tmp_shared, table);
let chain_controller = chain.start(Some("chain"));
profile_block_process(
shared.clone(),
chain_controller.clone(),
1,
std::cmp::max(1, from.saturating_sub(1)),
);
info!("start profling, re-process blocks {}..{}:", from, to);
let now = std::time::Instant::now();
let tx_count = profile_block_process(shared, chain_controller, from, to);
let duration = now.elapsed();
info!(
"end profling, duration {:?} txs {} tps {}",
duration,
tx_count,
tx_count as u64 / duration.as_secs()
);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions util/app-config/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct RunArgs {
pub struct ProfArgs {
pub config: Box<CKBAppConfig>,
pub consensus: Consensus,
pub tmp_target: PathBuf,
pub from: u64,
pub to: u64,
}
Expand Down
12 changes: 8 additions & 4 deletions util/app-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub const ARG_NETWORK: &str = "network";
pub const ARG_NETWORK_PEER_STORE: &str = "network-peer-store";
pub const ARG_NETWORK_SECRET_KEY: &str = "network-secret-key";
pub const ARG_LOGS: &str = "logs";
pub const ARG_TMP_TARGET: &str = "tmp-target";

const GROUP_BA: &str = "ba";

Expand Down Expand Up @@ -174,20 +175,23 @@ pub(crate) fn stats() -> App<'static, 'static> {
fn prof() -> App<'static, 'static> {
SubCommand::with_name(CMD_PROF)
.about(
"Profiles ckb node\n\
"Profiles ckb process block\n\
Example: Process 1..500 blocks then output flagme graph\n\
cargo flamegraph --bin ckb -- -C <dir> prof 1 500",
cargo flamegraph --bin ckb -- -C <dir> prof <TMP> 1 500",
)
.arg(Arg::with_name(ARG_TMP_TARGET).required(true).index(1).help(
"Specifies a target path, prof command make a temporary directory inside of target and the directory will be automatically deleted when finished",
))
.arg(
Arg::with_name(ARG_FROM)
.required(true)
.index(1)
.index(2)
.help("Specifies from block number."),
)
.arg(
Arg::with_name(ARG_TO)
.required(true)
.index(2)
.index(3)
.help("Specifies to block number."),
)
}
Expand Down
2 changes: 2 additions & 0 deletions util/app-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ impl Setup {
pub fn prof<'m>(self, matches: &ArgMatches<'m>) -> Result<ProfArgs, ExitCode> {
let consensus = self.consensus()?;
let config = self.config.into_ckb()?;
let tmp_target = value_t!(matches, cli::ARG_TMP_TARGET, PathBuf)?;
let from = value_t!(matches, cli::ARG_FROM, u64)?;
let to = value_t!(matches, cli::ARG_TO, u64)?;

Ok(ProfArgs {
config,
consensus,
tmp_target,
from,
to,
})
Expand Down