sched_ule.c revision 236141
1109864Sjeff/*- 2165762Sjeff * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org> 3109864Sjeff * All rights reserved. 4109864Sjeff * 5109864Sjeff * Redistribution and use in source and binary forms, with or without 6109864Sjeff * modification, are permitted provided that the following conditions 7109864Sjeff * are met: 8109864Sjeff * 1. Redistributions of source code must retain the above copyright 9109864Sjeff * notice unmodified, this list of conditions, and the following 10109864Sjeff * disclaimer. 11109864Sjeff * 2. Redistributions in binary form must reproduce the above copyright 12109864Sjeff * notice, this list of conditions and the following disclaimer in the 13109864Sjeff * documentation and/or other materials provided with the distribution. 14109864Sjeff * 15109864Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16109864Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17109864Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18109864Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19109864Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20109864Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21109864Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22109864Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23109864Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24109864Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25109864Sjeff */ 26109864Sjeff 27171482Sjeff/* 28171482Sjeff * This file implements the ULE scheduler. ULE supports independent CPU 29171482Sjeff * run queues and fine grain locking. It has superior interactive 30171482Sjeff * performance under load even on uni-processor systems. 31171482Sjeff * 32171482Sjeff * etymology: 33172293Sjeff * ULE is the last three letters in schedule. It owes its name to a 34171482Sjeff * generic user created for a scheduling system by Paul Mikesell at 35171482Sjeff * Isilon Systems and a general lack of creativity on the part of the author. 36171482Sjeff */ 37171482Sjeff 38116182Sobrien#include <sys/cdefs.h> 39191645Sjeff__FBSDID("$FreeBSD: head/sys/kern/sched_ule.c 236141 2012-05-27 10:25:20Z raj $"); 40116182Sobrien 41147565Speter#include "opt_hwpmc_hooks.h" 42179297Sjb#include "opt_kdtrace.h" 43147565Speter#include "opt_sched.h" 44134649Sscottl 45109864Sjeff#include <sys/param.h> 46109864Sjeff#include <sys/systm.h> 47131929Smarcel#include <sys/kdb.h> 48109864Sjeff#include <sys/kernel.h> 49109864Sjeff#include <sys/ktr.h> 50109864Sjeff#include <sys/lock.h> 51109864Sjeff#include <sys/mutex.h> 52109864Sjeff#include <sys/proc.h> 53112966Sjeff#include <sys/resource.h> 54122038Sjeff#include <sys/resourcevar.h> 55109864Sjeff#include <sys/sched.h> 56235459Srstone#include <sys/sdt.h> 57109864Sjeff#include <sys/smp.h> 58109864Sjeff#include <sys/sx.h> 59109864Sjeff#include <sys/sysctl.h> 60109864Sjeff#include <sys/sysproto.h> 61139453Sjhb#include <sys/turnstile.h> 62161599Sdavidxu#include <sys/umtx.h> 63109864Sjeff#include <sys/vmmeter.h> 64176735Sjeff#include <sys/cpuset.h> 65184439Sivoras#include <sys/sbuf.h> 66109864Sjeff 67145256Sjkoshy#ifdef HWPMC_HOOKS 68145256Sjkoshy#include <sys/pmckern.h> 69145256Sjkoshy#endif 70145256Sjkoshy 71179297Sjb#ifdef KDTRACE_HOOKS 72179297Sjb#include <sys/dtrace_bsd.h> 73179297Sjbint dtrace_vtime_active; 74179297Sjbdtrace_vtime_switch_func_t dtrace_vtime_switch_func; 75179297Sjb#endif 76179297Sjb 77109864Sjeff#include <machine/cpu.h> 78121790Sjeff#include <machine/smp.h> 79109864Sjeff 80236141Sraj#if defined(__powerpc__) && defined(BOOKE_E500) 81172345Sjeff#error "This architecture is not currently compatible with ULE" 82166190Sjeff#endif 83166190Sjeff 84171482Sjeff#define KTR_ULE 0 85166137Sjeff 86187679Sjeff#define TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX))) 87187679Sjeff#define TDQ_NAME_LEN (sizeof("sched lock ") + sizeof(__XSTRING(MAXCPU))) 88224221Sattilio#define TDQ_LOADNAME_LEN (sizeof("CPU ") + sizeof(__XSTRING(MAXCPU)) - 1 + sizeof(" load")) 89187357Sjeff 90166137Sjeff/* 91171482Sjeff * Thread scheduler specific section. All fields are protected 92171482Sjeff * by the thread lock. 93146954Sjeff */ 94164936Sjulianstruct td_sched { 95171482Sjeff struct runq *ts_runq; /* Run-queue we're queued on. */ 96171482Sjeff short ts_flags; /* TSF_* flags. */ 97164936Sjulian u_char ts_cpu; /* CPU that we have affinity for. */ 98177009Sjeff int ts_rltick; /* Real last tick, for affinity. */ 99171482Sjeff int ts_slice; /* Ticks of slice remaining. */ 100171482Sjeff u_int ts_slptime; /* Number of ticks we vol. slept */ 101171482Sjeff u_int ts_runtime; /* Number of ticks we were running */ 102164936Sjulian int ts_ltick; /* Last tick that we were running on */ 103164936Sjulian int ts_ftick; /* First tick that we were running on */ 104164936Sjulian int ts_ticks; /* Tick count */ 105187357Sjeff#ifdef KTR 106187357Sjeff char ts_name[TS_NAME_LEN]; 107187357Sjeff#endif 108134791Sjulian}; 109164936Sjulian/* flags kept in ts_flags */ 110166108Sjeff#define TSF_BOUND 0x0001 /* Thread can not migrate. */ 111166108Sjeff#define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */ 112121790Sjeff 113164936Sjulianstatic struct td_sched td_sched0; 114109864Sjeff 115176735Sjeff#define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0) 116176735Sjeff#define THREAD_CAN_SCHED(td, cpu) \ 117176735Sjeff CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask) 118176735Sjeff 119109864Sjeff/* 120217351Sjhb * Priority ranges used for interactive and non-interactive timeshare 121217410Sjhb * threads. The timeshare priorities are split up into four ranges. 122217410Sjhb * The first range handles interactive threads. The last three ranges 123217410Sjhb * (NHALF, x, and NHALF) handle non-interactive threads with the outer 124217410Sjhb * ranges supporting nice values. 125217351Sjhb */ 126217410Sjhb#define PRI_TIMESHARE_RANGE (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE + 1) 127217410Sjhb#define PRI_INTERACT_RANGE ((PRI_TIMESHARE_RANGE - SCHED_PRI_NRESV) / 2) 128228718Savg#define PRI_BATCH_RANGE (PRI_TIMESHARE_RANGE - PRI_INTERACT_RANGE) 129217410Sjhb 130217410Sjhb#define PRI_MIN_INTERACT PRI_MIN_TIMESHARE 131217410Sjhb#define PRI_MAX_INTERACT (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE - 1) 132217410Sjhb#define PRI_MIN_BATCH (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE) 133217351Sjhb#define PRI_MAX_BATCH PRI_MAX_TIMESHARE 134217351Sjhb 135217351Sjhb/* 136165762Sjeff * Cpu percentage computation macros and defines. 137111857Sjeff * 138165762Sjeff * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 139165762Sjeff * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 140165796Sjeff * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 141165762Sjeff * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 142165762Sjeff * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 143165762Sjeff * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 144165762Sjeff */ 145165762Sjeff#define SCHED_TICK_SECS 10 146165762Sjeff#define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 147165796Sjeff#define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 148165762Sjeff#define SCHED_TICK_SHIFT 10 149165762Sjeff#define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 150165830Sjeff#define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 151165762Sjeff 152165762Sjeff/* 153165762Sjeff * These macros determine priorities for non-interactive threads. They are 154165762Sjeff * assigned a priority based on their recent cpu utilization as expressed 155165762Sjeff * by the ratio of ticks to the tick total. NHALF priorities at the start 156165762Sjeff * and end of the MIN to MAX timeshare range are only reachable with negative 157165762Sjeff * or positive nice respectively. 158165762Sjeff * 159165762Sjeff * PRI_RANGE: Priority range for utilization dependent priorities. 160116642Sjeff * PRI_NRESV: Number of nice values. 161165762Sjeff * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 162165762Sjeff * PRI_NICE: Determines the part of the priority inherited from nice. 163109864Sjeff */ 164165762Sjeff#define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 165121869Sjeff#define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 166217351Sjhb#define SCHED_PRI_MIN (PRI_MIN_BATCH + SCHED_PRI_NHALF) 167217351Sjhb#define SCHED_PRI_MAX (PRI_MAX_BATCH - SCHED_PRI_NHALF) 168217237Sjhb#define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1) 169165762Sjeff#define SCHED_PRI_TICKS(ts) \ 170165762Sjeff (SCHED_TICK_HZ((ts)) / \ 171165827Sjeff (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 172165762Sjeff#define SCHED_PRI_NICE(nice) (nice) 173109864Sjeff 174109864Sjeff/* 175165762Sjeff * These determine the interactivity of a process. Interactivity differs from 176165762Sjeff * cpu utilization in that it expresses the voluntary time slept vs time ran 177165762Sjeff * while cpu utilization includes all time not running. This more accurately 178165762Sjeff * models the intent of the thread. 179109864Sjeff * 180110645Sjeff * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 181110645Sjeff * before throttling back. 182121868Sjeff * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 183116365Sjeff * INTERACT_MAX: Maximum interactivity value. Smaller is better. 184215102Sattilio * INTERACT_THRESH: Threshold for placement on the current runq. 185109864Sjeff */ 186165762Sjeff#define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 187165762Sjeff#define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 188116365Sjeff#define SCHED_INTERACT_MAX (100) 189116365Sjeff#define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 190121126Sjeff#define SCHED_INTERACT_THRESH (30) 191111857Sjeff 192109864Sjeff/* 193165762Sjeff * tickincr: Converts a stathz tick into a hz domain scaled by 194165762Sjeff * the shift factor. Without the shift the error rate 195165762Sjeff * due to rounding would be unacceptably high. 196165762Sjeff * realstathz: stathz is sometimes 0 and run off of hz. 197165762Sjeff * sched_slice: Runtime of each thread before rescheduling. 198171482Sjeff * preempt_thresh: Priority threshold for preemption and remote IPIs. 199109864Sjeff */ 200165762Sjeffstatic int sched_interact = SCHED_INTERACT_THRESH; 201165762Sjeffstatic int realstathz; 202165762Sjeffstatic int tickincr; 203177009Sjeffstatic int sched_slice = 1; 204172345Sjeff#ifdef PREEMPTION 205172345Sjeff#ifdef FULL_PREEMPTION 206172345Sjeffstatic int preempt_thresh = PRI_MAX_IDLE; 207172345Sjeff#else 208171482Sjeffstatic int preempt_thresh = PRI_MIN_KERN; 209172345Sjeff#endif 210172345Sjeff#else 211172345Sjeffstatic int preempt_thresh = 0; 212172345Sjeff#endif 213217351Sjhbstatic int static_boost = PRI_MIN_BATCH; 214178277Sjeffstatic int sched_idlespins = 10000; 215232740Smavstatic int sched_idlespinthresh = -1; 216109864Sjeff 217109864Sjeff/* 218171482Sjeff * tdq - per processor runqs and statistics. All fields are protected by the 219171482Sjeff * tdq_lock. The load and lowpri may be accessed without to avoid excess 220171482Sjeff * locking in sched_pickcpu(); 221109864Sjeff */ 222164936Sjulianstruct tdq { 223177009Sjeff /* Ordered to improve efficiency of cpu_search() and switch(). */ 224177009Sjeff struct mtx tdq_lock; /* run queue lock. */ 225176735Sjeff struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 226178277Sjeff volatile int tdq_load; /* Aggregate load. */ 227212416Smav volatile int tdq_cpu_idle; /* cpu_idle() is active. */ 228176735Sjeff int tdq_sysload; /* For loadavg, !ITHD load. */ 229177009Sjeff int tdq_transferable; /* Transferable thread count. */ 230178277Sjeff short tdq_switchcnt; /* Switches this tick. */ 231178277Sjeff short tdq_oldswitchcnt; /* Switches last tick. */ 232177009Sjeff u_char tdq_lowpri; /* Lowest priority thread. */ 233177009Sjeff u_char tdq_ipipending; /* IPI pending. */ 234166557Sjeff u_char tdq_idx; /* Current insert index. */ 235166557Sjeff u_char tdq_ridx; /* Current removal index. */ 236177009Sjeff struct runq tdq_realtime; /* real-time run queue. */ 237177009Sjeff struct runq tdq_timeshare; /* timeshare run queue. */ 238177009Sjeff struct runq tdq_idle; /* Queue of IDLE threads. */ 239187357Sjeff char tdq_name[TDQ_NAME_LEN]; 240187357Sjeff#ifdef KTR 241187357Sjeff char tdq_loadname[TDQ_LOADNAME_LEN]; 242187357Sjeff#endif 243171482Sjeff} __aligned(64); 244109864Sjeff 245178277Sjeff/* Idle thread states and config. */ 246178277Sjeff#define TDQ_RUNNING 1 247178277Sjeff#define TDQ_IDLE 2 248166108Sjeff 249123433Sjeff#ifdef SMP 250184439Sivorasstruct cpu_group *cpu_top; /* CPU topology */ 251123433Sjeff 252176735Sjeff#define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 253176735Sjeff#define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 254166108Sjeff 255123433Sjeff/* 256166108Sjeff * Run-time tunables. 257166108Sjeff */ 258171506Sjeffstatic int rebalance = 1; 259172409Sjeffstatic int balance_interval = 128; /* Default set in sched_initticks(). */ 260166108Sjeffstatic int affinity; 261171506Sjeffstatic int steal_idle = 1; 262171506Sjeffstatic int steal_thresh = 2; 263166108Sjeff 264166108Sjeff/* 265165620Sjeff * One thread queue per processor. 266109864Sjeff */ 267164936Sjulianstatic struct tdq tdq_cpu[MAXCPU]; 268172409Sjeffstatic struct tdq *balance_tdq; 269172409Sjeffstatic int balance_ticks; 270232207Smavstatic DPCPU_DEFINE(uint32_t, randomval); 271129982Sjeff 272164936Sjulian#define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 273164936Sjulian#define TDQ_CPU(x) (&tdq_cpu[(x)]) 274171713Sjeff#define TDQ_ID(x) ((int)((x) - tdq_cpu)) 275123433Sjeff#else /* !SMP */ 276164936Sjulianstatic struct tdq tdq_cpu; 277129982Sjeff 278170315Sjeff#define TDQ_ID(x) (0) 279164936Sjulian#define TDQ_SELF() (&tdq_cpu) 280164936Sjulian#define TDQ_CPU(x) (&tdq_cpu) 281110028Sjeff#endif 282109864Sjeff 283171482Sjeff#define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 284171482Sjeff#define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 285171482Sjeff#define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 286171482Sjeff#define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 287176735Sjeff#define TDQ_LOCKPTR(t) (&(t)->tdq_lock) 288171482Sjeff 289163709Sjbstatic void sched_priority(struct thread *); 290146954Sjeffstatic void sched_thread_priority(struct thread *, u_char); 291163709Sjbstatic int sched_interact_score(struct thread *); 292163709Sjbstatic void sched_interact_update(struct thread *); 293163709Sjbstatic void sched_interact_fork(struct thread *); 294232917Smavstatic void sched_pctcpu_update(struct td_sched *, int); 295109864Sjeff 296110267Sjeff/* Operations on per processor queues */ 297177435Sjeffstatic struct thread *tdq_choose(struct tdq *); 298164936Sjulianstatic void tdq_setup(struct tdq *); 299177435Sjeffstatic void tdq_load_add(struct tdq *, struct thread *); 300177435Sjeffstatic void tdq_load_rem(struct tdq *, struct thread *); 301177435Sjeffstatic __inline void tdq_runq_add(struct tdq *, struct thread *, int); 302177435Sjeffstatic __inline void tdq_runq_rem(struct tdq *, struct thread *); 303177005Sjeffstatic inline int sched_shouldpreempt(int, int, int); 304164936Sjulianvoid tdq_print(int cpu); 305165762Sjeffstatic void runq_print(struct runq *rq); 306171482Sjeffstatic void tdq_add(struct tdq *, struct thread *, int); 307110267Sjeff#ifdef SMP 308176735Sjeffstatic int tdq_move(struct tdq *, struct tdq *); 309171482Sjeffstatic int tdq_idled(struct tdq *); 310177435Sjeffstatic void tdq_notify(struct tdq *, struct thread *); 311177435Sjeffstatic struct thread *tdq_steal(struct tdq *, int); 312177435Sjeffstatic struct thread *runq_steal(struct runq *, int); 313177435Sjeffstatic int sched_pickcpu(struct thread *, int); 314172409Sjeffstatic void sched_balance(void); 315176735Sjeffstatic int sched_balance_pair(struct tdq *, struct tdq *); 316177435Sjeffstatic inline struct tdq *sched_setcpu(struct thread *, int, int); 317171482Sjeffstatic inline void thread_unblock_switch(struct thread *, struct mtx *); 318171713Sjeffstatic struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int); 319184439Sivorasstatic int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 320184439Sivorasstatic int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 321184439Sivoras struct cpu_group *cg, int indent); 322121790Sjeff#endif 323110028Sjeff 324165762Sjeffstatic void sched_setup(void *dummy); 325177253SrwatsonSYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 326165762Sjeff 327165762Sjeffstatic void sched_initticks(void *dummy); 328177253SrwatsonSYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 329177253Srwatson NULL); 330165762Sjeff 331235459SrstoneSDT_PROVIDER_DEFINE(sched); 332235459Srstone 333235459SrstoneSDT_PROBE_DEFINE3(sched, , , change_pri, change-pri, "struct thread *", 334235459Srstone "struct proc *", "uint8_t"); 335235459SrstoneSDT_PROBE_DEFINE3(sched, , , dequeue, dequeue, "struct thread *", 336235459Srstone "struct proc *", "void *"); 337235459SrstoneSDT_PROBE_DEFINE4(sched, , , enqueue, enqueue, "struct thread *", 338235459Srstone "struct proc *", "void *", "int"); 339235459SrstoneSDT_PROBE_DEFINE4(sched, , , lend_pri, lend-pri, "struct thread *", 340235459Srstone "struct proc *", "uint8_t", "struct thread *"); 341235459SrstoneSDT_PROBE_DEFINE2(sched, , , load_change, load-change, "int", "int"); 342235459SrstoneSDT_PROBE_DEFINE2(sched, , , off_cpu, off-cpu, "struct thread *", 343235459Srstone "struct proc *"); 344235459SrstoneSDT_PROBE_DEFINE(sched, , , on_cpu, on-cpu); 345235459SrstoneSDT_PROBE_DEFINE(sched, , , remain_cpu, remain-cpu); 346235459SrstoneSDT_PROBE_DEFINE2(sched, , , surrender, surrender, "struct thread *", 347235459Srstone "struct proc *"); 348235459Srstone 349171482Sjeff/* 350171482Sjeff * Print the threads waiting on a run-queue. 351171482Sjeff */ 352165762Sjeffstatic void 353165762Sjeffrunq_print(struct runq *rq) 354165762Sjeff{ 355165762Sjeff struct rqhead *rqh; 356177435Sjeff struct thread *td; 357165762Sjeff int pri; 358165762Sjeff int j; 359165762Sjeff int i; 360165762Sjeff 361165762Sjeff for (i = 0; i < RQB_LEN; i++) { 362165762Sjeff printf("\t\trunq bits %d 0x%zx\n", 363165762Sjeff i, rq->rq_status.rqb_bits[i]); 364165762Sjeff for (j = 0; j < RQB_BPW; j++) 365165762Sjeff if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 366165762Sjeff pri = j + (i << RQB_L2BPW); 367165762Sjeff rqh = &rq->rq_queues[pri]; 368177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 369165762Sjeff printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 370177435Sjeff td, td->td_name, td->td_priority, 371177435Sjeff td->td_rqindex, pri); 372165762Sjeff } 373165762Sjeff } 374165762Sjeff } 375165762Sjeff} 376165762Sjeff 377171482Sjeff/* 378171482Sjeff * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 379171482Sjeff */ 380113357Sjeffvoid 381164936Sjuliantdq_print(int cpu) 382110267Sjeff{ 383164936Sjulian struct tdq *tdq; 384112994Sjeff 385164936Sjulian tdq = TDQ_CPU(cpu); 386112994Sjeff 387171713Sjeff printf("tdq %d:\n", TDQ_ID(tdq)); 388176735Sjeff printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 389176735Sjeff printf("\tLock name: %s\n", tdq->tdq_name); 390165620Sjeff printf("\tload: %d\n", tdq->tdq_load); 391178277Sjeff printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 392178277Sjeff printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 393171482Sjeff printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 394165766Sjeff printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 395178277Sjeff printf("\tload transferable: %d\n", tdq->tdq_transferable); 396178277Sjeff printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 397165762Sjeff printf("\trealtime runq:\n"); 398165762Sjeff runq_print(&tdq->tdq_realtime); 399165762Sjeff printf("\ttimeshare runq:\n"); 400165762Sjeff runq_print(&tdq->tdq_timeshare); 401165762Sjeff printf("\tidle runq:\n"); 402165762Sjeff runq_print(&tdq->tdq_idle); 403113357Sjeff} 404112994Sjeff 405177005Sjeffstatic inline int 406177005Sjeffsched_shouldpreempt(int pri, int cpri, int remote) 407177005Sjeff{ 408177005Sjeff /* 409177005Sjeff * If the new priority is not better than the current priority there is 410177005Sjeff * nothing to do. 411177005Sjeff */ 412177005Sjeff if (pri >= cpri) 413177005Sjeff return (0); 414177005Sjeff /* 415177005Sjeff * Always preempt idle. 416177005Sjeff */ 417177005Sjeff if (cpri >= PRI_MIN_IDLE) 418177005Sjeff return (1); 419177005Sjeff /* 420177005Sjeff * If preemption is disabled don't preempt others. 421177005Sjeff */ 422177005Sjeff if (preempt_thresh == 0) 423177005Sjeff return (0); 424177005Sjeff /* 425177005Sjeff * Preempt if we exceed the threshold. 426177005Sjeff */ 427177005Sjeff if (pri <= preempt_thresh) 428177005Sjeff return (1); 429177005Sjeff /* 430217351Sjhb * If we're interactive or better and there is non-interactive 431217351Sjhb * or worse running preempt only remote processors. 432177005Sjeff */ 433217351Sjhb if (remote && pri <= PRI_MAX_INTERACT && cpri > PRI_MAX_INTERACT) 434177005Sjeff return (1); 435177005Sjeff return (0); 436177005Sjeff} 437177005Sjeff 438171482Sjeff/* 439171482Sjeff * Add a thread to the actual run-queue. Keeps transferable counts up to 440171482Sjeff * date with what is actually on the run-queue. Selects the correct 441171482Sjeff * queue position for timeshare threads. 442171482Sjeff */ 443122744Sjeffstatic __inline void 444177435Sjefftdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 445122744Sjeff{ 446177435Sjeff struct td_sched *ts; 447177042Sjeff u_char pri; 448177042Sjeff 449171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 450177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 451177009Sjeff 452177435Sjeff pri = td->td_priority; 453177435Sjeff ts = td->td_sched; 454177435Sjeff TD_SET_RUNQ(td); 455177435Sjeff if (THREAD_CAN_MIGRATE(td)) { 456165620Sjeff tdq->tdq_transferable++; 457164936Sjulian ts->ts_flags |= TSF_XFERABLE; 458123433Sjeff } 459217351Sjhb if (pri < PRI_MIN_BATCH) { 460177042Sjeff ts->ts_runq = &tdq->tdq_realtime; 461217351Sjhb } else if (pri <= PRI_MAX_BATCH) { 462177042Sjeff ts->ts_runq = &tdq->tdq_timeshare; 463217351Sjhb KASSERT(pri <= PRI_MAX_BATCH && pri >= PRI_MIN_BATCH, 464165762Sjeff ("Invalid priority %d on timeshare runq", pri)); 465165762Sjeff /* 466165762Sjeff * This queue contains only priorities between MIN and MAX 467165762Sjeff * realtime. Use the whole queue to represent these values. 468165762Sjeff */ 469171713Sjeff if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 470228718Savg pri = RQ_NQS * (pri - PRI_MIN_BATCH) / PRI_BATCH_RANGE; 471165762Sjeff pri = (pri + tdq->tdq_idx) % RQ_NQS; 472165766Sjeff /* 473165766Sjeff * This effectively shortens the queue by one so we 474165766Sjeff * can have a one slot difference between idx and 475165766Sjeff * ridx while we wait for threads to drain. 476165766Sjeff */ 477165766Sjeff if (tdq->tdq_ridx != tdq->tdq_idx && 478165766Sjeff pri == tdq->tdq_ridx) 479167664Sjeff pri = (unsigned char)(pri - 1) % RQ_NQS; 480165762Sjeff } else 481165766Sjeff pri = tdq->tdq_ridx; 482177435Sjeff runq_add_pri(ts->ts_runq, td, pri, flags); 483177042Sjeff return; 484165762Sjeff } else 485177009Sjeff ts->ts_runq = &tdq->tdq_idle; 486177435Sjeff runq_add(ts->ts_runq, td, flags); 487177009Sjeff} 488177009Sjeff 489171482Sjeff/* 490171482Sjeff * Remove a thread from a run-queue. This typically happens when a thread 491171482Sjeff * is selected to run. Running threads are not on the queue and the 492171482Sjeff * transferable count does not reflect them. 493171482Sjeff */ 494122744Sjeffstatic __inline void 495177435Sjefftdq_runq_rem(struct tdq *tdq, struct thread *td) 496122744Sjeff{ 497177435Sjeff struct td_sched *ts; 498177435Sjeff 499177435Sjeff ts = td->td_sched; 500171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 501171482Sjeff KASSERT(ts->ts_runq != NULL, 502177435Sjeff ("tdq_runq_remove: thread %p null ts_runq", td)); 503164936Sjulian if (ts->ts_flags & TSF_XFERABLE) { 504165620Sjeff tdq->tdq_transferable--; 505164936Sjulian ts->ts_flags &= ~TSF_XFERABLE; 506123433Sjeff } 507165766Sjeff if (ts->ts_runq == &tdq->tdq_timeshare) { 508165766Sjeff if (tdq->tdq_idx != tdq->tdq_ridx) 509177435Sjeff runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 510165766Sjeff else 511177435Sjeff runq_remove_idx(ts->ts_runq, td, NULL); 512165766Sjeff } else 513177435Sjeff runq_remove(ts->ts_runq, td); 514122744Sjeff} 515122744Sjeff 516171482Sjeff/* 517171482Sjeff * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 518171482Sjeff * for this thread to the referenced thread queue. 519171482Sjeff */ 520113357Sjeffstatic void 521177435Sjefftdq_load_add(struct tdq *tdq, struct thread *td) 522113357Sjeff{ 523171482Sjeff 524171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 525177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 526177902Sjeff 527165620Sjeff tdq->tdq_load++; 528198854Sattilio if ((td->td_flags & TDF_NOLOAD) == 0) 529177902Sjeff tdq->tdq_sysload++; 530187357Sjeff KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 531235459Srstone SDT_PROBE2(sched, , , load_change, (int)TDQ_ID(tdq), tdq->tdq_load); 532110267Sjeff} 533113357Sjeff 534171482Sjeff/* 535171482Sjeff * Remove the load from a thread that is transitioning to a sleep state or 536171482Sjeff * exiting. 537171482Sjeff */ 538112994Sjeffstatic void 539177435Sjefftdq_load_rem(struct tdq *tdq, struct thread *td) 540110267Sjeff{ 541171482Sjeff 542177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 543171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 544171482Sjeff KASSERT(tdq->tdq_load != 0, 545171713Sjeff ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 546177902Sjeff 547165620Sjeff tdq->tdq_load--; 548198854Sattilio if ((td->td_flags & TDF_NOLOAD) == 0) 549177902Sjeff tdq->tdq_sysload--; 550187357Sjeff KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 551235459Srstone SDT_PROBE2(sched, , , load_change, (int)TDQ_ID(tdq), tdq->tdq_load); 552110267Sjeff} 553110267Sjeff 554176735Sjeff/* 555176735Sjeff * Set lowpri to its exact value by searching the run-queue and 556176735Sjeff * evaluating curthread. curthread may be passed as an optimization. 557176735Sjeff */ 558176735Sjeffstatic void 559176735Sjefftdq_setlowpri(struct tdq *tdq, struct thread *ctd) 560176735Sjeff{ 561176735Sjeff struct thread *td; 562176735Sjeff 563176735Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 564176735Sjeff if (ctd == NULL) 565176735Sjeff ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread; 566177435Sjeff td = tdq_choose(tdq); 567177435Sjeff if (td == NULL || td->td_priority > ctd->td_priority) 568176735Sjeff tdq->tdq_lowpri = ctd->td_priority; 569176735Sjeff else 570176735Sjeff tdq->tdq_lowpri = td->td_priority; 571176735Sjeff} 572176735Sjeff 573113357Sjeff#ifdef SMP 574176735Sjeffstruct cpu_search { 575194779Sjeff cpuset_t cs_mask; 576232207Smav u_int cs_prefer; 577232207Smav int cs_pri; /* Min priority for low. */ 578232207Smav int cs_limit; /* Max load for low, min load for high. */ 579232207Smav int cs_cpu; 580232207Smav int cs_load; 581176735Sjeff}; 582176735Sjeff 583176735Sjeff#define CPU_SEARCH_LOWEST 0x1 584176735Sjeff#define CPU_SEARCH_HIGHEST 0x2 585176735Sjeff#define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST) 586176735Sjeff 587194779Sjeff#define CPUSET_FOREACH(cpu, mask) \ 588194779Sjeff for ((cpu) = 0; (cpu) <= mp_maxid; (cpu)++) \ 589222813Sattilio if (CPU_ISSET(cpu, &mask)) 590176735Sjeff 591232207Smavstatic __inline int cpu_search(const struct cpu_group *cg, struct cpu_search *low, 592176735Sjeff struct cpu_search *high, const int match); 593232207Smavint cpu_search_lowest(const struct cpu_group *cg, struct cpu_search *low); 594232207Smavint cpu_search_highest(const struct cpu_group *cg, struct cpu_search *high); 595232207Smavint cpu_search_both(const struct cpu_group *cg, struct cpu_search *low, 596176735Sjeff struct cpu_search *high); 597176735Sjeff 598116069Sjeff/* 599176735Sjeff * Search the tree of cpu_groups for the lowest or highest loaded cpu 600176735Sjeff * according to the match argument. This routine actually compares the 601176735Sjeff * load on all paths through the tree and finds the least loaded cpu on 602176735Sjeff * the least loaded path, which may differ from the least loaded cpu in 603176735Sjeff * the system. This balances work among caches and busses. 604116069Sjeff * 605176735Sjeff * This inline is instantiated in three forms below using constants for the 606176735Sjeff * match argument. It is reduced to the minimum set for each case. It is 607176735Sjeff * also recursive to the depth of the tree. 608116069Sjeff */ 609177169Sjhbstatic __inline int 610232207Smavcpu_search(const struct cpu_group *cg, struct cpu_search *low, 611176735Sjeff struct cpu_search *high, const int match) 612176735Sjeff{ 613232207Smav struct cpu_search lgroup; 614232207Smav struct cpu_search hgroup; 615232207Smav cpuset_t cpumask; 616232207Smav struct cpu_group *child; 617232207Smav struct tdq *tdq; 618234066Smav int cpu, i, hload, lload, load, total, rnd, *rndptr; 619176735Sjeff 620176735Sjeff total = 0; 621232207Smav cpumask = cg->cg_mask; 622232207Smav if (match & CPU_SEARCH_LOWEST) { 623232207Smav lload = INT_MAX; 624232207Smav lgroup = *low; 625232207Smav } 626232207Smav if (match & CPU_SEARCH_HIGHEST) { 627234066Smav hload = INT_MIN; 628232207Smav hgroup = *high; 629232207Smav } 630176735Sjeff 631232207Smav /* Iterate through the child CPU groups and then remaining CPUs. */ 632234066Smav for (i = cg->cg_children, cpu = mp_maxid; i >= 0; ) { 633234066Smav if (i == 0) { 634234066Smav while (cpu >= 0 && !CPU_ISSET(cpu, &cpumask)) 635234066Smav cpu--; 636234066Smav if (cpu < 0) 637232207Smav break; 638232207Smav child = NULL; 639232207Smav } else 640234066Smav child = &cg->cg_child[i - 1]; 641232207Smav 642234066Smav if (match & CPU_SEARCH_LOWEST) 643234066Smav lgroup.cs_cpu = -1; 644234066Smav if (match & CPU_SEARCH_HIGHEST) 645234066Smav hgroup.cs_cpu = -1; 646232207Smav if (child) { /* Handle child CPU group. */ 647232207Smav CPU_NAND(&cpumask, &child->cg_mask); 648176735Sjeff switch (match) { 649176735Sjeff case CPU_SEARCH_LOWEST: 650176735Sjeff load = cpu_search_lowest(child, &lgroup); 651176735Sjeff break; 652176735Sjeff case CPU_SEARCH_HIGHEST: 653176735Sjeff load = cpu_search_highest(child, &hgroup); 654176735Sjeff break; 655176735Sjeff case CPU_SEARCH_BOTH: 656176735Sjeff load = cpu_search_both(child, &lgroup, &hgroup); 657176735Sjeff break; 658176735Sjeff } 659232207Smav } else { /* Handle child CPU. */ 660232207Smav tdq = TDQ_CPU(cpu); 661232207Smav load = tdq->tdq_load * 256; 662234066Smav rndptr = DPCPU_PTR(randomval); 663234066Smav rnd = (*rndptr = *rndptr * 69069 + 5) >> 26; 664232207Smav if (match & CPU_SEARCH_LOWEST) { 665232207Smav if (cpu == low->cs_prefer) 666232207Smav load -= 64; 667232207Smav /* If that CPU is allowed and get data. */ 668234066Smav if (tdq->tdq_lowpri > lgroup.cs_pri && 669234066Smav tdq->tdq_load <= lgroup.cs_limit && 670234066Smav CPU_ISSET(cpu, &lgroup.cs_mask)) { 671232207Smav lgroup.cs_cpu = cpu; 672232207Smav lgroup.cs_load = load - rnd; 673176735Sjeff } 674232207Smav } 675232207Smav if (match & CPU_SEARCH_HIGHEST) 676234066Smav if (tdq->tdq_load >= hgroup.cs_limit && 677234066Smav tdq->tdq_transferable && 678234066Smav CPU_ISSET(cpu, &hgroup.cs_mask)) { 679232207Smav hgroup.cs_cpu = cpu; 680232207Smav hgroup.cs_load = load - rnd; 681176735Sjeff } 682176735Sjeff } 683232207Smav total += load; 684176735Sjeff 685232207Smav /* We have info about child item. Compare it. */ 686232207Smav if (match & CPU_SEARCH_LOWEST) { 687234066Smav if (lgroup.cs_cpu >= 0 && 688232454Smav (load < lload || 689232454Smav (load == lload && lgroup.cs_load < low->cs_load))) { 690232207Smav lload = load; 691232207Smav low->cs_cpu = lgroup.cs_cpu; 692232207Smav low->cs_load = lgroup.cs_load; 693232207Smav } 694232207Smav } 695232207Smav if (match & CPU_SEARCH_HIGHEST) 696234066Smav if (hgroup.cs_cpu >= 0 && 697232454Smav (load > hload || 698232454Smav (load == hload && hgroup.cs_load > high->cs_load))) { 699232207Smav hload = load; 700232207Smav high->cs_cpu = hgroup.cs_cpu; 701232207Smav high->cs_load = hgroup.cs_load; 702232207Smav } 703234066Smav if (child) { 704234066Smav i--; 705234066Smav if (i == 0 && CPU_EMPTY(&cpumask)) 706234066Smav break; 707234066Smav } else 708234066Smav cpu--; 709176735Sjeff } 710176735Sjeff return (total); 711176735Sjeff} 712176735Sjeff 713176735Sjeff/* 714176735Sjeff * cpu_search instantiations must pass constants to maintain the inline 715176735Sjeff * optimization. 716176735Sjeff */ 717176735Sjeffint 718232207Smavcpu_search_lowest(const struct cpu_group *cg, struct cpu_search *low) 719176735Sjeff{ 720176735Sjeff return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST); 721176735Sjeff} 722176735Sjeff 723176735Sjeffint 724232207Smavcpu_search_highest(const struct cpu_group *cg, struct cpu_search *high) 725176735Sjeff{ 726176735Sjeff return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST); 727176735Sjeff} 728176735Sjeff 729176735Sjeffint 730232207Smavcpu_search_both(const struct cpu_group *cg, struct cpu_search *low, 731176735Sjeff struct cpu_search *high) 732176735Sjeff{ 733176735Sjeff return cpu_search(cg, low, high, CPU_SEARCH_BOTH); 734176735Sjeff} 735176735Sjeff 736176735Sjeff/* 737176735Sjeff * Find the cpu with the least load via the least loaded path that has a 738176735Sjeff * lowpri greater than pri pri. A pri of -1 indicates any priority is 739176735Sjeff * acceptable. 740176735Sjeff */ 741176735Sjeffstatic inline int 742232207Smavsched_lowest(const struct cpu_group *cg, cpuset_t mask, int pri, int maxload, 743232207Smav int prefer) 744176735Sjeff{ 745176735Sjeff struct cpu_search low; 746176735Sjeff 747176735Sjeff low.cs_cpu = -1; 748232207Smav low.cs_prefer = prefer; 749176735Sjeff low.cs_mask = mask; 750232207Smav low.cs_pri = pri; 751232207Smav low.cs_limit = maxload; 752176735Sjeff cpu_search_lowest(cg, &low); 753176735Sjeff return low.cs_cpu; 754176735Sjeff} 755176735Sjeff 756176735Sjeff/* 757176735Sjeff * Find the cpu with the highest load via the highest loaded path. 758176735Sjeff */ 759176735Sjeffstatic inline int 760232207Smavsched_highest(const struct cpu_group *cg, cpuset_t mask, int minload) 761176735Sjeff{ 762176735Sjeff struct cpu_search high; 763176735Sjeff 764176735Sjeff high.cs_cpu = -1; 765176735Sjeff high.cs_mask = mask; 766176735Sjeff high.cs_limit = minload; 767176735Sjeff cpu_search_highest(cg, &high); 768176735Sjeff return high.cs_cpu; 769176735Sjeff} 770176735Sjeff 771176735Sjeff/* 772176735Sjeff * Simultaneously find the highest and lowest loaded cpu reachable via 773176735Sjeff * cg. 774176735Sjeff */ 775232207Smavstatic inline void 776232207Smavsched_both(const struct cpu_group *cg, cpuset_t mask, int *lowcpu, int *highcpu) 777176735Sjeff{ 778176735Sjeff struct cpu_search high; 779176735Sjeff struct cpu_search low; 780176735Sjeff 781176735Sjeff low.cs_cpu = -1; 782232207Smav low.cs_prefer = -1; 783232207Smav low.cs_pri = -1; 784232207Smav low.cs_limit = INT_MAX; 785176735Sjeff low.cs_mask = mask; 786176735Sjeff high.cs_cpu = -1; 787176735Sjeff high.cs_limit = -1; 788176735Sjeff high.cs_mask = mask; 789176735Sjeff cpu_search_both(cg, &low, &high); 790176735Sjeff *lowcpu = low.cs_cpu; 791176735Sjeff *highcpu = high.cs_cpu; 792176735Sjeff return; 793176735Sjeff} 794176735Sjeff 795121790Sjeffstatic void 796176735Sjeffsched_balance_group(struct cpu_group *cg) 797116069Sjeff{ 798232207Smav cpuset_t hmask, lmask; 799232207Smav int high, low, anylow; 800123487Sjeff 801232207Smav CPU_FILL(&hmask); 802176735Sjeff for (;;) { 803232207Smav high = sched_highest(cg, hmask, 1); 804232207Smav /* Stop if there is no more CPU with transferrable threads. */ 805232207Smav if (high == -1) 806176735Sjeff break; 807232207Smav CPU_CLR(high, &hmask); 808232207Smav CPU_COPY(&hmask, &lmask); 809232207Smav /* Stop if there is no more CPU left for low. */ 810232207Smav if (CPU_EMPTY(&lmask)) 811176735Sjeff break; 812232207Smav anylow = 1; 813232207Smavnextlow: 814232207Smav low = sched_lowest(cg, lmask, -1, 815232207Smav TDQ_CPU(high)->tdq_load - 1, high); 816232207Smav /* Stop if we looked well and found no less loaded CPU. */ 817232207Smav if (anylow && low == -1) 818232207Smav break; 819232207Smav /* Go to next high if we found no less loaded CPU. */ 820232207Smav if (low == -1) 821232207Smav continue; 822232207Smav /* Transfer thread from high to low. */ 823232207Smav if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low))) { 824232207Smav /* CPU that got thread can no longer be a donor. */ 825232207Smav CPU_CLR(low, &hmask); 826232207Smav } else { 827232207Smav /* 828232207Smav * If failed, then there is no threads on high 829232207Smav * that can run on this low. Drop low from low 830232207Smav * mask and look for different one. 831232207Smav */ 832232207Smav CPU_CLR(low, &lmask); 833232207Smav anylow = 0; 834232207Smav goto nextlow; 835232207Smav } 836123487Sjeff } 837123487Sjeff} 838123487Sjeff 839123487Sjeffstatic void 840201148Sedsched_balance(void) 841123487Sjeff{ 842172409Sjeff struct tdq *tdq; 843123487Sjeff 844172409Sjeff /* 845172409Sjeff * Select a random time between .5 * balance_interval and 846172409Sjeff * 1.5 * balance_interval. 847172409Sjeff */ 848176735Sjeff balance_ticks = max(balance_interval / 2, 1); 849176735Sjeff balance_ticks += random() % balance_interval; 850171482Sjeff if (smp_started == 0 || rebalance == 0) 851171482Sjeff return; 852172409Sjeff tdq = TDQ_SELF(); 853172409Sjeff TDQ_UNLOCK(tdq); 854176735Sjeff sched_balance_group(cpu_top); 855172409Sjeff TDQ_LOCK(tdq); 856123487Sjeff} 857123487Sjeff 858171482Sjeff/* 859171482Sjeff * Lock two thread queues using their address to maintain lock order. 860171482Sjeff */ 861123487Sjeffstatic void 862171482Sjefftdq_lock_pair(struct tdq *one, struct tdq *two) 863171482Sjeff{ 864171482Sjeff if (one < two) { 865171482Sjeff TDQ_LOCK(one); 866171482Sjeff TDQ_LOCK_FLAGS(two, MTX_DUPOK); 867171482Sjeff } else { 868171482Sjeff TDQ_LOCK(two); 869171482Sjeff TDQ_LOCK_FLAGS(one, MTX_DUPOK); 870171482Sjeff } 871171482Sjeff} 872171482Sjeff 873171482Sjeff/* 874172409Sjeff * Unlock two thread queues. Order is not important here. 875172409Sjeff */ 876172409Sjeffstatic void 877172409Sjefftdq_unlock_pair(struct tdq *one, struct tdq *two) 878172409Sjeff{ 879172409Sjeff TDQ_UNLOCK(one); 880172409Sjeff TDQ_UNLOCK(two); 881172409Sjeff} 882172409Sjeff 883172409Sjeff/* 884171482Sjeff * Transfer load between two imbalanced thread queues. 885171482Sjeff */ 886176735Sjeffstatic int 887164936Sjuliansched_balance_pair(struct tdq *high, struct tdq *low) 888123487Sjeff{ 889176735Sjeff int moved; 890226057Smarius int cpu; 891116069Sjeff 892171482Sjeff tdq_lock_pair(high, low); 893176735Sjeff moved = 0; 894116069Sjeff /* 895122744Sjeff * Determine what the imbalance is and then adjust that to how many 896165620Sjeff * threads we actually have to give up (transferable). 897122744Sjeff */ 898232207Smav if (high->tdq_transferable != 0 && high->tdq_load > low->tdq_load && 899232207Smav (moved = tdq_move(high, low)) > 0) { 900172293Sjeff /* 901226057Smarius * In case the target isn't the current cpu IPI it to force a 902226057Smarius * reschedule with the new workload. 903172293Sjeff */ 904226057Smarius cpu = TDQ_ID(low); 905226057Smarius sched_pin(); 906226057Smarius if (cpu != PCPU_GET(cpuid)) 907226057Smarius ipi_cpu(cpu, IPI_PREEMPT); 908226057Smarius sched_unpin(); 909171482Sjeff } 910172409Sjeff tdq_unlock_pair(high, low); 911176735Sjeff return (moved); 912116069Sjeff} 913116069Sjeff 914171482Sjeff/* 915171482Sjeff * Move a thread from one thread queue to another. 916171482Sjeff */ 917176735Sjeffstatic int 918171482Sjefftdq_move(struct tdq *from, struct tdq *to) 919116069Sjeff{ 920171482Sjeff struct td_sched *ts; 921171482Sjeff struct thread *td; 922164936Sjulian struct tdq *tdq; 923171482Sjeff int cpu; 924116069Sjeff 925172409Sjeff TDQ_LOCK_ASSERT(from, MA_OWNED); 926172409Sjeff TDQ_LOCK_ASSERT(to, MA_OWNED); 927172409Sjeff 928164936Sjulian tdq = from; 929171482Sjeff cpu = TDQ_ID(to); 930177435Sjeff td = tdq_steal(tdq, cpu); 931177435Sjeff if (td == NULL) 932176735Sjeff return (0); 933177435Sjeff ts = td->td_sched; 934171482Sjeff /* 935171482Sjeff * Although the run queue is locked the thread may be blocked. Lock 936172409Sjeff * it to clear this and acquire the run-queue lock. 937171482Sjeff */ 938171482Sjeff thread_lock(td); 939172409Sjeff /* Drop recursive lock on from acquired via thread_lock(). */ 940171482Sjeff TDQ_UNLOCK(from); 941171482Sjeff sched_rem(td); 942166108Sjeff ts->ts_cpu = cpu; 943171482Sjeff td->td_lock = TDQ_LOCKPTR(to); 944171482Sjeff tdq_add(to, td, SRQ_YIELDING); 945176735Sjeff return (1); 946116069Sjeff} 947110267Sjeff 948171482Sjeff/* 949171482Sjeff * This tdq has idled. Try to steal a thread from another cpu and switch 950171482Sjeff * to it. 951171482Sjeff */ 952123433Sjeffstatic int 953164936Sjuliantdq_idled(struct tdq *tdq) 954121790Sjeff{ 955176735Sjeff struct cpu_group *cg; 956164936Sjulian struct tdq *steal; 957194779Sjeff cpuset_t mask; 958176735Sjeff int thresh; 959171482Sjeff int cpu; 960123433Sjeff 961172484Sjeff if (smp_started == 0 || steal_idle == 0) 962172484Sjeff return (1); 963194779Sjeff CPU_FILL(&mask); 964194779Sjeff CPU_CLR(PCPU_GET(cpuid), &mask); 965176735Sjeff /* We don't want to be preempted while we're iterating. */ 966171482Sjeff spinlock_enter(); 967176735Sjeff for (cg = tdq->tdq_cg; cg != NULL; ) { 968191643Sjeff if ((cg->cg_flags & CG_FLAG_THREAD) == 0) 969176735Sjeff thresh = steal_thresh; 970176735Sjeff else 971176735Sjeff thresh = 1; 972176735Sjeff cpu = sched_highest(cg, mask, thresh); 973176735Sjeff if (cpu == -1) { 974176735Sjeff cg = cg->cg_parent; 975176735Sjeff continue; 976166108Sjeff } 977176735Sjeff steal = TDQ_CPU(cpu); 978194779Sjeff CPU_CLR(cpu, &mask); 979176735Sjeff tdq_lock_pair(tdq, steal); 980176735Sjeff if (steal->tdq_load < thresh || steal->tdq_transferable == 0) { 981176735Sjeff tdq_unlock_pair(tdq, steal); 982176735Sjeff continue; 983171482Sjeff } 984176735Sjeff /* 985176735Sjeff * If a thread was added while interrupts were disabled don't 986176735Sjeff * steal one here. If we fail to acquire one due to affinity 987176735Sjeff * restrictions loop again with this cpu removed from the 988176735Sjeff * set. 989176735Sjeff */ 990176735Sjeff if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) { 991176735Sjeff tdq_unlock_pair(tdq, steal); 992176735Sjeff continue; 993176735Sjeff } 994176735Sjeff spinlock_exit(); 995176735Sjeff TDQ_UNLOCK(steal); 996178272Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 997176735Sjeff thread_unlock(curthread); 998176735Sjeff 999176735Sjeff return (0); 1000123433Sjeff } 1001171482Sjeff spinlock_exit(); 1002123433Sjeff return (1); 1003121790Sjeff} 1004121790Sjeff 1005171482Sjeff/* 1006171482Sjeff * Notify a remote cpu of new work. Sends an IPI if criteria are met. 1007171482Sjeff */ 1008121790Sjeffstatic void 1009177435Sjefftdq_notify(struct tdq *tdq, struct thread *td) 1010121790Sjeff{ 1011185047Sjhb struct thread *ctd; 1012166247Sjeff int pri; 1013166108Sjeff int cpu; 1014121790Sjeff 1015177005Sjeff if (tdq->tdq_ipipending) 1016177005Sjeff return; 1017177435Sjeff cpu = td->td_sched->ts_cpu; 1018177435Sjeff pri = td->td_priority; 1019185047Sjhb ctd = pcpu_find(cpu)->pc_curthread; 1020185047Sjhb if (!sched_shouldpreempt(pri, ctd->td_priority, 1)) 1021166137Sjeff return; 1022185047Sjhb if (TD_IS_IDLETHREAD(ctd)) { 1023178277Sjeff /* 1024178471Sjeff * If the MD code has an idle wakeup routine try that before 1025178471Sjeff * falling back to IPI. 1026178471Sjeff */ 1027212416Smav if (!tdq->tdq_cpu_idle || cpu_idle_wakeup(cpu)) 1028178471Sjeff return; 1029178277Sjeff } 1030177005Sjeff tdq->tdq_ipipending = 1; 1031210939Sjhb ipi_cpu(cpu, IPI_PREEMPT); 1032121790Sjeff} 1033121790Sjeff 1034171482Sjeff/* 1035171482Sjeff * Steals load from a timeshare queue. Honors the rotating queue head 1036171482Sjeff * index. 1037171482Sjeff */ 1038177435Sjeffstatic struct thread * 1039176735Sjeffrunq_steal_from(struct runq *rq, int cpu, u_char start) 1040171482Sjeff{ 1041171482Sjeff struct rqbits *rqb; 1042171482Sjeff struct rqhead *rqh; 1043232207Smav struct thread *td, *first; 1044171482Sjeff int bit; 1045171482Sjeff int pri; 1046171482Sjeff int i; 1047171482Sjeff 1048171482Sjeff rqb = &rq->rq_status; 1049171482Sjeff bit = start & (RQB_BPW -1); 1050171482Sjeff pri = 0; 1051232207Smav first = NULL; 1052171482Sjeffagain: 1053171482Sjeff for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 1054171482Sjeff if (rqb->rqb_bits[i] == 0) 1055171482Sjeff continue; 1056171482Sjeff if (bit != 0) { 1057171482Sjeff for (pri = bit; pri < RQB_BPW; pri++) 1058171482Sjeff if (rqb->rqb_bits[i] & (1ul << pri)) 1059171482Sjeff break; 1060171482Sjeff if (pri >= RQB_BPW) 1061171482Sjeff continue; 1062171482Sjeff } else 1063171482Sjeff pri = RQB_FFS(rqb->rqb_bits[i]); 1064171482Sjeff pri += (i << RQB_L2BPW); 1065171482Sjeff rqh = &rq->rq_queues[pri]; 1066177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 1067177435Sjeff if (first && THREAD_CAN_MIGRATE(td) && 1068177435Sjeff THREAD_CAN_SCHED(td, cpu)) 1069177435Sjeff return (td); 1070232207Smav first = td; 1071171482Sjeff } 1072171482Sjeff } 1073171482Sjeff if (start != 0) { 1074171482Sjeff start = 0; 1075171482Sjeff goto again; 1076171482Sjeff } 1077171482Sjeff 1078232207Smav if (first && THREAD_CAN_MIGRATE(first) && 1079232207Smav THREAD_CAN_SCHED(first, cpu)) 1080232207Smav return (first); 1081171482Sjeff return (NULL); 1082171482Sjeff} 1083171482Sjeff 1084171482Sjeff/* 1085171482Sjeff * Steals load from a standard linear queue. 1086171482Sjeff */ 1087177435Sjeffstatic struct thread * 1088176735Sjeffrunq_steal(struct runq *rq, int cpu) 1089121790Sjeff{ 1090121790Sjeff struct rqhead *rqh; 1091121790Sjeff struct rqbits *rqb; 1092177435Sjeff struct thread *td; 1093121790Sjeff int word; 1094121790Sjeff int bit; 1095121790Sjeff 1096121790Sjeff rqb = &rq->rq_status; 1097121790Sjeff for (word = 0; word < RQB_LEN; word++) { 1098121790Sjeff if (rqb->rqb_bits[word] == 0) 1099121790Sjeff continue; 1100121790Sjeff for (bit = 0; bit < RQB_BPW; bit++) { 1101123231Speter if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 1102121790Sjeff continue; 1103121790Sjeff rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 1104177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) 1105177435Sjeff if (THREAD_CAN_MIGRATE(td) && 1106177435Sjeff THREAD_CAN_SCHED(td, cpu)) 1107177435Sjeff return (td); 1108121790Sjeff } 1109121790Sjeff } 1110121790Sjeff return (NULL); 1111121790Sjeff} 1112121790Sjeff 1113171482Sjeff/* 1114171482Sjeff * Attempt to steal a thread in priority order from a thread queue. 1115171482Sjeff */ 1116177435Sjeffstatic struct thread * 1117176735Sjefftdq_steal(struct tdq *tdq, int cpu) 1118121790Sjeff{ 1119177435Sjeff struct thread *td; 1120121790Sjeff 1121171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 1122177435Sjeff if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 1123177435Sjeff return (td); 1124177435Sjeff if ((td = runq_steal_from(&tdq->tdq_timeshare, 1125177435Sjeff cpu, tdq->tdq_ridx)) != NULL) 1126177435Sjeff return (td); 1127176735Sjeff return (runq_steal(&tdq->tdq_idle, cpu)); 1128121790Sjeff} 1129123433Sjeff 1130171482Sjeff/* 1131171482Sjeff * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 1132172409Sjeff * current lock and returns with the assigned queue locked. 1133171482Sjeff */ 1134171482Sjeffstatic inline struct tdq * 1135177435Sjeffsched_setcpu(struct thread *td, int cpu, int flags) 1136123433Sjeff{ 1137177435Sjeff 1138171482Sjeff struct tdq *tdq; 1139123433Sjeff 1140177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1141171482Sjeff tdq = TDQ_CPU(cpu); 1142177435Sjeff td->td_sched->ts_cpu = cpu; 1143177435Sjeff /* 1144177435Sjeff * If the lock matches just return the queue. 1145177435Sjeff */ 1146171482Sjeff if (td->td_lock == TDQ_LOCKPTR(tdq)) 1147171482Sjeff return (tdq); 1148171482Sjeff#ifdef notyet 1149123433Sjeff /* 1150172293Sjeff * If the thread isn't running its lockptr is a 1151171482Sjeff * turnstile or a sleepqueue. We can just lock_set without 1152171482Sjeff * blocking. 1153123685Sjeff */ 1154171482Sjeff if (TD_CAN_RUN(td)) { 1155171482Sjeff TDQ_LOCK(tdq); 1156171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 1157171482Sjeff return (tdq); 1158171482Sjeff } 1159171482Sjeff#endif 1160166108Sjeff /* 1161171482Sjeff * The hard case, migration, we need to block the thread first to 1162171482Sjeff * prevent order reversals with other cpus locks. 1163166108Sjeff */ 1164202889Sattilio spinlock_enter(); 1165171482Sjeff thread_lock_block(td); 1166171482Sjeff TDQ_LOCK(tdq); 1167171713Sjeff thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1168202889Sattilio spinlock_exit(); 1169171482Sjeff return (tdq); 1170166108Sjeff} 1171166108Sjeff 1172178272SjeffSCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 1173178272SjeffSCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 1174178272SjeffSCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 1175178272SjeffSCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 1176178272SjeffSCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 1177178272SjeffSCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 1178178272Sjeff 1179166108Sjeffstatic int 1180177435Sjeffsched_pickcpu(struct thread *td, int flags) 1181171482Sjeff{ 1182232207Smav struct cpu_group *cg, *ccg; 1183177435Sjeff struct td_sched *ts; 1184171482Sjeff struct tdq *tdq; 1185194779Sjeff cpuset_t mask; 1186232207Smav int cpu, pri, self; 1187166108Sjeff 1188176735Sjeff self = PCPU_GET(cpuid); 1189177435Sjeff ts = td->td_sched; 1190166108Sjeff if (smp_started == 0) 1191166108Sjeff return (self); 1192171506Sjeff /* 1193171506Sjeff * Don't migrate a running thread from sched_switch(). 1194171506Sjeff */ 1195176735Sjeff if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 1196176735Sjeff return (ts->ts_cpu); 1197166108Sjeff /* 1198176735Sjeff * Prefer to run interrupt threads on the processors that generate 1199176735Sjeff * the interrupt. 1200166108Sjeff */ 1201232207Smav pri = td->td_priority; 1202176735Sjeff if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 1203178272Sjeff curthread->td_intr_nesting_level && ts->ts_cpu != self) { 1204178272Sjeff SCHED_STAT_INC(pickcpu_intrbind); 1205176735Sjeff ts->ts_cpu = self; 1206232207Smav if (TDQ_CPU(self)->tdq_lowpri > pri) { 1207232207Smav SCHED_STAT_INC(pickcpu_affinity); 1208232207Smav return (ts->ts_cpu); 1209232207Smav } 1210178272Sjeff } 1211166108Sjeff /* 1212176735Sjeff * If the thread can run on the last cpu and the affinity has not 1213176735Sjeff * expired or it is idle run it there. 1214166108Sjeff */ 1215176735Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1216232207Smav cg = tdq->tdq_cg; 1217232207Smav if (THREAD_CAN_SCHED(td, ts->ts_cpu) && 1218232207Smav tdq->tdq_lowpri >= PRI_MIN_IDLE && 1219232207Smav SCHED_AFFINITY(ts, CG_SHARE_L2)) { 1220232207Smav if (cg->cg_flags & CG_FLAG_THREAD) { 1221232207Smav CPUSET_FOREACH(cpu, cg->cg_mask) { 1222232207Smav if (TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) 1223232207Smav break; 1224232207Smav } 1225232207Smav } else 1226232207Smav cpu = INT_MAX; 1227232207Smav if (cpu > mp_maxid) { 1228178272Sjeff SCHED_STAT_INC(pickcpu_idle_affinity); 1229176735Sjeff return (ts->ts_cpu); 1230178272Sjeff } 1231139334Sjeff } 1232123433Sjeff /* 1233232207Smav * Search for the last level cache CPU group in the tree. 1234232207Smav * Skip caches with expired affinity time and SMT groups. 1235232207Smav * Affinity to higher level caches will be handled less aggressively. 1236123433Sjeff */ 1237232207Smav for (ccg = NULL; cg != NULL; cg = cg->cg_parent) { 1238232207Smav if (cg->cg_flags & CG_FLAG_THREAD) 1239232207Smav continue; 1240232207Smav if (!SCHED_AFFINITY(ts, cg->cg_level)) 1241232207Smav continue; 1242232207Smav ccg = cg; 1243232207Smav } 1244232207Smav if (ccg != NULL) 1245232207Smav cg = ccg; 1246176735Sjeff cpu = -1; 1247232207Smav /* Search the group for the less loaded idle CPU we can run now. */ 1248194779Sjeff mask = td->td_cpuset->cs_mask; 1249232207Smav if (cg != NULL && cg != cpu_top && 1250232207Smav CPU_CMP(&cg->cg_mask, &cpu_top->cg_mask) != 0) 1251232207Smav cpu = sched_lowest(cg, mask, max(pri, PRI_MAX_TIMESHARE), 1252232207Smav INT_MAX, ts->ts_cpu); 1253232207Smav /* Search globally for the less loaded CPU we can run now. */ 1254176735Sjeff if (cpu == -1) 1255232207Smav cpu = sched_lowest(cpu_top, mask, pri, INT_MAX, ts->ts_cpu); 1256232207Smav /* Search globally for the less loaded CPU. */ 1257232207Smav if (cpu == -1) 1258232207Smav cpu = sched_lowest(cpu_top, mask, -1, INT_MAX, ts->ts_cpu); 1259232454Smav KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu.")); 1260171506Sjeff /* 1261176735Sjeff * Compare the lowest loaded cpu to current cpu. 1262171506Sjeff */ 1263177005Sjeff if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri && 1264232207Smav TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE && 1265232207Smav TDQ_CPU(self)->tdq_load <= TDQ_CPU(cpu)->tdq_load + 1) { 1266178272Sjeff SCHED_STAT_INC(pickcpu_local); 1267177005Sjeff cpu = self; 1268178272Sjeff } else 1269178272Sjeff SCHED_STAT_INC(pickcpu_lowest); 1270178272Sjeff if (cpu != ts->ts_cpu) 1271178272Sjeff SCHED_STAT_INC(pickcpu_migration); 1272171482Sjeff return (cpu); 1273123433Sjeff} 1274176735Sjeff#endif 1275123433Sjeff 1276117326Sjeff/* 1277121790Sjeff * Pick the highest priority task we have and return it. 1278117326Sjeff */ 1279177435Sjeffstatic struct thread * 1280164936Sjuliantdq_choose(struct tdq *tdq) 1281110267Sjeff{ 1282177435Sjeff struct thread *td; 1283110267Sjeff 1284171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 1285177435Sjeff td = runq_choose(&tdq->tdq_realtime); 1286177435Sjeff if (td != NULL) 1287177435Sjeff return (td); 1288177435Sjeff td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 1289177435Sjeff if (td != NULL) { 1290217351Sjhb KASSERT(td->td_priority >= PRI_MIN_BATCH, 1291165762Sjeff ("tdq_choose: Invalid priority on timeshare queue %d", 1292177435Sjeff td->td_priority)); 1293177435Sjeff return (td); 1294165762Sjeff } 1295177435Sjeff td = runq_choose(&tdq->tdq_idle); 1296177435Sjeff if (td != NULL) { 1297177435Sjeff KASSERT(td->td_priority >= PRI_MIN_IDLE, 1298165762Sjeff ("tdq_choose: Invalid priority on idle queue %d", 1299177435Sjeff td->td_priority)); 1300177435Sjeff return (td); 1301165762Sjeff } 1302165762Sjeff 1303165762Sjeff return (NULL); 1304110267Sjeff} 1305110267Sjeff 1306171482Sjeff/* 1307171482Sjeff * Initialize a thread queue. 1308171482Sjeff */ 1309109864Sjeffstatic void 1310164936Sjuliantdq_setup(struct tdq *tdq) 1311110028Sjeff{ 1312171482Sjeff 1313171713Sjeff if (bootverbose) 1314171713Sjeff printf("ULE: setup cpu %d\n", TDQ_ID(tdq)); 1315165762Sjeff runq_init(&tdq->tdq_realtime); 1316165762Sjeff runq_init(&tdq->tdq_timeshare); 1317165620Sjeff runq_init(&tdq->tdq_idle); 1318176735Sjeff snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 1319176735Sjeff "sched lock %d", (int)TDQ_ID(tdq)); 1320176735Sjeff mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", 1321176735Sjeff MTX_SPIN | MTX_RECURSE); 1322187357Sjeff#ifdef KTR 1323187357Sjeff snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 1324187357Sjeff "CPU %d load", (int)TDQ_ID(tdq)); 1325187357Sjeff#endif 1326110028Sjeff} 1327110028Sjeff 1328171713Sjeff#ifdef SMP 1329110028Sjeffstatic void 1330171713Sjeffsched_setup_smp(void) 1331171713Sjeff{ 1332171713Sjeff struct tdq *tdq; 1333171713Sjeff int i; 1334171713Sjeff 1335176735Sjeff cpu_top = smp_topo(); 1336209059Sjhb CPU_FOREACH(i) { 1337176735Sjeff tdq = TDQ_CPU(i); 1338171713Sjeff tdq_setup(tdq); 1339176735Sjeff tdq->tdq_cg = smp_topo_find(cpu_top, i); 1340176735Sjeff if (tdq->tdq_cg == NULL) 1341176735Sjeff panic("Can't find cpu group for %d\n", i); 1342123433Sjeff } 1343176735Sjeff balance_tdq = TDQ_SELF(); 1344176735Sjeff sched_balance(); 1345171713Sjeff} 1346171713Sjeff#endif 1347171713Sjeff 1348171713Sjeff/* 1349171713Sjeff * Setup the thread queues and initialize the topology based on MD 1350171713Sjeff * information. 1351171713Sjeff */ 1352171713Sjeffstatic void 1353171713Sjeffsched_setup(void *dummy) 1354171713Sjeff{ 1355171713Sjeff struct tdq *tdq; 1356171713Sjeff 1357171713Sjeff tdq = TDQ_SELF(); 1358171713Sjeff#ifdef SMP 1359176734Sjeff sched_setup_smp(); 1360117237Sjeff#else 1361171713Sjeff tdq_setup(tdq); 1362116069Sjeff#endif 1363171482Sjeff /* 1364171482Sjeff * To avoid divide-by-zero, we set realstathz a dummy value 1365171482Sjeff * in case which sched_clock() called before sched_initticks(). 1366171482Sjeff */ 1367171482Sjeff realstathz = hz; 1368171482Sjeff sched_slice = (realstathz/10); /* ~100ms */ 1369171482Sjeff tickincr = 1 << SCHED_TICK_SHIFT; 1370171482Sjeff 1371171482Sjeff /* Add thread0's load since it's running. */ 1372171482Sjeff TDQ_LOCK(tdq); 1373171713Sjeff thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1374177435Sjeff tdq_load_add(tdq, &thread0); 1375176735Sjeff tdq->tdq_lowpri = thread0.td_priority; 1376171482Sjeff TDQ_UNLOCK(tdq); 1377109864Sjeff} 1378109864Sjeff 1379171482Sjeff/* 1380171482Sjeff * This routine determines the tickincr after stathz and hz are setup. 1381171482Sjeff */ 1382153533Sdavidxu/* ARGSUSED */ 1383153533Sdavidxustatic void 1384153533Sdavidxusched_initticks(void *dummy) 1385153533Sdavidxu{ 1386171482Sjeff int incr; 1387171482Sjeff 1388153533Sdavidxu realstathz = stathz ? stathz : hz; 1389166229Sjeff sched_slice = (realstathz/10); /* ~100ms */ 1390153533Sdavidxu 1391153533Sdavidxu /* 1392165762Sjeff * tickincr is shifted out by 10 to avoid rounding errors due to 1393165766Sjeff * hz not being evenly divisible by stathz on all platforms. 1394153533Sdavidxu */ 1395171482Sjeff incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1396165762Sjeff /* 1397165762Sjeff * This does not work for values of stathz that are more than 1398165762Sjeff * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1399165762Sjeff */ 1400171482Sjeff if (incr == 0) 1401171482Sjeff incr = 1; 1402171482Sjeff tickincr = incr; 1403166108Sjeff#ifdef SMP 1404171899Sjeff /* 1405172409Sjeff * Set the default balance interval now that we know 1406172409Sjeff * what realstathz is. 1407172409Sjeff */ 1408172409Sjeff balance_interval = realstathz; 1409172409Sjeff /* 1410189787Sjeff * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4. 1411189787Sjeff * This prevents excess thrashing on large machines and excess idle 1412189787Sjeff * on smaller machines. 1413171899Sjeff */ 1414189787Sjeff steal_thresh = min(fls(mp_ncpus) - 1, 3); 1415166108Sjeff affinity = SCHED_AFFINITY_DEFAULT; 1416166108Sjeff#endif 1417232740Smav if (sched_idlespinthresh < 0) 1418232740Smav sched_idlespinthresh = max(16, 2 * hz / realstathz); 1419153533Sdavidxu} 1420153533Sdavidxu 1421153533Sdavidxu 1422109864Sjeff/* 1423171482Sjeff * This is the core of the interactivity algorithm. Determines a score based 1424171482Sjeff * on past behavior. It is the ratio of sleep time to run time scaled to 1425171482Sjeff * a [0, 100] integer. This is the voluntary sleep time of a process, which 1426171482Sjeff * differs from the cpu usage because it does not account for time spent 1427171482Sjeff * waiting on a run-queue. Would be prettier if we had floating point. 1428171482Sjeff */ 1429171482Sjeffstatic int 1430171482Sjeffsched_interact_score(struct thread *td) 1431171482Sjeff{ 1432171482Sjeff struct td_sched *ts; 1433171482Sjeff int div; 1434171482Sjeff 1435171482Sjeff ts = td->td_sched; 1436171482Sjeff /* 1437171482Sjeff * The score is only needed if this is likely to be an interactive 1438171482Sjeff * task. Don't go through the expense of computing it if there's 1439171482Sjeff * no chance. 1440171482Sjeff */ 1441171482Sjeff if (sched_interact <= SCHED_INTERACT_HALF && 1442171482Sjeff ts->ts_runtime >= ts->ts_slptime) 1443171482Sjeff return (SCHED_INTERACT_HALF); 1444171482Sjeff 1445171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1446171482Sjeff div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1447171482Sjeff return (SCHED_INTERACT_HALF + 1448171482Sjeff (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1449171482Sjeff } 1450171482Sjeff if (ts->ts_slptime > ts->ts_runtime) { 1451171482Sjeff div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1452171482Sjeff return (ts->ts_runtime / div); 1453171482Sjeff } 1454171482Sjeff /* runtime == slptime */ 1455171482Sjeff if (ts->ts_runtime) 1456171482Sjeff return (SCHED_INTERACT_HALF); 1457171482Sjeff 1458171482Sjeff /* 1459171482Sjeff * This can happen if slptime and runtime are 0. 1460171482Sjeff */ 1461171482Sjeff return (0); 1462171482Sjeff 1463171482Sjeff} 1464171482Sjeff 1465171482Sjeff/* 1466109864Sjeff * Scale the scheduling priority according to the "interactivity" of this 1467109864Sjeff * process. 1468109864Sjeff */ 1469113357Sjeffstatic void 1470163709Sjbsched_priority(struct thread *td) 1471109864Sjeff{ 1472165762Sjeff int score; 1473109864Sjeff int pri; 1474109864Sjeff 1475217291Sjhb if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 1476113357Sjeff return; 1477112966Sjeff /* 1478165762Sjeff * If the score is interactive we place the thread in the realtime 1479165762Sjeff * queue with a priority that is less than kernel and interrupt 1480165762Sjeff * priorities. These threads are not subject to nice restrictions. 1481112966Sjeff * 1482171482Sjeff * Scores greater than this are placed on the normal timeshare queue 1483165762Sjeff * where the priority is partially decided by the most recent cpu 1484165762Sjeff * utilization and the rest is decided by nice value. 1485172293Sjeff * 1486172293Sjeff * The nice value of the process has a linear effect on the calculated 1487172293Sjeff * score. Negative nice values make it easier for a thread to be 1488172293Sjeff * considered interactive. 1489112966Sjeff */ 1490198126Sjhb score = imax(0, sched_interact_score(td) + td->td_proc->p_nice); 1491165762Sjeff if (score < sched_interact) { 1492217351Sjhb pri = PRI_MIN_INTERACT; 1493217351Sjhb pri += ((PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) / 1494217237Sjhb sched_interact) * score; 1495217351Sjhb KASSERT(pri >= PRI_MIN_INTERACT && pri <= PRI_MAX_INTERACT, 1496166208Sjeff ("sched_priority: invalid interactive priority %d score %d", 1497166208Sjeff pri, score)); 1498165762Sjeff } else { 1499165762Sjeff pri = SCHED_PRI_MIN; 1500165762Sjeff if (td->td_sched->ts_ticks) 1501228960Sjhb pri += min(SCHED_PRI_TICKS(td->td_sched), 1502228960Sjhb SCHED_PRI_RANGE); 1503165762Sjeff pri += SCHED_PRI_NICE(td->td_proc->p_nice); 1504217351Sjhb KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH, 1505171482Sjeff ("sched_priority: invalid priority %d: nice %d, " 1506171482Sjeff "ticks %d ftick %d ltick %d tick pri %d", 1507171482Sjeff pri, td->td_proc->p_nice, td->td_sched->ts_ticks, 1508171482Sjeff td->td_sched->ts_ftick, td->td_sched->ts_ltick, 1509171482Sjeff SCHED_PRI_TICKS(td->td_sched))); 1510165762Sjeff } 1511165762Sjeff sched_user_prio(td, pri); 1512112966Sjeff 1513112966Sjeff return; 1514109864Sjeff} 1515109864Sjeff 1516121868Sjeff/* 1517121868Sjeff * This routine enforces a maximum limit on the amount of scheduling history 1518171482Sjeff * kept. It is called after either the slptime or runtime is adjusted. This 1519171482Sjeff * function is ugly due to integer math. 1520121868Sjeff */ 1521116463Sjeffstatic void 1522163709Sjbsched_interact_update(struct thread *td) 1523116463Sjeff{ 1524165819Sjeff struct td_sched *ts; 1525166208Sjeff u_int sum; 1526121605Sjeff 1527165819Sjeff ts = td->td_sched; 1528171482Sjeff sum = ts->ts_runtime + ts->ts_slptime; 1529121868Sjeff if (sum < SCHED_SLP_RUN_MAX) 1530121868Sjeff return; 1531121868Sjeff /* 1532165819Sjeff * This only happens from two places: 1533165819Sjeff * 1) We have added an unusual amount of run time from fork_exit. 1534165819Sjeff * 2) We have added an unusual amount of sleep time from sched_sleep(). 1535165819Sjeff */ 1536165819Sjeff if (sum > SCHED_SLP_RUN_MAX * 2) { 1537171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1538171482Sjeff ts->ts_runtime = SCHED_SLP_RUN_MAX; 1539171482Sjeff ts->ts_slptime = 1; 1540165819Sjeff } else { 1541171482Sjeff ts->ts_slptime = SCHED_SLP_RUN_MAX; 1542171482Sjeff ts->ts_runtime = 1; 1543165819Sjeff } 1544165819Sjeff return; 1545165819Sjeff } 1546165819Sjeff /* 1547121868Sjeff * If we have exceeded by more than 1/5th then the algorithm below 1548121868Sjeff * will not bring us back into range. Dividing by two here forces 1549133427Sjeff * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1550121868Sjeff */ 1551127850Sjeff if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1552171482Sjeff ts->ts_runtime /= 2; 1553171482Sjeff ts->ts_slptime /= 2; 1554121868Sjeff return; 1555116463Sjeff } 1556171482Sjeff ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1557171482Sjeff ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1558116463Sjeff} 1559116463Sjeff 1560171482Sjeff/* 1561171482Sjeff * Scale back the interactivity history when a child thread is created. The 1562171482Sjeff * history is inherited from the parent but the thread may behave totally 1563171482Sjeff * differently. For example, a shell spawning a compiler process. We want 1564171482Sjeff * to learn that the compiler is behaving badly very quickly. 1565171482Sjeff */ 1566121868Sjeffstatic void 1567163709Sjbsched_interact_fork(struct thread *td) 1568121868Sjeff{ 1569121868Sjeff int ratio; 1570121868Sjeff int sum; 1571121868Sjeff 1572171482Sjeff sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime; 1573121868Sjeff if (sum > SCHED_SLP_RUN_FORK) { 1574121868Sjeff ratio = sum / SCHED_SLP_RUN_FORK; 1575171482Sjeff td->td_sched->ts_runtime /= ratio; 1576171482Sjeff td->td_sched->ts_slptime /= ratio; 1577121868Sjeff } 1578121868Sjeff} 1579121868Sjeff 1580113357Sjeff/* 1581171482Sjeff * Called from proc0_init() to setup the scheduler fields. 1582134791Sjulian */ 1583134791Sjulianvoid 1584134791Sjulianschedinit(void) 1585134791Sjulian{ 1586165762Sjeff 1587134791Sjulian /* 1588134791Sjulian * Set up the scheduler specific parts of proc0. 1589134791Sjulian */ 1590136167Sjulian proc0.p_sched = NULL; /* XXX */ 1591164936Sjulian thread0.td_sched = &td_sched0; 1592165762Sjeff td_sched0.ts_ltick = ticks; 1593165796Sjeff td_sched0.ts_ftick = ticks; 1594177009Sjeff td_sched0.ts_slice = sched_slice; 1595134791Sjulian} 1596134791Sjulian 1597134791Sjulian/* 1598113357Sjeff * This is only somewhat accurate since given many processes of the same 1599113357Sjeff * priority they will switch when their slices run out, which will be 1600165762Sjeff * at most sched_slice stathz ticks. 1601113357Sjeff */ 1602109864Sjeffint 1603109864Sjeffsched_rr_interval(void) 1604109864Sjeff{ 1605165762Sjeff 1606165762Sjeff /* Convert sched_slice to hz */ 1607165762Sjeff return (hz/(realstathz/sched_slice)); 1608109864Sjeff} 1609109864Sjeff 1610171482Sjeff/* 1611171482Sjeff * Update the percent cpu tracking information when it is requested or 1612171482Sjeff * the total history exceeds the maximum. We keep a sliding history of 1613171482Sjeff * tick counts that slowly decays. This is less precise than the 4BSD 1614171482Sjeff * mechanism since it happens with less regular and frequent events. 1615171482Sjeff */ 1616121790Sjeffstatic void 1617232917Smavsched_pctcpu_update(struct td_sched *ts, int run) 1618109864Sjeff{ 1619232917Smav int t = ticks; 1620165762Sjeff 1621232917Smav if (t - ts->ts_ltick >= SCHED_TICK_TARG) { 1622164936Sjulian ts->ts_ticks = 0; 1623232917Smav ts->ts_ftick = t - SCHED_TICK_TARG; 1624232917Smav } else if (t - ts->ts_ftick >= SCHED_TICK_MAX) { 1625232917Smav ts->ts_ticks = (ts->ts_ticks / (ts->ts_ltick - ts->ts_ftick)) * 1626232917Smav (ts->ts_ltick - (t - SCHED_TICK_TARG)); 1627232917Smav ts->ts_ftick = t - SCHED_TICK_TARG; 1628232917Smav } 1629232917Smav if (run) 1630232917Smav ts->ts_ticks += (t - ts->ts_ltick) << SCHED_TICK_SHIFT; 1631232917Smav ts->ts_ltick = t; 1632109864Sjeff} 1633109864Sjeff 1634171482Sjeff/* 1635171482Sjeff * Adjust the priority of a thread. Move it to the appropriate run-queue 1636171482Sjeff * if necessary. This is the back-end for several priority related 1637171482Sjeff * functions. 1638171482Sjeff */ 1639165762Sjeffstatic void 1640139453Sjhbsched_thread_priority(struct thread *td, u_char prio) 1641109864Sjeff{ 1642164936Sjulian struct td_sched *ts; 1643177009Sjeff struct tdq *tdq; 1644177009Sjeff int oldpri; 1645109864Sjeff 1646187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 1647187357Sjeff "prio:%d", td->td_priority, "new prio:%d", prio, 1648187357Sjeff KTR_ATTR_LINKED, sched_tdname(curthread)); 1649235459Srstone SDT_PROBE3(sched, , , change_pri, td, td->td_proc, prio); 1650187357Sjeff if (td != curthread && prio > td->td_priority) { 1651187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 1652187357Sjeff "lend prio", "prio:%d", td->td_priority, "new prio:%d", 1653187357Sjeff prio, KTR_ATTR_LINKED, sched_tdname(td)); 1654235459Srstone SDT_PROBE4(sched, , , lend_pri, td, td->td_proc, prio, 1655235459Srstone curthread); 1656187357Sjeff } 1657164936Sjulian ts = td->td_sched; 1658170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1659139453Sjhb if (td->td_priority == prio) 1660139453Sjhb return; 1661177376Sjeff /* 1662177376Sjeff * If the priority has been elevated due to priority 1663177376Sjeff * propagation, we may have to move ourselves to a new 1664177376Sjeff * queue. This could be optimized to not re-add in some 1665177376Sjeff * cases. 1666177376Sjeff */ 1667165766Sjeff if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1668165762Sjeff sched_rem(td); 1669165762Sjeff td->td_priority = prio; 1670171482Sjeff sched_add(td, SRQ_BORROWING); 1671177009Sjeff return; 1672177009Sjeff } 1673177376Sjeff /* 1674177376Sjeff * If the thread is currently running we may have to adjust the lowpri 1675177376Sjeff * information so other cpus are aware of our current priority. 1676177376Sjeff */ 1677177009Sjeff if (TD_IS_RUNNING(td)) { 1678177376Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1679177376Sjeff oldpri = td->td_priority; 1680177376Sjeff td->td_priority = prio; 1681176735Sjeff if (prio < tdq->tdq_lowpri) 1682171482Sjeff tdq->tdq_lowpri = prio; 1683176735Sjeff else if (tdq->tdq_lowpri == oldpri) 1684176735Sjeff tdq_setlowpri(tdq, td); 1685177376Sjeff return; 1686177009Sjeff } 1687177376Sjeff td->td_priority = prio; 1688109864Sjeff} 1689109864Sjeff 1690139453Sjhb/* 1691139453Sjhb * Update a thread's priority when it is lent another thread's 1692139453Sjhb * priority. 1693139453Sjhb */ 1694109864Sjeffvoid 1695139453Sjhbsched_lend_prio(struct thread *td, u_char prio) 1696139453Sjhb{ 1697139453Sjhb 1698139453Sjhb td->td_flags |= TDF_BORROWING; 1699139453Sjhb sched_thread_priority(td, prio); 1700139453Sjhb} 1701139453Sjhb 1702139453Sjhb/* 1703139453Sjhb * Restore a thread's priority when priority propagation is 1704139453Sjhb * over. The prio argument is the minimum priority the thread 1705139453Sjhb * needs to have to satisfy other possible priority lending 1706139453Sjhb * requests. If the thread's regular priority is less 1707139453Sjhb * important than prio, the thread will keep a priority boost 1708139453Sjhb * of prio. 1709139453Sjhb */ 1710139453Sjhbvoid 1711139453Sjhbsched_unlend_prio(struct thread *td, u_char prio) 1712139453Sjhb{ 1713139453Sjhb u_char base_pri; 1714139453Sjhb 1715139453Sjhb if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1716139453Sjhb td->td_base_pri <= PRI_MAX_TIMESHARE) 1717163709Sjb base_pri = td->td_user_pri; 1718139453Sjhb else 1719139453Sjhb base_pri = td->td_base_pri; 1720139453Sjhb if (prio >= base_pri) { 1721139455Sjhb td->td_flags &= ~TDF_BORROWING; 1722139453Sjhb sched_thread_priority(td, base_pri); 1723139453Sjhb } else 1724139453Sjhb sched_lend_prio(td, prio); 1725139453Sjhb} 1726139453Sjhb 1727171482Sjeff/* 1728171482Sjeff * Standard entry for setting the priority to an absolute value. 1729171482Sjeff */ 1730139453Sjhbvoid 1731139453Sjhbsched_prio(struct thread *td, u_char prio) 1732139453Sjhb{ 1733139453Sjhb u_char oldprio; 1734139453Sjhb 1735139453Sjhb /* First, update the base priority. */ 1736139453Sjhb td->td_base_pri = prio; 1737139453Sjhb 1738139453Sjhb /* 1739139455Sjhb * If the thread is borrowing another thread's priority, don't 1740139453Sjhb * ever lower the priority. 1741139453Sjhb */ 1742139453Sjhb if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1743139453Sjhb return; 1744139453Sjhb 1745139453Sjhb /* Change the real priority. */ 1746139453Sjhb oldprio = td->td_priority; 1747139453Sjhb sched_thread_priority(td, prio); 1748139453Sjhb 1749139453Sjhb /* 1750139453Sjhb * If the thread is on a turnstile, then let the turnstile update 1751139453Sjhb * its state. 1752139453Sjhb */ 1753139453Sjhb if (TD_ON_LOCK(td) && oldprio != prio) 1754139453Sjhb turnstile_adjust(td, oldprio); 1755139453Sjhb} 1756139455Sjhb 1757171482Sjeff/* 1758171482Sjeff * Set the base user priority, does not effect current running priority. 1759171482Sjeff */ 1760139453Sjhbvoid 1761163709Sjbsched_user_prio(struct thread *td, u_char prio) 1762161599Sdavidxu{ 1763161599Sdavidxu 1764163709Sjb td->td_base_user_pri = prio; 1765216313Sdavidxu if (td->td_lend_user_pri <= prio) 1766216313Sdavidxu return; 1767163709Sjb td->td_user_pri = prio; 1768161599Sdavidxu} 1769161599Sdavidxu 1770161599Sdavidxuvoid 1771161599Sdavidxusched_lend_user_prio(struct thread *td, u_char prio) 1772161599Sdavidxu{ 1773161599Sdavidxu 1774174536Sdavidxu THREAD_LOCK_ASSERT(td, MA_OWNED); 1775216313Sdavidxu td->td_lend_user_pri = prio; 1776216791Sdavidxu td->td_user_pri = min(prio, td->td_base_user_pri); 1777216791Sdavidxu if (td->td_priority > td->td_user_pri) 1778216791Sdavidxu sched_prio(td, td->td_user_pri); 1779216791Sdavidxu else if (td->td_priority != td->td_user_pri) 1780216791Sdavidxu td->td_flags |= TDF_NEEDRESCHED; 1781161599Sdavidxu} 1782161599Sdavidxu 1783171482Sjeff/* 1784171713Sjeff * Handle migration from sched_switch(). This happens only for 1785171713Sjeff * cpu binding. 1786171713Sjeff */ 1787171713Sjeffstatic struct mtx * 1788171713Sjeffsched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1789171713Sjeff{ 1790171713Sjeff struct tdq *tdn; 1791171713Sjeff 1792171713Sjeff tdn = TDQ_CPU(td->td_sched->ts_cpu); 1793171713Sjeff#ifdef SMP 1794177435Sjeff tdq_load_rem(tdq, td); 1795171713Sjeff /* 1796171713Sjeff * Do the lock dance required to avoid LOR. We grab an extra 1797171713Sjeff * spinlock nesting to prevent preemption while we're 1798171713Sjeff * not holding either run-queue lock. 1799171713Sjeff */ 1800171713Sjeff spinlock_enter(); 1801202889Sattilio thread_lock_block(td); /* This releases the lock on tdq. */ 1802197223Sattilio 1803197223Sattilio /* 1804197223Sattilio * Acquire both run-queue locks before placing the thread on the new 1805197223Sattilio * run-queue to avoid deadlocks created by placing a thread with a 1806197223Sattilio * blocked lock on the run-queue of a remote processor. The deadlock 1807197223Sattilio * occurs when a third processor attempts to lock the two queues in 1808197223Sattilio * question while the target processor is spinning with its own 1809197223Sattilio * run-queue lock held while waiting for the blocked lock to clear. 1810197223Sattilio */ 1811197223Sattilio tdq_lock_pair(tdn, tdq); 1812171713Sjeff tdq_add(tdn, td, flags); 1813177435Sjeff tdq_notify(tdn, td); 1814197223Sattilio TDQ_UNLOCK(tdn); 1815171713Sjeff spinlock_exit(); 1816171713Sjeff#endif 1817171713Sjeff return (TDQ_LOCKPTR(tdn)); 1818171713Sjeff} 1819171713Sjeff 1820171713Sjeff/* 1821202889Sattilio * Variadic version of thread_lock_unblock() that does not assume td_lock 1822202889Sattilio * is blocked. 1823171482Sjeff */ 1824171482Sjeffstatic inline void 1825171482Sjeffthread_unblock_switch(struct thread *td, struct mtx *mtx) 1826171482Sjeff{ 1827171482Sjeff atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1828171482Sjeff (uintptr_t)mtx); 1829171482Sjeff} 1830171482Sjeff 1831171482Sjeff/* 1832171482Sjeff * Switch threads. This function has to handle threads coming in while 1833171482Sjeff * blocked for some reason, running, or idle. It also must deal with 1834171482Sjeff * migrating a thread from one queue to another as running threads may 1835171482Sjeff * be assigned elsewhere via binding. 1836171482Sjeff */ 1837161599Sdavidxuvoid 1838135051Sjuliansched_switch(struct thread *td, struct thread *newtd, int flags) 1839109864Sjeff{ 1840165627Sjeff struct tdq *tdq; 1841164936Sjulian struct td_sched *ts; 1842171482Sjeff struct mtx *mtx; 1843171713Sjeff int srqflag; 1844171482Sjeff int cpuid; 1845109864Sjeff 1846170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1847177376Sjeff KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument")); 1848109864Sjeff 1849171482Sjeff cpuid = PCPU_GET(cpuid); 1850171482Sjeff tdq = TDQ_CPU(cpuid); 1851164936Sjulian ts = td->td_sched; 1852171713Sjeff mtx = td->td_lock; 1853232917Smav sched_pctcpu_update(ts, 1); 1854171482Sjeff ts->ts_rltick = ticks; 1855133555Sjeff td->td_lastcpu = td->td_oncpu; 1856113339Sjulian td->td_oncpu = NOCPU; 1857220198Sfabient if (!(flags & SW_PREEMPT)) 1858220198Sfabient td->td_flags &= ~TDF_NEEDRESCHED; 1859144777Sups td->td_owepreempt = 0; 1860178277Sjeff tdq->tdq_switchcnt++; 1861123434Sjeff /* 1862171482Sjeff * The lock pointer in an idle thread should never change. Reset it 1863171482Sjeff * to CAN_RUN as well. 1864123434Sjeff */ 1865167327Sjulian if (TD_IS_IDLETHREAD(td)) { 1866171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1867139334Sjeff TD_SET_CAN_RUN(td); 1868170293Sjeff } else if (TD_IS_RUNNING(td)) { 1869171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1870171713Sjeff srqflag = (flags & SW_PREEMPT) ? 1871170293Sjeff SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1872171713Sjeff SRQ_OURSELF|SRQ_YIELDING; 1873212153Smdf#ifdef SMP 1874212115Smdf if (THREAD_CAN_MIGRATE(td) && !THREAD_CAN_SCHED(td, ts->ts_cpu)) 1875212115Smdf ts->ts_cpu = sched_pickcpu(td, 0); 1876212153Smdf#endif 1877171713Sjeff if (ts->ts_cpu == cpuid) 1878177435Sjeff tdq_runq_add(tdq, td, srqflag); 1879212115Smdf else { 1880212115Smdf KASSERT(THREAD_CAN_MIGRATE(td) || 1881212115Smdf (ts->ts_flags & TSF_BOUND) != 0, 1882212115Smdf ("Thread %p shouldn't migrate", td)); 1883171713Sjeff mtx = sched_switch_migrate(tdq, td, srqflag); 1884212115Smdf } 1885171482Sjeff } else { 1886171482Sjeff /* This thread must be going to sleep. */ 1887171482Sjeff TDQ_LOCK(tdq); 1888202889Sattilio mtx = thread_lock_block(td); 1889177435Sjeff tdq_load_rem(tdq, td); 1890171482Sjeff } 1891171482Sjeff /* 1892171482Sjeff * We enter here with the thread blocked and assigned to the 1893171482Sjeff * appropriate cpu run-queue or sleep-queue and with the current 1894171482Sjeff * thread-queue locked. 1895171482Sjeff */ 1896171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 1897171482Sjeff newtd = choosethread(); 1898171482Sjeff /* 1899171482Sjeff * Call the MD code to switch contexts if necessary. 1900171482Sjeff */ 1901145256Sjkoshy if (td != newtd) { 1902145256Sjkoshy#ifdef HWPMC_HOOKS 1903145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1904145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1905145256Sjkoshy#endif 1906235459Srstone SDT_PROBE2(sched, , , off_cpu, td, td->td_proc); 1907174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 1908172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 1909232917Smav sched_pctcpu_update(newtd->td_sched, 0); 1910179297Sjb 1911179297Sjb#ifdef KDTRACE_HOOKS 1912179297Sjb /* 1913179297Sjb * If DTrace has set the active vtime enum to anything 1914179297Sjb * other than INACTIVE (0), then it should have set the 1915179297Sjb * function to call. 1916179297Sjb */ 1917179297Sjb if (dtrace_vtime_active) 1918179297Sjb (*dtrace_vtime_switch_func)(newtd); 1919179297Sjb#endif 1920179297Sjb 1921171482Sjeff cpu_switch(td, newtd, mtx); 1922171482Sjeff /* 1923171482Sjeff * We may return from cpu_switch on a different cpu. However, 1924171482Sjeff * we always return with td_lock pointing to the current cpu's 1925171482Sjeff * run queue lock. 1926171482Sjeff */ 1927171482Sjeff cpuid = PCPU_GET(cpuid); 1928171482Sjeff tdq = TDQ_CPU(cpuid); 1929174629Sjeff lock_profile_obtain_lock_success( 1930174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 1931235459Srstone 1932235459Srstone SDT_PROBE0(sched, , , on_cpu); 1933145256Sjkoshy#ifdef HWPMC_HOOKS 1934145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1935145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1936145256Sjkoshy#endif 1937235459Srstone } else { 1938171482Sjeff thread_unblock_switch(td, mtx); 1939235459Srstone SDT_PROBE0(sched, , , remain_cpu); 1940235459Srstone } 1941171482Sjeff /* 1942171482Sjeff * Assert that all went well and return. 1943171482Sjeff */ 1944171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED); 1945171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1946171482Sjeff td->td_oncpu = cpuid; 1947109864Sjeff} 1948109864Sjeff 1949171482Sjeff/* 1950171482Sjeff * Adjust thread priorities as a result of a nice request. 1951171482Sjeff */ 1952109864Sjeffvoid 1953130551Sjuliansched_nice(struct proc *p, int nice) 1954109864Sjeff{ 1955109864Sjeff struct thread *td; 1956109864Sjeff 1957130551Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 1958165762Sjeff 1959130551Sjulian p->p_nice = nice; 1960163709Sjb FOREACH_THREAD_IN_PROC(p, td) { 1961170293Sjeff thread_lock(td); 1962163709Sjb sched_priority(td); 1963165762Sjeff sched_prio(td, td->td_base_user_pri); 1964170293Sjeff thread_unlock(td); 1965130551Sjulian } 1966109864Sjeff} 1967109864Sjeff 1968171482Sjeff/* 1969171482Sjeff * Record the sleep time for the interactivity scorer. 1970171482Sjeff */ 1971109864Sjeffvoid 1972177085Sjeffsched_sleep(struct thread *td, int prio) 1973109864Sjeff{ 1974165762Sjeff 1975170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1976109864Sjeff 1977172264Sjeff td->td_slptick = ticks; 1978201347Skib if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 1979177085Sjeff td->td_flags |= TDF_CANSWAP; 1980217410Sjhb if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 1981217410Sjhb return; 1982177903Sjeff if (static_boost == 1 && prio) 1983177085Sjeff sched_prio(td, prio); 1984177903Sjeff else if (static_boost && td->td_priority > static_boost) 1985177903Sjeff sched_prio(td, static_boost); 1986109864Sjeff} 1987109864Sjeff 1988171482Sjeff/* 1989171482Sjeff * Schedule a thread to resume execution and record how long it voluntarily 1990171482Sjeff * slept. We also update the pctcpu, interactivity, and priority. 1991171482Sjeff */ 1992109864Sjeffvoid 1993109864Sjeffsched_wakeup(struct thread *td) 1994109864Sjeff{ 1995166229Sjeff struct td_sched *ts; 1996171482Sjeff int slptick; 1997165762Sjeff 1998170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1999166229Sjeff ts = td->td_sched; 2000177085Sjeff td->td_flags &= ~TDF_CANSWAP; 2001109864Sjeff /* 2002165762Sjeff * If we slept for more than a tick update our interactivity and 2003165762Sjeff * priority. 2004109864Sjeff */ 2005172264Sjeff slptick = td->td_slptick; 2006172264Sjeff td->td_slptick = 0; 2007171482Sjeff if (slptick && slptick != ticks) { 2008232917Smav ts->ts_slptime += (ticks - slptick) << SCHED_TICK_SHIFT; 2009165819Sjeff sched_interact_update(td); 2010232917Smav sched_pctcpu_update(ts, 0); 2011109864Sjeff } 2012166229Sjeff /* Reset the slice value after we sleep. */ 2013166229Sjeff ts->ts_slice = sched_slice; 2014166190Sjeff sched_add(td, SRQ_BORING); 2015109864Sjeff} 2016109864Sjeff 2017109864Sjeff/* 2018109864Sjeff * Penalize the parent for creating a new child and initialize the child's 2019109864Sjeff * priority. 2020109864Sjeff */ 2021109864Sjeffvoid 2022163709Sjbsched_fork(struct thread *td, struct thread *child) 2023109864Sjeff{ 2024170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2025232917Smav sched_pctcpu_update(td->td_sched, 1); 2026164936Sjulian sched_fork_thread(td, child); 2027165762Sjeff /* 2028165762Sjeff * Penalize the parent and child for forking. 2029165762Sjeff */ 2030165762Sjeff sched_interact_fork(child); 2031165762Sjeff sched_priority(child); 2032171482Sjeff td->td_sched->ts_runtime += tickincr; 2033165762Sjeff sched_interact_update(td); 2034165762Sjeff sched_priority(td); 2035164936Sjulian} 2036109864Sjeff 2037171482Sjeff/* 2038171482Sjeff * Fork a new thread, may be within the same process. 2039171482Sjeff */ 2040164936Sjulianvoid 2041164936Sjuliansched_fork_thread(struct thread *td, struct thread *child) 2042164936Sjulian{ 2043164936Sjulian struct td_sched *ts; 2044164936Sjulian struct td_sched *ts2; 2045164936Sjulian 2046177426Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2047165762Sjeff /* 2048165762Sjeff * Initialize child. 2049165762Sjeff */ 2050177426Sjeff ts = td->td_sched; 2051177426Sjeff ts2 = child->td_sched; 2052171482Sjeff child->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 2053176735Sjeff child->td_cpuset = cpuset_ref(td->td_cpuset); 2054164936Sjulian ts2->ts_cpu = ts->ts_cpu; 2055177426Sjeff ts2->ts_flags = 0; 2056165762Sjeff /* 2057217078Sjhb * Grab our parents cpu estimation information. 2058165762Sjeff */ 2059164936Sjulian ts2->ts_ticks = ts->ts_ticks; 2060164936Sjulian ts2->ts_ltick = ts->ts_ltick; 2061164936Sjulian ts2->ts_ftick = ts->ts_ftick; 2062165762Sjeff /* 2063217078Sjhb * Do not inherit any borrowed priority from the parent. 2064217078Sjhb */ 2065217078Sjhb child->td_priority = child->td_base_pri; 2066217078Sjhb /* 2067165762Sjeff * And update interactivity score. 2068165762Sjeff */ 2069171482Sjeff ts2->ts_slptime = ts->ts_slptime; 2070171482Sjeff ts2->ts_runtime = ts->ts_runtime; 2071165762Sjeff ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 2072187357Sjeff#ifdef KTR 2073187357Sjeff bzero(ts2->ts_name, sizeof(ts2->ts_name)); 2074187357Sjeff#endif 2075113357Sjeff} 2076113357Sjeff 2077171482Sjeff/* 2078171482Sjeff * Adjust the priority class of a thread. 2079171482Sjeff */ 2080113357Sjeffvoid 2081163709Sjbsched_class(struct thread *td, int class) 2082113357Sjeff{ 2083113357Sjeff 2084170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2085163709Sjb if (td->td_pri_class == class) 2086113357Sjeff return; 2087163709Sjb td->td_pri_class = class; 2088109864Sjeff} 2089109864Sjeff 2090109864Sjeff/* 2091109864Sjeff * Return some of the child's priority and interactivity to the parent. 2092109864Sjeff */ 2093109864Sjeffvoid 2094164939Sjuliansched_exit(struct proc *p, struct thread *child) 2095109864Sjeff{ 2096165762Sjeff struct thread *td; 2097113372Sjeff 2098187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2099225199Sdelphij "prio:%d", child->td_priority); 2100177368Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 2101165762Sjeff td = FIRST_THREAD_IN_PROC(p); 2102165762Sjeff sched_exit_thread(td, child); 2103113372Sjeff} 2104113372Sjeff 2105171482Sjeff/* 2106171482Sjeff * Penalize another thread for the time spent on this one. This helps to 2107171482Sjeff * worsen the priority and interactivity of processes which schedule batch 2108171482Sjeff * jobs such as make. This has little effect on the make process itself but 2109171482Sjeff * causes new processes spawned by it to receive worse scores immediately. 2110171482Sjeff */ 2111113372Sjeffvoid 2112164939Sjuliansched_exit_thread(struct thread *td, struct thread *child) 2113164936Sjulian{ 2114165762Sjeff 2115187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2116225199Sdelphij "prio:%d", child->td_priority); 2117165762Sjeff /* 2118165762Sjeff * Give the child's runtime to the parent without returning the 2119165762Sjeff * sleep time as a penalty to the parent. This causes shells that 2120165762Sjeff * launch expensive things to mark their children as expensive. 2121165762Sjeff */ 2122170293Sjeff thread_lock(td); 2123171482Sjeff td->td_sched->ts_runtime += child->td_sched->ts_runtime; 2124164939Sjulian sched_interact_update(td); 2125165762Sjeff sched_priority(td); 2126170293Sjeff thread_unlock(td); 2127164936Sjulian} 2128164936Sjulian 2129177005Sjeffvoid 2130177005Sjeffsched_preempt(struct thread *td) 2131177005Sjeff{ 2132177005Sjeff struct tdq *tdq; 2133177005Sjeff 2134235459Srstone SDT_PROBE2(sched, , , surrender, td, td->td_proc); 2135235459Srstone 2136177005Sjeff thread_lock(td); 2137177005Sjeff tdq = TDQ_SELF(); 2138177005Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2139177005Sjeff tdq->tdq_ipipending = 0; 2140177005Sjeff if (td->td_priority > tdq->tdq_lowpri) { 2141178272Sjeff int flags; 2142178272Sjeff 2143178272Sjeff flags = SW_INVOL | SW_PREEMPT; 2144177005Sjeff if (td->td_critnest > 1) 2145177005Sjeff td->td_owepreempt = 1; 2146178272Sjeff else if (TD_IS_IDLETHREAD(td)) 2147178272Sjeff mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL); 2148177005Sjeff else 2149178272Sjeff mi_switch(flags | SWT_REMOTEPREEMPT, NULL); 2150177005Sjeff } 2151177005Sjeff thread_unlock(td); 2152177005Sjeff} 2153177005Sjeff 2154171482Sjeff/* 2155171482Sjeff * Fix priorities on return to user-space. Priorities may be elevated due 2156171482Sjeff * to static priorities in msleep() or similar. 2157171482Sjeff */ 2158164936Sjulianvoid 2159164936Sjuliansched_userret(struct thread *td) 2160164936Sjulian{ 2161164936Sjulian /* 2162164936Sjulian * XXX we cheat slightly on the locking here to avoid locking in 2163164936Sjulian * the usual case. Setting td_priority here is essentially an 2164164936Sjulian * incomplete workaround for not setting it properly elsewhere. 2165164936Sjulian * Now that some interrupt handlers are threads, not setting it 2166164936Sjulian * properly elsewhere can clobber it in the window between setting 2167164936Sjulian * it here and returning to user mode, so don't waste time setting 2168164936Sjulian * it perfectly here. 2169164936Sjulian */ 2170164936Sjulian KASSERT((td->td_flags & TDF_BORROWING) == 0, 2171164936Sjulian ("thread with borrowed priority returning to userland")); 2172164936Sjulian if (td->td_priority != td->td_user_pri) { 2173170293Sjeff thread_lock(td); 2174164936Sjulian td->td_priority = td->td_user_pri; 2175164936Sjulian td->td_base_pri = td->td_user_pri; 2176177005Sjeff tdq_setlowpri(TDQ_SELF(), td); 2177170293Sjeff thread_unlock(td); 2178164936Sjulian } 2179164936Sjulian} 2180164936Sjulian 2181171482Sjeff/* 2182171482Sjeff * Handle a stathz tick. This is really only relevant for timeshare 2183171482Sjeff * threads. 2184171482Sjeff */ 2185164936Sjulianvoid 2186121127Sjeffsched_clock(struct thread *td) 2187109864Sjeff{ 2188164936Sjulian struct tdq *tdq; 2189164936Sjulian struct td_sched *ts; 2190109864Sjeff 2191171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2192164936Sjulian tdq = TDQ_SELF(); 2193172409Sjeff#ifdef SMP 2194133427Sjeff /* 2195172409Sjeff * We run the long term load balancer infrequently on the first cpu. 2196172409Sjeff */ 2197172409Sjeff if (balance_tdq == tdq) { 2198172409Sjeff if (balance_ticks && --balance_ticks == 0) 2199172409Sjeff sched_balance(); 2200172409Sjeff } 2201172409Sjeff#endif 2202172409Sjeff /* 2203178277Sjeff * Save the old switch count so we have a record of the last ticks 2204178277Sjeff * activity. Initialize the new switch count based on our load. 2205178277Sjeff * If there is some activity seed it to reflect that. 2206178277Sjeff */ 2207178277Sjeff tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 2208178471Sjeff tdq->tdq_switchcnt = tdq->tdq_load; 2209178277Sjeff /* 2210165766Sjeff * Advance the insert index once for each tick to ensure that all 2211165766Sjeff * threads get a chance to run. 2212133427Sjeff */ 2213165766Sjeff if (tdq->tdq_idx == tdq->tdq_ridx) { 2214165766Sjeff tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 2215165766Sjeff if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 2216165766Sjeff tdq->tdq_ridx = tdq->tdq_idx; 2217165766Sjeff } 2218165766Sjeff ts = td->td_sched; 2219232917Smav sched_pctcpu_update(ts, 1); 2220175104Sjeff if (td->td_pri_class & PRI_FIFO_BIT) 2221113357Sjeff return; 2222217291Sjhb if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) { 2223175104Sjeff /* 2224175104Sjeff * We used a tick; charge it to the thread so 2225175104Sjeff * that we can compute our interactivity. 2226175104Sjeff */ 2227175104Sjeff td->td_sched->ts_runtime += tickincr; 2228175104Sjeff sched_interact_update(td); 2229177009Sjeff sched_priority(td); 2230175104Sjeff } 2231113357Sjeff /* 2232109864Sjeff * We used up one time slice. 2233109864Sjeff */ 2234164936Sjulian if (--ts->ts_slice > 0) 2235113357Sjeff return; 2236109864Sjeff /* 2237177009Sjeff * We're out of time, force a requeue at userret(). 2238109864Sjeff */ 2239177009Sjeff ts->ts_slice = sched_slice; 2240113357Sjeff td->td_flags |= TDF_NEEDRESCHED; 2241109864Sjeff} 2242109864Sjeff 2243171482Sjeff/* 2244232917Smav * Called once per hz tick. 2245171482Sjeff */ 2246171482Sjeffvoid 2247212541Smavsched_tick(int cnt) 2248171482Sjeff{ 2249171482Sjeff 2250171482Sjeff} 2251171482Sjeff 2252171482Sjeff/* 2253171482Sjeff * Return whether the current CPU has runnable tasks. Used for in-kernel 2254171482Sjeff * cooperative idle threads. 2255171482Sjeff */ 2256109864Sjeffint 2257109864Sjeffsched_runnable(void) 2258109864Sjeff{ 2259164936Sjulian struct tdq *tdq; 2260115998Sjeff int load; 2261109864Sjeff 2262115998Sjeff load = 1; 2263115998Sjeff 2264164936Sjulian tdq = TDQ_SELF(); 2265121605Sjeff if ((curthread->td_flags & TDF_IDLETD) != 0) { 2266165620Sjeff if (tdq->tdq_load > 0) 2267121605Sjeff goto out; 2268121605Sjeff } else 2269165620Sjeff if (tdq->tdq_load - 1 > 0) 2270121605Sjeff goto out; 2271115998Sjeff load = 0; 2272115998Sjeffout: 2273115998Sjeff return (load); 2274109864Sjeff} 2275109864Sjeff 2276171482Sjeff/* 2277171482Sjeff * Choose the highest priority thread to run. The thread is removed from 2278171482Sjeff * the run-queue while running however the load remains. For SMP we set 2279171482Sjeff * the tdq in the global idle bitmask if it idles here. 2280171482Sjeff */ 2281166190Sjeffstruct thread * 2282109970Sjeffsched_choose(void) 2283109970Sjeff{ 2284177435Sjeff struct thread *td; 2285164936Sjulian struct tdq *tdq; 2286109970Sjeff 2287164936Sjulian tdq = TDQ_SELF(); 2288171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2289177435Sjeff td = tdq_choose(tdq); 2290177435Sjeff if (td) { 2291177435Sjeff tdq_runq_rem(tdq, td); 2292177903Sjeff tdq->tdq_lowpri = td->td_priority; 2293177435Sjeff return (td); 2294109864Sjeff } 2295177903Sjeff tdq->tdq_lowpri = PRI_MAX_IDLE; 2296176735Sjeff return (PCPU_GET(idlethread)); 2297109864Sjeff} 2298109864Sjeff 2299171482Sjeff/* 2300171482Sjeff * Set owepreempt if necessary. Preemption never happens directly in ULE, 2301171482Sjeff * we always request it once we exit a critical section. 2302171482Sjeff */ 2303171482Sjeffstatic inline void 2304171482Sjeffsched_setpreempt(struct thread *td) 2305166190Sjeff{ 2306166190Sjeff struct thread *ctd; 2307166190Sjeff int cpri; 2308166190Sjeff int pri; 2309166190Sjeff 2310177005Sjeff THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2311177005Sjeff 2312166190Sjeff ctd = curthread; 2313166190Sjeff pri = td->td_priority; 2314166190Sjeff cpri = ctd->td_priority; 2315177005Sjeff if (pri < cpri) 2316177005Sjeff ctd->td_flags |= TDF_NEEDRESCHED; 2317166190Sjeff if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2318171482Sjeff return; 2319177005Sjeff if (!sched_shouldpreempt(pri, cpri, 0)) 2320171482Sjeff return; 2321171482Sjeff ctd->td_owepreempt = 1; 2322166190Sjeff} 2323166190Sjeff 2324171482Sjeff/* 2325177009Sjeff * Add a thread to a thread queue. Select the appropriate runq and add the 2326177009Sjeff * thread to it. This is the internal function called when the tdq is 2327177009Sjeff * predetermined. 2328171482Sjeff */ 2329109864Sjeffvoid 2330171482Sjefftdq_add(struct tdq *tdq, struct thread *td, int flags) 2331109864Sjeff{ 2332109864Sjeff 2333171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2334166190Sjeff KASSERT((td->td_inhibitors == 0), 2335166190Sjeff ("sched_add: trying to run inhibited thread")); 2336166190Sjeff KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 2337166190Sjeff ("sched_add: bad thread state")); 2338172207Sjeff KASSERT(td->td_flags & TDF_INMEM, 2339172207Sjeff ("sched_add: thread swapped out")); 2340171482Sjeff 2341171482Sjeff if (td->td_priority < tdq->tdq_lowpri) 2342171482Sjeff tdq->tdq_lowpri = td->td_priority; 2343177435Sjeff tdq_runq_add(tdq, td, flags); 2344177435Sjeff tdq_load_add(tdq, td); 2345171482Sjeff} 2346171482Sjeff 2347171482Sjeff/* 2348171482Sjeff * Select the target thread queue and add a thread to it. Request 2349171482Sjeff * preemption or IPI a remote processor if required. 2350171482Sjeff */ 2351171482Sjeffvoid 2352171482Sjeffsched_add(struct thread *td, int flags) 2353171482Sjeff{ 2354171482Sjeff struct tdq *tdq; 2355171482Sjeff#ifdef SMP 2356171482Sjeff int cpu; 2357171482Sjeff#endif 2358187357Sjeff 2359187357Sjeff KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 2360187357Sjeff "prio:%d", td->td_priority, KTR_ATTR_LINKED, 2361187357Sjeff sched_tdname(curthread)); 2362187357Sjeff KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 2363187357Sjeff KTR_ATTR_LINKED, sched_tdname(td)); 2364235459Srstone SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL, 2365235459Srstone flags & SRQ_PREEMPTED); 2366171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2367166108Sjeff /* 2368171482Sjeff * Recalculate the priority before we select the target cpu or 2369171482Sjeff * run-queue. 2370166108Sjeff */ 2371171482Sjeff if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2372171482Sjeff sched_priority(td); 2373171482Sjeff#ifdef SMP 2374171482Sjeff /* 2375171482Sjeff * Pick the destination cpu and if it isn't ours transfer to the 2376171482Sjeff * target cpu. 2377171482Sjeff */ 2378177435Sjeff cpu = sched_pickcpu(td, flags); 2379177435Sjeff tdq = sched_setcpu(td, cpu, flags); 2380171482Sjeff tdq_add(tdq, td, flags); 2381177009Sjeff if (cpu != PCPU_GET(cpuid)) { 2382177435Sjeff tdq_notify(tdq, td); 2383166108Sjeff return; 2384166108Sjeff } 2385171482Sjeff#else 2386171482Sjeff tdq = TDQ_SELF(); 2387171482Sjeff TDQ_LOCK(tdq); 2388171482Sjeff /* 2389171482Sjeff * Now that the thread is moving to the run-queue, set the lock 2390171482Sjeff * to the scheduler's lock. 2391171482Sjeff */ 2392171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2393171482Sjeff tdq_add(tdq, td, flags); 2394166108Sjeff#endif 2395171482Sjeff if (!(flags & SRQ_YIELDING)) 2396171482Sjeff sched_setpreempt(td); 2397109864Sjeff} 2398109864Sjeff 2399171482Sjeff/* 2400171482Sjeff * Remove a thread from a run-queue without running it. This is used 2401171482Sjeff * when we're stealing a thread from a remote queue. Otherwise all threads 2402171482Sjeff * exit by calling sched_exit_thread() and sched_throw() themselves. 2403171482Sjeff */ 2404109864Sjeffvoid 2405121127Sjeffsched_rem(struct thread *td) 2406109864Sjeff{ 2407164936Sjulian struct tdq *tdq; 2408113357Sjeff 2409187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 2410187357Sjeff "prio:%d", td->td_priority); 2411235459Srstone SDT_PROBE3(sched, , , dequeue, td, td->td_proc, NULL); 2412177435Sjeff tdq = TDQ_CPU(td->td_sched->ts_cpu); 2413171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2414171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2415166190Sjeff KASSERT(TD_ON_RUNQ(td), 2416164936Sjulian ("sched_rem: thread not on run queue")); 2417177435Sjeff tdq_runq_rem(tdq, td); 2418177435Sjeff tdq_load_rem(tdq, td); 2419166190Sjeff TD_SET_CAN_RUN(td); 2420176735Sjeff if (td->td_priority == tdq->tdq_lowpri) 2421176735Sjeff tdq_setlowpri(tdq, NULL); 2422109864Sjeff} 2423109864Sjeff 2424171482Sjeff/* 2425171482Sjeff * Fetch cpu utilization information. Updates on demand. 2426171482Sjeff */ 2427109864Sjefffixpt_t 2428121127Sjeffsched_pctcpu(struct thread *td) 2429109864Sjeff{ 2430109864Sjeff fixpt_t pctcpu; 2431164936Sjulian struct td_sched *ts; 2432109864Sjeff 2433109864Sjeff pctcpu = 0; 2434164936Sjulian ts = td->td_sched; 2435164936Sjulian if (ts == NULL) 2436121290Sjeff return (0); 2437109864Sjeff 2438208787Sjhb THREAD_LOCK_ASSERT(td, MA_OWNED); 2439232917Smav sched_pctcpu_update(ts, TD_IS_RUNNING(td)); 2440164936Sjulian if (ts->ts_ticks) { 2441109864Sjeff int rtick; 2442109864Sjeff 2443109864Sjeff /* How many rtick per second ? */ 2444165762Sjeff rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2445165762Sjeff pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 2446109864Sjeff } 2447109864Sjeff 2448109864Sjeff return (pctcpu); 2449109864Sjeff} 2450109864Sjeff 2451176735Sjeff/* 2452176735Sjeff * Enforce affinity settings for a thread. Called after adjustments to 2453176735Sjeff * cpumask. 2454176735Sjeff */ 2455176729Sjeffvoid 2456176729Sjeffsched_affinity(struct thread *td) 2457176729Sjeff{ 2458176735Sjeff#ifdef SMP 2459176735Sjeff struct td_sched *ts; 2460176735Sjeff 2461176735Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2462176735Sjeff ts = td->td_sched; 2463176735Sjeff if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 2464176735Sjeff return; 2465189787Sjeff if (TD_ON_RUNQ(td)) { 2466189787Sjeff sched_rem(td); 2467189787Sjeff sched_add(td, SRQ_BORING); 2468189787Sjeff return; 2469189787Sjeff } 2470176735Sjeff if (!TD_IS_RUNNING(td)) 2471176735Sjeff return; 2472176735Sjeff /* 2473212115Smdf * Force a switch before returning to userspace. If the 2474212115Smdf * target thread is not running locally send an ipi to force 2475212115Smdf * the issue. 2476176735Sjeff */ 2477212974Sjhb td->td_flags |= TDF_NEEDRESCHED; 2478212115Smdf if (td != curthread) 2479212115Smdf ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 2480176735Sjeff#endif 2481176729Sjeff} 2482176729Sjeff 2483171482Sjeff/* 2484171482Sjeff * Bind a thread to a target cpu. 2485171482Sjeff */ 2486122038Sjeffvoid 2487122038Sjeffsched_bind(struct thread *td, int cpu) 2488122038Sjeff{ 2489164936Sjulian struct td_sched *ts; 2490122038Sjeff 2491171713Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 2492208391Sjhb KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 2493164936Sjulian ts = td->td_sched; 2494166137Sjeff if (ts->ts_flags & TSF_BOUND) 2495166152Sjeff sched_unbind(td); 2496212115Smdf KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2497164936Sjulian ts->ts_flags |= TSF_BOUND; 2498166137Sjeff sched_pin(); 2499123433Sjeff if (PCPU_GET(cpuid) == cpu) 2500122038Sjeff return; 2501166137Sjeff ts->ts_cpu = cpu; 2502122038Sjeff /* When we return from mi_switch we'll be on the correct cpu. */ 2503131527Sphk mi_switch(SW_VOL, NULL); 2504122038Sjeff} 2505122038Sjeff 2506171482Sjeff/* 2507171482Sjeff * Release a bound thread. 2508171482Sjeff */ 2509122038Sjeffvoid 2510122038Sjeffsched_unbind(struct thread *td) 2511122038Sjeff{ 2512165762Sjeff struct td_sched *ts; 2513165762Sjeff 2514170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2515208391Sjhb KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 2516165762Sjeff ts = td->td_sched; 2517166137Sjeff if ((ts->ts_flags & TSF_BOUND) == 0) 2518166137Sjeff return; 2519165762Sjeff ts->ts_flags &= ~TSF_BOUND; 2520165762Sjeff sched_unpin(); 2521122038Sjeff} 2522122038Sjeff 2523109864Sjeffint 2524145256Sjkoshysched_is_bound(struct thread *td) 2525145256Sjkoshy{ 2526170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2527164936Sjulian return (td->td_sched->ts_flags & TSF_BOUND); 2528145256Sjkoshy} 2529145256Sjkoshy 2530171482Sjeff/* 2531171482Sjeff * Basic yield call. 2532171482Sjeff */ 2533159630Sdavidxuvoid 2534159630Sdavidxusched_relinquish(struct thread *td) 2535159630Sdavidxu{ 2536170293Sjeff thread_lock(td); 2537178272Sjeff mi_switch(SW_VOL | SWT_RELINQUISH, NULL); 2538170293Sjeff thread_unlock(td); 2539159630Sdavidxu} 2540159630Sdavidxu 2541171482Sjeff/* 2542171482Sjeff * Return the total system load. 2543171482Sjeff */ 2544145256Sjkoshyint 2545125289Sjeffsched_load(void) 2546125289Sjeff{ 2547125289Sjeff#ifdef SMP 2548125289Sjeff int total; 2549125289Sjeff int i; 2550125289Sjeff 2551125289Sjeff total = 0; 2552209059Sjhb CPU_FOREACH(i) 2553176735Sjeff total += TDQ_CPU(i)->tdq_sysload; 2554125289Sjeff return (total); 2555125289Sjeff#else 2556165620Sjeff return (TDQ_SELF()->tdq_sysload); 2557125289Sjeff#endif 2558125289Sjeff} 2559125289Sjeff 2560125289Sjeffint 2561109864Sjeffsched_sizeof_proc(void) 2562109864Sjeff{ 2563109864Sjeff return (sizeof(struct proc)); 2564109864Sjeff} 2565109864Sjeff 2566109864Sjeffint 2567109864Sjeffsched_sizeof_thread(void) 2568109864Sjeff{ 2569109864Sjeff return (sizeof(struct thread) + sizeof(struct td_sched)); 2570109864Sjeff} 2571159570Sdavidxu 2572191676Sjeff#ifdef SMP 2573191676Sjeff#define TDQ_IDLESPIN(tdq) \ 2574191676Sjeff ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 2575191676Sjeff#else 2576191676Sjeff#define TDQ_IDLESPIN(tdq) 1 2577191676Sjeff#endif 2578191676Sjeff 2579166190Sjeff/* 2580166190Sjeff * The actual idle process. 2581166190Sjeff */ 2582166190Sjeffvoid 2583166190Sjeffsched_idletd(void *dummy) 2584166190Sjeff{ 2585166190Sjeff struct thread *td; 2586171482Sjeff struct tdq *tdq; 2587178277Sjeff int switchcnt; 2588178277Sjeff int i; 2589166190Sjeff 2590191643Sjeff mtx_assert(&Giant, MA_NOTOWNED); 2591166190Sjeff td = curthread; 2592171482Sjeff tdq = TDQ_SELF(); 2593171482Sjeff for (;;) { 2594171482Sjeff#ifdef SMP 2595178277Sjeff if (tdq_idled(tdq) == 0) 2596178277Sjeff continue; 2597171482Sjeff#endif 2598178277Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2599178277Sjeff /* 2600178277Sjeff * If we're switching very frequently, spin while checking 2601178277Sjeff * for load rather than entering a low power state that 2602191643Sjeff * may require an IPI. However, don't do any busy 2603191643Sjeff * loops while on SMT machines as this simply steals 2604191643Sjeff * cycles from cores doing useful work. 2605178277Sjeff */ 2606191676Sjeff if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 2607178277Sjeff for (i = 0; i < sched_idlespins; i++) { 2608178277Sjeff if (tdq->tdq_load) 2609178277Sjeff break; 2610178277Sjeff cpu_spinwait(); 2611178277Sjeff } 2612178277Sjeff } 2613191643Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2614212416Smav if (tdq->tdq_load == 0) { 2615212416Smav tdq->tdq_cpu_idle = 1; 2616212416Smav if (tdq->tdq_load == 0) { 2617212541Smav cpu_idle(switchcnt > sched_idlespinthresh * 4); 2618212416Smav tdq->tdq_switchcnt++; 2619212416Smav } 2620212416Smav tdq->tdq_cpu_idle = 0; 2621212416Smav } 2622178277Sjeff if (tdq->tdq_load) { 2623178277Sjeff thread_lock(td); 2624178277Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 2625178277Sjeff thread_unlock(td); 2626178277Sjeff } 2627171482Sjeff } 2628166190Sjeff} 2629166190Sjeff 2630170293Sjeff/* 2631170293Sjeff * A CPU is entering for the first time or a thread is exiting. 2632170293Sjeff */ 2633170293Sjeffvoid 2634170293Sjeffsched_throw(struct thread *td) 2635170293Sjeff{ 2636172411Sjeff struct thread *newtd; 2637171482Sjeff struct tdq *tdq; 2638171482Sjeff 2639171482Sjeff tdq = TDQ_SELF(); 2640170293Sjeff if (td == NULL) { 2641171482Sjeff /* Correct spinlock nesting and acquire the correct lock. */ 2642171482Sjeff TDQ_LOCK(tdq); 2643170293Sjeff spinlock_exit(); 2644229429Sjhb PCPU_SET(switchtime, cpu_ticks()); 2645229429Sjhb PCPU_SET(switchticks, ticks); 2646170293Sjeff } else { 2647171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2648177435Sjeff tdq_load_rem(tdq, td); 2649174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 2650170293Sjeff } 2651170293Sjeff KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count")); 2652172411Sjeff newtd = choosethread(); 2653172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 2654172411Sjeff cpu_throw(td, newtd); /* doesn't return */ 2655170293Sjeff} 2656170293Sjeff 2657171482Sjeff/* 2658171482Sjeff * This is called from fork_exit(). Just acquire the correct locks and 2659171482Sjeff * let fork do the rest of the work. 2660171482Sjeff */ 2661170293Sjeffvoid 2662170600Sjeffsched_fork_exit(struct thread *td) 2663170293Sjeff{ 2664171482Sjeff struct td_sched *ts; 2665171482Sjeff struct tdq *tdq; 2666171482Sjeff int cpuid; 2667170293Sjeff 2668170293Sjeff /* 2669170293Sjeff * Finish setting up thread glue so that it begins execution in a 2670171482Sjeff * non-nested critical section with the scheduler lock held. 2671170293Sjeff */ 2672171482Sjeff cpuid = PCPU_GET(cpuid); 2673171482Sjeff tdq = TDQ_CPU(cpuid); 2674171482Sjeff ts = td->td_sched; 2675171482Sjeff if (TD_IS_IDLETHREAD(td)) 2676171482Sjeff td->td_lock = TDQ_LOCKPTR(tdq); 2677171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2678171482Sjeff td->td_oncpu = cpuid; 2679172411Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 2680174629Sjeff lock_profile_obtain_lock_success( 2681174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 2682170293Sjeff} 2683170293Sjeff 2684187357Sjeff/* 2685187357Sjeff * Create on first use to catch odd startup conditons. 2686187357Sjeff */ 2687187357Sjeffchar * 2688187357Sjeffsched_tdname(struct thread *td) 2689187357Sjeff{ 2690187357Sjeff#ifdef KTR 2691187357Sjeff struct td_sched *ts; 2692187357Sjeff 2693187357Sjeff ts = td->td_sched; 2694187357Sjeff if (ts->ts_name[0] == '\0') 2695187357Sjeff snprintf(ts->ts_name, sizeof(ts->ts_name), 2696187357Sjeff "%s tid %d", td->td_name, td->td_tid); 2697187357Sjeff return (ts->ts_name); 2698187357Sjeff#else 2699187357Sjeff return (td->td_name); 2700187357Sjeff#endif 2701187357Sjeff} 2702187357Sjeff 2703232700Sjhb#ifdef KTR 2704232700Sjhbvoid 2705232700Sjhbsched_clear_tdname(struct thread *td) 2706232700Sjhb{ 2707232700Sjhb struct td_sched *ts; 2708232700Sjhb 2709232700Sjhb ts = td->td_sched; 2710232700Sjhb ts->ts_name[0] = '\0'; 2711232700Sjhb} 2712232700Sjhb#endif 2713232700Sjhb 2714184439Sivoras#ifdef SMP 2715184439Sivoras 2716184439Sivoras/* 2717184439Sivoras * Build the CPU topology dump string. Is recursively called to collect 2718184439Sivoras * the topology tree. 2719184439Sivoras */ 2720184439Sivorasstatic int 2721184439Sivorassysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 2722184439Sivoras int indent) 2723184439Sivoras{ 2724222813Sattilio char cpusetbuf[CPUSETBUFSIZ]; 2725184439Sivoras int i, first; 2726184439Sivoras 2727184439Sivoras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 2728212821Savg "", 1 + indent / 2, cg->cg_level); 2729222813Sattilio sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"%s\">", indent, "", 2730222813Sattilio cg->cg_count, cpusetobj_strprint(cpusetbuf, &cg->cg_mask)); 2731184439Sivoras first = TRUE; 2732184439Sivoras for (i = 0; i < MAXCPU; i++) { 2733222813Sattilio if (CPU_ISSET(i, &cg->cg_mask)) { 2734184439Sivoras if (!first) 2735184439Sivoras sbuf_printf(sb, ", "); 2736184439Sivoras else 2737184439Sivoras first = FALSE; 2738184439Sivoras sbuf_printf(sb, "%d", i); 2739184439Sivoras } 2740184439Sivoras } 2741184439Sivoras sbuf_printf(sb, "</cpu>\n"); 2742184439Sivoras 2743184439Sivoras if (cg->cg_flags != 0) { 2744210117Sivoras sbuf_printf(sb, "%*s <flags>", indent, ""); 2745184439Sivoras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 2746208982Sivoras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 2747208983Sivoras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2748208983Sivoras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 2749191643Sjeff if ((cg->cg_flags & CG_FLAG_SMT) != 0) 2750208983Sivoras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 2751210117Sivoras sbuf_printf(sb, "</flags>\n"); 2752184439Sivoras } 2753184439Sivoras 2754184439Sivoras if (cg->cg_children > 0) { 2755184439Sivoras sbuf_printf(sb, "%*s <children>\n", indent, ""); 2756184439Sivoras for (i = 0; i < cg->cg_children; i++) 2757184439Sivoras sysctl_kern_sched_topology_spec_internal(sb, 2758184439Sivoras &cg->cg_child[i], indent+2); 2759184439Sivoras sbuf_printf(sb, "%*s </children>\n", indent, ""); 2760184439Sivoras } 2761184439Sivoras sbuf_printf(sb, "%*s</group>\n", indent, ""); 2762184439Sivoras return (0); 2763184439Sivoras} 2764184439Sivoras 2765184439Sivoras/* 2766184439Sivoras * Sysctl handler for retrieving topology dump. It's a wrapper for 2767184439Sivoras * the recursive sysctl_kern_smp_topology_spec_internal(). 2768184439Sivoras */ 2769184439Sivorasstatic int 2770184439Sivorassysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 2771184439Sivoras{ 2772184439Sivoras struct sbuf *topo; 2773184439Sivoras int err; 2774184439Sivoras 2775184439Sivoras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 2776184439Sivoras 2777184570Sivoras topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND); 2778184439Sivoras if (topo == NULL) 2779184439Sivoras return (ENOMEM); 2780184439Sivoras 2781184439Sivoras sbuf_printf(topo, "<groups>\n"); 2782184439Sivoras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 2783184439Sivoras sbuf_printf(topo, "</groups>\n"); 2784184439Sivoras 2785184439Sivoras if (err == 0) { 2786184439Sivoras sbuf_finish(topo); 2787184439Sivoras err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo)); 2788184439Sivoras } 2789184439Sivoras sbuf_delete(topo); 2790184439Sivoras return (err); 2791184439Sivoras} 2792214510Sdavidxu 2793184439Sivoras#endif 2794184439Sivoras 2795177435SjeffSYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2796171482SjeffSYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 2797165762Sjeff "Scheduler name"); 2798171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 2799171482Sjeff "Slice size for timeshare threads"); 2800171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 2801171482Sjeff "Interactivity score threshold"); 2802171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh, 2803171482Sjeff 0,"Min priority for preemption, lower priorities have greater precedence"); 2804177085SjeffSYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 2805177085Sjeff 0,"Controls whether static kernel priorities are assigned to sleeping threads."); 2806178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 2807178277Sjeff 0,"Number of times idle will spin waiting for new work."); 2808178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh, 2809178277Sjeff 0,"Threshold before we will permit idle spinning."); 2810166108Sjeff#ifdef SMP 2811171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 2812171482Sjeff "Number of hz ticks to keep thread affinity for"); 2813171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 2814171482Sjeff "Enables the long-term load balancer"); 2815172409SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 2816172409Sjeff &balance_interval, 0, 2817172409Sjeff "Average frequency in stathz ticks to run the long-term balancer"); 2818171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 2819171482Sjeff "Attempts to steal work from other cores before idling"); 2820171506SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 2821171506Sjeff "Minimum load on remote cpu before we'll steal"); 2822184439Sivoras 2823184439Sivoras/* Retrieve SMP topology */ 2824184439SivorasSYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 2825184439Sivoras CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 2826184439Sivoras "XML dump of detected CPU topology"); 2827214510Sdavidxu 2828166108Sjeff#endif 2829165762Sjeff 2830172264Sjeff/* ps compat. All cpu percentages from ULE are weighted. */ 2831172293Sjeffstatic int ccpu = 0; 2832165762SjeffSYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 2833