Skip to content

Commit

Permalink
profiler imports
Browse files Browse the repository at this point in the history
  • Loading branch information
PSeitz committed Sep 29, 2024
1 parent 7639b02 commit 6f2d148
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 56 deletions.
7 changes: 1 addition & 6 deletions src/plugins/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ impl EventManager {
}

pub fn downcast_listener<T: 'static>(&mut self, name: &str) -> Option<&mut T> {
Some(
self.get_listener(name)?
.as_any()
.downcast_mut::<T>()
.expect("Listener is not of the expected type"),
)
self.get_listener(name)?.as_any().downcast_mut::<T>()
}

/// Remove a listener by name.
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/profiler/dummy_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
50 changes: 0 additions & 50 deletions src/plugins/profiler/mod.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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<Option<PerfCounters>>,
}

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();
}
}
_ => {}
}
}
}
49 changes: 49 additions & 0 deletions src/plugins/profiler/perf_profiler.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<Option<PerfCounters>>,
}

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();
}
}
_ => {}
}
}
}

0 comments on commit 6f2d148

Please sign in to comment.