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>
This commit is contained in:
Martin Robinson 2024-09-05 11:50:09 -07:00 committed by GitHub
parent 35baf056f6
commit 312cf0df08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 854 additions and 665 deletions

View file

@ -9,6 +9,7 @@ use std::sync::{Arc as StdArc, Condvar, Mutex, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use async_recursion::async_recursion;
use base::cross_process_instant::CrossProcessInstant;
use base::id::{HistoryStateId, PipelineId};
use crossbeam_channel::Sender;
use devtools_traits::{
@ -116,10 +117,6 @@ impl Default for HttpState {
}
}
fn precise_time_ms() -> u64 {
time::precise_time_ns() / (1000 * 1000)
}
// Step 3 of https://fetch.spec.whatwg.org/#concept-fetch.
pub fn set_default_accept(destination: Destination, headers: &mut HeaderMap) {
if headers.contains_key(header::ACCEPT) {
@ -353,19 +350,22 @@ fn prepare_devtools_request(
headers: HeaderMap,
body: Option<Vec<u8>>,
pipeline_id: PipelineId,
now: SystemTime,
connect_time: u64,
send_time: u64,
connect_time: Duration,
send_time: Duration,
is_xhr: bool,
) -> ChromeToDevtoolsControlMsg {
let started_date_time = SystemTime::now();
let request = DevtoolsHttpRequest {
url,
method,
headers,
body,
pipeline_id,
started_date_time: now,
time_stamp: now.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs() as i64,
started_date_time,
time_stamp: started_date_time
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64,
connect_time,
send_time,
is_xhr,
@ -614,7 +614,7 @@ async fn obtain_response(
// TODO(#21261) connect_start: set if a persistent connection is *not* used and the last non-redirected
// fetch passes the timing allow check
let connect_start = precise_time_ms();
let connect_start = CrossProcessInstant::now();
context
.timing
.lock()
@ -638,7 +638,7 @@ async fn obtain_response(
};
*request.headers_mut() = headers.clone();
let connect_end = precise_time_ms();
let connect_end = CrossProcessInstant::now();
context
.timing
.lock()
@ -649,7 +649,7 @@ async fn obtain_response(
let pipeline_id = *pipeline_id;
let closure_url = url.clone();
let method = method.clone();
let send_start = precise_time_ms();
let send_start = CrossProcessInstant::now();
let host = request.uri().host().unwrap_or("").to_owned();
let override_manager = context.state.override_manager.clone();
@ -658,7 +658,7 @@ async fn obtain_response(
client
.request(request)
.and_then(move |res| {
let send_end = precise_time_ms();
let send_end = CrossProcessInstant::now();
// TODO(#21271) response_start: immediately after receiving first byte of response
@ -671,9 +671,8 @@ async fn obtain_response(
headers,
Some(devtools_bytes.lock().unwrap().clone()),
pipeline_id,
SystemTime::now(),
connect_end - connect_start,
send_end - send_start,
(connect_end - connect_start).unsigned_abs(),
(send_end - send_start).unsigned_abs(),
is_xhr,
))
// TODO: ^This is not right, connect_start is taken before contructing the