servo/components/profile/trace_dump.rs
Martin Robinson 312cf0df08
script: Create a CrossProcessInstant to enable serializable monotonic time (#33282)
Up until now, Servo was using a very old version of time to get a
cross-process monotonic timestamp (using `time::precise_time_ns()`).
This change replaces the usage of old time with a new serializable
monotonic time called `CrossProcessInstant` and uses it where `u64`
timestamps were stored before. The standard library doesn't provide this
functionality because it isn't something you can do reliably on all
platforms. The idea is that we do our best and then fall back
gracefully.

This is a big change, because Servo was using `u64` timestamps all over
the place some as raw values taken from `time::precise_time_ns()` and
some as relative offsets from the "navigation start," which is a concept
similar to DOM's `timeOrigin` (but not exactly the same). It's very
difficult to fix this situation without fixing it everywhere as the
`Instant` concept is supposed to be opaque. The good thing is that this
change clears up all ambiguity when passing times as a `time::Duration`
is unit agnostic and a `CrossProcessInstant` represents an absolute
moment in time.

The `time` version of `Duration` is used because it can both be negative
and is also serializable.

Good things:
 - No need too pass around `time` and `time_precise` any longer.
   `CrossProcessInstant` is also precise and monotonic.
 - The distinction between a time that is unset or at `0` (at some kind
   of timer epoch) is now gone.

There still a lot of work to do to clean up timing, but this is the
first step. In general, I've tried to preserve existing behavior, even
when not spec compliant, as much as possible. I plan to submit followup
PRs fixing some of the issues I've noticed.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2024-09-05 18:50:09 +00:00

80 lines
2.5 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! A module for writing time profiler traces out to a self contained HTML file.
use std::io::{self, Write};
use std::{fs, path};
use base::cross_process_instant::CrossProcessInstant;
use profile_traits::time::{ProfilerCategory, TimerMetadata};
use serde::Serialize;
/// An RAII class for writing the HTML trace dump.
#[derive(Debug)]
pub struct TraceDump {
file: fs::File,
}
#[derive(Debug, Serialize)]
struct TraceEntry {
category: ProfilerCategory,
metadata: Option<TimerMetadata>,
#[serde(rename = "startTime")]
start_time: u64,
#[serde(rename = "endTime")]
end_time: u64,
}
impl TraceDump {
/// Create a new TraceDump and write the prologue of the HTML file out to
/// disk.
pub fn new<P>(trace_file_path: P) -> io::Result<TraceDump>
where
P: AsRef<path::Path>,
{
let mut file = fs::File::create(trace_file_path)?;
write_prologue(&mut file)?;
Ok(TraceDump { file })
}
/// Write one trace to the trace dump file.
pub fn write_one(
&mut self,
category: &(ProfilerCategory, Option<TimerMetadata>),
start_time: CrossProcessInstant,
end_time: CrossProcessInstant,
) {
let entry = TraceEntry {
category: category.0,
metadata: category.1.clone(),
start_time: (start_time - CrossProcessInstant::epoch()).whole_nanoseconds() as u64,
end_time: (end_time - CrossProcessInstant::epoch()).whole_nanoseconds() as u64,
};
serde_json::to_writer(&mut self.file, &entry).unwrap();
writeln!(&mut self.file, ",").unwrap();
}
}
impl Drop for TraceDump {
/// Write the epilogue of the trace dump HTML file out to disk on
/// destruction.
fn drop(&mut self) {
write_epilogue(&mut self.file).unwrap();
}
}
fn write_prologue(file: &mut fs::File) -> io::Result<()> {
writeln!(file, "{}", include_str!("./trace-dump-prologue-1.html"))?;
writeln!(file, "{}", include_str!("./trace-dump.css"))?;
writeln!(file, "{}", include_str!("./trace-dump-prologue-2.html"))
}
fn write_epilogue(file: &mut fs::File) -> io::Result<()> {
writeln!(file, "{}", include_str!("./trace-dump-epilogue-1.html"))?;
writeln!(file, "{}", include_str!("./trace-dump.js"))?;
writeln!(file, "{}", include_str!("./trace-dump-epilogue-2.html"))
}