Skip to content

Commit 15c35a7

Browse files
committed
chore: send current benchmark to runner
1 parent 21faf86 commit 15c35a7

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

crates/codspeed/src/fifo.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,37 @@ use anyhow::bail;
22
use nix::libc::O_NONBLOCK;
33
use nix::sys::stat;
44
use nix::unistd::{self, unlink};
5-
use serde::{Deserialize, Serialize};
65
use std::fs::{File, OpenOptions};
7-
use std::io::{Read, Write};
6+
use std::io::Read;
87
use std::os::unix::fs::OpenOptionsExt;
98
use std::path::{Path, PathBuf};
109

10+
// !!!!!!!!!!!!!!!!!!!!!!!!
11+
// !! DO NOT TOUCH BELOW !!
12+
// !!!!!!!!!!!!!!!!!!!!!!!!
13+
// Has to be in sync with `runner`.
14+
//
1115
pub const RUNNER_CTL_FIFO: &str = "/tmp/runner.ctl.fifo";
1216
pub const RUNNER_ACK_FIFO: &str = "/tmp/runner.ack.fifo";
1317

14-
pub struct PerfGuard {
18+
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
19+
pub enum Command {
20+
CurrentBenchmark(String),
21+
StartBenchmark,
22+
StopBenchmark,
23+
Ack,
24+
}
25+
//
26+
// !!!!!!!!!!!!!!!!!!!!!!!!
27+
// !! DO NOT TOUCH ABOVE !!
28+
// !!!!!!!!!!!!!!!!!!!!!!!!
29+
30+
pub struct BenchGuard {
1531
ctl_fifo: FifoIpc,
1632
ack_fifo: FifoIpc,
1733
}
1834

19-
impl PerfGuard {
35+
impl BenchGuard {
2036
pub fn new(ctl_fifo: &str, ack_fifo: &str) -> anyhow::Result<Self> {
2137
let mut instance = Self {
2238
ctl_fifo: FifoIpc::connect(ctl_fifo)?.with_writer()?,
@@ -34,13 +50,23 @@ impl PerfGuard {
3450
}
3551
}
3652

37-
impl Drop for PerfGuard {
53+
impl Drop for BenchGuard {
3854
fn drop(&mut self) {
3955
self.send_cmd(Command::StopBenchmark)
4056
.expect("Failed to send stop command");
4157
}
4258
}
4359

60+
pub fn send_cmd(cmd: Command) -> anyhow::Result<()> {
61+
let mut writer = FifoIpc::connect(RUNNER_CTL_FIFO)?.with_writer()?;
62+
writer.send_cmd(cmd).unwrap();
63+
64+
let mut reader = FifoIpc::connect(RUNNER_ACK_FIFO)?.with_reader()?;
65+
reader.wait_for_ack();
66+
67+
Ok(())
68+
}
69+
4470
pub struct FifoIpc {
4571
path: PathBuf,
4672
reader: Option<File>,
@@ -129,6 +155,8 @@ impl FifoIpc {
129155
}
130156

131157
pub fn send_cmd(&mut self, cmd: Command) -> anyhow::Result<()> {
158+
use std::io::Write;
159+
132160
let encoded = bincode::serialize(&cmd)?;
133161
self.write_all(&(encoded.len() as u32).to_le_bytes())?;
134162
self.write_all(&encoded)?;
@@ -174,16 +202,10 @@ impl std::io::Read for FifoIpc {
174202
}
175203
}
176204

177-
#[derive(Serialize, Deserialize, Debug, PartialEq)]
178-
pub enum Command {
179-
StartBenchmark,
180-
StopBenchmark,
181-
Ack,
182-
}
183-
184205
#[cfg(test)]
185206
mod tests {
186207
use super::*;
208+
use std::io::Write;
187209

188210
#[test]
189211
fn test_ipc_write_read() {

crates/divan_compat/divan_fork/src/bench/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ impl<'a> BenchContext<'a> {
658658
let bench_overheads = timer.bench_overheads();
659659

660660
use codspeed::fifo::*;
661-
let _guard = PerfGuard::new(RUNNER_CTL_FIFO, RUNNER_ACK_FIFO);
661+
let _guard = BenchGuard::new(RUNNER_CTL_FIFO, RUNNER_ACK_FIFO);
662662
while {
663663
// Conditions for when sampling is over:
664664
if elapsed_picos >= max_picos {

crates/divan_compat/divan_fork/src/divan.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@ mod codspeed {
428428
bench_context.samples.time_samples.iter().map(|s| s.duration.picos / 1_000).collect();
429429
let max_time_ns = bench_context.options.max_time.map(|t| t.as_nanos());
430430

431+
if let Err(error) =
432+
::codspeed::fifo::send_cmd(codspeed::fifo::Command::CurrentBenchmark(uri.clone()))
433+
{
434+
eprintln!("Failed to send benchmark URI to runner: {}", error);
435+
}
436+
431437
::codspeed::walltime::collect_raw_walltime_results(
432438
"divan",
433439
bench_name,

0 commit comments

Comments
 (0)