Skip to content

Commit 90f3440

Browse files
get9facebook-github-bot
authored andcommitted
buck2: Allow passing --trace-id to buck2 debug chrome-trace
Summary: I've been analyzing chrome-traces from builds that don't always generate them; it'd be nice to allow passing a `--trace-id` to this command so that buck2 downloads it for me rather than me having to materialize it first and pass the path. Reviewed By: JakobDegen Differential Revision: D75388796 fbshipit-source-id: 6e7807a710af9d7c7d2429c1a2ba40a234878d9e
1 parent b5e6de5 commit 90f3440

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

app/buck2_client/src/commands/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl DebugCommand {
112112
DebugCommand::AllocatorStats(cmd) => ctx.exec(cmd, matches),
113113
DebugCommand::Replay(cmd) => cmd.exec(matches, ctx),
114114
DebugCommand::InternalVersion(cmd) => cmd.exec(matches, ctx),
115-
DebugCommand::ChromeTrace(cmd) => cmd.exec(matches, ctx),
115+
DebugCommand::ChromeTrace(cmd) => ctx.exec(cmd, matches),
116116
DebugCommand::FlushDepFiles(cmd) => ctx.exec(cmd, matches),
117117
DebugCommand::WhatRan(cmd) => cmd.exec(matches, ctx),
118118
DebugCommand::Materialize(cmd) => ctx.exec(cmd, matches),

app/buck2_client/src/commands/debug/chrome_trace.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ use std::sync::Arc;
1717
use std::time::Duration;
1818
use std::time::SystemTime;
1919

20+
use buck2_client_ctx::client_ctx::BuckSubcommand;
2021
use buck2_client_ctx::client_ctx::ClientCommandContext;
2122
use buck2_client_ctx::common::BuckArgMatches;
23+
use buck2_client_ctx::events_ctx::EventsCtx;
2224
use buck2_client_ctx::exit_result::ExitResult;
2325
use buck2_client_ctx::path_arg::PathArg;
2426
use buck2_common::convert::ProstDurationExt;
2527
use buck2_core::fs::paths::abs_path::AbsPathBuf;
2628
use buck2_error::BuckErrorContext;
2729
use buck2_error::buck2_error;
28-
use buck2_event_log::file_names::retrieve_nth_recent_log;
2930
use buck2_event_log::read::EventLogPathBuf;
3031
use buck2_event_log::stream_value::StreamValue;
3132
use buck2_event_log::utils::Invocation;
@@ -39,30 +40,28 @@ use futures::stream::BoxStream;
3940
use serde::Serialize;
4041
use serde_json::json;
4142

43+
use crate::commands::log::options::EventLogOptions;
44+
4245
#[derive(Debug, clap::Parser)]
4346
pub struct ChromeTraceCommand {
4447
#[clap(
4548
long,
4649
help = "Where to write the chrome trace JSON. If a directory is passed, the filename of the event log will be used as a base filename."
4750
)]
4851
pub trace_path: PathArg,
52+
4953
/// The path to read the event log from.
5054
#[clap(
5155
long,
5256
help = "A path to an event-log file to read from. Only works for log files with a single command in them. If no event-log is passed, the most recent one will be used.",
53-
group = "event_log",
54-
value_name = "PATH"
57+
value_name = "PATH",
58+
// Hide because `event_log` below subsumes this.
59+
hide = true
5560
)]
5661
pub path: Option<PathArg>,
5762

58-
/// Which recent command to read the event log from.
59-
#[clap(
60-
long,
61-
help = "Use the event-log from the Nth most recent command (`--recent 0` is the most recent).",
62-
group = "event_log",
63-
value_name = "NUMBER"
64-
)]
65-
pub recent: Option<usize>,
63+
#[clap(flatten)]
64+
pub(crate) event_log: EventLogOptions,
6665
}
6766

6867
struct ChromeTraceFirstPass {
@@ -959,6 +958,10 @@ impl ChromeTraceWriter {
959958
}
960959

961960
impl ChromeTraceCommand {
961+
pub fn exec(self, matches: BuckArgMatches<'_>, ctx: ClientCommandContext<'_>) -> ExitResult {
962+
ctx.exec(self, matches)
963+
}
964+
962965
async fn load_events(
963966
log_path: EventLogPathBuf,
964967
) -> buck2_error::Result<(
@@ -994,30 +997,32 @@ impl ChromeTraceCommand {
994997
}
995998
}
996999
}
1000+
}
9971001

998-
pub fn exec(self, _matches: BuckArgMatches<'_>, ctx: ClientCommandContext<'_>) -> ExitResult {
999-
let log = match self.path {
1000-
Some(path) => path.resolve(&ctx.working_dir),
1001-
None => retrieve_nth_recent_log(
1002-
ctx.paths()
1003-
.buck_error_context("Error identifying log dir")?,
1004-
self.recent.unwrap_or(0),
1005-
)?
1006-
.path()
1007-
.to_owned(),
1002+
impl BuckSubcommand for ChromeTraceCommand {
1003+
const COMMAND_NAME: &'static str = "chrome-trace";
1004+
1005+
async fn exec_impl(
1006+
self,
1007+
_matches: BuckArgMatches<'_>,
1008+
ctx: ClientCommandContext<'_>,
1009+
_events_ctx: &mut EventsCtx,
1010+
) -> ExitResult {
1011+
// For backward compatibility, use the path field if it's set
1012+
let log = if let Some(path) = self.path {
1013+
EventLogPathBuf::infer(path.resolve(&ctx.working_dir))?
1014+
} else {
1015+
self.event_log.get(&ctx).await?
10081016
};
1009-
10101017
let trace_path = self.trace_path.resolve(&ctx.working_dir);
10111018
let dest_path = if trace_path.is_dir() {
1012-
Self::trace_path_from_dir(trace_path, &log)
1019+
Self::trace_path_from_dir(trace_path, log.path())
10131020
.buck_error_context("Could not determine trace path")?
10141021
} else {
10151022
trace_path
10161023
};
10171024

1018-
let log = EventLogPathBuf::infer(log)?;
1019-
1020-
let writer = ctx.with_runtime(|_| Self::trace_writer(log))?;
1025+
let writer = Self::trace_writer(log).await?;
10211026

10221027
let tracefile = std::fs::OpenOptions::new()
10231028
.create(true)
@@ -1028,7 +1033,9 @@ impl ChromeTraceCommand {
10281033

10291034
ExitResult::success()
10301035
}
1036+
}
10311037

1038+
impl ChromeTraceCommand {
10321039
async fn trace_writer(log: EventLogPathBuf) -> buck2_error::Result<ChromeTraceWriter> {
10331040
let (invocation, mut stream) = Self::load_events(log.clone()).await?;
10341041
let mut first_pass = ChromeTraceFirstPass::new();

0 commit comments

Comments
 (0)