Skip to content

Commit

Permalink
Add perfmon initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
caizixian committed May 14, 2021
1 parent 64fe10d commit 80a1d17
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ atomic-traits = "0.2.0"
atomic = "0.4.6"
spin = "0.5.2"
env_logger = "0.8.2"
[target.'cfg(target_os = "linux")'.dependencies]
pfm-sys = "0.0.4"

[dev-dependencies]
crossbeam = "0.7.3"
Expand Down
2 changes: 2 additions & 0 deletions src/util/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub use self::counter::Timer;

pub mod counter;
pub mod stats;
#[cfg(target_os = "linux")]
pub mod perf;
26 changes: 26 additions & 0 deletions src/util/statistics/perf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use pfm_sys::{pfm_initialize, pfm_strerror, PFM_SUCCESS};
use std::ffi::CStr;

struct Perf {
initialized: bool,
}

impl Default for Perf {
fn default() -> Perf {
Perf { initialized: false }
}
}

impl Perf {
fn initialize(&mut self) -> Result<(), String> {
let result = unsafe { pfm_initialize() };
if result == PFM_SUCCESS {
self.initialized = true;
Ok(())
} else {
let err_c_char = unsafe { pfm_strerror(result) };
let cstr = unsafe { CStr::from_ptr(err_c_char) };
Err(String::from(cstr.to_str().unwrap()))
}
}
}

0 comments on commit 80a1d17

Please sign in to comment.