Skip to content

Commit 7ad6ef7

Browse files
chore: add instructions event, and use runner_shared for monitored events
1 parent 9b3b64b commit 7ad6ef7

3 files changed

Lines changed: 57 additions & 19 deletions

File tree

crates/runner-shared/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pub mod artifacts;
22
pub mod debug_info;
33
pub mod fifo;
44
pub mod metadata;
5+
pub mod perf_event;
56
pub mod unwind_data;
67
pub mod walltime_results;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Subset of perf events that CodSpeed supports.
2+
#[derive(Debug, Clone, Copy)]
3+
pub enum PerfEvent {
4+
CpuCycles,
5+
CacheReferences,
6+
CacheMisses,
7+
Instructions,
8+
}
9+
10+
impl PerfEvent {
11+
pub fn to_perf_string(&self) -> &'static str {
12+
match self {
13+
PerfEvent::CpuCycles => "cpu-cycles",
14+
PerfEvent::CacheReferences => "cache-references",
15+
PerfEvent::CacheMisses => "cache-misses",
16+
PerfEvent::Instructions => "instructions",
17+
}
18+
}
19+
20+
pub fn from_perf_string(event: &str) -> Option<PerfEvent> {
21+
match event {
22+
"cpu-cycles" => Some(PerfEvent::CpuCycles),
23+
"cache-references" => Some(PerfEvent::CacheReferences),
24+
"cache-misses" => Some(PerfEvent::CacheMisses),
25+
"instructions" => Some(PerfEvent::Instructions),
26+
_ => None,
27+
}
28+
}
29+
30+
pub fn all_events() -> Vec<PerfEvent> {
31+
vec![
32+
PerfEvent::CpuCycles,
33+
PerfEvent::CacheReferences,
34+
PerfEvent::CacheMisses,
35+
PerfEvent::Instructions,
36+
]
37+
}
38+
}
39+
40+
impl std::fmt::Display for PerfEvent {
41+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42+
write!(f, "{}", self.to_perf_string())
43+
}
44+
}

src/executor/wall_time/perf/perf_executable.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use runner_shared::perf_event::PerfEvent;
2+
13
use crate::prelude::*;
24
use std::path::Path;
35

@@ -67,15 +69,7 @@ pub fn get_working_perf_executable() -> Option<OsString> {
6769
/// Detects if the required perf events are available on this system.
6870
/// Returns the flags to pass to perf record command if they are available, otherwise returns None.
6971
pub fn get_event_flags(perf_executable: &OsString) -> anyhow::Result<Option<String>> {
70-
const CYCLES_EVENT_NAME: &str = "cycles";
71-
const CACHE_REFERENCES_EVENT_NAME: &str = "cache-references";
72-
const CACHE_MISSES_EVENT_NAME: &str = "cache-misses";
73-
74-
let perf_events = [
75-
CYCLES_EVENT_NAME,
76-
CACHE_REFERENCES_EVENT_NAME,
77-
CACHE_MISSES_EVENT_NAME,
78-
];
72+
let perf_events = PerfEvent::all_events();
7973

8074
let output = Command::new(perf_executable)
8175
.arg("list")
@@ -97,29 +91,28 @@ pub fn get_event_flags(perf_executable: &OsString) -> anyhow::Result<Option<Stri
9791
// cpu-cycles OR cycles [Hardware event]
9892
// instructions [Hardware event]
9993
// ref-cycles [Hardware event]
100-
let missing_events: Vec<&str> = perf_events
94+
let missing_events: Vec<PerfEvent> = perf_events
10195
.iter()
10296
.filter(|&&event| {
103-
!stdout
104-
.lines()
105-
.any(|line| line.split_whitespace().any(|word| word == event))
97+
!stdout.lines().any(|line| {
98+
line.split_whitespace()
99+
.any(|word| word == event.to_perf_string())
100+
})
106101
})
107102
.copied()
108103
.collect();
109104

110105
if !missing_events.is_empty() {
111106
warn!(
112107
"Not all required perf events available. Missing: [{}], using default events",
113-
missing_events.join(", ")
108+
missing_events.into_iter().join(", ")
114109
);
115110
return Ok(None);
116111
}
117112

118-
debug!(
119-
"All required perf events available: {}",
120-
perf_events.join(", ")
121-
);
122-
Ok(Some(format!("-e {{{}}}", perf_events.join(","))))
113+
let events_string = perf_events.into_iter().join(",");
114+
debug!("All required perf events available: {events_string}",);
115+
Ok(Some(format!("-e {{{events_string}}}")))
123116
}
124117

125118
pub fn get_compression_flags<S: AsRef<Path>>(perf_executable: S) -> Result<Option<String>> {

0 commit comments

Comments
 (0)