xref: /freebsd/sys/kern/sched_ule.c (revision c76ee82799a155e1999fc9ae808ed6a99334cef3)
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>
39113dda8aSJeff Roberson __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 
890d2cf837SJeff Roberson #define	TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX)))
900d2cf837SJeff Roberson #define	TDQ_NAME_LEN	(sizeof("sched lock ") + sizeof(__XSTRING(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 	short		tdq_switchcnt;		/* Switches this tick. */
2171690c6c1SJeff Roberson 	short		tdq_oldswitchcnt;	/* Switches last tick. */
21873daf66fSJeff Roberson 	u_char		tdq_lowpri;		/* Lowest priority thread. */
21973daf66fSJeff Roberson 	u_char		tdq_ipipending;		/* IPI pending. */
22073daf66fSJeff Roberson 	u_char		tdq_idx;		/* Current insert index. */
22173daf66fSJeff Roberson 	u_char		tdq_ridx;		/* Current removal index. */
222e7d50326SJeff Roberson 	struct runq	tdq_realtime;		/* real-time run queue. */
223ae7a6b38SJeff Roberson 	struct runq	tdq_timeshare;		/* timeshare run queue. */
224ae7a6b38SJeff Roberson 	struct runq	tdq_idle;		/* Queue of IDLE threads. */
2258f51ad55SJeff Roberson 	char		tdq_name[TDQ_NAME_LEN];
2268f51ad55SJeff Roberson #ifdef KTR
2278f51ad55SJeff Roberson 	char		tdq_loadname[TDQ_LOADNAME_LEN];
2288f51ad55SJeff Roberson #endif
229ae7a6b38SJeff Roberson } __aligned(64);
23035e6168fSJeff Roberson 
2311690c6c1SJeff Roberson /* Idle thread states and config. */
2321690c6c1SJeff Roberson #define	TDQ_RUNNING	1
2331690c6c1SJeff Roberson #define	TDQ_IDLE	2
2347b8bfa0dSJeff Roberson 
23580f86c9fSJeff Roberson #ifdef SMP
23607095abfSIvan Voras struct cpu_group *cpu_top;		/* CPU topology */
2377b8bfa0dSJeff Roberson 
23862fa74d9SJeff Roberson #define	SCHED_AFFINITY_DEFAULT	(max(1, hz / 1000))
23962fa74d9SJeff Roberson #define	SCHED_AFFINITY(ts, t)	((ts)->ts_rltick > ticks - ((t) * affinity))
2407b8bfa0dSJeff Roberson 
2417b8bfa0dSJeff Roberson /*
2427b8bfa0dSJeff Roberson  * Run-time tunables.
2437b8bfa0dSJeff Roberson  */
24428994a58SJeff Roberson static int rebalance = 1;
2457fcf154aSJeff Roberson static int balance_interval = 128;	/* Default set in sched_initticks(). */
2467b8bfa0dSJeff Roberson static int affinity;
2477fcf154aSJeff Roberson static int steal_htt = 1;
24828994a58SJeff Roberson static int steal_idle = 1;
24928994a58SJeff Roberson static int steal_thresh = 2;
25080f86c9fSJeff Roberson 
25135e6168fSJeff Roberson /*
252d2ad694cSJeff Roberson  * One thread queue per processor.
25335e6168fSJeff Roberson  */
254ad1e7d28SJulian Elischer static struct tdq	tdq_cpu[MAXCPU];
2557fcf154aSJeff Roberson static struct tdq	*balance_tdq;
2567fcf154aSJeff Roberson static int balance_ticks;
257dc03363dSJeff Roberson 
258ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu[PCPU_GET(cpuid)])
259ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu[(x)])
260c47f202bSJeff Roberson #define	TDQ_ID(x)	((int)((x) - tdq_cpu))
26180f86c9fSJeff Roberson #else	/* !SMP */
262ad1e7d28SJulian Elischer static struct tdq	tdq_cpu;
263dc03363dSJeff Roberson 
26436b36916SJeff Roberson #define	TDQ_ID(x)	(0)
265ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu)
266ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu)
2670a016a05SJeff Roberson #endif
26835e6168fSJeff Roberson 
269ae7a6b38SJeff Roberson #define	TDQ_LOCK_ASSERT(t, type)	mtx_assert(TDQ_LOCKPTR((t)), (type))
270ae7a6b38SJeff Roberson #define	TDQ_LOCK(t)		mtx_lock_spin(TDQ_LOCKPTR((t)))
271ae7a6b38SJeff Roberson #define	TDQ_LOCK_FLAGS(t, f)	mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
272ae7a6b38SJeff Roberson #define	TDQ_UNLOCK(t)		mtx_unlock_spin(TDQ_LOCKPTR((t)))
27362fa74d9SJeff Roberson #define	TDQ_LOCKPTR(t)		(&(t)->tdq_lock)
274ae7a6b38SJeff Roberson 
2758460a577SJohn Birrell static void sched_priority(struct thread *);
27621381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char);
2778460a577SJohn Birrell static int sched_interact_score(struct thread *);
2788460a577SJohn Birrell static void sched_interact_update(struct thread *);
2798460a577SJohn Birrell static void sched_interact_fork(struct thread *);
280ad1e7d28SJulian Elischer static void sched_pctcpu_update(struct td_sched *);
28135e6168fSJeff Roberson 
2825d7ef00cSJeff Roberson /* Operations on per processor queues */
2839727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *);
284ad1e7d28SJulian Elischer static void tdq_setup(struct tdq *);
2859727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *);
2869727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *);
2879727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int);
2889727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *);
289ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int);
290ad1e7d28SJulian Elischer void tdq_print(int cpu);
291e7d50326SJeff Roberson static void runq_print(struct runq *rq);
292ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int);
2935d7ef00cSJeff Roberson #ifdef SMP
29462fa74d9SJeff Roberson static int tdq_move(struct tdq *, struct tdq *);
295ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *);
2969727e637SJeff Roberson static void tdq_notify(struct tdq *, struct thread *);
2979727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int);
2989727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int);
2999727e637SJeff Roberson static int sched_pickcpu(struct thread *, int);
3007fcf154aSJeff Roberson static void sched_balance(void);
30162fa74d9SJeff Roberson static int sched_balance_pair(struct tdq *, struct tdq *);
3029727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int);
303ae7a6b38SJeff Roberson static inline struct mtx *thread_block_switch(struct thread *);
304ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *);
305c47f202bSJeff Roberson static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int);
30607095abfSIvan Voras static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS);
30707095abfSIvan Voras static int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb,
30807095abfSIvan Voras     struct cpu_group *cg, int indent);
3095d7ef00cSJeff Roberson #endif
3105d7ef00cSJeff Roberson 
311e7d50326SJeff Roberson static void sched_setup(void *dummy);
312237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
313e7d50326SJeff Roberson 
314e7d50326SJeff Roberson static void sched_initticks(void *dummy);
315237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks,
316237fdd78SRobert Watson     NULL);
317e7d50326SJeff Roberson 
318ae7a6b38SJeff Roberson /*
319ae7a6b38SJeff Roberson  * Print the threads waiting on a run-queue.
320ae7a6b38SJeff Roberson  */
321e7d50326SJeff Roberson static void
322e7d50326SJeff Roberson runq_print(struct runq *rq)
323e7d50326SJeff Roberson {
324e7d50326SJeff Roberson 	struct rqhead *rqh;
3259727e637SJeff Roberson 	struct thread *td;
326e7d50326SJeff Roberson 	int pri;
327e7d50326SJeff Roberson 	int j;
328e7d50326SJeff Roberson 	int i;
329e7d50326SJeff Roberson 
330e7d50326SJeff Roberson 	for (i = 0; i < RQB_LEN; i++) {
331e7d50326SJeff Roberson 		printf("\t\trunq bits %d 0x%zx\n",
332e7d50326SJeff Roberson 		    i, rq->rq_status.rqb_bits[i]);
333e7d50326SJeff Roberson 		for (j = 0; j < RQB_BPW; j++)
334e7d50326SJeff Roberson 			if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
335e7d50326SJeff Roberson 				pri = j + (i << RQB_L2BPW);
336e7d50326SJeff Roberson 				rqh = &rq->rq_queues[pri];
3379727e637SJeff Roberson 				TAILQ_FOREACH(td, rqh, td_runq) {
338e7d50326SJeff Roberson 					printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
3399727e637SJeff Roberson 					    td, td->td_name, td->td_priority,
3409727e637SJeff Roberson 					    td->td_rqindex, pri);
341e7d50326SJeff Roberson 				}
342e7d50326SJeff Roberson 			}
343e7d50326SJeff Roberson 	}
344e7d50326SJeff Roberson }
345e7d50326SJeff Roberson 
346ae7a6b38SJeff Roberson /*
347ae7a6b38SJeff Roberson  * Print the status of a per-cpu thread queue.  Should be a ddb show cmd.
348ae7a6b38SJeff Roberson  */
34915dc847eSJeff Roberson void
350ad1e7d28SJulian Elischer tdq_print(int cpu)
35115dc847eSJeff Roberson {
352ad1e7d28SJulian Elischer 	struct tdq *tdq;
35315dc847eSJeff Roberson 
354ad1e7d28SJulian Elischer 	tdq = TDQ_CPU(cpu);
35515dc847eSJeff Roberson 
356c47f202bSJeff Roberson 	printf("tdq %d:\n", TDQ_ID(tdq));
35762fa74d9SJeff Roberson 	printf("\tlock            %p\n", TDQ_LOCKPTR(tdq));
35862fa74d9SJeff Roberson 	printf("\tLock name:      %s\n", tdq->tdq_name);
359d2ad694cSJeff Roberson 	printf("\tload:           %d\n", tdq->tdq_load);
3601690c6c1SJeff Roberson 	printf("\tswitch cnt:     %d\n", tdq->tdq_switchcnt);
3611690c6c1SJeff Roberson 	printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt);
362e7d50326SJeff Roberson 	printf("\ttimeshare idx:  %d\n", tdq->tdq_idx);
3633f872f85SJeff Roberson 	printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
3641690c6c1SJeff Roberson 	printf("\tload transferable: %d\n", tdq->tdq_transferable);
3651690c6c1SJeff Roberson 	printf("\tlowest priority:   %d\n", tdq->tdq_lowpri);
366e7d50326SJeff Roberson 	printf("\trealtime runq:\n");
367e7d50326SJeff Roberson 	runq_print(&tdq->tdq_realtime);
368e7d50326SJeff Roberson 	printf("\ttimeshare runq:\n");
369e7d50326SJeff Roberson 	runq_print(&tdq->tdq_timeshare);
370e7d50326SJeff Roberson 	printf("\tidle runq:\n");
371e7d50326SJeff Roberson 	runq_print(&tdq->tdq_idle);
37215dc847eSJeff Roberson }
37315dc847eSJeff Roberson 
374ff256d9cSJeff Roberson static inline int
375ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote)
376ff256d9cSJeff Roberson {
377ff256d9cSJeff Roberson 	/*
378ff256d9cSJeff Roberson 	 * If the new priority is not better than the current priority there is
379ff256d9cSJeff Roberson 	 * nothing to do.
380ff256d9cSJeff Roberson 	 */
381ff256d9cSJeff Roberson 	if (pri >= cpri)
382ff256d9cSJeff Roberson 		return (0);
383ff256d9cSJeff Roberson 	/*
384ff256d9cSJeff Roberson 	 * Always preempt idle.
385ff256d9cSJeff Roberson 	 */
386ff256d9cSJeff Roberson 	if (cpri >= PRI_MIN_IDLE)
387ff256d9cSJeff Roberson 		return (1);
388ff256d9cSJeff Roberson 	/*
389ff256d9cSJeff Roberson 	 * If preemption is disabled don't preempt others.
390ff256d9cSJeff Roberson 	 */
391ff256d9cSJeff Roberson 	if (preempt_thresh == 0)
392ff256d9cSJeff Roberson 		return (0);
393ff256d9cSJeff Roberson 	/*
394ff256d9cSJeff Roberson 	 * Preempt if we exceed the threshold.
395ff256d9cSJeff Roberson 	 */
396ff256d9cSJeff Roberson 	if (pri <= preempt_thresh)
397ff256d9cSJeff Roberson 		return (1);
398ff256d9cSJeff Roberson 	/*
399ff256d9cSJeff Roberson 	 * If we're realtime or better and there is timeshare or worse running
400ff256d9cSJeff Roberson 	 * preempt only remote processors.
401ff256d9cSJeff Roberson 	 */
402ff256d9cSJeff Roberson 	if (remote && pri <= PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
403ff256d9cSJeff Roberson 		return (1);
404ff256d9cSJeff Roberson 	return (0);
405ff256d9cSJeff Roberson }
406ff256d9cSJeff Roberson 
407ae7a6b38SJeff Roberson #define	TS_RQ_PPQ	(((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS)
408ae7a6b38SJeff Roberson /*
409ae7a6b38SJeff Roberson  * Add a thread to the actual run-queue.  Keeps transferable counts up to
410ae7a6b38SJeff Roberson  * date with what is actually on the run-queue.  Selects the correct
411ae7a6b38SJeff Roberson  * queue position for timeshare threads.
412ae7a6b38SJeff Roberson  */
413155b9987SJeff Roberson static __inline void
4149727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags)
415155b9987SJeff Roberson {
4169727e637SJeff Roberson 	struct td_sched *ts;
417c143ac21SJeff Roberson 	u_char pri;
418c143ac21SJeff Roberson 
419ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
4209727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
42173daf66fSJeff Roberson 
4229727e637SJeff Roberson 	pri = td->td_priority;
4239727e637SJeff Roberson 	ts = td->td_sched;
4249727e637SJeff Roberson 	TD_SET_RUNQ(td);
4259727e637SJeff Roberson 	if (THREAD_CAN_MIGRATE(td)) {
426d2ad694cSJeff Roberson 		tdq->tdq_transferable++;
427ad1e7d28SJulian Elischer 		ts->ts_flags |= TSF_XFERABLE;
42880f86c9fSJeff Roberson 	}
429c143ac21SJeff Roberson 	if (pri <= PRI_MAX_REALTIME) {
430c143ac21SJeff Roberson 		ts->ts_runq = &tdq->tdq_realtime;
431c143ac21SJeff Roberson 	} else if (pri <= PRI_MAX_TIMESHARE) {
432c143ac21SJeff Roberson 		ts->ts_runq = &tdq->tdq_timeshare;
433e7d50326SJeff Roberson 		KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
434e7d50326SJeff Roberson 			("Invalid priority %d on timeshare runq", pri));
435e7d50326SJeff Roberson 		/*
436e7d50326SJeff Roberson 		 * This queue contains only priorities between MIN and MAX
437e7d50326SJeff Roberson 		 * realtime.  Use the whole queue to represent these values.
438e7d50326SJeff Roberson 		 */
439c47f202bSJeff Roberson 		if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
440e7d50326SJeff Roberson 			pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
441e7d50326SJeff Roberson 			pri = (pri + tdq->tdq_idx) % RQ_NQS;
4423f872f85SJeff Roberson 			/*
4433f872f85SJeff Roberson 			 * This effectively shortens the queue by one so we
4443f872f85SJeff Roberson 			 * can have a one slot difference between idx and
4453f872f85SJeff Roberson 			 * ridx while we wait for threads to drain.
4463f872f85SJeff Roberson 			 */
4473f872f85SJeff Roberson 			if (tdq->tdq_ridx != tdq->tdq_idx &&
4483f872f85SJeff Roberson 			    pri == tdq->tdq_ridx)
4494499aff6SJeff Roberson 				pri = (unsigned char)(pri - 1) % RQ_NQS;
450e7d50326SJeff Roberson 		} else
4513f872f85SJeff Roberson 			pri = tdq->tdq_ridx;
4529727e637SJeff Roberson 		runq_add_pri(ts->ts_runq, td, pri, flags);
453c143ac21SJeff Roberson 		return;
454e7d50326SJeff Roberson 	} else
45573daf66fSJeff Roberson 		ts->ts_runq = &tdq->tdq_idle;
4569727e637SJeff Roberson 	runq_add(ts->ts_runq, td, flags);
45773daf66fSJeff Roberson }
45873daf66fSJeff Roberson 
45973daf66fSJeff Roberson /*
460ae7a6b38SJeff Roberson  * Remove a thread from a run-queue.  This typically happens when a thread
461ae7a6b38SJeff Roberson  * is selected to run.  Running threads are not on the queue and the
462ae7a6b38SJeff Roberson  * transferable count does not reflect them.
463ae7a6b38SJeff Roberson  */
464155b9987SJeff Roberson static __inline void
4659727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td)
466155b9987SJeff Roberson {
4679727e637SJeff Roberson 	struct td_sched *ts;
4689727e637SJeff Roberson 
4699727e637SJeff Roberson 	ts = td->td_sched;
470ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
471ae7a6b38SJeff Roberson 	KASSERT(ts->ts_runq != NULL,
4729727e637SJeff Roberson 	    ("tdq_runq_remove: thread %p null ts_runq", td));
473ad1e7d28SJulian Elischer 	if (ts->ts_flags & TSF_XFERABLE) {
474d2ad694cSJeff Roberson 		tdq->tdq_transferable--;
475ad1e7d28SJulian Elischer 		ts->ts_flags &= ~TSF_XFERABLE;
47680f86c9fSJeff Roberson 	}
4773f872f85SJeff Roberson 	if (ts->ts_runq == &tdq->tdq_timeshare) {
4783f872f85SJeff Roberson 		if (tdq->tdq_idx != tdq->tdq_ridx)
4799727e637SJeff Roberson 			runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx);
480e7d50326SJeff Roberson 		else
4819727e637SJeff Roberson 			runq_remove_idx(ts->ts_runq, td, NULL);
4823f872f85SJeff Roberson 	} else
4839727e637SJeff Roberson 		runq_remove(ts->ts_runq, td);
484155b9987SJeff Roberson }
485155b9987SJeff Roberson 
486ae7a6b38SJeff Roberson /*
487ae7a6b38SJeff Roberson  * Load is maintained for all threads RUNNING and ON_RUNQ.  Add the load
488ae7a6b38SJeff Roberson  * for this thread to the referenced thread queue.
489ae7a6b38SJeff Roberson  */
490a8949de2SJeff Roberson static void
4919727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td)
4925d7ef00cSJeff Roberson {
493ae7a6b38SJeff Roberson 
494ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
4959727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
49603d17db7SJeff Roberson 
497d2ad694cSJeff Roberson 	tdq->tdq_load++;
49803d17db7SJeff Roberson 	if ((td->td_proc->p_flag & P_NOLOAD) == 0)
499d2ad694cSJeff Roberson 		tdq->tdq_sysload++;
5008f51ad55SJeff Roberson 	KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load);
5015d7ef00cSJeff Roberson }
50215dc847eSJeff Roberson 
503ae7a6b38SJeff Roberson /*
504ae7a6b38SJeff Roberson  * Remove the load from a thread that is transitioning to a sleep state or
505ae7a6b38SJeff Roberson  * exiting.
506ae7a6b38SJeff Roberson  */
507a8949de2SJeff Roberson static void
5089727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td)
5095d7ef00cSJeff Roberson {
510ae7a6b38SJeff Roberson 
5119727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
512ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
513ae7a6b38SJeff Roberson 	KASSERT(tdq->tdq_load != 0,
514c47f202bSJeff Roberson 	    ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq)));
51503d17db7SJeff Roberson 
516d2ad694cSJeff Roberson 	tdq->tdq_load--;
51703d17db7SJeff Roberson 	if ((td->td_proc->p_flag & P_NOLOAD) == 0)
51803d17db7SJeff Roberson 		tdq->tdq_sysload--;
5198f51ad55SJeff Roberson 	KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load);
52015dc847eSJeff Roberson }
52115dc847eSJeff Roberson 
522356500a3SJeff Roberson /*
52362fa74d9SJeff Roberson  * Set lowpri to its exact value by searching the run-queue and
52462fa74d9SJeff Roberson  * evaluating curthread.  curthread may be passed as an optimization.
525356500a3SJeff Roberson  */
52622bf7d9aSJeff Roberson static void
52762fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd)
52862fa74d9SJeff Roberson {
52962fa74d9SJeff Roberson 	struct thread *td;
53062fa74d9SJeff Roberson 
53162fa74d9SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
53262fa74d9SJeff Roberson 	if (ctd == NULL)
53362fa74d9SJeff Roberson 		ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread;
5349727e637SJeff Roberson 	td = tdq_choose(tdq);
5359727e637SJeff Roberson 	if (td == NULL || td->td_priority > ctd->td_priority)
53662fa74d9SJeff Roberson 		tdq->tdq_lowpri = ctd->td_priority;
53762fa74d9SJeff Roberson 	else
53862fa74d9SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
53962fa74d9SJeff Roberson }
54062fa74d9SJeff Roberson 
54162fa74d9SJeff Roberson #ifdef SMP
54262fa74d9SJeff Roberson struct cpu_search {
543c76ee827SJeff Roberson 	cpuset_t cs_mask;
54462fa74d9SJeff Roberson 	u_int	cs_load;
54562fa74d9SJeff Roberson 	u_int	cs_cpu;
54662fa74d9SJeff Roberson 	int	cs_limit;	/* Min priority for low min load for high. */
54762fa74d9SJeff Roberson };
54862fa74d9SJeff Roberson 
54962fa74d9SJeff Roberson #define	CPU_SEARCH_LOWEST	0x1
55062fa74d9SJeff Roberson #define	CPU_SEARCH_HIGHEST	0x2
55162fa74d9SJeff Roberson #define	CPU_SEARCH_BOTH		(CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST)
55262fa74d9SJeff Roberson 
553c76ee827SJeff Roberson #define	CPUSET_FOREACH(cpu, mask)				\
554c76ee827SJeff Roberson 	for ((cpu) = 0; (cpu) <= mp_maxid; (cpu)++)		\
55562fa74d9SJeff Roberson 		if ((mask) & 1 << (cpu))
55662fa74d9SJeff Roberson 
557d628fbfaSJohn Baldwin static __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low,
55862fa74d9SJeff Roberson     struct cpu_search *high, const int match);
55962fa74d9SJeff Roberson int cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low);
56062fa74d9SJeff Roberson int cpu_search_highest(struct cpu_group *cg, struct cpu_search *high);
56162fa74d9SJeff Roberson int cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
56262fa74d9SJeff Roberson     struct cpu_search *high);
56362fa74d9SJeff Roberson 
56462fa74d9SJeff Roberson /*
56562fa74d9SJeff Roberson  * This routine compares according to the match argument and should be
56662fa74d9SJeff Roberson  * reduced in actual instantiations via constant propagation and dead code
56762fa74d9SJeff Roberson  * elimination.
56862fa74d9SJeff Roberson  */
56962fa74d9SJeff Roberson static __inline int
57062fa74d9SJeff Roberson cpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high,
57162fa74d9SJeff Roberson     const int match)
57262fa74d9SJeff Roberson {
57362fa74d9SJeff Roberson 	struct tdq *tdq;
57462fa74d9SJeff Roberson 
57562fa74d9SJeff Roberson 	tdq = TDQ_CPU(cpu);
57662fa74d9SJeff Roberson 	if (match & CPU_SEARCH_LOWEST)
577c76ee827SJeff Roberson 		if (CPU_ISSET(cpu, &low->cs_mask) &&
57862fa74d9SJeff Roberson 		    tdq->tdq_load < low->cs_load &&
57962fa74d9SJeff Roberson 		    tdq->tdq_lowpri > low->cs_limit) {
58062fa74d9SJeff Roberson 			low->cs_cpu = cpu;
58162fa74d9SJeff Roberson 			low->cs_load = tdq->tdq_load;
58262fa74d9SJeff Roberson 		}
58362fa74d9SJeff Roberson 	if (match & CPU_SEARCH_HIGHEST)
584c76ee827SJeff Roberson 		if (CPU_ISSET(cpu, &high->cs_mask) &&
58562fa74d9SJeff Roberson 		    tdq->tdq_load >= high->cs_limit &&
58662fa74d9SJeff Roberson 		    tdq->tdq_load > high->cs_load &&
58762fa74d9SJeff Roberson 		    tdq->tdq_transferable) {
58862fa74d9SJeff Roberson 			high->cs_cpu = cpu;
58962fa74d9SJeff Roberson 			high->cs_load = tdq->tdq_load;
59062fa74d9SJeff Roberson 		}
59162fa74d9SJeff Roberson 	return (tdq->tdq_load);
59262fa74d9SJeff Roberson }
59362fa74d9SJeff Roberson 
59462fa74d9SJeff Roberson /*
59562fa74d9SJeff Roberson  * Search the tree of cpu_groups for the lowest or highest loaded cpu
59662fa74d9SJeff Roberson  * according to the match argument.  This routine actually compares the
59762fa74d9SJeff Roberson  * load on all paths through the tree and finds the least loaded cpu on
59862fa74d9SJeff Roberson  * the least loaded path, which may differ from the least loaded cpu in
59962fa74d9SJeff Roberson  * the system.  This balances work among caches and busses.
60062fa74d9SJeff Roberson  *
60162fa74d9SJeff Roberson  * This inline is instantiated in three forms below using constants for the
60262fa74d9SJeff Roberson  * match argument.  It is reduced to the minimum set for each case.  It is
60362fa74d9SJeff Roberson  * also recursive to the depth of the tree.
60462fa74d9SJeff Roberson  */
605d628fbfaSJohn Baldwin static __inline int
60662fa74d9SJeff Roberson cpu_search(struct cpu_group *cg, struct cpu_search *low,
60762fa74d9SJeff Roberson     struct cpu_search *high, const int match)
60862fa74d9SJeff Roberson {
60962fa74d9SJeff Roberson 	int total;
61062fa74d9SJeff Roberson 
61162fa74d9SJeff Roberson 	total = 0;
61262fa74d9SJeff Roberson 	if (cg->cg_children) {
61362fa74d9SJeff Roberson 		struct cpu_search lgroup;
61462fa74d9SJeff Roberson 		struct cpu_search hgroup;
61562fa74d9SJeff Roberson 		struct cpu_group *child;
61662fa74d9SJeff Roberson 		u_int lload;
61762fa74d9SJeff Roberson 		int hload;
61862fa74d9SJeff Roberson 		int load;
61962fa74d9SJeff Roberson 		int i;
62062fa74d9SJeff Roberson 
62162fa74d9SJeff Roberson 		lload = -1;
62262fa74d9SJeff Roberson 		hload = -1;
62362fa74d9SJeff Roberson 		for (i = 0; i < cg->cg_children; i++) {
62462fa74d9SJeff Roberson 			child = &cg->cg_child[i];
62562fa74d9SJeff Roberson 			if (match & CPU_SEARCH_LOWEST) {
62662fa74d9SJeff Roberson 				lgroup = *low;
62762fa74d9SJeff Roberson 				lgroup.cs_load = -1;
62862fa74d9SJeff Roberson 			}
62962fa74d9SJeff Roberson 			if (match & CPU_SEARCH_HIGHEST) {
63062fa74d9SJeff Roberson 				hgroup = *high;
63162fa74d9SJeff Roberson 				lgroup.cs_load = 0;
63262fa74d9SJeff Roberson 			}
63362fa74d9SJeff Roberson 			switch (match) {
63462fa74d9SJeff Roberson 			case CPU_SEARCH_LOWEST:
63562fa74d9SJeff Roberson 				load = cpu_search_lowest(child, &lgroup);
63662fa74d9SJeff Roberson 				break;
63762fa74d9SJeff Roberson 			case CPU_SEARCH_HIGHEST:
63862fa74d9SJeff Roberson 				load = cpu_search_highest(child, &hgroup);
63962fa74d9SJeff Roberson 				break;
64062fa74d9SJeff Roberson 			case CPU_SEARCH_BOTH:
64162fa74d9SJeff Roberson 				load = cpu_search_both(child, &lgroup, &hgroup);
64262fa74d9SJeff Roberson 				break;
64362fa74d9SJeff Roberson 			}
64462fa74d9SJeff Roberson 			total += load;
64562fa74d9SJeff Roberson 			if (match & CPU_SEARCH_LOWEST)
64662fa74d9SJeff Roberson 				if (load < lload || low->cs_cpu == -1) {
64762fa74d9SJeff Roberson 					*low = lgroup;
64862fa74d9SJeff Roberson 					lload = load;
64962fa74d9SJeff Roberson 				}
65062fa74d9SJeff Roberson 			if (match & CPU_SEARCH_HIGHEST)
65162fa74d9SJeff Roberson 				if (load > hload || high->cs_cpu == -1) {
65262fa74d9SJeff Roberson 					hload = load;
65362fa74d9SJeff Roberson 					*high = hgroup;
65462fa74d9SJeff Roberson 				}
65562fa74d9SJeff Roberson 		}
65662fa74d9SJeff Roberson 	} else {
65762fa74d9SJeff Roberson 		int cpu;
65862fa74d9SJeff Roberson 
659c76ee827SJeff Roberson 		CPUSET_FOREACH(cpu, cg->cg_mask)
66062fa74d9SJeff Roberson 			total += cpu_compare(cpu, low, high, match);
66162fa74d9SJeff Roberson 	}
66262fa74d9SJeff Roberson 	return (total);
66362fa74d9SJeff Roberson }
66462fa74d9SJeff Roberson 
66562fa74d9SJeff Roberson /*
66662fa74d9SJeff Roberson  * cpu_search instantiations must pass constants to maintain the inline
66762fa74d9SJeff Roberson  * optimization.
66862fa74d9SJeff Roberson  */
66962fa74d9SJeff Roberson int
67062fa74d9SJeff Roberson cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low)
67162fa74d9SJeff Roberson {
67262fa74d9SJeff Roberson 	return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST);
67362fa74d9SJeff Roberson }
67462fa74d9SJeff Roberson 
67562fa74d9SJeff Roberson int
67662fa74d9SJeff Roberson cpu_search_highest(struct cpu_group *cg, struct cpu_search *high)
67762fa74d9SJeff Roberson {
67862fa74d9SJeff Roberson 	return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST);
67962fa74d9SJeff Roberson }
68062fa74d9SJeff Roberson 
68162fa74d9SJeff Roberson int
68262fa74d9SJeff Roberson cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
68362fa74d9SJeff Roberson     struct cpu_search *high)
68462fa74d9SJeff Roberson {
68562fa74d9SJeff Roberson 	return cpu_search(cg, low, high, CPU_SEARCH_BOTH);
68662fa74d9SJeff Roberson }
68762fa74d9SJeff Roberson 
68862fa74d9SJeff Roberson /*
68962fa74d9SJeff Roberson  * Find the cpu with the least load via the least loaded path that has a
69062fa74d9SJeff Roberson  * lowpri greater than pri  pri.  A pri of -1 indicates any priority is
69162fa74d9SJeff Roberson  * acceptable.
69262fa74d9SJeff Roberson  */
69362fa74d9SJeff Roberson static inline int
694c76ee827SJeff Roberson sched_lowest(struct cpu_group *cg, cpuset_t mask, int pri)
69562fa74d9SJeff Roberson {
69662fa74d9SJeff Roberson 	struct cpu_search low;
69762fa74d9SJeff Roberson 
69862fa74d9SJeff Roberson 	low.cs_cpu = -1;
69962fa74d9SJeff Roberson 	low.cs_load = -1;
70062fa74d9SJeff Roberson 	low.cs_mask = mask;
70162fa74d9SJeff Roberson 	low.cs_limit = pri;
70262fa74d9SJeff Roberson 	cpu_search_lowest(cg, &low);
70362fa74d9SJeff Roberson 	return low.cs_cpu;
70462fa74d9SJeff Roberson }
70562fa74d9SJeff Roberson 
70662fa74d9SJeff Roberson /*
70762fa74d9SJeff Roberson  * Find the cpu with the highest load via the highest loaded path.
70862fa74d9SJeff Roberson  */
70962fa74d9SJeff Roberson static inline int
710c76ee827SJeff Roberson sched_highest(struct cpu_group *cg, cpuset_t mask, int minload)
71162fa74d9SJeff Roberson {
71262fa74d9SJeff Roberson 	struct cpu_search high;
71362fa74d9SJeff Roberson 
71462fa74d9SJeff Roberson 	high.cs_cpu = -1;
71562fa74d9SJeff Roberson 	high.cs_load = 0;
71662fa74d9SJeff Roberson 	high.cs_mask = mask;
71762fa74d9SJeff Roberson 	high.cs_limit = minload;
71862fa74d9SJeff Roberson 	cpu_search_highest(cg, &high);
71962fa74d9SJeff Roberson 	return high.cs_cpu;
72062fa74d9SJeff Roberson }
72162fa74d9SJeff Roberson 
72262fa74d9SJeff Roberson /*
72362fa74d9SJeff Roberson  * Simultaneously find the highest and lowest loaded cpu reachable via
72462fa74d9SJeff Roberson  * cg.
72562fa74d9SJeff Roberson  */
72662fa74d9SJeff Roberson static inline void
727c76ee827SJeff Roberson sched_both(struct cpu_group *cg, cpuset_t mask, int *lowcpu, int *highcpu)
72862fa74d9SJeff Roberson {
72962fa74d9SJeff Roberson 	struct cpu_search high;
73062fa74d9SJeff Roberson 	struct cpu_search low;
73162fa74d9SJeff Roberson 
73262fa74d9SJeff Roberson 	low.cs_cpu = -1;
73362fa74d9SJeff Roberson 	low.cs_limit = -1;
73462fa74d9SJeff Roberson 	low.cs_load = -1;
73562fa74d9SJeff Roberson 	low.cs_mask = mask;
73662fa74d9SJeff Roberson 	high.cs_load = 0;
73762fa74d9SJeff Roberson 	high.cs_cpu = -1;
73862fa74d9SJeff Roberson 	high.cs_limit = -1;
73962fa74d9SJeff Roberson 	high.cs_mask = mask;
74062fa74d9SJeff Roberson 	cpu_search_both(cg, &low, &high);
74162fa74d9SJeff Roberson 	*lowcpu = low.cs_cpu;
74262fa74d9SJeff Roberson 	*highcpu = high.cs_cpu;
74362fa74d9SJeff Roberson 	return;
74462fa74d9SJeff Roberson }
74562fa74d9SJeff Roberson 
74662fa74d9SJeff Roberson static void
74762fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg)
74862fa74d9SJeff Roberson {
749c76ee827SJeff Roberson 	cpuset_t mask;
75062fa74d9SJeff Roberson 	int high;
75162fa74d9SJeff Roberson 	int low;
75262fa74d9SJeff Roberson 	int i;
75362fa74d9SJeff Roberson 
754c76ee827SJeff Roberson 	CPU_FILL(&mask);
75562fa74d9SJeff Roberson 	for (;;) {
75662fa74d9SJeff Roberson 		sched_both(cg, mask, &low, &high);
75762fa74d9SJeff Roberson 		if (low == high || low == -1 || high == -1)
75862fa74d9SJeff Roberson 			break;
75962fa74d9SJeff Roberson 		if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low)))
76062fa74d9SJeff Roberson 			break;
76162fa74d9SJeff Roberson 		/*
76262fa74d9SJeff Roberson 		 * If we failed to move any threads determine which cpu
76362fa74d9SJeff Roberson 		 * to kick out of the set and try again.
76462fa74d9SJeff Roberson 	 	 */
76562fa74d9SJeff Roberson 		if (TDQ_CPU(high)->tdq_transferable == 0)
766c76ee827SJeff Roberson 			CPU_CLR(high, &mask);
76762fa74d9SJeff Roberson 		else
768c76ee827SJeff Roberson 			CPU_CLR(low, &mask);
76962fa74d9SJeff Roberson 	}
77062fa74d9SJeff Roberson 
77162fa74d9SJeff Roberson 	for (i = 0; i < cg->cg_children; i++)
77262fa74d9SJeff Roberson 		sched_balance_group(&cg->cg_child[i]);
77362fa74d9SJeff Roberson }
77462fa74d9SJeff Roberson 
77562fa74d9SJeff Roberson static void
7767fcf154aSJeff Roberson sched_balance()
777356500a3SJeff Roberson {
7787fcf154aSJeff Roberson 	struct tdq *tdq;
779356500a3SJeff Roberson 
7807fcf154aSJeff Roberson 	/*
7817fcf154aSJeff Roberson 	 * Select a random time between .5 * balance_interval and
7827fcf154aSJeff Roberson 	 * 1.5 * balance_interval.
7837fcf154aSJeff Roberson 	 */
7847fcf154aSJeff Roberson 	balance_ticks = max(balance_interval / 2, 1);
7857fcf154aSJeff Roberson 	balance_ticks += random() % balance_interval;
786ae7a6b38SJeff Roberson 	if (smp_started == 0 || rebalance == 0)
787598b368dSJeff Roberson 		return;
7887fcf154aSJeff Roberson 	tdq = TDQ_SELF();
7897fcf154aSJeff Roberson 	TDQ_UNLOCK(tdq);
79062fa74d9SJeff Roberson 	sched_balance_group(cpu_top);
7917fcf154aSJeff Roberson 	TDQ_LOCK(tdq);
792cac77d04SJeff Roberson }
79386f8ae96SJeff Roberson 
794ae7a6b38SJeff Roberson /*
795ae7a6b38SJeff Roberson  * Lock two thread queues using their address to maintain lock order.
796ae7a6b38SJeff Roberson  */
797ae7a6b38SJeff Roberson static void
798ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two)
799ae7a6b38SJeff Roberson {
800ae7a6b38SJeff Roberson 	if (one < two) {
801ae7a6b38SJeff Roberson 		TDQ_LOCK(one);
802ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(two, MTX_DUPOK);
803ae7a6b38SJeff Roberson 	} else {
804ae7a6b38SJeff Roberson 		TDQ_LOCK(two);
805ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(one, MTX_DUPOK);
806ae7a6b38SJeff Roberson 	}
807ae7a6b38SJeff Roberson }
808ae7a6b38SJeff Roberson 
809ae7a6b38SJeff Roberson /*
8107fcf154aSJeff Roberson  * Unlock two thread queues.  Order is not important here.
8117fcf154aSJeff Roberson  */
8127fcf154aSJeff Roberson static void
8137fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two)
8147fcf154aSJeff Roberson {
8157fcf154aSJeff Roberson 	TDQ_UNLOCK(one);
8167fcf154aSJeff Roberson 	TDQ_UNLOCK(two);
8177fcf154aSJeff Roberson }
8187fcf154aSJeff Roberson 
8197fcf154aSJeff Roberson /*
820ae7a6b38SJeff Roberson  * Transfer load between two imbalanced thread queues.
821ae7a6b38SJeff Roberson  */
82262fa74d9SJeff Roberson static int
823ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low)
824cac77d04SJeff Roberson {
825cac77d04SJeff Roberson 	int transferable;
826cac77d04SJeff Roberson 	int high_load;
827cac77d04SJeff Roberson 	int low_load;
82862fa74d9SJeff Roberson 	int moved;
829cac77d04SJeff Roberson 	int move;
830cac77d04SJeff Roberson 	int diff;
831cac77d04SJeff Roberson 	int i;
832cac77d04SJeff Roberson 
833ae7a6b38SJeff Roberson 	tdq_lock_pair(high, low);
834d2ad694cSJeff Roberson 	transferable = high->tdq_transferable;
835d2ad694cSJeff Roberson 	high_load = high->tdq_load;
836d2ad694cSJeff Roberson 	low_load = low->tdq_load;
83762fa74d9SJeff Roberson 	moved = 0;
838155b9987SJeff Roberson 	/*
839155b9987SJeff Roberson 	 * Determine what the imbalance is and then adjust that to how many
840d2ad694cSJeff Roberson 	 * threads we actually have to give up (transferable).
841155b9987SJeff Roberson 	 */
842ae7a6b38SJeff Roberson 	if (transferable != 0) {
843cac77d04SJeff Roberson 		diff = high_load - low_load;
844356500a3SJeff Roberson 		move = diff / 2;
845356500a3SJeff Roberson 		if (diff & 0x1)
846356500a3SJeff Roberson 			move++;
84780f86c9fSJeff Roberson 		move = min(move, transferable);
848356500a3SJeff Roberson 		for (i = 0; i < move; i++)
84962fa74d9SJeff Roberson 			moved += tdq_move(high, low);
850a5423ea3SJeff Roberson 		/*
851a5423ea3SJeff Roberson 		 * IPI the target cpu to force it to reschedule with the new
852a5423ea3SJeff Roberson 		 * workload.
853a5423ea3SJeff Roberson 		 */
854a5423ea3SJeff Roberson 		ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT);
855ae7a6b38SJeff Roberson 	}
8567fcf154aSJeff Roberson 	tdq_unlock_pair(high, low);
85762fa74d9SJeff Roberson 	return (moved);
858356500a3SJeff Roberson }
859356500a3SJeff Roberson 
860ae7a6b38SJeff Roberson /*
861ae7a6b38SJeff Roberson  * Move a thread from one thread queue to another.
862ae7a6b38SJeff Roberson  */
86362fa74d9SJeff Roberson static int
864ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to)
865356500a3SJeff Roberson {
866ad1e7d28SJulian Elischer 	struct td_sched *ts;
867ae7a6b38SJeff Roberson 	struct thread *td;
868ae7a6b38SJeff Roberson 	struct tdq *tdq;
869ae7a6b38SJeff Roberson 	int cpu;
870356500a3SJeff Roberson 
8717fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(from, MA_OWNED);
8727fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(to, MA_OWNED);
8737fcf154aSJeff Roberson 
874ad1e7d28SJulian Elischer 	tdq = from;
875ae7a6b38SJeff Roberson 	cpu = TDQ_ID(to);
8769727e637SJeff Roberson 	td = tdq_steal(tdq, cpu);
8779727e637SJeff Roberson 	if (td == NULL)
87862fa74d9SJeff Roberson 		return (0);
8799727e637SJeff Roberson 	ts = td->td_sched;
880ae7a6b38SJeff Roberson 	/*
881ae7a6b38SJeff Roberson 	 * Although the run queue is locked the thread may be blocked.  Lock
8827fcf154aSJeff Roberson 	 * it to clear this and acquire the run-queue lock.
883ae7a6b38SJeff Roberson 	 */
884ae7a6b38SJeff Roberson 	thread_lock(td);
8857fcf154aSJeff Roberson 	/* Drop recursive lock on from acquired via thread_lock(). */
886ae7a6b38SJeff Roberson 	TDQ_UNLOCK(from);
887ae7a6b38SJeff Roberson 	sched_rem(td);
8887b8bfa0dSJeff Roberson 	ts->ts_cpu = cpu;
889ae7a6b38SJeff Roberson 	td->td_lock = TDQ_LOCKPTR(to);
890ae7a6b38SJeff Roberson 	tdq_add(to, td, SRQ_YIELDING);
89162fa74d9SJeff Roberson 	return (1);
892356500a3SJeff Roberson }
89322bf7d9aSJeff Roberson 
894ae7a6b38SJeff Roberson /*
895ae7a6b38SJeff Roberson  * This tdq has idled.  Try to steal a thread from another cpu and switch
896ae7a6b38SJeff Roberson  * to it.
897ae7a6b38SJeff Roberson  */
89880f86c9fSJeff Roberson static int
899ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq)
90022bf7d9aSJeff Roberson {
90162fa74d9SJeff Roberson 	struct cpu_group *cg;
902ad1e7d28SJulian Elischer 	struct tdq *steal;
903c76ee827SJeff Roberson 	cpuset_t mask;
90462fa74d9SJeff Roberson 	int thresh;
905ae7a6b38SJeff Roberson 	int cpu;
90680f86c9fSJeff Roberson 
90788f530ccSJeff Roberson 	if (smp_started == 0 || steal_idle == 0)
90888f530ccSJeff Roberson 		return (1);
909c76ee827SJeff Roberson 	CPU_FILL(&mask);
910c76ee827SJeff Roberson 	CPU_CLR(PCPU_GET(cpuid), &mask);
91162fa74d9SJeff Roberson 	/* We don't want to be preempted while we're iterating. */
912ae7a6b38SJeff Roberson 	spinlock_enter();
91362fa74d9SJeff Roberson 	for (cg = tdq->tdq_cg; cg != NULL; ) {
9147b55ab05SJeff Roberson 		if ((cg->cg_flags & CG_FLAG_THREAD) == 0)
91562fa74d9SJeff Roberson 			thresh = steal_thresh;
91662fa74d9SJeff Roberson 		else
91762fa74d9SJeff Roberson 			thresh = 1;
91862fa74d9SJeff Roberson 		cpu = sched_highest(cg, mask, thresh);
91962fa74d9SJeff Roberson 		if (cpu == -1) {
92062fa74d9SJeff Roberson 			cg = cg->cg_parent;
92180f86c9fSJeff Roberson 			continue;
9227b8bfa0dSJeff Roberson 		}
9237b8bfa0dSJeff Roberson 		steal = TDQ_CPU(cpu);
924c76ee827SJeff Roberson 		CPU_CLR(cpu, &mask);
9257fcf154aSJeff Roberson 		tdq_lock_pair(tdq, steal);
92662fa74d9SJeff Roberson 		if (steal->tdq_load < thresh || steal->tdq_transferable == 0) {
9277fcf154aSJeff Roberson 			tdq_unlock_pair(tdq, steal);
92862fa74d9SJeff Roberson 			continue;
92962fa74d9SJeff Roberson 		}
93062fa74d9SJeff Roberson 		/*
93162fa74d9SJeff Roberson 		 * If a thread was added while interrupts were disabled don't
93262fa74d9SJeff Roberson 		 * steal one here.  If we fail to acquire one due to affinity
93362fa74d9SJeff Roberson 		 * restrictions loop again with this cpu removed from the
93462fa74d9SJeff Roberson 		 * set.
93562fa74d9SJeff Roberson 		 */
93662fa74d9SJeff Roberson 		if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) {
93762fa74d9SJeff Roberson 			tdq_unlock_pair(tdq, steal);
93862fa74d9SJeff Roberson 			continue;
93980f86c9fSJeff Roberson 		}
940ae7a6b38SJeff Roberson 		spinlock_exit();
941ae7a6b38SJeff Roberson 		TDQ_UNLOCK(steal);
9428df78c41SJeff Roberson 		mi_switch(SW_VOL | SWT_IDLE, NULL);
943ae7a6b38SJeff Roberson 		thread_unlock(curthread);
9447b8bfa0dSJeff Roberson 
9457b8bfa0dSJeff Roberson 		return (0);
94622bf7d9aSJeff Roberson 	}
94762fa74d9SJeff Roberson 	spinlock_exit();
94862fa74d9SJeff Roberson 	return (1);
94962fa74d9SJeff Roberson }
95022bf7d9aSJeff Roberson 
951ae7a6b38SJeff Roberson /*
952ae7a6b38SJeff Roberson  * Notify a remote cpu of new work.  Sends an IPI if criteria are met.
953ae7a6b38SJeff Roberson  */
95422bf7d9aSJeff Roberson static void
9559727e637SJeff Roberson tdq_notify(struct tdq *tdq, struct thread *td)
95622bf7d9aSJeff Roberson {
95702f0ff6dSJohn Baldwin 	struct thread *ctd;
958fc3a97dcSJeff Roberson 	int pri;
9597b8bfa0dSJeff Roberson 	int cpu;
96022bf7d9aSJeff Roberson 
961ff256d9cSJeff Roberson 	if (tdq->tdq_ipipending)
962ff256d9cSJeff Roberson 		return;
9639727e637SJeff Roberson 	cpu = td->td_sched->ts_cpu;
9649727e637SJeff Roberson 	pri = td->td_priority;
96502f0ff6dSJohn Baldwin 	ctd = pcpu_find(cpu)->pc_curthread;
96602f0ff6dSJohn Baldwin 	if (!sched_shouldpreempt(pri, ctd->td_priority, 1))
9676b2f763fSJeff Roberson 		return;
96802f0ff6dSJohn Baldwin 	if (TD_IS_IDLETHREAD(ctd)) {
9691690c6c1SJeff Roberson 		/*
9706c47aaaeSJeff Roberson 		 * If the MD code has an idle wakeup routine try that before
9716c47aaaeSJeff Roberson 		 * falling back to IPI.
9726c47aaaeSJeff Roberson 		 */
9736c47aaaeSJeff Roberson 		if (cpu_idle_wakeup(cpu))
9746c47aaaeSJeff Roberson 			return;
9751690c6c1SJeff Roberson 	}
976ff256d9cSJeff Roberson 	tdq->tdq_ipipending = 1;
97714618990SJeff Roberson 	ipi_selected(1 << cpu, IPI_PREEMPT);
97822bf7d9aSJeff Roberson }
97922bf7d9aSJeff Roberson 
980ae7a6b38SJeff Roberson /*
981ae7a6b38SJeff Roberson  * Steals load from a timeshare queue.  Honors the rotating queue head
982ae7a6b38SJeff Roberson  * index.
983ae7a6b38SJeff Roberson  */
9849727e637SJeff Roberson static struct thread *
98562fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start)
986ae7a6b38SJeff Roberson {
987ae7a6b38SJeff Roberson 	struct rqbits *rqb;
988ae7a6b38SJeff Roberson 	struct rqhead *rqh;
9899727e637SJeff Roberson 	struct thread *td;
990ae7a6b38SJeff Roberson 	int first;
991ae7a6b38SJeff Roberson 	int bit;
992ae7a6b38SJeff Roberson 	int pri;
993ae7a6b38SJeff Roberson 	int i;
994ae7a6b38SJeff Roberson 
995ae7a6b38SJeff Roberson 	rqb = &rq->rq_status;
996ae7a6b38SJeff Roberson 	bit = start & (RQB_BPW -1);
997ae7a6b38SJeff Roberson 	pri = 0;
998ae7a6b38SJeff Roberson 	first = 0;
999ae7a6b38SJeff Roberson again:
1000ae7a6b38SJeff Roberson 	for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
1001ae7a6b38SJeff Roberson 		if (rqb->rqb_bits[i] == 0)
1002ae7a6b38SJeff Roberson 			continue;
1003ae7a6b38SJeff Roberson 		if (bit != 0) {
1004ae7a6b38SJeff Roberson 			for (pri = bit; pri < RQB_BPW; pri++)
1005ae7a6b38SJeff Roberson 				if (rqb->rqb_bits[i] & (1ul << pri))
1006ae7a6b38SJeff Roberson 					break;
1007ae7a6b38SJeff Roberson 			if (pri >= RQB_BPW)
1008ae7a6b38SJeff Roberson 				continue;
1009ae7a6b38SJeff Roberson 		} else
1010ae7a6b38SJeff Roberson 			pri = RQB_FFS(rqb->rqb_bits[i]);
1011ae7a6b38SJeff Roberson 		pri += (i << RQB_L2BPW);
1012ae7a6b38SJeff Roberson 		rqh = &rq->rq_queues[pri];
10139727e637SJeff Roberson 		TAILQ_FOREACH(td, rqh, td_runq) {
10149727e637SJeff Roberson 			if (first && THREAD_CAN_MIGRATE(td) &&
10159727e637SJeff Roberson 			    THREAD_CAN_SCHED(td, cpu))
10169727e637SJeff Roberson 				return (td);
1017ae7a6b38SJeff Roberson 			first = 1;
1018ae7a6b38SJeff Roberson 		}
1019ae7a6b38SJeff Roberson 	}
1020ae7a6b38SJeff Roberson 	if (start != 0) {
1021ae7a6b38SJeff Roberson 		start = 0;
1022ae7a6b38SJeff Roberson 		goto again;
1023ae7a6b38SJeff Roberson 	}
1024ae7a6b38SJeff Roberson 
1025ae7a6b38SJeff Roberson 	return (NULL);
1026ae7a6b38SJeff Roberson }
1027ae7a6b38SJeff Roberson 
1028ae7a6b38SJeff Roberson /*
1029ae7a6b38SJeff Roberson  * Steals load from a standard linear queue.
1030ae7a6b38SJeff Roberson  */
10319727e637SJeff Roberson static struct thread *
103262fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu)
103322bf7d9aSJeff Roberson {
103422bf7d9aSJeff Roberson 	struct rqhead *rqh;
103522bf7d9aSJeff Roberson 	struct rqbits *rqb;
10369727e637SJeff Roberson 	struct thread *td;
103722bf7d9aSJeff Roberson 	int word;
103822bf7d9aSJeff Roberson 	int bit;
103922bf7d9aSJeff Roberson 
104022bf7d9aSJeff Roberson 	rqb = &rq->rq_status;
104122bf7d9aSJeff Roberson 	for (word = 0; word < RQB_LEN; word++) {
104222bf7d9aSJeff Roberson 		if (rqb->rqb_bits[word] == 0)
104322bf7d9aSJeff Roberson 			continue;
104422bf7d9aSJeff Roberson 		for (bit = 0; bit < RQB_BPW; bit++) {
1045a2640c9bSPeter Wemm 			if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
104622bf7d9aSJeff Roberson 				continue;
104722bf7d9aSJeff Roberson 			rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
10489727e637SJeff Roberson 			TAILQ_FOREACH(td, rqh, td_runq)
10499727e637SJeff Roberson 				if (THREAD_CAN_MIGRATE(td) &&
10509727e637SJeff Roberson 				    THREAD_CAN_SCHED(td, cpu))
10519727e637SJeff Roberson 					return (td);
105222bf7d9aSJeff Roberson 		}
105322bf7d9aSJeff Roberson 	}
105422bf7d9aSJeff Roberson 	return (NULL);
105522bf7d9aSJeff Roberson }
105622bf7d9aSJeff Roberson 
1057ae7a6b38SJeff Roberson /*
1058ae7a6b38SJeff Roberson  * Attempt to steal a thread in priority order from a thread queue.
1059ae7a6b38SJeff Roberson  */
10609727e637SJeff Roberson static struct thread *
106162fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu)
106222bf7d9aSJeff Roberson {
10639727e637SJeff Roberson 	struct thread *td;
106422bf7d9aSJeff Roberson 
1065ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
10669727e637SJeff Roberson 	if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL)
10679727e637SJeff Roberson 		return (td);
10689727e637SJeff Roberson 	if ((td = runq_steal_from(&tdq->tdq_timeshare,
10699727e637SJeff Roberson 	    cpu, tdq->tdq_ridx)) != NULL)
10709727e637SJeff Roberson 		return (td);
107162fa74d9SJeff Roberson 	return (runq_steal(&tdq->tdq_idle, cpu));
107222bf7d9aSJeff Roberson }
107380f86c9fSJeff Roberson 
1074ae7a6b38SJeff Roberson /*
1075ae7a6b38SJeff Roberson  * Sets the thread lock and ts_cpu to match the requested cpu.  Unlocks the
10767fcf154aSJeff Roberson  * current lock and returns with the assigned queue locked.
1077ae7a6b38SJeff Roberson  */
1078ae7a6b38SJeff Roberson static inline struct tdq *
10799727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags)
108080f86c9fSJeff Roberson {
10819727e637SJeff Roberson 
1082ae7a6b38SJeff Roberson 	struct tdq *tdq;
108380f86c9fSJeff Roberson 
10849727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1085ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpu);
10869727e637SJeff Roberson 	td->td_sched->ts_cpu = cpu;
10879727e637SJeff Roberson 	/*
10889727e637SJeff Roberson 	 * If the lock matches just return the queue.
10899727e637SJeff Roberson 	 */
1090ae7a6b38SJeff Roberson 	if (td->td_lock == TDQ_LOCKPTR(tdq))
1091ae7a6b38SJeff Roberson 		return (tdq);
1092ae7a6b38SJeff Roberson #ifdef notyet
109380f86c9fSJeff Roberson 	/*
1094a5423ea3SJeff Roberson 	 * If the thread isn't running its lockptr is a
1095ae7a6b38SJeff Roberson 	 * turnstile or a sleepqueue.  We can just lock_set without
1096ae7a6b38SJeff Roberson 	 * blocking.
1097670c524fSJeff Roberson 	 */
1098ae7a6b38SJeff Roberson 	if (TD_CAN_RUN(td)) {
1099ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1100ae7a6b38SJeff Roberson 		thread_lock_set(td, TDQ_LOCKPTR(tdq));
1101ae7a6b38SJeff Roberson 		return (tdq);
1102ae7a6b38SJeff Roberson 	}
1103ae7a6b38SJeff Roberson #endif
110480f86c9fSJeff Roberson 	/*
1105ae7a6b38SJeff Roberson 	 * The hard case, migration, we need to block the thread first to
1106ae7a6b38SJeff Roberson 	 * prevent order reversals with other cpus locks.
11077b8bfa0dSJeff Roberson 	 */
1108ae7a6b38SJeff Roberson 	thread_lock_block(td);
1109ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1110ae7a6b38SJeff Roberson 	thread_lock_unblock(td, TDQ_LOCKPTR(tdq));
1111ae7a6b38SJeff Roberson 	return (tdq);
111280f86c9fSJeff Roberson }
11132454aaf5SJeff Roberson 
11148df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding");
11158df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity");
11168df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity");
11178df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load");
11188df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu");
11198df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration");
11208df78c41SJeff Roberson 
1121ae7a6b38SJeff Roberson static int
11229727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags)
1123ae7a6b38SJeff Roberson {
112462fa74d9SJeff Roberson 	struct cpu_group *cg;
11259727e637SJeff Roberson 	struct td_sched *ts;
1126ae7a6b38SJeff Roberson 	struct tdq *tdq;
1127c76ee827SJeff Roberson 	cpuset_t mask;
11287b8bfa0dSJeff Roberson 	int self;
11297b8bfa0dSJeff Roberson 	int pri;
11307b8bfa0dSJeff Roberson 	int cpu;
11317b8bfa0dSJeff Roberson 
113262fa74d9SJeff Roberson 	self = PCPU_GET(cpuid);
11339727e637SJeff Roberson 	ts = td->td_sched;
11347b8bfa0dSJeff Roberson 	if (smp_started == 0)
11357b8bfa0dSJeff Roberson 		return (self);
113628994a58SJeff Roberson 	/*
113728994a58SJeff Roberson 	 * Don't migrate a running thread from sched_switch().
113828994a58SJeff Roberson 	 */
113962fa74d9SJeff Roberson 	if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td))
114062fa74d9SJeff Roberson 		return (ts->ts_cpu);
11417b8bfa0dSJeff Roberson 	/*
114262fa74d9SJeff Roberson 	 * Prefer to run interrupt threads on the processors that generate
114362fa74d9SJeff Roberson 	 * the interrupt.
11447b8bfa0dSJeff Roberson 	 */
114562fa74d9SJeff Roberson 	if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) &&
11468df78c41SJeff Roberson 	    curthread->td_intr_nesting_level && ts->ts_cpu != self) {
11478df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_intrbind);
114862fa74d9SJeff Roberson 		ts->ts_cpu = self;
11498df78c41SJeff Roberson 	}
115062fa74d9SJeff Roberson 	/*
115162fa74d9SJeff Roberson 	 * If the thread can run on the last cpu and the affinity has not
115262fa74d9SJeff Roberson 	 * expired or it is idle run it there.
115362fa74d9SJeff Roberson 	 */
115462fa74d9SJeff Roberson 	pri = td->td_priority;
115562fa74d9SJeff Roberson 	tdq = TDQ_CPU(ts->ts_cpu);
115662fa74d9SJeff Roberson 	if (THREAD_CAN_SCHED(td, ts->ts_cpu)) {
11578df78c41SJeff Roberson 		if (tdq->tdq_lowpri > PRI_MIN_IDLE) {
11588df78c41SJeff Roberson 			SCHED_STAT_INC(pickcpu_idle_affinity);
115962fa74d9SJeff Roberson 			return (ts->ts_cpu);
11608df78c41SJeff Roberson 		}
11618df78c41SJeff Roberson 		if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri) {
11628df78c41SJeff Roberson 			SCHED_STAT_INC(pickcpu_affinity);
11637b8bfa0dSJeff Roberson 			return (ts->ts_cpu);
11647b8bfa0dSJeff Roberson 		}
11658df78c41SJeff Roberson 	}
11667b8bfa0dSJeff Roberson 	/*
116762fa74d9SJeff Roberson 	 * Search for the highest level in the tree that still has affinity.
11687b8bfa0dSJeff Roberson 	 */
116962fa74d9SJeff Roberson 	cg = NULL;
117062fa74d9SJeff Roberson 	for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent)
117162fa74d9SJeff Roberson 		if (SCHED_AFFINITY(ts, cg->cg_level))
117262fa74d9SJeff Roberson 			break;
117362fa74d9SJeff Roberson 	cpu = -1;
1174c76ee827SJeff Roberson 	mask = td->td_cpuset->cs_mask;
117562fa74d9SJeff Roberson 	if (cg)
117662fa74d9SJeff Roberson 		cpu = sched_lowest(cg, mask, pri);
117762fa74d9SJeff Roberson 	if (cpu == -1)
117862fa74d9SJeff Roberson 		cpu = sched_lowest(cpu_top, mask, -1);
117962fa74d9SJeff Roberson 	/*
118062fa74d9SJeff Roberson 	 * Compare the lowest loaded cpu to current cpu.
118162fa74d9SJeff Roberson 	 */
1182ff256d9cSJeff Roberson 	if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri &&
11838df78c41SJeff Roberson 	    TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) {
11848df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_local);
118562fa74d9SJeff Roberson 		cpu = self;
11868df78c41SJeff Roberson 	} else
11878df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_lowest);
11888df78c41SJeff Roberson 	if (cpu != ts->ts_cpu)
11898df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_migration);
1190ff256d9cSJeff Roberson 	KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu."));
1191ae7a6b38SJeff Roberson 	return (cpu);
119280f86c9fSJeff Roberson }
119362fa74d9SJeff Roberson #endif
119422bf7d9aSJeff Roberson 
119522bf7d9aSJeff Roberson /*
119622bf7d9aSJeff Roberson  * Pick the highest priority task we have and return it.
11970c0a98b2SJeff Roberson  */
11989727e637SJeff Roberson static struct thread *
1199ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq)
12005d7ef00cSJeff Roberson {
12019727e637SJeff Roberson 	struct thread *td;
12025d7ef00cSJeff Roberson 
1203ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
12049727e637SJeff Roberson 	td = runq_choose(&tdq->tdq_realtime);
12059727e637SJeff Roberson 	if (td != NULL)
12069727e637SJeff Roberson 		return (td);
12079727e637SJeff Roberson 	td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
12089727e637SJeff Roberson 	if (td != NULL) {
12099727e637SJeff Roberson 		KASSERT(td->td_priority >= PRI_MIN_TIMESHARE,
1210e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on timeshare queue %d",
12119727e637SJeff Roberson 		    td->td_priority));
12129727e637SJeff Roberson 		return (td);
121315dc847eSJeff Roberson 	}
12149727e637SJeff Roberson 	td = runq_choose(&tdq->tdq_idle);
12159727e637SJeff Roberson 	if (td != NULL) {
12169727e637SJeff Roberson 		KASSERT(td->td_priority >= PRI_MIN_IDLE,
1217e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on idle queue %d",
12189727e637SJeff Roberson 		    td->td_priority));
12199727e637SJeff Roberson 		return (td);
1220e7d50326SJeff Roberson 	}
1221e7d50326SJeff Roberson 
1222e7d50326SJeff Roberson 	return (NULL);
1223245f3abfSJeff Roberson }
12240a016a05SJeff Roberson 
1225ae7a6b38SJeff Roberson /*
1226ae7a6b38SJeff Roberson  * Initialize a thread queue.
1227ae7a6b38SJeff Roberson  */
12280a016a05SJeff Roberson static void
1229ad1e7d28SJulian Elischer tdq_setup(struct tdq *tdq)
12300a016a05SJeff Roberson {
1231ae7a6b38SJeff Roberson 
1232c47f202bSJeff Roberson 	if (bootverbose)
1233c47f202bSJeff Roberson 		printf("ULE: setup cpu %d\n", TDQ_ID(tdq));
1234e7d50326SJeff Roberson 	runq_init(&tdq->tdq_realtime);
1235e7d50326SJeff Roberson 	runq_init(&tdq->tdq_timeshare);
1236d2ad694cSJeff Roberson 	runq_init(&tdq->tdq_idle);
123762fa74d9SJeff Roberson 	snprintf(tdq->tdq_name, sizeof(tdq->tdq_name),
123862fa74d9SJeff Roberson 	    "sched lock %d", (int)TDQ_ID(tdq));
123962fa74d9SJeff Roberson 	mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock",
124062fa74d9SJeff Roberson 	    MTX_SPIN | MTX_RECURSE);
12418f51ad55SJeff Roberson #ifdef KTR
12428f51ad55SJeff Roberson 	snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname),
12438f51ad55SJeff Roberson 	    "CPU %d load", (int)TDQ_ID(tdq));
12448f51ad55SJeff Roberson #endif
12450a016a05SJeff Roberson }
12460a016a05SJeff Roberson 
1247c47f202bSJeff Roberson #ifdef SMP
1248c47f202bSJeff Roberson static void
1249c47f202bSJeff Roberson sched_setup_smp(void)
1250c47f202bSJeff Roberson {
1251c47f202bSJeff Roberson 	struct tdq *tdq;
1252c47f202bSJeff Roberson 	int i;
1253c47f202bSJeff Roberson 
125462fa74d9SJeff Roberson 	cpu_top = smp_topo();
125562fa74d9SJeff Roberson 	for (i = 0; i < MAXCPU; i++) {
1256c47f202bSJeff Roberson 		if (CPU_ABSENT(i))
1257c47f202bSJeff Roberson 			continue;
125862fa74d9SJeff Roberson 		tdq = TDQ_CPU(i);
1259c47f202bSJeff Roberson 		tdq_setup(tdq);
126062fa74d9SJeff Roberson 		tdq->tdq_cg = smp_topo_find(cpu_top, i);
126162fa74d9SJeff Roberson 		if (tdq->tdq_cg == NULL)
126262fa74d9SJeff Roberson 			panic("Can't find cpu group for %d\n", i);
1263c47f202bSJeff Roberson 	}
126462fa74d9SJeff Roberson 	balance_tdq = TDQ_SELF();
126562fa74d9SJeff Roberson 	sched_balance();
1266c47f202bSJeff Roberson }
1267c47f202bSJeff Roberson #endif
1268c47f202bSJeff Roberson 
1269ae7a6b38SJeff Roberson /*
1270ae7a6b38SJeff Roberson  * Setup the thread queues and initialize the topology based on MD
1271ae7a6b38SJeff Roberson  * information.
1272ae7a6b38SJeff Roberson  */
127335e6168fSJeff Roberson static void
127435e6168fSJeff Roberson sched_setup(void *dummy)
127535e6168fSJeff Roberson {
1276ae7a6b38SJeff Roberson 	struct tdq *tdq;
1277c47f202bSJeff Roberson 
1278c47f202bSJeff Roberson 	tdq = TDQ_SELF();
12790ec896fdSJeff Roberson #ifdef SMP
1280c47f202bSJeff Roberson 	sched_setup_smp();
1281749d01b0SJeff Roberson #else
1282c47f202bSJeff Roberson 	tdq_setup(tdq);
1283356500a3SJeff Roberson #endif
1284ae7a6b38SJeff Roberson 	/*
1285ae7a6b38SJeff Roberson 	 * To avoid divide-by-zero, we set realstathz a dummy value
1286ae7a6b38SJeff Roberson 	 * in case which sched_clock() called before sched_initticks().
1287ae7a6b38SJeff Roberson 	 */
1288ae7a6b38SJeff Roberson 	realstathz = hz;
1289ae7a6b38SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1290ae7a6b38SJeff Roberson 	tickincr = 1 << SCHED_TICK_SHIFT;
1291ae7a6b38SJeff Roberson 
1292ae7a6b38SJeff Roberson 	/* Add thread0's load since it's running. */
1293ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1294c47f202bSJeff Roberson 	thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF());
12959727e637SJeff Roberson 	tdq_load_add(tdq, &thread0);
129662fa74d9SJeff Roberson 	tdq->tdq_lowpri = thread0.td_priority;
1297ae7a6b38SJeff Roberson 	TDQ_UNLOCK(tdq);
129835e6168fSJeff Roberson }
129935e6168fSJeff Roberson 
1300ae7a6b38SJeff Roberson /*
1301ae7a6b38SJeff Roberson  * This routine determines the tickincr after stathz and hz are setup.
1302ae7a6b38SJeff Roberson  */
1303a1d4fe69SDavid Xu /* ARGSUSED */
1304a1d4fe69SDavid Xu static void
1305a1d4fe69SDavid Xu sched_initticks(void *dummy)
1306a1d4fe69SDavid Xu {
1307ae7a6b38SJeff Roberson 	int incr;
1308ae7a6b38SJeff Roberson 
1309a1d4fe69SDavid Xu 	realstathz = stathz ? stathz : hz;
131014618990SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1311a1d4fe69SDavid Xu 
1312a1d4fe69SDavid Xu 	/*
1313e7d50326SJeff Roberson 	 * tickincr is shifted out by 10 to avoid rounding errors due to
13143f872f85SJeff Roberson 	 * hz not being evenly divisible by stathz on all platforms.
1315e7d50326SJeff Roberson 	 */
1316ae7a6b38SJeff Roberson 	incr = (hz << SCHED_TICK_SHIFT) / realstathz;
1317e7d50326SJeff Roberson 	/*
1318e7d50326SJeff Roberson 	 * This does not work for values of stathz that are more than
1319e7d50326SJeff Roberson 	 * 1 << SCHED_TICK_SHIFT * hz.  In practice this does not happen.
1320a1d4fe69SDavid Xu 	 */
1321ae7a6b38SJeff Roberson 	if (incr == 0)
1322ae7a6b38SJeff Roberson 		incr = 1;
1323ae7a6b38SJeff Roberson 	tickincr = incr;
13247b8bfa0dSJeff Roberson #ifdef SMP
13259862717aSJeff Roberson 	/*
13267fcf154aSJeff Roberson 	 * Set the default balance interval now that we know
13277fcf154aSJeff Roberson 	 * what realstathz is.
13287fcf154aSJeff Roberson 	 */
13297fcf154aSJeff Roberson 	balance_interval = realstathz;
13307fcf154aSJeff Roberson 	/*
133153a6c8b3SJeff Roberson 	 * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4.
133253a6c8b3SJeff Roberson 	 * This prevents excess thrashing on large machines and excess idle
133353a6c8b3SJeff Roberson 	 * on smaller machines.
13349862717aSJeff Roberson 	 */
133553a6c8b3SJeff Roberson 	steal_thresh = min(fls(mp_ncpus) - 1, 3);
13367b8bfa0dSJeff Roberson 	affinity = SCHED_AFFINITY_DEFAULT;
13377b8bfa0dSJeff Roberson #endif
1338a1d4fe69SDavid Xu }
1339a1d4fe69SDavid Xu 
1340a1d4fe69SDavid Xu 
134135e6168fSJeff Roberson /*
1342ae7a6b38SJeff Roberson  * This is the core of the interactivity algorithm.  Determines a score based
1343ae7a6b38SJeff Roberson  * on past behavior.  It is the ratio of sleep time to run time scaled to
1344ae7a6b38SJeff Roberson  * a [0, 100] integer.  This is the voluntary sleep time of a process, which
1345ae7a6b38SJeff Roberson  * differs from the cpu usage because it does not account for time spent
1346ae7a6b38SJeff Roberson  * waiting on a run-queue.  Would be prettier if we had floating point.
1347ae7a6b38SJeff Roberson  */
1348ae7a6b38SJeff Roberson static int
1349ae7a6b38SJeff Roberson sched_interact_score(struct thread *td)
1350ae7a6b38SJeff Roberson {
1351ae7a6b38SJeff Roberson 	struct td_sched *ts;
1352ae7a6b38SJeff Roberson 	int div;
1353ae7a6b38SJeff Roberson 
1354ae7a6b38SJeff Roberson 	ts = td->td_sched;
1355ae7a6b38SJeff Roberson 	/*
1356ae7a6b38SJeff Roberson 	 * The score is only needed if this is likely to be an interactive
1357ae7a6b38SJeff Roberson 	 * task.  Don't go through the expense of computing it if there's
1358ae7a6b38SJeff Roberson 	 * no chance.
1359ae7a6b38SJeff Roberson 	 */
1360ae7a6b38SJeff Roberson 	if (sched_interact <= SCHED_INTERACT_HALF &&
1361ae7a6b38SJeff Roberson 		ts->ts_runtime >= ts->ts_slptime)
1362ae7a6b38SJeff Roberson 			return (SCHED_INTERACT_HALF);
1363ae7a6b38SJeff Roberson 
1364ae7a6b38SJeff Roberson 	if (ts->ts_runtime > ts->ts_slptime) {
1365ae7a6b38SJeff Roberson 		div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF);
1366ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF +
1367ae7a6b38SJeff Roberson 		    (SCHED_INTERACT_HALF - (ts->ts_slptime / div)));
1368ae7a6b38SJeff Roberson 	}
1369ae7a6b38SJeff Roberson 	if (ts->ts_slptime > ts->ts_runtime) {
1370ae7a6b38SJeff Roberson 		div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF);
1371ae7a6b38SJeff Roberson 		return (ts->ts_runtime / div);
1372ae7a6b38SJeff Roberson 	}
1373ae7a6b38SJeff Roberson 	/* runtime == slptime */
1374ae7a6b38SJeff Roberson 	if (ts->ts_runtime)
1375ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF);
1376ae7a6b38SJeff Roberson 
1377ae7a6b38SJeff Roberson 	/*
1378ae7a6b38SJeff Roberson 	 * This can happen if slptime and runtime are 0.
1379ae7a6b38SJeff Roberson 	 */
1380ae7a6b38SJeff Roberson 	return (0);
1381ae7a6b38SJeff Roberson 
1382ae7a6b38SJeff Roberson }
1383ae7a6b38SJeff Roberson 
1384ae7a6b38SJeff Roberson /*
138535e6168fSJeff Roberson  * Scale the scheduling priority according to the "interactivity" of this
138635e6168fSJeff Roberson  * process.
138735e6168fSJeff Roberson  */
138815dc847eSJeff Roberson static void
13898460a577SJohn Birrell sched_priority(struct thread *td)
139035e6168fSJeff Roberson {
1391e7d50326SJeff Roberson 	int score;
139235e6168fSJeff Roberson 	int pri;
139335e6168fSJeff Roberson 
13948460a577SJohn Birrell 	if (td->td_pri_class != PRI_TIMESHARE)
139515dc847eSJeff Roberson 		return;
1396e7d50326SJeff Roberson 	/*
1397e7d50326SJeff Roberson 	 * If the score is interactive we place the thread in the realtime
1398e7d50326SJeff Roberson 	 * queue with a priority that is less than kernel and interrupt
1399e7d50326SJeff Roberson 	 * priorities.  These threads are not subject to nice restrictions.
1400e7d50326SJeff Roberson 	 *
1401ae7a6b38SJeff Roberson 	 * Scores greater than this are placed on the normal timeshare queue
1402e7d50326SJeff Roberson 	 * where the priority is partially decided by the most recent cpu
1403e7d50326SJeff Roberson 	 * utilization and the rest is decided by nice value.
1404a5423ea3SJeff Roberson 	 *
1405a5423ea3SJeff Roberson 	 * The nice value of the process has a linear effect on the calculated
1406a5423ea3SJeff Roberson 	 * score.  Negative nice values make it easier for a thread to be
1407a5423ea3SJeff Roberson 	 * considered interactive.
1408e7d50326SJeff Roberson 	 */
1409e270652bSJeff Roberson 	score = imax(0, sched_interact_score(td) - td->td_proc->p_nice);
1410e7d50326SJeff Roberson 	if (score < sched_interact) {
1411e7d50326SJeff Roberson 		pri = PRI_MIN_REALTIME;
1412e7d50326SJeff Roberson 		pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
1413e7d50326SJeff Roberson 		    * score;
1414e7d50326SJeff Roberson 		KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
14159a93305aSJeff Roberson 		    ("sched_priority: invalid interactive priority %d score %d",
14169a93305aSJeff Roberson 		    pri, score));
1417e7d50326SJeff Roberson 	} else {
1418e7d50326SJeff Roberson 		pri = SCHED_PRI_MIN;
1419e7d50326SJeff Roberson 		if (td->td_sched->ts_ticks)
1420e7d50326SJeff Roberson 			pri += SCHED_PRI_TICKS(td->td_sched);
1421e7d50326SJeff Roberson 		pri += SCHED_PRI_NICE(td->td_proc->p_nice);
1422ae7a6b38SJeff Roberson 		KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE,
1423ae7a6b38SJeff Roberson 		    ("sched_priority: invalid priority %d: nice %d, "
1424ae7a6b38SJeff Roberson 		    "ticks %d ftick %d ltick %d tick pri %d",
1425ae7a6b38SJeff Roberson 		    pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
1426ae7a6b38SJeff Roberson 		    td->td_sched->ts_ftick, td->td_sched->ts_ltick,
1427ae7a6b38SJeff Roberson 		    SCHED_PRI_TICKS(td->td_sched)));
1428e7d50326SJeff Roberson 	}
14298460a577SJohn Birrell 	sched_user_prio(td, pri);
143035e6168fSJeff Roberson 
143115dc847eSJeff Roberson 	return;
143235e6168fSJeff Roberson }
143335e6168fSJeff Roberson 
143435e6168fSJeff Roberson /*
1435d322132cSJeff Roberson  * This routine enforces a maximum limit on the amount of scheduling history
1436ae7a6b38SJeff Roberson  * kept.  It is called after either the slptime or runtime is adjusted.  This
1437ae7a6b38SJeff Roberson  * function is ugly due to integer math.
1438d322132cSJeff Roberson  */
14394b60e324SJeff Roberson static void
14408460a577SJohn Birrell sched_interact_update(struct thread *td)
14414b60e324SJeff Roberson {
1442155b6ca1SJeff Roberson 	struct td_sched *ts;
14439a93305aSJeff Roberson 	u_int sum;
14443f741ca1SJeff Roberson 
1445155b6ca1SJeff Roberson 	ts = td->td_sched;
1446ae7a6b38SJeff Roberson 	sum = ts->ts_runtime + ts->ts_slptime;
1447d322132cSJeff Roberson 	if (sum < SCHED_SLP_RUN_MAX)
1448d322132cSJeff Roberson 		return;
1449d322132cSJeff Roberson 	/*
1450155b6ca1SJeff Roberson 	 * This only happens from two places:
1451155b6ca1SJeff Roberson 	 * 1) We have added an unusual amount of run time from fork_exit.
1452155b6ca1SJeff Roberson 	 * 2) We have added an unusual amount of sleep time from sched_sleep().
1453155b6ca1SJeff Roberson 	 */
1454155b6ca1SJeff Roberson 	if (sum > SCHED_SLP_RUN_MAX * 2) {
1455ae7a6b38SJeff Roberson 		if (ts->ts_runtime > ts->ts_slptime) {
1456ae7a6b38SJeff Roberson 			ts->ts_runtime = SCHED_SLP_RUN_MAX;
1457ae7a6b38SJeff Roberson 			ts->ts_slptime = 1;
1458155b6ca1SJeff Roberson 		} else {
1459ae7a6b38SJeff Roberson 			ts->ts_slptime = SCHED_SLP_RUN_MAX;
1460ae7a6b38SJeff Roberson 			ts->ts_runtime = 1;
1461155b6ca1SJeff Roberson 		}
1462155b6ca1SJeff Roberson 		return;
1463155b6ca1SJeff Roberson 	}
1464155b6ca1SJeff Roberson 	/*
1465d322132cSJeff Roberson 	 * If we have exceeded by more than 1/5th then the algorithm below
1466d322132cSJeff Roberson 	 * will not bring us back into range.  Dividing by two here forces
14672454aaf5SJeff Roberson 	 * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
1468d322132cSJeff Roberson 	 */
146937a35e4aSJeff Roberson 	if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
1470ae7a6b38SJeff Roberson 		ts->ts_runtime /= 2;
1471ae7a6b38SJeff Roberson 		ts->ts_slptime /= 2;
1472d322132cSJeff Roberson 		return;
1473d322132cSJeff Roberson 	}
1474ae7a6b38SJeff Roberson 	ts->ts_runtime = (ts->ts_runtime / 5) * 4;
1475ae7a6b38SJeff Roberson 	ts->ts_slptime = (ts->ts_slptime / 5) * 4;
1476d322132cSJeff Roberson }
1477d322132cSJeff Roberson 
1478ae7a6b38SJeff Roberson /*
1479ae7a6b38SJeff Roberson  * Scale back the interactivity history when a child thread is created.  The
1480ae7a6b38SJeff Roberson  * history is inherited from the parent but the thread may behave totally
1481ae7a6b38SJeff Roberson  * differently.  For example, a shell spawning a compiler process.  We want
1482ae7a6b38SJeff Roberson  * to learn that the compiler is behaving badly very quickly.
1483ae7a6b38SJeff Roberson  */
1484d322132cSJeff Roberson static void
14858460a577SJohn Birrell sched_interact_fork(struct thread *td)
1486d322132cSJeff Roberson {
1487d322132cSJeff Roberson 	int ratio;
1488d322132cSJeff Roberson 	int sum;
1489d322132cSJeff Roberson 
1490ae7a6b38SJeff Roberson 	sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
1491d322132cSJeff Roberson 	if (sum > SCHED_SLP_RUN_FORK) {
1492d322132cSJeff Roberson 		ratio = sum / SCHED_SLP_RUN_FORK;
1493ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime /= ratio;
1494ae7a6b38SJeff Roberson 		td->td_sched->ts_slptime /= ratio;
14954b60e324SJeff Roberson 	}
14964b60e324SJeff Roberson }
14974b60e324SJeff Roberson 
149815dc847eSJeff Roberson /*
1499ae7a6b38SJeff Roberson  * Called from proc0_init() to setup the scheduler fields.
1500ed062c8dSJulian Elischer  */
1501ed062c8dSJulian Elischer void
1502ed062c8dSJulian Elischer schedinit(void)
1503ed062c8dSJulian Elischer {
1504e7d50326SJeff Roberson 
1505ed062c8dSJulian Elischer 	/*
1506ed062c8dSJulian Elischer 	 * Set up the scheduler specific parts of proc0.
1507ed062c8dSJulian Elischer 	 */
1508ed062c8dSJulian Elischer 	proc0.p_sched = NULL; /* XXX */
1509ad1e7d28SJulian Elischer 	thread0.td_sched = &td_sched0;
1510e7d50326SJeff Roberson 	td_sched0.ts_ltick = ticks;
15118ab80cf0SJeff Roberson 	td_sched0.ts_ftick = ticks;
151273daf66fSJeff Roberson 	td_sched0.ts_slice = sched_slice;
1513ed062c8dSJulian Elischer }
1514ed062c8dSJulian Elischer 
1515ed062c8dSJulian Elischer /*
151615dc847eSJeff Roberson  * This is only somewhat accurate since given many processes of the same
151715dc847eSJeff Roberson  * priority they will switch when their slices run out, which will be
1518e7d50326SJeff Roberson  * at most sched_slice stathz ticks.
151915dc847eSJeff Roberson  */
152035e6168fSJeff Roberson int
152135e6168fSJeff Roberson sched_rr_interval(void)
152235e6168fSJeff Roberson {
1523e7d50326SJeff Roberson 
1524e7d50326SJeff Roberson 	/* Convert sched_slice to hz */
1525e7d50326SJeff Roberson 	return (hz/(realstathz/sched_slice));
152635e6168fSJeff Roberson }
152735e6168fSJeff Roberson 
1528ae7a6b38SJeff Roberson /*
1529ae7a6b38SJeff Roberson  * Update the percent cpu tracking information when it is requested or
1530ae7a6b38SJeff Roberson  * the total history exceeds the maximum.  We keep a sliding history of
1531ae7a6b38SJeff Roberson  * tick counts that slowly decays.  This is less precise than the 4BSD
1532ae7a6b38SJeff Roberson  * mechanism since it happens with less regular and frequent events.
1533ae7a6b38SJeff Roberson  */
153422bf7d9aSJeff Roberson static void
1535ad1e7d28SJulian Elischer sched_pctcpu_update(struct td_sched *ts)
153635e6168fSJeff Roberson {
1537e7d50326SJeff Roberson 
1538e7d50326SJeff Roberson 	if (ts->ts_ticks == 0)
1539e7d50326SJeff Roberson 		return;
15408ab80cf0SJeff Roberson 	if (ticks - (hz / 10) < ts->ts_ltick &&
15418ab80cf0SJeff Roberson 	    SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
15428ab80cf0SJeff Roberson 		return;
154335e6168fSJeff Roberson 	/*
154435e6168fSJeff Roberson 	 * Adjust counters and watermark for pctcpu calc.
1545210491d3SJeff Roberson 	 */
1546e7d50326SJeff Roberson 	if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
1547ad1e7d28SJulian Elischer 		ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
1548e7d50326SJeff Roberson 			    SCHED_TICK_TARG;
1549e7d50326SJeff Roberson 	else
1550ad1e7d28SJulian Elischer 		ts->ts_ticks = 0;
1551ad1e7d28SJulian Elischer 	ts->ts_ltick = ticks;
1552e7d50326SJeff Roberson 	ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
155335e6168fSJeff Roberson }
155435e6168fSJeff Roberson 
1555ae7a6b38SJeff Roberson /*
1556ae7a6b38SJeff Roberson  * Adjust the priority of a thread.  Move it to the appropriate run-queue
1557ae7a6b38SJeff Roberson  * if necessary.  This is the back-end for several priority related
1558ae7a6b38SJeff Roberson  * functions.
1559ae7a6b38SJeff Roberson  */
1560e7d50326SJeff Roberson static void
1561f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio)
156235e6168fSJeff Roberson {
1563ad1e7d28SJulian Elischer 	struct td_sched *ts;
156473daf66fSJeff Roberson 	struct tdq *tdq;
156573daf66fSJeff Roberson 	int oldpri;
156635e6168fSJeff Roberson 
15678f51ad55SJeff Roberson 	KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio",
15688f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, "new prio:%d", prio,
15698f51ad55SJeff Roberson 	    KTR_ATTR_LINKED, sched_tdname(curthread));
15708f51ad55SJeff Roberson 	if (td != curthread && prio > td->td_priority) {
15718f51ad55SJeff Roberson 		KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread),
15728f51ad55SJeff Roberson 		    "lend prio", "prio:%d", td->td_priority, "new prio:%d",
15738f51ad55SJeff Roberson 		    prio, KTR_ATTR_LINKED, sched_tdname(td));
15748f51ad55SJeff Roberson 	}
1575ad1e7d28SJulian Elischer 	ts = td->td_sched;
15767b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1577f5c157d9SJohn Baldwin 	if (td->td_priority == prio)
1578f5c157d9SJohn Baldwin 		return;
15793f741ca1SJeff Roberson 	/*
15803f741ca1SJeff Roberson 	 * If the priority has been elevated due to priority
15813f741ca1SJeff Roberson 	 * propagation, we may have to move ourselves to a new
1582e7d50326SJeff Roberson 	 * queue.  This could be optimized to not re-add in some
1583e7d50326SJeff Roberson 	 * cases.
1584f2b74cbfSJeff Roberson 	 */
15856d55b3ecSJeff Roberson 	if (TD_ON_RUNQ(td) && prio < td->td_priority) {
1586e7d50326SJeff Roberson 		sched_rem(td);
1587e7d50326SJeff Roberson 		td->td_priority = prio;
1588ae7a6b38SJeff Roberson 		sched_add(td, SRQ_BORROWING);
158973daf66fSJeff Roberson 		return;
159073daf66fSJeff Roberson 	}
15916d55b3ecSJeff Roberson 	/*
15926d55b3ecSJeff Roberson 	 * If the thread is currently running we may have to adjust the lowpri
15936d55b3ecSJeff Roberson 	 * information so other cpus are aware of our current priority.
15946d55b3ecSJeff Roberson 	 */
15956d55b3ecSJeff Roberson 	if (TD_IS_RUNNING(td)) {
1596ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(ts->ts_cpu);
159762fa74d9SJeff Roberson 		oldpri = td->td_priority;
15983f741ca1SJeff Roberson 		td->td_priority = prio;
159962fa74d9SJeff Roberson 		if (prio < tdq->tdq_lowpri)
160062fa74d9SJeff Roberson 			tdq->tdq_lowpri = prio;
160162fa74d9SJeff Roberson 		else if (tdq->tdq_lowpri == oldpri)
160262fa74d9SJeff Roberson 			tdq_setlowpri(tdq, td);
16036d55b3ecSJeff Roberson 		return;
160473daf66fSJeff Roberson 	}
16056d55b3ecSJeff Roberson 	td->td_priority = prio;
1606ae7a6b38SJeff Roberson }
160735e6168fSJeff Roberson 
1608f5c157d9SJohn Baldwin /*
1609f5c157d9SJohn Baldwin  * Update a thread's priority when it is lent another thread's
1610f5c157d9SJohn Baldwin  * priority.
1611f5c157d9SJohn Baldwin  */
1612f5c157d9SJohn Baldwin void
1613f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio)
1614f5c157d9SJohn Baldwin {
1615f5c157d9SJohn Baldwin 
1616f5c157d9SJohn Baldwin 	td->td_flags |= TDF_BORROWING;
1617f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1618f5c157d9SJohn Baldwin }
1619f5c157d9SJohn Baldwin 
1620f5c157d9SJohn Baldwin /*
1621f5c157d9SJohn Baldwin  * Restore a thread's priority when priority propagation is
1622f5c157d9SJohn Baldwin  * over.  The prio argument is the minimum priority the thread
1623f5c157d9SJohn Baldwin  * needs to have to satisfy other possible priority lending
1624f5c157d9SJohn Baldwin  * requests.  If the thread's regular priority is less
1625f5c157d9SJohn Baldwin  * important than prio, the thread will keep a priority boost
1626f5c157d9SJohn Baldwin  * of prio.
1627f5c157d9SJohn Baldwin  */
1628f5c157d9SJohn Baldwin void
1629f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio)
1630f5c157d9SJohn Baldwin {
1631f5c157d9SJohn Baldwin 	u_char base_pri;
1632f5c157d9SJohn Baldwin 
1633f5c157d9SJohn Baldwin 	if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1634f5c157d9SJohn Baldwin 	    td->td_base_pri <= PRI_MAX_TIMESHARE)
16358460a577SJohn Birrell 		base_pri = td->td_user_pri;
1636f5c157d9SJohn Baldwin 	else
1637f5c157d9SJohn Baldwin 		base_pri = td->td_base_pri;
1638f5c157d9SJohn Baldwin 	if (prio >= base_pri) {
1639f5c157d9SJohn Baldwin 		td->td_flags &= ~TDF_BORROWING;
1640f5c157d9SJohn Baldwin 		sched_thread_priority(td, base_pri);
1641f5c157d9SJohn Baldwin 	} else
1642f5c157d9SJohn Baldwin 		sched_lend_prio(td, prio);
1643f5c157d9SJohn Baldwin }
1644f5c157d9SJohn Baldwin 
1645ae7a6b38SJeff Roberson /*
1646ae7a6b38SJeff Roberson  * Standard entry for setting the priority to an absolute value.
1647ae7a6b38SJeff Roberson  */
1648f5c157d9SJohn Baldwin void
1649f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio)
1650f5c157d9SJohn Baldwin {
1651f5c157d9SJohn Baldwin 	u_char oldprio;
1652f5c157d9SJohn Baldwin 
1653f5c157d9SJohn Baldwin 	/* First, update the base priority. */
1654f5c157d9SJohn Baldwin 	td->td_base_pri = prio;
1655f5c157d9SJohn Baldwin 
1656f5c157d9SJohn Baldwin 	/*
165750aaa791SJohn Baldwin 	 * If the thread is borrowing another thread's priority, don't
1658f5c157d9SJohn Baldwin 	 * ever lower the priority.
1659f5c157d9SJohn Baldwin 	 */
1660f5c157d9SJohn Baldwin 	if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1661f5c157d9SJohn Baldwin 		return;
1662f5c157d9SJohn Baldwin 
1663f5c157d9SJohn Baldwin 	/* Change the real priority. */
1664f5c157d9SJohn Baldwin 	oldprio = td->td_priority;
1665f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1666f5c157d9SJohn Baldwin 
1667f5c157d9SJohn Baldwin 	/*
1668f5c157d9SJohn Baldwin 	 * If the thread is on a turnstile, then let the turnstile update
1669f5c157d9SJohn Baldwin 	 * its state.
1670f5c157d9SJohn Baldwin 	 */
1671f5c157d9SJohn Baldwin 	if (TD_ON_LOCK(td) && oldprio != prio)
1672f5c157d9SJohn Baldwin 		turnstile_adjust(td, oldprio);
1673f5c157d9SJohn Baldwin }
1674f5c157d9SJohn Baldwin 
1675ae7a6b38SJeff Roberson /*
1676ae7a6b38SJeff Roberson  * Set the base user priority, does not effect current running priority.
1677ae7a6b38SJeff Roberson  */
167835e6168fSJeff Roberson void
16798460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio)
16803db720fdSDavid Xu {
16813db720fdSDavid Xu 	u_char oldprio;
16823db720fdSDavid Xu 
16838460a577SJohn Birrell 	td->td_base_user_pri = prio;
1684fc6c30f6SJulian Elischer 	if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
1685fc6c30f6SJulian Elischer                 return;
16868460a577SJohn Birrell 	oldprio = td->td_user_pri;
16878460a577SJohn Birrell 	td->td_user_pri = prio;
16883db720fdSDavid Xu }
16893db720fdSDavid Xu 
16903db720fdSDavid Xu void
16913db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio)
16923db720fdSDavid Xu {
16933db720fdSDavid Xu 	u_char oldprio;
16943db720fdSDavid Xu 
1695435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
16963db720fdSDavid Xu 	td->td_flags |= TDF_UBORROWING;
1697f645b5daSMaxim Konovalov 	oldprio = td->td_user_pri;
16988460a577SJohn Birrell 	td->td_user_pri = prio;
16993db720fdSDavid Xu }
17003db720fdSDavid Xu 
17013db720fdSDavid Xu void
17023db720fdSDavid Xu sched_unlend_user_prio(struct thread *td, u_char prio)
17033db720fdSDavid Xu {
17043db720fdSDavid Xu 	u_char base_pri;
17053db720fdSDavid Xu 
1706435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17078460a577SJohn Birrell 	base_pri = td->td_base_user_pri;
17083db720fdSDavid Xu 	if (prio >= base_pri) {
17093db720fdSDavid Xu 		td->td_flags &= ~TDF_UBORROWING;
17108460a577SJohn Birrell 		sched_user_prio(td, base_pri);
1711435806d3SDavid Xu 	} else {
17123db720fdSDavid Xu 		sched_lend_user_prio(td, prio);
17133db720fdSDavid Xu 	}
1714435806d3SDavid Xu }
17153db720fdSDavid Xu 
1716ae7a6b38SJeff Roberson /*
1717731016feSWojciech A. Koszek  * Block a thread for switching.  Similar to thread_block() but does not
1718731016feSWojciech A. Koszek  * bump the spin count.
1719731016feSWojciech A. Koszek  */
1720731016feSWojciech A. Koszek static inline struct mtx *
1721731016feSWojciech A. Koszek thread_block_switch(struct thread *td)
1722731016feSWojciech A. Koszek {
1723731016feSWojciech A. Koszek 	struct mtx *lock;
1724731016feSWojciech A. Koszek 
1725731016feSWojciech A. Koszek 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1726731016feSWojciech A. Koszek 	lock = td->td_lock;
1727731016feSWojciech A. Koszek 	td->td_lock = &blocked_lock;
1728731016feSWojciech A. Koszek 	mtx_unlock_spin(lock);
1729731016feSWojciech A. Koszek 
1730731016feSWojciech A. Koszek 	return (lock);
1731731016feSWojciech A. Koszek }
1732731016feSWojciech A. Koszek 
1733731016feSWojciech A. Koszek /*
1734c47f202bSJeff Roberson  * Handle migration from sched_switch().  This happens only for
1735c47f202bSJeff Roberson  * cpu binding.
1736c47f202bSJeff Roberson  */
1737c47f202bSJeff Roberson static struct mtx *
1738c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
1739c47f202bSJeff Roberson {
1740c47f202bSJeff Roberson 	struct tdq *tdn;
1741c47f202bSJeff Roberson 
1742c47f202bSJeff Roberson 	tdn = TDQ_CPU(td->td_sched->ts_cpu);
1743c47f202bSJeff Roberson #ifdef SMP
17449727e637SJeff Roberson 	tdq_load_rem(tdq, td);
1745c47f202bSJeff Roberson 	/*
1746c47f202bSJeff Roberson 	 * Do the lock dance required to avoid LOR.  We grab an extra
1747c47f202bSJeff Roberson 	 * spinlock nesting to prevent preemption while we're
1748c47f202bSJeff Roberson 	 * not holding either run-queue lock.
1749c47f202bSJeff Roberson 	 */
1750c47f202bSJeff Roberson 	spinlock_enter();
1751c47f202bSJeff Roberson 	thread_block_switch(td);	/* This releases the lock on tdq. */
1752c47f202bSJeff Roberson 	TDQ_LOCK(tdn);
1753c47f202bSJeff Roberson 	tdq_add(tdn, td, flags);
17549727e637SJeff Roberson 	tdq_notify(tdn, td);
1755c47f202bSJeff Roberson 	/*
1756c47f202bSJeff Roberson 	 * After we unlock tdn the new cpu still can't switch into this
1757c47f202bSJeff Roberson 	 * thread until we've unblocked it in cpu_switch().  The lock
1758c47f202bSJeff Roberson 	 * pointers may match in the case of HTT cores.  Don't unlock here
1759c47f202bSJeff Roberson 	 * or we can deadlock when the other CPU runs the IPI handler.
1760c47f202bSJeff Roberson 	 */
1761c47f202bSJeff Roberson 	if (TDQ_LOCKPTR(tdn) != TDQ_LOCKPTR(tdq)) {
1762c47f202bSJeff Roberson 		TDQ_UNLOCK(tdn);
1763c47f202bSJeff Roberson 		TDQ_LOCK(tdq);
1764c47f202bSJeff Roberson 	}
1765c47f202bSJeff Roberson 	spinlock_exit();
1766c47f202bSJeff Roberson #endif
1767c47f202bSJeff Roberson 	return (TDQ_LOCKPTR(tdn));
1768c47f202bSJeff Roberson }
1769c47f202bSJeff Roberson 
1770c47f202bSJeff Roberson /*
1771ae7a6b38SJeff Roberson  * Release a thread that was blocked with thread_block_switch().
1772ae7a6b38SJeff Roberson  */
1773ae7a6b38SJeff Roberson static inline void
1774ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx)
1775ae7a6b38SJeff Roberson {
1776ae7a6b38SJeff Roberson 	atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock,
1777ae7a6b38SJeff Roberson 	    (uintptr_t)mtx);
1778ae7a6b38SJeff Roberson }
1779ae7a6b38SJeff Roberson 
1780ae7a6b38SJeff Roberson /*
1781ae7a6b38SJeff Roberson  * Switch threads.  This function has to handle threads coming in while
1782ae7a6b38SJeff Roberson  * blocked for some reason, running, or idle.  It also must deal with
1783ae7a6b38SJeff Roberson  * migrating a thread from one queue to another as running threads may
1784ae7a6b38SJeff Roberson  * be assigned elsewhere via binding.
1785ae7a6b38SJeff Roberson  */
17863db720fdSDavid Xu void
17873389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags)
178835e6168fSJeff Roberson {
1789c02bbb43SJeff Roberson 	struct tdq *tdq;
1790ad1e7d28SJulian Elischer 	struct td_sched *ts;
1791ae7a6b38SJeff Roberson 	struct mtx *mtx;
1792c47f202bSJeff Roberson 	int srqflag;
1793ae7a6b38SJeff Roberson 	int cpuid;
179435e6168fSJeff Roberson 
17957b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17966d55b3ecSJeff Roberson 	KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument"));
179735e6168fSJeff Roberson 
1798ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
1799ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
1800e7d50326SJeff Roberson 	ts = td->td_sched;
1801c47f202bSJeff Roberson 	mtx = td->td_lock;
1802ae7a6b38SJeff Roberson 	ts->ts_rltick = ticks;
1803060563ecSJulian Elischer 	td->td_lastcpu = td->td_oncpu;
1804060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
180552eb8464SJohn Baldwin 	td->td_flags &= ~TDF_NEEDRESCHED;
180677918643SStephan Uphoff 	td->td_owepreempt = 0;
18071690c6c1SJeff Roberson 	tdq->tdq_switchcnt++;
1808b11fdad0SJeff Roberson 	/*
1809ae7a6b38SJeff Roberson 	 * The lock pointer in an idle thread should never change.  Reset it
1810ae7a6b38SJeff Roberson 	 * to CAN_RUN as well.
1811b11fdad0SJeff Roberson 	 */
1812486a9414SJulian Elischer 	if (TD_IS_IDLETHREAD(td)) {
1813ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1814bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
18157b20fb19SJeff Roberson 	} else if (TD_IS_RUNNING(td)) {
1816ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1817c47f202bSJeff Roberson 		srqflag = (flags & SW_PREEMPT) ?
1818598b368dSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1819c47f202bSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING;
1820c47f202bSJeff Roberson 		if (ts->ts_cpu == cpuid)
18219727e637SJeff Roberson 			tdq_runq_add(tdq, td, srqflag);
1822c47f202bSJeff Roberson 		else
1823c47f202bSJeff Roberson 			mtx = sched_switch_migrate(tdq, td, srqflag);
1824ae7a6b38SJeff Roberson 	} else {
1825ae7a6b38SJeff Roberson 		/* This thread must be going to sleep. */
1826ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1827ae7a6b38SJeff Roberson 		mtx = thread_block_switch(td);
18289727e637SJeff Roberson 		tdq_load_rem(tdq, td);
1829ae7a6b38SJeff Roberson 	}
1830ae7a6b38SJeff Roberson 	/*
1831ae7a6b38SJeff Roberson 	 * We enter here with the thread blocked and assigned to the
1832ae7a6b38SJeff Roberson 	 * appropriate cpu run-queue or sleep-queue and with the current
1833ae7a6b38SJeff Roberson 	 * thread-queue locked.
1834ae7a6b38SJeff Roberson 	 */
1835ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
18362454aaf5SJeff Roberson 	newtd = choosethread();
1837ae7a6b38SJeff Roberson 	/*
1838ae7a6b38SJeff Roberson 	 * Call the MD code to switch contexts if necessary.
1839ae7a6b38SJeff Roberson 	 */
1840ebccf1e3SJoseph Koshy 	if (td != newtd) {
1841ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1842ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1843ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1844ebccf1e3SJoseph Koshy #endif
1845eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
184659c68134SJeff Roberson 		TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
18476f5f25e5SJohn Birrell 
18486f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS
18496f5f25e5SJohn Birrell 		/*
18506f5f25e5SJohn Birrell 		 * If DTrace has set the active vtime enum to anything
18516f5f25e5SJohn Birrell 		 * other than INACTIVE (0), then it should have set the
18526f5f25e5SJohn Birrell 		 * function to call.
18536f5f25e5SJohn Birrell 		 */
18546f5f25e5SJohn Birrell 		if (dtrace_vtime_active)
18556f5f25e5SJohn Birrell 			(*dtrace_vtime_switch_func)(newtd);
18566f5f25e5SJohn Birrell #endif
18576f5f25e5SJohn Birrell 
1858ae7a6b38SJeff Roberson 		cpu_switch(td, newtd, mtx);
1859ae7a6b38SJeff Roberson 		/*
1860ae7a6b38SJeff Roberson 		 * We may return from cpu_switch on a different cpu.  However,
1861ae7a6b38SJeff Roberson 		 * we always return with td_lock pointing to the current cpu's
1862ae7a6b38SJeff Roberson 		 * run queue lock.
1863ae7a6b38SJeff Roberson 		 */
1864ae7a6b38SJeff Roberson 		cpuid = PCPU_GET(cpuid);
1865ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(cpuid);
1866eea4f254SJeff Roberson 		lock_profile_obtain_lock_success(
1867eea4f254SJeff Roberson 		    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
1868ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1869ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1870ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1871ebccf1e3SJoseph Koshy #endif
1872ae7a6b38SJeff Roberson 	} else
1873ae7a6b38SJeff Roberson 		thread_unblock_switch(td, mtx);
1874ae7a6b38SJeff Roberson 	/*
1875ae7a6b38SJeff Roberson 	 * Assert that all went well and return.
1876ae7a6b38SJeff Roberson 	 */
1877ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED);
1878ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1879ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
188035e6168fSJeff Roberson }
188135e6168fSJeff Roberson 
1882ae7a6b38SJeff Roberson /*
1883ae7a6b38SJeff Roberson  * Adjust thread priorities as a result of a nice request.
1884ae7a6b38SJeff Roberson  */
188535e6168fSJeff Roberson void
1886fa885116SJulian Elischer sched_nice(struct proc *p, int nice)
188735e6168fSJeff Roberson {
188835e6168fSJeff Roberson 	struct thread *td;
188935e6168fSJeff Roberson 
1890fa885116SJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
1891e7d50326SJeff Roberson 
1892fa885116SJulian Elischer 	p->p_nice = nice;
18938460a577SJohn Birrell 	FOREACH_THREAD_IN_PROC(p, td) {
18947b20fb19SJeff Roberson 		thread_lock(td);
18958460a577SJohn Birrell 		sched_priority(td);
1896e7d50326SJeff Roberson 		sched_prio(td, td->td_base_user_pri);
18977b20fb19SJeff Roberson 		thread_unlock(td);
189835e6168fSJeff Roberson 	}
1899fa885116SJulian Elischer }
190035e6168fSJeff Roberson 
1901ae7a6b38SJeff Roberson /*
1902ae7a6b38SJeff Roberson  * Record the sleep time for the interactivity scorer.
1903ae7a6b38SJeff Roberson  */
190435e6168fSJeff Roberson void
1905c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio)
190635e6168fSJeff Roberson {
1907e7d50326SJeff Roberson 
19087b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
190935e6168fSJeff Roberson 
191054b0e65fSJeff Roberson 	td->td_slptick = ticks;
1911c5aa6b58SJeff Roberson 	if (TD_IS_SUSPENDED(td) || prio <= PSOCK)
1912c5aa6b58SJeff Roberson 		td->td_flags |= TDF_CANSWAP;
19130502fe2eSJeff Roberson 	if (static_boost == 1 && prio)
1914c5aa6b58SJeff Roberson 		sched_prio(td, prio);
19150502fe2eSJeff Roberson 	else if (static_boost && td->td_priority > static_boost)
19160502fe2eSJeff Roberson 		sched_prio(td, static_boost);
191735e6168fSJeff Roberson }
191835e6168fSJeff Roberson 
1919ae7a6b38SJeff Roberson /*
1920ae7a6b38SJeff Roberson  * Schedule a thread to resume execution and record how long it voluntarily
1921ae7a6b38SJeff Roberson  * slept.  We also update the pctcpu, interactivity, and priority.
1922ae7a6b38SJeff Roberson  */
192335e6168fSJeff Roberson void
192435e6168fSJeff Roberson sched_wakeup(struct thread *td)
192535e6168fSJeff Roberson {
192614618990SJeff Roberson 	struct td_sched *ts;
1927ae7a6b38SJeff Roberson 	int slptick;
1928e7d50326SJeff Roberson 
19297b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
193014618990SJeff Roberson 	ts = td->td_sched;
1931c5aa6b58SJeff Roberson 	td->td_flags &= ~TDF_CANSWAP;
193235e6168fSJeff Roberson 	/*
1933e7d50326SJeff Roberson 	 * If we slept for more than a tick update our interactivity and
1934e7d50326SJeff Roberson 	 * priority.
193535e6168fSJeff Roberson 	 */
193654b0e65fSJeff Roberson 	slptick = td->td_slptick;
193754b0e65fSJeff Roberson 	td->td_slptick = 0;
1938ae7a6b38SJeff Roberson 	if (slptick && slptick != ticks) {
19399a93305aSJeff Roberson 		u_int hzticks;
1940f1e8dc4aSJeff Roberson 
1941ae7a6b38SJeff Roberson 		hzticks = (ticks - slptick) << SCHED_TICK_SHIFT;
1942ae7a6b38SJeff Roberson 		ts->ts_slptime += hzticks;
19438460a577SJohn Birrell 		sched_interact_update(td);
194414618990SJeff Roberson 		sched_pctcpu_update(ts);
1945f1e8dc4aSJeff Roberson 	}
194614618990SJeff Roberson 	/* Reset the slice value after we sleep. */
194714618990SJeff Roberson 	ts->ts_slice = sched_slice;
19487a5e5e2aSJeff Roberson 	sched_add(td, SRQ_BORING);
194935e6168fSJeff Roberson }
195035e6168fSJeff Roberson 
195135e6168fSJeff Roberson /*
195235e6168fSJeff Roberson  * Penalize the parent for creating a new child and initialize the child's
195335e6168fSJeff Roberson  * priority.
195435e6168fSJeff Roberson  */
195535e6168fSJeff Roberson void
19568460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child)
195715dc847eSJeff Roberson {
19587b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1959ad1e7d28SJulian Elischer 	sched_fork_thread(td, child);
1960e7d50326SJeff Roberson 	/*
1961e7d50326SJeff Roberson 	 * Penalize the parent and child for forking.
1962e7d50326SJeff Roberson 	 */
1963e7d50326SJeff Roberson 	sched_interact_fork(child);
1964e7d50326SJeff Roberson 	sched_priority(child);
1965ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += tickincr;
1966e7d50326SJeff Roberson 	sched_interact_update(td);
1967e7d50326SJeff Roberson 	sched_priority(td);
1968ad1e7d28SJulian Elischer }
1969ad1e7d28SJulian Elischer 
1970ae7a6b38SJeff Roberson /*
1971ae7a6b38SJeff Roberson  * Fork a new thread, may be within the same process.
1972ae7a6b38SJeff Roberson  */
1973ad1e7d28SJulian Elischer void
1974ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child)
1975ad1e7d28SJulian Elischer {
1976ad1e7d28SJulian Elischer 	struct td_sched *ts;
1977ad1e7d28SJulian Elischer 	struct td_sched *ts2;
19788460a577SJohn Birrell 
19798b16c208SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1980e7d50326SJeff Roberson 	/*
1981e7d50326SJeff Roberson 	 * Initialize child.
1982e7d50326SJeff Roberson 	 */
1983ad1e7d28SJulian Elischer 	ts = td->td_sched;
1984ad1e7d28SJulian Elischer 	ts2 = child->td_sched;
19858b16c208SJeff Roberson 	child->td_lock = TDQ_LOCKPTR(TDQ_SELF());
19868b16c208SJeff Roberson 	child->td_cpuset = cpuset_ref(td->td_cpuset);
1987ad1e7d28SJulian Elischer 	ts2->ts_cpu = ts->ts_cpu;
19888b16c208SJeff Roberson 	ts2->ts_flags = 0;
1989e7d50326SJeff Roberson 	/*
1990e7d50326SJeff Roberson 	 * Grab our parents cpu estimation information and priority.
1991e7d50326SJeff Roberson 	 */
1992ad1e7d28SJulian Elischer 	ts2->ts_ticks = ts->ts_ticks;
1993ad1e7d28SJulian Elischer 	ts2->ts_ltick = ts->ts_ltick;
1994ad1e7d28SJulian Elischer 	ts2->ts_ftick = ts->ts_ftick;
1995e7d50326SJeff Roberson 	child->td_user_pri = td->td_user_pri;
1996e7d50326SJeff Roberson 	child->td_base_user_pri = td->td_base_user_pri;
1997e7d50326SJeff Roberson 	/*
1998e7d50326SJeff Roberson 	 * And update interactivity score.
1999e7d50326SJeff Roberson 	 */
2000ae7a6b38SJeff Roberson 	ts2->ts_slptime = ts->ts_slptime;
2001ae7a6b38SJeff Roberson 	ts2->ts_runtime = ts->ts_runtime;
2002e7d50326SJeff Roberson 	ts2->ts_slice = 1;	/* Attempt to quickly learn interactivity. */
20038f51ad55SJeff Roberson #ifdef KTR
20048f51ad55SJeff Roberson 	bzero(ts2->ts_name, sizeof(ts2->ts_name));
20058f51ad55SJeff Roberson #endif
200615dc847eSJeff Roberson }
200715dc847eSJeff Roberson 
2008ae7a6b38SJeff Roberson /*
2009ae7a6b38SJeff Roberson  * Adjust the priority class of a thread.
2010ae7a6b38SJeff Roberson  */
201115dc847eSJeff Roberson void
20128460a577SJohn Birrell sched_class(struct thread *td, int class)
201315dc847eSJeff Roberson {
201415dc847eSJeff Roberson 
20157b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
20168460a577SJohn Birrell 	if (td->td_pri_class == class)
201715dc847eSJeff Roberson 		return;
20188460a577SJohn Birrell 	td->td_pri_class = class;
201935e6168fSJeff Roberson }
202035e6168fSJeff Roberson 
202135e6168fSJeff Roberson /*
202235e6168fSJeff Roberson  * Return some of the child's priority and interactivity to the parent.
202335e6168fSJeff Roberson  */
202435e6168fSJeff Roberson void
2025fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child)
202635e6168fSJeff Roberson {
2027e7d50326SJeff Roberson 	struct thread *td;
2028141ad61cSJeff Roberson 
20298f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit",
20308f51ad55SJeff Roberson 	    "prio:td", child->td_priority);
2031374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
2032e7d50326SJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
2033e7d50326SJeff Roberson 	sched_exit_thread(td, child);
2034ad1e7d28SJulian Elischer }
2035ad1e7d28SJulian Elischer 
2036ae7a6b38SJeff Roberson /*
2037ae7a6b38SJeff Roberson  * Penalize another thread for the time spent on this one.  This helps to
2038ae7a6b38SJeff Roberson  * worsen the priority and interactivity of processes which schedule batch
2039ae7a6b38SJeff Roberson  * jobs such as make.  This has little effect on the make process itself but
2040ae7a6b38SJeff Roberson  * causes new processes spawned by it to receive worse scores immediately.
2041ae7a6b38SJeff Roberson  */
2042ad1e7d28SJulian Elischer void
2043fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child)
2044ad1e7d28SJulian Elischer {
2045fc6c30f6SJulian Elischer 
20468f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit",
20478f51ad55SJeff Roberson 	    "prio:td", child->td_priority);
2048e7d50326SJeff Roberson 	/*
2049e7d50326SJeff Roberson 	 * Give the child's runtime to the parent without returning the
2050e7d50326SJeff Roberson 	 * sleep time as a penalty to the parent.  This causes shells that
2051e7d50326SJeff Roberson 	 * launch expensive things to mark their children as expensive.
2052e7d50326SJeff Roberson 	 */
20537b20fb19SJeff Roberson 	thread_lock(td);
2054ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += child->td_sched->ts_runtime;
2055fc6c30f6SJulian Elischer 	sched_interact_update(td);
2056e7d50326SJeff Roberson 	sched_priority(td);
20577b20fb19SJeff Roberson 	thread_unlock(td);
2058ad1e7d28SJulian Elischer }
2059ad1e7d28SJulian Elischer 
2060ff256d9cSJeff Roberson void
2061ff256d9cSJeff Roberson sched_preempt(struct thread *td)
2062ff256d9cSJeff Roberson {
2063ff256d9cSJeff Roberson 	struct tdq *tdq;
2064ff256d9cSJeff Roberson 
2065ff256d9cSJeff Roberson 	thread_lock(td);
2066ff256d9cSJeff Roberson 	tdq = TDQ_SELF();
2067ff256d9cSJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2068ff256d9cSJeff Roberson 	tdq->tdq_ipipending = 0;
2069ff256d9cSJeff Roberson 	if (td->td_priority > tdq->tdq_lowpri) {
20708df78c41SJeff Roberson 		int flags;
20718df78c41SJeff Roberson 
20728df78c41SJeff Roberson 		flags = SW_INVOL | SW_PREEMPT;
2073ff256d9cSJeff Roberson 		if (td->td_critnest > 1)
2074ff256d9cSJeff Roberson 			td->td_owepreempt = 1;
20758df78c41SJeff Roberson 		else if (TD_IS_IDLETHREAD(td))
20768df78c41SJeff Roberson 			mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL);
2077ff256d9cSJeff Roberson 		else
20788df78c41SJeff Roberson 			mi_switch(flags | SWT_REMOTEPREEMPT, NULL);
2079ff256d9cSJeff Roberson 	}
2080ff256d9cSJeff Roberson 	thread_unlock(td);
2081ff256d9cSJeff Roberson }
2082ff256d9cSJeff Roberson 
2083ae7a6b38SJeff Roberson /*
2084ae7a6b38SJeff Roberson  * Fix priorities on return to user-space.  Priorities may be elevated due
2085ae7a6b38SJeff Roberson  * to static priorities in msleep() or similar.
2086ae7a6b38SJeff Roberson  */
2087ad1e7d28SJulian Elischer void
2088ad1e7d28SJulian Elischer sched_userret(struct thread *td)
2089ad1e7d28SJulian Elischer {
2090ad1e7d28SJulian Elischer 	/*
2091ad1e7d28SJulian Elischer 	 * XXX we cheat slightly on the locking here to avoid locking in
2092ad1e7d28SJulian Elischer 	 * the usual case.  Setting td_priority here is essentially an
2093ad1e7d28SJulian Elischer 	 * incomplete workaround for not setting it properly elsewhere.
2094ad1e7d28SJulian Elischer 	 * Now that some interrupt handlers are threads, not setting it
2095ad1e7d28SJulian Elischer 	 * properly elsewhere can clobber it in the window between setting
2096ad1e7d28SJulian Elischer 	 * it here and returning to user mode, so don't waste time setting
2097ad1e7d28SJulian Elischer 	 * it perfectly here.
2098ad1e7d28SJulian Elischer 	 */
2099ad1e7d28SJulian Elischer 	KASSERT((td->td_flags & TDF_BORROWING) == 0,
2100ad1e7d28SJulian Elischer 	    ("thread with borrowed priority returning to userland"));
2101ad1e7d28SJulian Elischer 	if (td->td_priority != td->td_user_pri) {
21027b20fb19SJeff Roberson 		thread_lock(td);
2103ad1e7d28SJulian Elischer 		td->td_priority = td->td_user_pri;
2104ad1e7d28SJulian Elischer 		td->td_base_pri = td->td_user_pri;
210562fa74d9SJeff Roberson 		tdq_setlowpri(TDQ_SELF(), td);
21067b20fb19SJeff Roberson 		thread_unlock(td);
2107ad1e7d28SJulian Elischer         }
210835e6168fSJeff Roberson }
210935e6168fSJeff Roberson 
2110ae7a6b38SJeff Roberson /*
2111ae7a6b38SJeff Roberson  * Handle a stathz tick.  This is really only relevant for timeshare
2112ae7a6b38SJeff Roberson  * threads.
2113ae7a6b38SJeff Roberson  */
211435e6168fSJeff Roberson void
21157cf90fb3SJeff Roberson sched_clock(struct thread *td)
211635e6168fSJeff Roberson {
2117ad1e7d28SJulian Elischer 	struct tdq *tdq;
2118ad1e7d28SJulian Elischer 	struct td_sched *ts;
211935e6168fSJeff Roberson 
2120ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
21213f872f85SJeff Roberson 	tdq = TDQ_SELF();
21227fcf154aSJeff Roberson #ifdef SMP
21237fcf154aSJeff Roberson 	/*
21247fcf154aSJeff Roberson 	 * We run the long term load balancer infrequently on the first cpu.
21257fcf154aSJeff Roberson 	 */
21267fcf154aSJeff Roberson 	if (balance_tdq == tdq) {
21277fcf154aSJeff Roberson 		if (balance_ticks && --balance_ticks == 0)
21287fcf154aSJeff Roberson 			sched_balance();
21297fcf154aSJeff Roberson 	}
21307fcf154aSJeff Roberson #endif
21313f872f85SJeff Roberson 	/*
21321690c6c1SJeff Roberson 	 * Save the old switch count so we have a record of the last ticks
21331690c6c1SJeff Roberson 	 * activity.   Initialize the new switch count based on our load.
21341690c6c1SJeff Roberson 	 * If there is some activity seed it to reflect that.
21351690c6c1SJeff Roberson 	 */
21361690c6c1SJeff Roberson 	tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt;
21376c47aaaeSJeff Roberson 	tdq->tdq_switchcnt = tdq->tdq_load;
21381690c6c1SJeff Roberson 	/*
21393f872f85SJeff Roberson 	 * Advance the insert index once for each tick to ensure that all
21403f872f85SJeff Roberson 	 * threads get a chance to run.
21413f872f85SJeff Roberson 	 */
21423f872f85SJeff Roberson 	if (tdq->tdq_idx == tdq->tdq_ridx) {
21433f872f85SJeff Roberson 		tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
21443f872f85SJeff Roberson 		if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
21453f872f85SJeff Roberson 			tdq->tdq_ridx = tdq->tdq_idx;
21463f872f85SJeff Roberson 	}
21473f872f85SJeff Roberson 	ts = td->td_sched;
2148fd0b8c78SJeff Roberson 	if (td->td_pri_class & PRI_FIFO_BIT)
2149a8949de2SJeff Roberson 		return;
2150fd0b8c78SJeff Roberson 	if (td->td_pri_class == PRI_TIMESHARE) {
2151a8949de2SJeff Roberson 		/*
2152fd0b8c78SJeff Roberson 		 * We used a tick; charge it to the thread so
2153fd0b8c78SJeff Roberson 		 * that we can compute our interactivity.
215415dc847eSJeff Roberson 		 */
2155ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime += tickincr;
21568460a577SJohn Birrell 		sched_interact_update(td);
215773daf66fSJeff Roberson 		sched_priority(td);
2158fd0b8c78SJeff Roberson 	}
215935e6168fSJeff Roberson 	/*
216035e6168fSJeff Roberson 	 * We used up one time slice.
216135e6168fSJeff Roberson 	 */
2162ad1e7d28SJulian Elischer 	if (--ts->ts_slice > 0)
216315dc847eSJeff Roberson 		return;
216435e6168fSJeff Roberson 	/*
216573daf66fSJeff Roberson 	 * We're out of time, force a requeue at userret().
216635e6168fSJeff Roberson 	 */
216773daf66fSJeff Roberson 	ts->ts_slice = sched_slice;
21684a338afdSJulian Elischer 	td->td_flags |= TDF_NEEDRESCHED;
216935e6168fSJeff Roberson }
217035e6168fSJeff Roberson 
2171ae7a6b38SJeff Roberson /*
2172ae7a6b38SJeff Roberson  * Called once per hz tick.  Used for cpu utilization information.  This
2173ae7a6b38SJeff Roberson  * is easier than trying to scale based on stathz.
2174ae7a6b38SJeff Roberson  */
2175ae7a6b38SJeff Roberson void
2176ae7a6b38SJeff Roberson sched_tick(void)
2177ae7a6b38SJeff Roberson {
2178ae7a6b38SJeff Roberson 	struct td_sched *ts;
2179ae7a6b38SJeff Roberson 
2180ae7a6b38SJeff Roberson 	ts = curthread->td_sched;
2181e980fff6SJeff Roberson 	/*
2182e980fff6SJeff Roberson 	 * Ticks is updated asynchronously on a single cpu.  Check here to
2183e980fff6SJeff Roberson 	 * avoid incrementing ts_ticks multiple times in a single tick.
2184e980fff6SJeff Roberson 	 */
2185e980fff6SJeff Roberson 	if (ts->ts_ltick == ticks)
2186e980fff6SJeff Roberson 		return;
2187ae7a6b38SJeff Roberson 	/* Adjust ticks for pctcpu */
2188ae7a6b38SJeff Roberson 	ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
2189ae7a6b38SJeff Roberson 	ts->ts_ltick = ticks;
2190ae7a6b38SJeff Roberson 	/*
2191ae7a6b38SJeff Roberson 	 * Update if we've exceeded our desired tick threshhold by over one
2192ae7a6b38SJeff Roberson 	 * second.
2193ae7a6b38SJeff Roberson 	 */
2194ae7a6b38SJeff Roberson 	if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
2195ae7a6b38SJeff Roberson 		sched_pctcpu_update(ts);
2196ae7a6b38SJeff Roberson }
2197ae7a6b38SJeff Roberson 
2198ae7a6b38SJeff Roberson /*
2199ae7a6b38SJeff Roberson  * Return whether the current CPU has runnable tasks.  Used for in-kernel
2200ae7a6b38SJeff Roberson  * cooperative idle threads.
2201ae7a6b38SJeff Roberson  */
220235e6168fSJeff Roberson int
220335e6168fSJeff Roberson sched_runnable(void)
220435e6168fSJeff Roberson {
2205ad1e7d28SJulian Elischer 	struct tdq *tdq;
2206b90816f1SJeff Roberson 	int load;
220735e6168fSJeff Roberson 
2208b90816f1SJeff Roberson 	load = 1;
2209b90816f1SJeff Roberson 
2210ad1e7d28SJulian Elischer 	tdq = TDQ_SELF();
22113f741ca1SJeff Roberson 	if ((curthread->td_flags & TDF_IDLETD) != 0) {
2212d2ad694cSJeff Roberson 		if (tdq->tdq_load > 0)
22133f741ca1SJeff Roberson 			goto out;
22143f741ca1SJeff Roberson 	} else
2215d2ad694cSJeff Roberson 		if (tdq->tdq_load - 1 > 0)
2216b90816f1SJeff Roberson 			goto out;
2217b90816f1SJeff Roberson 	load = 0;
2218b90816f1SJeff Roberson out:
2219b90816f1SJeff Roberson 	return (load);
222035e6168fSJeff Roberson }
222135e6168fSJeff Roberson 
2222ae7a6b38SJeff Roberson /*
2223ae7a6b38SJeff Roberson  * Choose the highest priority thread to run.  The thread is removed from
2224ae7a6b38SJeff Roberson  * the run-queue while running however the load remains.  For SMP we set
2225ae7a6b38SJeff Roberson  * the tdq in the global idle bitmask if it idles here.
2226ae7a6b38SJeff Roberson  */
22277a5e5e2aSJeff Roberson struct thread *
2228c9f25d8fSJeff Roberson sched_choose(void)
2229c9f25d8fSJeff Roberson {
22309727e637SJeff Roberson 	struct thread *td;
2231ae7a6b38SJeff Roberson 	struct tdq *tdq;
2232ae7a6b38SJeff Roberson 
2233ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2234ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
22359727e637SJeff Roberson 	td = tdq_choose(tdq);
22369727e637SJeff Roberson 	if (td) {
22379727e637SJeff Roberson 		td->td_sched->ts_ltick = ticks;
22389727e637SJeff Roberson 		tdq_runq_rem(tdq, td);
22390502fe2eSJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
22409727e637SJeff Roberson 		return (td);
224135e6168fSJeff Roberson 	}
22420502fe2eSJeff Roberson 	tdq->tdq_lowpri = PRI_MAX_IDLE;
224362fa74d9SJeff Roberson 	return (PCPU_GET(idlethread));
22447a5e5e2aSJeff Roberson }
22457a5e5e2aSJeff Roberson 
2246ae7a6b38SJeff Roberson /*
2247ae7a6b38SJeff Roberson  * Set owepreempt if necessary.  Preemption never happens directly in ULE,
2248ae7a6b38SJeff Roberson  * we always request it once we exit a critical section.
2249ae7a6b38SJeff Roberson  */
2250ae7a6b38SJeff Roberson static inline void
2251ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td)
22527a5e5e2aSJeff Roberson {
22537a5e5e2aSJeff Roberson 	struct thread *ctd;
22547a5e5e2aSJeff Roberson 	int cpri;
22557a5e5e2aSJeff Roberson 	int pri;
22567a5e5e2aSJeff Roberson 
2257ff256d9cSJeff Roberson 	THREAD_LOCK_ASSERT(curthread, MA_OWNED);
2258ff256d9cSJeff Roberson 
22597a5e5e2aSJeff Roberson 	ctd = curthread;
22607a5e5e2aSJeff Roberson 	pri = td->td_priority;
22617a5e5e2aSJeff Roberson 	cpri = ctd->td_priority;
2262ff256d9cSJeff Roberson 	if (pri < cpri)
2263ff256d9cSJeff Roberson 		ctd->td_flags |= TDF_NEEDRESCHED;
22647a5e5e2aSJeff Roberson 	if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
2265ae7a6b38SJeff Roberson 		return;
2266ff256d9cSJeff Roberson 	if (!sched_shouldpreempt(pri, cpri, 0))
2267ae7a6b38SJeff Roberson 		return;
22687a5e5e2aSJeff Roberson 	ctd->td_owepreempt = 1;
226935e6168fSJeff Roberson }
227035e6168fSJeff Roberson 
2271ae7a6b38SJeff Roberson /*
227273daf66fSJeff Roberson  * Add a thread to a thread queue.  Select the appropriate runq and add the
227373daf66fSJeff Roberson  * thread to it.  This is the internal function called when the tdq is
227473daf66fSJeff Roberson  * predetermined.
2275ae7a6b38SJeff Roberson  */
227635e6168fSJeff Roberson void
2277ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags)
227835e6168fSJeff Roberson {
2279c9f25d8fSJeff Roberson 
2280ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
22817a5e5e2aSJeff Roberson 	KASSERT((td->td_inhibitors == 0),
22827a5e5e2aSJeff Roberson 	    ("sched_add: trying to run inhibited thread"));
22837a5e5e2aSJeff Roberson 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
22847a5e5e2aSJeff Roberson 	    ("sched_add: bad thread state"));
2285b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
2286b61ce5b0SJeff Roberson 	    ("sched_add: thread swapped out"));
2287ae7a6b38SJeff Roberson 
2288ae7a6b38SJeff Roberson 	if (td->td_priority < tdq->tdq_lowpri)
2289ae7a6b38SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
22909727e637SJeff Roberson 	tdq_runq_add(tdq, td, flags);
22919727e637SJeff Roberson 	tdq_load_add(tdq, td);
2292ae7a6b38SJeff Roberson }
2293ae7a6b38SJeff Roberson 
2294ae7a6b38SJeff Roberson /*
2295ae7a6b38SJeff Roberson  * Select the target thread queue and add a thread to it.  Request
2296ae7a6b38SJeff Roberson  * preemption or IPI a remote processor if required.
2297ae7a6b38SJeff Roberson  */
2298ae7a6b38SJeff Roberson void
2299ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags)
2300ae7a6b38SJeff Roberson {
2301ae7a6b38SJeff Roberson 	struct tdq *tdq;
23027b8bfa0dSJeff Roberson #ifdef SMP
2303ae7a6b38SJeff Roberson 	int cpu;
2304ae7a6b38SJeff Roberson #endif
23058f51ad55SJeff Roberson 
23068f51ad55SJeff Roberson 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add",
23078f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
23088f51ad55SJeff Roberson 	    sched_tdname(curthread));
23098f51ad55SJeff Roberson 	KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup",
23108f51ad55SJeff Roberson 	    KTR_ATTR_LINKED, sched_tdname(td));
2311ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2312ae7a6b38SJeff Roberson 	/*
2313ae7a6b38SJeff Roberson 	 * Recalculate the priority before we select the target cpu or
2314ae7a6b38SJeff Roberson 	 * run-queue.
2315ae7a6b38SJeff Roberson 	 */
2316ae7a6b38SJeff Roberson 	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
2317ae7a6b38SJeff Roberson 		sched_priority(td);
2318ae7a6b38SJeff Roberson #ifdef SMP
2319ae7a6b38SJeff Roberson 	/*
2320ae7a6b38SJeff Roberson 	 * Pick the destination cpu and if it isn't ours transfer to the
2321ae7a6b38SJeff Roberson 	 * target cpu.
2322ae7a6b38SJeff Roberson 	 */
23239727e637SJeff Roberson 	cpu = sched_pickcpu(td, flags);
23249727e637SJeff Roberson 	tdq = sched_setcpu(td, cpu, flags);
2325ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
232673daf66fSJeff Roberson 	if (cpu != PCPU_GET(cpuid)) {
23279727e637SJeff Roberson 		tdq_notify(tdq, td);
23287b8bfa0dSJeff Roberson 		return;
23297b8bfa0dSJeff Roberson 	}
2330ae7a6b38SJeff Roberson #else
2331ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2332ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
2333ae7a6b38SJeff Roberson 	/*
2334ae7a6b38SJeff Roberson 	 * Now that the thread is moving to the run-queue, set the lock
2335ae7a6b38SJeff Roberson 	 * to the scheduler's lock.
2336ae7a6b38SJeff Roberson 	 */
2337ae7a6b38SJeff Roberson 	thread_lock_set(td, TDQ_LOCKPTR(tdq));
2338ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
23397b8bfa0dSJeff Roberson #endif
2340ae7a6b38SJeff Roberson 	if (!(flags & SRQ_YIELDING))
2341ae7a6b38SJeff Roberson 		sched_setpreempt(td);
234235e6168fSJeff Roberson }
234335e6168fSJeff Roberson 
2344ae7a6b38SJeff Roberson /*
2345ae7a6b38SJeff Roberson  * Remove a thread from a run-queue without running it.  This is used
2346ae7a6b38SJeff Roberson  * when we're stealing a thread from a remote queue.  Otherwise all threads
2347ae7a6b38SJeff Roberson  * exit by calling sched_exit_thread() and sched_throw() themselves.
2348ae7a6b38SJeff Roberson  */
234935e6168fSJeff Roberson void
23507cf90fb3SJeff Roberson sched_rem(struct thread *td)
235135e6168fSJeff Roberson {
2352ad1e7d28SJulian Elischer 	struct tdq *tdq;
23537cf90fb3SJeff Roberson 
23548f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem",
23558f51ad55SJeff Roberson 	    "prio:%d", td->td_priority);
23569727e637SJeff Roberson 	tdq = TDQ_CPU(td->td_sched->ts_cpu);
2357ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2358ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
23597a5e5e2aSJeff Roberson 	KASSERT(TD_ON_RUNQ(td),
2360ad1e7d28SJulian Elischer 	    ("sched_rem: thread not on run queue"));
23619727e637SJeff Roberson 	tdq_runq_rem(tdq, td);
23629727e637SJeff Roberson 	tdq_load_rem(tdq, td);
23637a5e5e2aSJeff Roberson 	TD_SET_CAN_RUN(td);
236462fa74d9SJeff Roberson 	if (td->td_priority == tdq->tdq_lowpri)
236562fa74d9SJeff Roberson 		tdq_setlowpri(tdq, NULL);
236635e6168fSJeff Roberson }
236735e6168fSJeff Roberson 
2368ae7a6b38SJeff Roberson /*
2369ae7a6b38SJeff Roberson  * Fetch cpu utilization information.  Updates on demand.
2370ae7a6b38SJeff Roberson  */
237135e6168fSJeff Roberson fixpt_t
23727cf90fb3SJeff Roberson sched_pctcpu(struct thread *td)
237335e6168fSJeff Roberson {
237435e6168fSJeff Roberson 	fixpt_t pctcpu;
2375ad1e7d28SJulian Elischer 	struct td_sched *ts;
237635e6168fSJeff Roberson 
237735e6168fSJeff Roberson 	pctcpu = 0;
2378ad1e7d28SJulian Elischer 	ts = td->td_sched;
2379ad1e7d28SJulian Elischer 	if (ts == NULL)
2380484288deSJeff Roberson 		return (0);
238135e6168fSJeff Roberson 
23827b20fb19SJeff Roberson 	thread_lock(td);
2383ad1e7d28SJulian Elischer 	if (ts->ts_ticks) {
238435e6168fSJeff Roberson 		int rtick;
238535e6168fSJeff Roberson 
2386ad1e7d28SJulian Elischer 		sched_pctcpu_update(ts);
238735e6168fSJeff Roberson 		/* How many rtick per second ? */
2388e7d50326SJeff Roberson 		rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
2389e7d50326SJeff Roberson 		pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
239035e6168fSJeff Roberson 	}
23917b20fb19SJeff Roberson 	thread_unlock(td);
239235e6168fSJeff Roberson 
239335e6168fSJeff Roberson 	return (pctcpu);
239435e6168fSJeff Roberson }
239535e6168fSJeff Roberson 
239662fa74d9SJeff Roberson /*
239762fa74d9SJeff Roberson  * Enforce affinity settings for a thread.  Called after adjustments to
239862fa74d9SJeff Roberson  * cpumask.
239962fa74d9SJeff Roberson  */
2400885d51a3SJeff Roberson void
2401885d51a3SJeff Roberson sched_affinity(struct thread *td)
2402885d51a3SJeff Roberson {
240362fa74d9SJeff Roberson #ifdef SMP
240462fa74d9SJeff Roberson 	struct td_sched *ts;
240562fa74d9SJeff Roberson 	int cpu;
240662fa74d9SJeff Roberson 
240762fa74d9SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
240862fa74d9SJeff Roberson 	ts = td->td_sched;
240962fa74d9SJeff Roberson 	if (THREAD_CAN_SCHED(td, ts->ts_cpu))
241062fa74d9SJeff Roberson 		return;
241153a6c8b3SJeff Roberson 	if (TD_ON_RUNQ(td)) {
241253a6c8b3SJeff Roberson 		sched_rem(td);
241353a6c8b3SJeff Roberson 		sched_add(td, SRQ_BORING);
241453a6c8b3SJeff Roberson 		return;
241553a6c8b3SJeff Roberson 	}
241662fa74d9SJeff Roberson 	if (!TD_IS_RUNNING(td))
241762fa74d9SJeff Roberson 		return;
241862fa74d9SJeff Roberson 	td->td_flags |= TDF_NEEDRESCHED;
241962fa74d9SJeff Roberson 	if (!THREAD_CAN_MIGRATE(td))
242062fa74d9SJeff Roberson 		return;
242162fa74d9SJeff Roberson 	/*
242262fa74d9SJeff Roberson 	 * Assign the new cpu and force a switch before returning to
242362fa74d9SJeff Roberson 	 * userspace.  If the target thread is not running locally send
242462fa74d9SJeff Roberson 	 * an ipi to force the issue.
242562fa74d9SJeff Roberson 	 */
242662fa74d9SJeff Roberson 	cpu = ts->ts_cpu;
24279727e637SJeff Roberson 	ts->ts_cpu = sched_pickcpu(td, 0);
242862fa74d9SJeff Roberson 	if (cpu != PCPU_GET(cpuid))
242962fa74d9SJeff Roberson 		ipi_selected(1 << cpu, IPI_PREEMPT);
243062fa74d9SJeff Roberson #endif
2431885d51a3SJeff Roberson }
2432885d51a3SJeff Roberson 
2433ae7a6b38SJeff Roberson /*
2434ae7a6b38SJeff Roberson  * Bind a thread to a target cpu.
2435ae7a6b38SJeff Roberson  */
24369bacd788SJeff Roberson void
24379bacd788SJeff Roberson sched_bind(struct thread *td, int cpu)
24389bacd788SJeff Roberson {
2439ad1e7d28SJulian Elischer 	struct td_sched *ts;
24409bacd788SJeff Roberson 
2441c47f202bSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
2442ad1e7d28SJulian Elischer 	ts = td->td_sched;
24436b2f763fSJeff Roberson 	if (ts->ts_flags & TSF_BOUND)
2444c95d2db2SJeff Roberson 		sched_unbind(td);
2445ad1e7d28SJulian Elischer 	ts->ts_flags |= TSF_BOUND;
24466b2f763fSJeff Roberson 	sched_pin();
244780f86c9fSJeff Roberson 	if (PCPU_GET(cpuid) == cpu)
24489bacd788SJeff Roberson 		return;
24496b2f763fSJeff Roberson 	ts->ts_cpu = cpu;
24509bacd788SJeff Roberson 	/* When we return from mi_switch we'll be on the correct cpu. */
2451279f949eSPoul-Henning Kamp 	mi_switch(SW_VOL, NULL);
24529bacd788SJeff Roberson }
24539bacd788SJeff Roberson 
2454ae7a6b38SJeff Roberson /*
2455ae7a6b38SJeff Roberson  * Release a bound thread.
2456ae7a6b38SJeff Roberson  */
24579bacd788SJeff Roberson void
24589bacd788SJeff Roberson sched_unbind(struct thread *td)
24599bacd788SJeff Roberson {
2460e7d50326SJeff Roberson 	struct td_sched *ts;
2461e7d50326SJeff Roberson 
24627b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2463e7d50326SJeff Roberson 	ts = td->td_sched;
24646b2f763fSJeff Roberson 	if ((ts->ts_flags & TSF_BOUND) == 0)
24656b2f763fSJeff Roberson 		return;
2466e7d50326SJeff Roberson 	ts->ts_flags &= ~TSF_BOUND;
2467e7d50326SJeff Roberson 	sched_unpin();
24689bacd788SJeff Roberson }
24699bacd788SJeff Roberson 
247035e6168fSJeff Roberson int
2471ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td)
2472ebccf1e3SJoseph Koshy {
24737b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2474ad1e7d28SJulian Elischer 	return (td->td_sched->ts_flags & TSF_BOUND);
2475ebccf1e3SJoseph Koshy }
2476ebccf1e3SJoseph Koshy 
2477ae7a6b38SJeff Roberson /*
2478ae7a6b38SJeff Roberson  * Basic yield call.
2479ae7a6b38SJeff Roberson  */
248036ec198bSDavid Xu void
248136ec198bSDavid Xu sched_relinquish(struct thread *td)
248236ec198bSDavid Xu {
24837b20fb19SJeff Roberson 	thread_lock(td);
24848df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
24857b20fb19SJeff Roberson 	thread_unlock(td);
248636ec198bSDavid Xu }
248736ec198bSDavid Xu 
2488ae7a6b38SJeff Roberson /*
2489ae7a6b38SJeff Roberson  * Return the total system load.
2490ae7a6b38SJeff Roberson  */
2491ebccf1e3SJoseph Koshy int
249233916c36SJeff Roberson sched_load(void)
249333916c36SJeff Roberson {
249433916c36SJeff Roberson #ifdef SMP
249533916c36SJeff Roberson 	int total;
249633916c36SJeff Roberson 	int i;
249733916c36SJeff Roberson 
249833916c36SJeff Roberson 	total = 0;
249962fa74d9SJeff Roberson 	for (i = 0; i <= mp_maxid; i++)
250062fa74d9SJeff Roberson 		total += TDQ_CPU(i)->tdq_sysload;
250133916c36SJeff Roberson 	return (total);
250233916c36SJeff Roberson #else
2503d2ad694cSJeff Roberson 	return (TDQ_SELF()->tdq_sysload);
250433916c36SJeff Roberson #endif
250533916c36SJeff Roberson }
250633916c36SJeff Roberson 
250733916c36SJeff Roberson int
250835e6168fSJeff Roberson sched_sizeof_proc(void)
250935e6168fSJeff Roberson {
251035e6168fSJeff Roberson 	return (sizeof(struct proc));
251135e6168fSJeff Roberson }
251235e6168fSJeff Roberson 
251335e6168fSJeff Roberson int
251435e6168fSJeff Roberson sched_sizeof_thread(void)
251535e6168fSJeff Roberson {
251635e6168fSJeff Roberson 	return (sizeof(struct thread) + sizeof(struct td_sched));
251735e6168fSJeff Roberson }
2518b41f1452SDavid Xu 
251909c8a4ccSJeff Roberson #ifdef SMP
252009c8a4ccSJeff Roberson #define	TDQ_IDLESPIN(tdq)						\
252109c8a4ccSJeff Roberson     ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0)
252209c8a4ccSJeff Roberson #else
252309c8a4ccSJeff Roberson #define	TDQ_IDLESPIN(tdq)	1
252409c8a4ccSJeff Roberson #endif
252509c8a4ccSJeff Roberson 
25267a5e5e2aSJeff Roberson /*
25277a5e5e2aSJeff Roberson  * The actual idle process.
25287a5e5e2aSJeff Roberson  */
25297a5e5e2aSJeff Roberson void
25307a5e5e2aSJeff Roberson sched_idletd(void *dummy)
25317a5e5e2aSJeff Roberson {
25327a5e5e2aSJeff Roberson 	struct thread *td;
2533ae7a6b38SJeff Roberson 	struct tdq *tdq;
25341690c6c1SJeff Roberson 	int switchcnt;
25351690c6c1SJeff Roberson 	int i;
25367a5e5e2aSJeff Roberson 
25377b55ab05SJeff Roberson 	mtx_assert(&Giant, MA_NOTOWNED);
25387a5e5e2aSJeff Roberson 	td = curthread;
2539ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2540ae7a6b38SJeff Roberson 	for (;;) {
2541ae7a6b38SJeff Roberson #ifdef SMP
25421690c6c1SJeff Roberson 		if (tdq_idled(tdq) == 0)
25431690c6c1SJeff Roberson 			continue;
2544ae7a6b38SJeff Roberson #endif
25451690c6c1SJeff Roberson 		switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt;
25461690c6c1SJeff Roberson 		/*
25471690c6c1SJeff Roberson 		 * If we're switching very frequently, spin while checking
25481690c6c1SJeff Roberson 		 * for load rather than entering a low power state that
25497b55ab05SJeff Roberson 		 * may require an IPI.  However, don't do any busy
25507b55ab05SJeff Roberson 		 * loops while on SMT machines as this simply steals
25517b55ab05SJeff Roberson 		 * cycles from cores doing useful work.
25521690c6c1SJeff Roberson 		 */
255309c8a4ccSJeff Roberson 		if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) {
25541690c6c1SJeff Roberson 			for (i = 0; i < sched_idlespins; i++) {
25551690c6c1SJeff Roberson 				if (tdq->tdq_load)
25561690c6c1SJeff Roberson 					break;
25571690c6c1SJeff Roberson 				cpu_spinwait();
25581690c6c1SJeff Roberson 			}
25591690c6c1SJeff Roberson 		}
25606c47aaaeSJeff Roberson 		switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt;
25611690c6c1SJeff Roberson 		if (tdq->tdq_load == 0)
25626c47aaaeSJeff Roberson 			cpu_idle(switchcnt > 1);
25631690c6c1SJeff Roberson 		if (tdq->tdq_load) {
25641690c6c1SJeff Roberson 			thread_lock(td);
25651690c6c1SJeff Roberson 			mi_switch(SW_VOL | SWT_IDLE, NULL);
25661690c6c1SJeff Roberson 			thread_unlock(td);
25671690c6c1SJeff Roberson 		}
2568ae7a6b38SJeff Roberson 	}
2569b41f1452SDavid Xu }
2570e7d50326SJeff Roberson 
25717b20fb19SJeff Roberson /*
25727b20fb19SJeff Roberson  * A CPU is entering for the first time or a thread is exiting.
25737b20fb19SJeff Roberson  */
25747b20fb19SJeff Roberson void
25757b20fb19SJeff Roberson sched_throw(struct thread *td)
25767b20fb19SJeff Roberson {
257759c68134SJeff Roberson 	struct thread *newtd;
2578ae7a6b38SJeff Roberson 	struct tdq *tdq;
2579ae7a6b38SJeff Roberson 
2580ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
25817b20fb19SJeff Roberson 	if (td == NULL) {
2582ae7a6b38SJeff Roberson 		/* Correct spinlock nesting and acquire the correct lock. */
2583ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
25847b20fb19SJeff Roberson 		spinlock_exit();
25857b20fb19SJeff Roberson 	} else {
2586ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
25879727e637SJeff Roberson 		tdq_load_rem(tdq, td);
2588eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
25897b20fb19SJeff Roberson 	}
25907b20fb19SJeff Roberson 	KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
259159c68134SJeff Roberson 	newtd = choosethread();
259259c68134SJeff Roberson 	TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
25937b20fb19SJeff Roberson 	PCPU_SET(switchtime, cpu_ticks());
25947b20fb19SJeff Roberson 	PCPU_SET(switchticks, ticks);
259559c68134SJeff Roberson 	cpu_throw(td, newtd);		/* doesn't return */
25967b20fb19SJeff Roberson }
25977b20fb19SJeff Roberson 
2598ae7a6b38SJeff Roberson /*
2599ae7a6b38SJeff Roberson  * This is called from fork_exit().  Just acquire the correct locks and
2600ae7a6b38SJeff Roberson  * let fork do the rest of the work.
2601ae7a6b38SJeff Roberson  */
26027b20fb19SJeff Roberson void
2603fe54587fSJeff Roberson sched_fork_exit(struct thread *td)
26047b20fb19SJeff Roberson {
2605ae7a6b38SJeff Roberson 	struct td_sched *ts;
2606ae7a6b38SJeff Roberson 	struct tdq *tdq;
2607ae7a6b38SJeff Roberson 	int cpuid;
26087b20fb19SJeff Roberson 
26097b20fb19SJeff Roberson 	/*
26107b20fb19SJeff Roberson 	 * Finish setting up thread glue so that it begins execution in a
2611ae7a6b38SJeff Roberson 	 * non-nested critical section with the scheduler lock held.
26127b20fb19SJeff Roberson 	 */
2613ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
2614ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
2615ae7a6b38SJeff Roberson 	ts = td->td_sched;
2616ae7a6b38SJeff Roberson 	if (TD_IS_IDLETHREAD(td))
2617ae7a6b38SJeff Roberson 		td->td_lock = TDQ_LOCKPTR(tdq);
2618ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2619ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
262059c68134SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
2621eea4f254SJeff Roberson 	lock_profile_obtain_lock_success(
2622eea4f254SJeff Roberson 	    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
26237b20fb19SJeff Roberson }
26247b20fb19SJeff Roberson 
26258f51ad55SJeff Roberson /*
26268f51ad55SJeff Roberson  * Create on first use to catch odd startup conditons.
26278f51ad55SJeff Roberson  */
26288f51ad55SJeff Roberson char *
26298f51ad55SJeff Roberson sched_tdname(struct thread *td)
26308f51ad55SJeff Roberson {
26318f51ad55SJeff Roberson #ifdef KTR
26328f51ad55SJeff Roberson 	struct td_sched *ts;
26338f51ad55SJeff Roberson 
26348f51ad55SJeff Roberson 	ts = td->td_sched;
26358f51ad55SJeff Roberson 	if (ts->ts_name[0] == '\0')
26368f51ad55SJeff Roberson 		snprintf(ts->ts_name, sizeof(ts->ts_name),
26378f51ad55SJeff Roberson 		    "%s tid %d", td->td_name, td->td_tid);
26388f51ad55SJeff Roberson 	return (ts->ts_name);
26398f51ad55SJeff Roberson #else
26408f51ad55SJeff Roberson 	return (td->td_name);
26418f51ad55SJeff Roberson #endif
26428f51ad55SJeff Roberson }
26438f51ad55SJeff Roberson 
264407095abfSIvan Voras #ifdef SMP
264507095abfSIvan Voras 
264607095abfSIvan Voras /*
264707095abfSIvan Voras  * Build the CPU topology dump string. Is recursively called to collect
264807095abfSIvan Voras  * the topology tree.
264907095abfSIvan Voras  */
265007095abfSIvan Voras static int
265107095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg,
265207095abfSIvan Voras     int indent)
265307095abfSIvan Voras {
265407095abfSIvan Voras 	int i, first;
265507095abfSIvan Voras 
265607095abfSIvan Voras 	sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent,
265707095abfSIvan Voras 	    "", indent, cg->cg_level);
265807095abfSIvan Voras 	sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"0x%x\">", indent, "",
265907095abfSIvan Voras 	    cg->cg_count, cg->cg_mask);
266007095abfSIvan Voras 	first = TRUE;
266107095abfSIvan Voras 	for (i = 0; i < MAXCPU; i++) {
266207095abfSIvan Voras 		if ((cg->cg_mask & (1 << i)) != 0) {
266307095abfSIvan Voras 			if (!first)
266407095abfSIvan Voras 				sbuf_printf(sb, ", ");
266507095abfSIvan Voras 			else
266607095abfSIvan Voras 				first = FALSE;
266707095abfSIvan Voras 			sbuf_printf(sb, "%d", i);
266807095abfSIvan Voras 		}
266907095abfSIvan Voras 	}
267007095abfSIvan Voras 	sbuf_printf(sb, "</cpu>\n");
267107095abfSIvan Voras 
267207095abfSIvan Voras 	sbuf_printf(sb, "%*s <flags>", indent, "");
267307095abfSIvan Voras 	if (cg->cg_flags != 0) {
267407095abfSIvan Voras 		if ((cg->cg_flags & CG_FLAG_HTT) != 0)
267559d95789SIvan Voras 			sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>\n");
26767b55ab05SJeff Roberson 		if ((cg->cg_flags & CG_FLAG_SMT) != 0)
267759d95789SIvan Voras 			sbuf_printf(sb, "<flag name=\"THREAD\">SMT group</flag>\n");
267807095abfSIvan Voras 	}
267907095abfSIvan Voras 	sbuf_printf(sb, "</flags>\n");
268007095abfSIvan Voras 
268107095abfSIvan Voras 	if (cg->cg_children > 0) {
268207095abfSIvan Voras 		sbuf_printf(sb, "%*s <children>\n", indent, "");
268307095abfSIvan Voras 		for (i = 0; i < cg->cg_children; i++)
268407095abfSIvan Voras 			sysctl_kern_sched_topology_spec_internal(sb,
268507095abfSIvan Voras 			    &cg->cg_child[i], indent+2);
268607095abfSIvan Voras 		sbuf_printf(sb, "%*s </children>\n", indent, "");
268707095abfSIvan Voras 	}
268807095abfSIvan Voras 	sbuf_printf(sb, "%*s</group>\n", indent, "");
268907095abfSIvan Voras 	return (0);
269007095abfSIvan Voras }
269107095abfSIvan Voras 
269207095abfSIvan Voras /*
269307095abfSIvan Voras  * Sysctl handler for retrieving topology dump. It's a wrapper for
269407095abfSIvan Voras  * the recursive sysctl_kern_smp_topology_spec_internal().
269507095abfSIvan Voras  */
269607095abfSIvan Voras static int
269707095abfSIvan Voras sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS)
269807095abfSIvan Voras {
269907095abfSIvan Voras 	struct sbuf *topo;
270007095abfSIvan Voras 	int err;
270107095abfSIvan Voras 
270207095abfSIvan Voras 	KASSERT(cpu_top != NULL, ("cpu_top isn't initialized"));
270307095abfSIvan Voras 
2704aa880b90SIvan Voras 	topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
270507095abfSIvan Voras 	if (topo == NULL)
270607095abfSIvan Voras 		return (ENOMEM);
270707095abfSIvan Voras 
270807095abfSIvan Voras 	sbuf_printf(topo, "<groups>\n");
270907095abfSIvan Voras 	err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1);
271007095abfSIvan Voras 	sbuf_printf(topo, "</groups>\n");
271107095abfSIvan Voras 
271207095abfSIvan Voras 	if (err == 0) {
271307095abfSIvan Voras 		sbuf_finish(topo);
271407095abfSIvan Voras 		err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo));
271507095abfSIvan Voras 	}
271607095abfSIvan Voras 	sbuf_delete(topo);
271707095abfSIvan Voras 	return (err);
271807095abfSIvan Voras }
271907095abfSIvan Voras #endif
272007095abfSIvan Voras 
27219727e637SJeff Roberson SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler");
2722ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0,
2723e7d50326SJeff Roberson     "Scheduler name");
2724ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
2725ae7a6b38SJeff Roberson     "Slice size for timeshare threads");
2726ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
2727ae7a6b38SJeff Roberson      "Interactivity score threshold");
2728ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh,
2729ae7a6b38SJeff Roberson      0,"Min priority for preemption, lower priorities have greater precedence");
2730c5aa6b58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost,
2731c5aa6b58SJeff Roberson      0,"Controls whether static kernel priorities are assigned to sleeping threads.");
27321690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins,
27331690c6c1SJeff Roberson      0,"Number of times idle will spin waiting for new work.");
27341690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh,
27351690c6c1SJeff Roberson      0,"Threshold before we will permit idle spinning.");
27367b8bfa0dSJeff Roberson #ifdef SMP
2737ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
2738ae7a6b38SJeff Roberson     "Number of hz ticks to keep thread affinity for");
2739ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
2740ae7a6b38SJeff Roberson     "Enables the long-term load balancer");
27417fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
27427fcf154aSJeff Roberson     &balance_interval, 0,
27437fcf154aSJeff Roberson     "Average frequency in stathz ticks to run the long-term balancer");
2744ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0,
2745ae7a6b38SJeff Roberson     "Steals work from another hyper-threaded core on idle");
2746ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
2747ae7a6b38SJeff Roberson     "Attempts to steal work from other cores before idling");
274828994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
274928994a58SJeff Roberson     "Minimum load on remote cpu before we'll steal");
275007095abfSIvan Voras 
275107095abfSIvan Voras /* Retrieve SMP topology */
275207095abfSIvan Voras SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING |
275307095abfSIvan Voras     CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A",
275407095abfSIvan Voras     "XML dump of detected CPU topology");
27557b8bfa0dSJeff Roberson #endif
2756e7d50326SJeff Roberson 
275754b0e65fSJeff Roberson /* ps compat.  All cpu percentages from ULE are weighted. */
2758a5423ea3SJeff Roberson static int ccpu = 0;
2759e7d50326SJeff Roberson SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
2760