Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use monoio on stable. #227

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ Moreover, Monoio is designed with a thread-per-core model in mind. Users do not
As you may have guessed, this runtime is primarily targeted at servers, where operations are io-bound on network sockets, and therefore the use of native asynchronous I/O APIs maximizes the throughput of the server. In order for Monoio to be as efficient as possible, we've enabled some unstable Rust features, and we've designed a whole new IO abstraction, which unfortunately may cause some compatibility problems. [Our benchmarks](https://github.com/bytedance/monoio/blob/master/docs/en/benchmark.md) probe that, for our use-cases, Monoio has a better performance than other Rust runtimes.

## Quick Start
To use monoio, you need the latest nightly rust toolchain. If you already installed it, please make sure it is the latest version.

To force using nightly, create a file named `rust-toolchain` and write `nightly` in it. Also, you can use `cargo +nightly` to build or run.
To use monoio, you need rust 1.75. If you already installed it, please make sure it is the latest version.

Also, if you want to use io_uring, you must make sure your kernel supports it([5.6+](docs/en/platform-support.md)). And, memlock is [configured as a proper number](docs/en/memlock.md). If your kernel version does not meet the requirements, you can try to use the legacy driver to start, currently supports Linux and macOS([ref here](/docs/en/use-legacy-driver.md)).

Expand Down
2 changes: 2 additions & 0 deletions monoio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ local-sync = "0.0.5"
tempfile = "3.2"

[features]
# use nightly only feature flags
unstable = []
# async-cancel will push a async-cancel entry into sq when op is canceled
async-cancel = []
# enanle zero copy(enable SOCK_ZEROCOPY + MSG_ZEROCOPY flag)
Expand Down
9 changes: 9 additions & 0 deletions monoio/src/driver/uring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,21 @@ impl UringInner {
fn submit(&mut self) -> io::Result<()> {
loop {
match self.uring.submit() {
#[cfg(feature = "unstable")]
Err(ref e)
if e.kind() == io::ErrorKind::Other
|| e.kind() == io::ErrorKind::ResourceBusy =>
{
self.tick();
}
#[cfg(not(feature = "unstable"))]
Err(ref e) if e.raw_os_error() == Some(libc::EAGAIN) || e.raw_os_error() == Some(libc::EBUSY) => {
// This error is constructed with io::Error::last_os_error():
// https://github.com/tokio-rs/io-uring/blob/01c83bbce965d4aaf93ebfaa08c3aa8b7b0f5335/src/sys/mod.rs#L32
// So we can use https://doc.rust-lang.org/nightly/std/io/struct.Error.html#method.raw_os_error
// to get the raw error code.
self.tick();
},
e => return e.map(|_| ()),
}
}
Expand Down
8 changes: 4 additions & 4 deletions monoio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![doc = include_str!("../../README.md")]
#![warn(missing_docs, unreachable_pub)]
#![allow(stable_features)]
#![feature(io_error_more)]
#![feature(lazy_cell)]
#![feature(stmt_expr_attributes)]
#![feature(thread_local)]
#![cfg_attr(feature = "unstable", feature(io_error_more))]
#![cfg_attr(feature = "unstable", feature(lazy_cell))]
#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]
#![cfg_attr(feature = "unstable", feature(thread_local))]

#[macro_use]
pub mod macros;
Expand Down
6 changes: 6 additions & 0 deletions monoio/src/net/tcp/tfo/linux.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use std::{cell::Cell, io, os::fd::AsRawFd};

#[cfg(feature = "unstable")]
#[thread_local]
pub(crate) static TFO_CONNECT_AVAILABLE: Cell<bool> = Cell::new(true);

#[cfg(not(feature = "unstable"))]
thread_local!{
pub(crate) static TFO_CONNECT_AVAILABLE: Cell<bool> = Cell::new(true);
}

/// Call before listen.
pub(crate) fn set_tcp_fastopen<S: AsRawFd>(fd: &S, fast_open: i32) -> io::Result<()> {
crate::syscall!(setsockopt(
Expand Down
6 changes: 6 additions & 0 deletions monoio/src/task/waker_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ pub(crate) fn dummy_waker() -> Waker {
unsafe { Waker::from_raw(raw_waker()) }
}

#[cfg(feature = "unstable")]
#[thread_local]
static SHOULD_POLL: Cell<bool> = Cell::new(true);

#[cfg(not(feature = "unstable"))]
thread_local!{
static SHOULD_POLL: Cell<bool> = Cell::new(true);
}

#[inline]
pub(crate) fn should_poll() -> bool {
SHOULD_POLL.replace(false)
Expand Down
3 changes: 1 addition & 2 deletions monoio/src/utils/thread_id.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::sync::{
atomic::{AtomicUsize, Ordering::Relaxed},
LazyLock,
};

// thread id begins from 16.
// 0 is default thread
// 1-15 are unused
static ID_GEN: LazyLock<AtomicUsize> = LazyLock::new(|| AtomicUsize::new(16));
static ID_GEN: AtomicUsize = AtomicUsize::new(16);

pub(crate) const DEFAULT_THREAD_ID: usize = 0;

Expand Down
1 change: 0 additions & 1 deletion rust-toolchain

This file was deleted.