2002-09-27 12:38:58 +04:00
|
|
|
.\" $NetBSD: scheduler.9,v 1.2 2002/09/27 08:38:59 wiz Exp $
|
2002-09-22 10:26:33 +04:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 2002 The NetBSD Foundation, Inc.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Gregory McGarry.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the NetBSD
|
|
|
|
.\" Foundation, Inc. and its contributors.
|
|
|
|
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
.\" contributors may be used to endorse or promote products derived
|
|
|
|
.\" from this software without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2002-09-27 12:38:58 +04:00
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
2002-09-22 10:26:33 +04:00
|
|
|
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.Dd September 21, 2002
|
|
|
|
.Dt SCHEDULER 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm scheduler
|
|
|
|
.Nd process scheduling subsystem
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Fd #include \*[Lt]sys/sched.h\*[Gt]
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Bf -symbolic
|
|
|
|
This page documents the default
|
|
|
|
.Dq time-sharing
|
|
|
|
scheduler in
|
|
|
|
.Nx .
|
|
|
|
It is likely that in the future a new scheduler subsystem will be
|
|
|
|
developed which will allow new scheduling algorithms to be
|
|
|
|
implemented.
|
|
|
|
.Ef
|
|
|
|
.Pp
|
|
|
|
.Nx Ns 's
|
|
|
|
scheduler algorithm is based on
|
|
|
|
.Dq multilevel feedback queues .
|
|
|
|
All processes that are runnable are assigned a scheduling priority
|
2002-09-27 12:38:58 +04:00
|
|
|
that determines in which run queue they are placed.
|
|
|
|
The currently running process does not exist on a run queue.
|
|
|
|
In selecting a new process to run, the system scans the run queues
|
|
|
|
from highest to lowest priority and chooses the first process on the
|
|
|
|
first non-empty queue.
|
2002-09-22 10:26:33 +04:00
|
|
|
If multiple processes reside on a queue, the system runs them round
|
2002-09-27 12:38:58 +04:00
|
|
|
robin in the order found on the queue.
|
|
|
|
If a process uses up the time slice allocated to it, it is placed at
|
|
|
|
the end of the queue from which it came, and the process at the front of
|
|
|
|
the queue is selected to run.
|
2002-09-22 10:26:33 +04:00
|
|
|
.Pp
|
|
|
|
The system adjusts the scheduling priority of a process dynamically to
|
2002-09-27 12:38:58 +04:00
|
|
|
reflect resource requirements (being blocked) and the CPU utilization
|
|
|
|
consumed by the process.
|
|
|
|
Processes are moved between run queues based on changes in their
|
|
|
|
scheduling priority (hence the word feedback in the name).
|
2002-09-22 10:26:33 +04:00
|
|
|
.Pp
|
2002-09-27 12:38:58 +04:00
|
|
|
The process CPU utilization is incremented in
|
2002-09-22 10:26:33 +04:00
|
|
|
.Fn hardclock
|
|
|
|
each time that the system clock ticks and the process is found to be
|
2002-09-27 12:38:58 +04:00
|
|
|
executing.
|
|
|
|
CPU utilization is adjusted once per second in
|
2002-09-22 10:26:33 +04:00
|
|
|
.Fn schedcpu
|
2002-09-27 12:38:58 +04:00
|
|
|
via a digital decay filter.
|
|
|
|
Each time a process accumulates four ticks in its CPU utilization,
|
2002-09-22 10:26:33 +04:00
|
|
|
.Fn schedclock
|
|
|
|
invokes
|
|
|
|
.Fn resetpriority
|
|
|
|
to recalculate the process's scheduling priority.
|
|
|
|
.Pp
|
|
|
|
When a process other than the currently running process attains a
|
|
|
|
higher scheduling priority, the system notices this in
|
|
|
|
.Fn resetpriority
|
|
|
|
and schedules a context switch to the higher-priority process to occur
|
|
|
|
on return to user mode.
|
|
|
|
.Pp
|
|
|
|
A blocked process exists on the sleep queue and is not affected by the
|
2002-09-27 12:38:58 +04:00
|
|
|
scheduling behaviour discussed above.
|
|
|
|
However, each second
|
2002-09-22 10:26:33 +04:00
|
|
|
.Fn schedcpu
|
2002-09-27 12:38:58 +04:00
|
|
|
updates the time each process has been on the sleep queue.
|
|
|
|
The system recomputes a process's CPU utilization estimate in
|
2002-09-22 10:26:33 +04:00
|
|
|
.Fn updatepri
|
|
|
|
when it is awakened and has been sleeping for longer than 1 second.
|
|
|
|
When the process is placed on the run queue by
|
|
|
|
.Fn setrunnable ,
|
|
|
|
.Fn resetpriority
|
|
|
|
is invoked to recalculate the process's scheduling priority.
|
|
|
|
.Pp
|
2002-09-27 12:38:58 +04:00
|
|
|
The system tailors this short-term scheduling algorithm to favor
|
2002-09-22 10:26:33 +04:00
|
|
|
interactive jobs by raising the scheduling priority of processes that
|
|
|
|
are blocked waiting for I/O for one or more seconds, and by lowering the
|
|
|
|
priority of processes that accumulate significant amounts of CPU time.
|
|
|
|
.Sh CODE REFERENCES
|
|
|
|
This section describes places within the
|
|
|
|
.Nx
|
|
|
|
source tree where actual code implementing the scheduler can be found.
|
|
|
|
All pathnames are relative to
|
|
|
|
.Pa /usr/src .
|
|
|
|
.Pp
|
|
|
|
The scheduler subsystem is implemented within the file
|
|
|
|
.Pa sys/kern/kern_synch.c .
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr SCHED_LOCK 9 ,
|
|
|
|
.Xr hardclock 9 ,
|
|
|
|
.Xr mi_switch 9 ,
|
|
|
|
.Xr setrunqueue 9 ,
|
|
|
|
.Xr userret 9
|