feat: various power and thermal optimizations, fix Printers app regression

This commit is contained in:
2026-04-20 16:00:52 +02:00
parent ea4e3dbf96
commit 7d6f001659
10 changed files with 250 additions and 65 deletions
+46 -20
View File
@@ -443,6 +443,46 @@ namespace Sched {
schedLock.Release();
}
bool HasReadyProcesses() {
return readyCount > 0;
}
void RunBspMaintenance() {
schedLock.Acquire();
uint64_t now = Timekeeping::GetTicks();
for (int i = 0; i < MaxProcesses; i++) {
if (processTable[i].state == ProcessState::Blocked &&
processTable[i].sleepUntilTick != 0 &&
now >= processTable[i].sleepUntilTick) {
processTable[i].sleepUntilTick = 0;
processTable[i].waitingForPid = -1;
processTable[i].waitingOnObject = nullptr;
processTable[i].state = ProcessState::Ready;
readyCount++;
}
}
schedLock.Release();
ReclaimTerminated();
}
uint64_t GetNextDeadlineTick() {
uint64_t nextDeadline = 0;
schedLock.Acquire();
for (int i = 0; i < MaxProcesses; i++) {
if (processTable[i].state != ProcessState::Blocked) continue;
uint64_t deadline = processTable[i].sleepUntilTick;
if (deadline == 0) continue;
if (nextDeadline == 0 || deadline < nextDeadline) {
nextDeadline = deadline;
}
}
schedLock.Release();
return nextDeadline;
}
void Schedule() {
auto* cpu = Smp::GetCurrentCpuData();
@@ -532,28 +572,12 @@ namespace Sched {
schedLock.Release();
}
void Tick() {
void Tick(uint32_t elapsedMs) {
auto* cpu = Smp::GetCurrentCpuData();
// BSP: wake sleeping processes and reclaim terminated slots
if (cpu->cpuIndex == 0) {
schedLock.Acquire();
uint64_t now = Timekeeping::GetTicks();
for (int i = 0; i < MaxProcesses; i++) {
if (processTable[i].state == ProcessState::Blocked &&
processTable[i].sleepUntilTick != 0 &&
now >= processTable[i].sleepUntilTick) {
processTable[i].sleepUntilTick = 0;
processTable[i].waitingForPid = -1;
processTable[i].waitingOnObject = nullptr;
processTable[i].state = ProcessState::Ready;
readyCount++;
}
}
schedLock.Release();
// Reclaim terminated process memory (BSP only, once per tick)
ReclaimTerminated();
RunBspMaintenance();
}
int slot = cpu->currentSlot;
@@ -577,8 +601,10 @@ namespace Sched {
return;
}
if (processTable[slot].sliceRemaining > 0) {
processTable[slot].sliceRemaining--;
if (processTable[slot].sliceRemaining > elapsedMs) {
processTable[slot].sliceRemaining -= elapsedMs;
} else {
processTable[slot].sliceRemaining = 0;
}
if (processTable[slot].sliceRemaining == 0) {
+15 -3
View File
@@ -41,7 +41,7 @@ namespace Sched {
uint64_t savedRsp;
uint64_t stackBase; // Bottom of allocated kernel stack (lowest address)
uint64_t entryPoint;
uint64_t sliceRemaining; // Ticks left in current time slice
uint64_t sliceRemaining; // Milliseconds left in current time slice
uint64_t pml4Phys; // Physical address of per-process PML4
uint64_t kernelStackTop; // Top of kernel stack (for TSS RSP0 / SYSCALL)
uint64_t userStackTop; // User-space stack top
@@ -87,8 +87,12 @@ namespace Sched {
int Spawn(const char* vfsPath, const char* args = nullptr);
void Schedule();
// Called from the APIC timer handler on every tick (per-CPU).
void Tick();
// True when there is runnable work somewhere in the process table.
bool HasReadyProcesses();
// Called from the APIC timer handler with the elapsed time for that CPU's
// tick interval. The BSP runs at 1 ms; APs may use a coarser interval.
void Tick(uint32_t elapsedMs = 1);
// Get the PID of the currently running process (-1 if idle)
int GetCurrentPid();
@@ -112,6 +116,14 @@ namespace Sched {
// timeoutMs == 0 means wait indefinitely.
void BlockOnObject(void* object, uint64_t timeoutMs = 0);
// BSP-only scheduler housekeeping: wake expired sleepers and reclaim
// terminated process resources.
void RunBspMaintenance();
// Return the earliest blocked sleep/object timeout deadline in ticks,
// or 0 when no timed waits are pending.
uint64_t GetNextDeadlineTick();
// Wake any processes blocked on the given object.
void WakeObjectWaiters(void* object);