| 1 |
use crate::measurement; |
| 2 |
use colored::Colorize; |
| 3 |
use std::ffi::CString; |
| 4 |
|
| 5 |
pub use std::hint::black_box; |
| 6 |
|
| 7 |
pub const WARMUP_RUNS: u32 = 5; |
| 8 |
|
| 9 |
pub fn display_native_harness() { |
| 10 |
eprintln!("Harness: codspeed v{}", env!("CARGO_PKG_VERSION"),); |
| 11 |
} |
| 12 |
|
| 13 |
#[derive(PartialEq)] |
| 14 |
pub enum InstrumentationStatus { |
| 15 |
/// Instrumentation detected via inline assembly directly |
| 16 |
Valgrind, |
| 17 |
/// Instrumentation detected via InstrumentHooks |
| 18 |
InstrumentHooks(&'static crate::instrument_hooks::InstrumentHooks), |
| 19 |
NotInstrumented, |
| 20 |
} |
| 21 |
|
| 22 |
impl InstrumentationStatus { |
| 23 |
pub fn is_instrumented(&self) -> bool { |
| 24 |
*self != InstrumentationStatus::NotInstrumented |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
pub struct CodSpeed { |
| 29 |
benchmarked: Vec<String>, |
| 30 |
current_benchmark: CString, |
| 31 |
group_stack: Vec<String>, |
| 32 |
instrumentation_status: InstrumentationStatus, |
| 33 |
} |
| 34 |
|
| 35 |
impl CodSpeed { |
| 36 |
pub fn new() -> Self { |
| 37 |
use crate::instrument_hooks::InstrumentHooks; |
| 38 |
let instrumentation_status = { |
| 39 |
// Always initialize InstrumentHooks for environment collection, |
| 40 |
// even when using Valgrind for the actual measurements. |
| 41 |
let hooks_instance = InstrumentHooks::instance(); |
| 42 |
|
| 43 |
// We bypass InstrumentHooks if we detect Valgrind via inline assembly |
| 44 |
// Until we can reliably get rid of the inline assembly without causing breaking |
| 45 |
// changes in CPU simulation measurements by switching to InstrumentHooks only, we need |
| 46 |
// to keep this separation. |
| 47 |
if measurement::is_instrumented() { |
| 48 |
InstrumentationStatus::Valgrind |
| 49 |
} else if hooks_instance.is_instrumented() { |
| 50 |
InstrumentationStatus::InstrumentHooks(hooks_instance) |
| 51 |
} else { |
| 52 |
InstrumentationStatus::NotInstrumented |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
if !instrumentation_status.is_instrumented() { |
| 57 |
eprintln!( |
| 58 |
"{} codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment.", |
| 59 |
"NOTICE:".to_string().bold() |
| 60 |
); |
| 61 |
}; |
| 62 |
|
| 63 |
measurement::set_metadata(); |
| 64 |
|
| 65 |
Self { |
| 66 |
benchmarked: Vec::new(), |
| 67 |
current_benchmark: CString::new("").expect("CString::new failed"), |
| 68 |
group_stack: Vec::new(), |
| 69 |
instrumentation_status, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
pub fn push_group(&mut self, group: &str) { |
| 74 |
self.group_stack.push(group.to_string()); |
| 75 |
} |
| 76 |
|
| 77 |
pub fn pop_group(&mut self) { |
| 78 |
self.group_stack.pop(); |
| 79 |
} |
| 80 |
|
| 81 |
#[inline(always)] |
| 82 |
pub fn start_benchmark(&mut self, name: &str) { |
| 83 |
self.current_benchmark = CString::new(name).expect("CString::new failed"); |
| 84 |
|
| 85 |
if let InstrumentationStatus::InstrumentHooks(hooks_instance) = &self.instrumentation_status |
| 86 |
{ |
| 87 |
let _ = hooks_instance.start_benchmark(); |
| 88 |
} |
| 89 |
|
| 90 |
// We intentionally do this no matter the instrumentation status. |
| 91 |
// The overhead in case we are not running valgrind is extremely low, and we do not want to |
| 92 |
// add a conditionnal branch to valgrind measurements. |
| 93 |
measurement::start(); |
| 94 |
} |
| 95 |
|
| 96 |
#[inline(always)] |
| 97 |
pub fn end_benchmark(&mut self) { |
| 98 |
// We intentionally do this no matter the instrumentation status. |
| 99 |
// The overhead in case we are not running valgrind is extremely low, and we do not want to |
| 100 |
// add a conditionnal branch to valgrind measurements. |
| 101 |
measurement::stop(&self.current_benchmark); |
| 102 |
if let InstrumentationStatus::InstrumentHooks(hooks_instance) = &self.instrumentation_status |
| 103 |
{ |
| 104 |
let _ = hooks_instance.stop_benchmark(); |
| 105 |
let _ = |
| 106 |
hooks_instance.set_executed_benchmark(&self.current_benchmark.to_string_lossy()); |
| 107 |
} |
| 108 |
self.benchmarked |
| 109 |
.push(self.current_benchmark.to_str().unwrap().to_string()); |
| 110 |
|
| 111 |
let action_str = if self.instrumentation_status.is_instrumented() { |
| 112 |
"Measured" |
| 113 |
} else { |
| 114 |
"Checked" |
| 115 |
}; |
| 116 |
|
| 117 |
let group_str = if self.group_stack.is_empty() { |
| 118 |
"".to_string() |
| 119 |
} else { |
| 120 |
format!(" (group: {})", self.group_stack.join("/")) |
| 121 |
}; |
| 122 |
println!( |
| 123 |
"{}: {}{}", |
| 124 |
action_str, |
| 125 |
self.current_benchmark.to_string_lossy(), |
| 126 |
group_str |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
impl Default for CodSpeed { |
| 132 |
fn default() -> Self { |
| 133 |
Self::new() |
| 134 |
} |
| 135 |
} |