sched_ule.c revision 229429
1109864Sjeff/*- 2165762Sjeff * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org> 3109864Sjeff * All rights reserved. 4109864Sjeff * 5109864Sjeff * Redistribution and use in source and binary forms, with or without 6109864Sjeff * modification, are permitted provided that the following conditions 7109864Sjeff * are met: 8109864Sjeff * 1. Redistributions of source code must retain the above copyright 9109864Sjeff * notice unmodified, this list of conditions, and the following 10109864Sjeff * disclaimer. 11109864Sjeff * 2. Redistributions in binary form must reproduce the above copyright 12109864Sjeff * notice, this list of conditions and the following disclaimer in the 13109864Sjeff * documentation and/or other materials provided with the distribution. 14109864Sjeff * 15109864Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16109864Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17109864Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18109864Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19109864Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20109864Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21109864Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22109864Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23109864Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24109864Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25109864Sjeff */ 26109864Sjeff 27171482Sjeff/* 28171482Sjeff * This file implements the ULE scheduler. ULE supports independent CPU 29171482Sjeff * run queues and fine grain locking. It has superior interactive 30171482Sjeff * performance under load even on uni-processor systems. 31171482Sjeff * 32171482Sjeff * etymology: 33172293Sjeff * ULE is the last three letters in schedule. It owes its name to a 34171482Sjeff * generic user created for a scheduling system by Paul Mikesell at 35171482Sjeff * Isilon Systems and a general lack of creativity on the part of the author. 36171482Sjeff */ 37171482Sjeff 38116182Sobrien#include <sys/cdefs.h> 39191645Sjeff__FBSDID("$FreeBSD: head/sys/kern/sched_ule.c 229429 2012-01-03 21:03:28Z jhb $"); 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 66145256Sjkoshy#ifdef HWPMC_HOOKS 67145256Sjkoshy#include <sys/pmckern.h> 68145256Sjkoshy#endif 69145256Sjkoshy 70179297Sjb#ifdef KDTRACE_HOOKS 71179297Sjb#include <sys/dtrace_bsd.h> 72179297Sjbint dtrace_vtime_active; 73179297Sjbdtrace_vtime_switch_func_t dtrace_vtime_switch_func; 74179297Sjb#endif 75179297Sjb 76109864Sjeff#include <machine/cpu.h> 77121790Sjeff#include <machine/smp.h> 78109864Sjeff 79226057Smarius#if defined(__powerpc__) && defined(E500) 80172345Sjeff#error "This architecture is not currently compatible with ULE" 81166190Sjeff#endif 82166190Sjeff 83171482Sjeff#define KTR_ULE 0 84166137Sjeff 85187679Sjeff#define TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX))) 86187679Sjeff#define TDQ_NAME_LEN (sizeof("sched lock ") + sizeof(__XSTRING(MAXCPU))) 87224221Sattilio#define TDQ_LOADNAME_LEN (sizeof("CPU ") + sizeof(__XSTRING(MAXCPU)) - 1 + sizeof(" load")) 88187357Sjeff 89166137Sjeff/* 90171482Sjeff * Thread scheduler specific section. All fields are protected 91171482Sjeff * by the thread lock. 92146954Sjeff */ 93164936Sjulianstruct td_sched { 94171482Sjeff struct runq *ts_runq; /* Run-queue we're queued on. */ 95171482Sjeff short ts_flags; /* TSF_* flags. */ 96164936Sjulian u_char ts_cpu; /* CPU that we have affinity for. */ 97177009Sjeff int ts_rltick; /* Real last tick, for affinity. */ 98171482Sjeff int ts_slice; /* Ticks of slice remaining. */ 99171482Sjeff u_int ts_slptime; /* Number of ticks we vol. slept */ 100171482Sjeff u_int ts_runtime; /* Number of ticks we were running */ 101164936Sjulian int ts_ltick; /* Last tick that we were running on */ 102199764Sivoras int ts_incrtick; /* Last tick that we incremented on */ 103164936Sjulian int ts_ftick; /* First tick that we were running on */ 104164936Sjulian int ts_ticks; /* Tick count */ 105187357Sjeff#ifdef KTR 106187357Sjeff char ts_name[TS_NAME_LEN]; 107187357Sjeff#endif 108134791Sjulian}; 109164936Sjulian/* flags kept in ts_flags */ 110166108Sjeff#define TSF_BOUND 0x0001 /* Thread can not migrate. */ 111166108Sjeff#define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */ 112121790Sjeff 113164936Sjulianstatic struct td_sched td_sched0; 114109864Sjeff 115176735Sjeff#define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0) 116176735Sjeff#define THREAD_CAN_SCHED(td, cpu) \ 117176735Sjeff CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask) 118176735Sjeff 119109864Sjeff/* 120217351Sjhb * Priority ranges used for interactive and non-interactive timeshare 121217410Sjhb * threads. The timeshare priorities are split up into four ranges. 122217410Sjhb * The first range handles interactive threads. The last three ranges 123217410Sjhb * (NHALF, x, and NHALF) handle non-interactive threads with the outer 124217410Sjhb * ranges supporting nice values. 125217351Sjhb */ 126217410Sjhb#define PRI_TIMESHARE_RANGE (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE + 1) 127217410Sjhb#define PRI_INTERACT_RANGE ((PRI_TIMESHARE_RANGE - SCHED_PRI_NRESV) / 2) 128228718Savg#define PRI_BATCH_RANGE (PRI_TIMESHARE_RANGE - PRI_INTERACT_RANGE) 129217410Sjhb 130217410Sjhb#define PRI_MIN_INTERACT PRI_MIN_TIMESHARE 131217410Sjhb#define PRI_MAX_INTERACT (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE - 1) 132217410Sjhb#define PRI_MIN_BATCH (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE) 133217351Sjhb#define PRI_MAX_BATCH PRI_MAX_TIMESHARE 134217351Sjhb 135217351Sjhb/* 136165762Sjeff * Cpu percentage computation macros and defines. 137111857Sjeff * 138165762Sjeff * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 139165762Sjeff * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 140165796Sjeff * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 141165762Sjeff * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 142165762Sjeff * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 143165762Sjeff * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 144165762Sjeff */ 145165762Sjeff#define SCHED_TICK_SECS 10 146165762Sjeff#define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 147165796Sjeff#define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 148165762Sjeff#define SCHED_TICK_SHIFT 10 149165762Sjeff#define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 150165830Sjeff#define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 151165762Sjeff 152165762Sjeff/* 153165762Sjeff * These macros determine priorities for non-interactive threads. They are 154165762Sjeff * assigned a priority based on their recent cpu utilization as expressed 155165762Sjeff * by the ratio of ticks to the tick total. NHALF priorities at the start 156165762Sjeff * and end of the MIN to MAX timeshare range are only reachable with negative 157165762Sjeff * or positive nice respectively. 158165762Sjeff * 159165762Sjeff * PRI_RANGE: Priority range for utilization dependent priorities. 160116642Sjeff * PRI_NRESV: Number of nice values. 161165762Sjeff * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 162165762Sjeff * PRI_NICE: Determines the part of the priority inherited from nice. 163109864Sjeff */ 164165762Sjeff#define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 165121869Sjeff#define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 166217351Sjhb#define SCHED_PRI_MIN (PRI_MIN_BATCH + SCHED_PRI_NHALF) 167217351Sjhb#define SCHED_PRI_MAX (PRI_MAX_BATCH - SCHED_PRI_NHALF) 168217237Sjhb#define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1) 169165762Sjeff#define SCHED_PRI_TICKS(ts) \ 170165762Sjeff (SCHED_TICK_HZ((ts)) / \ 171165827Sjeff (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 172165762Sjeff#define SCHED_PRI_NICE(nice) (nice) 173109864Sjeff 174109864Sjeff/* 175165762Sjeff * These determine the interactivity of a process. Interactivity differs from 176165762Sjeff * cpu utilization in that it expresses the voluntary time slept vs time ran 177165762Sjeff * while cpu utilization includes all time not running. This more accurately 178165762Sjeff * models the intent of the thread. 179109864Sjeff * 180110645Sjeff * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 181110645Sjeff * before throttling back. 182121868Sjeff * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 183116365Sjeff * INTERACT_MAX: Maximum interactivity value. Smaller is better. 184215102Sattilio * INTERACT_THRESH: Threshold for placement on the current runq. 185109864Sjeff */ 186165762Sjeff#define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 187165762Sjeff#define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 188116365Sjeff#define SCHED_INTERACT_MAX (100) 189116365Sjeff#define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 190121126Sjeff#define SCHED_INTERACT_THRESH (30) 191111857Sjeff 192109864Sjeff/* 193165762Sjeff * tickincr: Converts a stathz tick into a hz domain scaled by 194165762Sjeff * the shift factor. Without the shift the error rate 195165762Sjeff * due to rounding would be unacceptably high. 196165762Sjeff * realstathz: stathz is sometimes 0 and run off of hz. 197165762Sjeff * sched_slice: Runtime of each thread before rescheduling. 198171482Sjeff * preempt_thresh: Priority threshold for preemption and remote IPIs. 199109864Sjeff */ 200165762Sjeffstatic int sched_interact = SCHED_INTERACT_THRESH; 201165762Sjeffstatic int realstathz; 202165762Sjeffstatic int tickincr; 203177009Sjeffstatic int sched_slice = 1; 204172345Sjeff#ifdef PREEMPTION 205172345Sjeff#ifdef FULL_PREEMPTION 206172345Sjeffstatic int preempt_thresh = PRI_MAX_IDLE; 207172345Sjeff#else 208171482Sjeffstatic int preempt_thresh = PRI_MIN_KERN; 209172345Sjeff#endif 210172345Sjeff#else 211172345Sjeffstatic int preempt_thresh = 0; 212172345Sjeff#endif 213217351Sjhbstatic int static_boost = PRI_MIN_BATCH; 214178277Sjeffstatic int sched_idlespins = 10000; 215212541Smavstatic int sched_idlespinthresh = 16; 216109864Sjeff 217109864Sjeff/* 218171482Sjeff * tdq - per processor runqs and statistics. All fields are protected by the 219171482Sjeff * tdq_lock. The load and lowpri may be accessed without to avoid excess 220171482Sjeff * locking in sched_pickcpu(); 221109864Sjeff */ 222164936Sjulianstruct tdq { 223177009Sjeff /* Ordered to improve efficiency of cpu_search() and switch(). */ 224177009Sjeff struct mtx tdq_lock; /* run queue lock. */ 225176735Sjeff struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 226178277Sjeff volatile int tdq_load; /* Aggregate load. */ 227212416Smav volatile int tdq_cpu_idle; /* cpu_idle() is active. */ 228176735Sjeff int tdq_sysload; /* For loadavg, !ITHD load. */ 229177009Sjeff int tdq_transferable; /* Transferable thread count. */ 230178277Sjeff short tdq_switchcnt; /* Switches this tick. */ 231178277Sjeff short tdq_oldswitchcnt; /* Switches last tick. */ 232177009Sjeff u_char tdq_lowpri; /* Lowest priority thread. */ 233177009Sjeff u_char tdq_ipipending; /* IPI pending. */ 234166557Sjeff u_char tdq_idx; /* Current insert index. */ 235166557Sjeff u_char tdq_ridx; /* Current removal index. */ 236177009Sjeff struct runq tdq_realtime; /* real-time run queue. */ 237177009Sjeff struct runq tdq_timeshare; /* timeshare run queue. */ 238177009Sjeff struct runq tdq_idle; /* Queue of IDLE threads. */ 239187357Sjeff char tdq_name[TDQ_NAME_LEN]; 240187357Sjeff#ifdef KTR 241187357Sjeff char tdq_loadname[TDQ_LOADNAME_LEN]; 242187357Sjeff#endif 243171482Sjeff} __aligned(64); 244109864Sjeff 245178277Sjeff/* Idle thread states and config. */ 246178277Sjeff#define TDQ_RUNNING 1 247178277Sjeff#define TDQ_IDLE 2 248166108Sjeff 249123433Sjeff#ifdef SMP 250184439Sivorasstruct cpu_group *cpu_top; /* CPU topology */ 251123433Sjeff 252176735Sjeff#define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 253176735Sjeff#define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 254166108Sjeff 255123433Sjeff/* 256166108Sjeff * Run-time tunables. 257166108Sjeff */ 258171506Sjeffstatic int rebalance = 1; 259172409Sjeffstatic int balance_interval = 128; /* Default set in sched_initticks(). */ 260166108Sjeffstatic int affinity; 261172409Sjeffstatic int steal_htt = 1; 262171506Sjeffstatic int steal_idle = 1; 263171506Sjeffstatic int steal_thresh = 2; 264166108Sjeff 265166108Sjeff/* 266165620Sjeff * One thread queue per processor. 267109864Sjeff */ 268164936Sjulianstatic struct tdq tdq_cpu[MAXCPU]; 269172409Sjeffstatic struct tdq *balance_tdq; 270172409Sjeffstatic int balance_ticks; 271129982Sjeff 272164936Sjulian#define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 273164936Sjulian#define TDQ_CPU(x) (&tdq_cpu[(x)]) 274171713Sjeff#define TDQ_ID(x) ((int)((x) - tdq_cpu)) 275123433Sjeff#else /* !SMP */ 276164936Sjulianstatic struct tdq tdq_cpu; 277129982Sjeff 278170315Sjeff#define TDQ_ID(x) (0) 279164936Sjulian#define TDQ_SELF() (&tdq_cpu) 280164936Sjulian#define TDQ_CPU(x) (&tdq_cpu) 281110028Sjeff#endif 282109864Sjeff 283171482Sjeff#define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 284171482Sjeff#define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 285171482Sjeff#define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 286171482Sjeff#define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 287176735Sjeff#define TDQ_LOCKPTR(t) (&(t)->tdq_lock) 288171482Sjeff 289163709Sjbstatic void sched_priority(struct thread *); 290146954Sjeffstatic void sched_thread_priority(struct thread *, u_char); 291163709Sjbstatic int sched_interact_score(struct thread *); 292163709Sjbstatic void sched_interact_update(struct thread *); 293163709Sjbstatic void sched_interact_fork(struct thread *); 294164936Sjulianstatic void sched_pctcpu_update(struct td_sched *); 295109864Sjeff 296110267Sjeff/* Operations on per processor queues */ 297177435Sjeffstatic struct thread *tdq_choose(struct tdq *); 298164936Sjulianstatic void tdq_setup(struct tdq *); 299177435Sjeffstatic void tdq_load_add(struct tdq *, struct thread *); 300177435Sjeffstatic void tdq_load_rem(struct tdq *, struct thread *); 301177435Sjeffstatic __inline void tdq_runq_add(struct tdq *, struct thread *, int); 302177435Sjeffstatic __inline void tdq_runq_rem(struct tdq *, struct thread *); 303177005Sjeffstatic inline int sched_shouldpreempt(int, int, int); 304164936Sjulianvoid tdq_print(int cpu); 305165762Sjeffstatic void runq_print(struct runq *rq); 306171482Sjeffstatic void tdq_add(struct tdq *, struct thread *, int); 307110267Sjeff#ifdef SMP 308176735Sjeffstatic int tdq_move(struct tdq *, struct tdq *); 309171482Sjeffstatic int tdq_idled(struct tdq *); 310177435Sjeffstatic void tdq_notify(struct tdq *, struct thread *); 311177435Sjeffstatic struct thread *tdq_steal(struct tdq *, int); 312177435Sjeffstatic struct thread *runq_steal(struct runq *, int); 313177435Sjeffstatic int sched_pickcpu(struct thread *, int); 314172409Sjeffstatic void sched_balance(void); 315176735Sjeffstatic int sched_balance_pair(struct tdq *, struct tdq *); 316177435Sjeffstatic inline struct tdq *sched_setcpu(struct thread *, int, int); 317171482Sjeffstatic inline void thread_unblock_switch(struct thread *, struct mtx *); 318171713Sjeffstatic struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int); 319184439Sivorasstatic int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 320184439Sivorasstatic int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 321184439Sivoras struct cpu_group *cg, int indent); 322121790Sjeff#endif 323110028Sjeff 324165762Sjeffstatic void sched_setup(void *dummy); 325177253SrwatsonSYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 326165762Sjeff 327165762Sjeffstatic void sched_initticks(void *dummy); 328177253SrwatsonSYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 329177253Srwatson NULL); 330165762Sjeff 331171482Sjeff/* 332171482Sjeff * Print the threads waiting on a run-queue. 333171482Sjeff */ 334165762Sjeffstatic void 335165762Sjeffrunq_print(struct runq *rq) 336165762Sjeff{ 337165762Sjeff struct rqhead *rqh; 338177435Sjeff struct thread *td; 339165762Sjeff int pri; 340165762Sjeff int j; 341165762Sjeff int i; 342165762Sjeff 343165762Sjeff for (i = 0; i < RQB_LEN; i++) { 344165762Sjeff printf("\t\trunq bits %d 0x%zx\n", 345165762Sjeff i, rq->rq_status.rqb_bits[i]); 346165762Sjeff for (j = 0; j < RQB_BPW; j++) 347165762Sjeff if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 348165762Sjeff pri = j + (i << RQB_L2BPW); 349165762Sjeff rqh = &rq->rq_queues[pri]; 350177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 351165762Sjeff printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 352177435Sjeff td, td->td_name, td->td_priority, 353177435Sjeff td->td_rqindex, pri); 354165762Sjeff } 355165762Sjeff } 356165762Sjeff } 357165762Sjeff} 358165762Sjeff 359171482Sjeff/* 360171482Sjeff * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 361171482Sjeff */ 362113357Sjeffvoid 363164936Sjuliantdq_print(int cpu) 364110267Sjeff{ 365164936Sjulian struct tdq *tdq; 366112994Sjeff 367164936Sjulian tdq = TDQ_CPU(cpu); 368112994Sjeff 369171713Sjeff printf("tdq %d:\n", TDQ_ID(tdq)); 370176735Sjeff printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 371176735Sjeff printf("\tLock name: %s\n", tdq->tdq_name); 372165620Sjeff printf("\tload: %d\n", tdq->tdq_load); 373178277Sjeff printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 374178277Sjeff printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 375171482Sjeff printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 376165766Sjeff printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 377178277Sjeff printf("\tload transferable: %d\n", tdq->tdq_transferable); 378178277Sjeff printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 379165762Sjeff printf("\trealtime runq:\n"); 380165762Sjeff runq_print(&tdq->tdq_realtime); 381165762Sjeff printf("\ttimeshare runq:\n"); 382165762Sjeff runq_print(&tdq->tdq_timeshare); 383165762Sjeff printf("\tidle runq:\n"); 384165762Sjeff runq_print(&tdq->tdq_idle); 385113357Sjeff} 386112994Sjeff 387177005Sjeffstatic inline int 388177005Sjeffsched_shouldpreempt(int pri, int cpri, int remote) 389177005Sjeff{ 390177005Sjeff /* 391177005Sjeff * If the new priority is not better than the current priority there is 392177005Sjeff * nothing to do. 393177005Sjeff */ 394177005Sjeff if (pri >= cpri) 395177005Sjeff return (0); 396177005Sjeff /* 397177005Sjeff * Always preempt idle. 398177005Sjeff */ 399177005Sjeff if (cpri >= PRI_MIN_IDLE) 400177005Sjeff return (1); 401177005Sjeff /* 402177005Sjeff * If preemption is disabled don't preempt others. 403177005Sjeff */ 404177005Sjeff if (preempt_thresh == 0) 405177005Sjeff return (0); 406177005Sjeff /* 407177005Sjeff * Preempt if we exceed the threshold. 408177005Sjeff */ 409177005Sjeff if (pri <= preempt_thresh) 410177005Sjeff return (1); 411177005Sjeff /* 412217351Sjhb * If we're interactive or better and there is non-interactive 413217351Sjhb * or worse running preempt only remote processors. 414177005Sjeff */ 415217351Sjhb if (remote && pri <= PRI_MAX_INTERACT && cpri > PRI_MAX_INTERACT) 416177005Sjeff return (1); 417177005Sjeff return (0); 418177005Sjeff} 419177005Sjeff 420171482Sjeff/* 421171482Sjeff * Add a thread to the actual run-queue. Keeps transferable counts up to 422171482Sjeff * date with what is actually on the run-queue. Selects the correct 423171482Sjeff * queue position for timeshare threads. 424171482Sjeff */ 425122744Sjeffstatic __inline void 426177435Sjefftdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 427122744Sjeff{ 428177435Sjeff struct td_sched *ts; 429177042Sjeff u_char pri; 430177042Sjeff 431171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 432177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 433177009Sjeff 434177435Sjeff pri = td->td_priority; 435177435Sjeff ts = td->td_sched; 436177435Sjeff TD_SET_RUNQ(td); 437177435Sjeff if (THREAD_CAN_MIGRATE(td)) { 438165620Sjeff tdq->tdq_transferable++; 439164936Sjulian ts->ts_flags |= TSF_XFERABLE; 440123433Sjeff } 441217351Sjhb if (pri < PRI_MIN_BATCH) { 442177042Sjeff ts->ts_runq = &tdq->tdq_realtime; 443217351Sjhb } else if (pri <= PRI_MAX_BATCH) { 444177042Sjeff ts->ts_runq = &tdq->tdq_timeshare; 445217351Sjhb KASSERT(pri <= PRI_MAX_BATCH && pri >= PRI_MIN_BATCH, 446165762Sjeff ("Invalid priority %d on timeshare runq", pri)); 447165762Sjeff /* 448165762Sjeff * This queue contains only priorities between MIN and MAX 449165762Sjeff * realtime. Use the whole queue to represent these values. 450165762Sjeff */ 451171713Sjeff if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 452228718Savg pri = RQ_NQS * (pri - PRI_MIN_BATCH) / PRI_BATCH_RANGE; 453165762Sjeff pri = (pri + tdq->tdq_idx) % RQ_NQS; 454165766Sjeff /* 455165766Sjeff * This effectively shortens the queue by one so we 456165766Sjeff * can have a one slot difference between idx and 457165766Sjeff * ridx while we wait for threads to drain. 458165766Sjeff */ 459165766Sjeff if (tdq->tdq_ridx != tdq->tdq_idx && 460165766Sjeff pri == tdq->tdq_ridx) 461167664Sjeff pri = (unsigned char)(pri - 1) % RQ_NQS; 462165762Sjeff } else 463165766Sjeff pri = tdq->tdq_ridx; 464177435Sjeff runq_add_pri(ts->ts_runq, td, pri, flags); 465177042Sjeff return; 466165762Sjeff } else 467177009Sjeff ts->ts_runq = &tdq->tdq_idle; 468177435Sjeff runq_add(ts->ts_runq, td, flags); 469177009Sjeff} 470177009Sjeff 471171482Sjeff/* 472171482Sjeff * Remove a thread from a run-queue. This typically happens when a thread 473171482Sjeff * is selected to run. Running threads are not on the queue and the 474171482Sjeff * transferable count does not reflect them. 475171482Sjeff */ 476122744Sjeffstatic __inline void 477177435Sjefftdq_runq_rem(struct tdq *tdq, struct thread *td) 478122744Sjeff{ 479177435Sjeff struct td_sched *ts; 480177435Sjeff 481177435Sjeff ts = td->td_sched; 482171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 483171482Sjeff KASSERT(ts->ts_runq != NULL, 484177435Sjeff ("tdq_runq_remove: thread %p null ts_runq", td)); 485164936Sjulian if (ts->ts_flags & TSF_XFERABLE) { 486165620Sjeff tdq->tdq_transferable--; 487164936Sjulian ts->ts_flags &= ~TSF_XFERABLE; 488123433Sjeff } 489165766Sjeff if (ts->ts_runq == &tdq->tdq_timeshare) { 490165766Sjeff if (tdq->tdq_idx != tdq->tdq_ridx) 491177435Sjeff runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 492165766Sjeff else 493177435Sjeff runq_remove_idx(ts->ts_runq, td, NULL); 494165766Sjeff } else 495177435Sjeff runq_remove(ts->ts_runq, td); 496122744Sjeff} 497122744Sjeff 498171482Sjeff/* 499171482Sjeff * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 500171482Sjeff * for this thread to the referenced thread queue. 501171482Sjeff */ 502113357Sjeffstatic void 503177435Sjefftdq_load_add(struct tdq *tdq, struct thread *td) 504113357Sjeff{ 505171482Sjeff 506171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 507177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 508177902Sjeff 509165620Sjeff tdq->tdq_load++; 510198854Sattilio if ((td->td_flags & TDF_NOLOAD) == 0) 511177902Sjeff tdq->tdq_sysload++; 512187357Sjeff KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 513110267Sjeff} 514113357Sjeff 515171482Sjeff/* 516171482Sjeff * Remove the load from a thread that is transitioning to a sleep state or 517171482Sjeff * exiting. 518171482Sjeff */ 519112994Sjeffstatic void 520177435Sjefftdq_load_rem(struct tdq *tdq, struct thread *td) 521110267Sjeff{ 522171482Sjeff 523177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 524171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 525171482Sjeff KASSERT(tdq->tdq_load != 0, 526171713Sjeff ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 527177902Sjeff 528165620Sjeff tdq->tdq_load--; 529198854Sattilio if ((td->td_flags & TDF_NOLOAD) == 0) 530177902Sjeff tdq->tdq_sysload--; 531187357Sjeff KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 532110267Sjeff} 533110267Sjeff 534176735Sjeff/* 535176735Sjeff * Set lowpri to its exact value by searching the run-queue and 536176735Sjeff * evaluating curthread. curthread may be passed as an optimization. 537176735Sjeff */ 538176735Sjeffstatic void 539176735Sjefftdq_setlowpri(struct tdq *tdq, struct thread *ctd) 540176735Sjeff{ 541176735Sjeff struct thread *td; 542176735Sjeff 543176735Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 544176735Sjeff if (ctd == NULL) 545176735Sjeff ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread; 546177435Sjeff td = tdq_choose(tdq); 547177435Sjeff if (td == NULL || td->td_priority > ctd->td_priority) 548176735Sjeff tdq->tdq_lowpri = ctd->td_priority; 549176735Sjeff else 550176735Sjeff tdq->tdq_lowpri = td->td_priority; 551176735Sjeff} 552176735Sjeff 553113357Sjeff#ifdef SMP 554176735Sjeffstruct cpu_search { 555194779Sjeff cpuset_t cs_mask; 556176735Sjeff u_int cs_load; 557176735Sjeff u_int cs_cpu; 558176735Sjeff int cs_limit; /* Min priority for low min load for high. */ 559176735Sjeff}; 560176735Sjeff 561176735Sjeff#define CPU_SEARCH_LOWEST 0x1 562176735Sjeff#define CPU_SEARCH_HIGHEST 0x2 563176735Sjeff#define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST) 564176735Sjeff 565194779Sjeff#define CPUSET_FOREACH(cpu, mask) \ 566194779Sjeff for ((cpu) = 0; (cpu) <= mp_maxid; (cpu)++) \ 567222813Sattilio if (CPU_ISSET(cpu, &mask)) 568176735Sjeff 569177169Sjhbstatic __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low, 570176735Sjeff struct cpu_search *high, const int match); 571176735Sjeffint cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low); 572176735Sjeffint cpu_search_highest(struct cpu_group *cg, struct cpu_search *high); 573176735Sjeffint cpu_search_both(struct cpu_group *cg, struct cpu_search *low, 574176735Sjeff struct cpu_search *high); 575176735Sjeff 576116069Sjeff/* 577176735Sjeff * This routine compares according to the match argument and should be 578176735Sjeff * reduced in actual instantiations via constant propagation and dead code 579176735Sjeff * elimination. 580176735Sjeff */ 581176735Sjeffstatic __inline int 582176735Sjeffcpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high, 583176735Sjeff const int match) 584176735Sjeff{ 585176735Sjeff struct tdq *tdq; 586176735Sjeff 587176735Sjeff tdq = TDQ_CPU(cpu); 588176735Sjeff if (match & CPU_SEARCH_LOWEST) 589194779Sjeff if (CPU_ISSET(cpu, &low->cs_mask) && 590176735Sjeff tdq->tdq_load < low->cs_load && 591176735Sjeff tdq->tdq_lowpri > low->cs_limit) { 592176735Sjeff low->cs_cpu = cpu; 593176735Sjeff low->cs_load = tdq->tdq_load; 594176735Sjeff } 595176735Sjeff if (match & CPU_SEARCH_HIGHEST) 596194779Sjeff if (CPU_ISSET(cpu, &high->cs_mask) && 597176735Sjeff tdq->tdq_load >= high->cs_limit && 598176735Sjeff tdq->tdq_load > high->cs_load && 599176735Sjeff tdq->tdq_transferable) { 600176735Sjeff high->cs_cpu = cpu; 601176735Sjeff high->cs_load = tdq->tdq_load; 602176735Sjeff } 603176735Sjeff return (tdq->tdq_load); 604176735Sjeff} 605176735Sjeff 606176735Sjeff/* 607176735Sjeff * Search the tree of cpu_groups for the lowest or highest loaded cpu 608176735Sjeff * according to the match argument. This routine actually compares the 609176735Sjeff * load on all paths through the tree and finds the least loaded cpu on 610176735Sjeff * the least loaded path, which may differ from the least loaded cpu in 611176735Sjeff * the system. This balances work among caches and busses. 612116069Sjeff * 613176735Sjeff * This inline is instantiated in three forms below using constants for the 614176735Sjeff * match argument. It is reduced to the minimum set for each case. It is 615176735Sjeff * also recursive to the depth of the tree. 616116069Sjeff */ 617177169Sjhbstatic __inline int 618176735Sjeffcpu_search(struct cpu_group *cg, struct cpu_search *low, 619176735Sjeff struct cpu_search *high, const int match) 620176735Sjeff{ 621176735Sjeff int total; 622176735Sjeff 623176735Sjeff total = 0; 624176735Sjeff if (cg->cg_children) { 625176735Sjeff struct cpu_search lgroup; 626176735Sjeff struct cpu_search hgroup; 627176735Sjeff struct cpu_group *child; 628176735Sjeff u_int lload; 629176735Sjeff int hload; 630176735Sjeff int load; 631176735Sjeff int i; 632176735Sjeff 633176735Sjeff lload = -1; 634176735Sjeff hload = -1; 635176735Sjeff for (i = 0; i < cg->cg_children; i++) { 636176735Sjeff child = &cg->cg_child[i]; 637176735Sjeff if (match & CPU_SEARCH_LOWEST) { 638176735Sjeff lgroup = *low; 639176735Sjeff lgroup.cs_load = -1; 640176735Sjeff } 641176735Sjeff if (match & CPU_SEARCH_HIGHEST) { 642176735Sjeff hgroup = *high; 643176735Sjeff lgroup.cs_load = 0; 644176735Sjeff } 645176735Sjeff switch (match) { 646176735Sjeff case CPU_SEARCH_LOWEST: 647176735Sjeff load = cpu_search_lowest(child, &lgroup); 648176735Sjeff break; 649176735Sjeff case CPU_SEARCH_HIGHEST: 650176735Sjeff load = cpu_search_highest(child, &hgroup); 651176735Sjeff break; 652176735Sjeff case CPU_SEARCH_BOTH: 653176735Sjeff load = cpu_search_both(child, &lgroup, &hgroup); 654176735Sjeff break; 655176735Sjeff } 656176735Sjeff total += load; 657176735Sjeff if (match & CPU_SEARCH_LOWEST) 658176735Sjeff if (load < lload || low->cs_cpu == -1) { 659176735Sjeff *low = lgroup; 660176735Sjeff lload = load; 661176735Sjeff } 662176735Sjeff if (match & CPU_SEARCH_HIGHEST) 663176735Sjeff if (load > hload || high->cs_cpu == -1) { 664176735Sjeff hload = load; 665176735Sjeff *high = hgroup; 666176735Sjeff } 667176735Sjeff } 668176735Sjeff } else { 669176735Sjeff int cpu; 670176735Sjeff 671194779Sjeff CPUSET_FOREACH(cpu, cg->cg_mask) 672176735Sjeff total += cpu_compare(cpu, low, high, match); 673176735Sjeff } 674176735Sjeff return (total); 675176735Sjeff} 676176735Sjeff 677176735Sjeff/* 678176735Sjeff * cpu_search instantiations must pass constants to maintain the inline 679176735Sjeff * optimization. 680176735Sjeff */ 681176735Sjeffint 682176735Sjeffcpu_search_lowest(struct cpu_group *cg, struct cpu_search *low) 683176735Sjeff{ 684176735Sjeff return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST); 685176735Sjeff} 686176735Sjeff 687176735Sjeffint 688176735Sjeffcpu_search_highest(struct cpu_group *cg, struct cpu_search *high) 689176735Sjeff{ 690176735Sjeff return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST); 691176735Sjeff} 692176735Sjeff 693176735Sjeffint 694176735Sjeffcpu_search_both(struct cpu_group *cg, struct cpu_search *low, 695176735Sjeff struct cpu_search *high) 696176735Sjeff{ 697176735Sjeff return cpu_search(cg, low, high, CPU_SEARCH_BOTH); 698176735Sjeff} 699176735Sjeff 700176735Sjeff/* 701176735Sjeff * Find the cpu with the least load via the least loaded path that has a 702176735Sjeff * lowpri greater than pri pri. A pri of -1 indicates any priority is 703176735Sjeff * acceptable. 704176735Sjeff */ 705176735Sjeffstatic inline int 706194779Sjeffsched_lowest(struct cpu_group *cg, cpuset_t mask, int pri) 707176735Sjeff{ 708176735Sjeff struct cpu_search low; 709176735Sjeff 710176735Sjeff low.cs_cpu = -1; 711176735Sjeff low.cs_load = -1; 712176735Sjeff low.cs_mask = mask; 713176735Sjeff low.cs_limit = pri; 714176735Sjeff cpu_search_lowest(cg, &low); 715176735Sjeff return low.cs_cpu; 716176735Sjeff} 717176735Sjeff 718176735Sjeff/* 719176735Sjeff * Find the cpu with the highest load via the highest loaded path. 720176735Sjeff */ 721176735Sjeffstatic inline int 722194779Sjeffsched_highest(struct cpu_group *cg, cpuset_t mask, int minload) 723176735Sjeff{ 724176735Sjeff struct cpu_search high; 725176735Sjeff 726176735Sjeff high.cs_cpu = -1; 727176735Sjeff high.cs_load = 0; 728176735Sjeff high.cs_mask = mask; 729176735Sjeff high.cs_limit = minload; 730176735Sjeff cpu_search_highest(cg, &high); 731176735Sjeff return high.cs_cpu; 732176735Sjeff} 733176735Sjeff 734176735Sjeff/* 735176735Sjeff * Simultaneously find the highest and lowest loaded cpu reachable via 736176735Sjeff * cg. 737176735Sjeff */ 738176735Sjeffstatic inline void 739194779Sjeffsched_both(struct cpu_group *cg, cpuset_t mask, int *lowcpu, int *highcpu) 740176735Sjeff{ 741176735Sjeff struct cpu_search high; 742176735Sjeff struct cpu_search low; 743176735Sjeff 744176735Sjeff low.cs_cpu = -1; 745176735Sjeff low.cs_limit = -1; 746176735Sjeff low.cs_load = -1; 747176735Sjeff low.cs_mask = mask; 748176735Sjeff high.cs_load = 0; 749176735Sjeff high.cs_cpu = -1; 750176735Sjeff high.cs_limit = -1; 751176735Sjeff high.cs_mask = mask; 752176735Sjeff cpu_search_both(cg, &low, &high); 753176735Sjeff *lowcpu = low.cs_cpu; 754176735Sjeff *highcpu = high.cs_cpu; 755176735Sjeff return; 756176735Sjeff} 757176735Sjeff 758121790Sjeffstatic void 759176735Sjeffsched_balance_group(struct cpu_group *cg) 760116069Sjeff{ 761194779Sjeff cpuset_t mask; 762176735Sjeff int high; 763176735Sjeff int low; 764123487Sjeff int i; 765123487Sjeff 766194779Sjeff CPU_FILL(&mask); 767176735Sjeff for (;;) { 768176735Sjeff sched_both(cg, mask, &low, &high); 769176735Sjeff if (low == high || low == -1 || high == -1) 770176735Sjeff break; 771176735Sjeff if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low))) 772176735Sjeff break; 773123487Sjeff /* 774176735Sjeff * If we failed to move any threads determine which cpu 775176735Sjeff * to kick out of the set and try again. 776176735Sjeff */ 777176735Sjeff if (TDQ_CPU(high)->tdq_transferable == 0) 778194779Sjeff CPU_CLR(high, &mask); 779176735Sjeff else 780194779Sjeff CPU_CLR(low, &mask); 781123487Sjeff } 782176735Sjeff 783176735Sjeff for (i = 0; i < cg->cg_children; i++) 784176735Sjeff sched_balance_group(&cg->cg_child[i]); 785123487Sjeff} 786123487Sjeff 787123487Sjeffstatic void 788201148Sedsched_balance(void) 789123487Sjeff{ 790172409Sjeff struct tdq *tdq; 791123487Sjeff 792172409Sjeff /* 793172409Sjeff * Select a random time between .5 * balance_interval and 794172409Sjeff * 1.5 * balance_interval. 795172409Sjeff */ 796176735Sjeff balance_ticks = max(balance_interval / 2, 1); 797176735Sjeff balance_ticks += random() % balance_interval; 798171482Sjeff if (smp_started == 0 || rebalance == 0) 799171482Sjeff return; 800172409Sjeff tdq = TDQ_SELF(); 801172409Sjeff TDQ_UNLOCK(tdq); 802176735Sjeff sched_balance_group(cpu_top); 803172409Sjeff TDQ_LOCK(tdq); 804123487Sjeff} 805123487Sjeff 806171482Sjeff/* 807171482Sjeff * Lock two thread queues using their address to maintain lock order. 808171482Sjeff */ 809123487Sjeffstatic void 810171482Sjefftdq_lock_pair(struct tdq *one, struct tdq *two) 811171482Sjeff{ 812171482Sjeff if (one < two) { 813171482Sjeff TDQ_LOCK(one); 814171482Sjeff TDQ_LOCK_FLAGS(two, MTX_DUPOK); 815171482Sjeff } else { 816171482Sjeff TDQ_LOCK(two); 817171482Sjeff TDQ_LOCK_FLAGS(one, MTX_DUPOK); 818171482Sjeff } 819171482Sjeff} 820171482Sjeff 821171482Sjeff/* 822172409Sjeff * Unlock two thread queues. Order is not important here. 823172409Sjeff */ 824172409Sjeffstatic void 825172409Sjefftdq_unlock_pair(struct tdq *one, struct tdq *two) 826172409Sjeff{ 827172409Sjeff TDQ_UNLOCK(one); 828172409Sjeff TDQ_UNLOCK(two); 829172409Sjeff} 830172409Sjeff 831172409Sjeff/* 832171482Sjeff * Transfer load between two imbalanced thread queues. 833171482Sjeff */ 834176735Sjeffstatic int 835164936Sjuliansched_balance_pair(struct tdq *high, struct tdq *low) 836123487Sjeff{ 837123433Sjeff int transferable; 838116069Sjeff int high_load; 839116069Sjeff int low_load; 840176735Sjeff int moved; 841116069Sjeff int move; 842226057Smarius int cpu; 843116069Sjeff int diff; 844116069Sjeff int i; 845116069Sjeff 846171482Sjeff tdq_lock_pair(high, low); 847176735Sjeff transferable = high->tdq_transferable; 848176735Sjeff high_load = high->tdq_load; 849176735Sjeff low_load = low->tdq_load; 850176735Sjeff moved = 0; 851116069Sjeff /* 852122744Sjeff * Determine what the imbalance is and then adjust that to how many 853165620Sjeff * threads we actually have to give up (transferable). 854122744Sjeff */ 855171482Sjeff if (transferable != 0) { 856171482Sjeff diff = high_load - low_load; 857171482Sjeff move = diff / 2; 858171482Sjeff if (diff & 0x1) 859171482Sjeff move++; 860171482Sjeff move = min(move, transferable); 861171482Sjeff for (i = 0; i < move; i++) 862176735Sjeff moved += tdq_move(high, low); 863172293Sjeff /* 864226057Smarius * In case the target isn't the current cpu IPI it to force a 865226057Smarius * reschedule with the new workload. 866172293Sjeff */ 867226057Smarius cpu = TDQ_ID(low); 868226057Smarius sched_pin(); 869226057Smarius if (cpu != PCPU_GET(cpuid)) 870226057Smarius ipi_cpu(cpu, IPI_PREEMPT); 871226057Smarius sched_unpin(); 872171482Sjeff } 873172409Sjeff tdq_unlock_pair(high, low); 874176735Sjeff return (moved); 875116069Sjeff} 876116069Sjeff 877171482Sjeff/* 878171482Sjeff * Move a thread from one thread queue to another. 879171482Sjeff */ 880176735Sjeffstatic int 881171482Sjefftdq_move(struct tdq *from, struct tdq *to) 882116069Sjeff{ 883171482Sjeff struct td_sched *ts; 884171482Sjeff struct thread *td; 885164936Sjulian struct tdq *tdq; 886171482Sjeff int cpu; 887116069Sjeff 888172409Sjeff TDQ_LOCK_ASSERT(from, MA_OWNED); 889172409Sjeff TDQ_LOCK_ASSERT(to, MA_OWNED); 890172409Sjeff 891164936Sjulian tdq = from; 892171482Sjeff cpu = TDQ_ID(to); 893177435Sjeff td = tdq_steal(tdq, cpu); 894177435Sjeff if (td == NULL) 895176735Sjeff return (0); 896177435Sjeff ts = td->td_sched; 897171482Sjeff /* 898171482Sjeff * Although the run queue is locked the thread may be blocked. Lock 899172409Sjeff * it to clear this and acquire the run-queue lock. 900171482Sjeff */ 901171482Sjeff thread_lock(td); 902172409Sjeff /* Drop recursive lock on from acquired via thread_lock(). */ 903171482Sjeff TDQ_UNLOCK(from); 904171482Sjeff sched_rem(td); 905166108Sjeff ts->ts_cpu = cpu; 906171482Sjeff td->td_lock = TDQ_LOCKPTR(to); 907171482Sjeff tdq_add(to, td, SRQ_YIELDING); 908176735Sjeff return (1); 909116069Sjeff} 910110267Sjeff 911171482Sjeff/* 912171482Sjeff * This tdq has idled. Try to steal a thread from another cpu and switch 913171482Sjeff * to it. 914171482Sjeff */ 915123433Sjeffstatic int 916164936Sjuliantdq_idled(struct tdq *tdq) 917121790Sjeff{ 918176735Sjeff struct cpu_group *cg; 919164936Sjulian struct tdq *steal; 920194779Sjeff cpuset_t mask; 921176735Sjeff int thresh; 922171482Sjeff int cpu; 923123433Sjeff 924172484Sjeff if (smp_started == 0 || steal_idle == 0) 925172484Sjeff return (1); 926194779Sjeff CPU_FILL(&mask); 927194779Sjeff CPU_CLR(PCPU_GET(cpuid), &mask); 928176735Sjeff /* We don't want to be preempted while we're iterating. */ 929171482Sjeff spinlock_enter(); 930176735Sjeff for (cg = tdq->tdq_cg; cg != NULL; ) { 931191643Sjeff if ((cg->cg_flags & CG_FLAG_THREAD) == 0) 932176735Sjeff thresh = steal_thresh; 933176735Sjeff else 934176735Sjeff thresh = 1; 935176735Sjeff cpu = sched_highest(cg, mask, thresh); 936176735Sjeff if (cpu == -1) { 937176735Sjeff cg = cg->cg_parent; 938176735Sjeff continue; 939166108Sjeff } 940176735Sjeff steal = TDQ_CPU(cpu); 941194779Sjeff CPU_CLR(cpu, &mask); 942176735Sjeff tdq_lock_pair(tdq, steal); 943176735Sjeff if (steal->tdq_load < thresh || steal->tdq_transferable == 0) { 944176735Sjeff tdq_unlock_pair(tdq, steal); 945176735Sjeff continue; 946171482Sjeff } 947176735Sjeff /* 948176735Sjeff * If a thread was added while interrupts were disabled don't 949176735Sjeff * steal one here. If we fail to acquire one due to affinity 950176735Sjeff * restrictions loop again with this cpu removed from the 951176735Sjeff * set. 952176735Sjeff */ 953176735Sjeff if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) { 954176735Sjeff tdq_unlock_pair(tdq, steal); 955176735Sjeff continue; 956176735Sjeff } 957176735Sjeff spinlock_exit(); 958176735Sjeff TDQ_UNLOCK(steal); 959178272Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 960176735Sjeff thread_unlock(curthread); 961176735Sjeff 962176735Sjeff return (0); 963123433Sjeff } 964171482Sjeff spinlock_exit(); 965123433Sjeff return (1); 966121790Sjeff} 967121790Sjeff 968171482Sjeff/* 969171482Sjeff * Notify a remote cpu of new work. Sends an IPI if criteria are met. 970171482Sjeff */ 971121790Sjeffstatic void 972177435Sjefftdq_notify(struct tdq *tdq, struct thread *td) 973121790Sjeff{ 974185047Sjhb struct thread *ctd; 975166247Sjeff int pri; 976166108Sjeff int cpu; 977121790Sjeff 978177005Sjeff if (tdq->tdq_ipipending) 979177005Sjeff return; 980177435Sjeff cpu = td->td_sched->ts_cpu; 981177435Sjeff pri = td->td_priority; 982185047Sjhb ctd = pcpu_find(cpu)->pc_curthread; 983185047Sjhb if (!sched_shouldpreempt(pri, ctd->td_priority, 1)) 984166137Sjeff return; 985185047Sjhb if (TD_IS_IDLETHREAD(ctd)) { 986178277Sjeff /* 987178471Sjeff * If the MD code has an idle wakeup routine try that before 988178471Sjeff * falling back to IPI. 989178471Sjeff */ 990212416Smav if (!tdq->tdq_cpu_idle || cpu_idle_wakeup(cpu)) 991178471Sjeff return; 992178277Sjeff } 993177005Sjeff tdq->tdq_ipipending = 1; 994210939Sjhb ipi_cpu(cpu, IPI_PREEMPT); 995121790Sjeff} 996121790Sjeff 997171482Sjeff/* 998171482Sjeff * Steals load from a timeshare queue. Honors the rotating queue head 999171482Sjeff * index. 1000171482Sjeff */ 1001177435Sjeffstatic struct thread * 1002176735Sjeffrunq_steal_from(struct runq *rq, int cpu, u_char start) 1003171482Sjeff{ 1004171482Sjeff struct rqbits *rqb; 1005171482Sjeff struct rqhead *rqh; 1006177435Sjeff struct thread *td; 1007171482Sjeff int first; 1008171482Sjeff int bit; 1009171482Sjeff int pri; 1010171482Sjeff int i; 1011171482Sjeff 1012171482Sjeff rqb = &rq->rq_status; 1013171482Sjeff bit = start & (RQB_BPW -1); 1014171482Sjeff pri = 0; 1015171482Sjeff first = 0; 1016171482Sjeffagain: 1017171482Sjeff for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 1018171482Sjeff if (rqb->rqb_bits[i] == 0) 1019171482Sjeff continue; 1020171482Sjeff if (bit != 0) { 1021171482Sjeff for (pri = bit; pri < RQB_BPW; pri++) 1022171482Sjeff if (rqb->rqb_bits[i] & (1ul << pri)) 1023171482Sjeff break; 1024171482Sjeff if (pri >= RQB_BPW) 1025171482Sjeff continue; 1026171482Sjeff } else 1027171482Sjeff pri = RQB_FFS(rqb->rqb_bits[i]); 1028171482Sjeff pri += (i << RQB_L2BPW); 1029171482Sjeff rqh = &rq->rq_queues[pri]; 1030177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 1031177435Sjeff if (first && THREAD_CAN_MIGRATE(td) && 1032177435Sjeff THREAD_CAN_SCHED(td, cpu)) 1033177435Sjeff return (td); 1034171482Sjeff first = 1; 1035171482Sjeff } 1036171482Sjeff } 1037171482Sjeff if (start != 0) { 1038171482Sjeff start = 0; 1039171482Sjeff goto again; 1040171482Sjeff } 1041171482Sjeff 1042171482Sjeff return (NULL); 1043171482Sjeff} 1044171482Sjeff 1045171482Sjeff/* 1046171482Sjeff * Steals load from a standard linear queue. 1047171482Sjeff */ 1048177435Sjeffstatic struct thread * 1049176735Sjeffrunq_steal(struct runq *rq, int cpu) 1050121790Sjeff{ 1051121790Sjeff struct rqhead *rqh; 1052121790Sjeff struct rqbits *rqb; 1053177435Sjeff struct thread *td; 1054121790Sjeff int word; 1055121790Sjeff int bit; 1056121790Sjeff 1057121790Sjeff rqb = &rq->rq_status; 1058121790Sjeff for (word = 0; word < RQB_LEN; word++) { 1059121790Sjeff if (rqb->rqb_bits[word] == 0) 1060121790Sjeff continue; 1061121790Sjeff for (bit = 0; bit < RQB_BPW; bit++) { 1062123231Speter if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 1063121790Sjeff continue; 1064121790Sjeff rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 1065177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) 1066177435Sjeff if (THREAD_CAN_MIGRATE(td) && 1067177435Sjeff THREAD_CAN_SCHED(td, cpu)) 1068177435Sjeff return (td); 1069121790Sjeff } 1070121790Sjeff } 1071121790Sjeff return (NULL); 1072121790Sjeff} 1073121790Sjeff 1074171482Sjeff/* 1075171482Sjeff * Attempt to steal a thread in priority order from a thread queue. 1076171482Sjeff */ 1077177435Sjeffstatic struct thread * 1078176735Sjefftdq_steal(struct tdq *tdq, int cpu) 1079121790Sjeff{ 1080177435Sjeff struct thread *td; 1081121790Sjeff 1082171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 1083177435Sjeff if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 1084177435Sjeff return (td); 1085177435Sjeff if ((td = runq_steal_from(&tdq->tdq_timeshare, 1086177435Sjeff cpu, tdq->tdq_ridx)) != NULL) 1087177435Sjeff return (td); 1088176735Sjeff return (runq_steal(&tdq->tdq_idle, cpu)); 1089121790Sjeff} 1090123433Sjeff 1091171482Sjeff/* 1092171482Sjeff * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 1093172409Sjeff * current lock and returns with the assigned queue locked. 1094171482Sjeff */ 1095171482Sjeffstatic inline struct tdq * 1096177435Sjeffsched_setcpu(struct thread *td, int cpu, int flags) 1097123433Sjeff{ 1098177435Sjeff 1099171482Sjeff struct tdq *tdq; 1100123433Sjeff 1101177435Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1102171482Sjeff tdq = TDQ_CPU(cpu); 1103177435Sjeff td->td_sched->ts_cpu = cpu; 1104177435Sjeff /* 1105177435Sjeff * If the lock matches just return the queue. 1106177435Sjeff */ 1107171482Sjeff if (td->td_lock == TDQ_LOCKPTR(tdq)) 1108171482Sjeff return (tdq); 1109171482Sjeff#ifdef notyet 1110123433Sjeff /* 1111172293Sjeff * If the thread isn't running its lockptr is a 1112171482Sjeff * turnstile or a sleepqueue. We can just lock_set without 1113171482Sjeff * blocking. 1114123685Sjeff */ 1115171482Sjeff if (TD_CAN_RUN(td)) { 1116171482Sjeff TDQ_LOCK(tdq); 1117171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 1118171482Sjeff return (tdq); 1119171482Sjeff } 1120171482Sjeff#endif 1121166108Sjeff /* 1122171482Sjeff * The hard case, migration, we need to block the thread first to 1123171482Sjeff * prevent order reversals with other cpus locks. 1124166108Sjeff */ 1125202889Sattilio spinlock_enter(); 1126171482Sjeff thread_lock_block(td); 1127171482Sjeff TDQ_LOCK(tdq); 1128171713Sjeff thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1129202889Sattilio spinlock_exit(); 1130171482Sjeff return (tdq); 1131166108Sjeff} 1132166108Sjeff 1133178272SjeffSCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 1134178272SjeffSCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 1135178272SjeffSCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 1136178272SjeffSCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 1137178272SjeffSCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 1138178272SjeffSCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 1139178272Sjeff 1140166108Sjeffstatic int 1141177435Sjeffsched_pickcpu(struct thread *td, int flags) 1142171482Sjeff{ 1143176735Sjeff struct cpu_group *cg; 1144177435Sjeff struct td_sched *ts; 1145171482Sjeff struct tdq *tdq; 1146194779Sjeff cpuset_t mask; 1147166108Sjeff int self; 1148166108Sjeff int pri; 1149166108Sjeff int cpu; 1150166108Sjeff 1151176735Sjeff self = PCPU_GET(cpuid); 1152177435Sjeff ts = td->td_sched; 1153166108Sjeff if (smp_started == 0) 1154166108Sjeff return (self); 1155171506Sjeff /* 1156171506Sjeff * Don't migrate a running thread from sched_switch(). 1157171506Sjeff */ 1158176735Sjeff if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 1159176735Sjeff return (ts->ts_cpu); 1160166108Sjeff /* 1161176735Sjeff * Prefer to run interrupt threads on the processors that generate 1162176735Sjeff * the interrupt. 1163166108Sjeff */ 1164176735Sjeff if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 1165178272Sjeff curthread->td_intr_nesting_level && ts->ts_cpu != self) { 1166178272Sjeff SCHED_STAT_INC(pickcpu_intrbind); 1167176735Sjeff ts->ts_cpu = self; 1168178272Sjeff } 1169166108Sjeff /* 1170176735Sjeff * If the thread can run on the last cpu and the affinity has not 1171176735Sjeff * expired or it is idle run it there. 1172166108Sjeff */ 1173176735Sjeff pri = td->td_priority; 1174176735Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1175176735Sjeff if (THREAD_CAN_SCHED(td, ts->ts_cpu)) { 1176178272Sjeff if (tdq->tdq_lowpri > PRI_MIN_IDLE) { 1177178272Sjeff SCHED_STAT_INC(pickcpu_idle_affinity); 1178176735Sjeff return (ts->ts_cpu); 1179178272Sjeff } 1180178272Sjeff if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri) { 1181178272Sjeff SCHED_STAT_INC(pickcpu_affinity); 1182176735Sjeff return (ts->ts_cpu); 1183178272Sjeff } 1184139334Sjeff } 1185123433Sjeff /* 1186176735Sjeff * Search for the highest level in the tree that still has affinity. 1187123433Sjeff */ 1188176735Sjeff cg = NULL; 1189176735Sjeff for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent) 1190176735Sjeff if (SCHED_AFFINITY(ts, cg->cg_level)) 1191176735Sjeff break; 1192176735Sjeff cpu = -1; 1193194779Sjeff mask = td->td_cpuset->cs_mask; 1194176735Sjeff if (cg) 1195176735Sjeff cpu = sched_lowest(cg, mask, pri); 1196176735Sjeff if (cpu == -1) 1197176735Sjeff cpu = sched_lowest(cpu_top, mask, -1); 1198171506Sjeff /* 1199176735Sjeff * Compare the lowest loaded cpu to current cpu. 1200171506Sjeff */ 1201177005Sjeff if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri && 1202178272Sjeff TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) { 1203178272Sjeff SCHED_STAT_INC(pickcpu_local); 1204177005Sjeff cpu = self; 1205178272Sjeff } else 1206178272Sjeff SCHED_STAT_INC(pickcpu_lowest); 1207178272Sjeff if (cpu != ts->ts_cpu) 1208178272Sjeff SCHED_STAT_INC(pickcpu_migration); 1209177005Sjeff KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu.")); 1210171482Sjeff return (cpu); 1211123433Sjeff} 1212176735Sjeff#endif 1213123433Sjeff 1214117326Sjeff/* 1215121790Sjeff * Pick the highest priority task we have and return it. 1216117326Sjeff */ 1217177435Sjeffstatic struct thread * 1218164936Sjuliantdq_choose(struct tdq *tdq) 1219110267Sjeff{ 1220177435Sjeff struct thread *td; 1221110267Sjeff 1222171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 1223177435Sjeff td = runq_choose(&tdq->tdq_realtime); 1224177435Sjeff if (td != NULL) 1225177435Sjeff return (td); 1226177435Sjeff td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 1227177435Sjeff if (td != NULL) { 1228217351Sjhb KASSERT(td->td_priority >= PRI_MIN_BATCH, 1229165762Sjeff ("tdq_choose: Invalid priority on timeshare queue %d", 1230177435Sjeff td->td_priority)); 1231177435Sjeff return (td); 1232165762Sjeff } 1233177435Sjeff td = runq_choose(&tdq->tdq_idle); 1234177435Sjeff if (td != NULL) { 1235177435Sjeff KASSERT(td->td_priority >= PRI_MIN_IDLE, 1236165762Sjeff ("tdq_choose: Invalid priority on idle queue %d", 1237177435Sjeff td->td_priority)); 1238177435Sjeff return (td); 1239165762Sjeff } 1240165762Sjeff 1241165762Sjeff return (NULL); 1242110267Sjeff} 1243110267Sjeff 1244171482Sjeff/* 1245171482Sjeff * Initialize a thread queue. 1246171482Sjeff */ 1247109864Sjeffstatic void 1248164936Sjuliantdq_setup(struct tdq *tdq) 1249110028Sjeff{ 1250171482Sjeff 1251171713Sjeff if (bootverbose) 1252171713Sjeff printf("ULE: setup cpu %d\n", TDQ_ID(tdq)); 1253165762Sjeff runq_init(&tdq->tdq_realtime); 1254165762Sjeff runq_init(&tdq->tdq_timeshare); 1255165620Sjeff runq_init(&tdq->tdq_idle); 1256176735Sjeff snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 1257176735Sjeff "sched lock %d", (int)TDQ_ID(tdq)); 1258176735Sjeff mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", 1259176735Sjeff MTX_SPIN | MTX_RECURSE); 1260187357Sjeff#ifdef KTR 1261187357Sjeff snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 1262187357Sjeff "CPU %d load", (int)TDQ_ID(tdq)); 1263187357Sjeff#endif 1264110028Sjeff} 1265110028Sjeff 1266171713Sjeff#ifdef SMP 1267110028Sjeffstatic void 1268171713Sjeffsched_setup_smp(void) 1269171713Sjeff{ 1270171713Sjeff struct tdq *tdq; 1271171713Sjeff int i; 1272171713Sjeff 1273176735Sjeff cpu_top = smp_topo(); 1274209059Sjhb CPU_FOREACH(i) { 1275176735Sjeff tdq = TDQ_CPU(i); 1276171713Sjeff tdq_setup(tdq); 1277176735Sjeff tdq->tdq_cg = smp_topo_find(cpu_top, i); 1278176735Sjeff if (tdq->tdq_cg == NULL) 1279176735Sjeff panic("Can't find cpu group for %d\n", i); 1280123433Sjeff } 1281176735Sjeff balance_tdq = TDQ_SELF(); 1282176735Sjeff sched_balance(); 1283171713Sjeff} 1284171713Sjeff#endif 1285171713Sjeff 1286171713Sjeff/* 1287171713Sjeff * Setup the thread queues and initialize the topology based on MD 1288171713Sjeff * information. 1289171713Sjeff */ 1290171713Sjeffstatic void 1291171713Sjeffsched_setup(void *dummy) 1292171713Sjeff{ 1293171713Sjeff struct tdq *tdq; 1294171713Sjeff 1295171713Sjeff tdq = TDQ_SELF(); 1296171713Sjeff#ifdef SMP 1297176734Sjeff sched_setup_smp(); 1298117237Sjeff#else 1299171713Sjeff tdq_setup(tdq); 1300116069Sjeff#endif 1301171482Sjeff /* 1302171482Sjeff * To avoid divide-by-zero, we set realstathz a dummy value 1303171482Sjeff * in case which sched_clock() called before sched_initticks(). 1304171482Sjeff */ 1305171482Sjeff realstathz = hz; 1306171482Sjeff sched_slice = (realstathz/10); /* ~100ms */ 1307171482Sjeff tickincr = 1 << SCHED_TICK_SHIFT; 1308171482Sjeff 1309171482Sjeff /* Add thread0's load since it's running. */ 1310171482Sjeff TDQ_LOCK(tdq); 1311171713Sjeff thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1312177435Sjeff tdq_load_add(tdq, &thread0); 1313176735Sjeff tdq->tdq_lowpri = thread0.td_priority; 1314171482Sjeff TDQ_UNLOCK(tdq); 1315109864Sjeff} 1316109864Sjeff 1317171482Sjeff/* 1318171482Sjeff * This routine determines the tickincr after stathz and hz are setup. 1319171482Sjeff */ 1320153533Sdavidxu/* ARGSUSED */ 1321153533Sdavidxustatic void 1322153533Sdavidxusched_initticks(void *dummy) 1323153533Sdavidxu{ 1324171482Sjeff int incr; 1325171482Sjeff 1326153533Sdavidxu realstathz = stathz ? stathz : hz; 1327166229Sjeff sched_slice = (realstathz/10); /* ~100ms */ 1328153533Sdavidxu 1329153533Sdavidxu /* 1330165762Sjeff * tickincr is shifted out by 10 to avoid rounding errors due to 1331165766Sjeff * hz not being evenly divisible by stathz on all platforms. 1332153533Sdavidxu */ 1333171482Sjeff incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1334165762Sjeff /* 1335165762Sjeff * This does not work for values of stathz that are more than 1336165762Sjeff * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1337165762Sjeff */ 1338171482Sjeff if (incr == 0) 1339171482Sjeff incr = 1; 1340171482Sjeff tickincr = incr; 1341166108Sjeff#ifdef SMP 1342171899Sjeff /* 1343172409Sjeff * Set the default balance interval now that we know 1344172409Sjeff * what realstathz is. 1345172409Sjeff */ 1346172409Sjeff balance_interval = realstathz; 1347172409Sjeff /* 1348189787Sjeff * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4. 1349189787Sjeff * This prevents excess thrashing on large machines and excess idle 1350189787Sjeff * on smaller machines. 1351171899Sjeff */ 1352189787Sjeff steal_thresh = min(fls(mp_ncpus) - 1, 3); 1353166108Sjeff affinity = SCHED_AFFINITY_DEFAULT; 1354166108Sjeff#endif 1355153533Sdavidxu} 1356153533Sdavidxu 1357153533Sdavidxu 1358109864Sjeff/* 1359171482Sjeff * This is the core of the interactivity algorithm. Determines a score based 1360171482Sjeff * on past behavior. It is the ratio of sleep time to run time scaled to 1361171482Sjeff * a [0, 100] integer. This is the voluntary sleep time of a process, which 1362171482Sjeff * differs from the cpu usage because it does not account for time spent 1363171482Sjeff * waiting on a run-queue. Would be prettier if we had floating point. 1364171482Sjeff */ 1365171482Sjeffstatic int 1366171482Sjeffsched_interact_score(struct thread *td) 1367171482Sjeff{ 1368171482Sjeff struct td_sched *ts; 1369171482Sjeff int div; 1370171482Sjeff 1371171482Sjeff ts = td->td_sched; 1372171482Sjeff /* 1373171482Sjeff * The score is only needed if this is likely to be an interactive 1374171482Sjeff * task. Don't go through the expense of computing it if there's 1375171482Sjeff * no chance. 1376171482Sjeff */ 1377171482Sjeff if (sched_interact <= SCHED_INTERACT_HALF && 1378171482Sjeff ts->ts_runtime >= ts->ts_slptime) 1379171482Sjeff return (SCHED_INTERACT_HALF); 1380171482Sjeff 1381171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1382171482Sjeff div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1383171482Sjeff return (SCHED_INTERACT_HALF + 1384171482Sjeff (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1385171482Sjeff } 1386171482Sjeff if (ts->ts_slptime > ts->ts_runtime) { 1387171482Sjeff div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1388171482Sjeff return (ts->ts_runtime / div); 1389171482Sjeff } 1390171482Sjeff /* runtime == slptime */ 1391171482Sjeff if (ts->ts_runtime) 1392171482Sjeff return (SCHED_INTERACT_HALF); 1393171482Sjeff 1394171482Sjeff /* 1395171482Sjeff * This can happen if slptime and runtime are 0. 1396171482Sjeff */ 1397171482Sjeff return (0); 1398171482Sjeff 1399171482Sjeff} 1400171482Sjeff 1401171482Sjeff/* 1402109864Sjeff * Scale the scheduling priority according to the "interactivity" of this 1403109864Sjeff * process. 1404109864Sjeff */ 1405113357Sjeffstatic void 1406163709Sjbsched_priority(struct thread *td) 1407109864Sjeff{ 1408165762Sjeff int score; 1409109864Sjeff int pri; 1410109864Sjeff 1411217291Sjhb if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 1412113357Sjeff return; 1413112966Sjeff /* 1414165762Sjeff * If the score is interactive we place the thread in the realtime 1415165762Sjeff * queue with a priority that is less than kernel and interrupt 1416165762Sjeff * priorities. These threads are not subject to nice restrictions. 1417112966Sjeff * 1418171482Sjeff * Scores greater than this are placed on the normal timeshare queue 1419165762Sjeff * where the priority is partially decided by the most recent cpu 1420165762Sjeff * utilization and the rest is decided by nice value. 1421172293Sjeff * 1422172293Sjeff * The nice value of the process has a linear effect on the calculated 1423172293Sjeff * score. Negative nice values make it easier for a thread to be 1424172293Sjeff * considered interactive. 1425112966Sjeff */ 1426198126Sjhb score = imax(0, sched_interact_score(td) + td->td_proc->p_nice); 1427165762Sjeff if (score < sched_interact) { 1428217351Sjhb pri = PRI_MIN_INTERACT; 1429217351Sjhb pri += ((PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) / 1430217237Sjhb sched_interact) * score; 1431217351Sjhb KASSERT(pri >= PRI_MIN_INTERACT && pri <= PRI_MAX_INTERACT, 1432166208Sjeff ("sched_priority: invalid interactive priority %d score %d", 1433166208Sjeff pri, score)); 1434165762Sjeff } else { 1435165762Sjeff pri = SCHED_PRI_MIN; 1436165762Sjeff if (td->td_sched->ts_ticks) 1437228960Sjhb pri += min(SCHED_PRI_TICKS(td->td_sched), 1438228960Sjhb SCHED_PRI_RANGE); 1439165762Sjeff pri += SCHED_PRI_NICE(td->td_proc->p_nice); 1440217351Sjhb KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH, 1441171482Sjeff ("sched_priority: invalid priority %d: nice %d, " 1442171482Sjeff "ticks %d ftick %d ltick %d tick pri %d", 1443171482Sjeff pri, td->td_proc->p_nice, td->td_sched->ts_ticks, 1444171482Sjeff td->td_sched->ts_ftick, td->td_sched->ts_ltick, 1445171482Sjeff SCHED_PRI_TICKS(td->td_sched))); 1446165762Sjeff } 1447165762Sjeff sched_user_prio(td, pri); 1448112966Sjeff 1449112966Sjeff return; 1450109864Sjeff} 1451109864Sjeff 1452121868Sjeff/* 1453121868Sjeff * This routine enforces a maximum limit on the amount of scheduling history 1454171482Sjeff * kept. It is called after either the slptime or runtime is adjusted. This 1455171482Sjeff * function is ugly due to integer math. 1456121868Sjeff */ 1457116463Sjeffstatic void 1458163709Sjbsched_interact_update(struct thread *td) 1459116463Sjeff{ 1460165819Sjeff struct td_sched *ts; 1461166208Sjeff u_int sum; 1462121605Sjeff 1463165819Sjeff ts = td->td_sched; 1464171482Sjeff sum = ts->ts_runtime + ts->ts_slptime; 1465121868Sjeff if (sum < SCHED_SLP_RUN_MAX) 1466121868Sjeff return; 1467121868Sjeff /* 1468165819Sjeff * This only happens from two places: 1469165819Sjeff * 1) We have added an unusual amount of run time from fork_exit. 1470165819Sjeff * 2) We have added an unusual amount of sleep time from sched_sleep(). 1471165819Sjeff */ 1472165819Sjeff if (sum > SCHED_SLP_RUN_MAX * 2) { 1473171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1474171482Sjeff ts->ts_runtime = SCHED_SLP_RUN_MAX; 1475171482Sjeff ts->ts_slptime = 1; 1476165819Sjeff } else { 1477171482Sjeff ts->ts_slptime = SCHED_SLP_RUN_MAX; 1478171482Sjeff ts->ts_runtime = 1; 1479165819Sjeff } 1480165819Sjeff return; 1481165819Sjeff } 1482165819Sjeff /* 1483121868Sjeff * If we have exceeded by more than 1/5th then the algorithm below 1484121868Sjeff * will not bring us back into range. Dividing by two here forces 1485133427Sjeff * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1486121868Sjeff */ 1487127850Sjeff if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1488171482Sjeff ts->ts_runtime /= 2; 1489171482Sjeff ts->ts_slptime /= 2; 1490121868Sjeff return; 1491116463Sjeff } 1492171482Sjeff ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1493171482Sjeff ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1494116463Sjeff} 1495116463Sjeff 1496171482Sjeff/* 1497171482Sjeff * Scale back the interactivity history when a child thread is created. The 1498171482Sjeff * history is inherited from the parent but the thread may behave totally 1499171482Sjeff * differently. For example, a shell spawning a compiler process. We want 1500171482Sjeff * to learn that the compiler is behaving badly very quickly. 1501171482Sjeff */ 1502121868Sjeffstatic void 1503163709Sjbsched_interact_fork(struct thread *td) 1504121868Sjeff{ 1505121868Sjeff int ratio; 1506121868Sjeff int sum; 1507121868Sjeff 1508171482Sjeff sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime; 1509121868Sjeff if (sum > SCHED_SLP_RUN_FORK) { 1510121868Sjeff ratio = sum / SCHED_SLP_RUN_FORK; 1511171482Sjeff td->td_sched->ts_runtime /= ratio; 1512171482Sjeff td->td_sched->ts_slptime /= ratio; 1513121868Sjeff } 1514121868Sjeff} 1515121868Sjeff 1516113357Sjeff/* 1517171482Sjeff * Called from proc0_init() to setup the scheduler fields. 1518134791Sjulian */ 1519134791Sjulianvoid 1520134791Sjulianschedinit(void) 1521134791Sjulian{ 1522165762Sjeff 1523134791Sjulian /* 1524134791Sjulian * Set up the scheduler specific parts of proc0. 1525134791Sjulian */ 1526136167Sjulian proc0.p_sched = NULL; /* XXX */ 1527164936Sjulian thread0.td_sched = &td_sched0; 1528165762Sjeff td_sched0.ts_ltick = ticks; 1529165796Sjeff td_sched0.ts_ftick = ticks; 1530177009Sjeff td_sched0.ts_slice = sched_slice; 1531134791Sjulian} 1532134791Sjulian 1533134791Sjulian/* 1534113357Sjeff * This is only somewhat accurate since given many processes of the same 1535113357Sjeff * priority they will switch when their slices run out, which will be 1536165762Sjeff * at most sched_slice stathz ticks. 1537113357Sjeff */ 1538109864Sjeffint 1539109864Sjeffsched_rr_interval(void) 1540109864Sjeff{ 1541165762Sjeff 1542165762Sjeff /* Convert sched_slice to hz */ 1543165762Sjeff return (hz/(realstathz/sched_slice)); 1544109864Sjeff} 1545109864Sjeff 1546171482Sjeff/* 1547171482Sjeff * Update the percent cpu tracking information when it is requested or 1548171482Sjeff * the total history exceeds the maximum. We keep a sliding history of 1549171482Sjeff * tick counts that slowly decays. This is less precise than the 4BSD 1550171482Sjeff * mechanism since it happens with less regular and frequent events. 1551171482Sjeff */ 1552121790Sjeffstatic void 1553164936Sjuliansched_pctcpu_update(struct td_sched *ts) 1554109864Sjeff{ 1555165762Sjeff 1556165762Sjeff if (ts->ts_ticks == 0) 1557165762Sjeff return; 1558165796Sjeff if (ticks - (hz / 10) < ts->ts_ltick && 1559165796Sjeff SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX) 1560165796Sjeff return; 1561109864Sjeff /* 1562109864Sjeff * Adjust counters and watermark for pctcpu calc. 1563116365Sjeff */ 1564165762Sjeff if (ts->ts_ltick > ticks - SCHED_TICK_TARG) 1565164936Sjulian ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) * 1566165762Sjeff SCHED_TICK_TARG; 1567165762Sjeff else 1568164936Sjulian ts->ts_ticks = 0; 1569164936Sjulian ts->ts_ltick = ticks; 1570165762Sjeff ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG; 1571109864Sjeff} 1572109864Sjeff 1573171482Sjeff/* 1574171482Sjeff * Adjust the priority of a thread. Move it to the appropriate run-queue 1575171482Sjeff * if necessary. This is the back-end for several priority related 1576171482Sjeff * functions. 1577171482Sjeff */ 1578165762Sjeffstatic void 1579139453Sjhbsched_thread_priority(struct thread *td, u_char prio) 1580109864Sjeff{ 1581164936Sjulian struct td_sched *ts; 1582177009Sjeff struct tdq *tdq; 1583177009Sjeff int oldpri; 1584109864Sjeff 1585187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 1586187357Sjeff "prio:%d", td->td_priority, "new prio:%d", prio, 1587187357Sjeff KTR_ATTR_LINKED, sched_tdname(curthread)); 1588187357Sjeff if (td != curthread && prio > td->td_priority) { 1589187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 1590187357Sjeff "lend prio", "prio:%d", td->td_priority, "new prio:%d", 1591187357Sjeff prio, KTR_ATTR_LINKED, sched_tdname(td)); 1592187357Sjeff } 1593164936Sjulian ts = td->td_sched; 1594170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1595139453Sjhb if (td->td_priority == prio) 1596139453Sjhb return; 1597177376Sjeff /* 1598177376Sjeff * If the priority has been elevated due to priority 1599177376Sjeff * propagation, we may have to move ourselves to a new 1600177376Sjeff * queue. This could be optimized to not re-add in some 1601177376Sjeff * cases. 1602177376Sjeff */ 1603165766Sjeff if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1604165762Sjeff sched_rem(td); 1605165762Sjeff td->td_priority = prio; 1606171482Sjeff sched_add(td, SRQ_BORROWING); 1607177009Sjeff return; 1608177009Sjeff } 1609177376Sjeff /* 1610177376Sjeff * If the thread is currently running we may have to adjust the lowpri 1611177376Sjeff * information so other cpus are aware of our current priority. 1612177376Sjeff */ 1613177009Sjeff if (TD_IS_RUNNING(td)) { 1614177376Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1615177376Sjeff oldpri = td->td_priority; 1616177376Sjeff td->td_priority = prio; 1617176735Sjeff if (prio < tdq->tdq_lowpri) 1618171482Sjeff tdq->tdq_lowpri = prio; 1619176735Sjeff else if (tdq->tdq_lowpri == oldpri) 1620176735Sjeff tdq_setlowpri(tdq, td); 1621177376Sjeff return; 1622177009Sjeff } 1623177376Sjeff td->td_priority = prio; 1624109864Sjeff} 1625109864Sjeff 1626139453Sjhb/* 1627139453Sjhb * Update a thread's priority when it is lent another thread's 1628139453Sjhb * priority. 1629139453Sjhb */ 1630109864Sjeffvoid 1631139453Sjhbsched_lend_prio(struct thread *td, u_char prio) 1632139453Sjhb{ 1633139453Sjhb 1634139453Sjhb td->td_flags |= TDF_BORROWING; 1635139453Sjhb sched_thread_priority(td, prio); 1636139453Sjhb} 1637139453Sjhb 1638139453Sjhb/* 1639139453Sjhb * Restore a thread's priority when priority propagation is 1640139453Sjhb * over. The prio argument is the minimum priority the thread 1641139453Sjhb * needs to have to satisfy other possible priority lending 1642139453Sjhb * requests. If the thread's regular priority is less 1643139453Sjhb * important than prio, the thread will keep a priority boost 1644139453Sjhb * of prio. 1645139453Sjhb */ 1646139453Sjhbvoid 1647139453Sjhbsched_unlend_prio(struct thread *td, u_char prio) 1648139453Sjhb{ 1649139453Sjhb u_char base_pri; 1650139453Sjhb 1651139453Sjhb if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1652139453Sjhb td->td_base_pri <= PRI_MAX_TIMESHARE) 1653163709Sjb base_pri = td->td_user_pri; 1654139453Sjhb else 1655139453Sjhb base_pri = td->td_base_pri; 1656139453Sjhb if (prio >= base_pri) { 1657139455Sjhb td->td_flags &= ~TDF_BORROWING; 1658139453Sjhb sched_thread_priority(td, base_pri); 1659139453Sjhb } else 1660139453Sjhb sched_lend_prio(td, prio); 1661139453Sjhb} 1662139453Sjhb 1663171482Sjeff/* 1664171482Sjeff * Standard entry for setting the priority to an absolute value. 1665171482Sjeff */ 1666139453Sjhbvoid 1667139453Sjhbsched_prio(struct thread *td, u_char prio) 1668139453Sjhb{ 1669139453Sjhb u_char oldprio; 1670139453Sjhb 1671139453Sjhb /* First, update the base priority. */ 1672139453Sjhb td->td_base_pri = prio; 1673139453Sjhb 1674139453Sjhb /* 1675139455Sjhb * If the thread is borrowing another thread's priority, don't 1676139453Sjhb * ever lower the priority. 1677139453Sjhb */ 1678139453Sjhb if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1679139453Sjhb return; 1680139453Sjhb 1681139453Sjhb /* Change the real priority. */ 1682139453Sjhb oldprio = td->td_priority; 1683139453Sjhb sched_thread_priority(td, prio); 1684139453Sjhb 1685139453Sjhb /* 1686139453Sjhb * If the thread is on a turnstile, then let the turnstile update 1687139453Sjhb * its state. 1688139453Sjhb */ 1689139453Sjhb if (TD_ON_LOCK(td) && oldprio != prio) 1690139453Sjhb turnstile_adjust(td, oldprio); 1691139453Sjhb} 1692139455Sjhb 1693171482Sjeff/* 1694171482Sjeff * Set the base user priority, does not effect current running priority. 1695171482Sjeff */ 1696139453Sjhbvoid 1697163709Sjbsched_user_prio(struct thread *td, u_char prio) 1698161599Sdavidxu{ 1699161599Sdavidxu 1700163709Sjb td->td_base_user_pri = prio; 1701216313Sdavidxu if (td->td_lend_user_pri <= prio) 1702216313Sdavidxu return; 1703163709Sjb td->td_user_pri = prio; 1704161599Sdavidxu} 1705161599Sdavidxu 1706161599Sdavidxuvoid 1707161599Sdavidxusched_lend_user_prio(struct thread *td, u_char prio) 1708161599Sdavidxu{ 1709161599Sdavidxu 1710174536Sdavidxu THREAD_LOCK_ASSERT(td, MA_OWNED); 1711216313Sdavidxu td->td_lend_user_pri = prio; 1712216791Sdavidxu td->td_user_pri = min(prio, td->td_base_user_pri); 1713216791Sdavidxu if (td->td_priority > td->td_user_pri) 1714216791Sdavidxu sched_prio(td, td->td_user_pri); 1715216791Sdavidxu else if (td->td_priority != td->td_user_pri) 1716216791Sdavidxu td->td_flags |= TDF_NEEDRESCHED; 1717161599Sdavidxu} 1718161599Sdavidxu 1719171482Sjeff/* 1720171713Sjeff * Handle migration from sched_switch(). This happens only for 1721171713Sjeff * cpu binding. 1722171713Sjeff */ 1723171713Sjeffstatic struct mtx * 1724171713Sjeffsched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1725171713Sjeff{ 1726171713Sjeff struct tdq *tdn; 1727171713Sjeff 1728171713Sjeff tdn = TDQ_CPU(td->td_sched->ts_cpu); 1729171713Sjeff#ifdef SMP 1730177435Sjeff tdq_load_rem(tdq, td); 1731171713Sjeff /* 1732171713Sjeff * Do the lock dance required to avoid LOR. We grab an extra 1733171713Sjeff * spinlock nesting to prevent preemption while we're 1734171713Sjeff * not holding either run-queue lock. 1735171713Sjeff */ 1736171713Sjeff spinlock_enter(); 1737202889Sattilio thread_lock_block(td); /* This releases the lock on tdq. */ 1738197223Sattilio 1739197223Sattilio /* 1740197223Sattilio * Acquire both run-queue locks before placing the thread on the new 1741197223Sattilio * run-queue to avoid deadlocks created by placing a thread with a 1742197223Sattilio * blocked lock on the run-queue of a remote processor. The deadlock 1743197223Sattilio * occurs when a third processor attempts to lock the two queues in 1744197223Sattilio * question while the target processor is spinning with its own 1745197223Sattilio * run-queue lock held while waiting for the blocked lock to clear. 1746197223Sattilio */ 1747197223Sattilio tdq_lock_pair(tdn, tdq); 1748171713Sjeff tdq_add(tdn, td, flags); 1749177435Sjeff tdq_notify(tdn, td); 1750197223Sattilio TDQ_UNLOCK(tdn); 1751171713Sjeff spinlock_exit(); 1752171713Sjeff#endif 1753171713Sjeff return (TDQ_LOCKPTR(tdn)); 1754171713Sjeff} 1755171713Sjeff 1756171713Sjeff/* 1757202889Sattilio * Variadic version of thread_lock_unblock() that does not assume td_lock 1758202889Sattilio * is blocked. 1759171482Sjeff */ 1760171482Sjeffstatic inline void 1761171482Sjeffthread_unblock_switch(struct thread *td, struct mtx *mtx) 1762171482Sjeff{ 1763171482Sjeff atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1764171482Sjeff (uintptr_t)mtx); 1765171482Sjeff} 1766171482Sjeff 1767171482Sjeff/* 1768171482Sjeff * Switch threads. This function has to handle threads coming in while 1769171482Sjeff * blocked for some reason, running, or idle. It also must deal with 1770171482Sjeff * migrating a thread from one queue to another as running threads may 1771171482Sjeff * be assigned elsewhere via binding. 1772171482Sjeff */ 1773161599Sdavidxuvoid 1774135051Sjuliansched_switch(struct thread *td, struct thread *newtd, int flags) 1775109864Sjeff{ 1776165627Sjeff struct tdq *tdq; 1777164936Sjulian struct td_sched *ts; 1778171482Sjeff struct mtx *mtx; 1779171713Sjeff int srqflag; 1780171482Sjeff int cpuid; 1781109864Sjeff 1782170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1783177376Sjeff KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument")); 1784109864Sjeff 1785171482Sjeff cpuid = PCPU_GET(cpuid); 1786171482Sjeff tdq = TDQ_CPU(cpuid); 1787164936Sjulian ts = td->td_sched; 1788171713Sjeff mtx = td->td_lock; 1789171482Sjeff ts->ts_rltick = ticks; 1790133555Sjeff td->td_lastcpu = td->td_oncpu; 1791113339Sjulian td->td_oncpu = NOCPU; 1792220198Sfabient if (!(flags & SW_PREEMPT)) 1793220198Sfabient td->td_flags &= ~TDF_NEEDRESCHED; 1794144777Sups td->td_owepreempt = 0; 1795178277Sjeff tdq->tdq_switchcnt++; 1796123434Sjeff /* 1797171482Sjeff * The lock pointer in an idle thread should never change. Reset it 1798171482Sjeff * to CAN_RUN as well. 1799123434Sjeff */ 1800167327Sjulian if (TD_IS_IDLETHREAD(td)) { 1801171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1802139334Sjeff TD_SET_CAN_RUN(td); 1803170293Sjeff } else if (TD_IS_RUNNING(td)) { 1804171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1805171713Sjeff srqflag = (flags & SW_PREEMPT) ? 1806170293Sjeff SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1807171713Sjeff SRQ_OURSELF|SRQ_YIELDING; 1808212153Smdf#ifdef SMP 1809212115Smdf if (THREAD_CAN_MIGRATE(td) && !THREAD_CAN_SCHED(td, ts->ts_cpu)) 1810212115Smdf ts->ts_cpu = sched_pickcpu(td, 0); 1811212153Smdf#endif 1812171713Sjeff if (ts->ts_cpu == cpuid) 1813177435Sjeff tdq_runq_add(tdq, td, srqflag); 1814212115Smdf else { 1815212115Smdf KASSERT(THREAD_CAN_MIGRATE(td) || 1816212115Smdf (ts->ts_flags & TSF_BOUND) != 0, 1817212115Smdf ("Thread %p shouldn't migrate", td)); 1818171713Sjeff mtx = sched_switch_migrate(tdq, td, srqflag); 1819212115Smdf } 1820171482Sjeff } else { 1821171482Sjeff /* This thread must be going to sleep. */ 1822171482Sjeff TDQ_LOCK(tdq); 1823202889Sattilio mtx = thread_lock_block(td); 1824177435Sjeff tdq_load_rem(tdq, td); 1825171482Sjeff } 1826171482Sjeff /* 1827171482Sjeff * We enter here with the thread blocked and assigned to the 1828171482Sjeff * appropriate cpu run-queue or sleep-queue and with the current 1829171482Sjeff * thread-queue locked. 1830171482Sjeff */ 1831171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 1832171482Sjeff newtd = choosethread(); 1833171482Sjeff /* 1834171482Sjeff * Call the MD code to switch contexts if necessary. 1835171482Sjeff */ 1836145256Sjkoshy if (td != newtd) { 1837145256Sjkoshy#ifdef HWPMC_HOOKS 1838145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1839145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1840145256Sjkoshy#endif 1841174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 1842172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 1843179297Sjb 1844179297Sjb#ifdef KDTRACE_HOOKS 1845179297Sjb /* 1846179297Sjb * If DTrace has set the active vtime enum to anything 1847179297Sjb * other than INACTIVE (0), then it should have set the 1848179297Sjb * function to call. 1849179297Sjb */ 1850179297Sjb if (dtrace_vtime_active) 1851179297Sjb (*dtrace_vtime_switch_func)(newtd); 1852179297Sjb#endif 1853179297Sjb 1854171482Sjeff cpu_switch(td, newtd, mtx); 1855171482Sjeff /* 1856171482Sjeff * We may return from cpu_switch on a different cpu. However, 1857171482Sjeff * we always return with td_lock pointing to the current cpu's 1858171482Sjeff * run queue lock. 1859171482Sjeff */ 1860171482Sjeff cpuid = PCPU_GET(cpuid); 1861171482Sjeff tdq = TDQ_CPU(cpuid); 1862174629Sjeff lock_profile_obtain_lock_success( 1863174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 1864145256Sjkoshy#ifdef HWPMC_HOOKS 1865145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1866145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1867145256Sjkoshy#endif 1868171482Sjeff } else 1869171482Sjeff thread_unblock_switch(td, mtx); 1870171482Sjeff /* 1871171482Sjeff * Assert that all went well and return. 1872171482Sjeff */ 1873171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED); 1874171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1875171482Sjeff td->td_oncpu = cpuid; 1876109864Sjeff} 1877109864Sjeff 1878171482Sjeff/* 1879171482Sjeff * Adjust thread priorities as a result of a nice request. 1880171482Sjeff */ 1881109864Sjeffvoid 1882130551Sjuliansched_nice(struct proc *p, int nice) 1883109864Sjeff{ 1884109864Sjeff struct thread *td; 1885109864Sjeff 1886130551Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 1887165762Sjeff 1888130551Sjulian p->p_nice = nice; 1889163709Sjb FOREACH_THREAD_IN_PROC(p, td) { 1890170293Sjeff thread_lock(td); 1891163709Sjb sched_priority(td); 1892165762Sjeff sched_prio(td, td->td_base_user_pri); 1893170293Sjeff thread_unlock(td); 1894130551Sjulian } 1895109864Sjeff} 1896109864Sjeff 1897171482Sjeff/* 1898171482Sjeff * Record the sleep time for the interactivity scorer. 1899171482Sjeff */ 1900109864Sjeffvoid 1901177085Sjeffsched_sleep(struct thread *td, int prio) 1902109864Sjeff{ 1903165762Sjeff 1904170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1905109864Sjeff 1906172264Sjeff td->td_slptick = ticks; 1907201347Skib if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 1908177085Sjeff td->td_flags |= TDF_CANSWAP; 1909217410Sjhb if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 1910217410Sjhb return; 1911177903Sjeff if (static_boost == 1 && prio) 1912177085Sjeff sched_prio(td, prio); 1913177903Sjeff else if (static_boost && td->td_priority > static_boost) 1914177903Sjeff sched_prio(td, static_boost); 1915109864Sjeff} 1916109864Sjeff 1917171482Sjeff/* 1918171482Sjeff * Schedule a thread to resume execution and record how long it voluntarily 1919171482Sjeff * slept. We also update the pctcpu, interactivity, and priority. 1920171482Sjeff */ 1921109864Sjeffvoid 1922109864Sjeffsched_wakeup(struct thread *td) 1923109864Sjeff{ 1924166229Sjeff struct td_sched *ts; 1925171482Sjeff int slptick; 1926165762Sjeff 1927170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1928166229Sjeff ts = td->td_sched; 1929177085Sjeff td->td_flags &= ~TDF_CANSWAP; 1930109864Sjeff /* 1931165762Sjeff * If we slept for more than a tick update our interactivity and 1932165762Sjeff * priority. 1933109864Sjeff */ 1934172264Sjeff slptick = td->td_slptick; 1935172264Sjeff td->td_slptick = 0; 1936171482Sjeff if (slptick && slptick != ticks) { 1937166208Sjeff u_int hzticks; 1938109864Sjeff 1939171482Sjeff hzticks = (ticks - slptick) << SCHED_TICK_SHIFT; 1940171482Sjeff ts->ts_slptime += hzticks; 1941165819Sjeff sched_interact_update(td); 1942166229Sjeff sched_pctcpu_update(ts); 1943109864Sjeff } 1944166229Sjeff /* Reset the slice value after we sleep. */ 1945166229Sjeff ts->ts_slice = sched_slice; 1946166190Sjeff sched_add(td, SRQ_BORING); 1947109864Sjeff} 1948109864Sjeff 1949109864Sjeff/* 1950109864Sjeff * Penalize the parent for creating a new child and initialize the child's 1951109864Sjeff * priority. 1952109864Sjeff */ 1953109864Sjeffvoid 1954163709Sjbsched_fork(struct thread *td, struct thread *child) 1955109864Sjeff{ 1956170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1957164936Sjulian sched_fork_thread(td, child); 1958165762Sjeff /* 1959165762Sjeff * Penalize the parent and child for forking. 1960165762Sjeff */ 1961165762Sjeff sched_interact_fork(child); 1962165762Sjeff sched_priority(child); 1963171482Sjeff td->td_sched->ts_runtime += tickincr; 1964165762Sjeff sched_interact_update(td); 1965165762Sjeff sched_priority(td); 1966164936Sjulian} 1967109864Sjeff 1968171482Sjeff/* 1969171482Sjeff * Fork a new thread, may be within the same process. 1970171482Sjeff */ 1971164936Sjulianvoid 1972164936Sjuliansched_fork_thread(struct thread *td, struct thread *child) 1973164936Sjulian{ 1974164936Sjulian struct td_sched *ts; 1975164936Sjulian struct td_sched *ts2; 1976164936Sjulian 1977177426Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1978165762Sjeff /* 1979165762Sjeff * Initialize child. 1980165762Sjeff */ 1981177426Sjeff ts = td->td_sched; 1982177426Sjeff ts2 = child->td_sched; 1983171482Sjeff child->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1984176735Sjeff child->td_cpuset = cpuset_ref(td->td_cpuset); 1985164936Sjulian ts2->ts_cpu = ts->ts_cpu; 1986177426Sjeff ts2->ts_flags = 0; 1987165762Sjeff /* 1988217078Sjhb * Grab our parents cpu estimation information. 1989165762Sjeff */ 1990164936Sjulian ts2->ts_ticks = ts->ts_ticks; 1991164936Sjulian ts2->ts_ltick = ts->ts_ltick; 1992199764Sivoras ts2->ts_incrtick = ts->ts_incrtick; 1993164936Sjulian ts2->ts_ftick = ts->ts_ftick; 1994165762Sjeff /* 1995217078Sjhb * Do not inherit any borrowed priority from the parent. 1996217078Sjhb */ 1997217078Sjhb child->td_priority = child->td_base_pri; 1998217078Sjhb /* 1999165762Sjeff * And update interactivity score. 2000165762Sjeff */ 2001171482Sjeff ts2->ts_slptime = ts->ts_slptime; 2002171482Sjeff ts2->ts_runtime = ts->ts_runtime; 2003165762Sjeff ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 2004187357Sjeff#ifdef KTR 2005187357Sjeff bzero(ts2->ts_name, sizeof(ts2->ts_name)); 2006187357Sjeff#endif 2007113357Sjeff} 2008113357Sjeff 2009171482Sjeff/* 2010171482Sjeff * Adjust the priority class of a thread. 2011171482Sjeff */ 2012113357Sjeffvoid 2013163709Sjbsched_class(struct thread *td, int class) 2014113357Sjeff{ 2015113357Sjeff 2016170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2017163709Sjb if (td->td_pri_class == class) 2018113357Sjeff return; 2019163709Sjb td->td_pri_class = class; 2020109864Sjeff} 2021109864Sjeff 2022109864Sjeff/* 2023109864Sjeff * Return some of the child's priority and interactivity to the parent. 2024109864Sjeff */ 2025109864Sjeffvoid 2026164939Sjuliansched_exit(struct proc *p, struct thread *child) 2027109864Sjeff{ 2028165762Sjeff struct thread *td; 2029113372Sjeff 2030187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2031225199Sdelphij "prio:%d", child->td_priority); 2032177368Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 2033165762Sjeff td = FIRST_THREAD_IN_PROC(p); 2034165762Sjeff sched_exit_thread(td, child); 2035113372Sjeff} 2036113372Sjeff 2037171482Sjeff/* 2038171482Sjeff * Penalize another thread for the time spent on this one. This helps to 2039171482Sjeff * worsen the priority and interactivity of processes which schedule batch 2040171482Sjeff * jobs such as make. This has little effect on the make process itself but 2041171482Sjeff * causes new processes spawned by it to receive worse scores immediately. 2042171482Sjeff */ 2043113372Sjeffvoid 2044164939Sjuliansched_exit_thread(struct thread *td, struct thread *child) 2045164936Sjulian{ 2046165762Sjeff 2047187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2048225199Sdelphij "prio:%d", child->td_priority); 2049165762Sjeff /* 2050165762Sjeff * Give the child's runtime to the parent without returning the 2051165762Sjeff * sleep time as a penalty to the parent. This causes shells that 2052165762Sjeff * launch expensive things to mark their children as expensive. 2053165762Sjeff */ 2054170293Sjeff thread_lock(td); 2055171482Sjeff td->td_sched->ts_runtime += child->td_sched->ts_runtime; 2056164939Sjulian sched_interact_update(td); 2057165762Sjeff sched_priority(td); 2058170293Sjeff thread_unlock(td); 2059164936Sjulian} 2060164936Sjulian 2061177005Sjeffvoid 2062177005Sjeffsched_preempt(struct thread *td) 2063177005Sjeff{ 2064177005Sjeff struct tdq *tdq; 2065177005Sjeff 2066177005Sjeff thread_lock(td); 2067177005Sjeff tdq = TDQ_SELF(); 2068177005Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2069177005Sjeff tdq->tdq_ipipending = 0; 2070177005Sjeff if (td->td_priority > tdq->tdq_lowpri) { 2071178272Sjeff int flags; 2072178272Sjeff 2073178272Sjeff flags = SW_INVOL | SW_PREEMPT; 2074177005Sjeff if (td->td_critnest > 1) 2075177005Sjeff td->td_owepreempt = 1; 2076178272Sjeff else if (TD_IS_IDLETHREAD(td)) 2077178272Sjeff mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL); 2078177005Sjeff else 2079178272Sjeff mi_switch(flags | SWT_REMOTEPREEMPT, NULL); 2080177005Sjeff } 2081177005Sjeff thread_unlock(td); 2082177005Sjeff} 2083177005Sjeff 2084171482Sjeff/* 2085171482Sjeff * Fix priorities on return to user-space. Priorities may be elevated due 2086171482Sjeff * to static priorities in msleep() or similar. 2087171482Sjeff */ 2088164936Sjulianvoid 2089164936Sjuliansched_userret(struct thread *td) 2090164936Sjulian{ 2091164936Sjulian /* 2092164936Sjulian * XXX we cheat slightly on the locking here to avoid locking in 2093164936Sjulian * the usual case. Setting td_priority here is essentially an 2094164936Sjulian * incomplete workaround for not setting it properly elsewhere. 2095164936Sjulian * Now that some interrupt handlers are threads, not setting it 2096164936Sjulian * properly elsewhere can clobber it in the window between setting 2097164936Sjulian * it here and returning to user mode, so don't waste time setting 2098164936Sjulian * it perfectly here. 2099164936Sjulian */ 2100164936Sjulian KASSERT((td->td_flags & TDF_BORROWING) == 0, 2101164936Sjulian ("thread with borrowed priority returning to userland")); 2102164936Sjulian if (td->td_priority != td->td_user_pri) { 2103170293Sjeff thread_lock(td); 2104164936Sjulian td->td_priority = td->td_user_pri; 2105164936Sjulian td->td_base_pri = td->td_user_pri; 2106177005Sjeff tdq_setlowpri(TDQ_SELF(), td); 2107170293Sjeff thread_unlock(td); 2108164936Sjulian } 2109164936Sjulian} 2110164936Sjulian 2111171482Sjeff/* 2112171482Sjeff * Handle a stathz tick. This is really only relevant for timeshare 2113171482Sjeff * threads. 2114171482Sjeff */ 2115164936Sjulianvoid 2116121127Sjeffsched_clock(struct thread *td) 2117109864Sjeff{ 2118164936Sjulian struct tdq *tdq; 2119164936Sjulian struct td_sched *ts; 2120109864Sjeff 2121171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2122164936Sjulian tdq = TDQ_SELF(); 2123172409Sjeff#ifdef SMP 2124133427Sjeff /* 2125172409Sjeff * We run the long term load balancer infrequently on the first cpu. 2126172409Sjeff */ 2127172409Sjeff if (balance_tdq == tdq) { 2128172409Sjeff if (balance_ticks && --balance_ticks == 0) 2129172409Sjeff sched_balance(); 2130172409Sjeff } 2131172409Sjeff#endif 2132172409Sjeff /* 2133178277Sjeff * Save the old switch count so we have a record of the last ticks 2134178277Sjeff * activity. Initialize the new switch count based on our load. 2135178277Sjeff * If there is some activity seed it to reflect that. 2136178277Sjeff */ 2137178277Sjeff tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 2138178471Sjeff tdq->tdq_switchcnt = tdq->tdq_load; 2139178277Sjeff /* 2140165766Sjeff * Advance the insert index once for each tick to ensure that all 2141165766Sjeff * threads get a chance to run. 2142133427Sjeff */ 2143165766Sjeff if (tdq->tdq_idx == tdq->tdq_ridx) { 2144165766Sjeff tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 2145165766Sjeff if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 2146165766Sjeff tdq->tdq_ridx = tdq->tdq_idx; 2147165766Sjeff } 2148165766Sjeff ts = td->td_sched; 2149175104Sjeff if (td->td_pri_class & PRI_FIFO_BIT) 2150113357Sjeff return; 2151217291Sjhb if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) { 2152175104Sjeff /* 2153175104Sjeff * We used a tick; charge it to the thread so 2154175104Sjeff * that we can compute our interactivity. 2155175104Sjeff */ 2156175104Sjeff td->td_sched->ts_runtime += tickincr; 2157175104Sjeff sched_interact_update(td); 2158177009Sjeff sched_priority(td); 2159175104Sjeff } 2160113357Sjeff /* 2161109864Sjeff * We used up one time slice. 2162109864Sjeff */ 2163164936Sjulian if (--ts->ts_slice > 0) 2164113357Sjeff return; 2165109864Sjeff /* 2166177009Sjeff * We're out of time, force a requeue at userret(). 2167109864Sjeff */ 2168177009Sjeff ts->ts_slice = sched_slice; 2169113357Sjeff td->td_flags |= TDF_NEEDRESCHED; 2170109864Sjeff} 2171109864Sjeff 2172171482Sjeff/* 2173171482Sjeff * Called once per hz tick. Used for cpu utilization information. This 2174171482Sjeff * is easier than trying to scale based on stathz. 2175171482Sjeff */ 2176171482Sjeffvoid 2177212541Smavsched_tick(int cnt) 2178171482Sjeff{ 2179171482Sjeff struct td_sched *ts; 2180171482Sjeff 2181171482Sjeff ts = curthread->td_sched; 2182180607Sjeff /* 2183180607Sjeff * Ticks is updated asynchronously on a single cpu. Check here to 2184180607Sjeff * avoid incrementing ts_ticks multiple times in a single tick. 2185180607Sjeff */ 2186199764Sivoras if (ts->ts_incrtick == ticks) 2187180607Sjeff return; 2188171482Sjeff /* Adjust ticks for pctcpu */ 2189212541Smav ts->ts_ticks += cnt << SCHED_TICK_SHIFT; 2190171482Sjeff ts->ts_ltick = ticks; 2191199764Sivoras ts->ts_incrtick = ticks; 2192171482Sjeff /* 2193215102Sattilio * Update if we've exceeded our desired tick threshold by over one 2194171482Sjeff * second. 2195171482Sjeff */ 2196171482Sjeff if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick) 2197171482Sjeff sched_pctcpu_update(ts); 2198171482Sjeff} 2199171482Sjeff 2200171482Sjeff/* 2201171482Sjeff * Return whether the current CPU has runnable tasks. Used for in-kernel 2202171482Sjeff * cooperative idle threads. 2203171482Sjeff */ 2204109864Sjeffint 2205109864Sjeffsched_runnable(void) 2206109864Sjeff{ 2207164936Sjulian struct tdq *tdq; 2208115998Sjeff int load; 2209109864Sjeff 2210115998Sjeff load = 1; 2211115998Sjeff 2212164936Sjulian tdq = TDQ_SELF(); 2213121605Sjeff if ((curthread->td_flags & TDF_IDLETD) != 0) { 2214165620Sjeff if (tdq->tdq_load > 0) 2215121605Sjeff goto out; 2216121605Sjeff } else 2217165620Sjeff if (tdq->tdq_load - 1 > 0) 2218121605Sjeff goto out; 2219115998Sjeff load = 0; 2220115998Sjeffout: 2221115998Sjeff return (load); 2222109864Sjeff} 2223109864Sjeff 2224171482Sjeff/* 2225171482Sjeff * Choose the highest priority thread to run. The thread is removed from 2226171482Sjeff * the run-queue while running however the load remains. For SMP we set 2227171482Sjeff * the tdq in the global idle bitmask if it idles here. 2228171482Sjeff */ 2229166190Sjeffstruct thread * 2230109970Sjeffsched_choose(void) 2231109970Sjeff{ 2232177435Sjeff struct thread *td; 2233164936Sjulian struct tdq *tdq; 2234109970Sjeff 2235164936Sjulian tdq = TDQ_SELF(); 2236171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2237177435Sjeff td = tdq_choose(tdq); 2238177435Sjeff if (td) { 2239177435Sjeff td->td_sched->ts_ltick = ticks; 2240177435Sjeff tdq_runq_rem(tdq, td); 2241177903Sjeff tdq->tdq_lowpri = td->td_priority; 2242177435Sjeff return (td); 2243109864Sjeff } 2244177903Sjeff tdq->tdq_lowpri = PRI_MAX_IDLE; 2245176735Sjeff return (PCPU_GET(idlethread)); 2246109864Sjeff} 2247109864Sjeff 2248171482Sjeff/* 2249171482Sjeff * Set owepreempt if necessary. Preemption never happens directly in ULE, 2250171482Sjeff * we always request it once we exit a critical section. 2251171482Sjeff */ 2252171482Sjeffstatic inline void 2253171482Sjeffsched_setpreempt(struct thread *td) 2254166190Sjeff{ 2255166190Sjeff struct thread *ctd; 2256166190Sjeff int cpri; 2257166190Sjeff int pri; 2258166190Sjeff 2259177005Sjeff THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2260177005Sjeff 2261166190Sjeff ctd = curthread; 2262166190Sjeff pri = td->td_priority; 2263166190Sjeff cpri = ctd->td_priority; 2264177005Sjeff if (pri < cpri) 2265177005Sjeff ctd->td_flags |= TDF_NEEDRESCHED; 2266166190Sjeff if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2267171482Sjeff return; 2268177005Sjeff if (!sched_shouldpreempt(pri, cpri, 0)) 2269171482Sjeff return; 2270171482Sjeff ctd->td_owepreempt = 1; 2271166190Sjeff} 2272166190Sjeff 2273171482Sjeff/* 2274177009Sjeff * Add a thread to a thread queue. Select the appropriate runq and add the 2275177009Sjeff * thread to it. This is the internal function called when the tdq is 2276177009Sjeff * predetermined. 2277171482Sjeff */ 2278109864Sjeffvoid 2279171482Sjefftdq_add(struct tdq *tdq, struct thread *td, int flags) 2280109864Sjeff{ 2281109864Sjeff 2282171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2283166190Sjeff KASSERT((td->td_inhibitors == 0), 2284166190Sjeff ("sched_add: trying to run inhibited thread")); 2285166190Sjeff KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 2286166190Sjeff ("sched_add: bad thread state")); 2287172207Sjeff KASSERT(td->td_flags & TDF_INMEM, 2288172207Sjeff ("sched_add: thread swapped out")); 2289171482Sjeff 2290171482Sjeff if (td->td_priority < tdq->tdq_lowpri) 2291171482Sjeff tdq->tdq_lowpri = td->td_priority; 2292177435Sjeff tdq_runq_add(tdq, td, flags); 2293177435Sjeff tdq_load_add(tdq, td); 2294171482Sjeff} 2295171482Sjeff 2296171482Sjeff/* 2297171482Sjeff * Select the target thread queue and add a thread to it. Request 2298171482Sjeff * preemption or IPI a remote processor if required. 2299171482Sjeff */ 2300171482Sjeffvoid 2301171482Sjeffsched_add(struct thread *td, int flags) 2302171482Sjeff{ 2303171482Sjeff struct tdq *tdq; 2304171482Sjeff#ifdef SMP 2305171482Sjeff int cpu; 2306171482Sjeff#endif 2307187357Sjeff 2308187357Sjeff KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 2309187357Sjeff "prio:%d", td->td_priority, KTR_ATTR_LINKED, 2310187357Sjeff sched_tdname(curthread)); 2311187357Sjeff KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 2312187357Sjeff KTR_ATTR_LINKED, sched_tdname(td)); 2313171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2314166108Sjeff /* 2315171482Sjeff * Recalculate the priority before we select the target cpu or 2316171482Sjeff * run-queue. 2317166108Sjeff */ 2318171482Sjeff if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2319171482Sjeff sched_priority(td); 2320171482Sjeff#ifdef SMP 2321171482Sjeff /* 2322171482Sjeff * Pick the destination cpu and if it isn't ours transfer to the 2323171482Sjeff * target cpu. 2324171482Sjeff */ 2325177435Sjeff cpu = sched_pickcpu(td, flags); 2326177435Sjeff tdq = sched_setcpu(td, cpu, flags); 2327171482Sjeff tdq_add(tdq, td, flags); 2328177009Sjeff if (cpu != PCPU_GET(cpuid)) { 2329177435Sjeff tdq_notify(tdq, td); 2330166108Sjeff return; 2331166108Sjeff } 2332171482Sjeff#else 2333171482Sjeff tdq = TDQ_SELF(); 2334171482Sjeff TDQ_LOCK(tdq); 2335171482Sjeff /* 2336171482Sjeff * Now that the thread is moving to the run-queue, set the lock 2337171482Sjeff * to the scheduler's lock. 2338171482Sjeff */ 2339171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2340171482Sjeff tdq_add(tdq, td, flags); 2341166108Sjeff#endif 2342171482Sjeff if (!(flags & SRQ_YIELDING)) 2343171482Sjeff sched_setpreempt(td); 2344109864Sjeff} 2345109864Sjeff 2346171482Sjeff/* 2347171482Sjeff * Remove a thread from a run-queue without running it. This is used 2348171482Sjeff * when we're stealing a thread from a remote queue. Otherwise all threads 2349171482Sjeff * exit by calling sched_exit_thread() and sched_throw() themselves. 2350171482Sjeff */ 2351109864Sjeffvoid 2352121127Sjeffsched_rem(struct thread *td) 2353109864Sjeff{ 2354164936Sjulian struct tdq *tdq; 2355113357Sjeff 2356187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 2357187357Sjeff "prio:%d", td->td_priority); 2358177435Sjeff tdq = TDQ_CPU(td->td_sched->ts_cpu); 2359171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2360171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2361166190Sjeff KASSERT(TD_ON_RUNQ(td), 2362164936Sjulian ("sched_rem: thread not on run queue")); 2363177435Sjeff tdq_runq_rem(tdq, td); 2364177435Sjeff tdq_load_rem(tdq, td); 2365166190Sjeff TD_SET_CAN_RUN(td); 2366176735Sjeff if (td->td_priority == tdq->tdq_lowpri) 2367176735Sjeff tdq_setlowpri(tdq, NULL); 2368109864Sjeff} 2369109864Sjeff 2370171482Sjeff/* 2371171482Sjeff * Fetch cpu utilization information. Updates on demand. 2372171482Sjeff */ 2373109864Sjefffixpt_t 2374121127Sjeffsched_pctcpu(struct thread *td) 2375109864Sjeff{ 2376109864Sjeff fixpt_t pctcpu; 2377164936Sjulian struct td_sched *ts; 2378109864Sjeff 2379109864Sjeff pctcpu = 0; 2380164936Sjulian ts = td->td_sched; 2381164936Sjulian if (ts == NULL) 2382121290Sjeff return (0); 2383109864Sjeff 2384208787Sjhb THREAD_LOCK_ASSERT(td, MA_OWNED); 2385164936Sjulian if (ts->ts_ticks) { 2386109864Sjeff int rtick; 2387109864Sjeff 2388165796Sjeff sched_pctcpu_update(ts); 2389109864Sjeff /* How many rtick per second ? */ 2390165762Sjeff rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2391165762Sjeff pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 2392109864Sjeff } 2393109864Sjeff 2394109864Sjeff return (pctcpu); 2395109864Sjeff} 2396109864Sjeff 2397176735Sjeff/* 2398176735Sjeff * Enforce affinity settings for a thread. Called after adjustments to 2399176735Sjeff * cpumask. 2400176735Sjeff */ 2401176729Sjeffvoid 2402176729Sjeffsched_affinity(struct thread *td) 2403176729Sjeff{ 2404176735Sjeff#ifdef SMP 2405176735Sjeff struct td_sched *ts; 2406176735Sjeff 2407176735Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2408176735Sjeff ts = td->td_sched; 2409176735Sjeff if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 2410176735Sjeff return; 2411189787Sjeff if (TD_ON_RUNQ(td)) { 2412189787Sjeff sched_rem(td); 2413189787Sjeff sched_add(td, SRQ_BORING); 2414189787Sjeff return; 2415189787Sjeff } 2416176735Sjeff if (!TD_IS_RUNNING(td)) 2417176735Sjeff return; 2418176735Sjeff /* 2419212115Smdf * Force a switch before returning to userspace. If the 2420212115Smdf * target thread is not running locally send an ipi to force 2421212115Smdf * the issue. 2422176735Sjeff */ 2423212974Sjhb td->td_flags |= TDF_NEEDRESCHED; 2424212115Smdf if (td != curthread) 2425212115Smdf ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 2426176735Sjeff#endif 2427176729Sjeff} 2428176729Sjeff 2429171482Sjeff/* 2430171482Sjeff * Bind a thread to a target cpu. 2431171482Sjeff */ 2432122038Sjeffvoid 2433122038Sjeffsched_bind(struct thread *td, int cpu) 2434122038Sjeff{ 2435164936Sjulian struct td_sched *ts; 2436122038Sjeff 2437171713Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 2438208391Sjhb KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 2439164936Sjulian ts = td->td_sched; 2440166137Sjeff if (ts->ts_flags & TSF_BOUND) 2441166152Sjeff sched_unbind(td); 2442212115Smdf KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2443164936Sjulian ts->ts_flags |= TSF_BOUND; 2444166137Sjeff sched_pin(); 2445123433Sjeff if (PCPU_GET(cpuid) == cpu) 2446122038Sjeff return; 2447166137Sjeff ts->ts_cpu = cpu; 2448122038Sjeff /* When we return from mi_switch we'll be on the correct cpu. */ 2449131527Sphk mi_switch(SW_VOL, NULL); 2450122038Sjeff} 2451122038Sjeff 2452171482Sjeff/* 2453171482Sjeff * Release a bound thread. 2454171482Sjeff */ 2455122038Sjeffvoid 2456122038Sjeffsched_unbind(struct thread *td) 2457122038Sjeff{ 2458165762Sjeff struct td_sched *ts; 2459165762Sjeff 2460170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2461208391Sjhb KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 2462165762Sjeff ts = td->td_sched; 2463166137Sjeff if ((ts->ts_flags & TSF_BOUND) == 0) 2464166137Sjeff return; 2465165762Sjeff ts->ts_flags &= ~TSF_BOUND; 2466165762Sjeff sched_unpin(); 2467122038Sjeff} 2468122038Sjeff 2469109864Sjeffint 2470145256Sjkoshysched_is_bound(struct thread *td) 2471145256Sjkoshy{ 2472170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2473164936Sjulian return (td->td_sched->ts_flags & TSF_BOUND); 2474145256Sjkoshy} 2475145256Sjkoshy 2476171482Sjeff/* 2477171482Sjeff * Basic yield call. 2478171482Sjeff */ 2479159630Sdavidxuvoid 2480159630Sdavidxusched_relinquish(struct thread *td) 2481159630Sdavidxu{ 2482170293Sjeff thread_lock(td); 2483178272Sjeff mi_switch(SW_VOL | SWT_RELINQUISH, NULL); 2484170293Sjeff thread_unlock(td); 2485159630Sdavidxu} 2486159630Sdavidxu 2487171482Sjeff/* 2488171482Sjeff * Return the total system load. 2489171482Sjeff */ 2490145256Sjkoshyint 2491125289Sjeffsched_load(void) 2492125289Sjeff{ 2493125289Sjeff#ifdef SMP 2494125289Sjeff int total; 2495125289Sjeff int i; 2496125289Sjeff 2497125289Sjeff total = 0; 2498209059Sjhb CPU_FOREACH(i) 2499176735Sjeff total += TDQ_CPU(i)->tdq_sysload; 2500125289Sjeff return (total); 2501125289Sjeff#else 2502165620Sjeff return (TDQ_SELF()->tdq_sysload); 2503125289Sjeff#endif 2504125289Sjeff} 2505125289Sjeff 2506125289Sjeffint 2507109864Sjeffsched_sizeof_proc(void) 2508109864Sjeff{ 2509109864Sjeff return (sizeof(struct proc)); 2510109864Sjeff} 2511109864Sjeff 2512109864Sjeffint 2513109864Sjeffsched_sizeof_thread(void) 2514109864Sjeff{ 2515109864Sjeff return (sizeof(struct thread) + sizeof(struct td_sched)); 2516109864Sjeff} 2517159570Sdavidxu 2518191676Sjeff#ifdef SMP 2519191676Sjeff#define TDQ_IDLESPIN(tdq) \ 2520191676Sjeff ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 2521191676Sjeff#else 2522191676Sjeff#define TDQ_IDLESPIN(tdq) 1 2523191676Sjeff#endif 2524191676Sjeff 2525166190Sjeff/* 2526166190Sjeff * The actual idle process. 2527166190Sjeff */ 2528166190Sjeffvoid 2529166190Sjeffsched_idletd(void *dummy) 2530166190Sjeff{ 2531166190Sjeff struct thread *td; 2532171482Sjeff struct tdq *tdq; 2533178277Sjeff int switchcnt; 2534178277Sjeff int i; 2535166190Sjeff 2536191643Sjeff mtx_assert(&Giant, MA_NOTOWNED); 2537166190Sjeff td = curthread; 2538171482Sjeff tdq = TDQ_SELF(); 2539171482Sjeff for (;;) { 2540171482Sjeff#ifdef SMP 2541178277Sjeff if (tdq_idled(tdq) == 0) 2542178277Sjeff continue; 2543171482Sjeff#endif 2544178277Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2545178277Sjeff /* 2546178277Sjeff * If we're switching very frequently, spin while checking 2547178277Sjeff * for load rather than entering a low power state that 2548191643Sjeff * may require an IPI. However, don't do any busy 2549191643Sjeff * loops while on SMT machines as this simply steals 2550191643Sjeff * cycles from cores doing useful work. 2551178277Sjeff */ 2552191676Sjeff if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 2553178277Sjeff for (i = 0; i < sched_idlespins; i++) { 2554178277Sjeff if (tdq->tdq_load) 2555178277Sjeff break; 2556178277Sjeff cpu_spinwait(); 2557178277Sjeff } 2558178277Sjeff } 2559191643Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2560212416Smav if (tdq->tdq_load == 0) { 2561212416Smav tdq->tdq_cpu_idle = 1; 2562212416Smav if (tdq->tdq_load == 0) { 2563212541Smav cpu_idle(switchcnt > sched_idlespinthresh * 4); 2564212416Smav tdq->tdq_switchcnt++; 2565212416Smav } 2566212416Smav tdq->tdq_cpu_idle = 0; 2567212416Smav } 2568178277Sjeff if (tdq->tdq_load) { 2569178277Sjeff thread_lock(td); 2570178277Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 2571178277Sjeff thread_unlock(td); 2572178277Sjeff } 2573171482Sjeff } 2574166190Sjeff} 2575166190Sjeff 2576170293Sjeff/* 2577170293Sjeff * A CPU is entering for the first time or a thread is exiting. 2578170293Sjeff */ 2579170293Sjeffvoid 2580170293Sjeffsched_throw(struct thread *td) 2581170293Sjeff{ 2582172411Sjeff struct thread *newtd; 2583171482Sjeff struct tdq *tdq; 2584171482Sjeff 2585171482Sjeff tdq = TDQ_SELF(); 2586170293Sjeff if (td == NULL) { 2587171482Sjeff /* Correct spinlock nesting and acquire the correct lock. */ 2588171482Sjeff TDQ_LOCK(tdq); 2589170293Sjeff spinlock_exit(); 2590229429Sjhb PCPU_SET(switchtime, cpu_ticks()); 2591229429Sjhb PCPU_SET(switchticks, ticks); 2592170293Sjeff } else { 2593171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2594177435Sjeff tdq_load_rem(tdq, td); 2595174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 2596170293Sjeff } 2597170293Sjeff KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count")); 2598172411Sjeff newtd = choosethread(); 2599172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 2600172411Sjeff cpu_throw(td, newtd); /* doesn't return */ 2601170293Sjeff} 2602170293Sjeff 2603171482Sjeff/* 2604171482Sjeff * This is called from fork_exit(). Just acquire the correct locks and 2605171482Sjeff * let fork do the rest of the work. 2606171482Sjeff */ 2607170293Sjeffvoid 2608170600Sjeffsched_fork_exit(struct thread *td) 2609170293Sjeff{ 2610171482Sjeff struct td_sched *ts; 2611171482Sjeff struct tdq *tdq; 2612171482Sjeff int cpuid; 2613170293Sjeff 2614170293Sjeff /* 2615170293Sjeff * Finish setting up thread glue so that it begins execution in a 2616171482Sjeff * non-nested critical section with the scheduler lock held. 2617170293Sjeff */ 2618171482Sjeff cpuid = PCPU_GET(cpuid); 2619171482Sjeff tdq = TDQ_CPU(cpuid); 2620171482Sjeff ts = td->td_sched; 2621171482Sjeff if (TD_IS_IDLETHREAD(td)) 2622171482Sjeff td->td_lock = TDQ_LOCKPTR(tdq); 2623171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2624171482Sjeff td->td_oncpu = cpuid; 2625172411Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 2626174629Sjeff lock_profile_obtain_lock_success( 2627174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 2628170293Sjeff} 2629170293Sjeff 2630187357Sjeff/* 2631187357Sjeff * Create on first use to catch odd startup conditons. 2632187357Sjeff */ 2633187357Sjeffchar * 2634187357Sjeffsched_tdname(struct thread *td) 2635187357Sjeff{ 2636187357Sjeff#ifdef KTR 2637187357Sjeff struct td_sched *ts; 2638187357Sjeff 2639187357Sjeff ts = td->td_sched; 2640187357Sjeff if (ts->ts_name[0] == '\0') 2641187357Sjeff snprintf(ts->ts_name, sizeof(ts->ts_name), 2642187357Sjeff "%s tid %d", td->td_name, td->td_tid); 2643187357Sjeff return (ts->ts_name); 2644187357Sjeff#else 2645187357Sjeff return (td->td_name); 2646187357Sjeff#endif 2647187357Sjeff} 2648187357Sjeff 2649184439Sivoras#ifdef SMP 2650184439Sivoras 2651184439Sivoras/* 2652184439Sivoras * Build the CPU topology dump string. Is recursively called to collect 2653184439Sivoras * the topology tree. 2654184439Sivoras */ 2655184439Sivorasstatic int 2656184439Sivorassysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 2657184439Sivoras int indent) 2658184439Sivoras{ 2659222813Sattilio char cpusetbuf[CPUSETBUFSIZ]; 2660184439Sivoras int i, first; 2661184439Sivoras 2662184439Sivoras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 2663212821Savg "", 1 + indent / 2, cg->cg_level); 2664222813Sattilio sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"%s\">", indent, "", 2665222813Sattilio cg->cg_count, cpusetobj_strprint(cpusetbuf, &cg->cg_mask)); 2666184439Sivoras first = TRUE; 2667184439Sivoras for (i = 0; i < MAXCPU; i++) { 2668222813Sattilio if (CPU_ISSET(i, &cg->cg_mask)) { 2669184439Sivoras if (!first) 2670184439Sivoras sbuf_printf(sb, ", "); 2671184439Sivoras else 2672184439Sivoras first = FALSE; 2673184439Sivoras sbuf_printf(sb, "%d", i); 2674184439Sivoras } 2675184439Sivoras } 2676184439Sivoras sbuf_printf(sb, "</cpu>\n"); 2677184439Sivoras 2678184439Sivoras if (cg->cg_flags != 0) { 2679210117Sivoras sbuf_printf(sb, "%*s <flags>", indent, ""); 2680184439Sivoras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 2681208982Sivoras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 2682208983Sivoras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2683208983Sivoras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 2684191643Sjeff if ((cg->cg_flags & CG_FLAG_SMT) != 0) 2685208983Sivoras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 2686210117Sivoras sbuf_printf(sb, "</flags>\n"); 2687184439Sivoras } 2688184439Sivoras 2689184439Sivoras if (cg->cg_children > 0) { 2690184439Sivoras sbuf_printf(sb, "%*s <children>\n", indent, ""); 2691184439Sivoras for (i = 0; i < cg->cg_children; i++) 2692184439Sivoras sysctl_kern_sched_topology_spec_internal(sb, 2693184439Sivoras &cg->cg_child[i], indent+2); 2694184439Sivoras sbuf_printf(sb, "%*s </children>\n", indent, ""); 2695184439Sivoras } 2696184439Sivoras sbuf_printf(sb, "%*s</group>\n", indent, ""); 2697184439Sivoras return (0); 2698184439Sivoras} 2699184439Sivoras 2700184439Sivoras/* 2701184439Sivoras * Sysctl handler for retrieving topology dump. It's a wrapper for 2702184439Sivoras * the recursive sysctl_kern_smp_topology_spec_internal(). 2703184439Sivoras */ 2704184439Sivorasstatic int 2705184439Sivorassysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 2706184439Sivoras{ 2707184439Sivoras struct sbuf *topo; 2708184439Sivoras int err; 2709184439Sivoras 2710184439Sivoras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 2711184439Sivoras 2712184570Sivoras topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND); 2713184439Sivoras if (topo == NULL) 2714184439Sivoras return (ENOMEM); 2715184439Sivoras 2716184439Sivoras sbuf_printf(topo, "<groups>\n"); 2717184439Sivoras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 2718184439Sivoras sbuf_printf(topo, "</groups>\n"); 2719184439Sivoras 2720184439Sivoras if (err == 0) { 2721184439Sivoras sbuf_finish(topo); 2722184439Sivoras err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo)); 2723184439Sivoras } 2724184439Sivoras sbuf_delete(topo); 2725184439Sivoras return (err); 2726184439Sivoras} 2727214510Sdavidxu 2728184439Sivoras#endif 2729184439Sivoras 2730177435SjeffSYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2731171482SjeffSYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 2732165762Sjeff "Scheduler name"); 2733171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 2734171482Sjeff "Slice size for timeshare threads"); 2735171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 2736171482Sjeff "Interactivity score threshold"); 2737171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh, 2738171482Sjeff 0,"Min priority for preemption, lower priorities have greater precedence"); 2739177085SjeffSYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 2740177085Sjeff 0,"Controls whether static kernel priorities are assigned to sleeping threads."); 2741178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 2742178277Sjeff 0,"Number of times idle will spin waiting for new work."); 2743178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh, 2744178277Sjeff 0,"Threshold before we will permit idle spinning."); 2745166108Sjeff#ifdef SMP 2746171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 2747171482Sjeff "Number of hz ticks to keep thread affinity for"); 2748171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 2749171482Sjeff "Enables the long-term load balancer"); 2750172409SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 2751172409Sjeff &balance_interval, 0, 2752172409Sjeff "Average frequency in stathz ticks to run the long-term balancer"); 2753171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0, 2754171482Sjeff "Steals work from another hyper-threaded core on idle"); 2755171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 2756171482Sjeff "Attempts to steal work from other cores before idling"); 2757171506SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 2758171506Sjeff "Minimum load on remote cpu before we'll steal"); 2759184439Sivoras 2760184439Sivoras/* Retrieve SMP topology */ 2761184439SivorasSYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 2762184439Sivoras CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 2763184439Sivoras "XML dump of detected CPU topology"); 2764214510Sdavidxu 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