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