sched_4bsd.c revision 1.1.2.23 1 /* $NetBSD: sched_4bsd.c,v 1.1.2.23 2007/04/02 00:28:09 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.2.23 2007/04/02 00:28:09 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
99 #include <uvm/uvm_extern.h>
100
101 /*
102 * Run queues.
103 *
104 * We have 32 run queues in descending priority of 0..31. We maintain
105 * a bitmask of non-empty queues in order speed up finding the first
106 * runnable process. The bitmask is maintained only by machine-dependent
107 * code, allowing the most efficient instructions to be used to find the
108 * first non-empty queue.
109 */
110
111 #define RUNQUE_NQS 32 /* number of runqueues */
112 #define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
113
114 typedef struct subqueue {
115 TAILQ_HEAD(, lwp) sq_queue;
116 } subqueue_t;
117 typedef struct runqueue {
118 subqueue_t rq_subqueues[RUNQUE_NQS]; /* run queues */
119 uint32_t rq_bitmap; /* bitmap of non-empty queues */
120 } runqueue_t;
121 static runqueue_t global_queue;
122
123 static void updatepri(struct lwp *);
124 static void resetpriority(struct lwp *);
125 static void resetprocpriority(struct proc *);
126
127 extern unsigned int sched_pstats_ticks; /* defined in kern_synch.c */
128
129 /* The global scheduler state */
130 kmutex_t sched_mutex;
131
132 /* Number of hardclock ticks per sched_tick() */
133 int rrticks;
134
135 /*
136 * Force switch among equal priority processes every 100ms.
137 * Called from hardclock every hz/10 == rrticks hardclock ticks.
138 */
139 /* ARGSUSED */
140 void
141 sched_tick(struct cpu_info *ci)
142 {
143 struct schedstate_percpu *spc = &ci->ci_schedstate;
144
145 spc->spc_ticks = rrticks;
146
147 if (!CURCPU_IDLE_P()) {
148 if (spc->spc_flags & SPCF_SEENRR) {
149 /*
150 * The process has already been through a roundrobin
151 * without switching and may be hogging the CPU.
152 * Indicate that the process should yield.
153 */
154 spc->spc_flags |= SPCF_SHOULDYIELD;
155 } else
156 spc->spc_flags |= SPCF_SEENRR;
157 }
158 cpu_need_resched(curcpu(), 0);
159 }
160
161 #define NICE_WEIGHT 2 /* priorities per nice level */
162
163 #define ESTCPU_SHIFT 11
164 #define ESTCPU_MAX ((NICE_WEIGHT * PRIO_MAX - PPQ) << ESTCPU_SHIFT)
165 #define ESTCPULIM(e) min((e), ESTCPU_MAX)
166
167 /*
168 * Constants for digital decay and forget:
169 * 90% of (p_estcpu) usage in 5 * loadav time
170 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
171 * Note that, as ps(1) mentions, this can let percentages
172 * total over 100% (I've seen 137.9% for 3 processes).
173 *
174 * Note that hardclock updates p_estcpu and p_cpticks independently.
175 *
176 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
177 * That is, the system wants to compute a value of decay such
178 * that the following for loop:
179 * for (i = 0; i < (5 * loadavg); i++)
180 * p_estcpu *= decay;
181 * will compute
182 * p_estcpu *= 0.1;
183 * for all values of loadavg:
184 *
185 * Mathematically this loop can be expressed by saying:
186 * decay ** (5 * loadavg) ~= .1
187 *
188 * The system computes decay as:
189 * decay = (2 * loadavg) / (2 * loadavg + 1)
190 *
191 * We wish to prove that the system's computation of decay
192 * will always fulfill the equation:
193 * decay ** (5 * loadavg) ~= .1
194 *
195 * If we compute b as:
196 * b = 2 * loadavg
197 * then
198 * decay = b / (b + 1)
199 *
200 * We now need to prove two things:
201 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
202 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
203 *
204 * Facts:
205 * For x close to zero, exp(x) =~ 1 + x, since
206 * exp(x) = 0! + x**1/1! + x**2/2! + ... .
207 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
208 * For x close to zero, ln(1+x) =~ x, since
209 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
210 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
211 * ln(.1) =~ -2.30
212 *
213 * Proof of (1):
214 * Solve (factor)**(power) =~ .1 given power (5*loadav):
215 * solving for factor,
216 * ln(factor) =~ (-2.30/5*loadav), or
217 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
218 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
219 *
220 * Proof of (2):
221 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
222 * solving for power,
223 * power*ln(b/(b+1)) =~ -2.30, or
224 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
225 *
226 * Actual power values for the implemented algorithm are as follows:
227 * loadav: 1 2 3 4
228 * power: 5.68 10.32 14.94 19.55
229 */
230
231 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
232 #define loadfactor(loadav) (2 * (loadav))
233
234 static fixpt_t
235 decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
236 {
237
238 if (estcpu == 0) {
239 return 0;
240 }
241
242 #if !defined(_LP64)
243 /* avoid 64bit arithmetics. */
244 #define FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
245 if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
246 return estcpu * loadfac / (loadfac + FSCALE);
247 }
248 #endif /* !defined(_LP64) */
249
250 return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
251 }
252
253 /*
254 * For all load averages >= 1 and max p_estcpu of (255 << ESTCPU_SHIFT),
255 * sleeping for at least seven times the loadfactor will decay p_estcpu to
256 * less than (1 << ESTCPU_SHIFT).
257 *
258 * note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
259 */
260 static fixpt_t
261 decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
262 {
263
264 if ((n << FSHIFT) >= 7 * loadfac) {
265 return 0;
266 }
267
268 while (estcpu != 0 && n > 1) {
269 estcpu = decay_cpu(loadfac, estcpu);
270 n--;
271 }
272
273 return estcpu;
274 }
275
276 /*
277 * sched_pstats_hook:
278 *
279 * Periodically called from sched_pstats(); used to recalculate priorities.
280 */
281 inline void
282 sched_pstats_hook(struct proc *p, int minslp)
283 {
284 struct lwp *l;
285 fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
286
287 /*
288 * If the process has slept the entire second,
289 * stop recalculating its priority until it wakes up.
290 */
291 if (minslp <= 1) {
292 p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
293
294 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
295 if ((l->l_flag & LW_IDLE) != 0)
296 continue;
297 lwp_lock(l);
298 if (l->l_slptime <= 1 &&
299 l->l_priority >= PUSER)
300 resetpriority(l);
301 lwp_unlock(l);
302 }
303 }
304 }
305
306 /*
307 * Recalculate the priority of a process after it has slept for a while.
308 */
309 static void
310 updatepri(struct lwp *l)
311 {
312 struct proc *p = l->l_proc;
313 fixpt_t loadfac;
314
315 LOCK_ASSERT(lwp_locked(l, NULL));
316 KASSERT(l->l_slptime > 1);
317
318 loadfac = loadfactor(averunnable.ldavg[0]);
319
320 l->l_slptime--; /* the first time was done in sched_pstats */
321 /* XXX NJWLWP */
322 /* XXXSMP occasionally unlocked, should be per-LWP */
323 p->p_estcpu = decay_cpu_batch(loadfac, p->p_estcpu, l->l_slptime);
324 resetpriority(l);
325 }
326
327 /*
328 * On some architectures, it's faster to use a MSB ordering for the priorites
329 * than the traditional LSB ordering.
330 */
331 #ifdef __HAVE_BIGENDIAN_BITOPS
332 #define RQMASK(n) (0x80000000 >> (n))
333 #else
334 #define RQMASK(n) (0x00000001 << (n))
335 #endif
336
337 /*
338 * The primitives that manipulate the run queues. whichqs tells which
339 * of the 32 queues qs have processes in them. sched_enqueue() puts processes
340 * into queues, sched_dequeue removes them from queues. The running process is
341 * on no queue, other processes are on a queue related to p->p_priority,
342 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
343 * available queues.
344 */
345 #ifdef RQDEBUG
346 static void
347 runqueue_check(const runqueue_t *rq, int whichq, struct lwp *l)
348 {
349 const subqueue_t * const sq = &rq->rq_subqueues[whichq];
350 const uint32_t bitmap = rq->rq_bitmap;
351 struct lwp *l2;
352 int found = 0;
353 int die = 0;
354 int empty = 1;
355
356 TAILQ_FOREACH(l2, &sq->sq_queue, l_runq) {
357 if (l2->l_stat != LSRUN) {
358 printf("runqueue_check[%d]: lwp %p state (%d) "
359 " != LSRUN\n", whichq, l2, l2->l_stat);
360 }
361 if (l2 == l)
362 found = 1;
363 empty = 0;
364 }
365 if (empty && (bitmap & RQMASK(whichq)) != 0) {
366 printf("runqueue_check[%d]: bit set for empty run-queue %p\n",
367 whichq, rq);
368 die = 1;
369 } else if (!empty && (bitmap & RQMASK(whichq)) == 0) {
370 printf("runqueue_check[%d]: bit clear for non-empty "
371 "run-queue %p\n", whichq, rq);
372 die = 1;
373 }
374 if (l != NULL && (bitmap & RQMASK(whichq)) == 0) {
375 printf("runqueue_check[%d]: bit clear for active lwp %p\n",
376 whichq, l);
377 die = 1;
378 }
379 if (l != NULL && empty) {
380 printf("runqueue_check[%d]: empty run-queue %p with "
381 "active lwp %p\n", whichq, rq, l);
382 die = 1;
383 }
384 if (l != NULL && !found) {
385 printf("runqueue_check[%d]: lwp %p not in runqueue %p!",
386 whichq, l, rq);
387 die = 1;
388 }
389 if (die)
390 panic("runqueue_check: inconsistency found");
391 }
392 #else /* RQDEBUG */
393 #define runqueue_check(a, b, c) /* nothing */
394 #endif /* RQDEBUG */
395
396 static void
397 runqueue_init(runqueue_t *rq)
398 {
399 int i;
400
401 for (i = 0; i < RUNQUE_NQS; i++)
402 TAILQ_INIT(&rq->rq_subqueues[i].sq_queue);
403 }
404
405 static void
406 runqueue_enqueue(runqueue_t *rq, struct lwp *l)
407 {
408 subqueue_t *sq;
409 const int whichq = lwp_eprio(l) / PPQ;
410
411 LOCK_ASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
412
413 runqueue_check(rq, whichq, NULL);
414 rq->rq_bitmap |= RQMASK(whichq);
415 sq = &rq->rq_subqueues[whichq];
416 TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_runq);
417 runqueue_check(rq, whichq, l);
418 }
419
420 static void
421 runqueue_dequeue(runqueue_t *rq, struct lwp *l)
422 {
423 subqueue_t *sq;
424 const int whichq = lwp_eprio(l) / PPQ;
425
426 LOCK_ASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
427
428 runqueue_check(rq, whichq, l);
429 KASSERT((rq->rq_bitmap & RQMASK(whichq)) != 0);
430 sq = &rq->rq_subqueues[whichq];
431 TAILQ_REMOVE(&sq->sq_queue, l, l_runq);
432 if (TAILQ_EMPTY(&sq->sq_queue))
433 rq->rq_bitmap &= ~RQMASK(whichq);
434 runqueue_check(rq, whichq, NULL);
435 }
436
437 static struct lwp *
438 runqueue_nextlwp(runqueue_t *rq)
439 {
440 const uint32_t bitmap = rq->rq_bitmap;
441 int whichq;
442
443 if (bitmap == 0) {
444 return NULL;
445 }
446 #ifdef __HAVE_BIGENDIAN_BITOPS
447 /* XXX should introduce a fast "fls" function. */
448 for (whichq = 0; ; whichq++) {
449 if ((bitmap & RQMASK(whichq)) != 0) {
450 break;
451 }
452 }
453 #else
454 whichq = ffs(bitmap) - 1;
455 #endif
456 return TAILQ_FIRST(&rq->rq_subqueues[whichq].sq_queue);
457 }
458
459 #if defined(DDB)
460 static void
461 runqueue_print(const runqueue_t *rq, void (*pr)(const char *, ...))
462 {
463 const uint32_t bitmap = rq->rq_bitmap;
464 struct lwp *l;
465 int i, first;
466
467 for (i = 0; i < RUNQUE_NQS; i++) {
468 const subqueue_t *sq;
469 first = 1;
470 sq = &rq->rq_subqueues[i];
471 TAILQ_FOREACH(l, &sq->sq_queue, l_runq) {
472 if (first) {
473 (*pr)("%c%d",
474 (bitmap & RQMASK(i)) ? ' ' : '!', i);
475 first = 0;
476 }
477 (*pr)("\t%d.%d (%s) pri=%d usrpri=%d\n",
478 l->l_proc->p_pid,
479 l->l_lid, l->l_proc->p_comm,
480 (int)l->l_priority, (int)l->l_usrpri);
481 }
482 }
483 }
484 #endif /* defined(DDB) */
485 #undef RQMASK
486
487 /*
488 * Initialize the (doubly-linked) run queues
489 * to be empty.
490 */
491 void
492 sched_rqinit()
493 {
494
495 runqueue_init(&global_queue);
496 mutex_init(&sched_mutex, MUTEX_SPIN, IPL_SCHED);
497 /* Initialize the lock pointer for lwp0 */
498 lwp0.l_mutex = &sched_mutex;
499 }
500
501 void
502 sched_cpuattach(struct cpu_info *ci)
503 {
504
505 ci->ci_schedstate.spc_mutex = &sched_mutex;
506 }
507
508 void
509 sched_setup()
510 {
511
512 rrticks = hz / 10;
513 sched_pstats(NULL);
514 }
515
516 void
517 sched_setrunnable(struct lwp *l)
518 {
519
520 if (l->l_slptime > 1)
521 updatepri(l);
522 }
523
524 bool
525 sched_curcpu_runnable_p(void)
526 {
527
528 return global_queue.rq_bitmap != 0;
529 }
530
531 void
532 sched_nice(struct proc *chgp, int n)
533 {
534
535 chgp->p_nice = n;
536 (void)resetprocpriority(chgp);
537 }
538
539 /*
540 * Compute the priority of a process when running in user mode.
541 * Arrange to reschedule if the resulting priority is better
542 * than that of the current process.
543 */
544 static void
545 resetpriority(struct lwp *l)
546 {
547 unsigned int newpriority;
548 struct proc *p = l->l_proc;
549
550 /* XXXSMP LOCK_ASSERT(mutex_owned(&p->p_stmutex)); */
551 LOCK_ASSERT(lwp_locked(l, NULL));
552
553 if ((l->l_flag & LW_SYSTEM) != 0)
554 return;
555
556 newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
557 NICE_WEIGHT * (p->p_nice - NZERO);
558 newpriority = min(newpriority, MAXPRI);
559 lwp_changepri(l, newpriority);
560 }
561
562 /*
563 * Recompute priority for all LWPs in a process.
564 */
565 static void
566 resetprocpriority(struct proc *p)
567 {
568 struct lwp *l;
569
570 LOCK_ASSERT(mutex_owned(&p->p_stmutex));
571
572 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
573 lwp_lock(l);
574 resetpriority(l);
575 lwp_unlock(l);
576 }
577 }
578
579 /*
580 * We adjust the priority of the current process. The priority of a process
581 * gets worse as it accumulates CPU time. The CPU usage estimator (p_estcpu)
582 * is increased here. The formula for computing priorities (in kern_synch.c)
583 * will compute a different value each time p_estcpu increases. This can
584 * cause a switch, but unless the priority crosses a PPQ boundary the actual
585 * queue will not change. The CPU usage estimator ramps up quite quickly
586 * when the process is running (linearly), and decays away exponentially, at
587 * a rate which is proportionally slower when the system is busy. The basic
588 * principle is that the system will 90% forget that the process used a lot
589 * of CPU time in 5 * loadav seconds. This causes the system to favor
590 * processes which haven't run much recently, and to round-robin among other
591 * processes.
592 */
593
594 void
595 sched_schedclock(struct lwp *l)
596 {
597 struct proc *p = l->l_proc;
598
599 KASSERT(!CURCPU_IDLE_P());
600 mutex_spin_enter(&p->p_stmutex);
601 p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
602 lwp_lock(l);
603 resetpriority(l);
604 mutex_spin_exit(&p->p_stmutex);
605 if ((l->l_flag & LW_SYSTEM) == 0 && l->l_priority >= PUSER)
606 l->l_priority = l->l_usrpri;
607 lwp_unlock(l);
608 }
609
610 /*
611 * scheduler_fork_hook:
612 *
613 * Inherit the parent's scheduler history.
614 */
615 void
616 sched_proc_fork(struct proc *parent, struct proc *child)
617 {
618
619 LOCK_ASSERT(mutex_owned(&parent->p_smutex));
620
621 child->p_estcpu = child->p_estcpu_inherited = parent->p_estcpu;
622 child->p_forktime = sched_pstats_ticks;
623 }
624
625 /*
626 * scheduler_wait_hook:
627 *
628 * Chargeback parents for the sins of their children.
629 */
630 void
631 sched_proc_exit(struct proc *parent, struct proc *child)
632 {
633 fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
634 fixpt_t estcpu;
635
636 /* XXX Only if parent != init?? */
637
638 mutex_spin_enter(&parent->p_stmutex);
639 estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
640 sched_pstats_ticks - child->p_forktime);
641 if (child->p_estcpu > estcpu)
642 parent->p_estcpu =
643 ESTCPULIM(parent->p_estcpu + child->p_estcpu - estcpu);
644 mutex_spin_exit(&parent->p_stmutex);
645 }
646
647 void
648 sched_enqueue(struct lwp *l, bool ctxswitch)
649 {
650
651 runqueue_enqueue(&global_queue, l);
652 }
653
654 /*
655 * XXXSMP When LWP dispatch (cpu_switch()) is changed to use sched_dequeue(),
656 * drop of the effective priority level from kernel to user needs to be
657 * moved here from userret(). The assignment in userret() is currently
658 * done unlocked.
659 */
660 void
661 sched_dequeue(struct lwp *l)
662 {
663
664 runqueue_dequeue(&global_queue, l);
665 }
666
667 struct lwp *
668 sched_nextlwp()
669 {
670
671 return runqueue_nextlwp(&global_queue);
672 }
673
674 /* Dummy */
675 void
676 sched_lwp_fork(struct lwp *l)
677 {
678
679 }
680
681 void
682 sched_lwp_exit(struct lwp *l)
683 {
684
685 }
686
687 void
688 sched_slept(struct lwp *l)
689 {
690
691 }
692
693 /* SysCtl */
694
695 SYSCTL_SETUP(sysctl_sched_setup, "sysctl kern.sched subtree setup")
696 {
697 sysctl_createv(clog, 0, NULL, NULL,
698 CTLFLAG_PERMANENT,
699 CTLTYPE_NODE, "kern", NULL,
700 NULL, 0, NULL, 0,
701 CTL_KERN, CTL_EOL);
702 sysctl_createv(clog, 0, NULL, NULL,
703 CTLFLAG_PERMANENT,
704 CTLTYPE_NODE, "sched",
705 SYSCTL_DESCR("Scheduler options"),
706 NULL, 0, NULL, 0,
707 CTL_KERN, KERN_SCHED, CTL_EOL);
708 sysctl_createv(clog, 0, NULL, NULL,
709 CTLFLAG_PERMANENT,
710 CTLTYPE_STRING, "name", NULL,
711 NULL, 0, __UNCONST("4.4BSD"), 0,
712 CTL_KERN, KERN_SCHED, CTL_CREATE, CTL_EOL);
713 }
714
715 #if defined(DDB)
716 void
717 sched_print_runqueue(void (*pr)(const char *, ...))
718 {
719
720 runqueue_print(&global_queue, pr);
721 }
722 #endif /* defined(DDB) */
723