From 5c5034bed89c7d2ae55a9bfa3c885b0c636692db Mon Sep 17 00:00:00 2001 From: quininer Date: Thu, 8 May 2025 10:46:36 +0800 Subject: [PATCH 1/2] sftrace support --- .cargo/config.toml | 5 +++-- Cargo.lock | 6 ++++++ crates/node_binding/Cargo.toml | 1 + crates/node_binding/src/lib.rs | 4 ++++ crates/rspack_allocator/Cargo.toml | 3 +++ crates/rspack_allocator/src/lib.rs | 3 ++- rust-toolchain.toml | 1 - 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 4392bf2dbf2c..b326b648660b 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -32,8 +32,9 @@ rustflags = [ "-Aclippy::default_constructed_unit_structs", "-Zshare-generics=y", # make the current crate share its generic instantiations - "-Zthreads=8" # parallel frontend https://blog.rust-lang.org/2023/11/09/parallel-rustc.html - + "-Zthreads=8", # parallel frontend https://blog.rust-lang.org/2023/11/09/parallel-rustc.html + + "-Zinstrument-xray=always" ] # Fix napi breaking in test environment diff --git a/Cargo.lock b/Cargo.lock index a96dc09bd925..016704e27435 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4063,6 +4063,7 @@ name = "rspack_allocator" version = "0.2.0" dependencies = [ "mimalloc-rspack", + "sftrace-setup", ] [[package]] @@ -4575,6 +4576,7 @@ dependencies = [ "rustc-hash 2.1.1", "serde", "serde_json", + "sftrace-setup", "swc_core", "tokio", "tracing", @@ -5863,6 +5865,10 @@ dependencies = [ "version_check", ] +[[package]] +name = "sftrace-setup" +version = "0.1.0" + [[package]] name = "sha1" version = "0.10.6" diff --git a/crates/node_binding/Cargo.toml b/crates/node_binding/Cargo.toml index 460db518ab89..aeb8496de395 100644 --- a/crates/node_binding/Cargo.toml +++ b/crates/node_binding/Cargo.toml @@ -14,6 +14,7 @@ crate-type = ["cdylib"] plugin = ["rspack_loader_swc/plugin"] [dependencies] +sftrace-setup = { path = "../../../sftrace/setup" } anyhow = { workspace = true } browserslist-rs = { workspace = true } ropey = { workspace = true } diff --git a/crates/node_binding/src/lib.rs b/crates/node_binding/src/lib.rs index b1e4ed42c835..5364b7148d11 100644 --- a/crates/node_binding/src/lib.rs +++ b/crates/node_binding/src/lib.rs @@ -399,6 +399,10 @@ fn init() { thread, }; + unsafe { + sftrace_setup::setup(); + } + panic::install_panic_handler(); // control the number of blocking threads, similar as https://github.com/tokio-rs/tokio/blob/946401c345d672d357693740bc51f77bc678c5c4/tokio/src/loom/std/mod.rs#L93 const ENV_BLOCKING_THREADS: &str = "RSPACK_BLOCKING_THREADS"; diff --git a/crates/rspack_allocator/Cargo.toml b/crates/rspack_allocator/Cargo.toml index ab2a0aeddc92..09b53ce10730 100644 --- a/crates/rspack_allocator/Cargo.toml +++ b/crates/rspack_allocator/Cargo.toml @@ -7,6 +7,9 @@ repository = "https://github.com/web-infra-dev/rspack" version = "0.2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +sftrace-setup = { path = "../../../sftrace/setup" } + [target.'cfg(target_os = "linux")'.dependencies] # Turned on `local_dynamic_tls` to avoid issue: https://github.com/microsoft/mimalloc/issues/147 mimalloc = { workspace = true, features = ["local_dynamic_tls"] } diff --git a/crates/rspack_allocator/src/lib.rs b/crates/rspack_allocator/src/lib.rs index 8e6d227a4fc6..277e1d220a58 100644 --- a/crates/rspack_allocator/src/lib.rs +++ b/crates/rspack_allocator/src/lib.rs @@ -1,3 +1,4 @@ #[global_allocator] #[cfg(not(any(miri, target_family = "wasm")))] -static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; +static A: sftrace_setup::SftraceAllocator = + sftrace_setup::SftraceAllocator(mimalloc::MiMalloc); diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 22eee8f3cb0f..7968dd60f531 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,3 @@ [toolchain] profile = "default" -# Use nightly for better access to the latest Rust features. channel = "nightly-2025-05-30" From 4ba472f3d68da94ca008e7d26bddf963bf33b93d Mon Sep 17 00:00:00 2001 From: quininer Date: Tue, 10 Jun 2025 15:42:43 +0800 Subject: [PATCH 2/2] chore: Enable sftrace for release-debug profile --- .cargo/config.toml | 3 --- Cargo.lock | 3 ++- Cargo.toml | 1 + crates/node_binding/Cargo.toml | 4 +--- crates/node_binding/scripts/build.js | 15 +++++++++++++-- crates/node_binding/src/lib.rs | 7 +++++-- crates/rspack_allocator/Cargo.toml | 3 --- crates/rspack_allocator/src/lib.rs | 3 +-- rust-toolchain.toml | 1 + 9 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index b326b648660b..5487a61bbce6 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -33,8 +33,6 @@ rustflags = [ "-Aclippy::default_constructed_unit_structs", "-Zshare-generics=y", # make the current crate share its generic instantiations "-Zthreads=8", # parallel frontend https://blog.rust-lang.org/2023/11/09/parallel-rustc.html - - "-Zinstrument-xray=always" ] # Fix napi breaking in test environment @@ -56,7 +54,6 @@ rustflags = [ "-C", "link-args=/DEFAULTLIB:ucrt.lib" ] - [target.wasm32-wasip1-threads] rustflags = ["--cfg", "tokio_unstable"] diff --git a/Cargo.lock b/Cargo.lock index 016704e27435..24ac34892cca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4063,7 +4063,6 @@ name = "rspack_allocator" version = "0.2.0" dependencies = [ "mimalloc-rspack", - "sftrace-setup", ] [[package]] @@ -5868,6 +5867,8 @@ dependencies = [ [[package]] name = "sftrace-setup" version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e3fd16d5db34706ab7200fb106bb1006f1801d1af9efabd65fb78ad8b9c8b0e" [[package]] name = "sha1" diff --git a/Cargo.toml b/Cargo.toml index 90d7aebd64e3..75b69fa55023 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,6 +87,7 @@ url = { version = "2.5.4" } urlencoding = { version = "2.1.3" } ustr = { package = "ustr-fxhash", version = "1.0.1" } xxhash-rust = { version = "0.8.14" } +sftrace-setup = { version = "0.1.0" } # Pinned napi = { version = "3.0.0-beta.5" } diff --git a/crates/node_binding/Cargo.toml b/crates/node_binding/Cargo.toml index aeb8496de395..493ba7f074f5 100644 --- a/crates/node_binding/Cargo.toml +++ b/crates/node_binding/Cargo.toml @@ -14,7 +14,6 @@ crate-type = ["cdylib"] plugin = ["rspack_loader_swc/plugin"] [dependencies] -sftrace-setup = { path = "../../../sftrace/setup" } anyhow = { workspace = true } browserslist-rs = { workspace = true } ropey = { workspace = true } @@ -112,9 +111,8 @@ ustr = { workspace = true } [target.'cfg(not(target_family = "wasm"))'.dependencies] tokio = { workspace = true, features = ["parking_lot"] } - rspack_tracing = { workspace = true } - +sftrace-setup = { workspace = true, optional = true } [build-dependencies] napi-build = { workspace = true } diff --git a/crates/node_binding/scripts/build.js b/crates/node_binding/scripts/build.js index d5538f592144..9f64540444db 100644 --- a/crates/node_binding/scripts/build.js +++ b/crates/node_binding/scripts/build.js @@ -38,6 +38,9 @@ async function build() { "--pipe", `"node ./scripts/dts-header.js"` ]; + let features = []; + let envs = { ...process.env }; + if (values.profile) { args.push("--profile", values.profile); } @@ -52,15 +55,23 @@ async function build() { } if (!process.env.DISABLE_PLUGIN) { args.push("--no-default-features"); - args.push("--features plugin"); + features.push("plugin"); } args.push("--no-dts-cache"); + if (values.profile === "release-debug") { + features.push("sftrace-setup"); + envs.RUSTFLAGS = "-Zinstrument-xray=always"; + } + if (features.length) { + args.push("--features " + features.join(",")); + } console.log(`Run command: napi ${args.join(" ")}`); let cp = spawn("napi", args, { stdio: "inherit", - shell: true + shell: true, + env: envs, }); cp.on("error", reject); diff --git a/crates/node_binding/src/lib.rs b/crates/node_binding/src/lib.rs index 5364b7148d11..d5255eb9ea30 100644 --- a/crates/node_binding/src/lib.rs +++ b/crates/node_binding/src/lib.rs @@ -399,8 +399,11 @@ fn init() { thread, }; - unsafe { - sftrace_setup::setup(); + #[cfg(feature = "sftrace-setup")] + if std::env::var_os("SFTRACE_OUTPUT_FILE").is_some() { + unsafe { + sftrace_setup::setup(); + } } panic::install_panic_handler(); diff --git a/crates/rspack_allocator/Cargo.toml b/crates/rspack_allocator/Cargo.toml index 09b53ce10730..ab2a0aeddc92 100644 --- a/crates/rspack_allocator/Cargo.toml +++ b/crates/rspack_allocator/Cargo.toml @@ -7,9 +7,6 @@ repository = "https://github.com/web-infra-dev/rspack" version = "0.2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -sftrace-setup = { path = "../../../sftrace/setup" } - [target.'cfg(target_os = "linux")'.dependencies] # Turned on `local_dynamic_tls` to avoid issue: https://github.com/microsoft/mimalloc/issues/147 mimalloc = { workspace = true, features = ["local_dynamic_tls"] } diff --git a/crates/rspack_allocator/src/lib.rs b/crates/rspack_allocator/src/lib.rs index 277e1d220a58..8e6d227a4fc6 100644 --- a/crates/rspack_allocator/src/lib.rs +++ b/crates/rspack_allocator/src/lib.rs @@ -1,4 +1,3 @@ #[global_allocator] #[cfg(not(any(miri, target_family = "wasm")))] -static A: sftrace_setup::SftraceAllocator = - sftrace_setup::SftraceAllocator(mimalloc::MiMalloc); +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 7968dd60f531..22eee8f3cb0f 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,4 @@ [toolchain] profile = "default" +# Use nightly for better access to the latest Rust features. channel = "nightly-2025-05-30"