kern_synch.c revision 1.177.2.2 1 /* $NetBSD: kern_synch.c,v 1.177.2.2 2007/02/17 11:00:52 yamt 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, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 1982, 1986, 1990, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.177.2.2 2007/02/17 11:00:52 yamt Exp $");
78
79 #include "opt_ddb.h"
80 #include "opt_kstack.h"
81 #include "opt_lockdebug.h"
82 #include "opt_multiprocessor.h"
83 #include "opt_perfctrs.h"
84
85 #define __MUTEX_PRIVATE
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/callout.h>
90 #include <sys/proc.h>
91 #include <sys/kernel.h>
92 #include <sys/buf.h>
93 #if defined(PERFCTRS)
94 #include <sys/pmc.h>
95 #endif
96 #include <sys/signalvar.h>
97 #include <sys/resourcevar.h>
98 #include <sys/sched.h>
99 #include <sys/kauth.h>
100 #include <sys/sleepq.h>
101 #include <sys/lockdebug.h>
102
103 #include <uvm/uvm_extern.h>
104
105 #include <machine/cpu.h>
106
107 int lbolt; /* once a second sleep address */
108 int rrticks; /* number of hardclock ticks per roundrobin() */
109
110 /*
111 * The global scheduler state.
112 */
113 kmutex_t sched_mutex; /* global sched state mutex */
114 struct prochd sched_qs[RUNQUE_NQS]; /* run queues */
115 volatile uint32_t sched_whichqs; /* bitmap of non-empty queues */
116
117 void schedcpu(void *);
118 void updatepri(struct lwp *);
119
120 void sched_unsleep(struct lwp *);
121 void sched_changepri(struct lwp *, int);
122
123 struct callout schedcpu_ch = CALLOUT_INITIALIZER_SETFUNC(schedcpu, NULL);
124 static unsigned int schedcpu_ticks;
125
126 syncobj_t sleep_syncobj = {
127 SOBJ_SLEEPQ_SORTED,
128 sleepq_unsleep,
129 sleepq_changepri
130 };
131
132 syncobj_t sched_syncobj = {
133 SOBJ_SLEEPQ_SORTED,
134 sched_unsleep,
135 sched_changepri
136 };
137
138 /*
139 * Force switch among equal priority processes every 100ms.
140 * Called from hardclock every hz/10 == rrticks hardclock ticks.
141 */
142 /* ARGSUSED */
143 void
144 roundrobin(struct cpu_info *ci)
145 {
146 struct schedstate_percpu *spc = &ci->ci_schedstate;
147
148 spc->spc_rrticks = rrticks;
149
150 if (!CURCPU_IDLE_P()) {
151 if (spc->spc_flags & SPCF_SEENRR) {
152 /*
153 * The process has already been through a roundrobin
154 * without switching and may be hogging the CPU.
155 * Indicate that the process should yield.
156 */
157 spc->spc_flags |= SPCF_SHOULDYIELD;
158 } else
159 spc->spc_flags |= SPCF_SEENRR;
160 }
161 cpu_need_resched(curcpu());
162 }
163
164 #define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
165 #define NICE_WEIGHT 2 /* priorities per nice level */
166
167 #define ESTCPU_SHIFT 11
168 #define ESTCPU_MAX ((NICE_WEIGHT * PRIO_MAX - PPQ) << ESTCPU_SHIFT)
169 #define ESTCPULIM(e) min((e), ESTCPU_MAX)
170
171 /*
172 * Constants for digital decay and forget:
173 * 90% of (p_estcpu) usage in 5 * loadav time
174 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
175 * Note that, as ps(1) mentions, this can let percentages
176 * total over 100% (I've seen 137.9% for 3 processes).
177 *
178 * Note that hardclock updates p_estcpu and p_cpticks independently.
179 *
180 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
181 * That is, the system wants to compute a value of decay such
182 * that the following for loop:
183 * for (i = 0; i < (5 * loadavg); i++)
184 * p_estcpu *= decay;
185 * will compute
186 * p_estcpu *= 0.1;
187 * for all values of loadavg:
188 *
189 * Mathematically this loop can be expressed by saying:
190 * decay ** (5 * loadavg) ~= .1
191 *
192 * The system computes decay as:
193 * decay = (2 * loadavg) / (2 * loadavg + 1)
194 *
195 * We wish to prove that the system's computation of decay
196 * will always fulfill the equation:
197 * decay ** (5 * loadavg) ~= .1
198 *
199 * If we compute b as:
200 * b = 2 * loadavg
201 * then
202 * decay = b / (b + 1)
203 *
204 * We now need to prove two things:
205 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
206 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
207 *
208 * Facts:
209 * For x close to zero, exp(x) =~ 1 + x, since
210 * exp(x) = 0! + x**1/1! + x**2/2! + ... .
211 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
212 * For x close to zero, ln(1+x) =~ x, since
213 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
214 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
215 * ln(.1) =~ -2.30
216 *
217 * Proof of (1):
218 * Solve (factor)**(power) =~ .1 given power (5*loadav):
219 * solving for factor,
220 * ln(factor) =~ (-2.30/5*loadav), or
221 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
222 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
223 *
224 * Proof of (2):
225 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
226 * solving for power,
227 * power*ln(b/(b+1)) =~ -2.30, or
228 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
229 *
230 * Actual power values for the implemented algorithm are as follows:
231 * loadav: 1 2 3 4
232 * power: 5.68 10.32 14.94 19.55
233 */
234
235 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
236 #define loadfactor(loadav) (2 * (loadav))
237
238 static fixpt_t
239 decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
240 {
241
242 if (estcpu == 0) {
243 return 0;
244 }
245
246 #if !defined(_LP64)
247 /* avoid 64bit arithmetics. */
248 #define FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
249 if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
250 return estcpu * loadfac / (loadfac + FSCALE);
251 }
252 #endif /* !defined(_LP64) */
253
254 return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
255 }
256
257 /*
258 * For all load averages >= 1 and max p_estcpu of (255 << ESTCPU_SHIFT),
259 * sleeping for at least seven times the loadfactor will decay p_estcpu to
260 * less than (1 << ESTCPU_SHIFT).
261 *
262 * note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
263 */
264 static fixpt_t
265 decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
266 {
267
268 if ((n << FSHIFT) >= 7 * loadfac) {
269 return 0;
270 }
271
272 while (estcpu != 0 && n > 1) {
273 estcpu = decay_cpu(loadfac, estcpu);
274 n--;
275 }
276
277 return estcpu;
278 }
279
280 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
281 fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
282
283 /*
284 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
285 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
286 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
287 *
288 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
289 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
290 *
291 * If you dont want to bother with the faster/more-accurate formula, you
292 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
293 * (more general) method of calculating the %age of CPU used by a process.
294 */
295 #define CCPU_SHIFT 11
296
297 /*
298 * schedcpu:
299 *
300 * Recompute process priorities, every hz ticks.
301 *
302 * XXXSMP This needs to be reorganised in order to reduce the locking
303 * burden.
304 */
305 /* ARGSUSED */
306 void
307 schedcpu(void *arg)
308 {
309 fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
310 struct rlimit *rlim;
311 struct lwp *l;
312 struct proc *p;
313 int minslp, clkhz, sig;
314 long runtm;
315
316 schedcpu_ticks++;
317
318 mutex_enter(&proclist_mutex);
319 PROCLIST_FOREACH(p, &allproc) {
320 /*
321 * Increment time in/out of memory and sleep time (if
322 * sleeping). We ignore overflow; with 16-bit int's
323 * (remember them?) overflow takes 45 days.
324 */
325 minslp = 2;
326 mutex_enter(&p->p_smutex);
327 runtm = p->p_rtime.tv_sec;
328 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
329 if ((l->l_flag & L_IDLE) != 0)
330 continue;
331 lwp_lock(l);
332 runtm += l->l_rtime.tv_sec;
333 l->l_swtime++;
334 if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
335 l->l_stat == LSSUSPENDED) {
336 l->l_slptime++;
337 minslp = min(minslp, l->l_slptime);
338 } else
339 minslp = 0;
340 lwp_unlock(l);
341 }
342 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
343
344 /*
345 * Check if the process exceeds its CPU resource allocation.
346 * If over max, kill it.
347 */
348 rlim = &p->p_rlimit[RLIMIT_CPU];
349 sig = 0;
350 if (runtm >= rlim->rlim_cur) {
351 if (runtm >= rlim->rlim_max)
352 sig = SIGKILL;
353 else {
354 sig = SIGXCPU;
355 if (rlim->rlim_cur < rlim->rlim_max)
356 rlim->rlim_cur += 5;
357 }
358 }
359
360 /*
361 * If the process has run for more than autonicetime, reduce
362 * priority to give others a chance.
363 */
364 if (autonicetime && runtm > autonicetime && p->p_nice == NZERO
365 && kauth_cred_geteuid(p->p_cred)) {
366 mutex_spin_enter(&p->p_stmutex);
367 p->p_nice = autoniceval + NZERO;
368 resetprocpriority(p);
369 mutex_spin_exit(&p->p_stmutex);
370 }
371
372 /*
373 * If the process has slept the entire second,
374 * stop recalculating its priority until it wakes up.
375 */
376 if (minslp <= 1) {
377 /*
378 * p_pctcpu is only for ps.
379 */
380 mutex_spin_enter(&p->p_stmutex);
381 clkhz = stathz != 0 ? stathz : hz;
382 #if (FSHIFT >= CCPU_SHIFT)
383 p->p_pctcpu += (clkhz == 100)?
384 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
385 100 * (((fixpt_t) p->p_cpticks)
386 << (FSHIFT - CCPU_SHIFT)) / clkhz;
387 #else
388 p->p_pctcpu += ((FSCALE - ccpu) *
389 (p->p_cpticks * FSCALE / clkhz)) >> FSHIFT;
390 #endif
391 p->p_cpticks = 0;
392 p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
393
394 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
395 if ((l->l_flag & L_IDLE) != 0)
396 continue;
397 lwp_lock(l);
398 if (l->l_slptime <= 1 &&
399 l->l_priority >= PUSER)
400 resetpriority(l);
401 lwp_unlock(l);
402 }
403 mutex_spin_exit(&p->p_stmutex);
404 }
405
406 mutex_exit(&p->p_smutex);
407 if (sig) {
408 psignal(p, sig);
409 }
410 }
411 mutex_exit(&proclist_mutex);
412 uvm_meter();
413 wakeup((caddr_t)&lbolt);
414 callout_schedule(&schedcpu_ch, hz);
415 }
416
417 /*
418 * Recalculate the priority of a process after it has slept for a while.
419 */
420 void
421 updatepri(struct lwp *l)
422 {
423 struct proc *p = l->l_proc;
424 fixpt_t loadfac;
425
426 LOCK_ASSERT(lwp_locked(l, NULL));
427 KASSERT(l->l_slptime > 1);
428
429 loadfac = loadfactor(averunnable.ldavg[0]);
430
431 l->l_slptime--; /* the first time was done in schedcpu */
432 /* XXX NJWLWP */
433 /* XXXSMP occasionally unlocked, should be per-LWP */
434 p->p_estcpu = decay_cpu_batch(loadfac, p->p_estcpu, l->l_slptime);
435 resetpriority(l);
436 }
437
438 /*
439 * During autoconfiguration or after a panic, a sleep will simply lower the
440 * priority briefly to allow interrupts, then return. The priority to be
441 * used (safepri) is machine-dependent, thus this value is initialized and
442 * maintained in the machine-dependent layers. This priority will typically
443 * be 0, or the lowest priority that is safe for use on the interrupt stack;
444 * it can be made higher to block network software interrupts after panics.
445 */
446 int safepri;
447
448 /*
449 * OBSOLETE INTERFACE
450 *
451 * General sleep call. Suspends the current process until a wakeup is
452 * performed on the specified identifier. The process will then be made
453 * runnable with the specified priority. Sleeps at most timo/hz seconds (0
454 * means no timeout). If pri includes PCATCH flag, signals are checked
455 * before and after sleeping, else signals are not checked. Returns 0 if
456 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
457 * signal needs to be delivered, ERESTART is returned if the current system
458 * call should be restarted if possible, and EINTR is returned if the system
459 * call should be interrupted by the signal (return EINTR).
460 *
461 * The interlock is held until we are on a sleep queue. The interlock will
462 * be locked before returning back to the caller unless the PNORELOCK flag
463 * is specified, in which case the interlock will always be unlocked upon
464 * return.
465 */
466 int
467 ltsleep(wchan_t ident, int priority, const char *wmesg, int timo,
468 volatile struct simplelock *interlock)
469 {
470 struct lwp *l = curlwp;
471 sleepq_t *sq;
472 int error, catch;
473
474 if (sleepq_dontsleep(l)) {
475 (void)sleepq_abort(NULL, 0);
476 if ((priority & PNORELOCK) != 0)
477 simple_unlock(interlock);
478 return 0;
479 }
480
481 sq = sleeptab_lookup(&sleeptab, ident);
482 sleepq_enter(sq, l);
483
484 if (interlock != NULL) {
485 LOCK_ASSERT(simple_lock_held(interlock));
486 simple_unlock(interlock);
487 }
488
489 catch = priority & PCATCH;
490 sleepq_block(sq, priority & PRIMASK, ident, wmesg, timo, catch,
491 &sleep_syncobj);
492 error = sleepq_unblock(timo, catch);
493
494 if (interlock != NULL && (priority & PNORELOCK) == 0)
495 simple_lock(interlock);
496
497 return error;
498 }
499
500 /*
501 * General sleep call for situations where a wake-up is not expected.
502 */
503 int
504 kpause(const char *wmesg, boolean_t intr, int timo, kmutex_t *mtx)
505 {
506 struct lwp *l = curlwp;
507 sleepq_t *sq;
508 int error;
509
510 if (sleepq_dontsleep(l))
511 return sleepq_abort(NULL, 0);
512
513 if (mtx != NULL)
514 mutex_exit(mtx);
515 sq = sleeptab_lookup(&sleeptab, l);
516 sleepq_enter(sq, l);
517 sleepq_block(sq, sched_kpri(l), l, wmesg, timo, intr, &sleep_syncobj);
518 error = sleepq_unblock(timo, intr);
519 if (mtx != NULL)
520 mutex_enter(mtx);
521
522 return error;
523 }
524
525 /*
526 * OBSOLETE INTERFACE
527 *
528 * Make all processes sleeping on the specified identifier runnable.
529 */
530 void
531 wakeup(wchan_t ident)
532 {
533 sleepq_t *sq;
534
535 if (cold)
536 return;
537
538 sq = sleeptab_lookup(&sleeptab, ident);
539 sleepq_wake(sq, ident, (u_int)-1);
540 }
541
542 /*
543 * OBSOLETE INTERFACE
544 *
545 * Make the highest priority process first in line on the specified
546 * identifier runnable.
547 */
548 void
549 wakeup_one(wchan_t ident)
550 {
551 sleepq_t *sq;
552
553 if (cold)
554 return;
555
556 sq = sleeptab_lookup(&sleeptab, ident);
557 sleepq_wake(sq, ident, 1);
558 }
559
560
561 /*
562 * General yield call. Puts the current process back on its run queue and
563 * performs a voluntary context switch. Should only be called when the
564 * current process explicitly requests it (eg sched_yield(2) in compat code).
565 */
566 void
567 yield(void)
568 {
569 struct lwp *l = curlwp;
570
571 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
572 lwp_lock(l);
573 if (l->l_stat == LSONPROC) {
574 KASSERT(lwp_locked(l, &sched_mutex));
575 l->l_priority = l->l_usrpri;
576 }
577 l->l_nvcsw++;
578 mi_switch(l, NULL);
579 KERNEL_LOCK(l->l_biglocks, l);
580 }
581
582 /*
583 * General preemption call. Puts the current process back on its run queue
584 * and performs an involuntary context switch.
585 */
586 void
587 preempt(void)
588 {
589 struct lwp *l = curlwp;
590
591 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
592 lwp_lock(l);
593 if (l->l_stat == LSONPROC) {
594 KASSERT(lwp_locked(l, &sched_mutex));
595 l->l_priority = l->l_usrpri;
596 }
597 l->l_nivcsw++;
598 (void)mi_switch(l, NULL);
599 KERNEL_LOCK(l->l_biglocks, l);
600 }
601
602 /*
603 * sched_switch_unlock: update 'curlwp' and release old lwp.
604 */
605
606 void
607 sched_switch_unlock(struct lwp *old, struct lwp *new)
608 {
609
610 KASSERT(old == NULL || old == curlwp);
611
612 if (old != NULL) {
613 LOCKDEBUG_BARRIER(&old->l_mutex, 1);
614 } else {
615 LOCKDEBUG_BARRIER(NULL, 1);
616 }
617
618 curlwp = new;
619 if (old != NULL) {
620 lwp_unlock(old);
621 }
622 spl0();
623 }
624
625 /*
626 * The machine independent parts of context switch. Switch to "new"
627 * if non-NULL, otherwise let cpu_switch choose the next lwp.
628 *
629 * Returns 1 if another process was actually run.
630 */
631 int
632 mi_switch(struct lwp *l, struct lwp *newl)
633 {
634 struct schedstate_percpu *spc;
635 struct timeval tv;
636 int retval, oldspl;
637 long s, u;
638
639 LOCK_ASSERT(lwp_locked(l, NULL));
640
641 #ifdef LOCKDEBUG
642 spinlock_switchcheck();
643 simple_lock_switchcheck();
644 #endif
645 #ifdef KSTACK_CHECK_MAGIC
646 kstack_check_magic(l);
647 #endif
648
649 /*
650 * It's safe to read the per CPU schedstate unlocked here, as all we
651 * are after is the run time and that's guarenteed to have been last
652 * updated by this CPU.
653 */
654 KDASSERT(l->l_cpu == curcpu());
655 spc = &l->l_cpu->ci_schedstate;
656
657 if ((l->l_flag & L_IDLE) == 0) {
658 /*
659 * Compute the amount of time during which the current
660 * process was running.
661 */
662 microtime(&tv);
663 u = l->l_rtime.tv_usec +
664 (tv.tv_usec - spc->spc_runtime.tv_usec);
665 s = l->l_rtime.tv_sec + (tv.tv_sec - spc->spc_runtime.tv_sec);
666 if (u < 0) {
667 u += 1000000;
668 s--;
669 } else if (u >= 1000000) {
670 u -= 1000000;
671 s++;
672 }
673 l->l_rtime.tv_usec = u;
674 l->l_rtime.tv_sec = s;
675 }
676
677 /*
678 * XXXSMP If we are using h/w performance counters, save context.
679 */
680 #if PERFCTRS
681 if (PMC_ENABLED(l->l_proc)) {
682 pmc_save_context(l->l_proc);
683 }
684 #endif
685
686 /*
687 * If on the CPU and we have gotten this far, then we must yield.
688 */
689 KASSERT(l->l_stat != LSRUN);
690 if (l->l_stat == LSONPROC) {
691 KASSERT(lwp_locked(l, &sched_mutex));
692 l->l_stat = LSRUN;
693 if ((l->l_flag & L_IDLE) == 0) {
694 setrunqueue(l);
695 }
696 }
697 uvmexp.swtch++;
698
699 /*
700 * Process is about to yield the CPU; clear the appropriate
701 * scheduling flags.
702 */
703 spc->spc_flags &= ~SPCF_SWITCHCLEAR;
704
705 LOCKDEBUG_BARRIER(l->l_mutex, 1);
706
707 /*
708 * Switch to the new LWP if necessary.
709 * When we run again, we'll return back here.
710 */
711 oldspl = MUTEX_SPIN_OLDSPL(l->l_cpu);
712
713 /*
714 * Acquire the sched_mutex if necessary.
715 */
716 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
717 if (l->l_mutex != &sched_mutex) {
718 mutex_enter(&sched_mutex);
719 }
720 #endif
721
722 if (newl == NULL) {
723 newl = nextrunqueue();
724 }
725 if (newl != NULL) {
726 KASSERT(lwp_locked(newl, &sched_mutex));
727 remrunqueue(newl);
728 } else {
729 newl = l->l_cpu->ci_data.cpu_idlelwp;
730 KASSERT(newl != NULL);
731 }
732
733 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
734 if (l->l_mutex != &sched_mutex) {
735 mutex_exit(&sched_mutex);
736 }
737 #endif
738
739 newl->l_stat = LSONPROC;
740 if (l != newl) {
741 struct lwp *prevlwp;
742
743 uvmexp.swtch++;
744 pmap_deactivate(l);
745 newl->l_cpu = l->l_cpu;
746 prevlwp = cpu_switchto(l, newl);
747 sched_switch_unlock(prevlwp, l);
748 pmap_activate(l);
749 retval = 1;
750 } else {
751 sched_switch_unlock(l, l);
752 retval = 0;
753 }
754
755 KASSERT(l == curlwp);
756 KASSERT(l->l_stat == LSONPROC);
757
758 /*
759 * XXXSMP If we are using h/w performance counters, restore context.
760 */
761 #if PERFCTRS
762 if (PMC_ENABLED(l->l_proc)) {
763 pmc_restore_context(l->l_proc);
764 }
765 #endif
766
767 /*
768 * We're running again; record our new start time. We might
769 * be running on a new CPU now, so don't use the cached
770 * schedstate_percpu pointer.
771 */
772 KDASSERT(l->l_cpu == curcpu());
773 if ((l->l_flag & L_IDLE) == 0) {
774 microtime(&l->l_cpu->ci_schedstate.spc_runtime);
775 }
776
777 (void)splsched();
778 splx(oldspl);
779 return retval;
780 }
781
782 /*
783 * Initialize the (doubly-linked) run queues
784 * to be empty.
785 */
786 void
787 rqinit()
788 {
789 int i;
790
791 for (i = 0; i < RUNQUE_NQS; i++)
792 sched_qs[i].ph_link = sched_qs[i].ph_rlink =
793 (struct lwp *)&sched_qs[i];
794
795 mutex_init(&sched_mutex, MUTEX_SPIN, IPL_SCHED);
796 }
797
798 static inline void
799 resched_lwp(struct lwp *l, u_char pri)
800 {
801 struct cpu_info *ci;
802
803 /*
804 * XXXSMP
805 * Since l->l_cpu persists across a context switch,
806 * this gives us *very weak* processor affinity, in
807 * that we notify the CPU on which the process last
808 * ran that it should try to switch.
809 *
810 * This does not guarantee that the process will run on
811 * that processor next, because another processor might
812 * grab it the next time it performs a context switch.
813 *
814 * This also does not handle the case where its last
815 * CPU is running a higher-priority process, but every
816 * other CPU is running a lower-priority process. There
817 * are ways to handle this situation, but they're not
818 * currently very pretty, and we also need to weigh the
819 * cost of moving a process from one CPU to another.
820 *
821 * XXXSMP
822 * There is also the issue of locking the other CPU's
823 * sched state, which we currently do not do.
824 */
825 ci = (l->l_cpu != NULL) ? l->l_cpu : curcpu();
826 if (pri < ci->ci_schedstate.spc_curpriority)
827 cpu_need_resched(ci);
828 }
829
830 /*
831 * Change process state to be runnable, placing it on the run queue if it is
832 * in memory, and awakening the swapper if it isn't in memory.
833 *
834 * Call with the process and LWP locked. Will return with the LWP unlocked.
835 */
836 void
837 setrunnable(struct lwp *l)
838 {
839 struct proc *p = l->l_proc;
840 sigset_t *ss;
841
842 KASSERT((l->l_flag & L_IDLE) == 0);
843 LOCK_ASSERT(mutex_owned(&p->p_smutex));
844 LOCK_ASSERT(lwp_locked(l, NULL));
845
846 switch (l->l_stat) {
847 case LSSTOP:
848 /*
849 * If we're being traced (possibly because someone attached us
850 * while we were stopped), check for a signal from the debugger.
851 */
852 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xstat != 0) {
853 if ((sigprop[p->p_xstat] & SA_TOLWP) != 0)
854 ss = &l->l_sigpend.sp_set;
855 else
856 ss = &p->p_sigpend.sp_set;
857 sigaddset(ss, p->p_xstat);
858 signotify(l);
859 }
860 p->p_nrlwps++;
861 break;
862 case LSSUSPENDED:
863 l->l_flag &= ~L_WSUSPEND;
864 p->p_nrlwps++;
865 break;
866 case LSSLEEP:
867 KASSERT(l->l_wchan != NULL);
868 break;
869 default:
870 panic("setrunnable: lwp %p state was %d", l, l->l_stat);
871 }
872
873 /*
874 * If the LWP was sleeping interruptably, then it's OK to start it
875 * again. If not, mark it as still sleeping.
876 */
877 if (l->l_wchan != NULL) {
878 l->l_stat = LSSLEEP;
879 if ((l->l_flag & L_SINTR) != 0)
880 lwp_unsleep(l);
881 else {
882 lwp_unlock(l);
883 #ifdef DIAGNOSTIC
884 panic("setrunnable: !L_SINTR");
885 #endif
886 }
887 return;
888 }
889
890 LOCK_ASSERT(lwp_locked(l, &sched_mutex));
891
892 /*
893 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
894 * about to call mi_switch(), in which case it will yield.
895 *
896 * XXXSMP Will need to change for preemption.
897 */
898 #ifdef MULTIPROCESSOR
899 if (l->l_cpu->ci_curlwp == l) {
900 #else
901 if (l == curlwp) {
902 #endif
903 l->l_stat = LSONPROC;
904 l->l_slptime = 0;
905 lwp_unlock(l);
906 return;
907 }
908
909 /*
910 * Set the LWP runnable. If it's swapped out, we need to wake the swapper
911 * to bring it back in. Otherwise, enter it into a run queue.
912 */
913 if (l->l_slptime > 1)
914 updatepri(l);
915 l->l_stat = LSRUN;
916 l->l_slptime = 0;
917
918 if (l->l_flag & L_INMEM) {
919 setrunqueue(l);
920 resched_lwp(l, l->l_priority);
921 lwp_unlock(l);
922 } else {
923 lwp_unlock(l);
924 uvm_kick_scheduler();
925 }
926 }
927
928 boolean_t
929 sched_curcpu_runnable_p(void)
930 {
931
932 return sched_whichqs != 0;
933 }
934
935 /*
936 * Compute the priority of a process when running in user mode.
937 * Arrange to reschedule if the resulting priority is better
938 * than that of the current process.
939 */
940 void
941 resetpriority(struct lwp *l)
942 {
943 unsigned int newpriority;
944 struct proc *p = l->l_proc;
945
946 /* XXXSMP LOCK_ASSERT(mutex_owned(&p->p_stmutex)); */
947 LOCK_ASSERT(lwp_locked(l, NULL));
948
949 if ((l->l_flag & L_SYSTEM) != 0)
950 return;
951
952 newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
953 NICE_WEIGHT * (p->p_nice - NZERO);
954 newpriority = min(newpriority, MAXPRI);
955 lwp_changepri(l, newpriority);
956 }
957
958 /*
959 * Recompute priority for all LWPs in a process.
960 */
961 void
962 resetprocpriority(struct proc *p)
963 {
964 struct lwp *l;
965
966 LOCK_ASSERT(mutex_owned(&p->p_stmutex));
967
968 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
969 lwp_lock(l);
970 resetpriority(l);
971 lwp_unlock(l);
972 }
973 }
974
975 /*
976 * We adjust the priority of the current process. The priority of a process
977 * gets worse as it accumulates CPU time. The CPU usage estimator (p_estcpu)
978 * is increased here. The formula for computing priorities (in kern_synch.c)
979 * will compute a different value each time p_estcpu increases. This can
980 * cause a switch, but unless the priority crosses a PPQ boundary the actual
981 * queue will not change. The CPU usage estimator ramps up quite quickly
982 * when the process is running (linearly), and decays away exponentially, at
983 * a rate which is proportionally slower when the system is busy. The basic
984 * principle is that the system will 90% forget that the process used a lot
985 * of CPU time in 5 * loadav seconds. This causes the system to favor
986 * processes which haven't run much recently, and to round-robin among other
987 * processes.
988 */
989
990 void
991 schedclock(struct lwp *l)
992 {
993 struct proc *p = l->l_proc;
994
995 KASSERT(!CURCPU_IDLE_P());
996 mutex_spin_enter(&p->p_stmutex);
997 p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
998 lwp_lock(l);
999 resetpriority(l);
1000 mutex_spin_exit(&p->p_stmutex);
1001 if ((l->l_flag & L_SYSTEM) == 0 && l->l_priority >= PUSER)
1002 l->l_priority = l->l_usrpri;
1003 lwp_unlock(l);
1004 }
1005
1006 /*
1007 * suspendsched:
1008 *
1009 * Convert all non-L_SYSTEM LSSLEEP or LSRUN LWPs to LSSUSPENDED.
1010 */
1011 void
1012 suspendsched(void)
1013 {
1014 #ifdef MULTIPROCESSOR
1015 CPU_INFO_ITERATOR cii;
1016 struct cpu_info *ci;
1017 #endif
1018 struct lwp *l;
1019 struct proc *p;
1020
1021 /*
1022 * We do this by process in order not to violate the locking rules.
1023 */
1024 mutex_enter(&proclist_mutex);
1025 PROCLIST_FOREACH(p, &allproc) {
1026 mutex_enter(&p->p_smutex);
1027
1028 if ((p->p_flag & P_SYSTEM) != 0) {
1029 mutex_exit(&p->p_smutex);
1030 continue;
1031 }
1032
1033 p->p_stat = SSTOP;
1034
1035 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1036 if (l == curlwp)
1037 continue;
1038
1039 lwp_lock(l);
1040
1041 /*
1042 * Set L_WREBOOT so that the LWP will suspend itself
1043 * when it tries to return to user mode. We want to
1044 * try and get to get as many LWPs as possible to
1045 * the user / kernel boundary, so that they will
1046 * release any locks that they hold.
1047 */
1048 l->l_flag |= (L_WREBOOT | L_WSUSPEND);
1049
1050 if (l->l_stat == LSSLEEP &&
1051 (l->l_flag & L_SINTR) != 0) {
1052 /* setrunnable() will release the lock. */
1053 setrunnable(l);
1054 continue;
1055 }
1056
1057 lwp_unlock(l);
1058 }
1059
1060 mutex_exit(&p->p_smutex);
1061 }
1062 mutex_exit(&proclist_mutex);
1063
1064 /*
1065 * Kick all CPUs to make them preempt any LWPs running in user mode.
1066 * They'll trap into the kernel and suspend themselves in userret().
1067 */
1068 sched_lock(0);
1069 #ifdef MULTIPROCESSOR
1070 for (CPU_INFO_FOREACH(cii, ci))
1071 cpu_need_resched(ci);
1072 #else
1073 cpu_need_resched(curcpu());
1074 #endif
1075 sched_unlock(0);
1076 }
1077
1078 /*
1079 * scheduler_fork_hook:
1080 *
1081 * Inherit the parent's scheduler history.
1082 */
1083 void
1084 scheduler_fork_hook(struct proc *parent, struct proc *child)
1085 {
1086
1087 LOCK_ASSERT(mutex_owned(&parent->p_smutex));
1088
1089 child->p_estcpu = child->p_estcpu_inherited = parent->p_estcpu;
1090 child->p_forktime = schedcpu_ticks;
1091 }
1092
1093 /*
1094 * scheduler_wait_hook:
1095 *
1096 * Chargeback parents for the sins of their children.
1097 */
1098 void
1099 scheduler_wait_hook(struct proc *parent, struct proc *child)
1100 {
1101 fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
1102 fixpt_t estcpu;
1103
1104 /* XXX Only if parent != init?? */
1105
1106 mutex_spin_enter(&parent->p_stmutex);
1107 estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
1108 schedcpu_ticks - child->p_forktime);
1109 if (child->p_estcpu > estcpu)
1110 parent->p_estcpu =
1111 ESTCPULIM(parent->p_estcpu + child->p_estcpu - estcpu);
1112 mutex_spin_exit(&parent->p_stmutex);
1113 }
1114
1115 /*
1116 * sched_kpri:
1117 *
1118 * Scale a priority level to a kernel priority level, usually
1119 * for an LWP that is about to sleep.
1120 */
1121 int
1122 sched_kpri(struct lwp *l)
1123 {
1124 /*
1125 * Scale user priorities (127 -> 50) up to kernel priorities
1126 * in the range (49 -> 8). Reserve the top 8 kernel priorities
1127 * for high priority kthreads. Kernel priorities passed in
1128 * are left "as is". XXX This is somewhat arbitrary.
1129 */
1130 static const uint8_t kpri_tab[] = {
1131 0, 1, 2, 3, 4, 5, 6, 7,
1132 8, 9, 10, 11, 12, 13, 14, 15,
1133 16, 17, 18, 19, 20, 21, 22, 23,
1134 24, 25, 26, 27, 28, 29, 30, 31,
1135 32, 33, 34, 35, 36, 37, 38, 39,
1136 40, 41, 42, 43, 44, 45, 46, 47,
1137 48, 49, 8, 8, 9, 9, 10, 10,
1138 11, 11, 12, 12, 13, 14, 14, 15,
1139 15, 16, 16, 17, 17, 18, 18, 19,
1140 20, 20, 21, 21, 22, 22, 23, 23,
1141 24, 24, 25, 26, 26, 27, 27, 28,
1142 28, 29, 29, 30, 30, 31, 32, 32,
1143 33, 33, 34, 34, 35, 35, 36, 36,
1144 37, 38, 38, 39, 39, 40, 40, 41,
1145 41, 42, 42, 43, 44, 44, 45, 45,
1146 46, 46, 47, 47, 48, 48, 49, 49,
1147 };
1148
1149 return kpri_tab[l->l_usrpri];
1150 }
1151
1152 /*
1153 * sched_unsleep:
1154 *
1155 * The is called when the LWP has not been awoken normally but instead
1156 * interrupted: for example, if the sleep timed out. Because of this,
1157 * it's not a valid action for running or idle LWPs.
1158 */
1159 void
1160 sched_unsleep(struct lwp *l)
1161 {
1162
1163 lwp_unlock(l);
1164 panic("sched_unsleep");
1165 }
1166
1167 /*
1168 * sched_changepri:
1169 *
1170 * Adjust the priority of an LWP.
1171 */
1172 void
1173 sched_changepri(struct lwp *l, int pri)
1174 {
1175
1176 LOCK_ASSERT(lwp_locked(l, &sched_mutex));
1177
1178 l->l_usrpri = pri;
1179
1180 if (l->l_priority < PUSER)
1181 return;
1182 if (l->l_stat != LSRUN || (l->l_flag & L_INMEM) == 0 ||
1183 (l->l_priority / PPQ) == (pri / PPQ)) {
1184 l->l_priority = pri;
1185 return;
1186 }
1187
1188 remrunqueue(l);
1189 l->l_priority = pri;
1190 setrunqueue(l);
1191 resched_lwp(l, pri);
1192 }
1193
1194 /*
1195 * On some architectures, it's faster to use a MSB ordering for the priorites
1196 * than the traditional LSB ordering.
1197 */
1198 #ifdef __HAVE_BIGENDIAN_BITOPS
1199 #define RQMASK(n) (0x80000000 >> (n))
1200 #else
1201 #define RQMASK(n) (0x00000001 << (n))
1202 #endif
1203
1204 /*
1205 * Low-level routines to access the run queue. Optimised assembler
1206 * routines can override these.
1207 */
1208
1209 #ifndef __HAVE_MD_RUNQUEUE
1210
1211 /*
1212 * The primitives that manipulate the run queues. whichqs tells which
1213 * of the 32 queues qs have processes in them. Setrunqueue puts processes
1214 * into queues, remrunqueue removes them from queues. The running process is
1215 * on no queue, other processes are on a queue related to p->p_priority,
1216 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1217 * available queues.
1218 */
1219 #ifdef RQDEBUG
1220 static void
1221 checkrunqueue(int whichq, struct lwp *l)
1222 {
1223 const struct prochd * const rq = &sched_qs[whichq];
1224 struct lwp *l2;
1225 int found = 0;
1226 int die = 0;
1227 int empty = 1;
1228 for (l2 = rq->ph_link; l2 != (const void*) rq; l2 = l2->l_forw) {
1229 if (l2->l_stat != LSRUN) {
1230 printf("checkrunqueue[%d]: lwp %p state (%d) "
1231 " != LSRUN\n", whichq, l2, l2->l_stat);
1232 }
1233 if (l2->l_back->l_forw != l2) {
1234 printf("checkrunqueue[%d]: lwp %p back-qptr (%p) "
1235 "corrupt %p\n", whichq, l2, l2->l_back,
1236 l2->l_back->l_forw);
1237 die = 1;
1238 }
1239 if (l2->l_forw->l_back != l2) {
1240 printf("checkrunqueue[%d]: lwp %p forw-qptr (%p) "
1241 "corrupt %p\n", whichq, l2, l2->l_forw,
1242 l2->l_forw->l_back);
1243 die = 1;
1244 }
1245 if (l2 == l)
1246 found = 1;
1247 empty = 0;
1248 }
1249 if (empty && (sched_whichqs & RQMASK(whichq)) != 0) {
1250 printf("checkrunqueue[%d]: bit set for empty run-queue %p\n",
1251 whichq, rq);
1252 die = 1;
1253 } else if (!empty && (sched_whichqs & RQMASK(whichq)) == 0) {
1254 printf("checkrunqueue[%d]: bit clear for non-empty "
1255 "run-queue %p\n", whichq, rq);
1256 die = 1;
1257 }
1258 if (l != NULL && (sched_whichqs & RQMASK(whichq)) == 0) {
1259 printf("checkrunqueue[%d]: bit clear for active lwp %p\n",
1260 whichq, l);
1261 die = 1;
1262 }
1263 if (l != NULL && empty) {
1264 printf("checkrunqueue[%d]: empty run-queue %p with "
1265 "active lwp %p\n", whichq, rq, l);
1266 die = 1;
1267 }
1268 if (l != NULL && !found) {
1269 printf("checkrunqueue[%d]: lwp %p not in runqueue %p!",
1270 whichq, l, rq);
1271 die = 1;
1272 }
1273 if (die)
1274 panic("checkrunqueue: inconsistency found");
1275 }
1276 #endif /* RQDEBUG */
1277
1278 void
1279 setrunqueue(struct lwp *l)
1280 {
1281 struct prochd *rq;
1282 struct lwp *prev;
1283 const int whichq = l->l_priority / PPQ;
1284
1285 LOCK_ASSERT(lwp_locked(l, &sched_mutex));
1286
1287 #ifdef RQDEBUG
1288 checkrunqueue(whichq, NULL);
1289 #endif
1290 #ifdef DIAGNOSTIC
1291 if (l->l_back != NULL || l->l_stat != LSRUN)
1292 panic("setrunqueue");
1293 #endif
1294 sched_whichqs |= RQMASK(whichq);
1295 rq = &sched_qs[whichq];
1296 prev = rq->ph_rlink;
1297 l->l_forw = (struct lwp *)rq;
1298 rq->ph_rlink = l;
1299 prev->l_forw = l;
1300 l->l_back = prev;
1301 #ifdef RQDEBUG
1302 checkrunqueue(whichq, l);
1303 #endif
1304 }
1305
1306 /*
1307 * XXXSMP When LWP dispatch (cpu_switch()) is changed to use remrunqueue(),
1308 * drop of the effective priority level from kernel to user needs to be
1309 * moved here from userret(). The assignment in userret() is currently
1310 * done unlocked.
1311 */
1312 void
1313 remrunqueue(struct lwp *l)
1314 {
1315 struct lwp *prev, *next;
1316 const int whichq = l->l_priority / PPQ;
1317
1318 LOCK_ASSERT(lwp_locked(l, &sched_mutex));
1319
1320 #ifdef RQDEBUG
1321 checkrunqueue(whichq, l);
1322 #endif
1323
1324 #if defined(DIAGNOSTIC)
1325 if (((sched_whichqs & RQMASK(whichq)) == 0) || l->l_back == NULL) {
1326 /* Shouldn't happen - interrupts disabled. */
1327 panic("remrunqueue: bit %d not set", whichq);
1328 }
1329 #endif
1330 prev = l->l_back;
1331 l->l_back = NULL;
1332 next = l->l_forw;
1333 prev->l_forw = next;
1334 next->l_back = prev;
1335 if (prev == next)
1336 sched_whichqs &= ~RQMASK(whichq);
1337 #ifdef RQDEBUG
1338 checkrunqueue(whichq, NULL);
1339 #endif
1340 }
1341
1342 struct lwp *
1343 nextrunqueue(void)
1344 {
1345 const struct prochd *rq;
1346 struct lwp *l;
1347 int whichq;
1348
1349 if (sched_whichqs == 0) {
1350 return NULL;
1351 }
1352 #ifdef __HAVE_BIGENDIAN_BITOPS
1353 for (whichq = 0; ; whichq++) {
1354 if ((sched_whichqs & RQMASK(whichq)) != 0) {
1355 break;
1356 }
1357 }
1358 #else
1359 whichq = ffs(sched_whichqs) - 1;
1360 #endif
1361 rq = &sched_qs[whichq];
1362 l = rq->ph_link;
1363 return l;
1364 }
1365
1366 #endif /* !defined(__HAVE_MD_RUNQUEUE) */
1367
1368 #if defined(DDB)
1369 void
1370 sched_print_runqueue(void (*pr)(const char *, ...))
1371 {
1372 struct prochd *ph;
1373 struct lwp *l;
1374 int i, first;
1375
1376 for (i = 0; i < RUNQUE_NQS; i++)
1377 {
1378 first = 1;
1379 ph = &sched_qs[i];
1380 for (l = ph->ph_link; l != (void *)ph; l = l->l_forw) {
1381 if (first) {
1382 (*pr)("%c%d",
1383 (sched_whichqs & RQMASK(i))
1384 ? ' ' : '!', i);
1385 first = 0;
1386 }
1387 (*pr)("\t%d.%d (%s) pri=%d usrpri=%d\n",
1388 l->l_proc->p_pid,
1389 l->l_lid, l->l_proc->p_comm,
1390 (int)l->l_priority, (int)l->l_usrpri);
1391 }
1392 }
1393 }
1394 #endif /* defined(DDB) */
1395 #undef RQMASK
1396