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

Implement poll function and add feature to generate random based on cpu instruction. #4

Merged
merged 1 commit into from
Sep 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/arceos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ net = ["dep:axnet", "axfeat/net", "fd"]
pipe = ["fd"]
select = ["fd"]
epoll = ["fd"]
poll = ["fd"]

[dependencies]
# ArceOS modules
Expand Down
2 changes: 2 additions & 0 deletions api/arceos_posix_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ typedef struct {{
"pthread_attr_t",
"pthread_mutex_t",
"pthread_mutexattr_t",
"pollfd",
"nfds_t",
"epoll_event",
"iovec",
"clockid_t",
Expand Down
1 change: 1 addition & 0 deletions api/arceos_posix_api/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <netinet/in.h>
#include <pthread.h>
#include <stddef.h>
#include <poll.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/select.h>
Expand Down
5 changes: 5 additions & 0 deletions api/arceos_posix_api/src/imp/io_mpx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
//! I/O multiplexing:
//!
//! * [`select`](select::sys_select)
//! * [`poll`](poll::sys_poll)
//! * [`epoll_create`](epoll::sys_epoll_create)
//! * [`epoll_ctl`](epoll::sys_epoll_ctl)
//! * [`epoll_wait`](epoll::sys_epoll_wait)

#[cfg(feature = "epoll")]
mod epoll;
#[cfg(feature = "poll")]
mod poll;
#[cfg(feature = "select")]
mod select;

#[cfg(feature = "epoll")]
pub use self::epoll::{sys_epoll_create, sys_epoll_ctl, sys_epoll_wait};
#[cfg(feature = "poll")]
pub use self::poll::sys_poll;
#[cfg(feature = "select")]
pub use self::select::sys_select;
74 changes: 74 additions & 0 deletions api/arceos_posix_api/src/imp/io_mpx/poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Rukos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

/// Add `poll` feature to use poll() interface.
/// poll() is a system call function used to monitor I/O events across multiple file descriptors.
/// poll() is a blocking type of interface, make sure that this does not cost too much.
/// To monitor I/O events, you can also use `select` or `epoll` instead.
use crate::{ctypes, imp::fd_ops::get_file_like};
use axerrno::{LinuxError, LinuxResult};
use axhal::time::current_time;

use core::{ffi::c_int, time::Duration};

fn poll_all(fds: &mut [ctypes::pollfd]) -> LinuxResult<usize> {
let mut events_num = 0;

for pollfd_item in fds.iter_mut() {
let intfd = pollfd_item.fd;
let events = pollfd_item.events;
let revents = &mut pollfd_item.revents;
match get_file_like(intfd as c_int)?.poll() {
Err(_) => {
if (events & ctypes::EPOLLERR as i16) != 0 {
*revents |= ctypes::EPOLLERR as i16;
}
}
Ok(state) => {
if state.readable && (events & ctypes::EPOLLIN as i16 != 0) {
*revents |= ctypes::EPOLLIN as i16;
}

if state.writable && (events & ctypes::EPOLLOUT as i16 != 0) {
*revents |= ctypes::EPOLLOUT as i16;
}
}
}
events_num += 1;
}
Ok(events_num)
}

/// Used to monitor multiple file descriptors for events
pub unsafe fn sys_poll(fds: *mut ctypes::pollfd, nfds: ctypes::nfds_t, timeout: c_int) -> c_int {
debug!("ax_poll <= nfds: {} timeout: {}", nfds, timeout);

syscall_body!(ax_poll, {
if nfds == 0 {
return Err(LinuxError::EINVAL);
}
let fds = core::slice::from_raw_parts_mut(fds, nfds as usize);
let deadline = (!timeout.is_negative())
.then(|| current_time() + Duration::from_millis(timeout as u64));
loop {
#[cfg(feature = "net")]
axnet::poll_interfaces();
let fds_num = poll_all(fds)?;
if fds_num > 0 {
return Ok(fds_num as c_int);
}

if deadline.map_or(false, |ddl| current_time() >= ddl) {
debug!(" timeout!");
return Ok(0);
}
crate::sys_sched_yield();
}
})
}
2 changes: 1 addition & 1 deletion api/arceos_posix_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod time;
pub mod fd_ops;
#[cfg(feature = "fs")]
pub mod fs;
#[cfg(any(feature = "select", feature = "epoll"))]
#[cfg(any(feature = "select", feature = "poll", feature = "epoll"))]
pub mod io_mpx;
#[cfg(feature = "net")]
pub mod net;
Expand Down
2 changes: 2 additions & 0 deletions api/arceos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub use imp::time::{sys_clock_gettime, sys_nanosleep};
pub use imp::fd_ops::{sys_close, sys_dup, sys_dup2, sys_fcntl};
#[cfg(feature = "fs")]
pub use imp::fs::{sys_fstat, sys_getcwd, sys_lseek, sys_lstat, sys_open, sys_rename, sys_stat};
#[cfg(feature = "poll")]
pub use imp::io_mpx::sys_poll;
#[cfg(feature = "select")]
pub use imp::io_mpx::sys_select;
#[cfg(feature = "epoll")]
Expand Down
1 change: 0 additions & 1 deletion apps/c/pthread/parallel/expect_info_smp4_fifo.out
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ part 12 finished
part 13 finished
part 14 finished
part 15 finished
actual sum = 61783189038
(C)Pthread parallel run OK!
Shutting down...
1 change: 0 additions & 1 deletion apps/c/pthread/parallel/expect_info_smp4_rr.out
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ part 12 finished
part 13 finished
part 14 finished
part 15 finished
actual sum = 61783189038
(C)Pthread parallel run OK!
Shutting down...
1 change: 1 addition & 0 deletions apps/c/redis/features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ fs
net
pipe
epoll
poll
coolyjg marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion apps/task/parallel/expect_info_smp1_fifo.out
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ part 13: ThreadId(17) \[1625000, 1750000)
part 14: ThreadId(18) \[1750000, 1875000)
part 15: ThreadId(19) \[1875000, 2000000)
part 15: ThreadId(19) finished
sum = 87362923216
Parallel summation tests run OK!
Shutting down...
1 change: 0 additions & 1 deletion apps/task/parallel/expect_info_smp4_cfs.out
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ part 11: ThreadId([0-9]\+) finished
part 12: ThreadId([0-9]\+) finished
part 13: ThreadId([0-9]\+) finished
part 14: ThreadId([0-9]\+) finished
sum = 87362923216
Parallel summation tests run OK!
Shutting down...
1 change: 0 additions & 1 deletion apps/task/parallel/expect_info_smp4_rr.out
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ part 11: ThreadId([0-9]\+) finished
part 12: ThreadId([0-9]\+) finished
part 13: ThreadId([0-9]\+) finished
part 14: ThreadId([0-9]\+) finished
sum = 87362923216
Parallel summation tests run OK!
Shutting down...
4 changes: 2 additions & 2 deletions scripts/make/features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ifeq ($(APP_TYPE),c)
ax_feat_prefix := axfeat/
lib_feat_prefix := axlibc/
lib_features := fp_simd alloc multitask fs net fd pipe select epoll
lib_features := fp_simd alloc multitask fs net fd pipe select poll epoll random-hw
else
# TODO: it's better to use `axfeat/` as `ax_feat_prefix`, but all apps need to have `axfeat` as a dependency
ax_feat_prefix := axstd/
Expand All @@ -28,7 +28,7 @@ ifeq ($(APP_TYPE), c)
ifneq ($(wildcard $(APP)/features.txt),) # check features.txt exists
override FEATURES += $(shell cat $(APP)/features.txt)
endif
ifneq ($(filter fs net pipe select epoll,$(FEATURES)),)
ifneq ($(filter fs net pipe select poll epoll,$(FEATURES)),)
override FEATURES += fd
endif
endif
Expand Down
2 changes: 2 additions & 0 deletions ulib/axlibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ net = ["arceos_posix_api/net", "fd"]
fd = []
pipe = ["arceos_posix_api/pipe"]
select = ["arceos_posix_api/select"]
poll = ["arceos_posix_api/poll"]
epoll = ["arceos_posix_api/epoll"]
random-hw = []

[dependencies]
axfeat = { path = "../../api/axfeat" }
Expand Down
18 changes: 0 additions & 18 deletions ulib/axlibc/c/poll.c

This file was deleted.

13 changes: 13 additions & 0 deletions ulib/axlibc/src/io_mpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{ctypes, utils::e};

use core::ffi::c_int;

#[cfg(feature = "poll")]
use arceos_posix_api::sys_poll;
#[cfg(feature = "select")]
use arceos_posix_api::sys_select;
#[cfg(feature = "epoll")]
Expand Down Expand Up @@ -61,3 +63,14 @@ pub unsafe extern "C" fn select(
) -> c_int {
e(sys_select(nfds, readfds, writefds, exceptfds, timeout))
}

/// Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation
#[cfg(feature = "poll")]
#[no_mangle]
pub unsafe extern "C" fn poll(
fds: *mut ctypes::pollfd,
nfds: ctypes::nfds_t,
timeout: c_int,
) -> c_int {
e(sys_poll(fds, nfds, timeout))
}
4 changes: 3 additions & 1 deletion ulib/axlibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod utils;
mod fd_ops;
#[cfg(feature = "fs")]
mod fs;
#[cfg(any(feature = "select", feature = "epoll"))]
#[cfg(any(feature = "select", feature = "poll", feature = "epoll"))]
mod io_mpx;
#[cfg(feature = "alloc")]
mod malloc;
Expand Down Expand Up @@ -125,6 +125,8 @@ pub use self::pthread::{pthread_mutex_init, pthread_mutex_lock, pthread_mutex_un
#[cfg(feature = "pipe")]
pub use self::pipe::pipe;

#[cfg(feature = "poll")]
pub use self::io_mpx::poll;
#[cfg(feature = "select")]
pub use self::io_mpx::select;
#[cfg(feature = "epoll")]
Expand Down
Loading
Loading