minix/servers/sched/main.c
Tomas Hruby b09bcf6779 Scheduling server (by Bjorn Swift)
In this second phase, scheduling is moved from PM to its own
scheduler (see r6557 for phase one). In the next phase we hope to a)
include useful information in the "out of quantum" message and b)
create some simple scheduling policy that makes use of that
information.

When the system starts up, PM will iterate over its process table and
ask SCHED to take over scheduling unprivileged processes. This is
done by sending a SCHEDULING_START message to SCHED. This message
includes the processes endpoint, the parent's endpoint and its nice
level. The scheduler adds this process to its schedproc table, issues
a schedctl, and returns its own endpoint to PM - as the endpoint of
the effective scheduler. When a process terminates, a SCHEDULING_STOP
message is sent to the scheduler.

The reason for this effective endpoint is for future compatibility.
Some day, we may have a scheduler that, instead of scheduling the
process itself, forwards the SCHEDULING_START message on to another
scheduler.

PM has information on who schedules whom. As such, scheduling
messages from user-land are sent through PM. An example is when
processes change their priority, using nice(). In that case, a
getsetpriority message is sent to PM, which then sends a
SCHEDULING_SET_NICE to the process's effective scheduler.

When a process is forked through PM, it inherits its parent's
scheduler, but is spawned with an empty quantum. As before, a request
to fork a process flows through VM before returning to PM, which then
wakes up the child process. This flow has been modified slightly so
that PM notifies the scheduler of the new process, before waking up
the child process. If the scheduler fails to take over scheduling,
the child process is torn down and the fork fails with an erroneous
value.

Process priority is entirely decided upon using nice levels. PM
stores a copy of each process's nice level and when a child is
forked, its parent's nice level is sent in the SCHEDULING_START
message. How this level is mapped to a priority queue is up to the
scheduler. It should be noted that the nice level is used to
determine the max_priority and the parent could have been in a lower
priority when it was spawned. To prevent a CPU intensive process from
hawking the CPU by continuously forking children that get scheduled
in the max_priority, the scheduler should determine in which queue
the parent is currently scheduled, and schedule the child in that
same queue.

Other fixes: The USER_Q in kernel/proc.h was incorrectly defined as
NR_SCHED_QUEUES/2. That results in a "off by one" error when
converting priority->nice->priority for nice=0. This also had the
side effect that if someone were to set the MAX_USER_Q to something
else than 0, then USER_Q would be off.
2010-05-18 13:39:04 +00:00

120 lines
3.3 KiB
C

/* This file contains the main program of the SCHED scheduler. It will sit idle
* until asked, by PM, to take over scheduling a particular process.
*/
/* The _MAIN def indicates that we want the schedproc structs to be created
* here. Used from within schedproc.h */
#define _MAIN
#include "sched.h"
#include "schedproc.h"
/* Declare some local functions. */
FORWARD _PROTOTYPE( void reply, (endpoint_t whom, message *m_ptr) );
FORWARD _PROTOTYPE( void sef_local_startup, (void) );
/*===========================================================================*
* main *
*===========================================================================*/
PUBLIC int main(void)
{
/* Main routine of the scheduler. */
message m_in; /* the incoming message itself is kept here. */
int call_nr; /* system call number */
int who_e; /* caller's endpoint */
int result; /* result to system call */
int rv;
/* SEF local startup. */
sef_local_startup();
/* Initialize scheduling timers, used for running balance_queues */
init_scheduling();
/* This is SCHED's main loop - get work and do it, forever and forever. */
while (TRUE) {
int ipc_status;
/* Wait for the next message and extract useful information from it. */
if (sef_receive_status(ANY, &m_in, &ipc_status) != OK)
panic("SCHED sef_receive error");
who_e = m_in.m_source; /* who sent the message */
call_nr = m_in.m_type; /* system call number */
/* Check for system notifications first. Special cases. */
if (is_ipc_notify(ipc_status)) {
switch(who_e) {
case CLOCK:
sched_expire_timers(m_in.NOTIFY_TIMESTAMP);
continue; /* don't reply */
default :
result = ENOSYS;
}
goto sendreply;
}
switch(call_nr) {
case SCHEDULING_START:
result = do_start_scheduling(&m_in);
break;
case SCHEDULING_STOP:
result = do_stop_scheduling(&m_in);
break;
case SCHEDULING_SET_NICE:
result = do_nice(&m_in);
break;
case SCHEDULING_NO_QUANTUM:
/* This message was sent from the kernel, don't reply */
if (IPC_STATUS_FLAGS_TEST(ipc_status,
IPC_FLG_MSG_FROM_KERNEL)) {
if ((rv = do_noquantum(&m_in)) != (OK)) {
printf("SCHED: Warning, do_noquantum "
"failed with %d\n", rv);
}
continue; /* Don't reply */
}
else {
printf("SCHED: process %d faked "
"SCHEDULING_NO_QUANTUM message!\n",
who_e);
result = EPERM;
}
break;
default:
result = no_sys(who_e, call_nr);
}
sendreply:
/* Send reply. */
if (result != SUSPEND) {
m_in.m_type = result; /* build reply message */
reply(who_e, &m_in); /* send it away */
}
}
return(OK);
}
/*===========================================================================*
* reply *
*===========================================================================*/
PRIVATE void reply(endpoint_t who_e, message *m_ptr)
{
int s = send(who_e, m_ptr); /* send the message */
if (OK != s)
printf("SCHED: unable to send reply to %d: %d\n", who_e, s);
}
/*===========================================================================*
* sef_local_startup *
*===========================================================================*/
PRIVATE void sef_local_startup(void)
{
/* No init callbacks for now. */
/* No live update support for now. */
/* No signal callbacks for now. */
/* Let SEF perform startup. */
sef_startup();
}