NetBSD/share/man/man9/scheduler.9

128 lines
5.0 KiB
Groff

.\" $NetBSD: scheduler.9,v 1.3 2003/04/16 13:35:33 wiz Exp $
.\"
.\" 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
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" 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
.In sys/sched.h
.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
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.
If multiple processes reside on a queue, the system runs them round
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.
.Pp
The system adjusts the scheduling priority of a process dynamically to
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).
.Pp
The process CPU utilization is incremented in
.Fn hardclock
each time that the system clock ticks and the process is found to be
executing.
CPU utilization is adjusted once per second in
.Fn schedcpu
via a digital decay filter.
Each time a process accumulates four ticks in its CPU utilization,
.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
scheduling behaviour discussed above.
However, each second
.Fn schedcpu
updates the time each process has been on the sleep queue.
The system recomputes a process's CPU utilization estimate in
.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
The system tailors this short-term scheduling algorithm to favor
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