mirror of
https://github.com/servo/servo.git
synced 2025-08-08 23: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
|
@ -20,4 +20,14 @@ malloc_size_of_derive = { workspace = true }
|
|||
parking_lot = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
size_of_test = { workspace = true }
|
||||
time_03 = { workspace = true }
|
||||
webrender_api = { workspace = true }
|
||||
|
||||
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
|
||||
mach2 = { workspace = true }
|
||||
|
||||
[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))'.dependencies]
|
||||
libc = { workspace = true }
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
windows-sys = { workspace = true, features = ["Win32_System_Performance"] }
|
||||
|
|
167
components/shared/base/cross_process_instant.rs
Normal file
167
components/shared/base/cross_process_instant.rs
Normal file
|
@ -0,0 +1,167 @@
|
|||
// Copyright 2024 The Servo Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! An implementation of a monotonic, nanosecond precision timer, like [`std::time::Instant`] that
|
||||
//! can be serialized and compared across processes.
|
||||
|
||||
use std::ops::{Add, Sub};
|
||||
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time_03::Duration;
|
||||
|
||||
/// A monotonic, nanosecond precision timer that can be used cross-process. The value
|
||||
/// stored internally is purposefully opaque as the origin is platform-specific. They can
|
||||
/// be compared and [`time_03::Duration`] can be found by subtracting one from another.
|
||||
/// The `time` crate is used in this case instead of `std::time` so that durations can
|
||||
/// be negative.
|
||||
#[derive(
|
||||
Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize,
|
||||
)]
|
||||
pub struct CrossProcessInstant {
|
||||
value: u64,
|
||||
}
|
||||
|
||||
impl CrossProcessInstant {
|
||||
pub fn now() -> Self {
|
||||
Self {
|
||||
value: platform::now(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Some unspecified time epoch. This is mainly useful for converting DOM's `timeOrigin` into a
|
||||
/// `DOMHighResolutionTimestamp`. See <https://w3c.github.io/hr-time/#sec-time-origin>.
|
||||
pub fn epoch() -> Self {
|
||||
Self { value: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for CrossProcessInstant {
|
||||
type Output = Duration;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Duration::nanoseconds(self.value as i64 - rhs.value as i64)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Duration> for CrossProcessInstant {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Duration) -> Self::Output {
|
||||
Self {
|
||||
value: self.value + rhs.whole_nanoseconds() as u64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
|
||||
mod platform {
|
||||
use libc::timespec;
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub(super) fn now() -> u64 {
|
||||
// SAFETY: libc::timespec is zero initializable.
|
||||
let time = unsafe {
|
||||
let mut time: timespec = std::mem::zeroed();
|
||||
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut time);
|
||||
time
|
||||
};
|
||||
(time.tv_sec as u64) * 1000000000 + (time.tv_nsec as u64)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
||||
mod platform {
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use mach2::mach_time::{mach_absolute_time, mach_timebase_info};
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn timebase_info() -> &'static mach_timebase_info {
|
||||
static TIMEBASE_INFO: LazyLock<mach_timebase_info> = LazyLock::new(|| {
|
||||
let mut timebase_info = mach_timebase_info { numer: 0, denom: 0 };
|
||||
unsafe { mach_timebase_info(&mut timebase_info) };
|
||||
timebase_info
|
||||
});
|
||||
&*TIMEBASE_INFO
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub(super) fn now() -> u64 {
|
||||
let timebase_info = timebase_info();
|
||||
let absolute_time = unsafe { mach_absolute_time() };
|
||||
absolute_time * timebase_info.numer as u64 / timebase_info.denom as u64
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod platform {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use windows_sys::Win32::System::Performance::{
|
||||
QueryPerformanceCounter, QueryPerformanceFrequency,
|
||||
};
|
||||
|
||||
/// The frequency of the value returned by `QueryPerformanceCounter` in counts per
|
||||
/// second. This is taken from the Rust source code at:
|
||||
/// <https://github.com/rust-lang/rust/blob/1a1cc050d8efc906ede39f444936ade1fdc9c6cb/library/std/src/sys/pal/windows/time.rs#L197>
|
||||
#[allow(unsafe_code)]
|
||||
fn frequency() -> i64 {
|
||||
// Either the cached result of `QueryPerformanceFrequency` or `0` for
|
||||
// uninitialized. Storing this as a single `AtomicU64` allows us to use
|
||||
// `Relaxed` operations, as we are only interested in the effects on a
|
||||
// single memory location.
|
||||
static FREQUENCY: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
let cached = FREQUENCY.load(Ordering::Relaxed);
|
||||
// If a previous thread has filled in this global state, use that.
|
||||
if cached != 0 {
|
||||
return cached as i64;
|
||||
}
|
||||
// ... otherwise learn for ourselves ...
|
||||
let mut frequency = 0;
|
||||
let result = unsafe { QueryPerformanceFrequency(&mut frequency) };
|
||||
|
||||
if result == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
FREQUENCY.store(frequency as u64, Ordering::Relaxed);
|
||||
frequency
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
/// Get the current instant value in nanoseconds.
|
||||
/// Originally from: <https://github.com/rust-lang/rust/blob/1a1cc050d8efc906ede39f444936ade1fdc9c6cb/library/std/src/sys/pal/windows/time.rs#L175>
|
||||
pub(super) fn now() -> u64 {
|
||||
let mut counter_value = 0;
|
||||
unsafe { QueryPerformanceCounter(&mut counter_value) };
|
||||
|
||||
/// Computes (value*numer)/denom without overflow, as long as both
|
||||
/// (numer*denom) and the overall result fit into i64 (which is the case
|
||||
/// for our time conversions).
|
||||
/// Originally from: <https://github.com/rust-lang/rust/blob/1a1cc050d8efc906ede39f444936ade1fdc9c6cb/library/std/src/sys_common/mod.rs#L75>
|
||||
fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
|
||||
let q = value / denom;
|
||||
let r = value % denom;
|
||||
// Decompose value as (value/denom*denom + value%denom),
|
||||
// substitute into (value*numer)/denom and simplify.
|
||||
// r < denom, so (denom*numer) is the upper bound of (r*numer)
|
||||
q * numer + r * numer / denom
|
||||
}
|
||||
|
||||
static NANOSECONDS_PER_SECOND: u64 = 1_000_000_000;
|
||||
mul_div_u64(
|
||||
counter_value as u64,
|
||||
NANOSECONDS_PER_SECOND,
|
||||
frequency() as u64,
|
||||
)
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
//! You should almost never need to add a data type to this crate. Instead look for
|
||||
//! a more shared crate that has fewer dependents.
|
||||
|
||||
pub mod cross_process_instant;
|
||||
pub mod generic_channel;
|
||||
pub mod id;
|
||||
pub mod print_tree;
|
||||
|
|
|
@ -341,8 +341,8 @@ pub struct HttpRequest {
|
|||
pub pipeline_id: PipelineId,
|
||||
pub started_date_time: SystemTime,
|
||||
pub time_stamp: i64,
|
||||
pub connect_time: u64,
|
||||
pub send_time: u64,
|
||||
pub connect_time: Duration,
|
||||
pub send_time: Duration,
|
||||
pub is_xhr: bool,
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
|
||||
use std::fmt::Display;
|
||||
use std::sync::LazyLock;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use base::cross_process_instant::CrossProcessInstant;
|
||||
use base::id::HistoryStateId;
|
||||
use cookie::Cookie;
|
||||
use headers::{ContentType, HeaderMapExt, ReferrerPolicy as ReferrerPolicyHeader};
|
||||
|
@ -20,7 +20,6 @@ use ipc_channel::Error as IpcError;
|
|||
use malloc_size_of::malloc_size_of_is_0;
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use mime::Mime;
|
||||
use num_traits::Zero;
|
||||
use rustls::Certificate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use servo_rand::RngCore;
|
||||
|
@ -494,21 +493,21 @@ pub struct ResourceCorsData {
|
|||
|
||||
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub struct ResourceFetchTiming {
|
||||
pub domain_lookup_start: u64,
|
||||
pub domain_lookup_start: Option<CrossProcessInstant>,
|
||||
pub timing_check_passed: bool,
|
||||
pub timing_type: ResourceTimingType,
|
||||
/// Number of redirects until final resource (currently limited to 20)
|
||||
pub redirect_count: u16,
|
||||
pub request_start: u64,
|
||||
pub secure_connection_start: u64,
|
||||
pub response_start: u64,
|
||||
pub fetch_start: u64,
|
||||
pub response_end: u64,
|
||||
pub redirect_start: u64,
|
||||
pub redirect_end: u64,
|
||||
pub connect_start: u64,
|
||||
pub connect_end: u64,
|
||||
pub start_time: u64,
|
||||
pub request_start: Option<CrossProcessInstant>,
|
||||
pub secure_connection_start: Option<CrossProcessInstant>,
|
||||
pub response_start: Option<CrossProcessInstant>,
|
||||
pub fetch_start: Option<CrossProcessInstant>,
|
||||
pub response_end: Option<CrossProcessInstant>,
|
||||
pub redirect_start: Option<CrossProcessInstant>,
|
||||
pub redirect_end: Option<CrossProcessInstant>,
|
||||
pub connect_start: Option<CrossProcessInstant>,
|
||||
pub connect_end: Option<CrossProcessInstant>,
|
||||
pub start_time: Option<CrossProcessInstant>,
|
||||
}
|
||||
|
||||
pub enum RedirectStartValue {
|
||||
|
@ -539,8 +538,8 @@ pub enum ResourceAttribute {
|
|||
RedirectStart(RedirectStartValue),
|
||||
RedirectEnd(RedirectEndValue),
|
||||
FetchStart,
|
||||
ConnectStart(u64),
|
||||
ConnectEnd(u64),
|
||||
ConnectStart(CrossProcessInstant),
|
||||
ConnectEnd(CrossProcessInstant),
|
||||
SecureConnectionStart,
|
||||
ResponseEnd,
|
||||
StartTime(ResourceTimeValue),
|
||||
|
@ -559,18 +558,18 @@ impl ResourceFetchTiming {
|
|||
ResourceFetchTiming {
|
||||
timing_type,
|
||||
timing_check_passed: true,
|
||||
domain_lookup_start: 0,
|
||||
domain_lookup_start: None,
|
||||
redirect_count: 0,
|
||||
secure_connection_start: 0,
|
||||
request_start: 0,
|
||||
response_start: 0,
|
||||
fetch_start: 0,
|
||||
redirect_start: 0,
|
||||
redirect_end: 0,
|
||||
connect_start: 0,
|
||||
connect_end: 0,
|
||||
response_end: 0,
|
||||
start_time: 0,
|
||||
secure_connection_start: None,
|
||||
request_start: None,
|
||||
response_start: None,
|
||||
fetch_start: None,
|
||||
redirect_start: None,
|
||||
redirect_end: None,
|
||||
connect_start: None,
|
||||
connect_end: None,
|
||||
response_end: None,
|
||||
start_time: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -586,47 +585,41 @@ impl ResourceFetchTiming {
|
|||
if !self.timing_check_passed && !should_attribute_always_be_updated {
|
||||
return;
|
||||
}
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos() as u64;
|
||||
let now = Some(CrossProcessInstant::now());
|
||||
match attribute {
|
||||
ResourceAttribute::DomainLookupStart => self.domain_lookup_start = now,
|
||||
ResourceAttribute::RedirectCount(count) => self.redirect_count = count,
|
||||
ResourceAttribute::RequestStart => self.request_start = now,
|
||||
ResourceAttribute::ResponseStart => self.response_start = now,
|
||||
ResourceAttribute::RedirectStart(val) => match val {
|
||||
RedirectStartValue::Zero => self.redirect_start = 0,
|
||||
RedirectStartValue::Zero => self.redirect_start = None,
|
||||
RedirectStartValue::FetchStart => {
|
||||
if self.redirect_start.is_zero() {
|
||||
if self.redirect_start.is_none() {
|
||||
self.redirect_start = self.fetch_start
|
||||
}
|
||||
},
|
||||
},
|
||||
ResourceAttribute::RedirectEnd(val) => match val {
|
||||
RedirectEndValue::Zero => self.redirect_end = 0,
|
||||
RedirectEndValue::Zero => self.redirect_end = None,
|
||||
RedirectEndValue::ResponseEnd => self.redirect_end = self.response_end,
|
||||
},
|
||||
ResourceAttribute::FetchStart => self.fetch_start = now,
|
||||
ResourceAttribute::ConnectStart(val) => self.connect_start = val,
|
||||
ResourceAttribute::ConnectEnd(val) => self.connect_end = val,
|
||||
ResourceAttribute::ConnectStart(instant) => self.connect_start = Some(instant),
|
||||
ResourceAttribute::ConnectEnd(instant) => self.connect_end = Some(instant),
|
||||
ResourceAttribute::SecureConnectionStart => self.secure_connection_start = now,
|
||||
ResourceAttribute::ResponseEnd => self.response_end = now,
|
||||
ResourceAttribute::StartTime(val) => match val {
|
||||
ResourceTimeValue::RedirectStart
|
||||
if self.redirect_start.is_zero() || !self.timing_check_passed => {},
|
||||
if self.redirect_start.is_none() || !self.timing_check_passed => {},
|
||||
_ => self.start_time = self.get_time_value(val),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_time_value(&self, time: ResourceTimeValue) -> u64 {
|
||||
fn get_time_value(&self, time: ResourceTimeValue) -> Option<CrossProcessInstant> {
|
||||
match time {
|
||||
ResourceTimeValue::Zero => 0,
|
||||
ResourceTimeValue::Now => SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos() as u64,
|
||||
ResourceTimeValue::Zero => None,
|
||||
ResourceTimeValue::Now => Some(CrossProcessInstant::now()),
|
||||
ResourceTimeValue::FetchStart => self.fetch_start,
|
||||
ResourceTimeValue::RedirectStart => self.redirect_start,
|
||||
}
|
||||
|
@ -634,13 +627,13 @@ impl ResourceFetchTiming {
|
|||
|
||||
pub fn mark_timing_check_failed(&mut self) {
|
||||
self.timing_check_passed = false;
|
||||
self.domain_lookup_start = 0;
|
||||
self.domain_lookup_start = None;
|
||||
self.redirect_count = 0;
|
||||
self.request_start = 0;
|
||||
self.response_start = 0;
|
||||
self.redirect_start = 0;
|
||||
self.connect_start = 0;
|
||||
self.connect_end = 0;
|
||||
self.request_start = None;
|
||||
self.response_start = None;
|
||||
self.redirect_start = None;
|
||||
self.connect_start = None;
|
||||
self.connect_end = None;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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`"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -11,10 +11,11 @@ name = "profile_traits"
|
|||
path = "lib.rs"
|
||||
|
||||
[dependencies]
|
||||
base = { workspace = true }
|
||||
crossbeam-channel = { workspace = true }
|
||||
ipc-channel = { workspace = true }
|
||||
log = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
servo_config = { path = "../../config" }
|
||||
signpost = { git = "https://github.com/pcwalton/signpost.git" }
|
||||
time = { workspace = true }
|
||||
time_03 = { workspace = true }
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
* 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 std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use base::cross_process_instant::CrossProcessInstant;
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use servo_config::opts;
|
||||
use time_03::Duration;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub struct TimerMetadata {
|
||||
|
@ -30,13 +30,16 @@ impl ProfilerChan {
|
|||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub enum ProfilerData {
|
||||
NoRecords,
|
||||
Record(Vec<f64>),
|
||||
Record(Vec<Duration>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub enum ProfilerMsg {
|
||||
/// Normal message used for reporting time
|
||||
Time((ProfilerCategory, Option<TimerMetadata>), (u64, u64)),
|
||||
Time(
|
||||
(ProfilerCategory, Option<TimerMetadata>),
|
||||
(CrossProcessInstant, CrossProcessInstant),
|
||||
),
|
||||
/// Message used to get time spend entries for a particular ProfilerBuckets (in nanoseconds)
|
||||
Get(
|
||||
(ProfilerCategory, Option<TimerMetadata>),
|
||||
|
@ -139,28 +142,15 @@ where
|
|||
if opts::get().debug.signpost {
|
||||
signpost::start(category as u32, &[0, 0, 0, (category as usize) >> 4]);
|
||||
}
|
||||
let start_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos();
|
||||
|
||||
let start_time = CrossProcessInstant::now();
|
||||
let val = callback();
|
||||
let end_time = CrossProcessInstant::now();
|
||||
|
||||
let end_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos();
|
||||
if opts::get().debug.signpost {
|
||||
signpost::end(category as u32, &[0, 0, 0, (category as usize) >> 4]);
|
||||
}
|
||||
|
||||
send_profile_data(
|
||||
category,
|
||||
meta,
|
||||
&profiler_chan,
|
||||
start_time as u64,
|
||||
end_time as u64,
|
||||
);
|
||||
send_profile_data(category, meta, &profiler_chan, start_time, end_time);
|
||||
val
|
||||
}
|
||||
|
||||
|
@ -168,8 +158,8 @@ pub fn send_profile_data(
|
|||
category: ProfilerCategory,
|
||||
meta: Option<TimerMetadata>,
|
||||
profiler_chan: &ProfilerChan,
|
||||
start_time: u64,
|
||||
end_time: u64,
|
||||
start_time: CrossProcessInstant,
|
||||
end_time: CrossProcessInstant,
|
||||
) {
|
||||
profiler_chan.send(ProfilerMsg::Time((category, meta), (start_time, end_time)));
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ use std::sync::Arc;
|
|||
use std::time::Duration;
|
||||
|
||||
use background_hang_monitor_api::BackgroundHangMonitorRegister;
|
||||
use base::cross_process_instant::CrossProcessInstant;
|
||||
use base::id::{
|
||||
BlobId, BrowsingContextId, HistoryStateId, MessagePortId, PipelineId, PipelineNamespaceId,
|
||||
TopLevelBrowsingContextId,
|
||||
|
@ -373,7 +374,7 @@ pub enum ConstellationControlMsg {
|
|||
/// Reload the given page.
|
||||
Reload(PipelineId),
|
||||
/// Notifies the script thread about a new recorded paint metric.
|
||||
PaintMetric(PipelineId, ProgressiveWebMetricType, u64),
|
||||
PaintMetric(PipelineId, ProgressiveWebMetricType, CrossProcessInstant),
|
||||
/// Notifies the media session about a user requested media session action.
|
||||
MediaSessionAction(PipelineId, MediaSessionActionType),
|
||||
/// Notifies script thread that WebGPU server has started
|
||||
|
@ -382,7 +383,7 @@ pub enum ConstellationControlMsg {
|
|||
/// pipeline via the Constellation.
|
||||
SetScrollStates(PipelineId, Vec<ScrollState>),
|
||||
/// Send the paint time for a specific epoch.
|
||||
SetEpochPaintTime(PipelineId, Epoch, u64),
|
||||
SetEpochPaintTime(PipelineId, Epoch, CrossProcessInstant),
|
||||
}
|
||||
|
||||
impl fmt::Debug for ConstellationControlMsg {
|
||||
|
|
|
@ -17,6 +17,7 @@ use std::sync::Arc;
|
|||
|
||||
use app_units::Au;
|
||||
use atomic_refcell::AtomicRefCell;
|
||||
use base::cross_process_instant::CrossProcessInstant;
|
||||
use base::id::{BrowsingContextId, PipelineId};
|
||||
use base::Epoch;
|
||||
use canvas_traits::canvas::{CanvasId, CanvasMsg};
|
||||
|
@ -229,7 +230,7 @@ pub trait Layout {
|
|||
fn set_scroll_states(&mut self, scroll_states: &[ScrollState]);
|
||||
|
||||
/// Set the paint time for a specific epoch.
|
||||
fn set_epoch_paint_time(&mut self, epoch: Epoch, paint_time: u64);
|
||||
fn set_epoch_paint_time(&mut self, epoch: Epoch, paint_time: CrossProcessInstant);
|
||||
|
||||
fn query_content_box(&self, node: OpaqueNode) -> Option<Rect<Au>>;
|
||||
fn query_content_boxes(&self, node: OpaqueNode) -> Vec<Rect<Au>>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue