From 48c9b431713bb0c4badaecdf91b3365fe671163e Mon Sep 17 00:00:00 2001 From: mwojcik Date: Fri, 13 May 2022 11:35:52 +0800 Subject: [PATCH 1/5] moninj: make it use async drtioaux --- src/runtime/src/moninj.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/runtime/src/moninj.rs b/src/runtime/src/moninj.rs index a95777d..78118a0 100644 --- a/src/runtime/src/moninj.rs +++ b/src/runtime/src/moninj.rs @@ -58,44 +58,44 @@ enum DeviceMessage { #[cfg(has_drtio)] mod remote_moninj { use super::*; - use libboard_artiq::drtioaux; + use libboard_artiq::drtioaux_async; use crate::rtio_mgt::drtio; use log::error; - pub fn read_probe(aux_mutex: &Rc>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, probe: i8) -> i64 { - let reply = task::block_on(drtio::aux_transact(aux_mutex, linkno, &drtioaux::Packet::MonitorRequest { + pub async fn read_probe(aux_mutex: &Rc>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, probe: i8) -> i64 { + let reply = drtio::aux_transact(aux_mutex, linkno, &drtioaux_async::Packet::MonitorRequest { destination: destination, channel: channel as _, probe: probe as _}, - timer)); + timer).await; match reply { - Ok(drtioaux::Packet::MonitorReply { value }) => return value as i64, + Ok(drtioaux_async::Packet::MonitorReply { value }) => return value as i64, Ok(packet) => error!("received unexpected aux packet: {:?}", packet), Err(e) => error!("aux packet error ({})", e) } 0 } - pub fn inject(aux_mutex: &Rc>, _timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8, value: i8) { + pub async fn inject(aux_mutex: &Rc>, _timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8, value: i8) { let _lock = aux_mutex.lock(); - drtioaux::send(linkno, &drtioaux::Packet::InjectionRequest { + drtioaux_async::send(linkno, &drtioaux_async::Packet::InjectionRequest { destination: destination, channel: channel as _, overrd: overrd as _, value: value as _ - }).unwrap(); + }).await.unwrap(); } - pub fn read_injection_status(aux_mutex: &Rc>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8) -> i8 { - let reply = task::block_on(drtio::aux_transact(aux_mutex, + pub async fn read_injection_status(aux_mutex: &Rc>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8) -> i8 { + let reply = drtio::aux_transact(aux_mutex, linkno, - &drtioaux::Packet::InjectionStatusRequest { + &drtioaux_async::Packet::InjectionStatusRequest { destination: destination, channel: channel as _, overrd: overrd as _}, - timer)); + timer).await; match reply { - Ok(drtioaux::Packet::InjectionStatusReply { value }) => return value as i8, + Ok(drtioaux_async::Packet::InjectionStatusReply { value }) => return value as i8, Ok(packet) => error!("received unexpected aux packet: {:?}", packet), Err(e) => error!("aux packet error ({})", e) } @@ -142,7 +142,7 @@ macro_rules! dispatch { local_moninj::$func(channel.into(), $($param, )*) } else { let linkno = hop - 1 as u8; - remote_moninj::$func($aux_mutex, $timer, linkno, destination, channel, $($param, )*) + remote_moninj::$func($aux_mutex, $timer, linkno, destination, channel, $($param, )*).await } }} } @@ -163,7 +163,7 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer, let mut probe_watch_list: BTreeMap<(i32, i8), Option> = BTreeMap::new(); let mut inject_watch_list: BTreeMap<(i32, i8), Option> = BTreeMap::new(); - let mut next_check = Milliseconds(0); + let mut next_check = timer.get_time(); loop { // TODO: we don't need fuse() here. // remove after https://github.com/rust-lang/futures-rs/issues/1989 lands -- 2.42.0 From 24df52268e13f2ca1ed9756bfe7c57faa7c71c98 Mon Sep 17 00:00:00 2001 From: mwojcik Date: Fri, 20 May 2022 12:56:00 +0800 Subject: [PATCH 2/5] moninj: restructure timeout stop logging errors if satellite is unavailable drtio: don't even send message if link is down --- src/runtime/src/moninj.rs | 22 +++++++++++++--------- src/runtime/src/rtio_mgt.rs | 3 +++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/runtime/src/moninj.rs b/src/runtime/src/moninj.rs index 78118a0..0a92b4b 100644 --- a/src/runtime/src/moninj.rs +++ b/src/runtime/src/moninj.rs @@ -21,6 +21,7 @@ pub enum Error { NetworkError(smoltcp::Error), UnexpectedPattern, UnrecognizedPacket, + } pub type Result = core::result::Result; @@ -71,6 +72,7 @@ mod remote_moninj { match reply { Ok(drtioaux_async::Packet::MonitorReply { value }) => return value as i64, Ok(packet) => error!("received unexpected aux packet: {:?}", packet), + Err("link went down") => {}, Err(e) => error!("aux packet error ({})", e) } 0 @@ -97,6 +99,7 @@ mod remote_moninj { match reply { Ok(drtioaux_async::Packet::InjectionStatusReply { value }) => return value as i8, Ok(packet) => error!("received unexpected aux packet: {:?}", packet), + Err("link went down") => {}, Err(e) => error!("aux packet error ({})", e) } 0 @@ -164,19 +167,20 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer, let mut probe_watch_list: BTreeMap<(i32, i8), Option> = BTreeMap::new(); let mut inject_watch_list: BTreeMap<(i32, i8), Option> = BTreeMap::new(); let mut next_check = timer.get_time(); + let timeout = |next_check: Milliseconds| -> nb::Result<(), Void> { + if timer.get_time() < next_check { + Err(nb::Error::WouldBlock) + } else { + Ok(()) + } + }; loop { // TODO: we don't need fuse() here. // remove after https://github.com/rust-lang/futures-rs/issues/1989 lands let read_message_f = read_i8(&stream).fuse(); let next_check_c = next_check.clone(); - let timeout = || -> nb::Result<(), Void> { - if timer.get_time() < next_check_c { - Err(nb::Error::WouldBlock) - } else { - Ok(()) - } - }; - let timeout_f = block_async!(timeout()).fuse(); + + let timeout_f = block_async!(timeout(next_check_c)).fuse(); pin_mut!(read_message_f, timeout_f); select_biased! { message = read_message_f => { @@ -246,7 +250,7 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer, *previous = Some(current); } } - next_check = next_check + Milliseconds(200); + next_check = timer.get_time() + Milliseconds(200); } } } diff --git a/src/runtime/src/rtio_mgt.rs b/src/runtime/src/rtio_mgt.rs index d72a902..17f8679 100644 --- a/src/runtime/src/rtio_mgt.rs +++ b/src/runtime/src/rtio_mgt.rs @@ -49,6 +49,9 @@ pub mod drtio { pub async fn aux_transact(aux_mutex: &Mutex, linkno: u8, request: &Packet, timer: GlobalTimer) -> Result { + if !link_rx_up(linkno).await { + return Err("link went down"); + } let _lock = aux_mutex.lock(); drtioaux_async::send(linkno, request).await.unwrap(); recv_aux_timeout(linkno, 200, timer).await -- 2.42.0 From 4f457d9c24b65f96f7d7137222fa27be97361f25 Mon Sep 17 00:00:00 2001 From: mwojcik Date: Fri, 20 May 2022 17:32:43 +0800 Subject: [PATCH 3/5] moninj: log link down at debug level --- src/runtime/src/moninj.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/runtime/src/moninj.rs b/src/runtime/src/moninj.rs index 0a92b4b..5b13606 100644 --- a/src/runtime/src/moninj.rs +++ b/src/runtime/src/moninj.rs @@ -20,8 +20,7 @@ use crate::proto_async::*; pub enum Error { NetworkError(smoltcp::Error), UnexpectedPattern, - UnrecognizedPacket, - + UnrecognizedPacket } pub type Result = core::result::Result; @@ -72,7 +71,7 @@ mod remote_moninj { match reply { Ok(drtioaux_async::Packet::MonitorReply { value }) => return value as i64, Ok(packet) => error!("received unexpected aux packet: {:?}", packet), - Err("link went down") => {}, + Err("link went down") => { debug!("link is down"); }, Err(e) => error!("aux packet error ({})", e) } 0 @@ -99,7 +98,7 @@ mod remote_moninj { match reply { Ok(drtioaux_async::Packet::InjectionStatusReply { value }) => return value as i8, Ok(packet) => error!("received unexpected aux packet: {:?}", packet), - Err("link went down") => {}, + Err("link went down") => { debug!("link is down"); }, Err(e) => error!("aux packet error ({})", e) } 0 -- 2.42.0 From 596edb480ce3eba8d081915d6998466b5504429b Mon Sep 17 00:00:00 2001 From: mwojcik Date: Wed, 25 May 2022 10:28:57 +0800 Subject: [PATCH 4/5] cargo: update zynq-rs --- flake.nix | 2 +- src/Cargo.lock | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index 52ba789..2107881 100644 --- a/flake.nix +++ b/flake.nix @@ -134,7 +134,7 @@ cargoLock = { lockFile = src/Cargo.lock; outputHashes = { - "libasync-0.0.0" = "sha256-7qRHEHg+CXqqZSLgV4j9XLrLj6mlaeXzCZ8eFkRa0U8="; + "libasync-0.0.0" = "sha256-xuwesRrQiccopPTCkwGqQxld74X74q7EVsKIrE0zirc="; }; }; diff --git a/src/Cargo.lock b/src/Cargo.lock index 2b11062..88db5db 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -219,7 +219,7 @@ dependencies = [ [[package]] name = "libasync" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935" dependencies = [ "embedded-hal", "libcortex_a9", @@ -251,7 +251,7 @@ dependencies = [ [[package]] name = "libboard_zynq" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935" dependencies = [ "bit_field", "embedded-hal", @@ -276,7 +276,7 @@ dependencies = [ [[package]] name = "libconfig" version = "0.1.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935" dependencies = [ "core_io", "fatfs", @@ -287,7 +287,7 @@ dependencies = [ [[package]] name = "libcortex_a9" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935" dependencies = [ "bit_field", "libregister", @@ -303,7 +303,7 @@ checksum = "33a33a362ce288760ec6a508b94caaec573ae7d3bbbd91b87aa0bad4456839db" [[package]] name = "libregister" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935" dependencies = [ "bit_field", "vcell", @@ -313,7 +313,7 @@ dependencies = [ [[package]] name = "libsupport_zynq" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935" dependencies = [ "cc", "compiler_builtins", -- 2.42.0 From 7a8f96dbd91796e90fbf52f354254b75d3e4ce3c Mon Sep 17 00:00:00 2001 From: mwojcik Date: Wed, 25 May 2022 10:36:38 +0800 Subject: [PATCH 5/5] rtio_mgt: use mutex's async_lock --- src/runtime/src/rtio_mgt.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/src/rtio_mgt.rs b/src/runtime/src/rtio_mgt.rs index 17f8679..a77f2e0 100644 --- a/src/runtime/src/rtio_mgt.rs +++ b/src/runtime/src/rtio_mgt.rs @@ -52,7 +52,7 @@ pub mod drtio { if !link_rx_up(linkno).await { return Err("link went down"); } - let _lock = aux_mutex.lock(); + let _lock = aux_mutex.async_lock().await; drtioaux_async::send(linkno, request).await.unwrap(); recv_aux_timeout(linkno, 200, timer).await } @@ -91,7 +91,7 @@ pub mod drtio { } async fn sync_tsc(aux_mutex: &Rc>, linkno: u8, timer: GlobalTimer) -> Result<(), &'static str> { - let _lock = aux_mutex.lock(); + let _lock = aux_mutex.async_lock().await; unsafe { (csr::DRTIO[linkno as usize].set_time_write)(1); @@ -145,7 +145,7 @@ pub mod drtio { } async fn process_unsolicited_aux(aux_mutex: &Rc>, linkno: u8) { - let _lock = aux_mutex.lock(); + let _lock = aux_mutex.async_lock().await; match drtioaux_async::recv(linkno).await { Ok(Some(packet)) => warn!("[LINK#{}] unsolicited aux packet: {:?}", linkno, packet), Ok(None) => (), -- 2.42.0