소스 검색

Kernel: Move Scheduler current time method to the TimeManagement code

Liav A 2 년 전
부모
커밋
7520acd4eb
5개의 변경된 파일29개의 추가작업 그리고 22개의 파일을 삭제
  1. 2 2
      Kernel/Arch/x86/common/Processor.cpp
  2. 3 19
      Kernel/Scheduler.cpp
  3. 0 1
      Kernel/Scheduler.h
  4. 22 0
      Kernel/Time/TimeManagement.cpp
  5. 2 0
      Kernel/Time/TimeManagement.h

+ 2 - 2
Kernel/Arch/x86/common/Processor.cpp

@@ -928,7 +928,7 @@ void Processor::enter_trap(TrapFrame& trap, bool raise_irq)
         // The cs register of this trap tells us where we will return back to
         auto new_previous_mode = ((trap.regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode;
         if (current_thread->set_previous_mode(new_previous_mode) && trap.prev_irq_level == 0) {
-            current_thread->update_time_scheduled(Scheduler::current_time(), new_previous_mode == Thread::PreviousMode::KernelMode, false);
+            current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), new_previous_mode == Thread::PreviousMode::KernelMode, false);
         }
     } else {
         trap.next_trap = nullptr;
@@ -975,7 +975,7 @@ void Processor::exit_trap(TrapFrame& trap)
         }
 
         if (current_thread->set_previous_mode(new_previous_mode))
-            current_thread->update_time_scheduled(Scheduler::current_time(), true, false);
+            current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), true, false);
     }
 
     VERIFY_INTERRUPTS_DISABLED();

+ 3 - 19
Kernel/Scheduler.cpp

@@ -8,7 +8,6 @@
 #include <AK/ScopeGuard.h>
 #include <AK/Singleton.h>
 #include <AK/Time.h>
-#include <Kernel/Arch/CurrentTime.h>
 #include <Kernel/Arch/InterruptDisabler.h>
 #include <Kernel/Arch/x86/TrapFrame.h>
 #include <Kernel/Debug.h>
@@ -51,10 +50,6 @@ static Singleton<SpinlockProtected<ThreadReadyQueues>> g_ready_queues;
 
 static SpinlockProtected<TotalTimeScheduled> g_total_time_scheduled { LockRank::None };
 
-// The Scheduler::current_time function provides a current time for scheduling purposes,
-// which may not necessarily relate to wall time
-u64 (*Scheduler::current_time)();
-
 static void dump_thread_list(bool = false);
 
 static inline u32 thread_priority_to_priority_index(u32 thread_priority)
@@ -309,7 +304,7 @@ void Scheduler::enter_current(Thread& prev_thread)
     VERIFY(g_scheduler_lock.is_locked_by_current_processor());
 
     // We already recorded the scheduled time when entering the trap, so this merely accounts for the kernel time since then
-    auto scheduler_time = Scheduler::current_time();
+    auto scheduler_time = TimeManagement::scheduler_current_time();
     prev_thread.update_time_scheduled(scheduler_time, true, true);
     auto* current_thread = Thread::current();
     current_thread->update_time_scheduled(scheduler_time, true, false);
@@ -366,21 +361,10 @@ Process* Scheduler::colonel()
     return s_colonel_process;
 }
 
-static u64 current_time_monotonic()
-{
-    // We always need a precise timestamp here, we cannot rely on a coarse timestamp
-    return (u64)TimeManagement::the().monotonic_time(TimePrecision::Precise).to_nanoseconds();
-}
-
 UNMAP_AFTER_INIT void Scheduler::initialize()
 {
     VERIFY(Processor::is_initialized()); // sanity check
-
-    auto* possible_arch_specific_current_time_function = optional_current_time();
-    if (possible_arch_specific_current_time_function)
-        current_time = possible_arch_specific_current_time_function;
-    else
-        current_time = current_time_monotonic;
+    VERIFY(TimeManagement::is_initialized());
 
     LockRefPtr<Thread> idle_thread;
     g_finalizer_wait_queue = new WaitQueue;
@@ -440,7 +424,7 @@ void Scheduler::timer_tick(RegisterState const& regs)
         // Because the previous mode when entering/exiting kernel threads never changes
         // we never update the time scheduled. So we need to update it manually on the
         // timer interrupt
-        current_thread->update_time_scheduled(current_time(), true, false);
+        current_thread->update_time_scheduled(TimeManagement::scheduler_current_time(), true, false);
     }
 
     if (current_thread->previous_mode() == Thread::PreviousMode::UserMode && current_thread->should_die() && !current_thread->is_blocked()) {

+ 0 - 1
Kernel/Scheduler.h

@@ -55,7 +55,6 @@ public:
     static bool is_initialized();
     static TotalTimeScheduled get_total_time_scheduled();
     static void add_time_scheduled(u64, bool);
-    static u64 (*current_time)();
 };
 
 }

+ 22 - 0
Kernel/Time/TimeManagement.cpp

@@ -17,6 +17,7 @@
 #    include <Kernel/Arch/x86/common/Interrupts/APIC.h>
 #    include <Kernel/Arch/x86/common/RTC.h>
 #endif
+#include <Kernel/Arch/CurrentTime.h>
 #include <Kernel/CommandLine.h>
 #include <Kernel/Firmware/ACPI/Parser.h>
 #include <Kernel/PerformanceManager.h>
@@ -40,6 +41,22 @@ TimeManagement& TimeManagement::the()
     return *s_the;
 }
 
+// The s_scheduler_specific_current_time function provides a current time for scheduling purposes,
+// which may not necessarily relate to wall time
+static u64 (*s_scheduler_current_time)();
+
+static u64 current_time_monotonic()
+{
+    // We always need a precise timestamp here, we cannot rely on a coarse timestamp
+    return (u64)TimeManagement::the().monotonic_time(TimePrecision::Precise).to_nanoseconds();
+}
+
+u64 TimeManagement::scheduler_current_time()
+{
+    VERIFY(s_scheduler_current_time);
+    return s_scheduler_current_time();
+}
+
 ErrorOr<void> TimeManagement::validate_clock_id(clockid_t clock_id)
 {
     switch (clock_id) {
@@ -163,6 +180,11 @@ UNMAP_AFTER_INIT void TimeManagement::initialize([[maybe_unused]] u32 cpu)
             apic_timer->enable_local_timer();
         }
     }
+    auto* possible_arch_specific_current_time_function = optional_current_time();
+    if (possible_arch_specific_current_time_function)
+        s_scheduler_current_time = possible_arch_specific_current_time_function;
+    else
+        s_scheduler_current_time = current_time_monotonic;
 #endif
 }
 

+ 2 - 0
Kernel/Time/TimeManagement.h

@@ -37,6 +37,8 @@ public:
     static bool is_initialized();
     static TimeManagement& the();
 
+    static u64 scheduler_current_time();
+
     static ErrorOr<void> validate_clock_id(clockid_t);
     Time current_time(clockid_t) const;
     Time monotonic_time(TimePrecision = TimePrecision::Coarse) const;