xref: /freebsd/sys/kern/kern_switch.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
19454b2d8SWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4d5a08a60SJake Burkholder  * Copyright (c) 2001 Jake Burkholder <jake@FreeBSD.org>
5d5a08a60SJake Burkholder  * All rights reserved.
6dba6c5a6SPeter Wemm  *
7dba6c5a6SPeter Wemm  * Redistribution and use in source and binary forms, with or without
8dba6c5a6SPeter Wemm  * modification, are permitted provided that the following conditions
9dba6c5a6SPeter Wemm  * are met:
10dba6c5a6SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
11dba6c5a6SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
12dba6c5a6SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
13dba6c5a6SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
14dba6c5a6SPeter Wemm  *    documentation and/or other materials provided with the distribution.
15dba6c5a6SPeter Wemm  *
16dba6c5a6SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17dba6c5a6SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18dba6c5a6SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19dba6c5a6SPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20dba6c5a6SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21dba6c5a6SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22dba6c5a6SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23dba6c5a6SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24dba6c5a6SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25dba6c5a6SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26dba6c5a6SPeter Wemm  * SUCH DAMAGE.
27dba6c5a6SPeter Wemm  */
28dba6c5a6SPeter Wemm 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
306804a3abSJulian Elischer #include "opt_sched.h"
310c0b25aeSJohn Baldwin 
32dba6c5a6SPeter Wemm #include <sys/param.h>
33dba6c5a6SPeter Wemm #include <sys/systm.h>
342d50560aSMarcel Moolenaar #include <sys/kdb.h>
35dba6c5a6SPeter Wemm #include <sys/kernel.h>
360384fff8SJason Evans #include <sys/ktr.h>
37f34fa851SJohn Baldwin #include <sys/lock.h>
3835e0e5b3SJohn Baldwin #include <sys/mutex.h>
39dba6c5a6SPeter Wemm #include <sys/proc.h>
40dba6c5a6SPeter Wemm #include <sys/queue.h>
41b43179fbSJeff Roberson #include <sys/sched.h>
42cc66ebe2SPeter Wemm #include <sys/smp.h>
439727e637SJeff Roberson #include <sys/sysctl.h>
446804a3abSJulian Elischer 
457b20fb19SJeff Roberson #include <machine/cpu.h>
467b20fb19SJeff Roberson 
471335c4dfSNate Lawson /* Uncomment this to enable logging of critical_enter/exit. */
481335c4dfSNate Lawson #if 0
491335c4dfSNate Lawson #define	KTR_CRITICAL	KTR_SCHED
501335c4dfSNate Lawson #else
511335c4dfSNate Lawson #define	KTR_CRITICAL	0
521335c4dfSNate Lawson #endif
531335c4dfSNate Lawson 
549923b511SScott Long #ifdef FULL_PREEMPTION
559923b511SScott Long #ifndef PREEMPTION
569923b511SScott Long #error "The FULL_PREEMPTION option requires the PREEMPTION option"
579923b511SScott Long #endif
589923b511SScott Long #endif
59dba6c5a6SPeter Wemm 
60d2ac2316SJake Burkholder CTASSERT((RQB_BPW * RQB_LEN) == RQ_NQS);
61d2ac2316SJake Burkholder 
626220dcbaSRobert Watson /*
636220dcbaSRobert Watson  * kern.sched.preemption allows user space to determine if preemption support
646220dcbaSRobert Watson  * is compiled in or not.  It is not currently a boot or runtime flag that
656220dcbaSRobert Watson  * can be changed.
666220dcbaSRobert Watson  */
676220dcbaSRobert Watson #ifdef PREEMPTION
686220dcbaSRobert Watson static int kern_sched_preemption = 1;
696220dcbaSRobert Watson #else
706220dcbaSRobert Watson static int kern_sched_preemption = 0;
716220dcbaSRobert Watson #endif
726220dcbaSRobert Watson SYSCTL_INT(_kern_sched, OID_AUTO, preemption, CTLFLAG_RD,
736220dcbaSRobert Watson     &kern_sched_preemption, 0, "Kernel preemption enabled");
746220dcbaSRobert Watson 
758df78c41SJeff Roberson /*
768df78c41SJeff Roberson  * Support for scheduler stats exported via kern.sched.stats.  All stats may
778df78c41SJeff Roberson  * be reset with kern.sched.stats.reset = 1.  Stats may be defined elsewhere
788df78c41SJeff Roberson  * with SCHED_STAT_DEFINE().
798df78c41SJeff Roberson  */
807b20fb19SJeff Roberson #ifdef SCHED_STATS
817029da5cSPawel Biernacki SYSCTL_NODE(_kern_sched, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
827029da5cSPawel Biernacki     "switch stats");
83791d9a6eSJeff Roberson 
841029dab6SMitchell Horne /* Switch reasons from mi_switch(9). */
85791d9a6eSJeff Roberson DPCPU_DEFINE(long, sched_switch_stats[SWT_COUNT]);
86791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(owepreempt,
87791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_OWEPREEMPT]), "");
88791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(turnstile,
89791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_TURNSTILE]), "");
90791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(sleepq,
91791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_SLEEPQ]), "");
92791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(relinquish,
93791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_RELINQUISH]), "");
94791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(needresched,
95791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_NEEDRESCHED]), "");
96791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(idle,
97791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_IDLE]), "");
98791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(iwait,
99791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_IWAIT]), "");
100791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(suspend,
101791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_SUSPEND]), "");
102791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(remotepreempt,
103791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_REMOTEPREEMPT]), "");
104791d9a6eSJeff Roberson SCHED_STAT_DEFINE_VAR(remotewakeidle,
105791d9a6eSJeff Roberson     &DPCPU_NAME(sched_switch_stats[SWT_REMOTEWAKEIDLE]), "");
1061029dab6SMitchell Horne SCHED_STAT_DEFINE_VAR(bind,
1071029dab6SMitchell Horne     &DPCPU_NAME(sched_switch_stats[SWT_BIND]), "");
1088df78c41SJeff Roberson 
1097b20fb19SJeff Roberson static int
sysctl_stats_reset(SYSCTL_HANDLER_ARGS)1107b20fb19SJeff Roberson sysctl_stats_reset(SYSCTL_HANDLER_ARGS)
1117b20fb19SJeff Roberson {
1128df78c41SJeff Roberson 	struct sysctl_oid *p;
113791d9a6eSJeff Roberson 	uintptr_t counter;
1147b20fb19SJeff Roberson         int error;
1157b20fb19SJeff Roberson 	int val;
116791d9a6eSJeff Roberson 	int i;
1177b20fb19SJeff Roberson 
1187b20fb19SJeff Roberson         val = 0;
1197b20fb19SJeff Roberson         error = sysctl_handle_int(oidp, &val, 0, req);
1207b20fb19SJeff Roberson         if (error != 0 || req->newptr == NULL)
1217b20fb19SJeff Roberson                 return (error);
1227b20fb19SJeff Roberson         if (val == 0)
1237b20fb19SJeff Roberson                 return (0);
1248df78c41SJeff Roberson 	/*
1258df78c41SJeff Roberson 	 * Traverse the list of children of _kern_sched_stats and reset each
1268df78c41SJeff Roberson 	 * to 0.  Skip the reset entry.
1278df78c41SJeff Roberson 	 */
1286622e299SAlan Somers 	RB_FOREACH(p, sysctl_oid_list, oidp->oid_parent) {
1298df78c41SJeff Roberson 		if (p == oidp || p->oid_arg1 == NULL)
1308df78c41SJeff Roberson 			continue;
131791d9a6eSJeff Roberson 		counter = (uintptr_t)p->oid_arg1;
1323aa6d94eSJohn Baldwin 		CPU_FOREACH(i) {
133791d9a6eSJeff Roberson 			*(long *)(dpcpu_off[i] + counter) = 0;
134791d9a6eSJeff Roberson 		}
1358df78c41SJeff Roberson 	}
1367b20fb19SJeff Roberson 	return (0);
1377b20fb19SJeff Roberson }
1387b20fb19SJeff Roberson 
1397029da5cSPawel Biernacki SYSCTL_PROC(_kern_sched_stats, OID_AUTO, reset,
140c6c52d8eSAlexander Motin     CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_MPSAFE, NULL, 0,
1417029da5cSPawel Biernacki     sysctl_stats_reset, "I",
1427029da5cSPawel Biernacki     "Reset scheduler statistics");
1437b20fb19SJeff Roberson #endif
1447b20fb19SJeff Roberson 
145e602ba25SJulian Elischer /************************************************************************
146e602ba25SJulian Elischer  * Functions that manipulate runnability from a thread perspective.	*
147e602ba25SJulian Elischer  ************************************************************************/
1488460a577SJohn Birrell /*
1498460a577SJohn Birrell  * Select the thread that will be run next.
1508460a577SJohn Birrell  */
151e602ba25SJulian Elischer 
15232aef9ffSMateusz Guzik static __noinline struct thread *
choosethread_panic(struct thread * td)15332aef9ffSMateusz Guzik choosethread_panic(struct thread *td)
15432aef9ffSMateusz Guzik {
15593a7aa79SJulian Elischer 
15693a7aa79SJulian Elischer 	/*
157faaa20f6SJulian Elischer 	 * If we are in panic, only allow system threads,
158faaa20f6SJulian Elischer 	 * plus the one we are running in, to be run.
15993a7aa79SJulian Elischer 	 */
16032aef9ffSMateusz Guzik retry:
16132aef9ffSMateusz Guzik 	if (((td->td_proc->p_flag & P_SYSTEM) == 0 &&
162faaa20f6SJulian Elischer 	    (td->td_flags & TDF_INPANIC) == 0)) {
163faaa20f6SJulian Elischer 		/* note that it is no longer on the run queue */
164faaa20f6SJulian Elischer 		TD_SET_CAN_RUN(td);
16532aef9ffSMateusz Guzik 		td = sched_choose();
166fe799533SAndrew Gallatin 		goto retry;
167faaa20f6SJulian Elischer 	}
16893a7aa79SJulian Elischer 
16971fad9fdSJulian Elischer 	TD_SET_RUNNING(td);
170e602ba25SJulian Elischer 	return (td);
171e602ba25SJulian Elischer }
172e602ba25SJulian Elischer 
17332aef9ffSMateusz Guzik struct thread *
choosethread(void)17432aef9ffSMateusz Guzik choosethread(void)
17532aef9ffSMateusz Guzik {
17632aef9ffSMateusz Guzik 	struct thread *td;
17732aef9ffSMateusz Guzik 
17832aef9ffSMateusz Guzik 	td = sched_choose();
17932aef9ffSMateusz Guzik 
180879e0604SMateusz Guzik 	if (KERNEL_PANICKED())
18132aef9ffSMateusz Guzik 		return (choosethread_panic(td));
18232aef9ffSMateusz Guzik 
18332aef9ffSMateusz Guzik 	TD_SET_RUNNING(td);
18432aef9ffSMateusz Guzik 	return (td);
18532aef9ffSMateusz Guzik }
18632aef9ffSMateusz Guzik 
1870c0b25aeSJohn Baldwin /*
1880c0b25aeSJohn Baldwin  * Kernel thread preemption implementation.  Critical sections mark
1890c0b25aeSJohn Baldwin  * regions of code in which preemptions are not allowed.
190e68ccbe8SAttilio Rao  *
191e68ccbe8SAttilio Rao  * It might seem a good idea to inline critical_enter() but, in order
192e68ccbe8SAttilio Rao  * to prevent instructions reordering by the compiler, a __compiler_membar()
193e68ccbe8SAttilio Rao  * would have to be used here (the same as sched_pin()).  The performance
194e68ccbe8SAttilio Rao  * penalty imposed by the membar could, then, produce slower code than
195e68ccbe8SAttilio Rao  * the function call itself, for most cases.
1960c0b25aeSJohn Baldwin  */
1977e1f6dfeSJohn Baldwin void
critical_enter_KBI(void)1986443773dSMatt Macy critical_enter_KBI(void)
1997e1f6dfeSJohn Baldwin {
2006443773dSMatt Macy #ifdef KTR
2016443773dSMatt Macy 	struct thread *td = curthread;
2026443773dSMatt Macy #endif
2036443773dSMatt Macy 	critical_enter();
2041335c4dfSNate Lawson 	CTR4(KTR_CRITICAL, "critical_enter by thread %p (%ld, %s) to %d", td,
205431f8906SJulian Elischer 	    (long)td->td_proc->p_pid, td->td_name, td->td_critnest);
2067e1f6dfeSJohn Baldwin }
2077e1f6dfeSJohn Baldwin 
2086443773dSMatt Macy void __noinline
critical_exit_preempt(void)209748b15fcSMateusz Guzik critical_exit_preempt(void)
2107e1f6dfeSJohn Baldwin {
2117e1f6dfeSJohn Baldwin 	struct thread *td;
2128df78c41SJeff Roberson 	int flags;
2137e1f6dfeSJohn Baldwin 
2146fee84e3SMateusz Guzik 	/*
2156fee84e3SMateusz Guzik 	 * If td_critnest is 0, it is possible that we are going to get
2166fee84e3SMateusz Guzik 	 * preempted again before reaching the code below. This happens
2176fee84e3SMateusz Guzik 	 * rarely and is harmless. However, this means td_owepreempt may
2186fee84e3SMateusz Guzik 	 * now be unset.
2196fee84e3SMateusz Guzik 	 */
2207e1f6dfeSJohn Baldwin 	td = curthread;
221748b15fcSMateusz Guzik 	if (td->td_critnest != 0)
222748b15fcSMateusz Guzik 		return;
223748b15fcSMateusz Guzik 	if (kdb_active)
224748b15fcSMateusz Guzik 		return;
2255bce4ae3SJeff Roberson 
2263467f88cSKonstantin Belousov 	/*
2273467f88cSKonstantin Belousov 	 * Microoptimization: we committed to switch,
2283467f88cSKonstantin Belousov 	 * disable preemption in interrupt handlers
2293467f88cSKonstantin Belousov 	 * while spinning for the thread lock.
2303467f88cSKonstantin Belousov 	 */
23177918643SStephan Uphoff 	td->td_critnest = 1;
2327b20fb19SJeff Roberson 	thread_lock(td);
23377918643SStephan Uphoff 	td->td_critnest--;
2348df78c41SJeff Roberson 	flags = SW_INVOL | SW_PREEMPT;
2358df78c41SJeff Roberson 	if (TD_IS_IDLETHREAD(td))
2368df78c41SJeff Roberson 		flags |= SWT_IDLE;
2378df78c41SJeff Roberson 	else
2388df78c41SJeff Roberson 		flags |= SWT_OWEPREEMPT;
239686bcb5cSJeff Roberson 	mi_switch(flags);
2400c0b25aeSJohn Baldwin }
241d13ec713SStephan Uphoff 
242748b15fcSMateusz Guzik void
critical_exit_KBI(void)2436443773dSMatt Macy critical_exit_KBI(void)
244748b15fcSMateusz Guzik {
2456443773dSMatt Macy #ifdef KTR
2466443773dSMatt Macy 	struct thread *td = curthread;
2476443773dSMatt Macy #endif
2486443773dSMatt Macy 	critical_exit();
2491335c4dfSNate Lawson 	CTR4(KTR_CRITICAL, "critical_exit by thread %p (%ld, %s) to %d", td,
250431f8906SJulian Elischer 	    (long)td->td_proc->p_pid, td->td_name, td->td_critnest);
251d74ac681SMatthew Dillon }
2527e1f6dfeSJohn Baldwin 
253e602ba25SJulian Elischer /************************************************************************
254e602ba25SJulian Elischer  * SYSTEM RUN QUEUE manipulations and tests				*
255e602ba25SJulian Elischer  ************************************************************************/
256e602ba25SJulian Elischer /*
257e602ba25SJulian Elischer  * Initialize a run structure.
258e602ba25SJulian Elischer  */
259e602ba25SJulian Elischer void
runq_init(struct runq * rq)260e602ba25SJulian Elischer runq_init(struct runq *rq)
261e602ba25SJulian Elischer {
262e602ba25SJulian Elischer 	int i;
263e602ba25SJulian Elischer 
264e602ba25SJulian Elischer 	bzero(rq, sizeof *rq);
265e602ba25SJulian Elischer 	for (i = 0; i < RQ_NQS; i++)
266e602ba25SJulian Elischer 		TAILQ_INIT(&rq->rq_queues[i]);
267e602ba25SJulian Elischer }
268e602ba25SJulian Elischer 
269d5a08a60SJake Burkholder /*
270d5a08a60SJake Burkholder  * Clear the status bit of the queue corresponding to priority level pri,
271d5a08a60SJake Burkholder  * indicating that it is empty.
272d5a08a60SJake Burkholder  */
273d5a08a60SJake Burkholder static __inline void
runq_clrbit(struct runq * rq,int pri)274d5a08a60SJake Burkholder runq_clrbit(struct runq *rq, int pri)
275d5a08a60SJake Burkholder {
276d5a08a60SJake Burkholder 	struct rqbits *rqb;
277d5a08a60SJake Burkholder 
278d5a08a60SJake Burkholder 	rqb = &rq->rq_status;
279d5a08a60SJake Burkholder 	CTR4(KTR_RUNQ, "runq_clrbit: bits=%#x %#x bit=%#x word=%d",
280d5a08a60SJake Burkholder 	    rqb->rqb_bits[RQB_WORD(pri)],
281d5a08a60SJake Burkholder 	    rqb->rqb_bits[RQB_WORD(pri)] & ~RQB_BIT(pri),
282d5a08a60SJake Burkholder 	    RQB_BIT(pri), RQB_WORD(pri));
283d5a08a60SJake Burkholder 	rqb->rqb_bits[RQB_WORD(pri)] &= ~RQB_BIT(pri);
284d5a08a60SJake Burkholder }
285d5a08a60SJake Burkholder 
286d5a08a60SJake Burkholder /*
287d5a08a60SJake Burkholder  * Find the index of the first non-empty run queue.  This is done by
288d5a08a60SJake Burkholder  * scanning the status bits, a set bit indicates a non-empty queue.
289d5a08a60SJake Burkholder  */
290d5a08a60SJake Burkholder static __inline int
runq_findbit(struct runq * rq)291d5a08a60SJake Burkholder runq_findbit(struct runq *rq)
292d5a08a60SJake Burkholder {
293d5a08a60SJake Burkholder 	struct rqbits *rqb;
294d5a08a60SJake Burkholder 	int pri;
295d5a08a60SJake Burkholder 	int i;
296d5a08a60SJake Burkholder 
297d5a08a60SJake Burkholder 	rqb = &rq->rq_status;
298d5a08a60SJake Burkholder 	for (i = 0; i < RQB_LEN; i++)
299d5a08a60SJake Burkholder 		if (rqb->rqb_bits[i]) {
3002f9267ecSPeter Wemm 			pri = RQB_FFS(rqb->rqb_bits[i]) + (i << RQB_L2BPW);
301d5a08a60SJake Burkholder 			CTR3(KTR_RUNQ, "runq_findbit: bits=%#x i=%d pri=%d",
302d5a08a60SJake Burkholder 			    rqb->rqb_bits[i], i, pri);
303d5a08a60SJake Burkholder 			return (pri);
304d5a08a60SJake Burkholder 		}
305d5a08a60SJake Burkholder 
306d5a08a60SJake Burkholder 	return (-1);
307d5a08a60SJake Burkholder }
308d5a08a60SJake Burkholder 
3093fed7d23SJeff Roberson static __inline int
runq_findbit_from(struct runq * rq,u_char pri)31067e20930SJeff Roberson runq_findbit_from(struct runq *rq, u_char pri)
3113fed7d23SJeff Roberson {
3123fed7d23SJeff Roberson 	struct rqbits *rqb;
31367e20930SJeff Roberson 	rqb_word_t mask;
3143fed7d23SJeff Roberson 	int i;
3153fed7d23SJeff Roberson 
31667e20930SJeff Roberson 	/*
31767e20930SJeff Roberson 	 * Set the mask for the first word so we ignore priorities before 'pri'.
31867e20930SJeff Roberson 	 */
31967e20930SJeff Roberson 	mask = (rqb_word_t)-1 << (pri & (RQB_BPW - 1));
3203fed7d23SJeff Roberson 	rqb = &rq->rq_status;
3213fed7d23SJeff Roberson again:
32267e20930SJeff Roberson 	for (i = RQB_WORD(pri); i < RQB_LEN; mask = -1, i++) {
32367e20930SJeff Roberson 		mask = rqb->rqb_bits[i] & mask;
32467e20930SJeff Roberson 		if (mask == 0)
3253fed7d23SJeff Roberson 			continue;
32667e20930SJeff Roberson 		pri = RQB_FFS(mask) + (i << RQB_L2BPW);
3273fed7d23SJeff Roberson 		CTR3(KTR_RUNQ, "runq_findbit_from: bits=%#x i=%d pri=%d",
32867e20930SJeff Roberson 		    mask, i, pri);
3293fed7d23SJeff Roberson 		return (pri);
3303fed7d23SJeff Roberson 	}
33167e20930SJeff Roberson 	if (pri == 0)
3323fed7d23SJeff Roberson 		return (-1);
33367e20930SJeff Roberson 	/*
33467e20930SJeff Roberson 	 * Wrap back around to the beginning of the list just once so we
33567e20930SJeff Roberson 	 * scan the whole thing.
33667e20930SJeff Roberson 	 */
33767e20930SJeff Roberson 	pri = 0;
33867e20930SJeff Roberson 	goto again;
3393fed7d23SJeff Roberson }
3403fed7d23SJeff Roberson 
341d5a08a60SJake Burkholder /*
342d5a08a60SJake Burkholder  * Set the status bit of the queue corresponding to priority level pri,
343d5a08a60SJake Burkholder  * indicating that it is non-empty.
344d5a08a60SJake Burkholder  */
345d5a08a60SJake Burkholder static __inline void
runq_setbit(struct runq * rq,int pri)346d5a08a60SJake Burkholder runq_setbit(struct runq *rq, int pri)
347d5a08a60SJake Burkholder {
348d5a08a60SJake Burkholder 	struct rqbits *rqb;
349d5a08a60SJake Burkholder 
350d5a08a60SJake Burkholder 	rqb = &rq->rq_status;
351d5a08a60SJake Burkholder 	CTR4(KTR_RUNQ, "runq_setbit: bits=%#x %#x bit=%#x word=%d",
352d5a08a60SJake Burkholder 	    rqb->rqb_bits[RQB_WORD(pri)],
353d5a08a60SJake Burkholder 	    rqb->rqb_bits[RQB_WORD(pri)] | RQB_BIT(pri),
354d5a08a60SJake Burkholder 	    RQB_BIT(pri), RQB_WORD(pri));
355d5a08a60SJake Burkholder 	rqb->rqb_bits[RQB_WORD(pri)] |= RQB_BIT(pri);
356d5a08a60SJake Burkholder }
357d5a08a60SJake Burkholder 
358d5a08a60SJake Burkholder /*
359ad1e7d28SJulian Elischer  * Add the thread to the queue specified by its priority, and set the
360d5a08a60SJake Burkholder  * corresponding status bit.
361d5a08a60SJake Burkholder  */
362d5a08a60SJake Burkholder void
runq_add(struct runq * rq,struct thread * td,int flags)3639727e637SJeff Roberson runq_add(struct runq *rq, struct thread *td, int flags)
364d5a08a60SJake Burkholder {
365d5a08a60SJake Burkholder 	struct rqhead *rqh;
366d5a08a60SJake Burkholder 	int pri;
367dba6c5a6SPeter Wemm 
3689727e637SJeff Roberson 	pri = td->td_priority / RQ_PPQ;
3699727e637SJeff Roberson 	td->td_rqindex = pri;
370d5a08a60SJake Burkholder 	runq_setbit(rq, pri);
371d5a08a60SJake Burkholder 	rqh = &rq->rq_queues[pri];
3729727e637SJeff Roberson 	CTR4(KTR_RUNQ, "runq_add: td=%p pri=%d %d rqh=%p",
3739727e637SJeff Roberson 	    td, td->td_priority, pri, rqh);
374c20c691bSJulian Elischer 	if (flags & SRQ_PREEMPTED) {
3759727e637SJeff Roberson 		TAILQ_INSERT_HEAD(rqh, td, td_runq);
376c20c691bSJulian Elischer 	} else {
3779727e637SJeff Roberson 		TAILQ_INSERT_TAIL(rqh, td, td_runq);
378dba6c5a6SPeter Wemm 	}
379c20c691bSJulian Elischer }
380d5a08a60SJake Burkholder 
3813fed7d23SJeff Roberson void
runq_add_pri(struct runq * rq,struct thread * td,u_char pri,int flags)3829727e637SJeff Roberson runq_add_pri(struct runq *rq, struct thread *td, u_char pri, int flags)
3833fed7d23SJeff Roberson {
3843fed7d23SJeff Roberson 	struct rqhead *rqh;
3853fed7d23SJeff Roberson 
3863fed7d23SJeff Roberson 	KASSERT(pri < RQ_NQS, ("runq_add_pri: %d out of range", pri));
3879727e637SJeff Roberson 	td->td_rqindex = pri;
3883fed7d23SJeff Roberson 	runq_setbit(rq, pri);
3893fed7d23SJeff Roberson 	rqh = &rq->rq_queues[pri];
3909727e637SJeff Roberson 	CTR4(KTR_RUNQ, "runq_add_pri: td=%p pri=%d idx=%d rqh=%p",
3919727e637SJeff Roberson 	    td, td->td_priority, pri, rqh);
3923fed7d23SJeff Roberson 	if (flags & SRQ_PREEMPTED) {
3939727e637SJeff Roberson 		TAILQ_INSERT_HEAD(rqh, td, td_runq);
3943fed7d23SJeff Roberson 	} else {
3959727e637SJeff Roberson 		TAILQ_INSERT_TAIL(rqh, td, td_runq);
3963fed7d23SJeff Roberson 	}
3973fed7d23SJeff Roberson }
398d5a08a60SJake Burkholder /*
399d5a08a60SJake Burkholder  * Return true if there are runnable processes of any priority on the run
400d5a08a60SJake Burkholder  * queue, false otherwise.  Has no side effects, does not modify the run
401d5a08a60SJake Burkholder  * queue structure.
402d5a08a60SJake Burkholder  */
403d5a08a60SJake Burkholder int
runq_check(struct runq * rq)404d5a08a60SJake Burkholder runq_check(struct runq *rq)
405d5a08a60SJake Burkholder {
406d5a08a60SJake Burkholder 	struct rqbits *rqb;
407d5a08a60SJake Burkholder 	int i;
408d5a08a60SJake Burkholder 
409d5a08a60SJake Burkholder 	rqb = &rq->rq_status;
410d5a08a60SJake Burkholder 	for (i = 0; i < RQB_LEN; i++)
411d5a08a60SJake Burkholder 		if (rqb->rqb_bits[i]) {
412d5a08a60SJake Burkholder 			CTR2(KTR_RUNQ, "runq_check: bits=%#x i=%d",
413d5a08a60SJake Burkholder 			    rqb->rqb_bits[i], i);
414d5a08a60SJake Burkholder 			return (1);
415dba6c5a6SPeter Wemm 		}
416d5a08a60SJake Burkholder 	CTR0(KTR_RUNQ, "runq_check: empty");
417d5a08a60SJake Burkholder 
418d5a08a60SJake Burkholder 	return (0);
419dba6c5a6SPeter Wemm }
420d5a08a60SJake Burkholder 
421a90f3f25SJeff Roberson /*
422a90f3f25SJeff Roberson  * Find the highest priority process on the run queue.
423a90f3f25SJeff Roberson  */
4249727e637SJeff Roberson struct thread *
runq_choose_fuzz(struct runq * rq,int fuzz)425a90f3f25SJeff Roberson runq_choose_fuzz(struct runq *rq, int fuzz)
426a90f3f25SJeff Roberson {
427a90f3f25SJeff Roberson 	struct rqhead *rqh;
4289727e637SJeff Roberson 	struct thread *td;
429a90f3f25SJeff Roberson 	int pri;
430a90f3f25SJeff Roberson 
431a90f3f25SJeff Roberson 	while ((pri = runq_findbit(rq)) != -1) {
432a90f3f25SJeff Roberson 		rqh = &rq->rq_queues[pri];
433a90f3f25SJeff Roberson 		/* fuzz == 1 is normal.. 0 or less are ignored */
434a90f3f25SJeff Roberson 		if (fuzz > 1) {
435a90f3f25SJeff Roberson 			/*
436a90f3f25SJeff Roberson 			 * In the first couple of entries, check if
437a90f3f25SJeff Roberson 			 * there is one for our CPU as a preference.
438a90f3f25SJeff Roberson 			 */
439a90f3f25SJeff Roberson 			int count = fuzz;
440a90f3f25SJeff Roberson 			int cpu = PCPU_GET(cpuid);
4419727e637SJeff Roberson 			struct thread *td2;
4429727e637SJeff Roberson 			td2 = td = TAILQ_FIRST(rqh);
443a90f3f25SJeff Roberson 
4449727e637SJeff Roberson 			while (count-- && td2) {
445681e4062SJulian Elischer 				if (td2->td_lastcpu == cpu) {
4469727e637SJeff Roberson 					td = td2;
447a90f3f25SJeff Roberson 					break;
448a90f3f25SJeff Roberson 				}
4499727e637SJeff Roberson 				td2 = TAILQ_NEXT(td2, td_runq);
450a90f3f25SJeff Roberson 			}
451a90f3f25SJeff Roberson 		} else
4529727e637SJeff Roberson 			td = TAILQ_FIRST(rqh);
4539727e637SJeff Roberson 		KASSERT(td != NULL, ("runq_choose_fuzz: no proc on busy queue"));
454a90f3f25SJeff Roberson 		CTR3(KTR_RUNQ,
4559727e637SJeff Roberson 		    "runq_choose_fuzz: pri=%d thread=%p rqh=%p", pri, td, rqh);
4569727e637SJeff Roberson 		return (td);
457a90f3f25SJeff Roberson 	}
458a90f3f25SJeff Roberson 	CTR1(KTR_RUNQ, "runq_choose_fuzz: idleproc pri=%d", pri);
459a90f3f25SJeff Roberson 
460a90f3f25SJeff Roberson 	return (NULL);
461a90f3f25SJeff Roberson }
4626804a3abSJulian Elischer 
463d5a08a60SJake Burkholder /*
464b43179fbSJeff Roberson  * Find the highest priority process on the run queue.
465d5a08a60SJake Burkholder  */
4669727e637SJeff Roberson struct thread *
runq_choose(struct runq * rq)467d5a08a60SJake Burkholder runq_choose(struct runq *rq)
468d5a08a60SJake Burkholder {
469d5a08a60SJake Burkholder 	struct rqhead *rqh;
4709727e637SJeff Roberson 	struct thread *td;
471d5a08a60SJake Burkholder 	int pri;
472d5a08a60SJake Burkholder 
473e602ba25SJulian Elischer 	while ((pri = runq_findbit(rq)) != -1) {
474d5a08a60SJake Burkholder 		rqh = &rq->rq_queues[pri];
4759727e637SJeff Roberson 		td = TAILQ_FIRST(rqh);
4769727e637SJeff Roberson 		KASSERT(td != NULL, ("runq_choose: no thread on busy queue"));
477e602ba25SJulian Elischer 		CTR3(KTR_RUNQ,
4789727e637SJeff Roberson 		    "runq_choose: pri=%d thread=%p rqh=%p", pri, td, rqh);
4799727e637SJeff Roberson 		return (td);
480d5a08a60SJake Burkholder 	}
4819727e637SJeff Roberson 	CTR1(KTR_RUNQ, "runq_choose: idlethread pri=%d", pri);
482d5a08a60SJake Burkholder 
483e602ba25SJulian Elischer 	return (NULL);
484d5a08a60SJake Burkholder }
485d5a08a60SJake Burkholder 
4869727e637SJeff Roberson struct thread *
runq_choose_from(struct runq * rq,u_char idx)487ed0e8f2fSJeff Roberson runq_choose_from(struct runq *rq, u_char idx)
4883fed7d23SJeff Roberson {
4893fed7d23SJeff Roberson 	struct rqhead *rqh;
4909727e637SJeff Roberson 	struct thread *td;
4913fed7d23SJeff Roberson 	int pri;
4923fed7d23SJeff Roberson 
493cd49bb70SJeff Roberson 	if ((pri = runq_findbit_from(rq, idx)) != -1) {
4943fed7d23SJeff Roberson 		rqh = &rq->rq_queues[pri];
4959727e637SJeff Roberson 		td = TAILQ_FIRST(rqh);
4969727e637SJeff Roberson 		KASSERT(td != NULL, ("runq_choose: no thread on busy queue"));
4973fed7d23SJeff Roberson 		CTR4(KTR_RUNQ,
4989727e637SJeff Roberson 		    "runq_choose_from: pri=%d thread=%p idx=%d rqh=%p",
4999727e637SJeff Roberson 		    pri, td, td->td_rqindex, rqh);
5009727e637SJeff Roberson 		return (td);
5013fed7d23SJeff Roberson 	}
5029727e637SJeff Roberson 	CTR1(KTR_RUNQ, "runq_choose_from: idlethread pri=%d", pri);
5033fed7d23SJeff Roberson 
5043fed7d23SJeff Roberson 	return (NULL);
5053fed7d23SJeff Roberson }
506d5a08a60SJake Burkholder /*
507ad1e7d28SJulian Elischer  * Remove the thread from the queue specified by its priority, and clear the
508d5a08a60SJake Burkholder  * corresponding status bit if the queue becomes empty.
509f0393f06SJeff Roberson  * Caller must set state afterwards.
510d5a08a60SJake Burkholder  */
511d5a08a60SJake Burkholder void
runq_remove(struct runq * rq,struct thread * td)5129727e637SJeff Roberson runq_remove(struct runq *rq, struct thread *td)
513d5a08a60SJake Burkholder {
5143fed7d23SJeff Roberson 
5159727e637SJeff Roberson 	runq_remove_idx(rq, td, NULL);
5163fed7d23SJeff Roberson }
5173fed7d23SJeff Roberson 
5183fed7d23SJeff Roberson void
runq_remove_idx(struct runq * rq,struct thread * td,u_char * idx)5199727e637SJeff Roberson runq_remove_idx(struct runq *rq, struct thread *td, u_char *idx)
5203fed7d23SJeff Roberson {
521d5a08a60SJake Burkholder 	struct rqhead *rqh;
522ed0e8f2fSJeff Roberson 	u_char pri;
523d5a08a60SJake Burkholder 
5249727e637SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
525b61ce5b0SJeff Roberson 		("runq_remove_idx: thread swapped out"));
5269727e637SJeff Roberson 	pri = td->td_rqindex;
5277b20fb19SJeff Roberson 	KASSERT(pri < RQ_NQS, ("runq_remove_idx: Invalid index %d\n", pri));
528d5a08a60SJake Burkholder 	rqh = &rq->rq_queues[pri];
5299727e637SJeff Roberson 	CTR4(KTR_RUNQ, "runq_remove_idx: td=%p, pri=%d %d rqh=%p",
5309727e637SJeff Roberson 	    td, td->td_priority, pri, rqh);
5319727e637SJeff Roberson 	TAILQ_REMOVE(rqh, td, td_runq);
532d5a08a60SJake Burkholder 	if (TAILQ_EMPTY(rqh)) {
5333fed7d23SJeff Roberson 		CTR0(KTR_RUNQ, "runq_remove_idx: empty");
534d5a08a60SJake Burkholder 		runq_clrbit(rq, pri);
5353fed7d23SJeff Roberson 		if (idx != NULL && *idx == pri)
5363fed7d23SJeff Roberson 			*idx = (pri + 1) % RQ_NQS;
537d5a08a60SJake Burkholder 	}
538dba6c5a6SPeter Wemm }
539