From 5f51d851840d889520c21cc4359d6db59d83ff06 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 3 Dec 2020 22:12:50 -0700 Subject: Kernel: Improve time keeping and dramatically reduce interrupt load This implements a number of changes related to time: * If a HPET is present, it is now used only as a system timer, unless the Local APIC timer is used (in which case the HPET timer will not trigger any interrupts at all). * If a HPET is present, the current time can now be as accurate as the chip can be, independently from the system timer. We now query the HPET main counter for the current time in CPU #0's system timer interrupt, and use that as a base line. If a high precision time is queried, that base line is used in combination with quering the HPET timer directly, which should give a much more accurate time stamp at the expense of more overhead. For faster time stamps, the more coarse value based on the last interrupt will be returned. This also means that any missed interrupts should not cause the time to drift. * The default system interrupt rate is reduced to about 250 per second. * Fix calculation of Thread CPU usage by using the amount of ticks they used rather than the number of times a context switch happened. * Implement CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE and use it for most cases where precise timestamps are not needed. --- Kernel/Scheduler.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Kernel/Scheduler.cpp') diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index 41395e311c..7283630f97 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -64,10 +64,10 @@ void Scheduler::init_thread(Thread& thread) static u32 time_slice_for(const Thread& thread) { - // One time slice unit == 1ms + // One time slice unit == 4ms (assuming 250 ticks/second) if (&thread == Processor::current().idle_thread()) return 1; - return 10; + return 2; } Thread* g_finalizer; @@ -219,6 +219,7 @@ bool Scheduler::pick_next() // but since we're still holding the scheduler lock we're still in a critical section critical.leave(); + thread_to_schedule->set_ticks_left(time_slice_for(*thread_to_schedule)); return context_switch(thread_to_schedule); } @@ -317,7 +318,6 @@ bool Scheduler::donate_to(RefPtr& beneficiary, const char* reason) bool Scheduler::context_switch(Thread* thread) { - thread->set_ticks_left(time_slice_for(*thread)); thread->did_schedule(); auto from_thread = Thread::current(); @@ -480,7 +480,7 @@ void Scheduler::timer_tick(const RegisterState& regs) } } - if (current_thread->tick()) + if (current_thread->tick((regs.cs & 3) == 0)) return; ASSERT_INTERRUPTS_DISABLED(); -- cgit v1.2.3