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