Timing diagnostics
Install a clock and the existing report plugin:
use mirui::app::plugins::{FpsSummaryPlugin, StdInstantClockPlugin};
app.add_plugin(StdInstantClockPlugin::default());
app.add_plugin(FpsSummaryPlugin::new(120).with_sink(|report| {
for operation in report.operations.iter().filter(|op| op.count != 0) {
println!("{:?}: wall {:?} ns, rate {:?}/s", operation.operation,
operation.wall_nanos(), report.operation_rate_hz(operation.operation));
}
}));new(120) reports every 120 closed App operations. Tick covers input, systems, drawing and platform maintenance. Hosts that deliver update and redraw separately produce separate Update and Draw samples. Direct render, render_dirty and snapshot calls produce Draw samples. Nested drawing inside a tick belongs to that tick and does not produce another operation sample.
Timing is collected on demand. Report plugins request it automatically, and an active Capture enables collection at the next outer App operation. Without either, applications that read timing resources or custom plugins that implement on_timing request it explicitly:
app.request_timings(true);
// Read FrameTimings / FrameStats through the existing resource APIs.
app.request_timings(false);The explicit request is independent of reporter and capture demand: releasing it does not turn off those consumers. An operation keeps the demand selected at entry, including nested drawing. With no demand, App skips timing clock reads, recording and publication, and never creates timing resources. When demand ends, existing FrameTimings becomes invalid and the FrameStats window becomes empty; retained storage is reused. An explicit release outside an operation clears results immediately if no other demand remains. A global capture ending is observed at each App's next operation boundary, before its business work. A copied historical sample remains a historical value.
Phase timings and ring summaries
| Phase / Metric | Accessor on FrameTimings / FrameStats | What It Measures |
|---|---|---|
input | timings.input() | Draining Surface::poll_event(), gesture recognition, and input/notification callbacks |
systems | timings.systems() | Scheduled #[system] and #[timer] execution, plus deferred commands!() commits |
layout | timings.layout() | Reactive signal flush, Flexbox/text measurement, and dirty-region collection (dirty.layout) |
render | timings.render() | Scene traversal, offscreen target production, and Renderer::submit |
flush | timings.flush() | Surface::begin_flush(), flush(PhysicalRect), end_flush(), and mirror_buffers |
seed | timings.seed() | Seeding previous visual coverage for next frame's partial-damage diffing |
| 256-Sample Ring | stats.summary(op, phases) | avg, min, max, nearest-rank P99, and jitter across the last 256 completed operations |
Operation wall time includes business post_render hooks and platform maintenance. It excludes the subsequent timing reports and host pacing. Instrumentation inside an operation remains included. Wall time includes blocking and time when the thread is not scheduled; it must not be described as CPU time. A long poll can wait for an earlier submission while still belonging to the operation that called it. Without submission and scheduling evidence, the cause of that wait is unknown.
FrameTimings records input, systems, layout, render, flush and previous-coverage seeding. Its accessors return Option<u64>: None means absent or invalid, while Some(0) is a measured zero. phase_wall_nanos() sums these measured phases, not all operation work. The difference from operation wall time is uncovered wall time, not automatically wasted computation. A failed attempt publishes its own attempted phases rather than repeating the preceding successful sample.
The layout phase includes dirty-region collection. Use dirty.layout and layout.* spans to establish whether layout construction, text measurement or solving actually ran. The render phase can contain bookkeeping on an idle operation with no accepted submission; its presence alone does not establish that pixels were drawn.
FrameStats::summary(operation, phases) selects an exact operation and phase set from one 256-sample ring. Only valid completed operations enter the ring. The result contains its own sample count, average, minimum, maximum, nearest-rank P99 and jitter. Different operation types or phase sets cannot be combined by this query. Missing CPU timing is unavailable; partial CPU sample counts must not be subtracted from a complete wall-time average.
Operation cadence counts callbacks in a half-open timestamp interval. Equal timestamps remain in the next window, and a missing or backward clock invalidates that interval. Callback cadence includes idle operations and does not measure GPU completion or display refresh. Presentation feedback requires its own timestamp and completion semantics; no display rate is inferred from phase sums, successful submissions or callback arrival time.
accepted_submission_rate_hz() counts actual App acceptances in that same interval. One operation may accept several submissions or none; a later failure does not erase an earlier acceptance. Events at the interval's end remain in the next interval, and events before the first covered endpoint are excluded. TimingSample::accepted_submissions exposes the operation's actual count, while FpsSummary::accepted_submissions is the covered window count. These counts do not imply native presentation or display completion.
Custom sinks receive the just-closed operation through the existing reporting plugins. Low-level plugins can implement on_timing and call app.request_timings(true) in build; installing a timing reporter also enables delivery. The read-only TimingSample refers to a closed snapshot, and FrameTimings in the World has already been updated. Business post_render retains its original ordering and sees the previous closed operation. Its render_nanos: Option<u64> argument is None when timing is disabled or the clock is unavailable; the hook still runs. Mutating application state inside a timing reporter is outside the measured operation.
Bounded traces
PerfReportPlugin owns the trace stream and drains registered producers after each closed operation. with_trace_budget(bytes) sets the per-producer diagnostic payload budget before installation: 128 KiB on std, 8 KiB on no_std. It includes recording storage, export scratch, encoding, aggregation and registration overhead. Allocator headers, active stack guards and user-owned sinks are outside this budget. Each extra producer conservatively reserves the same workspace allowance; the setting is not a process-wide memory limit. Capacity exhaustion drops records and increments counters instead of growing the buffer.
use mirui::core::perf::{self, Capture, DEFAULT_TRACE_BUDGET};
let mut capture = Capture::new(DEFAULT_TRACE_BUDGET)?;
{
let _span = perf::enter("assets.decode");
decode_assets();
}
capture.drain_events(|batch, events| {
// Borrowed until this callback returns. Empty batches carry track status.
consume(batch, events);
});
capture.stop();
capture.drain_events(|batch, events| consume(batch, events));Only one Capture can consume the process stream. A second capture returns CaptureError::InUse; a second report plugin logs a warning and leaves the existing consumer intact. The capture's thread is registered at construction. Other producer threads call perf::register_thread() before tracing. Registration may allocate; recording and draining reuse prepared storage. Unregistered producers count as lost records. A stopped capture rejects new scopes, and an old guard cannot write into a later capture. Stop and drain before dropping a direct capture to export its tail.
Capture::drain aggregates inclusive wall totals and optionally writes batches of complete newline-delimited Chrome JSON records. One operation can emit several batches. Sinks preserve their order and run outside recorder locks. Fractional microseconds preserve nanosecond timestamps. Capture::drain_events is the raw alternative; it does not also aggregate or encode the records it consumes. Copying a batch into application storage is outside the recorder budget.
Track IDs are capture-local logical identities, not operating-system thread IDs. Sequence numbers preserve close order, including gaps caused by loss. trace.status and trace.capture expose dropped records, invalid clocks or nesting, open scopes, and aggregation or encoding loss. Missing CPU, GPU or display measurements remain unavailable. An open scope can cross a drain boundary; a single batch is not necessarily a complete tree. Truncated trees cannot yield exact uncovered wall time. Inclusive totals overlap and must not be added together.
Software sw.blit spans cover raw draws and prepared texture submissions. Time outside a parent's measured child spans can include uninstrumented drawing and preparation; it is not automatically framework dispatch overhead. Check category coverage before assigning that residual time to a specific kind of work.
With perf enabled, text_input.native_state includes lifecycle preparation before reading the platform editor state. lifecycle.prepare separates reactive.flush, attachment maintenance and View reconciliation. Reactive spans distinguish reclamation, batched callback execution and dirty marking; reactive.order is nested inside the callback batch when retained-tree ordering is required. text_input.reconcile covers editor-session coordination inside text_input.flush. These spans describe work performed at the polling boundary, including application callbacks, rather than attributing the entire boundary to IME processing or backend polling. Records follow batches and passes, not individual widgets or Effects. Nested totals overlap, and callback counts cannot be inferred from span counts. These additional guards are absent when perf is disabled.
Web and bare-metal exports use pid: 0 and process_identity: "source-local" because no native process ID is available. Analyze independent exports separately or assign source namespaces before combining them; equal capture and track numbers do not identify the same browser instance or device. Native process IDs also require capture-lifetime context and are not permanent machine identities.
Operations, submissions and feedback
The same bounded stream contains RecordData variants for spans, operation contexts, submission attempts, waits, App acceptance and native feedback. Use record.span() when consuming only ordinary spans. An operation references its App and caller. A submission has its own capture-scoped identity; multiple requests and failed attempts are not collapsed into a frame number. app.accepted reports the existing App acceptance boundary and does not mean display completion.
Backends can attach a diagnostic token to an existing pending native request without changing the Surface trait:
let attempt = perf::submission("driver.submit");
let token = attempt.id();
match driver.submit() {
Ok(request) => {
pending.attach_trace(request, token);
attempt.finish(perf::SubmissionOutcome::Issued);
}
Err(error) => {
attempt.finish(perf::SubmissionOutcome::Failed);
return Err(error);
}
}
// In a later operation, use the token belonging to this particular request.
let _wait = perf::wait("driver.wait_previous", pending.trace_id());
driver.wait_previous()?;pending above is the backend's existing request storage, not a required Mirui interface. Dropping an unfinished attempt records Abandoned. A wait with no token records an unknown cause. The wait's wall interval stays in the operation that executes it, while waits_on points to the earlier request. Tokens from an ended capture cannot associate events with a new capture.
perf::feedback distinguishes GPU completion, presentation feedback, buffer release and transfer completion. perf::display_presented records presentation feedback specifically. Pass a PlatformTimestamp with a static ClockDomain, nonzero tick frequency and source epoch; native timestamps remain separate from the callback's Mirui observation time. The caller must supply the actual request's token and display identity. Clock names do not establish calibration, and sequence gaps do not create extra presented images. No pending map, device wait or completion callback is introduced by recording these events.
Record capacity depends on the compiled record layout and the configured byte budget. Capture::event_capacity() exposes it. Detailed captures can require a larger explicit budget or fewer probes; adding evidence never silently expands the configured storage.
Producer registration initializes the existing buffer mutex before exposing the producer, so platform lock initialization occurs during setup rather than the first measured recording call. Additional threads still register explicitly before tracing.
Backend evidence
With the perf feature, output.target records instantaneous offscreen decisions in the existing bounded stream. Unchanged reuses completed pixels; Reused reuses writable storage; Allocated creates storage. NotOffered means the renderer returned no native plan and does not identify why. CpuConsumerPreferred records a deliberate CPU choice for a pending source of a CPU consumer, even though a native plan was offered. Budget, ArithmeticOverflow, WorkspaceUnavailable, WorkspaceAllocation, AllocationFailed, Busy, HistoryFrozen and BackendError identify the observed rejection or failure. WorkingSetProtected means the native request would fit if capacity reserved for remaining outputs were available; it is rejected to protect that work. Reserved capacity reduces admission availability but does not represent resident pixels or GPU memory. A native rejection can precede a successful CPU allocation; count the final selection separately from rejected attempts. Backend errors still propagate without replaying the View.
These records include physical dimensions, effective scale, current history policy and the writable slot when known. resource is an exact decimal string packing the full entity ID in its high 32 bits and generation in its low 32 bits; combine it with the App from the associated operation context. A string preserves all 64 identity bits in JSON consumers. An operation of zero is unassociated and cannot establish a cross-App identity. Unknown resources, slots and requested byte counts export as null. An arithmetic overflow is unknown size, not zero work.
available_bytes is the output budget available at the decision point, including reclaimable old storage for an admission check. Native budget rejection compares additional storage, paint workspace and binding-workspace growth with that availability. Workspace rejection reports workspace capacity requirements. Allocated reports the resulting texture charge and remaining budget; Reused and Unchanged request zero new texture bytes. These figures do not measure driver memory, uploaded bytes or GPU execution.
sdl.target.workspace reports SDL destination-fallback workspace rejection separately from the output pool: missing workspace or the required and available fallback bytes. It has no entity or history-slot identity; dimensions are physical pixels and scale is one. Correlate it by operation and dimensions without claiming an exact resource association. This event can explain an output.target NotOffered result; third-party backends returning None remain unspecified unless they provide evidence. All built-in decision probes are compiled out without perf; recording and export use the existing byte limit and loss counters.
draw.offscreen.software and draw.offscreen.native split the existing production span by its selected renderer. sdl.texture.create measures image upload storage creation; sdl.texture.upload measures the shared image-source upload call, including required row copying or byte swapping. sdl.target.readback measures each native read call. Target editing separates the complete sdl.target.edit interval, its sdl.target.edit.body callback and sdl.target.writeback upload. All are inclusive calling-thread wall intervals: source preparation, driver blocking and nested calls may be included. They are not independent CPU samples and overlapping spans must not be added together.
The corresponding sdl.texture.upload.bytes, sdl.target.readback.bytes and sdl.target.writeback.bytes records describe individual native calls. payload_bytes counts the attempted packed logical pixel payload, excluding row padding and driver-side format conversion; invalid size arithmetic exports null. Issued means the synchronous native call returned success, while Failed means it returned an error. Neither indicates GPU execution or presentation completion. Split readback rows produce separate records. Records carry the current operation but no source, entity or submission identity. Image-source uploads and target edits are covered; glyph-atlas and generated-gradient texture creation are not included in these byte totals. Recording uses the same bounded stream and loss counters, so missing records cannot establish that no transfer occurred.
SDL software and GPU presentation record submission attempts at the native present call; SDL GPU records its explicit queue flush separately. WGPU records queue submissions, presentation calls and surface acquisition. Acquisition has no exact request association. Synchronous WGPU readback associates its existing device poll with the actual readback submission. These scopes measure calls, not GPU execution or display completion, and do not add waits or callbacks.
Linux DRM retains a trace token beside its existing pending page flip. Poll retries refer to that token until the matching CRTC event retires it. The event retains the kernel timestamp domain (drm.monotonic or drm.realtime), its 32-bit native refresh sequence and the separately sampled callback time. Unknown timestamp capability produces no presentation measurement. Display identities identify one surface's CRTC stream within a capture; reopening the surface or changing capture starts a distinct stream. Modesets and direct framebuffer damage are issued calls without presentation feedback. A capture ending with an outstanding flip cannot establish a complete display-rate window.
Web Canvas acknowledgement and embedded framebuffer flushes retain their existing App acceptance and wall spans. They do not provide native presentation feedback. Browser animation callbacks are scheduling events, and a completed display transfer does not establish scanout timing. Third-party backends retain the existing Surface contract; optional evidence uses the same recorder functions above.
Thread CPU time
FpsSummaryPlugin::with_thread_cpu_time(true) enables CPU samples at operation boundaries without requiring detailed traces. PerfReportPlugin::with_thread_cpu_time(true) also enables them for traced submission and wait scopes. A direct capture uses capture.set_thread_cpu_time(true). Ordinary draw spans do not read the thread CPU clock. CPU collection is disabled by default.
Linux, Android, macOS and iOS use CLOCK_THREAD_CPUTIME_ID; Windows adds the current thread's kernel and user times from GetThreadTimes. Failed reads, clock rollback and unsupported platforms return no sample. Web and bare-metal targets do not substitute wall time or cycle counts. Available native thread IDs are exported separately from logical track IDs for matching external scheduling evidence. A native ID is meaningful only within the recorded process and capture lifetime.
TimingSample::instrumented and each summary's instrumented_operations identify operations with detailed tracing enabled. Inline instrumentation remains in their wall and CPU measurements. thread_cpu_nanos() on a summary requires complete CPU sample coverage; partial_thread_cpu_nanos() and cpu_samples() expose partial coverage explicitly. Wall minus CPU is only a difference between measurements, not a measurement of GPU execution or a specific wait cause. Actual scheduling intervals require separate system evidence.
Observer cost
probe.aggregate, probe.encode and probe.sink report accumulated wall time over disjoint measured intervals of one drain. probe.encode excludes sink calls; probe.drain is the remaining measured drain wall time after those intervals. They are counters with intervals: disjoint-total, not contiguous execution slices or CPU measurements. probe.report measures the summary callback separately. These records carry the closed operation's identity when produced by its timing hook.
Observer records are delivered by a later drain. Exporting only observer records does not generate another observer report. Stopping and draining exports the known tail, but the last sink call itself has no later observation; metadata states that this tail is uncovered. Recorder append, clock reads and probe branches inside business operations remain included in instrumented measurements and require matched enabled/disabled runs to quantify.