From 9654363c187ee549b82bca8c4e3098e4c20c7287 Mon Sep 17 00:00:00 2001 From: Auguste Baum <52001167+augustebaum@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:23:18 +0100 Subject: script: Start replacing `time` with `std::time` and `chrono` (#30639) * Replace `time` with `chrono` in `script/animation_timeline` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/script_thread.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` and `chrono` in `script/script_thread.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/script_runtime.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/script_runtime.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/dom/workerglobalscope.rs` Signed-off-by: Auguste Baum * Replace `time` with `chrono` in `script/dom/workerglobalscope.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/dom/htmlmedialelement.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/dom/htmlmedialelement.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/dom/globalscope.rs` Signed-off-by: Auguste Baum * Replace `time` with `chrono` in `script/dom/globalscope.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/dom/htmlformelement.rs` Signed-off-by: Auguste Baum * Replace `time` with `std::time` in `script/dom/htmlformelement.rs` Signed-off-by: Auguste Baum * Increase precision of animation timeline * Some fixes Use Instant a bit more and stop using chrono. Do not transition `navigation_start_precise` to Instant yet as we need to coordinate this across all crates. --------- Signed-off-by: Auguste Baum Co-authored-by: Martin Robinson --- components/script/script_thread.rs | 46 ++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'components/script/script_thread.rs') diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index 25f47f3a440..5f87aaac47b 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -27,11 +27,12 @@ use std::rc::Rc; use std::result::Result; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use std::time::{Duration, SystemTime}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use std::{ptr, thread}; use bluetooth_traits::BluetoothRequest; use canvas_traits::webgl::WebGLPipeline; +use chrono::{DateTime, Local}; use crossbeam_channel::{select, unbounded, Receiver, Sender}; use devtools_traits::{ CSSError, DevtoolScriptControlMsg, DevtoolsPageInfo, NavigationState, @@ -91,7 +92,7 @@ use servo_config::opts; use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl}; use style::dom::OpaqueNode; use style::thread_state::{self, ThreadState}; -use time::{at_utc, get_time, precise_time_ns, Timespec}; +use time::precise_time_ns; use url::Position; use webgpu::identity::WebGPUMsg; use webrender_api::units::LayoutPixel; @@ -214,9 +215,9 @@ struct InProgressLoad { /// The origin for the document #[no_trace] origin: MutableOrigin, - /// Timestamp reporting the time when the browser started this load. + /// Timestamp reporting the time in milliseconds when the browser started this load. navigation_start: u64, - /// High res timestamp reporting the time when the browser started this load. + /// High res timestamp reporting the time in nanoseconds when the browser started this load. navigation_start_precise: u64, /// For cancelling the fetch canceller: FetchCanceller, @@ -241,10 +242,15 @@ impl InProgressLoad { layout_is_busy: Arc, inherited_secure_context: Option, ) -> InProgressLoad { - let current_time = get_time(); + let duration = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default(); + let navigation_start = duration.as_millis(); let navigation_start_precise = precise_time_ns(); layout_chan - .send(message::Msg::SetNavigationStart(navigation_start_precise)) + .send(message::Msg::SetNavigationStart( + navigation_start_precise as u64, + )) .unwrap(); InProgressLoad { pipeline_id: id, @@ -258,7 +264,7 @@ impl InProgressLoad { is_visible: true, url: url, origin: origin, - navigation_start: (current_time.sec * 1000 + current_time.nsec as i64 / 1000000) as u64, + navigation_start: navigation_start as u64, navigation_start_precise: navigation_start_precise, canceller: Default::default(), layout_is_busy: layout_is_busy, @@ -1864,7 +1870,7 @@ impl ScriptThread { F: FnOnce() -> R, { self.notify_activity_to_hang_monitor(&category); - let start = precise_time_ns(); + let start = Instant::now(); let value = if self.profile_script_events { let profiler_cat = match category { ScriptThreadEventCategory::AttachLayout => ProfilerCategory::ScriptAttachLayout, @@ -1911,15 +1917,15 @@ impl ScriptThread { } else { f() }; - let end = precise_time_ns(); + let task_duration = start.elapsed(); for (doc_id, doc) in self.documents.borrow().iter() { if let Some(pipeline_id) = pipeline_id { - if pipeline_id == doc_id && end - start > MAX_TASK_NS { + if pipeline_id == doc_id && task_duration.as_nanos() > MAX_TASK_NS.into() { if self.print_pwm { println!( "Task took longer than max allowed ({:?}) {:?}", category, - end - start + task_duration.as_nanos() ); } doc.start_tti(); @@ -3351,9 +3357,11 @@ impl ScriptThread { window.init_window_proxy(&window_proxy); let last_modified = metadata.headers.as_ref().and_then(|headers| { - headers - .typed_get::() - .map(|tm| dom_last_modified(&tm.into())) + headers.typed_get::().map(|tm| { + let tm: SystemTime = tm.into(); + let local_time: DateTime = tm.into(); + local_time.format("%m/%d/%Y %H:%M:%S").to_string() + }) }); let loader = DocumentLoader::new_with_threads( @@ -4057,13 +4065,3 @@ impl Drop for ScriptThread { }); } } - -fn dom_last_modified(tm: &SystemTime) -> String { - let tm = tm.duration_since(SystemTime::UNIX_EPOCH).unwrap(); - let tm = Timespec::new(tm.as_secs() as i64, 0); - let tm = at_utc(tm); - tm.to_local() - .strftime("%m/%d/%Y %H:%M:%S") - .unwrap() - .to_string() -} -- cgit v1.2.3