Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,43 @@ jobs:
mode: walltime
token: ${{ secrets.CODSPEED_TOKEN }}


compat-integration-test-memory:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have multi-dimensional matrices, as well as some dimensions precising two things

We could combine everything in one big matrix, one dimension being the library we are testing, the other one the mode and the runner it runs on like here https://github.com/CodSpeedHQ/codspeed-cpp/blob/f23b68b27f8543f88f95ff0bdc46785733dee061/.github/workflows/ci.yml#L57-L62

WDYT ?

We could conditionnallly remove the .cargo/config.toml, as well as conditionnally skip the uploading part for memory (for now)

runs-on: ubuntu-latest
strategy:
matrix:
package:
- codspeed-divan-compat
- codspeed-divan-compat-examples
- codspeed-criterion-compat
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: moonrepo/setup-rust@v1
with:
cache-target: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- run: cargo install --path crates/cargo-codspeed --locked

- run: |
# Remove the cargo config else it forces instrumentation mode
rm -f .cargo/config.toml
cargo codspeed build -p ${{ matrix.package }} -m instrumentation

# TODO: Enable once we can upload it
# - name: Run the benchmarks
# uses: CodSpeedHQ/action@chore/runner-branch
# env:
# MY_ENV_VAR: "YES"
# with:
# runner-branch: cod-1670-runner-profile-memory-of-command
# run: cargo codspeed run
# mode: memory
# token: ${{ secrets.CODSPEED_TOKEN }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to remove secrets now with OIDC


musl-build-check:
strategy:
matrix:
Expand Down
4 changes: 3 additions & 1 deletion crates/cargo-codspeed/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ See `cargo codspeed build --help` for more information.");
];

// Add the codspeed cfg flag if simulation mode is enabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment, or create a function that takes measurement_mode and outputs a bool, named should_build_benchmark_target_to_run_only_once or something like this, to convey the intention

if measurement_mode == MeasurementMode::Simulation {
if measurement_mode == MeasurementMode::Simulation
|| measurement_mode == MeasurementMode::Analysis
{
flags.push("--cfg=codspeed".to_owned());
}

Expand Down
2 changes: 2 additions & 0 deletions crates/cargo-codspeed/src/measurement_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum MeasurementMode {
#[value(alias = "instrumentation")]
Simulation,
Walltime,
Analysis,
}

impl fmt::Display for MeasurementMode {
Expand All @@ -19,6 +20,7 @@ impl fmt::Display for MeasurementMode {
match self {
MeasurementMode::Simulation => "simulation",
MeasurementMode::Walltime => "walltime",
MeasurementMode::Analysis => "analysis",
}
)
}
Expand Down
9 changes: 7 additions & 2 deletions crates/codspeed/src/codspeed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::measurement;
use crate::{instrument_hooks::InstrumentHooks, measurement};
use colored::Colorize;
use std::ffi::CString;

Expand All @@ -19,7 +19,8 @@ pub struct CodSpeed {

impl CodSpeed {
pub fn new() -> Self {
let is_instrumented = measurement::is_instrumented();
let ih = InstrumentHooks::instance();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of abbreviations overall, but here it does not bring that much, how about hooks ? or hooks_instance or instrument_hooks_instance ?

let is_instrumented = ih.is_instrumented();
if !is_instrumented {
println!(
"{} codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment.",
Expand All @@ -46,12 +47,16 @@ impl CodSpeed {
#[inline(always)]
pub fn start_benchmark(&mut self, name: &str) {
self.current_benchmark = CString::new(name).expect("CString::new failed");
let _ = InstrumentHooks::instance().start_benchmark();
measurement::start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will that not confuse valgrind to keep both this and the start_benchmark call when built in simulation ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be the time to say goodbye to the inline assembly and make a major out of this

}

#[inline(always)]
pub fn end_benchmark(&mut self) {
measurement::stop(&self.current_benchmark);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark here

let _ = InstrumentHooks::instance().stop_benchmark();
let _ = InstrumentHooks::instance()
.set_executed_benchmark(&self.current_benchmark.to_string_lossy());
self.benchmarked
.push(self.current_benchmark.to_str().unwrap().to_string());
let action_str = if self.is_instrumented {
Expand Down
11 changes: 0 additions & 11 deletions crates/codspeed/src/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ use std::ffi::CString;

use crate::request::{send_client_request, ClientRequest, Value};

#[inline(always)]
pub fn is_instrumented() -> bool {
let valgrind_depth = unsafe {
send_client_request(
0,
&[ClientRequest::RunningOnValgrind as Value, 0, 0, 0, 0, 0],
)
};
valgrind_depth > 0
}

#[inline(always)]
pub fn set_metadata() {
let full_metadata = CString::new(format!(
Expand Down
1 change: 0 additions & 1 deletion crates/codspeed/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const CG_BASE: u32 = ((b'C' as u32) << 24) + ((b'T' as u32) << 16);
#[allow(non_camel_case_types)]
#[repr(u32)]
pub enum ClientRequest {
RunningOnValgrind = 0x1001,
ZeroStatistics = CG_BASE + 1,
DumpStatisticsAt = CG_BASE + 3,
StartInstrumentation = CG_BASE + 4,
Expand Down
4 changes: 4 additions & 0 deletions crates/divan_compat/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ harness = false
[[bench]]
name = "counters"
harness = false

[[bench]]
name = "alloc"
harness = false
40 changes: 40 additions & 0 deletions crates/divan_compat/examples/benches/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{
alloc::Layout,
collections::{HashMap, HashSet},
};

#[divan::bench]
fn allocate() {
println!("Hello, world!");

let vec = vec![1, 2, 3];
println!("{vec:?}");

let mut map = HashMap::new();
map.insert("key", "value");
println!("{map:?}");

let mut set = HashSet::new();
set.insert("apple");
set.insert("banana");
println!("{set:?}");

std::thread::sleep(std::time::Duration::from_secs(1));

let mut bytes_vec = vec![0u8; 0x100];
println!("{:?}", bytes_vec.len());

bytes_vec.extend(&vec![0u8; 0x1000]);

// Alloc 42 bytes of memory per iteration (4200 bytes total)
for _ in 0..100 {
let layout = Layout::new::<[u8; 42]>();
let memory = unsafe { std::alloc::alloc(layout) };
core::hint::black_box(memory);
unsafe { std::alloc::dealloc(memory, layout) };
}
}

fn main() {
divan::main();
}
Loading