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