Unverified Commit 27438295 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

switch to incremental loading

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 859de65b
...@@ -10150,6 +10150,7 @@ dependencies = [ ...@@ -10150,6 +10150,7 @@ dependencies = [
name = "turbopack-trace-server-wasm" name = "turbopack-trace-server-wasm"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"flate2",
"napi", "napi",
"napi-build", "napi-build",
"napi-derive", "napi-derive",
......
...@@ -14,6 +14,7 @@ bench = false ...@@ -14,6 +14,7 @@ bench = false
workspace = true workspace = true
[dependencies] [dependencies]
flate2 = { workspace = true }
napi = { version = "3.10.0", default-features = false, features = ["napi3"] } napi = { version = "3.10.0", default-features = false, features = ["napi3"] }
napi-derive = { workspace = true } napi-derive = { workspace = true }
rayon = { workspace = true } rayon = { workspace = true }
......
# Turbopack trace server WASM # Turbopack trace server WASM
This crate exposes the transport-independent Turbopack trace viewer engine through NAPI-RS v3's browser WASM mode. It is intended to let a browser or Web Worker load a completed trace without starting the native localhost WebSocket server. This crate exposes the transport-independent Turbopack trace viewer engine through NAPI-RS v3's browser WASM mode. It is intended to let a browser or Web Worker load a trace without starting the native localhost WebSocket server.
The exported `TurbopackTraceServer` class accepts a complete raw or gzip-compressed trace as a `Uint8Array`. Its `handleMessage()` method accepts the existing trace viewer client JSON protocol and returns the corresponding server messages in protocol order. The exported `TurbopackTraceServer` class incrementally accepts raw or gzip-compressed trace chunks through its `read()` method. Its `handleMessage()` method accepts the existing trace viewer client JSON protocol and returns the corresponding server messages in protocol order. The store can be queried while more trace data is still arriving.
Loading is synchronous, so pass an optional progress callback to the constructor before parsing starts. It runs at most once every 250 milliseconds and always receives a final update with `done: true`: Each `read()` call parses synchronously. Pass an optional progress callback to the constructor to receive updates at most once every 250 milliseconds:
```js ```js
const server = new TurbopackTraceServer(trace, (progress) => { const server = new TurbopackTraceServer((progress) => {
worker.postMessage({ type: 'trace-load-progress', progress }) worker.postMessage({ type: 'trace-load-progress', progress })
}) })
for await (const chunk of traceStream) {
server.read(chunk)
}
``` ```
The callback receives `bytesRead`, `totalBytes`, `uncompressedBytesRead`, `percentage`, `elapsedMs`, `bytesPerSecond`, `etaMs`, `stats`, and `done`. Because construction is synchronous, use the callback from a Web Worker if progress needs to update the browser UI while the trace is loading. The callback receives `bytesRead`, `uncompressedBytesRead`, `elapsedMs`, `bytesPerSecond`, and `stats`. The caller owns the stream and therefore also knows its total size and when loading is complete. Use the API from a Web Worker if parsing must not block the browser UI.
Zstd-compressed traces and live file tailing remain native-only. Zstd-compressed traces remain native-only.
## Build ## Build
......
const { readFileSync } = require('node:fs') const { createReadStream, statSync } = require('node:fs')
const { availableParallelism } = require('node:os') const { availableParallelism } = require('node:os')
const { const {
configureRayonThreadPool, configureRayonThreadPool,
...@@ -12,31 +12,50 @@ if (!tracePath) { ...@@ -12,31 +12,50 @@ if (!tracePath) {
process.exit(1) process.exit(1)
} }
const trace = readFileSync(tracePath)
const requestedThreadCount = Number.parseInt(process.env.RAYON_NUM_THREADS, 10) const requestedThreadCount = Number.parseInt(process.env.RAYON_NUM_THREADS, 10)
configureRayonThreadPool( configureRayonThreadPool(
requestedThreadCount > 0 ? requestedThreadCount : availableParallelism() requestedThreadCount > 0 ? requestedThreadCount : availableParallelism()
) )
new TurbopackTraceServer(trace, (progress) => { const size = statSync(tracePath).size
const line = progress.done
? `Initial read completed (${Math.floor( const server = new TurbopackTraceServer((progress) => {
progress.totalBytes / 1024 / 1024 const percentage = size > 0 ? (progress.bytesRead * 100) / size : 100
)} MB, ${(progress.elapsedMs / 1000).toFixed(1)}s) - ${progress.stats}` const line = `${percentage.toFixed(0)}% read (${(
: `${progress.percentage.toFixed(0)}% read (${( progress.bytesRead /
progress.bytesRead / 1024 /
1024 / 1024
1024 ).toFixed(0)}/${(size / 1024 / 1024).toFixed(0)} MB, ${(
).toFixed(0)}/${(progress.totalBytes / 1024 / 1024).toFixed( progress.bytesPerSecond /
0 1024 /
)} MB, ${(progress.bytesPerSecond / 1024 / 1024).toFixed(0)} MB/s) - ${ 1024
progress.stats ).toFixed(0)} MB/s) - ${progress.stats}`
}`
if (process.stderr.isTTY) { if (process.stderr.isTTY) {
process.stderr.write(`\r\x1b[2K${line}${progress.done ? '\n' : ''}`) process.stderr.write(`\r\x1b[2K${line}`)
} else { } else {
console.error(line) console.error(line)
} }
}) })
console.log(`Loaded trace file: ${tracePath}`) async function load() {
const started = performance.now()
for await (const chunk of createReadStream(tracePath, {
highWaterMark: 4 * 1024 * 1024,
})) {
server.read(chunk)
}
const line = `Initial read completed (${Math.floor(
size / 1024 / 1024
)} MB, ${((performance.now() - started) / 1000).toFixed(1)}s)`
if (process.stderr.isTTY) {
process.stderr.write(`\r\x1b[2K${line}\n`)
} else {
console.error(line)
}
console.log(`Loaded trace file: ${tracePath}`)
}
load().catch((error) => {
console.error(error)
process.exitCode = 1
})
use std::{ use std::{
ops::ControlFlow, io::Write,
sync::Mutex, sync::Mutex,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use napi::{ use napi::{
Result, Result,
bindgen_prelude::{Function, Uint8Array}, bindgen_prelude::{Env, Function, FunctionRef, Uint8Array},
}; };
use napi_derive::napi; use napi_derive::napi;
use turbopack_trace_server::{ use turbopack_trace_server::{
protocol::ProtocolSession, read_trace_bytes, read_trace_bytes_with_progress, TraceParser, protocol::ProtocolSession, store_container::StoreContainer,
}; };
#[cfg(target_family = "wasm")] #[cfg(target_family = "wasm")]
...@@ -60,91 +60,90 @@ pub fn configure_rayon_thread_pool(thread_count: u32) -> Result<u32> { ...@@ -60,91 +60,90 @@ pub fn configure_rayon_thread_pool(thread_count: u32) -> Result<u32> {
#[napi(object)] #[napi(object)]
pub struct TraceLoadProgress { pub struct TraceLoadProgress {
pub bytes_read: f64, pub bytes_read: f64,
pub total_bytes: f64,
pub uncompressed_bytes_read: f64, pub uncompressed_bytes_read: f64,
pub percentage: f64,
pub elapsed_ms: f64, pub elapsed_ms: f64,
pub bytes_per_second: f64, pub bytes_per_second: f64,
pub eta_ms: Option<f64>,
pub stats: String, pub stats: String,
pub done: bool, }
enum Compression {
Undetermined(Vec<u8>),
Raw,
Gzip(flate2::write::GzDecoder<Vec<u8>>),
} }
/// In-memory implementation of the turbo trace viewer protocol. /// In-memory implementation of the turbo trace viewer protocol.
#[napi] #[napi]
pub struct TurbopackTraceServer { pub struct TurbopackTraceServer {
session: ProtocolSession, session: ProtocolSession,
parser: TraceParser,
compression: Compression,
on_progress: Option<FunctionRef<TraceLoadProgress, ()>>,
started: Instant,
last_reported: Option<Duration>,
bytes_read: usize,
uncompressed_bytes_read: usize,
} }
#[napi] #[napi]
impl TurbopackTraceServer { impl TurbopackTraceServer {
/// Loads a complete raw or gzip-compressed trace, optionally reporting progress. /// Creates an empty trace server. Feed raw or gzip-compressed bytes with `read`.
#[napi(constructor)] #[napi(constructor)]
pub fn new( pub fn new(on_progress: Option<Function<'_, TraceLoadProgress, ()>>) -> Result<Self> {
trace: Uint8Array, let on_progress = on_progress
on_progress: Option<Function<'_, TraceLoadProgress, ()>>, .map(|on_progress| on_progress.create_ref())
) -> Result<Self> { .transpose()?;
let started = Instant::now(); let store = std::sync::Arc::new(StoreContainer::new());
let mut last_reported = Duration::ZERO; let parser = TraceParser::new(store.clone());
let mut callback_error = None;
let store = if let Some(on_progress) = on_progress {
read_trace_bytes_with_progress(trace.as_ref(), |progress| {
let elapsed = started.elapsed();
if !progress.done && elapsed.saturating_sub(last_reported) < PROGRESS_INTERVAL {
return ControlFlow::Continue(());
}
last_reported = elapsed;
let bytes_read = progress.bytes_read as f64;
let total_bytes = progress.total_bytes as f64;
let elapsed_ms = elapsed.as_secs_f64() * 1000.0;
let bytes_per_second = if elapsed_ms > 0.0 {
bytes_read * 1000.0 / elapsed_ms
} else {
0.0
};
let eta_ms = if bytes_read > 0.0 && bytes_read < total_bytes {
Some(elapsed_ms * (total_bytes - bytes_read) / bytes_read)
} else {
None
};
if let Err(error) = on_progress.call(TraceLoadProgress {
bytes_read,
total_bytes,
uncompressed_bytes_read: progress.uncompressed_bytes_read as f64,
percentage: if total_bytes > 0.0 {
bytes_read * 100.0 / total_bytes
} else {
100.0
},
elapsed_ms,
bytes_per_second,
eta_ms,
stats: progress.stats(),
done: progress.done,
}) {
callback_error = Some(error);
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
} else {
read_trace_bytes(trace.as_ref())
};
if let Some(error) = callback_error {
return Err(error);
}
let store = store.map_err(|error| {
napi::Error::from_reason(format!("failed to load Turbopack trace: {error:#}"))
})?;
Ok(Self { Ok(Self {
session: ProtocolSession::new(store), session: ProtocolSession::new(store),
parser,
compression: Compression::Undetermined(Vec::new()),
on_progress,
started: Instant::now(),
last_reported: None,
bytes_read: 0,
uncompressed_bytes_read: 0,
}) })
} }
/// Incrementally adds raw or gzip-compressed trace bytes.
#[napi]
pub fn read(&mut self, env: Env, trace: Uint8Array) -> Result<()> {
self.bytes_read += trace.len();
self.push_bytes(trace.as_ref()).map_err(|error| {
napi::Error::from_reason(format!("failed to read Turbopack trace: {error:#}"))
})?;
let elapsed = self.started.elapsed();
if self
.last_reported
.is_some_and(|last_reported| elapsed.saturating_sub(last_reported) < PROGRESS_INTERVAL)
{
return Ok(());
}
self.last_reported = Some(elapsed);
if let Some(on_progress) = &self.on_progress {
let elapsed_ms = elapsed.as_secs_f64() * 1000.0;
let bytes_read = self.bytes_read as f64;
on_progress.borrow_back(&env)?.call(TraceLoadProgress {
bytes_read,
uncompressed_bytes_read: self.uncompressed_bytes_read as f64,
elapsed_ms,
bytes_per_second: if elapsed_ms > 0.0 {
bytes_read * 1000.0 / elapsed_ms
} else {
0.0
},
stats: self.parser.stats(),
})?;
}
Ok(())
}
/// Handles one existing trace viewer client message and returns the /// Handles one existing trace viewer client message and returns the
/// serialized server messages in wire-protocol order. /// serialized server messages in wire-protocol order.
#[napi] #[napi]
...@@ -154,3 +153,46 @@ impl TurbopackTraceServer { ...@@ -154,3 +153,46 @@ impl TurbopackTraceServer {
}) })
} }
} }
impl TurbopackTraceServer {
fn push_bytes(&mut self, bytes: &[u8]) -> std::io::Result<()> {
if let Compression::Undetermined(prefix) = &mut self.compression {
prefix.extend_from_slice(bytes);
if prefix.len() < 4 {
return Ok(());
}
const GZIP_MAGIC: &[u8] = &[0x1f, 0x8b];
const ZSTD_MAGIC: &[u8] = &[0x28, 0xb5, 0x2f, 0xfd];
if prefix.starts_with(ZSTD_MAGIC) {
return Err(std::io::Error::other(
"zstd-compressed traces are not supported by the WASM trace engine",
));
}
let prefix = std::mem::take(prefix);
self.compression = if prefix.starts_with(GZIP_MAGIC) {
Compression::Gzip(flate2::write::GzDecoder::new(Vec::new()))
} else {
Compression::Raw
};
return self.push_bytes(&prefix);
}
let uncompressed = match &mut self.compression {
Compression::Raw => bytes.to_vec(),
Compression::Gzip(decoder) => {
decoder.write_all(bytes)?;
std::mem::take(decoder.get_mut())
}
Compression::Undetermined(_) => unreachable!(),
};
self.parser
.push(&uncompressed)
.map_err(std::io::Error::other)?;
self.uncompressed_bytes_read += uncompressed.len();
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