xref: /freebsd/sys/kern/sched_ule.c (revision 8df78c41d60219744f69ac9ec4e2842407fdca40)
135e6168fSJeff Roberson /*-
2e7d50326SJeff Roberson  * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org>
335e6168fSJeff Roberson  * All rights reserved.
435e6168fSJeff Roberson  *
535e6168fSJeff Roberson  * Redistribution and use in source and binary forms, with or without
635e6168fSJeff Roberson  * modification, are permitted provided that the following conditions
735e6168fSJeff Roberson  * are met:
835e6168fSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
935e6168fSJeff Roberson  *    notice unmodified, this list of conditions, and the following
1035e6168fSJeff Roberson  *    disclaimer.
1135e6168fSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
1235e6168fSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
1335e6168fSJeff Roberson  *    documentation and/or other materials provided with the distribution.
1435e6168fSJeff Roberson  *
1535e6168fSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1635e6168fSJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1735e6168fSJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1835e6168fSJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1935e6168fSJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2035e6168fSJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2135e6168fSJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2235e6168fSJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2335e6168fSJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2435e6168fSJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2535e6168fSJeff Roberson  */
2635e6168fSJeff Roberson 
27ae7a6b38SJeff Roberson /*
28ae7a6b38SJeff Roberson  * This file implements the ULE scheduler.  ULE supports independent CPU
29ae7a6b38SJeff Roberson  * run queues and fine grain locking.  It has superior interactive
30ae7a6b38SJeff Roberson  * performance under load even on uni-processor systems.
31ae7a6b38SJeff Roberson  *
32ae7a6b38SJeff Roberson  * etymology:
33a5423ea3SJeff Roberson  *   ULE is the last three letters in schedule.  It owes its name to a
34ae7a6b38SJeff Roberson  * generic user created for a scheduling system by Paul Mikesell at
35ae7a6b38SJeff Roberson  * Isilon Systems and a general lack of creativity on the part of the author.
36ae7a6b38SJeff Roberson  */
37ae7a6b38SJeff Roberson 
38677b542eSDavid E. O'Brien #include <sys/cdefs.h>
39677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40677b542eSDavid E. O'Brien 
414da0d332SPeter Wemm #include "opt_hwpmc_hooks.h"
424da0d332SPeter Wemm #include "opt_sched.h"
439923b511SScott Long 
4435e6168fSJeff Roberson #include <sys/param.h>
4535e6168fSJeff Roberson #include <sys/systm.h>
462c3490b1SMarcel Moolenaar #include <sys/kdb.h>
4735e6168fSJeff Roberson #include <sys/kernel.h>
4835e6168fSJeff Roberson #include <sys/ktr.h>
4935e6168fSJeff Roberson #include <sys/lock.h>
5035e6168fSJeff Roberson #include <sys/mutex.h>
5135e6168fSJeff Roberson #include <sys/proc.h>
52245f3abfSJeff Roberson #include <sys/resource.h>
539bacd788SJeff Roberson #include <sys/resourcevar.h>
5435e6168fSJeff Roberson #include <sys/sched.h>
5535e6168fSJeff Roberson #include <sys/smp.h>
5635e6168fSJeff Roberson #include <sys/sx.h>
5735e6168fSJeff Roberson #include <sys/sysctl.h>
5835e6168fSJeff Roberson #include <sys/sysproto.h>
59f5c157d9SJohn Baldwin #include <sys/turnstile.h>
603db720fdSDavid Xu #include <sys/umtx.h>
6135e6168fSJeff Roberson #include <sys/vmmeter.h>
6262fa74d9SJeff Roberson #include <sys/cpuset.h>
6335e6168fSJeff Roberson #ifdef KTRACE
6435e6168fSJeff Roberson #include <sys/uio.h>
6535e6168fSJeff Roberson #include <sys/ktrace.h>
6635e6168fSJeff Roberson #endif
6735e6168fSJeff Roberson 
68ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS
69ebccf1e3SJoseph Koshy #include <sys/pmckern.h>
70ebccf1e3SJoseph Koshy #endif
71ebccf1e3SJoseph Koshy 
7235e6168fSJeff Roberson #include <machine/cpu.h>
7322bf7d9aSJeff Roberson #include <machine/smp.h>
7435e6168fSJeff Roberson 
75495168baSMarcel Moolenaar #if defined(__sparc64__) || defined(__mips__)
7602e2d6b4SJeff Roberson #error "This architecture is not currently compatible with ULE"
777a5e5e2aSJeff Roberson #endif
787a5e5e2aSJeff Roberson 
79ae7a6b38SJeff Roberson #define	KTR_ULE	0
8014618990SJeff Roberson 
816b2f763fSJeff Roberson /*
82ae7a6b38SJeff Roberson  * Thread scheduler specific section.  All fields are protected
83ae7a6b38SJeff Roberson  * by the thread lock.
84ed062c8dSJulian Elischer  */
85ad1e7d28SJulian Elischer struct td_sched {
86ae7a6b38SJeff Roberson 	struct runq	*ts_runq;	/* Run-queue we're queued on. */
87ae7a6b38SJeff Roberson 	short		ts_flags;	/* TSF_* flags. */
88ad1e7d28SJulian Elischer 	u_char		ts_cpu;		/* CPU that we have affinity for. */
8973daf66fSJeff Roberson 	int		ts_rltick;	/* Real last tick, for affinity. */
90ae7a6b38SJeff Roberson 	int		ts_slice;	/* Ticks of slice remaining. */
91ae7a6b38SJeff Roberson 	u_int		ts_slptime;	/* Number of ticks we vol. slept */
92ae7a6b38SJeff Roberson 	u_int		ts_runtime;	/* Number of ticks we were running */
93ad1e7d28SJulian Elischer 	int		ts_ltick;	/* Last tick that we were running on */
94ad1e7d28SJulian Elischer 	int		ts_ftick;	/* First tick that we were running on */
95ad1e7d28SJulian Elischer 	int		ts_ticks;	/* Tick count */
96ed062c8dSJulian Elischer };
97ad1e7d28SJulian Elischer /* flags kept in ts_flags */
987b8bfa0dSJeff Roberson #define	TSF_BOUND	0x0001		/* Thread can not migrate. */
997b8bfa0dSJeff Roberson #define	TSF_XFERABLE	0x0002		/* Thread was added as transferable. */
10035e6168fSJeff Roberson 
101ad1e7d28SJulian Elischer static struct td_sched td_sched0;
10235e6168fSJeff Roberson 
10362fa74d9SJeff Roberson #define	THREAD_CAN_MIGRATE(td)	((td)->td_pinned == 0)
10462fa74d9SJeff Roberson #define	THREAD_CAN_SCHED(td, cpu)	\
10562fa74d9SJeff Roberson     CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
10662fa74d9SJeff Roberson 
10735e6168fSJeff Roberson /*
108e7d50326SJeff Roberson  * Cpu percentage computation macros and defines.
109e1f89c22SJeff Roberson  *
110e7d50326SJeff Roberson  * SCHED_TICK_SECS:	Number of seconds to average the cpu usage across.
111e7d50326SJeff Roberson  * SCHED_TICK_TARG:	Number of hz ticks to average the cpu usage across.
1128ab80cf0SJeff Roberson  * SCHED_TICK_MAX:	Maximum number of ticks before scaling back.
113e7d50326SJeff Roberson  * SCHED_TICK_SHIFT:	Shift factor to avoid rounding away results.
114e7d50326SJeff Roberson  * SCHED_TICK_HZ:	Compute the number of hz ticks for a given ticks count.
115e7d50326SJeff Roberson  * SCHED_TICK_TOTAL:	Gives the amount of time we've been recording ticks.
11635e6168fSJeff Roberson  */
117e7d50326SJeff Roberson #define	SCHED_TICK_SECS		10
118e7d50326SJeff Roberson #define	SCHED_TICK_TARG		(hz * SCHED_TICK_SECS)
1198ab80cf0SJeff Roberson #define	SCHED_TICK_MAX		(SCHED_TICK_TARG + hz)
120e7d50326SJeff Roberson #define	SCHED_TICK_SHIFT	10
121e7d50326SJeff Roberson #define	SCHED_TICK_HZ(ts)	((ts)->ts_ticks >> SCHED_TICK_SHIFT)
122eddb4efaSJeff Roberson #define	SCHED_TICK_TOTAL(ts)	(max((ts)->ts_ltick - (ts)->ts_ftick, hz))
12335e6168fSJeff Roberson 
12435e6168fSJeff Roberson /*
125e7d50326SJeff Roberson  * These macros determine priorities for non-interactive threads.  They are
126e7d50326SJeff Roberson  * assigned a priority based on their recent cpu utilization as expressed
127e7d50326SJeff Roberson  * by the ratio of ticks to the tick total.  NHALF priorities at the start
128e7d50326SJeff Roberson  * and end of the MIN to MAX timeshare range are only reachable with negative
129e7d50326SJeff Roberson  * or positive nice respectively.
130e7d50326SJeff Roberson  *
131e7d50326SJeff Roberson  * PRI_RANGE:	Priority range for utilization dependent priorities.
132e7d50326SJeff Roberson  * PRI_NRESV:	Number of nice values.
133e7d50326SJeff Roberson  * PRI_TICKS:	Compute a priority in PRI_RANGE from the ticks count and total.
134e7d50326SJeff Roberson  * PRI_NICE:	Determines the part of the priority inherited from nice.
135e7d50326SJeff Roberson  */
136e7d50326SJeff Roberson #define	SCHED_PRI_NRESV		(PRIO_MAX - PRIO_MIN)
137e7d50326SJeff Roberson #define	SCHED_PRI_NHALF		(SCHED_PRI_NRESV / 2)
138e7d50326SJeff Roberson #define	SCHED_PRI_MIN		(PRI_MIN_TIMESHARE + SCHED_PRI_NHALF)
139e7d50326SJeff Roberson #define	SCHED_PRI_MAX		(PRI_MAX_TIMESHARE - SCHED_PRI_NHALF)
140dda713dfSJeff Roberson #define	SCHED_PRI_RANGE		(SCHED_PRI_MAX - SCHED_PRI_MIN)
141e7d50326SJeff Roberson #define	SCHED_PRI_TICKS(ts)						\
142e7d50326SJeff Roberson     (SCHED_TICK_HZ((ts)) /						\
1431e516cf5SJeff Roberson     (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
144e7d50326SJeff Roberson #define	SCHED_PRI_NICE(nice)	(nice)
145e7d50326SJeff Roberson 
146e7d50326SJeff Roberson /*
147e7d50326SJeff Roberson  * These determine the interactivity of a process.  Interactivity differs from
148e7d50326SJeff Roberson  * cpu utilization in that it expresses the voluntary time slept vs time ran
149e7d50326SJeff Roberson  * while cpu utilization includes all time not running.  This more accurately
150e7d50326SJeff Roberson  * models the intent of the thread.
15135e6168fSJeff Roberson  *
152407b0157SJeff Roberson  * SLP_RUN_MAX:	Maximum amount of sleep time + run time we'll accumulate
153407b0157SJeff Roberson  *		before throttling back.
154d322132cSJeff Roberson  * SLP_RUN_FORK:	Maximum slp+run time to inherit at fork time.
155210491d3SJeff Roberson  * INTERACT_MAX:	Maximum interactivity value.  Smaller is better.
156e1f89c22SJeff Roberson  * INTERACT_THRESH:	Threshhold for placement on the current runq.
15735e6168fSJeff Roberson  */
158e7d50326SJeff Roberson #define	SCHED_SLP_RUN_MAX	((hz * 5) << SCHED_TICK_SHIFT)
159e7d50326SJeff Roberson #define	SCHED_SLP_RUN_FORK	((hz / 2) << SCHED_TICK_SHIFT)
160210491d3SJeff Roberson #define	SCHED_INTERACT_MAX	(100)
161210491d3SJeff Roberson #define	SCHED_INTERACT_HALF	(SCHED_INTERACT_MAX / 2)
1624c9612c6SJeff Roberson #define	SCHED_INTERACT_THRESH	(30)
163e1f89c22SJeff Roberson 
16435e6168fSJeff Roberson /*
165e7d50326SJeff Roberson  * tickincr:		Converts a stathz tick into a hz domain scaled by
166e7d50326SJeff Roberson  *			the shift factor.  Without the shift the error rate
167e7d50326SJeff Roberson  *			due to rounding would be unacceptably high.
168e7d50326SJeff Roberson  * realstathz:		stathz is sometimes 0 and run off of hz.
169e7d50326SJeff Roberson  * sched_slice:		Runtime of each thread before rescheduling.
170ae7a6b38SJeff Roberson  * preempt_thresh:	Priority threshold for preemption and remote IPIs.
17135e6168fSJeff Roberson  */
172e7d50326SJeff Roberson static int sched_interact = SCHED_INTERACT_THRESH;
173e7d50326SJeff Roberson static int realstathz;
174e7d50326SJeff Roberson static int tickincr;
17573daf66fSJeff Roberson static int sched_slice = 1;
17602e2d6b4SJeff Roberson #ifdef PREEMPTION
17702e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION
17802e2d6b4SJeff Roberson static int preempt_thresh = PRI_MAX_IDLE;
17902e2d6b4SJeff Roberson #else
180ae7a6b38SJeff Roberson static int preempt_thresh = PRI_MIN_KERN;
18102e2d6b4SJeff Roberson #endif
18202e2d6b4SJeff Roberson #else
18302e2d6b4SJeff Roberson static int preempt_thresh = 0;
18402e2d6b4SJeff Roberson #endif
1850502fe2eSJeff Roberson static int static_boost = PRI_MIN_TIMESHARE;
186ae7a6b38SJeff Roberson 
18735e6168fSJeff Roberson /*
188ae7a6b38SJeff Roberson  * tdq - per processor runqs and statistics.  All fields are protected by the
189ae7a6b38SJeff Roberson  * tdq_lock.  The load and lowpri may be accessed without to avoid excess
190ae7a6b38SJeff Roberson  * locking in sched_pickcpu();
19135e6168fSJeff Roberson  */
192ad1e7d28SJulian Elischer struct tdq {
19373daf66fSJeff Roberson 	/* Ordered to improve efficiency of cpu_search() and switch(). */
19462fa74d9SJeff Roberson 	struct mtx	tdq_lock;		/* run queue lock. */
19573daf66fSJeff Roberson 	struct cpu_group *tdq_cg;		/* Pointer to cpu topology. */
19673daf66fSJeff Roberson 	int		tdq_load;		/* Aggregate load. */
19773daf66fSJeff Roberson 	int		tdq_sysload;		/* For loadavg, !ITHD load. */
19873daf66fSJeff Roberson 	int		tdq_transferable;	/* Transferable thread count. */
19973daf66fSJeff Roberson 	u_char		tdq_lowpri;		/* Lowest priority thread. */
20073daf66fSJeff Roberson 	u_char		tdq_ipipending;		/* IPI pending. */
20173daf66fSJeff Roberson 	u_char		tdq_idx;		/* Current insert index. */
20273daf66fSJeff Roberson 	u_char		tdq_ridx;		/* Current removal index. */
203e7d50326SJeff Roberson 	struct runq	tdq_realtime;		/* real-time run queue. */
204ae7a6b38SJeff Roberson 	struct runq	tdq_timeshare;		/* timeshare run queue. */
205ae7a6b38SJeff Roberson 	struct runq	tdq_idle;		/* Queue of IDLE threads. */
20662fa74d9SJeff Roberson 	char		tdq_name[sizeof("sched lock") + 6];
207ae7a6b38SJeff Roberson } __aligned(64);
20835e6168fSJeff Roberson 
2097b8bfa0dSJeff Roberson 
21080f86c9fSJeff Roberson #ifdef SMP
21162fa74d9SJeff Roberson struct cpu_group *cpu_top;
2127b8bfa0dSJeff Roberson 
21362fa74d9SJeff Roberson #define	SCHED_AFFINITY_DEFAULT	(max(1, hz / 1000))
21462fa74d9SJeff Roberson #define	SCHED_AFFINITY(ts, t)	((ts)->ts_rltick > ticks - ((t) * affinity))
2157b8bfa0dSJeff Roberson 
2167b8bfa0dSJeff Roberson /*
2177b8bfa0dSJeff Roberson  * Run-time tunables.
2187b8bfa0dSJeff Roberson  */
21928994a58SJeff Roberson static int rebalance = 1;
2207fcf154aSJeff Roberson static int balance_interval = 128;	/* Default set in sched_initticks(). */
2217b8bfa0dSJeff Roberson static int affinity;
2227fcf154aSJeff Roberson static int steal_htt = 1;
22328994a58SJeff Roberson static int steal_idle = 1;
22428994a58SJeff Roberson static int steal_thresh = 2;
22580f86c9fSJeff Roberson 
22635e6168fSJeff Roberson /*
227d2ad694cSJeff Roberson  * One thread queue per processor.
22835e6168fSJeff Roberson  */
229ad1e7d28SJulian Elischer static struct tdq	tdq_cpu[MAXCPU];
2307fcf154aSJeff Roberson static struct tdq	*balance_tdq;
2317fcf154aSJeff Roberson static int balance_ticks;
232dc03363dSJeff Roberson 
233ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu[PCPU_GET(cpuid)])
234ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu[(x)])
235c47f202bSJeff Roberson #define	TDQ_ID(x)	((int)((x) - tdq_cpu))
23680f86c9fSJeff Roberson #else	/* !SMP */
237ad1e7d28SJulian Elischer static struct tdq	tdq_cpu;
238dc03363dSJeff Roberson 
23936b36916SJeff Roberson #define	TDQ_ID(x)	(0)
240ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu)
241ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu)
2420a016a05SJeff Roberson #endif
24335e6168fSJeff Roberson 
244ae7a6b38SJeff Roberson #define	TDQ_LOCK_ASSERT(t, type)	mtx_assert(TDQ_LOCKPTR((t)), (type))
245ae7a6b38SJeff Roberson #define	TDQ_LOCK(t)		mtx_lock_spin(TDQ_LOCKPTR((t)))
246ae7a6b38SJeff Roberson #define	TDQ_LOCK_FLAGS(t, f)	mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
247ae7a6b38SJeff Roberson #define	TDQ_UNLOCK(t)		mtx_unlock_spin(TDQ_LOCKPTR((t)))
24862fa74d9SJeff Roberson #define	TDQ_LOCKPTR(t)		(&(t)->tdq_lock)
249ae7a6b38SJeff Roberson 
2508460a577SJohn Birrell static void sched_priority(struct thread *);
25121381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char);
2528460a577SJohn Birrell static int sched_interact_score(struct thread *);
2538460a577SJohn Birrell static void sched_interact_update(struct thread *);
2548460a577SJohn Birrell static void sched_interact_fork(struct thread *);
255ad1e7d28SJulian Elischer static void sched_pctcpu_update(struct td_sched *);
25635e6168fSJeff Roberson 
2575d7ef00cSJeff Roberson /* Operations on per processor queues */
2589727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *);
259ad1e7d28SJulian Elischer static void tdq_setup(struct tdq *);
2609727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *);
2619727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *);
2629727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int);
2639727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *);
264ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int);
265ad1e7d28SJulian Elischer void tdq_print(int cpu);
266e7d50326SJeff Roberson static void runq_print(struct runq *rq);
267ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int);
2685d7ef00cSJeff Roberson #ifdef SMP
26962fa74d9SJeff Roberson static int tdq_move(struct tdq *, struct tdq *);
270ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *);
2719727e637SJeff Roberson static void tdq_notify(struct tdq *, struct thread *);
2729727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int);
2739727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int);
2749727e637SJeff Roberson static int sched_pickcpu(struct thread *, int);
2757fcf154aSJeff Roberson static void sched_balance(void);
27662fa74d9SJeff Roberson static int sched_balance_pair(struct tdq *, struct tdq *);
2779727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int);
278ae7a6b38SJeff Roberson static inline struct mtx *thread_block_switch(struct thread *);
279ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *);
280c47f202bSJeff Roberson static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int);
2815d7ef00cSJeff Roberson #endif
2825d7ef00cSJeff Roberson 
283e7d50326SJeff Roberson static void sched_setup(void *dummy);
284237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
285e7d50326SJeff Roberson 
286e7d50326SJeff Roberson static void sched_initticks(void *dummy);
287237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks,
288237fdd78SRobert Watson     NULL);
289e7d50326SJeff Roberson 
290ae7a6b38SJeff Roberson /*
291ae7a6b38SJeff Roberson  * Print the threads waiting on a run-queue.
292ae7a6b38SJeff Roberson  */
293e7d50326SJeff Roberson static void
294e7d50326SJeff Roberson runq_print(struct runq *rq)
295e7d50326SJeff Roberson {
296e7d50326SJeff Roberson 	struct rqhead *rqh;
2979727e637SJeff Roberson 	struct thread *td;
298e7d50326SJeff Roberson 	int pri;
299e7d50326SJeff Roberson 	int j;
300e7d50326SJeff Roberson 	int i;
301e7d50326SJeff Roberson 
302e7d50326SJeff Roberson 	for (i = 0; i < RQB_LEN; i++) {
303e7d50326SJeff Roberson 		printf("\t\trunq bits %d 0x%zx\n",
304e7d50326SJeff Roberson 		    i, rq->rq_status.rqb_bits[i]);
305e7d50326SJeff Roberson 		for (j = 0; j < RQB_BPW; j++)
306e7d50326SJeff Roberson 			if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
307e7d50326SJeff Roberson 				pri = j + (i << RQB_L2BPW);
308e7d50326SJeff Roberson 				rqh = &rq->rq_queues[pri];
3099727e637SJeff Roberson 				TAILQ_FOREACH(td, rqh, td_runq) {
310e7d50326SJeff Roberson 					printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
3119727e637SJeff Roberson 					    td, td->td_name, td->td_priority,
3129727e637SJeff Roberson 					    td->td_rqindex, pri);
313e7d50326SJeff Roberson 				}
314e7d50326SJeff Roberson 			}
315e7d50326SJeff Roberson 	}
316e7d50326SJeff Roberson }
317e7d50326SJeff Roberson 
318ae7a6b38SJeff Roberson /*
319ae7a6b38SJeff Roberson  * Print the status of a per-cpu thread queue.  Should be a ddb show cmd.
320ae7a6b38SJeff Roberson  */
32115dc847eSJeff Roberson void
322ad1e7d28SJulian Elischer tdq_print(int cpu)
32315dc847eSJeff Roberson {
324ad1e7d28SJulian Elischer 	struct tdq *tdq;
32515dc847eSJeff Roberson 
326ad1e7d28SJulian Elischer 	tdq = TDQ_CPU(cpu);
32715dc847eSJeff Roberson 
328c47f202bSJeff Roberson 	printf("tdq %d:\n", TDQ_ID(tdq));
32962fa74d9SJeff Roberson 	printf("\tlock            %p\n", TDQ_LOCKPTR(tdq));
33062fa74d9SJeff Roberson 	printf("\tLock name:      %s\n", tdq->tdq_name);
331d2ad694cSJeff Roberson 	printf("\tload:           %d\n", tdq->tdq_load);
332e7d50326SJeff Roberson 	printf("\ttimeshare idx:  %d\n", tdq->tdq_idx);
3333f872f85SJeff Roberson 	printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
334e7d50326SJeff Roberson 	printf("\trealtime runq:\n");
335e7d50326SJeff Roberson 	runq_print(&tdq->tdq_realtime);
336e7d50326SJeff Roberson 	printf("\ttimeshare runq:\n");
337e7d50326SJeff Roberson 	runq_print(&tdq->tdq_timeshare);
338e7d50326SJeff Roberson 	printf("\tidle runq:\n");
339e7d50326SJeff Roberson 	runq_print(&tdq->tdq_idle);
340d2ad694cSJeff Roberson 	printf("\tload transferable: %d\n", tdq->tdq_transferable);
341ae7a6b38SJeff Roberson 	printf("\tlowest priority:   %d\n", tdq->tdq_lowpri);
34215dc847eSJeff Roberson }
34315dc847eSJeff Roberson 
344ff256d9cSJeff Roberson static inline int
345ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote)
346ff256d9cSJeff Roberson {
347ff256d9cSJeff Roberson 	/*
348ff256d9cSJeff Roberson 	 * If the new priority is not better than the current priority there is
349ff256d9cSJeff Roberson 	 * nothing to do.
350ff256d9cSJeff Roberson 	 */
351ff256d9cSJeff Roberson 	if (pri >= cpri)
352ff256d9cSJeff Roberson 		return (0);
353ff256d9cSJeff Roberson 	/*
354ff256d9cSJeff Roberson 	 * Always preempt idle.
355ff256d9cSJeff Roberson 	 */
356ff256d9cSJeff Roberson 	if (cpri >= PRI_MIN_IDLE)
357ff256d9cSJeff Roberson 		return (1);
358ff256d9cSJeff Roberson 	/*
359ff256d9cSJeff Roberson 	 * If preemption is disabled don't preempt others.
360ff256d9cSJeff Roberson 	 */
361ff256d9cSJeff Roberson 	if (preempt_thresh == 0)
362ff256d9cSJeff Roberson 		return (0);
363ff256d9cSJeff Roberson 	/*
364ff256d9cSJeff Roberson 	 * Preempt if we exceed the threshold.
365ff256d9cSJeff Roberson 	 */
366ff256d9cSJeff Roberson 	if (pri <= preempt_thresh)
367ff256d9cSJeff Roberson 		return (1);
368ff256d9cSJeff Roberson 	/*
369ff256d9cSJeff Roberson 	 * If we're realtime or better and there is timeshare or worse running
370ff256d9cSJeff Roberson 	 * preempt only remote processors.
371ff256d9cSJeff Roberson 	 */
372ff256d9cSJeff Roberson 	if (remote && pri <= PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
373ff256d9cSJeff Roberson 		return (1);
374ff256d9cSJeff Roberson 	return (0);
375ff256d9cSJeff Roberson }
376ff256d9cSJeff Roberson 
377ae7a6b38SJeff Roberson #define	TS_RQ_PPQ	(((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS)
378ae7a6b38SJeff Roberson /*
379ae7a6b38SJeff Roberson  * Add a thread to the actual run-queue.  Keeps transferable counts up to
380ae7a6b38SJeff Roberson  * date with what is actually on the run-queue.  Selects the correct
381ae7a6b38SJeff Roberson  * queue position for timeshare threads.
382ae7a6b38SJeff Roberson  */
383155b9987SJeff Roberson static __inline void
3849727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags)
385155b9987SJeff Roberson {
3869727e637SJeff Roberson 	struct td_sched *ts;
387c143ac21SJeff Roberson 	u_char pri;
388c143ac21SJeff Roberson 
389ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
3909727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
39173daf66fSJeff Roberson 
3929727e637SJeff Roberson 	pri = td->td_priority;
3939727e637SJeff Roberson 	ts = td->td_sched;
3949727e637SJeff Roberson 	TD_SET_RUNQ(td);
3959727e637SJeff Roberson 	if (THREAD_CAN_MIGRATE(td)) {
396d2ad694cSJeff Roberson 		tdq->tdq_transferable++;
397ad1e7d28SJulian Elischer 		ts->ts_flags |= TSF_XFERABLE;
39880f86c9fSJeff Roberson 	}
399c143ac21SJeff Roberson 	if (pri <= PRI_MAX_REALTIME) {
400c143ac21SJeff Roberson 		ts->ts_runq = &tdq->tdq_realtime;
401c143ac21SJeff Roberson 	} else if (pri <= PRI_MAX_TIMESHARE) {
402c143ac21SJeff Roberson 		ts->ts_runq = &tdq->tdq_timeshare;
403e7d50326SJeff Roberson 		KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
404e7d50326SJeff Roberson 			("Invalid priority %d on timeshare runq", pri));
405e7d50326SJeff Roberson 		/*
406e7d50326SJeff Roberson 		 * This queue contains only priorities between MIN and MAX
407e7d50326SJeff Roberson 		 * realtime.  Use the whole queue to represent these values.
408e7d50326SJeff Roberson 		 */
409c47f202bSJeff Roberson 		if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
410e7d50326SJeff Roberson 			pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
411e7d50326SJeff Roberson 			pri = (pri + tdq->tdq_idx) % RQ_NQS;
4123f872f85SJeff Roberson 			/*
4133f872f85SJeff Roberson 			 * This effectively shortens the queue by one so we
4143f872f85SJeff Roberson 			 * can have a one slot difference between idx and
4153f872f85SJeff Roberson 			 * ridx while we wait for threads to drain.
4163f872f85SJeff Roberson 			 */
4173f872f85SJeff Roberson 			if (tdq->tdq_ridx != tdq->tdq_idx &&
4183f872f85SJeff Roberson 			    pri == tdq->tdq_ridx)
4194499aff6SJeff Roberson 				pri = (unsigned char)(pri - 1) % RQ_NQS;
420e7d50326SJeff Roberson 		} else
4213f872f85SJeff Roberson 			pri = tdq->tdq_ridx;
4229727e637SJeff Roberson 		runq_add_pri(ts->ts_runq, td, pri, flags);
423c143ac21SJeff Roberson 		return;
424e7d50326SJeff Roberson 	} else
42573daf66fSJeff Roberson 		ts->ts_runq = &tdq->tdq_idle;
4269727e637SJeff Roberson 	runq_add(ts->ts_runq, td, flags);
42773daf66fSJeff Roberson }
42873daf66fSJeff Roberson 
42973daf66fSJeff Roberson /*
430ae7a6b38SJeff Roberson  * Remove a thread from a run-queue.  This typically happens when a thread
431ae7a6b38SJeff Roberson  * is selected to run.  Running threads are not on the queue and the
432ae7a6b38SJeff Roberson  * transferable count does not reflect them.
433ae7a6b38SJeff Roberson  */
434155b9987SJeff Roberson static __inline void
4359727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td)
436155b9987SJeff Roberson {
4379727e637SJeff Roberson 	struct td_sched *ts;
4389727e637SJeff Roberson 
4399727e637SJeff Roberson 	ts = td->td_sched;
440ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
441ae7a6b38SJeff Roberson 	KASSERT(ts->ts_runq != NULL,
4429727e637SJeff Roberson 	    ("tdq_runq_remove: thread %p null ts_runq", td));
443ad1e7d28SJulian Elischer 	if (ts->ts_flags & TSF_XFERABLE) {
444d2ad694cSJeff Roberson 		tdq->tdq_transferable--;
445ad1e7d28SJulian Elischer 		ts->ts_flags &= ~TSF_XFERABLE;
44680f86c9fSJeff Roberson 	}
4473f872f85SJeff Roberson 	if (ts->ts_runq == &tdq->tdq_timeshare) {
4483f872f85SJeff Roberson 		if (tdq->tdq_idx != tdq->tdq_ridx)
4499727e637SJeff Roberson 			runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx);
450e7d50326SJeff Roberson 		else
4519727e637SJeff Roberson 			runq_remove_idx(ts->ts_runq, td, NULL);
4523f872f85SJeff Roberson 	} else
4539727e637SJeff Roberson 		runq_remove(ts->ts_runq, td);
454155b9987SJeff Roberson }
455155b9987SJeff Roberson 
456ae7a6b38SJeff Roberson /*
457ae7a6b38SJeff Roberson  * Load is maintained for all threads RUNNING and ON_RUNQ.  Add the load
458ae7a6b38SJeff Roberson  * for this thread to the referenced thread queue.
459ae7a6b38SJeff Roberson  */
460a8949de2SJeff Roberson static void
4619727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td)
4625d7ef00cSJeff Roberson {
463ae7a6b38SJeff Roberson 
464ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
4659727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
46603d17db7SJeff Roberson 
467d2ad694cSJeff Roberson 	tdq->tdq_load++;
46803d17db7SJeff Roberson 	if ((td->td_proc->p_flag & P_NOLOAD) == 0)
469d2ad694cSJeff Roberson 		tdq->tdq_sysload++;
47003d17db7SJeff Roberson 	CTR2(KTR_SCHED, "cpu %d load: %d", TDQ_ID(tdq), tdq->tdq_load);
4715d7ef00cSJeff Roberson }
47215dc847eSJeff Roberson 
473ae7a6b38SJeff Roberson /*
474ae7a6b38SJeff Roberson  * Remove the load from a thread that is transitioning to a sleep state or
475ae7a6b38SJeff Roberson  * exiting.
476ae7a6b38SJeff Roberson  */
477a8949de2SJeff Roberson static void
4789727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td)
4795d7ef00cSJeff Roberson {
480ae7a6b38SJeff Roberson 
4819727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
482ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
483ae7a6b38SJeff Roberson 	KASSERT(tdq->tdq_load != 0,
484c47f202bSJeff Roberson 	    ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq)));
48503d17db7SJeff Roberson 
486d2ad694cSJeff Roberson 	tdq->tdq_load--;
48703d17db7SJeff Roberson 	if ((td->td_proc->p_flag & P_NOLOAD) == 0)
48803d17db7SJeff Roberson 		tdq->tdq_sysload--;
489d2ad694cSJeff Roberson 	CTR1(KTR_SCHED, "load: %d", tdq->tdq_load);
49015dc847eSJeff Roberson }
49115dc847eSJeff Roberson 
492356500a3SJeff Roberson /*
49362fa74d9SJeff Roberson  * Set lowpri to its exact value by searching the run-queue and
49462fa74d9SJeff Roberson  * evaluating curthread.  curthread may be passed as an optimization.
495356500a3SJeff Roberson  */
49622bf7d9aSJeff Roberson static void
49762fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd)
49862fa74d9SJeff Roberson {
49962fa74d9SJeff Roberson 	struct thread *td;
50062fa74d9SJeff Roberson 
50162fa74d9SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
50262fa74d9SJeff Roberson 	if (ctd == NULL)
50362fa74d9SJeff Roberson 		ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread;
5049727e637SJeff Roberson 	td = tdq_choose(tdq);
5059727e637SJeff Roberson 	if (td == NULL || td->td_priority > ctd->td_priority)
50662fa74d9SJeff Roberson 		tdq->tdq_lowpri = ctd->td_priority;
50762fa74d9SJeff Roberson 	else
50862fa74d9SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
50962fa74d9SJeff Roberson }
51062fa74d9SJeff Roberson 
51162fa74d9SJeff Roberson #ifdef SMP
51262fa74d9SJeff Roberson struct cpu_search {
51362fa74d9SJeff Roberson 	cpumask_t cs_mask;	/* Mask of valid cpus. */
51462fa74d9SJeff Roberson 	u_int	cs_load;
51562fa74d9SJeff Roberson 	u_int	cs_cpu;
51662fa74d9SJeff Roberson 	int	cs_limit;	/* Min priority for low min load for high. */
51762fa74d9SJeff Roberson };
51862fa74d9SJeff Roberson 
51962fa74d9SJeff Roberson #define	CPU_SEARCH_LOWEST	0x1
52062fa74d9SJeff Roberson #define	CPU_SEARCH_HIGHEST	0x2
52162fa74d9SJeff Roberson #define	CPU_SEARCH_BOTH		(CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST)
52262fa74d9SJeff Roberson 
52362fa74d9SJeff Roberson #define	CPUMASK_FOREACH(cpu, mask)				\
52462fa74d9SJeff Roberson 	for ((cpu) = 0; (cpu) < sizeof((mask)) * 8; (cpu)++)	\
52562fa74d9SJeff Roberson 		if ((mask) & 1 << (cpu))
52662fa74d9SJeff Roberson 
527d628fbfaSJohn Baldwin static __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low,
52862fa74d9SJeff Roberson     struct cpu_search *high, const int match);
52962fa74d9SJeff Roberson int cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low);
53062fa74d9SJeff Roberson int cpu_search_highest(struct cpu_group *cg, struct cpu_search *high);
53162fa74d9SJeff Roberson int cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
53262fa74d9SJeff Roberson     struct cpu_search *high);
53362fa74d9SJeff Roberson 
53462fa74d9SJeff Roberson /*
53562fa74d9SJeff Roberson  * This routine compares according to the match argument and should be
53662fa74d9SJeff Roberson  * reduced in actual instantiations via constant propagation and dead code
53762fa74d9SJeff Roberson  * elimination.
53862fa74d9SJeff Roberson  */
53962fa74d9SJeff Roberson static __inline int
54062fa74d9SJeff Roberson cpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high,
54162fa74d9SJeff Roberson     const int match)
54262fa74d9SJeff Roberson {
54362fa74d9SJeff Roberson 	struct tdq *tdq;
54462fa74d9SJeff Roberson 
54562fa74d9SJeff Roberson 	tdq = TDQ_CPU(cpu);
54662fa74d9SJeff Roberson 	if (match & CPU_SEARCH_LOWEST)
54762fa74d9SJeff Roberson 		if (low->cs_mask & (1 << cpu) &&
54862fa74d9SJeff Roberson 		    tdq->tdq_load < low->cs_load &&
54962fa74d9SJeff Roberson 		    tdq->tdq_lowpri > low->cs_limit) {
55062fa74d9SJeff Roberson 			low->cs_cpu = cpu;
55162fa74d9SJeff Roberson 			low->cs_load = tdq->tdq_load;
55262fa74d9SJeff Roberson 		}
55362fa74d9SJeff Roberson 	if (match & CPU_SEARCH_HIGHEST)
55462fa74d9SJeff Roberson 		if (high->cs_mask & (1 << cpu) &&
55562fa74d9SJeff Roberson 		    tdq->tdq_load >= high->cs_limit &&
55662fa74d9SJeff Roberson 		    tdq->tdq_load > high->cs_load &&
55762fa74d9SJeff Roberson 		    tdq->tdq_transferable) {
55862fa74d9SJeff Roberson 			high->cs_cpu = cpu;
55962fa74d9SJeff Roberson 			high->cs_load = tdq->tdq_load;
56062fa74d9SJeff Roberson 		}
56162fa74d9SJeff Roberson 	return (tdq->tdq_load);
56262fa74d9SJeff Roberson }
56362fa74d9SJeff Roberson 
56462fa74d9SJeff Roberson /*
56562fa74d9SJeff Roberson  * Search the tree of cpu_groups for the lowest or highest loaded cpu
56662fa74d9SJeff Roberson  * according to the match argument.  This routine actually compares the
56762fa74d9SJeff Roberson  * load on all paths through the tree and finds the least loaded cpu on
56862fa74d9SJeff Roberson  * the least loaded path, which may differ from the least loaded cpu in
56962fa74d9SJeff Roberson  * the system.  This balances work among caches and busses.
57062fa74d9SJeff Roberson  *
57162fa74d9SJeff Roberson  * This inline is instantiated in three forms below using constants for the
57262fa74d9SJeff Roberson  * match argument.  It is reduced to the minimum set for each case.  It is
57362fa74d9SJeff Roberson  * also recursive to the depth of the tree.
57462fa74d9SJeff Roberson  */
575d628fbfaSJohn Baldwin static __inline int
57662fa74d9SJeff Roberson cpu_search(struct cpu_group *cg, struct cpu_search *low,
57762fa74d9SJeff Roberson     struct cpu_search *high, const int match)
57862fa74d9SJeff Roberson {
57962fa74d9SJeff Roberson 	int total;
58062fa74d9SJeff Roberson 
58162fa74d9SJeff Roberson 	total = 0;
58262fa74d9SJeff Roberson 	if (cg->cg_children) {
58362fa74d9SJeff Roberson 		struct cpu_search lgroup;
58462fa74d9SJeff Roberson 		struct cpu_search hgroup;
58562fa74d9SJeff Roberson 		struct cpu_group *child;
58662fa74d9SJeff Roberson 		u_int lload;
58762fa74d9SJeff Roberson 		int hload;
58862fa74d9SJeff Roberson 		int load;
58962fa74d9SJeff Roberson 		int i;
59062fa74d9SJeff Roberson 
59162fa74d9SJeff Roberson 		lload = -1;
59262fa74d9SJeff Roberson 		hload = -1;
59362fa74d9SJeff Roberson 		for (i = 0; i < cg->cg_children; i++) {
59462fa74d9SJeff Roberson 			child = &cg->cg_child[i];
59562fa74d9SJeff Roberson 			if (match & CPU_SEARCH_LOWEST) {
59662fa74d9SJeff Roberson 				lgroup = *low;
59762fa74d9SJeff Roberson 				lgroup.cs_load = -1;
59862fa74d9SJeff Roberson 			}
59962fa74d9SJeff Roberson 			if (match & CPU_SEARCH_HIGHEST) {
60062fa74d9SJeff Roberson 				hgroup = *high;
60162fa74d9SJeff Roberson 				lgroup.cs_load = 0;
60262fa74d9SJeff Roberson 			}
60362fa74d9SJeff Roberson 			switch (match) {
60462fa74d9SJeff Roberson 			case CPU_SEARCH_LOWEST:
60562fa74d9SJeff Roberson 				load = cpu_search_lowest(child, &lgroup);
60662fa74d9SJeff Roberson 				break;
60762fa74d9SJeff Roberson 			case CPU_SEARCH_HIGHEST:
60862fa74d9SJeff Roberson 				load = cpu_search_highest(child, &hgroup);
60962fa74d9SJeff Roberson 				break;
61062fa74d9SJeff Roberson 			case CPU_SEARCH_BOTH:
61162fa74d9SJeff Roberson 				load = cpu_search_both(child, &lgroup, &hgroup);
61262fa74d9SJeff Roberson 				break;
61362fa74d9SJeff Roberson 			}
61462fa74d9SJeff Roberson 			total += load;
61562fa74d9SJeff Roberson 			if (match & CPU_SEARCH_LOWEST)
61662fa74d9SJeff Roberson 				if (load < lload || low->cs_cpu == -1) {
61762fa74d9SJeff Roberson 					*low = lgroup;
61862fa74d9SJeff Roberson 					lload = load;
61962fa74d9SJeff Roberson 				}
62062fa74d9SJeff Roberson 			if (match & CPU_SEARCH_HIGHEST)
62162fa74d9SJeff Roberson 				if (load > hload || high->cs_cpu == -1) {
62262fa74d9SJeff Roberson 					hload = load;
62362fa74d9SJeff Roberson 					*high = hgroup;
62462fa74d9SJeff Roberson 				}
62562fa74d9SJeff Roberson 		}
62662fa74d9SJeff Roberson 	} else {
62762fa74d9SJeff Roberson 		int cpu;
62862fa74d9SJeff Roberson 
62962fa74d9SJeff Roberson 		CPUMASK_FOREACH(cpu, cg->cg_mask)
63062fa74d9SJeff Roberson 			total += cpu_compare(cpu, low, high, match);
63162fa74d9SJeff Roberson 	}
63262fa74d9SJeff Roberson 	return (total);
63362fa74d9SJeff Roberson }
63462fa74d9SJeff Roberson 
63562fa74d9SJeff Roberson /*
63662fa74d9SJeff Roberson  * cpu_search instantiations must pass constants to maintain the inline
63762fa74d9SJeff Roberson  * optimization.
63862fa74d9SJeff Roberson  */
63962fa74d9SJeff Roberson int
64062fa74d9SJeff Roberson cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low)
64162fa74d9SJeff Roberson {
64262fa74d9SJeff Roberson 	return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST);
64362fa74d9SJeff Roberson }
64462fa74d9SJeff Roberson 
64562fa74d9SJeff Roberson int
64662fa74d9SJeff Roberson cpu_search_highest(struct cpu_group *cg, struct cpu_search *high)
64762fa74d9SJeff Roberson {
64862fa74d9SJeff Roberson 	return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST);
64962fa74d9SJeff Roberson }
65062fa74d9SJeff Roberson 
65162fa74d9SJeff Roberson int
65262fa74d9SJeff Roberson cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
65362fa74d9SJeff Roberson     struct cpu_search *high)
65462fa74d9SJeff Roberson {
65562fa74d9SJeff Roberson 	return cpu_search(cg, low, high, CPU_SEARCH_BOTH);
65662fa74d9SJeff Roberson }
65762fa74d9SJeff Roberson 
65862fa74d9SJeff Roberson /*
65962fa74d9SJeff Roberson  * Find the cpu with the least load via the least loaded path that has a
66062fa74d9SJeff Roberson  * lowpri greater than pri  pri.  A pri of -1 indicates any priority is
66162fa74d9SJeff Roberson  * acceptable.
66262fa74d9SJeff Roberson  */
66362fa74d9SJeff Roberson static inline int
66462fa74d9SJeff Roberson sched_lowest(struct cpu_group *cg, cpumask_t mask, int pri)
66562fa74d9SJeff Roberson {
66662fa74d9SJeff Roberson 	struct cpu_search low;
66762fa74d9SJeff Roberson 
66862fa74d9SJeff Roberson 	low.cs_cpu = -1;
66962fa74d9SJeff Roberson 	low.cs_load = -1;
67062fa74d9SJeff Roberson 	low.cs_mask = mask;
67162fa74d9SJeff Roberson 	low.cs_limit = pri;
67262fa74d9SJeff Roberson 	cpu_search_lowest(cg, &low);
67362fa74d9SJeff Roberson 	return low.cs_cpu;
67462fa74d9SJeff Roberson }
67562fa74d9SJeff Roberson 
67662fa74d9SJeff Roberson /*
67762fa74d9SJeff Roberson  * Find the cpu with the highest load via the highest loaded path.
67862fa74d9SJeff Roberson  */
67962fa74d9SJeff Roberson static inline int
68062fa74d9SJeff Roberson sched_highest(struct cpu_group *cg, cpumask_t mask, int minload)
68162fa74d9SJeff Roberson {
68262fa74d9SJeff Roberson 	struct cpu_search high;
68362fa74d9SJeff Roberson 
68462fa74d9SJeff Roberson 	high.cs_cpu = -1;
68562fa74d9SJeff Roberson 	high.cs_load = 0;
68662fa74d9SJeff Roberson 	high.cs_mask = mask;
68762fa74d9SJeff Roberson 	high.cs_limit = minload;
68862fa74d9SJeff Roberson 	cpu_search_highest(cg, &high);
68962fa74d9SJeff Roberson 	return high.cs_cpu;
69062fa74d9SJeff Roberson }
69162fa74d9SJeff Roberson 
69262fa74d9SJeff Roberson /*
69362fa74d9SJeff Roberson  * Simultaneously find the highest and lowest loaded cpu reachable via
69462fa74d9SJeff Roberson  * cg.
69562fa74d9SJeff Roberson  */
69662fa74d9SJeff Roberson static inline void
69762fa74d9SJeff Roberson sched_both(struct cpu_group *cg, cpumask_t mask, int *lowcpu, int *highcpu)
69862fa74d9SJeff Roberson {
69962fa74d9SJeff Roberson 	struct cpu_search high;
70062fa74d9SJeff Roberson 	struct cpu_search low;
70162fa74d9SJeff Roberson 
70262fa74d9SJeff Roberson 	low.cs_cpu = -1;
70362fa74d9SJeff Roberson 	low.cs_limit = -1;
70462fa74d9SJeff Roberson 	low.cs_load = -1;
70562fa74d9SJeff Roberson 	low.cs_mask = mask;
70662fa74d9SJeff Roberson 	high.cs_load = 0;
70762fa74d9SJeff Roberson 	high.cs_cpu = -1;
70862fa74d9SJeff Roberson 	high.cs_limit = -1;
70962fa74d9SJeff Roberson 	high.cs_mask = mask;
71062fa74d9SJeff Roberson 	cpu_search_both(cg, &low, &high);
71162fa74d9SJeff Roberson 	*lowcpu = low.cs_cpu;
71262fa74d9SJeff Roberson 	*highcpu = high.cs_cpu;
71362fa74d9SJeff Roberson 	return;
71462fa74d9SJeff Roberson }
71562fa74d9SJeff Roberson 
71662fa74d9SJeff Roberson static void
71762fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg)
71862fa74d9SJeff Roberson {
71962fa74d9SJeff Roberson 	cpumask_t mask;
72062fa74d9SJeff Roberson 	int high;
72162fa74d9SJeff Roberson 	int low;
72262fa74d9SJeff Roberson 	int i;
72362fa74d9SJeff Roberson 
72462fa74d9SJeff Roberson 	mask = -1;
72562fa74d9SJeff Roberson 	for (;;) {
72662fa74d9SJeff Roberson 		sched_both(cg, mask, &low, &high);
72762fa74d9SJeff Roberson 		if (low == high || low == -1 || high == -1)
72862fa74d9SJeff Roberson 			break;
72962fa74d9SJeff Roberson 		if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low)))
73062fa74d9SJeff Roberson 			break;
73162fa74d9SJeff Roberson 		/*
73262fa74d9SJeff Roberson 		 * If we failed to move any threads determine which cpu
73362fa74d9SJeff Roberson 		 * to kick out of the set and try again.
73462fa74d9SJeff Roberson 	 	 */
73562fa74d9SJeff Roberson 		if (TDQ_CPU(high)->tdq_transferable == 0)
73662fa74d9SJeff Roberson 			mask &= ~(1 << high);
73762fa74d9SJeff Roberson 		else
73862fa74d9SJeff Roberson 			mask &= ~(1 << low);
73962fa74d9SJeff Roberson 	}
74062fa74d9SJeff Roberson 
74162fa74d9SJeff Roberson 	for (i = 0; i < cg->cg_children; i++)
74262fa74d9SJeff Roberson 		sched_balance_group(&cg->cg_child[i]);
74362fa74d9SJeff Roberson }
74462fa74d9SJeff Roberson 
74562fa74d9SJeff Roberson static void
7467fcf154aSJeff Roberson sched_balance()
747356500a3SJeff Roberson {
7487fcf154aSJeff Roberson 	struct tdq *tdq;
749356500a3SJeff Roberson 
7507fcf154aSJeff Roberson 	/*
7517fcf154aSJeff Roberson 	 * Select a random time between .5 * balance_interval and
7527fcf154aSJeff Roberson 	 * 1.5 * balance_interval.
7537fcf154aSJeff Roberson 	 */
7547fcf154aSJeff Roberson 	balance_ticks = max(balance_interval / 2, 1);
7557fcf154aSJeff Roberson 	balance_ticks += random() % balance_interval;
756ae7a6b38SJeff Roberson 	if (smp_started == 0 || rebalance == 0)
757598b368dSJeff Roberson 		return;
7587fcf154aSJeff Roberson 	tdq = TDQ_SELF();
7597fcf154aSJeff Roberson 	TDQ_UNLOCK(tdq);
76062fa74d9SJeff Roberson 	sched_balance_group(cpu_top);
7617fcf154aSJeff Roberson 	TDQ_LOCK(tdq);
762cac77d04SJeff Roberson }
76386f8ae96SJeff Roberson 
764ae7a6b38SJeff Roberson /*
765ae7a6b38SJeff Roberson  * Lock two thread queues using their address to maintain lock order.
766ae7a6b38SJeff Roberson  */
767ae7a6b38SJeff Roberson static void
768ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two)
769ae7a6b38SJeff Roberson {
770ae7a6b38SJeff Roberson 	if (one < two) {
771ae7a6b38SJeff Roberson 		TDQ_LOCK(one);
772ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(two, MTX_DUPOK);
773ae7a6b38SJeff Roberson 	} else {
774ae7a6b38SJeff Roberson 		TDQ_LOCK(two);
775ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(one, MTX_DUPOK);
776ae7a6b38SJeff Roberson 	}
777ae7a6b38SJeff Roberson }
778ae7a6b38SJeff Roberson 
779ae7a6b38SJeff Roberson /*
7807fcf154aSJeff Roberson  * Unlock two thread queues.  Order is not important here.
7817fcf154aSJeff Roberson  */
7827fcf154aSJeff Roberson static void
7837fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two)
7847fcf154aSJeff Roberson {
7857fcf154aSJeff Roberson 	TDQ_UNLOCK(one);
7867fcf154aSJeff Roberson 	TDQ_UNLOCK(two);
7877fcf154aSJeff Roberson }
7887fcf154aSJeff Roberson 
7897fcf154aSJeff Roberson /*
790ae7a6b38SJeff Roberson  * Transfer load between two imbalanced thread queues.
791ae7a6b38SJeff Roberson  */
79262fa74d9SJeff Roberson static int
793ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low)
794cac77d04SJeff Roberson {
795cac77d04SJeff Roberson 	int transferable;
796cac77d04SJeff Roberson 	int high_load;
797cac77d04SJeff Roberson 	int low_load;
79862fa74d9SJeff Roberson 	int moved;
799cac77d04SJeff Roberson 	int move;
800cac77d04SJeff Roberson 	int diff;
801cac77d04SJeff Roberson 	int i;
802cac77d04SJeff Roberson 
803ae7a6b38SJeff Roberson 	tdq_lock_pair(high, low);
804d2ad694cSJeff Roberson 	transferable = high->tdq_transferable;
805d2ad694cSJeff Roberson 	high_load = high->tdq_load;
806d2ad694cSJeff Roberson 	low_load = low->tdq_load;
80762fa74d9SJeff Roberson 	moved = 0;
808155b9987SJeff Roberson 	/*
809155b9987SJeff Roberson 	 * Determine what the imbalance is and then adjust that to how many
810d2ad694cSJeff Roberson 	 * threads we actually have to give up (transferable).
811155b9987SJeff Roberson 	 */
812ae7a6b38SJeff Roberson 	if (transferable != 0) {
813cac77d04SJeff Roberson 		diff = high_load - low_load;
814356500a3SJeff Roberson 		move = diff / 2;
815356500a3SJeff Roberson 		if (diff & 0x1)
816356500a3SJeff Roberson 			move++;
81780f86c9fSJeff Roberson 		move = min(move, transferable);
818356500a3SJeff Roberson 		for (i = 0; i < move; i++)
81962fa74d9SJeff Roberson 			moved += tdq_move(high, low);
820a5423ea3SJeff Roberson 		/*
821a5423ea3SJeff Roberson 		 * IPI the target cpu to force it to reschedule with the new
822a5423ea3SJeff Roberson 		 * workload.
823a5423ea3SJeff Roberson 		 */
824a5423ea3SJeff Roberson 		ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT);
825ae7a6b38SJeff Roberson 	}
8267fcf154aSJeff Roberson 	tdq_unlock_pair(high, low);
82762fa74d9SJeff Roberson 	return (moved);
828356500a3SJeff Roberson }
829356500a3SJeff Roberson 
830ae7a6b38SJeff Roberson /*
831ae7a6b38SJeff Roberson  * Move a thread from one thread queue to another.
832ae7a6b38SJeff Roberson  */
83362fa74d9SJeff Roberson static int
834ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to)
835356500a3SJeff Roberson {
836ad1e7d28SJulian Elischer 	struct td_sched *ts;
837ae7a6b38SJeff Roberson 	struct thread *td;
838ae7a6b38SJeff Roberson 	struct tdq *tdq;
839ae7a6b38SJeff Roberson 	int cpu;
840356500a3SJeff Roberson 
8417fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(from, MA_OWNED);
8427fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(to, MA_OWNED);
8437fcf154aSJeff Roberson 
844ad1e7d28SJulian Elischer 	tdq = from;
845ae7a6b38SJeff Roberson 	cpu = TDQ_ID(to);
8469727e637SJeff Roberson 	td = tdq_steal(tdq, cpu);
8479727e637SJeff Roberson 	if (td == NULL)
84862fa74d9SJeff Roberson 		return (0);
8499727e637SJeff Roberson 	ts = td->td_sched;
850ae7a6b38SJeff Roberson 	/*
851ae7a6b38SJeff Roberson 	 * Although the run queue is locked the thread may be blocked.  Lock
8527fcf154aSJeff Roberson 	 * it to clear this and acquire the run-queue lock.
853ae7a6b38SJeff Roberson 	 */
854ae7a6b38SJeff Roberson 	thread_lock(td);
8557fcf154aSJeff Roberson 	/* Drop recursive lock on from acquired via thread_lock(). */
856ae7a6b38SJeff Roberson 	TDQ_UNLOCK(from);
857ae7a6b38SJeff Roberson 	sched_rem(td);
8587b8bfa0dSJeff Roberson 	ts->ts_cpu = cpu;
859ae7a6b38SJeff Roberson 	td->td_lock = TDQ_LOCKPTR(to);
860ae7a6b38SJeff Roberson 	tdq_add(to, td, SRQ_YIELDING);
86162fa74d9SJeff Roberson 	return (1);
862356500a3SJeff Roberson }
86322bf7d9aSJeff Roberson 
864ae7a6b38SJeff Roberson /*
865ae7a6b38SJeff Roberson  * This tdq has idled.  Try to steal a thread from another cpu and switch
866ae7a6b38SJeff Roberson  * to it.
867ae7a6b38SJeff Roberson  */
86880f86c9fSJeff Roberson static int
869ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq)
87022bf7d9aSJeff Roberson {
87162fa74d9SJeff Roberson 	struct cpu_group *cg;
872ad1e7d28SJulian Elischer 	struct tdq *steal;
87362fa74d9SJeff Roberson 	cpumask_t mask;
87462fa74d9SJeff Roberson 	int thresh;
875ae7a6b38SJeff Roberson 	int cpu;
87680f86c9fSJeff Roberson 
87788f530ccSJeff Roberson 	if (smp_started == 0 || steal_idle == 0)
87888f530ccSJeff Roberson 		return (1);
87962fa74d9SJeff Roberson 	mask = -1;
88062fa74d9SJeff Roberson 	mask &= ~PCPU_GET(cpumask);
88162fa74d9SJeff Roberson 	/* We don't want to be preempted while we're iterating. */
882ae7a6b38SJeff Roberson 	spinlock_enter();
88362fa74d9SJeff Roberson 	for (cg = tdq->tdq_cg; cg != NULL; ) {
88462fa74d9SJeff Roberson 		if ((cg->cg_flags & (CG_FLAG_HTT | CG_FLAG_THREAD)) == 0)
88562fa74d9SJeff Roberson 			thresh = steal_thresh;
88662fa74d9SJeff Roberson 		else
88762fa74d9SJeff Roberson 			thresh = 1;
88862fa74d9SJeff Roberson 		cpu = sched_highest(cg, mask, thresh);
88962fa74d9SJeff Roberson 		if (cpu == -1) {
89062fa74d9SJeff Roberson 			cg = cg->cg_parent;
89180f86c9fSJeff Roberson 			continue;
8927b8bfa0dSJeff Roberson 		}
8937b8bfa0dSJeff Roberson 		steal = TDQ_CPU(cpu);
89462fa74d9SJeff Roberson 		mask &= ~(1 << cpu);
8957fcf154aSJeff Roberson 		tdq_lock_pair(tdq, steal);
89662fa74d9SJeff Roberson 		if (steal->tdq_load < thresh || steal->tdq_transferable == 0) {
8977fcf154aSJeff Roberson 			tdq_unlock_pair(tdq, steal);
89862fa74d9SJeff Roberson 			continue;
89962fa74d9SJeff Roberson 		}
90062fa74d9SJeff Roberson 		/*
90162fa74d9SJeff Roberson 		 * If a thread was added while interrupts were disabled don't
90262fa74d9SJeff Roberson 		 * steal one here.  If we fail to acquire one due to affinity
90362fa74d9SJeff Roberson 		 * restrictions loop again with this cpu removed from the
90462fa74d9SJeff Roberson 		 * set.
90562fa74d9SJeff Roberson 		 */
90662fa74d9SJeff Roberson 		if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) {
90762fa74d9SJeff Roberson 			tdq_unlock_pair(tdq, steal);
90862fa74d9SJeff Roberson 			continue;
90980f86c9fSJeff Roberson 		}
910ae7a6b38SJeff Roberson 		spinlock_exit();
911ae7a6b38SJeff Roberson 		TDQ_UNLOCK(steal);
9128df78c41SJeff Roberson 		mi_switch(SW_VOL | SWT_IDLE, NULL);
913ae7a6b38SJeff Roberson 		thread_unlock(curthread);
9147b8bfa0dSJeff Roberson 
9157b8bfa0dSJeff Roberson 		return (0);
91622bf7d9aSJeff Roberson 	}
91762fa74d9SJeff Roberson 	spinlock_exit();
91862fa74d9SJeff Roberson 	return (1);
91962fa74d9SJeff Roberson }
92022bf7d9aSJeff Roberson 
921ae7a6b38SJeff Roberson /*
922ae7a6b38SJeff Roberson  * Notify a remote cpu of new work.  Sends an IPI if criteria are met.
923ae7a6b38SJeff Roberson  */
92422bf7d9aSJeff Roberson static void
9259727e637SJeff Roberson tdq_notify(struct tdq *tdq, struct thread *td)
92622bf7d9aSJeff Roberson {
927fc3a97dcSJeff Roberson 	int cpri;
928fc3a97dcSJeff Roberson 	int pri;
9297b8bfa0dSJeff Roberson 	int cpu;
93022bf7d9aSJeff Roberson 
931ff256d9cSJeff Roberson 	if (tdq->tdq_ipipending)
932ff256d9cSJeff Roberson 		return;
9339727e637SJeff Roberson 	cpu = td->td_sched->ts_cpu;
9349727e637SJeff Roberson 	pri = td->td_priority;
935ff256d9cSJeff Roberson 	cpri = pcpu_find(cpu)->pc_curthread->td_priority;
936ff256d9cSJeff Roberson 	if (!sched_shouldpreempt(pri, cpri, 1))
9376b2f763fSJeff Roberson 		return;
938ff256d9cSJeff Roberson 	tdq->tdq_ipipending = 1;
93914618990SJeff Roberson 	ipi_selected(1 << cpu, IPI_PREEMPT);
94022bf7d9aSJeff Roberson }
94122bf7d9aSJeff Roberson 
942ae7a6b38SJeff Roberson /*
943ae7a6b38SJeff Roberson  * Steals load from a timeshare queue.  Honors the rotating queue head
944ae7a6b38SJeff Roberson  * index.
945ae7a6b38SJeff Roberson  */
9469727e637SJeff Roberson static struct thread *
94762fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start)
948ae7a6b38SJeff Roberson {
949ae7a6b38SJeff Roberson 	struct rqbits *rqb;
950ae7a6b38SJeff Roberson 	struct rqhead *rqh;
9519727e637SJeff Roberson 	struct thread *td;
952ae7a6b38SJeff Roberson 	int first;
953ae7a6b38SJeff Roberson 	int bit;
954ae7a6b38SJeff Roberson 	int pri;
955ae7a6b38SJeff Roberson 	int i;
956ae7a6b38SJeff Roberson 
957ae7a6b38SJeff Roberson 	rqb = &rq->rq_status;
958ae7a6b38SJeff Roberson 	bit = start & (RQB_BPW -1);
959ae7a6b38SJeff Roberson 	pri = 0;
960ae7a6b38SJeff Roberson 	first = 0;
961ae7a6b38SJeff Roberson again:
962ae7a6b38SJeff Roberson 	for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
963ae7a6b38SJeff Roberson 		if (rqb->rqb_bits[i] == 0)
964ae7a6b38SJeff Roberson 			continue;
965ae7a6b38SJeff Roberson 		if (bit != 0) {
966ae7a6b38SJeff Roberson 			for (pri = bit; pri < RQB_BPW; pri++)
967ae7a6b38SJeff Roberson 				if (rqb->rqb_bits[i] & (1ul << pri))
968ae7a6b38SJeff Roberson 					break;
969ae7a6b38SJeff Roberson 			if (pri >= RQB_BPW)
970ae7a6b38SJeff Roberson 				continue;
971ae7a6b38SJeff Roberson 		} else
972ae7a6b38SJeff Roberson 			pri = RQB_FFS(rqb->rqb_bits[i]);
973ae7a6b38SJeff Roberson 		pri += (i << RQB_L2BPW);
974ae7a6b38SJeff Roberson 		rqh = &rq->rq_queues[pri];
9759727e637SJeff Roberson 		TAILQ_FOREACH(td, rqh, td_runq) {
9769727e637SJeff Roberson 			if (first && THREAD_CAN_MIGRATE(td) &&
9779727e637SJeff Roberson 			    THREAD_CAN_SCHED(td, cpu))
9789727e637SJeff Roberson 				return (td);
979ae7a6b38SJeff Roberson 			first = 1;
980ae7a6b38SJeff Roberson 		}
981ae7a6b38SJeff Roberson 	}
982ae7a6b38SJeff Roberson 	if (start != 0) {
983ae7a6b38SJeff Roberson 		start = 0;
984ae7a6b38SJeff Roberson 		goto again;
985ae7a6b38SJeff Roberson 	}
986ae7a6b38SJeff Roberson 
987ae7a6b38SJeff Roberson 	return (NULL);
988ae7a6b38SJeff Roberson }
989ae7a6b38SJeff Roberson 
990ae7a6b38SJeff Roberson /*
991ae7a6b38SJeff Roberson  * Steals load from a standard linear queue.
992ae7a6b38SJeff Roberson  */
9939727e637SJeff Roberson static struct thread *
99462fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu)
99522bf7d9aSJeff Roberson {
99622bf7d9aSJeff Roberson 	struct rqhead *rqh;
99722bf7d9aSJeff Roberson 	struct rqbits *rqb;
9989727e637SJeff Roberson 	struct thread *td;
99922bf7d9aSJeff Roberson 	int word;
100022bf7d9aSJeff Roberson 	int bit;
100122bf7d9aSJeff Roberson 
100222bf7d9aSJeff Roberson 	rqb = &rq->rq_status;
100322bf7d9aSJeff Roberson 	for (word = 0; word < RQB_LEN; word++) {
100422bf7d9aSJeff Roberson 		if (rqb->rqb_bits[word] == 0)
100522bf7d9aSJeff Roberson 			continue;
100622bf7d9aSJeff Roberson 		for (bit = 0; bit < RQB_BPW; bit++) {
1007a2640c9bSPeter Wemm 			if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
100822bf7d9aSJeff Roberson 				continue;
100922bf7d9aSJeff Roberson 			rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
10109727e637SJeff Roberson 			TAILQ_FOREACH(td, rqh, td_runq)
10119727e637SJeff Roberson 				if (THREAD_CAN_MIGRATE(td) &&
10129727e637SJeff Roberson 				    THREAD_CAN_SCHED(td, cpu))
10139727e637SJeff Roberson 					return (td);
101422bf7d9aSJeff Roberson 		}
101522bf7d9aSJeff Roberson 	}
101622bf7d9aSJeff Roberson 	return (NULL);
101722bf7d9aSJeff Roberson }
101822bf7d9aSJeff Roberson 
1019ae7a6b38SJeff Roberson /*
1020ae7a6b38SJeff Roberson  * Attempt to steal a thread in priority order from a thread queue.
1021ae7a6b38SJeff Roberson  */
10229727e637SJeff Roberson static struct thread *
102362fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu)
102422bf7d9aSJeff Roberson {
10259727e637SJeff Roberson 	struct thread *td;
102622bf7d9aSJeff Roberson 
1027ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
10289727e637SJeff Roberson 	if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL)
10299727e637SJeff Roberson 		return (td);
10309727e637SJeff Roberson 	if ((td = runq_steal_from(&tdq->tdq_timeshare,
10319727e637SJeff Roberson 	    cpu, tdq->tdq_ridx)) != NULL)
10329727e637SJeff Roberson 		return (td);
103362fa74d9SJeff Roberson 	return (runq_steal(&tdq->tdq_idle, cpu));
103422bf7d9aSJeff Roberson }
103580f86c9fSJeff Roberson 
1036ae7a6b38SJeff Roberson /*
1037ae7a6b38SJeff Roberson  * Sets the thread lock and ts_cpu to match the requested cpu.  Unlocks the
10387fcf154aSJeff Roberson  * current lock and returns with the assigned queue locked.
1039ae7a6b38SJeff Roberson  */
1040ae7a6b38SJeff Roberson static inline struct tdq *
10419727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags)
104280f86c9fSJeff Roberson {
10439727e637SJeff Roberson 
1044ae7a6b38SJeff Roberson 	struct tdq *tdq;
104580f86c9fSJeff Roberson 
10469727e637SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1047ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpu);
10489727e637SJeff Roberson 	td->td_sched->ts_cpu = cpu;
10499727e637SJeff Roberson 	/*
10509727e637SJeff Roberson 	 * If the lock matches just return the queue.
10519727e637SJeff Roberson 	 */
1052ae7a6b38SJeff Roberson 	if (td->td_lock == TDQ_LOCKPTR(tdq))
1053ae7a6b38SJeff Roberson 		return (tdq);
1054ae7a6b38SJeff Roberson #ifdef notyet
105580f86c9fSJeff Roberson 	/*
1056a5423ea3SJeff Roberson 	 * If the thread isn't running its lockptr is a
1057ae7a6b38SJeff Roberson 	 * turnstile or a sleepqueue.  We can just lock_set without
1058ae7a6b38SJeff Roberson 	 * blocking.
1059670c524fSJeff Roberson 	 */
1060ae7a6b38SJeff Roberson 	if (TD_CAN_RUN(td)) {
1061ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1062ae7a6b38SJeff Roberson 		thread_lock_set(td, TDQ_LOCKPTR(tdq));
1063ae7a6b38SJeff Roberson 		return (tdq);
1064ae7a6b38SJeff Roberson 	}
1065ae7a6b38SJeff Roberson #endif
106680f86c9fSJeff Roberson 	/*
1067ae7a6b38SJeff Roberson 	 * The hard case, migration, we need to block the thread first to
1068ae7a6b38SJeff Roberson 	 * prevent order reversals with other cpus locks.
10697b8bfa0dSJeff Roberson 	 */
1070ae7a6b38SJeff Roberson 	thread_lock_block(td);
1071ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1072ae7a6b38SJeff Roberson 	thread_lock_unblock(td, TDQ_LOCKPTR(tdq));
1073ae7a6b38SJeff Roberson 	return (tdq);
107480f86c9fSJeff Roberson }
10752454aaf5SJeff Roberson 
10768df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding");
10778df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity");
10788df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity");
10798df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load");
10808df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu");
10818df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration");
10828df78c41SJeff Roberson 
1083ae7a6b38SJeff Roberson static int
10849727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags)
1085ae7a6b38SJeff Roberson {
108662fa74d9SJeff Roberson 	struct cpu_group *cg;
10879727e637SJeff Roberson 	struct td_sched *ts;
1088ae7a6b38SJeff Roberson 	struct tdq *tdq;
108962fa74d9SJeff Roberson 	cpumask_t mask;
10907b8bfa0dSJeff Roberson 	int self;
10917b8bfa0dSJeff Roberson 	int pri;
10927b8bfa0dSJeff Roberson 	int cpu;
10937b8bfa0dSJeff Roberson 
109462fa74d9SJeff Roberson 	self = PCPU_GET(cpuid);
10959727e637SJeff Roberson 	ts = td->td_sched;
10967b8bfa0dSJeff Roberson 	if (smp_started == 0)
10977b8bfa0dSJeff Roberson 		return (self);
109828994a58SJeff Roberson 	/*
109928994a58SJeff Roberson 	 * Don't migrate a running thread from sched_switch().
110028994a58SJeff Roberson 	 */
110162fa74d9SJeff Roberson 	if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td))
110262fa74d9SJeff Roberson 		return (ts->ts_cpu);
11037b8bfa0dSJeff Roberson 	/*
110462fa74d9SJeff Roberson 	 * Prefer to run interrupt threads on the processors that generate
110562fa74d9SJeff Roberson 	 * the interrupt.
11067b8bfa0dSJeff Roberson 	 */
110762fa74d9SJeff Roberson 	if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) &&
11088df78c41SJeff Roberson 	    curthread->td_intr_nesting_level && ts->ts_cpu != self) {
11098df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_intrbind);
111062fa74d9SJeff Roberson 		ts->ts_cpu = self;
11118df78c41SJeff Roberson 	}
111262fa74d9SJeff Roberson 	/*
111362fa74d9SJeff Roberson 	 * If the thread can run on the last cpu and the affinity has not
111462fa74d9SJeff Roberson 	 * expired or it is idle run it there.
111562fa74d9SJeff Roberson 	 */
111662fa74d9SJeff Roberson 	pri = td->td_priority;
111762fa74d9SJeff Roberson 	tdq = TDQ_CPU(ts->ts_cpu);
111862fa74d9SJeff Roberson 	if (THREAD_CAN_SCHED(td, ts->ts_cpu)) {
11198df78c41SJeff Roberson 		if (tdq->tdq_lowpri > PRI_MIN_IDLE) {
11208df78c41SJeff Roberson 			SCHED_STAT_INC(pickcpu_idle_affinity);
112162fa74d9SJeff Roberson 			return (ts->ts_cpu);
11228df78c41SJeff Roberson 		}
11238df78c41SJeff Roberson 		if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri) {
11248df78c41SJeff Roberson 			SCHED_STAT_INC(pickcpu_affinity);
11257b8bfa0dSJeff Roberson 			return (ts->ts_cpu);
11267b8bfa0dSJeff Roberson 		}
11278df78c41SJeff Roberson 	}
11287b8bfa0dSJeff Roberson 	/*
112962fa74d9SJeff Roberson 	 * Search for the highest level in the tree that still has affinity.
11307b8bfa0dSJeff Roberson 	 */
113162fa74d9SJeff Roberson 	cg = NULL;
113262fa74d9SJeff Roberson 	for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent)
113362fa74d9SJeff Roberson 		if (SCHED_AFFINITY(ts, cg->cg_level))
113462fa74d9SJeff Roberson 			break;
113562fa74d9SJeff Roberson 	cpu = -1;
113662fa74d9SJeff Roberson 	mask = td->td_cpuset->cs_mask.__bits[0];
113762fa74d9SJeff Roberson 	if (cg)
113862fa74d9SJeff Roberson 		cpu = sched_lowest(cg, mask, pri);
113962fa74d9SJeff Roberson 	if (cpu == -1)
114062fa74d9SJeff Roberson 		cpu = sched_lowest(cpu_top, mask, -1);
114162fa74d9SJeff Roberson 	/*
114262fa74d9SJeff Roberson 	 * Compare the lowest loaded cpu to current cpu.
114362fa74d9SJeff Roberson 	 */
1144ff256d9cSJeff Roberson 	if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri &&
11458df78c41SJeff Roberson 	    TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) {
11468df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_local);
114762fa74d9SJeff Roberson 		cpu = self;
11488df78c41SJeff Roberson 	} else
11498df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_lowest);
11508df78c41SJeff Roberson 	if (cpu != ts->ts_cpu)
11518df78c41SJeff Roberson 		SCHED_STAT_INC(pickcpu_migration);
1152ff256d9cSJeff Roberson 	KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu."));
1153ae7a6b38SJeff Roberson 	return (cpu);
115480f86c9fSJeff Roberson }
115562fa74d9SJeff Roberson #endif
115622bf7d9aSJeff Roberson 
115722bf7d9aSJeff Roberson /*
115822bf7d9aSJeff Roberson  * Pick the highest priority task we have and return it.
11590c0a98b2SJeff Roberson  */
11609727e637SJeff Roberson static struct thread *
1161ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq)
11625d7ef00cSJeff Roberson {
11639727e637SJeff Roberson 	struct thread *td;
11645d7ef00cSJeff Roberson 
1165ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
11669727e637SJeff Roberson 	td = runq_choose(&tdq->tdq_realtime);
11679727e637SJeff Roberson 	if (td != NULL)
11689727e637SJeff Roberson 		return (td);
11699727e637SJeff Roberson 	td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
11709727e637SJeff Roberson 	if (td != NULL) {
11719727e637SJeff Roberson 		KASSERT(td->td_priority >= PRI_MIN_TIMESHARE,
1172e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on timeshare queue %d",
11739727e637SJeff Roberson 		    td->td_priority));
11749727e637SJeff Roberson 		return (td);
117515dc847eSJeff Roberson 	}
11769727e637SJeff Roberson 	td = runq_choose(&tdq->tdq_idle);
11779727e637SJeff Roberson 	if (td != NULL) {
11789727e637SJeff Roberson 		KASSERT(td->td_priority >= PRI_MIN_IDLE,
1179e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on idle queue %d",
11809727e637SJeff Roberson 		    td->td_priority));
11819727e637SJeff Roberson 		return (td);
1182e7d50326SJeff Roberson 	}
1183e7d50326SJeff Roberson 
1184e7d50326SJeff Roberson 	return (NULL);
1185245f3abfSJeff Roberson }
11860a016a05SJeff Roberson 
1187ae7a6b38SJeff Roberson /*
1188ae7a6b38SJeff Roberson  * Initialize a thread queue.
1189ae7a6b38SJeff Roberson  */
11900a016a05SJeff Roberson static void
1191ad1e7d28SJulian Elischer tdq_setup(struct tdq *tdq)
11920a016a05SJeff Roberson {
1193ae7a6b38SJeff Roberson 
1194c47f202bSJeff Roberson 	if (bootverbose)
1195c47f202bSJeff Roberson 		printf("ULE: setup cpu %d\n", TDQ_ID(tdq));
1196e7d50326SJeff Roberson 	runq_init(&tdq->tdq_realtime);
1197e7d50326SJeff Roberson 	runq_init(&tdq->tdq_timeshare);
1198d2ad694cSJeff Roberson 	runq_init(&tdq->tdq_idle);
119962fa74d9SJeff Roberson 	snprintf(tdq->tdq_name, sizeof(tdq->tdq_name),
120062fa74d9SJeff Roberson 	    "sched lock %d", (int)TDQ_ID(tdq));
120162fa74d9SJeff Roberson 	mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock",
120262fa74d9SJeff Roberson 	    MTX_SPIN | MTX_RECURSE);
12030a016a05SJeff Roberson }
12040a016a05SJeff Roberson 
1205c47f202bSJeff Roberson #ifdef SMP
1206c47f202bSJeff Roberson static void
1207c47f202bSJeff Roberson sched_setup_smp(void)
1208c47f202bSJeff Roberson {
1209c47f202bSJeff Roberson 	struct tdq *tdq;
1210c47f202bSJeff Roberson 	int i;
1211c47f202bSJeff Roberson 
121262fa74d9SJeff Roberson 	cpu_top = smp_topo();
121362fa74d9SJeff Roberson 	for (i = 0; i < MAXCPU; i++) {
1214c47f202bSJeff Roberson 		if (CPU_ABSENT(i))
1215c47f202bSJeff Roberson 			continue;
121662fa74d9SJeff Roberson 		tdq = TDQ_CPU(i);
1217c47f202bSJeff Roberson 		tdq_setup(tdq);
121862fa74d9SJeff Roberson 		tdq->tdq_cg = smp_topo_find(cpu_top, i);
121962fa74d9SJeff Roberson 		if (tdq->tdq_cg == NULL)
122062fa74d9SJeff Roberson 			panic("Can't find cpu group for %d\n", i);
1221c47f202bSJeff Roberson 	}
122262fa74d9SJeff Roberson 	balance_tdq = TDQ_SELF();
122362fa74d9SJeff Roberson 	sched_balance();
1224c47f202bSJeff Roberson }
1225c47f202bSJeff Roberson #endif
1226c47f202bSJeff Roberson 
1227ae7a6b38SJeff Roberson /*
1228ae7a6b38SJeff Roberson  * Setup the thread queues and initialize the topology based on MD
1229ae7a6b38SJeff Roberson  * information.
1230ae7a6b38SJeff Roberson  */
123135e6168fSJeff Roberson static void
123235e6168fSJeff Roberson sched_setup(void *dummy)
123335e6168fSJeff Roberson {
1234ae7a6b38SJeff Roberson 	struct tdq *tdq;
1235c47f202bSJeff Roberson 
1236c47f202bSJeff Roberson 	tdq = TDQ_SELF();
12370ec896fdSJeff Roberson #ifdef SMP
1238c47f202bSJeff Roberson 	sched_setup_smp();
1239749d01b0SJeff Roberson #else
1240c47f202bSJeff Roberson 	tdq_setup(tdq);
1241356500a3SJeff Roberson #endif
1242ae7a6b38SJeff Roberson 	/*
1243ae7a6b38SJeff Roberson 	 * To avoid divide-by-zero, we set realstathz a dummy value
1244ae7a6b38SJeff Roberson 	 * in case which sched_clock() called before sched_initticks().
1245ae7a6b38SJeff Roberson 	 */
1246ae7a6b38SJeff Roberson 	realstathz = hz;
1247ae7a6b38SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1248ae7a6b38SJeff Roberson 	tickincr = 1 << SCHED_TICK_SHIFT;
1249ae7a6b38SJeff Roberson 
1250ae7a6b38SJeff Roberson 	/* Add thread0's load since it's running. */
1251ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1252c47f202bSJeff Roberson 	thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF());
12539727e637SJeff Roberson 	tdq_load_add(tdq, &thread0);
125462fa74d9SJeff Roberson 	tdq->tdq_lowpri = thread0.td_priority;
1255ae7a6b38SJeff Roberson 	TDQ_UNLOCK(tdq);
125635e6168fSJeff Roberson }
125735e6168fSJeff Roberson 
1258ae7a6b38SJeff Roberson /*
1259ae7a6b38SJeff Roberson  * This routine determines the tickincr after stathz and hz are setup.
1260ae7a6b38SJeff Roberson  */
1261a1d4fe69SDavid Xu /* ARGSUSED */
1262a1d4fe69SDavid Xu static void
1263a1d4fe69SDavid Xu sched_initticks(void *dummy)
1264a1d4fe69SDavid Xu {
1265ae7a6b38SJeff Roberson 	int incr;
1266ae7a6b38SJeff Roberson 
1267a1d4fe69SDavid Xu 	realstathz = stathz ? stathz : hz;
126814618990SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1269a1d4fe69SDavid Xu 
1270a1d4fe69SDavid Xu 	/*
1271e7d50326SJeff Roberson 	 * tickincr is shifted out by 10 to avoid rounding errors due to
12723f872f85SJeff Roberson 	 * hz not being evenly divisible by stathz on all platforms.
1273e7d50326SJeff Roberson 	 */
1274ae7a6b38SJeff Roberson 	incr = (hz << SCHED_TICK_SHIFT) / realstathz;
1275e7d50326SJeff Roberson 	/*
1276e7d50326SJeff Roberson 	 * This does not work for values of stathz that are more than
1277e7d50326SJeff Roberson 	 * 1 << SCHED_TICK_SHIFT * hz.  In practice this does not happen.
1278a1d4fe69SDavid Xu 	 */
1279ae7a6b38SJeff Roberson 	if (incr == 0)
1280ae7a6b38SJeff Roberson 		incr = 1;
1281ae7a6b38SJeff Roberson 	tickincr = incr;
12827b8bfa0dSJeff Roberson #ifdef SMP
12839862717aSJeff Roberson 	/*
12847fcf154aSJeff Roberson 	 * Set the default balance interval now that we know
12857fcf154aSJeff Roberson 	 * what realstathz is.
12867fcf154aSJeff Roberson 	 */
12877fcf154aSJeff Roberson 	balance_interval = realstathz;
12887fcf154aSJeff Roberson 	/*
12899862717aSJeff Roberson 	 * Set steal thresh to log2(mp_ncpu) but no greater than 4.  This
12909862717aSJeff Roberson 	 * prevents excess thrashing on large machines and excess idle on
12919862717aSJeff Roberson 	 * smaller machines.
12929862717aSJeff Roberson 	 */
129362fa74d9SJeff Roberson 	steal_thresh = min(ffs(mp_ncpus) - 1, 3);
12947b8bfa0dSJeff Roberson 	affinity = SCHED_AFFINITY_DEFAULT;
12957b8bfa0dSJeff Roberson #endif
1296a1d4fe69SDavid Xu }
1297a1d4fe69SDavid Xu 
1298a1d4fe69SDavid Xu 
129935e6168fSJeff Roberson /*
1300ae7a6b38SJeff Roberson  * This is the core of the interactivity algorithm.  Determines a score based
1301ae7a6b38SJeff Roberson  * on past behavior.  It is the ratio of sleep time to run time scaled to
1302ae7a6b38SJeff Roberson  * a [0, 100] integer.  This is the voluntary sleep time of a process, which
1303ae7a6b38SJeff Roberson  * differs from the cpu usage because it does not account for time spent
1304ae7a6b38SJeff Roberson  * waiting on a run-queue.  Would be prettier if we had floating point.
1305ae7a6b38SJeff Roberson  */
1306ae7a6b38SJeff Roberson static int
1307ae7a6b38SJeff Roberson sched_interact_score(struct thread *td)
1308ae7a6b38SJeff Roberson {
1309ae7a6b38SJeff Roberson 	struct td_sched *ts;
1310ae7a6b38SJeff Roberson 	int div;
1311ae7a6b38SJeff Roberson 
1312ae7a6b38SJeff Roberson 	ts = td->td_sched;
1313ae7a6b38SJeff Roberson 	/*
1314ae7a6b38SJeff Roberson 	 * The score is only needed if this is likely to be an interactive
1315ae7a6b38SJeff Roberson 	 * task.  Don't go through the expense of computing it if there's
1316ae7a6b38SJeff Roberson 	 * no chance.
1317ae7a6b38SJeff Roberson 	 */
1318ae7a6b38SJeff Roberson 	if (sched_interact <= SCHED_INTERACT_HALF &&
1319ae7a6b38SJeff Roberson 		ts->ts_runtime >= ts->ts_slptime)
1320ae7a6b38SJeff Roberson 			return (SCHED_INTERACT_HALF);
1321ae7a6b38SJeff Roberson 
1322ae7a6b38SJeff Roberson 	if (ts->ts_runtime > ts->ts_slptime) {
1323ae7a6b38SJeff Roberson 		div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF);
1324ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF +
1325ae7a6b38SJeff Roberson 		    (SCHED_INTERACT_HALF - (ts->ts_slptime / div)));
1326ae7a6b38SJeff Roberson 	}
1327ae7a6b38SJeff Roberson 	if (ts->ts_slptime > ts->ts_runtime) {
1328ae7a6b38SJeff Roberson 		div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF);
1329ae7a6b38SJeff Roberson 		return (ts->ts_runtime / div);
1330ae7a6b38SJeff Roberson 	}
1331ae7a6b38SJeff Roberson 	/* runtime == slptime */
1332ae7a6b38SJeff Roberson 	if (ts->ts_runtime)
1333ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF);
1334ae7a6b38SJeff Roberson 
1335ae7a6b38SJeff Roberson 	/*
1336ae7a6b38SJeff Roberson 	 * This can happen if slptime and runtime are 0.
1337ae7a6b38SJeff Roberson 	 */
1338ae7a6b38SJeff Roberson 	return (0);
1339ae7a6b38SJeff Roberson 
1340ae7a6b38SJeff Roberson }
1341ae7a6b38SJeff Roberson 
1342ae7a6b38SJeff Roberson /*
134335e6168fSJeff Roberson  * Scale the scheduling priority according to the "interactivity" of this
134435e6168fSJeff Roberson  * process.
134535e6168fSJeff Roberson  */
134615dc847eSJeff Roberson static void
13478460a577SJohn Birrell sched_priority(struct thread *td)
134835e6168fSJeff Roberson {
1349e7d50326SJeff Roberson 	int score;
135035e6168fSJeff Roberson 	int pri;
135135e6168fSJeff Roberson 
13528460a577SJohn Birrell 	if (td->td_pri_class != PRI_TIMESHARE)
135315dc847eSJeff Roberson 		return;
1354e7d50326SJeff Roberson 	/*
1355e7d50326SJeff Roberson 	 * If the score is interactive we place the thread in the realtime
1356e7d50326SJeff Roberson 	 * queue with a priority that is less than kernel and interrupt
1357e7d50326SJeff Roberson 	 * priorities.  These threads are not subject to nice restrictions.
1358e7d50326SJeff Roberson 	 *
1359ae7a6b38SJeff Roberson 	 * Scores greater than this are placed on the normal timeshare queue
1360e7d50326SJeff Roberson 	 * where the priority is partially decided by the most recent cpu
1361e7d50326SJeff Roberson 	 * utilization and the rest is decided by nice value.
1362a5423ea3SJeff Roberson 	 *
1363a5423ea3SJeff Roberson 	 * The nice value of the process has a linear effect on the calculated
1364a5423ea3SJeff Roberson 	 * score.  Negative nice values make it easier for a thread to be
1365a5423ea3SJeff Roberson 	 * considered interactive.
1366e7d50326SJeff Roberson 	 */
1367e270652bSJeff Roberson 	score = imax(0, sched_interact_score(td) - td->td_proc->p_nice);
1368e7d50326SJeff Roberson 	if (score < sched_interact) {
1369e7d50326SJeff Roberson 		pri = PRI_MIN_REALTIME;
1370e7d50326SJeff Roberson 		pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
1371e7d50326SJeff Roberson 		    * score;
1372e7d50326SJeff Roberson 		KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
13739a93305aSJeff Roberson 		    ("sched_priority: invalid interactive priority %d score %d",
13749a93305aSJeff Roberson 		    pri, score));
1375e7d50326SJeff Roberson 	} else {
1376e7d50326SJeff Roberson 		pri = SCHED_PRI_MIN;
1377e7d50326SJeff Roberson 		if (td->td_sched->ts_ticks)
1378e7d50326SJeff Roberson 			pri += SCHED_PRI_TICKS(td->td_sched);
1379e7d50326SJeff Roberson 		pri += SCHED_PRI_NICE(td->td_proc->p_nice);
1380ae7a6b38SJeff Roberson 		KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE,
1381ae7a6b38SJeff Roberson 		    ("sched_priority: invalid priority %d: nice %d, "
1382ae7a6b38SJeff Roberson 		    "ticks %d ftick %d ltick %d tick pri %d",
1383ae7a6b38SJeff Roberson 		    pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
1384ae7a6b38SJeff Roberson 		    td->td_sched->ts_ftick, td->td_sched->ts_ltick,
1385ae7a6b38SJeff Roberson 		    SCHED_PRI_TICKS(td->td_sched)));
1386e7d50326SJeff Roberson 	}
13878460a577SJohn Birrell 	sched_user_prio(td, pri);
138835e6168fSJeff Roberson 
138915dc847eSJeff Roberson 	return;
139035e6168fSJeff Roberson }
139135e6168fSJeff Roberson 
139235e6168fSJeff Roberson /*
1393d322132cSJeff Roberson  * This routine enforces a maximum limit on the amount of scheduling history
1394ae7a6b38SJeff Roberson  * kept.  It is called after either the slptime or runtime is adjusted.  This
1395ae7a6b38SJeff Roberson  * function is ugly due to integer math.
1396d322132cSJeff Roberson  */
13974b60e324SJeff Roberson static void
13988460a577SJohn Birrell sched_interact_update(struct thread *td)
13994b60e324SJeff Roberson {
1400155b6ca1SJeff Roberson 	struct td_sched *ts;
14019a93305aSJeff Roberson 	u_int sum;
14023f741ca1SJeff Roberson 
1403155b6ca1SJeff Roberson 	ts = td->td_sched;
1404ae7a6b38SJeff Roberson 	sum = ts->ts_runtime + ts->ts_slptime;
1405d322132cSJeff Roberson 	if (sum < SCHED_SLP_RUN_MAX)
1406d322132cSJeff Roberson 		return;
1407d322132cSJeff Roberson 	/*
1408155b6ca1SJeff Roberson 	 * This only happens from two places:
1409155b6ca1SJeff Roberson 	 * 1) We have added an unusual amount of run time from fork_exit.
1410155b6ca1SJeff Roberson 	 * 2) We have added an unusual amount of sleep time from sched_sleep().
1411155b6ca1SJeff Roberson 	 */
1412155b6ca1SJeff Roberson 	if (sum > SCHED_SLP_RUN_MAX * 2) {
1413ae7a6b38SJeff Roberson 		if (ts->ts_runtime > ts->ts_slptime) {
1414ae7a6b38SJeff Roberson 			ts->ts_runtime = SCHED_SLP_RUN_MAX;
1415ae7a6b38SJeff Roberson 			ts->ts_slptime = 1;
1416155b6ca1SJeff Roberson 		} else {
1417ae7a6b38SJeff Roberson 			ts->ts_slptime = SCHED_SLP_RUN_MAX;
1418ae7a6b38SJeff Roberson 			ts->ts_runtime = 1;
1419155b6ca1SJeff Roberson 		}
1420155b6ca1SJeff Roberson 		return;
1421155b6ca1SJeff Roberson 	}
1422155b6ca1SJeff Roberson 	/*
1423d322132cSJeff Roberson 	 * If we have exceeded by more than 1/5th then the algorithm below
1424d322132cSJeff Roberson 	 * will not bring us back into range.  Dividing by two here forces
14252454aaf5SJeff Roberson 	 * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
1426d322132cSJeff Roberson 	 */
142737a35e4aSJeff Roberson 	if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
1428ae7a6b38SJeff Roberson 		ts->ts_runtime /= 2;
1429ae7a6b38SJeff Roberson 		ts->ts_slptime /= 2;
1430d322132cSJeff Roberson 		return;
1431d322132cSJeff Roberson 	}
1432ae7a6b38SJeff Roberson 	ts->ts_runtime = (ts->ts_runtime / 5) * 4;
1433ae7a6b38SJeff Roberson 	ts->ts_slptime = (ts->ts_slptime / 5) * 4;
1434d322132cSJeff Roberson }
1435d322132cSJeff Roberson 
1436ae7a6b38SJeff Roberson /*
1437ae7a6b38SJeff Roberson  * Scale back the interactivity history when a child thread is created.  The
1438ae7a6b38SJeff Roberson  * history is inherited from the parent but the thread may behave totally
1439ae7a6b38SJeff Roberson  * differently.  For example, a shell spawning a compiler process.  We want
1440ae7a6b38SJeff Roberson  * to learn that the compiler is behaving badly very quickly.
1441ae7a6b38SJeff Roberson  */
1442d322132cSJeff Roberson static void
14438460a577SJohn Birrell sched_interact_fork(struct thread *td)
1444d322132cSJeff Roberson {
1445d322132cSJeff Roberson 	int ratio;
1446d322132cSJeff Roberson 	int sum;
1447d322132cSJeff Roberson 
1448ae7a6b38SJeff Roberson 	sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
1449d322132cSJeff Roberson 	if (sum > SCHED_SLP_RUN_FORK) {
1450d322132cSJeff Roberson 		ratio = sum / SCHED_SLP_RUN_FORK;
1451ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime /= ratio;
1452ae7a6b38SJeff Roberson 		td->td_sched->ts_slptime /= ratio;
14534b60e324SJeff Roberson 	}
14544b60e324SJeff Roberson }
14554b60e324SJeff Roberson 
145615dc847eSJeff Roberson /*
1457ae7a6b38SJeff Roberson  * Called from proc0_init() to setup the scheduler fields.
1458ed062c8dSJulian Elischer  */
1459ed062c8dSJulian Elischer void
1460ed062c8dSJulian Elischer schedinit(void)
1461ed062c8dSJulian Elischer {
1462e7d50326SJeff Roberson 
1463ed062c8dSJulian Elischer 	/*
1464ed062c8dSJulian Elischer 	 * Set up the scheduler specific parts of proc0.
1465ed062c8dSJulian Elischer 	 */
1466ed062c8dSJulian Elischer 	proc0.p_sched = NULL; /* XXX */
1467ad1e7d28SJulian Elischer 	thread0.td_sched = &td_sched0;
1468e7d50326SJeff Roberson 	td_sched0.ts_ltick = ticks;
14698ab80cf0SJeff Roberson 	td_sched0.ts_ftick = ticks;
147073daf66fSJeff Roberson 	td_sched0.ts_slice = sched_slice;
1471ed062c8dSJulian Elischer }
1472ed062c8dSJulian Elischer 
1473ed062c8dSJulian Elischer /*
147415dc847eSJeff Roberson  * This is only somewhat accurate since given many processes of the same
147515dc847eSJeff Roberson  * priority they will switch when their slices run out, which will be
1476e7d50326SJeff Roberson  * at most sched_slice stathz ticks.
147715dc847eSJeff Roberson  */
147835e6168fSJeff Roberson int
147935e6168fSJeff Roberson sched_rr_interval(void)
148035e6168fSJeff Roberson {
1481e7d50326SJeff Roberson 
1482e7d50326SJeff Roberson 	/* Convert sched_slice to hz */
1483e7d50326SJeff Roberson 	return (hz/(realstathz/sched_slice));
148435e6168fSJeff Roberson }
148535e6168fSJeff Roberson 
1486ae7a6b38SJeff Roberson /*
1487ae7a6b38SJeff Roberson  * Update the percent cpu tracking information when it is requested or
1488ae7a6b38SJeff Roberson  * the total history exceeds the maximum.  We keep a sliding history of
1489ae7a6b38SJeff Roberson  * tick counts that slowly decays.  This is less precise than the 4BSD
1490ae7a6b38SJeff Roberson  * mechanism since it happens with less regular and frequent events.
1491ae7a6b38SJeff Roberson  */
149222bf7d9aSJeff Roberson static void
1493ad1e7d28SJulian Elischer sched_pctcpu_update(struct td_sched *ts)
149435e6168fSJeff Roberson {
1495e7d50326SJeff Roberson 
1496e7d50326SJeff Roberson 	if (ts->ts_ticks == 0)
1497e7d50326SJeff Roberson 		return;
14988ab80cf0SJeff Roberson 	if (ticks - (hz / 10) < ts->ts_ltick &&
14998ab80cf0SJeff Roberson 	    SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
15008ab80cf0SJeff Roberson 		return;
150135e6168fSJeff Roberson 	/*
150235e6168fSJeff Roberson 	 * Adjust counters and watermark for pctcpu calc.
1503210491d3SJeff Roberson 	 */
1504e7d50326SJeff Roberson 	if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
1505ad1e7d28SJulian Elischer 		ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
1506e7d50326SJeff Roberson 			    SCHED_TICK_TARG;
1507e7d50326SJeff Roberson 	else
1508ad1e7d28SJulian Elischer 		ts->ts_ticks = 0;
1509ad1e7d28SJulian Elischer 	ts->ts_ltick = ticks;
1510e7d50326SJeff Roberson 	ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
151135e6168fSJeff Roberson }
151235e6168fSJeff Roberson 
1513ae7a6b38SJeff Roberson /*
1514ae7a6b38SJeff Roberson  * Adjust the priority of a thread.  Move it to the appropriate run-queue
1515ae7a6b38SJeff Roberson  * if necessary.  This is the back-end for several priority related
1516ae7a6b38SJeff Roberson  * functions.
1517ae7a6b38SJeff Roberson  */
1518e7d50326SJeff Roberson static void
1519f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio)
152035e6168fSJeff Roberson {
1521ad1e7d28SJulian Elischer 	struct td_sched *ts;
152273daf66fSJeff Roberson 	struct tdq *tdq;
152373daf66fSJeff Roberson 	int oldpri;
152435e6168fSJeff Roberson 
152581d47d3fSJeff Roberson 	CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)",
1526431f8906SJulian Elischer 	    td, td->td_name, td->td_priority, prio, curthread,
1527431f8906SJulian Elischer 	    curthread->td_name);
1528ad1e7d28SJulian Elischer 	ts = td->td_sched;
15297b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1530f5c157d9SJohn Baldwin 	if (td->td_priority == prio)
1531f5c157d9SJohn Baldwin 		return;
15323f741ca1SJeff Roberson 	/*
15333f741ca1SJeff Roberson 	 * If the priority has been elevated due to priority
15343f741ca1SJeff Roberson 	 * propagation, we may have to move ourselves to a new
1535e7d50326SJeff Roberson 	 * queue.  This could be optimized to not re-add in some
1536e7d50326SJeff Roberson 	 * cases.
1537f2b74cbfSJeff Roberson 	 */
15386d55b3ecSJeff Roberson 	if (TD_ON_RUNQ(td) && prio < td->td_priority) {
1539e7d50326SJeff Roberson 		sched_rem(td);
1540e7d50326SJeff Roberson 		td->td_priority = prio;
1541ae7a6b38SJeff Roberson 		sched_add(td, SRQ_BORROWING);
154273daf66fSJeff Roberson 		return;
154373daf66fSJeff Roberson 	}
15446d55b3ecSJeff Roberson 	/*
15456d55b3ecSJeff Roberson 	 * If the thread is currently running we may have to adjust the lowpri
15466d55b3ecSJeff Roberson 	 * information so other cpus are aware of our current priority.
15476d55b3ecSJeff Roberson 	 */
15486d55b3ecSJeff Roberson 	if (TD_IS_RUNNING(td)) {
1549ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(ts->ts_cpu);
155062fa74d9SJeff Roberson 		oldpri = td->td_priority;
15513f741ca1SJeff Roberson 		td->td_priority = prio;
155262fa74d9SJeff Roberson 		if (prio < tdq->tdq_lowpri)
155362fa74d9SJeff Roberson 			tdq->tdq_lowpri = prio;
155462fa74d9SJeff Roberson 		else if (tdq->tdq_lowpri == oldpri)
155562fa74d9SJeff Roberson 			tdq_setlowpri(tdq, td);
15566d55b3ecSJeff Roberson 		return;
155773daf66fSJeff Roberson 	}
15586d55b3ecSJeff Roberson 	td->td_priority = prio;
1559ae7a6b38SJeff Roberson }
156035e6168fSJeff Roberson 
1561f5c157d9SJohn Baldwin /*
1562f5c157d9SJohn Baldwin  * Update a thread's priority when it is lent another thread's
1563f5c157d9SJohn Baldwin  * priority.
1564f5c157d9SJohn Baldwin  */
1565f5c157d9SJohn Baldwin void
1566f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio)
1567f5c157d9SJohn Baldwin {
1568f5c157d9SJohn Baldwin 
1569f5c157d9SJohn Baldwin 	td->td_flags |= TDF_BORROWING;
1570f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1571f5c157d9SJohn Baldwin }
1572f5c157d9SJohn Baldwin 
1573f5c157d9SJohn Baldwin /*
1574f5c157d9SJohn Baldwin  * Restore a thread's priority when priority propagation is
1575f5c157d9SJohn Baldwin  * over.  The prio argument is the minimum priority the thread
1576f5c157d9SJohn Baldwin  * needs to have to satisfy other possible priority lending
1577f5c157d9SJohn Baldwin  * requests.  If the thread's regular priority is less
1578f5c157d9SJohn Baldwin  * important than prio, the thread will keep a priority boost
1579f5c157d9SJohn Baldwin  * of prio.
1580f5c157d9SJohn Baldwin  */
1581f5c157d9SJohn Baldwin void
1582f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio)
1583f5c157d9SJohn Baldwin {
1584f5c157d9SJohn Baldwin 	u_char base_pri;
1585f5c157d9SJohn Baldwin 
1586f5c157d9SJohn Baldwin 	if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1587f5c157d9SJohn Baldwin 	    td->td_base_pri <= PRI_MAX_TIMESHARE)
15888460a577SJohn Birrell 		base_pri = td->td_user_pri;
1589f5c157d9SJohn Baldwin 	else
1590f5c157d9SJohn Baldwin 		base_pri = td->td_base_pri;
1591f5c157d9SJohn Baldwin 	if (prio >= base_pri) {
1592f5c157d9SJohn Baldwin 		td->td_flags &= ~TDF_BORROWING;
1593f5c157d9SJohn Baldwin 		sched_thread_priority(td, base_pri);
1594f5c157d9SJohn Baldwin 	} else
1595f5c157d9SJohn Baldwin 		sched_lend_prio(td, prio);
1596f5c157d9SJohn Baldwin }
1597f5c157d9SJohn Baldwin 
1598ae7a6b38SJeff Roberson /*
1599ae7a6b38SJeff Roberson  * Standard entry for setting the priority to an absolute value.
1600ae7a6b38SJeff Roberson  */
1601f5c157d9SJohn Baldwin void
1602f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio)
1603f5c157d9SJohn Baldwin {
1604f5c157d9SJohn Baldwin 	u_char oldprio;
1605f5c157d9SJohn Baldwin 
1606f5c157d9SJohn Baldwin 	/* First, update the base priority. */
1607f5c157d9SJohn Baldwin 	td->td_base_pri = prio;
1608f5c157d9SJohn Baldwin 
1609f5c157d9SJohn Baldwin 	/*
161050aaa791SJohn Baldwin 	 * If the thread is borrowing another thread's priority, don't
1611f5c157d9SJohn Baldwin 	 * ever lower the priority.
1612f5c157d9SJohn Baldwin 	 */
1613f5c157d9SJohn Baldwin 	if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1614f5c157d9SJohn Baldwin 		return;
1615f5c157d9SJohn Baldwin 
1616f5c157d9SJohn Baldwin 	/* Change the real priority. */
1617f5c157d9SJohn Baldwin 	oldprio = td->td_priority;
1618f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1619f5c157d9SJohn Baldwin 
1620f5c157d9SJohn Baldwin 	/*
1621f5c157d9SJohn Baldwin 	 * If the thread is on a turnstile, then let the turnstile update
1622f5c157d9SJohn Baldwin 	 * its state.
1623f5c157d9SJohn Baldwin 	 */
1624f5c157d9SJohn Baldwin 	if (TD_ON_LOCK(td) && oldprio != prio)
1625f5c157d9SJohn Baldwin 		turnstile_adjust(td, oldprio);
1626f5c157d9SJohn Baldwin }
1627f5c157d9SJohn Baldwin 
1628ae7a6b38SJeff Roberson /*
1629ae7a6b38SJeff Roberson  * Set the base user priority, does not effect current running priority.
1630ae7a6b38SJeff Roberson  */
163135e6168fSJeff Roberson void
16328460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio)
16333db720fdSDavid Xu {
16343db720fdSDavid Xu 	u_char oldprio;
16353db720fdSDavid Xu 
16368460a577SJohn Birrell 	td->td_base_user_pri = prio;
1637fc6c30f6SJulian Elischer 	if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
1638fc6c30f6SJulian Elischer                 return;
16398460a577SJohn Birrell 	oldprio = td->td_user_pri;
16408460a577SJohn Birrell 	td->td_user_pri = prio;
16413db720fdSDavid Xu }
16423db720fdSDavid Xu 
16433db720fdSDavid Xu void
16443db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio)
16453db720fdSDavid Xu {
16463db720fdSDavid Xu 	u_char oldprio;
16473db720fdSDavid Xu 
1648435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
16493db720fdSDavid Xu 	td->td_flags |= TDF_UBORROWING;
1650f645b5daSMaxim Konovalov 	oldprio = td->td_user_pri;
16518460a577SJohn Birrell 	td->td_user_pri = prio;
16523db720fdSDavid Xu }
16533db720fdSDavid Xu 
16543db720fdSDavid Xu void
16553db720fdSDavid Xu sched_unlend_user_prio(struct thread *td, u_char prio)
16563db720fdSDavid Xu {
16573db720fdSDavid Xu 	u_char base_pri;
16583db720fdSDavid Xu 
1659435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
16608460a577SJohn Birrell 	base_pri = td->td_base_user_pri;
16613db720fdSDavid Xu 	if (prio >= base_pri) {
16623db720fdSDavid Xu 		td->td_flags &= ~TDF_UBORROWING;
16638460a577SJohn Birrell 		sched_user_prio(td, base_pri);
1664435806d3SDavid Xu 	} else {
16653db720fdSDavid Xu 		sched_lend_user_prio(td, prio);
16663db720fdSDavid Xu 	}
1667435806d3SDavid Xu }
16683db720fdSDavid Xu 
1669ae7a6b38SJeff Roberson /*
1670731016feSWojciech A. Koszek  * Block a thread for switching.  Similar to thread_block() but does not
1671731016feSWojciech A. Koszek  * bump the spin count.
1672731016feSWojciech A. Koszek  */
1673731016feSWojciech A. Koszek static inline struct mtx *
1674731016feSWojciech A. Koszek thread_block_switch(struct thread *td)
1675731016feSWojciech A. Koszek {
1676731016feSWojciech A. Koszek 	struct mtx *lock;
1677731016feSWojciech A. Koszek 
1678731016feSWojciech A. Koszek 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1679731016feSWojciech A. Koszek 	lock = td->td_lock;
1680731016feSWojciech A. Koszek 	td->td_lock = &blocked_lock;
1681731016feSWojciech A. Koszek 	mtx_unlock_spin(lock);
1682731016feSWojciech A. Koszek 
1683731016feSWojciech A. Koszek 	return (lock);
1684731016feSWojciech A. Koszek }
1685731016feSWojciech A. Koszek 
1686731016feSWojciech A. Koszek /*
1687c47f202bSJeff Roberson  * Handle migration from sched_switch().  This happens only for
1688c47f202bSJeff Roberson  * cpu binding.
1689c47f202bSJeff Roberson  */
1690c47f202bSJeff Roberson static struct mtx *
1691c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
1692c47f202bSJeff Roberson {
1693c47f202bSJeff Roberson 	struct tdq *tdn;
1694c47f202bSJeff Roberson 
1695c47f202bSJeff Roberson 	tdn = TDQ_CPU(td->td_sched->ts_cpu);
1696c47f202bSJeff Roberson #ifdef SMP
16979727e637SJeff Roberson 	tdq_load_rem(tdq, td);
1698c47f202bSJeff Roberson 	/*
1699c47f202bSJeff Roberson 	 * Do the lock dance required to avoid LOR.  We grab an extra
1700c47f202bSJeff Roberson 	 * spinlock nesting to prevent preemption while we're
1701c47f202bSJeff Roberson 	 * not holding either run-queue lock.
1702c47f202bSJeff Roberson 	 */
1703c47f202bSJeff Roberson 	spinlock_enter();
1704c47f202bSJeff Roberson 	thread_block_switch(td);	/* This releases the lock on tdq. */
1705c47f202bSJeff Roberson 	TDQ_LOCK(tdn);
1706c47f202bSJeff Roberson 	tdq_add(tdn, td, flags);
17079727e637SJeff Roberson 	tdq_notify(tdn, td);
1708c47f202bSJeff Roberson 	/*
1709c47f202bSJeff Roberson 	 * After we unlock tdn the new cpu still can't switch into this
1710c47f202bSJeff Roberson 	 * thread until we've unblocked it in cpu_switch().  The lock
1711c47f202bSJeff Roberson 	 * pointers may match in the case of HTT cores.  Don't unlock here
1712c47f202bSJeff Roberson 	 * or we can deadlock when the other CPU runs the IPI handler.
1713c47f202bSJeff Roberson 	 */
1714c47f202bSJeff Roberson 	if (TDQ_LOCKPTR(tdn) != TDQ_LOCKPTR(tdq)) {
1715c47f202bSJeff Roberson 		TDQ_UNLOCK(tdn);
1716c47f202bSJeff Roberson 		TDQ_LOCK(tdq);
1717c47f202bSJeff Roberson 	}
1718c47f202bSJeff Roberson 	spinlock_exit();
1719c47f202bSJeff Roberson #endif
1720c47f202bSJeff Roberson 	return (TDQ_LOCKPTR(tdn));
1721c47f202bSJeff Roberson }
1722c47f202bSJeff Roberson 
1723c47f202bSJeff Roberson /*
1724ae7a6b38SJeff Roberson  * Release a thread that was blocked with thread_block_switch().
1725ae7a6b38SJeff Roberson  */
1726ae7a6b38SJeff Roberson static inline void
1727ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx)
1728ae7a6b38SJeff Roberson {
1729ae7a6b38SJeff Roberson 	atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock,
1730ae7a6b38SJeff Roberson 	    (uintptr_t)mtx);
1731ae7a6b38SJeff Roberson }
1732ae7a6b38SJeff Roberson 
1733ae7a6b38SJeff Roberson /*
1734ae7a6b38SJeff Roberson  * Switch threads.  This function has to handle threads coming in while
1735ae7a6b38SJeff Roberson  * blocked for some reason, running, or idle.  It also must deal with
1736ae7a6b38SJeff Roberson  * migrating a thread from one queue to another as running threads may
1737ae7a6b38SJeff Roberson  * be assigned elsewhere via binding.
1738ae7a6b38SJeff Roberson  */
17393db720fdSDavid Xu void
17403389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags)
174135e6168fSJeff Roberson {
1742c02bbb43SJeff Roberson 	struct tdq *tdq;
1743ad1e7d28SJulian Elischer 	struct td_sched *ts;
1744ae7a6b38SJeff Roberson 	struct mtx *mtx;
1745c47f202bSJeff Roberson 	int srqflag;
1746ae7a6b38SJeff Roberson 	int cpuid;
174735e6168fSJeff Roberson 
17487b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17496d55b3ecSJeff Roberson 	KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument"));
175035e6168fSJeff Roberson 
1751ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
1752ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
1753e7d50326SJeff Roberson 	ts = td->td_sched;
1754c47f202bSJeff Roberson 	mtx = td->td_lock;
1755ae7a6b38SJeff Roberson 	ts->ts_rltick = ticks;
1756060563ecSJulian Elischer 	td->td_lastcpu = td->td_oncpu;
1757060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
175852eb8464SJohn Baldwin 	td->td_flags &= ~TDF_NEEDRESCHED;
175977918643SStephan Uphoff 	td->td_owepreempt = 0;
1760b11fdad0SJeff Roberson 	/*
1761ae7a6b38SJeff Roberson 	 * The lock pointer in an idle thread should never change.  Reset it
1762ae7a6b38SJeff Roberson 	 * to CAN_RUN as well.
1763b11fdad0SJeff Roberson 	 */
1764486a9414SJulian Elischer 	if (TD_IS_IDLETHREAD(td)) {
1765ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1766bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
17677b20fb19SJeff Roberson 	} else if (TD_IS_RUNNING(td)) {
1768ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1769c47f202bSJeff Roberson 		srqflag = (flags & SW_PREEMPT) ?
1770598b368dSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1771c47f202bSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING;
1772c47f202bSJeff Roberson 		if (ts->ts_cpu == cpuid)
17739727e637SJeff Roberson 			tdq_runq_add(tdq, td, srqflag);
1774c47f202bSJeff Roberson 		else
1775c47f202bSJeff Roberson 			mtx = sched_switch_migrate(tdq, td, srqflag);
1776ae7a6b38SJeff Roberson 	} else {
1777ae7a6b38SJeff Roberson 		/* This thread must be going to sleep. */
1778ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1779ae7a6b38SJeff Roberson 		mtx = thread_block_switch(td);
17809727e637SJeff Roberson 		tdq_load_rem(tdq, td);
1781ae7a6b38SJeff Roberson 	}
1782ae7a6b38SJeff Roberson 	/*
1783ae7a6b38SJeff Roberson 	 * We enter here with the thread blocked and assigned to the
1784ae7a6b38SJeff Roberson 	 * appropriate cpu run-queue or sleep-queue and with the current
1785ae7a6b38SJeff Roberson 	 * thread-queue locked.
1786ae7a6b38SJeff Roberson 	 */
1787ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
17882454aaf5SJeff Roberson 	newtd = choosethread();
1789ae7a6b38SJeff Roberson 	/*
1790ae7a6b38SJeff Roberson 	 * Call the MD code to switch contexts if necessary.
1791ae7a6b38SJeff Roberson 	 */
1792ebccf1e3SJoseph Koshy 	if (td != newtd) {
1793ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1794ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1795ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1796ebccf1e3SJoseph Koshy #endif
1797eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
179859c68134SJeff Roberson 		TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
1799ae7a6b38SJeff Roberson 		cpu_switch(td, newtd, mtx);
1800ae7a6b38SJeff Roberson 		/*
1801ae7a6b38SJeff Roberson 		 * We may return from cpu_switch on a different cpu.  However,
1802ae7a6b38SJeff Roberson 		 * we always return with td_lock pointing to the current cpu's
1803ae7a6b38SJeff Roberson 		 * run queue lock.
1804ae7a6b38SJeff Roberson 		 */
1805ae7a6b38SJeff Roberson 		cpuid = PCPU_GET(cpuid);
1806ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(cpuid);
1807eea4f254SJeff Roberson 		lock_profile_obtain_lock_success(
1808eea4f254SJeff Roberson 		    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
1809ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1810ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1811ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1812ebccf1e3SJoseph Koshy #endif
1813ae7a6b38SJeff Roberson 	} else
1814ae7a6b38SJeff Roberson 		thread_unblock_switch(td, mtx);
1815ae7a6b38SJeff Roberson 	/*
1816ae7a6b38SJeff Roberson 	 * Assert that all went well and return.
1817ae7a6b38SJeff Roberson 	 */
1818ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED);
1819ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1820ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
182135e6168fSJeff Roberson }
182235e6168fSJeff Roberson 
1823ae7a6b38SJeff Roberson /*
1824ae7a6b38SJeff Roberson  * Adjust thread priorities as a result of a nice request.
1825ae7a6b38SJeff Roberson  */
182635e6168fSJeff Roberson void
1827fa885116SJulian Elischer sched_nice(struct proc *p, int nice)
182835e6168fSJeff Roberson {
182935e6168fSJeff Roberson 	struct thread *td;
183035e6168fSJeff Roberson 
1831fa885116SJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
1832e7d50326SJeff Roberson 
1833fa885116SJulian Elischer 	p->p_nice = nice;
18348460a577SJohn Birrell 	FOREACH_THREAD_IN_PROC(p, td) {
18357b20fb19SJeff Roberson 		thread_lock(td);
18368460a577SJohn Birrell 		sched_priority(td);
1837e7d50326SJeff Roberson 		sched_prio(td, td->td_base_user_pri);
18387b20fb19SJeff Roberson 		thread_unlock(td);
183935e6168fSJeff Roberson 	}
1840fa885116SJulian Elischer }
184135e6168fSJeff Roberson 
1842ae7a6b38SJeff Roberson /*
1843ae7a6b38SJeff Roberson  * Record the sleep time for the interactivity scorer.
1844ae7a6b38SJeff Roberson  */
184535e6168fSJeff Roberson void
1846c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio)
184735e6168fSJeff Roberson {
1848e7d50326SJeff Roberson 
18497b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
185035e6168fSJeff Roberson 
185154b0e65fSJeff Roberson 	td->td_slptick = ticks;
1852c5aa6b58SJeff Roberson 	if (TD_IS_SUSPENDED(td) || prio <= PSOCK)
1853c5aa6b58SJeff Roberson 		td->td_flags |= TDF_CANSWAP;
18540502fe2eSJeff Roberson 	if (static_boost == 1 && prio)
1855c5aa6b58SJeff Roberson 		sched_prio(td, prio);
18560502fe2eSJeff Roberson 	else if (static_boost && td->td_priority > static_boost)
18570502fe2eSJeff Roberson 		sched_prio(td, static_boost);
185835e6168fSJeff Roberson }
185935e6168fSJeff Roberson 
1860ae7a6b38SJeff Roberson /*
1861ae7a6b38SJeff Roberson  * Schedule a thread to resume execution and record how long it voluntarily
1862ae7a6b38SJeff Roberson  * slept.  We also update the pctcpu, interactivity, and priority.
1863ae7a6b38SJeff Roberson  */
186435e6168fSJeff Roberson void
186535e6168fSJeff Roberson sched_wakeup(struct thread *td)
186635e6168fSJeff Roberson {
186714618990SJeff Roberson 	struct td_sched *ts;
1868ae7a6b38SJeff Roberson 	int slptick;
1869e7d50326SJeff Roberson 
18707b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
187114618990SJeff Roberson 	ts = td->td_sched;
1872c5aa6b58SJeff Roberson 	td->td_flags &= ~TDF_CANSWAP;
187335e6168fSJeff Roberson 	/*
1874e7d50326SJeff Roberson 	 * If we slept for more than a tick update our interactivity and
1875e7d50326SJeff Roberson 	 * priority.
187635e6168fSJeff Roberson 	 */
187754b0e65fSJeff Roberson 	slptick = td->td_slptick;
187854b0e65fSJeff Roberson 	td->td_slptick = 0;
1879ae7a6b38SJeff Roberson 	if (slptick && slptick != ticks) {
18809a93305aSJeff Roberson 		u_int hzticks;
1881f1e8dc4aSJeff Roberson 
1882ae7a6b38SJeff Roberson 		hzticks = (ticks - slptick) << SCHED_TICK_SHIFT;
1883ae7a6b38SJeff Roberson 		ts->ts_slptime += hzticks;
18848460a577SJohn Birrell 		sched_interact_update(td);
188514618990SJeff Roberson 		sched_pctcpu_update(ts);
1886f1e8dc4aSJeff Roberson 	}
188714618990SJeff Roberson 	/* Reset the slice value after we sleep. */
188814618990SJeff Roberson 	ts->ts_slice = sched_slice;
18897a5e5e2aSJeff Roberson 	sched_add(td, SRQ_BORING);
189035e6168fSJeff Roberson }
189135e6168fSJeff Roberson 
189235e6168fSJeff Roberson /*
189335e6168fSJeff Roberson  * Penalize the parent for creating a new child and initialize the child's
189435e6168fSJeff Roberson  * priority.
189535e6168fSJeff Roberson  */
189635e6168fSJeff Roberson void
18978460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child)
189815dc847eSJeff Roberson {
18997b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1900ad1e7d28SJulian Elischer 	sched_fork_thread(td, child);
1901e7d50326SJeff Roberson 	/*
1902e7d50326SJeff Roberson 	 * Penalize the parent and child for forking.
1903e7d50326SJeff Roberson 	 */
1904e7d50326SJeff Roberson 	sched_interact_fork(child);
1905e7d50326SJeff Roberson 	sched_priority(child);
1906ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += tickincr;
1907e7d50326SJeff Roberson 	sched_interact_update(td);
1908e7d50326SJeff Roberson 	sched_priority(td);
1909ad1e7d28SJulian Elischer }
1910ad1e7d28SJulian Elischer 
1911ae7a6b38SJeff Roberson /*
1912ae7a6b38SJeff Roberson  * Fork a new thread, may be within the same process.
1913ae7a6b38SJeff Roberson  */
1914ad1e7d28SJulian Elischer void
1915ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child)
1916ad1e7d28SJulian Elischer {
1917ad1e7d28SJulian Elischer 	struct td_sched *ts;
1918ad1e7d28SJulian Elischer 	struct td_sched *ts2;
19198460a577SJohn Birrell 
19208b16c208SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1921e7d50326SJeff Roberson 	/*
1922e7d50326SJeff Roberson 	 * Initialize child.
1923e7d50326SJeff Roberson 	 */
1924ad1e7d28SJulian Elischer 	ts = td->td_sched;
1925ad1e7d28SJulian Elischer 	ts2 = child->td_sched;
19268b16c208SJeff Roberson 	child->td_lock = TDQ_LOCKPTR(TDQ_SELF());
19278b16c208SJeff Roberson 	child->td_cpuset = cpuset_ref(td->td_cpuset);
1928ad1e7d28SJulian Elischer 	ts2->ts_cpu = ts->ts_cpu;
19298b16c208SJeff Roberson 	ts2->ts_flags = 0;
1930e7d50326SJeff Roberson 	/*
1931e7d50326SJeff Roberson 	 * Grab our parents cpu estimation information and priority.
1932e7d50326SJeff Roberson 	 */
1933ad1e7d28SJulian Elischer 	ts2->ts_ticks = ts->ts_ticks;
1934ad1e7d28SJulian Elischer 	ts2->ts_ltick = ts->ts_ltick;
1935ad1e7d28SJulian Elischer 	ts2->ts_ftick = ts->ts_ftick;
1936e7d50326SJeff Roberson 	child->td_user_pri = td->td_user_pri;
1937e7d50326SJeff Roberson 	child->td_base_user_pri = td->td_base_user_pri;
1938e7d50326SJeff Roberson 	/*
1939e7d50326SJeff Roberson 	 * And update interactivity score.
1940e7d50326SJeff Roberson 	 */
1941ae7a6b38SJeff Roberson 	ts2->ts_slptime = ts->ts_slptime;
1942ae7a6b38SJeff Roberson 	ts2->ts_runtime = ts->ts_runtime;
1943e7d50326SJeff Roberson 	ts2->ts_slice = 1;	/* Attempt to quickly learn interactivity. */
194415dc847eSJeff Roberson }
194515dc847eSJeff Roberson 
1946ae7a6b38SJeff Roberson /*
1947ae7a6b38SJeff Roberson  * Adjust the priority class of a thread.
1948ae7a6b38SJeff Roberson  */
194915dc847eSJeff Roberson void
19508460a577SJohn Birrell sched_class(struct thread *td, int class)
195115dc847eSJeff Roberson {
195215dc847eSJeff Roberson 
19537b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
19548460a577SJohn Birrell 	if (td->td_pri_class == class)
195515dc847eSJeff Roberson 		return;
19568460a577SJohn Birrell 	td->td_pri_class = class;
195735e6168fSJeff Roberson }
195835e6168fSJeff Roberson 
195935e6168fSJeff Roberson /*
196035e6168fSJeff Roberson  * Return some of the child's priority and interactivity to the parent.
196135e6168fSJeff Roberson  */
196235e6168fSJeff Roberson void
1963fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child)
196435e6168fSJeff Roberson {
1965e7d50326SJeff Roberson 	struct thread *td;
1966141ad61cSJeff Roberson 
19678460a577SJohn Birrell 	CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d",
1968431f8906SJulian Elischer 	    child, child->td_name, child->td_priority);
19698460a577SJohn Birrell 
1970374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
1971e7d50326SJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
1972e7d50326SJeff Roberson 	sched_exit_thread(td, child);
1973ad1e7d28SJulian Elischer }
1974ad1e7d28SJulian Elischer 
1975ae7a6b38SJeff Roberson /*
1976ae7a6b38SJeff Roberson  * Penalize another thread for the time spent on this one.  This helps to
1977ae7a6b38SJeff Roberson  * worsen the priority and interactivity of processes which schedule batch
1978ae7a6b38SJeff Roberson  * jobs such as make.  This has little effect on the make process itself but
1979ae7a6b38SJeff Roberson  * causes new processes spawned by it to receive worse scores immediately.
1980ae7a6b38SJeff Roberson  */
1981ad1e7d28SJulian Elischer void
1982fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child)
1983ad1e7d28SJulian Elischer {
1984fc6c30f6SJulian Elischer 
1985e7d50326SJeff Roberson 	CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d",
1986431f8906SJulian Elischer 	    child, child->td_name, child->td_priority);
1987e7d50326SJeff Roberson 
1988e7d50326SJeff Roberson 	/*
1989e7d50326SJeff Roberson 	 * Give the child's runtime to the parent without returning the
1990e7d50326SJeff Roberson 	 * sleep time as a penalty to the parent.  This causes shells that
1991e7d50326SJeff Roberson 	 * launch expensive things to mark their children as expensive.
1992e7d50326SJeff Roberson 	 */
19937b20fb19SJeff Roberson 	thread_lock(td);
1994ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += child->td_sched->ts_runtime;
1995fc6c30f6SJulian Elischer 	sched_interact_update(td);
1996e7d50326SJeff Roberson 	sched_priority(td);
19977b20fb19SJeff Roberson 	thread_unlock(td);
1998ad1e7d28SJulian Elischer }
1999ad1e7d28SJulian Elischer 
2000ff256d9cSJeff Roberson void
2001ff256d9cSJeff Roberson sched_preempt(struct thread *td)
2002ff256d9cSJeff Roberson {
2003ff256d9cSJeff Roberson 	struct tdq *tdq;
2004ff256d9cSJeff Roberson 
2005ff256d9cSJeff Roberson 	thread_lock(td);
2006ff256d9cSJeff Roberson 	tdq = TDQ_SELF();
2007ff256d9cSJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2008ff256d9cSJeff Roberson 	tdq->tdq_ipipending = 0;
2009ff256d9cSJeff Roberson 	if (td->td_priority > tdq->tdq_lowpri) {
20108df78c41SJeff Roberson 		int flags;
20118df78c41SJeff Roberson 
20128df78c41SJeff Roberson 		flags = SW_INVOL | SW_PREEMPT;
2013ff256d9cSJeff Roberson 		if (td->td_critnest > 1)
2014ff256d9cSJeff Roberson 			td->td_owepreempt = 1;
20158df78c41SJeff Roberson 		else if (TD_IS_IDLETHREAD(td))
20168df78c41SJeff Roberson 			mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL);
2017ff256d9cSJeff Roberson 		else
20188df78c41SJeff Roberson 			mi_switch(flags | SWT_REMOTEPREEMPT, NULL);
2019ff256d9cSJeff Roberson 	}
2020ff256d9cSJeff Roberson 	thread_unlock(td);
2021ff256d9cSJeff Roberson }
2022ff256d9cSJeff Roberson 
2023ae7a6b38SJeff Roberson /*
2024ae7a6b38SJeff Roberson  * Fix priorities on return to user-space.  Priorities may be elevated due
2025ae7a6b38SJeff Roberson  * to static priorities in msleep() or similar.
2026ae7a6b38SJeff Roberson  */
2027ad1e7d28SJulian Elischer void
2028ad1e7d28SJulian Elischer sched_userret(struct thread *td)
2029ad1e7d28SJulian Elischer {
2030ad1e7d28SJulian Elischer 	/*
2031ad1e7d28SJulian Elischer 	 * XXX we cheat slightly on the locking here to avoid locking in
2032ad1e7d28SJulian Elischer 	 * the usual case.  Setting td_priority here is essentially an
2033ad1e7d28SJulian Elischer 	 * incomplete workaround for not setting it properly elsewhere.
2034ad1e7d28SJulian Elischer 	 * Now that some interrupt handlers are threads, not setting it
2035ad1e7d28SJulian Elischer 	 * properly elsewhere can clobber it in the window between setting
2036ad1e7d28SJulian Elischer 	 * it here and returning to user mode, so don't waste time setting
2037ad1e7d28SJulian Elischer 	 * it perfectly here.
2038ad1e7d28SJulian Elischer 	 */
2039ad1e7d28SJulian Elischer 	KASSERT((td->td_flags & TDF_BORROWING) == 0,
2040ad1e7d28SJulian Elischer 	    ("thread with borrowed priority returning to userland"));
2041ad1e7d28SJulian Elischer 	if (td->td_priority != td->td_user_pri) {
20427b20fb19SJeff Roberson 		thread_lock(td);
2043ad1e7d28SJulian Elischer 		td->td_priority = td->td_user_pri;
2044ad1e7d28SJulian Elischer 		td->td_base_pri = td->td_user_pri;
204562fa74d9SJeff Roberson 		tdq_setlowpri(TDQ_SELF(), td);
20467b20fb19SJeff Roberson 		thread_unlock(td);
2047ad1e7d28SJulian Elischer         }
204835e6168fSJeff Roberson }
204935e6168fSJeff Roberson 
2050ae7a6b38SJeff Roberson /*
2051ae7a6b38SJeff Roberson  * Handle a stathz tick.  This is really only relevant for timeshare
2052ae7a6b38SJeff Roberson  * threads.
2053ae7a6b38SJeff Roberson  */
205435e6168fSJeff Roberson void
20557cf90fb3SJeff Roberson sched_clock(struct thread *td)
205635e6168fSJeff Roberson {
2057ad1e7d28SJulian Elischer 	struct tdq *tdq;
2058ad1e7d28SJulian Elischer 	struct td_sched *ts;
205935e6168fSJeff Roberson 
2060ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
20613f872f85SJeff Roberson 	tdq = TDQ_SELF();
20627fcf154aSJeff Roberson #ifdef SMP
20637fcf154aSJeff Roberson 	/*
20647fcf154aSJeff Roberson 	 * We run the long term load balancer infrequently on the first cpu.
20657fcf154aSJeff Roberson 	 */
20667fcf154aSJeff Roberson 	if (balance_tdq == tdq) {
20677fcf154aSJeff Roberson 		if (balance_ticks && --balance_ticks == 0)
20687fcf154aSJeff Roberson 			sched_balance();
20697fcf154aSJeff Roberson 	}
20707fcf154aSJeff Roberson #endif
20713f872f85SJeff Roberson 	/*
20723f872f85SJeff Roberson 	 * Advance the insert index once for each tick to ensure that all
20733f872f85SJeff Roberson 	 * threads get a chance to run.
20743f872f85SJeff Roberson 	 */
20753f872f85SJeff Roberson 	if (tdq->tdq_idx == tdq->tdq_ridx) {
20763f872f85SJeff Roberson 		tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
20773f872f85SJeff Roberson 		if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
20783f872f85SJeff Roberson 			tdq->tdq_ridx = tdq->tdq_idx;
20793f872f85SJeff Roberson 	}
20803f872f85SJeff Roberson 	ts = td->td_sched;
2081fd0b8c78SJeff Roberson 	if (td->td_pri_class & PRI_FIFO_BIT)
2082a8949de2SJeff Roberson 		return;
2083fd0b8c78SJeff Roberson 	if (td->td_pri_class == PRI_TIMESHARE) {
2084a8949de2SJeff Roberson 		/*
2085fd0b8c78SJeff Roberson 		 * We used a tick; charge it to the thread so
2086fd0b8c78SJeff Roberson 		 * that we can compute our interactivity.
208715dc847eSJeff Roberson 		 */
2088ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime += tickincr;
20898460a577SJohn Birrell 		sched_interact_update(td);
209073daf66fSJeff Roberson 		sched_priority(td);
2091fd0b8c78SJeff Roberson 	}
209235e6168fSJeff Roberson 	/*
209335e6168fSJeff Roberson 	 * We used up one time slice.
209435e6168fSJeff Roberson 	 */
2095ad1e7d28SJulian Elischer 	if (--ts->ts_slice > 0)
209615dc847eSJeff Roberson 		return;
209735e6168fSJeff Roberson 	/*
209873daf66fSJeff Roberson 	 * We're out of time, force a requeue at userret().
209935e6168fSJeff Roberson 	 */
210073daf66fSJeff Roberson 	ts->ts_slice = sched_slice;
21014a338afdSJulian Elischer 	td->td_flags |= TDF_NEEDRESCHED;
210235e6168fSJeff Roberson }
210335e6168fSJeff Roberson 
2104ae7a6b38SJeff Roberson /*
2105ae7a6b38SJeff Roberson  * Called once per hz tick.  Used for cpu utilization information.  This
2106ae7a6b38SJeff Roberson  * is easier than trying to scale based on stathz.
2107ae7a6b38SJeff Roberson  */
2108ae7a6b38SJeff Roberson void
2109ae7a6b38SJeff Roberson sched_tick(void)
2110ae7a6b38SJeff Roberson {
2111ae7a6b38SJeff Roberson 	struct td_sched *ts;
2112ae7a6b38SJeff Roberson 
2113ae7a6b38SJeff Roberson 	ts = curthread->td_sched;
2114ae7a6b38SJeff Roberson 	/* Adjust ticks for pctcpu */
2115ae7a6b38SJeff Roberson 	ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
2116ae7a6b38SJeff Roberson 	ts->ts_ltick = ticks;
2117ae7a6b38SJeff Roberson 	/*
2118ae7a6b38SJeff Roberson 	 * Update if we've exceeded our desired tick threshhold by over one
2119ae7a6b38SJeff Roberson 	 * second.
2120ae7a6b38SJeff Roberson 	 */
2121ae7a6b38SJeff Roberson 	if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
2122ae7a6b38SJeff Roberson 		sched_pctcpu_update(ts);
2123ae7a6b38SJeff Roberson }
2124ae7a6b38SJeff Roberson 
2125ae7a6b38SJeff Roberson /*
2126ae7a6b38SJeff Roberson  * Return whether the current CPU has runnable tasks.  Used for in-kernel
2127ae7a6b38SJeff Roberson  * cooperative idle threads.
2128ae7a6b38SJeff Roberson  */
212935e6168fSJeff Roberson int
213035e6168fSJeff Roberson sched_runnable(void)
213135e6168fSJeff Roberson {
2132ad1e7d28SJulian Elischer 	struct tdq *tdq;
2133b90816f1SJeff Roberson 	int load;
213435e6168fSJeff Roberson 
2135b90816f1SJeff Roberson 	load = 1;
2136b90816f1SJeff Roberson 
2137ad1e7d28SJulian Elischer 	tdq = TDQ_SELF();
21383f741ca1SJeff Roberson 	if ((curthread->td_flags & TDF_IDLETD) != 0) {
2139d2ad694cSJeff Roberson 		if (tdq->tdq_load > 0)
21403f741ca1SJeff Roberson 			goto out;
21413f741ca1SJeff Roberson 	} else
2142d2ad694cSJeff Roberson 		if (tdq->tdq_load - 1 > 0)
2143b90816f1SJeff Roberson 			goto out;
2144b90816f1SJeff Roberson 	load = 0;
2145b90816f1SJeff Roberson out:
2146b90816f1SJeff Roberson 	return (load);
214735e6168fSJeff Roberson }
214835e6168fSJeff Roberson 
2149ae7a6b38SJeff Roberson /*
2150ae7a6b38SJeff Roberson  * Choose the highest priority thread to run.  The thread is removed from
2151ae7a6b38SJeff Roberson  * the run-queue while running however the load remains.  For SMP we set
2152ae7a6b38SJeff Roberson  * the tdq in the global idle bitmask if it idles here.
2153ae7a6b38SJeff Roberson  */
21547a5e5e2aSJeff Roberson struct thread *
2155c9f25d8fSJeff Roberson sched_choose(void)
2156c9f25d8fSJeff Roberson {
21579727e637SJeff Roberson 	struct thread *td;
2158ae7a6b38SJeff Roberson 	struct tdq *tdq;
2159ae7a6b38SJeff Roberson 
2160ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2161ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
21629727e637SJeff Roberson 	td = tdq_choose(tdq);
21639727e637SJeff Roberson 	if (td) {
21649727e637SJeff Roberson 		td->td_sched->ts_ltick = ticks;
21659727e637SJeff Roberson 		tdq_runq_rem(tdq, td);
21660502fe2eSJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
21679727e637SJeff Roberson 		return (td);
216835e6168fSJeff Roberson 	}
21690502fe2eSJeff Roberson 	tdq->tdq_lowpri = PRI_MAX_IDLE;
217062fa74d9SJeff Roberson 	return (PCPU_GET(idlethread));
21717a5e5e2aSJeff Roberson }
21727a5e5e2aSJeff Roberson 
2173ae7a6b38SJeff Roberson /*
2174ae7a6b38SJeff Roberson  * Set owepreempt if necessary.  Preemption never happens directly in ULE,
2175ae7a6b38SJeff Roberson  * we always request it once we exit a critical section.
2176ae7a6b38SJeff Roberson  */
2177ae7a6b38SJeff Roberson static inline void
2178ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td)
21797a5e5e2aSJeff Roberson {
21807a5e5e2aSJeff Roberson 	struct thread *ctd;
21817a5e5e2aSJeff Roberson 	int cpri;
21827a5e5e2aSJeff Roberson 	int pri;
21837a5e5e2aSJeff Roberson 
2184ff256d9cSJeff Roberson 	THREAD_LOCK_ASSERT(curthread, MA_OWNED);
2185ff256d9cSJeff Roberson 
21867a5e5e2aSJeff Roberson 	ctd = curthread;
21877a5e5e2aSJeff Roberson 	pri = td->td_priority;
21887a5e5e2aSJeff Roberson 	cpri = ctd->td_priority;
2189ff256d9cSJeff Roberson 	if (pri < cpri)
2190ff256d9cSJeff Roberson 		ctd->td_flags |= TDF_NEEDRESCHED;
21917a5e5e2aSJeff Roberson 	if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
2192ae7a6b38SJeff Roberson 		return;
2193ff256d9cSJeff Roberson 	if (!sched_shouldpreempt(pri, cpri, 0))
2194ae7a6b38SJeff Roberson 		return;
21957a5e5e2aSJeff Roberson 	ctd->td_owepreempt = 1;
219635e6168fSJeff Roberson }
219735e6168fSJeff Roberson 
2198ae7a6b38SJeff Roberson /*
219973daf66fSJeff Roberson  * Add a thread to a thread queue.  Select the appropriate runq and add the
220073daf66fSJeff Roberson  * thread to it.  This is the internal function called when the tdq is
220173daf66fSJeff Roberson  * predetermined.
2202ae7a6b38SJeff Roberson  */
220335e6168fSJeff Roberson void
2204ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags)
220535e6168fSJeff Roberson {
2206c9f25d8fSJeff Roberson 
2207ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
22087a5e5e2aSJeff Roberson 	KASSERT((td->td_inhibitors == 0),
22097a5e5e2aSJeff Roberson 	    ("sched_add: trying to run inhibited thread"));
22107a5e5e2aSJeff Roberson 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
22117a5e5e2aSJeff Roberson 	    ("sched_add: bad thread state"));
2212b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
2213b61ce5b0SJeff Roberson 	    ("sched_add: thread swapped out"));
2214ae7a6b38SJeff Roberson 
2215ae7a6b38SJeff Roberson 	if (td->td_priority < tdq->tdq_lowpri)
2216ae7a6b38SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
22179727e637SJeff Roberson 	tdq_runq_add(tdq, td, flags);
22189727e637SJeff Roberson 	tdq_load_add(tdq, td);
2219ae7a6b38SJeff Roberson }
2220ae7a6b38SJeff Roberson 
2221ae7a6b38SJeff Roberson /*
2222ae7a6b38SJeff Roberson  * Select the target thread queue and add a thread to it.  Request
2223ae7a6b38SJeff Roberson  * preemption or IPI a remote processor if required.
2224ae7a6b38SJeff Roberson  */
2225ae7a6b38SJeff Roberson void
2226ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags)
2227ae7a6b38SJeff Roberson {
2228ae7a6b38SJeff Roberson 	struct tdq *tdq;
22297b8bfa0dSJeff Roberson #ifdef SMP
2230ae7a6b38SJeff Roberson 	int cpu;
2231ae7a6b38SJeff Roberson #endif
2232ae7a6b38SJeff Roberson 	CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
2233431f8906SJulian Elischer 	    td, td->td_name, td->td_priority, curthread,
2234431f8906SJulian Elischer 	    curthread->td_name);
2235ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2236ae7a6b38SJeff Roberson 	/*
2237ae7a6b38SJeff Roberson 	 * Recalculate the priority before we select the target cpu or
2238ae7a6b38SJeff Roberson 	 * run-queue.
2239ae7a6b38SJeff Roberson 	 */
2240ae7a6b38SJeff Roberson 	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
2241ae7a6b38SJeff Roberson 		sched_priority(td);
2242ae7a6b38SJeff Roberson #ifdef SMP
2243ae7a6b38SJeff Roberson 	/*
2244ae7a6b38SJeff Roberson 	 * Pick the destination cpu and if it isn't ours transfer to the
2245ae7a6b38SJeff Roberson 	 * target cpu.
2246ae7a6b38SJeff Roberson 	 */
22479727e637SJeff Roberson 	cpu = sched_pickcpu(td, flags);
22489727e637SJeff Roberson 	tdq = sched_setcpu(td, cpu, flags);
2249ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
225073daf66fSJeff Roberson 	if (cpu != PCPU_GET(cpuid)) {
22519727e637SJeff Roberson 		tdq_notify(tdq, td);
22527b8bfa0dSJeff Roberson 		return;
22537b8bfa0dSJeff Roberson 	}
2254ae7a6b38SJeff Roberson #else
2255ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2256ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
2257ae7a6b38SJeff Roberson 	/*
2258ae7a6b38SJeff Roberson 	 * Now that the thread is moving to the run-queue, set the lock
2259ae7a6b38SJeff Roberson 	 * to the scheduler's lock.
2260ae7a6b38SJeff Roberson 	 */
2261ae7a6b38SJeff Roberson 	thread_lock_set(td, TDQ_LOCKPTR(tdq));
2262ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
22637b8bfa0dSJeff Roberson #endif
2264ae7a6b38SJeff Roberson 	if (!(flags & SRQ_YIELDING))
2265ae7a6b38SJeff Roberson 		sched_setpreempt(td);
226635e6168fSJeff Roberson }
226735e6168fSJeff Roberson 
2268ae7a6b38SJeff Roberson /*
2269ae7a6b38SJeff Roberson  * Remove a thread from a run-queue without running it.  This is used
2270ae7a6b38SJeff Roberson  * when we're stealing a thread from a remote queue.  Otherwise all threads
2271ae7a6b38SJeff Roberson  * exit by calling sched_exit_thread() and sched_throw() themselves.
2272ae7a6b38SJeff Roberson  */
227335e6168fSJeff Roberson void
22747cf90fb3SJeff Roberson sched_rem(struct thread *td)
227535e6168fSJeff Roberson {
2276ad1e7d28SJulian Elischer 	struct tdq *tdq;
22777cf90fb3SJeff Roberson 
227881d47d3fSJeff Roberson 	CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)",
2279431f8906SJulian Elischer 	    td, td->td_name, td->td_priority, curthread,
2280431f8906SJulian Elischer 	    curthread->td_name);
22819727e637SJeff Roberson 	tdq = TDQ_CPU(td->td_sched->ts_cpu);
2282ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2283ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
22847a5e5e2aSJeff Roberson 	KASSERT(TD_ON_RUNQ(td),
2285ad1e7d28SJulian Elischer 	    ("sched_rem: thread not on run queue"));
22869727e637SJeff Roberson 	tdq_runq_rem(tdq, td);
22879727e637SJeff Roberson 	tdq_load_rem(tdq, td);
22887a5e5e2aSJeff Roberson 	TD_SET_CAN_RUN(td);
228962fa74d9SJeff Roberson 	if (td->td_priority == tdq->tdq_lowpri)
229062fa74d9SJeff Roberson 		tdq_setlowpri(tdq, NULL);
229135e6168fSJeff Roberson }
229235e6168fSJeff Roberson 
2293ae7a6b38SJeff Roberson /*
2294ae7a6b38SJeff Roberson  * Fetch cpu utilization information.  Updates on demand.
2295ae7a6b38SJeff Roberson  */
229635e6168fSJeff Roberson fixpt_t
22977cf90fb3SJeff Roberson sched_pctcpu(struct thread *td)
229835e6168fSJeff Roberson {
229935e6168fSJeff Roberson 	fixpt_t pctcpu;
2300ad1e7d28SJulian Elischer 	struct td_sched *ts;
230135e6168fSJeff Roberson 
230235e6168fSJeff Roberson 	pctcpu = 0;
2303ad1e7d28SJulian Elischer 	ts = td->td_sched;
2304ad1e7d28SJulian Elischer 	if (ts == NULL)
2305484288deSJeff Roberson 		return (0);
230635e6168fSJeff Roberson 
23077b20fb19SJeff Roberson 	thread_lock(td);
2308ad1e7d28SJulian Elischer 	if (ts->ts_ticks) {
230935e6168fSJeff Roberson 		int rtick;
231035e6168fSJeff Roberson 
2311ad1e7d28SJulian Elischer 		sched_pctcpu_update(ts);
231235e6168fSJeff Roberson 		/* How many rtick per second ? */
2313e7d50326SJeff Roberson 		rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
2314e7d50326SJeff Roberson 		pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
231535e6168fSJeff Roberson 	}
23167b20fb19SJeff Roberson 	thread_unlock(td);
231735e6168fSJeff Roberson 
231835e6168fSJeff Roberson 	return (pctcpu);
231935e6168fSJeff Roberson }
232035e6168fSJeff Roberson 
232162fa74d9SJeff Roberson /*
232262fa74d9SJeff Roberson  * Enforce affinity settings for a thread.  Called after adjustments to
232362fa74d9SJeff Roberson  * cpumask.
232462fa74d9SJeff Roberson  */
2325885d51a3SJeff Roberson void
2326885d51a3SJeff Roberson sched_affinity(struct thread *td)
2327885d51a3SJeff Roberson {
232862fa74d9SJeff Roberson #ifdef SMP
232962fa74d9SJeff Roberson 	struct td_sched *ts;
233062fa74d9SJeff Roberson 	int cpu;
233162fa74d9SJeff Roberson 
233262fa74d9SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
233362fa74d9SJeff Roberson 	ts = td->td_sched;
233462fa74d9SJeff Roberson 	if (THREAD_CAN_SCHED(td, ts->ts_cpu))
233562fa74d9SJeff Roberson 		return;
233662fa74d9SJeff Roberson 	if (!TD_IS_RUNNING(td))
233762fa74d9SJeff Roberson 		return;
233862fa74d9SJeff Roberson 	td->td_flags |= TDF_NEEDRESCHED;
233962fa74d9SJeff Roberson 	if (!THREAD_CAN_MIGRATE(td))
234062fa74d9SJeff Roberson 		return;
234162fa74d9SJeff Roberson 	/*
234262fa74d9SJeff Roberson 	 * Assign the new cpu and force a switch before returning to
234362fa74d9SJeff Roberson 	 * userspace.  If the target thread is not running locally send
234462fa74d9SJeff Roberson 	 * an ipi to force the issue.
234562fa74d9SJeff Roberson 	 */
234662fa74d9SJeff Roberson 	cpu = ts->ts_cpu;
23479727e637SJeff Roberson 	ts->ts_cpu = sched_pickcpu(td, 0);
234862fa74d9SJeff Roberson 	if (cpu != PCPU_GET(cpuid))
234962fa74d9SJeff Roberson 		ipi_selected(1 << cpu, IPI_PREEMPT);
235062fa74d9SJeff Roberson #endif
2351885d51a3SJeff Roberson }
2352885d51a3SJeff Roberson 
2353ae7a6b38SJeff Roberson /*
2354ae7a6b38SJeff Roberson  * Bind a thread to a target cpu.
2355ae7a6b38SJeff Roberson  */
23569bacd788SJeff Roberson void
23579bacd788SJeff Roberson sched_bind(struct thread *td, int cpu)
23589bacd788SJeff Roberson {
2359ad1e7d28SJulian Elischer 	struct td_sched *ts;
23609bacd788SJeff Roberson 
2361c47f202bSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
2362ad1e7d28SJulian Elischer 	ts = td->td_sched;
23636b2f763fSJeff Roberson 	if (ts->ts_flags & TSF_BOUND)
2364c95d2db2SJeff Roberson 		sched_unbind(td);
2365ad1e7d28SJulian Elischer 	ts->ts_flags |= TSF_BOUND;
23666b2f763fSJeff Roberson 	sched_pin();
236780f86c9fSJeff Roberson 	if (PCPU_GET(cpuid) == cpu)
23689bacd788SJeff Roberson 		return;
23696b2f763fSJeff Roberson 	ts->ts_cpu = cpu;
23709bacd788SJeff Roberson 	/* When we return from mi_switch we'll be on the correct cpu. */
2371279f949eSPoul-Henning Kamp 	mi_switch(SW_VOL, NULL);
23729bacd788SJeff Roberson }
23739bacd788SJeff Roberson 
2374ae7a6b38SJeff Roberson /*
2375ae7a6b38SJeff Roberson  * Release a bound thread.
2376ae7a6b38SJeff Roberson  */
23779bacd788SJeff Roberson void
23789bacd788SJeff Roberson sched_unbind(struct thread *td)
23799bacd788SJeff Roberson {
2380e7d50326SJeff Roberson 	struct td_sched *ts;
2381e7d50326SJeff Roberson 
23827b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2383e7d50326SJeff Roberson 	ts = td->td_sched;
23846b2f763fSJeff Roberson 	if ((ts->ts_flags & TSF_BOUND) == 0)
23856b2f763fSJeff Roberson 		return;
2386e7d50326SJeff Roberson 	ts->ts_flags &= ~TSF_BOUND;
2387e7d50326SJeff Roberson 	sched_unpin();
23889bacd788SJeff Roberson }
23899bacd788SJeff Roberson 
239035e6168fSJeff Roberson int
2391ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td)
2392ebccf1e3SJoseph Koshy {
23937b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2394ad1e7d28SJulian Elischer 	return (td->td_sched->ts_flags & TSF_BOUND);
2395ebccf1e3SJoseph Koshy }
2396ebccf1e3SJoseph Koshy 
2397ae7a6b38SJeff Roberson /*
2398ae7a6b38SJeff Roberson  * Basic yield call.
2399ae7a6b38SJeff Roberson  */
240036ec198bSDavid Xu void
240136ec198bSDavid Xu sched_relinquish(struct thread *td)
240236ec198bSDavid Xu {
24037b20fb19SJeff Roberson 	thread_lock(td);
24048df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
24057b20fb19SJeff Roberson 	thread_unlock(td);
240636ec198bSDavid Xu }
240736ec198bSDavid Xu 
2408ae7a6b38SJeff Roberson /*
2409ae7a6b38SJeff Roberson  * Return the total system load.
2410ae7a6b38SJeff Roberson  */
2411ebccf1e3SJoseph Koshy int
241233916c36SJeff Roberson sched_load(void)
241333916c36SJeff Roberson {
241433916c36SJeff Roberson #ifdef SMP
241533916c36SJeff Roberson 	int total;
241633916c36SJeff Roberson 	int i;
241733916c36SJeff Roberson 
241833916c36SJeff Roberson 	total = 0;
241962fa74d9SJeff Roberson 	for (i = 0; i <= mp_maxid; i++)
242062fa74d9SJeff Roberson 		total += TDQ_CPU(i)->tdq_sysload;
242133916c36SJeff Roberson 	return (total);
242233916c36SJeff Roberson #else
2423d2ad694cSJeff Roberson 	return (TDQ_SELF()->tdq_sysload);
242433916c36SJeff Roberson #endif
242533916c36SJeff Roberson }
242633916c36SJeff Roberson 
242733916c36SJeff Roberson int
242835e6168fSJeff Roberson sched_sizeof_proc(void)
242935e6168fSJeff Roberson {
243035e6168fSJeff Roberson 	return (sizeof(struct proc));
243135e6168fSJeff Roberson }
243235e6168fSJeff Roberson 
243335e6168fSJeff Roberson int
243435e6168fSJeff Roberson sched_sizeof_thread(void)
243535e6168fSJeff Roberson {
243635e6168fSJeff Roberson 	return (sizeof(struct thread) + sizeof(struct td_sched));
243735e6168fSJeff Roberson }
2438b41f1452SDavid Xu 
24397a5e5e2aSJeff Roberson /*
24407a5e5e2aSJeff Roberson  * The actual idle process.
24417a5e5e2aSJeff Roberson  */
24427a5e5e2aSJeff Roberson void
24437a5e5e2aSJeff Roberson sched_idletd(void *dummy)
24447a5e5e2aSJeff Roberson {
24457a5e5e2aSJeff Roberson 	struct thread *td;
2446ae7a6b38SJeff Roberson 	struct tdq *tdq;
24477a5e5e2aSJeff Roberson 
24487a5e5e2aSJeff Roberson 	td = curthread;
2449ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
24507a5e5e2aSJeff Roberson 	mtx_assert(&Giant, MA_NOTOWNED);
2451ae7a6b38SJeff Roberson 	/* ULE relies on preemption for idle interruption. */
2452ae7a6b38SJeff Roberson 	for (;;) {
2453ae7a6b38SJeff Roberson #ifdef SMP
2454ae7a6b38SJeff Roberson 		if (tdq_idled(tdq))
24557a5e5e2aSJeff Roberson 			cpu_idle();
2456ae7a6b38SJeff Roberson #else
2457ae7a6b38SJeff Roberson 		cpu_idle();
2458ae7a6b38SJeff Roberson #endif
2459ae7a6b38SJeff Roberson 	}
2460b41f1452SDavid Xu }
2461e7d50326SJeff Roberson 
24627b20fb19SJeff Roberson /*
24637b20fb19SJeff Roberson  * A CPU is entering for the first time or a thread is exiting.
24647b20fb19SJeff Roberson  */
24657b20fb19SJeff Roberson void
24667b20fb19SJeff Roberson sched_throw(struct thread *td)
24677b20fb19SJeff Roberson {
246859c68134SJeff Roberson 	struct thread *newtd;
2469ae7a6b38SJeff Roberson 	struct tdq *tdq;
2470ae7a6b38SJeff Roberson 
2471ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
24727b20fb19SJeff Roberson 	if (td == NULL) {
2473ae7a6b38SJeff Roberson 		/* Correct spinlock nesting and acquire the correct lock. */
2474ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
24757b20fb19SJeff Roberson 		spinlock_exit();
24767b20fb19SJeff Roberson 	} else {
2477ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
24789727e637SJeff Roberson 		tdq_load_rem(tdq, td);
2479eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
24807b20fb19SJeff Roberson 	}
24817b20fb19SJeff Roberson 	KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
248259c68134SJeff Roberson 	newtd = choosethread();
248359c68134SJeff Roberson 	TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
24847b20fb19SJeff Roberson 	PCPU_SET(switchtime, cpu_ticks());
24857b20fb19SJeff Roberson 	PCPU_SET(switchticks, ticks);
248659c68134SJeff Roberson 	cpu_throw(td, newtd);		/* doesn't return */
24877b20fb19SJeff Roberson }
24887b20fb19SJeff Roberson 
2489ae7a6b38SJeff Roberson /*
2490ae7a6b38SJeff Roberson  * This is called from fork_exit().  Just acquire the correct locks and
2491ae7a6b38SJeff Roberson  * let fork do the rest of the work.
2492ae7a6b38SJeff Roberson  */
24937b20fb19SJeff Roberson void
2494fe54587fSJeff Roberson sched_fork_exit(struct thread *td)
24957b20fb19SJeff Roberson {
2496ae7a6b38SJeff Roberson 	struct td_sched *ts;
2497ae7a6b38SJeff Roberson 	struct tdq *tdq;
2498ae7a6b38SJeff Roberson 	int cpuid;
24997b20fb19SJeff Roberson 
25007b20fb19SJeff Roberson 	/*
25017b20fb19SJeff Roberson 	 * Finish setting up thread glue so that it begins execution in a
2502ae7a6b38SJeff Roberson 	 * non-nested critical section with the scheduler lock held.
25037b20fb19SJeff Roberson 	 */
2504ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
2505ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
2506ae7a6b38SJeff Roberson 	ts = td->td_sched;
2507ae7a6b38SJeff Roberson 	if (TD_IS_IDLETHREAD(td))
2508ae7a6b38SJeff Roberson 		td->td_lock = TDQ_LOCKPTR(tdq);
2509ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2510ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
251159c68134SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
2512eea4f254SJeff Roberson 	lock_profile_obtain_lock_success(
2513eea4f254SJeff Roberson 	    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
25147b20fb19SJeff Roberson }
25157b20fb19SJeff Roberson 
25169727e637SJeff Roberson SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler");
2517ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0,
2518e7d50326SJeff Roberson     "Scheduler name");
2519ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
2520ae7a6b38SJeff Roberson     "Slice size for timeshare threads");
2521ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
2522ae7a6b38SJeff Roberson      "Interactivity score threshold");
2523ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh,
2524ae7a6b38SJeff Roberson      0,"Min priority for preemption, lower priorities have greater precedence");
2525c5aa6b58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost,
2526c5aa6b58SJeff Roberson      0,"Controls whether static kernel priorities are assigned to sleeping threads.");
25277b8bfa0dSJeff Roberson #ifdef SMP
2528ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
2529ae7a6b38SJeff Roberson     "Number of hz ticks to keep thread affinity for");
2530ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
2531ae7a6b38SJeff Roberson     "Enables the long-term load balancer");
25327fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
25337fcf154aSJeff Roberson     &balance_interval, 0,
25347fcf154aSJeff Roberson     "Average frequency in stathz ticks to run the long-term balancer");
2535ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0,
2536ae7a6b38SJeff Roberson     "Steals work from another hyper-threaded core on idle");
2537ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
2538ae7a6b38SJeff Roberson     "Attempts to steal work from other cores before idling");
253928994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
254028994a58SJeff Roberson     "Minimum load on remote cpu before we'll steal");
25417b8bfa0dSJeff Roberson #endif
2542e7d50326SJeff Roberson 
254354b0e65fSJeff Roberson /* ps compat.  All cpu percentages from ULE are weighted. */
2544a5423ea3SJeff Roberson static int ccpu = 0;
2545e7d50326SJeff Roberson SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
2546