Skip to content

Commit

Permalink
refactor(scheduler): move scheduler input to core-local storage
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Sep 18, 2023
1 parent ceeb57d commit 64a9620
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/arch/aarch64/kernel/core_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ use core::cell::{Cell, RefCell, RefMut};
use core::ptr;
use core::sync::atomic::Ordering;

#[cfg(feature = "smp")]
use hermit_sync::InterruptTicketMutex;

use super::interrupts::{IrqStatistics, IRQ_COUNTERS};
use super::CPU_ONLINE;
use crate::executor::task::AsyncTask;
#[cfg(feature = "smp")]
use crate::scheduler::SchedulerInput;
use crate::scheduler::{CoreId, PerCoreScheduler};

pub(crate) struct CoreLocal {
Expand All @@ -20,6 +25,9 @@ pub(crate) struct CoreLocal {
irq_statistics: &'static IrqStatistics,
/// Queue of async tasks
async_tasks: RefCell<Vec<AsyncTask>>,
/// Queues to handle incoming requests from the other cores
#[cfg(feature = "smp")]
pub scheduler_input: InterruptTicketMutex<SchedulerInput>,
}

impl CoreLocal {
Expand All @@ -39,6 +47,8 @@ impl CoreLocal {
scheduler: Cell::new(ptr::null_mut()),
irq_statistics,
async_tasks: RefCell::new(Vec::new()),
#[cfg(feature = "smp")]
scheduler_input: InterruptTicketMutex::new(SchedulerInput::new()),
};
let this = if core_id == 0 {
take_static::take_static! {
Expand Down
9 changes: 9 additions & 0 deletions src/arch/x86_64/kernel/core_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ use core::cell::{Cell, RefCell, RefMut};
use core::ptr;
use core::sync::atomic::Ordering;

#[cfg(feature = "smp")]
use hermit_sync::InterruptTicketMutex;
use x86_64::registers::model_specific::GsBase;
use x86_64::structures::tss::TaskStateSegment;
use x86_64::VirtAddr;

use super::interrupts::{IrqStatistics, IRQ_COUNTERS};
use super::CPU_ONLINE;
use crate::executor::task::AsyncTask;
#[cfg(feature = "smp")]
use crate::scheduler::SchedulerInput;
use crate::scheduler::{CoreId, PerCoreScheduler};

#[repr(C)]
Expand All @@ -29,6 +33,9 @@ pub(crate) struct CoreLocal {
irq_statistics: &'static IrqStatistics,
/// Queue of async tasks
async_tasks: RefCell<Vec<AsyncTask>>,
/// Queues to handle incoming requests from the other cores
#[cfg(feature = "smp")]
pub scheduler_input: InterruptTicketMutex<SchedulerInput>,
}

impl CoreLocal {
Expand All @@ -52,6 +59,8 @@ impl CoreLocal {
kernel_stack: Cell::new(0),
irq_statistics,
async_tasks: RefCell::new(Vec::new()),
#[cfg(feature = "smp")]
scheduler_input: InterruptTicketMutex::new(SchedulerInput::new()),
};
let this = if core_id == 0 {
take_static::take_static! {
Expand Down
17 changes: 6 additions & 11 deletions src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static TASKS: InterruptTicketMutex<BTreeMap<TaskId, TaskHandle>> =
pub type CoreId = u32;

#[cfg(feature = "smp")]
struct SchedulerInput {
pub struct SchedulerInput {
/// Queue of new tasks
new_tasks: VecDeque<NewTask>,
/// Queue of task, which are wakeup by another core
Expand Down Expand Up @@ -74,9 +74,6 @@ pub struct PerCoreScheduler {
finished_tasks: VecDeque<Rc<RefCell<Task>>>,
/// Queue of blocked tasks, sorted by wakeup time.
blocked_tasks: BlockedTaskQueue,
/// Queues to handle incoming requests from the other cores
#[cfg(feature = "smp")]
input: InterruptTicketMutex<SchedulerInput>,
}

struct NewTask {
Expand Down Expand Up @@ -447,7 +444,7 @@ impl PerCoreScheduler {

#[cfg(all(target_arch = "x86_64", feature = "smp"))]
pub fn check_input(&mut self) {
let mut input_locked = self.input.lock();
let mut input_locked = CoreLocal::get().scheduler_input.lock();

while let Some(task) = input_locked.wakeup_tasks.pop_front() {
self.blocked_tasks.custom_wakeup(task);
Expand Down Expand Up @@ -689,18 +686,16 @@ pub fn add_current_core() {
ready_queue: PriorityTaskQueue::new(),
finished_tasks: VecDeque::new(),
blocked_tasks: BlockedTaskQueue::new(),
#[cfg(feature = "smp")]
input: InterruptTicketMutex::new(SchedulerInput::new()),
});

let scheduler = Box::into_raw(boxed_scheduler);
set_core_scheduler(scheduler);
#[cfg(feature = "smp")]
{
let scheduler = unsafe { scheduler.as_ref().unwrap() };
SCHEDULER_INPUTS
.lock()
.insert(core_id.try_into().unwrap(), &scheduler.input);
SCHEDULER_INPUTS.lock().insert(
core_id.try_into().unwrap(),
&CoreLocal::get().scheduler_input,
);
}
}

Expand Down

0 comments on commit 64a9620

Please sign in to comment.