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 79880bf8b9SMarius Strobl #if defined(__powerpc__) && defined(E500) 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))) 876338c579SAttilio Rao #define TDQ_LOADNAME_LEN (sizeof("CPU ") + sizeof(__XSTRING(MAXCPU)) - 1 + 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 /* 12012d56c0fSJohn Baldwin * Priority ranges used for interactive and non-interactive timeshare 1212dc29adbSJohn Baldwin * threads. The timeshare priorities are split up into four ranges. 1222dc29adbSJohn Baldwin * The first range handles interactive threads. The last three ranges 1232dc29adbSJohn Baldwin * (NHALF, x, and NHALF) handle non-interactive threads with the outer 1242dc29adbSJohn Baldwin * ranges supporting nice values. 12512d56c0fSJohn Baldwin */ 1262dc29adbSJohn Baldwin #define PRI_TIMESHARE_RANGE (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE + 1) 1272dc29adbSJohn Baldwin #define PRI_INTERACT_RANGE ((PRI_TIMESHARE_RANGE - SCHED_PRI_NRESV) / 2) 12816705791SAndriy Gapon #define PRI_BATCH_RANGE (PRI_TIMESHARE_RANGE - PRI_INTERACT_RANGE) 1292dc29adbSJohn Baldwin 1302dc29adbSJohn Baldwin #define PRI_MIN_INTERACT PRI_MIN_TIMESHARE 1312dc29adbSJohn Baldwin #define PRI_MAX_INTERACT (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE - 1) 1322dc29adbSJohn Baldwin #define PRI_MIN_BATCH (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE) 13312d56c0fSJohn Baldwin #define PRI_MAX_BATCH PRI_MAX_TIMESHARE 13412d56c0fSJohn Baldwin 13512d56c0fSJohn Baldwin /* 136e7d50326SJeff Roberson * Cpu percentage computation macros and defines. 137e1f89c22SJeff Roberson * 138e7d50326SJeff Roberson * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 139e7d50326SJeff Roberson * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 1408ab80cf0SJeff Roberson * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 141e7d50326SJeff Roberson * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 142e7d50326SJeff Roberson * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 143e7d50326SJeff Roberson * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 14435e6168fSJeff Roberson */ 145e7d50326SJeff Roberson #define SCHED_TICK_SECS 10 146e7d50326SJeff Roberson #define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 1478ab80cf0SJeff Roberson #define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 148e7d50326SJeff Roberson #define SCHED_TICK_SHIFT 10 149e7d50326SJeff Roberson #define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 150eddb4efaSJeff Roberson #define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 15135e6168fSJeff Roberson 15235e6168fSJeff Roberson /* 153e7d50326SJeff Roberson * These macros determine priorities for non-interactive threads. They are 154e7d50326SJeff Roberson * assigned a priority based on their recent cpu utilization as expressed 155e7d50326SJeff Roberson * by the ratio of ticks to the tick total. NHALF priorities at the start 156e7d50326SJeff Roberson * and end of the MIN to MAX timeshare range are only reachable with negative 157e7d50326SJeff Roberson * or positive nice respectively. 158e7d50326SJeff Roberson * 159e7d50326SJeff Roberson * PRI_RANGE: Priority range for utilization dependent priorities. 160e7d50326SJeff Roberson * PRI_NRESV: Number of nice values. 161e7d50326SJeff Roberson * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 162e7d50326SJeff Roberson * PRI_NICE: Determines the part of the priority inherited from nice. 163e7d50326SJeff Roberson */ 164e7d50326SJeff Roberson #define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 165e7d50326SJeff Roberson #define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 16612d56c0fSJohn Baldwin #define SCHED_PRI_MIN (PRI_MIN_BATCH + SCHED_PRI_NHALF) 16712d56c0fSJohn Baldwin #define SCHED_PRI_MAX (PRI_MAX_BATCH - SCHED_PRI_NHALF) 16878920008SJohn Baldwin #define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1) 169e7d50326SJeff Roberson #define SCHED_PRI_TICKS(ts) \ 170e7d50326SJeff Roberson (SCHED_TICK_HZ((ts)) / \ 1711e516cf5SJeff Roberson (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 172e7d50326SJeff Roberson #define SCHED_PRI_NICE(nice) (nice) 173e7d50326SJeff Roberson 174e7d50326SJeff Roberson /* 175e7d50326SJeff Roberson * These determine the interactivity of a process. Interactivity differs from 176e7d50326SJeff Roberson * cpu utilization in that it expresses the voluntary time slept vs time ran 177e7d50326SJeff Roberson * while cpu utilization includes all time not running. This more accurately 178e7d50326SJeff Roberson * models the intent of the thread. 17935e6168fSJeff Roberson * 180407b0157SJeff Roberson * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 181407b0157SJeff Roberson * before throttling back. 182d322132cSJeff Roberson * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 183210491d3SJeff Roberson * INTERACT_MAX: Maximum interactivity value. Smaller is better. 1849f518f20SAttilio Rao * INTERACT_THRESH: Threshold for placement on the current runq. 18535e6168fSJeff Roberson */ 186e7d50326SJeff Roberson #define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 187e7d50326SJeff Roberson #define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 188210491d3SJeff Roberson #define SCHED_INTERACT_MAX (100) 189210491d3SJeff Roberson #define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 1904c9612c6SJeff Roberson #define SCHED_INTERACT_THRESH (30) 191e1f89c22SJeff Roberson 19235e6168fSJeff Roberson /* 193e7d50326SJeff Roberson * tickincr: Converts a stathz tick into a hz domain scaled by 194e7d50326SJeff Roberson * the shift factor. Without the shift the error rate 195e7d50326SJeff Roberson * due to rounding would be unacceptably high. 196e7d50326SJeff Roberson * realstathz: stathz is sometimes 0 and run off of hz. 197e7d50326SJeff Roberson * sched_slice: Runtime of each thread before rescheduling. 198ae7a6b38SJeff Roberson * preempt_thresh: Priority threshold for preemption and remote IPIs. 19935e6168fSJeff Roberson */ 200e7d50326SJeff Roberson static int sched_interact = SCHED_INTERACT_THRESH; 201e7d50326SJeff Roberson static int realstathz; 202e7d50326SJeff Roberson static int tickincr; 20373daf66fSJeff Roberson static int sched_slice = 1; 20402e2d6b4SJeff Roberson #ifdef PREEMPTION 20502e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION 20602e2d6b4SJeff Roberson static int preempt_thresh = PRI_MAX_IDLE; 20702e2d6b4SJeff Roberson #else 208ae7a6b38SJeff Roberson static int preempt_thresh = PRI_MIN_KERN; 20902e2d6b4SJeff Roberson #endif 21002e2d6b4SJeff Roberson #else 21102e2d6b4SJeff Roberson static int preempt_thresh = 0; 21202e2d6b4SJeff Roberson #endif 21312d56c0fSJohn Baldwin static int static_boost = PRI_MIN_BATCH; 2141690c6c1SJeff Roberson static int sched_idlespins = 10000; 215a157e425SAlexander Motin static int sched_idlespinthresh = 16; 216ae7a6b38SJeff Roberson 21735e6168fSJeff Roberson /* 218ae7a6b38SJeff Roberson * tdq - per processor runqs and statistics. All fields are protected by the 219ae7a6b38SJeff Roberson * tdq_lock. The load and lowpri may be accessed without to avoid excess 220ae7a6b38SJeff Roberson * locking in sched_pickcpu(); 22135e6168fSJeff Roberson */ 222ad1e7d28SJulian Elischer struct tdq { 22373daf66fSJeff Roberson /* Ordered to improve efficiency of cpu_search() and switch(). */ 22462fa74d9SJeff Roberson struct mtx tdq_lock; /* run queue lock. */ 22573daf66fSJeff Roberson struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 2261690c6c1SJeff Roberson volatile int tdq_load; /* Aggregate load. */ 2279f9ad565SAlexander Motin volatile int tdq_cpu_idle; /* cpu_idle() is active. */ 22873daf66fSJeff Roberson int tdq_sysload; /* For loadavg, !ITHD load. */ 22973daf66fSJeff Roberson int tdq_transferable; /* Transferable thread count. */ 2301690c6c1SJeff Roberson short tdq_switchcnt; /* Switches this tick. */ 2311690c6c1SJeff Roberson short tdq_oldswitchcnt; /* Switches last tick. */ 23273daf66fSJeff Roberson u_char tdq_lowpri; /* Lowest priority thread. */ 23373daf66fSJeff Roberson u_char tdq_ipipending; /* IPI pending. */ 23473daf66fSJeff Roberson u_char tdq_idx; /* Current insert index. */ 23573daf66fSJeff Roberson u_char tdq_ridx; /* Current removal index. */ 236e7d50326SJeff Roberson struct runq tdq_realtime; /* real-time run queue. */ 237ae7a6b38SJeff Roberson struct runq tdq_timeshare; /* timeshare run queue. */ 238ae7a6b38SJeff Roberson struct runq tdq_idle; /* Queue of IDLE threads. */ 2398f51ad55SJeff Roberson char tdq_name[TDQ_NAME_LEN]; 2408f51ad55SJeff Roberson #ifdef KTR 2418f51ad55SJeff Roberson char tdq_loadname[TDQ_LOADNAME_LEN]; 2428f51ad55SJeff Roberson #endif 243ae7a6b38SJeff Roberson } __aligned(64); 24435e6168fSJeff Roberson 2451690c6c1SJeff Roberson /* Idle thread states and config. */ 2461690c6c1SJeff Roberson #define TDQ_RUNNING 1 2471690c6c1SJeff Roberson #define TDQ_IDLE 2 2487b8bfa0dSJeff Roberson 24980f86c9fSJeff Roberson #ifdef SMP 25007095abfSIvan Voras struct cpu_group *cpu_top; /* CPU topology */ 2517b8bfa0dSJeff Roberson 25262fa74d9SJeff Roberson #define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 25362fa74d9SJeff Roberson #define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 2547b8bfa0dSJeff Roberson 2557b8bfa0dSJeff Roberson /* 2567b8bfa0dSJeff Roberson * Run-time tunables. 2577b8bfa0dSJeff Roberson */ 25828994a58SJeff Roberson static int rebalance = 1; 2597fcf154aSJeff Roberson static int balance_interval = 128; /* Default set in sched_initticks(). */ 2607b8bfa0dSJeff Roberson static int affinity; 26128994a58SJeff Roberson static int steal_idle = 1; 26228994a58SJeff Roberson static int steal_thresh = 2; 26380f86c9fSJeff Roberson 26435e6168fSJeff Roberson /* 265d2ad694cSJeff Roberson * One thread queue per processor. 26635e6168fSJeff Roberson */ 267ad1e7d28SJulian Elischer static struct tdq tdq_cpu[MAXCPU]; 2687fcf154aSJeff Roberson static struct tdq *balance_tdq; 2697fcf154aSJeff Roberson static int balance_ticks; 270*36acfc65SAlexander Motin static DPCPU_DEFINE(uint32_t, randomval); 271dc03363dSJeff Roberson 272ad1e7d28SJulian Elischer #define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 273ad1e7d28SJulian Elischer #define TDQ_CPU(x) (&tdq_cpu[(x)]) 274c47f202bSJeff Roberson #define TDQ_ID(x) ((int)((x) - tdq_cpu)) 27580f86c9fSJeff Roberson #else /* !SMP */ 276ad1e7d28SJulian Elischer static struct tdq tdq_cpu; 277dc03363dSJeff Roberson 27836b36916SJeff Roberson #define TDQ_ID(x) (0) 279ad1e7d28SJulian Elischer #define TDQ_SELF() (&tdq_cpu) 280ad1e7d28SJulian Elischer #define TDQ_CPU(x) (&tdq_cpu) 2810a016a05SJeff Roberson #endif 28235e6168fSJeff Roberson 283ae7a6b38SJeff Roberson #define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 284ae7a6b38SJeff Roberson #define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 285ae7a6b38SJeff Roberson #define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 286ae7a6b38SJeff Roberson #define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 28762fa74d9SJeff Roberson #define TDQ_LOCKPTR(t) (&(t)->tdq_lock) 288ae7a6b38SJeff Roberson 2898460a577SJohn Birrell static void sched_priority(struct thread *); 29021381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char); 2918460a577SJohn Birrell static int sched_interact_score(struct thread *); 2928460a577SJohn Birrell static void sched_interact_update(struct thread *); 2938460a577SJohn Birrell static void sched_interact_fork(struct thread *); 294ad1e7d28SJulian Elischer static void sched_pctcpu_update(struct td_sched *); 29535e6168fSJeff Roberson 2965d7ef00cSJeff Roberson /* Operations on per processor queues */ 2979727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *); 298ad1e7d28SJulian Elischer static void tdq_setup(struct tdq *); 2999727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *); 3009727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *); 3019727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int); 3029727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *); 303ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int); 304ad1e7d28SJulian Elischer void tdq_print(int cpu); 305e7d50326SJeff Roberson static void runq_print(struct runq *rq); 306ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int); 3075d7ef00cSJeff Roberson #ifdef SMP 30862fa74d9SJeff Roberson static int tdq_move(struct tdq *, struct tdq *); 309ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *); 3109727e637SJeff Roberson static void tdq_notify(struct tdq *, struct thread *); 3119727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int); 3129727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int); 3139727e637SJeff Roberson static int sched_pickcpu(struct thread *, int); 3147fcf154aSJeff Roberson static void sched_balance(void); 31562fa74d9SJeff Roberson static int sched_balance_pair(struct tdq *, struct tdq *); 3169727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int); 317ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *); 318c47f202bSJeff Roberson static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int); 31907095abfSIvan Voras static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 32007095abfSIvan Voras static int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 32107095abfSIvan Voras struct cpu_group *cg, int indent); 3225d7ef00cSJeff Roberson #endif 3235d7ef00cSJeff Roberson 324e7d50326SJeff Roberson static void sched_setup(void *dummy); 325237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 326e7d50326SJeff Roberson 327e7d50326SJeff Roberson static void sched_initticks(void *dummy); 328237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 329237fdd78SRobert Watson NULL); 330e7d50326SJeff Roberson 331ae7a6b38SJeff Roberson /* 332ae7a6b38SJeff Roberson * Print the threads waiting on a run-queue. 333ae7a6b38SJeff Roberson */ 334e7d50326SJeff Roberson static void 335e7d50326SJeff Roberson runq_print(struct runq *rq) 336e7d50326SJeff Roberson { 337e7d50326SJeff Roberson struct rqhead *rqh; 3389727e637SJeff Roberson struct thread *td; 339e7d50326SJeff Roberson int pri; 340e7d50326SJeff Roberson int j; 341e7d50326SJeff Roberson int i; 342e7d50326SJeff Roberson 343e7d50326SJeff Roberson for (i = 0; i < RQB_LEN; i++) { 344e7d50326SJeff Roberson printf("\t\trunq bits %d 0x%zx\n", 345e7d50326SJeff Roberson i, rq->rq_status.rqb_bits[i]); 346e7d50326SJeff Roberson for (j = 0; j < RQB_BPW; j++) 347e7d50326SJeff Roberson if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 348e7d50326SJeff Roberson pri = j + (i << RQB_L2BPW); 349e7d50326SJeff Roberson rqh = &rq->rq_queues[pri]; 3509727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 351e7d50326SJeff Roberson printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 3529727e637SJeff Roberson td, td->td_name, td->td_priority, 3539727e637SJeff Roberson td->td_rqindex, pri); 354e7d50326SJeff Roberson } 355e7d50326SJeff Roberson } 356e7d50326SJeff Roberson } 357e7d50326SJeff Roberson } 358e7d50326SJeff Roberson 359ae7a6b38SJeff Roberson /* 360ae7a6b38SJeff Roberson * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 361ae7a6b38SJeff Roberson */ 36215dc847eSJeff Roberson void 363ad1e7d28SJulian Elischer tdq_print(int cpu) 36415dc847eSJeff Roberson { 365ad1e7d28SJulian Elischer struct tdq *tdq; 36615dc847eSJeff Roberson 367ad1e7d28SJulian Elischer tdq = TDQ_CPU(cpu); 36815dc847eSJeff Roberson 369c47f202bSJeff Roberson printf("tdq %d:\n", TDQ_ID(tdq)); 37062fa74d9SJeff Roberson printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 37162fa74d9SJeff Roberson printf("\tLock name: %s\n", tdq->tdq_name); 372d2ad694cSJeff Roberson printf("\tload: %d\n", tdq->tdq_load); 3731690c6c1SJeff Roberson printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 3741690c6c1SJeff Roberson printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 375e7d50326SJeff Roberson printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 3763f872f85SJeff Roberson printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 3771690c6c1SJeff Roberson printf("\tload transferable: %d\n", tdq->tdq_transferable); 3781690c6c1SJeff Roberson printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 379e7d50326SJeff Roberson printf("\trealtime runq:\n"); 380e7d50326SJeff Roberson runq_print(&tdq->tdq_realtime); 381e7d50326SJeff Roberson printf("\ttimeshare runq:\n"); 382e7d50326SJeff Roberson runq_print(&tdq->tdq_timeshare); 383e7d50326SJeff Roberson printf("\tidle runq:\n"); 384e7d50326SJeff Roberson runq_print(&tdq->tdq_idle); 38515dc847eSJeff Roberson } 38615dc847eSJeff Roberson 387ff256d9cSJeff Roberson static inline int 388ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote) 389ff256d9cSJeff Roberson { 390ff256d9cSJeff Roberson /* 391ff256d9cSJeff Roberson * If the new priority is not better than the current priority there is 392ff256d9cSJeff Roberson * nothing to do. 393ff256d9cSJeff Roberson */ 394ff256d9cSJeff Roberson if (pri >= cpri) 395ff256d9cSJeff Roberson return (0); 396ff256d9cSJeff Roberson /* 397ff256d9cSJeff Roberson * Always preempt idle. 398ff256d9cSJeff Roberson */ 399ff256d9cSJeff Roberson if (cpri >= PRI_MIN_IDLE) 400ff256d9cSJeff Roberson return (1); 401ff256d9cSJeff Roberson /* 402ff256d9cSJeff Roberson * If preemption is disabled don't preempt others. 403ff256d9cSJeff Roberson */ 404ff256d9cSJeff Roberson if (preempt_thresh == 0) 405ff256d9cSJeff Roberson return (0); 406ff256d9cSJeff Roberson /* 407ff256d9cSJeff Roberson * Preempt if we exceed the threshold. 408ff256d9cSJeff Roberson */ 409ff256d9cSJeff Roberson if (pri <= preempt_thresh) 410ff256d9cSJeff Roberson return (1); 411ff256d9cSJeff Roberson /* 41212d56c0fSJohn Baldwin * If we're interactive or better and there is non-interactive 41312d56c0fSJohn Baldwin * or worse running preempt only remote processors. 414ff256d9cSJeff Roberson */ 41512d56c0fSJohn Baldwin if (remote && pri <= PRI_MAX_INTERACT && cpri > PRI_MAX_INTERACT) 416ff256d9cSJeff Roberson return (1); 417ff256d9cSJeff Roberson return (0); 418ff256d9cSJeff Roberson } 419ff256d9cSJeff Roberson 420ae7a6b38SJeff Roberson /* 421ae7a6b38SJeff Roberson * Add a thread to the actual run-queue. Keeps transferable counts up to 422ae7a6b38SJeff Roberson * date with what is actually on the run-queue. Selects the correct 423ae7a6b38SJeff Roberson * queue position for timeshare threads. 424ae7a6b38SJeff Roberson */ 425155b9987SJeff Roberson static __inline void 4269727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 427155b9987SJeff Roberson { 4289727e637SJeff Roberson struct td_sched *ts; 429c143ac21SJeff Roberson u_char pri; 430c143ac21SJeff Roberson 431ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 4329727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 43373daf66fSJeff Roberson 4349727e637SJeff Roberson pri = td->td_priority; 4359727e637SJeff Roberson ts = td->td_sched; 4369727e637SJeff Roberson TD_SET_RUNQ(td); 4379727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td)) { 438d2ad694cSJeff Roberson tdq->tdq_transferable++; 439ad1e7d28SJulian Elischer ts->ts_flags |= TSF_XFERABLE; 44080f86c9fSJeff Roberson } 44112d56c0fSJohn Baldwin if (pri < PRI_MIN_BATCH) { 442c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_realtime; 44312d56c0fSJohn Baldwin } else if (pri <= PRI_MAX_BATCH) { 444c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_timeshare; 44512d56c0fSJohn Baldwin KASSERT(pri <= PRI_MAX_BATCH && pri >= PRI_MIN_BATCH, 446e7d50326SJeff Roberson ("Invalid priority %d on timeshare runq", pri)); 447e7d50326SJeff Roberson /* 448e7d50326SJeff Roberson * This queue contains only priorities between MIN and MAX 449e7d50326SJeff Roberson * realtime. Use the whole queue to represent these values. 450e7d50326SJeff Roberson */ 451c47f202bSJeff Roberson if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 45216705791SAndriy Gapon pri = RQ_NQS * (pri - PRI_MIN_BATCH) / PRI_BATCH_RANGE; 453e7d50326SJeff Roberson pri = (pri + tdq->tdq_idx) % RQ_NQS; 4543f872f85SJeff Roberson /* 4553f872f85SJeff Roberson * This effectively shortens the queue by one so we 4563f872f85SJeff Roberson * can have a one slot difference between idx and 4573f872f85SJeff Roberson * ridx while we wait for threads to drain. 4583f872f85SJeff Roberson */ 4593f872f85SJeff Roberson if (tdq->tdq_ridx != tdq->tdq_idx && 4603f872f85SJeff Roberson pri == tdq->tdq_ridx) 4614499aff6SJeff Roberson pri = (unsigned char)(pri - 1) % RQ_NQS; 462e7d50326SJeff Roberson } else 4633f872f85SJeff Roberson pri = tdq->tdq_ridx; 4649727e637SJeff Roberson runq_add_pri(ts->ts_runq, td, pri, flags); 465c143ac21SJeff Roberson return; 466e7d50326SJeff Roberson } else 46773daf66fSJeff Roberson ts->ts_runq = &tdq->tdq_idle; 4689727e637SJeff Roberson runq_add(ts->ts_runq, td, flags); 46973daf66fSJeff Roberson } 47073daf66fSJeff Roberson 47173daf66fSJeff Roberson /* 472ae7a6b38SJeff Roberson * Remove a thread from a run-queue. This typically happens when a thread 473ae7a6b38SJeff Roberson * is selected to run. Running threads are not on the queue and the 474ae7a6b38SJeff Roberson * transferable count does not reflect them. 475ae7a6b38SJeff Roberson */ 476155b9987SJeff Roberson static __inline void 4779727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td) 478155b9987SJeff Roberson { 4799727e637SJeff Roberson struct td_sched *ts; 4809727e637SJeff Roberson 4819727e637SJeff Roberson ts = td->td_sched; 482ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 483ae7a6b38SJeff Roberson KASSERT(ts->ts_runq != NULL, 4849727e637SJeff Roberson ("tdq_runq_remove: thread %p null ts_runq", td)); 485ad1e7d28SJulian Elischer if (ts->ts_flags & TSF_XFERABLE) { 486d2ad694cSJeff Roberson tdq->tdq_transferable--; 487ad1e7d28SJulian Elischer ts->ts_flags &= ~TSF_XFERABLE; 48880f86c9fSJeff Roberson } 4893f872f85SJeff Roberson if (ts->ts_runq == &tdq->tdq_timeshare) { 4903f872f85SJeff Roberson if (tdq->tdq_idx != tdq->tdq_ridx) 4919727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 492e7d50326SJeff Roberson else 4939727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, NULL); 4943f872f85SJeff Roberson } else 4959727e637SJeff Roberson runq_remove(ts->ts_runq, td); 496155b9987SJeff Roberson } 497155b9987SJeff Roberson 498ae7a6b38SJeff Roberson /* 499ae7a6b38SJeff Roberson * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 500ae7a6b38SJeff Roberson * for this thread to the referenced thread queue. 501ae7a6b38SJeff Roberson */ 502a8949de2SJeff Roberson static void 5039727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td) 5045d7ef00cSJeff Roberson { 505ae7a6b38SJeff Roberson 506ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 5079727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 50803d17db7SJeff Roberson 509d2ad694cSJeff Roberson tdq->tdq_load++; 5101b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 511d2ad694cSJeff Roberson tdq->tdq_sysload++; 5128f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 5135d7ef00cSJeff Roberson } 51415dc847eSJeff Roberson 515ae7a6b38SJeff Roberson /* 516ae7a6b38SJeff Roberson * Remove the load from a thread that is transitioning to a sleep state or 517ae7a6b38SJeff Roberson * exiting. 518ae7a6b38SJeff Roberson */ 519a8949de2SJeff Roberson static void 5209727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td) 5215d7ef00cSJeff Roberson { 522ae7a6b38SJeff Roberson 5239727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 524ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 525ae7a6b38SJeff Roberson KASSERT(tdq->tdq_load != 0, 526c47f202bSJeff Roberson ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 52703d17db7SJeff Roberson 528d2ad694cSJeff Roberson tdq->tdq_load--; 5291b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 53003d17db7SJeff Roberson tdq->tdq_sysload--; 5318f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 53215dc847eSJeff Roberson } 53315dc847eSJeff Roberson 534356500a3SJeff Roberson /* 53562fa74d9SJeff Roberson * Set lowpri to its exact value by searching the run-queue and 53662fa74d9SJeff Roberson * evaluating curthread. curthread may be passed as an optimization. 537356500a3SJeff Roberson */ 53822bf7d9aSJeff Roberson static void 53962fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd) 54062fa74d9SJeff Roberson { 54162fa74d9SJeff Roberson struct thread *td; 54262fa74d9SJeff Roberson 54362fa74d9SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 54462fa74d9SJeff Roberson if (ctd == NULL) 54562fa74d9SJeff Roberson ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread; 5469727e637SJeff Roberson td = tdq_choose(tdq); 5479727e637SJeff Roberson if (td == NULL || td->td_priority > ctd->td_priority) 54862fa74d9SJeff Roberson tdq->tdq_lowpri = ctd->td_priority; 54962fa74d9SJeff Roberson else 55062fa74d9SJeff Roberson tdq->tdq_lowpri = td->td_priority; 55162fa74d9SJeff Roberson } 55262fa74d9SJeff Roberson 55362fa74d9SJeff Roberson #ifdef SMP 55462fa74d9SJeff Roberson struct cpu_search { 555c76ee827SJeff Roberson cpuset_t cs_mask; 556*36acfc65SAlexander Motin u_int cs_prefer; 557*36acfc65SAlexander Motin int cs_pri; /* Min priority for low. */ 558*36acfc65SAlexander Motin int cs_limit; /* Max load for low, min load for high. */ 559*36acfc65SAlexander Motin int cs_cpu; 560*36acfc65SAlexander Motin int cs_load; 56162fa74d9SJeff Roberson }; 56262fa74d9SJeff Roberson 56362fa74d9SJeff Roberson #define CPU_SEARCH_LOWEST 0x1 56462fa74d9SJeff Roberson #define CPU_SEARCH_HIGHEST 0x2 56562fa74d9SJeff Roberson #define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST) 56662fa74d9SJeff Roberson 567c76ee827SJeff Roberson #define CPUSET_FOREACH(cpu, mask) \ 568c76ee827SJeff Roberson for ((cpu) = 0; (cpu) <= mp_maxid; (cpu)++) \ 56971a19bdcSAttilio Rao if (CPU_ISSET(cpu, &mask)) 57062fa74d9SJeff Roberson 571*36acfc65SAlexander Motin static __inline int cpu_search(const struct cpu_group *cg, struct cpu_search *low, 57262fa74d9SJeff Roberson struct cpu_search *high, const int match); 573*36acfc65SAlexander Motin int cpu_search_lowest(const struct cpu_group *cg, struct cpu_search *low); 574*36acfc65SAlexander Motin int cpu_search_highest(const struct cpu_group *cg, struct cpu_search *high); 575*36acfc65SAlexander Motin int cpu_search_both(const struct cpu_group *cg, struct cpu_search *low, 57662fa74d9SJeff Roberson struct cpu_search *high); 57762fa74d9SJeff Roberson 57862fa74d9SJeff Roberson /* 57962fa74d9SJeff Roberson * Search the tree of cpu_groups for the lowest or highest loaded cpu 58062fa74d9SJeff Roberson * according to the match argument. This routine actually compares the 58162fa74d9SJeff Roberson * load on all paths through the tree and finds the least loaded cpu on 58262fa74d9SJeff Roberson * the least loaded path, which may differ from the least loaded cpu in 58362fa74d9SJeff Roberson * the system. This balances work among caches and busses. 58462fa74d9SJeff Roberson * 58562fa74d9SJeff Roberson * This inline is instantiated in three forms below using constants for the 58662fa74d9SJeff Roberson * match argument. It is reduced to the minimum set for each case. It is 58762fa74d9SJeff Roberson * also recursive to the depth of the tree. 58862fa74d9SJeff Roberson */ 589d628fbfaSJohn Baldwin static __inline int 590*36acfc65SAlexander Motin cpu_search(const struct cpu_group *cg, struct cpu_search *low, 59162fa74d9SJeff Roberson struct cpu_search *high, const int match) 59262fa74d9SJeff Roberson { 59362fa74d9SJeff Roberson struct cpu_search lgroup; 59462fa74d9SJeff Roberson struct cpu_search hgroup; 595*36acfc65SAlexander Motin cpuset_t cpumask; 59662fa74d9SJeff Roberson struct cpu_group *child; 597*36acfc65SAlexander Motin struct tdq *tdq; 598*36acfc65SAlexander Motin int cpu, i, hload, lload, load, total, rnd; 59962fa74d9SJeff Roberson 600*36acfc65SAlexander Motin total = 0; 601*36acfc65SAlexander Motin cpumask = cg->cg_mask; 60262fa74d9SJeff Roberson if (match & CPU_SEARCH_LOWEST) { 603*36acfc65SAlexander Motin lload = INT_MAX; 604*36acfc65SAlexander Motin low->cs_load = INT_MAX; 60562fa74d9SJeff Roberson lgroup = *low; 60662fa74d9SJeff Roberson } 60762fa74d9SJeff Roberson if (match & CPU_SEARCH_HIGHEST) { 608*36acfc65SAlexander Motin hload = -1; 609*36acfc65SAlexander Motin high->cs_load = -1; 61062fa74d9SJeff Roberson hgroup = *high; 61162fa74d9SJeff Roberson } 612*36acfc65SAlexander Motin 613*36acfc65SAlexander Motin /* Iterate through the child CPU groups and then remaining CPUs. */ 614*36acfc65SAlexander Motin for (i = 0, cpu = 0; i <= cg->cg_children; ) { 615*36acfc65SAlexander Motin if (i >= cg->cg_children) { 616*36acfc65SAlexander Motin while (cpu <= mp_maxid && !CPU_ISSET(cpu, &cpumask)) 617*36acfc65SAlexander Motin cpu++; 618*36acfc65SAlexander Motin if (cpu > mp_maxid) 619*36acfc65SAlexander Motin break; 620*36acfc65SAlexander Motin child = NULL; 621*36acfc65SAlexander Motin } else 622*36acfc65SAlexander Motin child = &cg->cg_child[i]; 623*36acfc65SAlexander Motin 624*36acfc65SAlexander Motin if (child) { /* Handle child CPU group. */ 625*36acfc65SAlexander Motin CPU_NAND(&cpumask, &child->cg_mask); 62662fa74d9SJeff Roberson switch (match) { 62762fa74d9SJeff Roberson case CPU_SEARCH_LOWEST: 62862fa74d9SJeff Roberson load = cpu_search_lowest(child, &lgroup); 62962fa74d9SJeff Roberson break; 63062fa74d9SJeff Roberson case CPU_SEARCH_HIGHEST: 63162fa74d9SJeff Roberson load = cpu_search_highest(child, &hgroup); 63262fa74d9SJeff Roberson break; 63362fa74d9SJeff Roberson case CPU_SEARCH_BOTH: 63462fa74d9SJeff Roberson load = cpu_search_both(child, &lgroup, &hgroup); 63562fa74d9SJeff Roberson break; 63662fa74d9SJeff Roberson } 637*36acfc65SAlexander Motin } else { /* Handle child CPU. */ 638*36acfc65SAlexander Motin tdq = TDQ_CPU(cpu); 639*36acfc65SAlexander Motin load = tdq->tdq_load * 256; 640*36acfc65SAlexander Motin rnd = DPCPU_SET(randomval, 641*36acfc65SAlexander Motin DPCPU_GET(randomval) * 69069 + 5) >> 26; 642*36acfc65SAlexander Motin if (match & CPU_SEARCH_LOWEST) { 643*36acfc65SAlexander Motin if (cpu == low->cs_prefer) 644*36acfc65SAlexander Motin load -= 64; 645*36acfc65SAlexander Motin /* If that CPU is allowed and get data. */ 646*36acfc65SAlexander Motin if (CPU_ISSET(cpu, &lgroup.cs_mask) && 647*36acfc65SAlexander Motin tdq->tdq_lowpri > lgroup.cs_pri && 648*36acfc65SAlexander Motin tdq->tdq_load <= lgroup.cs_limit) { 649*36acfc65SAlexander Motin lgroup.cs_cpu = cpu; 650*36acfc65SAlexander Motin lgroup.cs_load = load - rnd; 651*36acfc65SAlexander Motin } 65262fa74d9SJeff Roberson } 65362fa74d9SJeff Roberson if (match & CPU_SEARCH_HIGHEST) 654*36acfc65SAlexander Motin if (CPU_ISSET(cpu, &hgroup.cs_mask) && 655*36acfc65SAlexander Motin tdq->tdq_load >= hgroup.cs_limit && 656*36acfc65SAlexander Motin tdq->tdq_transferable) { 657*36acfc65SAlexander Motin hgroup.cs_cpu = cpu; 658*36acfc65SAlexander Motin hgroup.cs_load = load - rnd; 65962fa74d9SJeff Roberson } 66062fa74d9SJeff Roberson } 661*36acfc65SAlexander Motin total += load; 66262fa74d9SJeff Roberson 663*36acfc65SAlexander Motin /* We have info about child item. Compare it. */ 664*36acfc65SAlexander Motin if (match & CPU_SEARCH_LOWEST) { 665*36acfc65SAlexander Motin if ((load < lload) || 666*36acfc65SAlexander Motin (load == lload && lgroup.cs_load < low->cs_load)) { 667*36acfc65SAlexander Motin lload = load; 668*36acfc65SAlexander Motin low->cs_cpu = lgroup.cs_cpu; 669*36acfc65SAlexander Motin low->cs_load = lgroup.cs_load; 670*36acfc65SAlexander Motin } 671*36acfc65SAlexander Motin } 672*36acfc65SAlexander Motin if (match & CPU_SEARCH_HIGHEST) 673*36acfc65SAlexander Motin if ((load > hload) || 674*36acfc65SAlexander Motin (load == hload && hgroup.cs_load > high->cs_load)) { 675*36acfc65SAlexander Motin hload = load; 676*36acfc65SAlexander Motin high->cs_cpu = hgroup.cs_cpu; 677*36acfc65SAlexander Motin high->cs_load = hgroup.cs_load; 678*36acfc65SAlexander Motin } 679*36acfc65SAlexander Motin if (child) 680*36acfc65SAlexander Motin i++; 681*36acfc65SAlexander Motin else 682*36acfc65SAlexander Motin cpu++; 68362fa74d9SJeff Roberson } 68462fa74d9SJeff Roberson return (total); 68562fa74d9SJeff Roberson } 68662fa74d9SJeff Roberson 68762fa74d9SJeff Roberson /* 68862fa74d9SJeff Roberson * cpu_search instantiations must pass constants to maintain the inline 68962fa74d9SJeff Roberson * optimization. 69062fa74d9SJeff Roberson */ 69162fa74d9SJeff Roberson int 692*36acfc65SAlexander Motin cpu_search_lowest(const struct cpu_group *cg, struct cpu_search *low) 69362fa74d9SJeff Roberson { 69462fa74d9SJeff Roberson return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST); 69562fa74d9SJeff Roberson } 69662fa74d9SJeff Roberson 69762fa74d9SJeff Roberson int 698*36acfc65SAlexander Motin cpu_search_highest(const struct cpu_group *cg, struct cpu_search *high) 69962fa74d9SJeff Roberson { 70062fa74d9SJeff Roberson return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST); 70162fa74d9SJeff Roberson } 70262fa74d9SJeff Roberson 70362fa74d9SJeff Roberson int 704*36acfc65SAlexander Motin cpu_search_both(const struct cpu_group *cg, struct cpu_search *low, 70562fa74d9SJeff Roberson struct cpu_search *high) 70662fa74d9SJeff Roberson { 70762fa74d9SJeff Roberson return cpu_search(cg, low, high, CPU_SEARCH_BOTH); 70862fa74d9SJeff Roberson } 70962fa74d9SJeff Roberson 71062fa74d9SJeff Roberson /* 71162fa74d9SJeff Roberson * Find the cpu with the least load via the least loaded path that has a 71262fa74d9SJeff Roberson * lowpri greater than pri pri. A pri of -1 indicates any priority is 71362fa74d9SJeff Roberson * acceptable. 71462fa74d9SJeff Roberson */ 71562fa74d9SJeff Roberson static inline int 716*36acfc65SAlexander Motin sched_lowest(const struct cpu_group *cg, cpuset_t mask, int pri, int maxload, 717*36acfc65SAlexander Motin int prefer) 71862fa74d9SJeff Roberson { 71962fa74d9SJeff Roberson struct cpu_search low; 72062fa74d9SJeff Roberson 72162fa74d9SJeff Roberson low.cs_cpu = -1; 722*36acfc65SAlexander Motin low.cs_prefer = prefer; 72362fa74d9SJeff Roberson low.cs_mask = mask; 724*36acfc65SAlexander Motin low.cs_pri = pri; 725*36acfc65SAlexander Motin low.cs_limit = maxload; 72662fa74d9SJeff Roberson cpu_search_lowest(cg, &low); 72762fa74d9SJeff Roberson return low.cs_cpu; 72862fa74d9SJeff Roberson } 72962fa74d9SJeff Roberson 73062fa74d9SJeff Roberson /* 73162fa74d9SJeff Roberson * Find the cpu with the highest load via the highest loaded path. 73262fa74d9SJeff Roberson */ 73362fa74d9SJeff Roberson static inline int 734*36acfc65SAlexander Motin sched_highest(const struct cpu_group *cg, cpuset_t mask, int minload) 73562fa74d9SJeff Roberson { 73662fa74d9SJeff Roberson struct cpu_search high; 73762fa74d9SJeff Roberson 73862fa74d9SJeff Roberson high.cs_cpu = -1; 73962fa74d9SJeff Roberson high.cs_mask = mask; 74062fa74d9SJeff Roberson high.cs_limit = minload; 74162fa74d9SJeff Roberson cpu_search_highest(cg, &high); 74262fa74d9SJeff Roberson return high.cs_cpu; 74362fa74d9SJeff Roberson } 74462fa74d9SJeff Roberson 74562fa74d9SJeff Roberson /* 74662fa74d9SJeff Roberson * Simultaneously find the highest and lowest loaded cpu reachable via 74762fa74d9SJeff Roberson * cg. 74862fa74d9SJeff Roberson */ 74962fa74d9SJeff Roberson static inline void 750*36acfc65SAlexander Motin sched_both(const struct cpu_group *cg, cpuset_t mask, int *lowcpu, int *highcpu) 75162fa74d9SJeff Roberson { 75262fa74d9SJeff Roberson struct cpu_search high; 75362fa74d9SJeff Roberson struct cpu_search low; 75462fa74d9SJeff Roberson 75562fa74d9SJeff Roberson low.cs_cpu = -1; 756*36acfc65SAlexander Motin low.cs_prefer = -1; 757*36acfc65SAlexander Motin low.cs_pri = -1; 758*36acfc65SAlexander Motin low.cs_limit = INT_MAX; 75962fa74d9SJeff Roberson low.cs_mask = mask; 76062fa74d9SJeff Roberson high.cs_cpu = -1; 76162fa74d9SJeff Roberson high.cs_limit = -1; 76262fa74d9SJeff Roberson high.cs_mask = mask; 76362fa74d9SJeff Roberson cpu_search_both(cg, &low, &high); 76462fa74d9SJeff Roberson *lowcpu = low.cs_cpu; 76562fa74d9SJeff Roberson *highcpu = high.cs_cpu; 76662fa74d9SJeff Roberson return; 76762fa74d9SJeff Roberson } 76862fa74d9SJeff Roberson 76962fa74d9SJeff Roberson static void 77062fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg) 77162fa74d9SJeff Roberson { 772*36acfc65SAlexander Motin cpuset_t hmask, lmask; 773*36acfc65SAlexander Motin int high, low, anylow; 77462fa74d9SJeff Roberson 775*36acfc65SAlexander Motin CPU_FILL(&hmask); 77662fa74d9SJeff Roberson for (;;) { 777*36acfc65SAlexander Motin high = sched_highest(cg, hmask, 1); 778*36acfc65SAlexander Motin /* Stop if there is no more CPU with transferrable threads. */ 779*36acfc65SAlexander Motin if (high == -1) 78062fa74d9SJeff Roberson break; 781*36acfc65SAlexander Motin CPU_CLR(high, &hmask); 782*36acfc65SAlexander Motin CPU_COPY(&hmask, &lmask); 783*36acfc65SAlexander Motin /* Stop if there is no more CPU left for low. */ 784*36acfc65SAlexander Motin if (CPU_EMPTY(&lmask)) 78562fa74d9SJeff Roberson break; 786*36acfc65SAlexander Motin anylow = 1; 787*36acfc65SAlexander Motin nextlow: 788*36acfc65SAlexander Motin low = sched_lowest(cg, lmask, -1, 789*36acfc65SAlexander Motin TDQ_CPU(high)->tdq_load - 1, high); 790*36acfc65SAlexander Motin /* Stop if we looked well and found no less loaded CPU. */ 791*36acfc65SAlexander Motin if (anylow && low == -1) 792*36acfc65SAlexander Motin break; 793*36acfc65SAlexander Motin /* Go to next high if we found no less loaded CPU. */ 794*36acfc65SAlexander Motin if (low == -1) 795*36acfc65SAlexander Motin continue; 796*36acfc65SAlexander Motin /* Transfer thread from high to low. */ 797*36acfc65SAlexander Motin if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low))) { 798*36acfc65SAlexander Motin /* CPU that got thread can no longer be a donor. */ 799*36acfc65SAlexander Motin CPU_CLR(low, &hmask); 800*36acfc65SAlexander Motin } else { 80162fa74d9SJeff Roberson /* 802*36acfc65SAlexander Motin * If failed, then there is no threads on high 803*36acfc65SAlexander Motin * that can run on this low. Drop low from low 804*36acfc65SAlexander Motin * mask and look for different one. 80562fa74d9SJeff Roberson */ 806*36acfc65SAlexander Motin CPU_CLR(low, &lmask); 807*36acfc65SAlexander Motin anylow = 0; 808*36acfc65SAlexander Motin goto nextlow; 80962fa74d9SJeff Roberson } 810*36acfc65SAlexander Motin } 81162fa74d9SJeff Roberson } 81262fa74d9SJeff Roberson 81362fa74d9SJeff Roberson static void 81462375ca8SEd Schouten sched_balance(void) 815356500a3SJeff Roberson { 8167fcf154aSJeff Roberson struct tdq *tdq; 817356500a3SJeff Roberson 8187fcf154aSJeff Roberson /* 8197fcf154aSJeff Roberson * Select a random time between .5 * balance_interval and 8207fcf154aSJeff Roberson * 1.5 * balance_interval. 8217fcf154aSJeff Roberson */ 8227fcf154aSJeff Roberson balance_ticks = max(balance_interval / 2, 1); 8237fcf154aSJeff Roberson balance_ticks += random() % balance_interval; 824ae7a6b38SJeff Roberson if (smp_started == 0 || rebalance == 0) 825598b368dSJeff Roberson return; 8267fcf154aSJeff Roberson tdq = TDQ_SELF(); 8277fcf154aSJeff Roberson TDQ_UNLOCK(tdq); 82862fa74d9SJeff Roberson sched_balance_group(cpu_top); 8297fcf154aSJeff Roberson TDQ_LOCK(tdq); 830cac77d04SJeff Roberson } 83186f8ae96SJeff Roberson 832ae7a6b38SJeff Roberson /* 833ae7a6b38SJeff Roberson * Lock two thread queues using their address to maintain lock order. 834ae7a6b38SJeff Roberson */ 835ae7a6b38SJeff Roberson static void 836ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two) 837ae7a6b38SJeff Roberson { 838ae7a6b38SJeff Roberson if (one < two) { 839ae7a6b38SJeff Roberson TDQ_LOCK(one); 840ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(two, MTX_DUPOK); 841ae7a6b38SJeff Roberson } else { 842ae7a6b38SJeff Roberson TDQ_LOCK(two); 843ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(one, MTX_DUPOK); 844ae7a6b38SJeff Roberson } 845ae7a6b38SJeff Roberson } 846ae7a6b38SJeff Roberson 847ae7a6b38SJeff Roberson /* 8487fcf154aSJeff Roberson * Unlock two thread queues. Order is not important here. 8497fcf154aSJeff Roberson */ 8507fcf154aSJeff Roberson static void 8517fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two) 8527fcf154aSJeff Roberson { 8537fcf154aSJeff Roberson TDQ_UNLOCK(one); 8547fcf154aSJeff Roberson TDQ_UNLOCK(two); 8557fcf154aSJeff Roberson } 8567fcf154aSJeff Roberson 8577fcf154aSJeff Roberson /* 858ae7a6b38SJeff Roberson * Transfer load between two imbalanced thread queues. 859ae7a6b38SJeff Roberson */ 86062fa74d9SJeff Roberson static int 861ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low) 862cac77d04SJeff Roberson { 86362fa74d9SJeff Roberson int moved; 864880bf8b9SMarius Strobl int cpu; 865cac77d04SJeff Roberson 866ae7a6b38SJeff Roberson tdq_lock_pair(high, low); 86762fa74d9SJeff Roberson moved = 0; 868155b9987SJeff Roberson /* 869155b9987SJeff Roberson * Determine what the imbalance is and then adjust that to how many 870d2ad694cSJeff Roberson * threads we actually have to give up (transferable). 871155b9987SJeff Roberson */ 872*36acfc65SAlexander Motin if (high->tdq_transferable != 0 && high->tdq_load > low->tdq_load && 873*36acfc65SAlexander Motin (moved = tdq_move(high, low)) > 0) { 874a5423ea3SJeff Roberson /* 875880bf8b9SMarius Strobl * In case the target isn't the current cpu IPI it to force a 876880bf8b9SMarius Strobl * reschedule with the new workload. 877a5423ea3SJeff Roberson */ 878880bf8b9SMarius Strobl cpu = TDQ_ID(low); 879880bf8b9SMarius Strobl sched_pin(); 880880bf8b9SMarius Strobl if (cpu != PCPU_GET(cpuid)) 881880bf8b9SMarius Strobl ipi_cpu(cpu, IPI_PREEMPT); 882880bf8b9SMarius Strobl sched_unpin(); 883ae7a6b38SJeff Roberson } 8847fcf154aSJeff Roberson tdq_unlock_pair(high, low); 88562fa74d9SJeff Roberson return (moved); 886356500a3SJeff Roberson } 887356500a3SJeff Roberson 888ae7a6b38SJeff Roberson /* 889ae7a6b38SJeff Roberson * Move a thread from one thread queue to another. 890ae7a6b38SJeff Roberson */ 89162fa74d9SJeff Roberson static int 892ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to) 893356500a3SJeff Roberson { 894ad1e7d28SJulian Elischer struct td_sched *ts; 895ae7a6b38SJeff Roberson struct thread *td; 896ae7a6b38SJeff Roberson struct tdq *tdq; 897ae7a6b38SJeff Roberson int cpu; 898356500a3SJeff Roberson 8997fcf154aSJeff Roberson TDQ_LOCK_ASSERT(from, MA_OWNED); 9007fcf154aSJeff Roberson TDQ_LOCK_ASSERT(to, MA_OWNED); 9017fcf154aSJeff Roberson 902ad1e7d28SJulian Elischer tdq = from; 903ae7a6b38SJeff Roberson cpu = TDQ_ID(to); 9049727e637SJeff Roberson td = tdq_steal(tdq, cpu); 9059727e637SJeff Roberson if (td == NULL) 90662fa74d9SJeff Roberson return (0); 9079727e637SJeff Roberson ts = td->td_sched; 908ae7a6b38SJeff Roberson /* 909ae7a6b38SJeff Roberson * Although the run queue is locked the thread may be blocked. Lock 9107fcf154aSJeff Roberson * it to clear this and acquire the run-queue lock. 911ae7a6b38SJeff Roberson */ 912ae7a6b38SJeff Roberson thread_lock(td); 9137fcf154aSJeff Roberson /* Drop recursive lock on from acquired via thread_lock(). */ 914ae7a6b38SJeff Roberson TDQ_UNLOCK(from); 915ae7a6b38SJeff Roberson sched_rem(td); 9167b8bfa0dSJeff Roberson ts->ts_cpu = cpu; 917ae7a6b38SJeff Roberson td->td_lock = TDQ_LOCKPTR(to); 918ae7a6b38SJeff Roberson tdq_add(to, td, SRQ_YIELDING); 91962fa74d9SJeff Roberson return (1); 920356500a3SJeff Roberson } 92122bf7d9aSJeff Roberson 922ae7a6b38SJeff Roberson /* 923ae7a6b38SJeff Roberson * This tdq has idled. Try to steal a thread from another cpu and switch 924ae7a6b38SJeff Roberson * to it. 925ae7a6b38SJeff Roberson */ 92680f86c9fSJeff Roberson static int 927ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq) 92822bf7d9aSJeff Roberson { 92962fa74d9SJeff Roberson struct cpu_group *cg; 930ad1e7d28SJulian Elischer struct tdq *steal; 931c76ee827SJeff Roberson cpuset_t mask; 93262fa74d9SJeff Roberson int thresh; 933ae7a6b38SJeff Roberson int cpu; 93480f86c9fSJeff Roberson 93588f530ccSJeff Roberson if (smp_started == 0 || steal_idle == 0) 93688f530ccSJeff Roberson return (1); 937c76ee827SJeff Roberson CPU_FILL(&mask); 938c76ee827SJeff Roberson CPU_CLR(PCPU_GET(cpuid), &mask); 93962fa74d9SJeff Roberson /* We don't want to be preempted while we're iterating. */ 940ae7a6b38SJeff Roberson spinlock_enter(); 94162fa74d9SJeff Roberson for (cg = tdq->tdq_cg; cg != NULL; ) { 9427b55ab05SJeff Roberson if ((cg->cg_flags & CG_FLAG_THREAD) == 0) 94362fa74d9SJeff Roberson thresh = steal_thresh; 94462fa74d9SJeff Roberson else 94562fa74d9SJeff Roberson thresh = 1; 94662fa74d9SJeff Roberson cpu = sched_highest(cg, mask, thresh); 94762fa74d9SJeff Roberson if (cpu == -1) { 94862fa74d9SJeff Roberson cg = cg->cg_parent; 94980f86c9fSJeff Roberson continue; 9507b8bfa0dSJeff Roberson } 9517b8bfa0dSJeff Roberson steal = TDQ_CPU(cpu); 952c76ee827SJeff Roberson CPU_CLR(cpu, &mask); 9537fcf154aSJeff Roberson tdq_lock_pair(tdq, steal); 95462fa74d9SJeff Roberson if (steal->tdq_load < thresh || steal->tdq_transferable == 0) { 9557fcf154aSJeff Roberson tdq_unlock_pair(tdq, steal); 95662fa74d9SJeff Roberson continue; 95762fa74d9SJeff Roberson } 95862fa74d9SJeff Roberson /* 95962fa74d9SJeff Roberson * If a thread was added while interrupts were disabled don't 96062fa74d9SJeff Roberson * steal one here. If we fail to acquire one due to affinity 96162fa74d9SJeff Roberson * restrictions loop again with this cpu removed from the 96262fa74d9SJeff Roberson * set. 96362fa74d9SJeff Roberson */ 96462fa74d9SJeff Roberson if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) { 96562fa74d9SJeff Roberson tdq_unlock_pair(tdq, steal); 96662fa74d9SJeff Roberson continue; 96780f86c9fSJeff Roberson } 968ae7a6b38SJeff Roberson spinlock_exit(); 969ae7a6b38SJeff Roberson TDQ_UNLOCK(steal); 9708df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IDLE, NULL); 971ae7a6b38SJeff Roberson thread_unlock(curthread); 9727b8bfa0dSJeff Roberson 9737b8bfa0dSJeff Roberson return (0); 97422bf7d9aSJeff Roberson } 97562fa74d9SJeff Roberson spinlock_exit(); 97662fa74d9SJeff Roberson return (1); 97762fa74d9SJeff Roberson } 97822bf7d9aSJeff Roberson 979ae7a6b38SJeff Roberson /* 980ae7a6b38SJeff Roberson * Notify a remote cpu of new work. Sends an IPI if criteria are met. 981ae7a6b38SJeff Roberson */ 98222bf7d9aSJeff Roberson static void 9839727e637SJeff Roberson tdq_notify(struct tdq *tdq, struct thread *td) 98422bf7d9aSJeff Roberson { 98502f0ff6dSJohn Baldwin struct thread *ctd; 986fc3a97dcSJeff Roberson int pri; 9877b8bfa0dSJeff Roberson int cpu; 98822bf7d9aSJeff Roberson 989ff256d9cSJeff Roberson if (tdq->tdq_ipipending) 990ff256d9cSJeff Roberson return; 9919727e637SJeff Roberson cpu = td->td_sched->ts_cpu; 9929727e637SJeff Roberson pri = td->td_priority; 99302f0ff6dSJohn Baldwin ctd = pcpu_find(cpu)->pc_curthread; 99402f0ff6dSJohn Baldwin if (!sched_shouldpreempt(pri, ctd->td_priority, 1)) 9956b2f763fSJeff Roberson return; 99602f0ff6dSJohn Baldwin if (TD_IS_IDLETHREAD(ctd)) { 9971690c6c1SJeff Roberson /* 9986c47aaaeSJeff Roberson * If the MD code has an idle wakeup routine try that before 9996c47aaaeSJeff Roberson * falling back to IPI. 10006c47aaaeSJeff Roberson */ 10019f9ad565SAlexander Motin if (!tdq->tdq_cpu_idle || cpu_idle_wakeup(cpu)) 10026c47aaaeSJeff Roberson return; 10031690c6c1SJeff Roberson } 1004ff256d9cSJeff Roberson tdq->tdq_ipipending = 1; 1005d9d8d144SJohn Baldwin ipi_cpu(cpu, IPI_PREEMPT); 100622bf7d9aSJeff Roberson } 100722bf7d9aSJeff Roberson 1008ae7a6b38SJeff Roberson /* 1009ae7a6b38SJeff Roberson * Steals load from a timeshare queue. Honors the rotating queue head 1010ae7a6b38SJeff Roberson * index. 1011ae7a6b38SJeff Roberson */ 10129727e637SJeff Roberson static struct thread * 101362fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start) 1014ae7a6b38SJeff Roberson { 1015ae7a6b38SJeff Roberson struct rqbits *rqb; 1016ae7a6b38SJeff Roberson struct rqhead *rqh; 1017*36acfc65SAlexander Motin struct thread *td, *first; 1018ae7a6b38SJeff Roberson int bit; 1019ae7a6b38SJeff Roberson int pri; 1020ae7a6b38SJeff Roberson int i; 1021ae7a6b38SJeff Roberson 1022ae7a6b38SJeff Roberson rqb = &rq->rq_status; 1023ae7a6b38SJeff Roberson bit = start & (RQB_BPW -1); 1024ae7a6b38SJeff Roberson pri = 0; 1025*36acfc65SAlexander Motin first = NULL; 1026ae7a6b38SJeff Roberson again: 1027ae7a6b38SJeff Roberson for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 1028ae7a6b38SJeff Roberson if (rqb->rqb_bits[i] == 0) 1029ae7a6b38SJeff Roberson continue; 1030ae7a6b38SJeff Roberson if (bit != 0) { 1031ae7a6b38SJeff Roberson for (pri = bit; pri < RQB_BPW; pri++) 1032ae7a6b38SJeff Roberson if (rqb->rqb_bits[i] & (1ul << pri)) 1033ae7a6b38SJeff Roberson break; 1034ae7a6b38SJeff Roberson if (pri >= RQB_BPW) 1035ae7a6b38SJeff Roberson continue; 1036ae7a6b38SJeff Roberson } else 1037ae7a6b38SJeff Roberson pri = RQB_FFS(rqb->rqb_bits[i]); 1038ae7a6b38SJeff Roberson pri += (i << RQB_L2BPW); 1039ae7a6b38SJeff Roberson rqh = &rq->rq_queues[pri]; 10409727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 10419727e637SJeff Roberson if (first && THREAD_CAN_MIGRATE(td) && 10429727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 10439727e637SJeff Roberson return (td); 1044*36acfc65SAlexander Motin first = td; 1045ae7a6b38SJeff Roberson } 1046ae7a6b38SJeff Roberson } 1047ae7a6b38SJeff Roberson if (start != 0) { 1048ae7a6b38SJeff Roberson start = 0; 1049ae7a6b38SJeff Roberson goto again; 1050ae7a6b38SJeff Roberson } 1051ae7a6b38SJeff Roberson 1052*36acfc65SAlexander Motin if (first && THREAD_CAN_MIGRATE(first) && 1053*36acfc65SAlexander Motin THREAD_CAN_SCHED(first, cpu)) 1054*36acfc65SAlexander Motin return (first); 1055ae7a6b38SJeff Roberson return (NULL); 1056ae7a6b38SJeff Roberson } 1057ae7a6b38SJeff Roberson 1058ae7a6b38SJeff Roberson /* 1059ae7a6b38SJeff Roberson * Steals load from a standard linear queue. 1060ae7a6b38SJeff Roberson */ 10619727e637SJeff Roberson static struct thread * 106262fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu) 106322bf7d9aSJeff Roberson { 106422bf7d9aSJeff Roberson struct rqhead *rqh; 106522bf7d9aSJeff Roberson struct rqbits *rqb; 10669727e637SJeff Roberson struct thread *td; 106722bf7d9aSJeff Roberson int word; 106822bf7d9aSJeff Roberson int bit; 106922bf7d9aSJeff Roberson 107022bf7d9aSJeff Roberson rqb = &rq->rq_status; 107122bf7d9aSJeff Roberson for (word = 0; word < RQB_LEN; word++) { 107222bf7d9aSJeff Roberson if (rqb->rqb_bits[word] == 0) 107322bf7d9aSJeff Roberson continue; 107422bf7d9aSJeff Roberson for (bit = 0; bit < RQB_BPW; bit++) { 1075a2640c9bSPeter Wemm if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 107622bf7d9aSJeff Roberson continue; 107722bf7d9aSJeff Roberson rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 10789727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) 10799727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td) && 10809727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 10819727e637SJeff Roberson return (td); 108222bf7d9aSJeff Roberson } 108322bf7d9aSJeff Roberson } 108422bf7d9aSJeff Roberson return (NULL); 108522bf7d9aSJeff Roberson } 108622bf7d9aSJeff Roberson 1087ae7a6b38SJeff Roberson /* 1088ae7a6b38SJeff Roberson * Attempt to steal a thread in priority order from a thread queue. 1089ae7a6b38SJeff Roberson */ 10909727e637SJeff Roberson static struct thread * 109162fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu) 109222bf7d9aSJeff Roberson { 10939727e637SJeff Roberson struct thread *td; 109422bf7d9aSJeff Roberson 1095ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 10969727e637SJeff Roberson if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 10979727e637SJeff Roberson return (td); 10989727e637SJeff Roberson if ((td = runq_steal_from(&tdq->tdq_timeshare, 10999727e637SJeff Roberson cpu, tdq->tdq_ridx)) != NULL) 11009727e637SJeff Roberson return (td); 110162fa74d9SJeff Roberson return (runq_steal(&tdq->tdq_idle, cpu)); 110222bf7d9aSJeff Roberson } 110380f86c9fSJeff Roberson 1104ae7a6b38SJeff Roberson /* 1105ae7a6b38SJeff Roberson * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 11067fcf154aSJeff Roberson * current lock and returns with the assigned queue locked. 1107ae7a6b38SJeff Roberson */ 1108ae7a6b38SJeff Roberson static inline struct tdq * 11099727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags) 111080f86c9fSJeff Roberson { 11119727e637SJeff Roberson 1112ae7a6b38SJeff Roberson struct tdq *tdq; 111380f86c9fSJeff Roberson 11149727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1115ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpu); 11169727e637SJeff Roberson td->td_sched->ts_cpu = cpu; 11179727e637SJeff Roberson /* 11189727e637SJeff Roberson * If the lock matches just return the queue. 11199727e637SJeff Roberson */ 1120ae7a6b38SJeff Roberson if (td->td_lock == TDQ_LOCKPTR(tdq)) 1121ae7a6b38SJeff Roberson return (tdq); 1122ae7a6b38SJeff Roberson #ifdef notyet 112380f86c9fSJeff Roberson /* 1124a5423ea3SJeff Roberson * If the thread isn't running its lockptr is a 1125ae7a6b38SJeff Roberson * turnstile or a sleepqueue. We can just lock_set without 1126ae7a6b38SJeff Roberson * blocking. 1127670c524fSJeff Roberson */ 1128ae7a6b38SJeff Roberson if (TD_CAN_RUN(td)) { 1129ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1130ae7a6b38SJeff Roberson thread_lock_set(td, TDQ_LOCKPTR(tdq)); 1131ae7a6b38SJeff Roberson return (tdq); 1132ae7a6b38SJeff Roberson } 1133ae7a6b38SJeff Roberson #endif 113480f86c9fSJeff Roberson /* 1135ae7a6b38SJeff Roberson * The hard case, migration, we need to block the thread first to 1136ae7a6b38SJeff Roberson * prevent order reversals with other cpus locks. 11377b8bfa0dSJeff Roberson */ 1138b0b9dee5SAttilio Rao spinlock_enter(); 1139ae7a6b38SJeff Roberson thread_lock_block(td); 1140ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1141ae7a6b38SJeff Roberson thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1142b0b9dee5SAttilio Rao spinlock_exit(); 1143ae7a6b38SJeff Roberson return (tdq); 114480f86c9fSJeff Roberson } 11452454aaf5SJeff Roberson 11468df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 11478df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 11488df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 11498df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 11508df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 11518df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 11528df78c41SJeff Roberson 1153ae7a6b38SJeff Roberson static int 11549727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags) 1155ae7a6b38SJeff Roberson { 1156*36acfc65SAlexander Motin struct cpu_group *cg, *ccg; 11579727e637SJeff Roberson struct td_sched *ts; 1158ae7a6b38SJeff Roberson struct tdq *tdq; 1159c76ee827SJeff Roberson cpuset_t mask; 1160*36acfc65SAlexander Motin int cpu, pri, self; 11617b8bfa0dSJeff Roberson 116262fa74d9SJeff Roberson self = PCPU_GET(cpuid); 11639727e637SJeff Roberson ts = td->td_sched; 11647b8bfa0dSJeff Roberson if (smp_started == 0) 11657b8bfa0dSJeff Roberson return (self); 116628994a58SJeff Roberson /* 116728994a58SJeff Roberson * Don't migrate a running thread from sched_switch(). 116828994a58SJeff Roberson */ 116962fa74d9SJeff Roberson if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 117062fa74d9SJeff Roberson return (ts->ts_cpu); 11717b8bfa0dSJeff Roberson /* 117262fa74d9SJeff Roberson * Prefer to run interrupt threads on the processors that generate 117362fa74d9SJeff Roberson * the interrupt. 11747b8bfa0dSJeff Roberson */ 1175*36acfc65SAlexander Motin pri = td->td_priority; 117662fa74d9SJeff Roberson if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 11778df78c41SJeff Roberson curthread->td_intr_nesting_level && ts->ts_cpu != self) { 11788df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_intrbind); 117962fa74d9SJeff Roberson ts->ts_cpu = self; 1180*36acfc65SAlexander Motin if (TDQ_CPU(self)->tdq_lowpri > pri) { 11818df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_affinity); 11827b8bfa0dSJeff Roberson return (ts->ts_cpu); 11837b8bfa0dSJeff Roberson } 11848df78c41SJeff Roberson } 11857b8bfa0dSJeff Roberson /* 1186*36acfc65SAlexander Motin * If the thread can run on the last cpu and the affinity has not 1187*36acfc65SAlexander Motin * expired or it is idle run it there. 11887b8bfa0dSJeff Roberson */ 1189*36acfc65SAlexander Motin tdq = TDQ_CPU(ts->ts_cpu); 1190*36acfc65SAlexander Motin cg = tdq->tdq_cg; 1191*36acfc65SAlexander Motin if (THREAD_CAN_SCHED(td, ts->ts_cpu) && 1192*36acfc65SAlexander Motin tdq->tdq_lowpri >= PRI_MIN_IDLE && 1193*36acfc65SAlexander Motin SCHED_AFFINITY(ts, CG_SHARE_L2)) { 1194*36acfc65SAlexander Motin if (cg->cg_flags & CG_FLAG_THREAD) { 1195*36acfc65SAlexander Motin CPUSET_FOREACH(cpu, cg->cg_mask) { 1196*36acfc65SAlexander Motin if (TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) 119762fa74d9SJeff Roberson break; 1198*36acfc65SAlexander Motin } 1199*36acfc65SAlexander Motin } else 1200*36acfc65SAlexander Motin cpu = INT_MAX; 1201*36acfc65SAlexander Motin if (cpu > mp_maxid) { 1202*36acfc65SAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 1203*36acfc65SAlexander Motin return (ts->ts_cpu); 1204*36acfc65SAlexander Motin } 1205*36acfc65SAlexander Motin } 1206*36acfc65SAlexander Motin /* 1207*36acfc65SAlexander Motin * Search for the last level cache CPU group in the tree. 1208*36acfc65SAlexander Motin * Skip caches with expired affinity time and SMT groups. 1209*36acfc65SAlexander Motin * Affinity to higher level caches will be handled less aggressively. 1210*36acfc65SAlexander Motin */ 1211*36acfc65SAlexander Motin for (ccg = NULL; cg != NULL; cg = cg->cg_parent) { 1212*36acfc65SAlexander Motin if (cg->cg_flags & CG_FLAG_THREAD) 1213*36acfc65SAlexander Motin continue; 1214*36acfc65SAlexander Motin if (!SCHED_AFFINITY(ts, cg->cg_level)) 1215*36acfc65SAlexander Motin continue; 1216*36acfc65SAlexander Motin ccg = cg; 1217*36acfc65SAlexander Motin } 1218*36acfc65SAlexander Motin if (ccg != NULL) 1219*36acfc65SAlexander Motin cg = ccg; 122062fa74d9SJeff Roberson cpu = -1; 1221*36acfc65SAlexander Motin /* Search the group for the less loaded idle CPU we can run now. */ 1222c76ee827SJeff Roberson mask = td->td_cpuset->cs_mask; 1223*36acfc65SAlexander Motin if (cg != NULL && cg != cpu_top && 1224*36acfc65SAlexander Motin CPU_CMP(&cg->cg_mask, &cpu_top->cg_mask) != 0) 1225*36acfc65SAlexander Motin cpu = sched_lowest(cg, mask, max(pri, PRI_MAX_TIMESHARE), 1226*36acfc65SAlexander Motin INT_MAX, ts->ts_cpu); 1227*36acfc65SAlexander Motin /* Search globally for the less loaded CPU we can run now. */ 122862fa74d9SJeff Roberson if (cpu == -1) 1229*36acfc65SAlexander Motin cpu = sched_lowest(cpu_top, mask, pri, INT_MAX, ts->ts_cpu); 1230*36acfc65SAlexander Motin /* Search globally for the less loaded CPU. */ 1231*36acfc65SAlexander Motin if (cpu == -1) 1232*36acfc65SAlexander Motin cpu = sched_lowest(cpu_top, mask, -1, INT_MAX, ts->ts_cpu); 123362fa74d9SJeff Roberson /* 123462fa74d9SJeff Roberson * Compare the lowest loaded cpu to current cpu. 123562fa74d9SJeff Roberson */ 1236ff256d9cSJeff Roberson if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri && 1237*36acfc65SAlexander Motin TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE && 1238*36acfc65SAlexander Motin TDQ_CPU(self)->tdq_load <= TDQ_CPU(cpu)->tdq_load + 1) { 12398df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_local); 124062fa74d9SJeff Roberson cpu = self; 12418df78c41SJeff Roberson } else 12428df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_lowest); 12438df78c41SJeff Roberson if (cpu != ts->ts_cpu) 12448df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_migration); 1245ff256d9cSJeff Roberson KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu.")); 1246ae7a6b38SJeff Roberson return (cpu); 124780f86c9fSJeff Roberson } 124862fa74d9SJeff Roberson #endif 124922bf7d9aSJeff Roberson 125022bf7d9aSJeff Roberson /* 125122bf7d9aSJeff Roberson * Pick the highest priority task we have and return it. 12520c0a98b2SJeff Roberson */ 12539727e637SJeff Roberson static struct thread * 1254ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq) 12555d7ef00cSJeff Roberson { 12569727e637SJeff Roberson struct thread *td; 12575d7ef00cSJeff Roberson 1258ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 12599727e637SJeff Roberson td = runq_choose(&tdq->tdq_realtime); 12609727e637SJeff Roberson if (td != NULL) 12619727e637SJeff Roberson return (td); 12629727e637SJeff Roberson td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 12639727e637SJeff Roberson if (td != NULL) { 126412d56c0fSJohn Baldwin KASSERT(td->td_priority >= PRI_MIN_BATCH, 1265e7d50326SJeff Roberson ("tdq_choose: Invalid priority on timeshare queue %d", 12669727e637SJeff Roberson td->td_priority)); 12679727e637SJeff Roberson return (td); 126815dc847eSJeff Roberson } 12699727e637SJeff Roberson td = runq_choose(&tdq->tdq_idle); 12709727e637SJeff Roberson if (td != NULL) { 12719727e637SJeff Roberson KASSERT(td->td_priority >= PRI_MIN_IDLE, 1272e7d50326SJeff Roberson ("tdq_choose: Invalid priority on idle queue %d", 12739727e637SJeff Roberson td->td_priority)); 12749727e637SJeff Roberson return (td); 1275e7d50326SJeff Roberson } 1276e7d50326SJeff Roberson 1277e7d50326SJeff Roberson return (NULL); 1278245f3abfSJeff Roberson } 12790a016a05SJeff Roberson 1280ae7a6b38SJeff Roberson /* 1281ae7a6b38SJeff Roberson * Initialize a thread queue. 1282ae7a6b38SJeff Roberson */ 12830a016a05SJeff Roberson static void 1284ad1e7d28SJulian Elischer tdq_setup(struct tdq *tdq) 12850a016a05SJeff Roberson { 1286ae7a6b38SJeff Roberson 1287c47f202bSJeff Roberson if (bootverbose) 1288c47f202bSJeff Roberson printf("ULE: setup cpu %d\n", TDQ_ID(tdq)); 1289e7d50326SJeff Roberson runq_init(&tdq->tdq_realtime); 1290e7d50326SJeff Roberson runq_init(&tdq->tdq_timeshare); 1291d2ad694cSJeff Roberson runq_init(&tdq->tdq_idle); 129262fa74d9SJeff Roberson snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 129362fa74d9SJeff Roberson "sched lock %d", (int)TDQ_ID(tdq)); 129462fa74d9SJeff Roberson mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", 129562fa74d9SJeff Roberson MTX_SPIN | MTX_RECURSE); 12968f51ad55SJeff Roberson #ifdef KTR 12978f51ad55SJeff Roberson snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 12988f51ad55SJeff Roberson "CPU %d load", (int)TDQ_ID(tdq)); 12998f51ad55SJeff Roberson #endif 13000a016a05SJeff Roberson } 13010a016a05SJeff Roberson 1302c47f202bSJeff Roberson #ifdef SMP 1303c47f202bSJeff Roberson static void 1304c47f202bSJeff Roberson sched_setup_smp(void) 1305c47f202bSJeff Roberson { 1306c47f202bSJeff Roberson struct tdq *tdq; 1307c47f202bSJeff Roberson int i; 1308c47f202bSJeff Roberson 130962fa74d9SJeff Roberson cpu_top = smp_topo(); 13103aa6d94eSJohn Baldwin CPU_FOREACH(i) { 131162fa74d9SJeff Roberson tdq = TDQ_CPU(i); 1312c47f202bSJeff Roberson tdq_setup(tdq); 131362fa74d9SJeff Roberson tdq->tdq_cg = smp_topo_find(cpu_top, i); 131462fa74d9SJeff Roberson if (tdq->tdq_cg == NULL) 131562fa74d9SJeff Roberson panic("Can't find cpu group for %d\n", i); 1316c47f202bSJeff Roberson } 131762fa74d9SJeff Roberson balance_tdq = TDQ_SELF(); 131862fa74d9SJeff Roberson sched_balance(); 1319c47f202bSJeff Roberson } 1320c47f202bSJeff Roberson #endif 1321c47f202bSJeff Roberson 1322ae7a6b38SJeff Roberson /* 1323ae7a6b38SJeff Roberson * Setup the thread queues and initialize the topology based on MD 1324ae7a6b38SJeff Roberson * information. 1325ae7a6b38SJeff Roberson */ 132635e6168fSJeff Roberson static void 132735e6168fSJeff Roberson sched_setup(void *dummy) 132835e6168fSJeff Roberson { 1329ae7a6b38SJeff Roberson struct tdq *tdq; 1330c47f202bSJeff Roberson 1331c47f202bSJeff Roberson tdq = TDQ_SELF(); 13320ec896fdSJeff Roberson #ifdef SMP 1333c47f202bSJeff Roberson sched_setup_smp(); 1334749d01b0SJeff Roberson #else 1335c47f202bSJeff Roberson tdq_setup(tdq); 1336356500a3SJeff Roberson #endif 1337ae7a6b38SJeff Roberson /* 1338ae7a6b38SJeff Roberson * To avoid divide-by-zero, we set realstathz a dummy value 1339ae7a6b38SJeff Roberson * in case which sched_clock() called before sched_initticks(). 1340ae7a6b38SJeff Roberson */ 1341ae7a6b38SJeff Roberson realstathz = hz; 1342ae7a6b38SJeff Roberson sched_slice = (realstathz/10); /* ~100ms */ 1343ae7a6b38SJeff Roberson tickincr = 1 << SCHED_TICK_SHIFT; 1344ae7a6b38SJeff Roberson 1345ae7a6b38SJeff Roberson /* Add thread0's load since it's running. */ 1346ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1347c47f202bSJeff Roberson thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF()); 13489727e637SJeff Roberson tdq_load_add(tdq, &thread0); 134962fa74d9SJeff Roberson tdq->tdq_lowpri = thread0.td_priority; 1350ae7a6b38SJeff Roberson TDQ_UNLOCK(tdq); 135135e6168fSJeff Roberson } 135235e6168fSJeff Roberson 1353ae7a6b38SJeff Roberson /* 1354ae7a6b38SJeff Roberson * This routine determines the tickincr after stathz and hz are setup. 1355ae7a6b38SJeff Roberson */ 1356a1d4fe69SDavid Xu /* ARGSUSED */ 1357a1d4fe69SDavid Xu static void 1358a1d4fe69SDavid Xu sched_initticks(void *dummy) 1359a1d4fe69SDavid Xu { 1360ae7a6b38SJeff Roberson int incr; 1361ae7a6b38SJeff Roberson 1362a1d4fe69SDavid Xu realstathz = stathz ? stathz : hz; 136314618990SJeff Roberson sched_slice = (realstathz/10); /* ~100ms */ 1364a1d4fe69SDavid Xu 1365a1d4fe69SDavid Xu /* 1366e7d50326SJeff Roberson * tickincr is shifted out by 10 to avoid rounding errors due to 13673f872f85SJeff Roberson * hz not being evenly divisible by stathz on all platforms. 1368e7d50326SJeff Roberson */ 1369ae7a6b38SJeff Roberson incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1370e7d50326SJeff Roberson /* 1371e7d50326SJeff Roberson * This does not work for values of stathz that are more than 1372e7d50326SJeff Roberson * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1373a1d4fe69SDavid Xu */ 1374ae7a6b38SJeff Roberson if (incr == 0) 1375ae7a6b38SJeff Roberson incr = 1; 1376ae7a6b38SJeff Roberson tickincr = incr; 13777b8bfa0dSJeff Roberson #ifdef SMP 13789862717aSJeff Roberson /* 13797fcf154aSJeff Roberson * Set the default balance interval now that we know 13807fcf154aSJeff Roberson * what realstathz is. 13817fcf154aSJeff Roberson */ 13827fcf154aSJeff Roberson balance_interval = realstathz; 13837fcf154aSJeff Roberson /* 138453a6c8b3SJeff Roberson * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4. 138553a6c8b3SJeff Roberson * This prevents excess thrashing on large machines and excess idle 138653a6c8b3SJeff Roberson * on smaller machines. 13879862717aSJeff Roberson */ 138853a6c8b3SJeff Roberson steal_thresh = min(fls(mp_ncpus) - 1, 3); 13897b8bfa0dSJeff Roberson affinity = SCHED_AFFINITY_DEFAULT; 13907b8bfa0dSJeff Roberson #endif 1391a1d4fe69SDavid Xu } 1392a1d4fe69SDavid Xu 1393a1d4fe69SDavid Xu 139435e6168fSJeff Roberson /* 1395ae7a6b38SJeff Roberson * This is the core of the interactivity algorithm. Determines a score based 1396ae7a6b38SJeff Roberson * on past behavior. It is the ratio of sleep time to run time scaled to 1397ae7a6b38SJeff Roberson * a [0, 100] integer. This is the voluntary sleep time of a process, which 1398ae7a6b38SJeff Roberson * differs from the cpu usage because it does not account for time spent 1399ae7a6b38SJeff Roberson * waiting on a run-queue. Would be prettier if we had floating point. 1400ae7a6b38SJeff Roberson */ 1401ae7a6b38SJeff Roberson static int 1402ae7a6b38SJeff Roberson sched_interact_score(struct thread *td) 1403ae7a6b38SJeff Roberson { 1404ae7a6b38SJeff Roberson struct td_sched *ts; 1405ae7a6b38SJeff Roberson int div; 1406ae7a6b38SJeff Roberson 1407ae7a6b38SJeff Roberson ts = td->td_sched; 1408ae7a6b38SJeff Roberson /* 1409ae7a6b38SJeff Roberson * The score is only needed if this is likely to be an interactive 1410ae7a6b38SJeff Roberson * task. Don't go through the expense of computing it if there's 1411ae7a6b38SJeff Roberson * no chance. 1412ae7a6b38SJeff Roberson */ 1413ae7a6b38SJeff Roberson if (sched_interact <= SCHED_INTERACT_HALF && 1414ae7a6b38SJeff Roberson ts->ts_runtime >= ts->ts_slptime) 1415ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1416ae7a6b38SJeff Roberson 1417ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1418ae7a6b38SJeff Roberson div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1419ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF + 1420ae7a6b38SJeff Roberson (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1421ae7a6b38SJeff Roberson } 1422ae7a6b38SJeff Roberson if (ts->ts_slptime > ts->ts_runtime) { 1423ae7a6b38SJeff Roberson div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1424ae7a6b38SJeff Roberson return (ts->ts_runtime / div); 1425ae7a6b38SJeff Roberson } 1426ae7a6b38SJeff Roberson /* runtime == slptime */ 1427ae7a6b38SJeff Roberson if (ts->ts_runtime) 1428ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1429ae7a6b38SJeff Roberson 1430ae7a6b38SJeff Roberson /* 1431ae7a6b38SJeff Roberson * This can happen if slptime and runtime are 0. 1432ae7a6b38SJeff Roberson */ 1433ae7a6b38SJeff Roberson return (0); 1434ae7a6b38SJeff Roberson 1435ae7a6b38SJeff Roberson } 1436ae7a6b38SJeff Roberson 1437ae7a6b38SJeff Roberson /* 143835e6168fSJeff Roberson * Scale the scheduling priority according to the "interactivity" of this 143935e6168fSJeff Roberson * process. 144035e6168fSJeff Roberson */ 144115dc847eSJeff Roberson static void 14428460a577SJohn Birrell sched_priority(struct thread *td) 144335e6168fSJeff Roberson { 1444e7d50326SJeff Roberson int score; 144535e6168fSJeff Roberson int pri; 144635e6168fSJeff Roberson 1447c9a8cba4SJohn Baldwin if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 144815dc847eSJeff Roberson return; 1449e7d50326SJeff Roberson /* 1450e7d50326SJeff Roberson * If the score is interactive we place the thread in the realtime 1451e7d50326SJeff Roberson * queue with a priority that is less than kernel and interrupt 1452e7d50326SJeff Roberson * priorities. These threads are not subject to nice restrictions. 1453e7d50326SJeff Roberson * 1454ae7a6b38SJeff Roberson * Scores greater than this are placed on the normal timeshare queue 1455e7d50326SJeff Roberson * where the priority is partially decided by the most recent cpu 1456e7d50326SJeff Roberson * utilization and the rest is decided by nice value. 1457a5423ea3SJeff Roberson * 1458a5423ea3SJeff Roberson * The nice value of the process has a linear effect on the calculated 1459a5423ea3SJeff Roberson * score. Negative nice values make it easier for a thread to be 1460a5423ea3SJeff Roberson * considered interactive. 1461e7d50326SJeff Roberson */ 1462a0f15352SJohn Baldwin score = imax(0, sched_interact_score(td) + td->td_proc->p_nice); 1463e7d50326SJeff Roberson if (score < sched_interact) { 146412d56c0fSJohn Baldwin pri = PRI_MIN_INTERACT; 146512d56c0fSJohn Baldwin pri += ((PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) / 146678920008SJohn Baldwin sched_interact) * score; 146712d56c0fSJohn Baldwin KASSERT(pri >= PRI_MIN_INTERACT && pri <= PRI_MAX_INTERACT, 14689a93305aSJeff Roberson ("sched_priority: invalid interactive priority %d score %d", 14699a93305aSJeff Roberson pri, score)); 1470e7d50326SJeff Roberson } else { 1471e7d50326SJeff Roberson pri = SCHED_PRI_MIN; 1472e7d50326SJeff Roberson if (td->td_sched->ts_ticks) 14730c0d27d5SJohn Baldwin pri += min(SCHED_PRI_TICKS(td->td_sched), 14740c0d27d5SJohn Baldwin SCHED_PRI_RANGE); 1475e7d50326SJeff Roberson pri += SCHED_PRI_NICE(td->td_proc->p_nice); 147612d56c0fSJohn Baldwin KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH, 1477ae7a6b38SJeff Roberson ("sched_priority: invalid priority %d: nice %d, " 1478ae7a6b38SJeff Roberson "ticks %d ftick %d ltick %d tick pri %d", 1479ae7a6b38SJeff Roberson pri, td->td_proc->p_nice, td->td_sched->ts_ticks, 1480ae7a6b38SJeff Roberson td->td_sched->ts_ftick, td->td_sched->ts_ltick, 1481ae7a6b38SJeff Roberson SCHED_PRI_TICKS(td->td_sched))); 1482e7d50326SJeff Roberson } 14838460a577SJohn Birrell sched_user_prio(td, pri); 148435e6168fSJeff Roberson 148515dc847eSJeff Roberson return; 148635e6168fSJeff Roberson } 148735e6168fSJeff Roberson 148835e6168fSJeff Roberson /* 1489d322132cSJeff Roberson * This routine enforces a maximum limit on the amount of scheduling history 1490ae7a6b38SJeff Roberson * kept. It is called after either the slptime or runtime is adjusted. This 1491ae7a6b38SJeff Roberson * function is ugly due to integer math. 1492d322132cSJeff Roberson */ 14934b60e324SJeff Roberson static void 14948460a577SJohn Birrell sched_interact_update(struct thread *td) 14954b60e324SJeff Roberson { 1496155b6ca1SJeff Roberson struct td_sched *ts; 14979a93305aSJeff Roberson u_int sum; 14983f741ca1SJeff Roberson 1499155b6ca1SJeff Roberson ts = td->td_sched; 1500ae7a6b38SJeff Roberson sum = ts->ts_runtime + ts->ts_slptime; 1501d322132cSJeff Roberson if (sum < SCHED_SLP_RUN_MAX) 1502d322132cSJeff Roberson return; 1503d322132cSJeff Roberson /* 1504155b6ca1SJeff Roberson * This only happens from two places: 1505155b6ca1SJeff Roberson * 1) We have added an unusual amount of run time from fork_exit. 1506155b6ca1SJeff Roberson * 2) We have added an unusual amount of sleep time from sched_sleep(). 1507155b6ca1SJeff Roberson */ 1508155b6ca1SJeff Roberson if (sum > SCHED_SLP_RUN_MAX * 2) { 1509ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1510ae7a6b38SJeff Roberson ts->ts_runtime = SCHED_SLP_RUN_MAX; 1511ae7a6b38SJeff Roberson ts->ts_slptime = 1; 1512155b6ca1SJeff Roberson } else { 1513ae7a6b38SJeff Roberson ts->ts_slptime = SCHED_SLP_RUN_MAX; 1514ae7a6b38SJeff Roberson ts->ts_runtime = 1; 1515155b6ca1SJeff Roberson } 1516155b6ca1SJeff Roberson return; 1517155b6ca1SJeff Roberson } 1518155b6ca1SJeff Roberson /* 1519d322132cSJeff Roberson * If we have exceeded by more than 1/5th then the algorithm below 1520d322132cSJeff Roberson * will not bring us back into range. Dividing by two here forces 15212454aaf5SJeff Roberson * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1522d322132cSJeff Roberson */ 152337a35e4aSJeff Roberson if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1524ae7a6b38SJeff Roberson ts->ts_runtime /= 2; 1525ae7a6b38SJeff Roberson ts->ts_slptime /= 2; 1526d322132cSJeff Roberson return; 1527d322132cSJeff Roberson } 1528ae7a6b38SJeff Roberson ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1529ae7a6b38SJeff Roberson ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1530d322132cSJeff Roberson } 1531d322132cSJeff Roberson 1532ae7a6b38SJeff Roberson /* 1533ae7a6b38SJeff Roberson * Scale back the interactivity history when a child thread is created. The 1534ae7a6b38SJeff Roberson * history is inherited from the parent but the thread may behave totally 1535ae7a6b38SJeff Roberson * differently. For example, a shell spawning a compiler process. We want 1536ae7a6b38SJeff Roberson * to learn that the compiler is behaving badly very quickly. 1537ae7a6b38SJeff Roberson */ 1538d322132cSJeff Roberson static void 15398460a577SJohn Birrell sched_interact_fork(struct thread *td) 1540d322132cSJeff Roberson { 1541d322132cSJeff Roberson int ratio; 1542d322132cSJeff Roberson int sum; 1543d322132cSJeff Roberson 1544ae7a6b38SJeff Roberson sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime; 1545d322132cSJeff Roberson if (sum > SCHED_SLP_RUN_FORK) { 1546d322132cSJeff Roberson ratio = sum / SCHED_SLP_RUN_FORK; 1547ae7a6b38SJeff Roberson td->td_sched->ts_runtime /= ratio; 1548ae7a6b38SJeff Roberson td->td_sched->ts_slptime /= ratio; 15494b60e324SJeff Roberson } 15504b60e324SJeff Roberson } 15514b60e324SJeff Roberson 155215dc847eSJeff Roberson /* 1553ae7a6b38SJeff Roberson * Called from proc0_init() to setup the scheduler fields. 1554ed062c8dSJulian Elischer */ 1555ed062c8dSJulian Elischer void 1556ed062c8dSJulian Elischer schedinit(void) 1557ed062c8dSJulian Elischer { 1558e7d50326SJeff Roberson 1559ed062c8dSJulian Elischer /* 1560ed062c8dSJulian Elischer * Set up the scheduler specific parts of proc0. 1561ed062c8dSJulian Elischer */ 1562ed062c8dSJulian Elischer proc0.p_sched = NULL; /* XXX */ 1563ad1e7d28SJulian Elischer thread0.td_sched = &td_sched0; 1564e7d50326SJeff Roberson td_sched0.ts_ltick = ticks; 15658ab80cf0SJeff Roberson td_sched0.ts_ftick = ticks; 156673daf66fSJeff Roberson td_sched0.ts_slice = sched_slice; 1567ed062c8dSJulian Elischer } 1568ed062c8dSJulian Elischer 1569ed062c8dSJulian Elischer /* 157015dc847eSJeff Roberson * This is only somewhat accurate since given many processes of the same 157115dc847eSJeff Roberson * priority they will switch when their slices run out, which will be 1572e7d50326SJeff Roberson * at most sched_slice stathz ticks. 157315dc847eSJeff Roberson */ 157435e6168fSJeff Roberson int 157535e6168fSJeff Roberson sched_rr_interval(void) 157635e6168fSJeff Roberson { 1577e7d50326SJeff Roberson 1578e7d50326SJeff Roberson /* Convert sched_slice to hz */ 1579e7d50326SJeff Roberson return (hz/(realstathz/sched_slice)); 158035e6168fSJeff Roberson } 158135e6168fSJeff Roberson 1582ae7a6b38SJeff Roberson /* 1583ae7a6b38SJeff Roberson * Update the percent cpu tracking information when it is requested or 1584ae7a6b38SJeff Roberson * the total history exceeds the maximum. We keep a sliding history of 1585ae7a6b38SJeff Roberson * tick counts that slowly decays. This is less precise than the 4BSD 1586ae7a6b38SJeff Roberson * mechanism since it happens with less regular and frequent events. 1587ae7a6b38SJeff Roberson */ 158822bf7d9aSJeff Roberson static void 1589ad1e7d28SJulian Elischer sched_pctcpu_update(struct td_sched *ts) 159035e6168fSJeff Roberson { 1591e7d50326SJeff Roberson 1592e7d50326SJeff Roberson if (ts->ts_ticks == 0) 1593e7d50326SJeff Roberson return; 15948ab80cf0SJeff Roberson if (ticks - (hz / 10) < ts->ts_ltick && 15958ab80cf0SJeff Roberson SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX) 15968ab80cf0SJeff Roberson return; 159735e6168fSJeff Roberson /* 159835e6168fSJeff Roberson * Adjust counters and watermark for pctcpu calc. 1599210491d3SJeff Roberson */ 1600e7d50326SJeff Roberson if (ts->ts_ltick > ticks - SCHED_TICK_TARG) 1601ad1e7d28SJulian Elischer ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) * 1602e7d50326SJeff Roberson SCHED_TICK_TARG; 1603e7d50326SJeff Roberson else 1604ad1e7d28SJulian Elischer ts->ts_ticks = 0; 1605ad1e7d28SJulian Elischer ts->ts_ltick = ticks; 1606e7d50326SJeff Roberson ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG; 160735e6168fSJeff Roberson } 160835e6168fSJeff Roberson 1609ae7a6b38SJeff Roberson /* 1610ae7a6b38SJeff Roberson * Adjust the priority of a thread. Move it to the appropriate run-queue 1611ae7a6b38SJeff Roberson * if necessary. This is the back-end for several priority related 1612ae7a6b38SJeff Roberson * functions. 1613ae7a6b38SJeff Roberson */ 1614e7d50326SJeff Roberson static void 1615f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio) 161635e6168fSJeff Roberson { 1617ad1e7d28SJulian Elischer struct td_sched *ts; 161873daf66fSJeff Roberson struct tdq *tdq; 161973daf66fSJeff Roberson int oldpri; 162035e6168fSJeff Roberson 16218f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 16228f51ad55SJeff Roberson "prio:%d", td->td_priority, "new prio:%d", prio, 16238f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(curthread)); 16248f51ad55SJeff Roberson if (td != curthread && prio > td->td_priority) { 16258f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 16268f51ad55SJeff Roberson "lend prio", "prio:%d", td->td_priority, "new prio:%d", 16278f51ad55SJeff Roberson prio, KTR_ATTR_LINKED, sched_tdname(td)); 16288f51ad55SJeff Roberson } 1629ad1e7d28SJulian Elischer ts = td->td_sched; 16307b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1631f5c157d9SJohn Baldwin if (td->td_priority == prio) 1632f5c157d9SJohn Baldwin return; 16333f741ca1SJeff Roberson /* 16343f741ca1SJeff Roberson * If the priority has been elevated due to priority 16353f741ca1SJeff Roberson * propagation, we may have to move ourselves to a new 1636e7d50326SJeff Roberson * queue. This could be optimized to not re-add in some 1637e7d50326SJeff Roberson * cases. 1638f2b74cbfSJeff Roberson */ 16396d55b3ecSJeff Roberson if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1640e7d50326SJeff Roberson sched_rem(td); 1641e7d50326SJeff Roberson td->td_priority = prio; 1642ae7a6b38SJeff Roberson sched_add(td, SRQ_BORROWING); 164373daf66fSJeff Roberson return; 164473daf66fSJeff Roberson } 16456d55b3ecSJeff Roberson /* 16466d55b3ecSJeff Roberson * If the thread is currently running we may have to adjust the lowpri 16476d55b3ecSJeff Roberson * information so other cpus are aware of our current priority. 16486d55b3ecSJeff Roberson */ 16496d55b3ecSJeff Roberson if (TD_IS_RUNNING(td)) { 1650ae7a6b38SJeff Roberson tdq = TDQ_CPU(ts->ts_cpu); 165162fa74d9SJeff Roberson oldpri = td->td_priority; 16523f741ca1SJeff Roberson td->td_priority = prio; 165362fa74d9SJeff Roberson if (prio < tdq->tdq_lowpri) 165462fa74d9SJeff Roberson tdq->tdq_lowpri = prio; 165562fa74d9SJeff Roberson else if (tdq->tdq_lowpri == oldpri) 165662fa74d9SJeff Roberson tdq_setlowpri(tdq, td); 16576d55b3ecSJeff Roberson return; 165873daf66fSJeff Roberson } 16596d55b3ecSJeff Roberson td->td_priority = prio; 1660ae7a6b38SJeff Roberson } 166135e6168fSJeff Roberson 1662f5c157d9SJohn Baldwin /* 1663f5c157d9SJohn Baldwin * Update a thread's priority when it is lent another thread's 1664f5c157d9SJohn Baldwin * priority. 1665f5c157d9SJohn Baldwin */ 1666f5c157d9SJohn Baldwin void 1667f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio) 1668f5c157d9SJohn Baldwin { 1669f5c157d9SJohn Baldwin 1670f5c157d9SJohn Baldwin td->td_flags |= TDF_BORROWING; 1671f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1672f5c157d9SJohn Baldwin } 1673f5c157d9SJohn Baldwin 1674f5c157d9SJohn Baldwin /* 1675f5c157d9SJohn Baldwin * Restore a thread's priority when priority propagation is 1676f5c157d9SJohn Baldwin * over. The prio argument is the minimum priority the thread 1677f5c157d9SJohn Baldwin * needs to have to satisfy other possible priority lending 1678f5c157d9SJohn Baldwin * requests. If the thread's regular priority is less 1679f5c157d9SJohn Baldwin * important than prio, the thread will keep a priority boost 1680f5c157d9SJohn Baldwin * of prio. 1681f5c157d9SJohn Baldwin */ 1682f5c157d9SJohn Baldwin void 1683f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio) 1684f5c157d9SJohn Baldwin { 1685f5c157d9SJohn Baldwin u_char base_pri; 1686f5c157d9SJohn Baldwin 1687f5c157d9SJohn Baldwin if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1688f5c157d9SJohn Baldwin td->td_base_pri <= PRI_MAX_TIMESHARE) 16898460a577SJohn Birrell base_pri = td->td_user_pri; 1690f5c157d9SJohn Baldwin else 1691f5c157d9SJohn Baldwin base_pri = td->td_base_pri; 1692f5c157d9SJohn Baldwin if (prio >= base_pri) { 1693f5c157d9SJohn Baldwin td->td_flags &= ~TDF_BORROWING; 1694f5c157d9SJohn Baldwin sched_thread_priority(td, base_pri); 1695f5c157d9SJohn Baldwin } else 1696f5c157d9SJohn Baldwin sched_lend_prio(td, prio); 1697f5c157d9SJohn Baldwin } 1698f5c157d9SJohn Baldwin 1699ae7a6b38SJeff Roberson /* 1700ae7a6b38SJeff Roberson * Standard entry for setting the priority to an absolute value. 1701ae7a6b38SJeff Roberson */ 1702f5c157d9SJohn Baldwin void 1703f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio) 1704f5c157d9SJohn Baldwin { 1705f5c157d9SJohn Baldwin u_char oldprio; 1706f5c157d9SJohn Baldwin 1707f5c157d9SJohn Baldwin /* First, update the base priority. */ 1708f5c157d9SJohn Baldwin td->td_base_pri = prio; 1709f5c157d9SJohn Baldwin 1710f5c157d9SJohn Baldwin /* 171150aaa791SJohn Baldwin * If the thread is borrowing another thread's priority, don't 1712f5c157d9SJohn Baldwin * ever lower the priority. 1713f5c157d9SJohn Baldwin */ 1714f5c157d9SJohn Baldwin if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1715f5c157d9SJohn Baldwin return; 1716f5c157d9SJohn Baldwin 1717f5c157d9SJohn Baldwin /* Change the real priority. */ 1718f5c157d9SJohn Baldwin oldprio = td->td_priority; 1719f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1720f5c157d9SJohn Baldwin 1721f5c157d9SJohn Baldwin /* 1722f5c157d9SJohn Baldwin * If the thread is on a turnstile, then let the turnstile update 1723f5c157d9SJohn Baldwin * its state. 1724f5c157d9SJohn Baldwin */ 1725f5c157d9SJohn Baldwin if (TD_ON_LOCK(td) && oldprio != prio) 1726f5c157d9SJohn Baldwin turnstile_adjust(td, oldprio); 1727f5c157d9SJohn Baldwin } 1728f5c157d9SJohn Baldwin 1729ae7a6b38SJeff Roberson /* 1730ae7a6b38SJeff Roberson * Set the base user priority, does not effect current running priority. 1731ae7a6b38SJeff Roberson */ 173235e6168fSJeff Roberson void 17338460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio) 17343db720fdSDavid Xu { 17353db720fdSDavid Xu 17368460a577SJohn Birrell td->td_base_user_pri = prio; 1737acbe332aSDavid Xu if (td->td_lend_user_pri <= prio) 1738fc6c30f6SJulian Elischer return; 17398460a577SJohn Birrell td->td_user_pri = prio; 17403db720fdSDavid Xu } 17413db720fdSDavid Xu 17423db720fdSDavid Xu void 17433db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio) 17443db720fdSDavid Xu { 17453db720fdSDavid Xu 1746435806d3SDavid Xu THREAD_LOCK_ASSERT(td, MA_OWNED); 1747acbe332aSDavid Xu td->td_lend_user_pri = prio; 1748c8e368a9SDavid Xu td->td_user_pri = min(prio, td->td_base_user_pri); 1749c8e368a9SDavid Xu if (td->td_priority > td->td_user_pri) 1750c8e368a9SDavid Xu sched_prio(td, td->td_user_pri); 1751c8e368a9SDavid Xu else if (td->td_priority != td->td_user_pri) 1752c8e368a9SDavid Xu td->td_flags |= TDF_NEEDRESCHED; 1753435806d3SDavid Xu } 17543db720fdSDavid Xu 1755ae7a6b38SJeff Roberson /* 1756c47f202bSJeff Roberson * Handle migration from sched_switch(). This happens only for 1757c47f202bSJeff Roberson * cpu binding. 1758c47f202bSJeff Roberson */ 1759c47f202bSJeff Roberson static struct mtx * 1760c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1761c47f202bSJeff Roberson { 1762c47f202bSJeff Roberson struct tdq *tdn; 1763c47f202bSJeff Roberson 1764c47f202bSJeff Roberson tdn = TDQ_CPU(td->td_sched->ts_cpu); 1765c47f202bSJeff Roberson #ifdef SMP 17669727e637SJeff Roberson tdq_load_rem(tdq, td); 1767c47f202bSJeff Roberson /* 1768c47f202bSJeff Roberson * Do the lock dance required to avoid LOR. We grab an extra 1769c47f202bSJeff Roberson * spinlock nesting to prevent preemption while we're 1770c47f202bSJeff Roberson * not holding either run-queue lock. 1771c47f202bSJeff Roberson */ 1772c47f202bSJeff Roberson spinlock_enter(); 1773b0b9dee5SAttilio Rao thread_lock_block(td); /* This releases the lock on tdq. */ 1774435068aaSAttilio Rao 1775435068aaSAttilio Rao /* 1776435068aaSAttilio Rao * Acquire both run-queue locks before placing the thread on the new 1777435068aaSAttilio Rao * run-queue to avoid deadlocks created by placing a thread with a 1778435068aaSAttilio Rao * blocked lock on the run-queue of a remote processor. The deadlock 1779435068aaSAttilio Rao * occurs when a third processor attempts to lock the two queues in 1780435068aaSAttilio Rao * question while the target processor is spinning with its own 1781435068aaSAttilio Rao * run-queue lock held while waiting for the blocked lock to clear. 1782435068aaSAttilio Rao */ 1783435068aaSAttilio Rao tdq_lock_pair(tdn, tdq); 1784c47f202bSJeff Roberson tdq_add(tdn, td, flags); 17859727e637SJeff Roberson tdq_notify(tdn, td); 1786c47f202bSJeff Roberson TDQ_UNLOCK(tdn); 1787c47f202bSJeff Roberson spinlock_exit(); 1788c47f202bSJeff Roberson #endif 1789c47f202bSJeff Roberson return (TDQ_LOCKPTR(tdn)); 1790c47f202bSJeff Roberson } 1791c47f202bSJeff Roberson 1792c47f202bSJeff Roberson /* 1793b0b9dee5SAttilio Rao * Variadic version of thread_lock_unblock() that does not assume td_lock 1794b0b9dee5SAttilio Rao * is blocked. 1795ae7a6b38SJeff Roberson */ 1796ae7a6b38SJeff Roberson static inline void 1797ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx) 1798ae7a6b38SJeff Roberson { 1799ae7a6b38SJeff Roberson atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1800ae7a6b38SJeff Roberson (uintptr_t)mtx); 1801ae7a6b38SJeff Roberson } 1802ae7a6b38SJeff Roberson 1803ae7a6b38SJeff Roberson /* 1804ae7a6b38SJeff Roberson * Switch threads. This function has to handle threads coming in while 1805ae7a6b38SJeff Roberson * blocked for some reason, running, or idle. It also must deal with 1806ae7a6b38SJeff Roberson * migrating a thread from one queue to another as running threads may 1807ae7a6b38SJeff Roberson * be assigned elsewhere via binding. 1808ae7a6b38SJeff Roberson */ 18093db720fdSDavid Xu void 18103389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags) 181135e6168fSJeff Roberson { 1812c02bbb43SJeff Roberson struct tdq *tdq; 1813ad1e7d28SJulian Elischer struct td_sched *ts; 1814ae7a6b38SJeff Roberson struct mtx *mtx; 1815c47f202bSJeff Roberson int srqflag; 1816ae7a6b38SJeff Roberson int cpuid; 181735e6168fSJeff Roberson 18187b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 18196d55b3ecSJeff Roberson KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument")); 182035e6168fSJeff Roberson 1821ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 1822ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpuid); 1823e7d50326SJeff Roberson ts = td->td_sched; 1824c47f202bSJeff Roberson mtx = td->td_lock; 1825ae7a6b38SJeff Roberson ts->ts_rltick = ticks; 1826060563ecSJulian Elischer td->td_lastcpu = td->td_oncpu; 1827060563ecSJulian Elischer td->td_oncpu = NOCPU; 1828586cb6ecSFabien Thomas if (!(flags & SW_PREEMPT)) 182952eb8464SJohn Baldwin td->td_flags &= ~TDF_NEEDRESCHED; 183077918643SStephan Uphoff td->td_owepreempt = 0; 18311690c6c1SJeff Roberson tdq->tdq_switchcnt++; 1832b11fdad0SJeff Roberson /* 1833ae7a6b38SJeff Roberson * The lock pointer in an idle thread should never change. Reset it 1834ae7a6b38SJeff Roberson * to CAN_RUN as well. 1835b11fdad0SJeff Roberson */ 1836486a9414SJulian Elischer if (TD_IS_IDLETHREAD(td)) { 1837ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1838bf0acc27SJohn Baldwin TD_SET_CAN_RUN(td); 18397b20fb19SJeff Roberson } else if (TD_IS_RUNNING(td)) { 1840ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1841c47f202bSJeff Roberson srqflag = (flags & SW_PREEMPT) ? 1842598b368dSJeff Roberson SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1843c47f202bSJeff Roberson SRQ_OURSELF|SRQ_YIELDING; 1844ba4932b5SMatthew D Fleming #ifdef SMP 18450f7a0ebdSMatthew D Fleming if (THREAD_CAN_MIGRATE(td) && !THREAD_CAN_SCHED(td, ts->ts_cpu)) 18460f7a0ebdSMatthew D Fleming ts->ts_cpu = sched_pickcpu(td, 0); 1847ba4932b5SMatthew D Fleming #endif 1848c47f202bSJeff Roberson if (ts->ts_cpu == cpuid) 18499727e637SJeff Roberson tdq_runq_add(tdq, td, srqflag); 18500f7a0ebdSMatthew D Fleming else { 18510f7a0ebdSMatthew D Fleming KASSERT(THREAD_CAN_MIGRATE(td) || 18520f7a0ebdSMatthew D Fleming (ts->ts_flags & TSF_BOUND) != 0, 18530f7a0ebdSMatthew D Fleming ("Thread %p shouldn't migrate", td)); 1854c47f202bSJeff Roberson mtx = sched_switch_migrate(tdq, td, srqflag); 18550f7a0ebdSMatthew D Fleming } 1856ae7a6b38SJeff Roberson } else { 1857ae7a6b38SJeff Roberson /* This thread must be going to sleep. */ 1858ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1859b0b9dee5SAttilio Rao mtx = thread_lock_block(td); 18609727e637SJeff Roberson tdq_load_rem(tdq, td); 1861ae7a6b38SJeff Roberson } 1862ae7a6b38SJeff Roberson /* 1863ae7a6b38SJeff Roberson * We enter here with the thread blocked and assigned to the 1864ae7a6b38SJeff Roberson * appropriate cpu run-queue or sleep-queue and with the current 1865ae7a6b38SJeff Roberson * thread-queue locked. 1866ae7a6b38SJeff Roberson */ 1867ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 18682454aaf5SJeff Roberson newtd = choosethread(); 1869ae7a6b38SJeff Roberson /* 1870ae7a6b38SJeff Roberson * Call the MD code to switch contexts if necessary. 1871ae7a6b38SJeff Roberson */ 1872ebccf1e3SJoseph Koshy if (td != newtd) { 1873ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 1874ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1875ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1876ebccf1e3SJoseph Koshy #endif 1877eea4f254SJeff Roberson lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 187859c68134SJeff Roberson TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 18796f5f25e5SJohn Birrell 18806f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS 18816f5f25e5SJohn Birrell /* 18826f5f25e5SJohn Birrell * If DTrace has set the active vtime enum to anything 18836f5f25e5SJohn Birrell * other than INACTIVE (0), then it should have set the 18846f5f25e5SJohn Birrell * function to call. 18856f5f25e5SJohn Birrell */ 18866f5f25e5SJohn Birrell if (dtrace_vtime_active) 18876f5f25e5SJohn Birrell (*dtrace_vtime_switch_func)(newtd); 18886f5f25e5SJohn Birrell #endif 18896f5f25e5SJohn Birrell 1890ae7a6b38SJeff Roberson cpu_switch(td, newtd, mtx); 1891ae7a6b38SJeff Roberson /* 1892ae7a6b38SJeff Roberson * We may return from cpu_switch on a different cpu. However, 1893ae7a6b38SJeff Roberson * we always return with td_lock pointing to the current cpu's 1894ae7a6b38SJeff Roberson * run queue lock. 1895ae7a6b38SJeff Roberson */ 1896ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 1897ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpuid); 1898eea4f254SJeff Roberson lock_profile_obtain_lock_success( 1899eea4f254SJeff Roberson &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 1900ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 1901ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1902ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1903ebccf1e3SJoseph Koshy #endif 1904ae7a6b38SJeff Roberson } else 1905ae7a6b38SJeff Roberson thread_unblock_switch(td, mtx); 1906ae7a6b38SJeff Roberson /* 1907ae7a6b38SJeff Roberson * Assert that all went well and return. 1908ae7a6b38SJeff Roberson */ 1909ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED); 1910ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1911ae7a6b38SJeff Roberson td->td_oncpu = cpuid; 191235e6168fSJeff Roberson } 191335e6168fSJeff Roberson 1914ae7a6b38SJeff Roberson /* 1915ae7a6b38SJeff Roberson * Adjust thread priorities as a result of a nice request. 1916ae7a6b38SJeff Roberson */ 191735e6168fSJeff Roberson void 1918fa885116SJulian Elischer sched_nice(struct proc *p, int nice) 191935e6168fSJeff Roberson { 192035e6168fSJeff Roberson struct thread *td; 192135e6168fSJeff Roberson 1922fa885116SJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 1923e7d50326SJeff Roberson 1924fa885116SJulian Elischer p->p_nice = nice; 19258460a577SJohn Birrell FOREACH_THREAD_IN_PROC(p, td) { 19267b20fb19SJeff Roberson thread_lock(td); 19278460a577SJohn Birrell sched_priority(td); 1928e7d50326SJeff Roberson sched_prio(td, td->td_base_user_pri); 19297b20fb19SJeff Roberson thread_unlock(td); 193035e6168fSJeff Roberson } 1931fa885116SJulian Elischer } 193235e6168fSJeff Roberson 1933ae7a6b38SJeff Roberson /* 1934ae7a6b38SJeff Roberson * Record the sleep time for the interactivity scorer. 1935ae7a6b38SJeff Roberson */ 193635e6168fSJeff Roberson void 1937c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio) 193835e6168fSJeff Roberson { 1939e7d50326SJeff Roberson 19407b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 194135e6168fSJeff Roberson 194254b0e65fSJeff Roberson td->td_slptick = ticks; 194317c4c356SKonstantin Belousov if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 1944c5aa6b58SJeff Roberson td->td_flags |= TDF_CANSWAP; 19452dc29adbSJohn Baldwin if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 19462dc29adbSJohn Baldwin return; 19470502fe2eSJeff Roberson if (static_boost == 1 && prio) 1948c5aa6b58SJeff Roberson sched_prio(td, prio); 19490502fe2eSJeff Roberson else if (static_boost && td->td_priority > static_boost) 19500502fe2eSJeff Roberson sched_prio(td, static_boost); 195135e6168fSJeff Roberson } 195235e6168fSJeff Roberson 1953ae7a6b38SJeff Roberson /* 1954ae7a6b38SJeff Roberson * Schedule a thread to resume execution and record how long it voluntarily 1955ae7a6b38SJeff Roberson * slept. We also update the pctcpu, interactivity, and priority. 1956ae7a6b38SJeff Roberson */ 195735e6168fSJeff Roberson void 195835e6168fSJeff Roberson sched_wakeup(struct thread *td) 195935e6168fSJeff Roberson { 196014618990SJeff Roberson struct td_sched *ts; 1961ae7a6b38SJeff Roberson int slptick; 1962e7d50326SJeff Roberson 19637b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 196414618990SJeff Roberson ts = td->td_sched; 1965c5aa6b58SJeff Roberson td->td_flags &= ~TDF_CANSWAP; 196635e6168fSJeff Roberson /* 1967e7d50326SJeff Roberson * If we slept for more than a tick update our interactivity and 1968e7d50326SJeff Roberson * priority. 196935e6168fSJeff Roberson */ 197054b0e65fSJeff Roberson slptick = td->td_slptick; 197154b0e65fSJeff Roberson td->td_slptick = 0; 1972ae7a6b38SJeff Roberson if (slptick && slptick != ticks) { 19739a93305aSJeff Roberson u_int hzticks; 1974f1e8dc4aSJeff Roberson 1975ae7a6b38SJeff Roberson hzticks = (ticks - slptick) << SCHED_TICK_SHIFT; 1976ae7a6b38SJeff Roberson ts->ts_slptime += hzticks; 19778460a577SJohn Birrell sched_interact_update(td); 197814618990SJeff Roberson sched_pctcpu_update(ts); 1979f1e8dc4aSJeff Roberson } 198014618990SJeff Roberson /* Reset the slice value after we sleep. */ 198114618990SJeff Roberson ts->ts_slice = sched_slice; 19827a5e5e2aSJeff Roberson sched_add(td, SRQ_BORING); 198335e6168fSJeff Roberson } 198435e6168fSJeff Roberson 198535e6168fSJeff Roberson /* 198635e6168fSJeff Roberson * Penalize the parent for creating a new child and initialize the child's 198735e6168fSJeff Roberson * priority. 198835e6168fSJeff Roberson */ 198935e6168fSJeff Roberson void 19908460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child) 199115dc847eSJeff Roberson { 19927b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1993ad1e7d28SJulian Elischer sched_fork_thread(td, child); 1994e7d50326SJeff Roberson /* 1995e7d50326SJeff Roberson * Penalize the parent and child for forking. 1996e7d50326SJeff Roberson */ 1997e7d50326SJeff Roberson sched_interact_fork(child); 1998e7d50326SJeff Roberson sched_priority(child); 1999ae7a6b38SJeff Roberson td->td_sched->ts_runtime += tickincr; 2000e7d50326SJeff Roberson sched_interact_update(td); 2001e7d50326SJeff Roberson sched_priority(td); 2002ad1e7d28SJulian Elischer } 2003ad1e7d28SJulian Elischer 2004ae7a6b38SJeff Roberson /* 2005ae7a6b38SJeff Roberson * Fork a new thread, may be within the same process. 2006ae7a6b38SJeff Roberson */ 2007ad1e7d28SJulian Elischer void 2008ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child) 2009ad1e7d28SJulian Elischer { 2010ad1e7d28SJulian Elischer struct td_sched *ts; 2011ad1e7d28SJulian Elischer struct td_sched *ts2; 20128460a577SJohn Birrell 20138b16c208SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2014e7d50326SJeff Roberson /* 2015e7d50326SJeff Roberson * Initialize child. 2016e7d50326SJeff Roberson */ 2017ad1e7d28SJulian Elischer ts = td->td_sched; 2018ad1e7d28SJulian Elischer ts2 = child->td_sched; 20198b16c208SJeff Roberson child->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 20208b16c208SJeff Roberson child->td_cpuset = cpuset_ref(td->td_cpuset); 2021ad1e7d28SJulian Elischer ts2->ts_cpu = ts->ts_cpu; 20228b16c208SJeff Roberson ts2->ts_flags = 0; 2023e7d50326SJeff Roberson /* 202422d19207SJohn Baldwin * Grab our parents cpu estimation information. 2025e7d50326SJeff Roberson */ 2026ad1e7d28SJulian Elischer ts2->ts_ticks = ts->ts_ticks; 2027ad1e7d28SJulian Elischer ts2->ts_ltick = ts->ts_ltick; 2028cbc4ea28SIvan Voras ts2->ts_incrtick = ts->ts_incrtick; 2029ad1e7d28SJulian Elischer ts2->ts_ftick = ts->ts_ftick; 203022d19207SJohn Baldwin /* 203122d19207SJohn Baldwin * Do not inherit any borrowed priority from the parent. 203222d19207SJohn Baldwin */ 203322d19207SJohn Baldwin child->td_priority = child->td_base_pri; 2034e7d50326SJeff Roberson /* 2035e7d50326SJeff Roberson * And update interactivity score. 2036e7d50326SJeff Roberson */ 2037ae7a6b38SJeff Roberson ts2->ts_slptime = ts->ts_slptime; 2038ae7a6b38SJeff Roberson ts2->ts_runtime = ts->ts_runtime; 2039e7d50326SJeff Roberson ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 20408f51ad55SJeff Roberson #ifdef KTR 20418f51ad55SJeff Roberson bzero(ts2->ts_name, sizeof(ts2->ts_name)); 20428f51ad55SJeff Roberson #endif 204315dc847eSJeff Roberson } 204415dc847eSJeff Roberson 2045ae7a6b38SJeff Roberson /* 2046ae7a6b38SJeff Roberson * Adjust the priority class of a thread. 2047ae7a6b38SJeff Roberson */ 204815dc847eSJeff Roberson void 20498460a577SJohn Birrell sched_class(struct thread *td, int class) 205015dc847eSJeff Roberson { 205115dc847eSJeff Roberson 20527b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 20538460a577SJohn Birrell if (td->td_pri_class == class) 205415dc847eSJeff Roberson return; 20558460a577SJohn Birrell td->td_pri_class = class; 205635e6168fSJeff Roberson } 205735e6168fSJeff Roberson 205835e6168fSJeff Roberson /* 205935e6168fSJeff Roberson * Return some of the child's priority and interactivity to the parent. 206035e6168fSJeff Roberson */ 206135e6168fSJeff Roberson void 2062fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child) 206335e6168fSJeff Roberson { 2064e7d50326SJeff Roberson struct thread *td; 2065141ad61cSJeff Roberson 20668f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2067cd39bb09SXin LI "prio:%d", child->td_priority); 2068374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2069e7d50326SJeff Roberson td = FIRST_THREAD_IN_PROC(p); 2070e7d50326SJeff Roberson sched_exit_thread(td, child); 2071ad1e7d28SJulian Elischer } 2072ad1e7d28SJulian Elischer 2073ae7a6b38SJeff Roberson /* 2074ae7a6b38SJeff Roberson * Penalize another thread for the time spent on this one. This helps to 2075ae7a6b38SJeff Roberson * worsen the priority and interactivity of processes which schedule batch 2076ae7a6b38SJeff Roberson * jobs such as make. This has little effect on the make process itself but 2077ae7a6b38SJeff Roberson * causes new processes spawned by it to receive worse scores immediately. 2078ae7a6b38SJeff Roberson */ 2079ad1e7d28SJulian Elischer void 2080fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child) 2081ad1e7d28SJulian Elischer { 2082fc6c30f6SJulian Elischer 20838f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2084cd39bb09SXin LI "prio:%d", child->td_priority); 2085e7d50326SJeff Roberson /* 2086e7d50326SJeff Roberson * Give the child's runtime to the parent without returning the 2087e7d50326SJeff Roberson * sleep time as a penalty to the parent. This causes shells that 2088e7d50326SJeff Roberson * launch expensive things to mark their children as expensive. 2089e7d50326SJeff Roberson */ 20907b20fb19SJeff Roberson thread_lock(td); 2091ae7a6b38SJeff Roberson td->td_sched->ts_runtime += child->td_sched->ts_runtime; 2092fc6c30f6SJulian Elischer sched_interact_update(td); 2093e7d50326SJeff Roberson sched_priority(td); 20947b20fb19SJeff Roberson thread_unlock(td); 2095ad1e7d28SJulian Elischer } 2096ad1e7d28SJulian Elischer 2097ff256d9cSJeff Roberson void 2098ff256d9cSJeff Roberson sched_preempt(struct thread *td) 2099ff256d9cSJeff Roberson { 2100ff256d9cSJeff Roberson struct tdq *tdq; 2101ff256d9cSJeff Roberson 2102ff256d9cSJeff Roberson thread_lock(td); 2103ff256d9cSJeff Roberson tdq = TDQ_SELF(); 2104ff256d9cSJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2105ff256d9cSJeff Roberson tdq->tdq_ipipending = 0; 2106ff256d9cSJeff Roberson if (td->td_priority > tdq->tdq_lowpri) { 21078df78c41SJeff Roberson int flags; 21088df78c41SJeff Roberson 21098df78c41SJeff Roberson flags = SW_INVOL | SW_PREEMPT; 2110ff256d9cSJeff Roberson if (td->td_critnest > 1) 2111ff256d9cSJeff Roberson td->td_owepreempt = 1; 21128df78c41SJeff Roberson else if (TD_IS_IDLETHREAD(td)) 21138df78c41SJeff Roberson mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL); 2114ff256d9cSJeff Roberson else 21158df78c41SJeff Roberson mi_switch(flags | SWT_REMOTEPREEMPT, NULL); 2116ff256d9cSJeff Roberson } 2117ff256d9cSJeff Roberson thread_unlock(td); 2118ff256d9cSJeff Roberson } 2119ff256d9cSJeff Roberson 2120ae7a6b38SJeff Roberson /* 2121ae7a6b38SJeff Roberson * Fix priorities on return to user-space. Priorities may be elevated due 2122ae7a6b38SJeff Roberson * to static priorities in msleep() or similar. 2123ae7a6b38SJeff Roberson */ 2124ad1e7d28SJulian Elischer void 2125ad1e7d28SJulian Elischer sched_userret(struct thread *td) 2126ad1e7d28SJulian Elischer { 2127ad1e7d28SJulian Elischer /* 2128ad1e7d28SJulian Elischer * XXX we cheat slightly on the locking here to avoid locking in 2129ad1e7d28SJulian Elischer * the usual case. Setting td_priority here is essentially an 2130ad1e7d28SJulian Elischer * incomplete workaround for not setting it properly elsewhere. 2131ad1e7d28SJulian Elischer * Now that some interrupt handlers are threads, not setting it 2132ad1e7d28SJulian Elischer * properly elsewhere can clobber it in the window between setting 2133ad1e7d28SJulian Elischer * it here and returning to user mode, so don't waste time setting 2134ad1e7d28SJulian Elischer * it perfectly here. 2135ad1e7d28SJulian Elischer */ 2136ad1e7d28SJulian Elischer KASSERT((td->td_flags & TDF_BORROWING) == 0, 2137ad1e7d28SJulian Elischer ("thread with borrowed priority returning to userland")); 2138ad1e7d28SJulian Elischer if (td->td_priority != td->td_user_pri) { 21397b20fb19SJeff Roberson thread_lock(td); 2140ad1e7d28SJulian Elischer td->td_priority = td->td_user_pri; 2141ad1e7d28SJulian Elischer td->td_base_pri = td->td_user_pri; 214262fa74d9SJeff Roberson tdq_setlowpri(TDQ_SELF(), td); 21437b20fb19SJeff Roberson thread_unlock(td); 2144ad1e7d28SJulian Elischer } 214535e6168fSJeff Roberson } 214635e6168fSJeff Roberson 2147ae7a6b38SJeff Roberson /* 2148ae7a6b38SJeff Roberson * Handle a stathz tick. This is really only relevant for timeshare 2149ae7a6b38SJeff Roberson * threads. 2150ae7a6b38SJeff Roberson */ 215135e6168fSJeff Roberson void 21527cf90fb3SJeff Roberson sched_clock(struct thread *td) 215335e6168fSJeff Roberson { 2154ad1e7d28SJulian Elischer struct tdq *tdq; 2155ad1e7d28SJulian Elischer struct td_sched *ts; 215635e6168fSJeff Roberson 2157ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 21583f872f85SJeff Roberson tdq = TDQ_SELF(); 21597fcf154aSJeff Roberson #ifdef SMP 21607fcf154aSJeff Roberson /* 21617fcf154aSJeff Roberson * We run the long term load balancer infrequently on the first cpu. 21627fcf154aSJeff Roberson */ 21637fcf154aSJeff Roberson if (balance_tdq == tdq) { 21647fcf154aSJeff Roberson if (balance_ticks && --balance_ticks == 0) 21657fcf154aSJeff Roberson sched_balance(); 21667fcf154aSJeff Roberson } 21677fcf154aSJeff Roberson #endif 21683f872f85SJeff Roberson /* 21691690c6c1SJeff Roberson * Save the old switch count so we have a record of the last ticks 21701690c6c1SJeff Roberson * activity. Initialize the new switch count based on our load. 21711690c6c1SJeff Roberson * If there is some activity seed it to reflect that. 21721690c6c1SJeff Roberson */ 21731690c6c1SJeff Roberson tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 21746c47aaaeSJeff Roberson tdq->tdq_switchcnt = tdq->tdq_load; 21751690c6c1SJeff Roberson /* 21763f872f85SJeff Roberson * Advance the insert index once for each tick to ensure that all 21773f872f85SJeff Roberson * threads get a chance to run. 21783f872f85SJeff Roberson */ 21793f872f85SJeff Roberson if (tdq->tdq_idx == tdq->tdq_ridx) { 21803f872f85SJeff Roberson tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 21813f872f85SJeff Roberson if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 21823f872f85SJeff Roberson tdq->tdq_ridx = tdq->tdq_idx; 21833f872f85SJeff Roberson } 21843f872f85SJeff Roberson ts = td->td_sched; 2185fd0b8c78SJeff Roberson if (td->td_pri_class & PRI_FIFO_BIT) 2186a8949de2SJeff Roberson return; 2187c9a8cba4SJohn Baldwin if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) { 2188a8949de2SJeff Roberson /* 2189fd0b8c78SJeff Roberson * We used a tick; charge it to the thread so 2190fd0b8c78SJeff Roberson * that we can compute our interactivity. 219115dc847eSJeff Roberson */ 2192ae7a6b38SJeff Roberson td->td_sched->ts_runtime += tickincr; 21938460a577SJohn Birrell sched_interact_update(td); 219473daf66fSJeff Roberson sched_priority(td); 2195fd0b8c78SJeff Roberson } 219635e6168fSJeff Roberson /* 219735e6168fSJeff Roberson * We used up one time slice. 219835e6168fSJeff Roberson */ 2199ad1e7d28SJulian Elischer if (--ts->ts_slice > 0) 220015dc847eSJeff Roberson return; 220135e6168fSJeff Roberson /* 220273daf66fSJeff Roberson * We're out of time, force a requeue at userret(). 220335e6168fSJeff Roberson */ 220473daf66fSJeff Roberson ts->ts_slice = sched_slice; 22054a338afdSJulian Elischer td->td_flags |= TDF_NEEDRESCHED; 220635e6168fSJeff Roberson } 220735e6168fSJeff Roberson 2208ae7a6b38SJeff Roberson /* 2209ae7a6b38SJeff Roberson * Called once per hz tick. Used for cpu utilization information. This 2210ae7a6b38SJeff Roberson * is easier than trying to scale based on stathz. 2211ae7a6b38SJeff Roberson */ 2212ae7a6b38SJeff Roberson void 2213a157e425SAlexander Motin sched_tick(int cnt) 2214ae7a6b38SJeff Roberson { 2215ae7a6b38SJeff Roberson struct td_sched *ts; 2216ae7a6b38SJeff Roberson 2217ae7a6b38SJeff Roberson ts = curthread->td_sched; 2218e980fff6SJeff Roberson /* 2219e980fff6SJeff Roberson * Ticks is updated asynchronously on a single cpu. Check here to 2220e980fff6SJeff Roberson * avoid incrementing ts_ticks multiple times in a single tick. 2221e980fff6SJeff Roberson */ 2222cbc4ea28SIvan Voras if (ts->ts_incrtick == ticks) 2223e980fff6SJeff Roberson return; 2224ae7a6b38SJeff Roberson /* Adjust ticks for pctcpu */ 2225a157e425SAlexander Motin ts->ts_ticks += cnt << SCHED_TICK_SHIFT; 2226ae7a6b38SJeff Roberson ts->ts_ltick = ticks; 2227cbc4ea28SIvan Voras ts->ts_incrtick = ticks; 2228ae7a6b38SJeff Roberson /* 22299f518f20SAttilio Rao * Update if we've exceeded our desired tick threshold by over one 2230ae7a6b38SJeff Roberson * second. 2231ae7a6b38SJeff Roberson */ 2232ae7a6b38SJeff Roberson if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick) 2233ae7a6b38SJeff Roberson sched_pctcpu_update(ts); 2234ae7a6b38SJeff Roberson } 2235ae7a6b38SJeff Roberson 2236ae7a6b38SJeff Roberson /* 2237ae7a6b38SJeff Roberson * Return whether the current CPU has runnable tasks. Used for in-kernel 2238ae7a6b38SJeff Roberson * cooperative idle threads. 2239ae7a6b38SJeff Roberson */ 224035e6168fSJeff Roberson int 224135e6168fSJeff Roberson sched_runnable(void) 224235e6168fSJeff Roberson { 2243ad1e7d28SJulian Elischer struct tdq *tdq; 2244b90816f1SJeff Roberson int load; 224535e6168fSJeff Roberson 2246b90816f1SJeff Roberson load = 1; 2247b90816f1SJeff Roberson 2248ad1e7d28SJulian Elischer tdq = TDQ_SELF(); 22493f741ca1SJeff Roberson if ((curthread->td_flags & TDF_IDLETD) != 0) { 2250d2ad694cSJeff Roberson if (tdq->tdq_load > 0) 22513f741ca1SJeff Roberson goto out; 22523f741ca1SJeff Roberson } else 2253d2ad694cSJeff Roberson if (tdq->tdq_load - 1 > 0) 2254b90816f1SJeff Roberson goto out; 2255b90816f1SJeff Roberson load = 0; 2256b90816f1SJeff Roberson out: 2257b90816f1SJeff Roberson return (load); 225835e6168fSJeff Roberson } 225935e6168fSJeff Roberson 2260ae7a6b38SJeff Roberson /* 2261ae7a6b38SJeff Roberson * Choose the highest priority thread to run. The thread is removed from 2262ae7a6b38SJeff Roberson * the run-queue while running however the load remains. For SMP we set 2263ae7a6b38SJeff Roberson * the tdq in the global idle bitmask if it idles here. 2264ae7a6b38SJeff Roberson */ 22657a5e5e2aSJeff Roberson struct thread * 2266c9f25d8fSJeff Roberson sched_choose(void) 2267c9f25d8fSJeff Roberson { 22689727e637SJeff Roberson struct thread *td; 2269ae7a6b38SJeff Roberson struct tdq *tdq; 2270ae7a6b38SJeff Roberson 2271ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2272ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 22739727e637SJeff Roberson td = tdq_choose(tdq); 22749727e637SJeff Roberson if (td) { 22759727e637SJeff Roberson td->td_sched->ts_ltick = ticks; 22769727e637SJeff Roberson tdq_runq_rem(tdq, td); 22770502fe2eSJeff Roberson tdq->tdq_lowpri = td->td_priority; 22789727e637SJeff Roberson return (td); 227935e6168fSJeff Roberson } 22800502fe2eSJeff Roberson tdq->tdq_lowpri = PRI_MAX_IDLE; 228162fa74d9SJeff Roberson return (PCPU_GET(idlethread)); 22827a5e5e2aSJeff Roberson } 22837a5e5e2aSJeff Roberson 2284ae7a6b38SJeff Roberson /* 2285ae7a6b38SJeff Roberson * Set owepreempt if necessary. Preemption never happens directly in ULE, 2286ae7a6b38SJeff Roberson * we always request it once we exit a critical section. 2287ae7a6b38SJeff Roberson */ 2288ae7a6b38SJeff Roberson static inline void 2289ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td) 22907a5e5e2aSJeff Roberson { 22917a5e5e2aSJeff Roberson struct thread *ctd; 22927a5e5e2aSJeff Roberson int cpri; 22937a5e5e2aSJeff Roberson int pri; 22947a5e5e2aSJeff Roberson 2295ff256d9cSJeff Roberson THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2296ff256d9cSJeff Roberson 22977a5e5e2aSJeff Roberson ctd = curthread; 22987a5e5e2aSJeff Roberson pri = td->td_priority; 22997a5e5e2aSJeff Roberson cpri = ctd->td_priority; 2300ff256d9cSJeff Roberson if (pri < cpri) 2301ff256d9cSJeff Roberson ctd->td_flags |= TDF_NEEDRESCHED; 23027a5e5e2aSJeff Roberson if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2303ae7a6b38SJeff Roberson return; 2304ff256d9cSJeff Roberson if (!sched_shouldpreempt(pri, cpri, 0)) 2305ae7a6b38SJeff Roberson return; 23067a5e5e2aSJeff Roberson ctd->td_owepreempt = 1; 230735e6168fSJeff Roberson } 230835e6168fSJeff Roberson 2309ae7a6b38SJeff Roberson /* 231073daf66fSJeff Roberson * Add a thread to a thread queue. Select the appropriate runq and add the 231173daf66fSJeff Roberson * thread to it. This is the internal function called when the tdq is 231273daf66fSJeff Roberson * predetermined. 2313ae7a6b38SJeff Roberson */ 231435e6168fSJeff Roberson void 2315ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags) 231635e6168fSJeff Roberson { 2317c9f25d8fSJeff Roberson 2318ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 23197a5e5e2aSJeff Roberson KASSERT((td->td_inhibitors == 0), 23207a5e5e2aSJeff Roberson ("sched_add: trying to run inhibited thread")); 23217a5e5e2aSJeff Roberson KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 23227a5e5e2aSJeff Roberson ("sched_add: bad thread state")); 2323b61ce5b0SJeff Roberson KASSERT(td->td_flags & TDF_INMEM, 2324b61ce5b0SJeff Roberson ("sched_add: thread swapped out")); 2325ae7a6b38SJeff Roberson 2326ae7a6b38SJeff Roberson if (td->td_priority < tdq->tdq_lowpri) 2327ae7a6b38SJeff Roberson tdq->tdq_lowpri = td->td_priority; 23289727e637SJeff Roberson tdq_runq_add(tdq, td, flags); 23299727e637SJeff Roberson tdq_load_add(tdq, td); 2330ae7a6b38SJeff Roberson } 2331ae7a6b38SJeff Roberson 2332ae7a6b38SJeff Roberson /* 2333ae7a6b38SJeff Roberson * Select the target thread queue and add a thread to it. Request 2334ae7a6b38SJeff Roberson * preemption or IPI a remote processor if required. 2335ae7a6b38SJeff Roberson */ 2336ae7a6b38SJeff Roberson void 2337ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags) 2338ae7a6b38SJeff Roberson { 2339ae7a6b38SJeff Roberson struct tdq *tdq; 23407b8bfa0dSJeff Roberson #ifdef SMP 2341ae7a6b38SJeff Roberson int cpu; 2342ae7a6b38SJeff Roberson #endif 23438f51ad55SJeff Roberson 23448f51ad55SJeff Roberson KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 23458f51ad55SJeff Roberson "prio:%d", td->td_priority, KTR_ATTR_LINKED, 23468f51ad55SJeff Roberson sched_tdname(curthread)); 23478f51ad55SJeff Roberson KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 23488f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(td)); 2349ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2350ae7a6b38SJeff Roberson /* 2351ae7a6b38SJeff Roberson * Recalculate the priority before we select the target cpu or 2352ae7a6b38SJeff Roberson * run-queue. 2353ae7a6b38SJeff Roberson */ 2354ae7a6b38SJeff Roberson if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2355ae7a6b38SJeff Roberson sched_priority(td); 2356ae7a6b38SJeff Roberson #ifdef SMP 2357ae7a6b38SJeff Roberson /* 2358ae7a6b38SJeff Roberson * Pick the destination cpu and if it isn't ours transfer to the 2359ae7a6b38SJeff Roberson * target cpu. 2360ae7a6b38SJeff Roberson */ 23619727e637SJeff Roberson cpu = sched_pickcpu(td, flags); 23629727e637SJeff Roberson tdq = sched_setcpu(td, cpu, flags); 2363ae7a6b38SJeff Roberson tdq_add(tdq, td, flags); 236473daf66fSJeff Roberson if (cpu != PCPU_GET(cpuid)) { 23659727e637SJeff Roberson tdq_notify(tdq, td); 23667b8bfa0dSJeff Roberson return; 23677b8bfa0dSJeff Roberson } 2368ae7a6b38SJeff Roberson #else 2369ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2370ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 2371ae7a6b38SJeff Roberson /* 2372ae7a6b38SJeff Roberson * Now that the thread is moving to the run-queue, set the lock 2373ae7a6b38SJeff Roberson * to the scheduler's lock. 2374ae7a6b38SJeff Roberson */ 2375ae7a6b38SJeff Roberson thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2376ae7a6b38SJeff Roberson tdq_add(tdq, td, flags); 23777b8bfa0dSJeff Roberson #endif 2378ae7a6b38SJeff Roberson if (!(flags & SRQ_YIELDING)) 2379ae7a6b38SJeff Roberson sched_setpreempt(td); 238035e6168fSJeff Roberson } 238135e6168fSJeff Roberson 2382ae7a6b38SJeff Roberson /* 2383ae7a6b38SJeff Roberson * Remove a thread from a run-queue without running it. This is used 2384ae7a6b38SJeff Roberson * when we're stealing a thread from a remote queue. Otherwise all threads 2385ae7a6b38SJeff Roberson * exit by calling sched_exit_thread() and sched_throw() themselves. 2386ae7a6b38SJeff Roberson */ 238735e6168fSJeff Roberson void 23887cf90fb3SJeff Roberson sched_rem(struct thread *td) 238935e6168fSJeff Roberson { 2390ad1e7d28SJulian Elischer struct tdq *tdq; 23917cf90fb3SJeff Roberson 23928f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 23938f51ad55SJeff Roberson "prio:%d", td->td_priority); 23949727e637SJeff Roberson tdq = TDQ_CPU(td->td_sched->ts_cpu); 2395ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2396ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 23977a5e5e2aSJeff Roberson KASSERT(TD_ON_RUNQ(td), 2398ad1e7d28SJulian Elischer ("sched_rem: thread not on run queue")); 23999727e637SJeff Roberson tdq_runq_rem(tdq, td); 24009727e637SJeff Roberson tdq_load_rem(tdq, td); 24017a5e5e2aSJeff Roberson TD_SET_CAN_RUN(td); 240262fa74d9SJeff Roberson if (td->td_priority == tdq->tdq_lowpri) 240362fa74d9SJeff Roberson tdq_setlowpri(tdq, NULL); 240435e6168fSJeff Roberson } 240535e6168fSJeff Roberson 2406ae7a6b38SJeff Roberson /* 2407ae7a6b38SJeff Roberson * Fetch cpu utilization information. Updates on demand. 2408ae7a6b38SJeff Roberson */ 240935e6168fSJeff Roberson fixpt_t 24107cf90fb3SJeff Roberson sched_pctcpu(struct thread *td) 241135e6168fSJeff Roberson { 241235e6168fSJeff Roberson fixpt_t pctcpu; 2413ad1e7d28SJulian Elischer struct td_sched *ts; 241435e6168fSJeff Roberson 241535e6168fSJeff Roberson pctcpu = 0; 2416ad1e7d28SJulian Elischer ts = td->td_sched; 2417ad1e7d28SJulian Elischer if (ts == NULL) 2418484288deSJeff Roberson return (0); 241935e6168fSJeff Roberson 24203da35a0aSJohn Baldwin THREAD_LOCK_ASSERT(td, MA_OWNED); 2421ad1e7d28SJulian Elischer if (ts->ts_ticks) { 242235e6168fSJeff Roberson int rtick; 242335e6168fSJeff Roberson 2424ad1e7d28SJulian Elischer sched_pctcpu_update(ts); 242535e6168fSJeff Roberson /* How many rtick per second ? */ 2426e7d50326SJeff Roberson rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2427e7d50326SJeff Roberson pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 242835e6168fSJeff Roberson } 242935e6168fSJeff Roberson 243035e6168fSJeff Roberson return (pctcpu); 243135e6168fSJeff Roberson } 243235e6168fSJeff Roberson 243362fa74d9SJeff Roberson /* 243462fa74d9SJeff Roberson * Enforce affinity settings for a thread. Called after adjustments to 243562fa74d9SJeff Roberson * cpumask. 243662fa74d9SJeff Roberson */ 2437885d51a3SJeff Roberson void 2438885d51a3SJeff Roberson sched_affinity(struct thread *td) 2439885d51a3SJeff Roberson { 244062fa74d9SJeff Roberson #ifdef SMP 244162fa74d9SJeff Roberson struct td_sched *ts; 244262fa74d9SJeff Roberson 244362fa74d9SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 244462fa74d9SJeff Roberson ts = td->td_sched; 244562fa74d9SJeff Roberson if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 244662fa74d9SJeff Roberson return; 244753a6c8b3SJeff Roberson if (TD_ON_RUNQ(td)) { 244853a6c8b3SJeff Roberson sched_rem(td); 244953a6c8b3SJeff Roberson sched_add(td, SRQ_BORING); 245053a6c8b3SJeff Roberson return; 245153a6c8b3SJeff Roberson } 245262fa74d9SJeff Roberson if (!TD_IS_RUNNING(td)) 245362fa74d9SJeff Roberson return; 245462fa74d9SJeff Roberson /* 24550f7a0ebdSMatthew D Fleming * Force a switch before returning to userspace. If the 24560f7a0ebdSMatthew D Fleming * target thread is not running locally send an ipi to force 24570f7a0ebdSMatthew D Fleming * the issue. 245862fa74d9SJeff Roberson */ 2459a8103ae8SJohn Baldwin td->td_flags |= TDF_NEEDRESCHED; 24600f7a0ebdSMatthew D Fleming if (td != curthread) 24610f7a0ebdSMatthew D Fleming ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 246262fa74d9SJeff Roberson #endif 2463885d51a3SJeff Roberson } 2464885d51a3SJeff Roberson 2465ae7a6b38SJeff Roberson /* 2466ae7a6b38SJeff Roberson * Bind a thread to a target cpu. 2467ae7a6b38SJeff Roberson */ 24689bacd788SJeff Roberson void 24699bacd788SJeff Roberson sched_bind(struct thread *td, int cpu) 24709bacd788SJeff Roberson { 2471ad1e7d28SJulian Elischer struct td_sched *ts; 24729bacd788SJeff Roberson 2473c47f202bSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 24741d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 2475ad1e7d28SJulian Elischer ts = td->td_sched; 24766b2f763fSJeff Roberson if (ts->ts_flags & TSF_BOUND) 2477c95d2db2SJeff Roberson sched_unbind(td); 24780f7a0ebdSMatthew D Fleming KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2479ad1e7d28SJulian Elischer ts->ts_flags |= TSF_BOUND; 24806b2f763fSJeff Roberson sched_pin(); 248180f86c9fSJeff Roberson if (PCPU_GET(cpuid) == cpu) 24829bacd788SJeff Roberson return; 24836b2f763fSJeff Roberson ts->ts_cpu = cpu; 24849bacd788SJeff Roberson /* When we return from mi_switch we'll be on the correct cpu. */ 2485279f949eSPoul-Henning Kamp mi_switch(SW_VOL, NULL); 24869bacd788SJeff Roberson } 24879bacd788SJeff Roberson 2488ae7a6b38SJeff Roberson /* 2489ae7a6b38SJeff Roberson * Release a bound thread. 2490ae7a6b38SJeff Roberson */ 24919bacd788SJeff Roberson void 24929bacd788SJeff Roberson sched_unbind(struct thread *td) 24939bacd788SJeff Roberson { 2494e7d50326SJeff Roberson struct td_sched *ts; 2495e7d50326SJeff Roberson 24967b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 24971d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 2498e7d50326SJeff Roberson ts = td->td_sched; 24996b2f763fSJeff Roberson if ((ts->ts_flags & TSF_BOUND) == 0) 25006b2f763fSJeff Roberson return; 2501e7d50326SJeff Roberson ts->ts_flags &= ~TSF_BOUND; 2502e7d50326SJeff Roberson sched_unpin(); 25039bacd788SJeff Roberson } 25049bacd788SJeff Roberson 250535e6168fSJeff Roberson int 2506ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td) 2507ebccf1e3SJoseph Koshy { 25087b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2509ad1e7d28SJulian Elischer return (td->td_sched->ts_flags & TSF_BOUND); 2510ebccf1e3SJoseph Koshy } 2511ebccf1e3SJoseph Koshy 2512ae7a6b38SJeff Roberson /* 2513ae7a6b38SJeff Roberson * Basic yield call. 2514ae7a6b38SJeff Roberson */ 251536ec198bSDavid Xu void 251636ec198bSDavid Xu sched_relinquish(struct thread *td) 251736ec198bSDavid Xu { 25187b20fb19SJeff Roberson thread_lock(td); 25198df78c41SJeff Roberson mi_switch(SW_VOL | SWT_RELINQUISH, NULL); 25207b20fb19SJeff Roberson thread_unlock(td); 252136ec198bSDavid Xu } 252236ec198bSDavid Xu 2523ae7a6b38SJeff Roberson /* 2524ae7a6b38SJeff Roberson * Return the total system load. 2525ae7a6b38SJeff Roberson */ 2526ebccf1e3SJoseph Koshy int 252733916c36SJeff Roberson sched_load(void) 252833916c36SJeff Roberson { 252933916c36SJeff Roberson #ifdef SMP 253033916c36SJeff Roberson int total; 253133916c36SJeff Roberson int i; 253233916c36SJeff Roberson 253333916c36SJeff Roberson total = 0; 25343aa6d94eSJohn Baldwin CPU_FOREACH(i) 253562fa74d9SJeff Roberson total += TDQ_CPU(i)->tdq_sysload; 253633916c36SJeff Roberson return (total); 253733916c36SJeff Roberson #else 2538d2ad694cSJeff Roberson return (TDQ_SELF()->tdq_sysload); 253933916c36SJeff Roberson #endif 254033916c36SJeff Roberson } 254133916c36SJeff Roberson 254233916c36SJeff Roberson int 254335e6168fSJeff Roberson sched_sizeof_proc(void) 254435e6168fSJeff Roberson { 254535e6168fSJeff Roberson return (sizeof(struct proc)); 254635e6168fSJeff Roberson } 254735e6168fSJeff Roberson 254835e6168fSJeff Roberson int 254935e6168fSJeff Roberson sched_sizeof_thread(void) 255035e6168fSJeff Roberson { 255135e6168fSJeff Roberson return (sizeof(struct thread) + sizeof(struct td_sched)); 255235e6168fSJeff Roberson } 2553b41f1452SDavid Xu 255409c8a4ccSJeff Roberson #ifdef SMP 255509c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) \ 255609c8a4ccSJeff Roberson ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 255709c8a4ccSJeff Roberson #else 255809c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) 1 255909c8a4ccSJeff Roberson #endif 256009c8a4ccSJeff Roberson 25617a5e5e2aSJeff Roberson /* 25627a5e5e2aSJeff Roberson * The actual idle process. 25637a5e5e2aSJeff Roberson */ 25647a5e5e2aSJeff Roberson void 25657a5e5e2aSJeff Roberson sched_idletd(void *dummy) 25667a5e5e2aSJeff Roberson { 25677a5e5e2aSJeff Roberson struct thread *td; 2568ae7a6b38SJeff Roberson struct tdq *tdq; 25691690c6c1SJeff Roberson int switchcnt; 25701690c6c1SJeff Roberson int i; 25717a5e5e2aSJeff Roberson 25727b55ab05SJeff Roberson mtx_assert(&Giant, MA_NOTOWNED); 25737a5e5e2aSJeff Roberson td = curthread; 2574ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2575ae7a6b38SJeff Roberson for (;;) { 2576ae7a6b38SJeff Roberson #ifdef SMP 25771690c6c1SJeff Roberson if (tdq_idled(tdq) == 0) 25781690c6c1SJeff Roberson continue; 2579ae7a6b38SJeff Roberson #endif 25801690c6c1SJeff Roberson switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 25811690c6c1SJeff Roberson /* 25821690c6c1SJeff Roberson * If we're switching very frequently, spin while checking 25831690c6c1SJeff Roberson * for load rather than entering a low power state that 25847b55ab05SJeff Roberson * may require an IPI. However, don't do any busy 25857b55ab05SJeff Roberson * loops while on SMT machines as this simply steals 25867b55ab05SJeff Roberson * cycles from cores doing useful work. 25871690c6c1SJeff Roberson */ 258809c8a4ccSJeff Roberson if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 25891690c6c1SJeff Roberson for (i = 0; i < sched_idlespins; i++) { 25901690c6c1SJeff Roberson if (tdq->tdq_load) 25911690c6c1SJeff Roberson break; 25921690c6c1SJeff Roberson cpu_spinwait(); 25931690c6c1SJeff Roberson } 25941690c6c1SJeff Roberson } 25956c47aaaeSJeff Roberson switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 25969f9ad565SAlexander Motin if (tdq->tdq_load == 0) { 25979f9ad565SAlexander Motin tdq->tdq_cpu_idle = 1; 25989f9ad565SAlexander Motin if (tdq->tdq_load == 0) { 2599a157e425SAlexander Motin cpu_idle(switchcnt > sched_idlespinthresh * 4); 26009f9ad565SAlexander Motin tdq->tdq_switchcnt++; 26019f9ad565SAlexander Motin } 26029f9ad565SAlexander Motin tdq->tdq_cpu_idle = 0; 26039f9ad565SAlexander Motin } 26041690c6c1SJeff Roberson if (tdq->tdq_load) { 26051690c6c1SJeff Roberson thread_lock(td); 26061690c6c1SJeff Roberson mi_switch(SW_VOL | SWT_IDLE, NULL); 26071690c6c1SJeff Roberson thread_unlock(td); 26081690c6c1SJeff Roberson } 2609ae7a6b38SJeff Roberson } 2610b41f1452SDavid Xu } 2611e7d50326SJeff Roberson 26127b20fb19SJeff Roberson /* 26137b20fb19SJeff Roberson * A CPU is entering for the first time or a thread is exiting. 26147b20fb19SJeff Roberson */ 26157b20fb19SJeff Roberson void 26167b20fb19SJeff Roberson sched_throw(struct thread *td) 26177b20fb19SJeff Roberson { 261859c68134SJeff Roberson struct thread *newtd; 2619ae7a6b38SJeff Roberson struct tdq *tdq; 2620ae7a6b38SJeff Roberson 2621ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 26227b20fb19SJeff Roberson if (td == NULL) { 2623ae7a6b38SJeff Roberson /* Correct spinlock nesting and acquire the correct lock. */ 2624ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 26257b20fb19SJeff Roberson spinlock_exit(); 26267e3a96eaSJohn Baldwin PCPU_SET(switchtime, cpu_ticks()); 26277e3a96eaSJohn Baldwin PCPU_SET(switchticks, ticks); 26287b20fb19SJeff Roberson } else { 2629ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 26309727e637SJeff Roberson tdq_load_rem(tdq, td); 2631eea4f254SJeff Roberson lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 26327b20fb19SJeff Roberson } 26337b20fb19SJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count")); 263459c68134SJeff Roberson newtd = choosethread(); 263559c68134SJeff Roberson TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 263659c68134SJeff Roberson cpu_throw(td, newtd); /* doesn't return */ 26377b20fb19SJeff Roberson } 26387b20fb19SJeff Roberson 2639ae7a6b38SJeff Roberson /* 2640ae7a6b38SJeff Roberson * This is called from fork_exit(). Just acquire the correct locks and 2641ae7a6b38SJeff Roberson * let fork do the rest of the work. 2642ae7a6b38SJeff Roberson */ 26437b20fb19SJeff Roberson void 2644fe54587fSJeff Roberson sched_fork_exit(struct thread *td) 26457b20fb19SJeff Roberson { 2646ae7a6b38SJeff Roberson struct td_sched *ts; 2647ae7a6b38SJeff Roberson struct tdq *tdq; 2648ae7a6b38SJeff Roberson int cpuid; 26497b20fb19SJeff Roberson 26507b20fb19SJeff Roberson /* 26517b20fb19SJeff Roberson * Finish setting up thread glue so that it begins execution in a 2652ae7a6b38SJeff Roberson * non-nested critical section with the scheduler lock held. 26537b20fb19SJeff Roberson */ 2654ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 2655ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpuid); 2656ae7a6b38SJeff Roberson ts = td->td_sched; 2657ae7a6b38SJeff Roberson if (TD_IS_IDLETHREAD(td)) 2658ae7a6b38SJeff Roberson td->td_lock = TDQ_LOCKPTR(tdq); 2659ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2660ae7a6b38SJeff Roberson td->td_oncpu = cpuid; 266159c68134SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 2662eea4f254SJeff Roberson lock_profile_obtain_lock_success( 2663eea4f254SJeff Roberson &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 26647b20fb19SJeff Roberson } 26657b20fb19SJeff Roberson 26668f51ad55SJeff Roberson /* 26678f51ad55SJeff Roberson * Create on first use to catch odd startup conditons. 26688f51ad55SJeff Roberson */ 26698f51ad55SJeff Roberson char * 26708f51ad55SJeff Roberson sched_tdname(struct thread *td) 26718f51ad55SJeff Roberson { 26728f51ad55SJeff Roberson #ifdef KTR 26738f51ad55SJeff Roberson struct td_sched *ts; 26748f51ad55SJeff Roberson 26758f51ad55SJeff Roberson ts = td->td_sched; 26768f51ad55SJeff Roberson if (ts->ts_name[0] == '\0') 26778f51ad55SJeff Roberson snprintf(ts->ts_name, sizeof(ts->ts_name), 26788f51ad55SJeff Roberson "%s tid %d", td->td_name, td->td_tid); 26798f51ad55SJeff Roberson return (ts->ts_name); 26808f51ad55SJeff Roberson #else 26818f51ad55SJeff Roberson return (td->td_name); 26828f51ad55SJeff Roberson #endif 26838f51ad55SJeff Roberson } 26848f51ad55SJeff Roberson 268507095abfSIvan Voras #ifdef SMP 268607095abfSIvan Voras 268707095abfSIvan Voras /* 268807095abfSIvan Voras * Build the CPU topology dump string. Is recursively called to collect 268907095abfSIvan Voras * the topology tree. 269007095abfSIvan Voras */ 269107095abfSIvan Voras static int 269207095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 269307095abfSIvan Voras int indent) 269407095abfSIvan Voras { 269571a19bdcSAttilio Rao char cpusetbuf[CPUSETBUFSIZ]; 269607095abfSIvan Voras int i, first; 269707095abfSIvan Voras 269807095abfSIvan Voras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 269919b8a6dbSAndriy Gapon "", 1 + indent / 2, cg->cg_level); 270071a19bdcSAttilio Rao sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"%s\">", indent, "", 270171a19bdcSAttilio Rao cg->cg_count, cpusetobj_strprint(cpusetbuf, &cg->cg_mask)); 270207095abfSIvan Voras first = TRUE; 270307095abfSIvan Voras for (i = 0; i < MAXCPU; i++) { 270471a19bdcSAttilio Rao if (CPU_ISSET(i, &cg->cg_mask)) { 270507095abfSIvan Voras if (!first) 270607095abfSIvan Voras sbuf_printf(sb, ", "); 270707095abfSIvan Voras else 270807095abfSIvan Voras first = FALSE; 270907095abfSIvan Voras sbuf_printf(sb, "%d", i); 271007095abfSIvan Voras } 271107095abfSIvan Voras } 271207095abfSIvan Voras sbuf_printf(sb, "</cpu>\n"); 271307095abfSIvan Voras 271407095abfSIvan Voras if (cg->cg_flags != 0) { 2715611daf7eSIvan Voras sbuf_printf(sb, "%*s <flags>", indent, ""); 271607095abfSIvan Voras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 27175368befbSIvan Voras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 2718a401f2d0SIvan Voras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2719a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 27207b55ab05SJeff Roberson if ((cg->cg_flags & CG_FLAG_SMT) != 0) 2721a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 272207095abfSIvan Voras sbuf_printf(sb, "</flags>\n"); 2723611daf7eSIvan Voras } 272407095abfSIvan Voras 272507095abfSIvan Voras if (cg->cg_children > 0) { 272607095abfSIvan Voras sbuf_printf(sb, "%*s <children>\n", indent, ""); 272707095abfSIvan Voras for (i = 0; i < cg->cg_children; i++) 272807095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(sb, 272907095abfSIvan Voras &cg->cg_child[i], indent+2); 273007095abfSIvan Voras sbuf_printf(sb, "%*s </children>\n", indent, ""); 273107095abfSIvan Voras } 273207095abfSIvan Voras sbuf_printf(sb, "%*s</group>\n", indent, ""); 273307095abfSIvan Voras return (0); 273407095abfSIvan Voras } 273507095abfSIvan Voras 273607095abfSIvan Voras /* 273707095abfSIvan Voras * Sysctl handler for retrieving topology dump. It's a wrapper for 273807095abfSIvan Voras * the recursive sysctl_kern_smp_topology_spec_internal(). 273907095abfSIvan Voras */ 274007095abfSIvan Voras static int 274107095abfSIvan Voras sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 274207095abfSIvan Voras { 274307095abfSIvan Voras struct sbuf *topo; 274407095abfSIvan Voras int err; 274507095abfSIvan Voras 274607095abfSIvan Voras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 274707095abfSIvan Voras 2748aa880b90SIvan Voras topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND); 274907095abfSIvan Voras if (topo == NULL) 275007095abfSIvan Voras return (ENOMEM); 275107095abfSIvan Voras 275207095abfSIvan Voras sbuf_printf(topo, "<groups>\n"); 275307095abfSIvan Voras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 275407095abfSIvan Voras sbuf_printf(topo, "</groups>\n"); 275507095abfSIvan Voras 275607095abfSIvan Voras if (err == 0) { 275707095abfSIvan Voras sbuf_finish(topo); 275807095abfSIvan Voras err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo)); 275907095abfSIvan Voras } 276007095abfSIvan Voras sbuf_delete(topo); 276107095abfSIvan Voras return (err); 276207095abfSIvan Voras } 2763b67cc292SDavid Xu 276407095abfSIvan Voras #endif 276507095abfSIvan Voras 27669727e637SJeff Roberson SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2767ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 2768e7d50326SJeff Roberson "Scheduler name"); 2769ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 2770ae7a6b38SJeff Roberson "Slice size for timeshare threads"); 2771ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 2772ae7a6b38SJeff Roberson "Interactivity score threshold"); 2773ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh, 2774ae7a6b38SJeff Roberson 0,"Min priority for preemption, lower priorities have greater precedence"); 2775c5aa6b58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 2776c5aa6b58SJeff Roberson 0,"Controls whether static kernel priorities are assigned to sleeping threads."); 27771690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 27781690c6c1SJeff Roberson 0,"Number of times idle will spin waiting for new work."); 27791690c6c1SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh, 27801690c6c1SJeff Roberson 0,"Threshold before we will permit idle spinning."); 27817b8bfa0dSJeff Roberson #ifdef SMP 2782ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 2783ae7a6b38SJeff Roberson "Number of hz ticks to keep thread affinity for"); 2784ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 2785ae7a6b38SJeff Roberson "Enables the long-term load balancer"); 27867fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 27877fcf154aSJeff Roberson &balance_interval, 0, 27887fcf154aSJeff Roberson "Average frequency in stathz ticks to run the long-term balancer"); 2789ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 2790ae7a6b38SJeff Roberson "Attempts to steal work from other cores before idling"); 279128994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 279228994a58SJeff Roberson "Minimum load on remote cpu before we'll steal"); 279307095abfSIvan Voras 279407095abfSIvan Voras /* Retrieve SMP topology */ 279507095abfSIvan Voras SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 279607095abfSIvan Voras CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 279707095abfSIvan Voras "XML dump of detected CPU topology"); 2798b67cc292SDavid Xu 27997b8bfa0dSJeff Roberson #endif 2800e7d50326SJeff Roberson 280154b0e65fSJeff Roberson /* ps compat. All cpu percentages from ULE are weighted. */ 2802a5423ea3SJeff Roberson static int ccpu = 0; 2803e7d50326SJeff Roberson SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 2804