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

feat: support percpu run-queue and cpu affinity for axtask #170

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [push, pull_request]
env:
qemu-version: 8.2.0
rust-toolchain: nightly-2024-05-02
arceos-apps: '68054e8'
arceos-apps: 'b25b7e2'

jobs:
unit-test:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,13 @@ justrun:
$(call run_qemu)

debug: build
$(call run_qemu_debug) &
sleep 1
$(call run_qemu_debug)

gdb:
$(GDB) $(OUT_ELF) \
-ex 'target remote localhost:1234' \
-ex 'b rust_entry' \
-ex 'b panic' \
-ex 'continue' \
-ex 'disp /16i $$pc'

Expand Down
2 changes: 1 addition & 1 deletion api/axfeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ documentation = "https://arceos-org.github.io/arceos/axfeat/index.html"
default = []

# Multicore
smp = ["axhal/smp", "axruntime/smp", "kspin/smp"]
smp = ["axhal/smp", "axruntime/smp", "axtask/smp", "kspin/smp"]

# Floating point/SIMD
fp_simd = ["axhal/fp_simd"]
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/arch/aarch64/trap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::arch::global_asm;

use aarch64_cpu::registers::{ESR_EL1, FAR_EL1};
use memory_addr::{va, VirtAddr};
use memory_addr::va;
use page_table_entry::MappingFlags;
use tock_registers::interfaces::Readable;

Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/arch/riscv/trap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use memory_addr::{va, VirtAddr};
use memory_addr::va;
use page_table_entry::MappingFlags;
use riscv::register::scause::{self, Exception as E, Trap};
use riscv::register::stval;
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/arch/x86_64/trap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use memory_addr::{va, VirtAddr};
use memory_addr::va;
use page_table_entry::MappingFlags;
use x86::{controlregs::cr2, irq::*};
use x86_64::structures::idt::PageFaultErrorCode;
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/aarch64_common/boot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use aarch64_cpu::{asm, asm::barrier, registers::*};
use core::ptr::addr_of_mut;
use memory_addr::{pa, PhysAddr};
use memory_addr::pa;
use page_table_entry::aarch64::{MemAttr, A64PTE};
use tock_registers::interfaces::{ReadWriteable, Readable, Writeable};

Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/aarch64_qemu_virt/mem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mem::{pa, MemRegion, PhysAddr};
use crate::mem::{pa, MemRegion};
use page_table_entry::{aarch64::A64PTE, GenericPTE, MappingFlags};

/// Returns platform-specific memory regions.
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/aarch64_qemu_virt/mp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mem::{va, virt_to_phys, PhysAddr, VirtAddr};
use crate::mem::{va, virt_to_phys, PhysAddr};

/// Starts the given secondary CPU with its boot stack.
pub fn start_secondary_cpu(cpu_id: usize, stack_top: PhysAddr) {
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/riscv64_qemu_virt/mp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mem::{va, virt_to_phys, PhysAddr, VirtAddr};
use crate::mem::{va, virt_to_phys, PhysAddr};

/// Starts the given secondary CPU with its boot stack.
pub fn start_secondary_cpu(hartid: usize, stack_top: PhysAddr) {
Expand Down
2 changes: 1 addition & 1 deletion modules/axruntime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ documentation = "https://arceos-org.github.io/arceos/axruntime/index.html"
[features]
default = []

smp = ["axhal/smp"]
smp = ["axhal/smp", "axtask?/smp"]
irq = ["axhal/irq", "axtask?/irq", "percpu", "kernel_guard"]
tls = ["axhal/tls", "axtask?/tls"]
alloc = ["axalloc"]
Expand Down
2 changes: 2 additions & 0 deletions modules/axruntime/src/mp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub extern "C" fn rust_main_secondary(cpu_id: usize) -> ! {
#[cfg(feature = "irq")]
axhal::arch::enable_irqs();

debug!("Secondary CPU {:x} running...", cpu_id);

#[cfg(all(feature = "tls", not(feature = "multitask")))]
super::init_tls();

Expand Down
16 changes: 13 additions & 3 deletions modules/axtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ documentation = "https://arceos-org.github.io/arceos/axtask/index.html"
default = []

multitask = [
"dep:axconfig", "dep:percpu", "dep:kspin", "dep:lazyinit", "dep:memory_addr",
"dep:scheduler", "dep:timer_list", "kernel_guard", "dep:crate_interface",
"dep:axconfig",
"dep:percpu",
"dep:kspin",
"dep:lazyinit",
"dep:memory_addr",
"dep:scheduler",
"dep:timer_list",
"kernel_guard",
"dep:crate_interface",
]
irq = []
tls = ["axhal/tls"]
preempt = ["irq", "percpu?/preempt", "kernel_guard/preempt"]
smp = ["kspin/smp"]

sched_fifo = ["multitask"]
sched_rr = ["multitask", "preempt"]
Expand All @@ -29,6 +37,7 @@ test = ["percpu?/sp-naive"]
[dependencies]
cfg-if = "1.0"
log = "0.4.21"
bitmaps = { version = "3.2.1", default-features = false }
axhal = { workspace = true }
axconfig = { workspace = true, optional = true }
percpu = { version = "0.1", optional = true }
Expand All @@ -38,7 +47,8 @@ memory_addr = { version = "0.3", optional = true }
timer_list = { version = "0.1", optional = true }
kernel_guard = { version = "0.1", optional = true }
crate_interface = { version = "0.1", optional = true }
scheduler = { git = "https://github.com/arceos-org/scheduler.git", tag = "v0.1.0", optional = true }
# scheduler = { git = "https://github.com/arceos-org/scheduler.git", tag = "v0.1.0", optional = true }
scheduler = { git = "https://github.com/arceos-org/scheduler.git", branch = "num_tasks", optional = true }

[dev-dependencies]
rand = "0.8"
Expand Down
23 changes: 15 additions & 8 deletions modules/axtask/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use alloc::{string::String, sync::Arc};

pub(crate) use crate::run_queue::{AxRunQueue, RUN_QUEUE};
pub(crate) use crate::run_queue::{current_run_queue, select_run_queue};

#[doc(cfg(feature = "multitask"))]
pub use crate::task::{CurrentTask, TaskId, TaskInner};
Expand Down Expand Up @@ -75,6 +75,8 @@ pub fn init_scheduler() {
/// Initializes the task scheduler for secondary CPUs.
pub fn init_scheduler_secondary() {
crate::run_queue::init_secondary();
#[cfg(feature = "irq")]
crate::timers::init();
}

/// Handles periodic timer ticks for the task manager.
Expand All @@ -84,7 +86,7 @@ pub fn init_scheduler_secondary() {
#[doc(cfg(feature = "irq"))]
pub fn on_timer_tick() {
crate::timers::check_events();
RUN_QUEUE.lock().scheduler_timer_tick();
current_run_queue().scheduler_timer_tick();
}

/// Spawns a new task with the given parameters.
Expand All @@ -94,8 +96,13 @@ pub fn spawn_raw<F>(f: F, name: String, stack_size: usize) -> AxTaskRef
where
F: FnOnce() + Send + 'static,
{
let task = TaskInner::new(f, name, stack_size);
RUN_QUEUE.lock().add_task(task.clone());
let task = TaskInner::new(f, name, stack_size, None);
let _kernel_guard = kernel_guard::NoPreemptIrqSave::new();
crate::select_run_queue(
#[cfg(feature = "smp")]
task.clone(),
)
.add_task(task.clone());
task
}

Expand All @@ -122,13 +129,13 @@ where
///
/// [CFS]: https://en.wikipedia.org/wiki/Completely_Fair_Scheduler
pub fn set_priority(prio: isize) -> bool {
RUN_QUEUE.lock().set_current_priority(prio)
current_run_queue().set_current_priority(prio)
}

/// Current task gives up the CPU time voluntarily, and switches to another
/// ready task.
pub fn yield_now() {
RUN_QUEUE.lock().yield_current();
current_run_queue().yield_current()
}

/// Current task is going to sleep for the given duration.
Expand All @@ -143,14 +150,14 @@ pub fn sleep(dur: core::time::Duration) {
/// If the feature `irq` is not enabled, it uses busy-wait instead.
pub fn sleep_until(deadline: axhal::time::TimeValue) {
#[cfg(feature = "irq")]
RUN_QUEUE.lock().sleep_until(deadline);
current_run_queue().sleep_until(deadline);
#[cfg(not(feature = "irq"))]
axhal::time::busy_wait_until(deadline);
}

/// Exits the current task.
pub fn exit(exit_code: i32) -> ! {
RUN_QUEUE.lock().exit_current(exit_code)
current_run_queue().exit_current(exit_code)
}

/// The idle task routine.
Expand Down
1 change: 1 addition & 0 deletions modules/axtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cfg_if::cfg_if! {
extern crate log;
extern crate alloc;

#[macro_use]
mod run_queue;
mod task;
mod api;
Expand Down
Loading
Loading