Measure native editor worker CPU at preview phase boundaries

This commit is contained in:
gloamxun committed 2026-12-02 04:25:44 +08:00
1 parent 10e497e6e0
commit 9f556d6a5f
5 files changed
+99 -17

No files matched your search

+1
View File
@@ -298,6 +298,7 @@ async function compile(edit, submitted, changes) {
engineMs: result.ready_ms,
admissionAndQueueMs: result.admission_and_queue_ms,
phases: result.phases,
workerUsage: result.worker_usage,
visibleMs: performance.now() - submitted,
steps: result.steps,
reusedSteps: result.reused_steps,
+46 -17
View File
@@ -21,6 +21,7 @@ use texrra_tex_core::{
execution::InputProfile,
resources::{InputPolicy, ResourceKind, SearchPath, WorldConfig},
};
use thread_usage::ThreadUsage;
use tiny_http::{Header, Method, Request, Response, Server};
mod library;
@@ -30,6 +31,7 @@ mod preview_socket;
mod projects;
mod resource_capture;
mod saved_project;
mod thread_usage;
const MAX_EDIT_BODY: u64 = 2 * 1024 * 1024;
const PAGE: &str = include_str!("index.html");
@@ -87,9 +89,16 @@ struct Work {
preview_draft: bool,
}
#[derive(Clone, Copy, Default)]
struct StageTiming {
wall_ms: f64,
worker: Option<ThreadUsage>,
}
struct EditorObserver {
started: Instant,
ended: Mutex<[f64; 5]>,
initial_worker_usage: Option<ThreadUsage>,
ended: Mutex<[StageTiming; 5]>,
queue: Arc<Queue>,
respond_to: Mutex<Option<PreviewReply>>,
sequence: u64,
@@ -107,7 +116,7 @@ impl CompileObserver for EditorObserver {
CompileStage::Exported => 3,
CompileStage::ReadyToPublish => 4,
};
self.ended.lock().unwrap()[index] = self.elapsed_ms();
self.ended.lock().unwrap()[index] = self.measure_stage();
Ok(())
}
@@ -157,6 +166,14 @@ impl EditorObserver {
self.started.elapsed().as_secs_f64() * 1000.0
}
fn measure_stage(&self) -> StageTiming {
let wall_ms = self.elapsed_ms();
StageTiming {
wall_ms,
worker: self.initial_worker_usage.and_then(|_| ThreadUsage::current()),
}
}
fn is_current(&self) -> bool {
self.sequence == self.queue.current.load(Ordering::Acquire)
}
@@ -168,27 +185,27 @@ impl EditorObserver {
work: &EntryCheckpointWork,
compiled: Option<f64>,
) -> Value {
let ready = self.elapsed_ms();
let ready = self.measure_stage();
let pending = compiled.is_none();
let [prepared, executed, fonts, exported, publish_ready] = *self.ended.lock().unwrap();
let exported = (exported > 0.0).then_some(exported);
let publish_ready = (publish_ready > 0.0).then_some(publish_ready);
let compiled = compiled.or(publish_ready);
json!({
let exported_ms = (exported.wall_ms > 0.0).then_some(exported.wall_ms);
let publish_ready_ms = (publish_ready.wall_ms > 0.0).then_some(publish_ready.wall_ms);
let compiled = compiled.or(publish_ready_ms);
let mut payload = json!({
"sequence": self.sequence,
"update": update,
"pending_publication": pending,
"compile_ms": compiled,
"ready_ms": ready,
"ready_ms": ready.wall_ms,
"admission_and_queue_ms": self.started.duration_since(self.received).as_secs_f64() * 1000.0,
"phases": {
"prepare_ms": prepared,
"execute_ms": executed - prepared,
"layout_and_fonts_ms": fonts - executed,
"pdf_ms": exported.map(|end| end - fonts),
"draft_ms": publish_ready.zip(exported).map(|(end, start)| end - start),
"seal_and_publish_ms": compiled.zip(publish_ready).map(|(end, start)| end - start),
"svg_ms": ready - compiled.unwrap_or(fonts),
"prepare_ms": prepared.wall_ms,
"execute_ms": executed.wall_ms - prepared.wall_ms,
"layout_and_fonts_ms": fonts.wall_ms - executed.wall_ms,
"pdf_ms": exported_ms.map(|end| end - fonts.wall_ms),
"draft_ms": publish_ready_ms.zip(exported_ms).map(|(end, start)| end - start),
"seal_and_publish_ms": compiled.zip(publish_ready_ms).map(|(end, start)| end - start),
"svg_ms": ready.wall_ms - compiled.unwrap_or(fonts.wall_ms),
},
"steps": steps,
"reused_steps": work.reused_steps,
@@ -198,7 +215,17 @@ impl EditorObserver {
.saturating_add(work.discarded_suffix_attempt_steps),
"reused_frontier": work.reused_frontier,
"replayed_calls": work.replayed_calls,
})
});
if let Some(initial) = self.initial_worker_usage {
payload["worker_usage"] = json!({
"prepare": ThreadUsage::between(Some(initial), prepared.worker),
"execute": ThreadUsage::between(prepared.worker, executed.worker),
"layout_and_fonts": ThreadUsage::between(executed.worker, fonts.worker),
"export": ThreadUsage::between(fonts.worker, exported.worker),
"ready": ThreadUsage::between(Some(initial), ready.worker),
});
}
payload
}
}
@@ -892,6 +919,7 @@ fn prepare(
}
fn worker(queue: &Arc<Queue>) {
let measure_worker = std::env::var_os("TEXRRA_EDITOR_THREAD_USAGE").is_some_and(|value| value == "1");
let mut resources = resource_capture::ResourceCapture::default();
loop {
let work = queue
@@ -915,7 +943,8 @@ fn worker(queue: &Arc<Queue>) {
};
let observer = Arc::new(EditorObserver {
started: Instant::now(),
ended: Mutex::new([0.0; 5]),
initial_worker_usage: measure_worker.then(ThreadUsage::current).flatten(),
ended: Mutex::new([StageTiming::default(); 5]),
queue: queue.clone(),
respond_to: Mutex::new(Some(respond_to)),
sequence,
+48
View File
@@ -0,0 +1,48 @@
//! Per-compilation worker counters, sampled only when requested.
use serde::Serialize;
#[derive(Clone, Copy, Serialize)]
pub(super) struct ThreadUsage {
cpu_ms: f64,
voluntary_switches: u64,
involuntary_switches: u64,
block_inputs: u64,
block_outputs: u64,
}
impl ThreadUsage {
#[cfg(target_os = "linux")]
pub(super) fn current() -> Option<Self> {
let mut usage = std::mem::MaybeUninit::<libc::rusage>::uninit();
// A successful RUSAGE_THREAD call initializes the provided struct.
if unsafe { libc::getrusage(libc::RUSAGE_THREAD, usage.as_mut_ptr()) } != 0 {
return None;
}
let usage = unsafe { usage.assume_init() };
let millis = |time: libc::timeval| time.tv_sec as f64 * 1000.0 + time.tv_usec as f64 / 1000.0;
Some(Self {
cpu_ms: millis(usage.ru_utime) + millis(usage.ru_stime),
voluntary_switches: usage.ru_nvcsw as u64,
involuntary_switches: usage.ru_nivcsw as u64,
block_inputs: usage.ru_inblock as u64,
block_outputs: usage.ru_oublock as u64,
})
}
#[cfg(not(target_os = "linux"))]
pub(super) fn current() -> Option<Self> {
None
}
pub(super) fn between(start: Option<Self>, end: Option<Self>) -> Option<Self> {
let start = start?;
let end = end?;
Some(Self {
cpu_ms: end.cpu_ms - start.cpu_ms,
voluntary_switches: end.voluntary_switches.checked_sub(start.voluntary_switches)?,
involuntary_switches: end.involuntary_switches.checked_sub(start.involuntary_switches)?,
block_inputs: end.block_inputs.checked_sub(start.block_inputs)?,
block_outputs: end.block_outputs.checked_sub(start.block_outputs)?,
})
}
}