sched_ule.c revision 189787
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> 39116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/sched_ule.c 189787 2009-03-14 11:41:36Z jeff $"); 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> 56109864Sjeff#include <sys/smp.h> 57109864Sjeff#include <sys/sx.h> 58109864Sjeff#include <sys/sysctl.h> 59109864Sjeff#include <sys/sysproto.h> 60139453Sjhb#include <sys/turnstile.h> 61161599Sdavidxu#include <sys/umtx.h> 62109864Sjeff#include <sys/vmmeter.h> 63176735Sjeff#include <sys/cpuset.h> 64184439Sivoras#include <sys/sbuf.h> 65109864Sjeff#ifdef KTRACE 66109864Sjeff#include <sys/uio.h> 67109864Sjeff#include <sys/ktrace.h> 68109864Sjeff#endif 69109864Sjeff 70145256Sjkoshy#ifdef HWPMC_HOOKS 71145256Sjkoshy#include <sys/pmckern.h> 72145256Sjkoshy#endif 73145256Sjkoshy 74179297Sjb#ifdef KDTRACE_HOOKS 75179297Sjb#include <sys/dtrace_bsd.h> 76179297Sjbint dtrace_vtime_active; 77179297Sjbdtrace_vtime_switch_func_t dtrace_vtime_switch_func; 78179297Sjb#endif 79179297Sjb 80109864Sjeff#include <machine/cpu.h> 81121790Sjeff#include <machine/smp.h> 82109864Sjeff 83178215Smarcel#if defined(__sparc64__) || defined(__mips__) 84172345Sjeff#error "This architecture is not currently compatible with ULE" 85166190Sjeff#endif 86166190Sjeff 87171482Sjeff#define KTR_ULE 0 88166137Sjeff 89187679Sjeff#define TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX))) 90187679Sjeff#define TDQ_NAME_LEN (sizeof("sched lock ") + sizeof(__XSTRING(MAXCPU))) 91187357Sjeff#define TDQ_LOADNAME_LEN (PCPU_NAME_LEN + sizeof(" load")) 92187357Sjeff 93166137Sjeff/* 94171482Sjeff * Thread scheduler specific section. All fields are protected 95171482Sjeff * by the thread lock. 96146954Sjeff */ 97164936Sjulianstruct td_sched { 98171482Sjeff struct runq *ts_runq; /* Run-queue we're queued on. */ 99171482Sjeff short ts_flags; /* TSF_* flags. */ 100164936Sjulian u_char ts_cpu; /* CPU that we have affinity for. */ 101177009Sjeff int ts_rltick; /* Real last tick, for affinity. */ 102171482Sjeff int ts_slice; /* Ticks of slice remaining. */ 103171482Sjeff u_int ts_slptime; /* Number of ticks we vol. slept */ 104171482Sjeff u_int ts_runtime; /* Number of ticks we were running */ 105164936Sjulian int ts_ltick; /* Last tick that we were running on */ 106164936Sjulian int ts_ftick; /* First tick that we were running on */ 107164936Sjulian int ts_ticks; /* Tick count */ 108187357Sjeff#ifdef KTR 109187357Sjeff char ts_name[TS_NAME_LEN]; 110187357Sjeff#endif 111134791Sjulian}; 112164936Sjulian/* flags kept in ts_flags */ 113166108Sjeff#define TSF_BOUND 0x0001 /* Thread can not migrate. */ 114166108Sjeff#define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */ 115121790Sjeff 116164936Sjulianstatic struct td_sched td_sched0; 117109864Sjeff 118176735Sjeff#define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0) 119176735Sjeff#define THREAD_CAN_SCHED(td, cpu) \ 120176735Sjeff CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask) 121176735Sjeff 122109864Sjeff/* 123165762Sjeff * Cpu percentage computation macros and defines. 124111857Sjeff * 125165762Sjeff * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 126165762Sjeff * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 127165796Sjeff * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 128165762Sjeff * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 129165762Sjeff * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 130165762Sjeff * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 131165762Sjeff */ 132165762Sjeff#define SCHED_TICK_SECS 10 133165762Sjeff#define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 134165796Sjeff#define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 135165762Sjeff#define SCHED_TICK_SHIFT 10 136165762Sjeff#define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 137165830Sjeff#define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 138165762Sjeff 139165762Sjeff/* 140165762Sjeff * These macros determine priorities for non-interactive threads. They are 141165762Sjeff * assigned a priority based on their recent cpu utilization as expressed 142165762Sjeff * by the ratio of ticks to the tick total. NHALF priorities at the start 143165762Sjeff * and end of the MIN to MAX timeshare range are only reachable with negative 144165762Sjeff * or positive nice respectively. 145165762Sjeff * 146165762Sjeff * PRI_RANGE: Priority range for utilization dependent priorities. 147116642Sjeff * PRI_NRESV: Number of nice values. 148165762Sjeff * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 149165762Sjeff * PRI_NICE: Determines the part of the priority inherited from nice. 150109864Sjeff */ 151165762Sjeff#define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 152121869Sjeff#define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 153165762Sjeff#define SCHED_PRI_MIN (PRI_MIN_TIMESHARE + SCHED_PRI_NHALF) 154165762Sjeff#define SCHED_PRI_MAX (PRI_MAX_TIMESHARE - SCHED_PRI_NHALF) 155170787Sjeff#define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN) 156165762Sjeff#define SCHED_PRI_TICKS(ts) \ 157165762Sjeff (SCHED_TICK_HZ((ts)) / \ 158165827Sjeff (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 159165762Sjeff#define SCHED_PRI_NICE(nice) (nice) 160109864Sjeff 161109864Sjeff/* 162165762Sjeff * These determine the interactivity of a process. Interactivity differs from 163165762Sjeff * cpu utilization in that it expresses the voluntary time slept vs time ran 164165762Sjeff * while cpu utilization includes all time not running. This more accurately 165165762Sjeff * models the intent of the thread. 166109864Sjeff * 167110645Sjeff * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 168110645Sjeff * before throttling back. 169121868Sjeff * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 170116365Sjeff * INTERACT_MAX: Maximum interactivity value. Smaller is better. 171111857Sjeff * INTERACT_THRESH: Threshhold for placement on the current runq. 172109864Sjeff */ 173165762Sjeff#define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 174165762Sjeff#define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 175116365Sjeff#define SCHED_INTERACT_MAX (100) 176116365Sjeff#define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 177121126Sjeff#define SCHED_INTERACT_THRESH (30) 178111857Sjeff 179109864Sjeff/* 180165762Sjeff * tickincr: Converts a stathz tick into a hz domain scaled by 181165762Sjeff * the shift factor. Without the shift the error rate 182165762Sjeff * due to rounding would be unacceptably high. 183165762Sjeff * realstathz: stathz is sometimes 0 and run off of hz. 184165762Sjeff * sched_slice: Runtime of each thread before rescheduling. 185171482Sjeff * preempt_thresh: Priority threshold for preemption and remote IPIs. 186109864Sjeff */ 187165762Sjeffstatic int sched_interact = SCHED_INTERACT_THRESH; 188165762Sjeffstatic int realstathz; 189165762Sjeffstatic int tickincr; 190177009Sjeffstatic int sched_slice = 1; 191172345Sjeff#ifdef PREEMPTION 192172345Sjeff#ifdef FULL_PREEMPTION 193172345Sjeffstatic int preempt_thresh = PRI_MAX_IDLE; 194172345Sjeff#else 195171482Sjeffstatic int preempt_thresh = PRI_MIN_KERN; 196172345Sjeff#endif 197172345Sjeff#else 198172345Sjeffstatic int preempt_thresh = 0; 199172345Sjeff#endif 200177903Sjeffstatic int static_boost = PRI_MIN_TIMESHARE; 201178277Sjeffstatic int sched_idlespins = 10000; 202178277Sjeffstatic int sched_idlespinthresh = 4; 203109864Sjeff 204109864Sjeff/* 205171482Sjeff * tdq - per processor runqs and statistics. All fields are protected by the 206171482Sjeff * tdq_lock. The load and lowpri may be accessed without to avoid excess 207171482Sjeff * locking in sched_pickcpu(); 208109864Sjeff */ 209164936Sjulianstruct tdq { 210177009Sjeff /* Ordered to improve efficiency of cpu_search() and switch(). */ 211177009Sjeff struct mtx tdq_lock; /* run queue lock. */ 212176735Sjeff struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 213178277Sjeff volatile int tdq_load; /* Aggregate load. */ 214176735Sjeff int tdq_sysload; /* For loadavg, !ITHD load. */ 215177009Sjeff int tdq_transferable; /* Transferable thread count. */ 216178277Sjeff volatile int tdq_idlestate; /* State of the idle thread. */ 217178277Sjeff short tdq_switchcnt; /* Switches this tick. */ 218178277Sjeff short tdq_oldswitchcnt; /* Switches last tick. */ 219177009Sjeff u_char tdq_lowpri; /* Lowest priority thread. */ 220177009Sjeff u_char tdq_ipipending; /* IPI pending. */ 221166557Sjeff u_char tdq_idx; /* Current insert index. */ 222166557Sjeff u_char tdq_ridx; /* Current removal index. */ 223177009Sjeff struct runq tdq_realtime; /* real-time run queue. */ 224177009Sjeff struct runq tdq_timeshare; /* timeshare run queue. */ 225177009Sjeff struct runq tdq_idle; /* Queue of IDLE threads. */ 226187357Sjeff char tdq_name[TDQ_NAME_LEN]; 227187357Sjeff#ifdef KTR 228187357Sjeff char tdq_loadname[TDQ_LOADNAME_LEN]; 229187357Sjeff#endif 230171482Sjeff} __aligned(64); 231109864Sjeff 232178277Sjeff/* Idle thread states and config. */ 233178277Sjeff#define TDQ_RUNNING 1 234178277Sjeff#define TDQ_IDLE 2 235166108Sjeff 236123433Sjeff#ifdef SMP 237184439Sivorasstruct cpu_group *cpu_top; /* CPU topology */ 238123433Sjeff 239176735Sjeff#define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 240176735Sjeff#define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 241166108Sjeff 242123433Sjeff/* 243166108Sjeff * Run-time tunables. 244166108Sjeff */ 245171506Sjeffstatic int rebalance = 1; 246172409Sjeffstatic int balance_interval = 128; /* Default set in sched_initticks(). */ 247166108Sjeffstatic int affinity; 248172409Sjeffstatic int steal_htt = 1; 249171506Sjeffstatic int steal_idle = 1; 250171506Sjeffstatic int steal_thresh = 2; 251166108Sjeff 252166108Sjeff/* 253165620Sjeff * One thread queue per processor. 254109864Sjeff */ 255164936Sjulianstatic struct tdq tdq_cpu[MAXCPU]; 256172409Sjeffstatic struct tdq *balance_tdq; 257172409Sjeffstatic int balance_ticks; 258129982Sjeff 259164936Sjulian#define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 260164936Sjulian#define TDQ_CPU(x) (&tdq_cpu[(x)]) 261171713Sjeff#define TDQ_ID(x) ((int)((x) - tdq_cpu)) 262123433Sjeff#else /* !SMP */ 263164936Sjulianstatic struct tdq tdq_cpu; 264129982Sjeff 265170315Sjeff#define TDQ_ID(x) (0) 266164936Sjulian#define TDQ_SELF() (&tdq_cpu) 267164936Sjulian#define TDQ_CPU(x) (&tdq_cpu) 268110028Sjeff#endif 269109864Sjeff 270171482Sjeff#define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 271171482Sjeff#define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 272171482Sjeff#define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 273171482Sjeff#define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 274176735Sjeff#define TDQ_LOCKPTR(t) (&(t)->tdq_lock) 275171482Sjeff 276163709Sjbstatic void sched_priority(struct thread *); 277146954Sjeffstatic void sched_thread_priority(struct thread *, u_char); 278163709Sjbstatic int sched_interact_score(struct thread *); 279163709Sjbstatic void sched_interact_update(struct thread *); 280163709Sjbstatic void sched_interact_fork(struct thread *); 281164936Sjulianstatic void sched_pctcpu_update(struct td_sched *); 282109864Sjeff 283110267Sjeff/* Operations on per processor queues */ 284177435Sjeffstatic struct thread *tdq_choose(struct tdq *); 285164936Sjulianstatic void tdq_setup(struct tdq *); 286177435Sjeffstatic void tdq_load_add(struct tdq *, struct thread *); 287177435Sjeffstatic void tdq_load_rem(struct tdq *, struct thread *); 288177435Sjeffstatic __inline void tdq_runq_add(struct tdq *, struct thread *, int); 289177435Sjeffstatic __inline void tdq_runq_rem(struct tdq *, struct thread *); 290177005Sjeffstatic inline int sched_shouldpreempt(int, int, int); 291164936Sjulianvoid tdq_print(int cpu); 292165762Sjeffstatic void runq_print(struct runq *rq); 293171482Sjeffstatic void tdq_add(struct tdq *, struct thread *, int); 294110267Sjeff#ifdef SMP 295176735Sjeffstatic int tdq_move(struct tdq *, struct tdq *); 296171482Sjeffstatic int tdq_idled(struct tdq *); 297177435Sjeffstatic void tdq_notify(struct tdq *, struct thread *); 298177435Sjeffstatic struct thread *tdq_steal(struct tdq *, int); 299177435Sjeffstatic struct thread *runq_steal(struct runq *, int); 300177435Sjeffstatic int sched_pickcpu(struct thread *, int); 301172409Sjeffstatic void sched_balance(void); 302176735Sjeffstatic int sched_balance_pair(struct tdq *, struct tdq *); 303177435Sjeffstatic inline struct tdq *sched_setcpu(struct thread *, int, int); 304171482Sjeffstatic inline struct mtx *thread_block_switch(struct thread *); 305171482Sjeffstatic inline void thread_unblock_switch(struct thread *, struct mtx *); 306171713Sjeffstatic struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int); 307184439Sivorasstatic int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 308184439Sivorasstatic int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 309184439Sivoras struct cpu_group *cg, int indent); 310121790Sjeff#endif 311110028Sjeff 312165762Sjeffstatic void sched_setup(void *dummy); 313177253SrwatsonSYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 314165762Sjeff 315165762Sjeffstatic void sched_initticks(void *dummy); 316177253SrwatsonSYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 317177253Srwatson NULL); 318165762Sjeff 319171482Sjeff/* 320171482Sjeff * Print the threads waiting on a run-queue. 321171482Sjeff */ 322165762Sjeffstatic void 323165762Sjeffrunq_print(struct runq *rq) 324165762Sjeff{ 325165762Sjeff struct rqhead *rqh; 326177435Sjeff struct thread *td; 327165762Sjeff int pri; 328165762Sjeff int j; 329165762Sjeff int i; 330165762Sjeff 331165762Sjeff for (i = 0; i < RQB_LEN; i++) { 332165762Sjeff printf("\t\trunq bits %d 0x%zx\n", 333165762Sjeff i, rq->rq_status.rqb_bits[i]); 334165762Sjeff for (j = 0; j < RQB_BPW; j++) 335165762Sjeff if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 336165762Sjeff pri = j + (i << RQB_L2BPW); 337165762Sjeff rqh = &rq->rq_queues[pri]; 338177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 339165762Sjeff printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 340177435Sjeff td, td->td_name, td->td_priority, 341177435Sjeff td->td_rqindex, pri); 342165762Sjeff } 343165762Sjeff } 344165762Sjeff } 345165762Sjeff} 346165762Sjeff 347171482Sjeff/* 348171482Sjeff * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 349171482Sjeff */ 350113357Sjeffvoid 351164936Sjuliantdq_print(int cpu) 352110267Sjeff{ 353164936Sjulian struct tdq *tdq; 354112994Sjeff 355164936Sjulian tdq = TDQ_CPU(cpu); 356112994Sjeff 357171713Sjeff printf("tdq %d:\n", TDQ_ID(tdq)); 358176735Sjeff printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 359176735Sjeff printf("\tLock name: %s\n", tdq->tdq_name); 360165620Sjeff printf("\tload: %d\n", tdq->tdq_load); 361178277Sjeff printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 362178277Sjeff printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 363178277Sjeff printf("\tidle state: %d\n", tdq->tdq_idlestate); 364171482Sjeff printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 365165766Sjeff printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 366178277Sjeff printf("\tload transferable: %d\n", tdq->tdq_transferable); 367178277Sjeff printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 368165762Sjeff printf("\trealtime runq:\n"); 369165762Sjeff runq_print(&tdq->tdq_realtime); 370165762Sjeff printf("\ttimeshare runq:\n"); 371165762Sjeff runq_print(&tdq->tdq_timeshare); 372165762Sjeff printf("\tidle runq:\n"); 373165762Sjeff runq_print(&tdq->tdq_idle); 374113357Sjeff} 375112994Sjeff 376177005Sjeffstatic inline int 377177005Sjeffsched_shouldpreempt(int pri, int cpri, int remote) 378177005Sjeff{ 379177005Sjeff /* 380177005Sjeff * If the new priority is not better than the current priority there is 381177005Sjeff * nothing to do. 382177005Sjeff */ 383177005Sjeff if (pri >= cpri) 384177005Sjeff return (0); 385177005Sjeff /* 386177005Sjeff * Always preempt idle. 387177005Sjeff */ 388177005Sjeff if (cpri >= PRI_MIN_IDLE) 389177005Sjeff return (1); 390177005Sjeff /* 391177005Sjeff * If preemption is disabled don't preempt others. 392177005Sjeff */ 393177005Sjeff if (preempt_thresh == 0) 394177005Sjeff return (0); 395177005Sjeff /* 396177005Sjeff * Preempt if we exceed the threshold. 397177005Sjeff */ 398177005Sjeff if (pri <= preempt_thresh) 399177005Sjeff return (1); 400177005Sjeff /* 401177005Sjeff * If we're realtime or better and there is timeshare or worse running 402177005Sjeff * preempt only remote processors. 403177005Sjeff */ 404177005Sjeff if (remote && pri <= PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME) 405177005Sjeff return (1); 406177005Sjeff return (0); 407177005Sjeff} 408177005Sjeff 409171482Sjeff#define TS_RQ_PPQ (((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS) 410171482Sjeff/* 411171482Sjeff * Add a thread to the actual run-queue. Keeps transferable counts up to 412171482Sjeff * date with what is actually on the run-queue. Selects the correct 413171482Sjeff * queue position for timeshare threads. 414171482Sjeff */ 415122744Sjeffstatic __inline void 416177435Sjefftdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 417122744Sjeff{ 418177435Sjeff struct td_sched *ts; 419177042Sjeff u_char pri; 420177042Sjeff 421171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 422177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 423177009Sjeff 424177435Sjeff pri = td->td_priority; 425177435Sjeff ts = td->td_sched; 426177435Sjeff TD_SET_RUNQ(td); 427177435Sjeff if (THREAD_CAN_MIGRATE(td)) { 428165620Sjeff tdq->tdq_transferable++; 429164936Sjulian ts->ts_flags |= TSF_XFERABLE; 430123433Sjeff } 431177042Sjeff if (pri <= PRI_MAX_REALTIME) { 432177042Sjeff ts->ts_runq = &tdq->tdq_realtime; 433177042Sjeff } else if (pri <= PRI_MAX_TIMESHARE) { 434177042Sjeff ts->ts_runq = &tdq->tdq_timeshare; 435165762Sjeff KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE, 436165762Sjeff ("Invalid priority %d on timeshare runq", pri)); 437165762Sjeff /* 438165762Sjeff * This queue contains only priorities between MIN and MAX 439165762Sjeff * realtime. Use the whole queue to represent these values. 440165762Sjeff */ 441171713Sjeff if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 442165762Sjeff pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ; 443165762Sjeff pri = (pri + tdq->tdq_idx) % RQ_NQS; 444165766Sjeff /* 445165766Sjeff * This effectively shortens the queue by one so we 446165766Sjeff * can have a one slot difference between idx and 447165766Sjeff * ridx while we wait for threads to drain. 448165766Sjeff */ 449165766Sjeff if (tdq->tdq_ridx != tdq->tdq_idx && 450165766Sjeff pri == tdq->tdq_ridx) 451167664Sjeff pri = (unsigned char)(pri - 1) % RQ_NQS; 452165762Sjeff } else 453165766Sjeff pri = tdq->tdq_ridx; 454177435Sjeff runq_add_pri(ts->ts_runq, td, pri, flags); 455177042Sjeff return; 456165762Sjeff } else 457177009Sjeff ts->ts_runq = &tdq->tdq_idle; 458177435Sjeff runq_add(ts->ts_runq, td, flags); 459177009Sjeff} 460177009Sjeff 461171482Sjeff/* 462171482Sjeff * Remove a thread from a run-queue. This typically happens when a thread 463171482Sjeff * is selected to run. Running threads are not on the queue and the 464171482Sjeff * transferable count does not reflect them. 465171482Sjeff */ 466122744Sjeffstatic __inline void 467177435Sjefftdq_runq_rem(struct tdq *tdq, struct thread *td) 468122744Sjeff{ 469177435Sjeff struct td_sched *ts; 470177435Sjeff 471177435Sjeff ts = td->td_sched; 472171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 473171482Sjeff KASSERT(ts->ts_runq != NULL, 474177435Sjeff ("tdq_runq_remove: thread %p null ts_runq", td)); 475164936Sjulian if (ts->ts_flags & TSF_XFERABLE) { 476165620Sjeff tdq->tdq_transferable--; 477164936Sjulian ts->ts_flags &= ~TSF_XFERABLE; 478123433Sjeff } 479165766Sjeff if (ts->ts_runq == &tdq->tdq_timeshare) { 480165766Sjeff if (tdq->tdq_idx != tdq->tdq_ridx) 481177435Sjeff runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 482165766Sjeff else 483177435Sjeff runq_remove_idx(ts->ts_runq, td, NULL); 484165766Sjeff } else 485177435Sjeff runq_remove(ts->ts_runq, td); 486122744Sjeff} 487122744Sjeff 488171482Sjeff/* 489171482Sjeff * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 490171482Sjeff * for this thread to the referenced thread queue. 491171482Sjeff */ 492113357Sjeffstatic void 493177435Sjefftdq_load_add(struct tdq *tdq, struct thread *td) 494113357Sjeff{ 495171482Sjeff 496171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 497177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 498177902Sjeff 499165620Sjeff tdq->tdq_load++; 500177902Sjeff if ((td->td_proc->p_flag & P_NOLOAD) == 0) 501177902Sjeff tdq->tdq_sysload++; 502187357Sjeff KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 503110267Sjeff} 504113357Sjeff 505171482Sjeff/* 506171482Sjeff * Remove the load from a thread that is transitioning to a sleep state or 507171482Sjeff * exiting. 508171482Sjeff */ 509112994Sjeffstatic void 510177435Sjefftdq_load_rem(struct tdq *tdq, struct thread *td) 511110267Sjeff{ 512171482Sjeff 513177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 514171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 515171482Sjeff KASSERT(tdq->tdq_load != 0, 516171713Sjeff ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 517177902Sjeff 518165620Sjeff tdq->tdq_load--; 519177902Sjeff if ((td->td_proc->p_flag & P_NOLOAD) == 0) 520177902Sjeff tdq->tdq_sysload--; 521187357Sjeff KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 522110267Sjeff} 523110267Sjeff 524176735Sjeff/* 525176735Sjeff * Set lowpri to its exact value by searching the run-queue and 526176735Sjeff * evaluating curthread. curthread may be passed as an optimization. 527176735Sjeff */ 528176735Sjeffstatic void 529176735Sjefftdq_setlowpri(struct tdq *tdq, struct thread *ctd) 530176735Sjeff{ 531176735Sjeff struct thread *td; 532176735Sjeff 533176735Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 534176735Sjeff if (ctd == NULL) 535176735Sjeff ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread; 536177435Sjeff td = tdq_choose(tdq); 537177435Sjeff if (td == NULL || td->td_priority > ctd->td_priority) 538176735Sjeff tdq->tdq_lowpri = ctd->td_priority; 539176735Sjeff else 540176735Sjeff tdq->tdq_lowpri = td->td_priority; 541176735Sjeff} 542176735Sjeff 543113357Sjeff#ifdef SMP 544176735Sjeffstruct cpu_search { 545176735Sjeff cpumask_t cs_mask; /* Mask of valid cpus. */ 546176735Sjeff u_int cs_load; 547176735Sjeff u_int cs_cpu; 548176735Sjeff int cs_limit; /* Min priority for low min load for high. */ 549176735Sjeff}; 550176735Sjeff 551176735Sjeff#define CPU_SEARCH_LOWEST 0x1 552176735Sjeff#define CPU_SEARCH_HIGHEST 0x2 553176735Sjeff#define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST) 554176735Sjeff 555176735Sjeff#define CPUMASK_FOREACH(cpu, mask) \ 556176735Sjeff for ((cpu) = 0; (cpu) < sizeof((mask)) * 8; (cpu)++) \ 557176735Sjeff if ((mask) & 1 << (cpu)) 558176735Sjeff 559177169Sjhbstatic __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low, 560176735Sjeff struct cpu_search *high, const int match); 561176735Sjeffint cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low); 562176735Sjeffint cpu_search_highest(struct cpu_group *cg, struct cpu_search *high); 563176735Sjeffint cpu_search_both(struct cpu_group *cg, struct cpu_search *low, 564176735Sjeff struct cpu_search *high); 565176735Sjeff 566116069Sjeff/* 567176735Sjeff * This routine compares according to the match argument and should be 568176735Sjeff * reduced in actual instantiations via constant propagation and dead code 569176735Sjeff * elimination. 570176735Sjeff */ 571176735Sjeffstatic __inline int 572176735Sjeffcpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high, 573176735Sjeff const int match) 574176735Sjeff{ 575176735Sjeff struct tdq *tdq; 576176735Sjeff 577176735Sjeff tdq = TDQ_CPU(cpu); 578176735Sjeff if (match & CPU_SEARCH_LOWEST) 579176735Sjeff if (low->cs_mask & (1 << cpu) && 580176735Sjeff tdq->tdq_load < low->cs_load && 581176735Sjeff tdq->tdq_lowpri > low->cs_limit) { 582176735Sjeff low->cs_cpu = cpu; 583176735Sjeff low->cs_load = tdq->tdq_load; 584176735Sjeff } 585176735Sjeff if (match & CPU_SEARCH_HIGHEST) 586176735Sjeff if (high->cs_mask & (1 << cpu) && 587176735Sjeff tdq->tdq_load >= high->cs_limit && 588176735Sjeff tdq->tdq_load > high->cs_load && 589176735Sjeff tdq->tdq_transferable) { 590176735Sjeff high->cs_cpu = cpu; 591176735Sjeff high->cs_load = tdq->tdq_load; 592176735Sjeff } 593176735Sjeff return (tdq->tdq_load); 594176735Sjeff} 595176735Sjeff 596176735Sjeff/* 597176735Sjeff * Search the tree of cpu_groups for the lowest or highest loaded cpu 598176735Sjeff * according to the match argument. This routine actually compares the 599176735Sjeff * load on all paths through the tree and finds the least loaded cpu on 600176735Sjeff * the least loaded path, which may differ from the least loaded cpu in 601176735Sjeff * the system. This balances work among caches and busses. 602116069Sjeff * 603176735Sjeff * This inline is instantiated in three forms below using constants for the 604176735Sjeff * match argument. It is reduced to the minimum set for each case. It is 605176735Sjeff * also recursive to the depth of the tree. 606116069Sjeff */ 607177169Sjhbstatic __inline int 608176735Sjeffcpu_search(struct cpu_group *cg, struct cpu_search *low, 609176735Sjeff struct cpu_search *high, const int match) 610176735Sjeff{ 611176735Sjeff int total; 612176735Sjeff 613176735Sjeff total = 0; 614176735Sjeff if (cg->cg_children) { 615176735Sjeff struct cpu_search lgroup; 616176735Sjeff struct cpu_search hgroup; 617176735Sjeff struct cpu_group *child; 618176735Sjeff u_int lload; 619176735Sjeff int hload; 620176735Sjeff int load; 621176735Sjeff int i; 622176735Sjeff 623176735Sjeff lload = -1; 624176735Sjeff hload = -1; 625176735Sjeff for (i = 0; i < cg->cg_children; i++) { 626176735Sjeff child = &cg->cg_child[i]; 627176735Sjeff if (match & CPU_SEARCH_LOWEST) { 628176735Sjeff lgroup = *low; 629176735Sjeff lgroup.cs_load = -1; 630176735Sjeff } 631176735Sjeff if (match & CPU_SEARCH_HIGHEST) { 632176735Sjeff hgroup = *high; 633176735Sjeff lgroup.cs_load = 0; 634176735Sjeff } 635176735Sjeff switch (match) { 636176735Sjeff case CPU_SEARCH_LOWEST: 637176735Sjeff load = cpu_search_lowest(child, &lgroup); 638176735Sjeff break; 639176735Sjeff case CPU_SEARCH_HIGHEST: 640176735Sjeff load = cpu_search_highest(child, &hgroup); 641176735Sjeff break; 642176735Sjeff case CPU_SEARCH_BOTH: 643176735Sjeff load = cpu_search_both(child, &lgroup, &hgroup); 644176735Sjeff break; 645176735Sjeff } 646176735Sjeff total += load; 647176735Sjeff if (match & CPU_SEARCH_LOWEST) 648176735Sjeff if (load < lload || low->cs_cpu == -1) { 649176735Sjeff *low = lgroup; 650176735Sjeff lload = load; 651176735Sjeff } 652176735Sjeff if (match & CPU_SEARCH_HIGHEST) 653176735Sjeff if (load > hload || high->cs_cpu == -1) { 654176735Sjeff hload = load; 655176735Sjeff *high = hgroup; 656176735Sjeff } 657176735Sjeff } 658176735Sjeff } else { 659176735Sjeff int cpu; 660176735Sjeff 661176735Sjeff CPUMASK_FOREACH(cpu, cg->cg_mask) 662176735Sjeff total += cpu_compare(cpu, low, high, match); 663176735Sjeff } 664176735Sjeff return (total); 665176735Sjeff} 666176735Sjeff 667176735Sjeff/* 668176735Sjeff * cpu_search instantiations must pass constants to maintain the inline 669176735Sjeff * optimization. 670176735Sjeff */ 671176735Sjeffint 672176735Sjeffcpu_search_lowest(struct cpu_group *cg, struct cpu_search *low) 673176735Sjeff{ 674176735Sjeff return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST); 675176735Sjeff} 676176735Sjeff 677176735Sjeffint 678176735Sjeffcpu_search_highest(struct cpu_group *cg, struct cpu_search *high) 679176735Sjeff{ 680176735Sjeff return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST); 681176735Sjeff} 682176735Sjeff 683176735Sjeffint 684176735Sjeffcpu_search_both(struct cpu_group *cg, struct cpu_search *low, 685176735Sjeff struct cpu_search *high) 686176735Sjeff{ 687176735Sjeff return cpu_search(cg, low, high, CPU_SEARCH_BOTH); 688176735Sjeff} 689176735Sjeff 690176735Sjeff/* 691176735Sjeff * Find the cpu with the least load via the least loaded path that has a 692176735Sjeff * lowpri greater than pri pri. A pri of -1 indicates any priority is 693176735Sjeff * acceptable. 694176735Sjeff */ 695176735Sjeffstatic inline int 696176735Sjeffsched_lowest(struct cpu_group *cg, cpumask_t mask, int pri) 697176735Sjeff{ 698176735Sjeff struct cpu_search low; 699176735Sjeff 700176735Sjeff low.cs_cpu = -1; 701176735Sjeff low.cs_load = -1; 702176735Sjeff low.cs_mask = mask; 703176735Sjeff low.cs_limit = pri; 704176735Sjeff cpu_search_lowest(cg, &low); 705176735Sjeff return low.cs_cpu; 706176735Sjeff} 707176735Sjeff 708176735Sjeff/* 709176735Sjeff * Find the cpu with the highest load via the highest loaded path. 710176735Sjeff */ 711176735Sjeffstatic inline int 712176735Sjeffsched_highest(struct cpu_group *cg, cpumask_t mask, int minload) 713176735Sjeff{ 714176735Sjeff struct cpu_search high; 715176735Sjeff 716176735Sjeff high.cs_cpu = -1; 717176735Sjeff high.cs_load = 0; 718176735Sjeff high.cs_mask = mask; 719176735Sjeff high.cs_limit = minload; 720176735Sjeff cpu_search_highest(cg, &high); 721176735Sjeff return high.cs_cpu; 722176735Sjeff} 723176735Sjeff 724176735Sjeff/* 725176735Sjeff * Simultaneously find the highest and lowest loaded cpu reachable via 726176735Sjeff * cg. 727176735Sjeff */ 728176735Sjeffstatic inline void 729176735Sjeffsched_both(struct cpu_group *cg, cpumask_t mask, int *lowcpu, int *highcpu) 730176735Sjeff{ 731176735Sjeff struct cpu_search high; 732176735Sjeff struct cpu_search low; 733176735Sjeff 734176735Sjeff low.cs_cpu = -1; 735176735Sjeff low.cs_limit = -1; 736176735Sjeff low.cs_load = -1; 737176735Sjeff low.cs_mask = mask; 738176735Sjeff high.cs_load = 0; 739176735Sjeff high.cs_cpu = -1; 740176735Sjeff high.cs_limit = -1; 741176735Sjeff high.cs_mask = mask; 742176735Sjeff cpu_search_both(cg, &low, &high); 743176735Sjeff *lowcpu = low.cs_cpu; 744176735Sjeff *highcpu = high.cs_cpu; 745176735Sjeff return; 746176735Sjeff} 747176735Sjeff 748121790Sjeffstatic void 749176735Sjeffsched_balance_group(struct cpu_group *cg) 750116069Sjeff{ 751176735Sjeff cpumask_t mask; 752176735Sjeff int high; 753176735Sjeff int low; 754123487Sjeff int i; 755123487Sjeff 756176735Sjeff mask = -1; 757176735Sjeff for (;;) { 758176735Sjeff sched_both(cg, mask, &low, &high); 759176735Sjeff if (low == high || low == -1 || high == -1) 760176735Sjeff break; 761176735Sjeff if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low))) 762176735Sjeff break; 763123487Sjeff /* 764176735Sjeff * If we failed to move any threads determine which cpu 765176735Sjeff * to kick out of the set and try again. 766176735Sjeff */ 767176735Sjeff if (TDQ_CPU(high)->tdq_transferable == 0) 768176735Sjeff mask &= ~(1 << high); 769176735Sjeff else 770176735Sjeff mask &= ~(1 << low); 771123487Sjeff } 772176735Sjeff 773176735Sjeff for (i = 0; i < cg->cg_children; i++) 774176735Sjeff sched_balance_group(&cg->cg_child[i]); 775123487Sjeff} 776123487Sjeff 777123487Sjeffstatic void 778176735Sjeffsched_balance() 779123487Sjeff{ 780172409Sjeff struct tdq *tdq; 781123487Sjeff 782172409Sjeff /* 783172409Sjeff * Select a random time between .5 * balance_interval and 784172409Sjeff * 1.5 * balance_interval. 785172409Sjeff */ 786176735Sjeff balance_ticks = max(balance_interval / 2, 1); 787176735Sjeff balance_ticks += random() % balance_interval; 788171482Sjeff if (smp_started == 0 || rebalance == 0) 789171482Sjeff return; 790172409Sjeff tdq = TDQ_SELF(); 791172409Sjeff TDQ_UNLOCK(tdq); 792176735Sjeff sched_balance_group(cpu_top); 793172409Sjeff TDQ_LOCK(tdq); 794123487Sjeff} 795123487Sjeff 796171482Sjeff/* 797171482Sjeff * Lock two thread queues using their address to maintain lock order. 798171482Sjeff */ 799123487Sjeffstatic void 800171482Sjefftdq_lock_pair(struct tdq *one, struct tdq *two) 801171482Sjeff{ 802171482Sjeff if (one < two) { 803171482Sjeff TDQ_LOCK(one); 804171482Sjeff TDQ_LOCK_FLAGS(two, MTX_DUPOK); 805171482Sjeff } else { 806171482Sjeff TDQ_LOCK(two); 807171482Sjeff TDQ_LOCK_FLAGS(one, MTX_DUPOK); 808171482Sjeff } 809171482Sjeff} 810171482Sjeff 811171482Sjeff/* 812172409Sjeff * Unlock two thread queues. Order is not important here. 813172409Sjeff */ 814172409Sjeffstatic void 815172409Sjefftdq_unlock_pair(struct tdq *one, struct tdq *two) 816172409Sjeff{ 817172409Sjeff TDQ_UNLOCK(one); 818172409Sjeff TDQ_UNLOCK(two); 819172409Sjeff} 820172409Sjeff 821172409Sjeff/* 822171482Sjeff * Transfer load between two imbalanced thread queues. 823171482Sjeff */ 824176735Sjeffstatic int 825164936Sjuliansched_balance_pair(struct tdq *high, struct tdq *low) 826123487Sjeff{ 827123433Sjeff int transferable; 828116069Sjeff int high_load; 829116069Sjeff int low_load; 830176735Sjeff int moved; 831116069Sjeff int move; 832116069Sjeff int diff; 833116069Sjeff int i; 834116069Sjeff 835171482Sjeff tdq_lock_pair(high, low); 836176735Sjeff transferable = high->tdq_transferable; 837176735Sjeff high_load = high->tdq_load; 838176735Sjeff low_load = low->tdq_load; 839176735Sjeff moved = 0; 840116069Sjeff /* 841122744Sjeff * Determine what the imbalance is and then adjust that to how many 842165620Sjeff * threads we actually have to give up (transferable). 843122744Sjeff */ 844171482Sjeff if (transferable != 0) { 845171482Sjeff diff = high_load - low_load; 846171482Sjeff move = diff / 2; 847171482Sjeff if (diff & 0x1) 848171482Sjeff move++; 849171482Sjeff move = min(move, transferable); 850171482Sjeff for (i = 0; i < move; i++) 851176735Sjeff moved += tdq_move(high, low); 852172293Sjeff /* 853172293Sjeff * IPI the target cpu to force it to reschedule with the new 854172293Sjeff * workload. 855172293Sjeff */ 856172293Sjeff ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT); 857171482Sjeff } 858172409Sjeff tdq_unlock_pair(high, low); 859176735Sjeff return (moved); 860116069Sjeff} 861116069Sjeff 862171482Sjeff/* 863171482Sjeff * Move a thread from one thread queue to another. 864171482Sjeff */ 865176735Sjeffstatic int 866171482Sjefftdq_move(struct tdq *from, struct tdq *to) 867116069Sjeff{ 868171482Sjeff struct td_sched *ts; 869171482Sjeff struct thread *td; 870164936Sjulian struct tdq *tdq; 871171482Sjeff int cpu; 872116069Sjeff 873172409Sjeff TDQ_LOCK_ASSERT(from, MA_OWNED); 874172409Sjeff TDQ_LOCK_ASSERT(to, MA_OWNED); 875172409Sjeff 876164936Sjulian tdq = from; 877171482Sjeff cpu = TDQ_ID(to); 878177435Sjeff td = tdq_steal(tdq, cpu); 879177435Sjeff if (td == NULL) 880176735Sjeff return (0); 881177435Sjeff ts = td->td_sched; 882171482Sjeff /* 883171482Sjeff * Although the run queue is locked the thread may be blocked. Lock 884172409Sjeff * it to clear this and acquire the run-queue lock. 885171482Sjeff */ 886171482Sjeff thread_lock(td); 887172409Sjeff /* Drop recursive lock on from acquired via thread_lock(). */ 888171482Sjeff TDQ_UNLOCK(from); 889171482Sjeff sched_rem(td); 890166108Sjeff ts->ts_cpu = cpu; 891171482Sjeff td->td_lock = TDQ_LOCKPTR(to); 892171482Sjeff tdq_add(to, td, SRQ_YIELDING); 893176735Sjeff return (1); 894116069Sjeff} 895110267Sjeff 896171482Sjeff/* 897171482Sjeff * This tdq has idled. Try to steal a thread from another cpu and switch 898171482Sjeff * to it. 899171482Sjeff */ 900123433Sjeffstatic int 901164936Sjuliantdq_idled(struct tdq *tdq) 902121790Sjeff{ 903176735Sjeff struct cpu_group *cg; 904164936Sjulian struct tdq *steal; 905176735Sjeff cpumask_t mask; 906176735Sjeff int thresh; 907171482Sjeff int cpu; 908123433Sjeff 909172484Sjeff if (smp_started == 0 || steal_idle == 0) 910172484Sjeff return (1); 911176735Sjeff mask = -1; 912176735Sjeff mask &= ~PCPU_GET(cpumask); 913176735Sjeff /* We don't want to be preempted while we're iterating. */ 914171482Sjeff spinlock_enter(); 915176735Sjeff for (cg = tdq->tdq_cg; cg != NULL; ) { 916176735Sjeff if ((cg->cg_flags & (CG_FLAG_HTT | CG_FLAG_THREAD)) == 0) 917176735Sjeff thresh = steal_thresh; 918176735Sjeff else 919176735Sjeff thresh = 1; 920176735Sjeff cpu = sched_highest(cg, mask, thresh); 921176735Sjeff if (cpu == -1) { 922176735Sjeff cg = cg->cg_parent; 923176735Sjeff continue; 924166108Sjeff } 925176735Sjeff steal = TDQ_CPU(cpu); 926176735Sjeff mask &= ~(1 << cpu); 927176735Sjeff tdq_lock_pair(tdq, steal); 928176735Sjeff if (steal->tdq_load < thresh || steal->tdq_transferable == 0) { 929176735Sjeff tdq_unlock_pair(tdq, steal); 930176735Sjeff continue; 931171482Sjeff } 932176735Sjeff /* 933176735Sjeff * If a thread was added while interrupts were disabled don't 934176735Sjeff * steal one here. If we fail to acquire one due to affinity 935176735Sjeff * restrictions loop again with this cpu removed from the 936176735Sjeff * set. 937176735Sjeff */ 938176735Sjeff if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) { 939176735Sjeff tdq_unlock_pair(tdq, steal); 940176735Sjeff continue; 941176735Sjeff } 942176735Sjeff spinlock_exit(); 943176735Sjeff TDQ_UNLOCK(steal); 944178272Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 945176735Sjeff thread_unlock(curthread); 946176735Sjeff 947176735Sjeff return (0); 948123433Sjeff } 949171482Sjeff spinlock_exit(); 950123433Sjeff return (1); 951121790Sjeff} 952121790Sjeff 953171482Sjeff/* 954171482Sjeff * Notify a remote cpu of new work. Sends an IPI if criteria are met. 955171482Sjeff */ 956121790Sjeffstatic void 957177435Sjefftdq_notify(struct tdq *tdq, struct thread *td) 958121790Sjeff{ 959185047Sjhb struct thread *ctd; 960166247Sjeff int pri; 961166108Sjeff int cpu; 962121790Sjeff 963177005Sjeff if (tdq->tdq_ipipending) 964177005Sjeff return; 965177435Sjeff cpu = td->td_sched->ts_cpu; 966177435Sjeff pri = td->td_priority; 967185047Sjhb ctd = pcpu_find(cpu)->pc_curthread; 968185047Sjhb if (!sched_shouldpreempt(pri, ctd->td_priority, 1)) 969166137Sjeff return; 970185047Sjhb if (TD_IS_IDLETHREAD(ctd)) { 971178277Sjeff /* 972178277Sjeff * If the idle thread is still 'running' it's probably 973178277Sjeff * waiting on us to release the tdq spinlock already. No 974178277Sjeff * need to ipi. 975178277Sjeff */ 976178277Sjeff if (tdq->tdq_idlestate == TDQ_RUNNING) 977178277Sjeff return; 978178471Sjeff /* 979178471Sjeff * If the MD code has an idle wakeup routine try that before 980178471Sjeff * falling back to IPI. 981178471Sjeff */ 982178471Sjeff if (cpu_idle_wakeup(cpu)) 983178471Sjeff return; 984178277Sjeff } 985177005Sjeff tdq->tdq_ipipending = 1; 986171482Sjeff ipi_selected(1 << cpu, IPI_PREEMPT); 987121790Sjeff} 988121790Sjeff 989171482Sjeff/* 990171482Sjeff * Steals load from a timeshare queue. Honors the rotating queue head 991171482Sjeff * index. 992171482Sjeff */ 993177435Sjeffstatic struct thread * 994176735Sjeffrunq_steal_from(struct runq *rq, int cpu, u_char start) 995171482Sjeff{ 996171482Sjeff struct rqbits *rqb; 997171482Sjeff struct rqhead *rqh; 998177435Sjeff struct thread *td; 999171482Sjeff int first; 1000171482Sjeff int bit; 1001171482Sjeff int pri; 1002171482Sjeff int i; 1003171482Sjeff 1004171482Sjeff rqb = &rq->rq_status; 1005171482Sjeff bit = start & (RQB_BPW -1); 1006171482Sjeff pri = 0; 1007171482Sjeff first = 0; 1008171482Sjeffagain: 1009171482Sjeff for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 1010171482Sjeff if (rqb->rqb_bits[i] == 0) 1011171482Sjeff continue; 1012171482Sjeff if (bit != 0) { 1013171482Sjeff for (pri = bit; pri < RQB_BPW; pri++) 1014171482Sjeff if (rqb->rqb_bits[i] & (1ul << pri)) 1015171482Sjeff break; 1016171482Sjeff if (pri >= RQB_BPW) 1017171482Sjeff continue; 1018171482Sjeff } else 1019171482Sjeff pri = RQB_FFS(rqb->rqb_bits[i]); 1020171482Sjeff pri += (i << RQB_L2BPW); 1021171482Sjeff rqh = &rq->rq_queues[pri]; 1022177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 1023177435Sjeff if (first && THREAD_CAN_MIGRATE(td) && 1024177435Sjeff THREAD_CAN_SCHED(td, cpu)) 1025177435Sjeff return (td); 1026171482Sjeff first = 1; 1027171482Sjeff } 1028171482Sjeff } 1029171482Sjeff if (start != 0) { 1030171482Sjeff start = 0; 1031171482Sjeff goto again; 1032171482Sjeff } 1033171482Sjeff 1034171482Sjeff return (NULL); 1035171482Sjeff} 1036171482Sjeff 1037171482Sjeff/* 1038171482Sjeff * Steals load from a standard linear queue. 1039171482Sjeff */ 1040177435Sjeffstatic struct thread * 1041176735Sjeffrunq_steal(struct runq *rq, int cpu) 1042121790Sjeff{ 1043121790Sjeff struct rqhead *rqh; 1044121790Sjeff struct rqbits *rqb; 1045177435Sjeff struct thread *td; 1046121790Sjeff int word; 1047121790Sjeff int bit; 1048121790Sjeff 1049121790Sjeff rqb = &rq->rq_status; 1050121790Sjeff for (word = 0; word < RQB_LEN; word++) { 1051121790Sjeff if (rqb->rqb_bits[word] == 0) 1052121790Sjeff continue; 1053121790Sjeff for (bit = 0; bit < RQB_BPW; bit++) { 1054123231Speter if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 1055121790Sjeff continue; 1056121790Sjeff rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 1057177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) 1058177435Sjeff if (THREAD_CAN_MIGRATE(td) && 1059177435Sjeff THREAD_CAN_SCHED(td, cpu)) 1060177435Sjeff return (td); 1061121790Sjeff } 1062121790Sjeff } 1063121790Sjeff return (NULL); 1064121790Sjeff} 1065121790Sjeff 1066171482Sjeff/* 1067171482Sjeff * Attempt to steal a thread in priority order from a thread queue. 1068171482Sjeff */ 1069177435Sjeffstatic struct thread * 1070176735Sjefftdq_steal(struct tdq *tdq, int cpu) 1071121790Sjeff{ 1072177435Sjeff struct thread *td; 1073121790Sjeff 1074171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 1075177435Sjeff if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 1076177435Sjeff return (td); 1077177435Sjeff if ((td = runq_steal_from(&tdq->tdq_timeshare, 1078177435Sjeff cpu, tdq->tdq_ridx)) != NULL) 1079177435Sjeff return (td); 1080176735Sjeff return (runq_steal(&tdq->tdq_idle, cpu)); 1081121790Sjeff} 1082123433Sjeff 1083171482Sjeff/* 1084171482Sjeff * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 1085172409Sjeff * current lock and returns with the assigned queue locked. 1086171482Sjeff */ 1087171482Sjeffstatic inline struct tdq * 1088177435Sjeffsched_setcpu(struct thread *td, int cpu, int flags) 1089123433Sjeff{ 1090177435Sjeff 1091171482Sjeff struct tdq *tdq; 1092123433Sjeff 1093177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1094171482Sjeff tdq = TDQ_CPU(cpu); 1095177435Sjeff td->td_sched->ts_cpu = cpu; 1096177435Sjeff /* 1097177435Sjeff * If the lock matches just return the queue. 1098177435Sjeff */ 1099171482Sjeff if (td->td_lock == TDQ_LOCKPTR(tdq)) 1100171482Sjeff return (tdq); 1101171482Sjeff#ifdef notyet 1102123433Sjeff /* 1103172293Sjeff * If the thread isn't running its lockptr is a 1104171482Sjeff * turnstile or a sleepqueue. We can just lock_set without 1105171482Sjeff * blocking. 1106123685Sjeff */ 1107171482Sjeff if (TD_CAN_RUN(td)) { 1108171482Sjeff TDQ_LOCK(tdq); 1109171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 1110171482Sjeff return (tdq); 1111171482Sjeff } 1112171482Sjeff#endif 1113166108Sjeff /* 1114171482Sjeff * The hard case, migration, we need to block the thread first to 1115171482Sjeff * prevent order reversals with other cpus locks. 1116166108Sjeff */ 1117171482Sjeff thread_lock_block(td); 1118171482Sjeff TDQ_LOCK(tdq); 1119171713Sjeff thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1120171482Sjeff return (tdq); 1121166108Sjeff} 1122166108Sjeff 1123178272SjeffSCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 1124178272SjeffSCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 1125178272SjeffSCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 1126178272SjeffSCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 1127178272SjeffSCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 1128178272SjeffSCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 1129178272Sjeff 1130166108Sjeffstatic int 1131177435Sjeffsched_pickcpu(struct thread *td, int flags) 1132171482Sjeff{ 1133176735Sjeff struct cpu_group *cg; 1134177435Sjeff struct td_sched *ts; 1135171482Sjeff struct tdq *tdq; 1136176735Sjeff cpumask_t mask; 1137166108Sjeff int self; 1138166108Sjeff int pri; 1139166108Sjeff int cpu; 1140166108Sjeff 1141176735Sjeff self = PCPU_GET(cpuid); 1142177435Sjeff ts = td->td_sched; 1143166108Sjeff if (smp_started == 0) 1144166108Sjeff return (self); 1145171506Sjeff /* 1146171506Sjeff * Don't migrate a running thread from sched_switch(). 1147171506Sjeff */ 1148176735Sjeff if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 1149176735Sjeff return (ts->ts_cpu); 1150166108Sjeff /* 1151176735Sjeff * Prefer to run interrupt threads on the processors that generate 1152176735Sjeff * the interrupt. 1153166108Sjeff */ 1154176735Sjeff if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 1155178272Sjeff curthread->td_intr_nesting_level && ts->ts_cpu != self) { 1156178272Sjeff SCHED_STAT_INC(pickcpu_intrbind); 1157176735Sjeff ts->ts_cpu = self; 1158178272Sjeff } 1159166108Sjeff /* 1160176735Sjeff * If the thread can run on the last cpu and the affinity has not 1161176735Sjeff * expired or it is idle run it there. 1162166108Sjeff */ 1163176735Sjeff pri = td->td_priority; 1164176735Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1165176735Sjeff if (THREAD_CAN_SCHED(td, ts->ts_cpu)) { 1166178272Sjeff if (tdq->tdq_lowpri > PRI_MIN_IDLE) { 1167178272Sjeff SCHED_STAT_INC(pickcpu_idle_affinity); 1168176735Sjeff return (ts->ts_cpu); 1169178272Sjeff } 1170178272Sjeff if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri) { 1171178272Sjeff SCHED_STAT_INC(pickcpu_affinity); 1172176735Sjeff return (ts->ts_cpu); 1173178272Sjeff } 1174139334Sjeff } 1175123433Sjeff /* 1176176735Sjeff * Search for the highest level in the tree that still has affinity. 1177123433Sjeff */ 1178176735Sjeff cg = NULL; 1179176735Sjeff for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent) 1180176735Sjeff if (SCHED_AFFINITY(ts, cg->cg_level)) 1181176735Sjeff break; 1182176735Sjeff cpu = -1; 1183176735Sjeff mask = td->td_cpuset->cs_mask.__bits[0]; 1184176735Sjeff if (cg) 1185176735Sjeff cpu = sched_lowest(cg, mask, pri); 1186176735Sjeff if (cpu == -1) 1187176735Sjeff cpu = sched_lowest(cpu_top, mask, -1); 1188171506Sjeff /* 1189176735Sjeff * Compare the lowest loaded cpu to current cpu. 1190171506Sjeff */ 1191177005Sjeff if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri && 1192178272Sjeff TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) { 1193178272Sjeff SCHED_STAT_INC(pickcpu_local); 1194177005Sjeff cpu = self; 1195178272Sjeff } else 1196178272Sjeff SCHED_STAT_INC(pickcpu_lowest); 1197178272Sjeff if (cpu != ts->ts_cpu) 1198178272Sjeff SCHED_STAT_INC(pickcpu_migration); 1199177005Sjeff KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu.")); 1200171482Sjeff return (cpu); 1201123433Sjeff} 1202176735Sjeff#endif 1203123433Sjeff 1204117326Sjeff/* 1205121790Sjeff * Pick the highest priority task we have and return it. 1206117326Sjeff */ 1207177435Sjeffstatic struct thread * 1208164936Sjuliantdq_choose(struct tdq *tdq) 1209110267Sjeff{ 1210177435Sjeff struct thread *td; 1211110267Sjeff 1212171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 1213177435Sjeff td = runq_choose(&tdq->tdq_realtime); 1214177435Sjeff if (td != NULL) 1215177435Sjeff return (td); 1216177435Sjeff td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 1217177435Sjeff if (td != NULL) { 1218177435Sjeff KASSERT(td->td_priority >= PRI_MIN_TIMESHARE, 1219165762Sjeff ("tdq_choose: Invalid priority on timeshare queue %d", 1220177435Sjeff td->td_priority)); 1221177435Sjeff return (td); 1222165762Sjeff } 1223177435Sjeff td = runq_choose(&tdq->tdq_idle); 1224177435Sjeff if (td != NULL) { 1225177435Sjeff KASSERT(td->td_priority >= PRI_MIN_IDLE, 1226165762Sjeff ("tdq_choose: Invalid priority on idle queue %d", 1227177435Sjeff td->td_priority)); 1228177435Sjeff return (td); 1229165762Sjeff } 1230165762Sjeff 1231165762Sjeff return (NULL); 1232110267Sjeff} 1233110267Sjeff 1234171482Sjeff/* 1235171482Sjeff * Initialize a thread queue. 1236171482Sjeff */ 1237109864Sjeffstatic void 1238164936Sjuliantdq_setup(struct tdq *tdq) 1239110028Sjeff{ 1240171482Sjeff 1241171713Sjeff if (bootverbose) 1242171713Sjeff printf("ULE: setup cpu %d\n", TDQ_ID(tdq)); 1243165762Sjeff runq_init(&tdq->tdq_realtime); 1244165762Sjeff runq_init(&tdq->tdq_timeshare); 1245165620Sjeff runq_init(&tdq->tdq_idle); 1246176735Sjeff snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 1247176735Sjeff "sched lock %d", (int)TDQ_ID(tdq)); 1248176735Sjeff mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", 1249176735Sjeff MTX_SPIN | MTX_RECURSE); 1250187357Sjeff#ifdef KTR 1251187357Sjeff snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 1252187357Sjeff "CPU %d load", (int)TDQ_ID(tdq)); 1253187357Sjeff#endif 1254110028Sjeff} 1255110028Sjeff 1256171713Sjeff#ifdef SMP 1257110028Sjeffstatic void 1258171713Sjeffsched_setup_smp(void) 1259171713Sjeff{ 1260171713Sjeff struct tdq *tdq; 1261171713Sjeff int i; 1262171713Sjeff 1263176735Sjeff cpu_top = smp_topo(); 1264176735Sjeff for (i = 0; i < MAXCPU; i++) { 1265171713Sjeff if (CPU_ABSENT(i)) 1266171713Sjeff continue; 1267176735Sjeff tdq = TDQ_CPU(i); 1268171713Sjeff tdq_setup(tdq); 1269176735Sjeff tdq->tdq_cg = smp_topo_find(cpu_top, i); 1270176735Sjeff if (tdq->tdq_cg == NULL) 1271176735Sjeff panic("Can't find cpu group for %d\n", i); 1272123433Sjeff } 1273176735Sjeff balance_tdq = TDQ_SELF(); 1274176735Sjeff sched_balance(); 1275171713Sjeff} 1276171713Sjeff#endif 1277171713Sjeff 1278171713Sjeff/* 1279171713Sjeff * Setup the thread queues and initialize the topology based on MD 1280171713Sjeff * information. 1281171713Sjeff */ 1282171713Sjeffstatic void 1283171713Sjeffsched_setup(void *dummy) 1284171713Sjeff{ 1285171713Sjeff struct tdq *tdq; 1286171713Sjeff 1287171713Sjeff tdq = TDQ_SELF(); 1288171713Sjeff#ifdef SMP 1289176734Sjeff sched_setup_smp(); 1290117237Sjeff#else 1291171713Sjeff tdq_setup(tdq); 1292116069Sjeff#endif 1293171482Sjeff /* 1294171482Sjeff * To avoid divide-by-zero, we set realstathz a dummy value 1295171482Sjeff * in case which sched_clock() called before sched_initticks(). 1296171482Sjeff */ 1297171482Sjeff realstathz = hz; 1298171482Sjeff sched_slice = (realstathz/10); /* ~100ms */ 1299171482Sjeff tickincr = 1 << SCHED_TICK_SHIFT; 1300171482Sjeff 1301171482Sjeff /* Add thread0's load since it's running. */ 1302171482Sjeff TDQ_LOCK(tdq); 1303171713Sjeff thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1304177435Sjeff tdq_load_add(tdq, &thread0); 1305176735Sjeff tdq->tdq_lowpri = thread0.td_priority; 1306171482Sjeff TDQ_UNLOCK(tdq); 1307109864Sjeff} 1308109864Sjeff 1309171482Sjeff/* 1310171482Sjeff * This routine determines the tickincr after stathz and hz are setup. 1311171482Sjeff */ 1312153533Sdavidxu/* ARGSUSED */ 1313153533Sdavidxustatic void 1314153533Sdavidxusched_initticks(void *dummy) 1315153533Sdavidxu{ 1316171482Sjeff int incr; 1317171482Sjeff 1318153533Sdavidxu realstathz = stathz ? stathz : hz; 1319166229Sjeff sched_slice = (realstathz/10); /* ~100ms */ 1320153533Sdavidxu 1321153533Sdavidxu /* 1322165762Sjeff * tickincr is shifted out by 10 to avoid rounding errors due to 1323165766Sjeff * hz not being evenly divisible by stathz on all platforms. 1324153533Sdavidxu */ 1325171482Sjeff incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1326165762Sjeff /* 1327165762Sjeff * This does not work for values of stathz that are more than 1328165762Sjeff * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1329165762Sjeff */ 1330171482Sjeff if (incr == 0) 1331171482Sjeff incr = 1; 1332171482Sjeff tickincr = incr; 1333166108Sjeff#ifdef SMP 1334171899Sjeff /* 1335172409Sjeff * Set the default balance interval now that we know 1336172409Sjeff * what realstathz is. 1337172409Sjeff */ 1338172409Sjeff balance_interval = realstathz; 1339172409Sjeff /* 1340189787Sjeff * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4. 1341189787Sjeff * This prevents excess thrashing on large machines and excess idle 1342189787Sjeff * on smaller machines. 1343171899Sjeff */ 1344189787Sjeff steal_thresh = min(fls(mp_ncpus) - 1, 3); 1345166108Sjeff affinity = SCHED_AFFINITY_DEFAULT; 1346166108Sjeff#endif 1347153533Sdavidxu} 1348153533Sdavidxu 1349153533Sdavidxu 1350109864Sjeff/* 1351171482Sjeff * This is the core of the interactivity algorithm. Determines a score based 1352171482Sjeff * on past behavior. It is the ratio of sleep time to run time scaled to 1353171482Sjeff * a [0, 100] integer. This is the voluntary sleep time of a process, which 1354171482Sjeff * differs from the cpu usage because it does not account for time spent 1355171482Sjeff * waiting on a run-queue. Would be prettier if we had floating point. 1356171482Sjeff */ 1357171482Sjeffstatic int 1358171482Sjeffsched_interact_score(struct thread *td) 1359171482Sjeff{ 1360171482Sjeff struct td_sched *ts; 1361171482Sjeff int div; 1362171482Sjeff 1363171482Sjeff ts = td->td_sched; 1364171482Sjeff /* 1365171482Sjeff * The score is only needed if this is likely to be an interactive 1366171482Sjeff * task. Don't go through the expense of computing it if there's 1367171482Sjeff * no chance. 1368171482Sjeff */ 1369171482Sjeff if (sched_interact <= SCHED_INTERACT_HALF && 1370171482Sjeff ts->ts_runtime >= ts->ts_slptime) 1371171482Sjeff return (SCHED_INTERACT_HALF); 1372171482Sjeff 1373171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1374171482Sjeff div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1375171482Sjeff return (SCHED_INTERACT_HALF + 1376171482Sjeff (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1377171482Sjeff } 1378171482Sjeff if (ts->ts_slptime > ts->ts_runtime) { 1379171482Sjeff div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1380171482Sjeff return (ts->ts_runtime / div); 1381171482Sjeff } 1382171482Sjeff /* runtime == slptime */ 1383171482Sjeff if (ts->ts_runtime) 1384171482Sjeff return (SCHED_INTERACT_HALF); 1385171482Sjeff 1386171482Sjeff /* 1387171482Sjeff * This can happen if slptime and runtime are 0. 1388171482Sjeff */ 1389171482Sjeff return (0); 1390171482Sjeff 1391171482Sjeff} 1392171482Sjeff 1393171482Sjeff/* 1394109864Sjeff * Scale the scheduling priority according to the "interactivity" of this 1395109864Sjeff * process. 1396109864Sjeff */ 1397113357Sjeffstatic void 1398163709Sjbsched_priority(struct thread *td) 1399109864Sjeff{ 1400165762Sjeff int score; 1401109864Sjeff int pri; 1402109864Sjeff 1403163709Sjb if (td->td_pri_class != PRI_TIMESHARE) 1404113357Sjeff return; 1405112966Sjeff /* 1406165762Sjeff * If the score is interactive we place the thread in the realtime 1407165762Sjeff * queue with a priority that is less than kernel and interrupt 1408165762Sjeff * priorities. These threads are not subject to nice restrictions. 1409112966Sjeff * 1410171482Sjeff * Scores greater than this are placed on the normal timeshare queue 1411165762Sjeff * where the priority is partially decided by the most recent cpu 1412165762Sjeff * utilization and the rest is decided by nice value. 1413172293Sjeff * 1414172293Sjeff * The nice value of the process has a linear effect on the calculated 1415172293Sjeff * score. Negative nice values make it easier for a thread to be 1416172293Sjeff * considered interactive. 1417112966Sjeff */ 1418172308Sjeff score = imax(0, sched_interact_score(td) - td->td_proc->p_nice); 1419165762Sjeff if (score < sched_interact) { 1420165762Sjeff pri = PRI_MIN_REALTIME; 1421165762Sjeff pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact) 1422165762Sjeff * score; 1423165762Sjeff KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME, 1424166208Sjeff ("sched_priority: invalid interactive priority %d score %d", 1425166208Sjeff pri, score)); 1426165762Sjeff } else { 1427165762Sjeff pri = SCHED_PRI_MIN; 1428165762Sjeff if (td->td_sched->ts_ticks) 1429165762Sjeff pri += SCHED_PRI_TICKS(td->td_sched); 1430165762Sjeff pri += SCHED_PRI_NICE(td->td_proc->p_nice); 1431171482Sjeff KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE, 1432171482Sjeff ("sched_priority: invalid priority %d: nice %d, " 1433171482Sjeff "ticks %d ftick %d ltick %d tick pri %d", 1434171482Sjeff pri, td->td_proc->p_nice, td->td_sched->ts_ticks, 1435171482Sjeff td->td_sched->ts_ftick, td->td_sched->ts_ltick, 1436171482Sjeff SCHED_PRI_TICKS(td->td_sched))); 1437165762Sjeff } 1438165762Sjeff sched_user_prio(td, pri); 1439112966Sjeff 1440112966Sjeff return; 1441109864Sjeff} 1442109864Sjeff 1443121868Sjeff/* 1444121868Sjeff * This routine enforces a maximum limit on the amount of scheduling history 1445171482Sjeff * kept. It is called after either the slptime or runtime is adjusted. This 1446171482Sjeff * function is ugly due to integer math. 1447121868Sjeff */ 1448116463Sjeffstatic void 1449163709Sjbsched_interact_update(struct thread *td) 1450116463Sjeff{ 1451165819Sjeff struct td_sched *ts; 1452166208Sjeff u_int sum; 1453121605Sjeff 1454165819Sjeff ts = td->td_sched; 1455171482Sjeff sum = ts->ts_runtime + ts->ts_slptime; 1456121868Sjeff if (sum < SCHED_SLP_RUN_MAX) 1457121868Sjeff return; 1458121868Sjeff /* 1459165819Sjeff * This only happens from two places: 1460165819Sjeff * 1) We have added an unusual amount of run time from fork_exit. 1461165819Sjeff * 2) We have added an unusual amount of sleep time from sched_sleep(). 1462165819Sjeff */ 1463165819Sjeff if (sum > SCHED_SLP_RUN_MAX * 2) { 1464171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1465171482Sjeff ts->ts_runtime = SCHED_SLP_RUN_MAX; 1466171482Sjeff ts->ts_slptime = 1; 1467165819Sjeff } else { 1468171482Sjeff ts->ts_slptime = SCHED_SLP_RUN_MAX; 1469171482Sjeff ts->ts_runtime = 1; 1470165819Sjeff } 1471165819Sjeff return; 1472165819Sjeff } 1473165819Sjeff /* 1474121868Sjeff * If we have exceeded by more than 1/5th then the algorithm below 1475121868Sjeff * will not bring us back into range. Dividing by two here forces 1476133427Sjeff * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1477121868Sjeff */ 1478127850Sjeff if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1479171482Sjeff ts->ts_runtime /= 2; 1480171482Sjeff ts->ts_slptime /= 2; 1481121868Sjeff return; 1482116463Sjeff } 1483171482Sjeff ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1484171482Sjeff ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1485116463Sjeff} 1486116463Sjeff 1487171482Sjeff/* 1488171482Sjeff * Scale back the interactivity history when a child thread is created. The 1489171482Sjeff * history is inherited from the parent but the thread may behave totally 1490171482Sjeff * differently. For example, a shell spawning a compiler process. We want 1491171482Sjeff * to learn that the compiler is behaving badly very quickly. 1492171482Sjeff */ 1493121868Sjeffstatic void 1494163709Sjbsched_interact_fork(struct thread *td) 1495121868Sjeff{ 1496121868Sjeff int ratio; 1497121868Sjeff int sum; 1498121868Sjeff 1499171482Sjeff sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime; 1500121868Sjeff if (sum > SCHED_SLP_RUN_FORK) { 1501121868Sjeff ratio = sum / SCHED_SLP_RUN_FORK; 1502171482Sjeff td->td_sched->ts_runtime /= ratio; 1503171482Sjeff td->td_sched->ts_slptime /= ratio; 1504121868Sjeff } 1505121868Sjeff} 1506121868Sjeff 1507113357Sjeff/* 1508171482Sjeff * Called from proc0_init() to setup the scheduler fields. 1509134791Sjulian */ 1510134791Sjulianvoid 1511134791Sjulianschedinit(void) 1512134791Sjulian{ 1513165762Sjeff 1514134791Sjulian /* 1515134791Sjulian * Set up the scheduler specific parts of proc0. 1516134791Sjulian */ 1517136167Sjulian proc0.p_sched = NULL; /* XXX */ 1518164936Sjulian thread0.td_sched = &td_sched0; 1519165762Sjeff td_sched0.ts_ltick = ticks; 1520165796Sjeff td_sched0.ts_ftick = ticks; 1521177009Sjeff td_sched0.ts_slice = sched_slice; 1522134791Sjulian} 1523134791Sjulian 1524134791Sjulian/* 1525113357Sjeff * This is only somewhat accurate since given many processes of the same 1526113357Sjeff * priority they will switch when their slices run out, which will be 1527165762Sjeff * at most sched_slice stathz ticks. 1528113357Sjeff */ 1529109864Sjeffint 1530109864Sjeffsched_rr_interval(void) 1531109864Sjeff{ 1532165762Sjeff 1533165762Sjeff /* Convert sched_slice to hz */ 1534165762Sjeff return (hz/(realstathz/sched_slice)); 1535109864Sjeff} 1536109864Sjeff 1537171482Sjeff/* 1538171482Sjeff * Update the percent cpu tracking information when it is requested or 1539171482Sjeff * the total history exceeds the maximum. We keep a sliding history of 1540171482Sjeff * tick counts that slowly decays. This is less precise than the 4BSD 1541171482Sjeff * mechanism since it happens with less regular and frequent events. 1542171482Sjeff */ 1543121790Sjeffstatic void 1544164936Sjuliansched_pctcpu_update(struct td_sched *ts) 1545109864Sjeff{ 1546165762Sjeff 1547165762Sjeff if (ts->ts_ticks == 0) 1548165762Sjeff return; 1549165796Sjeff if (ticks - (hz / 10) < ts->ts_ltick && 1550165796Sjeff SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX) 1551165796Sjeff return; 1552109864Sjeff /* 1553109864Sjeff * Adjust counters and watermark for pctcpu calc. 1554116365Sjeff */ 1555165762Sjeff if (ts->ts_ltick > ticks - SCHED_TICK_TARG) 1556164936Sjulian ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) * 1557165762Sjeff SCHED_TICK_TARG; 1558165762Sjeff else 1559164936Sjulian ts->ts_ticks = 0; 1560164936Sjulian ts->ts_ltick = ticks; 1561165762Sjeff ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG; 1562109864Sjeff} 1563109864Sjeff 1564171482Sjeff/* 1565171482Sjeff * Adjust the priority of a thread. Move it to the appropriate run-queue 1566171482Sjeff * if necessary. This is the back-end for several priority related 1567171482Sjeff * functions. 1568171482Sjeff */ 1569165762Sjeffstatic void 1570139453Sjhbsched_thread_priority(struct thread *td, u_char prio) 1571109864Sjeff{ 1572164936Sjulian struct td_sched *ts; 1573177009Sjeff struct tdq *tdq; 1574177009Sjeff int oldpri; 1575109864Sjeff 1576187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 1577187357Sjeff "prio:%d", td->td_priority, "new prio:%d", prio, 1578187357Sjeff KTR_ATTR_LINKED, sched_tdname(curthread)); 1579187357Sjeff if (td != curthread && prio > td->td_priority) { 1580187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 1581187357Sjeff "lend prio", "prio:%d", td->td_priority, "new prio:%d", 1582187357Sjeff prio, KTR_ATTR_LINKED, sched_tdname(td)); 1583187357Sjeff } 1584164936Sjulian ts = td->td_sched; 1585170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1586139453Sjhb if (td->td_priority == prio) 1587139453Sjhb return; 1588177376Sjeff /* 1589177376Sjeff * If the priority has been elevated due to priority 1590177376Sjeff * propagation, we may have to move ourselves to a new 1591177376Sjeff * queue. This could be optimized to not re-add in some 1592177376Sjeff * cases. 1593177376Sjeff */ 1594165766Sjeff if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1595165762Sjeff sched_rem(td); 1596165762Sjeff td->td_priority = prio; 1597171482Sjeff sched_add(td, SRQ_BORROWING); 1598177009Sjeff return; 1599177009Sjeff } 1600177376Sjeff /* 1601177376Sjeff * If the thread is currently running we may have to adjust the lowpri 1602177376Sjeff * information so other cpus are aware of our current priority. 1603177376Sjeff */ 1604177009Sjeff if (TD_IS_RUNNING(td)) { 1605177376Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1606177376Sjeff oldpri = td->td_priority; 1607177376Sjeff td->td_priority = prio; 1608176735Sjeff if (prio < tdq->tdq_lowpri) 1609171482Sjeff tdq->tdq_lowpri = prio; 1610176735Sjeff else if (tdq->tdq_lowpri == oldpri) 1611176735Sjeff tdq_setlowpri(tdq, td); 1612177376Sjeff return; 1613177009Sjeff } 1614177376Sjeff td->td_priority = prio; 1615109864Sjeff} 1616109864Sjeff 1617139453Sjhb/* 1618139453Sjhb * Update a thread's priority when it is lent another thread's 1619139453Sjhb * priority. 1620139453Sjhb */ 1621109864Sjeffvoid 1622139453Sjhbsched_lend_prio(struct thread *td, u_char prio) 1623139453Sjhb{ 1624139453Sjhb 1625139453Sjhb td->td_flags |= TDF_BORROWING; 1626139453Sjhb sched_thread_priority(td, prio); 1627139453Sjhb} 1628139453Sjhb 1629139453Sjhb/* 1630139453Sjhb * Restore a thread's priority when priority propagation is 1631139453Sjhb * over. The prio argument is the minimum priority the thread 1632139453Sjhb * needs to have to satisfy other possible priority lending 1633139453Sjhb * requests. If the thread's regular priority is less 1634139453Sjhb * important than prio, the thread will keep a priority boost 1635139453Sjhb * of prio. 1636139453Sjhb */ 1637139453Sjhbvoid 1638139453Sjhbsched_unlend_prio(struct thread *td, u_char prio) 1639139453Sjhb{ 1640139453Sjhb u_char base_pri; 1641139453Sjhb 1642139453Sjhb if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1643139453Sjhb td->td_base_pri <= PRI_MAX_TIMESHARE) 1644163709Sjb base_pri = td->td_user_pri; 1645139453Sjhb else 1646139453Sjhb base_pri = td->td_base_pri; 1647139453Sjhb if (prio >= base_pri) { 1648139455Sjhb td->td_flags &= ~TDF_BORROWING; 1649139453Sjhb sched_thread_priority(td, base_pri); 1650139453Sjhb } else 1651139453Sjhb sched_lend_prio(td, prio); 1652139453Sjhb} 1653139453Sjhb 1654171482Sjeff/* 1655171482Sjeff * Standard entry for setting the priority to an absolute value. 1656171482Sjeff */ 1657139453Sjhbvoid 1658139453Sjhbsched_prio(struct thread *td, u_char prio) 1659139453Sjhb{ 1660139453Sjhb u_char oldprio; 1661139453Sjhb 1662139453Sjhb /* First, update the base priority. */ 1663139453Sjhb td->td_base_pri = prio; 1664139453Sjhb 1665139453Sjhb /* 1666139455Sjhb * If the thread is borrowing another thread's priority, don't 1667139453Sjhb * ever lower the priority. 1668139453Sjhb */ 1669139453Sjhb if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1670139453Sjhb return; 1671139453Sjhb 1672139453Sjhb /* Change the real priority. */ 1673139453Sjhb oldprio = td->td_priority; 1674139453Sjhb sched_thread_priority(td, prio); 1675139453Sjhb 1676139453Sjhb /* 1677139453Sjhb * If the thread is on a turnstile, then let the turnstile update 1678139453Sjhb * its state. 1679139453Sjhb */ 1680139453Sjhb if (TD_ON_LOCK(td) && oldprio != prio) 1681139453Sjhb turnstile_adjust(td, oldprio); 1682139453Sjhb} 1683139455Sjhb 1684171482Sjeff/* 1685171482Sjeff * Set the base user priority, does not effect current running priority. 1686171482Sjeff */ 1687139453Sjhbvoid 1688163709Sjbsched_user_prio(struct thread *td, u_char prio) 1689161599Sdavidxu{ 1690161599Sdavidxu u_char oldprio; 1691161599Sdavidxu 1692163709Sjb td->td_base_user_pri = prio; 1693164939Sjulian if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio) 1694164939Sjulian return; 1695163709Sjb oldprio = td->td_user_pri; 1696163709Sjb td->td_user_pri = prio; 1697161599Sdavidxu} 1698161599Sdavidxu 1699161599Sdavidxuvoid 1700161599Sdavidxusched_lend_user_prio(struct thread *td, u_char prio) 1701161599Sdavidxu{ 1702161599Sdavidxu u_char oldprio; 1703161599Sdavidxu 1704174536Sdavidxu THREAD_LOCK_ASSERT(td, MA_OWNED); 1705161599Sdavidxu td->td_flags |= TDF_UBORROWING; 1706164091Smaxim oldprio = td->td_user_pri; 1707163709Sjb td->td_user_pri = prio; 1708161599Sdavidxu} 1709161599Sdavidxu 1710161599Sdavidxuvoid 1711161599Sdavidxusched_unlend_user_prio(struct thread *td, u_char prio) 1712161599Sdavidxu{ 1713161599Sdavidxu u_char base_pri; 1714161599Sdavidxu 1715174536Sdavidxu THREAD_LOCK_ASSERT(td, MA_OWNED); 1716163709Sjb base_pri = td->td_base_user_pri; 1717161599Sdavidxu if (prio >= base_pri) { 1718161599Sdavidxu td->td_flags &= ~TDF_UBORROWING; 1719163709Sjb sched_user_prio(td, base_pri); 1720174536Sdavidxu } else { 1721161599Sdavidxu sched_lend_user_prio(td, prio); 1722174536Sdavidxu } 1723161599Sdavidxu} 1724161599Sdavidxu 1725171482Sjeff/* 1726174847Swkoszek * Block a thread for switching. Similar to thread_block() but does not 1727174847Swkoszek * bump the spin count. 1728174847Swkoszek */ 1729174847Swkoszekstatic inline struct mtx * 1730174847Swkoszekthread_block_switch(struct thread *td) 1731174847Swkoszek{ 1732174847Swkoszek struct mtx *lock; 1733174847Swkoszek 1734174847Swkoszek THREAD_LOCK_ASSERT(td, MA_OWNED); 1735174847Swkoszek lock = td->td_lock; 1736174847Swkoszek td->td_lock = &blocked_lock; 1737174847Swkoszek mtx_unlock_spin(lock); 1738174847Swkoszek 1739174847Swkoszek return (lock); 1740174847Swkoszek} 1741174847Swkoszek 1742174847Swkoszek/* 1743171713Sjeff * Handle migration from sched_switch(). This happens only for 1744171713Sjeff * cpu binding. 1745171713Sjeff */ 1746171713Sjeffstatic struct mtx * 1747171713Sjeffsched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1748171713Sjeff{ 1749171713Sjeff struct tdq *tdn; 1750171713Sjeff 1751171713Sjeff tdn = TDQ_CPU(td->td_sched->ts_cpu); 1752171713Sjeff#ifdef SMP 1753177435Sjeff tdq_load_rem(tdq, td); 1754171713Sjeff /* 1755171713Sjeff * Do the lock dance required to avoid LOR. We grab an extra 1756171713Sjeff * spinlock nesting to prevent preemption while we're 1757171713Sjeff * not holding either run-queue lock. 1758171713Sjeff */ 1759171713Sjeff spinlock_enter(); 1760171713Sjeff thread_block_switch(td); /* This releases the lock on tdq. */ 1761171713Sjeff TDQ_LOCK(tdn); 1762171713Sjeff tdq_add(tdn, td, flags); 1763177435Sjeff tdq_notify(tdn, td); 1764171713Sjeff /* 1765171713Sjeff * After we unlock tdn the new cpu still can't switch into this 1766171713Sjeff * thread until we've unblocked it in cpu_switch(). The lock 1767171713Sjeff * pointers may match in the case of HTT cores. Don't unlock here 1768171713Sjeff * or we can deadlock when the other CPU runs the IPI handler. 1769171713Sjeff */ 1770171713Sjeff if (TDQ_LOCKPTR(tdn) != TDQ_LOCKPTR(tdq)) { 1771171713Sjeff TDQ_UNLOCK(tdn); 1772171713Sjeff TDQ_LOCK(tdq); 1773171713Sjeff } 1774171713Sjeff spinlock_exit(); 1775171713Sjeff#endif 1776171713Sjeff return (TDQ_LOCKPTR(tdn)); 1777171713Sjeff} 1778171713Sjeff 1779171713Sjeff/* 1780171482Sjeff * Release a thread that was blocked with thread_block_switch(). 1781171482Sjeff */ 1782171482Sjeffstatic inline void 1783171482Sjeffthread_unblock_switch(struct thread *td, struct mtx *mtx) 1784171482Sjeff{ 1785171482Sjeff atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1786171482Sjeff (uintptr_t)mtx); 1787171482Sjeff} 1788171482Sjeff 1789171482Sjeff/* 1790171482Sjeff * Switch threads. This function has to handle threads coming in while 1791171482Sjeff * blocked for some reason, running, or idle. It also must deal with 1792171482Sjeff * migrating a thread from one queue to another as running threads may 1793171482Sjeff * be assigned elsewhere via binding. 1794171482Sjeff */ 1795161599Sdavidxuvoid 1796135051Sjuliansched_switch(struct thread *td, struct thread *newtd, int flags) 1797109864Sjeff{ 1798165627Sjeff struct tdq *tdq; 1799164936Sjulian struct td_sched *ts; 1800171482Sjeff struct mtx *mtx; 1801171713Sjeff int srqflag; 1802171482Sjeff int cpuid; 1803109864Sjeff 1804170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1805177376Sjeff KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument")); 1806109864Sjeff 1807171482Sjeff cpuid = PCPU_GET(cpuid); 1808171482Sjeff tdq = TDQ_CPU(cpuid); 1809164936Sjulian ts = td->td_sched; 1810171713Sjeff mtx = td->td_lock; 1811171482Sjeff ts->ts_rltick = ticks; 1812133555Sjeff td->td_lastcpu = td->td_oncpu; 1813113339Sjulian td->td_oncpu = NOCPU; 1814132266Sjhb td->td_flags &= ~TDF_NEEDRESCHED; 1815144777Sups td->td_owepreempt = 0; 1816178277Sjeff tdq->tdq_switchcnt++; 1817123434Sjeff /* 1818171482Sjeff * The lock pointer in an idle thread should never change. Reset it 1819171482Sjeff * to CAN_RUN as well. 1820123434Sjeff */ 1821167327Sjulian if (TD_IS_IDLETHREAD(td)) { 1822171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1823139334Sjeff TD_SET_CAN_RUN(td); 1824170293Sjeff } else if (TD_IS_RUNNING(td)) { 1825171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1826171713Sjeff srqflag = (flags & SW_PREEMPT) ? 1827170293Sjeff SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1828171713Sjeff SRQ_OURSELF|SRQ_YIELDING; 1829171713Sjeff if (ts->ts_cpu == cpuid) 1830177435Sjeff tdq_runq_add(tdq, td, srqflag); 1831171713Sjeff else 1832171713Sjeff mtx = sched_switch_migrate(tdq, td, srqflag); 1833171482Sjeff } else { 1834171482Sjeff /* This thread must be going to sleep. */ 1835171482Sjeff TDQ_LOCK(tdq); 1836171482Sjeff mtx = thread_block_switch(td); 1837177435Sjeff tdq_load_rem(tdq, td); 1838171482Sjeff } 1839171482Sjeff /* 1840171482Sjeff * We enter here with the thread blocked and assigned to the 1841171482Sjeff * appropriate cpu run-queue or sleep-queue and with the current 1842171482Sjeff * thread-queue locked. 1843171482Sjeff */ 1844171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 1845171482Sjeff newtd = choosethread(); 1846171482Sjeff /* 1847171482Sjeff * Call the MD code to switch contexts if necessary. 1848171482Sjeff */ 1849145256Sjkoshy if (td != newtd) { 1850145256Sjkoshy#ifdef HWPMC_HOOKS 1851145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1852145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1853145256Sjkoshy#endif 1854174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 1855172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 1856179297Sjb 1857179297Sjb#ifdef KDTRACE_HOOKS 1858179297Sjb /* 1859179297Sjb * If DTrace has set the active vtime enum to anything 1860179297Sjb * other than INACTIVE (0), then it should have set the 1861179297Sjb * function to call. 1862179297Sjb */ 1863179297Sjb if (dtrace_vtime_active) 1864179297Sjb (*dtrace_vtime_switch_func)(newtd); 1865179297Sjb#endif 1866179297Sjb 1867171482Sjeff cpu_switch(td, newtd, mtx); 1868171482Sjeff /* 1869171482Sjeff * We may return from cpu_switch on a different cpu. However, 1870171482Sjeff * we always return with td_lock pointing to the current cpu's 1871171482Sjeff * run queue lock. 1872171482Sjeff */ 1873171482Sjeff cpuid = PCPU_GET(cpuid); 1874171482Sjeff tdq = TDQ_CPU(cpuid); 1875174629Sjeff lock_profile_obtain_lock_success( 1876174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 1877145256Sjkoshy#ifdef HWPMC_HOOKS 1878145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1879145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1880145256Sjkoshy#endif 1881171482Sjeff } else 1882171482Sjeff thread_unblock_switch(td, mtx); 1883171482Sjeff /* 1884171482Sjeff * Assert that all went well and return. 1885171482Sjeff */ 1886171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED); 1887171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1888171482Sjeff td->td_oncpu = cpuid; 1889109864Sjeff} 1890109864Sjeff 1891171482Sjeff/* 1892171482Sjeff * Adjust thread priorities as a result of a nice request. 1893171482Sjeff */ 1894109864Sjeffvoid 1895130551Sjuliansched_nice(struct proc *p, int nice) 1896109864Sjeff{ 1897109864Sjeff struct thread *td; 1898109864Sjeff 1899130551Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 1900165762Sjeff 1901130551Sjulian p->p_nice = nice; 1902163709Sjb FOREACH_THREAD_IN_PROC(p, td) { 1903170293Sjeff thread_lock(td); 1904163709Sjb sched_priority(td); 1905165762Sjeff sched_prio(td, td->td_base_user_pri); 1906170293Sjeff thread_unlock(td); 1907130551Sjulian } 1908109864Sjeff} 1909109864Sjeff 1910171482Sjeff/* 1911171482Sjeff * Record the sleep time for the interactivity scorer. 1912171482Sjeff */ 1913109864Sjeffvoid 1914177085Sjeffsched_sleep(struct thread *td, int prio) 1915109864Sjeff{ 1916165762Sjeff 1917170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1918109864Sjeff 1919172264Sjeff td->td_slptick = ticks; 1920177085Sjeff if (TD_IS_SUSPENDED(td) || prio <= PSOCK) 1921177085Sjeff td->td_flags |= TDF_CANSWAP; 1922177903Sjeff if (static_boost == 1 && prio) 1923177085Sjeff sched_prio(td, prio); 1924177903Sjeff else if (static_boost && td->td_priority > static_boost) 1925177903Sjeff sched_prio(td, static_boost); 1926109864Sjeff} 1927109864Sjeff 1928171482Sjeff/* 1929171482Sjeff * Schedule a thread to resume execution and record how long it voluntarily 1930171482Sjeff * slept. We also update the pctcpu, interactivity, and priority. 1931171482Sjeff */ 1932109864Sjeffvoid 1933109864Sjeffsched_wakeup(struct thread *td) 1934109864Sjeff{ 1935166229Sjeff struct td_sched *ts; 1936171482Sjeff int slptick; 1937165762Sjeff 1938170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1939166229Sjeff ts = td->td_sched; 1940177085Sjeff td->td_flags &= ~TDF_CANSWAP; 1941109864Sjeff /* 1942165762Sjeff * If we slept for more than a tick update our interactivity and 1943165762Sjeff * priority. 1944109864Sjeff */ 1945172264Sjeff slptick = td->td_slptick; 1946172264Sjeff td->td_slptick = 0; 1947171482Sjeff if (slptick && slptick != ticks) { 1948166208Sjeff u_int hzticks; 1949109864Sjeff 1950171482Sjeff hzticks = (ticks - slptick) << SCHED_TICK_SHIFT; 1951171482Sjeff ts->ts_slptime += hzticks; 1952165819Sjeff sched_interact_update(td); 1953166229Sjeff sched_pctcpu_update(ts); 1954109864Sjeff } 1955166229Sjeff /* Reset the slice value after we sleep. */ 1956166229Sjeff ts->ts_slice = sched_slice; 1957166190Sjeff sched_add(td, SRQ_BORING); 1958109864Sjeff} 1959109864Sjeff 1960109864Sjeff/* 1961109864Sjeff * Penalize the parent for creating a new child and initialize the child's 1962109864Sjeff * priority. 1963109864Sjeff */ 1964109864Sjeffvoid 1965163709Sjbsched_fork(struct thread *td, struct thread *child) 1966109864Sjeff{ 1967170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1968164936Sjulian sched_fork_thread(td, child); 1969165762Sjeff /* 1970165762Sjeff * Penalize the parent and child for forking. 1971165762Sjeff */ 1972165762Sjeff sched_interact_fork(child); 1973165762Sjeff sched_priority(child); 1974171482Sjeff td->td_sched->ts_runtime += tickincr; 1975165762Sjeff sched_interact_update(td); 1976165762Sjeff sched_priority(td); 1977164936Sjulian} 1978109864Sjeff 1979171482Sjeff/* 1980171482Sjeff * Fork a new thread, may be within the same process. 1981171482Sjeff */ 1982164936Sjulianvoid 1983164936Sjuliansched_fork_thread(struct thread *td, struct thread *child) 1984164936Sjulian{ 1985164936Sjulian struct td_sched *ts; 1986164936Sjulian struct td_sched *ts2; 1987164936Sjulian 1988177426Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1989165762Sjeff /* 1990165762Sjeff * Initialize child. 1991165762Sjeff */ 1992177426Sjeff ts = td->td_sched; 1993177426Sjeff ts2 = child->td_sched; 1994171482Sjeff child->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1995176735Sjeff child->td_cpuset = cpuset_ref(td->td_cpuset); 1996164936Sjulian ts2->ts_cpu = ts->ts_cpu; 1997177426Sjeff ts2->ts_flags = 0; 1998165762Sjeff /* 1999165762Sjeff * Grab our parents cpu estimation information and priority. 2000165762Sjeff */ 2001164936Sjulian ts2->ts_ticks = ts->ts_ticks; 2002164936Sjulian ts2->ts_ltick = ts->ts_ltick; 2003164936Sjulian ts2->ts_ftick = ts->ts_ftick; 2004165762Sjeff child->td_user_pri = td->td_user_pri; 2005165762Sjeff child->td_base_user_pri = td->td_base_user_pri; 2006165762Sjeff /* 2007165762Sjeff * And update interactivity score. 2008165762Sjeff */ 2009171482Sjeff ts2->ts_slptime = ts->ts_slptime; 2010171482Sjeff ts2->ts_runtime = ts->ts_runtime; 2011165762Sjeff ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 2012187357Sjeff#ifdef KTR 2013187357Sjeff bzero(ts2->ts_name, sizeof(ts2->ts_name)); 2014187357Sjeff#endif 2015113357Sjeff} 2016113357Sjeff 2017171482Sjeff/* 2018171482Sjeff * Adjust the priority class of a thread. 2019171482Sjeff */ 2020113357Sjeffvoid 2021163709Sjbsched_class(struct thread *td, int class) 2022113357Sjeff{ 2023113357Sjeff 2024170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2025163709Sjb if (td->td_pri_class == class) 2026113357Sjeff return; 2027163709Sjb td->td_pri_class = class; 2028109864Sjeff} 2029109864Sjeff 2030109864Sjeff/* 2031109864Sjeff * Return some of the child's priority and interactivity to the parent. 2032109864Sjeff */ 2033109864Sjeffvoid 2034164939Sjuliansched_exit(struct proc *p, struct thread *child) 2035109864Sjeff{ 2036165762Sjeff struct thread *td; 2037113372Sjeff 2038187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2039187357Sjeff "prio:td", child->td_priority); 2040177368Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 2041165762Sjeff td = FIRST_THREAD_IN_PROC(p); 2042165762Sjeff sched_exit_thread(td, child); 2043113372Sjeff} 2044113372Sjeff 2045171482Sjeff/* 2046171482Sjeff * Penalize another thread for the time spent on this one. This helps to 2047171482Sjeff * worsen the priority and interactivity of processes which schedule batch 2048171482Sjeff * jobs such as make. This has little effect on the make process itself but 2049171482Sjeff * causes new processes spawned by it to receive worse scores immediately. 2050171482Sjeff */ 2051113372Sjeffvoid 2052164939Sjuliansched_exit_thread(struct thread *td, struct thread *child) 2053164936Sjulian{ 2054165762Sjeff 2055187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2056187357Sjeff "prio:td", child->td_priority); 2057165762Sjeff /* 2058165762Sjeff * Give the child's runtime to the parent without returning the 2059165762Sjeff * sleep time as a penalty to the parent. This causes shells that 2060165762Sjeff * launch expensive things to mark their children as expensive. 2061165762Sjeff */ 2062170293Sjeff thread_lock(td); 2063171482Sjeff td->td_sched->ts_runtime += child->td_sched->ts_runtime; 2064164939Sjulian sched_interact_update(td); 2065165762Sjeff sched_priority(td); 2066170293Sjeff thread_unlock(td); 2067164936Sjulian} 2068164936Sjulian 2069177005Sjeffvoid 2070177005Sjeffsched_preempt(struct thread *td) 2071177005Sjeff{ 2072177005Sjeff struct tdq *tdq; 2073177005Sjeff 2074177005Sjeff thread_lock(td); 2075177005Sjeff tdq = TDQ_SELF(); 2076177005Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2077177005Sjeff tdq->tdq_ipipending = 0; 2078177005Sjeff if (td->td_priority > tdq->tdq_lowpri) { 2079178272Sjeff int flags; 2080178272Sjeff 2081178272Sjeff flags = SW_INVOL | SW_PREEMPT; 2082177005Sjeff if (td->td_critnest > 1) 2083177005Sjeff td->td_owepreempt = 1; 2084178272Sjeff else if (TD_IS_IDLETHREAD(td)) 2085178272Sjeff mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL); 2086177005Sjeff else 2087178272Sjeff mi_switch(flags | SWT_REMOTEPREEMPT, NULL); 2088177005Sjeff } 2089177005Sjeff thread_unlock(td); 2090177005Sjeff} 2091177005Sjeff 2092171482Sjeff/* 2093171482Sjeff * Fix priorities on return to user-space. Priorities may be elevated due 2094171482Sjeff * to static priorities in msleep() or similar. 2095171482Sjeff */ 2096164936Sjulianvoid 2097164936Sjuliansched_userret(struct thread *td) 2098164936Sjulian{ 2099164936Sjulian /* 2100164936Sjulian * XXX we cheat slightly on the locking here to avoid locking in 2101164936Sjulian * the usual case. Setting td_priority here is essentially an 2102164936Sjulian * incomplete workaround for not setting it properly elsewhere. 2103164936Sjulian * Now that some interrupt handlers are threads, not setting it 2104164936Sjulian * properly elsewhere can clobber it in the window between setting 2105164936Sjulian * it here and returning to user mode, so don't waste time setting 2106164936Sjulian * it perfectly here. 2107164936Sjulian */ 2108164936Sjulian KASSERT((td->td_flags & TDF_BORROWING) == 0, 2109164936Sjulian ("thread with borrowed priority returning to userland")); 2110164936Sjulian if (td->td_priority != td->td_user_pri) { 2111170293Sjeff thread_lock(td); 2112164936Sjulian td->td_priority = td->td_user_pri; 2113164936Sjulian td->td_base_pri = td->td_user_pri; 2114177005Sjeff tdq_setlowpri(TDQ_SELF(), td); 2115170293Sjeff thread_unlock(td); 2116164936Sjulian } 2117164936Sjulian} 2118164936Sjulian 2119171482Sjeff/* 2120171482Sjeff * Handle a stathz tick. This is really only relevant for timeshare 2121171482Sjeff * threads. 2122171482Sjeff */ 2123164936Sjulianvoid 2124121127Sjeffsched_clock(struct thread *td) 2125109864Sjeff{ 2126164936Sjulian struct tdq *tdq; 2127164936Sjulian struct td_sched *ts; 2128109864Sjeff 2129171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2130164936Sjulian tdq = TDQ_SELF(); 2131172409Sjeff#ifdef SMP 2132133427Sjeff /* 2133172409Sjeff * We run the long term load balancer infrequently on the first cpu. 2134172409Sjeff */ 2135172409Sjeff if (balance_tdq == tdq) { 2136172409Sjeff if (balance_ticks && --balance_ticks == 0) 2137172409Sjeff sched_balance(); 2138172409Sjeff } 2139172409Sjeff#endif 2140172409Sjeff /* 2141178277Sjeff * Save the old switch count so we have a record of the last ticks 2142178277Sjeff * activity. Initialize the new switch count based on our load. 2143178277Sjeff * If there is some activity seed it to reflect that. 2144178277Sjeff */ 2145178277Sjeff tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 2146178471Sjeff tdq->tdq_switchcnt = tdq->tdq_load; 2147178277Sjeff /* 2148165766Sjeff * Advance the insert index once for each tick to ensure that all 2149165766Sjeff * threads get a chance to run. 2150133427Sjeff */ 2151165766Sjeff if (tdq->tdq_idx == tdq->tdq_ridx) { 2152165766Sjeff tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 2153165766Sjeff if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 2154165766Sjeff tdq->tdq_ridx = tdq->tdq_idx; 2155165766Sjeff } 2156165766Sjeff ts = td->td_sched; 2157175104Sjeff if (td->td_pri_class & PRI_FIFO_BIT) 2158113357Sjeff return; 2159175104Sjeff if (td->td_pri_class == PRI_TIMESHARE) { 2160175104Sjeff /* 2161175104Sjeff * We used a tick; charge it to the thread so 2162175104Sjeff * that we can compute our interactivity. 2163175104Sjeff */ 2164175104Sjeff td->td_sched->ts_runtime += tickincr; 2165175104Sjeff sched_interact_update(td); 2166177009Sjeff sched_priority(td); 2167175104Sjeff } 2168113357Sjeff /* 2169109864Sjeff * We used up one time slice. 2170109864Sjeff */ 2171164936Sjulian if (--ts->ts_slice > 0) 2172113357Sjeff return; 2173109864Sjeff /* 2174177009Sjeff * We're out of time, force a requeue at userret(). 2175109864Sjeff */ 2176177009Sjeff ts->ts_slice = sched_slice; 2177113357Sjeff td->td_flags |= TDF_NEEDRESCHED; 2178109864Sjeff} 2179109864Sjeff 2180171482Sjeff/* 2181171482Sjeff * Called once per hz tick. Used for cpu utilization information. This 2182171482Sjeff * is easier than trying to scale based on stathz. 2183171482Sjeff */ 2184171482Sjeffvoid 2185171482Sjeffsched_tick(void) 2186171482Sjeff{ 2187171482Sjeff struct td_sched *ts; 2188171482Sjeff 2189171482Sjeff ts = curthread->td_sched; 2190180607Sjeff /* 2191180607Sjeff * Ticks is updated asynchronously on a single cpu. Check here to 2192180607Sjeff * avoid incrementing ts_ticks multiple times in a single tick. 2193180607Sjeff */ 2194180607Sjeff if (ts->ts_ltick == ticks) 2195180607Sjeff return; 2196171482Sjeff /* Adjust ticks for pctcpu */ 2197171482Sjeff ts->ts_ticks += 1 << SCHED_TICK_SHIFT; 2198171482Sjeff ts->ts_ltick = ticks; 2199171482Sjeff /* 2200171482Sjeff * Update if we've exceeded our desired tick threshhold by over one 2201171482Sjeff * second. 2202171482Sjeff */ 2203171482Sjeff if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick) 2204171482Sjeff sched_pctcpu_update(ts); 2205171482Sjeff} 2206171482Sjeff 2207171482Sjeff/* 2208171482Sjeff * Return whether the current CPU has runnable tasks. Used for in-kernel 2209171482Sjeff * cooperative idle threads. 2210171482Sjeff */ 2211109864Sjeffint 2212109864Sjeffsched_runnable(void) 2213109864Sjeff{ 2214164936Sjulian struct tdq *tdq; 2215115998Sjeff int load; 2216109864Sjeff 2217115998Sjeff load = 1; 2218115998Sjeff 2219164936Sjulian tdq = TDQ_SELF(); 2220121605Sjeff if ((curthread->td_flags & TDF_IDLETD) != 0) { 2221165620Sjeff if (tdq->tdq_load > 0) 2222121605Sjeff goto out; 2223121605Sjeff } else 2224165620Sjeff if (tdq->tdq_load - 1 > 0) 2225121605Sjeff goto out; 2226115998Sjeff load = 0; 2227115998Sjeffout: 2228115998Sjeff return (load); 2229109864Sjeff} 2230109864Sjeff 2231171482Sjeff/* 2232171482Sjeff * Choose the highest priority thread to run. The thread is removed from 2233171482Sjeff * the run-queue while running however the load remains. For SMP we set 2234171482Sjeff * the tdq in the global idle bitmask if it idles here. 2235171482Sjeff */ 2236166190Sjeffstruct thread * 2237109970Sjeffsched_choose(void) 2238109970Sjeff{ 2239177435Sjeff struct thread *td; 2240164936Sjulian struct tdq *tdq; 2241109970Sjeff 2242164936Sjulian tdq = TDQ_SELF(); 2243171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2244177435Sjeff td = tdq_choose(tdq); 2245177435Sjeff if (td) { 2246177435Sjeff td->td_sched->ts_ltick = ticks; 2247177435Sjeff tdq_runq_rem(tdq, td); 2248177903Sjeff tdq->tdq_lowpri = td->td_priority; 2249177435Sjeff return (td); 2250109864Sjeff } 2251177903Sjeff tdq->tdq_lowpri = PRI_MAX_IDLE; 2252176735Sjeff return (PCPU_GET(idlethread)); 2253109864Sjeff} 2254109864Sjeff 2255171482Sjeff/* 2256171482Sjeff * Set owepreempt if necessary. Preemption never happens directly in ULE, 2257171482Sjeff * we always request it once we exit a critical section. 2258171482Sjeff */ 2259171482Sjeffstatic inline void 2260171482Sjeffsched_setpreempt(struct thread *td) 2261166190Sjeff{ 2262166190Sjeff struct thread *ctd; 2263166190Sjeff int cpri; 2264166190Sjeff int pri; 2265166190Sjeff 2266177005Sjeff THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2267177005Sjeff 2268166190Sjeff ctd = curthread; 2269166190Sjeff pri = td->td_priority; 2270166190Sjeff cpri = ctd->td_priority; 2271177005Sjeff if (pri < cpri) 2272177005Sjeff ctd->td_flags |= TDF_NEEDRESCHED; 2273166190Sjeff if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2274171482Sjeff return; 2275177005Sjeff if (!sched_shouldpreempt(pri, cpri, 0)) 2276171482Sjeff return; 2277171482Sjeff ctd->td_owepreempt = 1; 2278166190Sjeff} 2279166190Sjeff 2280171482Sjeff/* 2281177009Sjeff * Add a thread to a thread queue. Select the appropriate runq and add the 2282177009Sjeff * thread to it. This is the internal function called when the tdq is 2283177009Sjeff * predetermined. 2284171482Sjeff */ 2285109864Sjeffvoid 2286171482Sjefftdq_add(struct tdq *tdq, struct thread *td, int flags) 2287109864Sjeff{ 2288109864Sjeff 2289171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2290166190Sjeff KASSERT((td->td_inhibitors == 0), 2291166190Sjeff ("sched_add: trying to run inhibited thread")); 2292166190Sjeff KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 2293166190Sjeff ("sched_add: bad thread state")); 2294172207Sjeff KASSERT(td->td_flags & TDF_INMEM, 2295172207Sjeff ("sched_add: thread swapped out")); 2296171482Sjeff 2297171482Sjeff if (td->td_priority < tdq->tdq_lowpri) 2298171482Sjeff tdq->tdq_lowpri = td->td_priority; 2299177435Sjeff tdq_runq_add(tdq, td, flags); 2300177435Sjeff tdq_load_add(tdq, td); 2301171482Sjeff} 2302171482Sjeff 2303171482Sjeff/* 2304171482Sjeff * Select the target thread queue and add a thread to it. Request 2305171482Sjeff * preemption or IPI a remote processor if required. 2306171482Sjeff */ 2307171482Sjeffvoid 2308171482Sjeffsched_add(struct thread *td, int flags) 2309171482Sjeff{ 2310171482Sjeff struct tdq *tdq; 2311171482Sjeff#ifdef SMP 2312171482Sjeff int cpu; 2313171482Sjeff#endif 2314187357Sjeff 2315187357Sjeff KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 2316187357Sjeff "prio:%d", td->td_priority, KTR_ATTR_LINKED, 2317187357Sjeff sched_tdname(curthread)); 2318187357Sjeff KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 2319187357Sjeff KTR_ATTR_LINKED, sched_tdname(td)); 2320171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2321166108Sjeff /* 2322171482Sjeff * Recalculate the priority before we select the target cpu or 2323171482Sjeff * run-queue. 2324166108Sjeff */ 2325171482Sjeff if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2326171482Sjeff sched_priority(td); 2327171482Sjeff#ifdef SMP 2328171482Sjeff /* 2329171482Sjeff * Pick the destination cpu and if it isn't ours transfer to the 2330171482Sjeff * target cpu. 2331171482Sjeff */ 2332177435Sjeff cpu = sched_pickcpu(td, flags); 2333177435Sjeff tdq = sched_setcpu(td, cpu, flags); 2334171482Sjeff tdq_add(tdq, td, flags); 2335177009Sjeff if (cpu != PCPU_GET(cpuid)) { 2336177435Sjeff tdq_notify(tdq, td); 2337166108Sjeff return; 2338166108Sjeff } 2339171482Sjeff#else 2340171482Sjeff tdq = TDQ_SELF(); 2341171482Sjeff TDQ_LOCK(tdq); 2342171482Sjeff /* 2343171482Sjeff * Now that the thread is moving to the run-queue, set the lock 2344171482Sjeff * to the scheduler's lock. 2345171482Sjeff */ 2346171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2347171482Sjeff tdq_add(tdq, td, flags); 2348166108Sjeff#endif 2349171482Sjeff if (!(flags & SRQ_YIELDING)) 2350171482Sjeff sched_setpreempt(td); 2351109864Sjeff} 2352109864Sjeff 2353171482Sjeff/* 2354171482Sjeff * Remove a thread from a run-queue without running it. This is used 2355171482Sjeff * when we're stealing a thread from a remote queue. Otherwise all threads 2356171482Sjeff * exit by calling sched_exit_thread() and sched_throw() themselves. 2357171482Sjeff */ 2358109864Sjeffvoid 2359121127Sjeffsched_rem(struct thread *td) 2360109864Sjeff{ 2361164936Sjulian struct tdq *tdq; 2362113357Sjeff 2363187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 2364187357Sjeff "prio:%d", td->td_priority); 2365177435Sjeff tdq = TDQ_CPU(td->td_sched->ts_cpu); 2366171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2367171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2368166190Sjeff KASSERT(TD_ON_RUNQ(td), 2369164936Sjulian ("sched_rem: thread not on run queue")); 2370177435Sjeff tdq_runq_rem(tdq, td); 2371177435Sjeff tdq_load_rem(tdq, td); 2372166190Sjeff TD_SET_CAN_RUN(td); 2373176735Sjeff if (td->td_priority == tdq->tdq_lowpri) 2374176735Sjeff tdq_setlowpri(tdq, NULL); 2375109864Sjeff} 2376109864Sjeff 2377171482Sjeff/* 2378171482Sjeff * Fetch cpu utilization information. Updates on demand. 2379171482Sjeff */ 2380109864Sjefffixpt_t 2381121127Sjeffsched_pctcpu(struct thread *td) 2382109864Sjeff{ 2383109864Sjeff fixpt_t pctcpu; 2384164936Sjulian struct td_sched *ts; 2385109864Sjeff 2386109864Sjeff pctcpu = 0; 2387164936Sjulian ts = td->td_sched; 2388164936Sjulian if (ts == NULL) 2389121290Sjeff return (0); 2390109864Sjeff 2391170293Sjeff thread_lock(td); 2392164936Sjulian if (ts->ts_ticks) { 2393109864Sjeff int rtick; 2394109864Sjeff 2395165796Sjeff sched_pctcpu_update(ts); 2396109864Sjeff /* How many rtick per second ? */ 2397165762Sjeff rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2398165762Sjeff pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 2399109864Sjeff } 2400170293Sjeff thread_unlock(td); 2401109864Sjeff 2402109864Sjeff return (pctcpu); 2403109864Sjeff} 2404109864Sjeff 2405176735Sjeff/* 2406176735Sjeff * Enforce affinity settings for a thread. Called after adjustments to 2407176735Sjeff * cpumask. 2408176735Sjeff */ 2409176729Sjeffvoid 2410176729Sjeffsched_affinity(struct thread *td) 2411176729Sjeff{ 2412176735Sjeff#ifdef SMP 2413176735Sjeff struct td_sched *ts; 2414176735Sjeff int cpu; 2415176735Sjeff 2416176735Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2417176735Sjeff ts = td->td_sched; 2418176735Sjeff if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 2419176735Sjeff return; 2420189787Sjeff if (TD_ON_RUNQ(td)) { 2421189787Sjeff sched_rem(td); 2422189787Sjeff sched_add(td, SRQ_BORING); 2423189787Sjeff return; 2424189787Sjeff } 2425176735Sjeff if (!TD_IS_RUNNING(td)) 2426176735Sjeff return; 2427176735Sjeff td->td_flags |= TDF_NEEDRESCHED; 2428176735Sjeff if (!THREAD_CAN_MIGRATE(td)) 2429176735Sjeff return; 2430176735Sjeff /* 2431176735Sjeff * Assign the new cpu and force a switch before returning to 2432176735Sjeff * userspace. If the target thread is not running locally send 2433176735Sjeff * an ipi to force the issue. 2434176735Sjeff */ 2435176735Sjeff cpu = ts->ts_cpu; 2436177435Sjeff ts->ts_cpu = sched_pickcpu(td, 0); 2437176735Sjeff if (cpu != PCPU_GET(cpuid)) 2438176735Sjeff ipi_selected(1 << cpu, IPI_PREEMPT); 2439176735Sjeff#endif 2440176729Sjeff} 2441176729Sjeff 2442171482Sjeff/* 2443171482Sjeff * Bind a thread to a target cpu. 2444171482Sjeff */ 2445122038Sjeffvoid 2446122038Sjeffsched_bind(struct thread *td, int cpu) 2447122038Sjeff{ 2448164936Sjulian struct td_sched *ts; 2449122038Sjeff 2450171713Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 2451164936Sjulian ts = td->td_sched; 2452166137Sjeff if (ts->ts_flags & TSF_BOUND) 2453166152Sjeff sched_unbind(td); 2454164936Sjulian ts->ts_flags |= TSF_BOUND; 2455166137Sjeff sched_pin(); 2456123433Sjeff if (PCPU_GET(cpuid) == cpu) 2457122038Sjeff return; 2458166137Sjeff ts->ts_cpu = cpu; 2459122038Sjeff /* When we return from mi_switch we'll be on the correct cpu. */ 2460131527Sphk mi_switch(SW_VOL, NULL); 2461122038Sjeff} 2462122038Sjeff 2463171482Sjeff/* 2464171482Sjeff * Release a bound thread. 2465171482Sjeff */ 2466122038Sjeffvoid 2467122038Sjeffsched_unbind(struct thread *td) 2468122038Sjeff{ 2469165762Sjeff struct td_sched *ts; 2470165762Sjeff 2471170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2472165762Sjeff ts = td->td_sched; 2473166137Sjeff if ((ts->ts_flags & TSF_BOUND) == 0) 2474166137Sjeff return; 2475165762Sjeff ts->ts_flags &= ~TSF_BOUND; 2476165762Sjeff sched_unpin(); 2477122038Sjeff} 2478122038Sjeff 2479109864Sjeffint 2480145256Sjkoshysched_is_bound(struct thread *td) 2481145256Sjkoshy{ 2482170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2483164936Sjulian return (td->td_sched->ts_flags & TSF_BOUND); 2484145256Sjkoshy} 2485145256Sjkoshy 2486171482Sjeff/* 2487171482Sjeff * Basic yield call. 2488171482Sjeff */ 2489159630Sdavidxuvoid 2490159630Sdavidxusched_relinquish(struct thread *td) 2491159630Sdavidxu{ 2492170293Sjeff thread_lock(td); 2493178272Sjeff mi_switch(SW_VOL | SWT_RELINQUISH, NULL); 2494170293Sjeff thread_unlock(td); 2495159630Sdavidxu} 2496159630Sdavidxu 2497171482Sjeff/* 2498171482Sjeff * Return the total system load. 2499171482Sjeff */ 2500145256Sjkoshyint 2501125289Sjeffsched_load(void) 2502125289Sjeff{ 2503125289Sjeff#ifdef SMP 2504125289Sjeff int total; 2505125289Sjeff int i; 2506125289Sjeff 2507125289Sjeff total = 0; 2508176735Sjeff for (i = 0; i <= mp_maxid; i++) 2509176735Sjeff total += TDQ_CPU(i)->tdq_sysload; 2510125289Sjeff return (total); 2511125289Sjeff#else 2512165620Sjeff return (TDQ_SELF()->tdq_sysload); 2513125289Sjeff#endif 2514125289Sjeff} 2515125289Sjeff 2516125289Sjeffint 2517109864Sjeffsched_sizeof_proc(void) 2518109864Sjeff{ 2519109864Sjeff return (sizeof(struct proc)); 2520109864Sjeff} 2521109864Sjeff 2522109864Sjeffint 2523109864Sjeffsched_sizeof_thread(void) 2524109864Sjeff{ 2525109864Sjeff return (sizeof(struct thread) + sizeof(struct td_sched)); 2526109864Sjeff} 2527159570Sdavidxu 2528166190Sjeff/* 2529166190Sjeff * The actual idle process. 2530166190Sjeff */ 2531166190Sjeffvoid 2532166190Sjeffsched_idletd(void *dummy) 2533166190Sjeff{ 2534166190Sjeff struct thread *td; 2535171482Sjeff struct tdq *tdq; 2536178277Sjeff int switchcnt; 2537178277Sjeff int i; 2538166190Sjeff 2539166190Sjeff td = curthread; 2540171482Sjeff tdq = TDQ_SELF(); 2541166190Sjeff mtx_assert(&Giant, MA_NOTOWNED); 2542171482Sjeff /* ULE relies on preemption for idle interruption. */ 2543171482Sjeff for (;;) { 2544178277Sjeff tdq->tdq_idlestate = TDQ_RUNNING; 2545171482Sjeff#ifdef SMP 2546178277Sjeff if (tdq_idled(tdq) == 0) 2547178277Sjeff continue; 2548171482Sjeff#endif 2549178277Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2550178277Sjeff /* 2551178277Sjeff * If we're switching very frequently, spin while checking 2552178277Sjeff * for load rather than entering a low power state that 2553178277Sjeff * requires an IPI. 2554178277Sjeff */ 2555178277Sjeff if (switchcnt > sched_idlespinthresh) { 2556178277Sjeff for (i = 0; i < sched_idlespins; i++) { 2557178277Sjeff if (tdq->tdq_load) 2558178277Sjeff break; 2559178277Sjeff cpu_spinwait(); 2560178277Sjeff } 2561178277Sjeff } 2562178277Sjeff /* 2563178277Sjeff * We must set our state to IDLE before checking 2564178277Sjeff * tdq_load for the last time to avoid a race with 2565178277Sjeff * tdq_notify(). 2566178277Sjeff */ 2567178277Sjeff if (tdq->tdq_load == 0) { 2568178471Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2569178277Sjeff tdq->tdq_idlestate = TDQ_IDLE; 2570178277Sjeff if (tdq->tdq_load == 0) 2571178471Sjeff cpu_idle(switchcnt > 1); 2572178277Sjeff } 2573178277Sjeff if (tdq->tdq_load) { 2574178277Sjeff thread_lock(td); 2575178277Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 2576178277Sjeff thread_unlock(td); 2577178277Sjeff } 2578171482Sjeff } 2579166190Sjeff} 2580166190Sjeff 2581170293Sjeff/* 2582170293Sjeff * A CPU is entering for the first time or a thread is exiting. 2583170293Sjeff */ 2584170293Sjeffvoid 2585170293Sjeffsched_throw(struct thread *td) 2586170293Sjeff{ 2587172411Sjeff struct thread *newtd; 2588171482Sjeff struct tdq *tdq; 2589171482Sjeff 2590171482Sjeff tdq = TDQ_SELF(); 2591170293Sjeff if (td == NULL) { 2592171482Sjeff /* Correct spinlock nesting and acquire the correct lock. */ 2593171482Sjeff TDQ_LOCK(tdq); 2594170293Sjeff spinlock_exit(); 2595170293Sjeff } else { 2596171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2597177435Sjeff tdq_load_rem(tdq, td); 2598174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 2599170293Sjeff } 2600170293Sjeff KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count")); 2601172411Sjeff newtd = choosethread(); 2602172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 2603170293Sjeff PCPU_SET(switchtime, cpu_ticks()); 2604170293Sjeff PCPU_SET(switchticks, ticks); 2605172411Sjeff cpu_throw(td, newtd); /* doesn't return */ 2606170293Sjeff} 2607170293Sjeff 2608171482Sjeff/* 2609171482Sjeff * This is called from fork_exit(). Just acquire the correct locks and 2610171482Sjeff * let fork do the rest of the work. 2611171482Sjeff */ 2612170293Sjeffvoid 2613170600Sjeffsched_fork_exit(struct thread *td) 2614170293Sjeff{ 2615171482Sjeff struct td_sched *ts; 2616171482Sjeff struct tdq *tdq; 2617171482Sjeff int cpuid; 2618170293Sjeff 2619170293Sjeff /* 2620170293Sjeff * Finish setting up thread glue so that it begins execution in a 2621171482Sjeff * non-nested critical section with the scheduler lock held. 2622170293Sjeff */ 2623171482Sjeff cpuid = PCPU_GET(cpuid); 2624171482Sjeff tdq = TDQ_CPU(cpuid); 2625171482Sjeff ts = td->td_sched; 2626171482Sjeff if (TD_IS_IDLETHREAD(td)) 2627171482Sjeff td->td_lock = TDQ_LOCKPTR(tdq); 2628171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2629171482Sjeff td->td_oncpu = cpuid; 2630172411Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 2631174629Sjeff lock_profile_obtain_lock_success( 2632174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 2633170293Sjeff} 2634170293Sjeff 2635187357Sjeff/* 2636187357Sjeff * Create on first use to catch odd startup conditons. 2637187357Sjeff */ 2638187357Sjeffchar * 2639187357Sjeffsched_tdname(struct thread *td) 2640187357Sjeff{ 2641187357Sjeff#ifdef KTR 2642187357Sjeff struct td_sched *ts; 2643187357Sjeff 2644187357Sjeff ts = td->td_sched; 2645187357Sjeff if (ts->ts_name[0] == '\0') 2646187357Sjeff snprintf(ts->ts_name, sizeof(ts->ts_name), 2647187357Sjeff "%s tid %d", td->td_name, td->td_tid); 2648187357Sjeff return (ts->ts_name); 2649187357Sjeff#else 2650187357Sjeff return (td->td_name); 2651187357Sjeff#endif 2652187357Sjeff} 2653187357Sjeff 2654184439Sivoras#ifdef SMP 2655184439Sivoras 2656184439Sivoras/* 2657184439Sivoras * Build the CPU topology dump string. Is recursively called to collect 2658184439Sivoras * the topology tree. 2659184439Sivoras */ 2660184439Sivorasstatic int 2661184439Sivorassysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 2662184439Sivoras int indent) 2663184439Sivoras{ 2664184439Sivoras int i, first; 2665184439Sivoras 2666184439Sivoras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 2667184439Sivoras "", indent, cg->cg_level); 2668184439Sivoras sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"0x%x\">", indent, "", 2669184439Sivoras cg->cg_count, cg->cg_mask); 2670184439Sivoras first = TRUE; 2671184439Sivoras for (i = 0; i < MAXCPU; i++) { 2672184439Sivoras if ((cg->cg_mask & (1 << i)) != 0) { 2673184439Sivoras if (!first) 2674184439Sivoras sbuf_printf(sb, ", "); 2675184439Sivoras else 2676184439Sivoras first = FALSE; 2677184439Sivoras sbuf_printf(sb, "%d", i); 2678184439Sivoras } 2679184439Sivoras } 2680184439Sivoras sbuf_printf(sb, "</cpu>\n"); 2681184439Sivoras 2682184439Sivoras sbuf_printf(sb, "%*s <flags>", indent, ""); 2683184439Sivoras if (cg->cg_flags != 0) { 2684184439Sivoras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 2685186435Sivoras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>\n"); 2686184439Sivoras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2687186435Sivoras sbuf_printf(sb, "<flag name=\"THREAD\">SMT group</flag>\n"); 2688184439Sivoras } 2689184439Sivoras sbuf_printf(sb, "</flags>\n"); 2690184439Sivoras 2691184439Sivoras if (cg->cg_children > 0) { 2692184439Sivoras sbuf_printf(sb, "%*s <children>\n", indent, ""); 2693184439Sivoras for (i = 0; i < cg->cg_children; i++) 2694184439Sivoras sysctl_kern_sched_topology_spec_internal(sb, 2695184439Sivoras &cg->cg_child[i], indent+2); 2696184439Sivoras sbuf_printf(sb, "%*s </children>\n", indent, ""); 2697184439Sivoras } 2698184439Sivoras sbuf_printf(sb, "%*s</group>\n", indent, ""); 2699184439Sivoras return (0); 2700184439Sivoras} 2701184439Sivoras 2702184439Sivoras/* 2703184439Sivoras * Sysctl handler for retrieving topology dump. It's a wrapper for 2704184439Sivoras * the recursive sysctl_kern_smp_topology_spec_internal(). 2705184439Sivoras */ 2706184439Sivorasstatic int 2707184439Sivorassysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 2708184439Sivoras{ 2709184439Sivoras struct sbuf *topo; 2710184439Sivoras int err; 2711184439Sivoras 2712184439Sivoras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 2713184439Sivoras 2714184570Sivoras topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND); 2715184439Sivoras if (topo == NULL) 2716184439Sivoras return (ENOMEM); 2717184439Sivoras 2718184439Sivoras sbuf_printf(topo, "<groups>\n"); 2719184439Sivoras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 2720184439Sivoras sbuf_printf(topo, "</groups>\n"); 2721184439Sivoras 2722184439Sivoras if (err == 0) { 2723184439Sivoras sbuf_finish(topo); 2724184439Sivoras err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo)); 2725184439Sivoras } 2726184439Sivoras sbuf_delete(topo); 2727184439Sivoras return (err); 2728184439Sivoras} 2729184439Sivoras#endif 2730184439Sivoras 2731177435SjeffSYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2732171482SjeffSYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 2733165762Sjeff "Scheduler name"); 2734171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 2735171482Sjeff "Slice size for timeshare threads"); 2736171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 2737171482Sjeff "Interactivity score threshold"); 2738171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh, 2739171482Sjeff 0,"Min priority for preemption, lower priorities have greater precedence"); 2740177085SjeffSYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 2741177085Sjeff 0,"Controls whether static kernel priorities are assigned to sleeping threads."); 2742178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 2743178277Sjeff 0,"Number of times idle will spin waiting for new work."); 2744178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh, 2745178277Sjeff 0,"Threshold before we will permit idle spinning."); 2746166108Sjeff#ifdef SMP 2747171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 2748171482Sjeff "Number of hz ticks to keep thread affinity for"); 2749171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 2750171482Sjeff "Enables the long-term load balancer"); 2751172409SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 2752172409Sjeff &balance_interval, 0, 2753172409Sjeff "Average frequency in stathz ticks to run the long-term balancer"); 2754171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0, 2755171482Sjeff "Steals work from another hyper-threaded core on idle"); 2756171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 2757171482Sjeff "Attempts to steal work from other cores before idling"); 2758171506SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 2759171506Sjeff "Minimum load on remote cpu before we'll steal"); 2760184439Sivoras 2761184439Sivoras/* Retrieve SMP topology */ 2762184439SivorasSYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 2763184439Sivoras CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 2764184439Sivoras "XML dump of detected CPU topology"); 2765166108Sjeff#endif 2766165762Sjeff 2767172264Sjeff/* ps compat. All cpu percentages from ULE are weighted. */ 2768172293Sjeffstatic int ccpu = 0; 2769165762SjeffSYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 2770