Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create heartbeats dynamically as needed - avoids the need for hardcod…
…ing and maintaining heartbeats for profiler categories.

Use consistent variable names.
Use Box::into_raw and Box::from_raw instead of std::mem::transmute.
  • Loading branch information
connorimes committed Feb 20, 2017
1 parent d10ad44 commit e602162
Showing 1 changed file with 14 additions and 60 deletions.
74 changes: 14 additions & 60 deletions components/profile/heartbeats.rs
Expand Up @@ -11,79 +11,30 @@ use std::collections::HashMap;
use std::env::var_os;
use std::error::Error;
use std::fs::File;
use std::mem;
use std::path::Path;


static mut HBS: Option<*mut HashMap<ProfilerCategory, Heartbeat>> = None;

/// Initialize heartbeats
pub fn init() {
let mut hbs: HashMap<ProfilerCategory, Heartbeat> = HashMap::new();
maybe_create_heartbeat(&mut hbs, ProfilerCategory::Compositing);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutPerform);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutStyleRecalc);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutTextShaping);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutRestyleDamagePropagation);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutNonIncrementalReset);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutSelectorMatch);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutTreeBuilder);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutDamagePropagate);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutGeneratedContent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutDisplayListSorting);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutFloatPlacementSpeculation);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutMain);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutStoreOverflow);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutParallelWarmup);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::LayoutDispListBuild);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::NetHTTPRequestResponse);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::PaintingPerTile);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::PaintingPrepBuff);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::Painting);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ImageDecoding);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ImageSaving);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptAttachLayout);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptConstellationMsg);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptDevtoolsMsg);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptDocumentEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptDomEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptEvaluate);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptFileRead);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptImageCacheMsg);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptInputEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptNetworkEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptParseHTML);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptPlannedNavigation);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptResize);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptSetScrollState);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptSetViewport);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptTimerEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptStylesheetLoad);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptUpdateReplacedElement);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptWebSocketEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptWorkerEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptServiceWorkerEvent);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptParseXML);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptEnterFullscreen);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptExitFullscreen);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ScriptWebVREvent);
let mut hbs: Box<HashMap<ProfilerCategory, Heartbeat>> = Box::new(HashMap::new());
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ApplicationHeartbeat);
unsafe {
HBS = Some(mem::transmute(Box::new(hbs)));
HBS = Some(Box::into_raw(hbs));
}
}

/// Log regmaining buffer data and cleanup heartbeats
pub fn cleanup() {
unsafe {
if let Some(hbs) = HBS {
let mut h: Box<HashMap<ProfilerCategory, Heartbeat>> = mem::transmute(hbs);
for (_, mut v) in h.iter_mut() {
if let Some(hbs_ptr) = HBS {
let mut hbs: Box<HashMap<ProfilerCategory, Heartbeat>> = Box::from_raw(hbs_ptr);
for (_, mut v) in hbs.iter_mut() {
// log any remaining heartbeat records before dropping
log_heartbeat_records(v);
}
h.clear();
hbs.clear();
}
HBS = None;
}
Expand All @@ -92,7 +43,7 @@ pub fn cleanup() {
/// Check if a heartbeat exists for the given category
pub fn is_heartbeat_enabled(category: &ProfilerCategory) -> bool {
unsafe {
HBS.map_or(false, |m| (*m).contains_key(category))
HBS.map_or(false, |hbs_ptr| (*hbs_ptr).contains_key(category) || is_create_heartbeat(category))
}
}

Expand All @@ -103,8 +54,11 @@ pub fn maybe_heartbeat(category: &ProfilerCategory,
start_energy: u64,
end_energy: u64) {
unsafe {
if let Some(map) = HBS {
if let Some(mut h) = (*map).get_mut(category) {
if let Some(hbs_ptr) = HBS {
if !(*hbs_ptr).contains_key(category) {
maybe_create_heartbeat(&mut (*hbs_ptr), category.clone());
}
if let Some(mut h) = (*hbs_ptr).get_mut(category) {
(*h).heartbeat(0, 1, start_time, end_time, start_energy, end_energy);
}
}
Expand Down Expand Up @@ -179,8 +133,8 @@ fn log_heartbeat_records(hb: &mut Heartbeat) {
/// When this is called from native C, the heartbeat is safely locked
extern fn heartbeat_window_callback(hb: *const HeartbeatContext) {
unsafe {
if let Some(map) = HBS {
for (_, v) in (*map).iter_mut() {
if let Some(hbs_ptr) = HBS {
for (_, v) in (*hbs_ptr).iter_mut() {
if &v.hb as *const HeartbeatContext == hb {
log_heartbeat_records(v);
}
Expand Down

0 comments on commit e602162

Please sign in to comment.