Time distribution across script event categories.

This commit is contained in:
benshu 2015-08-23 21:02:40 +02:00
parent 71b277d567
commit 1e6a2f08fc
11 changed files with 184 additions and 50 deletions

View file

@ -17,9 +17,10 @@ use dom::bindings::structuredclone::StructuredCloneData;
use dom::bindings::utils::Reflectable;
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::messageevent::MessageEvent;
use dom::worker::{TrustedWorkerAddress, WorkerMessageHandler, WorkerEventHandler};
use dom::worker::{TrustedWorkerAddress, WorkerMessageHandler, SimpleWorkerErrorHandler};
use dom::workerglobalscope::WorkerGlobalScope;
use dom::workerglobalscope::{WorkerGlobalScopeTypeId, WorkerGlobalScopeInit};
use script_task::ScriptTaskEventCategory::WorkerEvent;
use script_task::{ScriptTask, ScriptChan, TimerSource, ScriptPort, StackRootTLS, CommonScriptMsg};
use devtools_traits::DevtoolScriptControlMsg;
@ -207,8 +208,8 @@ impl DedicatedWorkerGlobalScope {
let (url, source) = match load_whole_resource(&init.resource_task, worker_url) {
Err(_) => {
println!("error loading script {}", serialized_worker_url);
parent_sender.send(CommonScriptMsg::RunnableMsg(
box WorkerEventHandler::new(worker))).unwrap();
parent_sender.send(CommonScriptMsg::RunnableMsg(WorkerEvent,
box SimpleWorkerErrorHandler::new(worker))).unwrap();
return;
}
Ok((metadata, bytes)) => {
@ -308,7 +309,7 @@ impl DedicatedWorkerGlobalScope {
data.read(GlobalRef::Worker(scope), message.handle_mut());
MessageEvent::dispatch_jsval(target, GlobalRef::Worker(scope), message.handle());
},
WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg(runnable)) => {
WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable)) => {
runnable.handler()
},
WorkerScriptMsg::Common(CommonScriptMsg::RefcountCleanup(addr)) => {
@ -359,7 +360,7 @@ impl DedicatedWorkerGlobalScopeMethods for DedicatedWorkerGlobalScope {
fn PostMessage(&self, cx: *mut JSContext, message: HandleValue) -> ErrorResult {
let data = try!(StructuredCloneData::write(cx, message));
let worker = self.worker.borrow().as_ref().unwrap().clone();
self.parent_sender.send(CommonScriptMsg::RunnableMsg(
self.parent_sender.send(CommonScriptMsg::RunnableMsg(WorkerEvent,
box WorkerMessageHandler::new(worker, data))).unwrap();
Ok(())
}

View file

@ -22,6 +22,7 @@ use encoding::label::encoding_from_whatwg_label;
use encoding::types::{EncodingRef, DecoderTrap};
use hyper::mime::{Mime, Attr};
use rustc_serialize::base64::{Config, ToBase64, CharacterSet, Newline};
use script_task::ScriptTaskEventCategory::FileRead;
use script_task::{ScriptChan, Runnable, ScriptPort, CommonScriptMsg};
use std::cell::{Cell, RefCell};
use std::sync::mpsc;
@ -401,22 +402,22 @@ fn perform_annotated_read_operation(gen_id: GenerationId, data: ReadMetaData, bl
let chan = &script_chan;
// Step 4
let task = box FileReaderEvent::ProcessRead(filereader.clone(), gen_id);
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(FileRead, task)).unwrap();
let task = box FileReaderEvent::ProcessReadData(filereader.clone(),
gen_id, DOMString::new());
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(FileRead, task)).unwrap();
let bytes = match blob_contents.recv() {
Ok(bytes) => bytes,
Err(_) => {
let task = box FileReaderEvent::ProcessReadError(filereader,
gen_id, DOMErrorName::NotFoundError);
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(FileRead, task)).unwrap();
return;
}
};
let task = box FileReaderEvent::ProcessReadEOF(filereader, gen_id, data, bytes);
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(FileRead, task)).unwrap();
}

View file

@ -21,6 +21,7 @@ use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{document_from_node, Node, NodeTypeId, NodeDamage, window_from_node};
use dom::virtualmethods::VirtualMethods;
use script_task::ScriptTaskEventCategory::UpdateReplacedElement;
use script_task::{Runnable, ScriptChan, CommonScriptMsg};
use string_cache::Atom;
use util::str::DOMString;
@ -131,7 +132,7 @@ impl HTMLImageElement {
// Return the image via a message to the script task, which marks the element
// as dirty and triggers a reflow.
let image_response = message.to().unwrap();
script_chan.send(CommonScriptMsg::RunnableMsg(
script_chan.send(CommonScriptMsg::RunnableMsg(UpdateReplacedElement,
box ImageResponseHandlerRunnable::new(
trusted_node.clone(), image_response))).unwrap();
});

View file

@ -32,6 +32,7 @@ use dom::window::ScriptHelpers;
use js::jsapi::RootedValue;
use js::jsval::UndefinedValue;
use network_listener::{NetworkListener, PreInvoke};
use script_task::ScriptTaskEventCategory::ScriptEvent;
use script_task::{ScriptChan, Runnable, CommonScriptMsg};
use encoding::all::UTF_8;
@ -426,7 +427,7 @@ impl HTMLScriptElement {
element: handler,
is_error: false,
};
chan.send(CommonScriptMsg::RunnableMsg(dispatcher)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(ScriptEvent, dispatcher)).unwrap();
}
}
@ -439,7 +440,7 @@ impl HTMLScriptElement {
element: handler,
is_error: true,
};
chan.send(CommonScriptMsg::RunnableMsg(dispatcher)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(ScriptEvent, dispatcher)).unwrap();
}
pub fn dispatch_before_script_execute_event(&self) -> bool {

View file

@ -25,6 +25,7 @@ use dom::node::{ChildrenMutation, Node, NodeDamage};
use dom::node::{NodeTypeId, document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan;
use script_task::ScriptTaskEventCategory::InputEvent;
use script_task::{Runnable, CommonScriptMsg};
use textinput::{TextInput, Lines, KeyReaction};
@ -356,7 +357,7 @@ impl VirtualMethods for HTMLTextAreaElement {
let dispatcher = ChangeEventRunnable {
element: handler,
};
let _ = chan.send(CommonScriptMsg::RunnableMsg(box dispatcher));
let _ = chan.send(CommonScriptMsg::RunnableMsg(InputEvent, box dispatcher));
}
self.force_relayout();

View file

@ -22,6 +22,7 @@ use dom::closeevent::CloseEvent;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::messageevent::MessageEvent;
use script_task::ScriptTaskEventCategory::WebSocketEvent;
use script_task::{Runnable, CommonScriptMsg};
use net_traits::hosts::replace_hosts;
@ -182,7 +183,7 @@ impl WebSocket {
let task = box CloseTask {
addr: address,
};
sender.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
sender.send(CommonScriptMsg::RunnableMsg(WebSocketEvent, task)).unwrap();
return;
}
};
@ -192,7 +193,7 @@ impl WebSocket {
addr: address.clone(),
sender: ws_sender.clone(),
};
sender.send(CommonScriptMsg::RunnableMsg(open_task)).unwrap();
sender.send(CommonScriptMsg::RunnableMsg(WebSocketEvent, open_task)).unwrap();
for message in receiver.incoming_messages() {
let message = match message {
@ -208,7 +209,7 @@ impl WebSocket {
let task = box CloseTask {
addr: address,
};
sender.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
sender.send(CommonScriptMsg::RunnableMsg(WebSocketEvent, task)).unwrap();
break;
},
Err(_) => break,
@ -217,7 +218,7 @@ impl WebSocket {
address: address.clone(),
message: message,
};
sender.send(CommonScriptMsg::RunnableMsg(message_task)).unwrap();
sender.send(CommonScriptMsg::RunnableMsg(WebSocketEvent, message_task)).unwrap();
}
});

View file

@ -186,20 +186,20 @@ impl Runnable for WorkerMessageHandler {
}
}
pub struct WorkerEventHandler {
pub struct SimpleWorkerErrorHandler {
addr: TrustedWorkerAddress,
}
impl WorkerEventHandler {
pub fn new(addr: TrustedWorkerAddress) -> WorkerEventHandler {
WorkerEventHandler {
impl SimpleWorkerErrorHandler {
pub fn new(addr: TrustedWorkerAddress) -> SimpleWorkerErrorHandler {
SimpleWorkerErrorHandler {
addr: addr
}
}
}
impl Runnable for WorkerEventHandler {
fn handler(self: Box<WorkerEventHandler>) {
impl Runnable for SimpleWorkerErrorHandler {
fn handler(self: Box<SimpleWorkerErrorHandler>) {
let this = *self;
Worker::dispatch_simple_error(this.addr);
}

View file

@ -27,6 +27,7 @@ use dom::xmlhttprequesteventtarget::XMLHttpRequestEventTarget;
use dom::xmlhttprequesteventtarget::XMLHttpRequestEventTargetTypeId;
use dom::xmlhttprequestupload::XMLHttpRequestUpload;
use network_listener::{NetworkListener, PreInvoke};
use script_task::ScriptTaskEventCategory::XhrEvent;
use script_task::{ScriptChan, Runnable, ScriptPort, CommonScriptMsg};
use encoding::all::UTF_8;
@ -989,7 +990,7 @@ impl XMLHttpRequest {
sleep_ms(duration_ms);
match cancel_rx.try_recv() {
Err(TryRecvError::Empty) => {
timeout_target.send(CommonScriptMsg::RunnableMsg(box XHRTimeout {
timeout_target.send(CommonScriptMsg::RunnableMsg(XhrEvent, box XHRTimeout {
xhr: xhr,
gen_id: gen_id,
})).unwrap();

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use net_traits::{AsyncResponseListener, ResponseAction};
use script_task::ScriptTaskEventCategory::NetworkEvent;
use script_task::{ScriptChan, Runnable, CommonScriptMsg};
use std::sync::{Arc, Mutex};
@ -15,7 +16,7 @@ pub struct NetworkListener<T: AsyncResponseListener + PreInvoke + Send + 'static
impl<T: AsyncResponseListener + PreInvoke + Send + 'static> NetworkListener<T> {
pub fn notify(&self, action: ResponseAction) {
if let Err(err) = self.script_chan.send(CommonScriptMsg::RunnableMsg(box ListenerRunnable {
if let Err(err) = self.script_chan.send(CommonScriptMsg::RunnableMsg(NetworkEvent, box ListenerRunnable {
context: self.context.clone(),
action: action,
})) {

View file

@ -94,11 +94,12 @@ use js::jsval::UndefinedValue;
use js::rust::Runtime;
use url::{Url, UrlParser};
use core::ops::Deref;
use libc;
use std::any::Any;
use std::borrow::ToOwned;
use std::cell::{Cell, RefCell};
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::io::{stdout, Write};
use std::mem as std_mem;
use std::option::Option;
@ -178,6 +179,13 @@ pub trait MainThreadRunnable {
fn handler(self: Box<Self>, script_task: &ScriptTask);
}
enum MixedMessage {
FromConstellation(ConstellationControlMsg),
FromScript(MainThreadScriptMsg),
FromDevtools(DevtoolScriptControlMsg),
FromImageCache(ImageCacheResult),
}
/// Common messages used to control the event loops in both the script and the worker
pub enum CommonScriptMsg {
/// Requests that the script task measure its memory usage. The results are sent back via the
@ -190,7 +198,27 @@ pub enum CommonScriptMsg {
/// A DOM object's last pinned reference was removed (dispatched to all tasks).
RefcountCleanup(TrustedReference),
/// Generic message that encapsulates event handling.
RunnableMsg(Box<Runnable + Send>),
RunnableMsg(ScriptTaskEventCategory, Box<Runnable + Send>),
}
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, PartialEq)]
pub enum ScriptTaskEventCategory {
AttachLayout,
ConstellationMsg,
DevtoolsMsg,
DocumentEvent,
DomEvent,
FileRead,
ImageCacheMsg,
InputEvent,
NetworkEvent,
Resize,
ScriptEvent,
UpdateReplacedElement,
SetViewport,
WebSocketEvent,
WorkerEvent,
XhrEvent,
}
/// Messages used to control the script event loop
@ -390,6 +418,11 @@ pub struct ScriptTask {
/// List of pipelines that have been owned and closed by this script task.
closed_pipelines: RefCell<HashSet<PipelineId>>,
/// When profiling data should be written out to stdout.
perf_profiler_next_report: Cell<Option<u64>>,
/// How much time was spent on what since the last report.
perf_profiler_times: RefCell<HashMap<ScriptTaskEventCategory, u64>>,
}
/// In the event of task failure, all data on the stack runs its destructor. However, there
@ -635,6 +668,9 @@ impl ScriptTask {
js_runtime: Rc::new(runtime),
mouse_over_targets: DOMRefCell::new(vec!()),
closed_pipelines: RefCell::new(HashSet::new()),
perf_profiler_next_report: Cell::new(None),
perf_profiler_times: RefCell::new(HashMap::new()),
}
}
@ -713,13 +749,6 @@ impl ScriptTask {
self.handle_event(id, ResizeEvent(size));
}
enum MixedMessage {
FromConstellation(ConstellationControlMsg),
FromScript(MainThreadScriptMsg),
FromDevtools(DevtoolScriptControlMsg),
FromImageCache(ImageCacheResult),
}
// Store new resizes, and gather all other events.
let mut sequential = vec!();
@ -762,13 +791,19 @@ impl ScriptTask {
// child list yet, causing the find() to fail.
MixedMessage::FromConstellation(ConstellationControlMsg::AttachLayout(
new_layout_info)) => {
self.handle_new_layout(new_layout_info);
self.profile_event(ScriptTaskEventCategory::AttachLayout, || {
self.handle_new_layout(new_layout_info);
})
}
MixedMessage::FromConstellation(ConstellationControlMsg::Resize(id, size)) => {
self.handle_resize(id, size);
self.profile_event(ScriptTaskEventCategory::Resize, || {
self.handle_resize(id, size);
})
}
MixedMessage::FromConstellation(ConstellationControlMsg::Viewport(id, rect)) => {
self.handle_viewport(id, rect);
self.profile_event(ScriptTaskEventCategory::SetViewport, || {
self.handle_viewport(id, rect);
})
}
MixedMessage::FromConstellation(ConstellationControlMsg::TickAllAnimations(
pipeline_id)) => {
@ -815,16 +850,26 @@ impl ScriptTask {
// Process the gathered events.
for msg in sequential {
match msg {
MixedMessage::FromConstellation(ConstellationControlMsg::ExitPipeline(id, exit_type)) => {
if self.handle_exit_pipeline_msg(id, exit_type) {
return false
}
},
MixedMessage::FromConstellation(inner_msg) => self.handle_msg_from_constellation(inner_msg),
MixedMessage::FromScript(inner_msg) => self.handle_msg_from_script(inner_msg),
MixedMessage::FromDevtools(inner_msg) => self.handle_msg_from_devtools(inner_msg),
MixedMessage::FromImageCache(inner_msg) => self.handle_msg_from_image_cache(inner_msg),
let category = self.categorize_msg(&msg);
let result = self.profile_event(category, move || {
match msg {
MixedMessage::FromConstellation(ConstellationControlMsg::ExitPipeline(id, exit_type)) => {
if self.handle_exit_pipeline_msg(id, exit_type) {
return Some(false)
}
},
MixedMessage::FromConstellation(inner_msg) => self.handle_msg_from_constellation(inner_msg),
MixedMessage::FromScript(inner_msg) => self.handle_msg_from_script(inner_msg),
MixedMessage::FromDevtools(inner_msg) => self.handle_msg_from_devtools(inner_msg),
MixedMessage::FromImageCache(inner_msg) => self.handle_msg_from_image_cache(inner_msg),
}
None
});
if let Some(retval) = result {
return retval
}
}
@ -847,6 +892,76 @@ impl ScriptTask {
true
}
fn categorize_msg(&self, msg: &MixedMessage) -> ScriptTaskEventCategory {
match *msg {
MixedMessage::FromConstellation(ref inner_msg) => {
match *inner_msg {
ConstellationControlMsg::SendEvent(_, _) =>
ScriptTaskEventCategory::DomEvent,
_ => ScriptTaskEventCategory::ConstellationMsg
}
},
MixedMessage::FromDevtools(_) => ScriptTaskEventCategory::DevtoolsMsg,
MixedMessage::FromImageCache(_) => ScriptTaskEventCategory::ImageCacheMsg,
MixedMessage::FromScript(ref inner_msg) => {
match *inner_msg {
MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(ref category, _)) =>
*category,
_ => ScriptTaskEventCategory::ScriptEvent
}
}
}
}
fn profile_event<F, R>(&self, category: ScriptTaskEventCategory, f: F) -> R
where F: FnOnce() -> R {
if opts::get().profile_script_events {
let start = time::precise_time_ns();
let result = f();
let end = time::precise_time_ns();
let duration = end - start;
let aggregate = {
let zero = 0;
let perf_profiler_times = self.perf_profiler_times.borrow();
let so_far = perf_profiler_times.get(&category).unwrap_or(&zero);
so_far + duration
};
self.perf_profiler_times.borrow_mut().insert(category, aggregate);
const NANO: u64 = 1000 * 1000 * 1000;
const REPORT_INTERVAL: u64 = 10 * NANO;
match self.perf_profiler_next_report.get() {
None => self.perf_profiler_next_report.set(Some(start + REPORT_INTERVAL)),
Some(time) if time <= end => {
self.perf_profiler_next_report.set(Some(end + REPORT_INTERVAL));
let stdout = stdout();
let mut stdout = stdout.lock();
writeln!(&mut stdout, "Script task time distribution:").unwrap();
for (c, t) in self.perf_profiler_times.borrow().deref() {
let secs = t / NANO;
let nanos = t % NANO;
writeln!(&mut stdout, " {:?}: {}.{}s", c, secs, nanos).unwrap();
}
stdout.flush().unwrap();
self.perf_profiler_times.borrow_mut().clear();
},
Some(_) => {}
}
result
} else {
f()
}
}
fn handle_msg_from_constellation(&self, msg: ConstellationControlMsg) {
match msg {
ConstellationControlMsg::AttachLayout(_) =>
@ -914,7 +1029,9 @@ impl ScriptTask {
MainThreadScriptMsg::Common(
CommonScriptMsg::FireTimer(TimerSource::FromWorker, _)) =>
panic!("Worker timeouts must not be sent to script task"),
MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(runnable)) =>
MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable)) =>
// The category of the runnable is ignored by the pattern, however
// it is still respected by profiling (see categorize_msg).
runnable.handler(),
MainThreadScriptMsg::Common(CommonScriptMsg::RefcountCleanup(addr)) =>
LiveDOMReferences::cleanup(addr),
@ -1128,7 +1245,7 @@ impl ScriptTask {
// https://html.spec.whatwg.org/multipage/#the-end step 7
let addr: Trusted<Document> = Trusted::new(self.get_cx(), doc, self.chan.clone());
let handler = box DocumentProgressHandler::new(addr.clone(), DocumentProgressTask::Load);
self.chan.send(CommonScriptMsg::RunnableMsg(handler)).unwrap();
self.chan.send(CommonScriptMsg::RunnableMsg(ScriptTaskEventCategory::DocumentEvent, handler)).unwrap();
let ConstellationChan(ref chan) = self.constellation_chan;
chan.send(ConstellationMsg::LoadComplete(pipeline)).unwrap();
@ -1881,7 +1998,7 @@ impl ScriptTask {
// https://html.spec.whatwg.org/multipage/#the-end step 4
let addr: Trusted<Document> = Trusted::new(self.get_cx(), document.r(), self.chan.clone());
let handler = box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
self.chan.send(CommonScriptMsg::RunnableMsg(handler)).unwrap();
self.chan.send(CommonScriptMsg::RunnableMsg(ScriptTaskEventCategory::DocumentEvent, handler)).unwrap();
window.r().set_fragment_name(final_url.fragment.clone());