1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/* 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/. */
extern crate gfx;
extern crate gfx_traits;
extern crate ipc_channel;
#[macro_use]
extern crate log;
extern crate msg;
extern crate profile_traits;
extern crate script_traits;
extern crate servo_config;
use gfx::display_list::{DisplayItem, DisplayList};
use gfx_traits::Epoch;
use ipc_channel::ipc::IpcSender;
use msg::constellation_msg::PipelineId;
use profile_traits::time::{ProfilerChan, ProfilerCategory, send_profile_data};
use profile_traits::time::TimerMetadata;
use script_traits::{ConstellationControlMsg, LayoutMsg, PaintMetricType};
use servo_config::opts;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
pub trait ProfilerMetadataFactory {
fn new_metadata(&self) -> Option<TimerMetadata>;
}
macro_rules! make_time_setter(
( $attr:ident, $func:ident, $category:ident, $label:expr, $metric_type:path ) => (
fn $func(&self,
profiler_metadata: Option<TimerMetadata>,
paint_time: f64) {
if self.$attr.get().is_some() {
return;
}
let navigation_start = match self.navigation_start {
Some(time) => time,
None => {
warn!("Trying to set metric before navigation start");
return;
}
};
let time = paint_time - navigation_start;
self.$attr.set(Some(time));
// Queue performance observer notification.
let msg = ConstellationControlMsg::PaintMetric(self.pipeline_id,
$metric_type,
time);
if let Err(e) = self.script_chan.send(msg) {
warn!("Sending paint metric to script thread failed ({}).", e);
}
// Send the metric to the time profiler.
send_profile_data(ProfilerCategory::$category,
profiler_metadata,
&self.time_profiler_chan,
time as u64, time as u64, 0, 0);
// Print the metric to console if the print-pwm option was given.
if opts::get().print_pwm {
println!("{:?} {:?}", $label, time);
}
}
);
);
pub struct PaintTimeMetrics {
pending_metrics: RefCell<HashMap<Epoch, (Option<TimerMetadata>, bool)>>,
navigation_start: Option<f64>,
first_paint: Cell<Option<f64>>,
first_contentful_paint: Cell<Option<f64>>,
pipeline_id: PipelineId,
time_profiler_chan: ProfilerChan,
constellation_chan: IpcSender<LayoutMsg>,
script_chan: IpcSender<ConstellationControlMsg>,
}
impl PaintTimeMetrics {
pub fn new(pipeline_id: PipelineId,
time_profiler_chan: ProfilerChan,
constellation_chan: IpcSender<LayoutMsg>,
script_chan: IpcSender<ConstellationControlMsg>)
-> PaintTimeMetrics {
PaintTimeMetrics {
pending_metrics: RefCell::new(HashMap::new()),
navigation_start: None,
first_paint: Cell::new(None),
first_contentful_paint: Cell::new(None),
pipeline_id,
time_profiler_chan,
constellation_chan,
script_chan,
}
}
pub fn set_navigation_start(&mut self, time: f64) {
self.navigation_start = Some(time);
}
make_time_setter!(first_paint, set_first_paint,
TimeToFirstPaint,
"first-paint",
PaintMetricType::FirstPaint);
make_time_setter!(first_contentful_paint, set_first_contentful_paint,
TimeToFirstContentfulPaint,
"first-contentful-paint",
PaintMetricType::FirstContentfulPaint);
pub fn maybe_observe_paint_time<T>(&self,
profiler_metadata_factory: &T,
epoch: Epoch,
display_list: &DisplayList)
where T: ProfilerMetadataFactory {
if self.first_paint.get().is_some() && self.first_contentful_paint.get().is_some() {
// If we already set all paint metrics, we just bail out.
return;
}
let mut is_contentful = false;
// Analyze the display list to figure out if this may be the first
// contentful paint (i.e. the display list contains items of type text,
// image, non-white canvas or SVG).
for item in &display_list.list {
match item {
&DisplayItem::Text(_) |
&DisplayItem::Image(_) => {
is_contentful = true;
break;
},
_ => (),
}
}
self.pending_metrics.borrow_mut().insert(
epoch,
(profiler_metadata_factory.new_metadata(), is_contentful)
);
// Send the pending metric information to the compositor thread.
// The compositor will record the current time after painting the
// frame with the given ID and will send the metric back to us.
let msg = LayoutMsg::PendingPaintMetric(self.pipeline_id, epoch);
if let Err(e) = self.constellation_chan.send(msg) {
warn!("Failed to send PendingPaintMetric {:?}", e);
}
}
pub fn maybe_set_metric(&mut self, epoch: Epoch, paint_time: f64) {
if (self.first_paint.get().is_some() && self.first_contentful_paint.get().is_some()) ||
self.navigation_start.is_none() {
// If we already set all paint metrics or we have not set navigation start yet,
// we just bail out.
return;
}
if let Some(pending_metric) = self.pending_metrics.borrow_mut().remove(&epoch) {
let profiler_metadata = pending_metric.0;
self.set_first_paint(profiler_metadata.clone(), paint_time);
if pending_metric.1 {
self.set_first_contentful_paint(profiler_metadata, paint_time);
}
}
}
pub fn get_navigation_start(&self) -> Option<f64> {
self.navigation_start
}
pub fn get_first_paint(&self) -> Option<f64> {
self.first_paint.get()
}
pub fn get_first_contentful_paint(&self) -> Option<f64> {
self.first_contentful_paint.get()
}
}
|