From 6f2d1482fe29bf16ce72607b9563c629e416da45 Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Sun, 29 Sep 2024 22:37:48 +0800 Subject: [PATCH] profiler imports --- src/plugins/events.rs | 7 +--- src/plugins/profiler/dummy_profiler.rs | 22 ++++++++++++ src/plugins/profiler/mod.rs | 50 -------------------------- src/plugins/profiler/perf_profiler.rs | 49 +++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/plugins/events.rs b/src/plugins/events.rs index a420774..b71ebfe 100644 --- a/src/plugins/events.rs +++ b/src/plugins/events.rs @@ -60,12 +60,7 @@ impl EventManager { } pub fn downcast_listener(&mut self, name: &str) -> Option<&mut T> { - Some( - self.get_listener(name)? - .as_any() - .downcast_mut::() - .expect("Listener is not of the expected type"), - ) + self.get_listener(name)?.as_any().downcast_mut::() } /// Remove a listener by name. diff --git a/src/plugins/profiler/dummy_profiler.rs b/src/plugins/profiler/dummy_profiler.rs index 0e5d4d2..f387c52 100644 --- a/src/plugins/profiler/dummy_profiler.rs +++ b/src/plugins/profiler/dummy_profiler.rs @@ -16,3 +16,25 @@ impl Profiler for PerfProfiler { Ok(CounterValues::default()) } } + +// Plugin +pub static PERF_CNT_EVENT_LISTENER_NAME: &str = "_binggan_perf"; + +#[derive(Default)] +pub struct PerfCounterPerBench {} + +impl PerfCounterPerBench { + pub fn get_by_bench_id_mut(&mut self, bench_id: &BenchId) -> Option<&mut PerfCounters> { + None + } +} + +impl EventListener for PerfCounterPerBench { + fn as_any(&mut self) -> &mut dyn Any { + self + } + fn name(&self) -> &'static str { + PERF_CNT_EVENT_LISTENER_NAME + } + fn on_event(&mut self, event: BingganEvents) {} +} diff --git a/src/plugins/profiler/mod.rs b/src/plugins/profiler/mod.rs index ca5b016..3f3163c 100644 --- a/src/plugins/profiler/mod.rs +++ b/src/plugins/profiler/mod.rs @@ -1,11 +1,6 @@ -use std::any::Any; - use crate::stats::*; use miniserde::*; -use crate::bench_id::BenchId; -use crate::plugins::{BingganEvents, EventListener, PerBenchData}; - #[cfg(not(target_os = "linux"))] pub(crate) mod dummy_profiler; #[cfg(target_os = "linux")] @@ -115,48 +110,3 @@ impl CounterValues { ] } } - -pub static PERF_CNT_EVENT_LISTENER_NAME: &str = "_binggan_perf"; - -/// Integration via EventListener -/// One counter per bench id. -#[derive(Default)] -pub struct PerfCounterPerBench { - perf_per_bench: PerBenchData>, -} - -impl PerfCounterPerBench { - pub fn get_by_bench_id_mut(&mut self, bench_id: &BenchId) -> Option<&mut PerfCounters> { - self.perf_per_bench - .get_mut(bench_id) - .and_then(Option::as_mut) - } -} - -impl EventListener for PerfCounterPerBench { - fn as_any(&mut self) -> &mut dyn Any { - self - } - fn name(&self) -> &'static str { - PERF_CNT_EVENT_LISTENER_NAME - } - fn on_event(&mut self, event: BingganEvents) { - match event { - BingganEvents::BenchStart(bench_id) => { - self.perf_per_bench - .insert_if_absent(bench_id, || PerfCounters::new().ok()); - let perf = self.perf_per_bench.get_mut(bench_id).unwrap(); - if let Some(perf) = perf { - perf.enable(); - } - } - BingganEvents::BenchStop(bench_id, _) => { - let perf = self.perf_per_bench.get_mut(bench_id).unwrap(); - if let Some(perf) = perf { - perf.disable(); - } - } - _ => {} - } - } -} diff --git a/src/plugins/profiler/perf_profiler.rs b/src/plugins/profiler/perf_profiler.rs index ba97f13..c642de2 100644 --- a/src/plugins/profiler/perf_profiler.rs +++ b/src/plugins/profiler/perf_profiler.rs @@ -1,10 +1,13 @@ use std::error::Error; +use crate::bench_id::BenchId; use crate::plugins::profiler::CounterValues; use crate::plugins::profiler::Profiler; +use crate::plugins::{BingganEvents, EventListener, PerBenchData}; use perf_event::events::{Cache, CacheOp, CacheResult, Hardware, WhichCache}; use perf_event::Counter; use perf_event::{Builder, Group}; +use std::any::Any; pub(crate) struct PerfCounters { group: Group, @@ -92,3 +95,49 @@ impl Profiler for PerfCounters { }) } } + +// Plugin +pub static PERF_CNT_EVENT_LISTENER_NAME: &str = "_binggan_perf"; + +/// Integration via EventListener +/// One counter per bench id. +#[derive(Default)] +pub struct PerfCounterPerBench { + perf_per_bench: PerBenchData>, +} + +impl PerfCounterPerBench { + pub fn get_by_bench_id_mut(&mut self, bench_id: &BenchId) -> Option<&mut PerfCounters> { + self.perf_per_bench + .get_mut(bench_id) + .and_then(Option::as_mut) + } +} + +impl EventListener for PerfCounterPerBench { + fn as_any(&mut self) -> &mut dyn Any { + self + } + fn name(&self) -> &'static str { + PERF_CNT_EVENT_LISTENER_NAME + } + fn on_event(&mut self, event: BingganEvents) { + match event { + BingganEvents::BenchStart(bench_id) => { + self.perf_per_bench + .insert_if_absent(bench_id, || PerfCounters::new().ok()); + let perf = self.perf_per_bench.get_mut(bench_id).unwrap(); + if let Some(perf) = perf { + perf.enable(); + } + } + BingganEvents::BenchStop(bench_id, _) => { + let perf = self.perf_per_bench.get_mut(bench_id).unwrap(); + if let Some(perf) = perf { + perf.disable(); + } + } + _ => {} + } + } +}