xref: /freebsd/sys/kern/sched_ule.c (revision 885d51a38a7d27227219d37738b67fd39064ec6a)
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>
6235e6168fSJeff Roberson #ifdef KTRACE
6335e6168fSJeff Roberson #include <sys/uio.h>
6435e6168fSJeff Roberson #include <sys/ktrace.h>
6535e6168fSJeff Roberson #endif
6635e6168fSJeff Roberson 
67ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS
68ebccf1e3SJoseph Koshy #include <sys/pmckern.h>
69ebccf1e3SJoseph Koshy #endif
70ebccf1e3SJoseph Koshy 
7135e6168fSJeff Roberson #include <machine/cpu.h>
7222bf7d9aSJeff Roberson #include <machine/smp.h>
7335e6168fSJeff Roberson 
74cbdd62adSPeter Grehan #if !defined(__i386__) && !defined(__amd64__) && !defined(__powerpc__) && !defined(__arm__)
7502e2d6b4SJeff Roberson #error "This architecture is not currently compatible with ULE"
767a5e5e2aSJeff Roberson #endif
777a5e5e2aSJeff Roberson 
78ae7a6b38SJeff Roberson #define	KTR_ULE	0
7914618990SJeff Roberson 
806b2f763fSJeff Roberson /*
81ae7a6b38SJeff Roberson  * Thread scheduler specific section.  All fields are protected
82ae7a6b38SJeff Roberson  * by the thread lock.
83ed062c8dSJulian Elischer  */
84ad1e7d28SJulian Elischer struct td_sched {
85ae7a6b38SJeff Roberson 	TAILQ_ENTRY(td_sched) ts_procq;	/* Run queue. */
86ae7a6b38SJeff Roberson 	struct thread	*ts_thread;	/* Active associated thread. */
87ae7a6b38SJeff Roberson 	struct runq	*ts_runq;	/* Run-queue we're queued on. */
88ae7a6b38SJeff Roberson 	short		ts_flags;	/* TSF_* flags. */
89ae7a6b38SJeff Roberson 	u_char		ts_rqindex;	/* Run queue index. */
90ad1e7d28SJulian Elischer 	u_char		ts_cpu;		/* CPU that we have affinity for. */
91ae7a6b38SJeff Roberson 	int		ts_slice;	/* Ticks of slice remaining. */
92ae7a6b38SJeff Roberson 	u_int		ts_slptime;	/* Number of ticks we vol. slept */
93ae7a6b38SJeff Roberson 	u_int		ts_runtime;	/* Number of ticks we were running */
94ed062c8dSJulian Elischer 	/* The following variables are only used for pctcpu calculation */
95ad1e7d28SJulian Elischer 	int		ts_ltick;	/* Last tick that we were running on */
96ad1e7d28SJulian Elischer 	int		ts_ftick;	/* First tick that we were running on */
97ad1e7d28SJulian Elischer 	int		ts_ticks;	/* Tick count */
987b8bfa0dSJeff Roberson #ifdef SMP
997b8bfa0dSJeff Roberson 	int		ts_rltick;	/* Real last tick, for affinity. */
1007b8bfa0dSJeff Roberson #endif
101ed062c8dSJulian Elischer };
102ad1e7d28SJulian Elischer /* flags kept in ts_flags */
1037b8bfa0dSJeff Roberson #define	TSF_BOUND	0x0001		/* Thread can not migrate. */
1047b8bfa0dSJeff Roberson #define	TSF_XFERABLE	0x0002		/* Thread was added as transferable. */
10535e6168fSJeff Roberson 
106ad1e7d28SJulian Elischer static struct td_sched td_sched0;
10735e6168fSJeff Roberson 
10835e6168fSJeff Roberson /*
109e7d50326SJeff Roberson  * Cpu percentage computation macros and defines.
110e1f89c22SJeff Roberson  *
111e7d50326SJeff Roberson  * SCHED_TICK_SECS:	Number of seconds to average the cpu usage across.
112e7d50326SJeff Roberson  * SCHED_TICK_TARG:	Number of hz ticks to average the cpu usage across.
1138ab80cf0SJeff Roberson  * SCHED_TICK_MAX:	Maximum number of ticks before scaling back.
114e7d50326SJeff Roberson  * SCHED_TICK_SHIFT:	Shift factor to avoid rounding away results.
115e7d50326SJeff Roberson  * SCHED_TICK_HZ:	Compute the number of hz ticks for a given ticks count.
116e7d50326SJeff Roberson  * SCHED_TICK_TOTAL:	Gives the amount of time we've been recording ticks.
11735e6168fSJeff Roberson  */
118e7d50326SJeff Roberson #define	SCHED_TICK_SECS		10
119e7d50326SJeff Roberson #define	SCHED_TICK_TARG		(hz * SCHED_TICK_SECS)
1208ab80cf0SJeff Roberson #define	SCHED_TICK_MAX		(SCHED_TICK_TARG + hz)
121e7d50326SJeff Roberson #define	SCHED_TICK_SHIFT	10
122e7d50326SJeff Roberson #define	SCHED_TICK_HZ(ts)	((ts)->ts_ticks >> SCHED_TICK_SHIFT)
123eddb4efaSJeff Roberson #define	SCHED_TICK_TOTAL(ts)	(max((ts)->ts_ltick - (ts)->ts_ftick, hz))
12435e6168fSJeff Roberson 
12535e6168fSJeff Roberson /*
126e7d50326SJeff Roberson  * These macros determine priorities for non-interactive threads.  They are
127e7d50326SJeff Roberson  * assigned a priority based on their recent cpu utilization as expressed
128e7d50326SJeff Roberson  * by the ratio of ticks to the tick total.  NHALF priorities at the start
129e7d50326SJeff Roberson  * and end of the MIN to MAX timeshare range are only reachable with negative
130e7d50326SJeff Roberson  * or positive nice respectively.
131e7d50326SJeff Roberson  *
132e7d50326SJeff Roberson  * PRI_RANGE:	Priority range for utilization dependent priorities.
133e7d50326SJeff Roberson  * PRI_NRESV:	Number of nice values.
134e7d50326SJeff Roberson  * PRI_TICKS:	Compute a priority in PRI_RANGE from the ticks count and total.
135e7d50326SJeff Roberson  * PRI_NICE:	Determines the part of the priority inherited from nice.
136e7d50326SJeff Roberson  */
137e7d50326SJeff Roberson #define	SCHED_PRI_NRESV		(PRIO_MAX - PRIO_MIN)
138e7d50326SJeff Roberson #define	SCHED_PRI_NHALF		(SCHED_PRI_NRESV / 2)
139e7d50326SJeff Roberson #define	SCHED_PRI_MIN		(PRI_MIN_TIMESHARE + SCHED_PRI_NHALF)
140e7d50326SJeff Roberson #define	SCHED_PRI_MAX		(PRI_MAX_TIMESHARE - SCHED_PRI_NHALF)
141dda713dfSJeff Roberson #define	SCHED_PRI_RANGE		(SCHED_PRI_MAX - SCHED_PRI_MIN)
142e7d50326SJeff Roberson #define	SCHED_PRI_TICKS(ts)						\
143e7d50326SJeff Roberson     (SCHED_TICK_HZ((ts)) /						\
1441e516cf5SJeff Roberson     (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
145e7d50326SJeff Roberson #define	SCHED_PRI_NICE(nice)	(nice)
146e7d50326SJeff Roberson 
147e7d50326SJeff Roberson /*
148e7d50326SJeff Roberson  * These determine the interactivity of a process.  Interactivity differs from
149e7d50326SJeff Roberson  * cpu utilization in that it expresses the voluntary time slept vs time ran
150e7d50326SJeff Roberson  * while cpu utilization includes all time not running.  This more accurately
151e7d50326SJeff Roberson  * models the intent of the thread.
15235e6168fSJeff Roberson  *
153407b0157SJeff Roberson  * SLP_RUN_MAX:	Maximum amount of sleep time + run time we'll accumulate
154407b0157SJeff Roberson  *		before throttling back.
155d322132cSJeff Roberson  * SLP_RUN_FORK:	Maximum slp+run time to inherit at fork time.
156210491d3SJeff Roberson  * INTERACT_MAX:	Maximum interactivity value.  Smaller is better.
157e1f89c22SJeff Roberson  * INTERACT_THRESH:	Threshhold for placement on the current runq.
15835e6168fSJeff Roberson  */
159e7d50326SJeff Roberson #define	SCHED_SLP_RUN_MAX	((hz * 5) << SCHED_TICK_SHIFT)
160e7d50326SJeff Roberson #define	SCHED_SLP_RUN_FORK	((hz / 2) << SCHED_TICK_SHIFT)
161210491d3SJeff Roberson #define	SCHED_INTERACT_MAX	(100)
162210491d3SJeff Roberson #define	SCHED_INTERACT_HALF	(SCHED_INTERACT_MAX / 2)
1634c9612c6SJeff Roberson #define	SCHED_INTERACT_THRESH	(30)
164e1f89c22SJeff Roberson 
16535e6168fSJeff Roberson /*
166e7d50326SJeff Roberson  * tickincr:		Converts a stathz tick into a hz domain scaled by
167e7d50326SJeff Roberson  *			the shift factor.  Without the shift the error rate
168e7d50326SJeff Roberson  *			due to rounding would be unacceptably high.
169e7d50326SJeff Roberson  * realstathz:		stathz is sometimes 0 and run off of hz.
170e7d50326SJeff Roberson  * sched_slice:		Runtime of each thread before rescheduling.
171ae7a6b38SJeff Roberson  * preempt_thresh:	Priority threshold for preemption and remote IPIs.
17235e6168fSJeff Roberson  */
173e7d50326SJeff Roberson static int sched_interact = SCHED_INTERACT_THRESH;
174e7d50326SJeff Roberson static int realstathz;
175e7d50326SJeff Roberson static int tickincr;
176e7d50326SJeff Roberson static int sched_slice;
17702e2d6b4SJeff Roberson #ifdef PREEMPTION
17802e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION
17902e2d6b4SJeff Roberson static int preempt_thresh = PRI_MAX_IDLE;
18002e2d6b4SJeff Roberson #else
181ae7a6b38SJeff Roberson static int preempt_thresh = PRI_MIN_KERN;
18202e2d6b4SJeff Roberson #endif
18302e2d6b4SJeff Roberson #else
18402e2d6b4SJeff Roberson static int preempt_thresh = 0;
18502e2d6b4SJeff Roberson #endif
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 {
193c47f202bSJeff Roberson 	struct mtx	*tdq_lock;		/* Pointer to group lock. */
194e7d50326SJeff Roberson 	struct runq	tdq_realtime;		/* real-time run queue. */
195ae7a6b38SJeff Roberson 	struct runq	tdq_timeshare;		/* timeshare run queue. */
196ae7a6b38SJeff Roberson 	struct runq	tdq_idle;		/* Queue of IDLE threads. */
197ae7a6b38SJeff Roberson 	int		tdq_load;		/* Aggregate load. */
198ed0e8f2fSJeff Roberson 	u_char		tdq_idx;		/* Current insert index. */
199ed0e8f2fSJeff Roberson 	u_char		tdq_ridx;		/* Current removal index. */
2005d7ef00cSJeff Roberson #ifdef SMP
201ae7a6b38SJeff Roberson 	u_char		tdq_lowpri;		/* Lowest priority thread. */
202ae7a6b38SJeff Roberson 	int		tdq_transferable;	/* Transferable thread count. */
203d2ad694cSJeff Roberson 	LIST_ENTRY(tdq)	tdq_siblings;		/* Next in tdq group. */
204d2ad694cSJeff Roberson 	struct tdq_group *tdq_group;		/* Our processor group. */
20533916c36SJeff Roberson #else
206d2ad694cSJeff Roberson 	int		tdq_sysload;		/* For loadavg, !ITHD load. */
2075d7ef00cSJeff Roberson #endif
208ae7a6b38SJeff Roberson } __aligned(64);
20935e6168fSJeff Roberson 
2107b8bfa0dSJeff Roberson 
21180f86c9fSJeff Roberson #ifdef SMP
21280f86c9fSJeff Roberson /*
213ad1e7d28SJulian Elischer  * tdq groups are groups of processors which can cheaply share threads.  When
21480f86c9fSJeff Roberson  * one processor in the group goes idle it will check the runqs of the other
21580f86c9fSJeff Roberson  * processors in its group prior to halting and waiting for an interrupt.
21680f86c9fSJeff Roberson  * These groups are suitable for SMT (Symetric Multi-Threading) and not NUMA.
21780f86c9fSJeff Roberson  * In a numa environment we'd want an idle bitmap per group and a two tiered
21880f86c9fSJeff Roberson  * load balancer.
21980f86c9fSJeff Roberson  */
220ad1e7d28SJulian Elischer struct tdq_group {
221c47f202bSJeff Roberson 	struct mtx	tdg_lock;	/* Protects all fields below. */
222d2ad694cSJeff Roberson 	int		tdg_cpus;	/* Count of CPUs in this tdq group. */
223d2ad694cSJeff Roberson 	cpumask_t 	tdg_cpumask;	/* Mask of cpus in this group. */
224d2ad694cSJeff Roberson 	cpumask_t 	tdg_idlemask;	/* Idle cpus in this group. */
225d2ad694cSJeff Roberson 	cpumask_t 	tdg_mask;	/* Bit mask for first cpu. */
226d2ad694cSJeff Roberson 	int		tdg_load;	/* Total load of this group. */
227d2ad694cSJeff Roberson 	int	tdg_transferable;	/* Transferable load of this group. */
228d2ad694cSJeff Roberson 	LIST_HEAD(, tdq) tdg_members;	/* Linked list of all members. */
229c47f202bSJeff Roberson 	char		tdg_name[16];	/* lock name. */
230ae7a6b38SJeff Roberson } __aligned(64);
2317b8bfa0dSJeff Roberson 
232ae7a6b38SJeff Roberson #define	SCHED_AFFINITY_DEFAULT	(max(1, hz / 300))
2337b8bfa0dSJeff Roberson #define	SCHED_AFFINITY(ts)	((ts)->ts_rltick > ticks - affinity)
2347b8bfa0dSJeff Roberson 
2357b8bfa0dSJeff Roberson /*
2367b8bfa0dSJeff Roberson  * Run-time tunables.
2377b8bfa0dSJeff Roberson  */
23828994a58SJeff Roberson static int rebalance = 1;
2397fcf154aSJeff Roberson static int balance_interval = 128;	/* Default set in sched_initticks(). */
24028994a58SJeff Roberson static int pick_pri = 1;
2417b8bfa0dSJeff Roberson static int affinity;
2427b8bfa0dSJeff Roberson static int tryself = 1;
2437fcf154aSJeff Roberson static int steal_htt = 1;
24428994a58SJeff Roberson static int steal_idle = 1;
24528994a58SJeff Roberson static int steal_thresh = 2;
2467b20fb19SJeff Roberson static int topology = 0;
24780f86c9fSJeff Roberson 
24835e6168fSJeff Roberson /*
249d2ad694cSJeff Roberson  * One thread queue per processor.
25035e6168fSJeff Roberson  */
2517b8bfa0dSJeff Roberson static volatile cpumask_t tdq_idle;
252d2ad694cSJeff Roberson static int tdg_maxid;
253ad1e7d28SJulian Elischer static struct tdq	tdq_cpu[MAXCPU];
254ad1e7d28SJulian Elischer static struct tdq_group tdq_groups[MAXCPU];
2557fcf154aSJeff Roberson static struct tdq	*balance_tdq;
2567fcf154aSJeff Roberson static int balance_group_ticks;
2577fcf154aSJeff Roberson static int balance_ticks;
258dc03363dSJeff Roberson 
259ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu[PCPU_GET(cpuid)])
260ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu[(x)])
261c47f202bSJeff Roberson #define	TDQ_ID(x)	((int)((x) - tdq_cpu))
262ad1e7d28SJulian Elischer #define	TDQ_GROUP(x)	(&tdq_groups[(x)])
263c47f202bSJeff Roberson #define	TDG_ID(x)	((int)((x) - tdq_groups))
26480f86c9fSJeff Roberson #else	/* !SMP */
265ad1e7d28SJulian Elischer static struct tdq	tdq_cpu;
266c47f202bSJeff Roberson static struct mtx	tdq_lock;
267dc03363dSJeff Roberson 
26836b36916SJeff Roberson #define	TDQ_ID(x)	(0)
269ad1e7d28SJulian Elischer #define	TDQ_SELF()	(&tdq_cpu)
270ad1e7d28SJulian Elischer #define	TDQ_CPU(x)	(&tdq_cpu)
2710a016a05SJeff Roberson #endif
27235e6168fSJeff Roberson 
273ae7a6b38SJeff Roberson #define	TDQ_LOCK_ASSERT(t, type)	mtx_assert(TDQ_LOCKPTR((t)), (type))
274ae7a6b38SJeff Roberson #define	TDQ_LOCK(t)		mtx_lock_spin(TDQ_LOCKPTR((t)))
275ae7a6b38SJeff Roberson #define	TDQ_LOCK_FLAGS(t, f)	mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
276ae7a6b38SJeff Roberson #define	TDQ_UNLOCK(t)		mtx_unlock_spin(TDQ_LOCKPTR((t)))
277c47f202bSJeff Roberson #define	TDQ_LOCKPTR(t)		((t)->tdq_lock)
278ae7a6b38SJeff Roberson 
2798460a577SJohn Birrell static void sched_priority(struct thread *);
28021381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char);
2818460a577SJohn Birrell static int sched_interact_score(struct thread *);
2828460a577SJohn Birrell static void sched_interact_update(struct thread *);
2838460a577SJohn Birrell static void sched_interact_fork(struct thread *);
284ad1e7d28SJulian Elischer static void sched_pctcpu_update(struct td_sched *);
28535e6168fSJeff Roberson 
2865d7ef00cSJeff Roberson /* Operations on per processor queues */
287ad1e7d28SJulian Elischer static struct td_sched * tdq_choose(struct tdq *);
288ad1e7d28SJulian Elischer static void tdq_setup(struct tdq *);
289ad1e7d28SJulian Elischer static void tdq_load_add(struct tdq *, struct td_sched *);
290ad1e7d28SJulian Elischer static void tdq_load_rem(struct tdq *, struct td_sched *);
291ad1e7d28SJulian Elischer static __inline void tdq_runq_add(struct tdq *, struct td_sched *, int);
292ad1e7d28SJulian Elischer static __inline void tdq_runq_rem(struct tdq *, struct td_sched *);
293ad1e7d28SJulian Elischer void tdq_print(int cpu);
294e7d50326SJeff Roberson static void runq_print(struct runq *rq);
295ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int);
2965d7ef00cSJeff Roberson #ifdef SMP
297ae7a6b38SJeff Roberson static void tdq_move(struct tdq *, struct tdq *);
298ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *);
2997b8bfa0dSJeff Roberson static void tdq_notify(struct td_sched *);
3007fcf154aSJeff Roberson static struct td_sched *tdq_steal(struct tdq *);
301ae7a6b38SJeff Roberson static struct td_sched *runq_steal(struct runq *);
302ae7a6b38SJeff Roberson static int sched_pickcpu(struct td_sched *, int);
3037fcf154aSJeff Roberson static void sched_balance(void);
3047fcf154aSJeff Roberson static void sched_balance_groups(void);
305ae7a6b38SJeff Roberson static void sched_balance_group(struct tdq_group *);
306ae7a6b38SJeff Roberson static void sched_balance_pair(struct tdq *, struct tdq *);
307ae7a6b38SJeff Roberson static inline struct tdq *sched_setcpu(struct td_sched *, int, int);
308ae7a6b38SJeff Roberson static inline struct mtx *thread_block_switch(struct thread *);
309ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *);
310c47f202bSJeff Roberson static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int);
3111e516cf5SJeff Roberson 
3127b8bfa0dSJeff Roberson #define	THREAD_CAN_MIGRATE(td)	 ((td)->td_pinned == 0)
3135d7ef00cSJeff Roberson #endif
3145d7ef00cSJeff Roberson 
315e7d50326SJeff Roberson static void sched_setup(void *dummy);
316e7d50326SJeff Roberson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL)
317e7d50326SJeff Roberson 
318e7d50326SJeff Roberson static void sched_initticks(void *dummy);
319e7d50326SJeff Roberson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, NULL)
320e7d50326SJeff Roberson 
321ae7a6b38SJeff Roberson /*
322ae7a6b38SJeff Roberson  * Print the threads waiting on a run-queue.
323ae7a6b38SJeff Roberson  */
324e7d50326SJeff Roberson static void
325e7d50326SJeff Roberson runq_print(struct runq *rq)
326e7d50326SJeff Roberson {
327e7d50326SJeff Roberson 	struct rqhead *rqh;
328e7d50326SJeff Roberson 	struct td_sched *ts;
329e7d50326SJeff Roberson 	int pri;
330e7d50326SJeff Roberson 	int j;
331e7d50326SJeff Roberson 	int i;
332e7d50326SJeff Roberson 
333e7d50326SJeff Roberson 	for (i = 0; i < RQB_LEN; i++) {
334e7d50326SJeff Roberson 		printf("\t\trunq bits %d 0x%zx\n",
335e7d50326SJeff Roberson 		    i, rq->rq_status.rqb_bits[i]);
336e7d50326SJeff Roberson 		for (j = 0; j < RQB_BPW; j++)
337e7d50326SJeff Roberson 			if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
338e7d50326SJeff Roberson 				pri = j + (i << RQB_L2BPW);
339e7d50326SJeff Roberson 				rqh = &rq->rq_queues[pri];
340e7d50326SJeff Roberson 				TAILQ_FOREACH(ts, rqh, ts_procq) {
341e7d50326SJeff Roberson 					printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
342431f8906SJulian Elischer 					    ts->ts_thread, ts->ts_thread->td_name, ts->ts_thread->td_priority, ts->ts_rqindex, pri);
343e7d50326SJeff Roberson 				}
344e7d50326SJeff Roberson 			}
345e7d50326SJeff Roberson 	}
346e7d50326SJeff Roberson }
347e7d50326SJeff Roberson 
348ae7a6b38SJeff Roberson /*
349ae7a6b38SJeff Roberson  * Print the status of a per-cpu thread queue.  Should be a ddb show cmd.
350ae7a6b38SJeff Roberson  */
35115dc847eSJeff Roberson void
352ad1e7d28SJulian Elischer tdq_print(int cpu)
35315dc847eSJeff Roberson {
354ad1e7d28SJulian Elischer 	struct tdq *tdq;
35515dc847eSJeff Roberson 
356ad1e7d28SJulian Elischer 	tdq = TDQ_CPU(cpu);
35715dc847eSJeff Roberson 
358c47f202bSJeff Roberson 	printf("tdq %d:\n", TDQ_ID(tdq));
359ae7a6b38SJeff Roberson 	printf("\tlockptr         %p\n", TDQ_LOCKPTR(tdq));
360d2ad694cSJeff Roberson 	printf("\tload:           %d\n", tdq->tdq_load);
361e7d50326SJeff Roberson 	printf("\ttimeshare idx:  %d\n", tdq->tdq_idx);
3623f872f85SJeff Roberson 	printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
363e7d50326SJeff Roberson 	printf("\trealtime runq:\n");
364e7d50326SJeff Roberson 	runq_print(&tdq->tdq_realtime);
365e7d50326SJeff Roberson 	printf("\ttimeshare runq:\n");
366e7d50326SJeff Roberson 	runq_print(&tdq->tdq_timeshare);
367e7d50326SJeff Roberson 	printf("\tidle runq:\n");
368e7d50326SJeff Roberson 	runq_print(&tdq->tdq_idle);
369ef1134c9SJeff Roberson #ifdef SMP
370d2ad694cSJeff Roberson 	printf("\tload transferable: %d\n", tdq->tdq_transferable);
371ae7a6b38SJeff Roberson 	printf("\tlowest priority:   %d\n", tdq->tdq_lowpri);
372c47f202bSJeff Roberson 	printf("\tgroup:             %d\n", TDG_ID(tdq->tdq_group));
373c47f202bSJeff Roberson 	printf("\tLock name:         %s\n", tdq->tdq_group->tdg_name);
374ef1134c9SJeff Roberson #endif
37515dc847eSJeff Roberson }
37615dc847eSJeff 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
384ad1e7d28SJulian Elischer tdq_runq_add(struct tdq *tdq, struct td_sched *ts, int flags)
385155b9987SJeff Roberson {
386ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
387ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
388155b9987SJeff Roberson #ifdef SMP
389e7d50326SJeff Roberson 	if (THREAD_CAN_MIGRATE(ts->ts_thread)) {
390d2ad694cSJeff Roberson 		tdq->tdq_transferable++;
391d2ad694cSJeff Roberson 		tdq->tdq_group->tdg_transferable++;
392ad1e7d28SJulian Elischer 		ts->ts_flags |= TSF_XFERABLE;
39380f86c9fSJeff Roberson 	}
394155b9987SJeff Roberson #endif
395e7d50326SJeff Roberson 	if (ts->ts_runq == &tdq->tdq_timeshare) {
396ed0e8f2fSJeff Roberson 		u_char pri;
397e7d50326SJeff Roberson 
398e7d50326SJeff Roberson 		pri = ts->ts_thread->td_priority;
399e7d50326SJeff Roberson 		KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
400e7d50326SJeff Roberson 			("Invalid priority %d on timeshare runq", pri));
401e7d50326SJeff Roberson 		/*
402e7d50326SJeff Roberson 		 * This queue contains only priorities between MIN and MAX
403e7d50326SJeff Roberson 		 * realtime.  Use the whole queue to represent these values.
404e7d50326SJeff Roberson 		 */
405c47f202bSJeff Roberson 		if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
406e7d50326SJeff Roberson 			pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
407e7d50326SJeff Roberson 			pri = (pri + tdq->tdq_idx) % RQ_NQS;
4083f872f85SJeff Roberson 			/*
4093f872f85SJeff Roberson 			 * This effectively shortens the queue by one so we
4103f872f85SJeff Roberson 			 * can have a one slot difference between idx and
4113f872f85SJeff Roberson 			 * ridx while we wait for threads to drain.
4123f872f85SJeff Roberson 			 */
4133f872f85SJeff Roberson 			if (tdq->tdq_ridx != tdq->tdq_idx &&
4143f872f85SJeff Roberson 			    pri == tdq->tdq_ridx)
4154499aff6SJeff Roberson 				pri = (unsigned char)(pri - 1) % RQ_NQS;
416e7d50326SJeff Roberson 		} else
4173f872f85SJeff Roberson 			pri = tdq->tdq_ridx;
418e7d50326SJeff Roberson 		runq_add_pri(ts->ts_runq, ts, pri, flags);
419e7d50326SJeff Roberson 	} else
420ad1e7d28SJulian Elischer 		runq_add(ts->ts_runq, ts, flags);
421155b9987SJeff Roberson }
422155b9987SJeff Roberson 
423ae7a6b38SJeff Roberson /*
424ae7a6b38SJeff Roberson  * Remove a thread from a run-queue.  This typically happens when a thread
425ae7a6b38SJeff Roberson  * is selected to run.  Running threads are not on the queue and the
426ae7a6b38SJeff Roberson  * transferable count does not reflect them.
427ae7a6b38SJeff Roberson  */
428155b9987SJeff Roberson static __inline void
429ad1e7d28SJulian Elischer tdq_runq_rem(struct tdq *tdq, struct td_sched *ts)
430155b9987SJeff Roberson {
431ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
432ae7a6b38SJeff Roberson 	KASSERT(ts->ts_runq != NULL,
433ae7a6b38SJeff Roberson 	    ("tdq_runq_remove: thread %p null ts_runq", ts->ts_thread));
434155b9987SJeff Roberson #ifdef SMP
435ad1e7d28SJulian Elischer 	if (ts->ts_flags & TSF_XFERABLE) {
436d2ad694cSJeff Roberson 		tdq->tdq_transferable--;
437d2ad694cSJeff Roberson 		tdq->tdq_group->tdg_transferable--;
438ad1e7d28SJulian Elischer 		ts->ts_flags &= ~TSF_XFERABLE;
43980f86c9fSJeff Roberson 	}
440155b9987SJeff Roberson #endif
4413f872f85SJeff Roberson 	if (ts->ts_runq == &tdq->tdq_timeshare) {
4423f872f85SJeff Roberson 		if (tdq->tdq_idx != tdq->tdq_ridx)
4433f872f85SJeff Roberson 			runq_remove_idx(ts->ts_runq, ts, &tdq->tdq_ridx);
444e7d50326SJeff Roberson 		else
4453f872f85SJeff Roberson 			runq_remove_idx(ts->ts_runq, ts, NULL);
4468ab80cf0SJeff Roberson 		/*
4478ab80cf0SJeff Roberson 		 * For timeshare threads we update the priority here so
4488ab80cf0SJeff Roberson 		 * the priority reflects the time we've been sleeping.
4498ab80cf0SJeff Roberson 		 */
4508ab80cf0SJeff Roberson 		ts->ts_ltick = ticks;
4518ab80cf0SJeff Roberson 		sched_pctcpu_update(ts);
4528ab80cf0SJeff Roberson 		sched_priority(ts->ts_thread);
4533f872f85SJeff Roberson 	} else
454ad1e7d28SJulian Elischer 		runq_remove(ts->ts_runq, ts);
455155b9987SJeff Roberson }
456155b9987SJeff Roberson 
457ae7a6b38SJeff Roberson /*
458ae7a6b38SJeff Roberson  * Load is maintained for all threads RUNNING and ON_RUNQ.  Add the load
459ae7a6b38SJeff Roberson  * for this thread to the referenced thread queue.
460ae7a6b38SJeff Roberson  */
461a8949de2SJeff Roberson static void
462ad1e7d28SJulian Elischer tdq_load_add(struct tdq *tdq, struct td_sched *ts)
4635d7ef00cSJeff Roberson {
464ef1134c9SJeff Roberson 	int class;
465ae7a6b38SJeff Roberson 
466ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
467ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
468ad1e7d28SJulian Elischer 	class = PRI_BASE(ts->ts_thread->td_pri_class);
469d2ad694cSJeff Roberson 	tdq->tdq_load++;
470c47f202bSJeff Roberson 	CTR2(KTR_SCHED, "cpu %d load: %d", TDQ_ID(tdq), tdq->tdq_load);
4717b8bfa0dSJeff Roberson 	if (class != PRI_ITHD &&
4727b8bfa0dSJeff Roberson 	    (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
47333916c36SJeff Roberson #ifdef SMP
474d2ad694cSJeff Roberson 		tdq->tdq_group->tdg_load++;
47533916c36SJeff Roberson #else
476d2ad694cSJeff Roberson 		tdq->tdq_sysload++;
477cac77d04SJeff Roberson #endif
4785d7ef00cSJeff Roberson }
47915dc847eSJeff Roberson 
480ae7a6b38SJeff Roberson /*
481ae7a6b38SJeff Roberson  * Remove the load from a thread that is transitioning to a sleep state or
482ae7a6b38SJeff Roberson  * exiting.
483ae7a6b38SJeff Roberson  */
484a8949de2SJeff Roberson static void
485ad1e7d28SJulian Elischer tdq_load_rem(struct tdq *tdq, struct td_sched *ts)
4865d7ef00cSJeff Roberson {
487ef1134c9SJeff Roberson 	int class;
488ae7a6b38SJeff Roberson 
489ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
490ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
491ad1e7d28SJulian Elischer 	class = PRI_BASE(ts->ts_thread->td_pri_class);
4927b8bfa0dSJeff Roberson 	if (class != PRI_ITHD &&
4937b8bfa0dSJeff Roberson 	    (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
49433916c36SJeff Roberson #ifdef SMP
495d2ad694cSJeff Roberson 		tdq->tdq_group->tdg_load--;
49633916c36SJeff Roberson #else
497d2ad694cSJeff Roberson 		tdq->tdq_sysload--;
498cac77d04SJeff Roberson #endif
499ae7a6b38SJeff Roberson 	KASSERT(tdq->tdq_load != 0,
500c47f202bSJeff Roberson 	    ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq)));
501d2ad694cSJeff Roberson 	tdq->tdq_load--;
502d2ad694cSJeff Roberson 	CTR1(KTR_SCHED, "load: %d", tdq->tdq_load);
503ad1e7d28SJulian Elischer 	ts->ts_runq = NULL;
50415dc847eSJeff Roberson }
50515dc847eSJeff Roberson 
5065d7ef00cSJeff Roberson #ifdef SMP
507356500a3SJeff Roberson /*
508155b9987SJeff Roberson  * sched_balance is a simple CPU load balancing algorithm.  It operates by
509356500a3SJeff Roberson  * finding the least loaded and most loaded cpu and equalizing their load
510356500a3SJeff Roberson  * by migrating some processes.
511356500a3SJeff Roberson  *
512356500a3SJeff Roberson  * Dealing only with two CPUs at a time has two advantages.  Firstly, most
513356500a3SJeff Roberson  * installations will only have 2 cpus.  Secondly, load balancing too much at
514356500a3SJeff Roberson  * once can have an unpleasant effect on the system.  The scheduler rarely has
515356500a3SJeff Roberson  * enough information to make perfect decisions.  So this algorithm chooses
516ae7a6b38SJeff Roberson  * simplicity and more gradual effects on load in larger systems.
517356500a3SJeff Roberson  *
518356500a3SJeff Roberson  */
51922bf7d9aSJeff Roberson static void
5207fcf154aSJeff Roberson sched_balance()
521356500a3SJeff Roberson {
522ad1e7d28SJulian Elischer 	struct tdq_group *high;
523ad1e7d28SJulian Elischer 	struct tdq_group *low;
524d2ad694cSJeff Roberson 	struct tdq_group *tdg;
5257fcf154aSJeff Roberson 	struct tdq *tdq;
526cac77d04SJeff Roberson 	int cnt;
527356500a3SJeff Roberson 	int i;
528356500a3SJeff Roberson 
5297fcf154aSJeff Roberson 	/*
5307fcf154aSJeff Roberson 	 * Select a random time between .5 * balance_interval and
5317fcf154aSJeff Roberson 	 * 1.5 * balance_interval.
5327fcf154aSJeff Roberson 	 */
5337fcf154aSJeff Roberson 	balance_ticks = max(balance_interval / 2, 1);
5347fcf154aSJeff Roberson 	balance_ticks += random() % balance_interval;
535ae7a6b38SJeff Roberson 	if (smp_started == 0 || rebalance == 0)
536598b368dSJeff Roberson 		return;
5377fcf154aSJeff Roberson 	tdq = TDQ_SELF();
5387fcf154aSJeff Roberson 	TDQ_UNLOCK(tdq);
539cac77d04SJeff Roberson 	low = high = NULL;
540d2ad694cSJeff Roberson 	i = random() % (tdg_maxid + 1);
541d2ad694cSJeff Roberson 	for (cnt = 0; cnt <= tdg_maxid; cnt++) {
542d2ad694cSJeff Roberson 		tdg = TDQ_GROUP(i);
543cac77d04SJeff Roberson 		/*
544cac77d04SJeff Roberson 		 * Find the CPU with the highest load that has some
545cac77d04SJeff Roberson 		 * threads to transfer.
546cac77d04SJeff Roberson 		 */
547d2ad694cSJeff Roberson 		if ((high == NULL || tdg->tdg_load > high->tdg_load)
548d2ad694cSJeff Roberson 		    && tdg->tdg_transferable)
549d2ad694cSJeff Roberson 			high = tdg;
550d2ad694cSJeff Roberson 		if (low == NULL || tdg->tdg_load < low->tdg_load)
551d2ad694cSJeff Roberson 			low = tdg;
552d2ad694cSJeff Roberson 		if (++i > tdg_maxid)
553cac77d04SJeff Roberson 			i = 0;
554cac77d04SJeff Roberson 	}
555cac77d04SJeff Roberson 	if (low != NULL && high != NULL && high != low)
556d2ad694cSJeff Roberson 		sched_balance_pair(LIST_FIRST(&high->tdg_members),
557d2ad694cSJeff Roberson 		    LIST_FIRST(&low->tdg_members));
5587fcf154aSJeff Roberson 	TDQ_LOCK(tdq);
559cac77d04SJeff Roberson }
56086f8ae96SJeff Roberson 
561ae7a6b38SJeff Roberson /*
562ae7a6b38SJeff Roberson  * Balance load between CPUs in a group.  Will only migrate within the group.
563ae7a6b38SJeff Roberson  */
564cac77d04SJeff Roberson static void
5657fcf154aSJeff Roberson sched_balance_groups()
566cac77d04SJeff Roberson {
5677fcf154aSJeff Roberson 	struct tdq *tdq;
568cac77d04SJeff Roberson 	int i;
569cac77d04SJeff Roberson 
5707fcf154aSJeff Roberson 	/*
5717fcf154aSJeff Roberson 	 * Select a random time between .5 * balance_interval and
5727fcf154aSJeff Roberson 	 * 1.5 * balance_interval.
5737fcf154aSJeff Roberson 	 */
5747fcf154aSJeff Roberson 	balance_group_ticks = max(balance_interval / 2, 1);
5757fcf154aSJeff Roberson 	balance_group_ticks += random() % balance_interval;
576ae7a6b38SJeff Roberson 	if (smp_started == 0 || rebalance == 0)
577ae7a6b38SJeff Roberson 		return;
5787fcf154aSJeff Roberson 	tdq = TDQ_SELF();
5797fcf154aSJeff Roberson 	TDQ_UNLOCK(tdq);
580d2ad694cSJeff Roberson 	for (i = 0; i <= tdg_maxid; i++)
581ad1e7d28SJulian Elischer 		sched_balance_group(TDQ_GROUP(i));
5827fcf154aSJeff Roberson 	TDQ_LOCK(tdq);
583356500a3SJeff Roberson }
584cac77d04SJeff Roberson 
585ae7a6b38SJeff Roberson /*
586ae7a6b38SJeff Roberson  * Finds the greatest imbalance between two tdqs in a group.
587ae7a6b38SJeff Roberson  */
588cac77d04SJeff Roberson static void
589d2ad694cSJeff Roberson sched_balance_group(struct tdq_group *tdg)
590cac77d04SJeff Roberson {
591ad1e7d28SJulian Elischer 	struct tdq *tdq;
592ad1e7d28SJulian Elischer 	struct tdq *high;
593ad1e7d28SJulian Elischer 	struct tdq *low;
594cac77d04SJeff Roberson 	int load;
595cac77d04SJeff Roberson 
596d2ad694cSJeff Roberson 	if (tdg->tdg_transferable == 0)
597cac77d04SJeff Roberson 		return;
598cac77d04SJeff Roberson 	low = NULL;
599cac77d04SJeff Roberson 	high = NULL;
600d2ad694cSJeff Roberson 	LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) {
601d2ad694cSJeff Roberson 		load = tdq->tdq_load;
602d2ad694cSJeff Roberson 		if (high == NULL || load > high->tdq_load)
603ad1e7d28SJulian Elischer 			high = tdq;
604d2ad694cSJeff Roberson 		if (low == NULL || load < low->tdq_load)
605ad1e7d28SJulian Elischer 			low = tdq;
606356500a3SJeff Roberson 	}
607cac77d04SJeff Roberson 	if (high != NULL && low != NULL && high != low)
608cac77d04SJeff Roberson 		sched_balance_pair(high, low);
609356500a3SJeff Roberson }
610cac77d04SJeff Roberson 
611ae7a6b38SJeff Roberson /*
612ae7a6b38SJeff Roberson  * Lock two thread queues using their address to maintain lock order.
613ae7a6b38SJeff Roberson  */
614ae7a6b38SJeff Roberson static void
615ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two)
616ae7a6b38SJeff Roberson {
617ae7a6b38SJeff Roberson 	if (one < two) {
618ae7a6b38SJeff Roberson 		TDQ_LOCK(one);
619ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(two, MTX_DUPOK);
620ae7a6b38SJeff Roberson 	} else {
621ae7a6b38SJeff Roberson 		TDQ_LOCK(two);
622ae7a6b38SJeff Roberson 		TDQ_LOCK_FLAGS(one, MTX_DUPOK);
623ae7a6b38SJeff Roberson 	}
624ae7a6b38SJeff Roberson }
625ae7a6b38SJeff Roberson 
626ae7a6b38SJeff Roberson /*
6277fcf154aSJeff Roberson  * Unlock two thread queues.  Order is not important here.
6287fcf154aSJeff Roberson  */
6297fcf154aSJeff Roberson static void
6307fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two)
6317fcf154aSJeff Roberson {
6327fcf154aSJeff Roberson 	TDQ_UNLOCK(one);
6337fcf154aSJeff Roberson 	TDQ_UNLOCK(two);
6347fcf154aSJeff Roberson }
6357fcf154aSJeff Roberson 
6367fcf154aSJeff Roberson /*
637ae7a6b38SJeff Roberson  * Transfer load between two imbalanced thread queues.
638ae7a6b38SJeff Roberson  */
639cac77d04SJeff Roberson static void
640ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low)
641cac77d04SJeff Roberson {
642cac77d04SJeff Roberson 	int transferable;
643cac77d04SJeff Roberson 	int high_load;
644cac77d04SJeff Roberson 	int low_load;
645cac77d04SJeff Roberson 	int move;
646cac77d04SJeff Roberson 	int diff;
647cac77d04SJeff Roberson 	int i;
648cac77d04SJeff Roberson 
649ae7a6b38SJeff Roberson 	tdq_lock_pair(high, low);
65080f86c9fSJeff Roberson 	/*
65180f86c9fSJeff Roberson 	 * If we're transfering within a group we have to use this specific
652ad1e7d28SJulian Elischer 	 * tdq's transferable count, otherwise we can steal from other members
65380f86c9fSJeff Roberson 	 * of the group.
65480f86c9fSJeff Roberson 	 */
655d2ad694cSJeff Roberson 	if (high->tdq_group == low->tdq_group) {
656d2ad694cSJeff Roberson 		transferable = high->tdq_transferable;
657d2ad694cSJeff Roberson 		high_load = high->tdq_load;
658d2ad694cSJeff Roberson 		low_load = low->tdq_load;
659cac77d04SJeff Roberson 	} else {
660d2ad694cSJeff Roberson 		transferable = high->tdq_group->tdg_transferable;
661d2ad694cSJeff Roberson 		high_load = high->tdq_group->tdg_load;
662d2ad694cSJeff Roberson 		low_load = low->tdq_group->tdg_load;
663cac77d04SJeff Roberson 	}
664155b9987SJeff Roberson 	/*
665155b9987SJeff Roberson 	 * Determine what the imbalance is and then adjust that to how many
666d2ad694cSJeff Roberson 	 * threads we actually have to give up (transferable).
667155b9987SJeff Roberson 	 */
668ae7a6b38SJeff Roberson 	if (transferable != 0) {
669cac77d04SJeff Roberson 		diff = high_load - low_load;
670356500a3SJeff Roberson 		move = diff / 2;
671356500a3SJeff Roberson 		if (diff & 0x1)
672356500a3SJeff Roberson 			move++;
67380f86c9fSJeff Roberson 		move = min(move, transferable);
674356500a3SJeff Roberson 		for (i = 0; i < move; i++)
675ae7a6b38SJeff Roberson 			tdq_move(high, low);
676a5423ea3SJeff Roberson 		/*
677a5423ea3SJeff Roberson 		 * IPI the target cpu to force it to reschedule with the new
678a5423ea3SJeff Roberson 		 * workload.
679a5423ea3SJeff Roberson 		 */
680a5423ea3SJeff Roberson 		ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT);
681ae7a6b38SJeff Roberson 	}
6827fcf154aSJeff Roberson 	tdq_unlock_pair(high, low);
683356500a3SJeff Roberson 	return;
684356500a3SJeff Roberson }
685356500a3SJeff Roberson 
686ae7a6b38SJeff Roberson /*
687ae7a6b38SJeff Roberson  * Move a thread from one thread queue to another.
688ae7a6b38SJeff Roberson  */
68922bf7d9aSJeff Roberson static void
690ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to)
691356500a3SJeff Roberson {
692ad1e7d28SJulian Elischer 	struct td_sched *ts;
693ae7a6b38SJeff Roberson 	struct thread *td;
694ae7a6b38SJeff Roberson 	struct tdq *tdq;
695ae7a6b38SJeff Roberson 	int cpu;
696356500a3SJeff Roberson 
6977fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(from, MA_OWNED);
6987fcf154aSJeff Roberson 	TDQ_LOCK_ASSERT(to, MA_OWNED);
6997fcf154aSJeff Roberson 
700ad1e7d28SJulian Elischer 	tdq = from;
701ae7a6b38SJeff Roberson 	cpu = TDQ_ID(to);
7027fcf154aSJeff Roberson 	ts = tdq_steal(tdq);
703ad1e7d28SJulian Elischer 	if (ts == NULL) {
704d2ad694cSJeff Roberson 		struct tdq_group *tdg;
70580f86c9fSJeff Roberson 
706d2ad694cSJeff Roberson 		tdg = tdq->tdq_group;
707d2ad694cSJeff Roberson 		LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) {
708d2ad694cSJeff Roberson 			if (tdq == from || tdq->tdq_transferable == 0)
70980f86c9fSJeff Roberson 				continue;
7107fcf154aSJeff Roberson 			ts = tdq_steal(tdq);
71180f86c9fSJeff Roberson 			break;
71280f86c9fSJeff Roberson 		}
713ad1e7d28SJulian Elischer 		if (ts == NULL)
714ae7a6b38SJeff Roberson 			return;
71580f86c9fSJeff Roberson 	}
716ad1e7d28SJulian Elischer 	if (tdq == to)
71780f86c9fSJeff Roberson 		return;
718ae7a6b38SJeff Roberson 	td = ts->ts_thread;
719ae7a6b38SJeff Roberson 	/*
720ae7a6b38SJeff Roberson 	 * Although the run queue is locked the thread may be blocked.  Lock
7217fcf154aSJeff Roberson 	 * it to clear this and acquire the run-queue lock.
722ae7a6b38SJeff Roberson 	 */
723ae7a6b38SJeff Roberson 	thread_lock(td);
7247fcf154aSJeff Roberson 	/* Drop recursive lock on from acquired via thread_lock(). */
725ae7a6b38SJeff Roberson 	TDQ_UNLOCK(from);
726ae7a6b38SJeff Roberson 	sched_rem(td);
7277b8bfa0dSJeff Roberson 	ts->ts_cpu = cpu;
728ae7a6b38SJeff Roberson 	td->td_lock = TDQ_LOCKPTR(to);
729ae7a6b38SJeff Roberson 	tdq_add(to, td, SRQ_YIELDING);
730356500a3SJeff Roberson }
73122bf7d9aSJeff Roberson 
732ae7a6b38SJeff Roberson /*
733ae7a6b38SJeff Roberson  * This tdq has idled.  Try to steal a thread from another cpu and switch
734ae7a6b38SJeff Roberson  * to it.
735ae7a6b38SJeff Roberson  */
73680f86c9fSJeff Roberson static int
737ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq)
73822bf7d9aSJeff Roberson {
739d2ad694cSJeff Roberson 	struct tdq_group *tdg;
740ad1e7d28SJulian Elischer 	struct tdq *steal;
741ae7a6b38SJeff Roberson 	int highload;
742ae7a6b38SJeff Roberson 	int highcpu;
743ae7a6b38SJeff Roberson 	int cpu;
74480f86c9fSJeff Roberson 
74588f530ccSJeff Roberson 	if (smp_started == 0 || steal_idle == 0)
74688f530ccSJeff Roberson 		return (1);
747ae7a6b38SJeff Roberson 	/* We don't want to be preempted while we're iterating over tdqs */
748ae7a6b38SJeff Roberson 	spinlock_enter();
749d2ad694cSJeff Roberson 	tdg = tdq->tdq_group;
75080f86c9fSJeff Roberson 	/*
751d2ad694cSJeff Roberson 	 * If we're in a cpu group, try and steal threads from another cpu in
7527fcf154aSJeff Roberson 	 * the group before idling.  In a HTT group all cpus share the same
7537fcf154aSJeff Roberson 	 * run-queue lock, however, we still need a recursive lock to
7547fcf154aSJeff Roberson 	 * call tdq_move().
75580f86c9fSJeff Roberson 	 */
7567b8bfa0dSJeff Roberson 	if (steal_htt && tdg->tdg_cpus > 1 && tdg->tdg_transferable) {
7577fcf154aSJeff Roberson 		TDQ_LOCK(tdq);
758d2ad694cSJeff Roberson 		LIST_FOREACH(steal, &tdg->tdg_members, tdq_siblings) {
759d2ad694cSJeff Roberson 			if (steal == tdq || steal->tdq_transferable == 0)
76080f86c9fSJeff Roberson 				continue;
761ae7a6b38SJeff Roberson 			TDQ_LOCK(steal);
7627b8bfa0dSJeff Roberson 			goto steal;
7637b8bfa0dSJeff Roberson 		}
7647fcf154aSJeff Roberson 		TDQ_UNLOCK(tdq);
7657b8bfa0dSJeff Roberson 	}
76688f530ccSJeff Roberson 	/*
76788f530ccSJeff Roberson 	 * Find the least loaded CPU with a transferable thread and attempt
76888f530ccSJeff Roberson 	 * to steal it.  We make a lockless pass and then verify that the
76988f530ccSJeff Roberson 	 * thread is still available after locking.
77088f530ccSJeff Roberson 	 */
771ae7a6b38SJeff Roberson 	for (;;) {
772ae7a6b38SJeff Roberson 		highcpu = 0;
773ae7a6b38SJeff Roberson 		highload = 0;
774ae7a6b38SJeff Roberson 		for (cpu = 0; cpu <= mp_maxid; cpu++) {
775ae7a6b38SJeff Roberson 			if (CPU_ABSENT(cpu))
776ae7a6b38SJeff Roberson 				continue;
7777b8bfa0dSJeff Roberson 			steal = TDQ_CPU(cpu);
77888f530ccSJeff Roberson 			if (steal->tdq_transferable == 0)
7797b8bfa0dSJeff Roberson 				continue;
78088f530ccSJeff Roberson 			if (steal->tdq_load < highload)
78188f530ccSJeff Roberson 				continue;
78288f530ccSJeff Roberson 			highload = steal->tdq_load;
783ae7a6b38SJeff Roberson 			highcpu = cpu;
784ae7a6b38SJeff Roberson 		}
78528994a58SJeff Roberson 		if (highload < steal_thresh)
786ae7a6b38SJeff Roberson 			break;
787ae7a6b38SJeff Roberson 		steal = TDQ_CPU(highcpu);
78888f530ccSJeff Roberson 		if (steal == tdq)
78988f530ccSJeff Roberson 			break;
7907fcf154aSJeff Roberson 		tdq_lock_pair(tdq, steal);
79188f530ccSJeff Roberson 		if (steal->tdq_load >= steal_thresh && steal->tdq_transferable)
7927b8bfa0dSJeff Roberson 			goto steal;
7937fcf154aSJeff Roberson 		tdq_unlock_pair(tdq, steal);
79480f86c9fSJeff Roberson 	}
795ae7a6b38SJeff Roberson 	spinlock_exit();
79680f86c9fSJeff Roberson 	return (1);
7977b8bfa0dSJeff Roberson steal:
798ae7a6b38SJeff Roberson 	spinlock_exit();
7997fcf154aSJeff Roberson 	tdq_move(steal, tdq);
800ae7a6b38SJeff Roberson 	TDQ_UNLOCK(steal);
801ae7a6b38SJeff Roberson 	mi_switch(SW_VOL, NULL);
802ae7a6b38SJeff Roberson 	thread_unlock(curthread);
8037b8bfa0dSJeff Roberson 
8047b8bfa0dSJeff Roberson 	return (0);
80522bf7d9aSJeff Roberson }
80622bf7d9aSJeff Roberson 
807ae7a6b38SJeff Roberson /*
808ae7a6b38SJeff Roberson  * Notify a remote cpu of new work.  Sends an IPI if criteria are met.
809ae7a6b38SJeff Roberson  */
81022bf7d9aSJeff Roberson static void
8117b8bfa0dSJeff Roberson tdq_notify(struct td_sched *ts)
81222bf7d9aSJeff Roberson {
813fc3a97dcSJeff Roberson 	struct thread *ctd;
81422bf7d9aSJeff Roberson 	struct pcpu *pcpu;
815fc3a97dcSJeff Roberson 	int cpri;
816fc3a97dcSJeff Roberson 	int pri;
8177b8bfa0dSJeff Roberson 	int cpu;
81822bf7d9aSJeff Roberson 
8197b8bfa0dSJeff Roberson 	cpu = ts->ts_cpu;
820fc3a97dcSJeff Roberson 	pri = ts->ts_thread->td_priority;
82122bf7d9aSJeff Roberson 	pcpu = pcpu_find(cpu);
822fc3a97dcSJeff Roberson 	ctd = pcpu->pc_curthread;
823fc3a97dcSJeff Roberson 	cpri = ctd->td_priority;
8246b2f763fSJeff Roberson 
8256b2f763fSJeff Roberson 	/*
8266b2f763fSJeff Roberson 	 * If our priority is not better than the current priority there is
8276b2f763fSJeff Roberson 	 * nothing to do.
8286b2f763fSJeff Roberson 	 */
829fc3a97dcSJeff Roberson 	if (pri > cpri)
8306b2f763fSJeff Roberson 		return;
8317b8bfa0dSJeff Roberson 	/*
832fc3a97dcSJeff Roberson 	 * Always IPI idle.
8337b8bfa0dSJeff Roberson 	 */
834fc3a97dcSJeff Roberson 	if (cpri > PRI_MIN_IDLE)
835fc3a97dcSJeff Roberson 		goto sendipi;
836fc3a97dcSJeff Roberson 	/*
837fc3a97dcSJeff Roberson 	 * If we're realtime or better and there is timeshare or worse running
838fc3a97dcSJeff Roberson 	 * send an IPI.
839fc3a97dcSJeff Roberson 	 */
840fc3a97dcSJeff Roberson 	if (pri < PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
841fc3a97dcSJeff Roberson 		goto sendipi;
842fc3a97dcSJeff Roberson 	/*
843fc3a97dcSJeff Roberson 	 * Otherwise only IPI if we exceed the threshold.
844fc3a97dcSJeff Roberson 	 */
845ae7a6b38SJeff Roberson 	if (pri > preempt_thresh)
8467b8bfa0dSJeff Roberson 		return;
847fc3a97dcSJeff Roberson sendipi:
848fc3a97dcSJeff Roberson 	ctd->td_flags |= TDF_NEEDRESCHED;
84914618990SJeff Roberson 	ipi_selected(1 << cpu, IPI_PREEMPT);
85022bf7d9aSJeff Roberson }
85122bf7d9aSJeff Roberson 
852ae7a6b38SJeff Roberson /*
853ae7a6b38SJeff Roberson  * Steals load from a timeshare queue.  Honors the rotating queue head
854ae7a6b38SJeff Roberson  * index.
855ae7a6b38SJeff Roberson  */
856ae7a6b38SJeff Roberson static struct td_sched *
857ae7a6b38SJeff Roberson runq_steal_from(struct runq *rq, u_char start)
858ae7a6b38SJeff Roberson {
859ae7a6b38SJeff Roberson 	struct td_sched *ts;
860ae7a6b38SJeff Roberson 	struct rqbits *rqb;
861ae7a6b38SJeff Roberson 	struct rqhead *rqh;
862ae7a6b38SJeff Roberson 	int first;
863ae7a6b38SJeff Roberson 	int bit;
864ae7a6b38SJeff Roberson 	int pri;
865ae7a6b38SJeff Roberson 	int i;
866ae7a6b38SJeff Roberson 
867ae7a6b38SJeff Roberson 	rqb = &rq->rq_status;
868ae7a6b38SJeff Roberson 	bit = start & (RQB_BPW -1);
869ae7a6b38SJeff Roberson 	pri = 0;
870ae7a6b38SJeff Roberson 	first = 0;
871ae7a6b38SJeff Roberson again:
872ae7a6b38SJeff Roberson 	for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
873ae7a6b38SJeff Roberson 		if (rqb->rqb_bits[i] == 0)
874ae7a6b38SJeff Roberson 			continue;
875ae7a6b38SJeff Roberson 		if (bit != 0) {
876ae7a6b38SJeff Roberson 			for (pri = bit; pri < RQB_BPW; pri++)
877ae7a6b38SJeff Roberson 				if (rqb->rqb_bits[i] & (1ul << pri))
878ae7a6b38SJeff Roberson 					break;
879ae7a6b38SJeff Roberson 			if (pri >= RQB_BPW)
880ae7a6b38SJeff Roberson 				continue;
881ae7a6b38SJeff Roberson 		} else
882ae7a6b38SJeff Roberson 			pri = RQB_FFS(rqb->rqb_bits[i]);
883ae7a6b38SJeff Roberson 		pri += (i << RQB_L2BPW);
884ae7a6b38SJeff Roberson 		rqh = &rq->rq_queues[pri];
885ae7a6b38SJeff Roberson 		TAILQ_FOREACH(ts, rqh, ts_procq) {
886ae7a6b38SJeff Roberson 			if (first && THREAD_CAN_MIGRATE(ts->ts_thread))
887ae7a6b38SJeff Roberson 				return (ts);
888ae7a6b38SJeff Roberson 			first = 1;
889ae7a6b38SJeff Roberson 		}
890ae7a6b38SJeff Roberson 	}
891ae7a6b38SJeff Roberson 	if (start != 0) {
892ae7a6b38SJeff Roberson 		start = 0;
893ae7a6b38SJeff Roberson 		goto again;
894ae7a6b38SJeff Roberson 	}
895ae7a6b38SJeff Roberson 
896ae7a6b38SJeff Roberson 	return (NULL);
897ae7a6b38SJeff Roberson }
898ae7a6b38SJeff Roberson 
899ae7a6b38SJeff Roberson /*
900ae7a6b38SJeff Roberson  * Steals load from a standard linear queue.
901ae7a6b38SJeff Roberson  */
902ad1e7d28SJulian Elischer static struct td_sched *
90322bf7d9aSJeff Roberson runq_steal(struct runq *rq)
90422bf7d9aSJeff Roberson {
90522bf7d9aSJeff Roberson 	struct rqhead *rqh;
90622bf7d9aSJeff Roberson 	struct rqbits *rqb;
907ad1e7d28SJulian Elischer 	struct td_sched *ts;
90822bf7d9aSJeff Roberson 	int word;
90922bf7d9aSJeff Roberson 	int bit;
91022bf7d9aSJeff Roberson 
91122bf7d9aSJeff Roberson 	rqb = &rq->rq_status;
91222bf7d9aSJeff Roberson 	for (word = 0; word < RQB_LEN; word++) {
91322bf7d9aSJeff Roberson 		if (rqb->rqb_bits[word] == 0)
91422bf7d9aSJeff Roberson 			continue;
91522bf7d9aSJeff Roberson 		for (bit = 0; bit < RQB_BPW; bit++) {
916a2640c9bSPeter Wemm 			if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
91722bf7d9aSJeff Roberson 				continue;
91822bf7d9aSJeff Roberson 			rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
91928994a58SJeff Roberson 			TAILQ_FOREACH(ts, rqh, ts_procq)
92028994a58SJeff Roberson 				if (THREAD_CAN_MIGRATE(ts->ts_thread))
921ad1e7d28SJulian Elischer 					return (ts);
92222bf7d9aSJeff Roberson 		}
92322bf7d9aSJeff Roberson 	}
92422bf7d9aSJeff Roberson 	return (NULL);
92522bf7d9aSJeff Roberson }
92622bf7d9aSJeff Roberson 
927ae7a6b38SJeff Roberson /*
928ae7a6b38SJeff Roberson  * Attempt to steal a thread in priority order from a thread queue.
929ae7a6b38SJeff Roberson  */
930ad1e7d28SJulian Elischer static struct td_sched *
9317fcf154aSJeff Roberson tdq_steal(struct tdq *tdq)
93222bf7d9aSJeff Roberson {
933ad1e7d28SJulian Elischer 	struct td_sched *ts;
93422bf7d9aSJeff Roberson 
935ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
936e7d50326SJeff Roberson 	if ((ts = runq_steal(&tdq->tdq_realtime)) != NULL)
937ad1e7d28SJulian Elischer 		return (ts);
938ae7a6b38SJeff Roberson 	if ((ts = runq_steal_from(&tdq->tdq_timeshare, tdq->tdq_ridx)) != NULL)
939ad1e7d28SJulian Elischer 		return (ts);
940d2ad694cSJeff Roberson 	return (runq_steal(&tdq->tdq_idle));
94122bf7d9aSJeff Roberson }
94280f86c9fSJeff Roberson 
943ae7a6b38SJeff Roberson /*
944ae7a6b38SJeff Roberson  * Sets the thread lock and ts_cpu to match the requested cpu.  Unlocks the
9457fcf154aSJeff Roberson  * current lock and returns with the assigned queue locked.
946ae7a6b38SJeff Roberson  */
947ae7a6b38SJeff Roberson static inline struct tdq *
948ae7a6b38SJeff Roberson sched_setcpu(struct td_sched *ts, int cpu, int flags)
94980f86c9fSJeff Roberson {
950ae7a6b38SJeff Roberson 	struct thread *td;
951ae7a6b38SJeff Roberson 	struct tdq *tdq;
95280f86c9fSJeff Roberson 
953ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
954ae7a6b38SJeff Roberson 
955ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpu);
956ae7a6b38SJeff Roberson 	td = ts->ts_thread;
957ae7a6b38SJeff Roberson 	ts->ts_cpu = cpu;
958c47f202bSJeff Roberson 
959c47f202bSJeff Roberson 	/* If the lock matches just return the queue. */
960ae7a6b38SJeff Roberson 	if (td->td_lock == TDQ_LOCKPTR(tdq))
961ae7a6b38SJeff Roberson 		return (tdq);
962ae7a6b38SJeff Roberson #ifdef notyet
96380f86c9fSJeff Roberson 	/*
964a5423ea3SJeff Roberson 	 * If the thread isn't running its lockptr is a
965ae7a6b38SJeff Roberson 	 * turnstile or a sleepqueue.  We can just lock_set without
966ae7a6b38SJeff Roberson 	 * blocking.
967670c524fSJeff Roberson 	 */
968ae7a6b38SJeff Roberson 	if (TD_CAN_RUN(td)) {
969ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
970ae7a6b38SJeff Roberson 		thread_lock_set(td, TDQ_LOCKPTR(tdq));
971ae7a6b38SJeff Roberson 		return (tdq);
972ae7a6b38SJeff Roberson 	}
973ae7a6b38SJeff Roberson #endif
97480f86c9fSJeff Roberson 	/*
975ae7a6b38SJeff Roberson 	 * The hard case, migration, we need to block the thread first to
976ae7a6b38SJeff Roberson 	 * prevent order reversals with other cpus locks.
9777b8bfa0dSJeff Roberson 	 */
978ae7a6b38SJeff Roberson 	thread_lock_block(td);
979ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
980ae7a6b38SJeff Roberson 	thread_lock_unblock(td, TDQ_LOCKPTR(tdq));
981ae7a6b38SJeff Roberson 	return (tdq);
98280f86c9fSJeff Roberson }
9832454aaf5SJeff Roberson 
984ae7a6b38SJeff Roberson /*
985ae7a6b38SJeff Roberson  * Find the thread queue running the lowest priority thread.
986ae7a6b38SJeff Roberson  */
9877b8bfa0dSJeff Roberson static int
988ae7a6b38SJeff Roberson tdq_lowestpri(void)
9897b8bfa0dSJeff Roberson {
990ae7a6b38SJeff Roberson 	struct tdq *tdq;
9917b8bfa0dSJeff Roberson 	int lowpri;
9927b8bfa0dSJeff Roberson 	int lowcpu;
9937b8bfa0dSJeff Roberson 	int lowload;
9947b8bfa0dSJeff Roberson 	int load;
995ae7a6b38SJeff Roberson 	int cpu;
996ae7a6b38SJeff Roberson 	int pri;
997ae7a6b38SJeff Roberson 
998ae7a6b38SJeff Roberson 	lowload = 0;
999ae7a6b38SJeff Roberson 	lowpri = lowcpu = 0;
1000ae7a6b38SJeff Roberson 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
1001ae7a6b38SJeff Roberson 		if (CPU_ABSENT(cpu))
1002ae7a6b38SJeff Roberson 			continue;
1003ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(cpu);
1004ae7a6b38SJeff Roberson 		pri = tdq->tdq_lowpri;
1005ae7a6b38SJeff Roberson 		load = TDQ_CPU(cpu)->tdq_load;
1006ae7a6b38SJeff Roberson 		CTR4(KTR_ULE,
1007ae7a6b38SJeff Roberson 		    "cpu %d pri %d lowcpu %d lowpri %d",
1008ae7a6b38SJeff Roberson 		    cpu, pri, lowcpu, lowpri);
1009ae7a6b38SJeff Roberson 		if (pri < lowpri)
1010ae7a6b38SJeff Roberson 			continue;
1011ae7a6b38SJeff Roberson 		if (lowpri && lowpri == pri && load > lowload)
1012ae7a6b38SJeff Roberson 			continue;
1013ae7a6b38SJeff Roberson 		lowpri = pri;
1014ae7a6b38SJeff Roberson 		lowcpu = cpu;
1015ae7a6b38SJeff Roberson 		lowload = load;
1016ae7a6b38SJeff Roberson 	}
1017ae7a6b38SJeff Roberson 
1018ae7a6b38SJeff Roberson 	return (lowcpu);
1019ae7a6b38SJeff Roberson }
1020ae7a6b38SJeff Roberson 
1021ae7a6b38SJeff Roberson /*
1022ae7a6b38SJeff Roberson  * Find the thread queue with the least load.
1023ae7a6b38SJeff Roberson  */
1024ae7a6b38SJeff Roberson static int
1025ae7a6b38SJeff Roberson tdq_lowestload(void)
1026ae7a6b38SJeff Roberson {
1027ae7a6b38SJeff Roberson 	struct tdq *tdq;
1028ae7a6b38SJeff Roberson 	int lowload;
1029ae7a6b38SJeff Roberson 	int lowpri;
1030ae7a6b38SJeff Roberson 	int lowcpu;
1031ae7a6b38SJeff Roberson 	int load;
1032ae7a6b38SJeff Roberson 	int cpu;
1033ae7a6b38SJeff Roberson 	int pri;
1034ae7a6b38SJeff Roberson 
1035ae7a6b38SJeff Roberson 	lowcpu = 0;
1036ae7a6b38SJeff Roberson 	lowload = TDQ_CPU(0)->tdq_load;
1037ae7a6b38SJeff Roberson 	lowpri = TDQ_CPU(0)->tdq_lowpri;
1038ae7a6b38SJeff Roberson 	for (cpu = 1; cpu <= mp_maxid; cpu++) {
1039ae7a6b38SJeff Roberson 		if (CPU_ABSENT(cpu))
1040ae7a6b38SJeff Roberson 			continue;
1041ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(cpu);
1042ae7a6b38SJeff Roberson 		load = tdq->tdq_load;
1043ae7a6b38SJeff Roberson 		pri = tdq->tdq_lowpri;
1044ae7a6b38SJeff Roberson 		CTR4(KTR_ULE, "cpu %d load %d lowcpu %d lowload %d",
1045ae7a6b38SJeff Roberson 		    cpu, load, lowcpu, lowload);
1046ae7a6b38SJeff Roberson 		if (load > lowload)
1047ae7a6b38SJeff Roberson 			continue;
1048ae7a6b38SJeff Roberson 		if (load == lowload && pri < lowpri)
1049ae7a6b38SJeff Roberson 			continue;
1050ae7a6b38SJeff Roberson 		lowcpu = cpu;
1051ae7a6b38SJeff Roberson 		lowload = load;
1052ae7a6b38SJeff Roberson 		lowpri = pri;
1053ae7a6b38SJeff Roberson 	}
1054ae7a6b38SJeff Roberson 
1055ae7a6b38SJeff Roberson 	return (lowcpu);
1056ae7a6b38SJeff Roberson }
1057ae7a6b38SJeff Roberson 
1058ae7a6b38SJeff Roberson /*
1059ae7a6b38SJeff Roberson  * Pick the destination cpu for sched_add().  Respects affinity and makes
1060ae7a6b38SJeff Roberson  * a determination based on load or priority of available processors.
1061ae7a6b38SJeff Roberson  */
1062ae7a6b38SJeff Roberson static int
1063ae7a6b38SJeff Roberson sched_pickcpu(struct td_sched *ts, int flags)
1064ae7a6b38SJeff Roberson {
1065ae7a6b38SJeff Roberson 	struct tdq *tdq;
10667b8bfa0dSJeff Roberson 	int self;
10677b8bfa0dSJeff Roberson 	int pri;
10687b8bfa0dSJeff Roberson 	int cpu;
10697b8bfa0dSJeff Roberson 
1070ae7a6b38SJeff Roberson 	cpu = self = PCPU_GET(cpuid);
10717b8bfa0dSJeff Roberson 	if (smp_started == 0)
10727b8bfa0dSJeff Roberson 		return (self);
107328994a58SJeff Roberson 	/*
107428994a58SJeff Roberson 	 * Don't migrate a running thread from sched_switch().
107528994a58SJeff Roberson 	 */
107628994a58SJeff Roberson 	if (flags & SRQ_OURSELF) {
107728994a58SJeff Roberson 		CTR1(KTR_ULE, "YIELDING %d",
107828994a58SJeff Roberson 		    curthread->td_priority);
107928994a58SJeff Roberson 		return (self);
108028994a58SJeff Roberson 	}
10817b8bfa0dSJeff Roberson 	pri = ts->ts_thread->td_priority;
1082ae7a6b38SJeff Roberson 	cpu = ts->ts_cpu;
10837b8bfa0dSJeff Roberson 	/*
10847b8bfa0dSJeff Roberson 	 * Regardless of affinity, if the last cpu is idle send it there.
10857b8bfa0dSJeff Roberson 	 */
1086ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpu);
1087ae7a6b38SJeff Roberson 	if (tdq->tdq_lowpri > PRI_MIN_IDLE) {
108814618990SJeff Roberson 		CTR5(KTR_ULE,
10897b8bfa0dSJeff Roberson 		    "ts_cpu %d idle, ltick %d ticks %d pri %d curthread %d",
10907b8bfa0dSJeff Roberson 		    ts->ts_cpu, ts->ts_rltick, ticks, pri,
1091ae7a6b38SJeff Roberson 		    tdq->tdq_lowpri);
10927b8bfa0dSJeff Roberson 		return (ts->ts_cpu);
10937b8bfa0dSJeff Roberson 	}
10947b8bfa0dSJeff Roberson 	/*
10957b8bfa0dSJeff Roberson 	 * If we have affinity, try to place it on the cpu we last ran on.
10967b8bfa0dSJeff Roberson 	 */
1097ae7a6b38SJeff Roberson 	if (SCHED_AFFINITY(ts) && tdq->tdq_lowpri > pri) {
109814618990SJeff Roberson 		CTR5(KTR_ULE,
10997b8bfa0dSJeff Roberson 		    "affinity for %d, ltick %d ticks %d pri %d curthread %d",
11007b8bfa0dSJeff Roberson 		    ts->ts_cpu, ts->ts_rltick, ticks, pri,
1101ae7a6b38SJeff Roberson 		    tdq->tdq_lowpri);
11027b8bfa0dSJeff Roberson 		return (ts->ts_cpu);
11037b8bfa0dSJeff Roberson 	}
11047b8bfa0dSJeff Roberson 	/*
11057b8bfa0dSJeff Roberson 	 * Look for an idle group.
11067b8bfa0dSJeff Roberson 	 */
110714618990SJeff Roberson 	CTR1(KTR_ULE, "tdq_idle %X", tdq_idle);
11087b8bfa0dSJeff Roberson 	cpu = ffs(tdq_idle);
11097b8bfa0dSJeff Roberson 	if (cpu)
1110ae7a6b38SJeff Roberson 		return (--cpu);
111128994a58SJeff Roberson 	/*
11127fcf154aSJeff Roberson 	 * If there are no idle cores see if we can run the thread locally.
11137fcf154aSJeff Roberson 	 * This may improve locality among sleepers and wakers when there
11147fcf154aSJeff Roberson 	 * is shared data.
111528994a58SJeff Roberson 	 */
1116a755f214SJeff Roberson 	if (tryself && pri < TDQ_CPU(self)->tdq_lowpri) {
111728994a58SJeff Roberson 		CTR1(KTR_ULE, "tryself %d",
11187b8bfa0dSJeff Roberson 		    curthread->td_priority);
11197b8bfa0dSJeff Roberson 		return (self);
11207b8bfa0dSJeff Roberson 	}
11217b8bfa0dSJeff Roberson 	/*
11227b8bfa0dSJeff Roberson  	 * Now search for the cpu running the lowest priority thread with
11237b8bfa0dSJeff Roberson 	 * the least load.
11247b8bfa0dSJeff Roberson 	 */
1125ae7a6b38SJeff Roberson 	if (pick_pri)
1126ae7a6b38SJeff Roberson 		cpu = tdq_lowestpri();
1127ae7a6b38SJeff Roberson 	else
1128ae7a6b38SJeff Roberson 		cpu = tdq_lowestload();
1129ae7a6b38SJeff Roberson 	return (cpu);
113080f86c9fSJeff Roberson }
113180f86c9fSJeff Roberson 
113222bf7d9aSJeff Roberson #endif	/* SMP */
113322bf7d9aSJeff Roberson 
113422bf7d9aSJeff Roberson /*
113522bf7d9aSJeff Roberson  * Pick the highest priority task we have and return it.
11360c0a98b2SJeff Roberson  */
1137ad1e7d28SJulian Elischer static struct td_sched *
1138ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq)
11395d7ef00cSJeff Roberson {
1140ad1e7d28SJulian Elischer 	struct td_sched *ts;
11415d7ef00cSJeff Roberson 
1142ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
1143e7d50326SJeff Roberson 	ts = runq_choose(&tdq->tdq_realtime);
1144dda713dfSJeff Roberson 	if (ts != NULL)
1145e7d50326SJeff Roberson 		return (ts);
11463f872f85SJeff Roberson 	ts = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
1147e7d50326SJeff Roberson 	if (ts != NULL) {
1148dda713dfSJeff Roberson 		KASSERT(ts->ts_thread->td_priority >= PRI_MIN_TIMESHARE,
1149e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on timeshare queue %d",
1150e7d50326SJeff Roberson 		    ts->ts_thread->td_priority));
1151ad1e7d28SJulian Elischer 		return (ts);
115215dc847eSJeff Roberson 	}
115315dc847eSJeff Roberson 
1154e7d50326SJeff Roberson 	ts = runq_choose(&tdq->tdq_idle);
1155e7d50326SJeff Roberson 	if (ts != NULL) {
1156e7d50326SJeff Roberson 		KASSERT(ts->ts_thread->td_priority >= PRI_MIN_IDLE,
1157e7d50326SJeff Roberson 		    ("tdq_choose: Invalid priority on idle queue %d",
1158e7d50326SJeff Roberson 		    ts->ts_thread->td_priority));
1159e7d50326SJeff Roberson 		return (ts);
1160e7d50326SJeff Roberson 	}
1161e7d50326SJeff Roberson 
1162e7d50326SJeff Roberson 	return (NULL);
1163245f3abfSJeff Roberson }
11640a016a05SJeff Roberson 
1165ae7a6b38SJeff Roberson /*
1166ae7a6b38SJeff Roberson  * Initialize a thread queue.
1167ae7a6b38SJeff Roberson  */
11680a016a05SJeff Roberson static void
1169ad1e7d28SJulian Elischer tdq_setup(struct tdq *tdq)
11700a016a05SJeff Roberson {
1171ae7a6b38SJeff Roberson 
1172c47f202bSJeff Roberson 	if (bootverbose)
1173c47f202bSJeff Roberson 		printf("ULE: setup cpu %d\n", TDQ_ID(tdq));
1174e7d50326SJeff Roberson 	runq_init(&tdq->tdq_realtime);
1175e7d50326SJeff Roberson 	runq_init(&tdq->tdq_timeshare);
1176d2ad694cSJeff Roberson 	runq_init(&tdq->tdq_idle);
1177d2ad694cSJeff Roberson 	tdq->tdq_load = 0;
11780a016a05SJeff Roberson }
11790a016a05SJeff Roberson 
1180c47f202bSJeff Roberson #ifdef SMP
1181c47f202bSJeff Roberson static void
1182c47f202bSJeff Roberson tdg_setup(struct tdq_group *tdg)
1183c47f202bSJeff Roberson {
1184c47f202bSJeff Roberson 	if (bootverbose)
1185c47f202bSJeff Roberson 		printf("ULE: setup cpu group %d\n", TDG_ID(tdg));
1186c47f202bSJeff Roberson 	snprintf(tdg->tdg_name, sizeof(tdg->tdg_name),
1187c47f202bSJeff Roberson 	    "sched lock %d", (int)TDG_ID(tdg));
1188c47f202bSJeff Roberson 	mtx_init(&tdg->tdg_lock, tdg->tdg_name, "sched lock",
1189c47f202bSJeff Roberson 	    MTX_SPIN | MTX_RECURSE);
1190c47f202bSJeff Roberson 	LIST_INIT(&tdg->tdg_members);
1191c47f202bSJeff Roberson 	tdg->tdg_load = 0;
1192c47f202bSJeff Roberson 	tdg->tdg_transferable = 0;
1193c47f202bSJeff Roberson 	tdg->tdg_cpus = 0;
1194c47f202bSJeff Roberson 	tdg->tdg_mask = 0;
1195c47f202bSJeff Roberson 	tdg->tdg_cpumask = 0;
1196c47f202bSJeff Roberson 	tdg->tdg_idlemask = 0;
1197c47f202bSJeff Roberson }
1198c47f202bSJeff Roberson 
1199c47f202bSJeff Roberson static void
1200c47f202bSJeff Roberson tdg_add(struct tdq_group *tdg, struct tdq *tdq)
1201c47f202bSJeff Roberson {
1202c47f202bSJeff Roberson 	if (tdg->tdg_mask == 0)
1203c47f202bSJeff Roberson 		tdg->tdg_mask |= 1 << TDQ_ID(tdq);
1204c47f202bSJeff Roberson 	tdg->tdg_cpumask |= 1 << TDQ_ID(tdq);
1205c47f202bSJeff Roberson 	tdg->tdg_cpus++;
1206c47f202bSJeff Roberson 	tdq->tdq_group = tdg;
1207c47f202bSJeff Roberson 	tdq->tdq_lock = &tdg->tdg_lock;
1208c47f202bSJeff Roberson 	LIST_INSERT_HEAD(&tdg->tdg_members, tdq, tdq_siblings);
1209c47f202bSJeff Roberson 	if (bootverbose)
1210c47f202bSJeff Roberson 		printf("ULE: adding cpu %d to group %d: cpus %d mask 0x%X\n",
1211c47f202bSJeff Roberson 		    TDQ_ID(tdq), TDG_ID(tdg), tdg->tdg_cpus, tdg->tdg_cpumask);
1212c47f202bSJeff Roberson }
1213c47f202bSJeff Roberson 
1214c47f202bSJeff Roberson static void
1215c47f202bSJeff Roberson sched_setup_topology(void)
1216c47f202bSJeff Roberson {
1217c47f202bSJeff Roberson 	struct tdq_group *tdg;
1218c47f202bSJeff Roberson 	struct cpu_group *cg;
1219c47f202bSJeff Roberson 	int balance_groups;
1220c47f202bSJeff Roberson 	struct tdq *tdq;
1221c47f202bSJeff Roberson 	int i;
1222c47f202bSJeff Roberson 	int j;
1223c47f202bSJeff Roberson 
1224c47f202bSJeff Roberson 	topology = 1;
1225c47f202bSJeff Roberson 	balance_groups = 0;
1226c47f202bSJeff Roberson 	for (i = 0; i < smp_topology->ct_count; i++) {
1227c47f202bSJeff Roberson 		cg = &smp_topology->ct_group[i];
1228c47f202bSJeff Roberson 		tdg = &tdq_groups[i];
1229c47f202bSJeff Roberson 		/*
1230c47f202bSJeff Roberson 		 * Initialize the group.
1231c47f202bSJeff Roberson 		 */
1232c47f202bSJeff Roberson 		tdg_setup(tdg);
1233c47f202bSJeff Roberson 		/*
1234c47f202bSJeff Roberson 		 * Find all of the group members and add them.
1235c47f202bSJeff Roberson 		 */
1236c47f202bSJeff Roberson 		for (j = 0; j < MAXCPU; j++) {
1237c47f202bSJeff Roberson 			if ((cg->cg_mask & (1 << j)) != 0) {
1238c47f202bSJeff Roberson 				tdq = TDQ_CPU(j);
1239c47f202bSJeff Roberson 				tdq_setup(tdq);
1240c47f202bSJeff Roberson 				tdg_add(tdg, tdq);
1241c47f202bSJeff Roberson 			}
1242c47f202bSJeff Roberson 		}
1243c47f202bSJeff Roberson 		if (tdg->tdg_cpus > 1)
1244c47f202bSJeff Roberson 			balance_groups = 1;
1245c47f202bSJeff Roberson 	}
1246c47f202bSJeff Roberson 	tdg_maxid = smp_topology->ct_count - 1;
1247c47f202bSJeff Roberson 	if (balance_groups)
12487fcf154aSJeff Roberson 		sched_balance_groups();
1249c47f202bSJeff Roberson }
1250c47f202bSJeff Roberson 
1251c47f202bSJeff Roberson static void
1252c47f202bSJeff Roberson sched_setup_smp(void)
1253c47f202bSJeff Roberson {
1254c47f202bSJeff Roberson 	struct tdq_group *tdg;
1255c47f202bSJeff Roberson 	struct tdq *tdq;
1256c47f202bSJeff Roberson 	int cpus;
1257c47f202bSJeff Roberson 	int i;
1258c47f202bSJeff Roberson 
1259c47f202bSJeff Roberson 	for (cpus = 0, i = 0; i < MAXCPU; i++) {
1260c47f202bSJeff Roberson 		if (CPU_ABSENT(i))
1261c47f202bSJeff Roberson 			continue;
1262c47f202bSJeff Roberson 		tdq = &tdq_cpu[i];
1263c47f202bSJeff Roberson 		tdg = &tdq_groups[i];
1264c47f202bSJeff Roberson 		/*
1265c47f202bSJeff Roberson 		 * Setup a tdq group with one member.
1266c47f202bSJeff Roberson 		 */
1267c47f202bSJeff Roberson 		tdg_setup(tdg);
1268c47f202bSJeff Roberson 		tdq_setup(tdq);
1269c47f202bSJeff Roberson 		tdg_add(tdg, tdq);
1270c47f202bSJeff Roberson 		cpus++;
1271c47f202bSJeff Roberson 	}
1272c47f202bSJeff Roberson 	tdg_maxid = cpus - 1;
1273c47f202bSJeff Roberson }
1274c47f202bSJeff Roberson 
1275c47f202bSJeff Roberson /*
1276c47f202bSJeff Roberson  * Fake a topology with one group containing all CPUs.
1277c47f202bSJeff Roberson  */
1278c47f202bSJeff Roberson static void
1279c47f202bSJeff Roberson sched_fake_topo(void)
1280c47f202bSJeff Roberson {
1281c47f202bSJeff Roberson #ifdef SCHED_FAKE_TOPOLOGY
1282c47f202bSJeff Roberson 	static struct cpu_top top;
1283c47f202bSJeff Roberson 	static struct cpu_group group;
1284c47f202bSJeff Roberson 
1285c47f202bSJeff Roberson 	top.ct_count = 1;
1286c47f202bSJeff Roberson 	top.ct_group = &group;
1287c47f202bSJeff Roberson 	group.cg_mask = all_cpus;
1288c47f202bSJeff Roberson 	group.cg_count = mp_ncpus;
1289c47f202bSJeff Roberson 	group.cg_children = 0;
1290c47f202bSJeff Roberson 	smp_topology = &top;
1291c47f202bSJeff Roberson #endif
1292c47f202bSJeff Roberson }
1293c47f202bSJeff Roberson #endif
1294c47f202bSJeff Roberson 
1295ae7a6b38SJeff Roberson /*
1296ae7a6b38SJeff Roberson  * Setup the thread queues and initialize the topology based on MD
1297ae7a6b38SJeff Roberson  * information.
1298ae7a6b38SJeff Roberson  */
129935e6168fSJeff Roberson static void
130035e6168fSJeff Roberson sched_setup(void *dummy)
130135e6168fSJeff Roberson {
1302ae7a6b38SJeff Roberson 	struct tdq *tdq;
1303c47f202bSJeff Roberson 
1304c47f202bSJeff Roberson 	tdq = TDQ_SELF();
13050ec896fdSJeff Roberson #ifdef SMP
1306c47f202bSJeff Roberson 	sched_fake_topo();
1307c47f202bSJeff Roberson 	/*
1308c47f202bSJeff Roberson 	 * Setup tdqs based on a topology configuration or vanilla SMP based
1309c47f202bSJeff Roberson 	 * on mp_maxid.
1310c47f202bSJeff Roberson 	 */
1311c47f202bSJeff Roberson 	if (smp_topology == NULL)
1312c47f202bSJeff Roberson 		sched_setup_smp();
1313c47f202bSJeff Roberson 	else
1314c47f202bSJeff Roberson 		sched_setup_topology();
13157fcf154aSJeff Roberson 	balance_tdq = tdq;
13167fcf154aSJeff Roberson 	sched_balance();
1317749d01b0SJeff Roberson #else
1318c47f202bSJeff Roberson 	tdq_setup(tdq);
1319c47f202bSJeff Roberson 	mtx_init(&tdq_lock, "sched lock", "sched lock", MTX_SPIN | MTX_RECURSE);
1320c47f202bSJeff Roberson 	tdq->tdq_lock = &tdq_lock;
1321356500a3SJeff Roberson #endif
1322ae7a6b38SJeff Roberson 	/*
1323ae7a6b38SJeff Roberson 	 * To avoid divide-by-zero, we set realstathz a dummy value
1324ae7a6b38SJeff Roberson 	 * in case which sched_clock() called before sched_initticks().
1325ae7a6b38SJeff Roberson 	 */
1326ae7a6b38SJeff Roberson 	realstathz = hz;
1327ae7a6b38SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1328ae7a6b38SJeff Roberson 	tickincr = 1 << SCHED_TICK_SHIFT;
1329ae7a6b38SJeff Roberson 
1330ae7a6b38SJeff Roberson 	/* Add thread0's load since it's running. */
1331ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
1332c47f202bSJeff Roberson 	thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF());
1333ae7a6b38SJeff Roberson 	tdq_load_add(tdq, &td_sched0);
1334ae7a6b38SJeff Roberson 	TDQ_UNLOCK(tdq);
133535e6168fSJeff Roberson }
133635e6168fSJeff Roberson 
1337ae7a6b38SJeff Roberson /*
1338ae7a6b38SJeff Roberson  * This routine determines the tickincr after stathz and hz are setup.
1339ae7a6b38SJeff Roberson  */
1340a1d4fe69SDavid Xu /* ARGSUSED */
1341a1d4fe69SDavid Xu static void
1342a1d4fe69SDavid Xu sched_initticks(void *dummy)
1343a1d4fe69SDavid Xu {
1344ae7a6b38SJeff Roberson 	int incr;
1345ae7a6b38SJeff Roberson 
1346a1d4fe69SDavid Xu 	realstathz = stathz ? stathz : hz;
134714618990SJeff Roberson 	sched_slice = (realstathz/10);	/* ~100ms */
1348a1d4fe69SDavid Xu 
1349a1d4fe69SDavid Xu 	/*
1350e7d50326SJeff Roberson 	 * tickincr is shifted out by 10 to avoid rounding errors due to
13513f872f85SJeff Roberson 	 * hz not being evenly divisible by stathz on all platforms.
1352e7d50326SJeff Roberson 	 */
1353ae7a6b38SJeff Roberson 	incr = (hz << SCHED_TICK_SHIFT) / realstathz;
1354e7d50326SJeff Roberson 	/*
1355e7d50326SJeff Roberson 	 * This does not work for values of stathz that are more than
1356e7d50326SJeff Roberson 	 * 1 << SCHED_TICK_SHIFT * hz.  In practice this does not happen.
1357a1d4fe69SDavid Xu 	 */
1358ae7a6b38SJeff Roberson 	if (incr == 0)
1359ae7a6b38SJeff Roberson 		incr = 1;
1360ae7a6b38SJeff Roberson 	tickincr = incr;
13617b8bfa0dSJeff Roberson #ifdef SMP
13629862717aSJeff Roberson 	/*
13637fcf154aSJeff Roberson 	 * Set the default balance interval now that we know
13647fcf154aSJeff Roberson 	 * what realstathz is.
13657fcf154aSJeff Roberson 	 */
13667fcf154aSJeff Roberson 	balance_interval = realstathz;
13677fcf154aSJeff Roberson 	/*
13689862717aSJeff Roberson 	 * Set steal thresh to log2(mp_ncpu) but no greater than 4.  This
13699862717aSJeff Roberson 	 * prevents excess thrashing on large machines and excess idle on
13709862717aSJeff Roberson 	 * smaller machines.
13719862717aSJeff Roberson 	 */
13729862717aSJeff Roberson 	steal_thresh = min(ffs(mp_ncpus) - 1, 4);
13737b8bfa0dSJeff Roberson 	affinity = SCHED_AFFINITY_DEFAULT;
13747b8bfa0dSJeff Roberson #endif
1375a1d4fe69SDavid Xu }
1376a1d4fe69SDavid Xu 
1377a1d4fe69SDavid Xu 
137835e6168fSJeff Roberson /*
1379ae7a6b38SJeff Roberson  * This is the core of the interactivity algorithm.  Determines a score based
1380ae7a6b38SJeff Roberson  * on past behavior.  It is the ratio of sleep time to run time scaled to
1381ae7a6b38SJeff Roberson  * a [0, 100] integer.  This is the voluntary sleep time of a process, which
1382ae7a6b38SJeff Roberson  * differs from the cpu usage because it does not account for time spent
1383ae7a6b38SJeff Roberson  * waiting on a run-queue.  Would be prettier if we had floating point.
1384ae7a6b38SJeff Roberson  */
1385ae7a6b38SJeff Roberson static int
1386ae7a6b38SJeff Roberson sched_interact_score(struct thread *td)
1387ae7a6b38SJeff Roberson {
1388ae7a6b38SJeff Roberson 	struct td_sched *ts;
1389ae7a6b38SJeff Roberson 	int div;
1390ae7a6b38SJeff Roberson 
1391ae7a6b38SJeff Roberson 	ts = td->td_sched;
1392ae7a6b38SJeff Roberson 	/*
1393ae7a6b38SJeff Roberson 	 * The score is only needed if this is likely to be an interactive
1394ae7a6b38SJeff Roberson 	 * task.  Don't go through the expense of computing it if there's
1395ae7a6b38SJeff Roberson 	 * no chance.
1396ae7a6b38SJeff Roberson 	 */
1397ae7a6b38SJeff Roberson 	if (sched_interact <= SCHED_INTERACT_HALF &&
1398ae7a6b38SJeff Roberson 		ts->ts_runtime >= ts->ts_slptime)
1399ae7a6b38SJeff Roberson 			return (SCHED_INTERACT_HALF);
1400ae7a6b38SJeff Roberson 
1401ae7a6b38SJeff Roberson 	if (ts->ts_runtime > ts->ts_slptime) {
1402ae7a6b38SJeff Roberson 		div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF);
1403ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF +
1404ae7a6b38SJeff Roberson 		    (SCHED_INTERACT_HALF - (ts->ts_slptime / div)));
1405ae7a6b38SJeff Roberson 	}
1406ae7a6b38SJeff Roberson 	if (ts->ts_slptime > ts->ts_runtime) {
1407ae7a6b38SJeff Roberson 		div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF);
1408ae7a6b38SJeff Roberson 		return (ts->ts_runtime / div);
1409ae7a6b38SJeff Roberson 	}
1410ae7a6b38SJeff Roberson 	/* runtime == slptime */
1411ae7a6b38SJeff Roberson 	if (ts->ts_runtime)
1412ae7a6b38SJeff Roberson 		return (SCHED_INTERACT_HALF);
1413ae7a6b38SJeff Roberson 
1414ae7a6b38SJeff Roberson 	/*
1415ae7a6b38SJeff Roberson 	 * This can happen if slptime and runtime are 0.
1416ae7a6b38SJeff Roberson 	 */
1417ae7a6b38SJeff Roberson 	return (0);
1418ae7a6b38SJeff Roberson 
1419ae7a6b38SJeff Roberson }
1420ae7a6b38SJeff Roberson 
1421ae7a6b38SJeff Roberson /*
142235e6168fSJeff Roberson  * Scale the scheduling priority according to the "interactivity" of this
142335e6168fSJeff Roberson  * process.
142435e6168fSJeff Roberson  */
142515dc847eSJeff Roberson static void
14268460a577SJohn Birrell sched_priority(struct thread *td)
142735e6168fSJeff Roberson {
1428e7d50326SJeff Roberson 	int score;
142935e6168fSJeff Roberson 	int pri;
143035e6168fSJeff Roberson 
14318460a577SJohn Birrell 	if (td->td_pri_class != PRI_TIMESHARE)
143215dc847eSJeff Roberson 		return;
1433e7d50326SJeff Roberson 	/*
1434e7d50326SJeff Roberson 	 * If the score is interactive we place the thread in the realtime
1435e7d50326SJeff Roberson 	 * queue with a priority that is less than kernel and interrupt
1436e7d50326SJeff Roberson 	 * priorities.  These threads are not subject to nice restrictions.
1437e7d50326SJeff Roberson 	 *
1438ae7a6b38SJeff Roberson 	 * Scores greater than this are placed on the normal timeshare queue
1439e7d50326SJeff Roberson 	 * where the priority is partially decided by the most recent cpu
1440e7d50326SJeff Roberson 	 * utilization and the rest is decided by nice value.
1441a5423ea3SJeff Roberson 	 *
1442a5423ea3SJeff Roberson 	 * The nice value of the process has a linear effect on the calculated
1443a5423ea3SJeff Roberson 	 * score.  Negative nice values make it easier for a thread to be
1444a5423ea3SJeff Roberson 	 * considered interactive.
1445e7d50326SJeff Roberson 	 */
1446e270652bSJeff Roberson 	score = imax(0, sched_interact_score(td) - td->td_proc->p_nice);
1447e7d50326SJeff Roberson 	if (score < sched_interact) {
1448e7d50326SJeff Roberson 		pri = PRI_MIN_REALTIME;
1449e7d50326SJeff Roberson 		pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
1450e7d50326SJeff Roberson 		    * score;
1451e7d50326SJeff Roberson 		KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
14529a93305aSJeff Roberson 		    ("sched_priority: invalid interactive priority %d score %d",
14539a93305aSJeff Roberson 		    pri, score));
1454e7d50326SJeff Roberson 	} else {
1455e7d50326SJeff Roberson 		pri = SCHED_PRI_MIN;
1456e7d50326SJeff Roberson 		if (td->td_sched->ts_ticks)
1457e7d50326SJeff Roberson 			pri += SCHED_PRI_TICKS(td->td_sched);
1458e7d50326SJeff Roberson 		pri += SCHED_PRI_NICE(td->td_proc->p_nice);
1459ae7a6b38SJeff Roberson 		KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE,
1460ae7a6b38SJeff Roberson 		    ("sched_priority: invalid priority %d: nice %d, "
1461ae7a6b38SJeff Roberson 		    "ticks %d ftick %d ltick %d tick pri %d",
1462ae7a6b38SJeff Roberson 		    pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
1463ae7a6b38SJeff Roberson 		    td->td_sched->ts_ftick, td->td_sched->ts_ltick,
1464ae7a6b38SJeff Roberson 		    SCHED_PRI_TICKS(td->td_sched)));
1465e7d50326SJeff Roberson 	}
14668460a577SJohn Birrell 	sched_user_prio(td, pri);
146735e6168fSJeff Roberson 
146815dc847eSJeff Roberson 	return;
146935e6168fSJeff Roberson }
147035e6168fSJeff Roberson 
147135e6168fSJeff Roberson /*
1472d322132cSJeff Roberson  * This routine enforces a maximum limit on the amount of scheduling history
1473ae7a6b38SJeff Roberson  * kept.  It is called after either the slptime or runtime is adjusted.  This
1474ae7a6b38SJeff Roberson  * function is ugly due to integer math.
1475d322132cSJeff Roberson  */
14764b60e324SJeff Roberson static void
14778460a577SJohn Birrell sched_interact_update(struct thread *td)
14784b60e324SJeff Roberson {
1479155b6ca1SJeff Roberson 	struct td_sched *ts;
14809a93305aSJeff Roberson 	u_int sum;
14813f741ca1SJeff Roberson 
1482155b6ca1SJeff Roberson 	ts = td->td_sched;
1483ae7a6b38SJeff Roberson 	sum = ts->ts_runtime + ts->ts_slptime;
1484d322132cSJeff Roberson 	if (sum < SCHED_SLP_RUN_MAX)
1485d322132cSJeff Roberson 		return;
1486d322132cSJeff Roberson 	/*
1487155b6ca1SJeff Roberson 	 * This only happens from two places:
1488155b6ca1SJeff Roberson 	 * 1) We have added an unusual amount of run time from fork_exit.
1489155b6ca1SJeff Roberson 	 * 2) We have added an unusual amount of sleep time from sched_sleep().
1490155b6ca1SJeff Roberson 	 */
1491155b6ca1SJeff Roberson 	if (sum > SCHED_SLP_RUN_MAX * 2) {
1492ae7a6b38SJeff Roberson 		if (ts->ts_runtime > ts->ts_slptime) {
1493ae7a6b38SJeff Roberson 			ts->ts_runtime = SCHED_SLP_RUN_MAX;
1494ae7a6b38SJeff Roberson 			ts->ts_slptime = 1;
1495155b6ca1SJeff Roberson 		} else {
1496ae7a6b38SJeff Roberson 			ts->ts_slptime = SCHED_SLP_RUN_MAX;
1497ae7a6b38SJeff Roberson 			ts->ts_runtime = 1;
1498155b6ca1SJeff Roberson 		}
1499155b6ca1SJeff Roberson 		return;
1500155b6ca1SJeff Roberson 	}
1501155b6ca1SJeff Roberson 	/*
1502d322132cSJeff Roberson 	 * If we have exceeded by more than 1/5th then the algorithm below
1503d322132cSJeff Roberson 	 * will not bring us back into range.  Dividing by two here forces
15042454aaf5SJeff Roberson 	 * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
1505d322132cSJeff Roberson 	 */
150637a35e4aSJeff Roberson 	if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
1507ae7a6b38SJeff Roberson 		ts->ts_runtime /= 2;
1508ae7a6b38SJeff Roberson 		ts->ts_slptime /= 2;
1509d322132cSJeff Roberson 		return;
1510d322132cSJeff Roberson 	}
1511ae7a6b38SJeff Roberson 	ts->ts_runtime = (ts->ts_runtime / 5) * 4;
1512ae7a6b38SJeff Roberson 	ts->ts_slptime = (ts->ts_slptime / 5) * 4;
1513d322132cSJeff Roberson }
1514d322132cSJeff Roberson 
1515ae7a6b38SJeff Roberson /*
1516ae7a6b38SJeff Roberson  * Scale back the interactivity history when a child thread is created.  The
1517ae7a6b38SJeff Roberson  * history is inherited from the parent but the thread may behave totally
1518ae7a6b38SJeff Roberson  * differently.  For example, a shell spawning a compiler process.  We want
1519ae7a6b38SJeff Roberson  * to learn that the compiler is behaving badly very quickly.
1520ae7a6b38SJeff Roberson  */
1521d322132cSJeff Roberson static void
15228460a577SJohn Birrell sched_interact_fork(struct thread *td)
1523d322132cSJeff Roberson {
1524d322132cSJeff Roberson 	int ratio;
1525d322132cSJeff Roberson 	int sum;
1526d322132cSJeff Roberson 
1527ae7a6b38SJeff Roberson 	sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
1528d322132cSJeff Roberson 	if (sum > SCHED_SLP_RUN_FORK) {
1529d322132cSJeff Roberson 		ratio = sum / SCHED_SLP_RUN_FORK;
1530ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime /= ratio;
1531ae7a6b38SJeff Roberson 		td->td_sched->ts_slptime /= ratio;
15324b60e324SJeff Roberson 	}
15334b60e324SJeff Roberson }
15344b60e324SJeff Roberson 
153515dc847eSJeff Roberson /*
1536ae7a6b38SJeff Roberson  * Called from proc0_init() to setup the scheduler fields.
1537ed062c8dSJulian Elischer  */
1538ed062c8dSJulian Elischer void
1539ed062c8dSJulian Elischer schedinit(void)
1540ed062c8dSJulian Elischer {
1541e7d50326SJeff Roberson 
1542ed062c8dSJulian Elischer 	/*
1543ed062c8dSJulian Elischer 	 * Set up the scheduler specific parts of proc0.
1544ed062c8dSJulian Elischer 	 */
1545ed062c8dSJulian Elischer 	proc0.p_sched = NULL; /* XXX */
1546ad1e7d28SJulian Elischer 	thread0.td_sched = &td_sched0;
1547e7d50326SJeff Roberson 	td_sched0.ts_ltick = ticks;
15488ab80cf0SJeff Roberson 	td_sched0.ts_ftick = ticks;
1549ad1e7d28SJulian Elischer 	td_sched0.ts_thread = &thread0;
1550ed062c8dSJulian Elischer }
1551ed062c8dSJulian Elischer 
1552ed062c8dSJulian Elischer /*
155315dc847eSJeff Roberson  * This is only somewhat accurate since given many processes of the same
155415dc847eSJeff Roberson  * priority they will switch when their slices run out, which will be
1555e7d50326SJeff Roberson  * at most sched_slice stathz ticks.
155615dc847eSJeff Roberson  */
155735e6168fSJeff Roberson int
155835e6168fSJeff Roberson sched_rr_interval(void)
155935e6168fSJeff Roberson {
1560e7d50326SJeff Roberson 
1561e7d50326SJeff Roberson 	/* Convert sched_slice to hz */
1562e7d50326SJeff Roberson 	return (hz/(realstathz/sched_slice));
156335e6168fSJeff Roberson }
156435e6168fSJeff Roberson 
1565ae7a6b38SJeff Roberson /*
1566ae7a6b38SJeff Roberson  * Update the percent cpu tracking information when it is requested or
1567ae7a6b38SJeff Roberson  * the total history exceeds the maximum.  We keep a sliding history of
1568ae7a6b38SJeff Roberson  * tick counts that slowly decays.  This is less precise than the 4BSD
1569ae7a6b38SJeff Roberson  * mechanism since it happens with less regular and frequent events.
1570ae7a6b38SJeff Roberson  */
157122bf7d9aSJeff Roberson static void
1572ad1e7d28SJulian Elischer sched_pctcpu_update(struct td_sched *ts)
157335e6168fSJeff Roberson {
1574e7d50326SJeff Roberson 
1575e7d50326SJeff Roberson 	if (ts->ts_ticks == 0)
1576e7d50326SJeff Roberson 		return;
15778ab80cf0SJeff Roberson 	if (ticks - (hz / 10) < ts->ts_ltick &&
15788ab80cf0SJeff Roberson 	    SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
15798ab80cf0SJeff Roberson 		return;
158035e6168fSJeff Roberson 	/*
158135e6168fSJeff Roberson 	 * Adjust counters and watermark for pctcpu calc.
1582210491d3SJeff Roberson 	 */
1583e7d50326SJeff Roberson 	if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
1584ad1e7d28SJulian Elischer 		ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
1585e7d50326SJeff Roberson 			    SCHED_TICK_TARG;
1586e7d50326SJeff Roberson 	else
1587ad1e7d28SJulian Elischer 		ts->ts_ticks = 0;
1588ad1e7d28SJulian Elischer 	ts->ts_ltick = ticks;
1589e7d50326SJeff Roberson 	ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
159035e6168fSJeff Roberson }
159135e6168fSJeff Roberson 
1592ae7a6b38SJeff Roberson /*
1593ae7a6b38SJeff Roberson  * Adjust the priority of a thread.  Move it to the appropriate run-queue
1594ae7a6b38SJeff Roberson  * if necessary.  This is the back-end for several priority related
1595ae7a6b38SJeff Roberson  * functions.
1596ae7a6b38SJeff Roberson  */
1597e7d50326SJeff Roberson static void
1598f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio)
159935e6168fSJeff Roberson {
1600ad1e7d28SJulian Elischer 	struct td_sched *ts;
160135e6168fSJeff Roberson 
160281d47d3fSJeff Roberson 	CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)",
1603431f8906SJulian Elischer 	    td, td->td_name, td->td_priority, prio, curthread,
1604431f8906SJulian Elischer 	    curthread->td_name);
1605ad1e7d28SJulian Elischer 	ts = td->td_sched;
16067b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1607f5c157d9SJohn Baldwin 	if (td->td_priority == prio)
1608f5c157d9SJohn Baldwin 		return;
1609e7d50326SJeff Roberson 
16103f872f85SJeff Roberson 	if (TD_ON_RUNQ(td) && prio < td->td_priority) {
16113f741ca1SJeff Roberson 		/*
16123f741ca1SJeff Roberson 		 * If the priority has been elevated due to priority
16133f741ca1SJeff Roberson 		 * propagation, we may have to move ourselves to a new
1614e7d50326SJeff Roberson 		 * queue.  This could be optimized to not re-add in some
1615e7d50326SJeff Roberson 		 * cases.
1616f2b74cbfSJeff Roberson 		 */
1617e7d50326SJeff Roberson 		sched_rem(td);
1618e7d50326SJeff Roberson 		td->td_priority = prio;
1619ae7a6b38SJeff Roberson 		sched_add(td, SRQ_BORROWING);
1620ae7a6b38SJeff Roberson #ifdef SMP
1621317da705SJeff Roberson 	} else if (TD_IS_RUNNING(td)) {
1622ae7a6b38SJeff Roberson 		struct tdq *tdq;
1623ae7a6b38SJeff Roberson 
1624ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(ts->ts_cpu);
1625317da705SJeff Roberson 		if (prio < tdq->tdq_lowpri ||
1626317da705SJeff Roberson 		   (td->td_priority == tdq->tdq_lowpri && tdq->tdq_load <= 1))
1627ae7a6b38SJeff Roberson 			tdq->tdq_lowpri = prio;
16283f741ca1SJeff Roberson 		td->td_priority = prio;
1629317da705SJeff Roberson #endif
1630317da705SJeff Roberson 	} else
1631317da705SJeff Roberson 		td->td_priority = prio;
1632ae7a6b38SJeff Roberson }
163335e6168fSJeff Roberson 
1634f5c157d9SJohn Baldwin /*
1635f5c157d9SJohn Baldwin  * Update a thread's priority when it is lent another thread's
1636f5c157d9SJohn Baldwin  * priority.
1637f5c157d9SJohn Baldwin  */
1638f5c157d9SJohn Baldwin void
1639f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio)
1640f5c157d9SJohn Baldwin {
1641f5c157d9SJohn Baldwin 
1642f5c157d9SJohn Baldwin 	td->td_flags |= TDF_BORROWING;
1643f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1644f5c157d9SJohn Baldwin }
1645f5c157d9SJohn Baldwin 
1646f5c157d9SJohn Baldwin /*
1647f5c157d9SJohn Baldwin  * Restore a thread's priority when priority propagation is
1648f5c157d9SJohn Baldwin  * over.  The prio argument is the minimum priority the thread
1649f5c157d9SJohn Baldwin  * needs to have to satisfy other possible priority lending
1650f5c157d9SJohn Baldwin  * requests.  If the thread's regular priority is less
1651f5c157d9SJohn Baldwin  * important than prio, the thread will keep a priority boost
1652f5c157d9SJohn Baldwin  * of prio.
1653f5c157d9SJohn Baldwin  */
1654f5c157d9SJohn Baldwin void
1655f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio)
1656f5c157d9SJohn Baldwin {
1657f5c157d9SJohn Baldwin 	u_char base_pri;
1658f5c157d9SJohn Baldwin 
1659f5c157d9SJohn Baldwin 	if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1660f5c157d9SJohn Baldwin 	    td->td_base_pri <= PRI_MAX_TIMESHARE)
16618460a577SJohn Birrell 		base_pri = td->td_user_pri;
1662f5c157d9SJohn Baldwin 	else
1663f5c157d9SJohn Baldwin 		base_pri = td->td_base_pri;
1664f5c157d9SJohn Baldwin 	if (prio >= base_pri) {
1665f5c157d9SJohn Baldwin 		td->td_flags &= ~TDF_BORROWING;
1666f5c157d9SJohn Baldwin 		sched_thread_priority(td, base_pri);
1667f5c157d9SJohn Baldwin 	} else
1668f5c157d9SJohn Baldwin 		sched_lend_prio(td, prio);
1669f5c157d9SJohn Baldwin }
1670f5c157d9SJohn Baldwin 
1671ae7a6b38SJeff Roberson /*
1672ae7a6b38SJeff Roberson  * Standard entry for setting the priority to an absolute value.
1673ae7a6b38SJeff Roberson  */
1674f5c157d9SJohn Baldwin void
1675f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio)
1676f5c157d9SJohn Baldwin {
1677f5c157d9SJohn Baldwin 	u_char oldprio;
1678f5c157d9SJohn Baldwin 
1679f5c157d9SJohn Baldwin 	/* First, update the base priority. */
1680f5c157d9SJohn Baldwin 	td->td_base_pri = prio;
1681f5c157d9SJohn Baldwin 
1682f5c157d9SJohn Baldwin 	/*
168350aaa791SJohn Baldwin 	 * If the thread is borrowing another thread's priority, don't
1684f5c157d9SJohn Baldwin 	 * ever lower the priority.
1685f5c157d9SJohn Baldwin 	 */
1686f5c157d9SJohn Baldwin 	if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1687f5c157d9SJohn Baldwin 		return;
1688f5c157d9SJohn Baldwin 
1689f5c157d9SJohn Baldwin 	/* Change the real priority. */
1690f5c157d9SJohn Baldwin 	oldprio = td->td_priority;
1691f5c157d9SJohn Baldwin 	sched_thread_priority(td, prio);
1692f5c157d9SJohn Baldwin 
1693f5c157d9SJohn Baldwin 	/*
1694f5c157d9SJohn Baldwin 	 * If the thread is on a turnstile, then let the turnstile update
1695f5c157d9SJohn Baldwin 	 * its state.
1696f5c157d9SJohn Baldwin 	 */
1697f5c157d9SJohn Baldwin 	if (TD_ON_LOCK(td) && oldprio != prio)
1698f5c157d9SJohn Baldwin 		turnstile_adjust(td, oldprio);
1699f5c157d9SJohn Baldwin }
1700f5c157d9SJohn Baldwin 
1701ae7a6b38SJeff Roberson /*
1702ae7a6b38SJeff Roberson  * Set the base user priority, does not effect current running priority.
1703ae7a6b38SJeff Roberson  */
170435e6168fSJeff Roberson void
17058460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio)
17063db720fdSDavid Xu {
17073db720fdSDavid Xu 	u_char oldprio;
17083db720fdSDavid Xu 
17098460a577SJohn Birrell 	td->td_base_user_pri = prio;
1710fc6c30f6SJulian Elischer 	if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
1711fc6c30f6SJulian Elischer                 return;
17128460a577SJohn Birrell 	oldprio = td->td_user_pri;
17138460a577SJohn Birrell 	td->td_user_pri = prio;
17143db720fdSDavid Xu }
17153db720fdSDavid Xu 
17163db720fdSDavid Xu void
17173db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio)
17183db720fdSDavid Xu {
17193db720fdSDavid Xu 	u_char oldprio;
17203db720fdSDavid Xu 
1721435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17223db720fdSDavid Xu 	td->td_flags |= TDF_UBORROWING;
1723f645b5daSMaxim Konovalov 	oldprio = td->td_user_pri;
17248460a577SJohn Birrell 	td->td_user_pri = prio;
17253db720fdSDavid Xu }
17263db720fdSDavid Xu 
17273db720fdSDavid Xu void
17283db720fdSDavid Xu sched_unlend_user_prio(struct thread *td, u_char prio)
17293db720fdSDavid Xu {
17303db720fdSDavid Xu 	u_char base_pri;
17313db720fdSDavid Xu 
1732435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
17338460a577SJohn Birrell 	base_pri = td->td_base_user_pri;
17343db720fdSDavid Xu 	if (prio >= base_pri) {
17353db720fdSDavid Xu 		td->td_flags &= ~TDF_UBORROWING;
17368460a577SJohn Birrell 		sched_user_prio(td, base_pri);
1737435806d3SDavid Xu 	} else {
17383db720fdSDavid Xu 		sched_lend_user_prio(td, prio);
17393db720fdSDavid Xu 	}
1740435806d3SDavid Xu }
17413db720fdSDavid Xu 
1742ae7a6b38SJeff Roberson /*
174308c9a16cSJeff Roberson  * Add the thread passed as 'newtd' to the run queue before selecting
174408c9a16cSJeff Roberson  * the next thread to run.  This is only used for KSE.
174508c9a16cSJeff Roberson  */
174608c9a16cSJeff Roberson static void
174708c9a16cSJeff Roberson sched_switchin(struct tdq *tdq, struct thread *td)
174808c9a16cSJeff Roberson {
174908c9a16cSJeff Roberson #ifdef SMP
175008c9a16cSJeff Roberson 	spinlock_enter();
175108c9a16cSJeff Roberson 	TDQ_UNLOCK(tdq);
175208c9a16cSJeff Roberson 	thread_lock(td);
175308c9a16cSJeff Roberson 	spinlock_exit();
175408c9a16cSJeff Roberson 	sched_setcpu(td->td_sched, TDQ_ID(tdq), SRQ_YIELDING);
175508c9a16cSJeff Roberson #else
175608c9a16cSJeff Roberson 	td->td_lock = TDQ_LOCKPTR(tdq);
175708c9a16cSJeff Roberson #endif
175808c9a16cSJeff Roberson 	tdq_add(tdq, td, SRQ_YIELDING);
175908c9a16cSJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
176008c9a16cSJeff Roberson }
176108c9a16cSJeff Roberson 
176208c9a16cSJeff Roberson /*
1763731016feSWojciech A. Koszek  * Block a thread for switching.  Similar to thread_block() but does not
1764731016feSWojciech A. Koszek  * bump the spin count.
1765731016feSWojciech A. Koszek  */
1766731016feSWojciech A. Koszek static inline struct mtx *
1767731016feSWojciech A. Koszek thread_block_switch(struct thread *td)
1768731016feSWojciech A. Koszek {
1769731016feSWojciech A. Koszek 	struct mtx *lock;
1770731016feSWojciech A. Koszek 
1771731016feSWojciech A. Koszek 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1772731016feSWojciech A. Koszek 	lock = td->td_lock;
1773731016feSWojciech A. Koszek 	td->td_lock = &blocked_lock;
1774731016feSWojciech A. Koszek 	mtx_unlock_spin(lock);
1775731016feSWojciech A. Koszek 
1776731016feSWojciech A. Koszek 	return (lock);
1777731016feSWojciech A. Koszek }
1778731016feSWojciech A. Koszek 
1779731016feSWojciech A. Koszek /*
1780c47f202bSJeff Roberson  * Handle migration from sched_switch().  This happens only for
1781c47f202bSJeff Roberson  * cpu binding.
1782c47f202bSJeff Roberson  */
1783c47f202bSJeff Roberson static struct mtx *
1784c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
1785c47f202bSJeff Roberson {
1786c47f202bSJeff Roberson 	struct tdq *tdn;
1787c47f202bSJeff Roberson 
1788c47f202bSJeff Roberson 	tdn = TDQ_CPU(td->td_sched->ts_cpu);
1789c47f202bSJeff Roberson #ifdef SMP
1790c47f202bSJeff Roberson 	/*
1791c47f202bSJeff Roberson 	 * Do the lock dance required to avoid LOR.  We grab an extra
1792c47f202bSJeff Roberson 	 * spinlock nesting to prevent preemption while we're
1793c47f202bSJeff Roberson 	 * not holding either run-queue lock.
1794c47f202bSJeff Roberson 	 */
1795c47f202bSJeff Roberson 	spinlock_enter();
1796c47f202bSJeff Roberson 	thread_block_switch(td);	/* This releases the lock on tdq. */
1797c47f202bSJeff Roberson 	TDQ_LOCK(tdn);
1798c47f202bSJeff Roberson 	tdq_add(tdn, td, flags);
1799c47f202bSJeff Roberson 	tdq_notify(td->td_sched);
1800c47f202bSJeff Roberson 	/*
1801c47f202bSJeff Roberson 	 * After we unlock tdn the new cpu still can't switch into this
1802c47f202bSJeff Roberson 	 * thread until we've unblocked it in cpu_switch().  The lock
1803c47f202bSJeff Roberson 	 * pointers may match in the case of HTT cores.  Don't unlock here
1804c47f202bSJeff Roberson 	 * or we can deadlock when the other CPU runs the IPI handler.
1805c47f202bSJeff Roberson 	 */
1806c47f202bSJeff Roberson 	if (TDQ_LOCKPTR(tdn) != TDQ_LOCKPTR(tdq)) {
1807c47f202bSJeff Roberson 		TDQ_UNLOCK(tdn);
1808c47f202bSJeff Roberson 		TDQ_LOCK(tdq);
1809c47f202bSJeff Roberson 	}
1810c47f202bSJeff Roberson 	spinlock_exit();
1811c47f202bSJeff Roberson #endif
1812c47f202bSJeff Roberson 	return (TDQ_LOCKPTR(tdn));
1813c47f202bSJeff Roberson }
1814c47f202bSJeff Roberson 
1815c47f202bSJeff Roberson /*
1816ae7a6b38SJeff Roberson  * Release a thread that was blocked with thread_block_switch().
1817ae7a6b38SJeff Roberson  */
1818ae7a6b38SJeff Roberson static inline void
1819ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx)
1820ae7a6b38SJeff Roberson {
1821ae7a6b38SJeff Roberson 	atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock,
1822ae7a6b38SJeff Roberson 	    (uintptr_t)mtx);
1823ae7a6b38SJeff Roberson }
1824ae7a6b38SJeff Roberson 
1825ae7a6b38SJeff Roberson /*
1826ae7a6b38SJeff Roberson  * Switch threads.  This function has to handle threads coming in while
1827ae7a6b38SJeff Roberson  * blocked for some reason, running, or idle.  It also must deal with
1828ae7a6b38SJeff Roberson  * migrating a thread from one queue to another as running threads may
1829ae7a6b38SJeff Roberson  * be assigned elsewhere via binding.
1830ae7a6b38SJeff Roberson  */
18313db720fdSDavid Xu void
18323389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags)
183335e6168fSJeff Roberson {
1834c02bbb43SJeff Roberson 	struct tdq *tdq;
1835ad1e7d28SJulian Elischer 	struct td_sched *ts;
1836ae7a6b38SJeff Roberson 	struct mtx *mtx;
1837c47f202bSJeff Roberson 	int srqflag;
1838ae7a6b38SJeff Roberson 	int cpuid;
183935e6168fSJeff Roberson 
18407b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
184135e6168fSJeff Roberson 
1842ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
1843ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
1844e7d50326SJeff Roberson 	ts = td->td_sched;
1845c47f202bSJeff Roberson 	mtx = td->td_lock;
1846ae7a6b38SJeff Roberson #ifdef SMP
1847ae7a6b38SJeff Roberson 	ts->ts_rltick = ticks;
1848ae7a6b38SJeff Roberson #endif
1849060563ecSJulian Elischer 	td->td_lastcpu = td->td_oncpu;
1850060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
185152eb8464SJohn Baldwin 	td->td_flags &= ~TDF_NEEDRESCHED;
185277918643SStephan Uphoff 	td->td_owepreempt = 0;
1853b11fdad0SJeff Roberson 	/*
1854ae7a6b38SJeff Roberson 	 * The lock pointer in an idle thread should never change.  Reset it
1855ae7a6b38SJeff Roberson 	 * to CAN_RUN as well.
1856b11fdad0SJeff Roberson 	 */
1857486a9414SJulian Elischer 	if (TD_IS_IDLETHREAD(td)) {
1858ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1859bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
18607b20fb19SJeff Roberson 	} else if (TD_IS_RUNNING(td)) {
1861ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
18627b20fb19SJeff Roberson 		tdq_load_rem(tdq, ts);
1863c47f202bSJeff Roberson 		srqflag = (flags & SW_PREEMPT) ?
1864598b368dSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1865c47f202bSJeff Roberson 		    SRQ_OURSELF|SRQ_YIELDING;
1866c47f202bSJeff Roberson 		if (ts->ts_cpu == cpuid)
1867c47f202bSJeff Roberson 			tdq_add(tdq, td, srqflag);
1868c47f202bSJeff Roberson 		else
1869c47f202bSJeff Roberson 			mtx = sched_switch_migrate(tdq, td, srqflag);
1870ae7a6b38SJeff Roberson 	} else {
1871ae7a6b38SJeff Roberson 		/* This thread must be going to sleep. */
1872ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
1873ae7a6b38SJeff Roberson 		mtx = thread_block_switch(td);
1874ae7a6b38SJeff Roberson 		tdq_load_rem(tdq, ts);
1875ae7a6b38SJeff Roberson 	}
1876ae7a6b38SJeff Roberson 	/*
1877ae7a6b38SJeff Roberson 	 * We enter here with the thread blocked and assigned to the
1878ae7a6b38SJeff Roberson 	 * appropriate cpu run-queue or sleep-queue and with the current
1879ae7a6b38SJeff Roberson 	 * thread-queue locked.
1880ae7a6b38SJeff Roberson 	 */
1881ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
1882ae7a6b38SJeff Roberson 	/*
188308c9a16cSJeff Roberson 	 * If KSE assigned a new thread just add it here and let choosethread
188408c9a16cSJeff Roberson 	 * select the best one.
1885ae7a6b38SJeff Roberson 	 */
188608c9a16cSJeff Roberson 	if (newtd != NULL)
188708c9a16cSJeff Roberson 		sched_switchin(tdq, newtd);
18882454aaf5SJeff Roberson 	newtd = choosethread();
1889ae7a6b38SJeff Roberson 	/*
1890ae7a6b38SJeff Roberson 	 * Call the MD code to switch contexts if necessary.
1891ae7a6b38SJeff Roberson 	 */
1892ebccf1e3SJoseph Koshy 	if (td != newtd) {
1893ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1894ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1895ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1896ebccf1e3SJoseph Koshy #endif
1897eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
189859c68134SJeff Roberson 		TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
1899ae7a6b38SJeff Roberson 		cpu_switch(td, newtd, mtx);
1900ae7a6b38SJeff Roberson 		/*
1901ae7a6b38SJeff Roberson 		 * We may return from cpu_switch on a different cpu.  However,
1902ae7a6b38SJeff Roberson 		 * we always return with td_lock pointing to the current cpu's
1903ae7a6b38SJeff Roberson 		 * run queue lock.
1904ae7a6b38SJeff Roberson 		 */
1905ae7a6b38SJeff Roberson 		cpuid = PCPU_GET(cpuid);
1906ae7a6b38SJeff Roberson 		tdq = TDQ_CPU(cpuid);
1907eea4f254SJeff Roberson 		lock_profile_obtain_lock_success(
1908eea4f254SJeff Roberson 		    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
1909ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1910ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1911ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1912ebccf1e3SJoseph Koshy #endif
1913ae7a6b38SJeff Roberson 	} else
1914ae7a6b38SJeff Roberson 		thread_unblock_switch(td, mtx);
1915ae7a6b38SJeff Roberson 	/*
1916ae7a6b38SJeff Roberson 	 * Assert that all went well and return.
1917ae7a6b38SJeff Roberson 	 */
1918ae7a6b38SJeff Roberson #ifdef SMP
1919ae7a6b38SJeff Roberson 	/* We should always get here with the lowest priority td possible */
1920ae7a6b38SJeff Roberson 	tdq->tdq_lowpri = td->td_priority;
1921ae7a6b38SJeff Roberson #endif
1922ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED);
1923ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1924ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
192535e6168fSJeff Roberson }
192635e6168fSJeff Roberson 
1927ae7a6b38SJeff Roberson /*
1928ae7a6b38SJeff Roberson  * Adjust thread priorities as a result of a nice request.
1929ae7a6b38SJeff Roberson  */
193035e6168fSJeff Roberson void
1931fa885116SJulian Elischer sched_nice(struct proc *p, int nice)
193235e6168fSJeff Roberson {
193335e6168fSJeff Roberson 	struct thread *td;
193435e6168fSJeff Roberson 
1935fa885116SJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
19367b20fb19SJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1937e7d50326SJeff Roberson 
1938fa885116SJulian Elischer 	p->p_nice = nice;
19398460a577SJohn Birrell 	FOREACH_THREAD_IN_PROC(p, td) {
19407b20fb19SJeff Roberson 		thread_lock(td);
19418460a577SJohn Birrell 		sched_priority(td);
1942e7d50326SJeff Roberson 		sched_prio(td, td->td_base_user_pri);
19437b20fb19SJeff Roberson 		thread_unlock(td);
194435e6168fSJeff Roberson 	}
1945fa885116SJulian Elischer }
194635e6168fSJeff Roberson 
1947ae7a6b38SJeff Roberson /*
1948ae7a6b38SJeff Roberson  * Record the sleep time for the interactivity scorer.
1949ae7a6b38SJeff Roberson  */
195035e6168fSJeff Roberson void
195144f3b092SJohn Baldwin sched_sleep(struct thread *td)
195235e6168fSJeff Roberson {
1953e7d50326SJeff Roberson 
19547b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
195535e6168fSJeff Roberson 
195654b0e65fSJeff Roberson 	td->td_slptick = ticks;
195735e6168fSJeff Roberson }
195835e6168fSJeff Roberson 
1959ae7a6b38SJeff Roberson /*
1960ae7a6b38SJeff Roberson  * Schedule a thread to resume execution and record how long it voluntarily
1961ae7a6b38SJeff Roberson  * slept.  We also update the pctcpu, interactivity, and priority.
1962ae7a6b38SJeff Roberson  */
196335e6168fSJeff Roberson void
196435e6168fSJeff Roberson sched_wakeup(struct thread *td)
196535e6168fSJeff Roberson {
196614618990SJeff Roberson 	struct td_sched *ts;
1967ae7a6b38SJeff Roberson 	int slptick;
1968e7d50326SJeff Roberson 
19697b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
197014618990SJeff Roberson 	ts = td->td_sched;
197135e6168fSJeff Roberson 	/*
1972e7d50326SJeff Roberson 	 * If we slept for more than a tick update our interactivity and
1973e7d50326SJeff Roberson 	 * priority.
197435e6168fSJeff Roberson 	 */
197554b0e65fSJeff Roberson 	slptick = td->td_slptick;
197654b0e65fSJeff Roberson 	td->td_slptick = 0;
1977ae7a6b38SJeff Roberson 	if (slptick && slptick != ticks) {
19789a93305aSJeff Roberson 		u_int hzticks;
1979f1e8dc4aSJeff Roberson 
1980ae7a6b38SJeff Roberson 		hzticks = (ticks - slptick) << SCHED_TICK_SHIFT;
1981ae7a6b38SJeff Roberson 		ts->ts_slptime += hzticks;
19828460a577SJohn Birrell 		sched_interact_update(td);
198314618990SJeff Roberson 		sched_pctcpu_update(ts);
19848460a577SJohn Birrell 		sched_priority(td);
1985f1e8dc4aSJeff Roberson 	}
198614618990SJeff Roberson 	/* Reset the slice value after we sleep. */
198714618990SJeff Roberson 	ts->ts_slice = sched_slice;
19887a5e5e2aSJeff Roberson 	sched_add(td, SRQ_BORING);
198935e6168fSJeff Roberson }
199035e6168fSJeff Roberson 
199135e6168fSJeff Roberson /*
199235e6168fSJeff Roberson  * Penalize the parent for creating a new child and initialize the child's
199335e6168fSJeff Roberson  * priority.
199435e6168fSJeff Roberson  */
199535e6168fSJeff Roberson void
19968460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child)
199715dc847eSJeff Roberson {
19987b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1999ad1e7d28SJulian Elischer 	sched_fork_thread(td, child);
2000e7d50326SJeff Roberson 	/*
2001e7d50326SJeff Roberson 	 * Penalize the parent and child for forking.
2002e7d50326SJeff Roberson 	 */
2003e7d50326SJeff Roberson 	sched_interact_fork(child);
2004e7d50326SJeff Roberson 	sched_priority(child);
2005ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += tickincr;
2006e7d50326SJeff Roberson 	sched_interact_update(td);
2007e7d50326SJeff Roberson 	sched_priority(td);
2008ad1e7d28SJulian Elischer }
2009ad1e7d28SJulian Elischer 
2010ae7a6b38SJeff Roberson /*
2011ae7a6b38SJeff Roberson  * Fork a new thread, may be within the same process.
2012ae7a6b38SJeff Roberson  */
2013ad1e7d28SJulian Elischer void
2014ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child)
2015ad1e7d28SJulian Elischer {
2016ad1e7d28SJulian Elischer 	struct td_sched *ts;
2017ad1e7d28SJulian Elischer 	struct td_sched *ts2;
20188460a577SJohn Birrell 
2019e7d50326SJeff Roberson 	/*
2020e7d50326SJeff Roberson 	 * Initialize child.
2021e7d50326SJeff Roberson 	 */
20227b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2023ed062c8dSJulian Elischer 	sched_newthread(child);
2024ae7a6b38SJeff Roberson 	child->td_lock = TDQ_LOCKPTR(TDQ_SELF());
2025ad1e7d28SJulian Elischer 	ts = td->td_sched;
2026ad1e7d28SJulian Elischer 	ts2 = child->td_sched;
2027ad1e7d28SJulian Elischer 	ts2->ts_cpu = ts->ts_cpu;
2028ad1e7d28SJulian Elischer 	ts2->ts_runq = NULL;
2029e7d50326SJeff Roberson 	/*
2030e7d50326SJeff Roberson 	 * Grab our parents cpu estimation information and priority.
2031e7d50326SJeff Roberson 	 */
2032ad1e7d28SJulian Elischer 	ts2->ts_ticks = ts->ts_ticks;
2033ad1e7d28SJulian Elischer 	ts2->ts_ltick = ts->ts_ltick;
2034ad1e7d28SJulian Elischer 	ts2->ts_ftick = ts->ts_ftick;
2035e7d50326SJeff Roberson 	child->td_user_pri = td->td_user_pri;
2036e7d50326SJeff Roberson 	child->td_base_user_pri = td->td_base_user_pri;
2037e7d50326SJeff Roberson 	/*
2038e7d50326SJeff Roberson 	 * And update interactivity score.
2039e7d50326SJeff Roberson 	 */
2040ae7a6b38SJeff Roberson 	ts2->ts_slptime = ts->ts_slptime;
2041ae7a6b38SJeff Roberson 	ts2->ts_runtime = ts->ts_runtime;
2042e7d50326SJeff Roberson 	ts2->ts_slice = 1;	/* Attempt to quickly learn interactivity. */
204315dc847eSJeff Roberson }
204415dc847eSJeff Roberson 
2045ae7a6b38SJeff Roberson /*
2046ae7a6b38SJeff Roberson  * Adjust the priority class of a thread.
2047ae7a6b38SJeff Roberson  */
204815dc847eSJeff Roberson void
20498460a577SJohn Birrell sched_class(struct thread *td, int class)
205015dc847eSJeff Roberson {
205115dc847eSJeff Roberson 
20527b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
20538460a577SJohn Birrell 	if (td->td_pri_class == class)
205415dc847eSJeff Roberson 		return;
205515dc847eSJeff Roberson 
2056ef1134c9SJeff Roberson #ifdef SMP
2057155b9987SJeff Roberson 	/*
2058155b9987SJeff Roberson 	 * On SMP if we're on the RUNQ we must adjust the transferable
2059155b9987SJeff Roberson 	 * count because could be changing to or from an interrupt
2060155b9987SJeff Roberson 	 * class.
2061155b9987SJeff Roberson 	 */
20627a5e5e2aSJeff Roberson 	if (TD_ON_RUNQ(td)) {
20631e516cf5SJeff Roberson 		struct tdq *tdq;
20641e516cf5SJeff Roberson 
20651e516cf5SJeff Roberson 		tdq = TDQ_CPU(td->td_sched->ts_cpu);
20661e516cf5SJeff Roberson 		if (THREAD_CAN_MIGRATE(td)) {
2067d2ad694cSJeff Roberson 			tdq->tdq_transferable--;
2068d2ad694cSJeff Roberson 			tdq->tdq_group->tdg_transferable--;
206980f86c9fSJeff Roberson 		}
20701e516cf5SJeff Roberson 		td->td_pri_class = class;
20711e516cf5SJeff Roberson 		if (THREAD_CAN_MIGRATE(td)) {
2072d2ad694cSJeff Roberson 			tdq->tdq_transferable++;
2073d2ad694cSJeff Roberson 			tdq->tdq_group->tdg_transferable++;
207480f86c9fSJeff Roberson 		}
2075155b9987SJeff Roberson 	}
2076ef1134c9SJeff Roberson #endif
20778460a577SJohn Birrell 	td->td_pri_class = class;
207835e6168fSJeff Roberson }
207935e6168fSJeff Roberson 
208035e6168fSJeff Roberson /*
208135e6168fSJeff Roberson  * Return some of the child's priority and interactivity to the parent.
208235e6168fSJeff Roberson  */
208335e6168fSJeff Roberson void
2084fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child)
208535e6168fSJeff Roberson {
2086e7d50326SJeff Roberson 	struct thread *td;
2087141ad61cSJeff Roberson 
20888460a577SJohn Birrell 	CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d",
2089431f8906SJulian Elischer 	    child, child->td_name, child->td_priority);
20908460a577SJohn Birrell 
20917b20fb19SJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2092e7d50326SJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
2093e7d50326SJeff Roberson 	sched_exit_thread(td, child);
2094ad1e7d28SJulian Elischer }
2095ad1e7d28SJulian Elischer 
2096ae7a6b38SJeff Roberson /*
2097ae7a6b38SJeff Roberson  * Penalize another thread for the time spent on this one.  This helps to
2098ae7a6b38SJeff Roberson  * worsen the priority and interactivity of processes which schedule batch
2099ae7a6b38SJeff Roberson  * jobs such as make.  This has little effect on the make process itself but
2100ae7a6b38SJeff Roberson  * causes new processes spawned by it to receive worse scores immediately.
2101ae7a6b38SJeff Roberson  */
2102ad1e7d28SJulian Elischer void
2103fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child)
2104ad1e7d28SJulian Elischer {
2105fc6c30f6SJulian Elischer 
2106e7d50326SJeff Roberson 	CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d",
2107431f8906SJulian Elischer 	    child, child->td_name, child->td_priority);
2108e7d50326SJeff Roberson 
2109e7d50326SJeff Roberson #ifdef KSE
2110e7d50326SJeff Roberson 	/*
2111e7d50326SJeff Roberson 	 * KSE forks and exits so often that this penalty causes short-lived
2112e7d50326SJeff Roberson 	 * threads to always be non-interactive.  This causes mozilla to
2113e7d50326SJeff Roberson 	 * crawl under load.
2114e7d50326SJeff Roberson 	 */
2115e7d50326SJeff Roberson 	if ((td->td_pflags & TDP_SA) && td->td_proc == child->td_proc)
2116e7d50326SJeff Roberson 		return;
2117e7d50326SJeff Roberson #endif
2118e7d50326SJeff Roberson 	/*
2119e7d50326SJeff Roberson 	 * Give the child's runtime to the parent without returning the
2120e7d50326SJeff Roberson 	 * sleep time as a penalty to the parent.  This causes shells that
2121e7d50326SJeff Roberson 	 * launch expensive things to mark their children as expensive.
2122e7d50326SJeff Roberson 	 */
21237b20fb19SJeff Roberson 	thread_lock(td);
2124ae7a6b38SJeff Roberson 	td->td_sched->ts_runtime += child->td_sched->ts_runtime;
2125fc6c30f6SJulian Elischer 	sched_interact_update(td);
2126e7d50326SJeff Roberson 	sched_priority(td);
21277b20fb19SJeff Roberson 	thread_unlock(td);
2128ad1e7d28SJulian Elischer }
2129ad1e7d28SJulian Elischer 
2130ae7a6b38SJeff Roberson /*
2131ae7a6b38SJeff Roberson  * Fix priorities on return to user-space.  Priorities may be elevated due
2132ae7a6b38SJeff Roberson  * to static priorities in msleep() or similar.
2133ae7a6b38SJeff Roberson  */
2134ad1e7d28SJulian Elischer void
2135ad1e7d28SJulian Elischer sched_userret(struct thread *td)
2136ad1e7d28SJulian Elischer {
2137ad1e7d28SJulian Elischer 	/*
2138ad1e7d28SJulian Elischer 	 * XXX we cheat slightly on the locking here to avoid locking in
2139ad1e7d28SJulian Elischer 	 * the usual case.  Setting td_priority here is essentially an
2140ad1e7d28SJulian Elischer 	 * incomplete workaround for not setting it properly elsewhere.
2141ad1e7d28SJulian Elischer 	 * Now that some interrupt handlers are threads, not setting it
2142ad1e7d28SJulian Elischer 	 * properly elsewhere can clobber it in the window between setting
2143ad1e7d28SJulian Elischer 	 * it here and returning to user mode, so don't waste time setting
2144ad1e7d28SJulian Elischer 	 * it perfectly here.
2145ad1e7d28SJulian Elischer 	 */
2146ad1e7d28SJulian Elischer 	KASSERT((td->td_flags & TDF_BORROWING) == 0,
2147ad1e7d28SJulian Elischer 	    ("thread with borrowed priority returning to userland"));
2148ad1e7d28SJulian Elischer 	if (td->td_priority != td->td_user_pri) {
21497b20fb19SJeff Roberson 		thread_lock(td);
2150ad1e7d28SJulian Elischer 		td->td_priority = td->td_user_pri;
2151ad1e7d28SJulian Elischer 		td->td_base_pri = td->td_user_pri;
21527b20fb19SJeff Roberson 		thread_unlock(td);
2153ad1e7d28SJulian Elischer         }
215435e6168fSJeff Roberson }
215535e6168fSJeff Roberson 
2156ae7a6b38SJeff Roberson /*
2157ae7a6b38SJeff Roberson  * Handle a stathz tick.  This is really only relevant for timeshare
2158ae7a6b38SJeff Roberson  * threads.
2159ae7a6b38SJeff Roberson  */
216035e6168fSJeff Roberson void
21617cf90fb3SJeff Roberson sched_clock(struct thread *td)
216235e6168fSJeff Roberson {
2163ad1e7d28SJulian Elischer 	struct tdq *tdq;
2164ad1e7d28SJulian Elischer 	struct td_sched *ts;
216535e6168fSJeff Roberson 
2166ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
21673f872f85SJeff Roberson 	tdq = TDQ_SELF();
21687fcf154aSJeff Roberson #ifdef SMP
21697fcf154aSJeff Roberson 	/*
21707fcf154aSJeff Roberson 	 * We run the long term load balancer infrequently on the first cpu.
21717fcf154aSJeff Roberson 	 */
21727fcf154aSJeff Roberson 	if (balance_tdq == tdq) {
21737fcf154aSJeff Roberson 		if (balance_ticks && --balance_ticks == 0)
21747fcf154aSJeff Roberson 			sched_balance();
21757fcf154aSJeff Roberson 		if (balance_group_ticks && --balance_group_ticks == 0)
21767fcf154aSJeff Roberson 			sched_balance_groups();
21777fcf154aSJeff Roberson 	}
21787fcf154aSJeff Roberson #endif
21793f872f85SJeff Roberson 	/*
21803f872f85SJeff Roberson 	 * Advance the insert index once for each tick to ensure that all
21813f872f85SJeff Roberson 	 * threads get a chance to run.
21823f872f85SJeff Roberson 	 */
21833f872f85SJeff Roberson 	if (tdq->tdq_idx == tdq->tdq_ridx) {
21843f872f85SJeff Roberson 		tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
21853f872f85SJeff Roberson 		if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
21863f872f85SJeff Roberson 			tdq->tdq_ridx = tdq->tdq_idx;
21873f872f85SJeff Roberson 	}
21883f872f85SJeff Roberson 	ts = td->td_sched;
2189fd0b8c78SJeff Roberson 	if (td->td_pri_class & PRI_FIFO_BIT)
2190a8949de2SJeff Roberson 		return;
2191fd0b8c78SJeff Roberson 	if (td->td_pri_class == PRI_TIMESHARE) {
2192a8949de2SJeff Roberson 		/*
2193fd0b8c78SJeff Roberson 		 * We used a tick; charge it to the thread so
2194fd0b8c78SJeff Roberson 		 * that we can compute our interactivity.
219515dc847eSJeff Roberson 		 */
2196ae7a6b38SJeff Roberson 		td->td_sched->ts_runtime += tickincr;
21978460a577SJohn Birrell 		sched_interact_update(td);
2198fd0b8c78SJeff Roberson 	}
219935e6168fSJeff Roberson 	/*
220035e6168fSJeff Roberson 	 * We used up one time slice.
220135e6168fSJeff Roberson 	 */
2202ad1e7d28SJulian Elischer 	if (--ts->ts_slice > 0)
220315dc847eSJeff Roberson 		return;
220435e6168fSJeff Roberson 	/*
220515dc847eSJeff Roberson 	 * We're out of time, recompute priorities and requeue.
220635e6168fSJeff Roberson 	 */
22078460a577SJohn Birrell 	sched_priority(td);
22084a338afdSJulian Elischer 	td->td_flags |= TDF_NEEDRESCHED;
220935e6168fSJeff Roberson }
221035e6168fSJeff Roberson 
2211ae7a6b38SJeff Roberson /*
2212ae7a6b38SJeff Roberson  * Called once per hz tick.  Used for cpu utilization information.  This
2213ae7a6b38SJeff Roberson  * is easier than trying to scale based on stathz.
2214ae7a6b38SJeff Roberson  */
2215ae7a6b38SJeff Roberson void
2216ae7a6b38SJeff Roberson sched_tick(void)
2217ae7a6b38SJeff Roberson {
2218ae7a6b38SJeff Roberson 	struct td_sched *ts;
2219ae7a6b38SJeff Roberson 
2220ae7a6b38SJeff Roberson 	ts = curthread->td_sched;
2221ae7a6b38SJeff Roberson 	/* Adjust ticks for pctcpu */
2222ae7a6b38SJeff Roberson 	ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
2223ae7a6b38SJeff Roberson 	ts->ts_ltick = ticks;
2224ae7a6b38SJeff Roberson 	/*
2225ae7a6b38SJeff Roberson 	 * Update if we've exceeded our desired tick threshhold by over one
2226ae7a6b38SJeff Roberson 	 * second.
2227ae7a6b38SJeff Roberson 	 */
2228ae7a6b38SJeff Roberson 	if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
2229ae7a6b38SJeff Roberson 		sched_pctcpu_update(ts);
2230ae7a6b38SJeff Roberson }
2231ae7a6b38SJeff Roberson 
2232ae7a6b38SJeff Roberson /*
2233ae7a6b38SJeff Roberson  * Return whether the current CPU has runnable tasks.  Used for in-kernel
2234ae7a6b38SJeff Roberson  * cooperative idle threads.
2235ae7a6b38SJeff Roberson  */
223635e6168fSJeff Roberson int
223735e6168fSJeff Roberson sched_runnable(void)
223835e6168fSJeff Roberson {
2239ad1e7d28SJulian Elischer 	struct tdq *tdq;
2240b90816f1SJeff Roberson 	int load;
224135e6168fSJeff Roberson 
2242b90816f1SJeff Roberson 	load = 1;
2243b90816f1SJeff Roberson 
2244ad1e7d28SJulian Elischer 	tdq = TDQ_SELF();
22453f741ca1SJeff Roberson 	if ((curthread->td_flags & TDF_IDLETD) != 0) {
2246d2ad694cSJeff Roberson 		if (tdq->tdq_load > 0)
22473f741ca1SJeff Roberson 			goto out;
22483f741ca1SJeff Roberson 	} else
2249d2ad694cSJeff Roberson 		if (tdq->tdq_load - 1 > 0)
2250b90816f1SJeff Roberson 			goto out;
2251b90816f1SJeff Roberson 	load = 0;
2252b90816f1SJeff Roberson out:
2253b90816f1SJeff Roberson 	return (load);
225435e6168fSJeff Roberson }
225535e6168fSJeff Roberson 
2256ae7a6b38SJeff Roberson /*
2257ae7a6b38SJeff Roberson  * Choose the highest priority thread to run.  The thread is removed from
2258ae7a6b38SJeff Roberson  * the run-queue while running however the load remains.  For SMP we set
2259ae7a6b38SJeff Roberson  * the tdq in the global idle bitmask if it idles here.
2260ae7a6b38SJeff Roberson  */
22617a5e5e2aSJeff Roberson struct thread *
2262c9f25d8fSJeff Roberson sched_choose(void)
2263c9f25d8fSJeff Roberson {
226415dc847eSJeff Roberson #ifdef SMP
2265ae7a6b38SJeff Roberson 	struct tdq_group *tdg;
226615dc847eSJeff Roberson #endif
2267ae7a6b38SJeff Roberson 	struct td_sched *ts;
2268317da705SJeff Roberson 	struct thread *td;
2269ae7a6b38SJeff Roberson 	struct tdq *tdq;
2270ae7a6b38SJeff Roberson 
2271ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2272ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2273ad1e7d28SJulian Elischer 	ts = tdq_choose(tdq);
2274ad1e7d28SJulian Elischer 	if (ts) {
2275ad1e7d28SJulian Elischer 		tdq_runq_rem(tdq, ts);
22767a5e5e2aSJeff Roberson 		return (ts->ts_thread);
227735e6168fSJeff Roberson 	}
2278317da705SJeff Roberson 	td = PCPU_GET(idlethread);
2279c9f25d8fSJeff Roberson #ifdef SMP
2280ae7a6b38SJeff Roberson 	/*
2281ae7a6b38SJeff Roberson 	 * We only set the idled bit when all of the cpus in the group are
2282ae7a6b38SJeff Roberson 	 * idle.  Otherwise we could get into a situation where a thread bounces
2283ae7a6b38SJeff Roberson 	 * back and forth between two idle cores on seperate physical CPUs.
2284ae7a6b38SJeff Roberson 	 */
2285ae7a6b38SJeff Roberson 	tdg = tdq->tdq_group;
2286ae7a6b38SJeff Roberson 	tdg->tdg_idlemask |= PCPU_GET(cpumask);
2287ae7a6b38SJeff Roberson 	if (tdg->tdg_idlemask == tdg->tdg_cpumask)
2288ae7a6b38SJeff Roberson 		atomic_set_int(&tdq_idle, tdg->tdg_mask);
2289317da705SJeff Roberson 	tdq->tdq_lowpri = td->td_priority;
2290c9f25d8fSJeff Roberson #endif
2291317da705SJeff Roberson 	return (td);
22927a5e5e2aSJeff Roberson }
22937a5e5e2aSJeff Roberson 
2294ae7a6b38SJeff Roberson /*
2295ae7a6b38SJeff Roberson  * Set owepreempt if necessary.  Preemption never happens directly in ULE,
2296ae7a6b38SJeff Roberson  * we always request it once we exit a critical section.
2297ae7a6b38SJeff Roberson  */
2298ae7a6b38SJeff Roberson static inline void
2299ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td)
23007a5e5e2aSJeff Roberson {
23017a5e5e2aSJeff Roberson 	struct thread *ctd;
23027a5e5e2aSJeff Roberson 	int cpri;
23037a5e5e2aSJeff Roberson 	int pri;
23047a5e5e2aSJeff Roberson 
23057a5e5e2aSJeff Roberson 	ctd = curthread;
23067a5e5e2aSJeff Roberson 	pri = td->td_priority;
23077a5e5e2aSJeff Roberson 	cpri = ctd->td_priority;
2308ae7a6b38SJeff Roberson 	if (td->td_priority < ctd->td_priority)
2309ae7a6b38SJeff Roberson 		curthread->td_flags |= TDF_NEEDRESCHED;
23107a5e5e2aSJeff Roberson 	if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
2311ae7a6b38SJeff Roberson 		return;
23127a5e5e2aSJeff Roberson 	/*
23137a5e5e2aSJeff Roberson 	 * Always preempt IDLE threads.  Otherwise only if the preempting
23147a5e5e2aSJeff Roberson 	 * thread is an ithread.
23157a5e5e2aSJeff Roberson 	 */
2316ae7a6b38SJeff Roberson 	if (pri > preempt_thresh && cpri < PRI_MIN_IDLE)
2317ae7a6b38SJeff Roberson 		return;
23187a5e5e2aSJeff Roberson 	ctd->td_owepreempt = 1;
2319ae7a6b38SJeff Roberson 	return;
232035e6168fSJeff Roberson }
232135e6168fSJeff Roberson 
2322ae7a6b38SJeff Roberson /*
2323ae7a6b38SJeff Roberson  * Add a thread to a thread queue.  Initializes priority, slice, runq, and
2324ae7a6b38SJeff Roberson  * add it to the appropriate queue.  This is the internal function called
2325ae7a6b38SJeff Roberson  * when the tdq is predetermined.
2326ae7a6b38SJeff Roberson  */
232735e6168fSJeff Roberson void
2328ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags)
232935e6168fSJeff Roberson {
2330ad1e7d28SJulian Elischer 	struct td_sched *ts;
233122bf7d9aSJeff Roberson 	int class;
23327b8bfa0dSJeff Roberson #ifdef SMP
23337b8bfa0dSJeff Roberson 	int cpumask;
23347b8bfa0dSJeff Roberson #endif
2335c9f25d8fSJeff Roberson 
2336ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
23377a5e5e2aSJeff Roberson 	KASSERT((td->td_inhibitors == 0),
23387a5e5e2aSJeff Roberson 	    ("sched_add: trying to run inhibited thread"));
23397a5e5e2aSJeff Roberson 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
23407a5e5e2aSJeff Roberson 	    ("sched_add: bad thread state"));
2341b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
2342b61ce5b0SJeff Roberson 	    ("sched_add: thread swapped out"));
2343ae7a6b38SJeff Roberson 
2344ae7a6b38SJeff Roberson 	ts = td->td_sched;
23457a5e5e2aSJeff Roberson 	class = PRI_BASE(td->td_pri_class);
2346ae7a6b38SJeff Roberson         TD_SET_RUNQ(td);
23477a5e5e2aSJeff Roberson 	if (ts->ts_slice == 0)
23487a5e5e2aSJeff Roberson 		ts->ts_slice = sched_slice;
23492454aaf5SJeff Roberson 	/*
2350ae7a6b38SJeff Roberson 	 * Pick the run queue based on priority.
23512454aaf5SJeff Roberson 	 */
2352ae7a6b38SJeff Roberson 	if (td->td_priority <= PRI_MAX_REALTIME)
2353ae7a6b38SJeff Roberson 		ts->ts_runq = &tdq->tdq_realtime;
2354ae7a6b38SJeff Roberson 	else if (td->td_priority <= PRI_MAX_TIMESHARE)
2355ae7a6b38SJeff Roberson 		ts->ts_runq = &tdq->tdq_timeshare;
23567b8bfa0dSJeff Roberson 	else
2357ae7a6b38SJeff Roberson 		ts->ts_runq = &tdq->tdq_idle;
2358ae7a6b38SJeff Roberson #ifdef SMP
23597b8bfa0dSJeff Roberson 	cpumask = 1 << ts->ts_cpu;
236022bf7d9aSJeff Roberson 	/*
2361670c524fSJeff Roberson 	 * If we had been idle, clear our bit in the group and potentially
23627b8bfa0dSJeff Roberson 	 * the global bitmap.
236322bf7d9aSJeff Roberson 	 */
2364e7d50326SJeff Roberson 	if ((class != PRI_IDLE && class != PRI_ITHD) &&
23657b8bfa0dSJeff Roberson 	    (tdq->tdq_group->tdg_idlemask & cpumask) != 0) {
236680f86c9fSJeff Roberson 		/*
236780f86c9fSJeff Roberson 		 * Check to see if our group is unidling, and if so, remove it
236880f86c9fSJeff Roberson 		 * from the global idle mask.
236980f86c9fSJeff Roberson 		 */
2370d2ad694cSJeff Roberson 		if (tdq->tdq_group->tdg_idlemask ==
2371d2ad694cSJeff Roberson 		    tdq->tdq_group->tdg_cpumask)
2372d2ad694cSJeff Roberson 			atomic_clear_int(&tdq_idle, tdq->tdq_group->tdg_mask);
237380f86c9fSJeff Roberson 		/*
237480f86c9fSJeff Roberson 		 * Now remove ourselves from the group specific idle mask.
237580f86c9fSJeff Roberson 		 */
23767b8bfa0dSJeff Roberson 		tdq->tdq_group->tdg_idlemask &= ~cpumask;
23777b8bfa0dSJeff Roberson 	}
2378ae7a6b38SJeff Roberson 	if (td->td_priority < tdq->tdq_lowpri)
2379ae7a6b38SJeff Roberson 		tdq->tdq_lowpri = td->td_priority;
238022bf7d9aSJeff Roberson #endif
2381ad1e7d28SJulian Elischer 	tdq_runq_add(tdq, ts, flags);
2382ad1e7d28SJulian Elischer 	tdq_load_add(tdq, ts);
2383ae7a6b38SJeff Roberson }
2384ae7a6b38SJeff Roberson 
2385ae7a6b38SJeff Roberson /*
2386ae7a6b38SJeff Roberson  * Select the target thread queue and add a thread to it.  Request
2387ae7a6b38SJeff Roberson  * preemption or IPI a remote processor if required.
2388ae7a6b38SJeff Roberson  */
2389ae7a6b38SJeff Roberson void
2390ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags)
2391ae7a6b38SJeff Roberson {
2392ae7a6b38SJeff Roberson 	struct td_sched *ts;
2393ae7a6b38SJeff Roberson 	struct tdq *tdq;
23947b8bfa0dSJeff Roberson #ifdef SMP
2395ae7a6b38SJeff Roberson 	int cpuid;
2396ae7a6b38SJeff Roberson 	int cpu;
2397ae7a6b38SJeff Roberson #endif
2398ae7a6b38SJeff Roberson 	CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
2399431f8906SJulian Elischer 	    td, td->td_name, td->td_priority, curthread,
2400431f8906SJulian Elischer 	    curthread->td_name);
2401ae7a6b38SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2402ae7a6b38SJeff Roberson 	ts = td->td_sched;
2403ae7a6b38SJeff Roberson 	/*
2404ae7a6b38SJeff Roberson 	 * Recalculate the priority before we select the target cpu or
2405ae7a6b38SJeff Roberson 	 * run-queue.
2406ae7a6b38SJeff Roberson 	 */
2407ae7a6b38SJeff Roberson 	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
2408ae7a6b38SJeff Roberson 		sched_priority(td);
2409ae7a6b38SJeff Roberson #ifdef SMP
2410ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
2411ae7a6b38SJeff Roberson 	/*
2412ae7a6b38SJeff Roberson 	 * Pick the destination cpu and if it isn't ours transfer to the
2413ae7a6b38SJeff Roberson 	 * target cpu.
2414ae7a6b38SJeff Roberson 	 */
2415a755f214SJeff Roberson 	if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_MIGRATE(td) &&
2416a755f214SJeff Roberson 	    curthread->td_intr_nesting_level)
2417a755f214SJeff Roberson 		ts->ts_cpu = cpuid;
2418a755f214SJeff Roberson 	if (!THREAD_CAN_MIGRATE(td))
2419ae7a6b38SJeff Roberson 		cpu = ts->ts_cpu;
2420ae7a6b38SJeff Roberson 	else
2421ae7a6b38SJeff Roberson 		cpu = sched_pickcpu(ts, flags);
2422ae7a6b38SJeff Roberson 	tdq = sched_setcpu(ts, cpu, flags);
2423ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
2424ae7a6b38SJeff Roberson 	if (cpu != cpuid) {
24257b8bfa0dSJeff Roberson 		tdq_notify(ts);
24267b8bfa0dSJeff Roberson 		return;
24277b8bfa0dSJeff Roberson 	}
2428ae7a6b38SJeff Roberson #else
2429ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
2430ae7a6b38SJeff Roberson 	TDQ_LOCK(tdq);
2431ae7a6b38SJeff Roberson 	/*
2432ae7a6b38SJeff Roberson 	 * Now that the thread is moving to the run-queue, set the lock
2433ae7a6b38SJeff Roberson 	 * to the scheduler's lock.
2434ae7a6b38SJeff Roberson 	 */
2435ae7a6b38SJeff Roberson 	thread_lock_set(td, TDQ_LOCKPTR(tdq));
2436ae7a6b38SJeff Roberson 	tdq_add(tdq, td, flags);
24377b8bfa0dSJeff Roberson #endif
2438ae7a6b38SJeff Roberson 	if (!(flags & SRQ_YIELDING))
2439ae7a6b38SJeff Roberson 		sched_setpreempt(td);
244035e6168fSJeff Roberson }
244135e6168fSJeff Roberson 
2442ae7a6b38SJeff Roberson /*
2443ae7a6b38SJeff Roberson  * Remove a thread from a run-queue without running it.  This is used
2444ae7a6b38SJeff Roberson  * when we're stealing a thread from a remote queue.  Otherwise all threads
2445ae7a6b38SJeff Roberson  * exit by calling sched_exit_thread() and sched_throw() themselves.
2446ae7a6b38SJeff Roberson  */
244735e6168fSJeff Roberson void
24487cf90fb3SJeff Roberson sched_rem(struct thread *td)
244935e6168fSJeff Roberson {
2450ad1e7d28SJulian Elischer 	struct tdq *tdq;
2451ad1e7d28SJulian Elischer 	struct td_sched *ts;
24527cf90fb3SJeff Roberson 
245381d47d3fSJeff Roberson 	CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)",
2454431f8906SJulian Elischer 	    td, td->td_name, td->td_priority, curthread,
2455431f8906SJulian Elischer 	    curthread->td_name);
2456ad1e7d28SJulian Elischer 	ts = td->td_sched;
2457ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(ts->ts_cpu);
2458ae7a6b38SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2459ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
24607a5e5e2aSJeff Roberson 	KASSERT(TD_ON_RUNQ(td),
2461ad1e7d28SJulian Elischer 	    ("sched_rem: thread not on run queue"));
2462ad1e7d28SJulian Elischer 	tdq_runq_rem(tdq, ts);
2463ad1e7d28SJulian Elischer 	tdq_load_rem(tdq, ts);
24647a5e5e2aSJeff Roberson 	TD_SET_CAN_RUN(td);
246535e6168fSJeff Roberson }
246635e6168fSJeff Roberson 
2467ae7a6b38SJeff Roberson /*
2468ae7a6b38SJeff Roberson  * Fetch cpu utilization information.  Updates on demand.
2469ae7a6b38SJeff Roberson  */
247035e6168fSJeff Roberson fixpt_t
24717cf90fb3SJeff Roberson sched_pctcpu(struct thread *td)
247235e6168fSJeff Roberson {
247335e6168fSJeff Roberson 	fixpt_t pctcpu;
2474ad1e7d28SJulian Elischer 	struct td_sched *ts;
247535e6168fSJeff Roberson 
247635e6168fSJeff Roberson 	pctcpu = 0;
2477ad1e7d28SJulian Elischer 	ts = td->td_sched;
2478ad1e7d28SJulian Elischer 	if (ts == NULL)
2479484288deSJeff Roberson 		return (0);
248035e6168fSJeff Roberson 
24817b20fb19SJeff Roberson 	thread_lock(td);
2482ad1e7d28SJulian Elischer 	if (ts->ts_ticks) {
248335e6168fSJeff Roberson 		int rtick;
248435e6168fSJeff Roberson 
2485ad1e7d28SJulian Elischer 		sched_pctcpu_update(ts);
248635e6168fSJeff Roberson 		/* How many rtick per second ? */
2487e7d50326SJeff Roberson 		rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
2488e7d50326SJeff Roberson 		pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
248935e6168fSJeff Roberson 	}
24907b20fb19SJeff Roberson 	thread_unlock(td);
249135e6168fSJeff Roberson 
249235e6168fSJeff Roberson 	return (pctcpu);
249335e6168fSJeff Roberson }
249435e6168fSJeff Roberson 
2495885d51a3SJeff Roberson void
2496885d51a3SJeff Roberson sched_affinity(struct thread *td)
2497885d51a3SJeff Roberson {
2498885d51a3SJeff Roberson }
2499885d51a3SJeff Roberson 
2500ae7a6b38SJeff Roberson /*
2501ae7a6b38SJeff Roberson  * Bind a thread to a target cpu.
2502ae7a6b38SJeff Roberson  */
25039bacd788SJeff Roberson void
25049bacd788SJeff Roberson sched_bind(struct thread *td, int cpu)
25059bacd788SJeff Roberson {
2506ad1e7d28SJulian Elischer 	struct td_sched *ts;
25079bacd788SJeff Roberson 
2508c47f202bSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
2509ad1e7d28SJulian Elischer 	ts = td->td_sched;
25106b2f763fSJeff Roberson 	if (ts->ts_flags & TSF_BOUND)
2511c95d2db2SJeff Roberson 		sched_unbind(td);
2512ad1e7d28SJulian Elischer 	ts->ts_flags |= TSF_BOUND;
251380f86c9fSJeff Roberson #ifdef SMP
25146b2f763fSJeff Roberson 	sched_pin();
251580f86c9fSJeff Roberson 	if (PCPU_GET(cpuid) == cpu)
25169bacd788SJeff Roberson 		return;
25176b2f763fSJeff Roberson 	ts->ts_cpu = cpu;
25189bacd788SJeff Roberson 	/* When we return from mi_switch we'll be on the correct cpu. */
2519279f949eSPoul-Henning Kamp 	mi_switch(SW_VOL, NULL);
25209bacd788SJeff Roberson #endif
25219bacd788SJeff Roberson }
25229bacd788SJeff Roberson 
2523ae7a6b38SJeff Roberson /*
2524ae7a6b38SJeff Roberson  * Release a bound thread.
2525ae7a6b38SJeff Roberson  */
25269bacd788SJeff Roberson void
25279bacd788SJeff Roberson sched_unbind(struct thread *td)
25289bacd788SJeff Roberson {
2529e7d50326SJeff Roberson 	struct td_sched *ts;
2530e7d50326SJeff Roberson 
25317b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2532e7d50326SJeff Roberson 	ts = td->td_sched;
25336b2f763fSJeff Roberson 	if ((ts->ts_flags & TSF_BOUND) == 0)
25346b2f763fSJeff Roberson 		return;
2535e7d50326SJeff Roberson 	ts->ts_flags &= ~TSF_BOUND;
2536e7d50326SJeff Roberson #ifdef SMP
2537e7d50326SJeff Roberson 	sched_unpin();
2538e7d50326SJeff Roberson #endif
25399bacd788SJeff Roberson }
25409bacd788SJeff Roberson 
254135e6168fSJeff Roberson int
2542ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td)
2543ebccf1e3SJoseph Koshy {
25447b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2545ad1e7d28SJulian Elischer 	return (td->td_sched->ts_flags & TSF_BOUND);
2546ebccf1e3SJoseph Koshy }
2547ebccf1e3SJoseph Koshy 
2548ae7a6b38SJeff Roberson /*
2549ae7a6b38SJeff Roberson  * Basic yield call.
2550ae7a6b38SJeff Roberson  */
255136ec198bSDavid Xu void
255236ec198bSDavid Xu sched_relinquish(struct thread *td)
255336ec198bSDavid Xu {
25547b20fb19SJeff Roberson 	thread_lock(td);
25557b20fb19SJeff Roberson 	SCHED_STAT_INC(switch_relinquish);
255636ec198bSDavid Xu 	mi_switch(SW_VOL, NULL);
25577b20fb19SJeff Roberson 	thread_unlock(td);
255836ec198bSDavid Xu }
255936ec198bSDavid Xu 
2560ae7a6b38SJeff Roberson /*
2561ae7a6b38SJeff Roberson  * Return the total system load.
2562ae7a6b38SJeff Roberson  */
2563ebccf1e3SJoseph Koshy int
256433916c36SJeff Roberson sched_load(void)
256533916c36SJeff Roberson {
256633916c36SJeff Roberson #ifdef SMP
256733916c36SJeff Roberson 	int total;
256833916c36SJeff Roberson 	int i;
256933916c36SJeff Roberson 
257033916c36SJeff Roberson 	total = 0;
2571d2ad694cSJeff Roberson 	for (i = 0; i <= tdg_maxid; i++)
2572d2ad694cSJeff Roberson 		total += TDQ_GROUP(i)->tdg_load;
257333916c36SJeff Roberson 	return (total);
257433916c36SJeff Roberson #else
2575d2ad694cSJeff Roberson 	return (TDQ_SELF()->tdq_sysload);
257633916c36SJeff Roberson #endif
257733916c36SJeff Roberson }
257833916c36SJeff Roberson 
257933916c36SJeff Roberson int
258035e6168fSJeff Roberson sched_sizeof_proc(void)
258135e6168fSJeff Roberson {
258235e6168fSJeff Roberson 	return (sizeof(struct proc));
258335e6168fSJeff Roberson }
258435e6168fSJeff Roberson 
258535e6168fSJeff Roberson int
258635e6168fSJeff Roberson sched_sizeof_thread(void)
258735e6168fSJeff Roberson {
258835e6168fSJeff Roberson 	return (sizeof(struct thread) + sizeof(struct td_sched));
258935e6168fSJeff Roberson }
2590b41f1452SDavid Xu 
25917a5e5e2aSJeff Roberson /*
25927a5e5e2aSJeff Roberson  * The actual idle process.
25937a5e5e2aSJeff Roberson  */
25947a5e5e2aSJeff Roberson void
25957a5e5e2aSJeff Roberson sched_idletd(void *dummy)
25967a5e5e2aSJeff Roberson {
25977a5e5e2aSJeff Roberson 	struct thread *td;
2598ae7a6b38SJeff Roberson 	struct tdq *tdq;
25997a5e5e2aSJeff Roberson 
26007a5e5e2aSJeff Roberson 	td = curthread;
2601ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
26027a5e5e2aSJeff Roberson 	mtx_assert(&Giant, MA_NOTOWNED);
2603ae7a6b38SJeff Roberson 	/* ULE relies on preemption for idle interruption. */
2604ae7a6b38SJeff Roberson 	for (;;) {
2605ae7a6b38SJeff Roberson #ifdef SMP
2606ae7a6b38SJeff Roberson 		if (tdq_idled(tdq))
26077a5e5e2aSJeff Roberson 			cpu_idle();
2608ae7a6b38SJeff Roberson #else
2609ae7a6b38SJeff Roberson 		cpu_idle();
2610ae7a6b38SJeff Roberson #endif
2611ae7a6b38SJeff Roberson 	}
2612b41f1452SDavid Xu }
2613e7d50326SJeff Roberson 
26147b20fb19SJeff Roberson /*
26157b20fb19SJeff Roberson  * A CPU is entering for the first time or a thread is exiting.
26167b20fb19SJeff Roberson  */
26177b20fb19SJeff Roberson void
26187b20fb19SJeff Roberson sched_throw(struct thread *td)
26197b20fb19SJeff Roberson {
262059c68134SJeff Roberson 	struct thread *newtd;
2621ae7a6b38SJeff Roberson 	struct tdq *tdq;
2622ae7a6b38SJeff Roberson 
2623ae7a6b38SJeff Roberson 	tdq = TDQ_SELF();
26247b20fb19SJeff Roberson 	if (td == NULL) {
2625ae7a6b38SJeff Roberson 		/* Correct spinlock nesting and acquire the correct lock. */
2626ae7a6b38SJeff Roberson 		TDQ_LOCK(tdq);
26277b20fb19SJeff Roberson 		spinlock_exit();
26287b20fb19SJeff Roberson 	} else {
2629ae7a6b38SJeff Roberson 		MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2630ae7a6b38SJeff Roberson 		tdq_load_rem(tdq, td->td_sched);
2631eea4f254SJeff Roberson 		lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
26327b20fb19SJeff Roberson 	}
26337b20fb19SJeff Roberson 	KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
263459c68134SJeff Roberson 	newtd = choosethread();
263559c68134SJeff Roberson 	TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
26367b20fb19SJeff Roberson 	PCPU_SET(switchtime, cpu_ticks());
26377b20fb19SJeff Roberson 	PCPU_SET(switchticks, ticks);
263859c68134SJeff Roberson 	cpu_throw(td, newtd);		/* doesn't return */
26397b20fb19SJeff Roberson }
26407b20fb19SJeff Roberson 
2641ae7a6b38SJeff Roberson /*
2642ae7a6b38SJeff Roberson  * This is called from fork_exit().  Just acquire the correct locks and
2643ae7a6b38SJeff Roberson  * let fork do the rest of the work.
2644ae7a6b38SJeff Roberson  */
26457b20fb19SJeff Roberson void
2646fe54587fSJeff Roberson sched_fork_exit(struct thread *td)
26477b20fb19SJeff Roberson {
2648ae7a6b38SJeff Roberson 	struct td_sched *ts;
2649ae7a6b38SJeff Roberson 	struct tdq *tdq;
2650ae7a6b38SJeff Roberson 	int cpuid;
26517b20fb19SJeff Roberson 
26527b20fb19SJeff Roberson 	/*
26537b20fb19SJeff Roberson 	 * Finish setting up thread glue so that it begins execution in a
2654ae7a6b38SJeff Roberson 	 * non-nested critical section with the scheduler lock held.
26557b20fb19SJeff Roberson 	 */
2656ae7a6b38SJeff Roberson 	cpuid = PCPU_GET(cpuid);
2657ae7a6b38SJeff Roberson 	tdq = TDQ_CPU(cpuid);
2658ae7a6b38SJeff Roberson 	ts = td->td_sched;
2659ae7a6b38SJeff Roberson 	if (TD_IS_IDLETHREAD(td))
2660ae7a6b38SJeff Roberson 		td->td_lock = TDQ_LOCKPTR(tdq);
2661ae7a6b38SJeff Roberson 	MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2662ae7a6b38SJeff Roberson 	td->td_oncpu = cpuid;
266359c68134SJeff Roberson 	TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
2664eea4f254SJeff Roberson 	lock_profile_obtain_lock_success(
2665eea4f254SJeff Roberson 	    &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
26667b20fb19SJeff Roberson }
26677b20fb19SJeff Roberson 
2668ae7a6b38SJeff Roberson static SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0,
2669ae7a6b38SJeff Roberson     "Scheduler");
2670ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0,
2671e7d50326SJeff Roberson     "Scheduler name");
2672ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
2673ae7a6b38SJeff Roberson     "Slice size for timeshare threads");
2674ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
2675ae7a6b38SJeff Roberson      "Interactivity score threshold");
2676ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh,
2677ae7a6b38SJeff Roberson      0,"Min priority for preemption, lower priorities have greater precedence");
26787b8bfa0dSJeff Roberson #ifdef SMP
2679ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri, CTLFLAG_RW, &pick_pri, 0,
2680ae7a6b38SJeff Roberson     "Pick the target cpu based on priority rather than load.");
2681ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
2682ae7a6b38SJeff Roberson     "Number of hz ticks to keep thread affinity for");
2683ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, tryself, CTLFLAG_RW, &tryself, 0, "");
2684ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
2685ae7a6b38SJeff Roberson     "Enables the long-term load balancer");
26867fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
26877fcf154aSJeff Roberson     &balance_interval, 0,
26887fcf154aSJeff Roberson     "Average frequency in stathz ticks to run the long-term balancer");
2689ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0,
2690ae7a6b38SJeff Roberson     "Steals work from another hyper-threaded core on idle");
2691ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
2692ae7a6b38SJeff Roberson     "Attempts to steal work from other cores before idling");
269328994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
269428994a58SJeff Roberson     "Minimum load on remote cpu before we'll steal");
2695ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, topology, CTLFLAG_RD, &topology, 0,
2696ae7a6b38SJeff Roberson     "True when a topology has been specified by the MD code.");
26977b8bfa0dSJeff Roberson #endif
2698e7d50326SJeff Roberson 
269954b0e65fSJeff Roberson /* ps compat.  All cpu percentages from ULE are weighted. */
2700a5423ea3SJeff Roberson static int ccpu = 0;
2701e7d50326SJeff Roberson SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
2702e7d50326SJeff Roberson 
2703e7d50326SJeff Roberson 
2704ed062c8dSJulian Elischer #define KERN_SWITCH_INCLUDE 1
2705ed062c8dSJulian Elischer #include "kern/kern_switch.c"
2706