mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +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,16 +2,20 @@
|
|||
* 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 net_traits::{ResourceAttribute, ResourceFetchTiming, ResourceTimeValue, ResourceTimingType};
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time_to_fetch_start_if_nonzero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.fetch_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
resource_timing.fetch_start = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.fetch_start > 0,
|
||||
resource_timing.start_time.is_none(),
|
||||
"`start_time` should be zero"
|
||||
);
|
||||
assert!(
|
||||
resource_timing.fetch_start.is_some(),
|
||||
"`fetch_start` should have a positive value"
|
||||
);
|
||||
|
||||
|
@ -27,13 +31,13 @@ fn test_set_start_time_to_fetch_start_if_nonzero_tao() {
|
|||
fn test_set_start_time_to_fetch_start_if_zero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.start_time = 1;
|
||||
resource_timing.start_time = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
resource_timing.start_time.is_some(),
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.fetch_start, 0,
|
||||
assert!(
|
||||
resource_timing.fetch_start.is_none(),
|
||||
"`fetch_start` should be zero"
|
||||
);
|
||||
|
||||
|
@ -50,10 +54,13 @@ fn test_set_start_time_to_fetch_start_if_nonzero_no_tao() {
|
|||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
resource_timing.fetch_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
resource_timing.fetch_start = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.fetch_start > 0,
|
||||
resource_timing.start_time.is_none(),
|
||||
"`start_time` should be zero"
|
||||
);
|
||||
assert!(
|
||||
!resource_timing.fetch_start.is_none(),
|
||||
"`fetch_start` should have a positive value"
|
||||
);
|
||||
|
||||
|
@ -70,13 +77,13 @@ fn test_set_start_time_to_fetch_start_if_zero_no_tao() {
|
|||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
resource_timing.start_time = 1;
|
||||
resource_timing.start_time = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
resource_timing.start_time.is_some(),
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.fetch_start, 0,
|
||||
assert!(
|
||||
resource_timing.fetch_start.is_none(),
|
||||
"`fetch_start` should be zero"
|
||||
);
|
||||
|
||||
|
@ -92,10 +99,13 @@ fn test_set_start_time_to_fetch_start_if_zero_no_tao() {
|
|||
fn test_set_start_time_to_redirect_start_if_nonzero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.redirect_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
resource_timing.redirect_start = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.redirect_start > 0,
|
||||
resource_timing.start_time.is_none(),
|
||||
"`start_time` should be zero"
|
||||
);
|
||||
assert!(
|
||||
resource_timing.redirect_start.is_some(),
|
||||
"`redirect_start` should have a positive value"
|
||||
);
|
||||
|
||||
|
@ -113,13 +123,13 @@ fn test_set_start_time_to_redirect_start_if_nonzero_tao() {
|
|||
fn test_not_set_start_time_to_redirect_start_if_zero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.start_time = 1;
|
||||
resource_timing.start_time = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
resource_timing.start_time.is_some(),
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.redirect_start, 0,
|
||||
assert!(
|
||||
resource_timing.redirect_start.is_none(),
|
||||
"`redirect_start` should be zero"
|
||||
);
|
||||
|
||||
|
@ -139,10 +149,13 @@ fn test_not_set_start_time_to_redirect_start_if_nonzero_no_tao() {
|
|||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
// Note: properly-behaved redirect_start should never be nonzero once TAO check has failed
|
||||
resource_timing.redirect_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
resource_timing.redirect_start = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.redirect_start > 0,
|
||||
resource_timing.start_time.is_none(),
|
||||
"`start_time` should be zero"
|
||||
);
|
||||
assert!(
|
||||
resource_timing.redirect_start.is_some(),
|
||||
"`redirect_start` should have a positive value"
|
||||
);
|
||||
|
||||
|
@ -161,13 +174,13 @@ fn test_not_set_start_time_to_redirect_start_if_zero_no_tao() {
|
|||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
resource_timing.start_time = 1;
|
||||
resource_timing.start_time = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
resource_timing.start_time.is_some(),
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.redirect_start, 0,
|
||||
assert!(
|
||||
resource_timing.redirect_start.is_none(),
|
||||
"`redirect_start` should be zero"
|
||||
);
|
||||
|
||||
|
@ -185,28 +198,37 @@ fn test_not_set_start_time_to_redirect_start_if_zero_no_tao() {
|
|||
fn test_set_start_time() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
assert!(
|
||||
resource_timing.start_time.is_none(),
|
||||
"`start_time` should be zero"
|
||||
);
|
||||
|
||||
// verify setting `start_time` to current time succeeds
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::Now));
|
||||
assert!(resource_timing.start_time > 0, "failed to set `start_time`");
|
||||
assert!(
|
||||
resource_timing.start_time.is_some(),
|
||||
"failed to set `start_time`"
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_reset_start_time() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
|
||||
resource_timing.start_time = 1;
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
resource_timing.start_time.is_none(),
|
||||
"`start_time` should be zero"
|
||||
);
|
||||
|
||||
resource_timing.start_time = Some(CrossProcessInstant::now());
|
||||
assert!(
|
||||
resource_timing.start_time.is_some(),
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
|
||||
// verify resetting `start_time` (to zero) succeeds
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::Zero));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, 0,
|
||||
assert!(
|
||||
resource_timing.start_time.is_none(),
|
||||
"failed to reset `start_time`"
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue