135e6168fSJeff Roberson /*- 2e7d50326SJeff Roberson * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org> 335e6168fSJeff Roberson * All rights reserved. 435e6168fSJeff Roberson * 535e6168fSJeff Roberson * Redistribution and use in source and binary forms, with or without 635e6168fSJeff Roberson * modification, are permitted provided that the following conditions 735e6168fSJeff Roberson * are met: 835e6168fSJeff Roberson * 1. Redistributions of source code must retain the above copyright 935e6168fSJeff Roberson * notice unmodified, this list of conditions, and the following 1035e6168fSJeff Roberson * disclaimer. 1135e6168fSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 1235e6168fSJeff Roberson * notice, this list of conditions and the following disclaimer in the 1335e6168fSJeff Roberson * documentation and/or other materials provided with the distribution. 1435e6168fSJeff Roberson * 1535e6168fSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1635e6168fSJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1735e6168fSJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1835e6168fSJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1935e6168fSJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2035e6168fSJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2135e6168fSJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2235e6168fSJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2335e6168fSJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2435e6168fSJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2535e6168fSJeff Roberson */ 2635e6168fSJeff Roberson 27ae7a6b38SJeff Roberson /* 28ae7a6b38SJeff Roberson * This file implements the ULE scheduler. ULE supports independent CPU 29ae7a6b38SJeff Roberson * run queues and fine grain locking. It has superior interactive 30ae7a6b38SJeff Roberson * performance under load even on uni-processor systems. 31ae7a6b38SJeff Roberson * 32ae7a6b38SJeff Roberson * etymology: 33a5423ea3SJeff Roberson * ULE is the last three letters in schedule. It owes its name to a 34ae7a6b38SJeff Roberson * generic user created for a scheduling system by Paul Mikesell at 35ae7a6b38SJeff Roberson * Isilon Systems and a general lack of creativity on the part of the author. 36ae7a6b38SJeff Roberson */ 37ae7a6b38SJeff Roberson 38677b542eSDavid E. O'Brien #include <sys/cdefs.h> 39113dda8aSJeff Roberson __FBSDID("$FreeBSD$"); 40677b542eSDavid E. O'Brien 414da0d332SPeter Wemm #include "opt_hwpmc_hooks.h" 426f5f25e5SJohn Birrell #include "opt_kdtrace.h" 434da0d332SPeter Wemm #include "opt_sched.h" 449923b511SScott Long 4535e6168fSJeff Roberson #include <sys/param.h> 4635e6168fSJeff Roberson #include <sys/systm.h> 472c3490b1SMarcel Moolenaar #include <sys/kdb.h> 4835e6168fSJeff Roberson #include <sys/kernel.h> 4935e6168fSJeff Roberson #include <sys/ktr.h> 5035e6168fSJeff Roberson #include <sys/lock.h> 5135e6168fSJeff Roberson #include <sys/mutex.h> 5235e6168fSJeff Roberson #include <sys/proc.h> 53245f3abfSJeff Roberson #include <sys/resource.h> 549bacd788SJeff Roberson #include <sys/resourcevar.h> 5535e6168fSJeff Roberson #include <sys/sched.h> 5635e6168fSJeff Roberson #include <sys/smp.h> 5735e6168fSJeff Roberson #include <sys/sx.h> 5835e6168fSJeff Roberson #include <sys/sysctl.h> 5935e6168fSJeff Roberson #include <sys/sysproto.h> 60f5c157d9SJohn Baldwin #include <sys/turnstile.h> 613db720fdSDavid Xu #include <sys/umtx.h> 6235e6168fSJeff Roberson #include <sys/vmmeter.h> 6362fa74d9SJeff Roberson #include <sys/cpuset.h> 6407095abfSIvan Voras #include <sys/sbuf.h> 6535e6168fSJeff Roberson 66ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 67ebccf1e3SJoseph Koshy #include <sys/pmckern.h> 68ebccf1e3SJoseph Koshy #endif 69ebccf1e3SJoseph Koshy 706f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS 716f5f25e5SJohn Birrell #include <sys/dtrace_bsd.h> 726f5f25e5SJohn Birrell int dtrace_vtime_active; 736f5f25e5SJohn Birrell dtrace_vtime_switch_func_t dtrace_vtime_switch_func; 746f5f25e5SJohn Birrell #endif 756f5f25e5SJohn Birrell 7635e6168fSJeff Roberson #include <machine/cpu.h> 7722bf7d9aSJeff Roberson #include <machine/smp.h> 7835e6168fSJeff Roberson 794542827dSRandall Stewart #if defined(__sparc64__) 8002e2d6b4SJeff Roberson #error "This architecture is not currently compatible with ULE" 817a5e5e2aSJeff Roberson #endif 827a5e5e2aSJeff Roberson 83ae7a6b38SJeff Roberson #define KTR_ULE 0 8414618990SJeff Roberson 850d2cf837SJeff Roberson #define TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX))) 860d2cf837SJeff Roberson #define TDQ_NAME_LEN (sizeof("sched lock ") + sizeof(__XSTRING(MAXCPU))) 878f51ad55SJeff Roberson #define TDQ_LOADNAME_LEN (PCPU_NAME_LEN + sizeof(" load")) 888f51ad55SJeff Roberson 896b2f763fSJeff Roberson /* 90ae7a6b38SJeff Roberson * Thread scheduler specific section. All fields are protected 91ae7a6b38SJeff Roberson * by the thread lock. 92ed062c8dSJulian Elischer */ 93ad1e7d28SJulian Elischer struct td_sched { 94ae7a6b38SJeff Roberson struct runq *ts_runq; /* Run-queue we're queued on. */ 95ae7a6b38SJeff Roberson short ts_flags; /* TSF_* flags. */ 96ad1e7d28SJulian Elischer u_char ts_cpu; /* CPU that we have affinity for. */ 9773daf66fSJeff Roberson int ts_rltick; /* Real last tick, for affinity. */ 98ae7a6b38SJeff Roberson int ts_slice; /* Ticks of slice remaining. */ 99ae7a6b38SJeff Roberson u_int ts_slptime; /* Number of ticks we vol. slept */ 100ae7a6b38SJeff Roberson u_int ts_runtime; /* Number of ticks we were running */ 101ad1e7d28SJulian Elischer int ts_ltick; /* Last tick that we were running on */ 102cbc4ea28SIvan Voras int ts_incrtick; /* Last tick that we incremented on */ 103ad1e7d28SJulian Elischer int ts_ftick; /* First tick that we were running on */ 104ad1e7d28SJulian Elischer int ts_ticks; /* Tick count */ 1058f51ad55SJeff Roberson #ifdef KTR 1068f51ad55SJeff Roberson char ts_name[TS_NAME_LEN]; 1078f51ad55SJeff Roberson #endif 108ed062c8dSJulian Elischer }; 109ad1e7d28SJulian Elischer /* flags kept in ts_flags */ 1107b8bfa0dSJeff Roberson #define TSF_BOUND 0x0001 /* Thread can not migrate. */ 1117b8bfa0dSJeff Roberson #define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */ 11235e6168fSJeff Roberson 113ad1e7d28SJulian Elischer static struct td_sched td_sched0; 11435e6168fSJeff Roberson 11562fa74d9SJeff Roberson #define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0) 11662fa74d9SJeff Roberson #define THREAD_CAN_SCHED(td, cpu) \ 11762fa74d9SJeff Roberson CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask) 11862fa74d9SJeff Roberson 11935e6168fSJeff Roberson /* 120e7d50326SJeff Roberson * Cpu percentage computation macros and defines. 121e1f89c22SJeff Roberson * 122e7d50326SJeff Roberson * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 123e7d50326SJeff Roberson * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 1248ab80cf0SJeff Roberson * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 125e7d50326SJeff Roberson * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 126e7d50326SJeff Roberson * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 127e7d50326SJeff Roberson * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 12835e6168fSJeff Roberson */ 129e7d50326SJeff Roberson #define SCHED_TICK_SECS 10 130e7d50326SJeff Roberson #define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 1318ab80cf0SJeff Roberson #define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 132e7d50326SJeff Roberson #define SCHED_TICK_SHIFT 10 133e7d50326SJeff Roberson #define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 134eddb4efaSJeff Roberson #define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 13535e6168fSJeff Roberson 13635e6168fSJeff Roberson /* 137e7d50326SJeff Roberson * These macros determine priorities for non-interactive threads. They are 138e7d50326SJeff Roberson * assigned a priority based on their recent cpu utilization as expressed 139e7d50326SJeff Roberson * by the ratio of ticks to the tick total. NHALF priorities at the start 140e7d50326SJeff Roberson * and end of the MIN to MAX timeshare range are only reachable with negative 141e7d50326SJeff Roberson * or positive nice respectively. 142e7d50326SJeff Roberson * 143e7d50326SJeff Roberson * PRI_RANGE: Priority range for utilization dependent priorities. 144e7d50326SJeff Roberson * PRI_NRESV: Number of nice values. 145e7d50326SJeff Roberson * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 146e7d50326SJeff Roberson * PRI_NICE: Determines the part of the priority inherited from nice. 147e7d50326SJeff Roberson */ 148e7d50326SJeff Roberson #define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 149e7d50326SJeff Roberson #define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 150e7d50326SJeff Roberson #define SCHED_PRI_MIN (PRI_MIN_TIMESHARE + SCHED_PRI_NHALF) 151e7d50326SJeff Roberson #define SCHED_PRI_MAX (PRI_MAX_TIMESHARE - SCHED_PRI_NHALF) 152dda713dfSJeff Roberson #define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN) 153e7d50326SJeff Roberson #define SCHED_PRI_TICKS(ts) \ 154e7d50326SJeff Roberson (SCHED_TICK_HZ((ts)) / \ 1551e516cf5SJeff Roberson (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 156e7d50326SJeff Roberson #define SCHED_PRI_NICE(nice) (nice) 157e7d50326SJeff Roberson 158e7d50326SJeff Roberson /* 159e7d50326SJeff Roberson * These determine the interactivity of a process. Interactivity differs from 160e7d50326SJeff Roberson * cpu utilization in that it expresses the voluntary time slept vs time ran 161e7d50326SJeff Roberson * while cpu utilization includes all time not running. This more accurately 162e7d50326SJeff Roberson * models the intent of the thread. 16335e6168fSJeff Roberson * 164407b0157SJeff Roberson * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 165407b0157SJeff Roberson * before throttling back. 166d322132cSJeff Roberson * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 167210491d3SJeff Roberson * INTERACT_MAX: Maximum interactivity value. Smaller is better. 168e1f89c22SJeff Roberson * INTERACT_THRESH: Threshhold for placement on the current runq. 16935e6168fSJeff Roberson */ 170e7d50326SJeff Roberson #define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 171e7d50326SJeff Roberson #define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 172210491d3SJeff Roberson #define SCHED_INTERACT_MAX (100) 173210491d3SJeff Roberson #define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 1744c9612c6SJeff Roberson #define SCHED_INTERACT_THRESH (30) 175e1f89c22SJeff Roberson 17635e6168fSJeff Roberson /* 177e7d50326SJeff Roberson * tickincr: Converts a stathz tick into a hz domain scaled by 178e7d50326SJeff Roberson * the shift factor. Without the shift the error rate 179e7d50326SJeff Roberson * due to rounding would be unacceptably high. 180e7d50326SJeff Roberson * realstathz: stathz is sometimes 0 and run off of hz. 181e7d50326SJeff Roberson * sched_slice: Runtime of each thread before rescheduling. 182ae7a6b38SJeff Roberson * preempt_thresh: Priority threshold for preemption and remote IPIs. 18335e6168fSJeff Roberson */ 184e7d50326SJeff Roberson static int sched_interact = SCHED_INTERACT_THRESH; 185e7d50326SJeff Roberson static int realstathz; 186e7d50326SJeff Roberson static int tickincr; 18773daf66fSJeff Roberson static int sched_slice = 1; 18802e2d6b4SJeff Roberson #ifdef PREEMPTION 18902e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION 19002e2d6b4SJeff Roberson static int preempt_thresh = PRI_MAX_IDLE; 19102e2d6b4SJeff Roberson #else 192ae7a6b38SJeff Roberson static int preempt_thresh = PRI_MIN_KERN; 19302e2d6b4SJeff Roberson #endif 19402e2d6b4SJeff Roberson #else 19502e2d6b4SJeff Roberson static int preempt_thresh = 0; 19602e2d6b4SJeff Roberson #endif 1970502fe2eSJeff Roberson static int static_boost = PRI_MIN_TIMESHARE; 1981690c6c1SJeff Roberson static int sched_idlespins = 10000; 1991690c6c1SJeff Roberson static int sched_idlespinthresh = 4; 200ae7a6b38SJeff Roberson 20135e6168fSJeff Roberson /* 202ae7a6b38SJeff Roberson * tdq - per processor runqs and statistics. All fields are protected by the 203ae7a6b38SJeff Roberson * tdq_lock. The load and lowpri may be accessed without to avoid excess 204ae7a6b38SJeff Roberson * locking in sched_pickcpu(); 20535e6168fSJeff Roberson */ 206ad1e7d28SJulian Elischer struct tdq { 20773daf66fSJeff Roberson /* Ordered to improve efficiency of cpu_search() and switch(). */ 20862fa74d9SJeff Roberson struct mtx tdq_lock; /* run queue lock. */ 20973daf66fSJeff Roberson struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 2101690c6c1SJeff Roberson volatile int tdq_load; /* Aggregate load. */ 21173daf66fSJeff Roberson int tdq_sysload; /* For loadavg, !ITHD load. */ 21273daf66fSJeff Roberson int tdq_transferable; /* Transferable thread count. */ 2131690c6c1SJeff Roberson short tdq_switchcnt; /* Switches this tick. */ 2141690c6c1SJeff Roberson short tdq_oldswitchcnt; /* Switches last tick. */ 21573daf66fSJeff Roberson u_char tdq_lowpri; /* Lowest priority thread. */ 21673daf66fSJeff Roberson u_char tdq_ipipending; /* IPI pending. */ 21773daf66fSJeff Roberson u_char tdq_idx; /* Current insert index. */ 21873daf66fSJeff Roberson u_char tdq_ridx; /* Current removal index. */ 219e7d50326SJeff Roberson struct runq tdq_realtime; /* real-time run queue. */ 220ae7a6b38SJeff Roberson struct runq tdq_timeshare; /* timeshare run queue. */ 221ae7a6b38SJeff Roberson struct runq tdq_idle; /* Queue of IDLE threads. */ 2228f51ad55SJeff Roberson char tdq_name[TDQ_NAME_LEN]; 2238f51ad55SJeff Roberson #ifdef KTR 2248f51ad55SJeff Roberson char tdq_loadname[TDQ_LOADNAME_LEN]; 2258f51ad55SJeff Roberson #endif 226ae7a6b38SJeff Roberson } __aligned(64); 22735e6168fSJeff Roberson 2281690c6c1SJeff Roberson /* Idle thread states and config. */ 2291690c6c1SJeff Roberson #define TDQ_RUNNING 1 2301690c6c1SJeff Roberson #define TDQ_IDLE 2 2317b8bfa0dSJeff Roberson 23280f86c9fSJeff Roberson #ifdef SMP 23307095abfSIvan Voras struct cpu_group *cpu_top; /* CPU topology */ 2347b8bfa0dSJeff Roberson 23562fa74d9SJeff Roberson #define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 23662fa74d9SJeff Roberson #define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 2377b8bfa0dSJeff Roberson 2387b8bfa0dSJeff Roberson /* 2397b8bfa0dSJeff Roberson * Run-time tunables. 2407b8bfa0dSJeff Roberson */ 24128994a58SJeff Roberson static int rebalance = 1; 2427fcf154aSJeff Roberson static int balance_interval = 128; /* Default set in sched_initticks(). */ 2437b8bfa0dSJeff Roberson static int affinity; 2447fcf154aSJeff Roberson static int steal_htt = 1; 24528994a58SJeff Roberson static int steal_idle = 1; 24628994a58SJeff Roberson static int steal_thresh = 2; 24780f86c9fSJeff Roberson 24835e6168fSJeff Roberson /* 249d2ad694cSJeff Roberson * One thread queue per processor. 25035e6168fSJeff Roberson */ 251ad1e7d28SJulian Elischer static struct tdq tdq_cpu[MAXCPU]; 2527fcf154aSJeff Roberson static struct tdq *balance_tdq; 2537fcf154aSJeff Roberson static int balance_ticks; 254dc03363dSJeff Roberson 255ad1e7d28SJulian Elischer #define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 256ad1e7d28SJulian Elischer #define TDQ_CPU(x) (&tdq_cpu[(x)]) 257c47f202bSJeff Roberson #define TDQ_ID(x) ((int)((x) - tdq_cpu)) 25880f86c9fSJeff Roberson #else /* !SMP */ 259ad1e7d28SJulian Elischer static struct tdq tdq_cpu; 260dc03363dSJeff Roberson 26136b36916SJeff Roberson #define TDQ_ID(x) (0) 262ad1e7d28SJulian Elischer #define TDQ_SELF() (&tdq_cpu) 263ad1e7d28SJulian Elischer #define TDQ_CPU(x) (&tdq_cpu) 2640a016a05SJeff Roberson #endif 26535e6168fSJeff Roberson 266ae7a6b38SJeff Roberson #define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 267ae7a6b38SJeff Roberson #define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 268ae7a6b38SJeff Roberson #define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 269ae7a6b38SJeff Roberson #define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 27062fa74d9SJeff Roberson #define TDQ_LOCKPTR(t) (&(t)->tdq_lock) 271ae7a6b38SJeff Roberson 2728460a577SJohn Birrell static void sched_priority(struct thread *); 27321381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char); 2748460a577SJohn Birrell static int sched_interact_score(struct thread *); 2758460a577SJohn Birrell static void sched_interact_update(struct thread *); 2768460a577SJohn Birrell static void sched_interact_fork(struct thread *); 277ad1e7d28SJulian Elischer static void sched_pctcpu_update(struct td_sched *); 27835e6168fSJeff Roberson 2795d7ef00cSJeff Roberson /* Operations on per processor queues */ 2809727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *); 281ad1e7d28SJulian Elischer static void tdq_setup(struct tdq *); 2829727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *); 2839727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *); 2849727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int); 2859727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *); 286ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int); 287ad1e7d28SJulian Elischer void tdq_print(int cpu); 288e7d50326SJeff Roberson static void runq_print(struct runq *rq); 289ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int); 2905d7ef00cSJeff Roberson #ifdef SMP 29162fa74d9SJeff Roberson static int tdq_move(struct tdq *, struct tdq *); 292ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *); 2939727e637SJeff Roberson static void tdq_notify(struct tdq *, struct thread *); 2949727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int); 2959727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int); 2969727e637SJeff Roberson static int sched_pickcpu(struct thread *, int); 2977fcf154aSJeff Roberson static void sched_balance(void); 29862fa74d9SJeff Roberson static int sched_balance_pair(struct tdq *, struct tdq *); 2999727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int); 300ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *); 301c47f202bSJeff Roberson static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int); 30207095abfSIvan Voras static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 30307095abfSIvan Voras static int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 30407095abfSIvan Voras struct cpu_group *cg, int indent); 3055d7ef00cSJeff Roberson #endif 3065d7ef00cSJeff Roberson 307e7d50326SJeff Roberson static void sched_setup(void *dummy); 308237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 309e7d50326SJeff Roberson 310e7d50326SJeff Roberson static void sched_initticks(void *dummy); 311237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 312237fdd78SRobert Watson NULL); 313e7d50326SJeff Roberson 314ae7a6b38SJeff Roberson /* 315ae7a6b38SJeff Roberson * Print the threads waiting on a run-queue. 316ae7a6b38SJeff Roberson */ 317e7d50326SJeff Roberson static void 318e7d50326SJeff Roberson runq_print(struct runq *rq) 319e7d50326SJeff Roberson { 320e7d50326SJeff Roberson struct rqhead *rqh; 3219727e637SJeff Roberson struct thread *td; 322e7d50326SJeff Roberson int pri; 323e7d50326SJeff Roberson int j; 324e7d50326SJeff Roberson int i; 325e7d50326SJeff Roberson 326e7d50326SJeff Roberson for (i = 0; i < RQB_LEN; i++) { 327e7d50326SJeff Roberson printf("\t\trunq bits %d 0x%zx\n", 328e7d50326SJeff Roberson i, rq->rq_status.rqb_bits[i]); 329e7d50326SJeff Roberson for (j = 0; j < RQB_BPW; j++) 330e7d50326SJeff Roberson if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 331e7d50326SJeff Roberson pri = j + (i << RQB_L2BPW); 332e7d50326SJeff Roberson rqh = &rq->rq_queues[pri]; 3339727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 334e7d50326SJeff Roberson printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 3359727e637SJeff Roberson td, td->td_name, td->td_priority, 3369727e637SJeff Roberson td->td_rqindex, pri); 337e7d50326SJeff Roberson } 338e7d50326SJeff Roberson } 339e7d50326SJeff Roberson } 340e7d50326SJeff Roberson } 341e7d50326SJeff Roberson 342ae7a6b38SJeff Roberson /* 343ae7a6b38SJeff Roberson * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 344ae7a6b38SJeff Roberson */ 34515dc847eSJeff Roberson void 346ad1e7d28SJulian Elischer tdq_print(int cpu) 34715dc847eSJeff Roberson { 348ad1e7d28SJulian Elischer struct tdq *tdq; 34915dc847eSJeff Roberson 350ad1e7d28SJulian Elischer tdq = TDQ_CPU(cpu); 35115dc847eSJeff Roberson 352c47f202bSJeff Roberson printf("tdq %d:\n", TDQ_ID(tdq)); 35362fa74d9SJeff Roberson printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 35462fa74d9SJeff Roberson printf("\tLock name: %s\n", tdq->tdq_name); 355d2ad694cSJeff Roberson printf("\tload: %d\n", tdq->tdq_load); 3561690c6c1SJeff Roberson printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 3571690c6c1SJeff Roberson printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 358e7d50326SJeff Roberson printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 3593f872f85SJeff Roberson printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 3601690c6c1SJeff Roberson printf("\tload transferable: %d\n", tdq->tdq_transferable); 3611690c6c1SJeff Roberson printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 362e7d50326SJeff Roberson printf("\trealtime runq:\n"); 363e7d50326SJeff Roberson runq_print(&tdq->tdq_realtime); 364e7d50326SJeff Roberson printf("\ttimeshare runq:\n"); 365e7d50326SJeff Roberson runq_print(&tdq->tdq_timeshare); 366e7d50326SJeff Roberson printf("\tidle runq:\n"); 367e7d50326SJeff Roberson runq_print(&tdq->tdq_idle); 36815dc847eSJeff Roberson } 36915dc847eSJeff Roberson 370ff256d9cSJeff Roberson static inline int 371ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote) 372ff256d9cSJeff Roberson { 373ff256d9cSJeff Roberson /* 374ff256d9cSJeff Roberson * If the new priority is not better than the current priority there is 375ff256d9cSJeff Roberson * nothing to do. 376ff256d9cSJeff Roberson */ 377ff256d9cSJeff Roberson if (pri >= cpri) 378ff256d9cSJeff Roberson return (0); 379ff256d9cSJeff Roberson /* 380ff256d9cSJeff Roberson * Always preempt idle. 381ff256d9cSJeff Roberson */ 382ff256d9cSJeff Roberson if (cpri >= PRI_MIN_IDLE) 383ff256d9cSJeff Roberson return (1); 384ff256d9cSJeff Roberson /* 385ff256d9cSJeff Roberson * If preemption is disabled don't preempt others. 386ff256d9cSJeff Roberson */ 387ff256d9cSJeff Roberson if (preempt_thresh == 0) 388ff256d9cSJeff Roberson return (0); 389ff256d9cSJeff Roberson /* 390ff256d9cSJeff Roberson * Preempt if we exceed the threshold. 391ff256d9cSJeff Roberson */ 392ff256d9cSJeff Roberson if (pri <= preempt_thresh) 393ff256d9cSJeff Roberson return (1); 394ff256d9cSJeff Roberson /* 395ff256d9cSJeff Roberson * If we're realtime or better and there is timeshare or worse running 396ff256d9cSJeff Roberson * preempt only remote processors. 397ff256d9cSJeff Roberson */ 398ff256d9cSJeff Roberson if (remote && pri <= PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME) 399ff256d9cSJeff Roberson return (1); 400ff256d9cSJeff Roberson return (0); 401ff256d9cSJeff Roberson } 402ff256d9cSJeff Roberson 403ae7a6b38SJeff Roberson #define TS_RQ_PPQ (((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS) 404ae7a6b38SJeff Roberson /* 405ae7a6b38SJeff Roberson * Add a thread to the actual run-queue. Keeps transferable counts up to 406ae7a6b38SJeff Roberson * date with what is actually on the run-queue. Selects the correct 407ae7a6b38SJeff Roberson * queue position for timeshare threads. 408ae7a6b38SJeff Roberson */ 409155b9987SJeff Roberson static __inline void 4109727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 411155b9987SJeff Roberson { 4129727e637SJeff Roberson struct td_sched *ts; 413c143ac21SJeff Roberson u_char pri; 414c143ac21SJeff Roberson 415ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 4169727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 41773daf66fSJeff Roberson 4189727e637SJeff Roberson pri = td->td_priority; 4199727e637SJeff Roberson ts = td->td_sched; 4209727e637SJeff Roberson TD_SET_RUNQ(td); 4219727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td)) { 422d2ad694cSJeff Roberson tdq->tdq_transferable++; 423ad1e7d28SJulian Elischer ts->ts_flags |= TSF_XFERABLE; 42480f86c9fSJeff Roberson } 425c143ac21SJeff Roberson if (pri <= PRI_MAX_REALTIME) { 426c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_realtime; 427c143ac21SJeff Roberson } else if (pri <= PRI_MAX_TIMESHARE) { 428c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_timeshare; 429e7d50326SJeff Roberson KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE, 430e7d50326SJeff Roberson ("Invalid priority %d on timeshare runq", pri)); 431e7d50326SJeff Roberson /* 432e7d50326SJeff Roberson * This queue contains only priorities between MIN and MAX 433e7d50326SJeff Roberson * realtime. Use the whole queue to represent these values. 434e7d50326SJeff Roberson */ 435c47f202bSJeff Roberson if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 436e7d50326SJeff Roberson pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ; 437e7d50326SJeff Roberson pri = (pri + tdq->tdq_idx) % RQ_NQS; 4383f872f85SJeff Roberson /* 4393f872f85SJeff Roberson * This effectively shortens the queue by one so we 4403f872f85SJeff Roberson * can have a one slot difference between idx and 4413f872f85SJeff Roberson * ridx while we wait for threads to drain. 4423f872f85SJeff Roberson */ 4433f872f85SJeff Roberson if (tdq->tdq_ridx != tdq->tdq_idx && 4443f872f85SJeff Roberson pri == tdq->tdq_ridx) 4454499aff6SJeff Roberson pri = (unsigned char)(pri - 1) % RQ_NQS; 446e7d50326SJeff Roberson } else 4473f872f85SJeff Roberson pri = tdq->tdq_ridx; 4489727e637SJeff Roberson runq_add_pri(ts->ts_runq, td, pri, flags); 449c143ac21SJeff Roberson return; 450e7d50326SJeff Roberson } else 45173daf66fSJeff Roberson ts->ts_runq = &tdq->tdq_idle; 4529727e637SJeff Roberson runq_add(ts->ts_runq, td, flags); 45373daf66fSJeff Roberson } 45473daf66fSJeff Roberson 45573daf66fSJeff Roberson /* 456ae7a6b38SJeff Roberson * Remove a thread from a run-queue. This typically happens when a thread 457ae7a6b38SJeff Roberson * is selected to run. Running threads are not on the queue and the 458ae7a6b38SJeff Roberson * transferable count does not reflect them. 459ae7a6b38SJeff Roberson */ 460155b9987SJeff Roberson static __inline void 4619727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td) 462155b9987SJeff Roberson { 4639727e637SJeff Roberson struct td_sched *ts; 4649727e637SJeff Roberson 4659727e637SJeff Roberson ts = td->td_sched; 466ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 467ae7a6b38SJeff Roberson KASSERT(ts->ts_runq != NULL, 4689727e637SJeff Roberson ("tdq_runq_remove: thread %p null ts_runq", td)); 469ad1e7d28SJulian Elischer if (ts->ts_flags & TSF_XFERABLE) { 470d2ad694cSJeff Roberson tdq->tdq_transferable--; 471ad1e7d28SJulian Elischer ts->ts_flags &= ~TSF_XFERABLE; 47280f86c9fSJeff Roberson } 4733f872f85SJeff Roberson if (ts->ts_runq == &tdq->tdq_timeshare) { 4743f872f85SJeff Roberson if (tdq->tdq_idx != tdq->tdq_ridx) 4759727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 476e7d50326SJeff Roberson else 4779727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, NULL); 4783f872f85SJeff Roberson } else 4799727e637SJeff Roberson runq_remove(ts->ts_runq, td); 480155b9987SJeff Roberson } 481155b9987SJeff Roberson 482ae7a6b38SJeff Roberson /* 483ae7a6b38SJeff Roberson * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 484ae7a6b38SJeff Roberson * for this thread to the referenced thread queue. 485ae7a6b38SJeff Roberson */ 486a8949de2SJeff Roberson static void 4879727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td) 4885d7ef00cSJeff Roberson { 489ae7a6b38SJeff Roberson 490ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 4919727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 49203d17db7SJeff Roberson 493d2ad694cSJeff Roberson tdq->tdq_load++; 4941b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 495d2ad694cSJeff Roberson tdq->tdq_sysload++; 4968f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 4975d7ef00cSJeff Roberson } 49815dc847eSJeff Roberson 499ae7a6b38SJeff Roberson /* 500ae7a6b38SJeff Roberson * Remove the load from a thread that is transitioning to a sleep state or 501ae7a6b38SJeff Roberson * exiting. 502ae7a6b38SJeff Roberson */ 503a8949de2SJeff Roberson static void 5049727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td) 5055d7ef00cSJeff Roberson { 506ae7a6b38SJeff Roberson 5079727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 508ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 509ae7a6b38SJeff Roberson KASSERT(tdq->tdq_load != 0, 510c47f202bSJeff Roberson ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 51103d17db7SJeff Roberson 512d2ad694cSJeff Roberson tdq->tdq_load--; 5131b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 51403d17db7SJeff Roberson tdq->tdq_sysload--; 5158f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 51615dc847eSJeff Roberson } 51715dc847eSJeff Roberson 518356500a3SJeff Roberson /* 51962fa74d9SJeff Roberson * Set lowpri to its exact value by searching the run-queue and 52062fa74d9SJeff Roberson * evaluating curthread. curthread may be passed as an optimization. 521356500a3SJeff Roberson */ 52222bf7d9aSJeff Roberson static void 52362fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd) 52462fa74d9SJeff Roberson { 52562fa74d9SJeff Roberson struct thread *td; 52662fa74d9SJeff Roberson 52762fa74d9SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 52862fa74d9SJeff Roberson if (ctd == NULL) 52962fa74d9SJeff Roberson ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread; 5309727e637SJeff Roberson td = tdq_choose(tdq); 5319727e637SJeff Roberson if (td == NULL || td->td_priority > ctd->td_priority) 53262fa74d9SJeff Roberson tdq->tdq_lowpri = ctd->td_priority; 53362fa74d9SJeff Roberson else 53462fa74d9SJeff Roberson tdq->tdq_lowpri = td->td_priority; 53562fa74d9SJeff Roberson } 53662fa74d9SJeff Roberson 53762fa74d9SJeff Roberson #ifdef SMP 53862fa74d9SJeff Roberson struct cpu_search { 539c76ee827SJeff Roberson cpuset_t cs_mask; 54062fa74d9SJeff Roberson u_int cs_load; 54162fa74d9SJeff Roberson u_int cs_cpu; 54262fa74d9SJeff Roberson int cs_limit; /* Min priority for low min load for high. */ 54362fa74d9SJeff Roberson }; 54462fa74d9SJeff Roberson 54562fa74d9SJeff Roberson #define CPU_SEARCH_LOWEST 0x1 54662fa74d9SJeff Roberson #define CPU_SEARCH_HIGHEST 0x2 54762fa74d9SJeff Roberson #define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST) 54862fa74d9SJeff Roberson 549c76ee827SJeff Roberson #define CPUSET_FOREACH(cpu, mask) \ 550c76ee827SJeff Roberson for ((cpu) = 0; (cpu) <= mp_maxid; (cpu)++) \ 55162fa74d9SJeff Roberson if ((mask) & 1 << (cpu)) 55262fa74d9SJeff Roberson 553d628fbfaSJohn Baldwin static __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low, 55462fa74d9SJeff Roberson struct cpu_search *high, const int match); 55562fa74d9SJeff Roberson int cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low); 55662fa74d9SJeff Roberson int cpu_search_highest(struct cpu_group *cg, struct cpu_search *high); 55762fa74d9SJeff Roberson int cpu_search_both(struct cpu_group *cg, struct cpu_search *low, 55862fa74d9SJeff Roberson struct cpu_search *high); 55962fa74d9SJeff Roberson 56062fa74d9SJeff Roberson /* 56162fa74d9SJeff Roberson * This routine compares according to the match argument and should be 56262fa74d9SJeff Roberson * reduced in actual instantiations via constant propagation and dead code 56362fa74d9SJeff Roberson * elimination. 56462fa74d9SJeff Roberson */ 56562fa74d9SJeff Roberson static __inline int 56662fa74d9SJeff Roberson cpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high, 56762fa74d9SJeff Roberson const int match) 56862fa74d9SJeff Roberson { 56962fa74d9SJeff Roberson struct tdq *tdq; 57062fa74d9SJeff Roberson 57162fa74d9SJeff Roberson tdq = TDQ_CPU(cpu); 57262fa74d9SJeff Roberson if (match & CPU_SEARCH_LOWEST) 573c76ee827SJeff Roberson if (CPU_ISSET(cpu, &low->cs_mask) && 57462fa74d9SJeff Roberson tdq->tdq_load < low->cs_load && 57562fa74d9SJeff Roberson tdq->tdq_lowpri > low->cs_limit) { 57662fa74d9SJeff Roberson low->cs_cpu = cpu; 57762fa74d9SJeff Roberson low->cs_load = tdq->tdq_load; 57862fa74d9SJeff Roberson } 57962fa74d9SJeff Roberson if (match & CPU_SEARCH_HIGHEST) 580c76ee827SJeff Roberson if (CPU_ISSET(cpu, &high->cs_mask) && 58162fa74d9SJeff Roberson tdq->tdq_load >= high->cs_limit && 58262fa74d9SJeff Roberson tdq->tdq_load > high->cs_load && 58362fa74d9SJeff Roberson tdq->tdq_transferable) { 58462fa74d9SJeff Roberson high->cs_cpu = cpu; 58562fa74d9SJeff Roberson high->cs_load = tdq->tdq_load; 58662fa74d9SJeff Roberson } 58762fa74d9SJeff Roberson return (tdq->tdq_load); 58862fa74d9SJeff Roberson } 58962fa74d9SJeff Roberson 59062fa74d9SJeff Roberson /* 59162fa74d9SJeff Roberson * Search the tree of cpu_groups for the lowest or highest loaded cpu 59262fa74d9SJeff Roberson * according to the match argument. This routine actually compares the 59362fa74d9SJeff Roberson * load on all paths through the tree and finds the least loaded cpu on 59462fa74d9SJeff Roberson * the least loaded path, which may differ from the least loaded cpu in 59562fa74d9SJeff Roberson * the system. This balances work among caches and busses. 59662fa74d9SJeff Roberson * 59762fa74d9SJeff Roberson * This inline is instantiated in three forms below using constants for the 59862fa74d9SJeff Roberson * match argument. It is reduced to the minimum set for each case. It is 59962fa74d9SJeff Roberson * also recursive to the depth of the tree. 60062fa74d9SJeff Roberson */ 601d628fbfaSJohn Baldwin static __inline int 60262fa74d9SJeff Roberson cpu_search(struct cpu_group *cg, struct cpu_search *low, 60362fa74d9SJeff Roberson struct cpu_search *high, const int match) 60462fa74d9SJeff Roberson { 60562fa74d9SJeff Roberson int total; 60662fa74d9SJeff Roberson 60762fa74d9SJeff Roberson total = 0; 60862fa74d9SJeff Roberson if (cg->cg_children) { 60962fa74d9SJeff Roberson struct cpu_search lgroup; 61062fa74d9SJeff Roberson struct cpu_search hgroup; 61162fa74d9SJeff Roberson struct cpu_group *child; 61262fa74d9SJeff Roberson u_int lload; 61362fa74d9SJeff Roberson int hload; 61462fa74d9SJeff Roberson int load; 61562fa74d9SJeff Roberson int i; 61662fa74d9SJeff Roberson 61762fa74d9SJeff Roberson lload = -1; 61862fa74d9SJeff Roberson hload = -1; 61962fa74d9SJeff Roberson for (i = 0; i < cg->cg_children; i++) { 62062fa74d9SJeff Roberson child = &cg->cg_child[i]; 62162fa74d9SJeff Roberson if (match & CPU_SEARCH_LOWEST) { 62262fa74d9SJeff Roberson lgroup = *low; 62362fa74d9SJeff Roberson lgroup.cs_load = -1; 62462fa74d9SJeff Roberson } 62562fa74d9SJeff Roberson if (match & CPU_SEARCH_HIGHEST) { 62662fa74d9SJeff Roberson hgroup = *high; 62762fa74d9SJeff Roberson lgroup.cs_load = 0; 62862fa74d9SJeff Roberson } 62962fa74d9SJeff Roberson switch (match) { 63062fa74d9SJeff Roberson case CPU_SEARCH_LOWEST: 63162fa74d9SJeff Roberson load = cpu_search_lowest(child, &lgroup); 63262fa74d9SJeff Roberson break; 63362fa74d9SJeff Roberson case CPU_SEARCH_HIGHEST: 63462fa74d9SJeff Roberson load = cpu_search_highest(child, &hgroup); 63562fa74d9SJeff Roberson break; 63662fa74d9SJeff Roberson case CPU_SEARCH_BOTH: 63762fa74d9SJeff Roberson load = cpu_search_both(child, &lgroup, &hgroup); 63862fa74d9SJeff Roberson break; 63962fa74d9SJeff Roberson } 64062fa74d9SJeff Roberson total += load; 64162fa74d9SJeff Roberson if (match & CPU_SEARCH_LOWEST) 64262fa74d9SJeff Roberson if (load < lload || low->cs_cpu == -1) { 64362fa74d9SJeff Roberson *low = lgroup; 64462fa74d9SJeff Roberson lload = load; 64562fa74d9SJeff Roberson } 64662fa74d9SJeff Roberson if (match & CPU_SEARCH_HIGHEST) 64762fa74d9SJeff Roberson if (load > hload || high->cs_cpu == -1) { 64862fa74d9SJeff Roberson hload = load; 64962fa74d9SJeff Roberson *high = hgroup; 65062fa74d9SJeff Roberson } 65162fa74d9SJeff Roberson } 65262fa74d9SJeff Roberson } else { 65362fa74d9SJeff Roberson int cpu; 65462fa74d9SJeff Roberson 655c76ee827SJeff Roberson CPUSET_FOREACH(cpu, cg->cg_mask) 65662fa74d9SJeff Roberson total += cpu_compare(cpu, low, high, match); 65762fa74d9SJeff Roberson } 65862fa74d9SJeff Roberson return (total); 65962fa74d9SJeff Roberson } 66062fa74d9SJeff Roberson 66162fa74d9SJeff Roberson /* 66262fa74d9SJeff Roberson * cpu_search instantiations must pass constants to maintain the inline 66362fa74d9SJeff Roberson * optimization. 66462fa74d9SJeff Roberson */ 66562fa74d9SJeff Roberson int 66662fa74d9SJeff Roberson cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low) 66762fa74d9SJeff Roberson { 66862fa74d9SJeff Roberson return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST); 66962fa74d9SJeff Roberson } 67062fa74d9SJeff Roberson 67162fa74d9SJeff Roberson int 67262fa74d9SJeff Roberson cpu_search_highest(struct cpu_group *cg, struct cpu_search *high) 67362fa74d9SJeff Roberson { 67462fa74d9SJeff Roberson return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST); 67562fa74d9SJeff Roberson } 67662fa74d9SJeff Roberson 67762fa74d9SJeff Roberson int 67862fa74d9SJeff Roberson cpu_search_both(struct cpu_group *cg, struct cpu_search *low, 67962fa74d9SJeff Roberson struct cpu_search *high) 68062fa74d9SJeff Roberson { 68162fa74d9SJeff Roberson return cpu_search(cg, low, high, CPU_SEARCH_BOTH); 68262fa74d9SJeff Roberson } 68362fa74d9SJeff Roberson 68462fa74d9SJeff Roberson /* 68562fa74d9SJeff Roberson * Find the cpu with the least load via the least loaded path that has a 68662fa74d9SJeff Roberson * lowpri greater than pri pri. A pri of -1 indicates any priority is 68762fa74d9SJeff Roberson * acceptable. 68862fa74d9SJeff Roberson */ 68962fa74d9SJeff Roberson static inline int 690c76ee827SJeff Roberson sched_lowest(struct cpu_group *cg, cpuset_t mask, int pri) 69162fa74d9SJeff Roberson { 69262fa74d9SJeff Roberson struct cpu_search low; 69362fa74d9SJeff Roberson 69462fa74d9SJeff Roberson low.cs_cpu = -1; 69562fa74d9SJeff Roberson low.cs_load = -1; 69662fa74d9SJeff Roberson low.cs_mask = mask; 69762fa74d9SJeff Roberson low.cs_limit = pri; 69862fa74d9SJeff Roberson cpu_search_lowest(cg, &low); 69962fa74d9SJeff Roberson return low.cs_cpu; 70062fa74d9SJeff Roberson } 70162fa74d9SJeff Roberson 70262fa74d9SJeff Roberson /* 70362fa74d9SJeff Roberson * Find the cpu with the highest load via the highest loaded path. 70462fa74d9SJeff Roberson */ 70562fa74d9SJeff Roberson static inline int 706c76ee827SJeff Roberson sched_highest(struct cpu_group *cg, cpuset_t mask, int minload) 70762fa74d9SJeff Roberson { 70862fa74d9SJeff Roberson struct cpu_search high; 70962fa74d9SJeff Roberson 71062fa74d9SJeff Roberson high.cs_cpu = -1; 71162fa74d9SJeff Roberson high.cs_load = 0; 71262fa74d9SJeff Roberson high.cs_mask = mask; 71362fa74d9SJeff Roberson high.cs_limit = minload; 71462fa74d9SJeff Roberson cpu_search_highest(cg, &high); 71562fa74d9SJeff Roberson return high.cs_cpu; 71662fa74d9SJeff Roberson } 71762fa74d9SJeff Roberson 71862fa74d9SJeff Roberson /* 71962fa74d9SJeff Roberson * Simultaneously find the highest and lowest loaded cpu reachable via 72062fa74d9SJeff Roberson * cg. 72162fa74d9SJeff Roberson */ 72262fa74d9SJeff Roberson static inline void 723c76ee827SJeff Roberson sched_both(struct cpu_group *cg, cpuset_t mask, int *lowcpu, int *highcpu) 72462fa74d9SJeff Roberson { 72562fa74d9SJeff Roberson struct cpu_search high; 72662fa74d9SJeff Roberson struct cpu_search low; 72762fa74d9SJeff Roberson 72862fa74d9SJeff Roberson low.cs_cpu = -1; 72962fa74d9SJeff Roberson low.cs_limit = -1; 73062fa74d9SJeff Roberson low.cs_load = -1; 73162fa74d9SJeff Roberson low.cs_mask = mask; 73262fa74d9SJeff Roberson high.cs_load = 0; 73362fa74d9SJeff Roberson high.cs_cpu = -1; 73462fa74d9SJeff Roberson high.cs_limit = -1; 73562fa74d9SJeff Roberson high.cs_mask = mask; 73662fa74d9SJeff Roberson cpu_search_both(cg, &low, &high); 73762fa74d9SJeff Roberson *lowcpu = low.cs_cpu; 73862fa74d9SJeff Roberson *highcpu = high.cs_cpu; 73962fa74d9SJeff Roberson return; 74062fa74d9SJeff Roberson } 74162fa74d9SJeff Roberson 74262fa74d9SJeff Roberson static void 74362fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg) 74462fa74d9SJeff Roberson { 745c76ee827SJeff Roberson cpuset_t mask; 74662fa74d9SJeff Roberson int high; 74762fa74d9SJeff Roberson int low; 74862fa74d9SJeff Roberson int i; 74962fa74d9SJeff Roberson 750c76ee827SJeff Roberson CPU_FILL(&mask); 75162fa74d9SJeff Roberson for (;;) { 75262fa74d9SJeff Roberson sched_both(cg, mask, &low, &high); 75362fa74d9SJeff Roberson if (low == high || low == -1 || high == -1) 75462fa74d9SJeff Roberson break; 75562fa74d9SJeff Roberson if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low))) 75662fa74d9SJeff Roberson break; 75762fa74d9SJeff Roberson /* 75862fa74d9SJeff Roberson * If we failed to move any threads determine which cpu 75962fa74d9SJeff Roberson * to kick out of the set and try again. 76062fa74d9SJeff Roberson */ 76162fa74d9SJeff Roberson if (TDQ_CPU(high)->tdq_transferable == 0) 762c76ee827SJeff Roberson CPU_CLR(high, &mask); 76362fa74d9SJeff Roberson else 764c76ee827SJeff Roberson CPU_CLR(low, &mask); 76562fa74d9SJeff Roberson } 76662fa74d9SJeff Roberson 76762fa74d9SJeff Roberson for (i = 0; i < cg->cg_children; i++) 76862fa74d9SJeff Roberson sched_balance_group(&cg->cg_child[i]); 76962fa74d9SJeff Roberson } 77062fa74d9SJeff Roberson 77162fa74d9SJeff Roberson static void 77262375ca8SEd Schouten sched_balance(void) 773356500a3SJeff Roberson { 7747fcf154aSJeff Roberson struct tdq *tdq; 775356500a3SJeff Roberson 7767fcf154aSJeff Roberson /* 7777fcf154aSJeff Roberson * Select a random time between .5 * balance_interval and 7787fcf154aSJeff Roberson * 1.5 * balance_interval. 7797fcf154aSJeff Roberson */ 7807fcf154aSJeff Roberson balance_ticks = max(balance_interval / 2, 1); 7817fcf154aSJeff Roberson balance_ticks += random() % balance_interval; 782ae7a6b38SJeff Roberson if (smp_started == 0 || rebalance == 0) 783598b368dSJeff Roberson return; 7847fcf154aSJeff Roberson tdq = TDQ_SELF(); 7857fcf154aSJeff Roberson TDQ_UNLOCK(tdq); 78662fa74d9SJeff Roberson sched_balance_group(cpu_top); 7877fcf154aSJeff Roberson TDQ_LOCK(tdq); 788cac77d04SJeff Roberson } 78986f8ae96SJeff Roberson 790ae7a6b38SJeff Roberson /* 791ae7a6b38SJeff Roberson * Lock two thread queues using their address to maintain lock order. 792ae7a6b38SJeff Roberson */ 793ae7a6b38SJeff Roberson static void 794ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two) 795ae7a6b38SJeff Roberson { 796ae7a6b38SJeff Roberson if (one < two) { 797ae7a6b38SJeff Roberson TDQ_LOCK(one); 798ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(two, MTX_DUPOK); 799ae7a6b38SJeff Roberson } else { 800ae7a6b38SJeff Roberson TDQ_LOCK(two); 801ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(one, MTX_DUPOK); 802ae7a6b38SJeff Roberson } 803ae7a6b38SJeff Roberson } 804ae7a6b38SJeff Roberson 805ae7a6b38SJeff Roberson /* 8067fcf154aSJeff Roberson * Unlock two thread queues. Order is not important here. 8077fcf154aSJeff Roberson */ 8087fcf154aSJeff Roberson static void 8097fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two) 8107fcf154aSJeff Roberson { 8117fcf154aSJeff Roberson TDQ_UNLOCK(one); 8127fcf154aSJeff Roberson TDQ_UNLOCK(two); 8137fcf154aSJeff Roberson } 8147fcf154aSJeff Roberson 8157fcf154aSJeff Roberson /* 816ae7a6b38SJeff Roberson * Transfer load between two imbalanced thread queues. 817ae7a6b38SJeff Roberson */ 81862fa74d9SJeff Roberson static int 819ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low) 820cac77d04SJeff Roberson { 821cac77d04SJeff Roberson int transferable; 822cac77d04SJeff Roberson int high_load; 823cac77d04SJeff Roberson int low_load; 82462fa74d9SJeff Roberson int moved; 825cac77d04SJeff Roberson int move; 826cac77d04SJeff Roberson int diff; 827cac77d04SJeff Roberson int i; 828cac77d04SJeff Roberson 829ae7a6b38SJeff Roberson tdq_lock_pair(high, low); 830d2ad694cSJeff Roberson transferable = high->tdq_transferable; 831d2ad694cSJeff Roberson high_load = high->tdq_load; 832d2ad694cSJeff Roberson low_load = low->tdq_load; 83362fa74d9SJeff Roberson moved = 0; 834155b9987SJeff Roberson /* 835155b9987SJeff Roberson * Determine what the imbalance is and then adjust that to how many 836d2ad694cSJeff Roberson * threads we actually have to give up (transferable). 837155b9987SJeff Roberson */ 838ae7a6b38SJeff Roberson if (transferable != 0) { 839cac77d04SJeff Roberson diff = high_load - low_load; 840356500a3SJeff Roberson move = diff / 2; 841356500a3SJeff Roberson if (diff & 0x1) 842356500a3SJeff Roberson move++; 84380f86c9fSJeff Roberson move = min(move, transferable); 844356500a3SJeff Roberson for (i = 0; i < move; i++) 84562fa74d9SJeff Roberson moved += tdq_move(high, low); 846a5423ea3SJeff Roberson /* 847a5423ea3SJeff Roberson * IPI the target cpu to force it to reschedule with the new 848a5423ea3SJeff Roberson * workload. 849a5423ea3SJeff Roberson */ 850d9d8d144SJohn Baldwin ipi_cpu(TDQ_ID(low), IPI_PREEMPT); 851ae7a6b38SJeff Roberson } 8527fcf154aSJeff Roberson tdq_unlock_pair(high, low); 85362fa74d9SJeff Roberson return (moved); 854356500a3SJeff Roberson } 855356500a3SJeff Roberson 856ae7a6b38SJeff Roberson /* 857ae7a6b38SJeff Roberson * Move a thread from one thread queue to another. 858ae7a6b38SJeff Roberson */ 85962fa74d9SJeff Roberson static int 860ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to) 861356500a3SJeff Roberson { 862ad1e7d28SJulian Elischer struct td_sched *ts; 863ae7a6b38SJeff Roberson struct thread *td; 864ae7a6b38SJeff Roberson struct tdq *tdq; 865ae7a6b38SJeff Roberson int cpu; 866356500a3SJeff Roberson 8677fcf154aSJeff Roberson TDQ_LOCK_ASSERT(from, MA_OWNED); 8687fcf154aSJeff Roberson TDQ_LOCK_ASSERT(to, MA_OWNED); 8697fcf154aSJeff Roberson 870ad1e7d28SJulian Elischer tdq = from; 871ae7a6b38SJeff Roberson cpu = TDQ_ID(to); 8729727e637SJeff Roberson td = tdq_steal(tdq, cpu); 8739727e637SJeff Roberson if (td == NULL) 87462fa74d9SJeff Roberson return (0); 8759727e637SJeff Roberson ts = td->td_sched; 876ae7a6b38SJeff Roberson /* 877ae7a6b38SJeff Roberson * Although the run queue is locked the thread may be blocked. Lock 8787fcf154aSJeff Roberson * it to clear this and acquire the run-queue lock. 879ae7a6b38SJeff Roberson */ 880ae7a6b38SJeff Roberson thread_lock(td); 8817fcf154aSJeff Roberson /* Drop recursive lock on from acquired via thread_lock(). */ 882ae7a6b38SJeff Roberson TDQ_UNLOCK(from); 883ae7a6b38SJeff Roberson sched_rem(td); 8847b8bfa0dSJeff Roberson ts->ts_cpu = cpu; 885ae7a6b38SJeff Roberson td->td_lock = TDQ_LOCKPTR(to); 886ae7a6b38SJeff Roberson tdq_add(to, td, SRQ_YIELDING); 88762fa74d9SJeff Roberson return (1); 888356500a3SJeff Roberson } 88922bf7d9aSJeff Roberson 890ae7a6b38SJeff Roberson /* 891ae7a6b38SJeff Roberson * This tdq has idled. Try to steal a thread from another cpu and switch 892ae7a6b38SJeff Roberson * to it. 893ae7a6b38SJeff Roberson */ 89480f86c9fSJeff Roberson static int 895ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq) 89622bf7d9aSJeff Roberson { 89762fa74d9SJeff Roberson struct cpu_group *cg; 898ad1e7d28SJulian Elischer struct tdq *steal; 899c76ee827SJeff Roberson cpuset_t mask; 90062fa74d9SJeff Roberson int thresh; 901ae7a6b38SJeff Roberson int cpu; 90280f86c9fSJeff Roberson 90388f530ccSJeff Roberson if (smp_started == 0 || steal_idle == 0) 90488f530ccSJeff Roberson return (1); 905c76ee827SJeff Roberson CPU_FILL(&mask); 906c76ee827SJeff Roberson CPU_CLR(PCPU_GET(cpuid), &mask); 90762fa74d9SJeff Roberson /* We don't want to be preempted while we're iterating. */ 908ae7a6b38SJeff Roberson spinlock_enter(); 90962fa74d9SJeff Roberson for (cg = tdq->tdq_cg; cg != NULL; ) { 9107b55ab05SJeff Roberson if ((cg->cg_flags & CG_FLAG_THREAD) == 0) 91162fa74d9SJeff Roberson thresh = steal_thresh; 91262fa74d9SJeff Roberson else 91362fa74d9SJeff Roberson thresh = 1; 91462fa74d9SJeff Roberson cpu = sched_highest(cg, mask, thresh); 91562fa74d9SJeff Roberson if (cpu == -1) { 91662fa74d9SJeff Roberson cg = cg->cg_parent; 91780f86c9fSJeff Roberson continue; 9187b8bfa0dSJeff Roberson } 9197b8bfa0dSJeff Roberson steal = TDQ_CPU(cpu); 920c76ee827SJeff Roberson CPU_CLR(cpu, &mask); 9217fcf154aSJeff Roberson tdq_lock_pair(tdq, steal); 92262fa74d9SJeff Roberson if (steal->tdq_load < thresh || steal->tdq_transferable == 0) { 9237fcf154aSJeff Roberson tdq_unlock_pair(tdq, steal); 92462fa74d9SJeff Roberson continue; 92562fa74d9SJeff Roberson } 92662fa74d9SJeff Roberson /* 92762fa74d9SJeff Roberson * If a thread was added while interrupts were disabled don't 92862fa74d9SJeff Roberson * steal one here. If we fail to acquire one due to affinity 92962fa74d9SJeff Roberson * restrictions loop again with this cpu removed from the 93062fa74d9SJeff Roberson * set. 93162fa74d9SJeff Roberson */ 93262fa74d9SJeff Roberson if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) { 93362fa74d9SJeff Roberson tdq_unlock_pair(tdq, steal); 93462fa74d9SJeff Roberson continue; 93580f86c9fSJeff Roberson } 936ae7a6b38SJeff Roberson spinlock_exit(); 937ae7a6b38SJeff Roberson TDQ_UNLOCK(steal); 9388df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IDLE, NULL); 939ae7a6b38SJeff Roberson thread_unlock(curthread); 9407b8bfa0dSJeff Roberson 9417b8bfa0dSJeff Roberson return (0); 94222bf7d9aSJeff Roberson } 94362fa74d9SJeff Roberson spinlock_exit(); 94462fa74d9SJeff Roberson return (1); 94562fa74d9SJeff Roberson } 94622bf7d9aSJeff Roberson 947ae7a6b38SJeff Roberson /* 948ae7a6b38SJeff Roberson * Notify a remote cpu of new work. Sends an IPI if criteria are met. 949ae7a6b38SJeff Roberson */ 95022bf7d9aSJeff Roberson static void 9519727e637SJeff Roberson tdq_notify(struct tdq *tdq, struct thread *td) 95222bf7d9aSJeff Roberson { 95302f0ff6dSJohn Baldwin struct thread *ctd; 954fc3a97dcSJeff Roberson int pri; 9557b8bfa0dSJeff Roberson int cpu; 95622bf7d9aSJeff Roberson 957ff256d9cSJeff Roberson if (tdq->tdq_ipipending) 958ff256d9cSJeff Roberson return; 9599727e637SJeff Roberson cpu = td->td_sched->ts_cpu; 9609727e637SJeff Roberson pri = td->td_priority; 96102f0ff6dSJohn Baldwin ctd = pcpu_find(cpu)->pc_curthread; 96202f0ff6dSJohn Baldwin if (!sched_shouldpreempt(pri, ctd->td_priority, 1)) 9636b2f763fSJeff Roberson return; 96402f0ff6dSJohn Baldwin if (TD_IS_IDLETHREAD(ctd)) { 9651690c6c1SJeff Roberson /* 9666c47aaaeSJeff Roberson * If the MD code has an idle wakeup routine try that before 9676c47aaaeSJeff Roberson * falling back to IPI. 9686c47aaaeSJeff Roberson */ 9696c47aaaeSJeff Roberson if (cpu_idle_wakeup(cpu)) 9706c47aaaeSJeff Roberson return; 9711690c6c1SJeff Roberson } 972ff256d9cSJeff Roberson tdq->tdq_ipipending = 1; 973d9d8d144SJohn Baldwin ipi_cpu(cpu, IPI_PREEMPT); 97422bf7d9aSJeff Roberson } 97522bf7d9aSJeff Roberson 976ae7a6b38SJeff Roberson /* 977ae7a6b38SJeff Roberson * Steals load from a timeshare queue. Honors the rotating queue head 978ae7a6b38SJeff Roberson * index. 979ae7a6b38SJeff Roberson */ 9809727e637SJeff Roberson static struct thread * 98162fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start) 982ae7a6b38SJeff Roberson { 983ae7a6b38SJeff Roberson struct rqbits *rqb; 984ae7a6b38SJeff Roberson struct rqhead *rqh; 9859727e637SJeff Roberson struct thread *td; 986ae7a6b38SJeff Roberson int first; 987ae7a6b38SJeff Roberson int bit; 988ae7a6b38SJeff Roberson int pri; 989ae7a6b38SJeff Roberson int i; 990ae7a6b38SJeff Roberson 991ae7a6b38SJeff Roberson rqb = &rq->rq_status; 992ae7a6b38SJeff Roberson bit = start & (RQB_BPW -1); 993ae7a6b38SJeff Roberson pri = 0; 994ae7a6b38SJeff Roberson first = 0; 995ae7a6b38SJeff Roberson again: 996ae7a6b38SJeff Roberson for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 997ae7a6b38SJeff Roberson if (rqb->rqb_bits[i] == 0) 998ae7a6b38SJeff Roberson continue; 999ae7a6b38SJeff Roberson if (bit != 0) { 1000ae7a6b38SJeff Roberson for (pri = bit; pri < RQB_BPW; pri++) 1001ae7a6b38SJeff Roberson if (rqb->rqb_bits[i] & (1ul << pri)) 1002ae7a6b38SJeff Roberson break; 1003ae7a6b38SJeff Roberson if (pri >= RQB_BPW) 1004ae7a6b38SJeff Roberson continue; 1005ae7a6b38SJeff Roberson } else 1006ae7a6b38SJeff Roberson pri = RQB_FFS(rqb->rqb_bits[i]); 1007ae7a6b38SJeff Roberson pri += (i << RQB_L2BPW); 1008ae7a6b38SJeff Roberson rqh = &rq->rq_queues[pri]; 10099727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 10109727e637SJeff Roberson if (first && THREAD_CAN_MIGRATE(td) && 10119727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 10129727e637SJeff Roberson return (td); 1013ae7a6b38SJeff Roberson first = 1; 1014ae7a6b38SJeff Roberson } 1015ae7a6b38SJeff Roberson } 1016ae7a6b38SJeff Roberson if (start != 0) { 1017ae7a6b38SJeff Roberson start = 0; 1018ae7a6b38SJeff Roberson goto again; 1019ae7a6b38SJeff Roberson } 1020ae7a6b38SJeff Roberson 1021ae7a6b38SJeff Roberson return (NULL); 1022ae7a6b38SJeff Roberson } 1023ae7a6b38SJeff Roberson 1024ae7a6b38SJeff Roberson /* 1025ae7a6b38SJeff Roberson * Steals load from a standard linear queue. 1026ae7a6b38SJeff Roberson */ 10279727e637SJeff Roberson static struct thread * 102862fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu) 102922bf7d9aSJeff Roberson { 103022bf7d9aSJeff Roberson struct rqhead *rqh; 103122bf7d9aSJeff Roberson struct rqbits *rqb; 10329727e637SJeff Roberson struct thread *td; 103322bf7d9aSJeff Roberson int word; 103422bf7d9aSJeff Roberson int bit; 103522bf7d9aSJeff Roberson 103622bf7d9aSJeff Roberson rqb = &rq->rq_status; 103722bf7d9aSJeff Roberson for (word = 0; word < RQB_LEN; word++) { 103822bf7d9aSJeff Roberson if (rqb->rqb_bits[word] == 0) 103922bf7d9aSJeff Roberson continue; 104022bf7d9aSJeff Roberson for (bit = 0; bit < RQB_BPW; bit++) { 1041a2640c9bSPeter Wemm if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 104222bf7d9aSJeff Roberson continue; 104322bf7d9aSJeff Roberson rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 10449727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) 10459727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td) && 10469727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 10479727e637SJeff Roberson return (td); 104822bf7d9aSJeff Roberson } 104922bf7d9aSJeff Roberson } 105022bf7d9aSJeff Roberson return (NULL); 105122bf7d9aSJeff Roberson } 105222bf7d9aSJeff Roberson 1053ae7a6b38SJeff Roberson /* 1054ae7a6b38SJeff Roberson * Attempt to steal a thread in priority order from a thread queue. 1055ae7a6b38SJeff Roberson */ 10569727e637SJeff Roberson static struct thread * 105762fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu) 105822bf7d9aSJeff Roberson { 10599727e637SJeff Roberson struct thread *td; 106022bf7d9aSJeff Roberson 1061ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 10629727e637SJeff Roberson if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 10639727e637SJeff Roberson return (td); 10649727e637SJeff Roberson if ((td = runq_steal_from(&tdq->tdq_timeshare, 10659727e637SJeff Roberson cpu, tdq->tdq_ridx)) != NULL) 10669727e637SJeff Roberson return (td); 106762fa74d9SJeff Roberson return (runq_steal(&tdq->tdq_idle, cpu)); 106822bf7d9aSJeff Roberson } 106980f86c9fSJeff Roberson 1070ae7a6b38SJeff Roberson /* 1071ae7a6b38SJeff Roberson * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 10727fcf154aSJeff Roberson * current lock and returns with the assigned queue locked. 1073ae7a6b38SJeff Roberson */ 1074ae7a6b38SJeff Roberson static inline struct tdq * 10759727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags) 107680f86c9fSJeff Roberson { 10779727e637SJeff Roberson 1078ae7a6b38SJeff Roberson struct tdq *tdq; 107980f86c9fSJeff Roberson 10809727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1081ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpu); 10829727e637SJeff Roberson td->td_sched->ts_cpu = cpu; 10839727e637SJeff Roberson /* 10849727e637SJeff Roberson * If the lock matches just return the queue. 10859727e637SJeff Roberson */ 1086ae7a6b38SJeff Roberson if (td->td_lock == TDQ_LOCKPTR(tdq)) 1087ae7a6b38SJeff Roberson return (tdq); 1088ae7a6b38SJeff Roberson #ifdef notyet 108980f86c9fSJeff Roberson /* 1090a5423ea3SJeff Roberson * If the thread isn't running its lockptr is a 1091ae7a6b38SJeff Roberson * turnstile or a sleepqueue. We can just lock_set without 1092ae7a6b38SJeff Roberson * blocking. 1093670c524fSJeff Roberson */ 1094ae7a6b38SJeff Roberson if (TD_CAN_RUN(td)) { 1095ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1096ae7a6b38SJeff Roberson thread_lock_set(td, TDQ_LOCKPTR(tdq)); 1097ae7a6b38SJeff Roberson return (tdq); 1098ae7a6b38SJeff Roberson } 1099ae7a6b38SJeff Roberson #endif 110080f86c9fSJeff Roberson /* 1101ae7a6b38SJeff Roberson * The hard case, migration, we need to block the thread first to 1102ae7a6b38SJeff Roberson * prevent order reversals with other cpus locks. 11037b8bfa0dSJeff Roberson */ 1104b0b9dee5SAttilio Rao spinlock_enter(); 1105ae7a6b38SJeff Roberson thread_lock_block(td); 1106ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1107ae7a6b38SJeff Roberson thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1108b0b9dee5SAttilio Rao spinlock_exit(); 1109ae7a6b38SJeff Roberson return (tdq); 111080f86c9fSJeff Roberson } 11112454aaf5SJeff Roberson 11128df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 11138df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 11148df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 11158df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 11168df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 11178df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 11188df78c41SJeff Roberson 1119ae7a6b38SJeff Roberson static int 11209727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags) 1121ae7a6b38SJeff Roberson { 112262fa74d9SJeff Roberson struct cpu_group *cg; 11239727e637SJeff Roberson struct td_sched *ts; 1124ae7a6b38SJeff Roberson struct tdq *tdq; 1125c76ee827SJeff Roberson cpuset_t mask; 11267b8bfa0dSJeff Roberson int self; 11277b8bfa0dSJeff Roberson int pri; 11287b8bfa0dSJeff Roberson int cpu; 11297b8bfa0dSJeff Roberson 113062fa74d9SJeff Roberson self = PCPU_GET(cpuid); 11319727e637SJeff Roberson ts = td->td_sched; 11327b8bfa0dSJeff Roberson if (smp_started == 0) 11337b8bfa0dSJeff Roberson return (self); 113428994a58SJeff Roberson /* 113528994a58SJeff Roberson * Don't migrate a running thread from sched_switch(). 113628994a58SJeff Roberson */ 113762fa74d9SJeff Roberson if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 113862fa74d9SJeff Roberson return (ts->ts_cpu); 11397b8bfa0dSJeff Roberson /* 114062fa74d9SJeff Roberson * Prefer to run interrupt threads on the processors that generate 114162fa74d9SJeff Roberson * the interrupt. 11427b8bfa0dSJeff Roberson */ 114362fa74d9SJeff Roberson if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 11448df78c41SJeff Roberson curthread->td_intr_nesting_level && ts->ts_cpu != self) { 11458df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_intrbind); 114662fa74d9SJeff Roberson ts->ts_cpu = self; 11478df78c41SJeff Roberson } 114862fa74d9SJeff Roberson /* 114962fa74d9SJeff Roberson * If the thread can run on the last cpu and the affinity has not 115062fa74d9SJeff Roberson * expired or it is idle run it there. 115162fa74d9SJeff Roberson */ 115262fa74d9SJeff Roberson pri = td->td_priority; 115362fa74d9SJeff Roberson tdq = TDQ_CPU(ts->ts_cpu); 115462fa74d9SJeff Roberson if (THREAD_CAN_SCHED(td, ts->ts_cpu)) { 11558df78c41SJeff Roberson if (tdq->tdq_lowpri > PRI_MIN_IDLE) { 11568df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_idle_affinity); 115762fa74d9SJeff Roberson return (ts->ts_cpu); 11588df78c41SJeff Roberson } 11598df78c41SJeff Roberson if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri) { 11608df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_affinity); 11617b8bfa0dSJeff Roberson return (ts->ts_cpu); 11627b8bfa0dSJeff Roberson } 11638df78c41SJeff Roberson } 11647b8bfa0dSJeff Roberson /* 116562fa74d9SJeff Roberson * Search for the highest level in the tree that still has affinity. 11667b8bfa0dSJeff Roberson */ 116762fa74d9SJeff Roberson cg = NULL; 116862fa74d9SJeff Roberson for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent) 116962fa74d9SJeff Roberson if (SCHED_AFFINITY(ts, cg->cg_level)) 117062fa74d9SJeff Roberson break; 117162fa74d9SJeff Roberson cpu = -1; 1172c76ee827SJeff Roberson mask = td->td_cpuset->cs_mask; 117362fa74d9SJeff Roberson if (cg) 117462fa74d9SJeff Roberson cpu = sched_lowest(cg, mask, pri); 117562fa74d9SJeff Roberson if (cpu == -1) 117662fa74d9SJeff Roberson cpu = sched_lowest(cpu_top, mask, -1); 117762fa74d9SJeff Roberson /* 117862fa74d9SJeff Roberson * Compare the lowest loaded cpu to current cpu. 117962fa74d9SJeff Roberson */ 1180ff256d9cSJeff Roberson if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri && 11818df78c41SJeff Roberson TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) { 11828df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_local); 118362fa74d9SJeff Roberson cpu = self; 11848df78c41SJeff Roberson } else 11858df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_lowest); 11868df78c41SJeff Roberson if (cpu != ts->ts_cpu) 11878df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_migration); 1188ff256d9cSJeff Roberson KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu.")); 1189ae7a6b38SJeff Roberson return (cpu); 119080f86c9fSJeff Roberson } 119162fa74d9SJeff Roberson #endif 119222bf7d9aSJeff Roberson 119322bf7d9aSJeff Roberson /* 119422bf7d9aSJeff Roberson * Pick the highest priority task we have and return it. 11950c0a98b2SJeff Roberson */ 11969727e637SJeff Roberson static struct thread * 1197ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq) 11985d7ef00cSJeff Roberson { 11999727e637SJeff Roberson struct thread *td; 12005d7ef00cSJeff Roberson 1201ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 12029727e637SJeff Roberson td = runq_choose(&tdq->tdq_realtime); 12039727e637SJeff Roberson if (td != NULL) 12049727e637SJeff Roberson return (td); 12059727e637SJeff Roberson td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 12069727e637SJeff Roberson if (td != NULL) { 12079727e637SJeff Roberson KASSERT(td->td_priority >= PRI_MIN_TIMESHARE, 1208e7d50326SJeff Roberson ("tdq_choose: Invalid priority on timeshare queue %d", 12099727e637SJeff Roberson td->td_priority)); 12109727e637SJeff Roberson return (td); 121115dc847eSJeff Roberson } 12129727e637SJeff Roberson td = runq_choose(&tdq->tdq_idle); 12139727e637SJeff Roberson if (td != NULL) { 12149727e637SJeff Roberson KASSERT(td->td_priority >= PRI_MIN_IDLE, 1215e7d50326SJeff Roberson ("tdq_choose: Invalid priority on idle queue %d", 12169727e637SJeff Roberson td->td_priority)); 12179727e637SJeff Roberson return (td); 1218e7d50326SJeff Roberson } 1219e7d50326SJeff Roberson 1220e7d50326SJeff Roberson return (NULL); 1221245f3abfSJeff Roberson } 12220a016a05SJeff Roberson 1223ae7a6b38SJeff Roberson /* 1224ae7a6b38SJeff Roberson * Initialize a thread queue. 1225ae7a6b38SJeff Roberson */ 12260a016a05SJeff Roberson static void 1227ad1e7d28SJulian Elischer tdq_setup(struct tdq *tdq) 12280a016a05SJeff Roberson { 1229ae7a6b38SJeff Roberson 1230c47f202bSJeff Roberson if (bootverbose) 1231c47f202bSJeff Roberson printf("ULE: setup cpu %d\n", TDQ_ID(tdq)); 1232e7d50326SJeff Roberson runq_init(&tdq->tdq_realtime); 1233e7d50326SJeff Roberson runq_init(&tdq->tdq_timeshare); 1234d2ad694cSJeff Roberson runq_init(&tdq->tdq_idle); 123562fa74d9SJeff Roberson snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 123662fa74d9SJeff Roberson "sched lock %d", (int)TDQ_ID(tdq)); 123762fa74d9SJeff Roberson mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", 123862fa74d9SJeff Roberson MTX_SPIN | MTX_RECURSE); 12398f51ad55SJeff Roberson #ifdef KTR 12408f51ad55SJeff Roberson snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 12418f51ad55SJeff Roberson "CPU %d load", (int)TDQ_ID(tdq)); 12428f51ad55SJeff Roberson #endif 12430a016a05SJeff Roberson } 12440a016a05SJeff Roberson 1245c47f202bSJeff Roberson #ifdef SMP 1246c47f202bSJeff Roberson static void 1247c47f202bSJeff Roberson sched_setup_smp(void) 1248c47f202bSJeff Roberson { 1249c47f202bSJeff Roberson struct tdq *tdq; 1250c47f202bSJeff Roberson int i; 1251c47f202bSJeff Roberson 125262fa74d9SJeff Roberson cpu_top = smp_topo(); 12533aa6d94eSJohn Baldwin CPU_FOREACH(i) { 125462fa74d9SJeff Roberson tdq = TDQ_CPU(i); 1255c47f202bSJeff Roberson tdq_setup(tdq); 125662fa74d9SJeff Roberson tdq->tdq_cg = smp_topo_find(cpu_top, i); 125762fa74d9SJeff Roberson if (tdq->tdq_cg == NULL) 125862fa74d9SJeff Roberson panic("Can't find cpu group for %d\n", i); 1259c47f202bSJeff Roberson } 126062fa74d9SJeff Roberson balance_tdq = TDQ_SELF(); 126162fa74d9SJeff Roberson sched_balance(); 1262c47f202bSJeff Roberson } 1263c47f202bSJeff Roberson #endif 1264c47f202bSJeff Roberson 1265ae7a6b38SJeff Roberson /* 1266ae7a6b38SJeff Roberson * Setup the thread queues and initialize the topology based on MD 1267ae7a6b38SJeff Roberson * information. 1268ae7a6b38SJeff Roberson */ 126935e6168fSJeff Roberson static void 127035e6168fSJeff Roberson sched_setup(void *dummy) 127135e6168fSJeff Roberson { 1272ae7a6b38SJeff Roberson struct tdq *tdq; 1273c47f202bSJeff Roberson 1274c47f202bSJeff Roberson tdq = TDQ_SELF(); 12750ec896fdSJeff Roberson #ifdef SMP 1276c47f202bSJeff Roberson sched_setup_smp(); 1277749d01b0SJeff Roberson #else 1278c47f202bSJeff Roberson tdq_setup(tdq); 1279356500a3SJeff Roberson #endif 1280ae7a6b38SJeff Roberson /* 1281ae7a6b38SJeff Roberson * To avoid divide-by-zero, we set realstathz a dummy value 1282ae7a6b38SJeff Roberson * in case which sched_clock() called before sched_initticks(). 1283ae7a6b38SJeff Roberson */ 1284ae7a6b38SJeff Roberson realstathz = hz; 1285ae7a6b38SJeff Roberson sched_slice = (realstathz/10); /* ~100ms */ 1286ae7a6b38SJeff Roberson tickincr = 1 << SCHED_TICK_SHIFT; 1287ae7a6b38SJeff Roberson 1288ae7a6b38SJeff Roberson /* Add thread0's load since it's running. */ 1289ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1290c47f202bSJeff Roberson thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF()); 12919727e637SJeff Roberson tdq_load_add(tdq, &thread0); 129262fa74d9SJeff Roberson tdq->tdq_lowpri = thread0.td_priority; 1293ae7a6b38SJeff Roberson TDQ_UNLOCK(tdq); 129435e6168fSJeff Roberson } 129535e6168fSJeff Roberson 1296ae7a6b38SJeff Roberson /* 1297ae7a6b38SJeff Roberson * This routine determines the tickincr after stathz and hz are setup. 1298ae7a6b38SJeff Roberson */ 1299a1d4fe69SDavid Xu /* ARGSUSED */ 1300a1d4fe69SDavid Xu static void 1301a1d4fe69SDavid Xu sched_initticks(void *dummy) 1302a1d4fe69SDavid Xu { 1303ae7a6b38SJeff Roberson int incr; 1304ae7a6b38SJeff Roberson 1305a1d4fe69SDavid Xu realstathz = stathz ? stathz : hz; 130614618990SJeff Roberson sched_slice = (realstathz/10); /* ~100ms */ 1307a1d4fe69SDavid Xu 1308a1d4fe69SDavid Xu /* 1309e7d50326SJeff Roberson * tickincr is shifted out by 10 to avoid rounding errors due to 13103f872f85SJeff Roberson * hz not being evenly divisible by stathz on all platforms. 1311e7d50326SJeff Roberson */ 1312ae7a6b38SJeff Roberson incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1313e7d50326SJeff Roberson /* 1314e7d50326SJeff Roberson * This does not work for values of stathz that are more than 1315e7d50326SJeff Roberson * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1316a1d4fe69SDavid Xu */ 1317ae7a6b38SJeff Roberson if (incr == 0) 1318ae7a6b38SJeff Roberson incr = 1; 1319ae7a6b38SJeff Roberson tickincr = incr; 13207b8bfa0dSJeff Roberson #ifdef SMP 13219862717aSJeff Roberson /* 13227fcf154aSJeff Roberson * Set the default balance interval now that we know 13237fcf154aSJeff Roberson * what realstathz is. 13247fcf154aSJeff Roberson */ 13257fcf154aSJeff Roberson balance_interval = realstathz; 13267fcf154aSJeff Roberson /* 132753a6c8b3SJeff Roberson * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4. 132853a6c8b3SJeff Roberson * This prevents excess thrashing on large machines and excess idle 132953a6c8b3SJeff Roberson * on smaller machines. 13309862717aSJeff Roberson */ 133153a6c8b3SJeff Roberson steal_thresh = min(fls(mp_ncpus) - 1, 3); 13327b8bfa0dSJeff Roberson affinity = SCHED_AFFINITY_DEFAULT; 13337b8bfa0dSJeff Roberson #endif 1334a1d4fe69SDavid Xu } 1335a1d4fe69SDavid Xu 1336a1d4fe69SDavid Xu 133735e6168fSJeff Roberson /* 1338ae7a6b38SJeff Roberson * This is the core of the interactivity algorithm. Determines a score based 1339ae7a6b38SJeff Roberson * on past behavior. It is the ratio of sleep time to run time scaled to 1340ae7a6b38SJeff Roberson * a [0, 100] integer. This is the voluntary sleep time of a process, which 1341ae7a6b38SJeff Roberson * differs from the cpu usage because it does not account for time spent 1342ae7a6b38SJeff Roberson * waiting on a run-queue. Would be prettier if we had floating point. 1343ae7a6b38SJeff Roberson */ 1344ae7a6b38SJeff Roberson static int 1345ae7a6b38SJeff Roberson sched_interact_score(struct thread *td) 1346ae7a6b38SJeff Roberson { 1347ae7a6b38SJeff Roberson struct td_sched *ts; 1348ae7a6b38SJeff Roberson int div; 1349ae7a6b38SJeff Roberson 1350ae7a6b38SJeff Roberson ts = td->td_sched; 1351ae7a6b38SJeff Roberson /* 1352ae7a6b38SJeff Roberson * The score is only needed if this is likely to be an interactive 1353ae7a6b38SJeff Roberson * task. Don't go through the expense of computing it if there's 1354ae7a6b38SJeff Roberson * no chance. 1355ae7a6b38SJeff Roberson */ 1356ae7a6b38SJeff Roberson if (sched_interact <= SCHED_INTERACT_HALF && 1357ae7a6b38SJeff Roberson ts->ts_runtime >= ts->ts_slptime) 1358ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1359ae7a6b38SJeff Roberson 1360ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1361ae7a6b38SJeff Roberson div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1362ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF + 1363ae7a6b38SJeff Roberson (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1364ae7a6b38SJeff Roberson } 1365ae7a6b38SJeff Roberson if (ts->ts_slptime > ts->ts_runtime) { 1366ae7a6b38SJeff Roberson div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1367ae7a6b38SJeff Roberson return (ts->ts_runtime / div); 1368ae7a6b38SJeff Roberson } 1369ae7a6b38SJeff Roberson /* runtime == slptime */ 1370ae7a6b38SJeff Roberson if (ts->ts_runtime) 1371ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1372ae7a6b38SJeff Roberson 1373ae7a6b38SJeff Roberson /* 1374ae7a6b38SJeff Roberson * This can happen if slptime and runtime are 0. 1375ae7a6b38SJeff Roberson */ 1376ae7a6b38SJeff Roberson return (0); 1377ae7a6b38SJeff Roberson 1378ae7a6b38SJeff Roberson } 1379ae7a6b38SJeff Roberson 1380ae7a6b38SJeff Roberson /* 138135e6168fSJeff Roberson * Scale the scheduling priority according to the "interactivity" of this 138235e6168fSJeff Roberson * process. 138335e6168fSJeff Roberson */ 138415dc847eSJeff Roberson static void 13858460a577SJohn Birrell sched_priority(struct thread *td) 138635e6168fSJeff Roberson { 1387e7d50326SJeff Roberson int score; 138835e6168fSJeff Roberson int pri; 138935e6168fSJeff Roberson 13908460a577SJohn Birrell if (td->td_pri_class != PRI_TIMESHARE) 139115dc847eSJeff Roberson return; 1392e7d50326SJeff Roberson /* 1393e7d50326SJeff Roberson * If the score is interactive we place the thread in the realtime 1394e7d50326SJeff Roberson * queue with a priority that is less than kernel and interrupt 1395e7d50326SJeff Roberson * priorities. These threads are not subject to nice restrictions. 1396e7d50326SJeff Roberson * 1397ae7a6b38SJeff Roberson * Scores greater than this are placed on the normal timeshare queue 1398e7d50326SJeff Roberson * where the priority is partially decided by the most recent cpu 1399e7d50326SJeff Roberson * utilization and the rest is decided by nice value. 1400a5423ea3SJeff Roberson * 1401a5423ea3SJeff Roberson * The nice value of the process has a linear effect on the calculated 1402a5423ea3SJeff Roberson * score. Negative nice values make it easier for a thread to be 1403a5423ea3SJeff Roberson * considered interactive. 1404e7d50326SJeff Roberson */ 1405a0f15352SJohn Baldwin score = imax(0, sched_interact_score(td) + td->td_proc->p_nice); 1406e7d50326SJeff Roberson if (score < sched_interact) { 1407e7d50326SJeff Roberson pri = PRI_MIN_REALTIME; 1408e7d50326SJeff Roberson pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact) 1409e7d50326SJeff Roberson * score; 1410e7d50326SJeff Roberson KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME, 14119a93305aSJeff Roberson ("sched_priority: invalid interactive priority %d score %d", 14129a93305aSJeff Roberson pri, score)); 1413e7d50326SJeff Roberson } else { 1414e7d50326SJeff Roberson pri = SCHED_PRI_MIN; 1415e7d50326SJeff Roberson if (td->td_sched->ts_ticks) 1416e7d50326SJeff Roberson pri += SCHED_PRI_TICKS(td->td_sched); 1417e7d50326SJeff Roberson pri += SCHED_PRI_NICE(td->td_proc->p_nice); 1418ae7a6b38SJeff Roberson KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE, 1419ae7a6b38SJeff Roberson ("sched_priority: invalid priority %d: nice %d, " 1420ae7a6b38SJeff Roberson "ticks %d ftick %d ltick %d tick pri %d", 1421ae7a6b38SJeff Roberson pri, td->td_proc->p_nice, td->td_sched->ts_ticks, 1422ae7a6b38SJeff Roberson td->td_sched->ts_ftick, td->td_sched->ts_ltick, 1423ae7a6b38SJeff Roberson SCHED_PRI_TICKS(td->td_sched))); 1424e7d50326SJeff Roberson } 14258460a577SJohn Birrell sched_user_prio(td, pri); 142635e6168fSJeff Roberson 142715dc847eSJeff Roberson return; 142835e6168fSJeff Roberson } 142935e6168fSJeff Roberson 143035e6168fSJeff Roberson /* 1431d322132cSJeff Roberson * This routine enforces a maximum limit on the amount of scheduling history 1432ae7a6b38SJeff Roberson * kept. It is called after either the slptime or runtime is adjusted. This 1433ae7a6b38SJeff Roberson * function is ugly due to integer math. 1434d322132cSJeff Roberson */ 14354b60e324SJeff Roberson static void 14368460a577SJohn Birrell sched_interact_update(struct thread *td) 14374b60e324SJeff Roberson { 1438155b6ca1SJeff Roberson struct td_sched *ts; 14399a93305aSJeff Roberson u_int sum; 14403f741ca1SJeff Roberson 1441155b6ca1SJeff Roberson ts = td->td_sched; 1442ae7a6b38SJeff Roberson sum = ts->ts_runtime + ts->ts_slptime; 1443d322132cSJeff Roberson if (sum < SCHED_SLP_RUN_MAX) 1444d322132cSJeff Roberson return; 1445d322132cSJeff Roberson /* 1446155b6ca1SJeff Roberson * This only happens from two places: 1447155b6ca1SJeff Roberson * 1) We have added an unusual amount of run time from fork_exit. 1448155b6ca1SJeff Roberson * 2) We have added an unusual amount of sleep time from sched_sleep(). 1449155b6ca1SJeff Roberson */ 1450155b6ca1SJeff Roberson if (sum > SCHED_SLP_RUN_MAX * 2) { 1451ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1452ae7a6b38SJeff Roberson ts->ts_runtime = SCHED_SLP_RUN_MAX; 1453ae7a6b38SJeff Roberson ts->ts_slptime = 1; 1454155b6ca1SJeff Roberson } else { 1455ae7a6b38SJeff Roberson ts->ts_slptime = SCHED_SLP_RUN_MAX; 1456ae7a6b38SJeff Roberson ts->ts_runtime = 1; 1457155b6ca1SJeff Roberson } 1458155b6ca1SJeff Roberson return; 1459155b6ca1SJeff Roberson } 1460155b6ca1SJeff Roberson /* 1461d322132cSJeff Roberson * If we have exceeded by more than 1/5th then the algorithm below 1462d322132cSJeff Roberson * will not bring us back into range. Dividing by two here forces 14632454aaf5SJeff Roberson * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1464d322132cSJeff Roberson */ 146537a35e4aSJeff Roberson if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1466ae7a6b38SJeff Roberson ts->ts_runtime /= 2; 1467ae7a6b38SJeff Roberson ts->ts_slptime /= 2; 1468d322132cSJeff Roberson return; 1469d322132cSJeff Roberson } 1470ae7a6b38SJeff Roberson ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1471ae7a6b38SJeff Roberson ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1472d322132cSJeff Roberson } 1473d322132cSJeff Roberson 1474ae7a6b38SJeff Roberson /* 1475ae7a6b38SJeff Roberson * Scale back the interactivity history when a child thread is created. The 1476ae7a6b38SJeff Roberson * history is inherited from the parent but the thread may behave totally 1477ae7a6b38SJeff Roberson * differently. For example, a shell spawning a compiler process. We want 1478ae7a6b38SJeff Roberson * to learn that the compiler is behaving badly very quickly. 1479ae7a6b38SJeff Roberson */ 1480d322132cSJeff Roberson static void 14818460a577SJohn Birrell sched_interact_fork(struct thread *td) 1482d322132cSJeff Roberson { 1483d322132cSJeff Roberson int ratio; 1484d322132cSJeff Roberson int sum; 1485d322132cSJeff Roberson 1486ae7a6b38SJeff Roberson sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime; 1487d322132cSJeff Roberson if (sum > SCHED_SLP_RUN_FORK) { 1488d322132cSJeff Roberson ratio = sum / SCHED_SLP_RUN_FORK; 1489ae7a6b38SJeff Roberson td->td_sched->ts_runtime /= ratio; 1490ae7a6b38SJeff Roberson td->td_sched->ts_slptime /= ratio; 14914b60e324SJeff Roberson } 14924b60e324SJeff Roberson } 14934b60e324SJeff Roberson 149415dc847eSJeff Roberson /* 1495ae7a6b38SJeff Roberson * Called from proc0_init() to setup the scheduler fields. 1496ed062c8dSJulian Elischer */ 1497ed062c8dSJulian Elischer void 1498ed062c8dSJulian Elischer schedinit(void) 1499ed062c8dSJulian Elischer { 1500e7d50326SJeff Roberson 1501ed062c8dSJulian Elischer /* 1502ed062c8dSJulian Elischer * Set up the scheduler specific parts of proc0. 1503ed062c8dSJulian Elischer */ 1504ed062c8dSJulian Elischer proc0.p_sched = NULL; /* XXX */ 1505ad1e7d28SJulian Elischer thread0.td_sched = &td_sched0; 1506e7d50326SJeff Roberson td_sched0.ts_ltick = ticks; 15078ab80cf0SJeff Roberson td_sched0.ts_ftick = ticks; 150873daf66fSJeff Roberson td_sched0.ts_slice = sched_slice; 1509ed062c8dSJulian Elischer } 1510ed062c8dSJulian Elischer 1511ed062c8dSJulian Elischer /* 151215dc847eSJeff Roberson * This is only somewhat accurate since given many processes of the same 151315dc847eSJeff Roberson * priority they will switch when their slices run out, which will be 1514e7d50326SJeff Roberson * at most sched_slice stathz ticks. 151515dc847eSJeff Roberson */ 151635e6168fSJeff Roberson int 151735e6168fSJeff Roberson sched_rr_interval(void) 151835e6168fSJeff Roberson { 1519e7d50326SJeff Roberson 1520e7d50326SJeff Roberson /* Convert sched_slice to hz */ 1521e7d50326SJeff Roberson return (hz/(realstathz/sched_slice)); 152235e6168fSJeff Roberson } 152335e6168fSJeff Roberson 1524ae7a6b38SJeff Roberson /* 1525ae7a6b38SJeff Roberson * Update the percent cpu tracking information when it is requested or 1526ae7a6b38SJeff Roberson * the total history exceeds the maximum. We keep a sliding history of 1527ae7a6b38SJeff Roberson * tick counts that slowly decays. This is less precise than the 4BSD 1528ae7a6b38SJeff Roberson * mechanism since it happens with less regular and frequent events. 1529ae7a6b38SJeff Roberson */ 153022bf7d9aSJeff Roberson static void 1531ad1e7d28SJulian Elischer sched_pctcpu_update(struct td_sched *ts) 153235e6168fSJeff Roberson { 1533e7d50326SJeff Roberson 1534e7d50326SJeff Roberson if (ts->ts_ticks == 0) 1535e7d50326SJeff Roberson return; 15368ab80cf0SJeff Roberson if (ticks - (hz / 10) < ts->ts_ltick && 15378ab80cf0SJeff Roberson SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX) 15388ab80cf0SJeff Roberson return; 153935e6168fSJeff Roberson /* 154035e6168fSJeff Roberson * Adjust counters and watermark for pctcpu calc. 1541210491d3SJeff Roberson */ 1542e7d50326SJeff Roberson if (ts->ts_ltick > ticks - SCHED_TICK_TARG) 1543ad1e7d28SJulian Elischer ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) * 1544e7d50326SJeff Roberson SCHED_TICK_TARG; 1545e7d50326SJeff Roberson else 1546ad1e7d28SJulian Elischer ts->ts_ticks = 0; 1547ad1e7d28SJulian Elischer ts->ts_ltick = ticks; 1548e7d50326SJeff Roberson ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG; 154935e6168fSJeff Roberson } 155035e6168fSJeff Roberson 1551ae7a6b38SJeff Roberson /* 1552ae7a6b38SJeff Roberson * Adjust the priority of a thread. Move it to the appropriate run-queue 1553ae7a6b38SJeff Roberson * if necessary. This is the back-end for several priority related 1554ae7a6b38SJeff Roberson * functions. 1555ae7a6b38SJeff Roberson */ 1556e7d50326SJeff Roberson static void 1557f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio) 155835e6168fSJeff Roberson { 1559ad1e7d28SJulian Elischer struct td_sched *ts; 156073daf66fSJeff Roberson struct tdq *tdq; 156173daf66fSJeff Roberson int oldpri; 156235e6168fSJeff Roberson 15638f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 15648f51ad55SJeff Roberson "prio:%d", td->td_priority, "new prio:%d", prio, 15658f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(curthread)); 15668f51ad55SJeff Roberson if (td != curthread && prio > td->td_priority) { 15678f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 15688f51ad55SJeff Roberson "lend prio", "prio:%d", td->td_priority, "new prio:%d", 15698f51ad55SJeff Roberson prio, KTR_ATTR_LINKED, sched_tdname(td)); 15708f51ad55SJeff Roberson } 1571ad1e7d28SJulian Elischer ts = td->td_sched; 15727b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1573f5c157d9SJohn Baldwin if (td->td_priority == prio) 1574f5c157d9SJohn Baldwin return; 15753f741ca1SJeff Roberson /* 15763f741ca1SJeff Roberson * If the priority has been elevated due to priority 15773f741ca1SJeff Roberson * propagation, we may have to move ourselves to a new 1578e7d50326SJeff Roberson * queue. This could be optimized to not re-add in some 1579e7d50326SJeff Roberson * cases. 1580f2b74cbfSJeff Roberson */ 15816d55b3ecSJeff Roberson if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1582e7d50326SJeff Roberson sched_rem(td); 1583e7d50326SJeff Roberson td->td_priority = prio; 1584ae7a6b38SJeff Roberson sched_add(td, SRQ_BORROWING); 158573daf66fSJeff Roberson return; 158673daf66fSJeff Roberson } 15876d55b3ecSJeff Roberson /* 15886d55b3ecSJeff Roberson * If the thread is currently running we may have to adjust the lowpri 15896d55b3ecSJeff Roberson * information so other cpus are aware of our current priority. 15906d55b3ecSJeff Roberson */ 15916d55b3ecSJeff Roberson if (TD_IS_RUNNING(td)) { 1592ae7a6b38SJeff Roberson tdq = TDQ_CPU(ts->ts_cpu); 159362fa74d9SJeff Roberson oldpri = td->td_priority; 15943f741ca1SJeff Roberson td->td_priority = prio; 159562fa74d9SJeff Roberson if (prio < tdq->tdq_lowpri) 159662fa74d9SJeff Roberson tdq->tdq_lowpri = prio; 159762fa74d9SJeff Roberson else if (tdq->tdq_lowpri == oldpri) 159862fa74d9SJeff Roberson tdq_setlowpri(tdq, td); 15996d55b3ecSJeff Roberson return; 160073daf66fSJeff Roberson } 16016d55b3ecSJeff Roberson td->td_priority = prio; 1602ae7a6b38SJeff Roberson } 160335e6168fSJeff Roberson 1604f5c157d9SJohn Baldwin /* 1605f5c157d9SJohn Baldwin * Update a thread's priority when it is lent another thread's 1606f5c157d9SJohn Baldwin * priority. 1607f5c157d9SJohn Baldwin */ 1608f5c157d9SJohn Baldwin void 1609f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio) 1610f5c157d9SJohn Baldwin { 1611f5c157d9SJohn Baldwin 1612f5c157d9SJohn Baldwin td->td_flags |= TDF_BORROWING; 1613f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1614f5c157d9SJohn Baldwin } 1615f5c157d9SJohn Baldwin 1616f5c157d9SJohn Baldwin /* 1617f5c157d9SJohn Baldwin * Restore a thread's priority when priority propagation is 1618f5c157d9SJohn Baldwin * over. The prio argument is the minimum priority the thread 1619f5c157d9SJohn Baldwin * needs to have to satisfy other possible priority lending 1620f5c157d9SJohn Baldwin * requests. If the thread's regular priority is less 1621f5c157d9SJohn Baldwin * important than prio, the thread will keep a priority boost 1622f5c157d9SJohn Baldwin * of prio. 1623f5c157d9SJohn Baldwin */ 1624f5c157d9SJohn Baldwin void 1625f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio) 1626f5c157d9SJohn Baldwin { 1627f5c157d9SJohn Baldwin u_char base_pri; 1628f5c157d9SJohn Baldwin 1629f5c157d9SJohn Baldwin if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1630f5c157d9SJohn Baldwin td->td_base_pri <= PRI_MAX_TIMESHARE) 16318460a577SJohn Birrell base_pri = td->td_user_pri; 1632f5c157d9SJohn Baldwin else 1633f5c157d9SJohn Baldwin base_pri = td->td_base_pri; 1634f5c157d9SJohn Baldwin if (prio >= base_pri) { 1635f5c157d9SJohn Baldwin td->td_flags &= ~TDF_BORROWING; 1636f5c157d9SJohn Baldwin sched_thread_priority(td, base_pri); 1637f5c157d9SJohn Baldwin } else 1638f5c157d9SJohn Baldwin sched_lend_prio(td, prio); 1639f5c157d9SJohn Baldwin } 1640f5c157d9SJohn Baldwin 1641ae7a6b38SJeff Roberson /* 1642ae7a6b38SJeff Roberson * Standard entry for setting the priority to an absolute value. 1643ae7a6b38SJeff Roberson */ 1644f5c157d9SJohn Baldwin void 1645f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio) 1646f5c157d9SJohn Baldwin { 1647f5c157d9SJohn Baldwin u_char oldprio; 1648f5c157d9SJohn Baldwin 1649f5c157d9SJohn Baldwin /* First, update the base priority. */ 1650f5c157d9SJohn Baldwin td->td_base_pri = prio; 1651f5c157d9SJohn Baldwin 1652f5c157d9SJohn Baldwin /* 165350aaa791SJohn Baldwin * If the thread is borrowing another thread's priority, don't 1654f5c157d9SJohn Baldwin * ever lower the priority. 1655f5c157d9SJohn Baldwin */ 1656f5c157d9SJohn Baldwin if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1657f5c157d9SJohn Baldwin return; 1658f5c157d9SJohn Baldwin 1659f5c157d9SJohn Baldwin /* Change the real priority. */ 1660f5c157d9SJohn Baldwin oldprio = td->td_priority; 1661f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1662f5c157d9SJohn Baldwin 1663f5c157d9SJohn Baldwin /* 1664f5c157d9SJohn Baldwin * If the thread is on a turnstile, then let the turnstile update 1665f5c157d9SJohn Baldwin * its state. 1666f5c157d9SJohn Baldwin */ 1667f5c157d9SJohn Baldwin if (TD_ON_LOCK(td) && oldprio != prio) 1668f5c157d9SJohn Baldwin turnstile_adjust(td, oldprio); 1669f5c157d9SJohn Baldwin } 1670f5c157d9SJohn Baldwin 1671ae7a6b38SJeff Roberson /* 1672ae7a6b38SJeff Roberson * Set the base user priority, does not effect current running priority. 1673ae7a6b38SJeff Roberson */ 167435e6168fSJeff Roberson void 16758460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio) 16763db720fdSDavid Xu { 16773db720fdSDavid Xu u_char oldprio; 16783db720fdSDavid Xu 16798460a577SJohn Birrell td->td_base_user_pri = prio; 1680fc6c30f6SJulian Elischer if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio) 1681fc6c30f6SJulian Elischer return; 16828460a577SJohn Birrell oldprio = td->td_user_pri; 16838460a577SJohn Birrell td->td_user_pri = prio; 16843db720fdSDavid Xu } 16853db720fdSDavid Xu 16863db720fdSDavid Xu void 16873db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio) 16883db720fdSDavid Xu { 16893db720fdSDavid Xu u_char oldprio; 16903db720fdSDavid Xu 1691435806d3SDavid Xu THREAD_LOCK_ASSERT(td, MA_OWNED); 16923db720fdSDavid Xu td->td_flags |= TDF_UBORROWING; 1693f645b5daSMaxim Konovalov oldprio = td->td_user_pri; 16948460a577SJohn Birrell td->td_user_pri = prio; 16953db720fdSDavid Xu } 16963db720fdSDavid Xu 16973db720fdSDavid Xu void 16983db720fdSDavid Xu sched_unlend_user_prio(struct thread *td, u_char prio) 16993db720fdSDavid Xu { 17003db720fdSDavid Xu u_char base_pri; 17013db720fdSDavid Xu 1702435806d3SDavid Xu THREAD_LOCK_ASSERT(td, MA_OWNED); 17038460a577SJohn Birrell base_pri = td->td_base_user_pri; 17043db720fdSDavid Xu if (prio >= base_pri) { 17053db720fdSDavid Xu td->td_flags &= ~TDF_UBORROWING; 17068460a577SJohn Birrell sched_user_prio(td, base_pri); 1707435806d3SDavid Xu } else { 17083db720fdSDavid Xu sched_lend_user_prio(td, prio); 17093db720fdSDavid Xu } 1710435806d3SDavid Xu } 17113db720fdSDavid Xu 1712ae7a6b38SJeff Roberson /* 1713c47f202bSJeff Roberson * Handle migration from sched_switch(). This happens only for 1714c47f202bSJeff Roberson * cpu binding. 1715c47f202bSJeff Roberson */ 1716c47f202bSJeff Roberson static struct mtx * 1717c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1718c47f202bSJeff Roberson { 1719c47f202bSJeff Roberson struct tdq *tdn; 1720c47f202bSJeff Roberson 1721c47f202bSJeff Roberson tdn = TDQ_CPU(td->td_sched->ts_cpu); 1722c47f202bSJeff Roberson #ifdef SMP 17239727e637SJeff Roberson tdq_load_rem(tdq, td); 1724c47f202bSJeff Roberson /* 1725c47f202bSJeff Roberson * Do the lock dance required to avoid LOR. We grab an extra 1726c47f202bSJeff Roberson * spinlock nesting to prevent preemption while we're 1727c47f202bSJeff Roberson * not holding either run-queue lock. 1728c47f202bSJeff Roberson */ 1729c47f202bSJeff Roberson spinlock_enter(); 1730b0b9dee5SAttilio Rao thread_lock_block(td); /* This releases the lock on tdq. */ 1731435068aaSAttilio Rao 1732435068aaSAttilio Rao /* 1733435068aaSAttilio Rao * Acquire both run-queue locks before placing the thread on the new 1734435068aaSAttilio Rao * run-queue to avoid deadlocks created by placing a thread with a 1735435068aaSAttilio Rao * blocked lock on the run-queue of a remote processor. The deadlock 1736435068aaSAttilio Rao * occurs when a third processor attempts to lock the two queues in 1737435068aaSAttilio Rao * question while the target processor is spinning with its own 1738435068aaSAttilio Rao * run-queue lock held while waiting for the blocked lock to clear. 1739435068aaSAttilio Rao */ 1740435068aaSAttilio Rao tdq_lock_pair(tdn, tdq); 1741c47f202bSJeff Roberson tdq_add(tdn, td, flags); 17429727e637SJeff Roberson tdq_notify(tdn, td); 1743c47f202bSJeff Roberson TDQ_UNLOCK(tdn); 1744c47f202bSJeff Roberson spinlock_exit(); 1745c47f202bSJeff Roberson #endif 1746c47f202bSJeff Roberson return (TDQ_LOCKPTR(tdn)); 1747c47f202bSJeff Roberson } 1748c47f202bSJeff Roberson 1749c47f202bSJeff Roberson /* 1750b0b9dee5SAttilio Rao * Variadic version of thread_lock_unblock() that does not assume td_lock 1751b0b9dee5SAttilio Rao * is blocked. 1752ae7a6b38SJeff Roberson */ 1753ae7a6b38SJeff Roberson static inline void 1754ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx) 1755ae7a6b38SJeff Roberson { 1756ae7a6b38SJeff Roberson atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1757ae7a6b38SJeff Roberson (uintptr_t)mtx); 1758ae7a6b38SJeff Roberson } 1759ae7a6b38SJeff Roberson 1760ae7a6b38SJeff Roberson /* 1761ae7a6b38SJeff Roberson * Switch threads. This function has to handle threads coming in while 1762ae7a6b38SJeff Roberson * blocked for some reason, running, or idle. It also must deal with 1763ae7a6b38SJeff Roberson * migrating a thread from one queue to another as running threads may 1764ae7a6b38SJeff Roberson * be assigned elsewhere via binding. 1765ae7a6b38SJeff Roberson */ 17663db720fdSDavid Xu void 17673389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags) 176835e6168fSJeff Roberson { 1769c02bbb43SJeff Roberson struct tdq *tdq; 1770ad1e7d28SJulian Elischer struct td_sched *ts; 1771ae7a6b38SJeff Roberson struct mtx *mtx; 1772c47f202bSJeff Roberson int srqflag; 1773ae7a6b38SJeff Roberson int cpuid; 177435e6168fSJeff Roberson 17757b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 17766d55b3ecSJeff Roberson KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument")); 177735e6168fSJeff Roberson 1778ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 1779ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpuid); 1780e7d50326SJeff Roberson ts = td->td_sched; 1781c47f202bSJeff Roberson mtx = td->td_lock; 1782ae7a6b38SJeff Roberson ts->ts_rltick = ticks; 1783060563ecSJulian Elischer td->td_lastcpu = td->td_oncpu; 1784060563ecSJulian Elischer td->td_oncpu = NOCPU; 178552eb8464SJohn Baldwin td->td_flags &= ~TDF_NEEDRESCHED; 178677918643SStephan Uphoff td->td_owepreempt = 0; 17871690c6c1SJeff Roberson tdq->tdq_switchcnt++; 1788b11fdad0SJeff Roberson /* 1789ae7a6b38SJeff Roberson * The lock pointer in an idle thread should never change. Reset it 1790ae7a6b38SJeff Roberson * to CAN_RUN as well. 1791b11fdad0SJeff Roberson */ 1792486a9414SJulian Elischer if (TD_IS_IDLETHREAD(td)) { 1793ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1794bf0acc27SJohn Baldwin TD_SET_CAN_RUN(td); 17957b20fb19SJeff Roberson } else if (TD_IS_RUNNING(td)) { 1796ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1797c47f202bSJeff Roberson srqflag = (flags & SW_PREEMPT) ? 1798598b368dSJeff Roberson SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1799c47f202bSJeff Roberson SRQ_OURSELF|SRQ_YIELDING; 1800*ba4932b5SMatthew D Fleming #ifdef SMP 18010f7a0ebdSMatthew D Fleming if (THREAD_CAN_MIGRATE(td) && !THREAD_CAN_SCHED(td, ts->ts_cpu)) 18020f7a0ebdSMatthew D Fleming ts->ts_cpu = sched_pickcpu(td, 0); 1803*ba4932b5SMatthew D Fleming #endif 1804c47f202bSJeff Roberson if (ts->ts_cpu == cpuid) 18059727e637SJeff Roberson tdq_runq_add(tdq, td, srqflag); 18060f7a0ebdSMatthew D Fleming else { 18070f7a0ebdSMatthew D Fleming KASSERT(THREAD_CAN_MIGRATE(td) || 18080f7a0ebdSMatthew D Fleming (ts->ts_flags & TSF_BOUND) != 0, 18090f7a0ebdSMatthew D Fleming ("Thread %p shouldn't migrate", td)); 1810c47f202bSJeff Roberson mtx = sched_switch_migrate(tdq, td, srqflag); 18110f7a0ebdSMatthew D Fleming } 1812ae7a6b38SJeff Roberson } else { 1813ae7a6b38SJeff Roberson /* This thread must be going to sleep. */ 1814ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1815b0b9dee5SAttilio Rao mtx = thread_lock_block(td); 18169727e637SJeff Roberson tdq_load_rem(tdq, td); 1817ae7a6b38SJeff Roberson } 1818ae7a6b38SJeff Roberson /* 1819ae7a6b38SJeff Roberson * We enter here with the thread blocked and assigned to the 1820ae7a6b38SJeff Roberson * appropriate cpu run-queue or sleep-queue and with the current 1821ae7a6b38SJeff Roberson * thread-queue locked. 1822ae7a6b38SJeff Roberson */ 1823ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 18242454aaf5SJeff Roberson newtd = choosethread(); 1825ae7a6b38SJeff Roberson /* 1826ae7a6b38SJeff Roberson * Call the MD code to switch contexts if necessary. 1827ae7a6b38SJeff Roberson */ 1828ebccf1e3SJoseph Koshy if (td != newtd) { 1829ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 1830ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1831ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1832ebccf1e3SJoseph Koshy #endif 1833eea4f254SJeff Roberson lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 183459c68134SJeff Roberson TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 18356f5f25e5SJohn Birrell 18366f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS 18376f5f25e5SJohn Birrell /* 18386f5f25e5SJohn Birrell * If DTrace has set the active vtime enum to anything 18396f5f25e5SJohn Birrell * other than INACTIVE (0), then it should have set the 18406f5f25e5SJohn Birrell * function to call. 18416f5f25e5SJohn Birrell */ 18426f5f25e5SJohn Birrell if (dtrace_vtime_active) 18436f5f25e5SJohn Birrell (*dtrace_vtime_switch_func)(newtd); 18446f5f25e5SJohn Birrell #endif 18456f5f25e5SJohn Birrell 1846ae7a6b38SJeff Roberson cpu_switch(td, newtd, mtx); 1847ae7a6b38SJeff Roberson /* 1848ae7a6b38SJeff Roberson * We may return from cpu_switch on a different cpu. However, 1849ae7a6b38SJeff Roberson * we always return with td_lock pointing to the current cpu's 1850ae7a6b38SJeff Roberson * run queue lock. 1851ae7a6b38SJeff Roberson */ 1852ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 1853ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpuid); 1854eea4f254SJeff Roberson lock_profile_obtain_lock_success( 1855eea4f254SJeff Roberson &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 1856ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 1857ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1858ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1859ebccf1e3SJoseph Koshy #endif 1860ae7a6b38SJeff Roberson } else 1861ae7a6b38SJeff Roberson thread_unblock_switch(td, mtx); 1862ae7a6b38SJeff Roberson /* 1863ae7a6b38SJeff Roberson * Assert that all went well and return. 1864ae7a6b38SJeff Roberson */ 1865ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED); 1866ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1867ae7a6b38SJeff Roberson td->td_oncpu = cpuid; 186835e6168fSJeff Roberson } 186935e6168fSJeff Roberson 1870ae7a6b38SJeff Roberson /* 1871ae7a6b38SJeff Roberson * Adjust thread priorities as a result of a nice request. 1872ae7a6b38SJeff Roberson */ 187335e6168fSJeff Roberson void 1874fa885116SJulian Elischer sched_nice(struct proc *p, int nice) 187535e6168fSJeff Roberson { 187635e6168fSJeff Roberson struct thread *td; 187735e6168fSJeff Roberson 1878fa885116SJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 1879e7d50326SJeff Roberson 1880fa885116SJulian Elischer p->p_nice = nice; 18818460a577SJohn Birrell FOREACH_THREAD_IN_PROC(p, td) { 18827b20fb19SJeff Roberson thread_lock(td); 18838460a577SJohn Birrell sched_priority(td); 1884e7d50326SJeff Roberson sched_prio(td, td->td_base_user_pri); 18857b20fb19SJeff Roberson thread_unlock(td); 188635e6168fSJeff Roberson } 1887fa885116SJulian Elischer } 188835e6168fSJeff Roberson 1889ae7a6b38SJeff Roberson /* 1890ae7a6b38SJeff Roberson * Record the sleep time for the interactivity scorer. 1891ae7a6b38SJeff Roberson */ 189235e6168fSJeff Roberson void 1893c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio) 189435e6168fSJeff Roberson { 1895e7d50326SJeff Roberson 18967b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 189735e6168fSJeff Roberson 189854b0e65fSJeff Roberson td->td_slptick = ticks; 189917c4c356SKonstantin Belousov if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 1900c5aa6b58SJeff Roberson td->td_flags |= TDF_CANSWAP; 19010502fe2eSJeff Roberson if (static_boost == 1 && prio) 1902c5aa6b58SJeff Roberson sched_prio(td, prio); 19030502fe2eSJeff Roberson else if (static_boost && td->td_priority > static_boost) 19040502fe2eSJeff Roberson sched_prio(td, static_boost); 190535e6168fSJeff Roberson } 190635e6168fSJeff Roberson 1907ae7a6b38SJeff Roberson /* 1908ae7a6b38SJeff Roberson * Schedule a thread to resume execution and record how long it voluntarily 1909ae7a6b38SJeff Roberson * slept. We also update the pctcpu, interactivity, and priority. 1910ae7a6b38SJeff Roberson */ 191135e6168fSJeff Roberson void 191235e6168fSJeff Roberson sched_wakeup(struct thread *td) 191335e6168fSJeff Roberson { 191414618990SJeff Roberson struct td_sched *ts; 1915ae7a6b38SJeff Roberson int slptick; 1916e7d50326SJeff Roberson 19177b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 191814618990SJeff Roberson ts = td->td_sched; 1919c5aa6b58SJeff Roberson td->td_flags &= ~TDF_CANSWAP; 192035e6168fSJeff Roberson /* 1921e7d50326SJeff Roberson * If we slept for more than a tick update our interactivity and 1922e7d50326SJeff Roberson * priority. 192335e6168fSJeff Roberson */ 192454b0e65fSJeff Roberson slptick = td->td_slptick; 192554b0e65fSJeff Roberson td->td_slptick = 0; 1926ae7a6b38SJeff Roberson if (slptick && slptick != ticks) { 19279a93305aSJeff Roberson u_int hzticks; 1928f1e8dc4aSJeff Roberson 1929ae7a6b38SJeff Roberson hzticks = (ticks - slptick) << SCHED_TICK_SHIFT; 1930ae7a6b38SJeff Roberson ts->ts_slptime += hzticks; 19318460a577SJohn Birrell sched_interact_update(td); 193214618990SJeff Roberson sched_pctcpu_update(ts); 1933f1e8dc4aSJeff Roberson } 193414618990SJeff Roberson /* Reset the slice value after we sleep. */ 193514618990SJeff Roberson ts->ts_slice = sched_slice; 19367a5e5e2aSJeff Roberson sched_add(td, SRQ_BORING); 193735e6168fSJeff Roberson } 193835e6168fSJeff Roberson 193935e6168fSJeff Roberson /* 194035e6168fSJeff Roberson * Penalize the parent for creating a new child and initialize the child's 194135e6168fSJeff Roberson * priority. 194235e6168fSJeff Roberson */ 194335e6168fSJeff Roberson void 19448460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child) 194515dc847eSJeff Roberson { 19467b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1947ad1e7d28SJulian Elischer sched_fork_thread(td, child); 1948e7d50326SJeff Roberson /* 1949e7d50326SJeff Roberson * Penalize the parent and child for forking. 1950e7d50326SJeff Roberson */ 1951e7d50326SJeff Roberson sched_interact_fork(child); 1952e7d50326SJeff Roberson sched_priority(child); 1953ae7a6b38SJeff Roberson td->td_sched->ts_runtime += tickincr; 1954e7d50326SJeff Roberson sched_interact_update(td); 1955e7d50326SJeff Roberson sched_priority(td); 1956ad1e7d28SJulian Elischer } 1957ad1e7d28SJulian Elischer 1958ae7a6b38SJeff Roberson /* 1959ae7a6b38SJeff Roberson * Fork a new thread, may be within the same process. 1960ae7a6b38SJeff Roberson */ 1961ad1e7d28SJulian Elischer void 1962ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child) 1963ad1e7d28SJulian Elischer { 1964ad1e7d28SJulian Elischer struct td_sched *ts; 1965ad1e7d28SJulian Elischer struct td_sched *ts2; 19668460a577SJohn Birrell 19678b16c208SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1968e7d50326SJeff Roberson /* 1969e7d50326SJeff Roberson * Initialize child. 1970e7d50326SJeff Roberson */ 1971ad1e7d28SJulian Elischer ts = td->td_sched; 1972ad1e7d28SJulian Elischer ts2 = child->td_sched; 19738b16c208SJeff Roberson child->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 19748b16c208SJeff Roberson child->td_cpuset = cpuset_ref(td->td_cpuset); 1975ad1e7d28SJulian Elischer ts2->ts_cpu = ts->ts_cpu; 19768b16c208SJeff Roberson ts2->ts_flags = 0; 1977e7d50326SJeff Roberson /* 1978e7d50326SJeff Roberson * Grab our parents cpu estimation information and priority. 1979e7d50326SJeff Roberson */ 1980ad1e7d28SJulian Elischer ts2->ts_ticks = ts->ts_ticks; 1981ad1e7d28SJulian Elischer ts2->ts_ltick = ts->ts_ltick; 1982cbc4ea28SIvan Voras ts2->ts_incrtick = ts->ts_incrtick; 1983ad1e7d28SJulian Elischer ts2->ts_ftick = ts->ts_ftick; 1984e7d50326SJeff Roberson child->td_user_pri = td->td_user_pri; 1985e7d50326SJeff Roberson child->td_base_user_pri = td->td_base_user_pri; 1986e7d50326SJeff Roberson /* 1987e7d50326SJeff Roberson * And update interactivity score. 1988e7d50326SJeff Roberson */ 1989ae7a6b38SJeff Roberson ts2->ts_slptime = ts->ts_slptime; 1990ae7a6b38SJeff Roberson ts2->ts_runtime = ts->ts_runtime; 1991e7d50326SJeff Roberson ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 19928f51ad55SJeff Roberson #ifdef KTR 19938f51ad55SJeff Roberson bzero(ts2->ts_name, sizeof(ts2->ts_name)); 19948f51ad55SJeff Roberson #endif 199515dc847eSJeff Roberson } 199615dc847eSJeff Roberson 1997ae7a6b38SJeff Roberson /* 1998ae7a6b38SJeff Roberson * Adjust the priority class of a thread. 1999ae7a6b38SJeff Roberson */ 200015dc847eSJeff Roberson void 20018460a577SJohn Birrell sched_class(struct thread *td, int class) 200215dc847eSJeff Roberson { 200315dc847eSJeff Roberson 20047b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 20058460a577SJohn Birrell if (td->td_pri_class == class) 200615dc847eSJeff Roberson return; 20078460a577SJohn Birrell td->td_pri_class = class; 200835e6168fSJeff Roberson } 200935e6168fSJeff Roberson 201035e6168fSJeff Roberson /* 201135e6168fSJeff Roberson * Return some of the child's priority and interactivity to the parent. 201235e6168fSJeff Roberson */ 201335e6168fSJeff Roberson void 2014fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child) 201535e6168fSJeff Roberson { 2016e7d50326SJeff Roberson struct thread *td; 2017141ad61cSJeff Roberson 20188f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 20198f51ad55SJeff Roberson "prio:td", child->td_priority); 2020374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2021e7d50326SJeff Roberson td = FIRST_THREAD_IN_PROC(p); 2022e7d50326SJeff Roberson sched_exit_thread(td, child); 2023ad1e7d28SJulian Elischer } 2024ad1e7d28SJulian Elischer 2025ae7a6b38SJeff Roberson /* 2026ae7a6b38SJeff Roberson * Penalize another thread for the time spent on this one. This helps to 2027ae7a6b38SJeff Roberson * worsen the priority and interactivity of processes which schedule batch 2028ae7a6b38SJeff Roberson * jobs such as make. This has little effect on the make process itself but 2029ae7a6b38SJeff Roberson * causes new processes spawned by it to receive worse scores immediately. 2030ae7a6b38SJeff Roberson */ 2031ad1e7d28SJulian Elischer void 2032fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child) 2033ad1e7d28SJulian Elischer { 2034fc6c30f6SJulian Elischer 20358f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 20368f51ad55SJeff Roberson "prio:td", child->td_priority); 2037e7d50326SJeff Roberson /* 2038e7d50326SJeff Roberson * Give the child's runtime to the parent without returning the 2039e7d50326SJeff Roberson * sleep time as a penalty to the parent. This causes shells that 2040e7d50326SJeff Roberson * launch expensive things to mark their children as expensive. 2041e7d50326SJeff Roberson */ 20427b20fb19SJeff Roberson thread_lock(td); 2043ae7a6b38SJeff Roberson td->td_sched->ts_runtime += child->td_sched->ts_runtime; 2044fc6c30f6SJulian Elischer sched_interact_update(td); 2045e7d50326SJeff Roberson sched_priority(td); 20467b20fb19SJeff Roberson thread_unlock(td); 2047ad1e7d28SJulian Elischer } 2048ad1e7d28SJulian Elischer 2049ff256d9cSJeff Roberson void 2050ff256d9cSJeff Roberson sched_preempt(struct thread *td) 2051ff256d9cSJeff Roberson { 2052ff256d9cSJeff Roberson struct tdq *tdq; 2053ff256d9cSJeff Roberson 2054ff256d9cSJeff Roberson thread_lock(td); 2055ff256d9cSJeff Roberson tdq = TDQ_SELF(); 2056ff256d9cSJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2057ff256d9cSJeff Roberson tdq->tdq_ipipending = 0; 2058ff256d9cSJeff Roberson if (td->td_priority > tdq->tdq_lowpri) { 20598df78c41SJeff Roberson int flags; 20608df78c41SJeff Roberson 20618df78c41SJeff Roberson flags = SW_INVOL | SW_PREEMPT; 2062ff256d9cSJeff Roberson if (td->td_critnest > 1) 2063ff256d9cSJeff Roberson td->td_owepreempt = 1; 20648df78c41SJeff Roberson else if (TD_IS_IDLETHREAD(td)) 20658df78c41SJeff Roberson mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL); 2066ff256d9cSJeff Roberson else 20678df78c41SJeff Roberson mi_switch(flags | SWT_REMOTEPREEMPT, NULL); 2068ff256d9cSJeff Roberson } 2069ff256d9cSJeff Roberson thread_unlock(td); 2070ff256d9cSJeff Roberson } 2071ff256d9cSJeff Roberson 2072ae7a6b38SJeff Roberson /* 2073ae7a6b38SJeff Roberson * Fix priorities on return to user-space. Priorities may be elevated due 2074ae7a6b38SJeff Roberson * to static priorities in msleep() or similar. 2075ae7a6b38SJeff Roberson */ 2076ad1e7d28SJulian Elischer void 2077ad1e7d28SJulian Elischer sched_userret(struct thread *td) 2078ad1e7d28SJulian Elischer { 2079ad1e7d28SJulian Elischer /* 2080ad1e7d28SJulian Elischer * XXX we cheat slightly on the locking here to avoid locking in 2081ad1e7d28SJulian Elischer * the usual case. Setting td_priority here is essentially an 2082ad1e7d28SJulian Elischer * incomplete workaround for not setting it properly elsewhere. 2083ad1e7d28SJulian Elischer * Now that some interrupt handlers are threads, not setting it 2084ad1e7d28SJulian Elischer * properly elsewhere can clobber it in the window between setting 2085ad1e7d28SJulian Elischer * it here and returning to user mode, so don't waste time setting 2086ad1e7d28SJulian Elischer * it perfectly here. 2087ad1e7d28SJulian Elischer */ 2088ad1e7d28SJulian Elischer KASSERT((td->td_flags & TDF_BORROWING) == 0, 2089ad1e7d28SJulian Elischer ("thread with borrowed priority returning to userland")); 2090ad1e7d28SJulian Elischer if (td->td_priority != td->td_user_pri) { 20917b20fb19SJeff Roberson thread_lock(td); 2092ad1e7d28SJulian Elischer td->td_priority = td->td_user_pri; 2093ad1e7d28SJulian Elischer td->td_base_pri = td->td_user_pri; 209462fa74d9SJeff Roberson tdq_setlowpri(TDQ_SELF(), td); 20957b20fb19SJeff Roberson thread_unlock(td); 2096ad1e7d28SJulian Elischer } 209735e6168fSJeff Roberson } 209835e6168fSJeff Roberson 2099ae7a6b38SJeff Roberson /* 2100ae7a6b38SJeff Roberson * Handle a stathz tick. This is really only relevant for timeshare 2101ae7a6b38SJeff Roberson * threads. 2102ae7a6b38SJeff Roberson */ 210335e6168fSJeff Roberson void 21047cf90fb3SJeff Roberson sched_clock(struct thread *td) 210535e6168fSJeff Roberson { 2106ad1e7d28SJulian Elischer struct tdq *tdq; 2107ad1e7d28SJulian Elischer struct td_sched *ts; 210835e6168fSJeff Roberson 2109ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 21103f872f85SJeff Roberson tdq = TDQ_SELF(); 21117fcf154aSJeff Roberson #ifdef SMP 21127fcf154aSJeff Roberson /* 21137fcf154aSJeff Roberson * We run the long term load balancer infrequently on the first cpu. 21147fcf154aSJeff Roberson */ 21157fcf154aSJeff Roberson if (balance_tdq == tdq) { 21167fcf154aSJeff Roberson if (balance_ticks && --balance_ticks == 0) 21177fcf154aSJeff Roberson sched_balance(); 21187fcf154aSJeff Roberson } 21197fcf154aSJeff Roberson #endif 21203f872f85SJeff Roberson /* 21211690c6c1SJeff Roberson * Save the old switch count so we have a record of the last ticks 21221690c6c1SJeff Roberson * activity. Initialize the new switch count based on our load. 21231690c6c1SJeff Roberson * If there is some activity seed it to reflect that. 21241690c6c1SJeff Roberson */ 21251690c6c1SJeff Roberson tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 21266c47aaaeSJeff Roberson tdq->tdq_switchcnt = tdq->tdq_load; 21271690c6c1SJeff Roberson /* 21283f872f85SJeff Roberson * Advance the insert index once for each tick to ensure that all 21293f872f85SJeff Roberson * threads get a chance to run. 21303f872f85SJeff Roberson */ 21313f872f85SJeff Roberson if (tdq->tdq_idx == tdq->tdq_ridx) { 21323f872f85SJeff Roberson tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 21333f872f85SJeff Roberson if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 21343f872f85SJeff Roberson tdq->tdq_ridx = tdq->tdq_idx; 21353f872f85SJeff Roberson } 21363f872f85SJeff Roberson ts = td->td_sched; 2137fd0b8c78SJeff Roberson if (td->td_pri_class & PRI_FIFO_BIT) 2138a8949de2SJeff Roberson return; 2139fd0b8c78SJeff Roberson if (td->td_pri_class == PRI_TIMESHARE) { 2140a8949de2SJeff Roberson /* 2141fd0b8c78SJeff Roberson * We used a tick; charge it to the thread so 2142fd0b8c78SJeff Roberson * that we can compute our interactivity. 214315dc847eSJeff Roberson */ 2144ae7a6b38SJeff Roberson td->td_sched->ts_runtime += tickincr; 21458460a577SJohn Birrell sched_interact_update(td); 214673daf66fSJeff Roberson sched_priority(td); 2147fd0b8c78SJeff Roberson } 214835e6168fSJeff Roberson /* 214935e6168fSJeff Roberson * We used up one time slice. 215035e6168fSJeff Roberson */ 2151ad1e7d28SJulian Elischer if (--ts->ts_slice > 0) 215215dc847eSJeff Roberson return; 215335e6168fSJeff Roberson /* 215473daf66fSJeff Roberson * We're out of time, force a requeue at userret(). 215535e6168fSJeff Roberson */ 215673daf66fSJeff Roberson ts->ts_slice = sched_slice; 21574a338afdSJulian Elischer td->td_flags |= TDF_NEEDRESCHED; 215835e6168fSJeff Roberson } 215935e6168fSJeff Roberson 2160ae7a6b38SJeff Roberson /* 2161ae7a6b38SJeff Roberson * Called once per hz tick. Used for cpu utilization information. This 2162ae7a6b38SJeff Roberson * is easier than trying to scale based on stathz. 2163ae7a6b38SJeff Roberson */ 2164ae7a6b38SJeff Roberson void 2165ae7a6b38SJeff Roberson sched_tick(void) 2166ae7a6b38SJeff Roberson { 2167ae7a6b38SJeff Roberson struct td_sched *ts; 2168ae7a6b38SJeff Roberson 2169ae7a6b38SJeff Roberson ts = curthread->td_sched; 2170e980fff6SJeff Roberson /* 2171e980fff6SJeff Roberson * Ticks is updated asynchronously on a single cpu. Check here to 2172e980fff6SJeff Roberson * avoid incrementing ts_ticks multiple times in a single tick. 2173e980fff6SJeff Roberson */ 2174cbc4ea28SIvan Voras if (ts->ts_incrtick == ticks) 2175e980fff6SJeff Roberson return; 2176ae7a6b38SJeff Roberson /* Adjust ticks for pctcpu */ 2177ae7a6b38SJeff Roberson ts->ts_ticks += 1 << SCHED_TICK_SHIFT; 2178ae7a6b38SJeff Roberson ts->ts_ltick = ticks; 2179cbc4ea28SIvan Voras ts->ts_incrtick = ticks; 2180ae7a6b38SJeff Roberson /* 2181ae7a6b38SJeff Roberson * Update if we've exceeded our desired tick threshhold by over one 2182ae7a6b38SJeff Roberson * second. 2183ae7a6b38SJeff Roberson */ 2184ae7a6b38SJeff Roberson if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick) 2185ae7a6b38SJeff Roberson sched_pctcpu_update(ts); 2186ae7a6b38SJeff Roberson } 2187ae7a6b38SJeff Roberson 2188ae7a6b38SJeff Roberson /* 2189ae7a6b38SJeff Roberson * Return whether the current CPU has runnable tasks. Used for in-kernel 2190ae7a6b38SJeff Roberson * cooperative idle threads. 2191ae7a6b38SJeff Roberson */ 219235e6168fSJeff Roberson int 219335e6168fSJeff Roberson sched_runnable(void) 219435e6168fSJeff Roberson { 2195ad1e7d28SJulian Elischer struct tdq *tdq; 2196b90816f1SJeff Roberson int load; 219735e6168fSJeff Roberson 2198b90816f1SJeff Roberson load = 1; 2199b90816f1SJeff Roberson 2200ad1e7d28SJulian Elischer tdq = TDQ_SELF(); 22013f741ca1SJeff Roberson if ((curthread->td_flags & TDF_IDLETD) != 0) { 2202d2ad694cSJeff Roberson if (tdq->tdq_load > 0) 22033f741ca1SJeff Roberson goto out; 22043f741ca1SJeff Roberson } else 2205d2ad694cSJeff Roberson if (tdq->tdq_load - 1 > 0) 2206b90816f1SJeff Roberson goto out; 2207b90816f1SJeff Roberson load = 0; 2208b90816f1SJeff Roberson out: 2209b90816f1SJeff Roberson return (load); 221035e6168fSJeff Roberson } 221135e6168fSJeff Roberson 2212ae7a6b38SJeff Roberson /* 2213ae7a6b38SJeff Roberson * Choose the highest priority thread to run. The thread is removed from 2214ae7a6b38SJeff Roberson * the run-queue while running however the load remains. For SMP we set 2215ae7a6b38SJeff Roberson * the tdq in the global idle bitmask if it idles here. 2216ae7a6b38SJeff Roberson */ 22177a5e5e2aSJeff Roberson struct thread * 2218c9f25d8fSJeff Roberson sched_choose(void) 2219c9f25d8fSJeff Roberson { 22209727e637SJeff Roberson struct thread *td; 2221ae7a6b38SJeff Roberson struct tdq *tdq; 2222ae7a6b38SJeff Roberson 2223ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2224ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 22259727e637SJeff Roberson td = tdq_choose(tdq); 22269727e637SJeff Roberson if (td) { 22279727e637SJeff Roberson td->td_sched->ts_ltick = ticks; 22289727e637SJeff Roberson tdq_runq_rem(tdq, td); 22290502fe2eSJeff Roberson tdq->tdq_lowpri = td->td_priority; 22309727e637SJeff Roberson return (td); 223135e6168fSJeff Roberson } 22320502fe2eSJeff Roberson tdq->tdq_lowpri = PRI_MAX_IDLE; 223362fa74d9SJeff Roberson return (PCPU_GET(idlethread)); 22347a5e5e2aSJeff Roberson } 22357a5e5e2aSJeff Roberson 2236ae7a6b38SJeff Roberson /* 2237ae7a6b38SJeff Roberson * Set owepreempt if necessary. Preemption never happens directly in ULE, 2238ae7a6b38SJeff Roberson * we always request it once we exit a critical section. 2239ae7a6b38SJeff Roberson */ 2240ae7a6b38SJeff Roberson static inline void 2241ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td) 22427a5e5e2aSJeff Roberson { 22437a5e5e2aSJeff Roberson struct thread *ctd; 22447a5e5e2aSJeff Roberson int cpri; 22457a5e5e2aSJeff Roberson int pri; 22467a5e5e2aSJeff Roberson 2247ff256d9cSJeff Roberson THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2248ff256d9cSJeff Roberson 22497a5e5e2aSJeff Roberson ctd = curthread; 22507a5e5e2aSJeff Roberson pri = td->td_priority; 22517a5e5e2aSJeff Roberson cpri = ctd->td_priority; 2252ff256d9cSJeff Roberson if (pri < cpri) 2253ff256d9cSJeff Roberson ctd->td_flags |= TDF_NEEDRESCHED; 22547a5e5e2aSJeff Roberson if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2255ae7a6b38SJeff Roberson return; 2256ff256d9cSJeff Roberson if (!sched_shouldpreempt(pri, cpri, 0)) 2257ae7a6b38SJeff Roberson return; 22587a5e5e2aSJeff Roberson ctd->td_owepreempt = 1; 225935e6168fSJeff Roberson } 226035e6168fSJeff Roberson 2261ae7a6b38SJeff Roberson /* 226273daf66fSJeff Roberson * Add a thread to a thread queue. Select the appropriate runq and add the 226373daf66fSJeff Roberson * thread to it. This is the internal function called when the tdq is 226473daf66fSJeff Roberson * predetermined. 2265ae7a6b38SJeff Roberson */ 226635e6168fSJeff Roberson void 2267ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags) 226835e6168fSJeff Roberson { 2269c9f25d8fSJeff Roberson 2270ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 22717a5e5e2aSJeff Roberson KASSERT((td->td_inhibitors == 0), 22727a5e5e2aSJeff Roberson ("sched_add: trying to run inhibited thread")); 22737a5e5e2aSJeff Roberson KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 22747a5e5e2aSJeff Roberson ("sched_add: bad thread state")); 2275b61ce5b0SJeff Roberson KASSERT(td->td_flags & TDF_INMEM, 2276b61ce5b0SJeff Roberson ("sched_add: thread swapped out")); 2277ae7a6b38SJeff Roberson 2278ae7a6b38SJeff Roberson if (td->td_priority < tdq->tdq_lowpri) 2279ae7a6b38SJeff Roberson tdq->tdq_lowpri = td->td_priority; 22809727e637SJeff Roberson tdq_runq_add(tdq, td, flags); 22819727e637SJeff Roberson tdq_load_add(tdq, td); 2282ae7a6b38SJeff Roberson } 2283ae7a6b38SJeff Roberson 2284ae7a6b38SJeff Roberson /* 2285ae7a6b38SJeff Roberson * Select the target thread queue and add a thread to it. Request 2286ae7a6b38SJeff Roberson * preemption or IPI a remote processor if required. 2287ae7a6b38SJeff Roberson */ 2288ae7a6b38SJeff Roberson void 2289ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags) 2290ae7a6b38SJeff Roberson { 2291ae7a6b38SJeff Roberson struct tdq *tdq; 22927b8bfa0dSJeff Roberson #ifdef SMP 2293ae7a6b38SJeff Roberson int cpu; 2294ae7a6b38SJeff Roberson #endif 22958f51ad55SJeff Roberson 22968f51ad55SJeff Roberson KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 22978f51ad55SJeff Roberson "prio:%d", td->td_priority, KTR_ATTR_LINKED, 22988f51ad55SJeff Roberson sched_tdname(curthread)); 22998f51ad55SJeff Roberson KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 23008f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(td)); 2301ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2302ae7a6b38SJeff Roberson /* 2303ae7a6b38SJeff Roberson * Recalculate the priority before we select the target cpu or 2304ae7a6b38SJeff Roberson * run-queue. 2305ae7a6b38SJeff Roberson */ 2306ae7a6b38SJeff Roberson if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2307ae7a6b38SJeff Roberson sched_priority(td); 2308ae7a6b38SJeff Roberson #ifdef SMP 2309ae7a6b38SJeff Roberson /* 2310ae7a6b38SJeff Roberson * Pick the destination cpu and if it isn't ours transfer to the 2311ae7a6b38SJeff Roberson * target cpu. 2312ae7a6b38SJeff Roberson */ 23139727e637SJeff Roberson cpu = sched_pickcpu(td, flags); 23149727e637SJeff Roberson tdq = sched_setcpu(td, cpu, flags); 2315ae7a6b38SJeff Roberson tdq_add(tdq, td, flags); 231673daf66fSJeff Roberson if (cpu != PCPU_GET(cpuid)) { 23179727e637SJeff Roberson tdq_notify(tdq, td); 23187b8bfa0dSJeff Roberson return; 23197b8bfa0dSJeff Roberson } 2320ae7a6b38SJeff Roberson #else 2321ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2322ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 2323ae7a6b38SJeff Roberson /* 2324ae7a6b38SJeff Roberson * Now that the thread is moving to the run-queue, set the lock 2325ae7a6b38SJeff Roberson * to the scheduler's lock. 2326ae7a6b38SJeff Roberson */ 2327ae7a6b38SJeff Roberson thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2328ae7a6b38SJeff Roberson tdq_add(tdq, td, flags); 23297b8bfa0dSJeff Roberson #endif 2330ae7a6b38SJeff Roberson if (!(flags & SRQ_YIELDING)) 2331ae7a6b38SJeff Roberson sched_setpreempt(td); 233235e6168fSJeff Roberson } 233335e6168fSJeff Roberson 2334ae7a6b38SJeff Roberson /* 2335ae7a6b38SJeff Roberson * Remove a thread from a run-queue without running it. This is used 2336ae7a6b38SJeff Roberson * when we're stealing a thread from a remote queue. Otherwise all threads 2337ae7a6b38SJeff Roberson * exit by calling sched_exit_thread() and sched_throw() themselves. 2338ae7a6b38SJeff Roberson */ 233935e6168fSJeff Roberson void 23407cf90fb3SJeff Roberson sched_rem(struct thread *td) 234135e6168fSJeff Roberson { 2342ad1e7d28SJulian Elischer struct tdq *tdq; 23437cf90fb3SJeff Roberson 23448f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 23458f51ad55SJeff Roberson "prio:%d", td->td_priority); 23469727e637SJeff Roberson tdq = TDQ_CPU(td->td_sched->ts_cpu); 2347ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2348ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 23497a5e5e2aSJeff Roberson KASSERT(TD_ON_RUNQ(td), 2350ad1e7d28SJulian Elischer ("sched_rem: thread not on run queue")); 23519727e637SJeff Roberson tdq_runq_rem(tdq, td); 23529727e637SJeff Roberson tdq_load_rem(tdq, td); 23537a5e5e2aSJeff Roberson TD_SET_CAN_RUN(td); 235462fa74d9SJeff Roberson if (td->td_priority == tdq->tdq_lowpri) 235562fa74d9SJeff Roberson tdq_setlowpri(tdq, NULL); 235635e6168fSJeff Roberson } 235735e6168fSJeff Roberson 2358ae7a6b38SJeff Roberson /* 2359ae7a6b38SJeff Roberson * Fetch cpu utilization information. Updates on demand. 2360ae7a6b38SJeff Roberson */ 236135e6168fSJeff Roberson fixpt_t 23627cf90fb3SJeff Roberson sched_pctcpu(struct thread *td) 236335e6168fSJeff Roberson { 236435e6168fSJeff Roberson fixpt_t pctcpu; 2365ad1e7d28SJulian Elischer struct td_sched *ts; 236635e6168fSJeff Roberson 236735e6168fSJeff Roberson pctcpu = 0; 2368ad1e7d28SJulian Elischer ts = td->td_sched; 2369ad1e7d28SJulian Elischer if (ts == NULL) 2370484288deSJeff Roberson return (0); 237135e6168fSJeff Roberson 23723da35a0aSJohn Baldwin THREAD_LOCK_ASSERT(td, MA_OWNED); 2373ad1e7d28SJulian Elischer if (ts->ts_ticks) { 237435e6168fSJeff Roberson int rtick; 237535e6168fSJeff Roberson 2376ad1e7d28SJulian Elischer sched_pctcpu_update(ts); 237735e6168fSJeff Roberson /* How many rtick per second ? */ 2378e7d50326SJeff Roberson rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2379e7d50326SJeff Roberson pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 238035e6168fSJeff Roberson } 238135e6168fSJeff Roberson 238235e6168fSJeff Roberson return (pctcpu); 238335e6168fSJeff Roberson } 238435e6168fSJeff Roberson 238562fa74d9SJeff Roberson /* 238662fa74d9SJeff Roberson * Enforce affinity settings for a thread. Called after adjustments to 238762fa74d9SJeff Roberson * cpumask. 238862fa74d9SJeff Roberson */ 2389885d51a3SJeff Roberson void 2390885d51a3SJeff Roberson sched_affinity(struct thread *td) 2391885d51a3SJeff Roberson { 239262fa74d9SJeff Roberson #ifdef SMP 239362fa74d9SJeff Roberson struct td_sched *ts; 239462fa74d9SJeff Roberson 239562fa74d9SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 239662fa74d9SJeff Roberson ts = td->td_sched; 239762fa74d9SJeff Roberson if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 239862fa74d9SJeff Roberson return; 239953a6c8b3SJeff Roberson if (TD_ON_RUNQ(td)) { 240053a6c8b3SJeff Roberson sched_rem(td); 240153a6c8b3SJeff Roberson sched_add(td, SRQ_BORING); 240253a6c8b3SJeff Roberson return; 240353a6c8b3SJeff Roberson } 240462fa74d9SJeff Roberson if (!TD_IS_RUNNING(td)) 240562fa74d9SJeff Roberson return; 240662fa74d9SJeff Roberson td->td_flags |= TDF_NEEDRESCHED; 240762fa74d9SJeff Roberson /* 24080f7a0ebdSMatthew D Fleming * Force a switch before returning to userspace. If the 24090f7a0ebdSMatthew D Fleming * target thread is not running locally send an ipi to force 24100f7a0ebdSMatthew D Fleming * the issue. 241162fa74d9SJeff Roberson */ 24120f7a0ebdSMatthew D Fleming if (td != curthread) 24130f7a0ebdSMatthew D Fleming ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 241462fa74d9SJeff Roberson #endif 2415885d51a3SJeff Roberson } 2416885d51a3SJeff Roberson 2417ae7a6b38SJeff Roberson /* 2418ae7a6b38SJeff Roberson * Bind a thread to a target cpu. 2419ae7a6b38SJeff Roberson */ 24209bacd788SJeff Roberson void 24219bacd788SJeff Roberson sched_bind(struct thread *td, int cpu) 24229bacd788SJeff Roberson { 2423ad1e7d28SJulian Elischer struct td_sched *ts; 24249bacd788SJeff Roberson 2425c47f202bSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 24261d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 2427ad1e7d28SJulian Elischer ts = td->td_sched; 24286b2f763fSJeff Roberson if (ts->ts_flags & TSF_BOUND) 2429c95d2db2SJeff Roberson sched_unbind(td); 24300f7a0ebdSMatthew D Fleming KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2431ad1e7d28SJulian Elischer ts->ts_flags |= TSF_BOUND; 24326b2f763fSJeff Roberson sched_pin(); 243380f86c9fSJeff Roberson if (PCPU_GET(cpuid) == cpu) 24349bacd788SJeff Roberson return; 24356b2f763fSJeff Roberson ts->ts_cpu = cpu; 24369bacd788SJeff Roberson /* When we return from mi_switch we'll be on the correct cpu. */ 2437279f949eSPoul-Henning Kamp mi_switch(SW_VOL, NULL); 24389bacd788SJeff Roberson } 24399bacd788SJeff Roberson 2440ae7a6b38SJeff Roberson /* 2441ae7a6b38SJeff Roberson * Release a bound thread. 2442ae7a6b38SJeff Roberson */ 24439bacd788SJeff Roberson void 24449bacd788SJeff Roberson sched_unbind(struct thread *td) 24459bacd788SJeff Roberson { 2446e7d50326SJeff Roberson struct td_sched *ts; 2447e7d50326SJeff Roberson 24487b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 24491d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 2450e7d50326SJeff Roberson ts = td->td_sched; 24516b2f763fSJeff Roberson if ((ts->ts_flags & TSF_BOUND) == 0) 24526b2f763fSJeff Roberson return; 2453e7d50326SJeff Roberson ts->ts_flags &= ~TSF_BOUND; 2454e7d50326SJeff Roberson sched_unpin(); 24559bacd788SJeff Roberson } 24569bacd788SJeff Roberson 245735e6168fSJeff Roberson int 2458ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td) 2459ebccf1e3SJoseph Koshy { 24607b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2461ad1e7d28SJulian Elischer return (td->td_sched->ts_flags & TSF_BOUND); 2462ebccf1e3SJoseph Koshy } 2463ebccf1e3SJoseph Koshy 2464ae7a6b38SJeff Roberson /* 2465ae7a6b38SJeff Roberson * Basic yield call. 2466ae7a6b38SJeff Roberson */ 246736ec198bSDavid Xu void 246836ec198bSDavid Xu sched_relinquish(struct thread *td) 246936ec198bSDavid Xu { 24707b20fb19SJeff Roberson thread_lock(td); 24718df78c41SJeff Roberson mi_switch(SW_VOL | SWT_RELINQUISH, NULL); 24727b20fb19SJeff Roberson thread_unlock(td); 247336ec198bSDavid Xu } 247436ec198bSDavid Xu 2475ae7a6b38SJeff Roberson /* 2476ae7a6b38SJeff Roberson * Return the total system load. 2477ae7a6b38SJeff Roberson */ 2478ebccf1e3SJoseph Koshy int 247933916c36SJeff Roberson sched_load(void) 248033916c36SJeff Roberson { 248133916c36SJeff Roberson #ifdef SMP 248233916c36SJeff Roberson int total; 248333916c36SJeff Roberson int i; 248433916c36SJeff Roberson 248533916c36SJeff Roberson total = 0; 24863aa6d94eSJohn Baldwin CPU_FOREACH(i) 248762fa74d9SJeff Roberson total += TDQ_CPU(i)->tdq_sysload; 248833916c36SJeff Roberson return (total); 248933916c36SJeff Roberson #else 2490d2ad694cSJeff Roberson return (TDQ_SELF()->tdq_sysload); 249133916c36SJeff Roberson #endif 249233916c36SJeff Roberson } 249333916c36SJeff Roberson 249433916c36SJeff Roberson int 249535e6168fSJeff Roberson sched_sizeof_proc(void) 249635e6168fSJeff Roberson { 249735e6168fSJeff Roberson return (sizeof(struct proc)); 249835e6168fSJeff Roberson } 249935e6168fSJeff Roberson 250035e6168fSJeff Roberson int 250135e6168fSJeff Roberson sched_sizeof_thread(void) 250235e6168fSJeff Roberson { 250335e6168fSJeff Roberson return (sizeof(struct thread) + sizeof(struct td_sched)); 250435e6168fSJeff Roberson } 2505b41f1452SDavid Xu 250609c8a4ccSJeff Roberson #ifdef SMP 250709c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) \ 250809c8a4ccSJeff Roberson ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 250909c8a4ccSJeff Roberson #else 251009c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) 1 251109c8a4ccSJeff Roberson #endif 251209c8a4ccSJeff Roberson 25137a5e5e2aSJeff Roberson /* 25147a5e5e2aSJeff Roberson * The actual idle process. 25157a5e5e2aSJeff Roberson */ 25167a5e5e2aSJeff Roberson void 25177a5e5e2aSJeff Roberson sched_idletd(void *dummy) 25187a5e5e2aSJeff Roberson { 25197a5e5e2aSJeff Roberson struct thread *td; 2520ae7a6b38SJeff Roberson struct tdq *tdq; 25211690c6c1SJeff Roberson int switchcnt; 25221690c6c1SJeff Roberson int i; 25237a5e5e2aSJeff Roberson 25247b55ab05SJeff Roberson mtx_assert(&Giant, MA_NOTOWNED); 25257a5e5e2aSJeff Roberson td = curthread; 2526ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2527ae7a6b38SJeff Roberson for (;;) { 2528ae7a6b38SJeff Roberson #ifdef SMP 25291690c6c1SJeff Roberson if (tdq_idled(tdq) == 0) 25301690c6c1SJeff Roberson continue; 2531ae7a6b38SJeff Roberson #endif 25321690c6c1SJeff Roberson switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 25331690c6c1SJeff Roberson /* 25341690c6c1SJeff Roberson * If we're switching very frequently, spin while checking 25351690c6c1SJeff Roberson * for load rather than entering a low power state that 25367b55ab05SJeff Roberson * may require an IPI. However, don't do any busy 25377b55ab05SJeff Roberson * loops while on SMT machines as this simply steals 25387b55ab05SJeff Roberson * cycles from cores doing useful work. 25391690c6c1SJeff Roberson */ 254009c8a4ccSJeff Roberson if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 25411690c6c1SJeff Roberson for (i = 0; i < sched_idlespins; i++) { 25421690c6c1SJeff Roberson if (tdq->tdq_load) 25431690c6c1SJeff Roberson break; 25441690c6c1SJeff Roberson cpu_spinwait(); 25451690c6c1SJeff Roberson } 25461690c6c1SJeff Roberson } 25476c47aaaeSJeff Roberson switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 25481690c6c1SJeff Roberson if (tdq->tdq_load == 0) 25496c47aaaeSJeff Roberson cpu_idle(switchcnt > 1); 25501690c6c1SJeff Roberson if (tdq->tdq_load) { 25511690c6c1SJeff Roberson thread_lock(td); 25521690c6c1SJeff Roberson mi_switch(SW_VOL | SWT_IDLE, NULL); 25531690c6c1SJeff Roberson thread_unlock(td); 25541690c6c1SJeff Roberson } 2555ae7a6b38SJeff Roberson } 2556b41f1452SDavid Xu } 2557e7d50326SJeff Roberson 25587b20fb19SJeff Roberson /* 25597b20fb19SJeff Roberson * A CPU is entering for the first time or a thread is exiting. 25607b20fb19SJeff Roberson */ 25617b20fb19SJeff Roberson void 25627b20fb19SJeff Roberson sched_throw(struct thread *td) 25637b20fb19SJeff Roberson { 256459c68134SJeff Roberson struct thread *newtd; 2565ae7a6b38SJeff Roberson struct tdq *tdq; 2566ae7a6b38SJeff Roberson 2567ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 25687b20fb19SJeff Roberson if (td == NULL) { 2569ae7a6b38SJeff Roberson /* Correct spinlock nesting and acquire the correct lock. */ 2570ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 25717b20fb19SJeff Roberson spinlock_exit(); 25727b20fb19SJeff Roberson } else { 2573ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 25749727e637SJeff Roberson tdq_load_rem(tdq, td); 2575eea4f254SJeff Roberson lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 25767b20fb19SJeff Roberson } 25777b20fb19SJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count")); 257859c68134SJeff Roberson newtd = choosethread(); 257959c68134SJeff Roberson TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 25807b20fb19SJeff Roberson PCPU_SET(switchtime, cpu_ticks()); 25817b20fb19SJeff Roberson PCPU_SET(switchticks, ticks); 258259c68134SJeff Roberson cpu_throw(td, newtd); /* doesn't return */ 25837b20fb19SJeff Roberson } 25847b20fb19SJeff Roberson 2585ae7a6b38SJeff Roberson /* 2586ae7a6b38SJeff Roberson * This is called from fork_exit(). Just acquire the correct locks and 2587ae7a6b38SJeff Roberson * let fork do the rest of the work. 2588ae7a6b38SJeff Roberson */ 25897b20fb19SJeff Roberson void 2590fe54587fSJeff Roberson sched_fork_exit(struct thread *td) 25917b20fb19SJeff Roberson { 2592ae7a6b38SJeff Roberson struct td_sched *ts; 2593ae7a6b38SJeff Roberson struct tdq *tdq; 2594ae7a6b38SJeff Roberson int cpuid; 25957b20fb19SJeff Roberson 25967b20fb19SJeff Roberson /* 25977b20fb19SJeff Roberson * Finish setting up thread glue so that it begins execution in a 2598ae7a6b38SJeff Roberson * non-nested critical section with the scheduler lock held. 25997b20fb19SJeff Roberson */ 2600ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 2601ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpuid); 2602ae7a6b38SJeff Roberson ts = td->td_sched; 2603ae7a6b38SJeff Roberson if (TD_IS_IDLETHREAD(td)) 2604ae7a6b38SJeff Roberson td->td_lock = TDQ_LOCKPTR(tdq); 2605ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2606ae7a6b38SJeff Roberson td->td_oncpu = cpuid; 260759c68134SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 2608eea4f254SJeff Roberson lock_profile_obtain_lock_success( 2609eea4f254SJeff Roberson &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 26107b20fb19SJeff Roberson } 26117b20fb19SJeff Roberson 26128f51ad55SJeff Roberson /* 26138f51ad55SJeff Roberson * Create on first use to catch odd startup conditons. 26148f51ad55SJeff Roberson */ 26158f51ad55SJeff Roberson char * 26168f51ad55SJeff Roberson sched_tdname(struct thread *td) 26178f51ad55SJeff Roberson { 26188f51ad55SJeff Roberson #ifdef KTR 26198f51ad55SJeff Roberson struct td_sched *ts; 26208f51ad55SJeff Roberson 26218f51ad55SJeff Roberson ts = td->td_sched; 26228f51ad55SJeff Roberson if (ts->ts_name[0] == '\0') 26238f51ad55SJeff Roberson snprintf(ts->ts_name, sizeof(ts->ts_name), 26248f51ad55SJeff Roberson "%s tid %d", td->td_name, td->td_tid); 26258f51ad55SJeff Roberson return (ts->ts_name); 26268f51ad55SJeff Roberson #else 26278f51ad55SJeff Roberson return (td->td_name); 26288f51ad55SJeff Roberson #endif 26298f51ad55SJeff Roberson } 26308f51ad55SJeff Roberson 263107095abfSIvan Voras #ifdef SMP 263207095abfSIvan Voras 263307095abfSIvan Voras /* 263407095abfSIvan Voras * Build the CPU topology dump string. Is recursively called to collect 263507095abfSIvan Voras * the topology tree. 263607095abfSIvan Voras */ 263707095abfSIvan Voras static int 263807095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 263907095abfSIvan Voras int indent) 264007095abfSIvan Voras { 264107095abfSIvan Voras int i, first; 264207095abfSIvan Voras 264307095abfSIvan Voras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 264407095abfSIvan Voras "", indent, cg->cg_level); 264507095abfSIvan Voras sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"0x%x\">", indent, "", 264607095abfSIvan Voras cg->cg_count, cg->cg_mask); 264707095abfSIvan Voras first = TRUE; 264807095abfSIvan Voras for (i = 0; i < MAXCPU; i++) { 264907095abfSIvan Voras if ((cg->cg_mask & (1 << i)) != 0) { 265007095abfSIvan Voras if (!first) 265107095abfSIvan Voras sbuf_printf(sb, ", "); 265207095abfSIvan Voras else 265307095abfSIvan Voras first = FALSE; 265407095abfSIvan Voras sbuf_printf(sb, "%d", i); 265507095abfSIvan Voras } 265607095abfSIvan Voras } 265707095abfSIvan Voras sbuf_printf(sb, "</cpu>\n"); 265807095abfSIvan Voras 265907095abfSIvan Voras if (cg->cg_flags != 0) { 2660611daf7eSIvan Voras sbuf_printf(sb, "%*s <flags>", indent, ""); 266107095abfSIvan Voras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 26625368befbSIvan Voras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 2663a401f2d0SIvan Voras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2664a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 26657b55ab05SJeff Roberson if ((cg->cg_flags & CG_FLAG_SMT) != 0) 2666a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 266707095abfSIvan Voras sbuf_printf(sb, "</flags>\n"); 2668611daf7eSIvan Voras } 266907095abfSIvan Voras 267007095abfSIvan Voras if (cg->cg_children > 0) { 267107095abfSIvan Voras sbuf_printf(sb, "%*s <children>\n", indent, ""); 267207095abfSIvan Voras for (i = 0; i < cg->cg_children; i++) 267307095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(sb, 267407095abfSIvan Voras &cg->cg_child[i], indent+2); 267507095abfSIvan Voras sbuf_printf(sb, "%*s </children>\n", indent, ""); 267607095abfSIvan Voras } 267707095abfSIvan Voras sbuf_printf(sb, "%*s</group>\n", indent, ""); 267807095abfSIvan Voras return (0); 267907095abfSIvan Voras } 268007095abfSIvan Voras 268107095abfSIvan Voras /* 268207095abfSIvan Voras * Sysctl handler for retrieving topology dump. It's a wrapper for 268307095abfSIvan Voras * the recursive sysctl_kern_smp_topology_spec_internal(). 268407095abfSIvan Voras */ 268507095abfSIvan Voras static int 268607095abfSIvan Voras sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 268707095abfSIvan Voras { 268807095abfSIvan Voras struct sbuf *topo; 268907095abfSIvan Voras int err; 269007095abfSIvan Voras 269107095abfSIvan Voras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 269207095abfSIvan Voras 2693aa880b90SIvan Voras topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND); 269407095abfSIvan Voras if (topo == NULL) 269507095abfSIvan Voras return (ENOMEM); 269607095abfSIvan Voras 269707095abfSIvan Voras sbuf_printf(topo, "<groups>\n"); 269807095abfSIvan Voras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 269907095abfSIvan Voras sbuf_printf(topo, "</groups>\n"); 270007095abfSIvan Voras 270107095abfSIvan Voras if (err == 0) { 270207095abfSIvan Voras sbuf_finish(topo); 270307095abfSIvan Voras err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo)); 270407095abfSIvan Voras } 270507095abfSIvan Voras sbuf_delete(topo); 270607095abfSIvan Voras return (err); 270707095abfSIvan Voras } 270807095abfSIvan Voras #endif 270907095abfSIvan Voras 27109727e637SJeff Roberson SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2711ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 2712e7d50326SJeff Roberson "Scheduler name"); 2713ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 2714ae7a6b38SJeff Roberson "Slice size for timeshare threads"); 2715ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 2716ae7a6b38SJeff Roberson "Interactivity score threshold"); 2717ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh, 2718ae7a6b38SJeff Roberson 0,"Min priority for preemption, lower priorities have greater precedence"); 2719c5aa6b58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 2720c5aa6b58SJeff Roberson 0,"Controls whether static kernel priorities are assigned to sleeping threads."); 27211690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 27221690c6c1SJeff Roberson 0,"Number of times idle will spin waiting for new work."); 27231690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh, 27241690c6c1SJeff Roberson 0,"Threshold before we will permit idle spinning."); 27257b8bfa0dSJeff Roberson #ifdef SMP 2726ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 2727ae7a6b38SJeff Roberson "Number of hz ticks to keep thread affinity for"); 2728ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 2729ae7a6b38SJeff Roberson "Enables the long-term load balancer"); 27307fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 27317fcf154aSJeff Roberson &balance_interval, 0, 27327fcf154aSJeff Roberson "Average frequency in stathz ticks to run the long-term balancer"); 2733ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0, 2734ae7a6b38SJeff Roberson "Steals work from another hyper-threaded core on idle"); 2735ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 2736ae7a6b38SJeff Roberson "Attempts to steal work from other cores before idling"); 273728994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 273828994a58SJeff Roberson "Minimum load on remote cpu before we'll steal"); 273907095abfSIvan Voras 274007095abfSIvan Voras /* Retrieve SMP topology */ 274107095abfSIvan Voras SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 274207095abfSIvan Voras CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 274307095abfSIvan Voras "XML dump of detected CPU topology"); 27447b8bfa0dSJeff Roberson #endif 2745e7d50326SJeff Roberson 274654b0e65fSJeff Roberson /* ps compat. All cpu percentages from ULE are weighted. */ 2747a5423ea3SJeff Roberson static int ccpu = 0; 2748e7d50326SJeff Roberson SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 2749