Create heartbeats dynamically as needed - avoids the need for hardcoding and maintaining heartbeats for profiler categories.

Use consistent variable names.
Use Box::into_raw and Box::from_raw instead of std::mem::transmute.
This commit is contained in:
Connor Imes 2017-02-07 14:20:43 -06:00
parent d10ad4465f
commit e602162a3d

View file

@ -11,7 +11,6 @@ use std::collections::HashMap;
use std::env::var_os; use std::env::var_os;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::mem;
use std::path::Path; use std::path::Path;
@ -19,71 +18,23 @@ static mut HBS: Option<*mut HashMap<ProfilerCategory, Heartbeat>> = None;
/// Initialize heartbeats /// Initialize heartbeats
pub fn init() { pub fn init() {
let mut hbs: HashMap<ProfilerCategory, Heartbeat> = HashMap::new(); let mut hbs: Box<HashMap<ProfilerCategory, Heartbeat>> = Box::new(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);
maybe_create_heartbeat(&mut hbs, ProfilerCategory::ApplicationHeartbeat); maybe_create_heartbeat(&mut hbs, ProfilerCategory::ApplicationHeartbeat);
unsafe { unsafe {
HBS = Some(mem::transmute(Box::new(hbs))); HBS = Some(Box::into_raw(hbs));
} }
} }
/// Log regmaining buffer data and cleanup heartbeats /// Log regmaining buffer data and cleanup heartbeats
pub fn cleanup() { pub fn cleanup() {
unsafe { unsafe {
if let Some(hbs) = HBS { if let Some(hbs_ptr) = HBS {
let mut h: Box<HashMap<ProfilerCategory, Heartbeat>> = mem::transmute(hbs); let mut hbs: Box<HashMap<ProfilerCategory, Heartbeat>> = Box::from_raw(hbs_ptr);
for (_, mut v) in h.iter_mut() { for (_, mut v) in hbs.iter_mut() {
// log any remaining heartbeat records before dropping // log any remaining heartbeat records before dropping
log_heartbeat_records(v); log_heartbeat_records(v);
} }
h.clear(); hbs.clear();
} }
HBS = None; HBS = None;
} }
@ -92,7 +43,7 @@ pub fn cleanup() {
/// Check if a heartbeat exists for the given category /// Check if a heartbeat exists for the given category
pub fn is_heartbeat_enabled(category: &ProfilerCategory) -> bool { pub fn is_heartbeat_enabled(category: &ProfilerCategory) -> bool {
unsafe { 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))
} }
} }
@ -103,8 +54,11 @@ pub fn maybe_heartbeat(category: &ProfilerCategory,
start_energy: u64, start_energy: u64,
end_energy: u64) { end_energy: u64) {
unsafe { unsafe {
if let Some(map) = HBS { if let Some(hbs_ptr) = HBS {
if let Some(mut h) = (*map).get_mut(category) { 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); (*h).heartbeat(0, 1, start_time, end_time, start_energy, end_energy);
} }
} }
@ -179,8 +133,8 @@ fn log_heartbeat_records(hb: &mut Heartbeat) {
/// When this is called from native C, the heartbeat is safely locked /// When this is called from native C, the heartbeat is safely locked
extern fn heartbeat_window_callback(hb: *const HeartbeatContext) { extern fn heartbeat_window_callback(hb: *const HeartbeatContext) {
unsafe { unsafe {
if let Some(map) = HBS { if let Some(hbs_ptr) = HBS {
for (_, v) in (*map).iter_mut() { for (_, v) in (*hbs_ptr).iter_mut() {
if &v.hb as *const HeartbeatContext == hb { if &v.hb as *const HeartbeatContext == hb {
log_heartbeat_records(v); log_heartbeat_records(v);
} }