feat: On RedoxOS, use epoll instead of the poll backend

Technically RedoxOS supports the poll syscall, so we already support
RedoxOS. However, this is very slow. This commit ports this code to
epoll, which should be more efficient.

Closes #176
This commit is contained in:
John Nunley 2024-02-11 08:31:13 -08:00 committed by GitHub
parent ac7fbcae31
commit 77b4ed1156
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 23 deletions

View File

@ -117,6 +117,10 @@ jobs:
run: | run: |
rustup target add x86_64-unknown-illumos rustup target add x86_64-unknown-illumos
cargo build --target x86_64-unknown-illumos cargo build --target x86_64-unknown-illumos
- name: Redox
run: |
rustup target add x86_64-unknown-redox
cargo check --target x86_64-unknown-redox
wine: wine:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04

View File

@ -22,7 +22,7 @@ cfg-if = "1"
tracing = { version = "0.1.37", default-features = false } tracing = { version = "0.1.37", default-features = false }
[target.'cfg(any(unix, target_os = "fuchsia", target_os = "vxworks"))'.dependencies] [target.'cfg(any(unix, target_os = "fuchsia", target_os = "vxworks"))'.dependencies]
rustix = { version = "0.38.8", features = ["event", "fs", "pipe", "process", "std", "time"], default-features = false } rustix = { version = "0.38.31", features = ["event", "fs", "pipe", "process", "std", "time"], default-features = false }
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
concurrent-queue = "2.2.0" concurrent-queue = "2.2.0"

View File

@ -4,16 +4,20 @@ use std::io;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::time::Duration; use std::time::Duration;
use rustix::event::{epoll, eventfd, EventfdFlags}; #[cfg(not(target_os = "redox"))]
use rustix::fd::OwnedFd; use rustix::event::{eventfd, EventfdFlags};
use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags}; #[cfg(not(target_os = "redox"))]
use rustix::io::{fcntl_getfd, fcntl_setfd, read, write, FdFlags};
use rustix::pipe::{pipe, pipe_with, PipeFlags};
use rustix::time::{ use rustix::time::{
timerfd_create, timerfd_settime, Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags, timerfd_create, timerfd_settime, Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags,
Timespec, Timespec,
}; };
use rustix::event::epoll;
use rustix::fd::OwnedFd;
use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags};
use rustix::io::{fcntl_getfd, fcntl_setfd, read, write, FdFlags};
use rustix::pipe::{pipe, pipe_with, PipeFlags};
use crate::{Event, PollMode}; use crate::{Event, PollMode};
/// Interface to epoll. /// Interface to epoll.
@ -26,6 +30,9 @@ pub struct Poller {
notifier: Notifier, notifier: Notifier,
/// File descriptor for the timerfd that produces timeouts. /// File descriptor for the timerfd that produces timeouts.
///
/// Redox does not support timerfd.
#[cfg(not(target_os = "redox"))]
timer_fd: Option<OwnedFd>, timer_fd: Option<OwnedFd>,
} }
@ -39,6 +46,7 @@ impl Poller {
// Set up notifier and timerfd. // Set up notifier and timerfd.
let notifier = Notifier::new()?; let notifier = Notifier::new()?;
#[cfg(not(target_os = "redox"))]
let timer_fd = timerfd_create( let timer_fd = timerfd_create(
TimerfdClockId::Monotonic, TimerfdClockId::Monotonic,
TimerfdFlags::CLOEXEC | TimerfdFlags::NONBLOCK, TimerfdFlags::CLOEXEC | TimerfdFlags::NONBLOCK,
@ -48,10 +56,12 @@ impl Poller {
let poller = Poller { let poller = Poller {
epoll_fd, epoll_fd,
notifier, notifier,
#[cfg(not(target_os = "redox"))]
timer_fd, timer_fd,
}; };
unsafe { unsafe {
#[cfg(not(target_os = "redox"))]
if let Some(ref timer_fd) = poller.timer_fd { if let Some(ref timer_fd) = poller.timer_fd {
poller.add( poller.add(
timer_fd.as_raw_fd(), timer_fd.as_raw_fd(),
@ -70,7 +80,6 @@ impl Poller {
tracing::trace!( tracing::trace!(
epoll_fd = ?poller.epoll_fd.as_raw_fd(), epoll_fd = ?poller.epoll_fd.as_raw_fd(),
notifier = ?poller.notifier, notifier = ?poller.notifier,
timer_fd = ?poller.timer_fd,
"new", "new",
); );
Ok(poller) Ok(poller)
@ -155,6 +164,7 @@ impl Poller {
); );
let _enter = span.enter(); let _enter = span.enter();
#[cfg(not(target_os = "redox"))]
if let Some(ref timer_fd) = self.timer_fd { if let Some(ref timer_fd) = self.timer_fd {
// Configure the timeout using timerfd. // Configure the timeout using timerfd.
let new_val = Itimerspec { let new_val = Itimerspec {
@ -181,8 +191,13 @@ impl Poller {
)?; )?;
} }
#[cfg(not(target_os = "redox"))]
let timer_fd = &self.timer_fd;
#[cfg(target_os = "redox")]
let timer_fd: Option<core::convert::Infallible> = None;
// Timeout in milliseconds for epoll. // Timeout in milliseconds for epoll.
let timeout_ms = match (&self.timer_fd, timeout) { let timeout_ms = match (timer_fd, timeout) {
(_, Some(t)) if t == Duration::from_secs(0) => 0, (_, Some(t)) if t == Duration::from_secs(0) => 0,
(None, Some(t)) => { (None, Some(t)) => {
// Round up to a whole millisecond. // Round up to a whole millisecond.
@ -245,10 +260,10 @@ impl Drop for Poller {
"drop", "drop",
epoll_fd = ?self.epoll_fd.as_raw_fd(), epoll_fd = ?self.epoll_fd.as_raw_fd(),
notifier = ?self.notifier, notifier = ?self.notifier,
timer_fd = ?self.timer_fd
); );
let _enter = span.enter(); let _enter = span.enter();
#[cfg(not(target_os = "redox"))]
if let Some(timer_fd) = self.timer_fd.take() { if let Some(timer_fd) = self.timer_fd.take() {
let _ = self.delete(timer_fd.as_fd()); let _ = self.delete(timer_fd.as_fd());
} }
@ -257,6 +272,7 @@ impl Drop for Poller {
} }
/// `timespec` value that equals zero. /// `timespec` value that equals zero.
#[cfg(not(target_os = "redox"))]
const TS_ZERO: Timespec = unsafe { std::mem::transmute([0u8; std::mem::size_of::<Timespec>()]) }; const TS_ZERO: Timespec = unsafe { std::mem::transmute([0u8; std::mem::size_of::<Timespec>()]) };
/// Get the EPOLL flags for the interest. /// Get the EPOLL flags for the interest.
@ -385,6 +401,7 @@ impl EventExtra {
#[derive(Debug)] #[derive(Debug)]
enum Notifier { enum Notifier {
/// The primary notifier, using eventfd. /// The primary notifier, using eventfd.
#[cfg(not(target_os = "redox"))]
EventFd(OwnedFd), EventFd(OwnedFd),
/// The fallback notifier, using a pipe. /// The fallback notifier, using a pipe.
@ -401,19 +418,22 @@ impl Notifier {
/// Create a new notifier. /// Create a new notifier.
fn new() -> io::Result<Self> { fn new() -> io::Result<Self> {
// Skip eventfd for testing if necessary. // Skip eventfd for testing if necessary.
if !cfg!(polling_test_epoll_pipe) { #[cfg(not(target_os = "redox"))]
// Try to create an eventfd. {
match eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK) { if !cfg!(polling_test_epoll_pipe) {
Ok(fd) => { // Try to create an eventfd.
tracing::trace!("created eventfd for notifier"); match eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK) {
return Ok(Notifier::EventFd(fd)); Ok(fd) => {
} tracing::trace!("created eventfd for notifier");
return Ok(Notifier::EventFd(fd));
}
Err(err) => { Err(err) => {
tracing::warn!( tracing::warn!(
"eventfd() failed with error ({}), falling back to pipe", "eventfd() failed with error ({}), falling back to pipe",
err err
); );
}
} }
} }
} }
@ -435,6 +455,7 @@ impl Notifier {
/// The file descriptor to register in the poller. /// The file descriptor to register in the poller.
fn as_fd(&self) -> BorrowedFd<'_> { fn as_fd(&self) -> BorrowedFd<'_> {
match self { match self {
#[cfg(not(target_os = "redox"))]
Notifier::EventFd(fd) => fd.as_fd(), Notifier::EventFd(fd) => fd.as_fd(),
Notifier::Pipe { Notifier::Pipe {
read_pipe: read, .. read_pipe: read, ..
@ -445,6 +466,7 @@ impl Notifier {
/// Notify the poller. /// Notify the poller.
fn notify(&self) { fn notify(&self) {
match self { match self {
#[cfg(not(target_os = "redox"))]
Self::EventFd(fd) => { Self::EventFd(fd) => {
let buf: [u8; 8] = 1u64.to_ne_bytes(); let buf: [u8; 8] = 1u64.to_ne_bytes();
let _ = write(fd, &buf); let _ = write(fd, &buf);
@ -459,6 +481,7 @@ impl Notifier {
/// Clear the notification. /// Clear the notification.
fn clear(&self) { fn clear(&self) {
match self { match self {
#[cfg(not(target_os = "redox"))]
Self::EventFd(fd) => { Self::EventFd(fd) => {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
let _ = read(fd, &mut buf); let _ = read(fd, &mut buf);

View File

@ -1,7 +1,7 @@
//! Portable interface to epoll, kqueue, event ports, and IOCP. //! Portable interface to epoll, kqueue, event ports, and IOCP.
//! //!
//! Supported platforms: //! Supported platforms:
//! - [epoll](https://en.wikipedia.org/wiki/Epoll): Linux, Android //! - [epoll](https://en.wikipedia.org/wiki/Epoll): Linux, Android, RedoxOS
//! - [kqueue](https://en.wikipedia.org/wiki/Kqueue): macOS, iOS, tvOS, watchOS, FreeBSD, NetBSD, OpenBSD, //! - [kqueue](https://en.wikipedia.org/wiki/Kqueue): macOS, iOS, tvOS, watchOS, FreeBSD, NetBSD, OpenBSD,
//! DragonFly BSD //! DragonFly BSD
//! - [event ports](https://illumos.org/man/port_create): illumos, Solaris //! - [event ports](https://illumos.org/man/port_create): illumos, Solaris
@ -80,7 +80,11 @@ cfg_if! {
if #[cfg(polling_test_poll_backend)] { if #[cfg(polling_test_poll_backend)] {
mod poll; mod poll;
use poll as sys; use poll as sys;
} else if #[cfg(any(target_os = "linux", target_os = "android"))] { } else if #[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "redox"
))] {
mod epoll; mod epoll;
use epoll as sys; use epoll as sys;
} else if #[cfg(any( } else if #[cfg(any(

View File

@ -20,6 +20,7 @@ pub mod iocp;
mod __private { mod __private {
#[doc(hidden)] #[doc(hidden)]
#[allow(dead_code)]
pub trait PollerSealed {} pub trait PollerSealed {}
impl PollerSealed for crate::Poller {} impl PollerSealed for crate::Poller {}