@@ -2,21 +2,37 @@ use anyhow::bail;
22use nix:: libc:: O_NONBLOCK ;
33use nix:: sys:: stat;
44use nix:: unistd:: { self , unlink} ;
5- use serde:: { Deserialize , Serialize } ;
65use std:: fs:: { File , OpenOptions } ;
7- use std:: io:: { Read , Write } ;
6+ use std:: io:: Read ;
87use std:: os:: unix:: fs:: OpenOptionsExt ;
98use std:: path:: { Path , PathBuf } ;
109
10+ // !!!!!!!!!!!!!!!!!!!!!!!!
11+ // !! DO NOT TOUCH BELOW !!
12+ // !!!!!!!!!!!!!!!!!!!!!!!!
13+ // Has to be in sync with `runner`.
14+ //
1115pub const RUNNER_CTL_FIFO : & str = "/tmp/runner.ctl.fifo" ;
1216pub 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+
4470pub 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) ]
185206mod tests {
186207 use super :: * ;
208+ use std:: io:: Write ;
187209
188210 #[ test]
189211 fn test_ipc_write_read ( ) {
0 commit comments