1
0
Fork 0
forked from wry/wry

io: use io_uring for all io

There should no longer be any

- read
- write
- connect
- sendmsg
- recvmsg
- accept

calls in the codebase. Previously we were using a mix of io_uring and
these calls which had some negative effects: Since we were using the old
system calls, we had to set the file descriptors to non-blocking. But
our io_uring code did not handle EAGAIN. This lead to programs sometimes
being killed when the wayland IO was actually blocking.

Now all file descriptors are set to blocking, but io_uring makes it
non-blocking from our perspective. The one exception are evdev files
because they are read via libinput and libinput uses the old system
calls.
This commit is contained in:
Julian Orth 2022-12-31 17:55:58 +01:00
parent 2db0ee8995
commit 9812a02f87
55 changed files with 900 additions and 672 deletions

View file

@ -1,7 +1,7 @@
use {
crate::{
async_engine::{AsyncEngine, SpawnedFuture},
io_uring::IoUring,
io_uring::{IoUring, IoUringError},
pipewire::{
pw_formatter::{format, PwFormatter},
pw_ifs::{
@ -43,7 +43,7 @@ pub enum PwConError {
#[error("Could not create a unix socket")]
CreateSocket(#[source] OsError),
#[error("Could not connect to the pipewire daemon")]
ConnectSocket(#[source] OsError),
ConnectSocket(#[source] IoUringError),
#[error(transparent)]
BufIoError(#[from] BufIoError),
#[error("Server did not sent a required fd")]
@ -194,7 +194,10 @@ impl PwCon {
log::trace!("{:#?}", parser.read_pod().unwrap());
}
}
self.io.send(BufIoMessage { fds, buf });
self.io.send(BufIoMessage {
fds,
buf: buf.unwrap(),
});
}
#[allow(dead_code)]
@ -295,13 +298,8 @@ impl Drop for PwConHolder {
}
impl PwConHolder {
#[allow(dead_code)]
pub fn new(eng: &Rc<AsyncEngine>, ring: &Rc<IoUring>) -> Result<Rc<Self>, PwConError> {
let fd = match uapi::socket(
c::AF_UNIX,
c::SOCK_STREAM | c::SOCK_CLOEXEC | c::SOCK_NONBLOCK,
0,
) {
pub async fn new(eng: &Rc<AsyncEngine>, ring: &Rc<IoUring>) -> Result<Rc<Self>, PwConError> {
let fd = match uapi::socket(c::AF_UNIX, c::SOCK_STREAM | c::SOCK_CLOEXEC, 0) {
Ok(fd) => Rc::new(fd),
Err(e) => return Err(PwConError::CreateSocket(e.into())),
};
@ -317,8 +315,8 @@ impl PwConHolder {
let mut path = uapi::as_bytes_mut(&mut addr.sun_path[..]);
let _ = write!(path, "{}/pipewire-0", xrd);
}
if let Err(e) = uapi::connect(fd.raw(), &addr) {
return Err(PwConError::ConnectSocket(e.into()));
if let Err(e) = ring.connect(&fd, &addr).await {
return Err(PwConError::ConnectSocket(e));
}
let io = Rc::new(BufIo::new(&fd, ring));
let data = Rc::new(PwCon {

View file

@ -1,16 +1,19 @@
use {
crate::pipewire::pw_pod::{
PW_TYPE_Array, PW_TYPE_Bitmap, PW_TYPE_Bool, PW_TYPE_Bytes, PW_TYPE_Choice, PW_TYPE_Double,
PW_TYPE_Fd, PW_TYPE_Float, PW_TYPE_Fraction, PW_TYPE_Id, PW_TYPE_Int, PW_TYPE_Long,
PW_TYPE_None, PW_TYPE_Object, PW_TYPE_Rectangle, PW_TYPE_String, PW_TYPE_Struct,
PwChoiceType, PwPodObjectType, PwPodType, PwPropFlag,
crate::{
pipewire::pw_pod::{
PW_TYPE_Array, PW_TYPE_Bitmap, PW_TYPE_Bool, PW_TYPE_Bytes, PW_TYPE_Choice,
PW_TYPE_Double, PW_TYPE_Fd, PW_TYPE_Float, PW_TYPE_Fraction, PW_TYPE_Id, PW_TYPE_Int,
PW_TYPE_Long, PW_TYPE_None, PW_TYPE_Object, PW_TYPE_Rectangle, PW_TYPE_String,
PW_TYPE_Struct, PwChoiceType, PwPodObjectType, PwPodType, PwPropFlag,
},
utils::buf::DynamicBuf,
},
std::rc::Rc,
uapi::OwnedFd,
};
pub struct PwFormatter<'a> {
data: &'a mut Vec<u8>,
data: &'a mut DynamicBuf,
fds: &'a mut Vec<Rc<OwnedFd>>,
array: bool,
first: bool,
@ -260,7 +263,7 @@ impl<'a> PwFormatter<'a> {
}
pub struct PwObjectFormatter<'a> {
data: &'a mut Vec<u8>,
data: &'a mut DynamicBuf,
fds: &'a mut Vec<Rc<OwnedFd>>,
}
@ -281,8 +284,14 @@ impl<'a> PwObjectFormatter<'a> {
}
}
pub fn format<F>(buf: &mut Vec<u8>, fds: &mut Vec<Rc<OwnedFd>>, id: u32, opcode: u8, seq: u32, f: F)
where
pub fn format<F>(
buf: &mut DynamicBuf,
fds: &mut Vec<Rc<OwnedFd>>,
id: u32,
opcode: u8,
seq: u32,
f: F,
) where
F: FnOnce(&mut PwFormatter),
{
buf.clear();

View file

@ -30,7 +30,7 @@ use {
},
},
utils::{
bitfield::Bitfield, bitflags::BitflagsExt, clonecell::CloneCell,
bitfield::Bitfield, bitflags::BitflagsExt, buf::TypedBuf, clonecell::CloneCell,
copyhashmap::CopyHashMap, errorfmt::ErrorFmt,
},
video::dmabuf::DmaBuf,
@ -798,17 +798,11 @@ impl PwClientNode {
_activation: Rc<PwMemTyped<pw_node_activation>>,
fd: Rc<OwnedFd>,
) {
let mut buf = TypedBuf::<u64>::new();
loop {
// unsafe {
// log::info!("transport = {:#?}", activation.read());
// }
if let Err(e) = self.con.ring.readable(&fd).await {
log::error!(
"Could not wait for transport to become readable: {}",
ErrorFmt(e)
);
return;
}
// log::info!("transport in");
// for port in self.ports.lock().values() {
// for io in port.io_buffers.lock().values() {
@ -820,11 +814,11 @@ impl PwClientNode {
// unsafe {
// log::info!("state = {:#?}", activation.read().state[0]);
// }
let mut n = 0u64;
if let Err(e) = uapi::read(fd.raw(), &mut n) {
if let Err(e) = self.con.ring.read(&fd, buf.buf()).await {
log::error!("Could not read from eventfd: {}", ErrorFmt(e));
return;
}
let n = buf.t();
if n > 1 {
log::warn!("Missed {} transport changes", n - 1);
}