minor refactor of profiler

This commit is contained in:
Tim Kuehn 2013-08-27 21:08:41 -04:00
parent 4aaaf60d3f
commit ca620992a6

View file

@ -42,11 +42,22 @@ pub enum ProfilerCategory {
RenderingPrepBuffCategory, RenderingPrepBuffCategory,
RenderingCategory, RenderingCategory,
// hackish but helps prevent errors when adding new categories // hackish but helps prevent errors when adding new categories
NUM_BUCKETS, NumBuckets,
} }
// FIXME(#5873) this should be initialized by a NUM_BUCKETS cast, struct ProfilerBucket {
static BUCKETS: uint = 13; category: ProfilerCategory,
type ProfilerBuckets = [(ProfilerCategory, ~[float]), ..BUCKETS]; data: ~[float],
}
impl ProfilerBucket {
fn new(category: ProfilerCategory) -> ProfilerBucket {
ProfilerBucket {
category: category,
data: ~[],
}
}
}
// FIXME(rust#5873) this should be initialized by a NumBuckets cast,
type ProfilerBuckets = [ProfilerBucket, ..13];
pub enum ProfilerMsg { pub enum ProfilerMsg {
// Normal message used for reporting time // Normal message used for reporting time
@ -65,26 +76,26 @@ pub struct Profiler {
impl ProfilerCategory { impl ProfilerCategory {
// convenience function to not have to cast every time // convenience function to not have to cast every time
pub fn num_buckets() -> uint { pub fn num_buckets() -> uint {
NUM_BUCKETS as uint NumBuckets as uint
} }
// enumeration of all ProfilerCategory types // enumeration of all ProfilerCategory types
// TODO(tkuehn): is there a better way to ensure proper order of categories? // TODO(tkuehn): is there a better way to ensure proper order of categories?
fn empty_buckets() -> ProfilerBuckets { fn empty_buckets() -> ProfilerBuckets {
let buckets = [ let buckets = [
(CompositingCategory, ~[]), ProfilerBucket::new(CompositingCategory),
(LayoutQueryCategory, ~[]), ProfilerBucket::new(LayoutQueryCategory),
(LayoutPerformCategory, ~[]), ProfilerBucket::new(LayoutPerformCategory),
(LayoutAuxInitCategory, ~[]), ProfilerBucket::new(LayoutAuxInitCategory),
(LayoutSelectorMatchCategory, ~[]), ProfilerBucket::new(LayoutSelectorMatchCategory),
(LayoutTreeBuilderCategory, ~[]), ProfilerBucket::new(LayoutTreeBuilderCategory),
(LayoutMainCategory, ~[]), ProfilerBucket::new(LayoutMainCategory),
(LayoutShapingCategory, ~[]), ProfilerBucket::new(LayoutShapingCategory),
(LayoutDispListBuildCategory, ~[]), ProfilerBucket::new(LayoutDispListBuildCategory),
(GfxRegenAvailableFontsCategory, ~[]), ProfilerBucket::new(GfxRegenAvailableFontsCategory),
(RenderingDrawingCategory, ~[]), ProfilerBucket::new(RenderingDrawingCategory),
(RenderingPrepBuffCategory, ~[]), ProfilerBucket::new(RenderingPrepBuffCategory),
(RenderingCategory, ~[]), ProfilerBucket::new(RenderingCategory),
]; ];
ProfilerCategory::check_order(&buckets); ProfilerCategory::check_order(&buckets);
@ -93,8 +104,8 @@ impl ProfilerCategory {
// ensure that the order of the buckets matches the order of the enum categories // ensure that the order of the buckets matches the order of the enum categories
fn check_order(vec: &ProfilerBuckets) { fn check_order(vec: &ProfilerBuckets) {
for &(category, _) in vec.iter() { for (i, bucket) in vec.iter().enumerate() {
if category != vec[category as uint].first() { if bucket.category as uint != i {
fail!("Enum category does not match bucket index. This is a bug."); fail!("Enum category does not match bucket index. This is a bug.");
} }
} }
@ -141,14 +152,11 @@ impl Profiler {
fn handle_msg(&mut self, msg: ProfilerMsg) { fn handle_msg(&mut self, msg: ProfilerMsg) {
match msg { match msg {
TimeMsg(category, t) => match self.buckets[category as uint] { TimeMsg(category, t) => self.buckets[category as uint].data.push(t),
//TODO(tkuehn): would be nice to have tuple.second_mut()
(_, ref mut data) => data.push(t),
},
PrintMsg => match self.last_msg { PrintMsg => match self.last_msg {
// only print if more data has arrived since the last printout // only print if more data has arrived since the last printout
Some(TimeMsg(*)) => self.print_buckets(), Some(TimeMsg(*)) => self.print_buckets(),
_ => {} _ => ()
}, },
}; };
self.last_msg = Some(msg); self.last_msg = Some(msg);
@ -158,10 +166,7 @@ impl Profiler {
println(fmt!("%31s %15s %15s %15s %15s %15s", println(fmt!("%31s %15s %15s %15s %15s %15s",
"_category_", "_mean (ms)_", "_median (ms)_", "_category_", "_mean (ms)_", "_median (ms)_",
"_min (ms)_", "_max (ms)_", "_bucket size_")); "_min (ms)_", "_max (ms)_", "_bucket size_"));
for bucket in self.buckets.mut_iter() { for &ProfilerBucket { category: ref category, data: ref mut data } in self.buckets.mut_iter() {
let (category, data) = match *bucket {
(category, ref mut data) => (category, data),
};
tim_sort(*data); tim_sort(*data);
let data_len = data.len(); let data_len = data.len();
if data_len > 0 { if data_len > 0 {