xref: /freebsd/sys/kern/sched_ule.c (revision 8f51ad55e728371ce9a00ef8fb9fcc89b9b3a7e6)
135e6168fSJeff Roberson /*-
2e7d50326SJeff Roberson  * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org>
335e6168fSJeff Roberson  * All rights reserved.
435e6168fSJeff Roberson  *
535e6168fSJeff Roberson  * Redistribution and use in source and binary forms, with or without
635e6168fSJeff Roberson  * modification, are permitted provided that the following conditions
735e6168fSJeff Roberson  * are met:
835e6168fSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
935e6168fSJeff Roberson  *    notice unmodified, this list of conditions, and the following
1035e6168fSJeff Roberson  *    disclaimer.
1135e6168fSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
1235e6168fSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
1335e6168fSJeff Roberson  *    documentation and/or other materials provided with the distribution.
1435e6168fSJeff Roberson  *
1535e6168fSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1635e6168fSJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1735e6168fSJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1835e6168fSJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1935e6168fSJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2035e6168fSJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2135e6168fSJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2235e6168fSJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2335e6168fSJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2435e6168fSJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2535e6168fSJeff Roberson  */
2635e6168fSJeff Roberson 
27ae7a6b38SJeff Roberson /*
28ae7a6b38SJeff Roberson  * This file implements the ULE scheduler.  ULE supports independent CPU
29ae7a6b38SJeff Roberson  * run queues and fine grain locking.  It has superior interactive
30ae7a6b38SJeff Roberson  * performance under load even on uni-processor systems.
31ae7a6b38SJeff Roberson  *
32ae7a6b38SJeff Roberson  * etymology:
33a5423ea3SJeff Roberson  *   ULE is the last three letters in schedule.  It owes its name to a
34ae7a6b38SJeff Roberson  * generic user created for a scheduling system by Paul Mikesell at
35ae7a6b38SJeff Roberson  * Isilon Systems and a general lack of creativity on the part of the author.
36ae7a6b38SJeff Roberson  */
37ae7a6b38SJeff Roberson 
38677b542eSDavid E. O'Brien #include <sys/cdefs.h>
39677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40677b542eSDavid E. O'Brien 
414da0d332SPeter Wemm #include "opt_hwpmc_hooks.h"
426f5f25e5SJohn Birrell #include "opt_kdtrace.h"
434da0d332SPeter Wemm #include "opt_sched.h"
449923b511SScott Long 
4535e6168fSJeff Roberson #include <sys/param.h>
4635e6168fSJeff Roberson #include <sys/systm.h>
472c3490b1SMarcel Moolenaar #include <sys/kdb.h>
4835e6168fSJeff Roberson #include <sys/kernel.h>
4935e6168fSJeff Roberson #include <sys/ktr.h>
5035e6168fSJeff Roberson #include <sys/lock.h>
5135e6168fSJeff Roberson #include <sys/mutex.h>
5235e6168fSJeff Roberson #include <sys/proc.h>
53245f3abfSJeff Roberson #include <sys/resource.h>
549bacd788SJeff Roberson #include <sys/resourcevar.h>
5535e6168fSJeff Roberson #include <sys/sched.h>
5635e6168fSJeff Roberson #include <sys/smp.h>
5735e6168fSJeff Roberson #include <sys/sx.h>
5835e6168fSJeff Roberson #include <sys/sysctl.h>
5935e6168fSJeff Roberson #include <sys/sysproto.h>
60f5c157d9SJohn Baldwin #include <sys/turnstile.h>
613db720fdSDavid Xu #include <sys/umtx.h>
6235e6168fSJeff Roberson #include <sys/vmmeter.h>
6362fa74d9SJeff Roberson #include <sys/cpuset.h>
6407095abfSIvan Voras #include <sys/sbuf.h>
6535e6168fSJeff Roberson #ifdef KTRACE
6635e6168fSJeff Roberson #include <sys/uio.h>
6735e6168fSJeff Roberson #include <sys/ktrace.h>
6835e6168fSJeff Roberson #endif
6935e6168fSJeff Roberson 
70ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS
71ebccf1e3SJoseph Koshy #include <sys/pmckern.h>
72ebccf1e3SJoseph Koshy #endif
73ebccf1e3SJoseph Koshy 
746f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS
756f5f25e5SJohn Birrell #include <sys/dtrace_bsd.h>
766f5f25e5SJohn Birrell int				dtrace_vtime_active;
776f5f25e5SJohn Birrell dtrace_vtime_switch_func_t	dtrace_vtime_switch_func;
786f5f25e5SJohn Birrell #endif
796f5f25e5SJohn Birrell 
8035e6168fSJeff Roberson #include <machine/cpu.h>
8122bf7d9aSJeff Roberson #include <machine/smp.h>
8235e6168fSJeff Roberson 
83495168baSMarcel Moolenaar #if defined(__sparc64__) || defined(__mips__)
8402e2d6b4SJeff Roberson #error "This architecture is not currently compatible with ULE"
857a5e5e2aSJeff Roberson #endif
867a5e5e2aSJeff Roberson 
87ae7a6b38SJeff Roberson #define	KTR_ULE	0
8814618990SJeff Roberson 
898f51ad55SJeff Roberson #define	TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__STRING(UINT_MAX)))
908f51ad55SJeff Roberson #define	TDQ_NAME_LEN	(sizeof("sched lock ") + sizeof(__STRING(MAXCPU)))
918f51ad55SJeff Roberson #define	TDQ_LOADNAME_LEN	(PCPU_NAME_LEN + sizeof(" load"))
928f51ad55SJeff Roberson 
936b2f763fSJeff Roberson /*
94ae7a6b38SJeff Roberson  * Thread scheduler specific section.  All fields are protected
95ae7a6b38SJeff Roberson  * by the thread lock.
96ed062c8dSJulian Elischer  */
97ad1e7d28SJulian Elischer struct td_sched {
98ae7a6b38SJeff Roberson 	struct runq	*ts_runq;	/* Run-queue we're queued on. */
99ae7a6b38SJeff Roberson 	short		ts_flags;	/* TSF_* flags. */
100ad1e7d28SJulian Elischer 	u_char		ts_cpu;		/* CPU that we have affinity for. */
10173daf66fSJeff Roberson 	int		ts_rltick;	/* Real last tick, for affinity. */
102ae7a6b38SJeff Roberson 	int		ts_slice;	/* Ticks of slice remaining. */
103ae7a6b38SJeff Roberson 	u_int		ts_slptime;	/* Number of ticks we vol. slept */
104ae7a6b38SJeff Roberson 	u_int		ts_runtime;	/* Number of ticks we were running */
105ad1e7d28SJulian Elischer 	int		ts_ltick;	/* Last tick that we were running on */
106ad1e7d28SJulian Elischer 	int		ts_ftick;	/* First tick that we were running on */
107ad1e7d28SJulian Elischer 	int		ts_ticks;	/* Tick count */
1088f51ad55SJeff Roberson #ifdef KTR
1098f51ad55SJeff Roberson 	char		ts_name[TS_NAME_LEN];
1108f51ad55SJeff Roberson #endif
111ed062c8dSJulian Elischer };
112ad1e7d28SJulian Elischer /* flags kept in ts_flags */
1137b8bfa0dSJeff Roberson #define	TSF_BOUND	0x0001		/* Thread can not migrate. */
1147b8bfa0dSJeff Roberson #define	TSF_XFERABLE	0x0002		/* Thread was added as transferable. */
11535e6168fSJeff Roberson 
116ad1e7d28SJulian Elischer static struct td_sched td_sched0;
11735e6168fSJeff Roberson 
11862fa74d9SJeff Roberson #define	THREAD_CAN_MIGRATE(td)	((td)->td_pinned == 0)
11962fa74d9SJeff Roberson #define	THREAD_CAN_SCHED(td, cpu)	\
12062fa74d9SJeff Roberson     CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
12162fa74d9SJeff Roberson 
12235e6168fSJeff Roberson /*
123e7d50326SJeff Roberson  * Cpu percentage computation macros and defines.
124e1f89c22SJeff Roberson  *
125e7d50326SJeff Roberson  * SCHED_TICK_SECS:	Number of seconds to average the cpu usage across.
126e7d50326SJeff Roberson  * SCHED_TICK_TARG:	Number of hz ticks to average the cpu usage across.
1278ab80cf0SJeff Roberson  * SCHED_TICK_MAX:	Maximum number of ticks before scaling back.
128e7d50326SJeff Roberson  * SCHED_TICK_SHIFT:	Shift factor to avoid rounding away results.
129e7d50326SJeff Roberson  * SCHED_TICK_HZ:	Compute the number of hz ticks for a given ticks count.
130e7d50326SJeff Roberson  * SCHED_TICK_TOTAL:	Gives the amount of time we've been recording ticks.
13135e6168fSJeff Roberson  */
132e7d50326SJeff Roberson #define	SCHED_TICK_SECS		10
133e7d50326SJeff Roberson #define	SCHED_TICK_TARG		(hz * SCHED_TICK_SECS)
1348ab80cf0SJeff Roberson #define	SCHED_TICK_MAX		(SCHED_TICK_TARG + hz)
135e7d50326SJeff Roberson #define	SCHED_TICK_SHIFT	10
136e7d50326SJeff Roberson #define	SCHED_TICK_HZ(ts)	((ts)->ts_ticks >> SCHED_TICK_SHIFT)
137eddb4efaSJeff Roberson #define	SCHED_TICK_TOTAL(ts)	(max((ts)->ts_ltick - (ts)->ts_ftick, hz))
13835e6168fSJeff Roberson 
13935e6168fSJeff Roberson /*
140e7d50326SJeff Roberson  * These macros determine priorities for non-interactive threads.  They are
141e7d50326SJeff Roberson  * assigned a priority based on their recent cpu utilization as expressed
142e7d50326SJeff Roberson  * by the ratio of ticks to the tick total.  NHALF priorities at the start
143e7d50326SJeff Roberson  * and end of the MIN to MAX timeshare range are only reachable with negative
144e7d50326SJeff Roberson  * or positive nice respectively.
145e7d50326SJeff Roberson  *
146e7d50326SJeff Roberson  * PRI_RANGE:	Priority range for utilization dependent priorities.
147e7d50326SJeff Roberson  * PRI_NRESV:	Number of nice values.
148e7d50326SJeff Roberson  * PRI_TICKS:	Compute a priority in PRI_RANGE from the ticks count and total.
149e7d50326SJeff Roberson  * PRI_NICE:	Determines the part of the priority inherited from nice.
150e7d50326SJeff Roberson  */
151e7d50326SJeff Roberson #define	SCHED_PRI_NRESV		(PRIO_MAX - PRIO_MIN)
152e7d50326SJeff Roberson #define	SCHED_PRI_NHALF		(SCHED_PRI_NRESV / 2)
153e7d50326SJeff Roberson #define	SCHED_PRI_MIN		(PRI_MIN_TIMESHARE + SCHED_PRI_NHALF)
154e7d50326SJeff Roberson #define	SCHED_PRI_MAX		(PRI_MAX_TIMESHARE - SCHED_PRI_NHALF)
155dda713dfSJeff Roberson #define	SCHED_PRI_RANGE		(SCHED_PRI_MAX - SCHED_PRI_MIN)
156e7d50326SJeff Roberson #define	SCHED_PRI_TICKS(ts)						\
157e7d50326SJeff Roberson     (SCHED_TICK_HZ((ts)) /						\
1581e516cf5SJeff Roberson     (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
159e7d50326SJeff Roberson #define	SCHED_PRI_NICE(nice)	(nice)
160e7d50326SJeff Roberson 
161e7d50326SJeff Roberson /*
162e7d50326SJeff Roberson  * These determine the interactivity of a process.  Interactivity differs from
163e7d50326SJeff Roberson  * cpu utilization in that it expresses the voluntary time slept vs time ran
164e7d50326SJeff Roberson  * while cpu utilization includes all time not running.  This more accurately
165e7d50326SJeff Roberson  * models the intent of the thread.
16635e6168fSJeff Roberson  *
167407b0157SJeff Roberson  * SLP_RUN_MAX:	Maximum amount of sleep time + run time we'll accumulate
168407b0157SJeff Roberson  *		before throttling back.
169d322132cSJeff Roberson  * SLP_RUN_FORK:	Maximum slp+run time to inherit at fork time.
170210491d3SJeff Roberson  * INTERACT_MAX:	Maximum interactivity value.  Smaller is better.
171e1f89c22SJeff Roberson  * INTERACT_THRESH:	Threshhold for placement on the current runq.
17235e6168fSJeff Roberson  */
173e7d50326SJeff Roberson #define	SCHED_SLP_RUN_MAX	((hz * 5) << SCHED_TICK_SHIFT)
174e7d50326SJeff Roberson #define	SCHED_SLP_RUN_FORK	((hz / 2) << SCHED_TICK_SHIFT)
175210491d3SJeff Roberson #define	SCHED_INTERACT_MAX	(100)
176210491d3SJeff Roberson #define	SCHED_INTERACT_HALF	(SCHED_INTERACT_MAX / 2)
1774c9612c6SJeff Roberson #define	SCHED_INTERACT_THRESH	(30)
178e1f89c22SJeff Roberson 
17935e6168fSJeff Roberson /*
180e7d50326SJeff Roberson  * tickincr:		Converts a stathz tick into a hz domain scaled by
181e7d50326SJeff Roberson  *			the shift factor.  Without the shift the error rate
182e7d50326SJeff Roberson  *			due to rounding would be unacceptably high.
183e7d50326SJeff Roberson  * realstathz:		stathz is sometimes 0 and run off of hz.
184e7d50326SJeff Roberson  * sched_slice:		Runtime of each thread before rescheduling.
185ae7a6b38SJeff Roberson  * preempt_thresh:	Priority threshold for preemption and remote IPIs.
18635e6168fSJeff Roberson  */
187e7d50326SJeff Roberson static int sched_interact = SCHED_INTERACT_THRESH;
188e7d50326SJeff Roberson static int realstathz;
189e7d50326SJeff Roberson static int tickincr;
19073daf66fSJeff Roberson static int sched_slice = 1;
19102e2d6b4SJeff Roberson #ifdef PREEMPTION
19202e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION
19302e2d6b4SJeff Roberson static int preempt_thresh = PRI_MAX_IDLE;
19402e2d6b4SJeff Roberson #else
195ae7a6b38SJeff Roberson static int preempt_thresh = PRI_MIN_KERN;
19602e2d6b4SJeff Roberson #endif
19702e2d6b4SJeff Roberson #else
19802e2d6b4SJeff Roberson static int preempt_thresh = 0;
19902e2d6b4SJeff Roberson #endif
2000502fe2eSJeff Roberson static int static_boost = PRI_MIN_TIMESHARE;
2011690c6c1SJeff Roberson static int sched_idlespins = 10000;
2021690c6c1SJeff Roberson static int sched_idlespinthresh = 4;
203ae7a6b38SJeff Roberson 
20435e6168fSJeff Roberson /*
205ae7a6b38SJeff Roberson  * tdq - per processor runqs and statistics.  All fields are protected by the
206ae7a6b38SJeff Roberson  * tdq_lock.  The load and lowpri may be accessed without to avoid excess
207ae7a6b38SJeff Roberson  * locking in sched_pickcpu();
20835e6168fSJeff Roberson  */
209ad1e7d28SJulian Elischer struct tdq {
21073daf66fSJeff Roberson 	/* Ordered to improve efficiency of cpu_search() and switch(). */
21162fa74d9SJeff Roberson 	struct mtx	tdq_lock;		/* run queue lock. */
21273daf66fSJeff Roberson 	struct cpu_group *tdq_cg;		/* Pointer to cpu topology. */
2131690c6c1SJeff Roberson 	volatile int	tdq_load;		/* Aggregate load. */
21473daf66fSJeff Roberson 	int		tdq_sysload;		/* For loadavg, !ITHD load. */
21573daf66fSJeff Roberson 	int		tdq_transferable;	/* Transferable thread count. */
2161690c6c1SJeff Roberson 	volatile int	tdq_idlestate;		/* State of the idle thread. */
2171690c6c1SJeff Roberson 	short		tdq_switchcnt;		/* Switches this tick. */
2181690c6c1SJeff Roberson 	short		tdq_oldswitchcnt;	/* Switches last tick. */
21973daf66fSJeff Roberson 	u_char		tdq_lowpri;		/* Lowest priority thread. */
22073daf66fSJeff Roberson 	u_char		tdq_ipipending;		/* IPI pending. */
22173daf66fSJeff Roberson 	u_char		tdq_idx;		/* Current insert index. */
22273daf66fSJeff Roberson 	u_char		tdq_ridx;		/* Current removal index. */
223e7d50326SJeff Roberson 	struct runq	tdq_realtime;		/* real-time run queue. */
224ae7a6b38SJeff Roberson 	struct runq	tdq_timeshare;		/* timeshare run queue. */
225ae7a6b38SJeff Roberson 	struct runq	tdq_idle;		/* Queue of IDLE threads. */
2268f51ad55SJeff Roberson 	char		tdq_name[TDQ_NAME_LEN];
2278f51ad55SJeff Roberson #ifdef KTR
2288f51ad55SJeff Roberson 	char		tdq_loadname[TDQ_LOADNAME_LEN];
2298f51ad55SJeff Roberson #endif
230ae7a6b38SJeff Roberson } __aligned(64);
23135e6168fSJeff Roberson 
2321690c6c1SJeff Roberson /* Idle thread states and config. */
2331690c6c1SJeff Roberson #define	TDQ_RUNNING	1
2341690c6c1SJeff Roberson #define	TDQ_IDLE	2
2357b8bfa0dSJeff Roberson 
23680f86c9fSJeff Roberson #ifdef SMP
23707095abfSIvan Voras struct cpu_group *cpu_top;		/* CPU topology */
2387b8bfa0dSJeff Roberson 
23962fa74d9SJeff Roberson #define	SCHED_AFFINITY_DEFAULT	(max(1, hz / 1000))
24062fa74d9SJeff Roberson #define	SCHED_AFFINITY(ts, t)	((ts)->ts_rltick > ticks - ((t) * affinity))
2417b8bfa0dSJeff Roberson 
2427b8bfa0dSJeff Roberson /*
2437b8bfa0dSJeff Roberson  * Run-time tunables.
2447b8bfa0dSJeff Roberson  */
24528994a58SJeff Roberson static int rebalance = 1;
2467fcf154aSJeff Roberson static int balance_interval = 128;	/* Default set in sched_initticks(). */
2477b8bfa0dSJeff Roberson static int affinity;
2487fcf154aSJeff Roberson static int steal_htt = 1;
24928994a58SJeff Roberson static int steal_idle = 1;
25028994a58SJeff Roberson static int steal_thresh = 2;
25180f86c9fSJeff Roberson 
25235e6168fSJeff Roberson /*
253d2ad694cSJeff Roberson  * One thread queue per processor.
25435e6168fSJeff Roberson  */
255ad1e7d28SJulian Elischer static struct tdq	tdq_cpu[MAXCPU];
2567fcf154aSJeff Roberson static struct tdq	*balance_tdq;
2577fcf154aSJeff Roberson static int balance_ticks;
258dc03363dSJeff Roberson 
259ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu[PCPU_GET(cpuid)])
260ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu[(x)])
261c47f202bSJeff Roberson #define	TDQ_ID(x)	((int)((x) - tdq_cpu))
26280f86c9fSJeff Roberson #else	/* !SMP */
263ad1e7d28SJulian Elischer static struct tdq	tdq_cpu;
264dc03363dSJeff Roberson 
26536b36916SJeff Roberson #define	TDQ_ID(x)	(0)
266ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu)
267ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu)
2680a016a05SJeff Roberson #endif
26935e6168fSJeff Roberson 
270ae7a6b38SJeff Roberson #define	TDQ_LOCK_ASSERT(t, type)	mtx_assert(TDQ_LOCKPTR((t)), (type))
271ae7a6b38SJeff Roberson #define	TDQ_LOCK(t)		mtx_lock_spin(TDQ_LOCKPTR((t)))
272ae7a6b38SJeff Roberson #define	TDQ_LOCK_FLAGS(t, f)	mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
273ae7a6b38SJeff Roberson #define	TDQ_UNLOCK(t)		mtx_unlock_spin(TDQ_LOCKPTR((t)))
27462fa74d9SJeff Roberson #define	TDQ_LOCKPTR(t)		(&(t)->tdq_lock)
275ae7a6b38SJeff Roberson 
2768460a577SJohn Birrell static void sched_priority(struct thread *);
27721381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char);
2788460a577SJohn Birrell static int sched_interact_score(struct thread *);
2798460a577SJohn Birrell static void sched_interact_update(struct thread *);
2808460a577SJohn Birrell static void sched_interact_fork(struct thread *);
281ad1e7d28SJulian Elischer static void sched_pctcpu_update(struct td_sched *);
28235e6168fSJeff Roberson 
2835d7ef00cSJeff Roberson /* Operations on per processor queues */
2849727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *);
285ad1e7d28SJulian Elischer static void tdq_setup(struct tdq *);
2869727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *);
2879727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *);
2889727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int);
2899727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *);
290ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int);
291ad1e7d28SJulian Elischer void tdq_print(int cpu);
292e7d50326SJeff Roberson static void runq_print(struct runq *rq);
293ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int);
2945d7ef00cSJeff Roberson #ifdef SMP
29562fa74d9SJeff Roberson static int tdq_move(struct tdq *, struct tdq *);
296ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *);
2979727e637SJeff Roberson static void tdq_notify(struct tdq *, struct thread *);
2989727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int);
2999727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int);
3009727e637SJeff Roberson static int sched_pickcpu(struct thread *, int);
3017fcf154aSJeff Roberson static void sched_balance(void);
30262fa74d9SJeff Roberson static int sched_balance_pair(struct tdq *, struct tdq *);
3039727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int);
304ae7a6b38SJeff Roberson static inline struct mtx *thread_block_switch(struct thread *);
305ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *);
306c47f202bSJeff Roberson static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int);
30707095abfSIvan Voras static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS);
30807095abfSIvan Voras static int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb,
30907095abfSIvan Voras     struct cpu_group *cg, int indent);
3105d7ef00cSJeff Roberson #endif
3115d7ef00cSJeff Roberson 
312e7d50326SJeff Roberson static void sched_setup(void *dummy);
313237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
314e7d50326SJeff Roberson 
315e7d50326SJeff Roberson static void sched_initticks(void *dummy);
316237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks,
317237fdd78SRobert Watson     NULL);
318e7d50326SJeff Roberson 
319ae7a6b38SJeff Roberson /*
320ae7a6b38SJeff Roberson  * Print the threads waiting on a run-queue.
321ae7a6b38SJeff Roberson  */
322e7d50326SJeff Roberson static void
323e7d50326SJeff Roberson runq_print(struct runq *rq)
324e7d50326SJeff Roberson {
325e7d50326SJeff Roberson 	struct rqhead *rqh;
3269727e637SJeff Roberson 	struct thread *td;
327e7d50326SJeff Roberson 	int pri;
328e7d50326SJeff Roberson 	int j;
329e7d50326SJeff Roberson 	int i;
330e7d50326SJeff Roberson 
331e7d50326SJeff Roberson 	for (i = 0; i < RQB_LEN; i++) {
332e7d50326SJeff Roberson 		printf("\t\trunq bits %d 0x%zx\n",
333e7d50326SJeff Roberson 		    i, rq->rq_status.rqb_bits[i]);
334e7d50326SJeff Roberson 		for (j = 0; j < RQB_BPW; j++)
335e7d50326SJeff Roberson 			if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
336e7d50326SJeff Roberson 				pri = j + (i << RQB_L2BPW);
337e7d50326SJeff Roberson 				rqh = &rq->rq_queues[pri];
3389727e637SJeff Roberson 				TAILQ_FOREACH(td, rqh, td_runq) {
339e7d50326SJeff Roberson 					printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
3409727e637SJeff Roberson 					    td, td->td_name, td->td_priority,
3419727e637SJeff Roberson 					    td->td_rqindex, pri);
342e7d50326SJeff Roberson 				}
343e7d50326SJeff Roberson 			}
344e7d50326SJeff Roberson 	}
345e7d50326SJeff Roberson }
346e7d50326SJeff Roberson 
347ae7a6b38SJeff Roberson /*
348ae7a6b38SJeff Roberson  * Print the status of a per-cpu thread queue.  Should be a ddb show cmd.
349ae7a6b38SJeff Roberson  */
35015dc847eSJeff Roberson void
351ad1e7d28SJulian Elischer tdq_print(int cpu)
35215dc847eSJeff Roberson {
353ad1e7d28SJulian Elischer 	struct tdq *tdq;
35415dc847eSJeff Roberson 
355ad1e7d28SJulian Elischer 	tdq = TDQ_CPU(cpu);
35615dc847eSJeff Roberson 
357c47f202bSJeff Roberson 	printf("tdq %d:\n", TDQ_ID(tdq));
35862fa74d9SJeff Roberson 	printf("\tlock            %p\n", TDQ_LOCKPTR(tdq));
35962fa74d9SJeff Roberson 	printf("\tLock name:      %s\n", tdq->tdq_name);
360d2ad694cSJeff Roberson 	printf("\tload:           %d\n", tdq->tdq_load);
3611690c6c1SJeff Roberson 	printf("\tswitch cnt:     %d\n", tdq->tdq_switchcnt);
3621690c6c1SJeff Roberson 	printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt);
3631690c6c1SJeff Roberson 	printf("\tidle state:     %d\n", tdq->tdq_idlestate);
364e7d50326SJeff Roberson 	printf("\ttimeshare idx:  %d\n", tdq->tdq_idx);
3653f872f85SJeff Roberson 	printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
3661690c6c1SJeff Roberson 	printf("\tload transferable: %d\n", tdq->tdq_transferable);
3671690c6c1SJeff Roberson 	printf("\tlowest priority:   %d\n", tdq->tdq_lowpri);
368e7d50326SJeff Roberson 	printf("\trealtime runq:\n");
369e7d50326SJeff Roberson 	runq_print(&tdq->tdq_realtime);
370e7d50326SJeff Roberson 	printf("\ttimeshare runq:\n");
371e7d50326SJeff Roberson 	runq_print(&tdq->tdq_timeshare);
372e7d50326SJeff Roberson 	printf("\tidle runq:\n");
373e7d50326SJeff Roberson 	runq_print(&tdq->tdq_idle);
37415dc847eSJeff Roberson }
37515dc847eSJeff Roberson 
376ff256d9cSJeff Roberson static inline int
377ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote)
378ff256d9cSJeff Roberson {
379ff256d9cSJeff Roberson 	/*
380ff256d9cSJeff Roberson 	 * If the new priority is not better than the current priority there is
381ff256d9cSJeff Roberson 	 * nothing to do.
382ff256d9cSJeff Roberson 	 */
383ff256d9cSJeff Roberson 	if (pri >= cpri)
384ff256d9cSJeff Roberson 		return (0);
385ff256d9cSJeff Roberson 	/*
386ff256d9cSJeff Roberson 	 * Always preempt idle.
387ff256d9cSJeff Roberson 	 */
388ff256d9cSJeff Roberson 	if (cpri >= PRI_MIN_IDLE)
389ff256d9cSJeff Roberson 		return (1);
390ff256d9cSJeff Roberson 	/*
391ff256d9cSJeff Roberson 	 * If preemption is disabled don't preempt others.
392ff256d9cSJeff Roberson 	 */
393ff256d9cSJeff Roberson 	if (preempt_thresh == 0)
394ff256d9cSJeff Roberson 		return (0);
395ff256d9cSJeff Roberson 	/*
396ff256d9cSJeff Roberson 	 * Preempt if we exceed the threshold.
397ff256d9cSJeff Roberson 	 */
398ff256d9cSJeff Roberson 	if (pri <= preempt_thresh)
399ff256d9cSJeff Roberson 		return (1);
400ff256d9cSJeff Roberson 	/*
401ff256d9cSJeff Roberson 	 * If we're realtime or better and there is timeshare or worse running
402ff256d9cSJeff Roberson 	 * preempt only remote processors.
403ff256d9cSJeff Roberson 	 */
404ff256d9cSJeff Roberson 	if (remote && pri <= PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
405ff256d9cSJeff Roberson 		return (1);
406ff256d9cSJeff Roberson 	return (0);
407ff256d9cSJeff Roberson }
408ff256d9cSJeff Roberson 
409ae7a6b38SJeff Roberson #define	TS_RQ_PPQ	(((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS)
410ae7a6b38SJeff Roberson /*
411ae7a6b38SJeff Roberson  * Add a thread to the actual run-queue.  Keeps transferable counts up to
412ae7a6b38SJeff Roberson  * date with what is actually on the run-queue.  Selects the correct
413ae7a6b38SJeff Roberson  * queue position for timeshare threads.
414ae7a6b38SJeff Roberson  */
415155b9987SJeff Roberson static __inline void
4169727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags)
417155b9987SJeff Roberson {
4189727e637SJeff Roberson 	struct td_sched *ts;
419c143ac21SJeff Roberson 	u_char pri;
420c143ac21SJeff Roberson 
421ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
4229727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
42373daf66fSJeff Roberson 
4249727e637SJeff Roberson 	pri = td->td_priority;
4259727e637SJeff Roberson 	ts = td->td_sched;
4269727e637SJeff Roberson 	TD_SET_RUNQ(td);
4279727e637SJeff Roberson 	if (THREAD_CAN_MIGRATE(td)) {
428d2ad694cSJeff Roberson 		tdq->tdq_transferable++;
429ad1e7d28SJulian Elischer 		ts->ts_flags |= TSF_XFERABLE;
43080f86c9fSJeff Roberson 	}
431c143ac21SJeff Roberson 	if (pri <= PRI_MAX_REALTIME) {
432c143ac21SJeff Roberson 		ts->ts_runq = &tdq->tdq_realtime;
433c143ac21SJeff Roberson 	} else if (pri <= PRI_MAX_TIMESHARE) {
434c143ac21SJeff Roberson 		ts->ts_runq = &tdq->tdq_timeshare;
435e7d50326SJeff Roberson 		KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
436e7d50326SJeff Roberson 			("Invalid priority %d on timeshare runq", pri));
437e7d50326SJeff Roberson 		/*
438e7d50326SJeff Roberson 		 * This queue contains only priorities between MIN and MAX
439e7d50326SJeff Roberson 		 * realtime.  Use the whole queue to represent these values.
440e7d50326SJeff Roberson 		 */
441c47f202bSJeff Roberson 		if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
442e7d50326SJeff Roberson 			pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
443e7d50326SJeff Roberson 			pri = (pri + tdq->tdq_idx) % RQ_NQS;
4443f872f85SJeff Roberson 			/*
4453f872f85SJeff Roberson 			 * This effectively shortens the queue by one so we
4463f872f85SJeff Roberson 			 * can have a one slot difference between idx and
4473f872f85SJeff Roberson 			 * ridx while we wait for threads to drain.
4483f872f85SJeff Roberson 			 */
4493f872f85SJeff Roberson 			if (tdq->tdq_ridx != tdq->tdq_idx &&
4503f872f85SJeff Roberson 			    pri == tdq->tdq_ridx)
4514499aff6SJeff Roberson 				pri = (unsigned char)(pri - 1) % RQ_NQS;
452e7d50326SJeff Roberson 		} else
4533f872f85SJeff Roberson 			pri = tdq->tdq_ridx;
4549727e637SJeff Roberson 		runq_add_pri(ts->ts_runq, td, pri, flags);
455c143ac21SJeff Roberson 		return;
456e7d50326SJeff Roberson 	} else
45773daf66fSJeff Roberson 		ts->ts_runq = &tdq->tdq_idle;
4589727e637SJeff Roberson 	runq_add(ts->ts_runq, td, flags);
45973daf66fSJeff Roberson }
46073daf66fSJeff Roberson 
46173daf66fSJeff Roberson /*
462ae7a6b38SJeff Roberson  * Remove a thread from a run-queue.  This typically happens when a thread
463ae7a6b38SJeff Roberson  * is selected to run.  Running threads are not on the queue and the
464ae7a6b38SJeff Roberson  * transferable count does not reflect them.
465ae7a6b38SJeff Roberson  */
466155b9987SJeff Roberson static __inline void
4679727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td)
468155b9987SJeff Roberson {
4699727e637SJeff Roberson 	struct td_sched *ts;
4709727e637SJeff Roberson 
4719727e637SJeff Roberson 	ts = td->td_sched;
472ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
473ae7a6b38SJeff Roberson 	KASSERT(ts->ts_runq != NULL,
4749727e637SJeff Roberson 	    ("tdq_runq_remove: thread %p null ts_runq", td));
475ad1e7d28SJulian Elischer 	if (ts->ts_flags & TSF_XFERABLE) {
476d2ad694cSJeff Roberson 		tdq->tdq_transferable--;
477ad1e7d28SJulian Elischer 		ts->ts_flags &= ~TSF_XFERABLE;
47880f86c9fSJeff Roberson 	}
4793f872f85SJeff Roberson 	if (ts->ts_runq == &tdq->tdq_timeshare) {
4803f872f85SJeff Roberson 		if (tdq->tdq_idx != tdq->tdq_ridx)
4819727e637SJeff Roberson 			runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx);
482e7d50326SJeff Roberson 		else
4839727e637SJeff Roberson 			runq_remove_idx(ts->ts_runq, td, NULL);
4843f872f85SJeff Roberson 	} else
4859727e637SJeff Roberson 		runq_remove(ts->ts_runq, td);
486155b9987SJeff Roberson }
487155b9987SJeff Roberson 
488ae7a6b38SJeff Roberson /*
489ae7a6b38SJeff Roberson  * Load is maintained for all threads RUNNING and ON_RUNQ.  Add the load
490ae7a6b38SJeff Roberson  * for this thread to the referenced thread queue.
491ae7a6b38SJeff Roberson  */
492a8949de2SJeff Roberson static void
4939727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td)
4945d7ef00cSJeff Roberson {
495ae7a6b38SJeff Roberson 
496ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
4979727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
49803d17db7SJeff Roberson 
499d2ad694cSJeff Roberson 	tdq->tdq_load++;
50003d17db7SJeff Roberson 	if ((td->td_proc->p_flag & P_NOLOAD) == 0)
501d2ad694cSJeff Roberson 		tdq->tdq_sysload++;
5028f51ad55SJeff Roberson 	KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load);
5035d7ef00cSJeff Roberson }
50415dc847eSJeff Roberson 
505ae7a6b38SJeff Roberson /*
506ae7a6b38SJeff Roberson  * Remove the load from a thread that is transitioning to a sleep state or
507ae7a6b38SJeff Roberson  * exiting.
508ae7a6b38SJeff Roberson  */
509a8949de2SJeff Roberson static void
5109727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td)
5115d7ef00cSJeff Roberson {
512ae7a6b38SJeff Roberson 
5139727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
514ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
515ae7a6b38SJeff Roberson 	KASSERT(tdq->tdq_load != 0,
516c47f202bSJeff Roberson 	    ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq)));
51703d17db7SJeff Roberson 
518d2ad694cSJeff Roberson 	tdq->tdq_load--;
51903d17db7SJeff Roberson 	if ((td->td_proc->p_flag & P_NOLOAD) == 0)
52003d17db7SJeff Roberson 		tdq->tdq_sysload--;
5218f51ad55SJeff Roberson 	KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load);
52215dc847eSJeff Roberson }
52315dc847eSJeff Roberson 
524356500a3SJeff Roberson /*
52562fa74d9SJeff Roberson  * Set lowpri to its exact value by searching the run-queue and
52662fa74d9SJeff Roberson  * evaluating curthread.  curthread may be passed as an optimization.
527356500a3SJeff Roberson  */
52822bf7d9aSJeff Roberson static void
52962fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd)
53062fa74d9SJeff Roberson {
53162fa74d9SJeff Roberson 	struct thread *td;
53262fa74d9SJeff Roberson 
53362fa74d9SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
53462fa74d9SJeff Roberson 	if (ctd == NULL)
53562fa74d9SJeff Roberson 		ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread;
5369727e637SJeff Roberson 	td = tdq_choose(tdq);
5379727e637SJeff Roberson 	if (td == NULL || td->td_priority > ctd->td_priority)
53862fa74d9SJeff Roberson 		tdq->tdq_lowpri = ctd->td_priority;
53962fa74d9SJeff Roberson 	else
54062fa74d9SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
54162fa74d9SJeff Roberson }
54262fa74d9SJeff Roberson 
54362fa74d9SJeff Roberson #ifdef SMP
54462fa74d9SJeff Roberson struct cpu_search {
54562fa74d9SJeff Roberson 	cpumask_t cs_mask;	/* Mask of valid cpus. */
54662fa74d9SJeff Roberson 	u_int	cs_load;
54762fa74d9SJeff Roberson 	u_int	cs_cpu;
54862fa74d9SJeff Roberson 	int	cs_limit;	/* Min priority for low min load for high. */
54962fa74d9SJeff Roberson };
55062fa74d9SJeff Roberson 
55162fa74d9SJeff Roberson #define	CPU_SEARCH_LOWEST	0x1
55262fa74d9SJeff Roberson #define	CPU_SEARCH_HIGHEST	0x2
55362fa74d9SJeff Roberson #define	CPU_SEARCH_BOTH		(CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST)
55462fa74d9SJeff Roberson 
55562fa74d9SJeff Roberson #define	CPUMASK_FOREACH(cpu, mask)				\
55662fa74d9SJeff Roberson 	for ((cpu) = 0; (cpu) < sizeof((mask)) * 8; (cpu)++)	\
55762fa74d9SJeff Roberson 		if ((mask) & 1 << (cpu))
55862fa74d9SJeff Roberson 
559d628fbfaSJohn Baldwin static __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low,
56062fa74d9SJeff Roberson     struct cpu_search *high, const int match);
56162fa74d9SJeff Roberson int cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low);
56262fa74d9SJeff Roberson int cpu_search_highest(struct cpu_group *cg, struct cpu_search *high);
56362fa74d9SJeff Roberson int cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
56462fa74d9SJeff Roberson     struct cpu_search *high);
56562fa74d9SJeff Roberson 
56662fa74d9SJeff Roberson /*
56762fa74d9SJeff Roberson  * This routine compares according to the match argument and should be
56862fa74d9SJeff Roberson  * reduced in actual instantiations via constant propagation and dead code
56962fa74d9SJeff Roberson  * elimination.
57062fa74d9SJeff Roberson  */
57162fa74d9SJeff Roberson static __inline int
57262fa74d9SJeff Roberson cpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high,
57362fa74d9SJeff Roberson     const int match)
57462fa74d9SJeff Roberson {
57562fa74d9SJeff Roberson 	struct tdq *tdq;
57662fa74d9SJeff Roberson 
57762fa74d9SJeff Roberson 	tdq = TDQ_CPU(cpu);
57862fa74d9SJeff Roberson 	if (match & CPU_SEARCH_LOWEST)
57962fa74d9SJeff Roberson 		if (low->cs_mask & (1 << cpu) &&
58062fa74d9SJeff Roberson 		    tdq->tdq_load < low->cs_load &&
58162fa74d9SJeff Roberson 		    tdq->tdq_lowpri > low->cs_limit) {
58262fa74d9SJeff Roberson 			low->cs_cpu = cpu;
58362fa74d9SJeff Roberson 			low->cs_load = tdq->tdq_load;
58462fa74d9SJeff Roberson 		}
58562fa74d9SJeff Roberson 	if (match & CPU_SEARCH_HIGHEST)
58662fa74d9SJeff Roberson 		if (high->cs_mask & (1 << cpu) &&
58762fa74d9SJeff Roberson 		    tdq->tdq_load >= high->cs_limit &&
58862fa74d9SJeff Roberson 		    tdq->tdq_load > high->cs_load &&
58962fa74d9SJeff Roberson 		    tdq->tdq_transferable) {
59062fa74d9SJeff Roberson 			high->cs_cpu = cpu;
59162fa74d9SJeff Roberson 			high->cs_load = tdq->tdq_load;
59262fa74d9SJeff Roberson 		}
59362fa74d9SJeff Roberson 	return (tdq->tdq_load);
59462fa74d9SJeff Roberson }
59562fa74d9SJeff Roberson 
59662fa74d9SJeff Roberson /*
59762fa74d9SJeff Roberson  * Search the tree of cpu_groups for the lowest or highest loaded cpu
59862fa74d9SJeff Roberson  * according to the match argument.  This routine actually compares the
59962fa74d9SJeff Roberson  * load on all paths through the tree and finds the least loaded cpu on
60062fa74d9SJeff Roberson  * the least loaded path, which may differ from the least loaded cpu in
60162fa74d9SJeff Roberson  * the system.  This balances work among caches and busses.
60262fa74d9SJeff Roberson  *
60362fa74d9SJeff Roberson  * This inline is instantiated in three forms below using constants for the
60462fa74d9SJeff Roberson  * match argument.  It is reduced to the minimum set for each case.  It is
60562fa74d9SJeff Roberson  * also recursive to the depth of the tree.
60662fa74d9SJeff Roberson  */
607d628fbfaSJohn Baldwin static __inline int
60862fa74d9SJeff Roberson cpu_search(struct cpu_group *cg, struct cpu_search *low,
60962fa74d9SJeff Roberson     struct cpu_search *high, const int match)
61062fa74d9SJeff Roberson {
61162fa74d9SJeff Roberson 	int total;
61262fa74d9SJeff Roberson 
61362fa74d9SJeff Roberson 	total = 0;
61462fa74d9SJeff Roberson 	if (cg->cg_children) {
61562fa74d9SJeff Roberson 		struct cpu_search lgroup;
61662fa74d9SJeff Roberson 		struct cpu_search hgroup;
61762fa74d9SJeff Roberson 		struct cpu_group *child;
61862fa74d9SJeff Roberson 		u_int lload;
61962fa74d9SJeff Roberson 		int hload;
62062fa74d9SJeff Roberson 		int load;
62162fa74d9SJeff Roberson 		int i;
62262fa74d9SJeff Roberson 
62362fa74d9SJeff Roberson 		lload = -1;
62462fa74d9SJeff Roberson 		hload = -1;
62562fa74d9SJeff Roberson 		for (i = 0; i < cg->cg_children; i++) {
62662fa74d9SJeff Roberson 			child = &cg->cg_child[i];
62762fa74d9SJeff Roberson 			if (match & CPU_SEARCH_LOWEST) {
62862fa74d9SJeff Roberson 				lgroup = *low;
62962fa74d9SJeff Roberson 				lgroup.cs_load = -1;
63062fa74d9SJeff Roberson 			}
63162fa74d9SJeff Roberson 			if (match & CPU_SEARCH_HIGHEST) {
63262fa74d9SJeff Roberson 				hgroup = *high;
63362fa74d9SJeff Roberson 				lgroup.cs_load = 0;
63462fa74d9SJeff Roberson 			}
63562fa74d9SJeff Roberson 			switch (match) {
63662fa74d9SJeff Roberson 			case CPU_SEARCH_LOWEST:
63762fa74d9SJeff Roberson 				load = cpu_search_lowest(child, &lgroup);
63862fa74d9SJeff Roberson 				break;
63962fa74d9SJeff Roberson 			case CPU_SEARCH_HIGHEST:
64062fa74d9SJeff Roberson 				load = cpu_search_highest(child, &hgroup);
64162fa74d9SJeff Roberson 				break;
64262fa74d9SJeff Roberson 			case CPU_SEARCH_BOTH:
64362fa74d9SJeff Roberson 				load = cpu_search_both(child, &lgroup, &hgroup);
64462fa74d9SJeff Roberson 				break;
64562fa74d9SJeff Roberson 			}
64662fa74d9SJeff Roberson 			total += load;
64762fa74d9SJeff Roberson 			if (match & CPU_SEARCH_LOWEST)
64862fa74d9SJeff Roberson 				if (load < lload || low->cs_cpu == -1) {
64962fa74d9SJeff Roberson 					*low = lgroup;
65062fa74d9SJeff Roberson 					lload = load;
65162fa74d9SJeff Roberson 				}
65262fa74d9SJeff Roberson 			if (match & CPU_SEARCH_HIGHEST)
65362fa74d9SJeff Roberson 				if (load > hload || high->cs_cpu == -1) {
65462fa74d9SJeff Roberson 					hload = load;
65562fa74d9SJeff Roberson 					*high = hgroup;
65662fa74d9SJeff Roberson 				}
65762fa74d9SJeff Roberson 		}
65862fa74d9SJeff Roberson 	} else {
65962fa74d9SJeff Roberson 		int cpu;
66062fa74d9SJeff Roberson 
66162fa74d9SJeff Roberson 		CPUMASK_FOREACH(cpu, cg->cg_mask)
66262fa74d9SJeff Roberson 			total += cpu_compare(cpu, low, high, match);
66362fa74d9SJeff Roberson 	}
66462fa74d9SJeff Roberson 	return (total);
66562fa74d9SJeff Roberson }
66662fa74d9SJeff Roberson 
66762fa74d9SJeff Roberson /*
66862fa74d9SJeff Roberson  * cpu_search instantiations must pass constants to maintain the inline
66962fa74d9SJeff Roberson  * optimization.
67062fa74d9SJeff Roberson  */
67162fa74d9SJeff Roberson int
67262fa74d9SJeff Roberson cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low)
67362fa74d9SJeff Roberson {
67462fa74d9SJeff Roberson 	return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST);
67562fa74d9SJeff Roberson }
67662fa74d9SJeff Roberson 
67762fa74d9SJeff Roberson int
67862fa74d9SJeff Roberson cpu_search_highest(struct cpu_group *cg, struct cpu_search *high)
67962fa74d9SJeff Roberson {
68062fa74d9SJeff Roberson 	return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST);
68162fa74d9SJeff Roberson }
68262fa74d9SJeff Roberson 
68362fa74d9SJeff Roberson int
68462fa74d9SJeff Roberson cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
68562fa74d9SJeff Roberson     struct cpu_search *high)
68662fa74d9SJeff Roberson {
68762fa74d9SJeff Roberson 	return cpu_search(cg, low, high, CPU_SEARCH_BOTH);
68862fa74d9SJeff Roberson }
68962fa74d9SJeff Roberson 
69062fa74d9SJeff Roberson /*
69162fa74d9SJeff Roberson  * Find the cpu with the least load via the least loaded path that has a
69262fa74d9SJeff Roberson  * lowpri greater than pri  pri.  A pri of -1 indicates any priority is
69362fa74d9SJeff Roberson  * acceptable.
69462fa74d9SJeff Roberson  */
69562fa74d9SJeff Roberson static inline int
69662fa74d9SJeff Roberson sched_lowest(struct cpu_group *cg, cpumask_t mask, int pri)
69762fa74d9SJeff Roberson {
69862fa74d9SJeff Roberson 	struct cpu_search low;
69962fa74d9SJeff Roberson 
70062fa74d9SJeff Roberson 	low.cs_cpu = -1;
70162fa74d9SJeff Roberson 	low.cs_load = -1;
70262fa74d9SJeff Roberson 	low.cs_mask = mask;
70362fa74d9SJeff Roberson 	low.cs_limit = pri;
70462fa74d9SJeff Roberson 	cpu_search_lowest(cg, &low);
70562fa74d9SJeff Roberson 	return low.cs_cpu;
70662fa74d9SJeff Roberson }
70762fa74d9SJeff Roberson 
70862fa74d9SJeff Roberson /*
70962fa74d9SJeff Roberson  * Find the cpu with the highest load via the highest loaded path.
71062fa74d9SJeff Roberson  */
71162fa74d9SJeff Roberson static inline int
71262fa74d9SJeff Roberson sched_highest(struct cpu_group *cg, cpumask_t mask, int minload)
71362fa74d9SJeff Roberson {
71462fa74d9SJeff Roberson 	struct cpu_search high;
71562fa74d9SJeff Roberson 
71662fa74d9SJeff Roberson 	high.cs_cpu = -1;
71762fa74d9SJeff Roberson 	high.cs_load = 0;
71862fa74d9SJeff Roberson 	high.cs_mask = mask;
71962fa74d9SJeff Roberson 	high.cs_limit = minload;
72062fa74d9SJeff Roberson 	cpu_search_highest(cg, &high);
72162fa74d9SJeff Roberson 	return high.cs_cpu;
72262fa74d9SJeff Roberson }
72362fa74d9SJeff Roberson 
72462fa74d9SJeff Roberson /*
72562fa74d9SJeff Roberson  * Simultaneously find the highest and lowest loaded cpu reachable via
72662fa74d9SJeff Roberson  * cg.
72762fa74d9SJeff Roberson  */
72862fa74d9SJeff Roberson static inline void
72962fa74d9SJeff Roberson sched_both(struct cpu_group *cg, cpumask_t mask, int *lowcpu, int *highcpu)
73062fa74d9SJeff Roberson {
73162fa74d9SJeff Roberson 	struct cpu_search high;
73262fa74d9SJeff Roberson 	struct cpu_search low;
73362fa74d9SJeff Roberson 
73462fa74d9SJeff Roberson 	low.cs_cpu = -1;
73562fa74d9SJeff Roberson 	low.cs_limit = -1;
73662fa74d9SJeff Roberson 	low.cs_load = -1;
73762fa74d9SJeff Roberson 	low.cs_mask = mask;
73862fa74d9SJeff Roberson 	high.cs_load = 0;
73962fa74d9SJeff Roberson 	high.cs_cpu = -1;
74062fa74d9SJeff Roberson 	high.cs_limit = -1;
74162fa74d9SJeff Roberson 	high.cs_mask = mask;
74262fa74d9SJeff Roberson 	cpu_search_both(cg, &low, &high);
74362fa74d9SJeff Roberson 	*lowcpu = low.cs_cpu;
74462fa74d9SJeff Roberson 	*highcpu = high.cs_cpu;
74562fa74d9SJeff Roberson 	return;
74662fa74d9SJeff Roberson }
74762fa74d9SJeff Roberson 
74862fa74d9SJeff Roberson static void
74962fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg)
75062fa74d9SJeff Roberson {
75162fa74d9SJeff Roberson 	cpumask_t mask;
75262fa74d9SJeff Roberson 	int high;
75362fa74d9SJeff Roberson 	int low;
75462fa74d9SJeff Roberson 	int i;
75562fa74d9SJeff Roberson 
75662fa74d9SJeff Roberson 	mask = -1;
75762fa74d9SJeff Roberson 	for (;;) {
75862fa74d9SJeff Roberson 		sched_both(cg, mask, &low, &high);
75962fa74d9SJeff Roberson 		if (low == high || low == -1 || high == -1)
76062fa74d9SJeff Roberson 			break;
76162fa74d9SJeff Roberson 		if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low)))
76262fa74d9SJeff Roberson 			break;
76362fa74d9SJeff Roberson 		/*
76462fa74d9SJeff Roberson 		 * If we failed to move any threads determine which cpu
76562fa74d9SJeff Roberson 		 * to kick out of the set and try again.
76662fa74d9SJeff Roberson 	 	 */
76762fa74d9SJeff Roberson 		if (TDQ_CPU(high)->tdq_transferable == 0)
76862fa74d9SJeff Roberson 			mask &= ~(1 << high);
76962fa74d9SJeff Roberson 		else
77062fa74d9SJeff Roberson 			mask &= ~(1 << low);
77162fa74d9SJeff Roberson 	}
77262fa74d9SJeff Roberson 
77362fa74d9SJeff Roberson 	for (i = 0; i < cg->cg_children; i++)
77462fa74d9SJeff Roberson 		sched_balance_group(&cg->cg_child[i]);
77562fa74d9SJeff Roberson }
77662fa74d9SJeff Roberson 
77762fa74d9SJeff Roberson static void
7787fcf154aSJeff Roberson sched_balance()
779356500a3SJeff Roberson {
7807fcf154aSJeff Roberson 	struct tdq *tdq;
781356500a3SJeff Roberson 
7827fcf154aSJeff Roberson 	/*
7837fcf154aSJeff Roberson 	 * Select a random time between .5 * balance_interval and
7847fcf154aSJeff Roberson 	 * 1.5 * balance_interval.
7857fcf154aSJeff Roberson 	 */
7867fcf154aSJeff Roberson 	balance_ticks = max(balance_interval / 2, 1);
7877fcf154aSJeff Roberson 	balance_ticks += random() % balance_interval;
788ae7a6b38SJeff Roberson 	if (smp_started == 0 || rebalance == 0)
789598b368dSJeff Roberson 		return;
7907fcf154aSJeff Roberson 	tdq = TDQ_SELF();
7917fcf154aSJeff Roberson 	TDQ_UNLOCK(tdq);
79262fa74d9SJeff Roberson 	sched_balance_group(cpu_top);
7937fcf154aSJeff Roberson 	TDQ_LOCK(tdq);
794cac77d04SJeff Roberson }
79586f8ae96SJeff Roberson 
796ae7a6b38SJeff Roberson /*
797ae7a6b38SJeff Roberson  * Lock two thread queues using their address to maintain lock order.
798ae7a6b38SJeff Roberson  */
799ae7a6b38SJeff Roberson static void
800ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two)
801ae7a6b38SJeff Roberson {
802ae7a6b38SJeff Roberson 	if (one < two) {
803ae7a6b38SJeff Roberson 		TDQ_LOCK(one);
804ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(two, MTX_DUPOK);
805ae7a6b38SJeff Roberson 	} else {
806ae7a6b38SJeff Roberson 		TDQ_LOCK(two);
807ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(one, MTX_DUPOK);
808ae7a6b38SJeff Roberson 	}
809ae7a6b38SJeff Roberson }
810ae7a6b38SJeff Roberson 
811ae7a6b38SJeff Roberson /*
8127fcf154aSJeff Roberson  * Unlock two thread queues.  Order is not important here.
8137fcf154aSJeff Roberson  */
8147fcf154aSJeff Roberson static void
8157fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two)
8167fcf154aSJeff Roberson {
8177fcf154aSJeff Roberson 	TDQ_UNLOCK(one);
8187fcf154aSJeff Roberson 	TDQ_UNLOCK(two);
8197fcf154aSJeff Roberson }
8207fcf154aSJeff Roberson 
8217fcf154aSJeff Roberson /*
822ae7a6b38SJeff Roberson  * Transfer load between two imbalanced thread queues.
823ae7a6b38SJeff Roberson  */
82462fa74d9SJeff Roberson static int
825ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low)
826cac77d04SJeff Roberson {
827cac77d04SJeff Roberson 	int transferable;
828cac77d04SJeff Roberson 	int high_load;
829cac77d04SJeff Roberson 	int low_load;
83062fa74d9SJeff Roberson 	int moved;
831cac77d04SJeff Roberson 	int move;
832cac77d04SJeff Roberson 	int diff;
833cac77d04SJeff Roberson 	int i;
834cac77d04SJeff Roberson 
835ae7a6b38SJeff Roberson 	tdq_lock_pair(high, low);
836d2ad694cSJeff Roberson 	transferable = high->tdq_transferable;
837d2ad694cSJeff Roberson 	high_load = high->tdq_load;
838d2ad694cSJeff Roberson 	low_load = low->tdq_load;
83962fa74d9SJeff Roberson 	moved = 0;
840155b9987SJeff Roberson 	/*
841155b9987SJeff Roberson 	 * Determine what the imbalance is and then adjust that to how many
842d2ad694cSJeff Roberson 	 * threads we actually have to give up (transferable).
843155b9987SJeff Roberson 	 */
844ae7a6b38SJeff Roberson 	if (transferable != 0) {
845cac77d04SJeff Roberson 		diff = high_load - low_load;
846356500a3SJeff Roberson 		move = diff / 2;
847356500a3SJeff Roberson 		if (diff & 0x1)
848356500a3SJeff Roberson 			move++;
84980f86c9fSJeff Roberson 		move = min(move, transferable);
850356500a3SJeff Roberson 		for (i = 0; i < move; i++)
85162fa74d9SJeff Roberson 			moved += tdq_move(high, low);
852a5423ea3SJeff Roberson 		/*
853a5423ea3SJeff Roberson 		 * IPI the target cpu to force it to reschedule with the new
854a5423ea3SJeff Roberson 		 * workload.
855a5423ea3SJeff Roberson 		 */
856a5423ea3SJeff Roberson 		ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT);
857ae7a6b38SJeff Roberson 	}
8587fcf154aSJeff Roberson 	tdq_unlock_pair(high, low);
85962fa74d9SJeff Roberson 	return (moved);
860356500a3SJeff Roberson }
861356500a3SJeff Roberson 
862ae7a6b38SJeff Roberson /*
863ae7a6b38SJeff Roberson  * Move a thread from one thread queue to another.
864ae7a6b38SJeff Roberson  */
86562fa74d9SJeff Roberson static int
866ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to)
867356500a3SJeff Roberson {
868ad1e7d28SJulian Elischer 	struct td_sched *ts;
869ae7a6b38SJeff Roberson 	struct thread *td;
870ae7a6b38SJeff Roberson 	struct tdq *tdq;
871ae7a6b38SJeff Roberson 	int cpu;
872356500a3SJeff Roberson 
8737fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(from, MA_OWNED);
8747fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(to, MA_OWNED);
8757fcf154aSJeff Roberson 
876ad1e7d28SJulian Elischer 	tdq = from;
877ae7a6b38SJeff Roberson 	cpu = TDQ_ID(to);
8789727e637SJeff Roberson 	td = tdq_steal(tdq, cpu);
8799727e637SJeff Roberson 	if (td == NULL)
88062fa74d9SJeff Roberson 		return (0);
8819727e637SJeff Roberson 	ts = td->td_sched;
882ae7a6b38SJeff Roberson 	/*
883ae7a6b38SJeff Roberson 	 * Although the run queue is locked the thread may be blocked.  Lock
8847fcf154aSJeff Roberson 	 * it to clear this and acquire the run-queue lock.
885ae7a6b38SJeff Roberson 	 */
886ae7a6b38SJeff Roberson 	thread_lock(td);
8877fcf154aSJeff Roberson 	/* Drop recursive lock on from acquired via thread_lock(). */
888ae7a6b38SJeff Roberson 	TDQ_UNLOCK(from);
889ae7a6b38SJeff Roberson 	sched_rem(td);
8907b8bfa0dSJeff Roberson 	ts->ts_cpu = cpu;
891ae7a6b38SJeff Roberson 	td->td_lock = TDQ_LOCKPTR(to);
892ae7a6b38SJeff Roberson 	tdq_add(to, td, SRQ_YIELDING);
89362fa74d9SJeff Roberson 	return (1);
894356500a3SJeff Roberson }
89522bf7d9aSJeff Roberson 
896ae7a6b38SJeff Roberson /*
897ae7a6b38SJeff Roberson  * This tdq has idled.  Try to steal a thread from another cpu and switch
898ae7a6b38SJeff Roberson  * to it.
899ae7a6b38SJeff Roberson  */
90080f86c9fSJeff Roberson static int
901ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq)
90222bf7d9aSJeff Roberson {
90362fa74d9SJeff Roberson 	struct cpu_group *cg;
904ad1e7d28SJulian Elischer 	struct tdq *steal;
90562fa74d9SJeff Roberson 	cpumask_t mask;
90662fa74d9SJeff Roberson 	int thresh;
907ae7a6b38SJeff Roberson 	int cpu;
90880f86c9fSJeff Roberson 
90988f530ccSJeff Roberson 	if (smp_started == 0 || steal_idle == 0)
91088f530ccSJeff Roberson 		return (1);
91162fa74d9SJeff Roberson 	mask = -1;
91262fa74d9SJeff Roberson 	mask &= ~PCPU_GET(cpumask);
91362fa74d9SJeff Roberson 	/* We don't want to be preempted while we're iterating. */
914ae7a6b38SJeff Roberson 	spinlock_enter();
91562fa74d9SJeff Roberson 	for (cg = tdq->tdq_cg; cg != NULL; ) {
91662fa74d9SJeff Roberson 		if ((cg->cg_flags & (CG_FLAG_HTT | CG_FLAG_THREAD)) == 0)
91762fa74d9SJeff Roberson 			thresh = steal_thresh;
91862fa74d9SJeff Roberson 		else
91962fa74d9SJeff Roberson 			thresh = 1;
92062fa74d9SJeff Roberson 		cpu = sched_highest(cg, mask, thresh);
92162fa74d9SJeff Roberson 		if (cpu == -1) {
92262fa74d9SJeff Roberson 			cg = cg->cg_parent;
92380f86c9fSJeff Roberson 			continue;
9247b8bfa0dSJeff Roberson 		}
9257b8bfa0dSJeff Roberson 		steal = TDQ_CPU(cpu);
92662fa74d9SJeff Roberson 		mask &= ~(1 << cpu);
9277fcf154aSJeff Roberson 		tdq_lock_pair(tdq, steal);
92862fa74d9SJeff Roberson 		if (steal->tdq_load < thresh || steal->tdq_transferable == 0) {
9297fcf154aSJeff Roberson 			tdq_unlock_pair(tdq, steal);
93062fa74d9SJeff Roberson 			continue;
93162fa74d9SJeff Roberson 		}
93262fa74d9SJeff Roberson 		/*
93362fa74d9SJeff Roberson 		 * If a thread was added while interrupts were disabled don't
93462fa74d9SJeff Roberson 		 * steal one here.  If we fail to acquire one due to affinity
93562fa74d9SJeff Roberson 		 * restrictions loop again with this cpu removed from the
93662fa74d9SJeff Roberson 		 * set.
93762fa74d9SJeff Roberson 		 */
93862fa74d9SJeff Roberson 		if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) {
93962fa74d9SJeff Roberson 			tdq_unlock_pair(tdq, steal);
94062fa74d9SJeff Roberson 			continue;
94180f86c9fSJeff Roberson 		}
942ae7a6b38SJeff Roberson 		spinlock_exit();
943ae7a6b38SJeff Roberson 		TDQ_UNLOCK(steal);
9448df78c41SJeff Roberson 		mi_switch(SW_VOL | SWT_IDLE, NULL);
945ae7a6b38SJeff Roberson 		thread_unlock(curthread);
9467b8bfa0dSJeff Roberson 
9477b8bfa0dSJeff Roberson 		return (0);
94822bf7d9aSJeff Roberson 	}
94962fa74d9SJeff Roberson 	spinlock_exit();
95062fa74d9SJeff Roberson 	return (1);
95162fa74d9SJeff Roberson }
95222bf7d9aSJeff Roberson 
953ae7a6b38SJeff Roberson /*
954ae7a6b38SJeff Roberson  * Notify a remote cpu of new work.  Sends an IPI if criteria are met.
955ae7a6b38SJeff Roberson  */
95622bf7d9aSJeff Roberson static void
9579727e637SJeff Roberson tdq_notify(struct tdq *tdq, struct thread *td)
95822bf7d9aSJeff Roberson {
95902f0ff6dSJohn Baldwin 	struct thread *ctd;
960fc3a97dcSJeff Roberson 	int pri;
9617b8bfa0dSJeff Roberson 	int cpu;
96222bf7d9aSJeff Roberson 
963ff256d9cSJeff Roberson 	if (tdq->tdq_ipipending)
964ff256d9cSJeff Roberson 		return;
9659727e637SJeff Roberson 	cpu = td->td_sched->ts_cpu;
9669727e637SJeff Roberson 	pri = td->td_priority;
96702f0ff6dSJohn Baldwin 	ctd = pcpu_find(cpu)->pc_curthread;
96802f0ff6dSJohn Baldwin 	if (!sched_shouldpreempt(pri, ctd->td_priority, 1))
9696b2f763fSJeff Roberson 		return;
97002f0ff6dSJohn Baldwin 	if (TD_IS_IDLETHREAD(ctd)) {
9711690c6c1SJeff Roberson 		/*
9721690c6c1SJeff Roberson 		 * If the idle thread is still 'running' it's probably
9731690c6c1SJeff Roberson 		 * waiting on us to release the tdq spinlock already.  No
9741690c6c1SJeff Roberson 		 * need to ipi.
9751690c6c1SJeff Roberson 		 */
9761690c6c1SJeff Roberson 		if (tdq->tdq_idlestate == TDQ_RUNNING)
9771690c6c1SJeff Roberson 			return;
9786c47aaaeSJeff Roberson 		/*
9796c47aaaeSJeff Roberson 		 * If the MD code has an idle wakeup routine try that before
9806c47aaaeSJeff Roberson 		 * falling back to IPI.
9816c47aaaeSJeff Roberson 		 */
9826c47aaaeSJeff Roberson 		if (cpu_idle_wakeup(cpu))
9836c47aaaeSJeff Roberson 			return;
9841690c6c1SJeff Roberson 	}
985ff256d9cSJeff Roberson 	tdq->tdq_ipipending = 1;
98614618990SJeff Roberson 	ipi_selected(1 << cpu, IPI_PREEMPT);
98722bf7d9aSJeff Roberson }
98822bf7d9aSJeff Roberson 
989ae7a6b38SJeff Roberson /*
990ae7a6b38SJeff Roberson  * Steals load from a timeshare queue.  Honors the rotating queue head
991ae7a6b38SJeff Roberson  * index.
992ae7a6b38SJeff Roberson  */
9939727e637SJeff Roberson static struct thread *
99462fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start)
995ae7a6b38SJeff Roberson {
996ae7a6b38SJeff Roberson 	struct rqbits *rqb;
997ae7a6b38SJeff Roberson 	struct rqhead *rqh;
9989727e637SJeff Roberson 	struct thread *td;
999ae7a6b38SJeff Roberson 	int first;
1000ae7a6b38SJeff Roberson 	int bit;
1001ae7a6b38SJeff Roberson 	int pri;
1002ae7a6b38SJeff Roberson 	int i;
1003ae7a6b38SJeff Roberson 
1004ae7a6b38SJeff Roberson 	rqb = &rq->rq_status;
1005ae7a6b38SJeff Roberson 	bit = start & (RQB_BPW -1);
1006ae7a6b38SJeff Roberson 	pri = 0;
1007ae7a6b38SJeff Roberson 	first = 0;
1008ae7a6b38SJeff Roberson again:
1009ae7a6b38SJeff Roberson 	for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
1010ae7a6b38SJeff Roberson 		if (rqb->rqb_bits[i] == 0)
1011ae7a6b38SJeff Roberson 			continue;
1012ae7a6b38SJeff Roberson 		if (bit != 0) {
1013ae7a6b38SJeff Roberson 			for (pri = bit; pri < RQB_BPW; pri++)
1014ae7a6b38SJeff Roberson 				if (rqb->rqb_bits[i] & (1ul << pri))
1015ae7a6b38SJeff Roberson 					break;
1016ae7a6b38SJeff Roberson 			if (pri >= RQB_BPW)
1017ae7a6b38SJeff Roberson 				continue;
1018ae7a6b38SJeff Roberson 		} else
1019ae7a6b38SJeff Roberson 			pri = RQB_FFS(rqb->rqb_bits[i]);
1020ae7a6b38SJeff Roberson 		pri += (i << RQB_L2BPW);
1021ae7a6b38SJeff Roberson 		rqh = &rq->rq_queues[pri];
10229727e637SJeff Roberson 		TAILQ_FOREACH(td, rqh, td_runq) {
10239727e637SJeff Roberson 			if (first && THREAD_CAN_MIGRATE(td) &&
10249727e637SJeff Roberson 			    THREAD_CAN_SCHED(td, cpu))
10259727e637SJeff Roberson 				return (td);
1026ae7a6b38SJeff Roberson 			first = 1;
1027ae7a6b38SJeff Roberson 		}
1028ae7a6b38SJeff Roberson 	}
1029ae7a6b38SJeff Roberson 	if (start != 0) {
1030ae7a6b38SJeff Roberson 		start = 0;
1031ae7a6b38SJeff Roberson 		goto again;
1032ae7a6b38SJeff Roberson 	}
1033ae7a6b38SJeff Roberson 
1034ae7a6b38SJeff Roberson 	return (NULL);
1035ae7a6b38SJeff Roberson }
1036ae7a6b38SJeff Roberson 
1037ae7a6b38SJeff Roberson /*
1038ae7a6b38SJeff Roberson  * Steals load from a standard linear queue.
1039ae7a6b38SJeff Roberson  */
10409727e637SJeff Roberson static struct thread *
104162fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu)
104222bf7d9aSJeff Roberson {
104322bf7d9aSJeff Roberson 	struct rqhead *rqh;
104422bf7d9aSJeff Roberson 	struct rqbits *rqb;
10459727e637SJeff Roberson 	struct thread *td;
104622bf7d9aSJeff Roberson 	int word;
104722bf7d9aSJeff Roberson 	int bit;
104822bf7d9aSJeff Roberson 
104922bf7d9aSJeff Roberson 	rqb = &rq->rq_status;
105022bf7d9aSJeff Roberson 	for (word = 0; word < RQB_LEN; word++) {
105122bf7d9aSJeff Roberson 		if (rqb->rqb_bits[word] == 0)
105222bf7d9aSJeff Roberson 			continue;
105322bf7d9aSJeff Roberson 		for (bit = 0; bit < RQB_BPW; bit++) {
1054a2640c9bSPeter Wemm 			if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
105522bf7d9aSJeff Roberson 				continue;
105622bf7d9aSJeff Roberson 			rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
10579727e637SJeff Roberson 			TAILQ_FOREACH(td, rqh, td_runq)
10589727e637SJeff Roberson 				if (THREAD_CAN_MIGRATE(td) &&
10599727e637SJeff Roberson 				    THREAD_CAN_SCHED(td, cpu))
10609727e637SJeff Roberson 					return (td);
106122bf7d9aSJeff Roberson 		}
106222bf7d9aSJeff Roberson 	}
106322bf7d9aSJeff Roberson 	return (NULL);
106422bf7d9aSJeff Roberson }
106522bf7d9aSJeff Roberson 
1066ae7a6b38SJeff Roberson /*
1067ae7a6b38SJeff Roberson  * Attempt to steal a thread in priority order from a thread queue.
1068ae7a6b38SJeff Roberson  */
10699727e637SJeff Roberson static struct thread *
107062fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu)
107122bf7d9aSJeff Roberson {
10729727e637SJeff Roberson 	struct thread *td;
107322bf7d9aSJeff Roberson 
1074ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
10759727e637SJeff Roberson 	if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL)
10769727e637SJeff Roberson 		return (td);
10779727e637SJeff Roberson 	if ((td = runq_steal_from(&tdq->tdq_timeshare,
10789727e637SJeff Roberson 	    cpu, tdq->tdq_ridx)) != NULL)
10799727e637SJeff Roberson 		return (td);
108062fa74d9SJeff Roberson 	return (runq_steal(&tdq->tdq_idle, cpu));
108122bf7d9aSJeff Roberson }
108280f86c9fSJeff Roberson 
1083ae7a6b38SJeff Roberson /*
1084ae7a6b38SJeff Roberson  * Sets the thread lock and ts_cpu to match the requested cpu.  Unlocks the
10857fcf154aSJeff Roberson  * current lock and returns with the assigned queue locked.
1086ae7a6b38SJeff Roberson  */
1087ae7a6b38SJeff Roberson static inline struct tdq *
10889727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags)
108980f86c9fSJeff Roberson {
10909727e637SJeff Roberson 
1091ae7a6b38SJeff Roberson 	struct tdq *tdq;
109280f86c9fSJeff Roberson 
10939727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1094ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpu);
10959727e637SJeff Roberson 	td->td_sched->ts_cpu = cpu;
10969727e637SJeff Roberson 	/*
10979727e637SJeff Roberson 	 * If the lock matches just return the queue.
10989727e637SJeff Roberson 	 */
1099ae7a6b38SJeff Roberson 	if (td->td_lock == TDQ_LOCKPTR(tdq))
1100ae7a6b38SJeff Roberson 		return (tdq);
1101ae7a6b38SJeff Roberson #ifdef notyet
110280f86c9fSJeff Roberson 	/*
1103a5423ea3SJeff Roberson 	 * If the thread isn't running its lockptr is a
1104ae7a6b38SJeff Roberson 	 * turnstile or a sleepqueue.  We can just lock_set without
1105ae7a6b38SJeff Roberson 	 * blocking.
1106670c524fSJeff Roberson 	 */
1107ae7a6b38SJeff Roberson 	if (TD_CAN_RUN(td)) {
1108ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1109ae7a6b38SJeff Roberson 		thread_lock_set(td, TDQ_LOCKPTR(tdq));
1110ae7a6b38SJeff Roberson 		return (tdq);
1111ae7a6b38SJeff Roberson 	}
1112ae7a6b38SJeff Roberson #endif
111380f86c9fSJeff Roberson 	/*
1114ae7a6b38SJeff Roberson 	 * The hard case, migration, we need to block the thread first to
1115ae7a6b38SJeff Roberson 	 * prevent order reversals with other cpus locks.
11167b8bfa0dSJeff Roberson 	 */
1117ae7a6b38SJeff Roberson 	thread_lock_block(td);
1118ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1119ae7a6b38SJeff Roberson 	thread_lock_unblock(td, TDQ_LOCKPTR(tdq));
1120ae7a6b38SJeff Roberson 	return (tdq);
112180f86c9fSJeff Roberson }
11222454aaf5SJeff Roberson 
11238df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding");
11248df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity");
11258df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity");
11268df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load");
11278df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu");
11288df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration");
11298df78c41SJeff Roberson 
1130ae7a6b38SJeff Roberson static int
11319727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags)
1132ae7a6b38SJeff Roberson {
113362fa74d9SJeff Roberson 	struct cpu_group *cg;
11349727e637SJeff Roberson 	struct td_sched *ts;
1135ae7a6b38SJeff Roberson 	struct tdq *tdq;
113662fa74d9SJeff Roberson 	cpumask_t mask;
11377b8bfa0dSJeff Roberson 	int self;
11387b8bfa0dSJeff Roberson 	int pri;
11397b8bfa0dSJeff Roberson 	int cpu;
11407b8bfa0dSJeff Roberson 
114162fa74d9SJeff Roberson 	self = PCPU_GET(cpuid);
11429727e637SJeff Roberson 	ts = td->td_sched;
11437b8bfa0dSJeff Roberson 	if (smp_started == 0)
11447b8bfa0dSJeff Roberson 		return (self);
114528994a58SJeff Roberson 	/*
114628994a58SJeff Roberson 	 * Don't migrate a running thread from sched_switch().
114728994a58SJeff Roberson 	 */
114862fa74d9SJeff Roberson 	if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td))
114962fa74d9SJeff Roberson 		return (ts->ts_cpu);
11507b8bfa0dSJeff Roberson 	/*
115162fa74d9SJeff Roberson 	 * Prefer to run interrupt threads on the processors that generate
115262fa74d9SJeff Roberson 	 * the interrupt.
11537b8bfa0dSJeff Roberson 	 */
115462fa74d9SJeff Roberson 	if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) &&
11558df78c41SJeff Roberson 	    curthread->td_intr_nesting_level && ts->ts_cpu != self) {
11568df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_intrbind);
115762fa74d9SJeff Roberson 		ts->ts_cpu = self;
11588df78c41SJeff Roberson 	}
115962fa74d9SJeff Roberson 	/*
116062fa74d9SJeff Roberson 	 * If the thread can run on the last cpu and the affinity has not
116162fa74d9SJeff Roberson 	 * expired or it is idle run it there.
116262fa74d9SJeff Roberson 	 */
116362fa74d9SJeff Roberson 	pri = td->td_priority;
116462fa74d9SJeff Roberson 	tdq = TDQ_CPU(ts->ts_cpu);
116562fa74d9SJeff Roberson 	if (THREAD_CAN_SCHED(td, ts->ts_cpu)) {
11668df78c41SJeff Roberson 		if (tdq->tdq_lowpri > PRI_MIN_IDLE) {
11678df78c41SJeff Roberson 			SCHED_STAT_INC(pickcpu_idle_affinity);
116862fa74d9SJeff Roberson 			return (ts->ts_cpu);
11698df78c41SJeff Roberson 		}
11708df78c41SJeff Roberson 		if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri) {
11718df78c41SJeff Roberson 			SCHED_STAT_INC(pickcpu_affinity);
11727b8bfa0dSJeff Roberson 			return (ts->ts_cpu);
11737b8bfa0dSJeff Roberson 		}
11748df78c41SJeff Roberson 	}
11757b8bfa0dSJeff Roberson 	/*
117662fa74d9SJeff Roberson 	 * Search for the highest level in the tree that still has affinity.
11777b8bfa0dSJeff Roberson 	 */
117862fa74d9SJeff Roberson 	cg = NULL;
117962fa74d9SJeff Roberson 	for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent)
118062fa74d9SJeff Roberson 		if (SCHED_AFFINITY(ts, cg->cg_level))
118162fa74d9SJeff Roberson 			break;
118262fa74d9SJeff Roberson 	cpu = -1;
118362fa74d9SJeff Roberson 	mask = td->td_cpuset->cs_mask.__bits[0];
118462fa74d9SJeff Roberson 	if (cg)
118562fa74d9SJeff Roberson 		cpu = sched_lowest(cg, mask, pri);
118662fa74d9SJeff Roberson 	if (cpu == -1)
118762fa74d9SJeff Roberson 		cpu = sched_lowest(cpu_top, mask, -1);
118862fa74d9SJeff Roberson 	/*
118962fa74d9SJeff Roberson 	 * Compare the lowest loaded cpu to current cpu.
119062fa74d9SJeff Roberson 	 */
1191ff256d9cSJeff Roberson 	if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri &&
11928df78c41SJeff Roberson 	    TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) {
11938df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_local);
119462fa74d9SJeff Roberson 		cpu = self;
11958df78c41SJeff Roberson 	} else
11968df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_lowest);
11978df78c41SJeff Roberson 	if (cpu != ts->ts_cpu)
11988df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_migration);
1199ff256d9cSJeff Roberson 	KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu."));
1200ae7a6b38SJeff Roberson 	return (cpu);
120180f86c9fSJeff Roberson }
120262fa74d9SJeff Roberson #endif
120322bf7d9aSJeff Roberson 
120422bf7d9aSJeff Roberson /*
120522bf7d9aSJeff Roberson  * Pick the highest priority task we have and return it.
12060c0a98b2SJeff Roberson  */
12079727e637SJeff Roberson static struct thread *
1208ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq)
12095d7ef00cSJeff Roberson {
12109727e637SJeff Roberson 	struct thread *td;
12115d7ef00cSJeff Roberson 
1212ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
12139727e637SJeff Roberson 	td = runq_choose(&tdq->tdq_realtime);
12149727e637SJeff Roberson 	if (td != NULL)
12159727e637SJeff Roberson 		return (td);
12169727e637SJeff Roberson 	td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
12179727e637SJeff Roberson 	if (td != NULL) {
12189727e637SJeff Roberson 		KASSERT(td->td_priority >= PRI_MIN_TIMESHARE,
1219e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on timeshare queue %d",
12209727e637SJeff Roberson 		    td->td_priority));
12219727e637SJeff Roberson 		return (td);
122215dc847eSJeff Roberson 	}
12239727e637SJeff Roberson 	td = runq_choose(&tdq->tdq_idle);
12249727e637SJeff Roberson 	if (td != NULL) {
12259727e637SJeff Roberson 		KASSERT(td->td_priority >= PRI_MIN_IDLE,
1226e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on idle queue %d",
12279727e637SJeff Roberson 		    td->td_priority));
12289727e637SJeff Roberson 		return (td);
1229e7d50326SJeff Roberson 	}
1230e7d50326SJeff Roberson 
1231e7d50326SJeff Roberson 	return (NULL);
1232245f3abfSJeff Roberson }
12330a016a05SJeff Roberson 
1234ae7a6b38SJeff Roberson /*
1235ae7a6b38SJeff Roberson  * Initialize a thread queue.
1236ae7a6b38SJeff Roberson  */
12370a016a05SJeff Roberson static void
1238ad1e7d28SJulian Elischer tdq_setup(struct tdq *tdq)
12390a016a05SJeff Roberson {
1240ae7a6b38SJeff Roberson 
1241c47f202bSJeff Roberson 	if (bootverbose)
1242c47f202bSJeff Roberson 		printf("ULE: setup cpu %d\n", TDQ_ID(tdq));
1243e7d50326SJeff Roberson 	runq_init(&tdq->tdq_realtime);
1244e7d50326SJeff Roberson 	runq_init(&tdq->tdq_timeshare);
1245d2ad694cSJeff Roberson 	runq_init(&tdq->tdq_idle);
124662fa74d9SJeff Roberson 	snprintf(tdq->tdq_name, sizeof(tdq->tdq_name),
124762fa74d9SJeff Roberson 	    "sched lock %d", (int)TDQ_ID(tdq));
124862fa74d9SJeff Roberson 	mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock",
124962fa74d9SJeff Roberson 	    MTX_SPIN | MTX_RECURSE);
12508f51ad55SJeff Roberson #ifdef KTR
12518f51ad55SJeff Roberson 	snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname),
12528f51ad55SJeff Roberson 	    "CPU %d load", (int)TDQ_ID(tdq));
12538f51ad55SJeff Roberson #endif
12540a016a05SJeff Roberson }
12550a016a05SJeff Roberson 
1256c47f202bSJeff Roberson #ifdef SMP
1257c47f202bSJeff Roberson static void
1258c47f202bSJeff Roberson sched_setup_smp(void)
1259c47f202bSJeff Roberson {
1260c47f202bSJeff Roberson 	struct tdq *tdq;
1261c47f202bSJeff Roberson 	int i;
1262c47f202bSJeff Roberson 
126362fa74d9SJeff Roberson 	cpu_top = smp_topo();
126462fa74d9SJeff Roberson 	for (i = 0; i < MAXCPU; i++) {
1265c47f202bSJeff Roberson 		if (CPU_ABSENT(i))
1266c47f202bSJeff Roberson 			continue;
126762fa74d9SJeff Roberson 		tdq = TDQ_CPU(i);
1268c47f202bSJeff Roberson 		tdq_setup(tdq);
126962fa74d9SJeff Roberson 		tdq->tdq_cg = smp_topo_find(cpu_top, i);
127062fa74d9SJeff Roberson 		if (tdq->tdq_cg == NULL)
127162fa74d9SJeff Roberson 			panic("Can't find cpu group for %d\n", i);
1272c47f202bSJeff Roberson 	}
127362fa74d9SJeff Roberson 	balance_tdq = TDQ_SELF();
127462fa74d9SJeff Roberson 	sched_balance();
1275c47f202bSJeff Roberson }
1276c47f202bSJeff Roberson #endif
1277c47f202bSJeff Roberson 
1278ae7a6b38SJeff Roberson /*
1279ae7a6b38SJeff Roberson  * Setup the thread queues and initialize the topology based on MD
1280ae7a6b38SJeff Roberson  * information.
1281ae7a6b38SJeff Roberson  */
128235e6168fSJeff Roberson static void
128335e6168fSJeff Roberson sched_setup(void *dummy)
128435e6168fSJeff Roberson {
1285ae7a6b38SJeff Roberson 	struct tdq *tdq;
1286c47f202bSJeff Roberson 
1287c47f202bSJeff Roberson 	tdq = TDQ_SELF();
12880ec896fdSJeff Roberson #ifdef SMP
1289c47f202bSJeff Roberson 	sched_setup_smp();
1290749d01b0SJeff Roberson #else
1291c47f202bSJeff Roberson 	tdq_setup(tdq);
1292356500a3SJeff Roberson #endif
1293ae7a6b38SJeff Roberson 	/*
1294ae7a6b38SJeff Roberson 	 * To avoid divide-by-zero, we set realstathz a dummy value
1295ae7a6b38SJeff Roberson 	 * in case which sched_clock() called before sched_initticks().
1296ae7a6b38SJeff Roberson 	 */
1297ae7a6b38SJeff Roberson 	realstathz = hz;
1298ae7a6b38SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1299ae7a6b38SJeff Roberson 	tickincr = 1 << SCHED_TICK_SHIFT;
1300ae7a6b38SJeff Roberson 
1301ae7a6b38SJeff Roberson 	/* Add thread0's load since it's running. */
1302ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1303c47f202bSJeff Roberson 	thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF());
13049727e637SJeff Roberson 	tdq_load_add(tdq, &thread0);
130562fa74d9SJeff Roberson 	tdq->tdq_lowpri = thread0.td_priority;
1306ae7a6b38SJeff Roberson 	TDQ_UNLOCK(tdq);
130735e6168fSJeff Roberson }
130835e6168fSJeff Roberson 
1309ae7a6b38SJeff Roberson /*
1310ae7a6b38SJeff Roberson  * This routine determines the tickincr after stathz and hz are setup.
1311ae7a6b38SJeff Roberson  */
1312a1d4fe69SDavid Xu /* ARGSUSED */
1313a1d4fe69SDavid Xu static void
1314a1d4fe69SDavid Xu sched_initticks(void *dummy)
1315a1d4fe69SDavid Xu {
1316ae7a6b38SJeff Roberson 	int incr;
1317ae7a6b38SJeff Roberson 
1318a1d4fe69SDavid Xu 	realstathz = stathz ? stathz : hz;
131914618990SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1320a1d4fe69SDavid Xu 
1321a1d4fe69SDavid Xu 	/*
1322e7d50326SJeff Roberson 	 * tickincr is shifted out by 10 to avoid rounding errors due to
13233f872f85SJeff Roberson 	 * hz not being evenly divisible by stathz on all platforms.
1324e7d50326SJeff Roberson 	 */
1325ae7a6b38SJeff Roberson 	incr = (hz << SCHED_TICK_SHIFT) / realstathz;
1326e7d50326SJeff Roberson 	/*
1327e7d50326SJeff Roberson 	 * This does not work for values of stathz that are more than
1328e7d50326SJeff Roberson 	 * 1 << SCHED_TICK_SHIFT * hz.  In practice this does not happen.
1329a1d4fe69SDavid Xu 	 */
1330ae7a6b38SJeff Roberson 	if (incr == 0)
1331ae7a6b38SJeff Roberson 		incr = 1;
1332ae7a6b38SJeff Roberson 	tickincr = incr;
13337b8bfa0dSJeff Roberson #ifdef SMP
13349862717aSJeff Roberson 	/*
13357fcf154aSJeff Roberson 	 * Set the default balance interval now that we know
13367fcf154aSJeff Roberson 	 * what realstathz is.
13377fcf154aSJeff Roberson 	 */
13387fcf154aSJeff Roberson 	balance_interval = realstathz;
13397fcf154aSJeff Roberson 	/*
13409862717aSJeff Roberson 	 * Set steal thresh to log2(mp_ncpu) but no greater than 4.  This
13419862717aSJeff Roberson 	 * prevents excess thrashing on large machines and excess idle on
13429862717aSJeff Roberson 	 * smaller machines.
13439862717aSJeff Roberson 	 */
134462fa74d9SJeff Roberson 	steal_thresh = min(ffs(mp_ncpus) - 1, 3);
13457b8bfa0dSJeff Roberson 	affinity = SCHED_AFFINITY_DEFAULT;
13467b8bfa0dSJeff Roberson #endif
1347a1d4fe69SDavid Xu }
1348a1d4fe69SDavid Xu 
1349a1d4fe69SDavid Xu 
135035e6168fSJeff Roberson /*
1351ae7a6b38SJeff Roberson  * This is the core of the interactivity algorithm.  Determines a score based
1352ae7a6b38SJeff Roberson  * on past behavior.  It is the ratio of sleep time to run time scaled to
1353ae7a6b38SJeff Roberson  * a [0, 100] integer.  This is the voluntary sleep time of a process, which
1354ae7a6b38SJeff Roberson  * differs from the cpu usage because it does not account for time spent
1355ae7a6b38SJeff Roberson  * waiting on a run-queue.  Would be prettier if we had floating point.
1356ae7a6b38SJeff Roberson  */
1357ae7a6b38SJeff Roberson static int
1358ae7a6b38SJeff Roberson sched_interact_score(struct thread *td)
1359ae7a6b38SJeff Roberson {
1360ae7a6b38SJeff Roberson 	struct td_sched *ts;
1361ae7a6b38SJeff Roberson 	int div;
1362ae7a6b38SJeff Roberson 
1363ae7a6b38SJeff Roberson 	ts = td->td_sched;
1364ae7a6b38SJeff Roberson 	/*
1365ae7a6b38SJeff Roberson 	 * The score is only needed if this is likely to be an interactive
1366ae7a6b38SJeff Roberson 	 * task.  Don't go through the expense of computing it if there's
1367ae7a6b38SJeff Roberson 	 * no chance.
1368ae7a6b38SJeff Roberson 	 */
1369ae7a6b38SJeff Roberson 	if (sched_interact <= SCHED_INTERACT_HALF &&
1370ae7a6b38SJeff Roberson 		ts->ts_runtime >= ts->ts_slptime)
1371ae7a6b38SJeff Roberson 			return (SCHED_INTERACT_HALF);
1372ae7a6b38SJeff Roberson 
1373ae7a6b38SJeff Roberson 	if (ts->ts_runtime > ts->ts_slptime) {
1374ae7a6b38SJeff Roberson 		div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF);
1375ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF +
1376ae7a6b38SJeff Roberson 		    (SCHED_INTERACT_HALF - (ts->ts_slptime / div)));
1377ae7a6b38SJeff Roberson 	}
1378ae7a6b38SJeff Roberson 	if (ts->ts_slptime > ts->ts_runtime) {
1379ae7a6b38SJeff Roberson 		div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF);
1380ae7a6b38SJeff Roberson 		return (ts->ts_runtime / div);
1381ae7a6b38SJeff Roberson 	}
1382ae7a6b38SJeff Roberson 	/* runtime == slptime */
1383ae7a6b38SJeff Roberson 	if (ts->ts_runtime)
1384ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF);
1385ae7a6b38SJeff Roberson 
1386ae7a6b38SJeff Roberson 	/*
1387ae7a6b38SJeff Roberson 	 * This can happen if slptime and runtime are 0.
1388ae7a6b38SJeff Roberson 	 */
1389ae7a6b38SJeff Roberson 	return (0);
1390ae7a6b38SJeff Roberson 
1391ae7a6b38SJeff Roberson }
1392ae7a6b38SJeff Roberson 
1393ae7a6b38SJeff Roberson /*
139435e6168fSJeff Roberson  * Scale the scheduling priority according to the "interactivity" of this
139535e6168fSJeff Roberson  * process.
139635e6168fSJeff Roberson  */
139715dc847eSJeff Roberson static void
13988460a577SJohn Birrell sched_priority(struct thread *td)
139935e6168fSJeff Roberson {
1400e7d50326SJeff Roberson 	int score;
140135e6168fSJeff Roberson 	int pri;
140235e6168fSJeff Roberson 
14038460a577SJohn Birrell 	if (td->td_pri_class != PRI_TIMESHARE)
140415dc847eSJeff Roberson 		return;
1405e7d50326SJeff Roberson 	/*
1406e7d50326SJeff Roberson 	 * If the score is interactive we place the thread in the realtime
1407e7d50326SJeff Roberson 	 * queue with a priority that is less than kernel and interrupt
1408e7d50326SJeff Roberson 	 * priorities.  These threads are not subject to nice restrictions.
1409e7d50326SJeff Roberson 	 *
1410ae7a6b38SJeff Roberson 	 * Scores greater than this are placed on the normal timeshare queue
1411e7d50326SJeff Roberson 	 * where the priority is partially decided by the most recent cpu
1412e7d50326SJeff Roberson 	 * utilization and the rest is decided by nice value.
1413a5423ea3SJeff Roberson 	 *
1414a5423ea3SJeff Roberson 	 * The nice value of the process has a linear effect on the calculated
1415a5423ea3SJeff Roberson 	 * score.  Negative nice values make it easier for a thread to be
1416a5423ea3SJeff Roberson 	 * considered interactive.
1417e7d50326SJeff Roberson 	 */
1418e270652bSJeff Roberson 	score = imax(0, sched_interact_score(td) - td->td_proc->p_nice);
1419e7d50326SJeff Roberson 	if (score < sched_interact) {
1420e7d50326SJeff Roberson 		pri = PRI_MIN_REALTIME;
1421e7d50326SJeff Roberson 		pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
1422e7d50326SJeff Roberson 		    * score;
1423e7d50326SJeff Roberson 		KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
14249a93305aSJeff Roberson 		    ("sched_priority: invalid interactive priority %d score %d",
14259a93305aSJeff Roberson 		    pri, score));
1426e7d50326SJeff Roberson 	} else {
1427e7d50326SJeff Roberson 		pri = SCHED_PRI_MIN;
1428e7d50326SJeff Roberson 		if (td->td_sched->ts_ticks)
1429e7d50326SJeff Roberson 			pri += SCHED_PRI_TICKS(td->td_sched);
1430e7d50326SJeff Roberson 		pri += SCHED_PRI_NICE(td->td_proc->p_nice);
1431ae7a6b38SJeff Roberson 		KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE,
1432ae7a6b38SJeff Roberson 		    ("sched_priority: invalid priority %d: nice %d, "
1433ae7a6b38SJeff Roberson 		    "ticks %d ftick %d ltick %d tick pri %d",
1434ae7a6b38SJeff Roberson 		    pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
1435ae7a6b38SJeff Roberson 		    td->td_sched->ts_ftick, td->td_sched->ts_ltick,
1436ae7a6b38SJeff Roberson 		    SCHED_PRI_TICKS(td->td_sched)));
1437e7d50326SJeff Roberson 	}
14388460a577SJohn Birrell 	sched_user_prio(td, pri);
143935e6168fSJeff Roberson 
144015dc847eSJeff Roberson 	return;
144135e6168fSJeff Roberson }
144235e6168fSJeff Roberson 
144335e6168fSJeff Roberson /*
1444d322132cSJeff Roberson  * This routine enforces a maximum limit on the amount of scheduling history
1445ae7a6b38SJeff Roberson  * kept.  It is called after either the slptime or runtime is adjusted.  This
1446ae7a6b38SJeff Roberson  * function is ugly due to integer math.
1447d322132cSJeff Roberson  */
14484b60e324SJeff Roberson static void
14498460a577SJohn Birrell sched_interact_update(struct thread *td)
14504b60e324SJeff Roberson {
1451155b6ca1SJeff Roberson 	struct td_sched *ts;
14529a93305aSJeff Roberson 	u_int sum;
14533f741ca1SJeff Roberson 
1454155b6ca1SJeff Roberson 	ts = td->td_sched;
1455ae7a6b38SJeff Roberson 	sum = ts->ts_runtime + ts->ts_slptime;
1456d322132cSJeff Roberson 	if (sum < SCHED_SLP_RUN_MAX)
1457d322132cSJeff Roberson 		return;
1458d322132cSJeff Roberson 	/*
1459155b6ca1SJeff Roberson 	 * This only happens from two places:
1460155b6ca1SJeff Roberson 	 * 1) We have added an unusual amount of run time from fork_exit.
1461155b6ca1SJeff Roberson 	 * 2) We have added an unusual amount of sleep time from sched_sleep().
1462155b6ca1SJeff Roberson 	 */
1463155b6ca1SJeff Roberson 	if (sum > SCHED_SLP_RUN_MAX * 2) {
1464ae7a6b38SJeff Roberson 		if (ts->ts_runtime > ts->ts_slptime) {
1465ae7a6b38SJeff Roberson 			ts->ts_runtime = SCHED_SLP_RUN_MAX;
1466ae7a6b38SJeff Roberson 			ts->ts_slptime = 1;
1467155b6ca1SJeff Roberson 		} else {
1468ae7a6b38SJeff Roberson 			ts->ts_slptime = SCHED_SLP_RUN_MAX;
1469ae7a6b38SJeff Roberson 			ts->ts_runtime = 1;
1470155b6ca1SJeff Roberson 		}
1471155b6ca1SJeff Roberson 		return;
1472155b6ca1SJeff Roberson 	}
1473155b6ca1SJeff Roberson 	/*
1474d322132cSJeff Roberson 	 * If we have exceeded by more than 1/5th then the algorithm below
1475d322132cSJeff Roberson 	 * will not bring us back into range.  Dividing by two here forces
14762454aaf5SJeff Roberson 	 * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
1477d322132cSJeff Roberson 	 */
147837a35e4aSJeff Roberson 	if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
1479ae7a6b38SJeff Roberson 		ts->ts_runtime /= 2;
1480ae7a6b38SJeff Roberson 		ts->ts_slptime /= 2;
1481d322132cSJeff Roberson 		return;
1482d322132cSJeff Roberson 	}
1483ae7a6b38SJeff Roberson 	ts->ts_runtime = (ts->ts_runtime / 5) * 4;
1484ae7a6b38SJeff Roberson 	ts->ts_slptime = (ts->ts_slptime / 5) * 4;
1485d322132cSJeff Roberson }
1486d322132cSJeff Roberson 
1487ae7a6b38SJeff Roberson /*
1488ae7a6b38SJeff Roberson  * Scale back the interactivity history when a child thread is created.  The
1489ae7a6b38SJeff Roberson  * history is inherited from the parent but the thread may behave totally
1490ae7a6b38SJeff Roberson  * differently.  For example, a shell spawning a compiler process.  We want
1491ae7a6b38SJeff Roberson  * to learn that the compiler is behaving badly very quickly.
1492ae7a6b38SJeff Roberson  */
1493d322132cSJeff Roberson static void
14948460a577SJohn Birrell sched_interact_fork(struct thread *td)
1495d322132cSJeff Roberson {
1496d322132cSJeff Roberson 	int ratio;
1497d322132cSJeff Roberson 	int sum;
1498d322132cSJeff Roberson 
1499ae7a6b38SJeff Roberson 	sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
1500d322132cSJeff Roberson 	if (sum > SCHED_SLP_RUN_FORK) {
1501d322132cSJeff Roberson 		ratio = sum / SCHED_SLP_RUN_FORK;
1502ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime /= ratio;
1503ae7a6b38SJeff Roberson 		td->td_sched->ts_slptime /= ratio;
15044b60e324SJeff Roberson 	}
15054b60e324SJeff Roberson }
15064b60e324SJeff Roberson 
150715dc847eSJeff Roberson /*
1508ae7a6b38SJeff Roberson  * Called from proc0_init() to setup the scheduler fields.
1509ed062c8dSJulian Elischer  */
1510ed062c8dSJulian Elischer void
1511ed062c8dSJulian Elischer schedinit(void)
1512ed062c8dSJulian Elischer {
1513e7d50326SJeff Roberson 
1514ed062c8dSJulian Elischer 	/*
1515ed062c8dSJulian Elischer 	 * Set up the scheduler specific parts of proc0.
1516ed062c8dSJulian Elischer 	 */
1517ed062c8dSJulian Elischer 	proc0.p_sched = NULL; /* XXX */
1518ad1e7d28SJulian Elischer 	thread0.td_sched = &td_sched0;
1519e7d50326SJeff Roberson 	td_sched0.ts_ltick = ticks;
15208ab80cf0SJeff Roberson 	td_sched0.ts_ftick = ticks;
152173daf66fSJeff Roberson 	td_sched0.ts_slice = sched_slice;
1522ed062c8dSJulian Elischer }
1523ed062c8dSJulian Elischer 
1524ed062c8dSJulian Elischer /*
152515dc847eSJeff Roberson  * This is only somewhat accurate since given many processes of the same
152615dc847eSJeff Roberson  * priority they will switch when their slices run out, which will be
1527e7d50326SJeff Roberson  * at most sched_slice stathz ticks.
152815dc847eSJeff Roberson  */
152935e6168fSJeff Roberson int
153035e6168fSJeff Roberson sched_rr_interval(void)
153135e6168fSJeff Roberson {
1532e7d50326SJeff Roberson 
1533e7d50326SJeff Roberson 	/* Convert sched_slice to hz */
1534e7d50326SJeff Roberson 	return (hz/(realstathz/sched_slice));
153535e6168fSJeff Roberson }
153635e6168fSJeff Roberson 
1537ae7a6b38SJeff Roberson /*
1538ae7a6b38SJeff Roberson  * Update the percent cpu tracking information when it is requested or
1539ae7a6b38SJeff Roberson  * the total history exceeds the maximum.  We keep a sliding history of
1540ae7a6b38SJeff Roberson  * tick counts that slowly decays.  This is less precise than the 4BSD
1541ae7a6b38SJeff Roberson  * mechanism since it happens with less regular and frequent events.
1542ae7a6b38SJeff Roberson  */
154322bf7d9aSJeff Roberson static void
1544ad1e7d28SJulian Elischer sched_pctcpu_update(struct td_sched *ts)
154535e6168fSJeff Roberson {
1546e7d50326SJeff Roberson 
1547e7d50326SJeff Roberson 	if (ts->ts_ticks == 0)
1548e7d50326SJeff Roberson 		return;
15498ab80cf0SJeff Roberson 	if (ticks - (hz / 10) < ts->ts_ltick &&
15508ab80cf0SJeff Roberson 	    SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
15518ab80cf0SJeff Roberson 		return;
155235e6168fSJeff Roberson 	/*
155335e6168fSJeff Roberson 	 * Adjust counters and watermark for pctcpu calc.
1554210491d3SJeff Roberson 	 */
1555e7d50326SJeff Roberson 	if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
1556ad1e7d28SJulian Elischer 		ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
1557e7d50326SJeff Roberson 			    SCHED_TICK_TARG;
1558e7d50326SJeff Roberson 	else
1559ad1e7d28SJulian Elischer 		ts->ts_ticks = 0;
1560ad1e7d28SJulian Elischer 	ts->ts_ltick = ticks;
1561e7d50326SJeff Roberson 	ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
156235e6168fSJeff Roberson }
156335e6168fSJeff Roberson 
1564ae7a6b38SJeff Roberson /*
1565ae7a6b38SJeff Roberson  * Adjust the priority of a thread.  Move it to the appropriate run-queue
1566ae7a6b38SJeff Roberson  * if necessary.  This is the back-end for several priority related
1567ae7a6b38SJeff Roberson  * functions.
1568ae7a6b38SJeff Roberson  */
1569e7d50326SJeff Roberson static void
1570f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio)
157135e6168fSJeff Roberson {
1572ad1e7d28SJulian Elischer 	struct td_sched *ts;
157373daf66fSJeff Roberson 	struct tdq *tdq;
157473daf66fSJeff Roberson 	int oldpri;
157535e6168fSJeff Roberson 
15768f51ad55SJeff Roberson 	KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio",
15778f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, "new prio:%d", prio,
15788f51ad55SJeff Roberson 	    KTR_ATTR_LINKED, sched_tdname(curthread));
15798f51ad55SJeff Roberson 	if (td != curthread && prio > td->td_priority) {
15808f51ad55SJeff Roberson 		KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread),
15818f51ad55SJeff Roberson 		    "lend prio", "prio:%d", td->td_priority, "new prio:%d",
15828f51ad55SJeff Roberson 		    prio, KTR_ATTR_LINKED, sched_tdname(td));
15838f51ad55SJeff Roberson 	}
1584ad1e7d28SJulian Elischer 	ts = td->td_sched;
15857b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1586f5c157d9SJohn Baldwin 	if (td->td_priority == prio)
1587f5c157d9SJohn Baldwin 		return;
15883f741ca1SJeff Roberson 	/*
15893f741ca1SJeff Roberson 	 * If the priority has been elevated due to priority
15903f741ca1SJeff Roberson 	 * propagation, we may have to move ourselves to a new
1591e7d50326SJeff Roberson 	 * queue.  This could be optimized to not re-add in some
1592e7d50326SJeff Roberson 	 * cases.
1593f2b74cbfSJeff Roberson 	 */
15946d55b3ecSJeff Roberson 	if (TD_ON_RUNQ(td) && prio < td->td_priority) {
1595e7d50326SJeff Roberson 		sched_rem(td);
1596e7d50326SJeff Roberson 		td->td_priority = prio;
1597ae7a6b38SJeff Roberson 		sched_add(td, SRQ_BORROWING);
159873daf66fSJeff Roberson 		return;
159973daf66fSJeff Roberson 	}
16006d55b3ecSJeff Roberson 	/*
16016d55b3ecSJeff Roberson 	 * If the thread is currently running we may have to adjust the lowpri
16026d55b3ecSJeff Roberson 	 * information so other cpus are aware of our current priority.
16036d55b3ecSJeff Roberson 	 */
16046d55b3ecSJeff Roberson 	if (TD_IS_RUNNING(td)) {
1605ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(ts->ts_cpu);
160662fa74d9SJeff Roberson 		oldpri = td->td_priority;
16073f741ca1SJeff Roberson 		td->td_priority = prio;
160862fa74d9SJeff Roberson 		if (prio < tdq->tdq_lowpri)
160962fa74d9SJeff Roberson 			tdq->tdq_lowpri = prio;
161062fa74d9SJeff Roberson 		else if (tdq->tdq_lowpri == oldpri)
161162fa74d9SJeff Roberson 			tdq_setlowpri(tdq, td);
16126d55b3ecSJeff Roberson 		return;
161373daf66fSJeff Roberson 	}
16146d55b3ecSJeff Roberson 	td->td_priority = prio;
1615ae7a6b38SJeff Roberson }
161635e6168fSJeff Roberson 
1617f5c157d9SJohn Baldwin /*
1618f5c157d9SJohn Baldwin  * Update a thread's priority when it is lent another thread's
1619f5c157d9SJohn Baldwin  * priority.
1620f5c157d9SJohn Baldwin  */
1621f5c157d9SJohn Baldwin void
1622f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio)
1623f5c157d9SJohn Baldwin {
1624f5c157d9SJohn Baldwin 
1625f5c157d9SJohn Baldwin 	td->td_flags |= TDF_BORROWING;
1626f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1627f5c157d9SJohn Baldwin }
1628f5c157d9SJohn Baldwin 
1629f5c157d9SJohn Baldwin /*
1630f5c157d9SJohn Baldwin  * Restore a thread's priority when priority propagation is
1631f5c157d9SJohn Baldwin  * over.  The prio argument is the minimum priority the thread
1632f5c157d9SJohn Baldwin  * needs to have to satisfy other possible priority lending
1633f5c157d9SJohn Baldwin  * requests.  If the thread's regular priority is less
1634f5c157d9SJohn Baldwin  * important than prio, the thread will keep a priority boost
1635f5c157d9SJohn Baldwin  * of prio.
1636f5c157d9SJohn Baldwin  */
1637f5c157d9SJohn Baldwin void
1638f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio)
1639f5c157d9SJohn Baldwin {
1640f5c157d9SJohn Baldwin 	u_char base_pri;
1641f5c157d9SJohn Baldwin 
1642f5c157d9SJohn Baldwin 	if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1643f5c157d9SJohn Baldwin 	    td->td_base_pri <= PRI_MAX_TIMESHARE)
16448460a577SJohn Birrell 		base_pri = td->td_user_pri;
1645f5c157d9SJohn Baldwin 	else
1646f5c157d9SJohn Baldwin 		base_pri = td->td_base_pri;
1647f5c157d9SJohn Baldwin 	if (prio >= base_pri) {
1648f5c157d9SJohn Baldwin 		td->td_flags &= ~TDF_BORROWING;
1649f5c157d9SJohn Baldwin 		sched_thread_priority(td, base_pri);
1650f5c157d9SJohn Baldwin 	} else
1651f5c157d9SJohn Baldwin 		sched_lend_prio(td, prio);
1652f5c157d9SJohn Baldwin }
1653f5c157d9SJohn Baldwin 
1654ae7a6b38SJeff Roberson /*
1655ae7a6b38SJeff Roberson  * Standard entry for setting the priority to an absolute value.
1656ae7a6b38SJeff Roberson  */
1657f5c157d9SJohn Baldwin void
1658f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio)
1659f5c157d9SJohn Baldwin {
1660f5c157d9SJohn Baldwin 	u_char oldprio;
1661f5c157d9SJohn Baldwin 
1662f5c157d9SJohn Baldwin 	/* First, update the base priority. */
1663f5c157d9SJohn Baldwin 	td->td_base_pri = prio;
1664f5c157d9SJohn Baldwin 
1665f5c157d9SJohn Baldwin 	/*
166650aaa791SJohn Baldwin 	 * If the thread is borrowing another thread's priority, don't
1667f5c157d9SJohn Baldwin 	 * ever lower the priority.
1668f5c157d9SJohn Baldwin 	 */
1669f5c157d9SJohn Baldwin 	if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1670f5c157d9SJohn Baldwin 		return;
1671f5c157d9SJohn Baldwin 
1672f5c157d9SJohn Baldwin 	/* Change the real priority. */
1673f5c157d9SJohn Baldwin 	oldprio = td->td_priority;
1674f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1675f5c157d9SJohn Baldwin 
1676f5c157d9SJohn Baldwin 	/*
1677f5c157d9SJohn Baldwin 	 * If the thread is on a turnstile, then let the turnstile update
1678f5c157d9SJohn Baldwin 	 * its state.
1679f5c157d9SJohn Baldwin 	 */
1680f5c157d9SJohn Baldwin 	if (TD_ON_LOCK(td) && oldprio != prio)
1681f5c157d9SJohn Baldwin 		turnstile_adjust(td, oldprio);
1682f5c157d9SJohn Baldwin }
1683f5c157d9SJohn Baldwin 
1684ae7a6b38SJeff Roberson /*
1685ae7a6b38SJeff Roberson  * Set the base user priority, does not effect current running priority.
1686ae7a6b38SJeff Roberson  */
168735e6168fSJeff Roberson void
16888460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio)
16893db720fdSDavid Xu {
16903db720fdSDavid Xu 	u_char oldprio;
16913db720fdSDavid Xu 
16928460a577SJohn Birrell 	td->td_base_user_pri = prio;
1693fc6c30f6SJulian Elischer 	if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
1694fc6c30f6SJulian Elischer                 return;
16958460a577SJohn Birrell 	oldprio = td->td_user_pri;
16968460a577SJohn Birrell 	td->td_user_pri = prio;
16973db720fdSDavid Xu }
16983db720fdSDavid Xu 
16993db720fdSDavid Xu void
17003db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio)
17013db720fdSDavid Xu {
17023db720fdSDavid Xu 	u_char oldprio;
17033db720fdSDavid Xu 
1704435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17053db720fdSDavid Xu 	td->td_flags |= TDF_UBORROWING;
1706f645b5daSMaxim Konovalov 	oldprio = td->td_user_pri;
17078460a577SJohn Birrell 	td->td_user_pri = prio;
17083db720fdSDavid Xu }
17093db720fdSDavid Xu 
17103db720fdSDavid Xu void
17113db720fdSDavid Xu sched_unlend_user_prio(struct thread *td, u_char prio)
17123db720fdSDavid Xu {
17133db720fdSDavid Xu 	u_char base_pri;
17143db720fdSDavid Xu 
1715435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17168460a577SJohn Birrell 	base_pri = td->td_base_user_pri;
17173db720fdSDavid Xu 	if (prio >= base_pri) {
17183db720fdSDavid Xu 		td->td_flags &= ~TDF_UBORROWING;
17198460a577SJohn Birrell 		sched_user_prio(td, base_pri);
1720435806d3SDavid Xu 	} else {
17213db720fdSDavid Xu 		sched_lend_user_prio(td, prio);
17223db720fdSDavid Xu 	}
1723435806d3SDavid Xu }
17243db720fdSDavid Xu 
1725ae7a6b38SJeff Roberson /*
1726731016feSWojciech A. Koszek  * Block a thread for switching.  Similar to thread_block() but does not
1727731016feSWojciech A. Koszek  * bump the spin count.
1728731016feSWojciech A. Koszek  */
1729731016feSWojciech A. Koszek static inline struct mtx *
1730731016feSWojciech A. Koszek thread_block_switch(struct thread *td)
1731731016feSWojciech A. Koszek {
1732731016feSWojciech A. Koszek 	struct mtx *lock;
1733731016feSWojciech A. Koszek 
1734731016feSWojciech A. Koszek 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1735731016feSWojciech A. Koszek 	lock = td->td_lock;
1736731016feSWojciech A. Koszek 	td->td_lock = &blocked_lock;
1737731016feSWojciech A. Koszek 	mtx_unlock_spin(lock);
1738731016feSWojciech A. Koszek 
1739731016feSWojciech A. Koszek 	return (lock);
1740731016feSWojciech A. Koszek }
1741731016feSWojciech A. Koszek 
1742731016feSWojciech A. Koszek /*
1743c47f202bSJeff Roberson  * Handle migration from sched_switch().  This happens only for
1744c47f202bSJeff Roberson  * cpu binding.
1745c47f202bSJeff Roberson  */
1746c47f202bSJeff Roberson static struct mtx *
1747c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
1748c47f202bSJeff Roberson {
1749c47f202bSJeff Roberson 	struct tdq *tdn;
1750c47f202bSJeff Roberson 
1751c47f202bSJeff Roberson 	tdn = TDQ_CPU(td->td_sched->ts_cpu);
1752c47f202bSJeff Roberson #ifdef SMP
17539727e637SJeff Roberson 	tdq_load_rem(tdq, td);
1754c47f202bSJeff Roberson 	/*
1755c47f202bSJeff Roberson 	 * Do the lock dance required to avoid LOR.  We grab an extra
1756c47f202bSJeff Roberson 	 * spinlock nesting to prevent preemption while we're
1757c47f202bSJeff Roberson 	 * not holding either run-queue lock.
1758c47f202bSJeff Roberson 	 */
1759c47f202bSJeff Roberson 	spinlock_enter();
1760c47f202bSJeff Roberson 	thread_block_switch(td);	/* This releases the lock on tdq. */
1761c47f202bSJeff Roberson 	TDQ_LOCK(tdn);
1762c47f202bSJeff Roberson 	tdq_add(tdn, td, flags);
17639727e637SJeff Roberson 	tdq_notify(tdn, td);
1764c47f202bSJeff Roberson 	/*
1765c47f202bSJeff Roberson 	 * After we unlock tdn the new cpu still can't switch into this
1766c47f202bSJeff Roberson 	 * thread until we've unblocked it in cpu_switch().  The lock
1767c47f202bSJeff Roberson 	 * pointers may match in the case of HTT cores.  Don't unlock here
1768c47f202bSJeff Roberson 	 * or we can deadlock when the other CPU runs the IPI handler.
1769c47f202bSJeff Roberson 	 */
1770c47f202bSJeff Roberson 	if (TDQ_LOCKPTR(tdn) != TDQ_LOCKPTR(tdq)) {
1771c47f202bSJeff Roberson 		TDQ_UNLOCK(tdn);
1772c47f202bSJeff Roberson 		TDQ_LOCK(tdq);
1773c47f202bSJeff Roberson 	}
1774c47f202bSJeff Roberson 	spinlock_exit();
1775c47f202bSJeff Roberson #endif
1776c47f202bSJeff Roberson 	return (TDQ_LOCKPTR(tdn));
1777c47f202bSJeff Roberson }
1778c47f202bSJeff Roberson 
1779c47f202bSJeff Roberson /*
1780ae7a6b38SJeff Roberson  * Release a thread that was blocked with thread_block_switch().
1781ae7a6b38SJeff Roberson  */
1782ae7a6b38SJeff Roberson static inline void
1783ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx)
1784ae7a6b38SJeff Roberson {
1785ae7a6b38SJeff Roberson 	atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock,
1786ae7a6b38SJeff Roberson 	    (uintptr_t)mtx);
1787ae7a6b38SJeff Roberson }
1788ae7a6b38SJeff Roberson 
1789ae7a6b38SJeff Roberson /*
1790ae7a6b38SJeff Roberson  * Switch threads.  This function has to handle threads coming in while
1791ae7a6b38SJeff Roberson  * blocked for some reason, running, or idle.  It also must deal with
1792ae7a6b38SJeff Roberson  * migrating a thread from one queue to another as running threads may
1793ae7a6b38SJeff Roberson  * be assigned elsewhere via binding.
1794ae7a6b38SJeff Roberson  */
17953db720fdSDavid Xu void
17963389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags)
179735e6168fSJeff Roberson {
1798c02bbb43SJeff Roberson 	struct tdq *tdq;
1799ad1e7d28SJulian Elischer 	struct td_sched *ts;
1800ae7a6b38SJeff Roberson 	struct mtx *mtx;
1801c47f202bSJeff Roberson 	int srqflag;
1802ae7a6b38SJeff Roberson 	int cpuid;
180335e6168fSJeff Roberson 
18047b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
18056d55b3ecSJeff Roberson 	KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument"));
180635e6168fSJeff Roberson 
1807ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
1808ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
1809e7d50326SJeff Roberson 	ts = td->td_sched;
1810c47f202bSJeff Roberson 	mtx = td->td_lock;
1811ae7a6b38SJeff Roberson 	ts->ts_rltick = ticks;
1812060563ecSJulian Elischer 	td->td_lastcpu = td->td_oncpu;
1813060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
181452eb8464SJohn Baldwin 	td->td_flags &= ~TDF_NEEDRESCHED;
181577918643SStephan Uphoff 	td->td_owepreempt = 0;
18161690c6c1SJeff Roberson 	tdq->tdq_switchcnt++;
1817b11fdad0SJeff Roberson 	/*
1818ae7a6b38SJeff Roberson 	 * The lock pointer in an idle thread should never change.  Reset it
1819ae7a6b38SJeff Roberson 	 * to CAN_RUN as well.
1820b11fdad0SJeff Roberson 	 */
1821486a9414SJulian Elischer 	if (TD_IS_IDLETHREAD(td)) {
1822ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1823bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
18247b20fb19SJeff Roberson 	} else if (TD_IS_RUNNING(td)) {
1825ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1826c47f202bSJeff Roberson 		srqflag = (flags & SW_PREEMPT) ?
1827598b368dSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1828c47f202bSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING;
1829c47f202bSJeff Roberson 		if (ts->ts_cpu == cpuid)
18309727e637SJeff Roberson 			tdq_runq_add(tdq, td, srqflag);
1831c47f202bSJeff Roberson 		else
1832c47f202bSJeff Roberson 			mtx = sched_switch_migrate(tdq, td, srqflag);
1833ae7a6b38SJeff Roberson 	} else {
1834ae7a6b38SJeff Roberson 		/* This thread must be going to sleep. */
1835ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1836ae7a6b38SJeff Roberson 		mtx = thread_block_switch(td);
18379727e637SJeff Roberson 		tdq_load_rem(tdq, td);
1838ae7a6b38SJeff Roberson 	}
1839ae7a6b38SJeff Roberson 	/*
1840ae7a6b38SJeff Roberson 	 * We enter here with the thread blocked and assigned to the
1841ae7a6b38SJeff Roberson 	 * appropriate cpu run-queue or sleep-queue and with the current
1842ae7a6b38SJeff Roberson 	 * thread-queue locked.
1843ae7a6b38SJeff Roberson 	 */
1844ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
18452454aaf5SJeff Roberson 	newtd = choosethread();
1846ae7a6b38SJeff Roberson 	/*
1847ae7a6b38SJeff Roberson 	 * Call the MD code to switch contexts if necessary.
1848ae7a6b38SJeff Roberson 	 */
1849ebccf1e3SJoseph Koshy 	if (td != newtd) {
1850ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1851ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1852ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1853ebccf1e3SJoseph Koshy #endif
1854eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
185559c68134SJeff Roberson 		TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
18566f5f25e5SJohn Birrell 
18576f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS
18586f5f25e5SJohn Birrell 		/*
18596f5f25e5SJohn Birrell 		 * If DTrace has set the active vtime enum to anything
18606f5f25e5SJohn Birrell 		 * other than INACTIVE (0), then it should have set the
18616f5f25e5SJohn Birrell 		 * function to call.
18626f5f25e5SJohn Birrell 		 */
18636f5f25e5SJohn Birrell 		if (dtrace_vtime_active)
18646f5f25e5SJohn Birrell 			(*dtrace_vtime_switch_func)(newtd);
18656f5f25e5SJohn Birrell #endif
18666f5f25e5SJohn Birrell 
1867ae7a6b38SJeff Roberson 		cpu_switch(td, newtd, mtx);
1868ae7a6b38SJeff Roberson 		/*
1869ae7a6b38SJeff Roberson 		 * We may return from cpu_switch on a different cpu.  However,
1870ae7a6b38SJeff Roberson 		 * we always return with td_lock pointing to the current cpu's
1871ae7a6b38SJeff Roberson 		 * run queue lock.
1872ae7a6b38SJeff Roberson 		 */
1873ae7a6b38SJeff Roberson 		cpuid = PCPU_GET(cpuid);
1874ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(cpuid);
1875eea4f254SJeff Roberson 		lock_profile_obtain_lock_success(
1876eea4f254SJeff Roberson 		    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
1877ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1878ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1879ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1880ebccf1e3SJoseph Koshy #endif
1881ae7a6b38SJeff Roberson 	} else
1882ae7a6b38SJeff Roberson 		thread_unblock_switch(td, mtx);
1883ae7a6b38SJeff Roberson 	/*
1884ae7a6b38SJeff Roberson 	 * Assert that all went well and return.
1885ae7a6b38SJeff Roberson 	 */
1886ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED);
1887ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1888ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
188935e6168fSJeff Roberson }
189035e6168fSJeff Roberson 
1891ae7a6b38SJeff Roberson /*
1892ae7a6b38SJeff Roberson  * Adjust thread priorities as a result of a nice request.
1893ae7a6b38SJeff Roberson  */
189435e6168fSJeff Roberson void
1895fa885116SJulian Elischer sched_nice(struct proc *p, int nice)
189635e6168fSJeff Roberson {
189735e6168fSJeff Roberson 	struct thread *td;
189835e6168fSJeff Roberson 
1899fa885116SJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
1900e7d50326SJeff Roberson 
1901fa885116SJulian Elischer 	p->p_nice = nice;
19028460a577SJohn Birrell 	FOREACH_THREAD_IN_PROC(p, td) {
19037b20fb19SJeff Roberson 		thread_lock(td);
19048460a577SJohn Birrell 		sched_priority(td);
1905e7d50326SJeff Roberson 		sched_prio(td, td->td_base_user_pri);
19067b20fb19SJeff Roberson 		thread_unlock(td);
190735e6168fSJeff Roberson 	}
1908fa885116SJulian Elischer }
190935e6168fSJeff Roberson 
1910ae7a6b38SJeff Roberson /*
1911ae7a6b38SJeff Roberson  * Record the sleep time for the interactivity scorer.
1912ae7a6b38SJeff Roberson  */
191335e6168fSJeff Roberson void
1914c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio)
191535e6168fSJeff Roberson {
1916e7d50326SJeff Roberson 
19177b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
191835e6168fSJeff Roberson 
191954b0e65fSJeff Roberson 	td->td_slptick = ticks;
1920c5aa6b58SJeff Roberson 	if (TD_IS_SUSPENDED(td) || prio <= PSOCK)
1921c5aa6b58SJeff Roberson 		td->td_flags |= TDF_CANSWAP;
19220502fe2eSJeff Roberson 	if (static_boost == 1 && prio)
1923c5aa6b58SJeff Roberson 		sched_prio(td, prio);
19240502fe2eSJeff Roberson 	else if (static_boost && td->td_priority > static_boost)
19250502fe2eSJeff Roberson 		sched_prio(td, static_boost);
192635e6168fSJeff Roberson }
192735e6168fSJeff Roberson 
1928ae7a6b38SJeff Roberson /*
1929ae7a6b38SJeff Roberson  * Schedule a thread to resume execution and record how long it voluntarily
1930ae7a6b38SJeff Roberson  * slept.  We also update the pctcpu, interactivity, and priority.
1931ae7a6b38SJeff Roberson  */
193235e6168fSJeff Roberson void
193335e6168fSJeff Roberson sched_wakeup(struct thread *td)
193435e6168fSJeff Roberson {
193514618990SJeff Roberson 	struct td_sched *ts;
1936ae7a6b38SJeff Roberson 	int slptick;
1937e7d50326SJeff Roberson 
19387b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
193914618990SJeff Roberson 	ts = td->td_sched;
1940c5aa6b58SJeff Roberson 	td->td_flags &= ~TDF_CANSWAP;
194135e6168fSJeff Roberson 	/*
1942e7d50326SJeff Roberson 	 * If we slept for more than a tick update our interactivity and
1943e7d50326SJeff Roberson 	 * priority.
194435e6168fSJeff Roberson 	 */
194554b0e65fSJeff Roberson 	slptick = td->td_slptick;
194654b0e65fSJeff Roberson 	td->td_slptick = 0;
1947ae7a6b38SJeff Roberson 	if (slptick && slptick != ticks) {
19489a93305aSJeff Roberson 		u_int hzticks;
1949f1e8dc4aSJeff Roberson 
1950ae7a6b38SJeff Roberson 		hzticks = (ticks - slptick) << SCHED_TICK_SHIFT;
1951ae7a6b38SJeff Roberson 		ts->ts_slptime += hzticks;
19528460a577SJohn Birrell 		sched_interact_update(td);
195314618990SJeff Roberson 		sched_pctcpu_update(ts);
1954f1e8dc4aSJeff Roberson 	}
195514618990SJeff Roberson 	/* Reset the slice value after we sleep. */
195614618990SJeff Roberson 	ts->ts_slice = sched_slice;
19577a5e5e2aSJeff Roberson 	sched_add(td, SRQ_BORING);
195835e6168fSJeff Roberson }
195935e6168fSJeff Roberson 
196035e6168fSJeff Roberson /*
196135e6168fSJeff Roberson  * Penalize the parent for creating a new child and initialize the child's
196235e6168fSJeff Roberson  * priority.
196335e6168fSJeff Roberson  */
196435e6168fSJeff Roberson void
19658460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child)
196615dc847eSJeff Roberson {
19677b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1968ad1e7d28SJulian Elischer 	sched_fork_thread(td, child);
1969e7d50326SJeff Roberson 	/*
1970e7d50326SJeff Roberson 	 * Penalize the parent and child for forking.
1971e7d50326SJeff Roberson 	 */
1972e7d50326SJeff Roberson 	sched_interact_fork(child);
1973e7d50326SJeff Roberson 	sched_priority(child);
1974ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += tickincr;
1975e7d50326SJeff Roberson 	sched_interact_update(td);
1976e7d50326SJeff Roberson 	sched_priority(td);
1977ad1e7d28SJulian Elischer }
1978ad1e7d28SJulian Elischer 
1979ae7a6b38SJeff Roberson /*
1980ae7a6b38SJeff Roberson  * Fork a new thread, may be within the same process.
1981ae7a6b38SJeff Roberson  */
1982ad1e7d28SJulian Elischer void
1983ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child)
1984ad1e7d28SJulian Elischer {
1985ad1e7d28SJulian Elischer 	struct td_sched *ts;
1986ad1e7d28SJulian Elischer 	struct td_sched *ts2;
19878460a577SJohn Birrell 
19888b16c208SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1989e7d50326SJeff Roberson 	/*
1990e7d50326SJeff Roberson 	 * Initialize child.
1991e7d50326SJeff Roberson 	 */
1992ad1e7d28SJulian Elischer 	ts = td->td_sched;
1993ad1e7d28SJulian Elischer 	ts2 = child->td_sched;
19948b16c208SJeff Roberson 	child->td_lock = TDQ_LOCKPTR(TDQ_SELF());
19958b16c208SJeff Roberson 	child->td_cpuset = cpuset_ref(td->td_cpuset);
1996ad1e7d28SJulian Elischer 	ts2->ts_cpu = ts->ts_cpu;
19978b16c208SJeff Roberson 	ts2->ts_flags = 0;
1998e7d50326SJeff Roberson 	/*
1999e7d50326SJeff Roberson 	 * Grab our parents cpu estimation information and priority.
2000e7d50326SJeff Roberson 	 */
2001ad1e7d28SJulian Elischer 	ts2->ts_ticks = ts->ts_ticks;
2002ad1e7d28SJulian Elischer 	ts2->ts_ltick = ts->ts_ltick;
2003ad1e7d28SJulian Elischer 	ts2->ts_ftick = ts->ts_ftick;
2004e7d50326SJeff Roberson 	child->td_user_pri = td->td_user_pri;
2005e7d50326SJeff Roberson 	child->td_base_user_pri = td->td_base_user_pri;
2006e7d50326SJeff Roberson 	/*
2007e7d50326SJeff Roberson 	 * And update interactivity score.
2008e7d50326SJeff Roberson 	 */
2009ae7a6b38SJeff Roberson 	ts2->ts_slptime = ts->ts_slptime;
2010ae7a6b38SJeff Roberson 	ts2->ts_runtime = ts->ts_runtime;
2011e7d50326SJeff Roberson 	ts2->ts_slice = 1;	/* Attempt to quickly learn interactivity. */
20128f51ad55SJeff Roberson #ifdef KTR
20138f51ad55SJeff Roberson 	bzero(ts2->ts_name, sizeof(ts2->ts_name));
20148f51ad55SJeff Roberson #endif
201515dc847eSJeff Roberson }
201615dc847eSJeff Roberson 
2017ae7a6b38SJeff Roberson /*
2018ae7a6b38SJeff Roberson  * Adjust the priority class of a thread.
2019ae7a6b38SJeff Roberson  */
202015dc847eSJeff Roberson void
20218460a577SJohn Birrell sched_class(struct thread *td, int class)
202215dc847eSJeff Roberson {
202315dc847eSJeff Roberson 
20247b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
20258460a577SJohn Birrell 	if (td->td_pri_class == class)
202615dc847eSJeff Roberson 		return;
20278460a577SJohn Birrell 	td->td_pri_class = class;
202835e6168fSJeff Roberson }
202935e6168fSJeff Roberson 
203035e6168fSJeff Roberson /*
203135e6168fSJeff Roberson  * Return some of the child's priority and interactivity to the parent.
203235e6168fSJeff Roberson  */
203335e6168fSJeff Roberson void
2034fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child)
203535e6168fSJeff Roberson {
2036e7d50326SJeff Roberson 	struct thread *td;
2037141ad61cSJeff Roberson 
20388f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit",
20398f51ad55SJeff Roberson 	    "prio:td", child->td_priority);
2040374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
2041e7d50326SJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
2042e7d50326SJeff Roberson 	sched_exit_thread(td, child);
2043ad1e7d28SJulian Elischer }
2044ad1e7d28SJulian Elischer 
2045ae7a6b38SJeff Roberson /*
2046ae7a6b38SJeff Roberson  * Penalize another thread for the time spent on this one.  This helps to
2047ae7a6b38SJeff Roberson  * worsen the priority and interactivity of processes which schedule batch
2048ae7a6b38SJeff Roberson  * jobs such as make.  This has little effect on the make process itself but
2049ae7a6b38SJeff Roberson  * causes new processes spawned by it to receive worse scores immediately.
2050ae7a6b38SJeff Roberson  */
2051ad1e7d28SJulian Elischer void
2052fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child)
2053ad1e7d28SJulian Elischer {
2054fc6c30f6SJulian Elischer 
20558f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit",
20568f51ad55SJeff Roberson 	    "prio:td", child->td_priority);
2057e7d50326SJeff Roberson 	/*
2058e7d50326SJeff Roberson 	 * Give the child's runtime to the parent without returning the
2059e7d50326SJeff Roberson 	 * sleep time as a penalty to the parent.  This causes shells that
2060e7d50326SJeff Roberson 	 * launch expensive things to mark their children as expensive.
2061e7d50326SJeff Roberson 	 */
20627b20fb19SJeff Roberson 	thread_lock(td);
2063ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += child->td_sched->ts_runtime;
2064fc6c30f6SJulian Elischer 	sched_interact_update(td);
2065e7d50326SJeff Roberson 	sched_priority(td);
20667b20fb19SJeff Roberson 	thread_unlock(td);
2067ad1e7d28SJulian Elischer }
2068ad1e7d28SJulian Elischer 
2069ff256d9cSJeff Roberson void
2070ff256d9cSJeff Roberson sched_preempt(struct thread *td)
2071ff256d9cSJeff Roberson {
2072ff256d9cSJeff Roberson 	struct tdq *tdq;
2073ff256d9cSJeff Roberson 
2074ff256d9cSJeff Roberson 	thread_lock(td);
2075ff256d9cSJeff Roberson 	tdq = TDQ_SELF();
2076ff256d9cSJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2077ff256d9cSJeff Roberson 	tdq->tdq_ipipending = 0;
2078ff256d9cSJeff Roberson 	if (td->td_priority > tdq->tdq_lowpri) {
20798df78c41SJeff Roberson 		int flags;
20808df78c41SJeff Roberson 
20818df78c41SJeff Roberson 		flags = SW_INVOL | SW_PREEMPT;
2082ff256d9cSJeff Roberson 		if (td->td_critnest > 1)
2083ff256d9cSJeff Roberson 			td->td_owepreempt = 1;
20848df78c41SJeff Roberson 		else if (TD_IS_IDLETHREAD(td))
20858df78c41SJeff Roberson 			mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL);
2086ff256d9cSJeff Roberson 		else
20878df78c41SJeff Roberson 			mi_switch(flags | SWT_REMOTEPREEMPT, NULL);
2088ff256d9cSJeff Roberson 	}
2089ff256d9cSJeff Roberson 	thread_unlock(td);
2090ff256d9cSJeff Roberson }
2091ff256d9cSJeff Roberson 
2092ae7a6b38SJeff Roberson /*
2093ae7a6b38SJeff Roberson  * Fix priorities on return to user-space.  Priorities may be elevated due
2094ae7a6b38SJeff Roberson  * to static priorities in msleep() or similar.
2095ae7a6b38SJeff Roberson  */
2096ad1e7d28SJulian Elischer void
2097ad1e7d28SJulian Elischer sched_userret(struct thread *td)
2098ad1e7d28SJulian Elischer {
2099ad1e7d28SJulian Elischer 	/*
2100ad1e7d28SJulian Elischer 	 * XXX we cheat slightly on the locking here to avoid locking in
2101ad1e7d28SJulian Elischer 	 * the usual case.  Setting td_priority here is essentially an
2102ad1e7d28SJulian Elischer 	 * incomplete workaround for not setting it properly elsewhere.
2103ad1e7d28SJulian Elischer 	 * Now that some interrupt handlers are threads, not setting it
2104ad1e7d28SJulian Elischer 	 * properly elsewhere can clobber it in the window between setting
2105ad1e7d28SJulian Elischer 	 * it here and returning to user mode, so don't waste time setting
2106ad1e7d28SJulian Elischer 	 * it perfectly here.
2107ad1e7d28SJulian Elischer 	 */
2108ad1e7d28SJulian Elischer 	KASSERT((td->td_flags & TDF_BORROWING) == 0,
2109ad1e7d28SJulian Elischer 	    ("thread with borrowed priority returning to userland"));
2110ad1e7d28SJulian Elischer 	if (td->td_priority != td->td_user_pri) {
21117b20fb19SJeff Roberson 		thread_lock(td);
2112ad1e7d28SJulian Elischer 		td->td_priority = td->td_user_pri;
2113ad1e7d28SJulian Elischer 		td->td_base_pri = td->td_user_pri;
211462fa74d9SJeff Roberson 		tdq_setlowpri(TDQ_SELF(), td);
21157b20fb19SJeff Roberson 		thread_unlock(td);
2116ad1e7d28SJulian Elischer         }
211735e6168fSJeff Roberson }
211835e6168fSJeff Roberson 
2119ae7a6b38SJeff Roberson /*
2120ae7a6b38SJeff Roberson  * Handle a stathz tick.  This is really only relevant for timeshare
2121ae7a6b38SJeff Roberson  * threads.
2122ae7a6b38SJeff Roberson  */
212335e6168fSJeff Roberson void
21247cf90fb3SJeff Roberson sched_clock(struct thread *td)
212535e6168fSJeff Roberson {
2126ad1e7d28SJulian Elischer 	struct tdq *tdq;
2127ad1e7d28SJulian Elischer 	struct td_sched *ts;
212835e6168fSJeff Roberson 
2129ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
21303f872f85SJeff Roberson 	tdq = TDQ_SELF();
21317fcf154aSJeff Roberson #ifdef SMP
21327fcf154aSJeff Roberson 	/*
21337fcf154aSJeff Roberson 	 * We run the long term load balancer infrequently on the first cpu.
21347fcf154aSJeff Roberson 	 */
21357fcf154aSJeff Roberson 	if (balance_tdq == tdq) {
21367fcf154aSJeff Roberson 		if (balance_ticks && --balance_ticks == 0)
21377fcf154aSJeff Roberson 			sched_balance();
21387fcf154aSJeff Roberson 	}
21397fcf154aSJeff Roberson #endif
21403f872f85SJeff Roberson 	/*
21411690c6c1SJeff Roberson 	 * Save the old switch count so we have a record of the last ticks
21421690c6c1SJeff Roberson 	 * activity.   Initialize the new switch count based on our load.
21431690c6c1SJeff Roberson 	 * If there is some activity seed it to reflect that.
21441690c6c1SJeff Roberson 	 */
21451690c6c1SJeff Roberson 	tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt;
21466c47aaaeSJeff Roberson 	tdq->tdq_switchcnt = tdq->tdq_load;
21471690c6c1SJeff Roberson 	/*
21483f872f85SJeff Roberson 	 * Advance the insert index once for each tick to ensure that all
21493f872f85SJeff Roberson 	 * threads get a chance to run.
21503f872f85SJeff Roberson 	 */
21513f872f85SJeff Roberson 	if (tdq->tdq_idx == tdq->tdq_ridx) {
21523f872f85SJeff Roberson 		tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
21533f872f85SJeff Roberson 		if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
21543f872f85SJeff Roberson 			tdq->tdq_ridx = tdq->tdq_idx;
21553f872f85SJeff Roberson 	}
21563f872f85SJeff Roberson 	ts = td->td_sched;
2157fd0b8c78SJeff Roberson 	if (td->td_pri_class & PRI_FIFO_BIT)
2158a8949de2SJeff Roberson 		return;
2159fd0b8c78SJeff Roberson 	if (td->td_pri_class == PRI_TIMESHARE) {
2160a8949de2SJeff Roberson 		/*
2161fd0b8c78SJeff Roberson 		 * We used a tick; charge it to the thread so
2162fd0b8c78SJeff Roberson 		 * that we can compute our interactivity.
216315dc847eSJeff Roberson 		 */
2164ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime += tickincr;
21658460a577SJohn Birrell 		sched_interact_update(td);
216673daf66fSJeff Roberson 		sched_priority(td);
2167fd0b8c78SJeff Roberson 	}
216835e6168fSJeff Roberson 	/*
216935e6168fSJeff Roberson 	 * We used up one time slice.
217035e6168fSJeff Roberson 	 */
2171ad1e7d28SJulian Elischer 	if (--ts->ts_slice > 0)
217215dc847eSJeff Roberson 		return;
217335e6168fSJeff Roberson 	/*
217473daf66fSJeff Roberson 	 * We're out of time, force a requeue at userret().
217535e6168fSJeff Roberson 	 */
217673daf66fSJeff Roberson 	ts->ts_slice = sched_slice;
21774a338afdSJulian Elischer 	td->td_flags |= TDF_NEEDRESCHED;
217835e6168fSJeff Roberson }
217935e6168fSJeff Roberson 
2180ae7a6b38SJeff Roberson /*
2181ae7a6b38SJeff Roberson  * Called once per hz tick.  Used for cpu utilization information.  This
2182ae7a6b38SJeff Roberson  * is easier than trying to scale based on stathz.
2183ae7a6b38SJeff Roberson  */
2184ae7a6b38SJeff Roberson void
2185ae7a6b38SJeff Roberson sched_tick(void)
2186ae7a6b38SJeff Roberson {
2187ae7a6b38SJeff Roberson 	struct td_sched *ts;
2188ae7a6b38SJeff Roberson 
2189ae7a6b38SJeff Roberson 	ts = curthread->td_sched;
2190e980fff6SJeff Roberson 	/*
2191e980fff6SJeff Roberson 	 * Ticks is updated asynchronously on a single cpu.  Check here to
2192e980fff6SJeff Roberson 	 * avoid incrementing ts_ticks multiple times in a single tick.
2193e980fff6SJeff Roberson 	 */
2194e980fff6SJeff Roberson 	if (ts->ts_ltick == ticks)
2195e980fff6SJeff Roberson 		return;
2196ae7a6b38SJeff Roberson 	/* Adjust ticks for pctcpu */
2197ae7a6b38SJeff Roberson 	ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
2198ae7a6b38SJeff Roberson 	ts->ts_ltick = ticks;
2199ae7a6b38SJeff Roberson 	/*
2200ae7a6b38SJeff Roberson 	 * Update if we've exceeded our desired tick threshhold by over one
2201ae7a6b38SJeff Roberson 	 * second.
2202ae7a6b38SJeff Roberson 	 */
2203ae7a6b38SJeff Roberson 	if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
2204ae7a6b38SJeff Roberson 		sched_pctcpu_update(ts);
2205ae7a6b38SJeff Roberson }
2206ae7a6b38SJeff Roberson 
2207ae7a6b38SJeff Roberson /*
2208ae7a6b38SJeff Roberson  * Return whether the current CPU has runnable tasks.  Used for in-kernel
2209ae7a6b38SJeff Roberson  * cooperative idle threads.
2210ae7a6b38SJeff Roberson  */
221135e6168fSJeff Roberson int
221235e6168fSJeff Roberson sched_runnable(void)
221335e6168fSJeff Roberson {
2214ad1e7d28SJulian Elischer 	struct tdq *tdq;
2215b90816f1SJeff Roberson 	int load;
221635e6168fSJeff Roberson 
2217b90816f1SJeff Roberson 	load = 1;
2218b90816f1SJeff Roberson 
2219ad1e7d28SJulian Elischer 	tdq = TDQ_SELF();
22203f741ca1SJeff Roberson 	if ((curthread->td_flags & TDF_IDLETD) != 0) {
2221d2ad694cSJeff Roberson 		if (tdq->tdq_load > 0)
22223f741ca1SJeff Roberson 			goto out;
22233f741ca1SJeff Roberson 	} else
2224d2ad694cSJeff Roberson 		if (tdq->tdq_load - 1 > 0)
2225b90816f1SJeff Roberson 			goto out;
2226b90816f1SJeff Roberson 	load = 0;
2227b90816f1SJeff Roberson out:
2228b90816f1SJeff Roberson 	return (load);
222935e6168fSJeff Roberson }
223035e6168fSJeff Roberson 
2231ae7a6b38SJeff Roberson /*
2232ae7a6b38SJeff Roberson  * Choose the highest priority thread to run.  The thread is removed from
2233ae7a6b38SJeff Roberson  * the run-queue while running however the load remains.  For SMP we set
2234ae7a6b38SJeff Roberson  * the tdq in the global idle bitmask if it idles here.
2235ae7a6b38SJeff Roberson  */
22367a5e5e2aSJeff Roberson struct thread *
2237c9f25d8fSJeff Roberson sched_choose(void)
2238c9f25d8fSJeff Roberson {
22399727e637SJeff Roberson 	struct thread *td;
2240ae7a6b38SJeff Roberson 	struct tdq *tdq;
2241ae7a6b38SJeff Roberson 
2242ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2243ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
22449727e637SJeff Roberson 	td = tdq_choose(tdq);
22459727e637SJeff Roberson 	if (td) {
22469727e637SJeff Roberson 		td->td_sched->ts_ltick = ticks;
22479727e637SJeff Roberson 		tdq_runq_rem(tdq, td);
22480502fe2eSJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
22499727e637SJeff Roberson 		return (td);
225035e6168fSJeff Roberson 	}
22510502fe2eSJeff Roberson 	tdq->tdq_lowpri = PRI_MAX_IDLE;
225262fa74d9SJeff Roberson 	return (PCPU_GET(idlethread));
22537a5e5e2aSJeff Roberson }
22547a5e5e2aSJeff Roberson 
2255ae7a6b38SJeff Roberson /*
2256ae7a6b38SJeff Roberson  * Set owepreempt if necessary.  Preemption never happens directly in ULE,
2257ae7a6b38SJeff Roberson  * we always request it once we exit a critical section.
2258ae7a6b38SJeff Roberson  */
2259ae7a6b38SJeff Roberson static inline void
2260ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td)
22617a5e5e2aSJeff Roberson {
22627a5e5e2aSJeff Roberson 	struct thread *ctd;
22637a5e5e2aSJeff Roberson 	int cpri;
22647a5e5e2aSJeff Roberson 	int pri;
22657a5e5e2aSJeff Roberson 
2266ff256d9cSJeff Roberson 	THREAD_LOCK_ASSERT(curthread, MA_OWNED);
2267ff256d9cSJeff Roberson 
22687a5e5e2aSJeff Roberson 	ctd = curthread;
22697a5e5e2aSJeff Roberson 	pri = td->td_priority;
22707a5e5e2aSJeff Roberson 	cpri = ctd->td_priority;
2271ff256d9cSJeff Roberson 	if (pri < cpri)
2272ff256d9cSJeff Roberson 		ctd->td_flags |= TDF_NEEDRESCHED;
22737a5e5e2aSJeff Roberson 	if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
2274ae7a6b38SJeff Roberson 		return;
2275ff256d9cSJeff Roberson 	if (!sched_shouldpreempt(pri, cpri, 0))
2276ae7a6b38SJeff Roberson 		return;
22777a5e5e2aSJeff Roberson 	ctd->td_owepreempt = 1;
227835e6168fSJeff Roberson }
227935e6168fSJeff Roberson 
2280ae7a6b38SJeff Roberson /*
228173daf66fSJeff Roberson  * Add a thread to a thread queue.  Select the appropriate runq and add the
228273daf66fSJeff Roberson  * thread to it.  This is the internal function called when the tdq is
228373daf66fSJeff Roberson  * predetermined.
2284ae7a6b38SJeff Roberson  */
228535e6168fSJeff Roberson void
2286ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags)
228735e6168fSJeff Roberson {
2288c9f25d8fSJeff Roberson 
2289ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
22907a5e5e2aSJeff Roberson 	KASSERT((td->td_inhibitors == 0),
22917a5e5e2aSJeff Roberson 	    ("sched_add: trying to run inhibited thread"));
22927a5e5e2aSJeff Roberson 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
22937a5e5e2aSJeff Roberson 	    ("sched_add: bad thread state"));
2294b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
2295b61ce5b0SJeff Roberson 	    ("sched_add: thread swapped out"));
2296ae7a6b38SJeff Roberson 
2297ae7a6b38SJeff Roberson 	if (td->td_priority < tdq->tdq_lowpri)
2298ae7a6b38SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
22999727e637SJeff Roberson 	tdq_runq_add(tdq, td, flags);
23009727e637SJeff Roberson 	tdq_load_add(tdq, td);
2301ae7a6b38SJeff Roberson }
2302ae7a6b38SJeff Roberson 
2303ae7a6b38SJeff Roberson /*
2304ae7a6b38SJeff Roberson  * Select the target thread queue and add a thread to it.  Request
2305ae7a6b38SJeff Roberson  * preemption or IPI a remote processor if required.
2306ae7a6b38SJeff Roberson  */
2307ae7a6b38SJeff Roberson void
2308ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags)
2309ae7a6b38SJeff Roberson {
2310ae7a6b38SJeff Roberson 	struct tdq *tdq;
23117b8bfa0dSJeff Roberson #ifdef SMP
2312ae7a6b38SJeff Roberson 	int cpu;
2313ae7a6b38SJeff Roberson #endif
23148f51ad55SJeff Roberson 
23158f51ad55SJeff Roberson 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add",
23168f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
23178f51ad55SJeff Roberson 	    sched_tdname(curthread));
23188f51ad55SJeff Roberson 	KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup",
23198f51ad55SJeff Roberson 	    KTR_ATTR_LINKED, sched_tdname(td));
2320ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2321ae7a6b38SJeff Roberson 	/*
2322ae7a6b38SJeff Roberson 	 * Recalculate the priority before we select the target cpu or
2323ae7a6b38SJeff Roberson 	 * run-queue.
2324ae7a6b38SJeff Roberson 	 */
2325ae7a6b38SJeff Roberson 	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
2326ae7a6b38SJeff Roberson 		sched_priority(td);
2327ae7a6b38SJeff Roberson #ifdef SMP
2328ae7a6b38SJeff Roberson 	/*
2329ae7a6b38SJeff Roberson 	 * Pick the destination cpu and if it isn't ours transfer to the
2330ae7a6b38SJeff Roberson 	 * target cpu.
2331ae7a6b38SJeff Roberson 	 */
23329727e637SJeff Roberson 	cpu = sched_pickcpu(td, flags);
23339727e637SJeff Roberson 	tdq = sched_setcpu(td, cpu, flags);
2334ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
233573daf66fSJeff Roberson 	if (cpu != PCPU_GET(cpuid)) {
23369727e637SJeff Roberson 		tdq_notify(tdq, td);
23377b8bfa0dSJeff Roberson 		return;
23387b8bfa0dSJeff Roberson 	}
2339ae7a6b38SJeff Roberson #else
2340ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2341ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
2342ae7a6b38SJeff Roberson 	/*
2343ae7a6b38SJeff Roberson 	 * Now that the thread is moving to the run-queue, set the lock
2344ae7a6b38SJeff Roberson 	 * to the scheduler's lock.
2345ae7a6b38SJeff Roberson 	 */
2346ae7a6b38SJeff Roberson 	thread_lock_set(td, TDQ_LOCKPTR(tdq));
2347ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
23487b8bfa0dSJeff Roberson #endif
2349ae7a6b38SJeff Roberson 	if (!(flags & SRQ_YIELDING))
2350ae7a6b38SJeff Roberson 		sched_setpreempt(td);
235135e6168fSJeff Roberson }
235235e6168fSJeff Roberson 
2353ae7a6b38SJeff Roberson /*
2354ae7a6b38SJeff Roberson  * Remove a thread from a run-queue without running it.  This is used
2355ae7a6b38SJeff Roberson  * when we're stealing a thread from a remote queue.  Otherwise all threads
2356ae7a6b38SJeff Roberson  * exit by calling sched_exit_thread() and sched_throw() themselves.
2357ae7a6b38SJeff Roberson  */
235835e6168fSJeff Roberson void
23597cf90fb3SJeff Roberson sched_rem(struct thread *td)
236035e6168fSJeff Roberson {
2361ad1e7d28SJulian Elischer 	struct tdq *tdq;
23627cf90fb3SJeff Roberson 
23638f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem",
23648f51ad55SJeff Roberson 	    "prio:%d", td->td_priority);
23659727e637SJeff Roberson 	tdq = TDQ_CPU(td->td_sched->ts_cpu);
2366ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2367ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
23687a5e5e2aSJeff Roberson 	KASSERT(TD_ON_RUNQ(td),
2369ad1e7d28SJulian Elischer 	    ("sched_rem: thread not on run queue"));
23709727e637SJeff Roberson 	tdq_runq_rem(tdq, td);
23719727e637SJeff Roberson 	tdq_load_rem(tdq, td);
23727a5e5e2aSJeff Roberson 	TD_SET_CAN_RUN(td);
237362fa74d9SJeff Roberson 	if (td->td_priority == tdq->tdq_lowpri)
237462fa74d9SJeff Roberson 		tdq_setlowpri(tdq, NULL);
237535e6168fSJeff Roberson }
237635e6168fSJeff Roberson 
2377ae7a6b38SJeff Roberson /*
2378ae7a6b38SJeff Roberson  * Fetch cpu utilization information.  Updates on demand.
2379ae7a6b38SJeff Roberson  */
238035e6168fSJeff Roberson fixpt_t
23817cf90fb3SJeff Roberson sched_pctcpu(struct thread *td)
238235e6168fSJeff Roberson {
238335e6168fSJeff Roberson 	fixpt_t pctcpu;
2384ad1e7d28SJulian Elischer 	struct td_sched *ts;
238535e6168fSJeff Roberson 
238635e6168fSJeff Roberson 	pctcpu = 0;
2387ad1e7d28SJulian Elischer 	ts = td->td_sched;
2388ad1e7d28SJulian Elischer 	if (ts == NULL)
2389484288deSJeff Roberson 		return (0);
239035e6168fSJeff Roberson 
23917b20fb19SJeff Roberson 	thread_lock(td);
2392ad1e7d28SJulian Elischer 	if (ts->ts_ticks) {
239335e6168fSJeff Roberson 		int rtick;
239435e6168fSJeff Roberson 
2395ad1e7d28SJulian Elischer 		sched_pctcpu_update(ts);
239635e6168fSJeff Roberson 		/* How many rtick per second ? */
2397e7d50326SJeff Roberson 		rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
2398e7d50326SJeff Roberson 		pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
239935e6168fSJeff Roberson 	}
24007b20fb19SJeff Roberson 	thread_unlock(td);
240135e6168fSJeff Roberson 
240235e6168fSJeff Roberson 	return (pctcpu);
240335e6168fSJeff Roberson }
240435e6168fSJeff Roberson 
240562fa74d9SJeff Roberson /*
240662fa74d9SJeff Roberson  * Enforce affinity settings for a thread.  Called after adjustments to
240762fa74d9SJeff Roberson  * cpumask.
240862fa74d9SJeff Roberson  */
2409885d51a3SJeff Roberson void
2410885d51a3SJeff Roberson sched_affinity(struct thread *td)
2411885d51a3SJeff Roberson {
241262fa74d9SJeff Roberson #ifdef SMP
241362fa74d9SJeff Roberson 	struct td_sched *ts;
241462fa74d9SJeff Roberson 	int cpu;
241562fa74d9SJeff Roberson 
241662fa74d9SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
241762fa74d9SJeff Roberson 	ts = td->td_sched;
241862fa74d9SJeff Roberson 	if (THREAD_CAN_SCHED(td, ts->ts_cpu))
241962fa74d9SJeff Roberson 		return;
242062fa74d9SJeff Roberson 	if (!TD_IS_RUNNING(td))
242162fa74d9SJeff Roberson 		return;
242262fa74d9SJeff Roberson 	td->td_flags |= TDF_NEEDRESCHED;
242362fa74d9SJeff Roberson 	if (!THREAD_CAN_MIGRATE(td))
242462fa74d9SJeff Roberson 		return;
242562fa74d9SJeff Roberson 	/*
242662fa74d9SJeff Roberson 	 * Assign the new cpu and force a switch before returning to
242762fa74d9SJeff Roberson 	 * userspace.  If the target thread is not running locally send
242862fa74d9SJeff Roberson 	 * an ipi to force the issue.
242962fa74d9SJeff Roberson 	 */
243062fa74d9SJeff Roberson 	cpu = ts->ts_cpu;
24319727e637SJeff Roberson 	ts->ts_cpu = sched_pickcpu(td, 0);
243262fa74d9SJeff Roberson 	if (cpu != PCPU_GET(cpuid))
243362fa74d9SJeff Roberson 		ipi_selected(1 << cpu, IPI_PREEMPT);
243462fa74d9SJeff Roberson #endif
2435885d51a3SJeff Roberson }
2436885d51a3SJeff Roberson 
2437ae7a6b38SJeff Roberson /*
2438ae7a6b38SJeff Roberson  * Bind a thread to a target cpu.
2439ae7a6b38SJeff Roberson  */
24409bacd788SJeff Roberson void
24419bacd788SJeff Roberson sched_bind(struct thread *td, int cpu)
24429bacd788SJeff Roberson {
2443ad1e7d28SJulian Elischer 	struct td_sched *ts;
24449bacd788SJeff Roberson 
2445c47f202bSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
2446ad1e7d28SJulian Elischer 	ts = td->td_sched;
24476b2f763fSJeff Roberson 	if (ts->ts_flags & TSF_BOUND)
2448c95d2db2SJeff Roberson 		sched_unbind(td);
2449ad1e7d28SJulian Elischer 	ts->ts_flags |= TSF_BOUND;
24506b2f763fSJeff Roberson 	sched_pin();
245180f86c9fSJeff Roberson 	if (PCPU_GET(cpuid) == cpu)
24529bacd788SJeff Roberson 		return;
24536b2f763fSJeff Roberson 	ts->ts_cpu = cpu;
24549bacd788SJeff Roberson 	/* When we return from mi_switch we'll be on the correct cpu. */
2455279f949eSPoul-Henning Kamp 	mi_switch(SW_VOL, NULL);
24569bacd788SJeff Roberson }
24579bacd788SJeff Roberson 
2458ae7a6b38SJeff Roberson /*
2459ae7a6b38SJeff Roberson  * Release a bound thread.
2460ae7a6b38SJeff Roberson  */
24619bacd788SJeff Roberson void
24629bacd788SJeff Roberson sched_unbind(struct thread *td)
24639bacd788SJeff Roberson {
2464e7d50326SJeff Roberson 	struct td_sched *ts;
2465e7d50326SJeff Roberson 
24667b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2467e7d50326SJeff Roberson 	ts = td->td_sched;
24686b2f763fSJeff Roberson 	if ((ts->ts_flags & TSF_BOUND) == 0)
24696b2f763fSJeff Roberson 		return;
2470e7d50326SJeff Roberson 	ts->ts_flags &= ~TSF_BOUND;
2471e7d50326SJeff Roberson 	sched_unpin();
24729bacd788SJeff Roberson }
24739bacd788SJeff Roberson 
247435e6168fSJeff Roberson int
2475ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td)
2476ebccf1e3SJoseph Koshy {
24777b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2478ad1e7d28SJulian Elischer 	return (td->td_sched->ts_flags & TSF_BOUND);
2479ebccf1e3SJoseph Koshy }
2480ebccf1e3SJoseph Koshy 
2481ae7a6b38SJeff Roberson /*
2482ae7a6b38SJeff Roberson  * Basic yield call.
2483ae7a6b38SJeff Roberson  */
248436ec198bSDavid Xu void
248536ec198bSDavid Xu sched_relinquish(struct thread *td)
248636ec198bSDavid Xu {
24877b20fb19SJeff Roberson 	thread_lock(td);
24888df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
24897b20fb19SJeff Roberson 	thread_unlock(td);
249036ec198bSDavid Xu }
249136ec198bSDavid Xu 
2492ae7a6b38SJeff Roberson /*
2493ae7a6b38SJeff Roberson  * Return the total system load.
2494ae7a6b38SJeff Roberson  */
2495ebccf1e3SJoseph Koshy int
249633916c36SJeff Roberson sched_load(void)
249733916c36SJeff Roberson {
249833916c36SJeff Roberson #ifdef SMP
249933916c36SJeff Roberson 	int total;
250033916c36SJeff Roberson 	int i;
250133916c36SJeff Roberson 
250233916c36SJeff Roberson 	total = 0;
250362fa74d9SJeff Roberson 	for (i = 0; i <= mp_maxid; i++)
250462fa74d9SJeff Roberson 		total += TDQ_CPU(i)->tdq_sysload;
250533916c36SJeff Roberson 	return (total);
250633916c36SJeff Roberson #else
2507d2ad694cSJeff Roberson 	return (TDQ_SELF()->tdq_sysload);
250833916c36SJeff Roberson #endif
250933916c36SJeff Roberson }
251033916c36SJeff Roberson 
251133916c36SJeff Roberson int
251235e6168fSJeff Roberson sched_sizeof_proc(void)
251335e6168fSJeff Roberson {
251435e6168fSJeff Roberson 	return (sizeof(struct proc));
251535e6168fSJeff Roberson }
251635e6168fSJeff Roberson 
251735e6168fSJeff Roberson int
251835e6168fSJeff Roberson sched_sizeof_thread(void)
251935e6168fSJeff Roberson {
252035e6168fSJeff Roberson 	return (sizeof(struct thread) + sizeof(struct td_sched));
252135e6168fSJeff Roberson }
2522b41f1452SDavid Xu 
25237a5e5e2aSJeff Roberson /*
25247a5e5e2aSJeff Roberson  * The actual idle process.
25257a5e5e2aSJeff Roberson  */
25267a5e5e2aSJeff Roberson void
25277a5e5e2aSJeff Roberson sched_idletd(void *dummy)
25287a5e5e2aSJeff Roberson {
25297a5e5e2aSJeff Roberson 	struct thread *td;
2530ae7a6b38SJeff Roberson 	struct tdq *tdq;
25311690c6c1SJeff Roberson 	int switchcnt;
25321690c6c1SJeff Roberson 	int i;
25337a5e5e2aSJeff Roberson 
25347a5e5e2aSJeff Roberson 	td = curthread;
2535ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
25367a5e5e2aSJeff Roberson 	mtx_assert(&Giant, MA_NOTOWNED);
2537ae7a6b38SJeff Roberson 	/* ULE relies on preemption for idle interruption. */
2538ae7a6b38SJeff Roberson 	for (;;) {
25391690c6c1SJeff Roberson 		tdq->tdq_idlestate = TDQ_RUNNING;
2540ae7a6b38SJeff Roberson #ifdef SMP
25411690c6c1SJeff Roberson 		if (tdq_idled(tdq) == 0)
25421690c6c1SJeff Roberson 			continue;
2543ae7a6b38SJeff Roberson #endif
25441690c6c1SJeff Roberson 		switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt;
25451690c6c1SJeff Roberson 		/*
25461690c6c1SJeff Roberson 		 * If we're switching very frequently, spin while checking
25471690c6c1SJeff Roberson 		 * for load rather than entering a low power state that
25481690c6c1SJeff Roberson 		 * requires an IPI.
25491690c6c1SJeff Roberson 		 */
25501690c6c1SJeff Roberson 		if (switchcnt > sched_idlespinthresh) {
25511690c6c1SJeff Roberson 			for (i = 0; i < sched_idlespins; i++) {
25521690c6c1SJeff Roberson 				if (tdq->tdq_load)
25531690c6c1SJeff Roberson 					break;
25541690c6c1SJeff Roberson 				cpu_spinwait();
25551690c6c1SJeff Roberson 			}
25561690c6c1SJeff Roberson 		}
25571690c6c1SJeff Roberson 		/*
25581690c6c1SJeff Roberson 		 * We must set our state to IDLE before checking
25591690c6c1SJeff Roberson 		 * tdq_load for the last time to avoid a race with
25601690c6c1SJeff Roberson 		 * tdq_notify().
25611690c6c1SJeff Roberson 		 */
25621690c6c1SJeff Roberson 		if (tdq->tdq_load == 0) {
25636c47aaaeSJeff Roberson 			switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt;
25641690c6c1SJeff Roberson 			tdq->tdq_idlestate = TDQ_IDLE;
25651690c6c1SJeff Roberson 			if (tdq->tdq_load == 0)
25666c47aaaeSJeff Roberson 				cpu_idle(switchcnt > 1);
25671690c6c1SJeff Roberson 		}
25681690c6c1SJeff Roberson 		if (tdq->tdq_load) {
25691690c6c1SJeff Roberson 			thread_lock(td);
25701690c6c1SJeff Roberson 			mi_switch(SW_VOL | SWT_IDLE, NULL);
25711690c6c1SJeff Roberson 			thread_unlock(td);
25721690c6c1SJeff Roberson 		}
2573ae7a6b38SJeff Roberson 	}
2574b41f1452SDavid Xu }
2575e7d50326SJeff Roberson 
25767b20fb19SJeff Roberson /*
25777b20fb19SJeff Roberson  * A CPU is entering for the first time or a thread is exiting.
25787b20fb19SJeff Roberson  */
25797b20fb19SJeff Roberson void
25807b20fb19SJeff Roberson sched_throw(struct thread *td)
25817b20fb19SJeff Roberson {
258259c68134SJeff Roberson 	struct thread *newtd;
2583ae7a6b38SJeff Roberson 	struct tdq *tdq;
2584ae7a6b38SJeff Roberson 
2585ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
25867b20fb19SJeff Roberson 	if (td == NULL) {
2587ae7a6b38SJeff Roberson 		/* Correct spinlock nesting and acquire the correct lock. */
2588ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
25897b20fb19SJeff Roberson 		spinlock_exit();
25907b20fb19SJeff Roberson 	} else {
2591ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
25929727e637SJeff Roberson 		tdq_load_rem(tdq, td);
2593eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
25947b20fb19SJeff Roberson 	}
25957b20fb19SJeff Roberson 	KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
259659c68134SJeff Roberson 	newtd = choosethread();
259759c68134SJeff Roberson 	TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
25987b20fb19SJeff Roberson 	PCPU_SET(switchtime, cpu_ticks());
25997b20fb19SJeff Roberson 	PCPU_SET(switchticks, ticks);
260059c68134SJeff Roberson 	cpu_throw(td, newtd);		/* doesn't return */
26017b20fb19SJeff Roberson }
26027b20fb19SJeff Roberson 
2603ae7a6b38SJeff Roberson /*
2604ae7a6b38SJeff Roberson  * This is called from fork_exit().  Just acquire the correct locks and
2605ae7a6b38SJeff Roberson  * let fork do the rest of the work.
2606ae7a6b38SJeff Roberson  */
26077b20fb19SJeff Roberson void
2608fe54587fSJeff Roberson sched_fork_exit(struct thread *td)
26097b20fb19SJeff Roberson {
2610ae7a6b38SJeff Roberson 	struct td_sched *ts;
2611ae7a6b38SJeff Roberson 	struct tdq *tdq;
2612ae7a6b38SJeff Roberson 	int cpuid;
26137b20fb19SJeff Roberson 
26147b20fb19SJeff Roberson 	/*
26157b20fb19SJeff Roberson 	 * Finish setting up thread glue so that it begins execution in a
2616ae7a6b38SJeff Roberson 	 * non-nested critical section with the scheduler lock held.
26177b20fb19SJeff Roberson 	 */
2618ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
2619ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
2620ae7a6b38SJeff Roberson 	ts = td->td_sched;
2621ae7a6b38SJeff Roberson 	if (TD_IS_IDLETHREAD(td))
2622ae7a6b38SJeff Roberson 		td->td_lock = TDQ_LOCKPTR(tdq);
2623ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2624ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
262559c68134SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
2626eea4f254SJeff Roberson 	lock_profile_obtain_lock_success(
2627eea4f254SJeff Roberson 	    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
26287b20fb19SJeff Roberson }
26297b20fb19SJeff Roberson 
26308f51ad55SJeff Roberson /*
26318f51ad55SJeff Roberson  * Create on first use to catch odd startup conditons.
26328f51ad55SJeff Roberson  */
26338f51ad55SJeff Roberson char *
26348f51ad55SJeff Roberson sched_tdname(struct thread *td)
26358f51ad55SJeff Roberson {
26368f51ad55SJeff Roberson #ifdef KTR
26378f51ad55SJeff Roberson 	struct td_sched *ts;
26388f51ad55SJeff Roberson 
26398f51ad55SJeff Roberson 	ts = td->td_sched;
26408f51ad55SJeff Roberson 	if (ts->ts_name[0] == '\0')
26418f51ad55SJeff Roberson 		snprintf(ts->ts_name, sizeof(ts->ts_name),
26428f51ad55SJeff Roberson 		    "%s tid %d", td->td_name, td->td_tid);
26438f51ad55SJeff Roberson 	return (ts->ts_name);
26448f51ad55SJeff Roberson #else
26458f51ad55SJeff Roberson 	return (td->td_name);
26468f51ad55SJeff Roberson #endif
26478f51ad55SJeff Roberson }
26488f51ad55SJeff Roberson 
264907095abfSIvan Voras #ifdef SMP
265007095abfSIvan Voras 
265107095abfSIvan Voras /*
265207095abfSIvan Voras  * Build the CPU topology dump string. Is recursively called to collect
265307095abfSIvan Voras  * the topology tree.
265407095abfSIvan Voras  */
265507095abfSIvan Voras static int
265607095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg,
265707095abfSIvan Voras     int indent)
265807095abfSIvan Voras {
265907095abfSIvan Voras 	int i, first;
266007095abfSIvan Voras 
266107095abfSIvan Voras 	sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent,
266207095abfSIvan Voras 	    "", indent, cg->cg_level);
266307095abfSIvan Voras 	sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"0x%x\">", indent, "",
266407095abfSIvan Voras 	    cg->cg_count, cg->cg_mask);
266507095abfSIvan Voras 	first = TRUE;
266607095abfSIvan Voras 	for (i = 0; i < MAXCPU; i++) {
266707095abfSIvan Voras 		if ((cg->cg_mask & (1 << i)) != 0) {
266807095abfSIvan Voras 			if (!first)
266907095abfSIvan Voras 				sbuf_printf(sb, ", ");
267007095abfSIvan Voras 			else
267107095abfSIvan Voras 				first = FALSE;
267207095abfSIvan Voras 			sbuf_printf(sb, "%d", i);
267307095abfSIvan Voras 		}
267407095abfSIvan Voras 	}
267507095abfSIvan Voras 	sbuf_printf(sb, "</cpu>\n");
267607095abfSIvan Voras 
267707095abfSIvan Voras 	sbuf_printf(sb, "%*s <flags>", indent, "");
267807095abfSIvan Voras 	if (cg->cg_flags != 0) {
267907095abfSIvan Voras 		if ((cg->cg_flags & CG_FLAG_HTT) != 0)
268059d95789SIvan Voras 			sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>\n");
268107095abfSIvan Voras 		if ((cg->cg_flags & CG_FLAG_THREAD) != 0)
268259d95789SIvan Voras 			sbuf_printf(sb, "<flag name=\"THREAD\">SMT group</flag>\n");
268307095abfSIvan Voras 	}
268407095abfSIvan Voras 	sbuf_printf(sb, "</flags>\n");
268507095abfSIvan Voras 
268607095abfSIvan Voras 	if (cg->cg_children > 0) {
268707095abfSIvan Voras 		sbuf_printf(sb, "%*s <children>\n", indent, "");
268807095abfSIvan Voras 		for (i = 0; i < cg->cg_children; i++)
268907095abfSIvan Voras 			sysctl_kern_sched_topology_spec_internal(sb,
269007095abfSIvan Voras 			    &cg->cg_child[i], indent+2);
269107095abfSIvan Voras 		sbuf_printf(sb, "%*s </children>\n", indent, "");
269207095abfSIvan Voras 	}
269307095abfSIvan Voras 	sbuf_printf(sb, "%*s</group>\n", indent, "");
269407095abfSIvan Voras 	return (0);
269507095abfSIvan Voras }
269607095abfSIvan Voras 
269707095abfSIvan Voras /*
269807095abfSIvan Voras  * Sysctl handler for retrieving topology dump. It's a wrapper for
269907095abfSIvan Voras  * the recursive sysctl_kern_smp_topology_spec_internal().
270007095abfSIvan Voras  */
270107095abfSIvan Voras static int
270207095abfSIvan Voras sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS)
270307095abfSIvan Voras {
270407095abfSIvan Voras 	struct sbuf *topo;
270507095abfSIvan Voras 	int err;
270607095abfSIvan Voras 
270707095abfSIvan Voras 	KASSERT(cpu_top != NULL, ("cpu_top isn't initialized"));
270807095abfSIvan Voras 
2709aa880b90SIvan Voras 	topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
271007095abfSIvan Voras 	if (topo == NULL)
271107095abfSIvan Voras 		return (ENOMEM);
271207095abfSIvan Voras 
271307095abfSIvan Voras 	sbuf_printf(topo, "<groups>\n");
271407095abfSIvan Voras 	err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1);
271507095abfSIvan Voras 	sbuf_printf(topo, "</groups>\n");
271607095abfSIvan Voras 
271707095abfSIvan Voras 	if (err == 0) {
271807095abfSIvan Voras 		sbuf_finish(topo);
271907095abfSIvan Voras 		err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo));
272007095abfSIvan Voras 	}
272107095abfSIvan Voras 	sbuf_delete(topo);
272207095abfSIvan Voras 	return (err);
272307095abfSIvan Voras }
272407095abfSIvan Voras #endif
272507095abfSIvan Voras 
27269727e637SJeff Roberson SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler");
2727ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0,
2728e7d50326SJeff Roberson     "Scheduler name");
2729ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
2730ae7a6b38SJeff Roberson     "Slice size for timeshare threads");
2731ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
2732ae7a6b38SJeff Roberson      "Interactivity score threshold");
2733ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh,
2734ae7a6b38SJeff Roberson      0,"Min priority for preemption, lower priorities have greater precedence");
2735c5aa6b58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost,
2736c5aa6b58SJeff Roberson      0,"Controls whether static kernel priorities are assigned to sleeping threads.");
27371690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins,
27381690c6c1SJeff Roberson      0,"Number of times idle will spin waiting for new work.");
27391690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh,
27401690c6c1SJeff Roberson      0,"Threshold before we will permit idle spinning.");
27417b8bfa0dSJeff Roberson #ifdef SMP
2742ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
2743ae7a6b38SJeff Roberson     "Number of hz ticks to keep thread affinity for");
2744ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
2745ae7a6b38SJeff Roberson     "Enables the long-term load balancer");
27467fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
27477fcf154aSJeff Roberson     &balance_interval, 0,
27487fcf154aSJeff Roberson     "Average frequency in stathz ticks to run the long-term balancer");
2749ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0,
2750ae7a6b38SJeff Roberson     "Steals work from another hyper-threaded core on idle");
2751ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
2752ae7a6b38SJeff Roberson     "Attempts to steal work from other cores before idling");
275328994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
275428994a58SJeff Roberson     "Minimum load on remote cpu before we'll steal");
275507095abfSIvan Voras 
275607095abfSIvan Voras /* Retrieve SMP topology */
275707095abfSIvan Voras SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING |
275807095abfSIvan Voras     CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A",
275907095abfSIvan Voras     "XML dump of detected CPU topology");
27607b8bfa0dSJeff Roberson #endif
2761e7d50326SJeff Roberson 
276254b0e65fSJeff Roberson /* ps compat.  All cpu percentages from ULE are weighted. */
2763a5423ea3SJeff Roberson static int ccpu = 0;
2764e7d50326SJeff Roberson SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
2765