Skip to content

Commit

Permalink
bound sampler buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Mar 31, 2019
1 parent fcd6beb commit 0780298
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
21 changes: 15 additions & 6 deletions components/background_hang_monitor/background_hang_monitor.rs
Expand Up @@ -12,7 +12,7 @@ use msg::constellation_msg::{
};
use msg::constellation_msg::{HangAlert, HangAnnotation, HangMonitorAlert, SamplerControlMsg};
use std::cell::Cell;
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::thread;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -153,10 +153,11 @@ pub struct BackgroundHangMonitorWorker {
port: Receiver<(MonitoredComponentId, MonitoredComponentMsg)>,
control_port: Receiver<SamplerControlMsg>,
sampling_duration: Option<Duration>,
sampling_max_duration: Option<Duration>,
last_sample: Instant,
creation: Instant,
sampling_baseline: Instant,
samples: Vec<Sample>,
samples: VecDeque<Sample>,
}

impl BackgroundHangMonitorWorker {
Expand All @@ -173,10 +174,11 @@ impl BackgroundHangMonitorWorker {
port,
control_port,
sampling_duration: None,
sampling_max_duration: None,
last_sample: Instant::now(),
sampling_baseline: Instant::now(),
creation: Instant::now(),
samples: vec![],
samples: Default::default(),
}
}

Expand Down Expand Up @@ -239,9 +241,10 @@ impl BackgroundHangMonitorWorker {
},
recv(self.control_port) -> event => {
match event {
Ok(SamplerControlMsg::Enable(rate)) => {
Ok(SamplerControlMsg::Enable(rate, max_duration)) => {
println!("Enabling profiler.");
self.sampling_duration = Some(rate);
self.sampling_max_duration = Some(max_duration);
self.sampling_baseline = Instant::now();
None
}
Expand Down Expand Up @@ -381,9 +384,15 @@ impl BackgroundHangMonitorWorker {
for (component_id, monitored) in self.monitored_components.iter_mut() {
let instant = Instant::now();
if let Ok(stack) = monitored.sampler.suspend_and_sample_thread() {
// TODO: support a bounded buffer that discards older samples.
if self.sampling_baseline.elapsed() >
self.sampling_max_duration
.expect("Max duration has been set")
{
// Buffer is full, start discarding older samples.
self.samples.pop_front();
}
self.samples
.push(Sample(component_id.clone(), instant, stack));
.push_back(Sample(component_id.clone(), instant, stack));
}
}
}
Expand Down
Expand Up @@ -30,11 +30,15 @@ fn test_sampler() {
);

const RATE: u64 = 10;
const MAX_DURATION: u64 = 10;
sampler_sender
.send(SamplerControlMsg::Enable(Duration::from_millis(RATE)))
.send(SamplerControlMsg::Enable(
Duration::from_millis(RATE),
Duration::from_secs(MAX_DURATION),
))
.unwrap();

thread::sleep(Duration::from_millis(30));
thread::sleep(Duration::from_millis(100));

sampler_sender.send(SamplerControlMsg::Disable).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions components/compositing/windowing.rs
Expand Up @@ -95,8 +95,8 @@ pub enum WindowEvent {
ToggleWebRenderDebug(WebRenderDebugOption),
/// Capture current WebRender
CaptureWebRender,
/// Toggle sampling profiler with the given sampling rate
ToggleSamplingProfiler(Duration),
/// Toggle sampling profiler with the given sampling rate and max duration.
ToggleSamplingProfiler(Duration, Duration),
}

impl Debug for WindowEvent {
Expand Down
4 changes: 2 additions & 2 deletions components/constellation/constellation.rs
Expand Up @@ -1216,9 +1216,9 @@ where
self.forward_event(destination_pipeline_id, event);
},
FromCompositorMsg::SetCursor(cursor) => self.handle_set_cursor_msg(cursor),
FromCompositorMsg::EnableProfiler(rate) => {
FromCompositorMsg::EnableProfiler(rate, max_duration) => {
for chan in &self.sampling_profiler_control {
if let Err(e) = chan.send(SamplerControlMsg::Enable(rate)) {
if let Err(e) = chan.send(SamplerControlMsg::Enable(rate, max_duration)) {
warn!("error communicating with sampling profiler: {}", e);
}
}
Expand Down
3 changes: 2 additions & 1 deletion components/msg/constellation_msg.rs
Expand Up @@ -494,6 +494,7 @@ pub trait BackgroundHangMonitor {
/// Messages to control the sampling profiler.
#[derive(Deserialize, Serialize)]
pub enum SamplerControlMsg {
Enable(Duration),
/// Enable the sampler, with a given sampling rate and max total sampling duration.
Enable(Duration, Duration),
Disable,
}
4 changes: 2 additions & 2 deletions components/script_traits/lib.rs
Expand Up @@ -778,8 +778,8 @@ pub enum ConstellationMsg {
ForwardEvent(PipelineId, CompositorEvent),
/// Requesting a change to the onscreen cursor.
SetCursor(Cursor),
/// Enable the sampling profiler.
EnableProfiler(Duration),
/// Enable the sampling profiler, with a given sampling rate and max total sampling duration.
EnableProfiler(Duration, Duration),
/// Disable the sampling profiler.
DisableProfiler,
}
Expand Down
4 changes: 2 additions & 2 deletions components/servo/lib.rs
Expand Up @@ -408,10 +408,10 @@ where
}
},

WindowEvent::ToggleSamplingProfiler(rate) => {
WindowEvent::ToggleSamplingProfiler(rate, max_duration) => {
self.profiler_enabled = !self.profiler_enabled;
let msg = if self.profiler_enabled {
ConstellationMsg::EnableProfiler(rate)
ConstellationMsg::EnableProfiler(rate, max_duration)
} else {
ConstellationMsg::DisableProfiler
};
Expand Down
6 changes: 5 additions & 1 deletion ports/servo/browser.rs
Expand Up @@ -121,8 +121,12 @@ impl Browser {
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(10);
let duration = env::var("SAMPLING_DURATION")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(10);
self.event_queue.push(WindowEvent::ToggleSamplingProfiler(
Duration::from_millis(rate)));
Duration::from_millis(rate), Duration::from_secs(duration)));
})
.shortcut(Modifiers::CONTROL, Key::F9, || {
self.event_queue.push(WindowEvent::CaptureWebRender)
Expand Down

0 comments on commit 0780298

Please sign in to comment.