Browse Source

Kernel tweaks

徐启航 2 năm trước cách đây
mục cha
commit
4b8316b4b2

+ 1 - 1
h2o/kernel/src/cpu/intr/imp.rs

@@ -141,7 +141,7 @@ mod syscall {
         }
 
         let blocker = crate::sched::Blocker::new(&(Arc::clone(&intr) as _), false, SIG_GENERIC);
-        blocker.wait(pree, time::from_us(timeout_us))?;
+        blocker.wait(Some(pree), time::from_us(timeout_us))?;
         if !blocker.detach().0 {
             return Err(ETIME);
         }

+ 3 - 1
h2o/kernel/src/cpu/time/chip.rs

@@ -1,5 +1,7 @@
 use archop::Azy;
 
+use core::sync::atomic::Ordering::Release;
+
 use super::Instant;
 use crate::{cpu::arch::tsc::TSC_CLOCK, dev::hpet::HPET_CLOCK};
 
@@ -8,7 +10,7 @@ pub static CLOCK: Azy<&'static dyn ClockChip> = Azy::new(|| {
         Some(ref tsc) => tsc,
         None => HPET_CLOCK.as_ref().expect("No available clock"),
     };
-    *crate::log::HAS_TIME.write() = true;
+    crate::log::HAS_TIME.store(true, Release);
     ret
 });
 

+ 11 - 6
h2o/kernel/src/cpu/time/timer.rs

@@ -23,10 +23,17 @@ impl TimerQueue {
         }
     }
 
+    #[inline]
+    fn with_inner<F, R>(&self, func: F) -> R
+    where
+        F: FnOnce(&mut LinkedList<Arsc<Timer>>) -> R,
+    {
+        PREEMPT.scope(|| func(unsafe { &mut *self.inner.get() }))
+    }
+
     fn push(&self, timer: Arsc<Timer>) {
         let ddl = timer.deadline;
-        PREEMPT.scope(|| {
-            let queue = unsafe { &mut *self.inner.get() };
+        self.with_inner(|queue| {
             let mut cur = queue.cursor_front_mut();
             loop {
                 match cur.current() {
@@ -45,8 +52,7 @@ impl TimerQueue {
     }
 
     fn pop(&self, timer: &Arsc<Timer>) -> bool {
-        PREEMPT.scope(|| {
-            let queue = unsafe { &mut *self.inner.get() };
+        self.with_inner(|queue| {
             let mut cur = queue.cursor_front_mut();
             loop {
                 match cur.current() {
@@ -153,8 +159,7 @@ impl Timer {
 
 pub unsafe fn tick() {
     let now = Instant::now();
-    PREEMPT.scope(|| {
-        let queue = unsafe { &mut *TIMER_QUEUE.inner.get() };
+    TIMER_QUEUE.with_inner(|queue| {
         let mut cur = queue.cursor_front_mut();
         loop {
             match cur.current() {

+ 10 - 7
h2o/kernel/src/log.rs

@@ -1,10 +1,13 @@
 pub mod flags;
 mod serial;
 
-use core::{fmt::*, mem::MaybeUninit};
+use core::{
+    fmt::*,
+    mem::MaybeUninit,
+    sync::atomic::{AtomicBool, Ordering::*},
+};
 
-use archop::IntrMutex;
-use spin::RwLock;
+use spin::Mutex;
 
 pub use self::serial::COM_LOG;
 use crate::{cpu::time::Instant, sched::PREEMPT};
@@ -21,17 +24,17 @@ impl core::fmt::Display for OptionU32Display {
     }
 }
 
-pub static HAS_TIME: RwLock<bool> = RwLock::new(false);
+pub static HAS_TIME: AtomicBool = AtomicBool::new(false);
 
 struct Logger {
-    output: IntrMutex<serial::Output>,
+    output: Mutex<serial::Output>,
     level: log::Level,
 }
 
 impl Logger {
     pub fn new(level: log::Level) -> Logger {
         Logger {
-            output: IntrMutex::new(unsafe { serial::Output::new(COM_LOG) }),
+            output: Mutex::new(unsafe { serial::Output::new(COM_LOG) }),
             level,
         }
     }
@@ -51,7 +54,7 @@ impl log::Log for Logger {
         let _pree = PREEMPT.lock();
         let mut os = self.output.lock();
         let cur_time = HAS_TIME
-            .read()
+            .load(Acquire)
             .then(Instant::now)
             .unwrap_or(unsafe { Instant::from_raw(0) });
 

+ 5 - 5
h2o/kernel/src/sched/imp.rs

@@ -369,7 +369,8 @@ fn select_cpu(
     }
 
     for b in iter {
-        if b == cur_cpu && SCHED_INFO[b].expected_runtime() == 0 {
+        let rb = SCHED_INFO[b].expected_runtime();
+        if b == cur_cpu && rb == 0 {
             return Some(b);
         }
 
@@ -390,8 +391,7 @@ fn select_cpu(
         };
 
         let wruntime = {
-            let ra = SCHED_INFO[a].expected_runtime.load(Acquire);
-            let rb = SCHED_INFO[b].expected_runtime.load(Acquire);
+            let ra = SCHED_INFO[a].expected_runtime();
             let diff = ra.abs_diff(rb);
             if diff <= 1 {
                 0
@@ -400,7 +400,7 @@ fn select_cpu(
             }
         };
 
-        let weight = wlast_cpu * 10 + wcur_cpu * 2 + wruntime * 50;
+        let weight = wlast_cpu * 10 + wcur_cpu * 2 + wruntime * 20;
 
         ret = if weight > 0 { a } else { b };
     }
@@ -408,7 +408,7 @@ fn select_cpu(
     Some(ret)
 }
 
-fn block_callback(_: Arsc<Timer>, _: Instant, arg: CallbackArg) {
+fn block_callback(arg: CallbackArg) {
     let blocked = unsafe { Box::from_raw(arg.as_ptr()) };
     SCHED.unblock(Box::into_inner(blocked), true);
 }

+ 7 - 3
h2o/kernel/src/sched/imp/waiter.rs

@@ -1,6 +1,7 @@
 use alloc::sync::{Arc, Weak};
 use core::{fmt::Debug, time::Duration};
 
+use archop::PreemptStateGuard;
 use spin::Mutex;
 use sv_call::Feature;
 
@@ -32,13 +33,16 @@ impl Blocker {
         ret
     }
 
-    pub fn wait<T>(&self, guard: T, timeout: Duration) -> sv_call::Result {
-        let pree = PREEMPT.lock();
+    pub fn wait(&self, pree: Option<PreemptStateGuard>, timeout: Duration) -> sv_call::Result {
+        let pree = match pree {
+            Some(pree) => pree,
+            None => PREEMPT.lock(),
+        };
         let status = self.status.lock();
         if timeout.is_zero() || status.1 != 0 {
             Ok(())
         } else {
-            self.wo.wait((guard, status, pree), timeout, "Blocker::wait")
+            self.wo.wait((status, pree), timeout, "Blocker::wait")
         }
     }
 

+ 2 - 2
h2o/kernel/src/sched/ipc.rs

@@ -176,7 +176,7 @@ mod syscall {
         let event = obj.event().upgrade().ok_or(EPIPE)?;
 
         let blocker = Blocker::new(&event, wake_all, signal);
-        blocker.wait(pree, time::from_us(timeout_us))?;
+        blocker.wait(Some(pree), time::from_us(timeout_us))?;
 
         let (detach_ret, signal) = blocker.detach();
         if !detach_ret {
@@ -205,7 +205,7 @@ mod syscall {
         let cur = unsafe { (*SCHED.current()).as_ref().ok_or(ESRCH) }?;
 
         let blocker = cur.space().handles().get::<Blocker>(waiter)?;
-        blocker.wait(pree, time::from_us(timeout_us))?;
+        blocker.wait(Some(pree), time::from_us(timeout_us))?;
 
         let (detach_ret, signal) = Arc::clone(&blocker).detach();
         SCHED.with_current(|cur| cur.space().handles().remove::<Blocker>(waiter))?;

+ 1 - 1
h2o/kernel/src/sched/ipc/channel/syscall.rs

@@ -159,7 +159,7 @@ fn chan_crecv(
     } else {
         let pree = PREEMPT.lock();
         let blocker = crate::sched::Blocker::new(&call_event, true, SIG_READ);
-        blocker.wait(pree, time::from_us(timeout_us))?;
+        blocker.wait(Some(pree), time::from_us(timeout_us))?;
         Some(blocker)
     };
 

+ 1 - 1
h2o/kernel/src/sched/task/excep.rs

@@ -45,7 +45,7 @@ pub fn dispatch_exception(frame: &mut Frame, vec: ExVec) -> bool {
 
     let blocker =
         crate::sched::Blocker::new(&(Arc::clone(excep_chan.event()) as _), false, SIG_READ);
-    if blocker.wait((), Duration::MAX).is_err() {
+    if blocker.wait(None, Duration::MAX).is_err() {
         return false;
     }
     if !blocker.detach().0 {

+ 2 - 15
h2o/kernel/src/sched/task/sm.rs

@@ -16,7 +16,7 @@ use crate::{
 };
 
 #[derive(Debug, Builder)]
-#[builder(no_std)]
+#[builder(no_std, pattern = "owned")]
 pub struct TaskInfo {
     from: Option<Tid>,
     #[builder(setter(skip))]
@@ -77,14 +77,6 @@ impl TaskInfo {
     pub fn excep_chan(&self) -> Arsc<Mutex<Option<Channel>>> {
         Arsc::clone(&self.excep_chan)
     }
-
-    #[inline]
-    pub fn with_excep_chan<F, R>(&self, func: F) -> R
-    where
-        F: FnOnce(&mut Option<Channel>) -> R,
-    {
-        PREEMPT.scope(|| func(&mut self.excep_chan.lock()))
-    }
 }
 
 #[derive(Debug)]
@@ -210,12 +202,7 @@ impl IntoReady for Init {
 }
 
 impl Init {
-    pub fn new(
-        tid: Tid,
-        space: Arc<Space>,
-        kstack: ctx::Kstack,
-        ext_frame: ctx::ExtFrame,
-    ) -> Self {
+    pub fn new(tid: Tid, space: Arc<Space>, kstack: ctx::Kstack, ext_frame: ctx::ExtFrame) -> Self {
         Init {
             ctx: Box::new(Context {
                 tid,