minix/servers/pm/schedule.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

108 lines
3.3 KiB
C

#include "pm.h"
#include <minix/callnr.h>
#include <minix/com.h>
#include <minix/config.h>
#include <minix/sysinfo.h>
#include <minix/type.h>
#include <machine/archtypes.h>
#include <lib.h>
#include "mproc.h"
/*===========================================================================*
* init_scheduling *
*===========================================================================*/
PUBLIC void sched_init(void)
{
struct mproc *trmp;
int proc_nr;
for (proc_nr=0, trmp=mproc; proc_nr < NR_PROCS; proc_nr++, trmp++) {
/* Don't take over system processes. When the system starts,
* this will typically only take over init, from which other
* user space processes will inherit. */
if (trmp->mp_flags & IN_USE && !(trmp->mp_flags & PRIV_PROC)) {
if (sched_start(SCHED_PROC_NR, trmp,
(SEND_PRIORITY | SEND_TIME_SLICE))) {
printf("PM: SCHED denied taking over scheduling of %s\n",
trmp->mp_name);
}
}
}
}
/*===========================================================================*
* sched_start *
*===========================================================================*/
PUBLIC int sched_start(endpoint_t ep, struct mproc *rmp, int flags)
{
int rv;
message m;
m.SCHEDULING_ENDPOINT = rmp->mp_endpoint;
m.SCHEDULING_PARENT = mproc[rmp->mp_parent].mp_endpoint;
m.SCHEDULING_NICE = rmp->mp_nice;
/* Send the request to the scheduler */
if ((rv = _taskcall(ep, SCHEDULING_START, &m))) {
return rv;
}
/* Store the process' scheduler. Note that this might not be the
* scheduler we sent the SCHEDULING_START message to. That scheduler
* might have forwarded the scheduling message on to another scheduler
* before returning the message.
*/
rmp->mp_scheduler = m.SCHEDULING_SCHEDULER;
return (OK);
}
/*===========================================================================*
* sched_stop *
*===========================================================================*/
PUBLIC int sched_stop(struct mproc *rmp)
{
int rv;
message m;
/* If the kernel is the scheduler, it will implicitly stop scheduling
* once another process takes over or the process terminates */
if (rmp->mp_scheduler == KERNEL || rmp->mp_scheduler == NONE)
return(OK);
m.SCHEDULING_ENDPOINT = rmp->mp_endpoint;
if ((rv = _taskcall(rmp->mp_scheduler, SCHEDULING_STOP, &m))) {
return rv;
}
/* sched_stop is either called when the process is exiting or it is
* being moved between schedulers. If it is being moved between
* schedulers, we need to set the mp_scheduler to NONE so that PM
* doesn't forward messages to the process' scheduler while being moved
* (such as sched_nice). */
rmp->mp_scheduler = NONE;
return (OK);
}
/*===========================================================================*
* sched_nice *
*===========================================================================*/
PUBLIC int sched_nice(struct mproc *rmp, int nice)
{
int rv;
message m;
/* If the kernel is the scheduler, we don't allow messing with the
* priority. If you want to control process priority, assign the process
* to a user-space scheduler */
if (rmp->mp_scheduler == KERNEL || rmp->mp_scheduler == NONE)
return (EINVAL);
m.SCHEDULING_ENDPOINT = rmp->mp_endpoint;
m.SCHEDULING_NICE = nice;
if ((rv = _taskcall(rmp->mp_scheduler, SCHEDULING_SET_NICE, &m))) {
return rv;
}
return (OK);
}