turbopack benchmark cli: use our span tracing for the benchmarking cli to understand time spent

parent 0541c5d1
......@@ -10511,6 +10511,7 @@ dependencies = [
"serde",
"tempfile",
"tokio",
"tracing",
"turbo-rcstr",
"turbo-tasks",
"turbo-tasks-backend",
......@@ -10878,6 +10879,7 @@ dependencies = [
"serde_json",
"tokio",
"tracing",
"tracing-subscriber",
"turbo-rcstr",
"turbo-tasks",
"turbo-tasks-backend",
......@@ -10885,6 +10887,7 @@ dependencies = [
"turbopack-cli-api",
"turbopack-cli-core",
"turbopack-core",
"turbopack-trace-utils",
"vergen-gitcl",
]
......
......@@ -16,6 +16,7 @@ workspace = true
anyhow = { workspace = true }
bincode = { workspace = true }
serde = { workspace = true }
tracing = { workspace = true }
turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true }
turbo-tasks-backend = { workspace = true }
......
//! Build driver: compile each HTML entry's local `<script>` / `<link>` references and emit the
//! rewritten HTML plus its content-hashed chunks into the output directory.
use std::{env::current_dir, path::PathBuf, sync::Arc};
use std::{env::current_dir, future::IntoFuture, path::PathBuf, sync::Arc};
use anyhow::{Context, Result};
use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
Effects, OperationVc, ReadRef, ResolvedVc, TransientInstance, TryJoinIterExt, TurboTasks, Vc,
......@@ -258,6 +259,8 @@ async fn build_internal(container: ResolvedVc<ProjectContainer>) -> Result<()> {
true,
);
let mut module_graph = ModuleGraph::from_graphs(vec![single_graph], None);
// Force graph construction (module discovery, parse, analyze) before timing the rest.
module_graph.connect().to_resolved().await?;
let binding_usage = compute_binding_usage_info(module_graph, true);
let unused_references = binding_usage
.connect()
......@@ -355,11 +358,15 @@ async fn build_internal(container: ResolvedVc<ProjectContainer>) -> Result<()> {
}
// Expand the transitive output-asset graph (HTML + all chunks) and write each to disk.
let all_assets = all_assets_from_entries(Vc::<OutputAssets>::cell(roots)).await?;
let all_assets = all_assets_from_entries(Vc::<OutputAssets>::cell(roots))
.into_future()
.instrument(tracing::info_span!("build: expand output graph"))
.await?;
all_assets
.iter()
.map(|asset| async move { asset.content().write(asset.path().owned().await?).await })
.try_join()
.instrument(tracing::info_span!("build: generate content and write"))
.await?;
Ok(())
......
......@@ -26,6 +26,8 @@ serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
turbopack-trace-utils = { workspace = true }
turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true }
turbo-tasks-backend = { workspace = true }
......
......@@ -19,6 +19,8 @@ fn init() {
use turbo_tasks::{panic_hooks::handle_panic, parallel::available_parallelism};
use turbo_tasks_malloc::TurboMalloc;
init_tracing();
let prev_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
handle_panic(info);
......@@ -42,3 +44,73 @@ fn init() {
.unwrap();
create_custom_tokio_runtime(rt);
}
/// Holds the trace writer's flush guard for the life of the process. Dropping it flushes the
/// buffered trace to disk, so `project_shutdown` takes it rather than letting the process exit
/// with a truncated file.
static TRACE_GUARD: std::sync::Mutex<
Option<turbopack_trace_utils::trace_writer::TraceWriterGuard>,
> = std::sync::Mutex::new(None);
/// Enable tracing when `TURBOPACK_TRACING` is set, mirroring `turbopack-cli`. Writes a raw trace
/// to `<cwd>/.turbopack/trace.log`, which `turbopack-trace-server` reads.
///
/// The CLI entry point has had this for a long time; the napi addon did not, which left the
/// standalone CLI unprofilable through the normal tooling.
fn init_tracing() {
use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt};
use turbopack_trace_utils::{
filter_layer::FilterLayer,
raw_trace::RawTraceLayer,
trace_writer::TraceWriter,
tracing_presets::{
TRACING_OVERVIEW_TARGETS, TRACING_TURBO_TASKS_TARGETS, TRACING_TURBOPACK_TARGETS,
},
};
let Some(mut trace) = std::env::var("TURBOPACK_TRACING")
.ok()
.filter(|v| !v.is_empty())
else {
return;
};
match trace.as_str() {
"overview" | "1" => trace = TRACING_OVERVIEW_TARGETS.join(","),
"turbopack" => trace = TRACING_TURBOPACK_TARGETS.join(","),
"turbo-tasks" => trace = TRACING_TURBO_TASKS_TARGETS.join(","),
_ => {}
}
let internal_dir = std::env::current_dir()
.unwrap_or_else(|_| std::path::PathBuf::from("."))
.join(".turbopack");
if let Err(err) = std::fs::create_dir_all(&internal_dir) {
eprintln!(
"turbopack: unable to create {}: {err}",
internal_dir.display()
);
return;
}
let trace_path = internal_dir.join("trace.log");
let file = match std::fs::File::create(&trace_path) {
Ok(file) => file,
Err(err) => {
eprintln!("turbopack: unable to write {}: {err}", trace_path.display());
return;
}
};
eprintln!("turbopack: tracing to {}", trace_path.display());
let (trace_writer, guard) = TraceWriter::new(file);
*TRACE_GUARD.lock().unwrap() = Some(guard);
Registry::default()
.with(FilterLayer::try_new(&trace).unwrap())
.with(RawTraceLayer::new(trace_writer))
.init();
}
/// Flush the trace file, if tracing is on.
pub fn flush_tracing() {
drop(TRACE_GUARD.lock().unwrap().take());
}
......@@ -145,5 +145,6 @@ pub async fn project_shutdown(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: &External<ProjectInstance>,
) -> napi::Result<()> {
project.turbo_tasks.stop_and_wait().await;
crate::flush_tracing();
Ok(())
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment