#![deny(unsafe_code)]
use std::time::Duration;
use std::{fmt, mem};
use base::id::PipelineId;
use ipc_channel::ipc::IpcSender;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum ScriptHangAnnotation {
AttachLayout,
ConstellationMsg,
DevtoolsMsg,
DocumentEvent,
DomEvent,
FileRead,
FormPlannedNavigation,
ImageCacheMsg,
InputEvent,
HistoryEvent,
NetworkEvent,
Resize,
ScriptEvent,
SetScrollState,
SetViewport,
StylesheetLoad,
TimerEvent,
UpdateReplacedElement,
WebSocketEvent,
WorkerEvent,
WorkletEvent,
ServiceWorkerEvent,
EnterFullscreen,
ExitFullscreen,
WebVREvent,
PerformanceTimelineTask,
PortMessage,
WebGPUMsg,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum HangAnnotation {
Script(ScriptHangAnnotation),
}
#[derive(Deserialize, Serialize)]
pub enum HangMonitorAlert {
Hang(HangAlert),
Profile(Vec<u8>),
}
impl fmt::Debug for HangMonitorAlert {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
HangMonitorAlert::Hang(..) => write!(fmt, "Hang"),
HangMonitorAlert::Profile(..) => write!(fmt, "Profile"),
}
}
}
#[derive(Deserialize, Serialize)]
pub enum HangAlert {
Transient(MonitoredComponentId, HangAnnotation),
Permanent(MonitoredComponentId, HangAnnotation, Option<HangProfile>),
}
impl fmt::Debug for HangAlert {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let (annotation, profile) = match self {
HangAlert::Transient(component_id, annotation) => {
write!(
fmt,
"\n The following component is experiencing a transient hang: \n {:?}",
component_id
)?;
(*annotation, None)
},
HangAlert::Permanent(component_id, annotation, profile) => {
write!(
fmt,
"\n The following component is experiencing a permanent hang: \n {:?}",
component_id
)?;
(*annotation, profile.clone())
},
};
write!(fmt, "\n Annotation for the hang:\n{:?}", annotation)?;
if let Some(profile) = profile {
write!(fmt, "\n {:?}", profile)?;
}
Ok(())
}
}
#[derive(Clone, Deserialize, Serialize)]
pub struct HangProfileSymbol {
pub name: Option<String>,
pub filename: Option<String>,
pub lineno: Option<u32>,
}
#[derive(Clone, Deserialize, Serialize)]
pub struct HangProfile {
pub backtrace: Vec<HangProfileSymbol>,
}
impl fmt::Debug for HangProfile {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let hex_width = mem::size_of::<usize>() * 2 + 2;
write!(fmt, "HangProfile backtrace:")?;
if self.backtrace.is_empty() {
write!(fmt, "backtrace failed to resolve")?;
return Ok(());
}
for symbol in self.backtrace.iter() {
write!(fmt, "\n {:1$}", "", hex_width)?;
if let Some(ref name) = symbol.name {
write!(fmt, " - {}", name)?;
} else {
write!(fmt, " - <unknown>")?;
}
if let (Some(ref file), Some(ref line)) = (symbol.filename.as_ref(), symbol.lineno) {
write!(fmt, "\n {:3$}at {}:{}", "", file, line, hex_width)?;
}
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum MonitoredComponentType {
Script,
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct MonitoredComponentId(pub PipelineId, pub MonitoredComponentType);
pub trait BackgroundHangMonitorRegister: BackgroundHangMonitorClone + Send {
fn register_component(
&self,
component: MonitoredComponentId,
transient_hang_timeout: Duration,
permanent_hang_timeout: Duration,
exit_signal: Option<Box<dyn BackgroundHangMonitorExitSignal>>,
) -> Box<dyn BackgroundHangMonitor>;
}
impl Clone for Box<dyn BackgroundHangMonitorRegister> {
fn clone(&self) -> Box<dyn BackgroundHangMonitorRegister> {
self.clone_box()
}
}
pub trait BackgroundHangMonitorClone {
fn clone_box(&self) -> Box<dyn BackgroundHangMonitorRegister>;
}
pub trait BackgroundHangMonitor {
fn notify_activity(&self, annotation: HangAnnotation);
fn notify_wait(&self);
fn unregister(&self);
}
pub trait BackgroundHangMonitorExitSignal: Send {
fn signal_to_exit(&self);
}
#[derive(Deserialize, Serialize)]
pub enum BackgroundHangMonitorControlMsg {
EnableSampler(Duration, Duration),
DisableSampler,
Exit(IpcSender<()>),
}