sched_4bsd.c revision 1.3.2.2 1 1.3.2.2 mjf /* $NetBSD: sched_4bsd.c,v 1.3.2.2 2007/07/11 20:10:01 mjf Exp $ */
2 1.3.2.2 mjf
3 1.3.2.2 mjf /*-
4 1.3.2.2 mjf * Copyright (c) 1999, 2000, 2004, 2006, 2007 The NetBSD Foundation, Inc.
5 1.3.2.2 mjf * All rights reserved.
6 1.3.2.2 mjf *
7 1.3.2.2 mjf * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 mjf * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.3.2.2 mjf * NASA Ames Research Center, by Charles M. Hannum, Andrew Doran, and
10 1.3.2.2 mjf * Daniel Sieger.
11 1.3.2.2 mjf *
12 1.3.2.2 mjf * Redistribution and use in source and binary forms, with or without
13 1.3.2.2 mjf * modification, are permitted provided that the following conditions
14 1.3.2.2 mjf * are met:
15 1.3.2.2 mjf * 1. Redistributions of source code must retain the above copyright
16 1.3.2.2 mjf * notice, this list of conditions and the following disclaimer.
17 1.3.2.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
18 1.3.2.2 mjf * notice, this list of conditions and the following disclaimer in the
19 1.3.2.2 mjf * documentation and/or other materials provided with the distribution.
20 1.3.2.2 mjf * 3. All advertising materials mentioning features or use of this software
21 1.3.2.2 mjf * must display the following acknowledgement:
22 1.3.2.2 mjf * This product includes software developed by the NetBSD
23 1.3.2.2 mjf * Foundation, Inc. and its contributors.
24 1.3.2.2 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.3.2.2 mjf * contributors may be used to endorse or promote products derived
26 1.3.2.2 mjf * from this software without specific prior written permission.
27 1.3.2.2 mjf *
28 1.3.2.2 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.3.2.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.3.2.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.3.2.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.3.2.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.3.2.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.3.2.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.3.2.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.3.2.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.3.2.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.3.2.2 mjf * POSSIBILITY OF SUCH DAMAGE.
39 1.3.2.2 mjf */
40 1.3.2.2 mjf
41 1.3.2.2 mjf /*-
42 1.3.2.2 mjf * Copyright (c) 1982, 1986, 1990, 1991, 1993
43 1.3.2.2 mjf * The Regents of the University of California. All rights reserved.
44 1.3.2.2 mjf * (c) UNIX System Laboratories, Inc.
45 1.3.2.2 mjf * All or some portions of this file are derived from material licensed
46 1.3.2.2 mjf * to the University of California by American Telephone and Telegraph
47 1.3.2.2 mjf * Co. or Unix System Laboratories, Inc. and are reproduced herein with
48 1.3.2.2 mjf * the permission of UNIX System Laboratories, Inc.
49 1.3.2.2 mjf *
50 1.3.2.2 mjf * Redistribution and use in source and binary forms, with or without
51 1.3.2.2 mjf * modification, are permitted provided that the following conditions
52 1.3.2.2 mjf * are met:
53 1.3.2.2 mjf * 1. Redistributions of source code must retain the above copyright
54 1.3.2.2 mjf * notice, this list of conditions and the following disclaimer.
55 1.3.2.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
56 1.3.2.2 mjf * notice, this list of conditions and the following disclaimer in the
57 1.3.2.2 mjf * documentation and/or other materials provided with the distribution.
58 1.3.2.2 mjf * 3. Neither the name of the University nor the names of its contributors
59 1.3.2.2 mjf * may be used to endorse or promote products derived from this software
60 1.3.2.2 mjf * without specific prior written permission.
61 1.3.2.2 mjf *
62 1.3.2.2 mjf * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.3.2.2 mjf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.3.2.2 mjf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.3.2.2 mjf * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.3.2.2 mjf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.3.2.2 mjf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.3.2.2 mjf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.3.2.2 mjf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.3.2.2 mjf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.3.2.2 mjf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.3.2.2 mjf * SUCH DAMAGE.
73 1.3.2.2 mjf *
74 1.3.2.2 mjf * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
75 1.3.2.2 mjf */
76 1.3.2.2 mjf
77 1.3.2.2 mjf #include <sys/cdefs.h>
78 1.3.2.2 mjf __KERNEL_RCSID(0, "$NetBSD: sched_4bsd.c,v 1.3.2.2 2007/07/11 20:10:01 mjf Exp $");
79 1.3.2.2 mjf
80 1.3.2.2 mjf #include "opt_ddb.h"
81 1.3.2.2 mjf #include "opt_lockdebug.h"
82 1.3.2.2 mjf #include "opt_perfctrs.h"
83 1.3.2.2 mjf
84 1.3.2.2 mjf #define __MUTEX_PRIVATE
85 1.3.2.2 mjf
86 1.3.2.2 mjf #include <sys/param.h>
87 1.3.2.2 mjf #include <sys/systm.h>
88 1.3.2.2 mjf #include <sys/callout.h>
89 1.3.2.2 mjf #include <sys/cpu.h>
90 1.3.2.2 mjf #include <sys/proc.h>
91 1.3.2.2 mjf #include <sys/kernel.h>
92 1.3.2.2 mjf #include <sys/signalvar.h>
93 1.3.2.2 mjf #include <sys/resourcevar.h>
94 1.3.2.2 mjf #include <sys/sched.h>
95 1.3.2.2 mjf #include <sys/sysctl.h>
96 1.3.2.2 mjf #include <sys/kauth.h>
97 1.3.2.2 mjf #include <sys/lockdebug.h>
98 1.3.2.2 mjf #include <sys/kmem.h>
99 1.3.2.2 mjf
100 1.3.2.2 mjf #include <uvm/uvm_extern.h>
101 1.3.2.2 mjf
102 1.3.2.2 mjf /*
103 1.3.2.2 mjf * Run queues.
104 1.3.2.2 mjf *
105 1.3.2.2 mjf * We have 32 run queues in descending priority of 0..31. We maintain
106 1.3.2.2 mjf * a bitmask of non-empty queues in order speed up finding the first
107 1.3.2.2 mjf * runnable process. The bitmask is maintained only by machine-dependent
108 1.3.2.2 mjf * code, allowing the most efficient instructions to be used to find the
109 1.3.2.2 mjf * first non-empty queue.
110 1.3.2.2 mjf */
111 1.3.2.2 mjf
112 1.3.2.2 mjf #define RUNQUE_NQS 32 /* number of runqueues */
113 1.3.2.2 mjf #define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
114 1.3.2.2 mjf
115 1.3.2.2 mjf typedef struct subqueue {
116 1.3.2.2 mjf TAILQ_HEAD(, lwp) sq_queue;
117 1.3.2.2 mjf } subqueue_t;
118 1.3.2.2 mjf typedef struct runqueue {
119 1.3.2.2 mjf subqueue_t rq_subqueues[RUNQUE_NQS]; /* run queues */
120 1.3.2.2 mjf uint32_t rq_bitmap; /* bitmap of non-empty queues */
121 1.3.2.2 mjf } runqueue_t;
122 1.3.2.2 mjf static runqueue_t global_queue;
123 1.3.2.2 mjf
124 1.3.2.2 mjf static void updatepri(struct lwp *);
125 1.3.2.2 mjf static void resetpriority(struct lwp *);
126 1.3.2.2 mjf static void resetprocpriority(struct proc *);
127 1.3.2.2 mjf
128 1.3.2.2 mjf extern unsigned int sched_pstats_ticks; /* defined in kern_synch.c */
129 1.3.2.2 mjf
130 1.3.2.2 mjf /* The global scheduler state */
131 1.3.2.2 mjf kmutex_t sched_mutex;
132 1.3.2.2 mjf
133 1.3.2.2 mjf /* Number of hardclock ticks per sched_tick() */
134 1.3.2.2 mjf int rrticks;
135 1.3.2.2 mjf
136 1.3.2.2 mjf /*
137 1.3.2.2 mjf * Force switch among equal priority processes every 100ms.
138 1.3.2.2 mjf * Called from hardclock every hz/10 == rrticks hardclock ticks.
139 1.3.2.2 mjf */
140 1.3.2.2 mjf /* ARGSUSED */
141 1.3.2.2 mjf void
142 1.3.2.2 mjf sched_tick(struct cpu_info *ci)
143 1.3.2.2 mjf {
144 1.3.2.2 mjf struct schedstate_percpu *spc = &ci->ci_schedstate;
145 1.3.2.2 mjf
146 1.3.2.2 mjf spc->spc_ticks = rrticks;
147 1.3.2.2 mjf
148 1.3.2.2 mjf if (!CURCPU_IDLE_P()) {
149 1.3.2.2 mjf if (spc->spc_flags & SPCF_SEENRR) {
150 1.3.2.2 mjf /*
151 1.3.2.2 mjf * The process has already been through a roundrobin
152 1.3.2.2 mjf * without switching and may be hogging the CPU.
153 1.3.2.2 mjf * Indicate that the process should yield.
154 1.3.2.2 mjf */
155 1.3.2.2 mjf spc->spc_flags |= SPCF_SHOULDYIELD;
156 1.3.2.2 mjf } else
157 1.3.2.2 mjf spc->spc_flags |= SPCF_SEENRR;
158 1.3.2.2 mjf }
159 1.3.2.2 mjf cpu_need_resched(curcpu(), 0);
160 1.3.2.2 mjf }
161 1.3.2.2 mjf
162 1.3.2.2 mjf #define NICE_WEIGHT 2 /* priorities per nice level */
163 1.3.2.2 mjf
164 1.3.2.2 mjf #define ESTCPU_SHIFT 11
165 1.3.2.2 mjf #define ESTCPU_MAX ((NICE_WEIGHT * PRIO_MAX - PPQ) << ESTCPU_SHIFT)
166 1.3.2.2 mjf #define ESTCPULIM(e) min((e), ESTCPU_MAX)
167 1.3.2.2 mjf
168 1.3.2.2 mjf /*
169 1.3.2.2 mjf * Constants for digital decay and forget:
170 1.3.2.2 mjf * 90% of (p_estcpu) usage in 5 * loadav time
171 1.3.2.2 mjf * 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
172 1.3.2.2 mjf * Note that, as ps(1) mentions, this can let percentages
173 1.3.2.2 mjf * total over 100% (I've seen 137.9% for 3 processes).
174 1.3.2.2 mjf *
175 1.3.2.2 mjf * Note that hardclock updates p_estcpu and p_cpticks independently.
176 1.3.2.2 mjf *
177 1.3.2.2 mjf * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
178 1.3.2.2 mjf * That is, the system wants to compute a value of decay such
179 1.3.2.2 mjf * that the following for loop:
180 1.3.2.2 mjf * for (i = 0; i < (5 * loadavg); i++)
181 1.3.2.2 mjf * p_estcpu *= decay;
182 1.3.2.2 mjf * will compute
183 1.3.2.2 mjf * p_estcpu *= 0.1;
184 1.3.2.2 mjf * for all values of loadavg:
185 1.3.2.2 mjf *
186 1.3.2.2 mjf * Mathematically this loop can be expressed by saying:
187 1.3.2.2 mjf * decay ** (5 * loadavg) ~= .1
188 1.3.2.2 mjf *
189 1.3.2.2 mjf * The system computes decay as:
190 1.3.2.2 mjf * decay = (2 * loadavg) / (2 * loadavg + 1)
191 1.3.2.2 mjf *
192 1.3.2.2 mjf * We wish to prove that the system's computation of decay
193 1.3.2.2 mjf * will always fulfill the equation:
194 1.3.2.2 mjf * decay ** (5 * loadavg) ~= .1
195 1.3.2.2 mjf *
196 1.3.2.2 mjf * If we compute b as:
197 1.3.2.2 mjf * b = 2 * loadavg
198 1.3.2.2 mjf * then
199 1.3.2.2 mjf * decay = b / (b + 1)
200 1.3.2.2 mjf *
201 1.3.2.2 mjf * We now need to prove two things:
202 1.3.2.2 mjf * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
203 1.3.2.2 mjf * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
204 1.3.2.2 mjf *
205 1.3.2.2 mjf * Facts:
206 1.3.2.2 mjf * For x close to zero, exp(x) =~ 1 + x, since
207 1.3.2.2 mjf * exp(x) = 0! + x**1/1! + x**2/2! + ... .
208 1.3.2.2 mjf * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
209 1.3.2.2 mjf * For x close to zero, ln(1+x) =~ x, since
210 1.3.2.2 mjf * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
211 1.3.2.2 mjf * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
212 1.3.2.2 mjf * ln(.1) =~ -2.30
213 1.3.2.2 mjf *
214 1.3.2.2 mjf * Proof of (1):
215 1.3.2.2 mjf * Solve (factor)**(power) =~ .1 given power (5*loadav):
216 1.3.2.2 mjf * solving for factor,
217 1.3.2.2 mjf * ln(factor) =~ (-2.30/5*loadav), or
218 1.3.2.2 mjf * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
219 1.3.2.2 mjf * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
220 1.3.2.2 mjf *
221 1.3.2.2 mjf * Proof of (2):
222 1.3.2.2 mjf * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
223 1.3.2.2 mjf * solving for power,
224 1.3.2.2 mjf * power*ln(b/(b+1)) =~ -2.30, or
225 1.3.2.2 mjf * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
226 1.3.2.2 mjf *
227 1.3.2.2 mjf * Actual power values for the implemented algorithm are as follows:
228 1.3.2.2 mjf * loadav: 1 2 3 4
229 1.3.2.2 mjf * power: 5.68 10.32 14.94 19.55
230 1.3.2.2 mjf */
231 1.3.2.2 mjf
232 1.3.2.2 mjf /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
233 1.3.2.2 mjf #define loadfactor(loadav) (2 * (loadav))
234 1.3.2.2 mjf
235 1.3.2.2 mjf static fixpt_t
236 1.3.2.2 mjf decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
237 1.3.2.2 mjf {
238 1.3.2.2 mjf
239 1.3.2.2 mjf if (estcpu == 0) {
240 1.3.2.2 mjf return 0;
241 1.3.2.2 mjf }
242 1.3.2.2 mjf
243 1.3.2.2 mjf #if !defined(_LP64)
244 1.3.2.2 mjf /* avoid 64bit arithmetics. */
245 1.3.2.2 mjf #define FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
246 1.3.2.2 mjf if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
247 1.3.2.2 mjf return estcpu * loadfac / (loadfac + FSCALE);
248 1.3.2.2 mjf }
249 1.3.2.2 mjf #endif /* !defined(_LP64) */
250 1.3.2.2 mjf
251 1.3.2.2 mjf return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
252 1.3.2.2 mjf }
253 1.3.2.2 mjf
254 1.3.2.2 mjf /*
255 1.3.2.2 mjf * For all load averages >= 1 and max p_estcpu of (255 << ESTCPU_SHIFT),
256 1.3.2.2 mjf * sleeping for at least seven times the loadfactor will decay p_estcpu to
257 1.3.2.2 mjf * less than (1 << ESTCPU_SHIFT).
258 1.3.2.2 mjf *
259 1.3.2.2 mjf * note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
260 1.3.2.2 mjf */
261 1.3.2.2 mjf static fixpt_t
262 1.3.2.2 mjf decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
263 1.3.2.2 mjf {
264 1.3.2.2 mjf
265 1.3.2.2 mjf if ((n << FSHIFT) >= 7 * loadfac) {
266 1.3.2.2 mjf return 0;
267 1.3.2.2 mjf }
268 1.3.2.2 mjf
269 1.3.2.2 mjf while (estcpu != 0 && n > 1) {
270 1.3.2.2 mjf estcpu = decay_cpu(loadfac, estcpu);
271 1.3.2.2 mjf n--;
272 1.3.2.2 mjf }
273 1.3.2.2 mjf
274 1.3.2.2 mjf return estcpu;
275 1.3.2.2 mjf }
276 1.3.2.2 mjf
277 1.3.2.2 mjf /*
278 1.3.2.2 mjf * sched_pstats_hook:
279 1.3.2.2 mjf *
280 1.3.2.2 mjf * Periodically called from sched_pstats(); used to recalculate priorities.
281 1.3.2.2 mjf */
282 1.3.2.2 mjf void
283 1.3.2.2 mjf sched_pstats_hook(struct proc *p, int minslp)
284 1.3.2.2 mjf {
285 1.3.2.2 mjf struct lwp *l;
286 1.3.2.2 mjf fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
287 1.3.2.2 mjf
288 1.3.2.2 mjf /*
289 1.3.2.2 mjf * If the process has slept the entire second,
290 1.3.2.2 mjf * stop recalculating its priority until it wakes up.
291 1.3.2.2 mjf */
292 1.3.2.2 mjf if (minslp <= 1) {
293 1.3.2.2 mjf p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
294 1.3.2.2 mjf
295 1.3.2.2 mjf LIST_FOREACH(l, &p->p_lwps, l_sibling) {
296 1.3.2.2 mjf if ((l->l_flag & LW_IDLE) != 0)
297 1.3.2.2 mjf continue;
298 1.3.2.2 mjf lwp_lock(l);
299 1.3.2.2 mjf if (l->l_slptime <= 1 && l->l_priority >= PUSER)
300 1.3.2.2 mjf resetpriority(l);
301 1.3.2.2 mjf lwp_unlock(l);
302 1.3.2.2 mjf }
303 1.3.2.2 mjf }
304 1.3.2.2 mjf }
305 1.3.2.2 mjf
306 1.3.2.2 mjf /*
307 1.3.2.2 mjf * Recalculate the priority of a process after it has slept for a while.
308 1.3.2.2 mjf */
309 1.3.2.2 mjf static void
310 1.3.2.2 mjf updatepri(struct lwp *l)
311 1.3.2.2 mjf {
312 1.3.2.2 mjf struct proc *p = l->l_proc;
313 1.3.2.2 mjf fixpt_t loadfac;
314 1.3.2.2 mjf
315 1.3.2.2 mjf KASSERT(lwp_locked(l, NULL));
316 1.3.2.2 mjf KASSERT(l->l_slptime > 1);
317 1.3.2.2 mjf
318 1.3.2.2 mjf loadfac = loadfactor(averunnable.ldavg[0]);
319 1.3.2.2 mjf
320 1.3.2.2 mjf l->l_slptime--; /* the first time was done in sched_pstats */
321 1.3.2.2 mjf /* XXX NJWLWP */
322 1.3.2.2 mjf /* XXXSMP occasionally unlocked, should be per-LWP */
323 1.3.2.2 mjf p->p_estcpu = decay_cpu_batch(loadfac, p->p_estcpu, l->l_slptime);
324 1.3.2.2 mjf resetpriority(l);
325 1.3.2.2 mjf }
326 1.3.2.2 mjf
327 1.3.2.2 mjf /*
328 1.3.2.2 mjf * On some architectures, it's faster to use a MSB ordering for the priorites
329 1.3.2.2 mjf * than the traditional LSB ordering.
330 1.3.2.2 mjf */
331 1.3.2.2 mjf #define RQMASK(n) (0x00000001 << (n))
332 1.3.2.2 mjf
333 1.3.2.2 mjf /*
334 1.3.2.2 mjf * The primitives that manipulate the run queues. whichqs tells which
335 1.3.2.2 mjf * of the 32 queues qs have processes in them. sched_enqueue() puts processes
336 1.3.2.2 mjf * into queues, sched_dequeue removes them from queues. The running process is
337 1.3.2.2 mjf * on no queue, other processes are on a queue related to p->p_priority,
338 1.3.2.2 mjf * divided by 4 actually to shrink the 0-127 range of priorities into the 32
339 1.3.2.2 mjf * available queues.
340 1.3.2.2 mjf */
341 1.3.2.2 mjf #ifdef RQDEBUG
342 1.3.2.2 mjf static void
343 1.3.2.2 mjf runqueue_check(const runqueue_t *rq, int whichq, struct lwp *l)
344 1.3.2.2 mjf {
345 1.3.2.2 mjf const subqueue_t * const sq = &rq->rq_subqueues[whichq];
346 1.3.2.2 mjf const uint32_t bitmap = rq->rq_bitmap;
347 1.3.2.2 mjf struct lwp *l2;
348 1.3.2.2 mjf int found = 0;
349 1.3.2.2 mjf int die = 0;
350 1.3.2.2 mjf int empty = 1;
351 1.3.2.2 mjf
352 1.3.2.2 mjf TAILQ_FOREACH(l2, &sq->sq_queue, l_runq) {
353 1.3.2.2 mjf if (l2->l_stat != LSRUN) {
354 1.3.2.2 mjf printf("runqueue_check[%d]: lwp %p state (%d) "
355 1.3.2.2 mjf " != LSRUN\n", whichq, l2, l2->l_stat);
356 1.3.2.2 mjf }
357 1.3.2.2 mjf if (l2 == l)
358 1.3.2.2 mjf found = 1;
359 1.3.2.2 mjf empty = 0;
360 1.3.2.2 mjf }
361 1.3.2.2 mjf if (empty && (bitmap & RQMASK(whichq)) != 0) {
362 1.3.2.2 mjf printf("runqueue_check[%d]: bit set for empty run-queue %p\n",
363 1.3.2.2 mjf whichq, rq);
364 1.3.2.2 mjf die = 1;
365 1.3.2.2 mjf } else if (!empty && (bitmap & RQMASK(whichq)) == 0) {
366 1.3.2.2 mjf printf("runqueue_check[%d]: bit clear for non-empty "
367 1.3.2.2 mjf "run-queue %p\n", whichq, rq);
368 1.3.2.2 mjf die = 1;
369 1.3.2.2 mjf }
370 1.3.2.2 mjf if (l != NULL && (bitmap & RQMASK(whichq)) == 0) {
371 1.3.2.2 mjf printf("runqueue_check[%d]: bit clear for active lwp %p\n",
372 1.3.2.2 mjf whichq, l);
373 1.3.2.2 mjf die = 1;
374 1.3.2.2 mjf }
375 1.3.2.2 mjf if (l != NULL && empty) {
376 1.3.2.2 mjf printf("runqueue_check[%d]: empty run-queue %p with "
377 1.3.2.2 mjf "active lwp %p\n", whichq, rq, l);
378 1.3.2.2 mjf die = 1;
379 1.3.2.2 mjf }
380 1.3.2.2 mjf if (l != NULL && !found) {
381 1.3.2.2 mjf printf("runqueue_check[%d]: lwp %p not in runqueue %p!",
382 1.3.2.2 mjf whichq, l, rq);
383 1.3.2.2 mjf die = 1;
384 1.3.2.2 mjf }
385 1.3.2.2 mjf if (die)
386 1.3.2.2 mjf panic("runqueue_check: inconsistency found");
387 1.3.2.2 mjf }
388 1.3.2.2 mjf #else /* RQDEBUG */
389 1.3.2.2 mjf #define runqueue_check(a, b, c) /* nothing */
390 1.3.2.2 mjf #endif /* RQDEBUG */
391 1.3.2.2 mjf
392 1.3.2.2 mjf static void
393 1.3.2.2 mjf runqueue_init(runqueue_t *rq)
394 1.3.2.2 mjf {
395 1.3.2.2 mjf int i;
396 1.3.2.2 mjf
397 1.3.2.2 mjf for (i = 0; i < RUNQUE_NQS; i++)
398 1.3.2.2 mjf TAILQ_INIT(&rq->rq_subqueues[i].sq_queue);
399 1.3.2.2 mjf }
400 1.3.2.2 mjf
401 1.3.2.2 mjf static void
402 1.3.2.2 mjf runqueue_enqueue(runqueue_t *rq, struct lwp *l)
403 1.3.2.2 mjf {
404 1.3.2.2 mjf subqueue_t *sq;
405 1.3.2.2 mjf const int whichq = lwp_eprio(l) / PPQ;
406 1.3.2.2 mjf
407 1.3.2.2 mjf KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
408 1.3.2.2 mjf
409 1.3.2.2 mjf runqueue_check(rq, whichq, NULL);
410 1.3.2.2 mjf rq->rq_bitmap |= RQMASK(whichq);
411 1.3.2.2 mjf sq = &rq->rq_subqueues[whichq];
412 1.3.2.2 mjf TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_runq);
413 1.3.2.2 mjf runqueue_check(rq, whichq, l);
414 1.3.2.2 mjf }
415 1.3.2.2 mjf
416 1.3.2.2 mjf static void
417 1.3.2.2 mjf runqueue_dequeue(runqueue_t *rq, struct lwp *l)
418 1.3.2.2 mjf {
419 1.3.2.2 mjf subqueue_t *sq;
420 1.3.2.2 mjf const int whichq = lwp_eprio(l) / PPQ;
421 1.3.2.2 mjf
422 1.3.2.2 mjf KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
423 1.3.2.2 mjf
424 1.3.2.2 mjf runqueue_check(rq, whichq, l);
425 1.3.2.2 mjf KASSERT((rq->rq_bitmap & RQMASK(whichq)) != 0);
426 1.3.2.2 mjf sq = &rq->rq_subqueues[whichq];
427 1.3.2.2 mjf TAILQ_REMOVE(&sq->sq_queue, l, l_runq);
428 1.3.2.2 mjf if (TAILQ_EMPTY(&sq->sq_queue))
429 1.3.2.2 mjf rq->rq_bitmap &= ~RQMASK(whichq);
430 1.3.2.2 mjf runqueue_check(rq, whichq, NULL);
431 1.3.2.2 mjf }
432 1.3.2.2 mjf
433 1.3.2.2 mjf static struct lwp *
434 1.3.2.2 mjf runqueue_nextlwp(runqueue_t *rq)
435 1.3.2.2 mjf {
436 1.3.2.2 mjf const uint32_t bitmap = rq->rq_bitmap;
437 1.3.2.2 mjf int whichq;
438 1.3.2.2 mjf
439 1.3.2.2 mjf if (bitmap == 0) {
440 1.3.2.2 mjf return NULL;
441 1.3.2.2 mjf }
442 1.3.2.2 mjf whichq = ffs(bitmap) - 1;
443 1.3.2.2 mjf return TAILQ_FIRST(&rq->rq_subqueues[whichq].sq_queue);
444 1.3.2.2 mjf }
445 1.3.2.2 mjf
446 1.3.2.2 mjf #if defined(DDB)
447 1.3.2.2 mjf static void
448 1.3.2.2 mjf runqueue_print(const runqueue_t *rq, void (*pr)(const char *, ...))
449 1.3.2.2 mjf {
450 1.3.2.2 mjf const uint32_t bitmap = rq->rq_bitmap;
451 1.3.2.2 mjf struct lwp *l;
452 1.3.2.2 mjf int i, first;
453 1.3.2.2 mjf
454 1.3.2.2 mjf for (i = 0; i < RUNQUE_NQS; i++) {
455 1.3.2.2 mjf const subqueue_t *sq;
456 1.3.2.2 mjf first = 1;
457 1.3.2.2 mjf sq = &rq->rq_subqueues[i];
458 1.3.2.2 mjf TAILQ_FOREACH(l, &sq->sq_queue, l_runq) {
459 1.3.2.2 mjf if (first) {
460 1.3.2.2 mjf (*pr)("%c%d",
461 1.3.2.2 mjf (bitmap & RQMASK(i)) ? ' ' : '!', i);
462 1.3.2.2 mjf first = 0;
463 1.3.2.2 mjf }
464 1.3.2.2 mjf (*pr)("\t%d.%d (%s) pri=%d usrpri=%d\n",
465 1.3.2.2 mjf l->l_proc->p_pid,
466 1.3.2.2 mjf l->l_lid, l->l_proc->p_comm,
467 1.3.2.2 mjf (int)l->l_priority, (int)l->l_usrpri);
468 1.3.2.2 mjf }
469 1.3.2.2 mjf }
470 1.3.2.2 mjf }
471 1.3.2.2 mjf #endif /* defined(DDB) */
472 1.3.2.2 mjf #undef RQMASK
473 1.3.2.2 mjf
474 1.3.2.2 mjf /*
475 1.3.2.2 mjf * Initialize the (doubly-linked) run queues
476 1.3.2.2 mjf * to be empty.
477 1.3.2.2 mjf */
478 1.3.2.2 mjf void
479 1.3.2.2 mjf sched_rqinit()
480 1.3.2.2 mjf {
481 1.3.2.2 mjf
482 1.3.2.2 mjf runqueue_init(&global_queue);
483 1.3.2.2 mjf mutex_init(&sched_mutex, MUTEX_SPIN, IPL_SCHED);
484 1.3.2.2 mjf /* Initialize the lock pointer for lwp0 */
485 1.3.2.2 mjf lwp0.l_mutex = &curcpu()->ci_schedstate.spc_lwplock;
486 1.3.2.2 mjf }
487 1.3.2.2 mjf
488 1.3.2.2 mjf void
489 1.3.2.2 mjf sched_cpuattach(struct cpu_info *ci)
490 1.3.2.2 mjf {
491 1.3.2.2 mjf runqueue_t *rq;
492 1.3.2.2 mjf
493 1.3.2.2 mjf ci->ci_schedstate.spc_mutex = &sched_mutex;
494 1.3.2.2 mjf rq = kmem_zalloc(sizeof(*rq), KM_NOSLEEP);
495 1.3.2.2 mjf runqueue_init(rq);
496 1.3.2.2 mjf ci->ci_schedstate.spc_sched_info = rq;
497 1.3.2.2 mjf }
498 1.3.2.2 mjf
499 1.3.2.2 mjf void
500 1.3.2.2 mjf sched_setup()
501 1.3.2.2 mjf {
502 1.3.2.2 mjf
503 1.3.2.2 mjf rrticks = hz / 10;
504 1.3.2.2 mjf }
505 1.3.2.2 mjf
506 1.3.2.2 mjf void
507 1.3.2.2 mjf sched_setrunnable(struct lwp *l)
508 1.3.2.2 mjf {
509 1.3.2.2 mjf
510 1.3.2.2 mjf if (l->l_slptime > 1)
511 1.3.2.2 mjf updatepri(l);
512 1.3.2.2 mjf }
513 1.3.2.2 mjf
514 1.3.2.2 mjf bool
515 1.3.2.2 mjf sched_curcpu_runnable_p(void)
516 1.3.2.2 mjf {
517 1.3.2.2 mjf runqueue_t *rq = curcpu()->ci_schedstate.spc_sched_info;
518 1.3.2.2 mjf
519 1.3.2.2 mjf return (global_queue.rq_bitmap | rq->rq_bitmap) != 0;
520 1.3.2.2 mjf }
521 1.3.2.2 mjf
522 1.3.2.2 mjf void
523 1.3.2.2 mjf sched_nice(struct proc *chgp, int n)
524 1.3.2.2 mjf {
525 1.3.2.2 mjf
526 1.3.2.2 mjf chgp->p_nice = n;
527 1.3.2.2 mjf (void)resetprocpriority(chgp);
528 1.3.2.2 mjf }
529 1.3.2.2 mjf
530 1.3.2.2 mjf /*
531 1.3.2.2 mjf * Compute the priority of a process when running in user mode.
532 1.3.2.2 mjf * Arrange to reschedule if the resulting priority is better
533 1.3.2.2 mjf * than that of the current process.
534 1.3.2.2 mjf */
535 1.3.2.2 mjf static void
536 1.3.2.2 mjf resetpriority(struct lwp *l)
537 1.3.2.2 mjf {
538 1.3.2.2 mjf unsigned int newpriority;
539 1.3.2.2 mjf struct proc *p = l->l_proc;
540 1.3.2.2 mjf
541 1.3.2.2 mjf /* XXXSMP LOCK_ASSERT(mutex_owned(&p->p_stmutex)); */
542 1.3.2.2 mjf LOCK_ASSERT(lwp_locked(l, NULL));
543 1.3.2.2 mjf
544 1.3.2.2 mjf if ((l->l_flag & LW_SYSTEM) != 0)
545 1.3.2.2 mjf return;
546 1.3.2.2 mjf
547 1.3.2.2 mjf newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
548 1.3.2.2 mjf NICE_WEIGHT * (p->p_nice - NZERO);
549 1.3.2.2 mjf newpriority = min(newpriority, MAXPRI);
550 1.3.2.2 mjf lwp_changepri(l, newpriority);
551 1.3.2.2 mjf }
552 1.3.2.2 mjf
553 1.3.2.2 mjf /*
554 1.3.2.2 mjf * Recompute priority for all LWPs in a process.
555 1.3.2.2 mjf */
556 1.3.2.2 mjf static void
557 1.3.2.2 mjf resetprocpriority(struct proc *p)
558 1.3.2.2 mjf {
559 1.3.2.2 mjf struct lwp *l;
560 1.3.2.2 mjf
561 1.3.2.2 mjf KASSERT(mutex_owned(&p->p_stmutex));
562 1.3.2.2 mjf
563 1.3.2.2 mjf LIST_FOREACH(l, &p->p_lwps, l_sibling) {
564 1.3.2.2 mjf lwp_lock(l);
565 1.3.2.2 mjf resetpriority(l);
566 1.3.2.2 mjf lwp_unlock(l);
567 1.3.2.2 mjf }
568 1.3.2.2 mjf }
569 1.3.2.2 mjf
570 1.3.2.2 mjf /*
571 1.3.2.2 mjf * We adjust the priority of the current process. The priority of a process
572 1.3.2.2 mjf * gets worse as it accumulates CPU time. The CPU usage estimator (p_estcpu)
573 1.3.2.2 mjf * is increased here. The formula for computing priorities (in kern_synch.c)
574 1.3.2.2 mjf * will compute a different value each time p_estcpu increases. This can
575 1.3.2.2 mjf * cause a switch, but unless the priority crosses a PPQ boundary the actual
576 1.3.2.2 mjf * queue will not change. The CPU usage estimator ramps up quite quickly
577 1.3.2.2 mjf * when the process is running (linearly), and decays away exponentially, at
578 1.3.2.2 mjf * a rate which is proportionally slower when the system is busy. The basic
579 1.3.2.2 mjf * principle is that the system will 90% forget that the process used a lot
580 1.3.2.2 mjf * of CPU time in 5 * loadav seconds. This causes the system to favor
581 1.3.2.2 mjf * processes which haven't run much recently, and to round-robin among other
582 1.3.2.2 mjf * processes.
583 1.3.2.2 mjf */
584 1.3.2.2 mjf
585 1.3.2.2 mjf void
586 1.3.2.2 mjf sched_schedclock(struct lwp *l)
587 1.3.2.2 mjf {
588 1.3.2.2 mjf struct proc *p = l->l_proc;
589 1.3.2.2 mjf
590 1.3.2.2 mjf KASSERT(!CURCPU_IDLE_P());
591 1.3.2.2 mjf mutex_spin_enter(&p->p_stmutex);
592 1.3.2.2 mjf p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
593 1.3.2.2 mjf lwp_lock(l);
594 1.3.2.2 mjf resetpriority(l);
595 1.3.2.2 mjf mutex_spin_exit(&p->p_stmutex);
596 1.3.2.2 mjf if ((l->l_flag & LW_SYSTEM) == 0 && l->l_priority >= PUSER)
597 1.3.2.2 mjf l->l_priority = l->l_usrpri;
598 1.3.2.2 mjf lwp_unlock(l);
599 1.3.2.2 mjf }
600 1.3.2.2 mjf
601 1.3.2.2 mjf /*
602 1.3.2.2 mjf * sched_proc_fork:
603 1.3.2.2 mjf *
604 1.3.2.2 mjf * Inherit the parent's scheduler history.
605 1.3.2.2 mjf */
606 1.3.2.2 mjf void
607 1.3.2.2 mjf sched_proc_fork(struct proc *parent, struct proc *child)
608 1.3.2.2 mjf {
609 1.3.2.2 mjf
610 1.3.2.2 mjf KASSERT(mutex_owned(&parent->p_smutex));
611 1.3.2.2 mjf
612 1.3.2.2 mjf child->p_estcpu = child->p_estcpu_inherited = parent->p_estcpu;
613 1.3.2.2 mjf child->p_forktime = sched_pstats_ticks;
614 1.3.2.2 mjf }
615 1.3.2.2 mjf
616 1.3.2.2 mjf /*
617 1.3.2.2 mjf * sched_proc_exit:
618 1.3.2.2 mjf *
619 1.3.2.2 mjf * Chargeback parents for the sins of their children.
620 1.3.2.2 mjf */
621 1.3.2.2 mjf void
622 1.3.2.2 mjf sched_proc_exit(struct proc *parent, struct proc *child)
623 1.3.2.2 mjf {
624 1.3.2.2 mjf fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
625 1.3.2.2 mjf fixpt_t estcpu;
626 1.3.2.2 mjf
627 1.3.2.2 mjf /* XXX Only if parent != init?? */
628 1.3.2.2 mjf
629 1.3.2.2 mjf mutex_spin_enter(&parent->p_stmutex);
630 1.3.2.2 mjf estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
631 1.3.2.2 mjf sched_pstats_ticks - child->p_forktime);
632 1.3.2.2 mjf if (child->p_estcpu > estcpu)
633 1.3.2.2 mjf parent->p_estcpu =
634 1.3.2.2 mjf ESTCPULIM(parent->p_estcpu + child->p_estcpu - estcpu);
635 1.3.2.2 mjf mutex_spin_exit(&parent->p_stmutex);
636 1.3.2.2 mjf }
637 1.3.2.2 mjf
638 1.3.2.2 mjf void
639 1.3.2.2 mjf sched_enqueue(struct lwp *l, bool ctxswitch)
640 1.3.2.2 mjf {
641 1.3.2.2 mjf
642 1.3.2.2 mjf if ((l->l_flag & LW_BOUND) != 0)
643 1.3.2.2 mjf runqueue_enqueue(l->l_cpu->ci_schedstate.spc_sched_info, l);
644 1.3.2.2 mjf else
645 1.3.2.2 mjf runqueue_enqueue(&global_queue, l);
646 1.3.2.2 mjf }
647 1.3.2.2 mjf
648 1.3.2.2 mjf /*
649 1.3.2.2 mjf * XXXSMP When LWP dispatch (cpu_switch()) is changed to use sched_dequeue(),
650 1.3.2.2 mjf * drop of the effective priority level from kernel to user needs to be
651 1.3.2.2 mjf * moved here from userret(). The assignment in userret() is currently
652 1.3.2.2 mjf * done unlocked.
653 1.3.2.2 mjf */
654 1.3.2.2 mjf void
655 1.3.2.2 mjf sched_dequeue(struct lwp *l)
656 1.3.2.2 mjf {
657 1.3.2.2 mjf
658 1.3.2.2 mjf if ((l->l_flag & LW_BOUND) != 0)
659 1.3.2.2 mjf runqueue_dequeue(l->l_cpu->ci_schedstate.spc_sched_info, l);
660 1.3.2.2 mjf else
661 1.3.2.2 mjf runqueue_dequeue(&global_queue, l);
662 1.3.2.2 mjf }
663 1.3.2.2 mjf
664 1.3.2.2 mjf struct lwp *
665 1.3.2.2 mjf sched_nextlwp(void)
666 1.3.2.2 mjf {
667 1.3.2.2 mjf lwp_t *l1, *l2;
668 1.3.2.2 mjf
669 1.3.2.2 mjf /* For now, just pick the highest priority LWP. */
670 1.3.2.2 mjf l1 = runqueue_nextlwp(curcpu()->ci_schedstate.spc_sched_info);
671 1.3.2.2 mjf l2 = runqueue_nextlwp(&global_queue);
672 1.3.2.2 mjf
673 1.3.2.2 mjf if (l1 == NULL)
674 1.3.2.2 mjf return l2;
675 1.3.2.2 mjf if (l2 == NULL)
676 1.3.2.2 mjf return l1;
677 1.3.2.2 mjf if (lwp_eprio(l2) < lwp_eprio(l1))
678 1.3.2.2 mjf return l2;
679 1.3.2.2 mjf else
680 1.3.2.2 mjf return l1;
681 1.3.2.2 mjf }
682 1.3.2.2 mjf
683 1.3.2.2 mjf /* Dummy */
684 1.3.2.2 mjf void
685 1.3.2.2 mjf sched_lwp_fork(struct lwp *l)
686 1.3.2.2 mjf {
687 1.3.2.2 mjf
688 1.3.2.2 mjf }
689 1.3.2.2 mjf
690 1.3.2.2 mjf void
691 1.3.2.2 mjf sched_lwp_exit(struct lwp *l)
692 1.3.2.2 mjf {
693 1.3.2.2 mjf
694 1.3.2.2 mjf }
695 1.3.2.2 mjf
696 1.3.2.2 mjf /* SysCtl */
697 1.3.2.2 mjf
698 1.3.2.2 mjf SYSCTL_SETUP(sysctl_sched_setup, "sysctl kern.sched subtree setup")
699 1.3.2.2 mjf {
700 1.3.2.2 mjf const struct sysctlnode *node = NULL;
701 1.3.2.2 mjf
702 1.3.2.2 mjf sysctl_createv(clog, 0, NULL, NULL,
703 1.3.2.2 mjf CTLFLAG_PERMANENT,
704 1.3.2.2 mjf CTLTYPE_NODE, "kern", NULL,
705 1.3.2.2 mjf NULL, 0, NULL, 0,
706 1.3.2.2 mjf CTL_KERN, CTL_EOL);
707 1.3.2.2 mjf sysctl_createv(clog, 0, NULL, &node,
708 1.3.2.2 mjf CTLFLAG_PERMANENT,
709 1.3.2.2 mjf CTLTYPE_NODE, "sched",
710 1.3.2.2 mjf SYSCTL_DESCR("Scheduler options"),
711 1.3.2.2 mjf NULL, 0, NULL, 0,
712 1.3.2.2 mjf CTL_KERN, CTL_CREATE, CTL_EOL);
713 1.3.2.2 mjf
714 1.3.2.2 mjf if (node != NULL) {
715 1.3.2.2 mjf sysctl_createv(clog, 0, &node, NULL,
716 1.3.2.2 mjf CTLFLAG_PERMANENT,
717 1.3.2.2 mjf CTLTYPE_STRING, "name", NULL,
718 1.3.2.2 mjf NULL, 0, __UNCONST("4.4BSD"), 0,
719 1.3.2.2 mjf CTL_CREATE, CTL_EOL);
720 1.3.2.2 mjf }
721 1.3.2.2 mjf }
722 1.3.2.2 mjf
723 1.3.2.2 mjf #if defined(DDB)
724 1.3.2.2 mjf void
725 1.3.2.2 mjf sched_print_runqueue(void (*pr)(const char *, ...))
726 1.3.2.2 mjf {
727 1.3.2.2 mjf
728 1.3.2.2 mjf runqueue_print(&global_queue, pr);
729 1.3.2.2 mjf }
730 1.3.2.2 mjf #endif /* defined(DDB) */
731