use detcore_model::fd::OpenFileId;
use serde::Deserialize;
use serde::Serialize;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
Serialize,
Deserialize
)]
pub enum ChannelId {
Pipe(OpenFileId),
Fifo(u64),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ChannelEnd {
pub channel: ChannelId,
pub reads: bool,
pub writes: bool,
}
impl ChannelEnd {
pub fn close_readies(&self) -> impl Iterator<Item = Wants> {
[(self.writes, Wants::Data), (self.reads, Wants::Space)]
.into_iter()
.filter_map(|(applies, wants)| applies.then_some(wants))
}
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
Serialize,
Deserialize
)]
pub enum Wants {
Data,
Space,
PeerWriter,
PeerReader,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct ReadyOutcome {
pub has_pending: bool,
pub starts_window: bool,
}
pub(crate) fn poll_of(
call: &reverie::syscalls::Syscall,
) -> Option<(std::os::unix::io::RawFd, Wants)> {
progress_of(call).map(|(fd, readies)| {
(
fd,
match readies {
Wants::Data => Wants::Space,
_ => Wants::Data,
},
)
})
}
pub(crate) fn progress_of(
call: &reverie::syscalls::Syscall,
) -> Option<(std::os::unix::io::RawFd, Wants)> {
use reverie::syscalls::Syscall;
match call {
Syscall::Write(write) => Some((write.fd(), Wants::Data)),
Syscall::Writev(writev) => Some((writev.fd(), Wants::Data)),
Syscall::Read(read) => Some((read.fd(), Wants::Space)),
Syscall::Readv(readv) => Some((readv.fd(), Wants::Space)),
_ => None,
}
}
impl<T: crate::record_or_replay::RecordOrReplay> crate::Detcore<T> {
pub(crate) fn pipe_channel_end<G: reverie::Guest<Self>>(
&self,
guest: &G,
fd: std::os::unix::io::RawFd,
) -> Option<ChannelEnd> {
self.cfg.pipe_wake_latency_in_effect()?;
guest
.thread_state()
.with_detfd(fd, |detfd| detfd.channel())
.ok()
.flatten()
}
pub(crate) async fn note_pipe_readied<G: reverie::Guest<Self>>(
&self,
guest: &mut G,
channel: ChannelId,
wants: Wants,
) {
let Some(latency) = self.cfg.pipe_wake_latency_in_effect() else {
return;
};
let outcome = crate::tool_global::ready_channel(guest, channel, wants).await;
if outcome.has_pending
&& (outcome.starts_window || guest.thread_state().pipe_handoff_due.is_none())
{
let due = guest.thread_state().thread_logical_time.as_nanos()
+ std::time::Duration::from_nanos(latency);
guest.thread_state_mut().pipe_handoff_due = Some(due);
}
}
pub(crate) async fn note_fd_readied<G: reverie::Guest<Self>>(
&self,
guest: &mut G,
fd: std::os::unix::io::RawFd,
wants: Wants,
) {
if let Some(end) = self.pipe_channel_end(guest, fd) {
self.note_pipe_readied(guest, end.channel, wants).await;
}
}
pub(crate) async fn pipe_hand_off_if_due<G: reverie::Guest<Self>>(&self, guest: &mut G) {
let Some(due) = guest.thread_state().pipe_handoff_due else {
return;
};
if guest.thread_state().own_signal_turn_hold.is_some()
|| guest.thread_state().thread_logical_time.as_nanos() < due
{
return;
}
guest.thread_state_mut().pipe_handoff_due = None;
if crate::tool_global::hand_off_turn(guest).await {
tracing::trace!(
"[dtid {}] handing the turn to the pollers it readied",
guest.thread_state().dettid
);
let request = Self::yield_request(guest);
crate::tool_global::resource_request(guest, request).await;
}
}
pub(crate) fn pipe_channel_ends_in<G: reverie::Guest<Self>>(
&self,
guest: &G,
first: u32,
last: u32,
) -> Vec<ChannelEnd> {
if self.cfg.pipe_wake_latency_in_effect().is_none() || first > last {
return Vec::new();
}
guest
.thread_state()
.open_fds()
.into_iter()
.filter(|fd| u32::try_from(*fd).is_ok_and(|fd| (first..=last).contains(&fd)))
.filter_map(|fd| self.pipe_channel_end(guest, fd))
.collect()
}
pub(crate) async fn note_channel_end_closed<G: reverie::Guest<Self>>(
&self,
guest: &mut G,
end: Option<ChannelEnd>,
) {
let Some(end) = end else {
return;
};
for wants in end.close_readies() {
self.note_pipe_readied(guest, end.channel, wants).await;
}
}
}