mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
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:
parent
35baf056f6
commit
312cf0df08
51 changed files with 854 additions and 665 deletions
|
@ -2,17 +2,18 @@
|
|||
* 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/. */
|
||||
|
||||
use base::cross_process_instant::CrossProcessInstant;
|
||||
use dom_struct::dom_struct;
|
||||
use net_traits::ResourceFetchTiming;
|
||||
use servo_url::ServoUrl;
|
||||
use time_03::Duration;
|
||||
|
||||
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
|
||||
use crate::dom::bindings::codegen::Bindings::PerformanceResourceTimingBinding::PerformanceResourceTimingMethods;
|
||||
use crate::dom::bindings::reflector::reflect_dom_object;
|
||||
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::dom::performance::reduce_timing_resolution;
|
||||
use crate::dom::performanceentry::PerformanceEntry;
|
||||
|
||||
// TODO UA may choose to limit how many resources are included as PerformanceResourceTiming objects
|
||||
|
@ -37,18 +38,30 @@ pub struct PerformanceResourceTiming {
|
|||
entry: PerformanceEntry,
|
||||
initiator_type: InitiatorType,
|
||||
next_hop: Option<DOMString>,
|
||||
worker_start: f64,
|
||||
redirect_start: f64,
|
||||
redirect_end: f64,
|
||||
fetch_start: f64,
|
||||
domain_lookup_start: f64,
|
||||
domain_lookup_end: f64,
|
||||
connect_start: f64,
|
||||
connect_end: f64,
|
||||
secure_connection_start: f64,
|
||||
request_start: f64,
|
||||
response_start: f64,
|
||||
response_end: f64,
|
||||
#[no_trace]
|
||||
worker_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
redirect_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
redirect_end: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
fetch_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
domain_lookup_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
domain_lookup_end: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
connect_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
connect_end: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
secure_connection_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
request_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
response_start: Option<CrossProcessInstant>,
|
||||
#[no_trace]
|
||||
response_end: Option<CrossProcessInstant>,
|
||||
transfer_size: u64, //size in octets
|
||||
encoded_body_size: u64, //size in octets
|
||||
decoded_body_size: u64, //size in octets
|
||||
|
@ -66,7 +79,7 @@ impl PerformanceResourceTiming {
|
|||
url: ServoUrl,
|
||||
initiator_type: InitiatorType,
|
||||
next_hop: Option<DOMString>,
|
||||
fetch_start: f64,
|
||||
fetch_start: Option<CrossProcessInstant>,
|
||||
) -> PerformanceResourceTiming {
|
||||
let entry_type = if initiator_type == InitiatorType::Navigation {
|
||||
DOMString::from("navigation")
|
||||
|
@ -77,23 +90,23 @@ impl PerformanceResourceTiming {
|
|||
entry: PerformanceEntry::new_inherited(
|
||||
DOMString::from(url.into_string()),
|
||||
entry_type,
|
||||
0.,
|
||||
0.,
|
||||
None,
|
||||
Duration::ZERO,
|
||||
),
|
||||
initiator_type,
|
||||
next_hop,
|
||||
worker_start: 0.,
|
||||
redirect_start: 0.,
|
||||
redirect_end: 0.,
|
||||
worker_start: None,
|
||||
redirect_start: None,
|
||||
redirect_end: None,
|
||||
fetch_start,
|
||||
domain_lookup_end: 0.,
|
||||
domain_lookup_start: 0.,
|
||||
connect_start: 0.,
|
||||
connect_end: 0.,
|
||||
secure_connection_start: 0.,
|
||||
request_start: 0.,
|
||||
response_start: 0.,
|
||||
response_end: 0.,
|
||||
domain_lookup_end: None,
|
||||
domain_lookup_start: None,
|
||||
connect_start: None,
|
||||
connect_end: None,
|
||||
secure_connection_start: None,
|
||||
request_start: None,
|
||||
response_start: None,
|
||||
response_end: None,
|
||||
transfer_size: 0,
|
||||
encoded_body_size: 0,
|
||||
decoded_body_size: 0,
|
||||
|
@ -108,28 +121,32 @@ impl PerformanceResourceTiming {
|
|||
next_hop: Option<DOMString>,
|
||||
resource_timing: &ResourceFetchTiming,
|
||||
) -> PerformanceResourceTiming {
|
||||
let duration = match (resource_timing.start_time, resource_timing.response_end) {
|
||||
(Some(start_time), Some(end_time)) => end_time - start_time,
|
||||
_ => Duration::ZERO,
|
||||
};
|
||||
PerformanceResourceTiming {
|
||||
entry: PerformanceEntry::new_inherited(
|
||||
DOMString::from(url.into_string()),
|
||||
DOMString::from("resource"),
|
||||
resource_timing.start_time as f64,
|
||||
resource_timing.response_end as f64 - resource_timing.start_time as f64,
|
||||
resource_timing.start_time,
|
||||
duration,
|
||||
),
|
||||
initiator_type,
|
||||
next_hop,
|
||||
worker_start: 0.,
|
||||
redirect_start: resource_timing.redirect_start as f64,
|
||||
redirect_end: resource_timing.redirect_end as f64,
|
||||
fetch_start: resource_timing.fetch_start as f64,
|
||||
domain_lookup_start: resource_timing.domain_lookup_start as f64,
|
||||
worker_start: None,
|
||||
redirect_start: resource_timing.redirect_start,
|
||||
redirect_end: resource_timing.redirect_end,
|
||||
fetch_start: resource_timing.fetch_start,
|
||||
domain_lookup_start: resource_timing.domain_lookup_start,
|
||||
//TODO (#21260)
|
||||
domain_lookup_end: 0.,
|
||||
connect_start: resource_timing.connect_start as f64,
|
||||
connect_end: resource_timing.connect_end as f64,
|
||||
secure_connection_start: resource_timing.secure_connection_start as f64,
|
||||
request_start: resource_timing.request_start as f64,
|
||||
response_start: resource_timing.response_start as f64,
|
||||
response_end: resource_timing.response_end as f64,
|
||||
domain_lookup_end: None,
|
||||
connect_start: resource_timing.connect_start,
|
||||
connect_end: resource_timing.connect_end,
|
||||
secure_connection_start: resource_timing.secure_connection_start,
|
||||
request_start: resource_timing.request_start,
|
||||
response_start: resource_timing.response_start,
|
||||
response_end: resource_timing.response_end,
|
||||
transfer_size: 0,
|
||||
encoded_body_size: 0,
|
||||
decoded_body_size: 0,
|
||||
|
@ -153,6 +170,18 @@ impl PerformanceResourceTiming {
|
|||
global,
|
||||
)
|
||||
}
|
||||
|
||||
/// Convert an optional [`CrossProcessInstant`] to a [`DOMHighResTimeStamp`]. If none
|
||||
/// return a timestamp for [`Self::fetch_start`] instead, so that timestamps are
|
||||
/// always after that time.
|
||||
pub(crate) fn to_dom_high_res_time_stamp(
|
||||
&self,
|
||||
instant: Option<CrossProcessInstant>,
|
||||
) -> DOMHighResTimeStamp {
|
||||
self.global()
|
||||
.performance()
|
||||
.maybe_to_dom_high_res_time_stamp(instant)
|
||||
}
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/
|
||||
|
@ -180,17 +209,17 @@ impl PerformanceResourceTimingMethods for PerformanceResourceTiming {
|
|||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-domainlookupstart
|
||||
fn DomainLookupStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.domain_lookup_start)
|
||||
self.to_dom_high_res_time_stamp(self.domain_lookup_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-domainlookupend
|
||||
fn DomainLookupEnd(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.domain_lookup_end)
|
||||
self.to_dom_high_res_time_stamp(self.domain_lookup_end)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-secureconnectionstart
|
||||
fn SecureConnectionStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.secure_connection_start)
|
||||
self.to_dom_high_res_time_stamp(self.secure_connection_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-transfersize
|
||||
|
@ -210,41 +239,41 @@ impl PerformanceResourceTimingMethods for PerformanceResourceTiming {
|
|||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-requeststart
|
||||
fn RequestStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.request_start)
|
||||
self.to_dom_high_res_time_stamp(self.request_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-redirectstart
|
||||
fn RedirectStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.redirect_start)
|
||||
self.to_dom_high_res_time_stamp(self.redirect_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-redirectend
|
||||
fn RedirectEnd(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.redirect_end)
|
||||
self.to_dom_high_res_time_stamp(self.redirect_end)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-responsestart
|
||||
fn ResponseStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.response_start)
|
||||
self.to_dom_high_res_time_stamp(self.response_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-fetchstart
|
||||
fn FetchStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.fetch_start)
|
||||
self.to_dom_high_res_time_stamp(self.fetch_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-connectstart
|
||||
fn ConnectStart(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.connect_start)
|
||||
self.to_dom_high_res_time_stamp(self.connect_start)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-connectend
|
||||
fn ConnectEnd(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.connect_end)
|
||||
self.to_dom_high_res_time_stamp(self.connect_end)
|
||||
}
|
||||
|
||||
// https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-responseend
|
||||
fn ResponseEnd(&self) -> DOMHighResTimeStamp {
|
||||
reduce_timing_resolution(self.response_end)
|
||||
self.to_dom_high_res_time_stamp(self.response_end)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue