1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/kernel/ps/sched.c
2019-04-21 21:18:13 +02:00

422 lines
12 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Process scheduler //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <extras/list.h>
#include <kernel/proc.h>
#include <kernel/sched.h>
#include <kernel/iomisc.h>
//
// For test purpose only
//
int procslen = 10;
Process_t procs[] = {
{ 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 4, 3, 3, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
{ 9, 2, 2, 21, 21, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL },
};
//------------------------------------------//
#define ReSchedFlag (KeCurCPU->needReSched)
#define PreemptCount (KeCurCPU->preemptCount)
#define IdlePrioProcs (KeCurCPU->idlePrioProcs)
#define ReglPrioProcs (KeCurCPU->reglPrioProcs)
#define ServPrioProcs (KeCurCPU->servPrioProcs)
#define TimeCritProcs (KeCurCPU->timeCritProcs)
//------------------------------------------//
//
// Set current process
//
static void SetCurProc(Process_t *proc)
{
PsCurProc = proc;
if (PsCurProc) PsCurProc->procState = STATE_RUNNING;
}
//
// (Un)Lock priority class list heads
//
static inline
void PsLockSched(void) {
KeDisableIRQs();
}
static inline
void PsUnlockSched(void) {
//KeEnableIRQs();
}
//
// The four priority classes of OS/2
//
const char *PsPrioClassesNames[] = {
"Time-critical class",
"Server priority class",
"Regular priority class",
"Idle priority class",
};
//
// Get priority class list head
//
static ListHead_t *GetPrioClassHead(int prioClass)
{
switch (prioClass) {
case TIME_CRIT_PROC: return TimeCritProcs;
case SERV_PRIO_PROC: return ServPrioProcs;
case REGL_PRIO_PROC: return ReglPrioProcs;
case IDLE_PRIO_PROC: return IdlePrioProcs;
default: KalAssert(FALSE && "Unknown priority class");
}
return NULL;
}
//
// Determine which process is going to run first
// Return NULL for "equal" processes
//
static Process_t *CompareProcs(Process_t *proc1, Process_t *proc2)
{
KalAssert(proc1 && proc2);
if (proc1->prioClass < proc2->prioClass) return proc1;
if (proc1->prioClass > proc2->prioClass) return proc2;
if (proc1->prioLevel > proc2->prioLevel) return proc1;
if (proc1->prioLevel < proc2->prioLevel) return proc2;
return NULL; // same class and level
}
//
// Add process to schedule lists (unlocked)
//
static void SchedThisProcUnlocked(Process_t *proc)
{
KalAssert(proc && proc->procState == STATE_RUNNABLE && !proc->schedNode);
bool found = 0;
ListNode_t *iterNode = NULL;
ListNode_t *procNode = ExCreateNode(proc);
ListHead_t *head = GetPrioClassHead(proc->prioClass);
KalAssert(head);
KalAssert(procNode);
proc->schedNode = procNode;
// Find a process with lesser priority
for (iterNode = head->first; iterNode; iterNode = iterNode->next) {
if (proc->prioLevel > ExGetNodeData(iterNode, Process_t *)->prioLevel) {
// Detect double insertions
KalAssert(proc->pid != ExGetNodeData(iterNode, Process_t *)->pid);
// Add process to schedule
ExAddNodeBefore(head, iterNode, procNode);
found = true;
break;
}
}
// Didn't find any process with lesser priority
if (found == false) {
ExAppendNode(head, procNode);
}
}
//
// Add process to schedule lists
//
void PsSchedThisProc(Process_t *proc)
{
PsLockSched();
SchedThisProcUnlocked(proc);
PsUnlockSched();
}
//
// Selects process to schedule next
//
static Process_t *SelectSchedNext(void)
{
if (TimeCritProcs->length > 0)
return ExGetNodeData(TimeCritProcs->first, Process_t *);
if (ServPrioProcs->length > 0)
return ExGetNodeData(ServPrioProcs->first, Process_t *);
if (ReglPrioProcs->length > 0)
return ExGetNodeData(ReglPrioProcs->first, Process_t *);
if (IdlePrioProcs->length > 0)
return ExGetNodeData(IdlePrioProcs->first, Process_t *);
return NULL;
}
//
// Remove running process from schedule lists
// and schedule next runnable process
//
void PsBlockCurProc(void)
{
KalAssert(PsCurProc && PsCurProc->procState == STATE_RUNNING);
ListNode_t *procNode = PsCurProc->schedNode;
KalAssert(procNode && "Blocking non-scheduled process");
PsCurProc->procState = STATE_BLOCKED;
ExRemoveNode(procNode->head, procNode);
PsCurProc->schedNode = NULL;
SetCurProc(SelectSchedNext());
}
static void ReSchedCurProc(void)
{
KalAssert(PsCurProc && PsCurProc->procState == STATE_RUNNING);
KalAssert(PsCurProc->schedNode);
// Restore default attributes, cancelling boosts
PsCurProc->prioClass = PsCurProc->defPrioClass;
PsCurProc->prioLevel = PsCurProc->defPrioLevel;
PsCurProc->timeSlice = PsCurProc->defTimeSlice;
PsCurProc->procState = STATE_RUNNABLE;
// Remove from list
ExRemoveNode(PsCurProc->schedNode->head, PsCurProc->schedNode);
PsCurProc->schedNode = NULL;
// Schedule again, with default attributes now
SchedThisProcUnlocked(PsCurProc);
}
//
// Should we schedule another process?
// Called at each tick
//
void PsSchedOnTick(void)
{
PsLockSched();
Process_t *procNext, *winner, *previous = PsCurProc;
// We're either idle or running something
KalAssert(PsCurProc == NULL || PsCurProc->procState == STATE_RUNNING);
// Have the current process spent its timeslice?
// (To be handled in CPU decisions function)
if (PsCurProc != NULL) {
if (PsCurProc->timeSlice <= 1) {
// Re-schedule
ReSchedCurProc();
// See next 'if' statement
PsCurProc = NULL;
}
// Otherwise, make it lose a tick
else {
PsCurProc->timeSlice--;
}
}
// Are we idle, or scheduling next process?
if (PsCurProc == NULL) {
SetCurProc(SelectSchedNext());
goto leave;
}
// Is preemption on and a re-schedule is needed?
if (PreemptCount == PREEMPT_ON && ReSchedFlag) {
// Is there a higher priority process that is runnable?
procNext = SelectSchedNext();
winner = CompareProcs(PsCurProc, procNext);
// Yes, procNext should preempt current process
if (winner == procNext) {
// Re-schedule
ReSchedCurProc();
// Switch to procNext
SetCurProc(procNext);
}
}
// Current process won't be preempted and has time remaining
leave:
PsUnlockSched();
if (PsCurProc != NULL && PsCurProc != previous) {
// dispatch & context switch
}
}
//
// Initialize scheduler
//
void PsInitSched(void)
{
int pid;
PsLockSched();
TimeCritProcs = ExCreateListHead();
ServPrioProcs = ExCreateListHead();
ReglPrioProcs = ExCreateListHead();
IdlePrioProcs = ExCreateListHead();
KalAssert(IdlePrioProcs && ReglPrioProcs && ServPrioProcs && TimeCritProcs);
for (pid = 0; pid < procslen; pid++) {
if (procs[pid].procState == STATE_RUNNABLE) {
SchedThisProcUnlocked(&procs[pid]);
}
}
PsUnlockSched();
}
//
// Shutdowns scheduler
//
void PsFiniSched(void)
{
KalAssert(IdlePrioProcs && ReglPrioProcs && ServPrioProcs && TimeCritProcs);
PsLockSched();
while (IdlePrioProcs->length > 0)
ExRemoveNode(IdlePrioProcs, IdlePrioProcs->first);
while (ReglPrioProcs->length > 0)
ExRemoveNode(ReglPrioProcs, ReglPrioProcs->first);
while (ServPrioProcs->length > 0)
ExRemoveNode(ServPrioProcs, ServPrioProcs->first);
while (TimeCritProcs->length > 0)
ExRemoveNode(TimeCritProcs, TimeCritProcs->first);
ExDestroyListHead(IdlePrioProcs); IdlePrioProcs = NULL;
ExDestroyListHead(ReglPrioProcs); ReglPrioProcs = NULL;
ExDestroyListHead(ServPrioProcs); ServPrioProcs = NULL;
ExDestroyListHead(TimeCritProcs); TimeCritProcs = NULL;
PsUnlockSched();
}
#define PrintProc(proc) KernLog("{ %d, '%s', %d , %lu}\n", (proc)->pid, \
PsPrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice);
//
// Print out process list
//
void PrintList(ListHead_t *head)
{
KalAssert(head);
Process_t *proc;
ListNode_t *node = head->first;
KernLog("len: %lu\n", head->length);
while (node) {
proc = ExGetNodeData(node, Process_t *);
PrintProc(proc);
node = node->next;
}
KernLog("");
}
void pstest(void)
{
//ClearTerm(StdOut);
KernLog("\nTime Critical: ");
PrintList(TimeCritProcs);
KernLog("\nServer: ");
PrintList(ServPrioProcs);
KernLog("\nRegular: ");
PrintList(ReglPrioProcs);
KernLog("\nIdle: ");
PrintList(IdlePrioProcs);
int tick = 0;
while (tick < 14) {
//if (tick%25==0)ClearTerm(StdOut);
if (tick > 0 && tick != 50 && tick % 10 == 0) {
KernLog("Blocking current process\n");
PsBlockCurProc();
}
if (tick == 50) {
procs[0].procState = STATE_RUNNABLE;
PsSchedThisProc(&procs[0]);
}
KernLog("Tick %d - Running: ", tick);
if (PsCurProc == NULL) {
KernLog("IDLE\n");
}
else {
PrintProc(PsCurProc);
}
PsSchedOnTick();
if (tick == 50) // already done
KernLog("Re-scheduling process 0\n");
tick++;
}
}