-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfifo.rs
More file actions
133 lines (104 loc) · 3.68 KB
/
fifo.rs
File metadata and controls
133 lines (104 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use super::{FifoCommand, RUNNER_ACK_FIFO, RUNNER_CTL_FIFO};
use std::path::PathBuf;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::unix::pipe::OpenOptions as TokioPipeOpenOptions;
use tokio::net::unix::pipe::Receiver as TokioPipeReader;
use tokio::net::unix::pipe::Sender as TokioPipeSender;
fn create_fifo<P: AsRef<std::path::Path>>(path: P) -> anyhow::Result<()> {
// Remove the previous FIFO (if it exists)
let _ = nix::unistd::unlink(path.as_ref());
// Create the FIFO with RWX permissions for the owner
nix::unistd::mkfifo(path.as_ref(), nix::sys::stat::Mode::S_IRWXU)?;
Ok(())
}
pub struct RunnerFifo {
ack_fifo: TokioPipeSender,
ctl_fifo: TokioPipeReader,
}
fn get_pipe_open_options() -> TokioPipeOpenOptions {
#[cfg_attr(not(target_os = "linux"), allow(unused_mut))]
let mut options = TokioPipeOpenOptions::new();
#[cfg(target_os = "linux")]
options.read_write(true);
options
}
impl RunnerFifo {
pub fn new() -> anyhow::Result<Self> {
create_fifo(RUNNER_CTL_FIFO)?;
create_fifo(RUNNER_ACK_FIFO)?;
let ack_fifo = get_pipe_open_options().open_sender(RUNNER_ACK_FIFO)?;
let ctl_fifo = get_pipe_open_options().open_receiver(RUNNER_CTL_FIFO)?;
Ok(Self { ctl_fifo, ack_fifo })
}
pub async fn recv_cmd(&mut self) -> anyhow::Result<FifoCommand> {
let mut len_buffer = [0u8; 4];
self.ctl_fifo.read_exact(&mut len_buffer).await?;
let message_len = u32::from_le_bytes(len_buffer) as usize;
let mut buffer = vec![0u8; message_len];
loop {
if self.ctl_fifo.read_exact(&mut buffer).await.is_ok() {
break;
}
}
let decoded = bincode::deserialize(&buffer)?;
Ok(decoded)
}
pub async fn send_cmd(&mut self, cmd: FifoCommand) -> anyhow::Result<()> {
let encoded = bincode::serialize(&cmd)?;
self.ack_fifo
.write_all(&(encoded.len() as u32).to_le_bytes())
.await?;
self.ack_fifo.write_all(&encoded).await?;
Ok(())
}
}
pub struct PerfFifo {
ctl_fifo: TokioPipeSender,
ack_fifo: TokioPipeReader,
pub(crate) ctl_fifo_path: PathBuf,
pub(crate) ack_fifo_path: PathBuf,
}
impl PerfFifo {
pub fn new() -> anyhow::Result<Self> {
let fifo_dir = tempfile::tempdir()?.into_path();
let ctl_fifo_path = fifo_dir.join("codspeed_perf.ctl.fifo");
let ack_fifo_path = fifo_dir.join("codspeed_perf.ack.fifo");
create_fifo(&ctl_fifo_path)?;
create_fifo(&ack_fifo_path)?;
let ack_fifo = get_pipe_open_options().open_receiver(&ack_fifo_path)?;
let ctl_fifo = get_pipe_open_options().open_sender(&ctl_fifo_path)?;
Ok(Self {
ctl_fifo,
ack_fifo,
ctl_fifo_path,
ack_fifo_path,
})
}
pub async fn start_events(&mut self) -> anyhow::Result<()> {
self.ctl_fifo.write_all(b"enable\n\0").await?;
self.wait_for_ack().await;
Ok(())
}
pub async fn stop_events(&mut self) -> anyhow::Result<()> {
self.ctl_fifo.write_all(b"disable\n\0").await?;
self.wait_for_ack().await;
Ok(())
}
pub async fn ping(&mut self) -> anyhow::Result<()> {
self.ctl_fifo.write_all(b"ping\n\0").await?;
self.wait_for_ack().await;
Ok(())
}
async fn wait_for_ack(&mut self) {
const ACK: &[u8] = b"ack\n\0";
loop {
let mut buf: [u8; ACK.len()] = [0; ACK.len()];
if self.ack_fifo.read_exact(&mut buf).await.is_err() {
continue;
}
if buf == ACK {
break;
}
}
}
}