Rename lots of profiling-related things.

------------------------------------------------------------------------
BEFORE                              AFTER
------------------------------------------------------------------------
util::memory                        util::mem
- heap_size_of                      - heap_size_of (unchanged)
- SizeOf                            - HeapSizeOf
  - size_of_excluding_self            - heap_size_of_children

prof::mem                           prof::mem
- MemoryProfilerChan                - ProfilerChan
- MemoryReport                      - Report
- MemoryReportsChan                 - ReportsChan
- MemoryReporter                    - Reporter
- MemoryProfilerMsg                 - ProfilerMsg
  - {R,UnR}egisterMemoryReporter      - {R,UnR}egisterReporter
- MemoryProfiler                    - Prof
- ReportsForest                     - ReportsForest (unchanged)
- ReportsTree                       - ReportsTree   (unchanged)
- SystemMemoryReporter              - SystemReporter

prof::time                          prof::time
- TimeProfilerChan                  - ProfilerChan
- TimerMetadata                     - TimerMetadata (unchanged)
- Formatable                        - Formattable [spelling!]
- TimeProfilerMsg                   - ProfilerMsg
- TimeProfilerCategory              - ProfilerCategory
- TimeProfilerBuckets               - ProfilerBuckets
- TimeProfiler                      - Profiler
- TimerMetadataFrameType            - TimerMetadataFrameType (unchanged)
- TimerMetadataReflowType           - TimerMetadataReflowType (unchanged)
- ProfilerMetadata                  - ProfilerMetadata (unchanged)

In a few places both prof::time and prof::mem are used, and so
module-qualification is needed to avoid overlap, e.g. time::Profiler and
mem::Profiler. Likewise with std::mem and prof::mem. This is not a big
deal.
This commit is contained in:
Nicholas Nethercote 2015-03-22 15:20:31 -07:00
parent 7f587f6cb5
commit ce36e574f4
19 changed files with 322 additions and 331 deletions

View file

@ -4,7 +4,7 @@
//! Memory profiling functions.
use self::system_reporter::SystemMemoryReporter;
use self::system_reporter::SystemReporter;
use std::borrow::ToOwned;
use std::cmp::Ordering;
use std::collections::HashMap;
@ -14,11 +14,11 @@ use std::time::duration::Duration;
use util::task::spawn_named;
#[derive(Clone)]
pub struct MemoryProfilerChan(pub Sender<MemoryProfilerMsg>);
pub struct ProfilerChan(pub Sender<ProfilerMsg>);
impl MemoryProfilerChan {
pub fn send(&self, msg: MemoryProfilerMsg) {
let MemoryProfilerChan(ref c) = *self;
impl ProfilerChan {
pub fn send(&self, msg: ProfilerMsg) {
let ProfilerChan(ref c) = *self;
c.send(msg).unwrap();
}
}
@ -32,7 +32,7 @@ macro_rules! path {
}}
}
pub struct MemoryReport {
pub struct Report {
/// The identifying path for this report.
pub path: Vec<String>,
@ -42,36 +42,36 @@ pub struct MemoryReport {
/// A channel through which memory reports can be sent.
#[derive(Clone)]
pub struct MemoryReportsChan(pub Sender<Vec<MemoryReport>>);
pub struct ReportsChan(pub Sender<Vec<Report>>);
impl MemoryReportsChan {
pub fn send(&self, report: Vec<MemoryReport>) {
let MemoryReportsChan(ref c) = *self;
impl ReportsChan {
pub fn send(&self, report: Vec<Report>) {
let ReportsChan(ref c) = *self;
c.send(report).unwrap();
}
}
/// A memory reporter is capable of measuring some data structure of interest. Because it needs
/// to be passed to and registered with the MemoryProfiler, it's typically a "small" (i.e. easily
/// to be passed to and registered with the Profiler, it's typically a "small" (i.e. easily
/// cloneable) value that provides access to a "large" data structure, e.g. a channel that can
/// inject a request for measurements into the event queue associated with the "large" data
/// structure.
pub trait MemoryReporter {
pub trait Reporter {
/// Collect one or more memory reports. Returns true on success, and false on failure.
fn collect_reports(&self, reports_chan: MemoryReportsChan) -> bool;
fn collect_reports(&self, reports_chan: ReportsChan) -> bool;
}
/// Messages that can be sent to the memory profiler thread.
pub enum MemoryProfilerMsg {
/// Register a MemoryReporter with the memory profiler. The String is only used to identify the
pub enum ProfilerMsg {
/// Register a Reporter with the memory profiler. The String is only used to identify the
/// reporter so it can be unregistered later. The String must be distinct from that used by any
/// other registered reporter otherwise a panic will occur.
RegisterMemoryReporter(String, Box<MemoryReporter + Send>),
RegisterReporter(String, Box<Reporter + Send>),
/// Unregister a MemoryReporter with the memory profiler. The String must match the name given
/// when the reporter was registered. If the String does not match the name of a registered
/// reporter a panic will occur.
UnregisterMemoryReporter(String),
/// Unregister a Reporter with the memory profiler. The String must match the name given when
/// the reporter was registered. If the String does not match the name of a registered reporter
/// a panic will occur.
UnregisterReporter(String),
/// Triggers printing of the memory profiling metrics.
Print,
@ -80,16 +80,16 @@ pub enum MemoryProfilerMsg {
Exit,
}
pub struct MemoryProfiler {
pub struct Profiler {
/// The port through which messages are received.
pub port: Receiver<MemoryProfilerMsg>,
pub port: Receiver<ProfilerMsg>,
/// Registered memory reporters.
reporters: HashMap<String, Box<MemoryReporter + Send>>,
reporters: HashMap<String, Box<Reporter + Send>>,
}
impl MemoryProfiler {
pub fn create(period: Option<f64>) -> MemoryProfilerChan {
impl Profiler {
pub fn create(period: Option<f64>) -> ProfilerChan {
let (chan, port) = channel();
// Create the timer thread if a period was provided.
@ -99,7 +99,7 @@ impl MemoryProfiler {
spawn_named("Memory profiler timer".to_owned(), move || {
loop {
sleep(period_ms);
if chan.send(MemoryProfilerMsg::Print).is_err() {
if chan.send(ProfilerMsg::Print).is_err() {
break;
}
}
@ -109,24 +109,23 @@ impl MemoryProfiler {
// Always spawn the memory profiler. If there is no timer thread it won't receive regular
// `Print` events, but it will still receive the other events.
spawn_named("Memory profiler".to_owned(), move || {
let mut memory_profiler = MemoryProfiler::new(port);
memory_profiler.start();
let mut mem_profiler = Profiler::new(port);
mem_profiler.start();
});
let memory_profiler_chan = MemoryProfilerChan(chan);
let mem_profiler_chan = ProfilerChan(chan);
// Register the system memory reporter, which will run on the memory profiler's own thread.
// It never needs to be unregistered, because as long as the memory profiler is running the
// system memory reporter can make measurements.
let system_reporter = Box::new(SystemMemoryReporter);
memory_profiler_chan.send(MemoryProfilerMsg::RegisterMemoryReporter("system".to_owned(),
system_reporter));
let system_reporter = Box::new(SystemReporter);
mem_profiler_chan.send(ProfilerMsg::RegisterReporter("system".to_owned(), system_reporter));
memory_profiler_chan
mem_profiler_chan
}
pub fn new(port: Receiver<MemoryProfilerMsg>) -> MemoryProfiler {
MemoryProfiler {
pub fn new(port: Receiver<ProfilerMsg>) -> Profiler {
Profiler {
port: port,
reporters: HashMap::new(),
}
@ -145,34 +144,33 @@ impl MemoryProfiler {
}
}
fn handle_msg(&mut self, msg: MemoryProfilerMsg) -> bool {
fn handle_msg(&mut self, msg: ProfilerMsg) -> bool {
match msg {
MemoryProfilerMsg::RegisterMemoryReporter(name, reporter) => {
ProfilerMsg::RegisterReporter(name, reporter) => {
// Panic if it has already been registered.
let name_clone = name.clone();
match self.reporters.insert(name, reporter) {
None => true,
Some(_) =>
panic!(format!("RegisterMemoryReporter: '{}' name is already in use",
name_clone)),
Some(_) => panic!(format!("RegisterReporter: '{}' name is already in use",
name_clone)),
}
},
MemoryProfilerMsg::UnregisterMemoryReporter(name) => {
ProfilerMsg::UnregisterReporter(name) => {
// Panic if it hasn't previously been registered.
match self.reporters.remove(&name) {
Some(_) => true,
None =>
panic!(format!("UnregisterMemoryReporter: '{}' name is unknown", &name)),
panic!(format!("UnregisterReporter: '{}' name is unknown", &name)),
}
},
MemoryProfilerMsg::Print => {
ProfilerMsg::Print => {
self.handle_print_msg();
true
},
MemoryProfilerMsg::Exit => false
ProfilerMsg::Exit => false
}
}
@ -189,7 +187,7 @@ impl MemoryProfiler {
let mut forest = ReportsForest::new();
for reporter in self.reporters.values() {
let (chan, port) = channel();
if reporter.collect_reports(MemoryReportsChan(chan)) {
if reporter.collect_reports(ReportsChan(chan)) {
if let Ok(reports) = port.recv() {
for report in reports.iter() {
forest.insert(&report.path, report.size);
@ -368,20 +366,20 @@ mod system_reporter {
use std::ffi::CString;
use std::mem::size_of;
use std::ptr::null_mut;
use super::{MemoryReport, MemoryReporter, MemoryReportsChan};
use super::{Report, Reporter, ReportsChan};
#[cfg(target_os="macos")]
use task_info::task_basic_info::{virtual_size, resident_size};
/// Collects global measurements from the OS and heap allocators.
pub struct SystemMemoryReporter;
pub struct SystemReporter;
impl MemoryReporter for SystemMemoryReporter {
fn collect_reports(&self, reports_chan: MemoryReportsChan) -> bool {
impl Reporter for SystemReporter {
fn collect_reports(&self, reports_chan: ReportsChan) -> bool {
let mut reports = vec![];
{
let mut report = |path, size| {
if let Some(size) = size {
reports.push(MemoryReport { path: path, size: size });
reports.push(Report { path: path, size: size });
}
};

View file

@ -19,11 +19,11 @@ use util::task::spawn_named;
// front-end representation of the profiler used to communicate with the profiler
#[derive(Clone)]
pub struct TimeProfilerChan(pub Sender<TimeProfilerMsg>);
pub struct ProfilerChan(pub Sender<ProfilerMsg>);
impl TimeProfilerChan {
pub fn send(&self, msg: TimeProfilerMsg) {
let TimeProfilerChan(ref c) = *self;
impl ProfilerChan {
pub fn send(&self, msg: ProfilerMsg) {
let ProfilerChan(ref c) = *self;
c.send(msg).unwrap();
}
}
@ -35,11 +35,11 @@ pub struct TimerMetadata {
incremental: bool,
}
pub trait Formatable {
pub trait Formattable {
fn format(&self) -> String;
}
impl Formatable for Option<TimerMetadata> {
impl Formattable for Option<TimerMetadata> {
fn format(&self) -> String {
match self {
// TODO(cgaebel): Center-align in the format strings as soon as rustc supports it.
@ -61,9 +61,9 @@ impl Formatable for Option<TimerMetadata> {
}
#[derive(Clone)]
pub enum TimeProfilerMsg {
pub enum ProfilerMsg {
/// Normal message used for reporting time
Time((TimeProfilerCategory, Option<TimerMetadata>), f64),
Time((ProfilerCategory, Option<TimerMetadata>), f64),
/// Message used to force print the profiling metrics
Print,
/// Tells the profiler to shut down.
@ -72,7 +72,7 @@ pub enum TimeProfilerMsg {
#[repr(u32)]
#[derive(PartialEq, Clone, PartialOrd, Eq, Ord)]
pub enum TimeProfilerCategory {
pub enum ProfilerCategory {
Compositing,
LayoutPerform,
LayoutStyleRecalc,
@ -92,60 +92,60 @@ pub enum TimeProfilerCategory {
ImageDecoding,
}
impl Formatable for TimeProfilerCategory {
impl Formattable for ProfilerCategory {
// some categories are subcategories of LayoutPerformCategory
// and should be printed to indicate this
fn format(&self) -> String {
let padding = match *self {
TimeProfilerCategory::LayoutStyleRecalc |
TimeProfilerCategory::LayoutRestyleDamagePropagation |
TimeProfilerCategory::LayoutNonIncrementalReset |
TimeProfilerCategory::LayoutGeneratedContent |
TimeProfilerCategory::LayoutMain |
TimeProfilerCategory::LayoutDispListBuild |
TimeProfilerCategory::LayoutShaping |
TimeProfilerCategory::LayoutDamagePropagate |
TimeProfilerCategory::PaintingPerTile |
TimeProfilerCategory::PaintingPrepBuff => "+ ",
TimeProfilerCategory::LayoutParallelWarmup |
TimeProfilerCategory::LayoutSelectorMatch |
TimeProfilerCategory::LayoutTreeBuilder => "| + ",
ProfilerCategory::LayoutStyleRecalc |
ProfilerCategory::LayoutRestyleDamagePropagation |
ProfilerCategory::LayoutNonIncrementalReset |
ProfilerCategory::LayoutGeneratedContent |
ProfilerCategory::LayoutMain |
ProfilerCategory::LayoutDispListBuild |
ProfilerCategory::LayoutShaping |
ProfilerCategory::LayoutDamagePropagate |
ProfilerCategory::PaintingPerTile |
ProfilerCategory::PaintingPrepBuff => "+ ",
ProfilerCategory::LayoutParallelWarmup |
ProfilerCategory::LayoutSelectorMatch |
ProfilerCategory::LayoutTreeBuilder => "| + ",
_ => ""
};
let name = match *self {
TimeProfilerCategory::Compositing => "Compositing",
TimeProfilerCategory::LayoutPerform => "Layout",
TimeProfilerCategory::LayoutStyleRecalc => "Style Recalc",
TimeProfilerCategory::LayoutRestyleDamagePropagation => "Restyle Damage Propagation",
TimeProfilerCategory::LayoutNonIncrementalReset => "Non-incremental reset (temporary)",
TimeProfilerCategory::LayoutSelectorMatch => "Selector Matching",
TimeProfilerCategory::LayoutTreeBuilder => "Tree Building",
TimeProfilerCategory::LayoutDamagePropagate => "Damage Propagation",
TimeProfilerCategory::LayoutGeneratedContent => "Generated Content Resolution",
TimeProfilerCategory::LayoutMain => "Primary Layout Pass",
TimeProfilerCategory::LayoutParallelWarmup => "Parallel Warmup",
TimeProfilerCategory::LayoutShaping => "Shaping",
TimeProfilerCategory::LayoutDispListBuild => "Display List Construction",
TimeProfilerCategory::PaintingPerTile => "Painting Per Tile",
TimeProfilerCategory::PaintingPrepBuff => "Buffer Prep",
TimeProfilerCategory::Painting => "Painting",
TimeProfilerCategory::ImageDecoding => "Image Decoding",
ProfilerCategory::Compositing => "Compositing",
ProfilerCategory::LayoutPerform => "Layout",
ProfilerCategory::LayoutStyleRecalc => "Style Recalc",
ProfilerCategory::LayoutRestyleDamagePropagation => "Restyle Damage Propagation",
ProfilerCategory::LayoutNonIncrementalReset => "Non-incremental reset (temporary)",
ProfilerCategory::LayoutSelectorMatch => "Selector Matching",
ProfilerCategory::LayoutTreeBuilder => "Tree Building",
ProfilerCategory::LayoutDamagePropagate => "Damage Propagation",
ProfilerCategory::LayoutGeneratedContent => "Generated Content Resolution",
ProfilerCategory::LayoutMain => "Primary Layout Pass",
ProfilerCategory::LayoutParallelWarmup => "Parallel Warmup",
ProfilerCategory::LayoutShaping => "Shaping",
ProfilerCategory::LayoutDispListBuild => "Display List Construction",
ProfilerCategory::PaintingPerTile => "Painting Per Tile",
ProfilerCategory::PaintingPrepBuff => "Buffer Prep",
ProfilerCategory::Painting => "Painting",
ProfilerCategory::ImageDecoding => "Image Decoding",
};
format!("{}{}", padding, name)
}
}
type TimeProfilerBuckets = BTreeMap<(TimeProfilerCategory, Option<TimerMetadata>), Vec<f64>>;
type ProfilerBuckets = BTreeMap<(ProfilerCategory, Option<TimerMetadata>), Vec<f64>>;
// back end of the profiler that handles data aggregation and performance metrics
pub struct TimeProfiler {
pub port: Receiver<TimeProfilerMsg>,
buckets: TimeProfilerBuckets,
pub last_msg: Option<TimeProfilerMsg>,
pub struct Profiler {
pub port: Receiver<ProfilerMsg>,
buckets: ProfilerBuckets,
pub last_msg: Option<ProfilerMsg>,
}
impl TimeProfiler {
pub fn create(period: Option<f64>) -> TimeProfilerChan {
impl Profiler {
pub fn create(period: Option<f64>) -> ProfilerChan {
let (chan, port) = channel();
match period {
Some(period) => {
@ -154,14 +154,14 @@ impl TimeProfiler {
spawn_named("Time profiler timer".to_owned(), move || {
loop {
sleep(period);
if chan.send(TimeProfilerMsg::Print).is_err() {
if chan.send(ProfilerMsg::Print).is_err() {
break;
}
}
});
// Spawn the time profiler.
spawn_named("Time profiler".to_owned(), move || {
let mut profiler = TimeProfiler::new(port);
let mut profiler = Profiler::new(port);
profiler.start();
});
}
@ -170,7 +170,7 @@ impl TimeProfiler {
spawn_named("Time profiler".to_owned(), move || {
loop {
match port.recv() {
Err(_) | Ok(TimeProfilerMsg::Exit) => break,
Err(_) | Ok(ProfilerMsg::Exit) => break,
_ => {}
}
}
@ -178,11 +178,11 @@ impl TimeProfiler {
}
}
TimeProfilerChan(chan)
ProfilerChan(chan)
}
pub fn new(port: Receiver<TimeProfilerMsg>) -> TimeProfiler {
TimeProfiler {
pub fn new(port: Receiver<ProfilerMsg>) -> Profiler {
Profiler {
port: port,
buckets: BTreeMap::new(),
last_msg: None,
@ -203,7 +203,7 @@ impl TimeProfiler {
}
}
fn find_or_insert(&mut self, k: (TimeProfilerCategory, Option<TimerMetadata>), t: f64) {
fn find_or_insert(&mut self, k: (ProfilerCategory, Option<TimerMetadata>), t: f64) {
match self.buckets.get_mut(&k) {
None => {},
Some(v) => { v.push(t); return; },
@ -212,15 +212,15 @@ impl TimeProfiler {
self.buckets.insert(k, vec!(t));
}
fn handle_msg(&mut self, msg: TimeProfilerMsg) -> bool {
fn handle_msg(&mut self, msg: ProfilerMsg) -> bool {
match msg.clone() {
TimeProfilerMsg::Time(k, t) => self.find_or_insert(k, t),
TimeProfilerMsg::Print => match self.last_msg {
ProfilerMsg::Time(k, t) => self.find_or_insert(k, t),
ProfilerMsg::Print => match self.last_msg {
// only print if more data has arrived since the last printout
Some(TimeProfilerMsg::Time(..)) => self.print_buckets(),
Some(ProfilerMsg::Time(..)) => self.print_buckets(),
_ => ()
},
TimeProfilerMsg::Exit => return false,
ProfilerMsg::Exit => return false,
};
self.last_msg = Some(msg);
true
@ -268,9 +268,9 @@ pub enum TimerMetadataReflowType {
pub type ProfilerMetadata<'a> = Option<(&'a Url, TimerMetadataFrameType, TimerMetadataReflowType)>;
pub fn profile<T, F>(category: TimeProfilerCategory,
pub fn profile<T, F>(category: ProfilerCategory,
meta: ProfilerMetadata,
time_profiler_chan: TimeProfilerChan,
profiler_chan: ProfilerChan,
callback: F)
-> T
where F: FnOnce() -> T
@ -285,7 +285,7 @@ pub fn profile<T, F>(category: TimeProfilerCategory,
iframe: iframe == TimerMetadataFrameType::IFrame,
incremental: reflow_type == TimerMetadataReflowType::Incremental,
});
time_profiler_chan.send(TimeProfilerMsg::Time((category, meta), ms));
profiler_chan.send(ProfilerMsg::Time((category, meta), ms));
return val;
}