Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,5 @@ diff_output
*.trace

# rspack trace file
trace.json
trace.json
*.pftrace
58 changes: 53 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ rspack_storage = { version = "0.2.0", path = "crates/rsp
rspack_swc_plugin_import = { version = "0.2.0", path = "crates/swc_plugin_import" }
rspack_testing = { version = "0.2.0", path = "crates/rspack_testing" }
rspack_tracing = { version = "0.2.0", path = "crates/rspack_tracing" }
rspack_tracing_chrome = { version = "0.2.0", path = "crates/rspack_tracing_chrome" }
rspack_tracing_perfetto = { version = "0.2.0", path = "crates/rspack_tracing_perfetto" }
rspack_util = { version = "0.2.0", path = "crates/rspack_util" }

[workspace.metadata.release]
Expand Down
15 changes: 14 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,17 @@ export interface RawToOptions {
absoluteFilename?: string
}

export interface RawTraceEvent {
name: string
trackName?: string
processName?: string
args?: Record<string, string>
uuid: number
ts: bigint
ph: string
categories?: Array<string>
}

export interface RawTrustedTypes {
policyName?: string
onPolicyCreationFailure?: string
Expand All @@ -2574,7 +2585,7 @@ export interface RealDependencyLocation {
* Author Donny/강동윤
* Copyright (c)
*/
export declare function registerGlobalTrace(filter: string, layer: "chrome" | "logger" , output: string): void
export declare function registerGlobalTrace(filter: string, layer: "logger" | "perfetto" , output: string): void

export declare enum RegisterJsTapKind {
CompilerThisCompilation = 0,
Expand Down Expand Up @@ -2719,6 +2730,8 @@ export interface SourcePosition {
*/
export declare function startAsyncRuntime(): void

export declare function syncTraceEvent(events: Array<RawTraceEvent>): void

export interface SyntheticDependencyLocation {
name: string
}
Expand Down
42 changes: 36 additions & 6 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use rspack_core::{
use rspack_error::Diagnostic;
use rspack_fs::IntermediateFileSystem;

use crate::fs_node::{NodeFileSystem, ThreadsafeNodeFS};
use crate::{
fs_node::{NodeFileSystem, ThreadsafeNodeFS},
trace_event::RawTraceEvent,
};

mod allocator;
mod asset;
Expand Down Expand Up @@ -63,6 +66,7 @@ mod runtime;
mod source;
mod stats;
mod swc;
mod trace_event;
mod utils;

pub use asset::*;
Expand Down Expand Up @@ -100,7 +104,7 @@ use resolver_factory::*;
pub use resource_data::*;
pub use rsdoctor::*;
use rspack_macros::rspack_version;
use rspack_tracing::{ChromeTracer, StdoutTracer, Tracer};
use rspack_tracing::{PerfettoTracer, StdoutTracer, TraceEvent, Tracer};
pub use rstest::*;
pub use runtime::*;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -435,7 +439,6 @@ fn print_error_diagnostic(e: rspack_error::Error, colored: bool) -> String {
thread_local! {
static GLOBAL_TRACE_STATE: RefCell<TraceState> = const { RefCell::new(TraceState::Off) };
}

/**
* Some code is modified based on
* https://github.com/swc-project/swc/blob/d1d0607158ab40463d1b123fed52cc526eba8385/bindings/binding_core_node/src/util.rs#L29-L58
Expand All @@ -446,17 +449,17 @@ thread_local! {
#[napi]
pub fn register_global_trace(
filter: String,
#[napi(ts_arg_type = "\"chrome\" | \"logger\" ")] layer: String,
#[napi(ts_arg_type = " \"logger\" | \"perfetto\" ")] layer: String,
output: String,
) -> anyhow::Result<()> {
GLOBAL_TRACE_STATE.with(|state| {
let mut state = state.borrow_mut();
if let TraceState::Off = *state {
let mut tracer: Box<dyn Tracer> = match layer.as_str() {
"chrome" => Box::new(ChromeTracer::default()),
"logger" => Box::new(StdoutTracer),
"perfetto"=> Box::new(PerfettoTracer::default()),
_ => anyhow::bail!(
"Unexpected layer: {}, supported layers: 'chrome', 'logger', 'console' ",
"Unexpected layer: {}, supported layers:'logger', 'perfetto' ",
layer
),
};
Expand Down Expand Up @@ -490,6 +493,33 @@ pub fn cleanup_global_trace() {
*state = TraceState::Off;
});
}
// sync Node.js event to Rust side
#[napi]
pub fn sync_trace_event(events: Vec<RawTraceEvent>) {
use std::borrow::BorrowMut;
GLOBAL_TRACE_STATE.with(|state| {
let mut state = state.borrow_mut();
if let TraceState::On(tracer) = &mut **state.borrow_mut() {
tracer.sync_trace(
events
.into_iter()
.map(|event| TraceEvent {
name: event.name,
track_name: event.track_name,
process_name: event.process_name,
args: event
.args
.map(|args| args.into_iter().map(|(k, v)| (k, v.to_string())).collect()),
uuid: event.uuid,
ts: event.ts.get_u64().1,
ph: event.ph,
categories: event.categories,
})
.collect(),
);
}
});
}

fn node_init(mut _exports: Object, env: Env) -> Result<()> {
rspack_core::set_thread_local_allocator(Box::new(allocator::NapiAllocatorImpl::new(env)));
Expand Down
25 changes: 25 additions & 0 deletions crates/node_binding/src/trace_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::collections::HashMap;

use napi::bindgen_prelude::BigInt;
use napi_derive::napi;

#[napi(object)]
#[derive(Debug)]
pub struct RawTraceEvent {
// event name
pub name: String,
// separate track name
pub track_name: Option<String>,
// separate group sliced name
pub process_name: Option<String>,
// extra debug arguments
pub args: Option<HashMap<String, String>>,
// track_uuid
pub uuid: u32,
// timestamp in microseconds
pub ts: BigInt,
// chrome trace event ph
pub ph: String,
// category
pub categories: Option<Vec<String>>,
}
10 changes: 4 additions & 6 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl NormalModule {

#[tracing::instrument(
"NormalModule:build_hash", skip_all,fields(
id2 = self.resource_data.resource.as_str()
resource = self.resource_data.resource.as_str()
)
)]
fn init_build_hash(
Expand Down Expand Up @@ -367,9 +367,10 @@ impl Module for NormalModule {
}

#[tracing::instrument("NormalModule:build", skip_all, fields(
perfetto.track_name = format!("Module Build"),
perfetto.process_name = format!("Rspack Build Detail"),
module.resource = self.resource_resolved_data().resource.as_str(),
module.identifier = self.identifier().as_str(),
id2 = self.resource_data.resource.as_str(),
module.loaders = ?self.loaders.iter().map(|l| l.identifier().as_str()).collect::<Vec<_>>())
)]
async fn build(
Expand Down Expand Up @@ -413,10 +414,7 @@ impl Module for NormalModule {
},
build_context.fs.clone(),
)
.instrument(info_span!(
"NormalModule:run_loaders",
id2 = self.resource_data.resource.as_str(),
))
.instrument(info_span!("NormalModule:run_loaders",))
.await;
let (mut loader_result, ds) = match loader_result {
Ok(r) => r.split_into_parts(),
Expand Down
5 changes: 5 additions & 0 deletions crates/rspack_loader_lightningcss/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ impl Identifiable for LightningCssLoader {
#[cacheable_dyn]
#[async_trait::async_trait]
impl Loader<RunnerContext> for LightningCssLoader {
#[tracing::instrument("loader:lightningcss", skip_all, fields(
perfetto.track_name = "loader:lightningcss",
perfetto.process_name = "Loader Analysis",
resource =loader_context.resource(),
))]
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
// for better diagnostic, as async_trait macro don't show beautiful error message
self.loader_impl(loader_context).await
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_loader_runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<Context> LoaderContext<Context> {

#[tracing::instrument("LoaderRunner:process_resource",
skip_all,
fields(id2 = loader_context.resource_data.resource)
fields(resource= loader_context.resource_data.resource)
)]
async fn process_resource<Context: Send>(
loader_context: &mut LoaderContext<Context>,
Expand Down Expand Up @@ -134,7 +134,7 @@ pub async fn run_loaders<Context: Send>(
cx.state.transition(State::ProcessResource);
continue;
}
let span = info_span!("run_loader:pitch:yield_to_js", id2 = resource);
let span = info_span!("run_loader:pitch:yield_to_js", resource);
if cx.start_yielding().instrument(span).await? {
if cx.content.is_some() {
cx.state.transition(State::Normal);
Expand All @@ -150,7 +150,7 @@ pub async fn run_loaders<Context: Send>(

cx.current_loader().set_pitch_executed();
let loader = cx.current_loader().loader().clone();
let span = info_span!("run_loader:pitch", id2 = resource);
let span = info_span!("run_loader:pitch", resource);
loader.pitch(&mut cx).instrument(span).await?;
if cx.content.is_some() {
cx.state.transition(State::Normal);
Expand All @@ -159,7 +159,7 @@ pub async fn run_loaders<Context: Send>(
}
}
State::ProcessResource => {
let span = info_span!("run_loader:process_resource", id2 = resource);
let span = info_span!("run_loader:process_resource", resource);
process_resource(&mut cx, fs.clone())
.instrument(span)
.await?;
Expand All @@ -176,7 +176,7 @@ pub async fn run_loaders<Context: Send>(
cx.state.transition(State::Finished);
continue;
}
let span = info_span!("run_loader:yield_to_js", id2 = resource);
let span = info_span!("run_loader:yield_to_js", resource);
if cx.start_yielding().instrument(span).await? {
continue;
}
Expand All @@ -189,7 +189,7 @@ pub async fn run_loaders<Context: Send>(
cx.current_loader().set_normal_executed();
let loader = cx.current_loader().loader().clone();

let span = info_span!("run_loader:normal", id2 = resource);
let span = info_span!("run_loader:normal", resource);
loader.run(&mut cx).instrument(span).await?;
if !cx.current_loader().finish_called() {
// If nothing is returned from this loader,
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ pub const SWC_LOADER_IDENTIFIER: &str = "builtin:swc-loader";
#[async_trait::async_trait]
impl Loader<RunnerContext> for SwcLoader {
#[tracing::instrument("loader:builtin-swc", skip_all, fields(
id2 =loader_context.resource(),
perfetto.track_name = "loader:builtin-swc",
perfetto.process_name = "Loader Analysis",
resource =loader_context.resource(),
))]
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
#[allow(unused_mut)]
Expand Down
Loading
Loading