sched_4bsd.c revision 1.1.6.13 1 /* $NetBSD: sched_4bsd.c,v 1.1.6.13 2007/11/01 21:58:21 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.13 2007/11/01 21:58:21 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 #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_rt; /* realtime */
121 u_int rq_count; /* total # jobs */
122 uint32_t rq_bitmap[NUM_B]; /* bitmap of queues */
123 TAILQ_HEAD(, lwp) rq_queue[NUM_Q]; /* user+kernel */
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 /*
176 * Why PRIO_MAX - 2? From setpriority(2):
177 *
178 * prio is a value in the range -20 to 20. The default priority is
179 * 0; lower priorities cause more favorable scheduling. A value of
180 * 19 or 20 will schedule a process only when nothing at priority <=
181 * 0 is runnable.
182 *
183 * This gives estcpu influence over 18 priority levels, and leaves nice
184 * with 40 levels. One way to think about it is that nice has 20 levels
185 * either side of estcpu's 18.
186 */
187 #define ESTCPU_SHIFT 11
188 #define ESTCPU_MAX ((PRIO_MAX - 2) << ESTCPU_SHIFT)
189 #define ESTCPU_ACCUM (1 << (ESTCPU_SHIFT - 1))
190 #define ESTCPULIM(e) min((e), ESTCPU_MAX)
191
192 /*
193 * Constants for digital decay and forget:
194 * 90% of (l_estcpu) usage in 5 * loadav time
195 * 95% of (l_pctcpu) usage in 60 seconds (load insensitive)
196 * Note that, as ps(1) mentions, this can let percentages
197 * total over 100% (I've seen 137.9% for 3 processes).
198 *
199 * Note that hardclock updates l_estcpu and l_cpticks independently.
200 *
201 * We wish to decay away 90% of l_estcpu in (5 * loadavg) seconds.
202 * That is, the system wants to compute a value of decay such
203 * that the following for loop:
204 * for (i = 0; i < (5 * loadavg); i++)
205 * l_estcpu *= decay;
206 * will compute
207 * l_estcpu *= 0.1;
208 * for all values of loadavg:
209 *
210 * Mathematically this loop can be expressed by saying:
211 * decay ** (5 * loadavg) ~= .1
212 *
213 * The system computes decay as:
214 * decay = (2 * loadavg) / (2 * loadavg + 1)
215 *
216 * We wish to prove that the system's computation of decay
217 * will always fulfill the equation:
218 * decay ** (5 * loadavg) ~= .1
219 *
220 * If we compute b as:
221 * b = 2 * loadavg
222 * then
223 * decay = b / (b + 1)
224 *
225 * We now need to prove two things:
226 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
227 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
228 *
229 * Facts:
230 * For x close to zero, exp(x) =~ 1 + x, since
231 * exp(x) = 0! + x**1/1! + x**2/2! + ... .
232 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
233 * For x close to zero, ln(1+x) =~ x, since
234 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
235 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
236 * ln(.1) =~ -2.30
237 *
238 * Proof of (1):
239 * Solve (factor)**(power) =~ .1 given power (5*loadav):
240 * solving for factor,
241 * ln(factor) =~ (-2.30/5*loadav), or
242 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
243 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
244 *
245 * Proof of (2):
246 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
247 * solving for power,
248 * power*ln(b/(b+1)) =~ -2.30, or
249 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
250 *
251 * Actual power values for the implemented algorithm are as follows:
252 * loadav: 1 2 3 4
253 * power: 5.68 10.32 14.94 19.55
254 */
255
256 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
257 #define loadfactor(loadav) (2 * (loadav))
258
259 fixpt_t
260 decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
261 {
262
263 if (estcpu == 0) {
264 return 0;
265 }
266
267 #if !defined(_LP64)
268 /* avoid 64bit arithmetics. */
269 #define FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
270 if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
271 return estcpu * loadfac / (loadfac + FSCALE);
272 }
273 #endif /* !defined(_LP64) */
274
275 return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
276 }
277
278 /*
279 * For all load averages >= 1 and max l_estcpu of (255 << ESTCPU_SHIFT),
280 * sleeping for at least seven times the loadfactor will decay l_estcpu to
281 * less than (1 << ESTCPU_SHIFT).
282 *
283 * note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
284 */
285 static fixpt_t
286 decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
287 {
288
289 if ((n << FSHIFT) >= 7 * loadfac) {
290 return 0;
291 }
292
293 while (estcpu != 0 && n > 1) {
294 estcpu = decay_cpu(loadfac, estcpu);
295 n--;
296 }
297
298 return estcpu;
299 }
300
301 /*
302 * sched_pstats_hook:
303 *
304 * Periodically called from sched_pstats(); used to recalculate priorities.
305 */
306 void
307 sched_pstats_hook(struct lwp *l)
308 {
309 fixpt_t loadfac;
310
311 /*
312 * If the LWP has slept an entire second, stop recalculating
313 * its priority until it wakes up.
314 */
315 if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
316 l->l_stat == LSSUSPENDED) {
317 l->l_slptime++;
318 if (l->l_slptime <= 1) {
319 loadfac = 2 * (averunnable.ldavg[0]);
320 l->l_estcpu = decay_cpu(loadfac, l->l_estcpu);
321 }
322 }
323 if (l->l_slptime <= 1)
324 resetpriority(l);
325 }
326
327 /*
328 * Recalculate the priority of a process after it has slept for a while.
329 */
330 static void
331 updatepri(struct lwp *l)
332 {
333 fixpt_t loadfac;
334
335 KASSERT(lwp_locked(l, NULL));
336 KASSERT(l->l_slptime > 1);
337
338 loadfac = loadfactor(averunnable.ldavg[0]);
339
340 l->l_slptime--; /* the first time was done in sched_pstats */
341 l->l_estcpu = decay_cpu_batch(loadfac, l->l_estcpu, l->l_slptime);
342 resetpriority(l);
343 }
344
345 static void
346 runqueue_init(runqueue_t *rq)
347 {
348 int i;
349
350 for (i = 0; i < NUM_Q; i++)
351 TAILQ_INIT(&rq->rq_queue[i]);
352 for (i = 0; i < NUM_B; i++)
353 rq->rq_bitmap[i] = 0;
354 TAILQ_INIT(&rq->rq_rt);
355 rq->rq_count = 0;
356 }
357
358 static void
359 runqueue_enqueue(runqueue_t *rq, struct lwp *l)
360 {
361 pri_t pri;
362 lwp_t *l2;
363
364 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
365
366 pri = lwp_eprio(l);
367 rq->rq_count++;
368
369 if (pri >= PRI_USER_RT) {
370 TAILQ_FOREACH(l2, &rq->rq_rt, l_runq) {
371 if (lwp_eprio(l2) < pri) {
372 TAILQ_INSERT_BEFORE(l2, l, l_runq);
373 return;
374 }
375 }
376 TAILQ_INSERT_TAIL(&rq->rq_rt, l, l_runq);
377 return;
378 }
379
380 rq->rq_bitmap[pri >> PPB_SHIFT] |=
381 (0x80000000U >> (pri & PPB_MASK));
382 TAILQ_INSERT_TAIL(&rq->rq_queue[pri], l, l_runq);
383 }
384
385 static void
386 runqueue_dequeue(runqueue_t *rq, struct lwp *l)
387 {
388 pri_t pri;
389
390 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
391
392 pri = lwp_eprio(l);
393 rq->rq_count--;
394
395 if (pri >= PRI_USER_RT) {
396 TAILQ_REMOVE(&rq->rq_rt, l, l_runq);
397 return;
398 }
399
400 TAILQ_REMOVE(&rq->rq_queue[pri], l, l_runq);
401 if (TAILQ_EMPTY(&rq->rq_queue[pri]))
402 rq->rq_bitmap[pri >> PPB_SHIFT] ^=
403 (0x80000000U >> (pri & PPB_MASK));
404 }
405
406 #if (NUM_B != 3) || (NUM_Q != 96)
407 #error adjust runqueue_nextlwp
408 #endif
409
410 static struct lwp *
411 runqueue_nextlwp(runqueue_t *rq)
412 {
413 pri_t pri;
414
415 KASSERT(rq->rq_count != 0);
416
417 if (!TAILQ_EMPTY(&rq->rq_rt))
418 return TAILQ_FIRST(&rq->rq_rt);
419
420 if (rq->rq_bitmap[2] != 0)
421 pri = 96 - ffs(rq->rq_bitmap[2]);
422 else if (rq->rq_bitmap[1] != 0)
423 pri = 64 - ffs(rq->rq_bitmap[1]);
424 else
425 pri = 32 - ffs(rq->rq_bitmap[0]);
426 return TAILQ_FIRST(&rq->rq_queue[pri]);
427 }
428
429 #if defined(DDB)
430 static void
431 runqueue_print(const runqueue_t *rq, void (*pr)(const char *, ...))
432 {
433 CPU_INFO_ITERATOR cii;
434 struct cpu_info *ci;
435 lwp_t *l;
436 int i;
437
438 printf("PID\tLID\tPRI\tIPRI\tEPRI\tLWP\t\t NAME\n");
439
440 TAILQ_FOREACH(l, &rq->rq_rt, l_runq) {
441 (*pr)("%d\t%d\%d\t%d\t%d\t%016lx %s\n",
442 l->l_proc->p_pid, l->l_lid, (int)l->l_priority,
443 (int)l->l_inheritedprio, lwp_eprio(l),
444 (long)l, l->l_proc->p_comm);
445 }
446
447 for (i = NUM_Q - 1; i >= 0; i--) {
448 TAILQ_FOREACH(l, &rq->rq_queue[i], l_runq) {
449 (*pr)("%d\t%d\t%d\t%d\t%d\t%016lx %s\n",
450 l->l_proc->p_pid, l->l_lid, (int)l->l_priority,
451 (int)l->l_inheritedprio, lwp_eprio(l),
452 (long)l, l->l_proc->p_comm);
453 }
454 }
455
456 printf("CPUIDX\tRESCHED\tCURPRI\tFLAGS\n");
457 for (CPU_INFO_FOREACH(cii, ci)) {
458 printf("%d\t%d\t%d\t%04x\n", (int)ci->ci_index,
459 (int)ci->ci_want_resched,
460 (int)ci->ci_schedstate.spc_curpriority,
461 (int)ci->ci_schedstate.spc_flags);
462 }
463
464 printf("NEXTLWP\n%016lx\n", (long)sched_nextlwp());
465 }
466 #endif /* defined(DDB) */
467
468 /*
469 * Initialize the (doubly-linked) run queues
470 * to be empty.
471 */
472 void
473 sched_rqinit()
474 {
475
476 runqueue_init(&global_queue);
477 mutex_init(&sched_mutex, MUTEX_SPIN, IPL_SCHED);
478 /* Initialize the lock pointer for lwp0 */
479 lwp0.l_mutex = &curcpu()->ci_schedstate.spc_lwplock;
480 }
481
482 void
483 sched_cpuattach(struct cpu_info *ci)
484 {
485 runqueue_t *rq;
486
487 ci->ci_schedstate.spc_mutex = &sched_mutex;
488 rq = kmem_zalloc(sizeof(*rq), KM_NOSLEEP);
489 runqueue_init(rq);
490 ci->ci_schedstate.spc_sched_info = rq;
491 }
492
493 void
494 sched_setup()
495 {
496
497 rrticks = hz / 10;
498 }
499
500 void
501 sched_setrunnable(struct lwp *l)
502 {
503
504 if (l->l_slptime > 1)
505 updatepri(l);
506 }
507
508 bool
509 sched_curcpu_runnable_p(void)
510 {
511 struct schedstate_percpu *spc;
512 struct cpu_info *ci;
513 int bits;
514
515 ci = curcpu();
516 spc = &ci->ci_schedstate;
517 #ifndef __HAVE_FAST_SOFTINTS
518 bits = ci->ci_data.cpu_softints;
519 bits |= ((runqueue_t *)spc->spc_sched_info)->rq_count;
520 #else
521 bits = ((runqueue_t *)spc->spc_sched_info)->rq_count;
522 #endif
523 if (__predict_true((spc->spc_flags & SPCF_OFFLINE) == 0))
524 bits |= global_queue.rq_count;
525 return bits != 0;
526 }
527
528 void
529 sched_nice(struct proc *chgp, int n)
530 {
531
532 chgp->p_nice = n;
533 (void)resetprocpriority(chgp);
534 }
535
536 /*
537 * Recompute the priority of an LWP. Arrange to reschedule if
538 * the resulting priority is better than that of the current LWP.
539 */
540 static void
541 resetpriority(struct lwp *l)
542 {
543 pri_t pri;
544 struct proc *p = l->l_proc;
545
546 KASSERT(lwp_locked(l, NULL));
547
548 if (l->l_class != SCHED_OTHER)
549 return;
550
551 /* See comments above ESTCPU_SHIFT definition. */
552 pri = (PRI_KERNEL - 1) - (l->l_estcpu >> ESTCPU_SHIFT) - p->p_nice;
553 pri = imax(pri, 0);
554 if (pri != l->l_priority)
555 lwp_changepri(l, pri);
556 }
557
558 /*
559 * Recompute priority for all LWPs in a process.
560 */
561 static void
562 resetprocpriority(struct proc *p)
563 {
564 struct lwp *l;
565
566 KASSERT(mutex_owned(&p->p_stmutex));
567
568 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
569 lwp_lock(l);
570 resetpriority(l);
571 lwp_unlock(l);
572 }
573 }
574
575 /*
576 * We adjust the priority of the current process. The priority of a process
577 * gets worse as it accumulates CPU time. The CPU usage estimator (l_estcpu)
578 * is increased here. The formula for computing priorities (in kern_synch.c)
579 * will compute a different value each time l_estcpu increases. This can
580 * cause a switch, but unless the priority crosses a PPQ boundary the actual
581 * queue will not change. The CPU usage estimator ramps up quite quickly
582 * when the process is running (linearly), and decays away exponentially, at
583 * a rate which is proportionally slower when the system is busy. The basic
584 * principle is that the system will 90% forget that the process used a lot
585 * of CPU time in 5 * loadav seconds. This causes the system to favor
586 * processes which haven't run much recently, and to round-robin among other
587 * processes.
588 */
589
590 void
591 sched_schedclock(struct lwp *l)
592 {
593 struct proc *p = l->l_proc;
594
595 if (l->l_class != SCHED_OTHER)
596 return;
597
598 KASSERT(!CURCPU_IDLE_P());
599 mutex_spin_enter(&p->p_stmutex);
600 l->l_estcpu = ESTCPULIM(l->l_estcpu + ESTCPU_ACCUM);
601 lwp_lock(l);
602 resetpriority(l);
603 mutex_spin_exit(&p->p_stmutex);
604 lwp_unlock(l);
605 }
606
607 /*
608 * sched_proc_fork:
609 *
610 * Inherit the parent's scheduler history.
611 */
612 void
613 sched_proc_fork(struct proc *parent, struct proc *child)
614 {
615 lwp_t *pl;
616
617 KASSERT(mutex_owned(&parent->p_smutex));
618
619 pl = LIST_FIRST(&parent->p_lwps);
620 child->p_estcpu_inherited = pl->l_estcpu;
621 child->p_forktime = sched_pstats_ticks;
622 }
623
624 /*
625 * sched_proc_exit:
626 *
627 * Chargeback parents for the sins of their children.
628 */
629 void
630 sched_proc_exit(struct proc *parent, struct proc *child)
631 {
632 fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
633 fixpt_t estcpu;
634 lwp_t *pl, *cl;
635
636 /* XXX Only if parent != init?? */
637
638 mutex_enter(&parent->p_smutex);
639 pl = LIST_FIRST(&parent->p_lwps);
640 cl = LIST_FIRST(&child->p_lwps);
641 estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
642 sched_pstats_ticks - child->p_forktime);
643 if (cl->l_estcpu > estcpu) {
644 lwp_lock(pl);
645 pl->l_estcpu = ESTCPULIM(pl->l_estcpu + cl->l_estcpu - estcpu);
646 lwp_unlock(pl);
647 }
648 mutex_exit(&parent->p_smutex);
649 }
650
651 void
652 sched_enqueue(struct lwp *l, bool ctxswitch)
653 {
654
655 if ((l->l_flag & LW_BOUND) != 0)
656 runqueue_enqueue(l->l_cpu->ci_schedstate.spc_sched_info, l);
657 else
658 runqueue_enqueue(&global_queue, l);
659 }
660
661 /*
662 * XXXSMP When LWP dispatch (cpu_switch()) is changed to use sched_dequeue(),
663 * drop of the effective priority level from kernel to user needs to be
664 * moved here from userret(). The assignment in userret() is currently
665 * done unlocked.
666 */
667 void
668 sched_dequeue(struct lwp *l)
669 {
670
671 if ((l->l_flag & LW_BOUND) != 0)
672 runqueue_dequeue(l->l_cpu->ci_schedstate.spc_sched_info, l);
673 else
674 runqueue_dequeue(&global_queue, l);
675 }
676
677 struct lwp *
678 sched_nextlwp(void)
679 {
680 struct schedstate_percpu *spc;
681 runqueue_t *rq;
682 lwp_t *l1, *l2;
683
684 spc = &curcpu()->ci_schedstate;
685
686 /* For now, just pick the highest priority LWP. */
687 rq = spc->spc_sched_info;
688 l1 = NULL;
689 if (rq->rq_count != 0)
690 l1 = runqueue_nextlwp(rq);
691
692 rq = &global_queue;
693 if (__predict_false((spc->spc_flags & SPCF_OFFLINE) != 0) ||
694 rq->rq_count == 0)
695 return l1;
696 l2 = runqueue_nextlwp(rq);
697
698 if (l1 == NULL)
699 return l2;
700 if (l2 == NULL)
701 return l1;
702 if (lwp_eprio(l2) > lwp_eprio(l1))
703 return l2;
704 else
705 return l1;
706 }
707
708 struct cpu_info *
709 sched_takecpu(struct lwp *l)
710 {
711
712 return l->l_cpu;
713 }
714
715 void
716 sched_wakeup(struct lwp *l)
717 {
718
719 }
720
721 void
722 sched_slept(struct lwp *l)
723 {
724
725 }
726
727 void
728 sched_lwp_fork(struct lwp *l)
729 {
730
731 }
732
733 void
734 sched_lwp_exit(struct lwp *l)
735 {
736
737 }
738
739 /*
740 * sysctl setup. XXX This should be split with kern_synch.c.
741 */
742 SYSCTL_SETUP(sysctl_sched_setup, "sysctl kern.sched subtree setup")
743 {
744 const struct sysctlnode *node = NULL;
745
746 sysctl_createv(clog, 0, NULL, NULL,
747 CTLFLAG_PERMANENT,
748 CTLTYPE_NODE, "kern", NULL,
749 NULL, 0, NULL, 0,
750 CTL_KERN, CTL_EOL);
751 sysctl_createv(clog, 0, NULL, &node,
752 CTLFLAG_PERMANENT,
753 CTLTYPE_NODE, "sched",
754 SYSCTL_DESCR("Scheduler options"),
755 NULL, 0, NULL, 0,
756 CTL_KERN, CTL_CREATE, CTL_EOL);
757
758 KASSERT(node != NULL);
759
760 sysctl_createv(clog, 0, &node, NULL,
761 CTLFLAG_PERMANENT,
762 CTLTYPE_STRING, "name", NULL,
763 NULL, 0, __UNCONST("4.4BSD"), 0,
764 CTL_CREATE, CTL_EOL);
765 sysctl_createv(clog, 0, &node, NULL,
766 CTLFLAG_READWRITE,
767 CTLTYPE_INT, "timesoftints",
768 SYSCTL_DESCR("Track CPU time for soft interrupts"),
769 NULL, 0, &softint_timing, 0,
770 CTL_CREATE, CTL_EOL);
771 }
772
773 #if defined(DDB)
774 void
775 sched_print_runqueue(void (*pr)(const char *, ...))
776 {
777
778 runqueue_print(&global_queue, pr);
779 }
780 #endif /* defined(DDB) */
781