better scheduler time allocation, proper quota tracking
This commit is contained in:
1 parent
03bf767ac2
commit
b3569a2b90
6 files changed
+362
-235
No files matched your search
+103
-12
@@ -10,8 +10,6 @@
|
||||
#include <ke_list.h>
|
||||
#include <mm_virt.h>
|
||||
|
||||
#define DEMOTION_QUANTA 4 /* ticks at a level before demotion */
|
||||
|
||||
static LIST_ENTRY gRunQueue[PriorityCount];
|
||||
static SPINLOCK gSchedLock;
|
||||
|
||||
@@ -22,6 +20,39 @@ static uint32_t gAgingCounter = 0;
|
||||
static volatile long gReschedulePending = 0;
|
||||
static KE_SCHED_STATS gSchedStats;
|
||||
|
||||
static uint32_t KiBaseQuantumForPriority(THREAD_PRIORITY priority) {
|
||||
switch (priority) {
|
||||
case PriorityHigh: return SCHED_HIGH_QUANTUM_TICKS;
|
||||
case PriorityNormal: return SCHED_NORMAL_QUANTUM_TICKS;
|
||||
case PriorityLow: return SCHED_LOW_QUANTUM_TICKS;
|
||||
default: return SCHED_NORMAL_QUANTUM_TICKS;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t KiComputeQuantumLocked(THREAD* thread) {
|
||||
if (!thread || thread == gIdleThread)
|
||||
return 1;
|
||||
|
||||
uint32_t quantum = KiBaseQuantumForPriority(thread->Priority);
|
||||
|
||||
// Low-priority CPU-bound threads get longer batches once they have proven
|
||||
// they mostly burn CPU. Higher-priority work still preempts them as soon
|
||||
// as it becomes ready, so this reduces switch overhead without hurting
|
||||
// interactive wake latency.
|
||||
if (thread->Priority == PriorityLow && thread->CpuBurstTicks >=
|
||||
(SCHED_DEMOTE_AFTER_BURST_TICKS * 2u)) {
|
||||
quantum += 4;
|
||||
}
|
||||
|
||||
return quantum ? quantum : 1;
|
||||
}
|
||||
|
||||
static void KiRefillQuantumLocked(THREAD* thread) {
|
||||
if (!thread) return;
|
||||
thread->QuantumTicks = KiComputeQuantumLocked(thread);
|
||||
thread->QuantumRemaining = thread->QuantumTicks;
|
||||
}
|
||||
|
||||
THREAD* KeGetCurrentThread(void) { return gCurrentThread; }
|
||||
void KeSetCurrentThread(THREAD* t) { gCurrentThread = t; }
|
||||
|
||||
@@ -55,6 +86,10 @@ void KeInitializeScheduler(void) {
|
||||
idle->State = ThreadReady;
|
||||
idle->Priority = PriorityLow;
|
||||
idle->QuantaConsumed = 0;
|
||||
idle->QuantumTicks = 1;
|
||||
idle->QuantumRemaining = 1;
|
||||
idle->CpuBurstTicks = 0;
|
||||
idle->LastRunTicks = 0;
|
||||
idle->Owner = idleProc;
|
||||
idle->WaitObject = (OBJECT *)0;
|
||||
idle->WaitStatus = STATUS_SUCCESS;
|
||||
@@ -119,6 +154,15 @@ void KeReadyThread(THREAD* thread) {
|
||||
|
||||
uintptr_t flags = KeAcquireSpinlock(&gSchedLock);
|
||||
ListInsertTail(&gRunQueue[p], &thread->SchedulerLink);
|
||||
|
||||
THREAD* cur = gCurrentThread;
|
||||
if (!cur || cur == gIdleThread) {
|
||||
KiRequestReschedule();
|
||||
} else if (cur->State == ThreadRunning && thread->Priority > cur->Priority) {
|
||||
gSchedStats.PriorityPreemptions++;
|
||||
KiRequestReschedule();
|
||||
}
|
||||
|
||||
KeReleaseSpinlock(&gSchedLock, flags);
|
||||
}
|
||||
|
||||
@@ -170,6 +214,8 @@ static void KiSchedule(void) {
|
||||
// lone CPU-bound thread gets alternated with the idle thread on every timer
|
||||
// tick, effectively turning a periodic preemption check into a 50% throttle.
|
||||
if (cur != gIdleThread && cur->State == ThreadRunning && !KiAnyRunnableLocked()) {
|
||||
if (cur->QuantumRemaining == 0)
|
||||
KiRefillQuantumLocked(cur);
|
||||
gSchedStats.KeepCurrentDecisions++;
|
||||
KeReleaseSpinlock(&gSchedLock, flags);
|
||||
return;
|
||||
@@ -188,6 +234,7 @@ static void KiSchedule(void) {
|
||||
}
|
||||
|
||||
next->State = ThreadRunning;
|
||||
KiRefillQuantumLocked(next);
|
||||
gCurrentThread = next;
|
||||
|
||||
// Only re-queue the outgoing thread if it is still in the running state.
|
||||
@@ -211,33 +258,76 @@ void KiScheduleFromInterrupt(void) {
|
||||
KiSchedule();
|
||||
}
|
||||
|
||||
void KiSchedulerNoteVoluntaryBlock(THREAD* thread) {
|
||||
if (!thread) return;
|
||||
uintptr_t flags = KeAcquireSpinlock(&gSchedLock);
|
||||
gSchedStats.VoluntaryBlocks++;
|
||||
thread->QuantumRemaining = 0;
|
||||
KeReleaseSpinlock(&gSchedLock, flags);
|
||||
}
|
||||
|
||||
void KeSchedulerTick(void) {
|
||||
THREAD* cur = gCurrentThread;
|
||||
if (!cur) return;
|
||||
|
||||
uintptr_t flags = KeAcquireSpinlock(&gSchedLock);
|
||||
cur->SchedLockFlags = flags; // Issue 5 fix
|
||||
cur->SchedLockFlags = flags;
|
||||
|
||||
gSchedStats.Ticks++;
|
||||
cur->QuantaConsumed++;
|
||||
|
||||
if (cur != gIdleThread && cur->QuantaConsumed >= DEMOTION_QUANTA) {
|
||||
cur->QuantaConsumed = 0;
|
||||
if (cur->Priority > PriorityLow) {
|
||||
cur->Priority = (THREAD_PRIORITY)(cur->Priority - 1);
|
||||
gSchedStats.Demotions++;
|
||||
int requestReschedule = 0;
|
||||
|
||||
if (cur == gIdleThread) {
|
||||
if (KiAnyRunnableLocked())
|
||||
requestReschedule = 1;
|
||||
} else {
|
||||
cur->CpuBurstTicks++;
|
||||
cur->QuantaConsumed = cur->CpuBurstTicks;
|
||||
|
||||
if (cur->CpuBurstTicks >= SCHED_DEMOTE_AFTER_BURST_TICKS) {
|
||||
cur->CpuBurstTicks = 0;
|
||||
cur->QuantaConsumed = 0;
|
||||
if (cur->Priority > PriorityLow) {
|
||||
cur->Priority = (THREAD_PRIORITY)(cur->Priority - 1);
|
||||
gSchedStats.Demotions++;
|
||||
gSchedStats.CpuBurstDemotions++;
|
||||
}
|
||||
}
|
||||
|
||||
if (cur->QuantumRemaining == 0)
|
||||
KiRefillQuantumLocked(cur);
|
||||
|
||||
if (cur->QuantumRemaining > 0)
|
||||
cur->QuantumRemaining--;
|
||||
|
||||
if (cur->QuantumRemaining == 0) {
|
||||
if (KiAnyRunnableLocked()) {
|
||||
gSchedStats.QuantumExpirations++;
|
||||
requestReschedule = 1;
|
||||
} else {
|
||||
// No peer is ready, so keep the CPU hot and refill the budget.
|
||||
// This avoids reintroducing the old every-other-tick idle dance
|
||||
// as a pointless reschedule-pending chatter loop.
|
||||
KiRefillQuantumLocked(cur);
|
||||
gSchedStats.KeepCurrentDecisions++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Periodic aging pass to prevent indefinite starvation of PriorityLow threads
|
||||
// Periodic aging pass to prevent indefinite starvation of PriorityLow and
|
||||
// PriorityNormal threads. Ready threads promoted by aging get a fresh CPU
|
||||
// burst history; they earned the boost by waiting, not by burning CPU.
|
||||
gAgingCounter++;
|
||||
if (gAgingCounter >= 100) {
|
||||
if (gAgingCounter >= SCHED_AGING_INTERVAL_TICKS) {
|
||||
gAgingCounter = 0;
|
||||
for (int p = 0; p < PriorityHigh; p++) {
|
||||
while (!LIST_IS_EMPTY(&gRunQueue[p])) {
|
||||
LIST_ENTRY* node = ListPopHead(&gRunQueue[p]);
|
||||
THREAD* t = CONTAINING_RECORD(node, THREAD, SchedulerLink);
|
||||
t->Priority = PriorityHigh;
|
||||
t->CpuBurstTicks = 0;
|
||||
t->QuantaConsumed = 0;
|
||||
t->QuantumRemaining = 0;
|
||||
gSchedStats.AgingPromotions++;
|
||||
ListInsertTail(&gRunQueue[PriorityHigh], &t->SchedulerLink);
|
||||
}
|
||||
@@ -246,7 +336,8 @@ void KeSchedulerTick(void) {
|
||||
|
||||
KeReleaseSpinlock(&gSchedLock, flags);
|
||||
|
||||
KiRequestReschedule();
|
||||
if (requestReschedule)
|
||||
KiRequestReschedule();
|
||||
}
|
||||
|
||||
__declspec(dllexport) void KeYield(void) {
|
||||
|
||||
+15
-3
@@ -3,7 +3,12 @@
|
||||
// Three run queues (Low / Normal / High). IRQ 0 drives KeSchedulerTick.
|
||||
#include <ke_thread.h>
|
||||
|
||||
#define QUANTUM_MS 10 // base time slice in milliseconds
|
||||
#define SCHED_TICK_MS 10u
|
||||
#define SCHED_HIGH_QUANTUM_TICKS 2u
|
||||
#define SCHED_NORMAL_QUANTUM_TICKS 4u
|
||||
#define SCHED_LOW_QUANTUM_TICKS 8u
|
||||
#define SCHED_DEMOTE_AFTER_BURST_TICKS 16u
|
||||
#define SCHED_AGING_INTERVAL_TICKS 100u
|
||||
|
||||
typedef struct KE_SCHED_STATS {
|
||||
uint64_t Ticks;
|
||||
@@ -12,6 +17,10 @@ typedef struct KE_SCHED_STATS {
|
||||
uint64_t IdleSelections;
|
||||
uint64_t Demotions;
|
||||
uint64_t AgingPromotions;
|
||||
uint64_t QuantumExpirations;
|
||||
uint64_t PriorityPreemptions;
|
||||
uint64_t VoluntaryBlocks;
|
||||
uint64_t CpuBurstDemotions;
|
||||
} KE_SCHED_STATS;
|
||||
|
||||
// Called once by KernelMain before any threads are created.
|
||||
@@ -21,8 +30,10 @@ void KeInitializeScheduler(void);
|
||||
void KeReadyThread(THREAD* thread);
|
||||
void KeUnreadyThread(THREAD* thread);
|
||||
|
||||
// Called from the timer ISR every QUANTUM_MS. This only performs timer
|
||||
// accounting and requests a reschedule; the actual context switch happens from
|
||||
// Called from the timer ISR every SCHED_TICK_MS. This performs timer
|
||||
// accounting and requests a reschedule only when the current thread has
|
||||
// exhausted its scheduler budget, or when idle work becomes available. The
|
||||
// actual context switch happens from
|
||||
// KiExitInterrupt after the IRQ handler has returned and EOI has been sent.
|
||||
void KeSchedulerTick(void);
|
||||
|
||||
@@ -32,6 +43,7 @@ void KeSchedulerTick(void);
|
||||
void KiRequestReschedule(void);
|
||||
int KiIsReschedulePending(void);
|
||||
void KiScheduleFromInterrupt(void);
|
||||
void KiSchedulerNoteVoluntaryBlock(THREAD* thread);
|
||||
|
||||
// Voluntarily yield the CPU. Picks the next runnable thread immediately.
|
||||
__declspec(dllexport) void KeYield(void);
|
||||
|
||||
+16
-1
@@ -87,6 +87,10 @@ __declspec(dllexport) KSTATUS KeCreateThread(PROCESS* owner, uintptr_t entry, ui
|
||||
thread->State = ThreadReady;
|
||||
thread->Priority = priority;
|
||||
thread->QuantaConsumed = 0;
|
||||
thread->QuantumTicks = 0;
|
||||
thread->QuantumRemaining = 0;
|
||||
thread->CpuBurstTicks = 0;
|
||||
thread->LastRunTicks = 0;
|
||||
thread->WaitObject = (OBJECT *)0;
|
||||
thread->WaitStatus = STATUS_SUCCESS;
|
||||
thread->WaitListHead = (LIST_ENTRY*)0;
|
||||
@@ -241,6 +245,10 @@ __declspec(dllexport) KSTATUS KeCreateKernelThread(uintptr_t fn, uintptr_t arg,
|
||||
thread->State = ThreadReady;
|
||||
thread->Priority = priority;
|
||||
thread->QuantaConsumed = 0;
|
||||
thread->QuantumTicks = 0;
|
||||
thread->QuantumRemaining = 0;
|
||||
thread->CpuBurstTicks = 0;
|
||||
thread->LastRunTicks = 0;
|
||||
thread->WaitObject = (OBJECT*)0;
|
||||
thread->WaitStatus = STATUS_SUCCESS;
|
||||
thread->WaitListHead = (LIST_ENTRY*)0;
|
||||
@@ -344,6 +352,7 @@ KSTATUS KeCommitBlockThread(THREAD* thread, uint8_t alertable) {
|
||||
return STATUS_USER_APC;
|
||||
}
|
||||
|
||||
KiSchedulerNoteVoluntaryBlock(thread);
|
||||
KeYield();
|
||||
|
||||
// Kernel APCs are delivered before an alertable wait returns
|
||||
@@ -369,6 +378,9 @@ void KeUnblockThreadWithStatus(THREAD* thread, KSTATUS status) {
|
||||
// instead of inserting the running thread into the ready queue.
|
||||
if (thread == KeGetCurrentThread()) {
|
||||
KiFinishWait(thread, status);
|
||||
thread->CpuBurstTicks = 0;
|
||||
thread->QuantaConsumed = 0;
|
||||
thread->QuantumRemaining = 0;
|
||||
if (thread->State == ThreadWaiting)
|
||||
thread->State = ThreadRunning;
|
||||
return;
|
||||
@@ -385,9 +397,12 @@ void KeUnblockThreadWithStatus(THREAD* thread, KSTATUS status) {
|
||||
KiFinishWait(thread, status);
|
||||
thread->State = ThreadReady;
|
||||
|
||||
thread->CpuBurstTicks = 0;
|
||||
thread->QuantaConsumed = 0;
|
||||
thread->QuantumRemaining = 0;
|
||||
|
||||
if (thread->Priority < PriorityHigh) {
|
||||
thread->Priority = (THREAD_PRIORITY)(thread->Priority + 1);
|
||||
thread->QuantaConsumed = 0;
|
||||
}
|
||||
|
||||
KeReadyThread(thread);
|
||||
|
||||
+10
-1
@@ -41,7 +41,8 @@ typedef struct THREAD {
|
||||
THREAD_STATE State;
|
||||
THREAD_PRIORITY Priority;
|
||||
|
||||
// Quanta consumed without blocking - used by the MLFQ for demotion.
|
||||
// CPU-burst mirror kept here for the assembly-stable THREAD layout.
|
||||
// Dynamic quantum state lives later in the structure.
|
||||
uint32_t QuantaConsumed;
|
||||
|
||||
PROCESS* Owner;
|
||||
@@ -80,6 +81,14 @@ typedef struct THREAD {
|
||||
void* FpuContext;
|
||||
uint8_t FpuStorage[528];
|
||||
|
||||
// Scheduler accounting. QuantaConsumed is kept in the assembly-stable
|
||||
// portion of THREAD for layout compatibility; the dynamic scheduler uses
|
||||
// these fields for per-dispatch budget and CPU-burst tracking.
|
||||
uint32_t QuantumTicks;
|
||||
uint32_t QuantumRemaining;
|
||||
uint32_t CpuBurstTicks;
|
||||
uint32_t LastRunTicks;
|
||||
|
||||
// Set by wait primitives before blocking and completed by the object
|
||||
// that wakes the thread. Keep this field after the assembly-visible
|
||||
// context-switch fields: ke_switch.asm hard-codes offsets through
|
||||
|
||||
Reference in new issue
Block a user