2013-04-06 03:31:01 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-10-29 00:31:44 +04:00
|
|
|
//! Timing functions.
|
2013-09-21 02:39:46 +04:00
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
use collections::BTreeMap;
|
2015-01-21 13:48:49 +03:00
|
|
|
use std::borrow::ToOwned;
|
2015-01-22 18:06:50 +03:00
|
|
|
use std::cmp::Ordering;
|
2014-04-05 02:52:50 +04:00
|
|
|
use std::f64;
|
2014-04-05 14:46:27 +04:00
|
|
|
use std::io::timer::sleep;
|
2014-11-05 04:12:32 +03:00
|
|
|
use std::iter::AdditiveIterator;
|
2015-01-28 04:15:50 +03:00
|
|
|
use std::num::Float;
|
|
|
|
use std::sync::mpsc::{Sender, channel, Receiver};
|
2014-09-21 02:35:08 +04:00
|
|
|
use std::time::duration::Duration;
|
2014-11-05 04:12:32 +03:00
|
|
|
use std_time::precise_time_ns;
|
2014-01-14 00:11:35 +04:00
|
|
|
use task::{spawn_named};
|
2014-09-18 20:12:34 +04:00
|
|
|
use url::Url;
|
2014-01-13 07:16:21 +04:00
|
|
|
|
2013-06-18 00:21:34 +04:00
|
|
|
// front-end representation of the profiler used to communicate with the profiler
|
2015-01-28 04:15:50 +03:00
|
|
|
#[derive(Clone)]
|
2014-06-27 04:17:59 +04:00
|
|
|
pub struct TimeProfilerChan(pub Sender<TimeProfilerMsg>);
|
2013-10-29 00:31:44 +04:00
|
|
|
|
2014-06-27 04:17:59 +04:00
|
|
|
impl TimeProfilerChan {
|
|
|
|
pub fn send(&self, msg: TimeProfilerMsg) {
|
|
|
|
let TimeProfilerChan(ref c) = *self;
|
2014-03-19 20:35:17 +04:00
|
|
|
c.send(msg);
|
2013-10-29 00:31:44 +04:00
|
|
|
}
|
2013-06-18 00:21:34 +04:00
|
|
|
}
|
2013-06-14 04:45:25 +04:00
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
#[derive(PartialEq, Clone, PartialOrd, Eq, Ord)]
|
2014-09-18 20:12:34 +04:00
|
|
|
pub struct TimerMetadata {
|
2014-10-23 00:45:35 +04:00
|
|
|
url: String,
|
|
|
|
iframe: bool,
|
|
|
|
incremental: bool,
|
2014-09-18 20:12:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Formatable {
|
|
|
|
fn format(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Formatable for Option<TimerMetadata> {
|
|
|
|
fn format(&self) -> String {
|
|
|
|
match self {
|
|
|
|
// TODO(cgaebel): Center-align in the format strings as soon as rustc supports it.
|
|
|
|
&Some(ref meta) => {
|
|
|
|
let url = meta.url.as_slice();
|
2014-10-23 00:45:35 +04:00
|
|
|
let url = if url.len() > 30 {
|
|
|
|
url.slice_to(30)
|
|
|
|
} else {
|
|
|
|
url
|
|
|
|
};
|
|
|
|
let incremental = if meta.incremental { " yes" } else { " no " };
|
2014-09-18 20:12:34 +04:00
|
|
|
let iframe = if meta.iframe { " yes" } else { " no " };
|
2014-10-23 00:45:35 +04:00
|
|
|
format!(" {:14} {:9} {:30}", incremental, iframe, url)
|
2014-09-18 20:12:34 +04:00
|
|
|
},
|
|
|
|
&None =>
|
|
|
|
format!(" {:14} {:9} {:30}", " N/A", " N/A", " N/A")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
#[derive(Clone)]
|
2014-06-27 04:17:59 +04:00
|
|
|
pub enum TimeProfilerMsg {
|
2014-01-25 08:51:59 +04:00
|
|
|
/// Normal message used for reporting time
|
2014-12-18 04:45:49 +03:00
|
|
|
Time((TimeProfilerCategory, Option<TimerMetadata>), f64),
|
2014-01-25 08:51:59 +04:00
|
|
|
/// Message used to force print the profiling metrics
|
2014-12-18 04:45:49 +03:00
|
|
|
Print,
|
2014-01-25 08:51:59 +04:00
|
|
|
/// Tells the profiler to shut down.
|
2014-12-18 04:45:49 +03:00
|
|
|
Exit,
|
2013-08-29 09:06:27 +04:00
|
|
|
}
|
|
|
|
|
2014-04-05 02:52:50 +04:00
|
|
|
#[repr(u32)]
|
2015-01-28 04:15:50 +03:00
|
|
|
#[derive(PartialEq, Clone, PartialOrd, Eq, Ord)]
|
2014-06-27 04:17:59 +04:00
|
|
|
pub enum TimeProfilerCategory {
|
2015-01-04 22:39:47 +03:00
|
|
|
Compositing,
|
|
|
|
LayoutPerform,
|
|
|
|
LayoutStyleRecalc,
|
2014-10-15 02:51:30 +04:00
|
|
|
LayoutRestyleDamagePropagation,
|
|
|
|
LayoutNonIncrementalReset,
|
2015-01-04 22:39:47 +03:00
|
|
|
LayoutSelectorMatch,
|
|
|
|
LayoutTreeBuilder,
|
|
|
|
LayoutDamagePropagate,
|
|
|
|
LayoutMain,
|
|
|
|
LayoutParallelWarmup,
|
|
|
|
LayoutShaping,
|
|
|
|
LayoutDispListBuild,
|
|
|
|
PaintingPerTile,
|
|
|
|
PaintingPrepBuff,
|
|
|
|
Painting,
|
2013-06-14 04:45:25 +04:00
|
|
|
}
|
|
|
|
|
2014-09-18 20:12:34 +04:00
|
|
|
impl Formatable for TimeProfilerCategory {
|
2013-06-14 04:45:25 +04:00
|
|
|
// some categories are subcategories of LayoutPerformCategory
|
|
|
|
// and should be printed to indicate this
|
2014-09-18 20:12:34 +04:00
|
|
|
fn format(&self) -> String {
|
|
|
|
let padding = match *self {
|
2015-01-04 22:39:47 +03:00
|
|
|
TimeProfilerCategory::LayoutStyleRecalc |
|
2014-12-18 04:45:49 +03:00
|
|
|
TimeProfilerCategory::LayoutRestyleDamagePropagation |
|
|
|
|
TimeProfilerCategory::LayoutNonIncrementalReset |
|
2015-01-04 22:39:47 +03:00
|
|
|
TimeProfilerCategory::LayoutMain |
|
|
|
|
TimeProfilerCategory::LayoutDispListBuild |
|
|
|
|
TimeProfilerCategory::LayoutShaping |
|
|
|
|
TimeProfilerCategory::LayoutDamagePropagate |
|
|
|
|
TimeProfilerCategory::PaintingPerTile |
|
|
|
|
TimeProfilerCategory::PaintingPrepBuff => "+ ",
|
|
|
|
TimeProfilerCategory::LayoutParallelWarmup |
|
|
|
|
TimeProfilerCategory::LayoutSelectorMatch |
|
|
|
|
TimeProfilerCategory::LayoutTreeBuilder => "| + ",
|
2013-06-14 04:45:25 +04:00
|
|
|
_ => ""
|
|
|
|
};
|
2014-09-18 20:12:34 +04:00
|
|
|
let name = match *self {
|
2015-01-04 22:39:47 +03:00
|
|
|
TimeProfilerCategory::Compositing => "Compositing",
|
|
|
|
TimeProfilerCategory::LayoutPerform => "Layout",
|
|
|
|
TimeProfilerCategory::LayoutStyleRecalc => "Style Recalc",
|
2014-12-18 04:45:49 +03:00
|
|
|
TimeProfilerCategory::LayoutRestyleDamagePropagation => "Restyle Damage Propagation",
|
|
|
|
TimeProfilerCategory::LayoutNonIncrementalReset => "Non-incremental reset (temporary)",
|
2015-01-04 22:39:47 +03:00
|
|
|
TimeProfilerCategory::LayoutSelectorMatch => "Selector Matching",
|
|
|
|
TimeProfilerCategory::LayoutTreeBuilder => "Tree Building",
|
|
|
|
TimeProfilerCategory::LayoutDamagePropagate => "Damage Propagation",
|
|
|
|
TimeProfilerCategory::LayoutMain => "Primary Layout Pass",
|
|
|
|
TimeProfilerCategory::LayoutParallelWarmup => "Parallel Warmup",
|
|
|
|
TimeProfilerCategory::LayoutShaping => "Shaping",
|
|
|
|
TimeProfilerCategory::LayoutDispListBuild => "Display List Construction",
|
|
|
|
TimeProfilerCategory::PaintingPerTile => "Painting Per Tile",
|
|
|
|
TimeProfilerCategory::PaintingPrepBuff => "Buffer Prep",
|
|
|
|
TimeProfilerCategory::Painting => "Painting",
|
2014-09-18 20:12:34 +04:00
|
|
|
};
|
2014-12-18 04:45:49 +03:00
|
|
|
format!("{}{}", padding, name)
|
2013-06-14 04:45:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
type TimeProfilerBuckets = BTreeMap<(TimeProfilerCategory, Option<TimerMetadata>), Vec<f64>>;
|
2013-08-29 09:06:27 +04:00
|
|
|
|
|
|
|
// back end of the profiler that handles data aggregation and performance metrics
|
2014-06-27 04:17:59 +04:00
|
|
|
pub struct TimeProfiler {
|
|
|
|
pub port: Receiver<TimeProfilerMsg>,
|
|
|
|
buckets: TimeProfilerBuckets,
|
|
|
|
pub last_msg: Option<TimeProfilerMsg>,
|
2013-08-29 09:06:27 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 04:17:59 +04:00
|
|
|
impl TimeProfiler {
|
|
|
|
pub fn create(period: Option<f64>) -> TimeProfilerChan {
|
2014-04-05 02:52:50 +04:00
|
|
|
let (chan, port) = channel();
|
2013-09-21 02:39:46 +04:00
|
|
|
match period {
|
|
|
|
Some(period) => {
|
2014-09-21 02:35:08 +04:00
|
|
|
let period = Duration::milliseconds((period * 1000f64) as i64);
|
2014-01-13 07:16:21 +04:00
|
|
|
let chan = chan.clone();
|
2015-01-28 04:15:50 +03:00
|
|
|
spawn_named("Time profiler timer".to_owned(), move || {
|
2013-09-21 02:39:46 +04:00
|
|
|
loop {
|
2014-04-05 14:46:27 +04:00
|
|
|
sleep(period);
|
2015-01-28 04:15:50 +03:00
|
|
|
if chan.send(TimeProfilerMsg::Print).is_err() {
|
2013-09-21 02:39:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-13 07:16:21 +04:00
|
|
|
});
|
2014-06-27 04:17:59 +04:00
|
|
|
// Spawn the time profiler.
|
2015-01-28 04:15:50 +03:00
|
|
|
spawn_named("Time profiler".to_owned(), move || {
|
2014-06-27 04:17:59 +04:00
|
|
|
let mut profiler = TimeProfiler::new(port);
|
2013-09-21 02:39:46 +04:00
|
|
|
profiler.start();
|
2014-01-13 07:16:21 +04:00
|
|
|
});
|
2013-09-21 02:39:46 +04:00
|
|
|
}
|
|
|
|
None => {
|
2014-06-27 04:17:59 +04:00
|
|
|
// No-op to handle messages when the time profiler is inactive.
|
2015-01-28 04:15:50 +03:00
|
|
|
spawn_named("Time profiler".to_owned(), move || {
|
2014-01-25 08:51:59 +04:00
|
|
|
loop {
|
2015-01-28 04:15:50 +03:00
|
|
|
match port.recv() {
|
2014-12-18 04:45:49 +03:00
|
|
|
Err(_) | Ok(TimeProfilerMsg::Exit) => break,
|
2014-01-25 08:51:59 +04:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2014-01-13 07:16:21 +04:00
|
|
|
});
|
2013-09-21 02:39:46 +04:00
|
|
|
}
|
2013-05-24 06:03:29 +04:00
|
|
|
}
|
2014-01-13 07:16:21 +04:00
|
|
|
|
2014-06-27 04:17:59 +04:00
|
|
|
TimeProfilerChan(chan)
|
2013-05-24 06:03:29 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 04:17:59 +04:00
|
|
|
pub fn new(port: Receiver<TimeProfilerMsg>) -> TimeProfiler {
|
|
|
|
TimeProfiler {
|
2013-05-24 06:03:29 +04:00
|
|
|
port: port,
|
2015-01-28 04:15:50 +03:00
|
|
|
buckets: BTreeMap::new(),
|
2013-06-18 00:21:34 +04:00
|
|
|
last_msg: None,
|
2013-05-24 06:03:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start(&mut self) {
|
|
|
|
loop {
|
2015-01-28 04:15:50 +03:00
|
|
|
let msg = self.port.recv();
|
2013-08-23 20:30:32 +04:00
|
|
|
match msg {
|
2014-05-23 04:14:24 +04:00
|
|
|
Ok(msg) => {
|
2014-01-25 08:51:59 +04:00
|
|
|
if !self.handle_msg(msg) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-05-23 04:14:24 +04:00
|
|
|
_ => break
|
2013-08-23 20:30:32 +04:00
|
|
|
}
|
2013-05-24 06:03:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 20:12:34 +04:00
|
|
|
fn find_or_insert(&mut self, k: (TimeProfilerCategory, Option<TimerMetadata>), t: f64) {
|
2014-11-13 06:48:31 +03:00
|
|
|
match self.buckets.get_mut(&k) {
|
2014-09-18 20:12:34 +04:00
|
|
|
None => {},
|
|
|
|
Some(v) => { v.push(t); return; },
|
|
|
|
}
|
|
|
|
|
|
|
|
self.buckets.insert(k, vec!(t));
|
|
|
|
}
|
|
|
|
|
2014-06-27 04:17:59 +04:00
|
|
|
fn handle_msg(&mut self, msg: TimeProfilerMsg) -> bool {
|
2014-09-18 20:12:34 +04:00
|
|
|
match msg.clone() {
|
2014-12-18 04:45:49 +03:00
|
|
|
TimeProfilerMsg::Time(k, t) => self.find_or_insert(k, t),
|
|
|
|
TimeProfilerMsg::Print => match self.last_msg {
|
2013-07-11 03:15:51 +04:00
|
|
|
// only print if more data has arrived since the last printout
|
2014-12-18 04:45:49 +03:00
|
|
|
Some(TimeProfilerMsg::Time(..)) => self.print_buckets(),
|
2013-08-29 09:06:27 +04:00
|
|
|
_ => ()
|
2013-06-18 00:21:34 +04:00
|
|
|
},
|
2014-12-18 04:45:49 +03:00
|
|
|
TimeProfilerMsg::Exit => return false,
|
2013-06-14 04:45:25 +04:00
|
|
|
};
|
2013-06-18 00:21:34 +04:00
|
|
|
self.last_msg = Some(msg);
|
2014-01-25 08:51:59 +04:00
|
|
|
true
|
2013-06-14 04:45:25 +04:00
|
|
|
}
|
2013-05-24 06:03:29 +04:00
|
|
|
|
2013-08-17 02:46:03 +04:00
|
|
|
fn print_buckets(&mut self) {
|
2014-12-18 04:45:49 +03:00
|
|
|
println!("{:35} {:14} {:9} {:30} {:15} {:15} {:-15} {:-15} {:-15}",
|
2014-09-18 20:12:34 +04:00
|
|
|
"_category_", "_incremental?_", "_iframe?_",
|
|
|
|
" _url_", " _mean (ms)_", " _median (ms)_",
|
|
|
|
" _min (ms)_", " _max (ms)_", " _events_");
|
2014-09-21 02:35:08 +04:00
|
|
|
for (&(ref category, ref meta), ref mut data) in self.buckets.iter_mut() {
|
2014-01-13 07:16:21 +04:00
|
|
|
data.sort_by(|a, b| {
|
|
|
|
if a < b {
|
2015-01-22 18:06:50 +03:00
|
|
|
Ordering::Less
|
2014-01-13 07:16:21 +04:00
|
|
|
} else {
|
2015-01-22 18:06:50 +03:00
|
|
|
Ordering::Greater
|
2014-01-13 07:16:21 +04:00
|
|
|
}
|
|
|
|
});
|
2013-07-11 03:15:51 +04:00
|
|
|
let data_len = data.len();
|
|
|
|
if data_len > 0 {
|
2014-04-05 02:52:50 +04:00
|
|
|
let (mean, median, min, max) =
|
2013-10-22 20:16:17 +04:00
|
|
|
(data.iter().map(|&x|x).sum() / (data_len as f64),
|
2014-09-18 20:12:34 +04:00
|
|
|
data.as_slice()[data_len / 2],
|
2014-04-05 02:52:50 +04:00
|
|
|
data.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
|
|
|
|
data.iter().fold(-f64::INFINITY, |a, &b| a.max(b)));
|
2014-12-18 04:45:49 +03:00
|
|
|
println!("{:-35}{} {:15.4} {:15.4} {:15.4} {:15.4} {:15}",
|
2014-09-18 20:12:34 +04:00
|
|
|
category.format(), meta.format(), mean, median, min, max, data_len);
|
2013-06-14 04:45:25 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-19 20:35:17 +04:00
|
|
|
println!("");
|
2013-05-24 06:03:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
#[derive(Eq, PartialEq)]
|
2014-12-06 03:34:03 +03:00
|
|
|
pub enum TimerMetadataFrameType {
|
2014-12-18 04:45:49 +03:00
|
|
|
RootWindow,
|
|
|
|
IFrame,
|
2014-12-06 03:34:03 +03:00
|
|
|
}
|
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
#[derive(Eq, PartialEq)]
|
2014-12-06 03:34:03 +03:00
|
|
|
pub enum TimerMetadataReflowType {
|
2014-12-18 04:45:49 +03:00
|
|
|
Incremental,
|
|
|
|
FirstReflow,
|
2014-12-06 03:34:03 +03:00
|
|
|
}
|
2013-06-14 04:45:25 +04:00
|
|
|
|
2014-12-18 10:24:49 +03:00
|
|
|
pub type ProfilerMetadata<'a> = Option<(&'a Url, TimerMetadataFrameType, TimerMetadataReflowType)>;
|
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
pub fn profile<T, F>(category: TimeProfilerCategory,
|
|
|
|
meta: ProfilerMetadata,
|
|
|
|
time_profiler_chan: TimeProfilerChan,
|
|
|
|
callback: F)
|
|
|
|
-> T
|
|
|
|
where F: FnOnce() -> T
|
|
|
|
{
|
2013-05-24 06:03:29 +04:00
|
|
|
let start_time = precise_time_ns();
|
|
|
|
let val = callback();
|
|
|
|
let end_time = precise_time_ns();
|
2014-03-19 20:35:17 +04:00
|
|
|
let ms = (end_time - start_time) as f64 / 1000000f64;
|
2014-12-06 03:34:03 +03:00
|
|
|
let meta = meta.map(|(url, iframe, reflow_type)|
|
2014-09-18 20:12:34 +04:00
|
|
|
TimerMetadata {
|
|
|
|
url: url.serialize(),
|
2014-12-18 04:45:49 +03:00
|
|
|
iframe: iframe == TimerMetadataFrameType::IFrame,
|
|
|
|
incremental: reflow_type == TimerMetadataReflowType::Incremental,
|
2014-09-18 20:12:34 +04:00
|
|
|
});
|
2014-12-18 04:45:49 +03:00
|
|
|
time_profiler_chan.send(TimeProfilerMsg::Time((category, meta), ms));
|
2013-05-24 06:03:29 +04:00
|
|
|
return val;
|
|
|
|
}
|
2012-11-13 00:08:38 +04:00
|
|
|
|
2015-01-28 04:15:50 +03:00
|
|
|
pub fn time<T, F>(msg: &str, callback: F) -> T
|
|
|
|
where F: Fn() -> T
|
|
|
|
{
|
2012-11-13 00:08:38 +04:00
|
|
|
let start_time = precise_time_ns();
|
|
|
|
let val = callback();
|
|
|
|
let end_time = precise_time_ns();
|
2014-03-19 20:35:17 +04:00
|
|
|
let ms = (end_time - start_time) as f64 / 1000000f64;
|
2013-10-22 20:16:17 +04:00
|
|
|
if ms >= 5f64 {
|
2014-12-18 04:45:49 +03:00
|
|
|
debug!("{} took {} ms", msg, ms);
|
2012-11-13 00:08:38 +04:00
|
|
|
}
|
2013-02-20 05:49:31 +04:00
|
|
|
return val;
|
2012-11-13 00:08:38 +04:00
|
|
|
}
|