Skip to content

Commit ac75526

Browse files
committed
support more config
1 parent 6ae9409 commit ac75526

File tree

12 files changed

+109
-46
lines changed

12 files changed

+109
-46
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ jobs:
8282
aarch64-apple-darwin,
8383

8484
x86_64-pc-windows-gnu,
85-
i686-pc-windows-gnu,
85+
# i686-pc-windows-gnu,
8686
x86_64-pc-windows-msvc,
87-
i686-pc-windows-msvc,
87+
# i686-pc-windows-msvc,
8888
]
8989
channel: [ 1.81.0, nightly-2024-08-02 ]
9090
include:

core/src/common/constants.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ pub const COROUTINE_GLOBAL_QUEUE_BEAN: &str = "coroutineGlobalQueueBean";
1515
/// Task global queue bean name.
1616
pub const TASK_GLOBAL_QUEUE_BEAN: &str = "taskGlobalQueueBean";
1717

18-
/// Stack pool bean name.
19-
pub const STACK_POOL_BEAN: &str = "stackPoolBean";
20-
2118
/// Monitor bean name.
2219
pub const MONITOR_BEAN: &str = "monitorBean";
2320

core/src/net/config.rs renamed to core/src/config.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ pub struct Config {
88
min_size: usize,
99
max_size: usize,
1010
keep_alive_time: u64,
11+
min_memory_count: usize,
12+
memory_keep_alive_time: u64,
1113
hook: bool,
1214
}
1315

1416
impl Config {
1517
#[must_use]
1618
pub fn single() -> Self {
17-
Self::new(1, DEFAULT_STACK_SIZE, 0, 65536, 0, true)
19+
Self::new(1, DEFAULT_STACK_SIZE, 0, 65536, 0, 0, 10_000_000_000, true)
1820
}
1921

22+
#[allow(clippy::too_many_arguments)]
2023
#[must_use]
2124
pub fn new(
2225
event_loop_size: usize,
2326
stack_size: usize,
2427
min_size: usize,
2528
max_size: usize,
2629
keep_alive_time: u64,
30+
min_memory_count: usize,
31+
memory_keep_alive_time: u64,
2732
hook: bool,
2833
) -> Self {
2934
Self {
@@ -32,6 +37,8 @@ impl Config {
3237
min_size,
3338
max_size,
3439
keep_alive_time,
40+
min_memory_count,
41+
memory_keep_alive_time,
3542
hook,
3643
}
3744
}
@@ -61,6 +68,16 @@ impl Config {
6168
self.keep_alive_time
6269
}
6370

71+
#[must_use]
72+
pub fn min_memory_count(&self) -> usize {
73+
self.min_memory_count
74+
}
75+
76+
#[must_use]
77+
pub fn memory_keep_alive_time(&self) -> u64 {
78+
self.memory_keep_alive_time
79+
}
80+
6481
#[must_use]
6582
pub fn hook(&self) -> bool {
6683
self.hook
@@ -101,6 +118,16 @@ impl Config {
101118
self
102119
}
103120

121+
pub fn set_min_memory_count(&mut self, min_memory_count: usize) -> &mut Self {
122+
self.min_memory_count = min_memory_count;
123+
self
124+
}
125+
126+
pub fn set_memory_keep_alive_time(&mut self, memory_keep_alive_time: u64) -> &mut Self {
127+
self.memory_keep_alive_time = memory_keep_alive_time;
128+
self
129+
}
130+
104131
pub fn set_hook(&mut self, hook: bool) -> &mut Self {
105132
self.hook = hook;
106133
self
@@ -109,6 +136,15 @@ impl Config {
109136

110137
impl Default for Config {
111138
fn default() -> Self {
112-
Self::new(cpu_count(), DEFAULT_STACK_SIZE, 0, 65536, 0, true)
139+
Self::new(
140+
cpu_count(),
141+
DEFAULT_STACK_SIZE,
142+
0,
143+
65536,
144+
0,
145+
0,
146+
10_000_000_000,
147+
true,
148+
)
113149
}
114150
}

core/src/coroutine/korosensei.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::catch;
22
use crate::common::constants::CoroutineState;
33
use crate::coroutine::listener::Listener;
44
use crate::coroutine::local::CoroutineLocal;
5-
use crate::coroutine::stack_pool::{PooledStack, StackPool};
5+
use crate::coroutine::stack_pool::{MemoryPool, PooledStack};
66
use crate::coroutine::suspender::Suspender;
77
use crate::coroutine::StackInfo;
88
use corosensei::stack::Stack;
@@ -29,7 +29,6 @@ pub struct Coroutine<'c, Param, Yield, Return> {
2929
pub(crate) name: String,
3030
inner: corosensei::Coroutine<Param, Yield, Result<Return, &'static str>, PooledStack>,
3131
pub(crate) state: Cell<CoroutineState<Yield, Return>>,
32-
pub(crate) stack_size: usize,
3332
pub(crate) stack_infos: RefCell<VecDeque<StackInfo>>,
3433
pub(crate) listeners: VecDeque<&'c dyn Listener<Yield, Return>>,
3534
pub(crate) local: CoroutineLocal<'c>,
@@ -308,7 +307,7 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
308307
stack_size: usize,
309308
callback: F,
310309
) -> std::io::Result<R> {
311-
let stack_pool = StackPool::get_instance();
310+
let stack_pool = MemoryPool::get_instance();
312311
if let Some(co) = Self::current() {
313312
let remaining_stack = unsafe { co.remaining_stack() };
314313
if remaining_stack >= red_zone {
@@ -380,7 +379,7 @@ where
380379
F: FnOnce(&Suspender<Param, Yield>, Param) -> Return + 'static,
381380
{
382381
let stack_size = stack_size.max(crate::common::page_size());
383-
let stack = StackPool::get_instance().allocate(stack_size)?;
382+
let stack = MemoryPool::get_instance().allocate(stack_size)?;
384383
let stack_infos = RefCell::new(VecDeque::from([StackInfo {
385384
stack_top: stack.base().get(),
386385
stack_bottom: stack.limit().get(),
@@ -403,7 +402,6 @@ where
403402
let mut co = Coroutine {
404403
name,
405404
inner,
406-
stack_size,
407405
stack_infos,
408406
state: Cell::new(CoroutineState::Ready),
409407
listeners: VecDeque::default(),

core/src/coroutine/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::common::constants::CoroutineState;
2+
use crate::common::ordered_work_steal::Ordered;
23
use crate::coroutine::listener::Listener;
34
use crate::coroutine::local::CoroutineLocal;
45
use crate::{impl_current_for, impl_display_by_debug, impl_for_named};
@@ -16,10 +17,11 @@ pub mod local;
1617
/// Coroutine listener abstraction and impl.
1718
pub mod listener;
1819

19-
use crate::common::ordered_work_steal::Ordered;
20+
/// Reuse stacks.
21+
pub mod stack_pool;
22+
2023
#[cfg(feature = "korosensei")]
2124
pub use korosensei::Coroutine;
22-
2325
#[cfg(feature = "korosensei")]
2426
mod korosensei;
2527

@@ -76,8 +78,6 @@ pub struct StackInfo {
7678
/// Coroutine state abstraction and impl.
7779
mod state;
7880

79-
pub(crate) mod stack_pool;
80-
8181
impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
8282
/// Get the name of this coroutine.
8383
pub fn name(&self) -> &str {
@@ -201,8 +201,10 @@ where
201201
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
202202
f.debug_struct("Coroutine")
203203
.field("name", &self.name())
204-
.field("status", &self.state())
204+
.field("state", &self.state())
205+
.field("stack_infos", &self.stack_infos)
205206
.field("local", &self.local)
207+
.field("priority", &self.priority)
206208
.finish()
207209
}
208210
}

core/src/coroutine/stack_pool.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::common::beans::BeanFactory;
2-
use crate::common::constants::STACK_POOL_BEAN;
31
use crate::common::now;
2+
use crate::config::Config;
43
use corosensei::stack::{DefaultStack, Stack, StackPointer};
4+
use once_cell::sync::OnceCell;
55
use std::cell::UnsafeCell;
66
use std::cmp::Ordering;
77
use std::collections::BinaryHeap;
@@ -138,35 +138,49 @@ impl PooledStack {
138138
}
139139
}
140140

141-
pub(crate) struct StackPool {
141+
static STACK_POOL: OnceCell<MemoryPool> = OnceCell::new();
142+
143+
/// A memory pool for reusing stacks.
144+
#[derive(educe::Educe)]
145+
#[educe(Debug)]
146+
pub struct MemoryPool {
147+
#[educe(Debug(ignore))]
142148
pool: UnsafeCell<BinaryHeap<PooledStack>>,
143149
len: AtomicUsize,
144150
//最小内存数,即核心内存数
145-
min_size: AtomicUsize,
151+
min_count: AtomicUsize,
146152
//非核心内存的最大存活时间,单位ns
147153
keep_alive_time: AtomicU64,
148154
}
149155

150-
unsafe impl Send for StackPool {}
156+
unsafe impl Send for MemoryPool {}
151157

152-
unsafe impl Sync for StackPool {}
158+
unsafe impl Sync for MemoryPool {}
153159

154-
impl Default for StackPool {
160+
impl Default for MemoryPool {
155161
fn default() -> Self {
156162
Self::new(0, 10_000_000_000)
157163
}
158164
}
159165

160-
impl StackPool {
166+
impl MemoryPool {
167+
/// Init the `MemoryPool`.
168+
pub fn init(config: &Config) -> Result<(), MemoryPool> {
169+
STACK_POOL.set(MemoryPool::new(
170+
config.min_memory_count(),
171+
config.memory_keep_alive_time(),
172+
))
173+
}
174+
161175
pub(crate) fn get_instance<'m>() -> &'m Self {
162-
BeanFactory::get_or_default(STACK_POOL_BEAN)
176+
STACK_POOL.get_or_init(MemoryPool::default)
163177
}
164178

165-
pub(crate) fn new(min_size: usize, keep_alive_time: u64) -> Self {
179+
pub(crate) fn new(min_count: usize, keep_alive_time: u64) -> Self {
166180
Self {
167181
pool: UnsafeCell::new(BinaryHeap::default()),
168182
len: AtomicUsize::default(),
169-
min_size: AtomicUsize::new(min_size),
183+
min_count: AtomicUsize::new(min_count),
170184
keep_alive_time: AtomicU64::new(keep_alive_time),
171185
}
172186
}
@@ -194,7 +208,7 @@ impl StackPool {
194208
stack.update_stack_teb_fields();
195209
return Ok(stack);
196210
}
197-
if self.min_size() < self.len()
211+
if self.min_count() < self.len()
198212
&& now() <= stack.create_time.saturating_add(self.keep_alive_time())
199213
{
200214
// clean the expired stack
@@ -221,13 +235,13 @@ impl StackPool {
221235
}
222236

223237
#[allow(dead_code)]
224-
pub(crate) fn set_min_size(&self, min_size: usize) {
225-
self.min_size
226-
.store(min_size, std::sync::atomic::Ordering::Release);
238+
pub(crate) fn set_min_count(&self, min_count: usize) {
239+
self.min_count
240+
.store(min_count, std::sync::atomic::Ordering::Release);
227241
}
228242

229-
pub(crate) fn min_size(&self) -> usize {
230-
self.min_size.load(std::sync::atomic::Ordering::Acquire)
243+
pub(crate) fn min_count(&self) -> usize {
244+
self.min_count.load(std::sync::atomic::Ordering::Acquire)
231245
}
232246

233247
pub(crate) fn len(&self) -> usize {
@@ -264,7 +278,7 @@ impl StackPool {
264278
}
265279
}
266280
for stack in maybe_free {
267-
if self.min_size() < self.len()
281+
if self.min_count() < self.len()
268282
&& now() <= stack.create_time.saturating_add(self.keep_alive_time())
269283
{
270284
// free the stack
@@ -283,7 +297,7 @@ mod tests {
283297

284298
#[test]
285299
fn test_stack_pool() -> std::io::Result<()> {
286-
let pool = StackPool::default();
300+
let pool = MemoryPool::default();
287301
let stack = pool.allocate(DEFAULT_STACK_SIZE)?;
288302
assert_eq!(Rc::strong_count(&stack.stack), 2);
289303
drop(stack);

core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
/// Common traits and impl.
5757
pub mod common;
5858

59+
/// Configuration for `EventLoops`.
60+
#[allow(missing_docs)]
61+
pub mod config;
62+
5963
/// Coroutine impls.
6064
pub mod coroutine;
6165

core/src/monitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::common::constants::{CoroutineState, MONITOR_BEAN};
33
use crate::common::{get_timeout_time, now, CondvarBlocker};
44
use crate::coroutine::listener::Listener;
55
use crate::coroutine::local::CoroutineLocal;
6-
use crate::coroutine::stack_pool::StackPool;
6+
use crate::coroutine::stack_pool::MemoryPool;
77
use crate::scheduler::SchedulableSuspender;
88
use crate::{catch, error, impl_current_for, impl_display_by_debug, info};
99
use nix::sys::pthread::{pthread_kill, pthread_self, Pthread};
@@ -137,7 +137,7 @@ impl Monitor {
137137
);
138138
}
139139
}
140-
StackPool::get_instance().clean();
140+
MemoryPool::get_instance().clean();
141141
//monitor线程不执行协程计算任务,每次循环至少wait 1ms
142142
monitor.blocker.clone().block(Duration::from_millis(1));
143143
}

core/src/net/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::config::Config;
12
use crate::coroutine::suspender::Suspender;
2-
use crate::net::config::Config;
33
use crate::net::event_loop::EventLoop;
44
use crate::net::join::JoinHandle;
55
use crate::{error, info};
@@ -30,10 +30,6 @@ mod operator;
3030
#[allow(missing_docs)]
3131
pub mod event_loop;
3232

33-
/// Configuration for `EventLoops`.
34-
#[allow(missing_docs)]
35-
pub mod config;
36-
3733
/// Task join abstraction and impl.
3834
pub mod join;
3935

hook/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
5050
use once_cell::sync::OnceCell;
5151
use open_coroutine_core::co_pool::task::UserTaskFunc;
52-
use open_coroutine_core::net::config::Config;
52+
use open_coroutine_core::config::Config;
53+
use open_coroutine_core::coroutine::stack_pool::MemoryPool;
5354
use open_coroutine_core::net::join::JoinHandle;
5455
use open_coroutine_core::net::{EventLoops, UserFunc};
5556
use open_coroutine_core::scheduler::SchedulableCoroutine;
@@ -75,6 +76,9 @@ pub mod syscall;
7576
/// Start the framework.
7677
#[no_mangle]
7778
pub extern "C" fn open_coroutine_init(config: Config) -> c_int {
79+
if MemoryPool::init(&config).is_err() {
80+
return -1;
81+
}
7882
EventLoops::init(&config);
7983
_ = HOOK.get_or_init(|| config.hook());
8084
0

0 commit comments

Comments
 (0)