sched_ule.c revision 226057
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 226057 2011-10-06 11:48:13Z marius $"); 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) 128217410Sjhb 129217410Sjhb#define PRI_MIN_INTERACT PRI_MIN_TIMESHARE 130217410Sjhb#define PRI_MAX_INTERACT (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE - 1) 131217410Sjhb#define PRI_MIN_BATCH (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE) 132217351Sjhb#define PRI_MAX_BATCH PRI_MAX_TIMESHARE 133217351Sjhb 134217351Sjhb/* 135165762Sjeff * Cpu percentage computation macros and defines. 136111857Sjeff * 137165762Sjeff * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 138165762Sjeff * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 139165796Sjeff * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 140165762Sjeff * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 141165762Sjeff * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 142165762Sjeff * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 143165762Sjeff */ 144165762Sjeff#define SCHED_TICK_SECS 10 145165762Sjeff#define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 146165796Sjeff#define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 147165762Sjeff#define SCHED_TICK_SHIFT 10 148165762Sjeff#define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 149165830Sjeff#define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 150165762Sjeff 151165762Sjeff/* 152165762Sjeff * These macros determine priorities for non-interactive threads. They are 153165762Sjeff * assigned a priority based on their recent cpu utilization as expressed 154165762Sjeff * by the ratio of ticks to the tick total. NHALF priorities at the start 155165762Sjeff * and end of the MIN to MAX timeshare range are only reachable with negative 156165762Sjeff * or positive nice respectively. 157165762Sjeff * 158165762Sjeff * PRI_RANGE: Priority range for utilization dependent priorities. 159116642Sjeff * PRI_NRESV: Number of nice values. 160165762Sjeff * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 161165762Sjeff * PRI_NICE: Determines the part of the priority inherited from nice. 162109864Sjeff */ 163165762Sjeff#define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 164121869Sjeff#define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 165217351Sjhb#define SCHED_PRI_MIN (PRI_MIN_BATCH + SCHED_PRI_NHALF) 166217351Sjhb#define SCHED_PRI_MAX (PRI_MAX_BATCH - SCHED_PRI_NHALF) 167217237Sjhb#define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1) 168165762Sjeff#define SCHED_PRI_TICKS(ts) \ 169165762Sjeff (SCHED_TICK_HZ((ts)) / \ 170165827Sjeff (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 171165762Sjeff#define SCHED_PRI_NICE(nice) (nice) 172109864Sjeff 173109864Sjeff/* 174165762Sjeff * These determine the interactivity of a process. Interactivity differs from 175165762Sjeff * cpu utilization in that it expresses the voluntary time slept vs time ran 176165762Sjeff * while cpu utilization includes all time not running. This more accurately 177165762Sjeff * models the intent of the thread. 178109864Sjeff * 179110645Sjeff * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 180110645Sjeff * before throttling back. 181121868Sjeff * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 182116365Sjeff * INTERACT_MAX: Maximum interactivity value. Smaller is better. 183215102Sattilio * INTERACT_THRESH: Threshold for placement on the current runq. 184109864Sjeff */ 185165762Sjeff#define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 186165762Sjeff#define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 187116365Sjeff#define SCHED_INTERACT_MAX (100) 188116365Sjeff#define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 189121126Sjeff#define SCHED_INTERACT_THRESH (30) 190111857Sjeff 191109864Sjeff/* 192165762Sjeff * tickincr: Converts a stathz tick into a hz domain scaled by 193165762Sjeff * the shift factor. Without the shift the error rate 194165762Sjeff * due to rounding would be unacceptably high. 195165762Sjeff * realstathz: stathz is sometimes 0 and run off of hz. 196165762Sjeff * sched_slice: Runtime of each thread before rescheduling. 197171482Sjeff * preempt_thresh: Priority threshold for preemption and remote IPIs. 198109864Sjeff */ 199165762Sjeffstatic int sched_interact = SCHED_INTERACT_THRESH; 200165762Sjeffstatic int realstathz; 201165762Sjeffstatic int tickincr; 202177009Sjeffstatic int sched_slice = 1; 203172345Sjeff#ifdef PREEMPTION 204172345Sjeff#ifdef FULL_PREEMPTION 205172345Sjeffstatic int preempt_thresh = PRI_MAX_IDLE; 206172345Sjeff#else 207171482Sjeffstatic int preempt_thresh = PRI_MIN_KERN; 208172345Sjeff#endif 209172345Sjeff#else 210172345Sjeffstatic int preempt_thresh = 0; 211172345Sjeff#endif 212217351Sjhbstatic int static_boost = PRI_MIN_BATCH; 213178277Sjeffstatic int sched_idlespins = 10000; 214212541Smavstatic int sched_idlespinthresh = 16; 215109864Sjeff 216109864Sjeff/* 217171482Sjeff * tdq - per processor runqs and statistics. All fields are protected by the 218171482Sjeff * tdq_lock. The load and lowpri may be accessed without to avoid excess 219171482Sjeff * locking in sched_pickcpu(); 220109864Sjeff */ 221164936Sjulianstruct tdq { 222177009Sjeff /* Ordered to improve efficiency of cpu_search() and switch(). */ 223177009Sjeff struct mtx tdq_lock; /* run queue lock. */ 224176735Sjeff struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 225178277Sjeff volatile int tdq_load; /* Aggregate load. */ 226212416Smav volatile int tdq_cpu_idle; /* cpu_idle() is active. */ 227176735Sjeff int tdq_sysload; /* For loadavg, !ITHD load. */ 228177009Sjeff int tdq_transferable; /* Transferable thread count. */ 229178277Sjeff short tdq_switchcnt; /* Switches this tick. */ 230178277Sjeff short tdq_oldswitchcnt; /* Switches last tick. */ 231177009Sjeff u_char tdq_lowpri; /* Lowest priority thread. */ 232177009Sjeff u_char tdq_ipipending; /* IPI pending. */ 233166557Sjeff u_char tdq_idx; /* Current insert index. */ 234166557Sjeff u_char tdq_ridx; /* Current removal index. */ 235177009Sjeff struct runq tdq_realtime; /* real-time run queue. */ 236177009Sjeff struct runq tdq_timeshare; /* timeshare run queue. */ 237177009Sjeff struct runq tdq_idle; /* Queue of IDLE threads. */ 238187357Sjeff char tdq_name[TDQ_NAME_LEN]; 239187357Sjeff#ifdef KTR 240187357Sjeff char tdq_loadname[TDQ_LOADNAME_LEN]; 241187357Sjeff#endif 242171482Sjeff} __aligned(64); 243109864Sjeff 244178277Sjeff/* Idle thread states and config. */ 245178277Sjeff#define TDQ_RUNNING 1 246178277Sjeff#define TDQ_IDLE 2 247166108Sjeff 248123433Sjeff#ifdef SMP 249184439Sivorasstruct cpu_group *cpu_top; /* CPU topology */ 250123433Sjeff 251176735Sjeff#define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 252176735Sjeff#define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 253166108Sjeff 254123433Sjeff/* 255166108Sjeff * Run-time tunables. 256166108Sjeff */ 257171506Sjeffstatic int rebalance = 1; 258172409Sjeffstatic int balance_interval = 128; /* Default set in sched_initticks(). */ 259166108Sjeffstatic int affinity; 260172409Sjeffstatic int steal_htt = 1; 261171506Sjeffstatic int steal_idle = 1; 262171506Sjeffstatic int steal_thresh = 2; 263166108Sjeff 264166108Sjeff/* 265165620Sjeff * One thread queue per processor. 266109864Sjeff */ 267164936Sjulianstatic struct tdq tdq_cpu[MAXCPU]; 268172409Sjeffstatic struct tdq *balance_tdq; 269172409Sjeffstatic int balance_ticks; 270129982Sjeff 271164936Sjulian#define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 272164936Sjulian#define TDQ_CPU(x) (&tdq_cpu[(x)]) 273171713Sjeff#define TDQ_ID(x) ((int)((x) - tdq_cpu)) 274123433Sjeff#else /* !SMP */ 275164936Sjulianstatic struct tdq tdq_cpu; 276129982Sjeff 277170315Sjeff#define TDQ_ID(x) (0) 278164936Sjulian#define TDQ_SELF() (&tdq_cpu) 279164936Sjulian#define TDQ_CPU(x) (&tdq_cpu) 280110028Sjeff#endif 281109864Sjeff 282171482Sjeff#define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 283171482Sjeff#define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 284171482Sjeff#define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 285171482Sjeff#define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 286176735Sjeff#define TDQ_LOCKPTR(t) (&(t)->tdq_lock) 287171482Sjeff 288163709Sjbstatic void sched_priority(struct thread *); 289146954Sjeffstatic void sched_thread_priority(struct thread *, u_char); 290163709Sjbstatic int sched_interact_score(struct thread *); 291163709Sjbstatic void sched_interact_update(struct thread *); 292163709Sjbstatic void sched_interact_fork(struct thread *); 293164936Sjulianstatic void sched_pctcpu_update(struct td_sched *); 294109864Sjeff 295110267Sjeff/* Operations on per processor queues */ 296177435Sjeffstatic struct thread *tdq_choose(struct tdq *); 297164936Sjulianstatic void tdq_setup(struct tdq *); 298177435Sjeffstatic void tdq_load_add(struct tdq *, struct thread *); 299177435Sjeffstatic void tdq_load_rem(struct tdq *, struct thread *); 300177435Sjeffstatic __inline void tdq_runq_add(struct tdq *, struct thread *, int); 301177435Sjeffstatic __inline void tdq_runq_rem(struct tdq *, struct thread *); 302177005Sjeffstatic inline int sched_shouldpreempt(int, int, int); 303164936Sjulianvoid tdq_print(int cpu); 304165762Sjeffstatic void runq_print(struct runq *rq); 305171482Sjeffstatic void tdq_add(struct tdq *, struct thread *, int); 306110267Sjeff#ifdef SMP 307176735Sjeffstatic int tdq_move(struct tdq *, struct tdq *); 308171482Sjeffstatic int tdq_idled(struct tdq *); 309177435Sjeffstatic void tdq_notify(struct tdq *, struct thread *); 310177435Sjeffstatic struct thread *tdq_steal(struct tdq *, int); 311177435Sjeffstatic struct thread *runq_steal(struct runq *, int); 312177435Sjeffstatic int sched_pickcpu(struct thread *, int); 313172409Sjeffstatic void sched_balance(void); 314176735Sjeffstatic int sched_balance_pair(struct tdq *, struct tdq *); 315177435Sjeffstatic inline struct tdq *sched_setcpu(struct thread *, int, int); 316171482Sjeffstatic inline void thread_unblock_switch(struct thread *, struct mtx *); 317171713Sjeffstatic struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int); 318184439Sivorasstatic int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 319184439Sivorasstatic int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 320184439Sivoras struct cpu_group *cg, int indent); 321121790Sjeff#endif 322110028Sjeff 323165762Sjeffstatic void sched_setup(void *dummy); 324177253SrwatsonSYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 325165762Sjeff 326165762Sjeffstatic void sched_initticks(void *dummy); 327177253SrwatsonSYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 328177253Srwatson NULL); 329165762Sjeff 330171482Sjeff/* 331171482Sjeff * Print the threads waiting on a run-queue. 332171482Sjeff */ 333165762Sjeffstatic void 334165762Sjeffrunq_print(struct runq *rq) 335165762Sjeff{ 336165762Sjeff struct rqhead *rqh; 337177435Sjeff struct thread *td; 338165762Sjeff int pri; 339165762Sjeff int j; 340165762Sjeff int i; 341165762Sjeff 342165762Sjeff for (i = 0; i < RQB_LEN; i++) { 343165762Sjeff printf("\t\trunq bits %d 0x%zx\n", 344165762Sjeff i, rq->rq_status.rqb_bits[i]); 345165762Sjeff for (j = 0; j < RQB_BPW; j++) 346165762Sjeff if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 347165762Sjeff pri = j + (i << RQB_L2BPW); 348165762Sjeff rqh = &rq->rq_queues[pri]; 349177435Sjeff TAILQ_FOREACH(td, rqh, td_runq) { 350165762Sjeff printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 351177435Sjeff td, td->td_name, td->td_priority, 352177435Sjeff td->td_rqindex, pri); 353165762Sjeff } 354165762Sjeff } 355165762Sjeff } 356165762Sjeff} 357165762Sjeff 358171482Sjeff/* 359171482Sjeff * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 360171482Sjeff */ 361113357Sjeffvoid 362164936Sjuliantdq_print(int cpu) 363110267Sjeff{ 364164936Sjulian struct tdq *tdq; 365112994Sjeff 366164936Sjulian tdq = TDQ_CPU(cpu); 367112994Sjeff 368171713Sjeff printf("tdq %d:\n", TDQ_ID(tdq)); 369176735Sjeff printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 370176735Sjeff printf("\tLock name: %s\n", tdq->tdq_name); 371165620Sjeff printf("\tload: %d\n", tdq->tdq_load); 372178277Sjeff printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 373178277Sjeff printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 374171482Sjeff printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 375165766Sjeff printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 376178277Sjeff printf("\tload transferable: %d\n", tdq->tdq_transferable); 377178277Sjeff printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 378165762Sjeff printf("\trealtime runq:\n"); 379165762Sjeff runq_print(&tdq->tdq_realtime); 380165762Sjeff printf("\ttimeshare runq:\n"); 381165762Sjeff runq_print(&tdq->tdq_timeshare); 382165762Sjeff printf("\tidle runq:\n"); 383165762Sjeff runq_print(&tdq->tdq_idle); 384113357Sjeff} 385112994Sjeff 386177005Sjeffstatic inline int 387177005Sjeffsched_shouldpreempt(int pri, int cpri, int remote) 388177005Sjeff{ 389177005Sjeff /* 390177005Sjeff * If the new priority is not better than the current priority there is 391177005Sjeff * nothing to do. 392177005Sjeff */ 393177005Sjeff if (pri >= cpri) 394177005Sjeff return (0); 395177005Sjeff /* 396177005Sjeff * Always preempt idle. 397177005Sjeff */ 398177005Sjeff if (cpri >= PRI_MIN_IDLE) 399177005Sjeff return (1); 400177005Sjeff /* 401177005Sjeff * If preemption is disabled don't preempt others. 402177005Sjeff */ 403177005Sjeff if (preempt_thresh == 0) 404177005Sjeff return (0); 405177005Sjeff /* 406177005Sjeff * Preempt if we exceed the threshold. 407177005Sjeff */ 408177005Sjeff if (pri <= preempt_thresh) 409177005Sjeff return (1); 410177005Sjeff /* 411217351Sjhb * If we're interactive or better and there is non-interactive 412217351Sjhb * or worse running preempt only remote processors. 413177005Sjeff */ 414217351Sjhb if (remote && pri <= PRI_MAX_INTERACT && cpri > PRI_MAX_INTERACT) 415177005Sjeff return (1); 416177005Sjeff return (0); 417177005Sjeff} 418177005Sjeff 419217351Sjhb#define TS_RQ_PPQ (((PRI_MAX_BATCH - PRI_MIN_BATCH) + 1) / RQ_NQS) 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) { 452217351Sjhb pri = (pri - PRI_MIN_BATCH) / TS_RQ_PPQ; 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) 1437165762Sjeff pri += SCHED_PRI_TICKS(td->td_sched); 1438165762Sjeff pri += SCHED_PRI_NICE(td->td_proc->p_nice); 1439217351Sjhb KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH, 1440171482Sjeff ("sched_priority: invalid priority %d: nice %d, " 1441171482Sjeff "ticks %d ftick %d ltick %d tick pri %d", 1442171482Sjeff pri, td->td_proc->p_nice, td->td_sched->ts_ticks, 1443171482Sjeff td->td_sched->ts_ftick, td->td_sched->ts_ltick, 1444171482Sjeff SCHED_PRI_TICKS(td->td_sched))); 1445165762Sjeff } 1446165762Sjeff sched_user_prio(td, pri); 1447112966Sjeff 1448112966Sjeff return; 1449109864Sjeff} 1450109864Sjeff 1451121868Sjeff/* 1452121868Sjeff * This routine enforces a maximum limit on the amount of scheduling history 1453171482Sjeff * kept. It is called after either the slptime or runtime is adjusted. This 1454171482Sjeff * function is ugly due to integer math. 1455121868Sjeff */ 1456116463Sjeffstatic void 1457163709Sjbsched_interact_update(struct thread *td) 1458116463Sjeff{ 1459165819Sjeff struct td_sched *ts; 1460166208Sjeff u_int sum; 1461121605Sjeff 1462165819Sjeff ts = td->td_sched; 1463171482Sjeff sum = ts->ts_runtime + ts->ts_slptime; 1464121868Sjeff if (sum < SCHED_SLP_RUN_MAX) 1465121868Sjeff return; 1466121868Sjeff /* 1467165819Sjeff * This only happens from two places: 1468165819Sjeff * 1) We have added an unusual amount of run time from fork_exit. 1469165819Sjeff * 2) We have added an unusual amount of sleep time from sched_sleep(). 1470165819Sjeff */ 1471165819Sjeff if (sum > SCHED_SLP_RUN_MAX * 2) { 1472171482Sjeff if (ts->ts_runtime > ts->ts_slptime) { 1473171482Sjeff ts->ts_runtime = SCHED_SLP_RUN_MAX; 1474171482Sjeff ts->ts_slptime = 1; 1475165819Sjeff } else { 1476171482Sjeff ts->ts_slptime = SCHED_SLP_RUN_MAX; 1477171482Sjeff ts->ts_runtime = 1; 1478165819Sjeff } 1479165819Sjeff return; 1480165819Sjeff } 1481165819Sjeff /* 1482121868Sjeff * If we have exceeded by more than 1/5th then the algorithm below 1483121868Sjeff * will not bring us back into range. Dividing by two here forces 1484133427Sjeff * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1485121868Sjeff */ 1486127850Sjeff if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1487171482Sjeff ts->ts_runtime /= 2; 1488171482Sjeff ts->ts_slptime /= 2; 1489121868Sjeff return; 1490116463Sjeff } 1491171482Sjeff ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1492171482Sjeff ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1493116463Sjeff} 1494116463Sjeff 1495171482Sjeff/* 1496171482Sjeff * Scale back the interactivity history when a child thread is created. The 1497171482Sjeff * history is inherited from the parent but the thread may behave totally 1498171482Sjeff * differently. For example, a shell spawning a compiler process. We want 1499171482Sjeff * to learn that the compiler is behaving badly very quickly. 1500171482Sjeff */ 1501121868Sjeffstatic void 1502163709Sjbsched_interact_fork(struct thread *td) 1503121868Sjeff{ 1504121868Sjeff int ratio; 1505121868Sjeff int sum; 1506121868Sjeff 1507171482Sjeff sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime; 1508121868Sjeff if (sum > SCHED_SLP_RUN_FORK) { 1509121868Sjeff ratio = sum / SCHED_SLP_RUN_FORK; 1510171482Sjeff td->td_sched->ts_runtime /= ratio; 1511171482Sjeff td->td_sched->ts_slptime /= ratio; 1512121868Sjeff } 1513121868Sjeff} 1514121868Sjeff 1515113357Sjeff/* 1516171482Sjeff * Called from proc0_init() to setup the scheduler fields. 1517134791Sjulian */ 1518134791Sjulianvoid 1519134791Sjulianschedinit(void) 1520134791Sjulian{ 1521165762Sjeff 1522134791Sjulian /* 1523134791Sjulian * Set up the scheduler specific parts of proc0. 1524134791Sjulian */ 1525136167Sjulian proc0.p_sched = NULL; /* XXX */ 1526164936Sjulian thread0.td_sched = &td_sched0; 1527165762Sjeff td_sched0.ts_ltick = ticks; 1528165796Sjeff td_sched0.ts_ftick = ticks; 1529177009Sjeff td_sched0.ts_slice = sched_slice; 1530134791Sjulian} 1531134791Sjulian 1532134791Sjulian/* 1533113357Sjeff * This is only somewhat accurate since given many processes of the same 1534113357Sjeff * priority they will switch when their slices run out, which will be 1535165762Sjeff * at most sched_slice stathz ticks. 1536113357Sjeff */ 1537109864Sjeffint 1538109864Sjeffsched_rr_interval(void) 1539109864Sjeff{ 1540165762Sjeff 1541165762Sjeff /* Convert sched_slice to hz */ 1542165762Sjeff return (hz/(realstathz/sched_slice)); 1543109864Sjeff} 1544109864Sjeff 1545171482Sjeff/* 1546171482Sjeff * Update the percent cpu tracking information when it is requested or 1547171482Sjeff * the total history exceeds the maximum. We keep a sliding history of 1548171482Sjeff * tick counts that slowly decays. This is less precise than the 4BSD 1549171482Sjeff * mechanism since it happens with less regular and frequent events. 1550171482Sjeff */ 1551121790Sjeffstatic void 1552164936Sjuliansched_pctcpu_update(struct td_sched *ts) 1553109864Sjeff{ 1554165762Sjeff 1555165762Sjeff if (ts->ts_ticks == 0) 1556165762Sjeff return; 1557165796Sjeff if (ticks - (hz / 10) < ts->ts_ltick && 1558165796Sjeff SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX) 1559165796Sjeff return; 1560109864Sjeff /* 1561109864Sjeff * Adjust counters and watermark for pctcpu calc. 1562116365Sjeff */ 1563165762Sjeff if (ts->ts_ltick > ticks - SCHED_TICK_TARG) 1564164936Sjulian ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) * 1565165762Sjeff SCHED_TICK_TARG; 1566165762Sjeff else 1567164936Sjulian ts->ts_ticks = 0; 1568164936Sjulian ts->ts_ltick = ticks; 1569165762Sjeff ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG; 1570109864Sjeff} 1571109864Sjeff 1572171482Sjeff/* 1573171482Sjeff * Adjust the priority of a thread. Move it to the appropriate run-queue 1574171482Sjeff * if necessary. This is the back-end for several priority related 1575171482Sjeff * functions. 1576171482Sjeff */ 1577165762Sjeffstatic void 1578139453Sjhbsched_thread_priority(struct thread *td, u_char prio) 1579109864Sjeff{ 1580164936Sjulian struct td_sched *ts; 1581177009Sjeff struct tdq *tdq; 1582177009Sjeff int oldpri; 1583109864Sjeff 1584187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 1585187357Sjeff "prio:%d", td->td_priority, "new prio:%d", prio, 1586187357Sjeff KTR_ATTR_LINKED, sched_tdname(curthread)); 1587187357Sjeff if (td != curthread && prio > td->td_priority) { 1588187357Sjeff KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 1589187357Sjeff "lend prio", "prio:%d", td->td_priority, "new prio:%d", 1590187357Sjeff prio, KTR_ATTR_LINKED, sched_tdname(td)); 1591187357Sjeff } 1592164936Sjulian ts = td->td_sched; 1593170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1594139453Sjhb if (td->td_priority == prio) 1595139453Sjhb return; 1596177376Sjeff /* 1597177376Sjeff * If the priority has been elevated due to priority 1598177376Sjeff * propagation, we may have to move ourselves to a new 1599177376Sjeff * queue. This could be optimized to not re-add in some 1600177376Sjeff * cases. 1601177376Sjeff */ 1602165766Sjeff if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1603165762Sjeff sched_rem(td); 1604165762Sjeff td->td_priority = prio; 1605171482Sjeff sched_add(td, SRQ_BORROWING); 1606177009Sjeff return; 1607177009Sjeff } 1608177376Sjeff /* 1609177376Sjeff * If the thread is currently running we may have to adjust the lowpri 1610177376Sjeff * information so other cpus are aware of our current priority. 1611177376Sjeff */ 1612177009Sjeff if (TD_IS_RUNNING(td)) { 1613177376Sjeff tdq = TDQ_CPU(ts->ts_cpu); 1614177376Sjeff oldpri = td->td_priority; 1615177376Sjeff td->td_priority = prio; 1616176735Sjeff if (prio < tdq->tdq_lowpri) 1617171482Sjeff tdq->tdq_lowpri = prio; 1618176735Sjeff else if (tdq->tdq_lowpri == oldpri) 1619176735Sjeff tdq_setlowpri(tdq, td); 1620177376Sjeff return; 1621177009Sjeff } 1622177376Sjeff td->td_priority = prio; 1623109864Sjeff} 1624109864Sjeff 1625139453Sjhb/* 1626139453Sjhb * Update a thread's priority when it is lent another thread's 1627139453Sjhb * priority. 1628139453Sjhb */ 1629109864Sjeffvoid 1630139453Sjhbsched_lend_prio(struct thread *td, u_char prio) 1631139453Sjhb{ 1632139453Sjhb 1633139453Sjhb td->td_flags |= TDF_BORROWING; 1634139453Sjhb sched_thread_priority(td, prio); 1635139453Sjhb} 1636139453Sjhb 1637139453Sjhb/* 1638139453Sjhb * Restore a thread's priority when priority propagation is 1639139453Sjhb * over. The prio argument is the minimum priority the thread 1640139453Sjhb * needs to have to satisfy other possible priority lending 1641139453Sjhb * requests. If the thread's regular priority is less 1642139453Sjhb * important than prio, the thread will keep a priority boost 1643139453Sjhb * of prio. 1644139453Sjhb */ 1645139453Sjhbvoid 1646139453Sjhbsched_unlend_prio(struct thread *td, u_char prio) 1647139453Sjhb{ 1648139453Sjhb u_char base_pri; 1649139453Sjhb 1650139453Sjhb if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1651139453Sjhb td->td_base_pri <= PRI_MAX_TIMESHARE) 1652163709Sjb base_pri = td->td_user_pri; 1653139453Sjhb else 1654139453Sjhb base_pri = td->td_base_pri; 1655139453Sjhb if (prio >= base_pri) { 1656139455Sjhb td->td_flags &= ~TDF_BORROWING; 1657139453Sjhb sched_thread_priority(td, base_pri); 1658139453Sjhb } else 1659139453Sjhb sched_lend_prio(td, prio); 1660139453Sjhb} 1661139453Sjhb 1662171482Sjeff/* 1663171482Sjeff * Standard entry for setting the priority to an absolute value. 1664171482Sjeff */ 1665139453Sjhbvoid 1666139453Sjhbsched_prio(struct thread *td, u_char prio) 1667139453Sjhb{ 1668139453Sjhb u_char oldprio; 1669139453Sjhb 1670139453Sjhb /* First, update the base priority. */ 1671139453Sjhb td->td_base_pri = prio; 1672139453Sjhb 1673139453Sjhb /* 1674139455Sjhb * If the thread is borrowing another thread's priority, don't 1675139453Sjhb * ever lower the priority. 1676139453Sjhb */ 1677139453Sjhb if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1678139453Sjhb return; 1679139453Sjhb 1680139453Sjhb /* Change the real priority. */ 1681139453Sjhb oldprio = td->td_priority; 1682139453Sjhb sched_thread_priority(td, prio); 1683139453Sjhb 1684139453Sjhb /* 1685139453Sjhb * If the thread is on a turnstile, then let the turnstile update 1686139453Sjhb * its state. 1687139453Sjhb */ 1688139453Sjhb if (TD_ON_LOCK(td) && oldprio != prio) 1689139453Sjhb turnstile_adjust(td, oldprio); 1690139453Sjhb} 1691139455Sjhb 1692171482Sjeff/* 1693171482Sjeff * Set the base user priority, does not effect current running priority. 1694171482Sjeff */ 1695139453Sjhbvoid 1696163709Sjbsched_user_prio(struct thread *td, u_char prio) 1697161599Sdavidxu{ 1698161599Sdavidxu 1699163709Sjb td->td_base_user_pri = prio; 1700216313Sdavidxu if (td->td_lend_user_pri <= prio) 1701216313Sdavidxu return; 1702163709Sjb td->td_user_pri = prio; 1703161599Sdavidxu} 1704161599Sdavidxu 1705161599Sdavidxuvoid 1706161599Sdavidxusched_lend_user_prio(struct thread *td, u_char prio) 1707161599Sdavidxu{ 1708161599Sdavidxu 1709174536Sdavidxu THREAD_LOCK_ASSERT(td, MA_OWNED); 1710216313Sdavidxu td->td_lend_user_pri = prio; 1711216791Sdavidxu td->td_user_pri = min(prio, td->td_base_user_pri); 1712216791Sdavidxu if (td->td_priority > td->td_user_pri) 1713216791Sdavidxu sched_prio(td, td->td_user_pri); 1714216791Sdavidxu else if (td->td_priority != td->td_user_pri) 1715216791Sdavidxu td->td_flags |= TDF_NEEDRESCHED; 1716161599Sdavidxu} 1717161599Sdavidxu 1718171482Sjeff/* 1719171713Sjeff * Handle migration from sched_switch(). This happens only for 1720171713Sjeff * cpu binding. 1721171713Sjeff */ 1722171713Sjeffstatic struct mtx * 1723171713Sjeffsched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1724171713Sjeff{ 1725171713Sjeff struct tdq *tdn; 1726171713Sjeff 1727171713Sjeff tdn = TDQ_CPU(td->td_sched->ts_cpu); 1728171713Sjeff#ifdef SMP 1729177435Sjeff tdq_load_rem(tdq, td); 1730171713Sjeff /* 1731171713Sjeff * Do the lock dance required to avoid LOR. We grab an extra 1732171713Sjeff * spinlock nesting to prevent preemption while we're 1733171713Sjeff * not holding either run-queue lock. 1734171713Sjeff */ 1735171713Sjeff spinlock_enter(); 1736202889Sattilio thread_lock_block(td); /* This releases the lock on tdq. */ 1737197223Sattilio 1738197223Sattilio /* 1739197223Sattilio * Acquire both run-queue locks before placing the thread on the new 1740197223Sattilio * run-queue to avoid deadlocks created by placing a thread with a 1741197223Sattilio * blocked lock on the run-queue of a remote processor. The deadlock 1742197223Sattilio * occurs when a third processor attempts to lock the two queues in 1743197223Sattilio * question while the target processor is spinning with its own 1744197223Sattilio * run-queue lock held while waiting for the blocked lock to clear. 1745197223Sattilio */ 1746197223Sattilio tdq_lock_pair(tdn, tdq); 1747171713Sjeff tdq_add(tdn, td, flags); 1748177435Sjeff tdq_notify(tdn, td); 1749197223Sattilio TDQ_UNLOCK(tdn); 1750171713Sjeff spinlock_exit(); 1751171713Sjeff#endif 1752171713Sjeff return (TDQ_LOCKPTR(tdn)); 1753171713Sjeff} 1754171713Sjeff 1755171713Sjeff/* 1756202889Sattilio * Variadic version of thread_lock_unblock() that does not assume td_lock 1757202889Sattilio * is blocked. 1758171482Sjeff */ 1759171482Sjeffstatic inline void 1760171482Sjeffthread_unblock_switch(struct thread *td, struct mtx *mtx) 1761171482Sjeff{ 1762171482Sjeff atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1763171482Sjeff (uintptr_t)mtx); 1764171482Sjeff} 1765171482Sjeff 1766171482Sjeff/* 1767171482Sjeff * Switch threads. This function has to handle threads coming in while 1768171482Sjeff * blocked for some reason, running, or idle. It also must deal with 1769171482Sjeff * migrating a thread from one queue to another as running threads may 1770171482Sjeff * be assigned elsewhere via binding. 1771171482Sjeff */ 1772161599Sdavidxuvoid 1773135051Sjuliansched_switch(struct thread *td, struct thread *newtd, int flags) 1774109864Sjeff{ 1775165627Sjeff struct tdq *tdq; 1776164936Sjulian struct td_sched *ts; 1777171482Sjeff struct mtx *mtx; 1778171713Sjeff int srqflag; 1779171482Sjeff int cpuid; 1780109864Sjeff 1781170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1782177376Sjeff KASSERT(newtd == NULL, ("sched_switch: Unsupported newtd argument")); 1783109864Sjeff 1784171482Sjeff cpuid = PCPU_GET(cpuid); 1785171482Sjeff tdq = TDQ_CPU(cpuid); 1786164936Sjulian ts = td->td_sched; 1787171713Sjeff mtx = td->td_lock; 1788171482Sjeff ts->ts_rltick = ticks; 1789133555Sjeff td->td_lastcpu = td->td_oncpu; 1790113339Sjulian td->td_oncpu = NOCPU; 1791220198Sfabient if (!(flags & SW_PREEMPT)) 1792220198Sfabient td->td_flags &= ~TDF_NEEDRESCHED; 1793144777Sups td->td_owepreempt = 0; 1794178277Sjeff tdq->tdq_switchcnt++; 1795123434Sjeff /* 1796171482Sjeff * The lock pointer in an idle thread should never change. Reset it 1797171482Sjeff * to CAN_RUN as well. 1798123434Sjeff */ 1799167327Sjulian if (TD_IS_IDLETHREAD(td)) { 1800171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1801139334Sjeff TD_SET_CAN_RUN(td); 1802170293Sjeff } else if (TD_IS_RUNNING(td)) { 1803171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1804171713Sjeff srqflag = (flags & SW_PREEMPT) ? 1805170293Sjeff SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1806171713Sjeff SRQ_OURSELF|SRQ_YIELDING; 1807212153Smdf#ifdef SMP 1808212115Smdf if (THREAD_CAN_MIGRATE(td) && !THREAD_CAN_SCHED(td, ts->ts_cpu)) 1809212115Smdf ts->ts_cpu = sched_pickcpu(td, 0); 1810212153Smdf#endif 1811171713Sjeff if (ts->ts_cpu == cpuid) 1812177435Sjeff tdq_runq_add(tdq, td, srqflag); 1813212115Smdf else { 1814212115Smdf KASSERT(THREAD_CAN_MIGRATE(td) || 1815212115Smdf (ts->ts_flags & TSF_BOUND) != 0, 1816212115Smdf ("Thread %p shouldn't migrate", td)); 1817171713Sjeff mtx = sched_switch_migrate(tdq, td, srqflag); 1818212115Smdf } 1819171482Sjeff } else { 1820171482Sjeff /* This thread must be going to sleep. */ 1821171482Sjeff TDQ_LOCK(tdq); 1822202889Sattilio mtx = thread_lock_block(td); 1823177435Sjeff tdq_load_rem(tdq, td); 1824171482Sjeff } 1825171482Sjeff /* 1826171482Sjeff * We enter here with the thread blocked and assigned to the 1827171482Sjeff * appropriate cpu run-queue or sleep-queue and with the current 1828171482Sjeff * thread-queue locked. 1829171482Sjeff */ 1830171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 1831171482Sjeff newtd = choosethread(); 1832171482Sjeff /* 1833171482Sjeff * Call the MD code to switch contexts if necessary. 1834171482Sjeff */ 1835145256Sjkoshy if (td != newtd) { 1836145256Sjkoshy#ifdef HWPMC_HOOKS 1837145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1838145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1839145256Sjkoshy#endif 1840174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 1841172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 1842179297Sjb 1843179297Sjb#ifdef KDTRACE_HOOKS 1844179297Sjb /* 1845179297Sjb * If DTrace has set the active vtime enum to anything 1846179297Sjb * other than INACTIVE (0), then it should have set the 1847179297Sjb * function to call. 1848179297Sjb */ 1849179297Sjb if (dtrace_vtime_active) 1850179297Sjb (*dtrace_vtime_switch_func)(newtd); 1851179297Sjb#endif 1852179297Sjb 1853171482Sjeff cpu_switch(td, newtd, mtx); 1854171482Sjeff /* 1855171482Sjeff * We may return from cpu_switch on a different cpu. However, 1856171482Sjeff * we always return with td_lock pointing to the current cpu's 1857171482Sjeff * run queue lock. 1858171482Sjeff */ 1859171482Sjeff cpuid = PCPU_GET(cpuid); 1860171482Sjeff tdq = TDQ_CPU(cpuid); 1861174629Sjeff lock_profile_obtain_lock_success( 1862174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 1863145256Sjkoshy#ifdef HWPMC_HOOKS 1864145256Sjkoshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1865145256Sjkoshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1866145256Sjkoshy#endif 1867171482Sjeff } else 1868171482Sjeff thread_unblock_switch(td, mtx); 1869171482Sjeff /* 1870171482Sjeff * Assert that all went well and return. 1871171482Sjeff */ 1872171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED); 1873171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 1874171482Sjeff td->td_oncpu = cpuid; 1875109864Sjeff} 1876109864Sjeff 1877171482Sjeff/* 1878171482Sjeff * Adjust thread priorities as a result of a nice request. 1879171482Sjeff */ 1880109864Sjeffvoid 1881130551Sjuliansched_nice(struct proc *p, int nice) 1882109864Sjeff{ 1883109864Sjeff struct thread *td; 1884109864Sjeff 1885130551Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 1886165762Sjeff 1887130551Sjulian p->p_nice = nice; 1888163709Sjb FOREACH_THREAD_IN_PROC(p, td) { 1889170293Sjeff thread_lock(td); 1890163709Sjb sched_priority(td); 1891165762Sjeff sched_prio(td, td->td_base_user_pri); 1892170293Sjeff thread_unlock(td); 1893130551Sjulian } 1894109864Sjeff} 1895109864Sjeff 1896171482Sjeff/* 1897171482Sjeff * Record the sleep time for the interactivity scorer. 1898171482Sjeff */ 1899109864Sjeffvoid 1900177085Sjeffsched_sleep(struct thread *td, int prio) 1901109864Sjeff{ 1902165762Sjeff 1903170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1904109864Sjeff 1905172264Sjeff td->td_slptick = ticks; 1906201347Skib if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 1907177085Sjeff td->td_flags |= TDF_CANSWAP; 1908217410Sjhb if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 1909217410Sjhb return; 1910177903Sjeff if (static_boost == 1 && prio) 1911177085Sjeff sched_prio(td, prio); 1912177903Sjeff else if (static_boost && td->td_priority > static_boost) 1913177903Sjeff sched_prio(td, static_boost); 1914109864Sjeff} 1915109864Sjeff 1916171482Sjeff/* 1917171482Sjeff * Schedule a thread to resume execution and record how long it voluntarily 1918171482Sjeff * slept. We also update the pctcpu, interactivity, and priority. 1919171482Sjeff */ 1920109864Sjeffvoid 1921109864Sjeffsched_wakeup(struct thread *td) 1922109864Sjeff{ 1923166229Sjeff struct td_sched *ts; 1924171482Sjeff int slptick; 1925165762Sjeff 1926170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1927166229Sjeff ts = td->td_sched; 1928177085Sjeff td->td_flags &= ~TDF_CANSWAP; 1929109864Sjeff /* 1930165762Sjeff * If we slept for more than a tick update our interactivity and 1931165762Sjeff * priority. 1932109864Sjeff */ 1933172264Sjeff slptick = td->td_slptick; 1934172264Sjeff td->td_slptick = 0; 1935171482Sjeff if (slptick && slptick != ticks) { 1936166208Sjeff u_int hzticks; 1937109864Sjeff 1938171482Sjeff hzticks = (ticks - slptick) << SCHED_TICK_SHIFT; 1939171482Sjeff ts->ts_slptime += hzticks; 1940165819Sjeff sched_interact_update(td); 1941166229Sjeff sched_pctcpu_update(ts); 1942109864Sjeff } 1943166229Sjeff /* Reset the slice value after we sleep. */ 1944166229Sjeff ts->ts_slice = sched_slice; 1945166190Sjeff sched_add(td, SRQ_BORING); 1946109864Sjeff} 1947109864Sjeff 1948109864Sjeff/* 1949109864Sjeff * Penalize the parent for creating a new child and initialize the child's 1950109864Sjeff * priority. 1951109864Sjeff */ 1952109864Sjeffvoid 1953163709Sjbsched_fork(struct thread *td, struct thread *child) 1954109864Sjeff{ 1955170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1956164936Sjulian sched_fork_thread(td, child); 1957165762Sjeff /* 1958165762Sjeff * Penalize the parent and child for forking. 1959165762Sjeff */ 1960165762Sjeff sched_interact_fork(child); 1961165762Sjeff sched_priority(child); 1962171482Sjeff td->td_sched->ts_runtime += tickincr; 1963165762Sjeff sched_interact_update(td); 1964165762Sjeff sched_priority(td); 1965164936Sjulian} 1966109864Sjeff 1967171482Sjeff/* 1968171482Sjeff * Fork a new thread, may be within the same process. 1969171482Sjeff */ 1970164936Sjulianvoid 1971164936Sjuliansched_fork_thread(struct thread *td, struct thread *child) 1972164936Sjulian{ 1973164936Sjulian struct td_sched *ts; 1974164936Sjulian struct td_sched *ts2; 1975164936Sjulian 1976177426Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1977165762Sjeff /* 1978165762Sjeff * Initialize child. 1979165762Sjeff */ 1980177426Sjeff ts = td->td_sched; 1981177426Sjeff ts2 = child->td_sched; 1982171482Sjeff child->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1983176735Sjeff child->td_cpuset = cpuset_ref(td->td_cpuset); 1984164936Sjulian ts2->ts_cpu = ts->ts_cpu; 1985177426Sjeff ts2->ts_flags = 0; 1986165762Sjeff /* 1987217078Sjhb * Grab our parents cpu estimation information. 1988165762Sjeff */ 1989164936Sjulian ts2->ts_ticks = ts->ts_ticks; 1990164936Sjulian ts2->ts_ltick = ts->ts_ltick; 1991199764Sivoras ts2->ts_incrtick = ts->ts_incrtick; 1992164936Sjulian ts2->ts_ftick = ts->ts_ftick; 1993165762Sjeff /* 1994217078Sjhb * Do not inherit any borrowed priority from the parent. 1995217078Sjhb */ 1996217078Sjhb child->td_priority = child->td_base_pri; 1997217078Sjhb /* 1998165762Sjeff * And update interactivity score. 1999165762Sjeff */ 2000171482Sjeff ts2->ts_slptime = ts->ts_slptime; 2001171482Sjeff ts2->ts_runtime = ts->ts_runtime; 2002165762Sjeff ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 2003187357Sjeff#ifdef KTR 2004187357Sjeff bzero(ts2->ts_name, sizeof(ts2->ts_name)); 2005187357Sjeff#endif 2006113357Sjeff} 2007113357Sjeff 2008171482Sjeff/* 2009171482Sjeff * Adjust the priority class of a thread. 2010171482Sjeff */ 2011113357Sjeffvoid 2012163709Sjbsched_class(struct thread *td, int class) 2013113357Sjeff{ 2014113357Sjeff 2015170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2016163709Sjb if (td->td_pri_class == class) 2017113357Sjeff return; 2018163709Sjb td->td_pri_class = class; 2019109864Sjeff} 2020109864Sjeff 2021109864Sjeff/* 2022109864Sjeff * Return some of the child's priority and interactivity to the parent. 2023109864Sjeff */ 2024109864Sjeffvoid 2025164939Sjuliansched_exit(struct proc *p, struct thread *child) 2026109864Sjeff{ 2027165762Sjeff struct thread *td; 2028113372Sjeff 2029187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2030225199Sdelphij "prio:%d", child->td_priority); 2031177368Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 2032165762Sjeff td = FIRST_THREAD_IN_PROC(p); 2033165762Sjeff sched_exit_thread(td, child); 2034113372Sjeff} 2035113372Sjeff 2036171482Sjeff/* 2037171482Sjeff * Penalize another thread for the time spent on this one. This helps to 2038171482Sjeff * worsen the priority and interactivity of processes which schedule batch 2039171482Sjeff * jobs such as make. This has little effect on the make process itself but 2040171482Sjeff * causes new processes spawned by it to receive worse scores immediately. 2041171482Sjeff */ 2042113372Sjeffvoid 2043164939Sjuliansched_exit_thread(struct thread *td, struct thread *child) 2044164936Sjulian{ 2045165762Sjeff 2046187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2047225199Sdelphij "prio:%d", child->td_priority); 2048165762Sjeff /* 2049165762Sjeff * Give the child's runtime to the parent without returning the 2050165762Sjeff * sleep time as a penalty to the parent. This causes shells that 2051165762Sjeff * launch expensive things to mark their children as expensive. 2052165762Sjeff */ 2053170293Sjeff thread_lock(td); 2054171482Sjeff td->td_sched->ts_runtime += child->td_sched->ts_runtime; 2055164939Sjulian sched_interact_update(td); 2056165762Sjeff sched_priority(td); 2057170293Sjeff thread_unlock(td); 2058164936Sjulian} 2059164936Sjulian 2060177005Sjeffvoid 2061177005Sjeffsched_preempt(struct thread *td) 2062177005Sjeff{ 2063177005Sjeff struct tdq *tdq; 2064177005Sjeff 2065177005Sjeff thread_lock(td); 2066177005Sjeff tdq = TDQ_SELF(); 2067177005Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2068177005Sjeff tdq->tdq_ipipending = 0; 2069177005Sjeff if (td->td_priority > tdq->tdq_lowpri) { 2070178272Sjeff int flags; 2071178272Sjeff 2072178272Sjeff flags = SW_INVOL | SW_PREEMPT; 2073177005Sjeff if (td->td_critnest > 1) 2074177005Sjeff td->td_owepreempt = 1; 2075178272Sjeff else if (TD_IS_IDLETHREAD(td)) 2076178272Sjeff mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL); 2077177005Sjeff else 2078178272Sjeff mi_switch(flags | SWT_REMOTEPREEMPT, NULL); 2079177005Sjeff } 2080177005Sjeff thread_unlock(td); 2081177005Sjeff} 2082177005Sjeff 2083171482Sjeff/* 2084171482Sjeff * Fix priorities on return to user-space. Priorities may be elevated due 2085171482Sjeff * to static priorities in msleep() or similar. 2086171482Sjeff */ 2087164936Sjulianvoid 2088164936Sjuliansched_userret(struct thread *td) 2089164936Sjulian{ 2090164936Sjulian /* 2091164936Sjulian * XXX we cheat slightly on the locking here to avoid locking in 2092164936Sjulian * the usual case. Setting td_priority here is essentially an 2093164936Sjulian * incomplete workaround for not setting it properly elsewhere. 2094164936Sjulian * Now that some interrupt handlers are threads, not setting it 2095164936Sjulian * properly elsewhere can clobber it in the window between setting 2096164936Sjulian * it here and returning to user mode, so don't waste time setting 2097164936Sjulian * it perfectly here. 2098164936Sjulian */ 2099164936Sjulian KASSERT((td->td_flags & TDF_BORROWING) == 0, 2100164936Sjulian ("thread with borrowed priority returning to userland")); 2101164936Sjulian if (td->td_priority != td->td_user_pri) { 2102170293Sjeff thread_lock(td); 2103164936Sjulian td->td_priority = td->td_user_pri; 2104164936Sjulian td->td_base_pri = td->td_user_pri; 2105177005Sjeff tdq_setlowpri(TDQ_SELF(), td); 2106170293Sjeff thread_unlock(td); 2107164936Sjulian } 2108164936Sjulian} 2109164936Sjulian 2110171482Sjeff/* 2111171482Sjeff * Handle a stathz tick. This is really only relevant for timeshare 2112171482Sjeff * threads. 2113171482Sjeff */ 2114164936Sjulianvoid 2115121127Sjeffsched_clock(struct thread *td) 2116109864Sjeff{ 2117164936Sjulian struct tdq *tdq; 2118164936Sjulian struct td_sched *ts; 2119109864Sjeff 2120171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2121164936Sjulian tdq = TDQ_SELF(); 2122172409Sjeff#ifdef SMP 2123133427Sjeff /* 2124172409Sjeff * We run the long term load balancer infrequently on the first cpu. 2125172409Sjeff */ 2126172409Sjeff if (balance_tdq == tdq) { 2127172409Sjeff if (balance_ticks && --balance_ticks == 0) 2128172409Sjeff sched_balance(); 2129172409Sjeff } 2130172409Sjeff#endif 2131172409Sjeff /* 2132178277Sjeff * Save the old switch count so we have a record of the last ticks 2133178277Sjeff * activity. Initialize the new switch count based on our load. 2134178277Sjeff * If there is some activity seed it to reflect that. 2135178277Sjeff */ 2136178277Sjeff tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 2137178471Sjeff tdq->tdq_switchcnt = tdq->tdq_load; 2138178277Sjeff /* 2139165766Sjeff * Advance the insert index once for each tick to ensure that all 2140165766Sjeff * threads get a chance to run. 2141133427Sjeff */ 2142165766Sjeff if (tdq->tdq_idx == tdq->tdq_ridx) { 2143165766Sjeff tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 2144165766Sjeff if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 2145165766Sjeff tdq->tdq_ridx = tdq->tdq_idx; 2146165766Sjeff } 2147165766Sjeff ts = td->td_sched; 2148175104Sjeff if (td->td_pri_class & PRI_FIFO_BIT) 2149113357Sjeff return; 2150217291Sjhb if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) { 2151175104Sjeff /* 2152175104Sjeff * We used a tick; charge it to the thread so 2153175104Sjeff * that we can compute our interactivity. 2154175104Sjeff */ 2155175104Sjeff td->td_sched->ts_runtime += tickincr; 2156175104Sjeff sched_interact_update(td); 2157177009Sjeff sched_priority(td); 2158175104Sjeff } 2159113357Sjeff /* 2160109864Sjeff * We used up one time slice. 2161109864Sjeff */ 2162164936Sjulian if (--ts->ts_slice > 0) 2163113357Sjeff return; 2164109864Sjeff /* 2165177009Sjeff * We're out of time, force a requeue at userret(). 2166109864Sjeff */ 2167177009Sjeff ts->ts_slice = sched_slice; 2168113357Sjeff td->td_flags |= TDF_NEEDRESCHED; 2169109864Sjeff} 2170109864Sjeff 2171171482Sjeff/* 2172171482Sjeff * Called once per hz tick. Used for cpu utilization information. This 2173171482Sjeff * is easier than trying to scale based on stathz. 2174171482Sjeff */ 2175171482Sjeffvoid 2176212541Smavsched_tick(int cnt) 2177171482Sjeff{ 2178171482Sjeff struct td_sched *ts; 2179171482Sjeff 2180171482Sjeff ts = curthread->td_sched; 2181180607Sjeff /* 2182180607Sjeff * Ticks is updated asynchronously on a single cpu. Check here to 2183180607Sjeff * avoid incrementing ts_ticks multiple times in a single tick. 2184180607Sjeff */ 2185199764Sivoras if (ts->ts_incrtick == ticks) 2186180607Sjeff return; 2187171482Sjeff /* Adjust ticks for pctcpu */ 2188212541Smav ts->ts_ticks += cnt << SCHED_TICK_SHIFT; 2189171482Sjeff ts->ts_ltick = ticks; 2190199764Sivoras ts->ts_incrtick = ticks; 2191171482Sjeff /* 2192215102Sattilio * Update if we've exceeded our desired tick threshold by over one 2193171482Sjeff * second. 2194171482Sjeff */ 2195171482Sjeff if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick) 2196171482Sjeff sched_pctcpu_update(ts); 2197171482Sjeff} 2198171482Sjeff 2199171482Sjeff/* 2200171482Sjeff * Return whether the current CPU has runnable tasks. Used for in-kernel 2201171482Sjeff * cooperative idle threads. 2202171482Sjeff */ 2203109864Sjeffint 2204109864Sjeffsched_runnable(void) 2205109864Sjeff{ 2206164936Sjulian struct tdq *tdq; 2207115998Sjeff int load; 2208109864Sjeff 2209115998Sjeff load = 1; 2210115998Sjeff 2211164936Sjulian tdq = TDQ_SELF(); 2212121605Sjeff if ((curthread->td_flags & TDF_IDLETD) != 0) { 2213165620Sjeff if (tdq->tdq_load > 0) 2214121605Sjeff goto out; 2215121605Sjeff } else 2216165620Sjeff if (tdq->tdq_load - 1 > 0) 2217121605Sjeff goto out; 2218115998Sjeff load = 0; 2219115998Sjeffout: 2220115998Sjeff return (load); 2221109864Sjeff} 2222109864Sjeff 2223171482Sjeff/* 2224171482Sjeff * Choose the highest priority thread to run. The thread is removed from 2225171482Sjeff * the run-queue while running however the load remains. For SMP we set 2226171482Sjeff * the tdq in the global idle bitmask if it idles here. 2227171482Sjeff */ 2228166190Sjeffstruct thread * 2229109970Sjeffsched_choose(void) 2230109970Sjeff{ 2231177435Sjeff struct thread *td; 2232164936Sjulian struct tdq *tdq; 2233109970Sjeff 2234164936Sjulian tdq = TDQ_SELF(); 2235171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2236177435Sjeff td = tdq_choose(tdq); 2237177435Sjeff if (td) { 2238177435Sjeff td->td_sched->ts_ltick = ticks; 2239177435Sjeff tdq_runq_rem(tdq, td); 2240177903Sjeff tdq->tdq_lowpri = td->td_priority; 2241177435Sjeff return (td); 2242109864Sjeff } 2243177903Sjeff tdq->tdq_lowpri = PRI_MAX_IDLE; 2244176735Sjeff return (PCPU_GET(idlethread)); 2245109864Sjeff} 2246109864Sjeff 2247171482Sjeff/* 2248171482Sjeff * Set owepreempt if necessary. Preemption never happens directly in ULE, 2249171482Sjeff * we always request it once we exit a critical section. 2250171482Sjeff */ 2251171482Sjeffstatic inline void 2252171482Sjeffsched_setpreempt(struct thread *td) 2253166190Sjeff{ 2254166190Sjeff struct thread *ctd; 2255166190Sjeff int cpri; 2256166190Sjeff int pri; 2257166190Sjeff 2258177005Sjeff THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2259177005Sjeff 2260166190Sjeff ctd = curthread; 2261166190Sjeff pri = td->td_priority; 2262166190Sjeff cpri = ctd->td_priority; 2263177005Sjeff if (pri < cpri) 2264177005Sjeff ctd->td_flags |= TDF_NEEDRESCHED; 2265166190Sjeff if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2266171482Sjeff return; 2267177005Sjeff if (!sched_shouldpreempt(pri, cpri, 0)) 2268171482Sjeff return; 2269171482Sjeff ctd->td_owepreempt = 1; 2270166190Sjeff} 2271166190Sjeff 2272171482Sjeff/* 2273177009Sjeff * Add a thread to a thread queue. Select the appropriate runq and add the 2274177009Sjeff * thread to it. This is the internal function called when the tdq is 2275177009Sjeff * predetermined. 2276171482Sjeff */ 2277109864Sjeffvoid 2278171482Sjefftdq_add(struct tdq *tdq, struct thread *td, int flags) 2279109864Sjeff{ 2280109864Sjeff 2281171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2282166190Sjeff KASSERT((td->td_inhibitors == 0), 2283166190Sjeff ("sched_add: trying to run inhibited thread")); 2284166190Sjeff KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 2285166190Sjeff ("sched_add: bad thread state")); 2286172207Sjeff KASSERT(td->td_flags & TDF_INMEM, 2287172207Sjeff ("sched_add: thread swapped out")); 2288171482Sjeff 2289171482Sjeff if (td->td_priority < tdq->tdq_lowpri) 2290171482Sjeff tdq->tdq_lowpri = td->td_priority; 2291177435Sjeff tdq_runq_add(tdq, td, flags); 2292177435Sjeff tdq_load_add(tdq, td); 2293171482Sjeff} 2294171482Sjeff 2295171482Sjeff/* 2296171482Sjeff * Select the target thread queue and add a thread to it. Request 2297171482Sjeff * preemption or IPI a remote processor if required. 2298171482Sjeff */ 2299171482Sjeffvoid 2300171482Sjeffsched_add(struct thread *td, int flags) 2301171482Sjeff{ 2302171482Sjeff struct tdq *tdq; 2303171482Sjeff#ifdef SMP 2304171482Sjeff int cpu; 2305171482Sjeff#endif 2306187357Sjeff 2307187357Sjeff KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 2308187357Sjeff "prio:%d", td->td_priority, KTR_ATTR_LINKED, 2309187357Sjeff sched_tdname(curthread)); 2310187357Sjeff KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 2311187357Sjeff KTR_ATTR_LINKED, sched_tdname(td)); 2312171482Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2313166108Sjeff /* 2314171482Sjeff * Recalculate the priority before we select the target cpu or 2315171482Sjeff * run-queue. 2316166108Sjeff */ 2317171482Sjeff if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2318171482Sjeff sched_priority(td); 2319171482Sjeff#ifdef SMP 2320171482Sjeff /* 2321171482Sjeff * Pick the destination cpu and if it isn't ours transfer to the 2322171482Sjeff * target cpu. 2323171482Sjeff */ 2324177435Sjeff cpu = sched_pickcpu(td, flags); 2325177435Sjeff tdq = sched_setcpu(td, cpu, flags); 2326171482Sjeff tdq_add(tdq, td, flags); 2327177009Sjeff if (cpu != PCPU_GET(cpuid)) { 2328177435Sjeff tdq_notify(tdq, td); 2329166108Sjeff return; 2330166108Sjeff } 2331171482Sjeff#else 2332171482Sjeff tdq = TDQ_SELF(); 2333171482Sjeff TDQ_LOCK(tdq); 2334171482Sjeff /* 2335171482Sjeff * Now that the thread is moving to the run-queue, set the lock 2336171482Sjeff * to the scheduler's lock. 2337171482Sjeff */ 2338171482Sjeff thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2339171482Sjeff tdq_add(tdq, td, flags); 2340166108Sjeff#endif 2341171482Sjeff if (!(flags & SRQ_YIELDING)) 2342171482Sjeff sched_setpreempt(td); 2343109864Sjeff} 2344109864Sjeff 2345171482Sjeff/* 2346171482Sjeff * Remove a thread from a run-queue without running it. This is used 2347171482Sjeff * when we're stealing a thread from a remote queue. Otherwise all threads 2348171482Sjeff * exit by calling sched_exit_thread() and sched_throw() themselves. 2349171482Sjeff */ 2350109864Sjeffvoid 2351121127Sjeffsched_rem(struct thread *td) 2352109864Sjeff{ 2353164936Sjulian struct tdq *tdq; 2354113357Sjeff 2355187357Sjeff KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 2356187357Sjeff "prio:%d", td->td_priority); 2357177435Sjeff tdq = TDQ_CPU(td->td_sched->ts_cpu); 2358171482Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2359171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2360166190Sjeff KASSERT(TD_ON_RUNQ(td), 2361164936Sjulian ("sched_rem: thread not on run queue")); 2362177435Sjeff tdq_runq_rem(tdq, td); 2363177435Sjeff tdq_load_rem(tdq, td); 2364166190Sjeff TD_SET_CAN_RUN(td); 2365176735Sjeff if (td->td_priority == tdq->tdq_lowpri) 2366176735Sjeff tdq_setlowpri(tdq, NULL); 2367109864Sjeff} 2368109864Sjeff 2369171482Sjeff/* 2370171482Sjeff * Fetch cpu utilization information. Updates on demand. 2371171482Sjeff */ 2372109864Sjefffixpt_t 2373121127Sjeffsched_pctcpu(struct thread *td) 2374109864Sjeff{ 2375109864Sjeff fixpt_t pctcpu; 2376164936Sjulian struct td_sched *ts; 2377109864Sjeff 2378109864Sjeff pctcpu = 0; 2379164936Sjulian ts = td->td_sched; 2380164936Sjulian if (ts == NULL) 2381121290Sjeff return (0); 2382109864Sjeff 2383208787Sjhb THREAD_LOCK_ASSERT(td, MA_OWNED); 2384164936Sjulian if (ts->ts_ticks) { 2385109864Sjeff int rtick; 2386109864Sjeff 2387165796Sjeff sched_pctcpu_update(ts); 2388109864Sjeff /* How many rtick per second ? */ 2389165762Sjeff rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2390165762Sjeff pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 2391109864Sjeff } 2392109864Sjeff 2393109864Sjeff return (pctcpu); 2394109864Sjeff} 2395109864Sjeff 2396176735Sjeff/* 2397176735Sjeff * Enforce affinity settings for a thread. Called after adjustments to 2398176735Sjeff * cpumask. 2399176735Sjeff */ 2400176729Sjeffvoid 2401176729Sjeffsched_affinity(struct thread *td) 2402176729Sjeff{ 2403176735Sjeff#ifdef SMP 2404176735Sjeff struct td_sched *ts; 2405176735Sjeff 2406176735Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2407176735Sjeff ts = td->td_sched; 2408176735Sjeff if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 2409176735Sjeff return; 2410189787Sjeff if (TD_ON_RUNQ(td)) { 2411189787Sjeff sched_rem(td); 2412189787Sjeff sched_add(td, SRQ_BORING); 2413189787Sjeff return; 2414189787Sjeff } 2415176735Sjeff if (!TD_IS_RUNNING(td)) 2416176735Sjeff return; 2417176735Sjeff /* 2418212115Smdf * Force a switch before returning to userspace. If the 2419212115Smdf * target thread is not running locally send an ipi to force 2420212115Smdf * the issue. 2421176735Sjeff */ 2422212974Sjhb td->td_flags |= TDF_NEEDRESCHED; 2423212115Smdf if (td != curthread) 2424212115Smdf ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 2425176735Sjeff#endif 2426176729Sjeff} 2427176729Sjeff 2428171482Sjeff/* 2429171482Sjeff * Bind a thread to a target cpu. 2430171482Sjeff */ 2431122038Sjeffvoid 2432122038Sjeffsched_bind(struct thread *td, int cpu) 2433122038Sjeff{ 2434164936Sjulian struct td_sched *ts; 2435122038Sjeff 2436171713Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 2437208391Sjhb KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 2438164936Sjulian ts = td->td_sched; 2439166137Sjeff if (ts->ts_flags & TSF_BOUND) 2440166152Sjeff sched_unbind(td); 2441212115Smdf KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2442164936Sjulian ts->ts_flags |= TSF_BOUND; 2443166137Sjeff sched_pin(); 2444123433Sjeff if (PCPU_GET(cpuid) == cpu) 2445122038Sjeff return; 2446166137Sjeff ts->ts_cpu = cpu; 2447122038Sjeff /* When we return from mi_switch we'll be on the correct cpu. */ 2448131527Sphk mi_switch(SW_VOL, NULL); 2449122038Sjeff} 2450122038Sjeff 2451171482Sjeff/* 2452171482Sjeff * Release a bound thread. 2453171482Sjeff */ 2454122038Sjeffvoid 2455122038Sjeffsched_unbind(struct thread *td) 2456122038Sjeff{ 2457165762Sjeff struct td_sched *ts; 2458165762Sjeff 2459170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2460208391Sjhb KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 2461165762Sjeff ts = td->td_sched; 2462166137Sjeff if ((ts->ts_flags & TSF_BOUND) == 0) 2463166137Sjeff return; 2464165762Sjeff ts->ts_flags &= ~TSF_BOUND; 2465165762Sjeff sched_unpin(); 2466122038Sjeff} 2467122038Sjeff 2468109864Sjeffint 2469145256Sjkoshysched_is_bound(struct thread *td) 2470145256Sjkoshy{ 2471170293Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 2472164936Sjulian return (td->td_sched->ts_flags & TSF_BOUND); 2473145256Sjkoshy} 2474145256Sjkoshy 2475171482Sjeff/* 2476171482Sjeff * Basic yield call. 2477171482Sjeff */ 2478159630Sdavidxuvoid 2479159630Sdavidxusched_relinquish(struct thread *td) 2480159630Sdavidxu{ 2481170293Sjeff thread_lock(td); 2482178272Sjeff mi_switch(SW_VOL | SWT_RELINQUISH, NULL); 2483170293Sjeff thread_unlock(td); 2484159630Sdavidxu} 2485159630Sdavidxu 2486171482Sjeff/* 2487171482Sjeff * Return the total system load. 2488171482Sjeff */ 2489145256Sjkoshyint 2490125289Sjeffsched_load(void) 2491125289Sjeff{ 2492125289Sjeff#ifdef SMP 2493125289Sjeff int total; 2494125289Sjeff int i; 2495125289Sjeff 2496125289Sjeff total = 0; 2497209059Sjhb CPU_FOREACH(i) 2498176735Sjeff total += TDQ_CPU(i)->tdq_sysload; 2499125289Sjeff return (total); 2500125289Sjeff#else 2501165620Sjeff return (TDQ_SELF()->tdq_sysload); 2502125289Sjeff#endif 2503125289Sjeff} 2504125289Sjeff 2505125289Sjeffint 2506109864Sjeffsched_sizeof_proc(void) 2507109864Sjeff{ 2508109864Sjeff return (sizeof(struct proc)); 2509109864Sjeff} 2510109864Sjeff 2511109864Sjeffint 2512109864Sjeffsched_sizeof_thread(void) 2513109864Sjeff{ 2514109864Sjeff return (sizeof(struct thread) + sizeof(struct td_sched)); 2515109864Sjeff} 2516159570Sdavidxu 2517191676Sjeff#ifdef SMP 2518191676Sjeff#define TDQ_IDLESPIN(tdq) \ 2519191676Sjeff ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 2520191676Sjeff#else 2521191676Sjeff#define TDQ_IDLESPIN(tdq) 1 2522191676Sjeff#endif 2523191676Sjeff 2524166190Sjeff/* 2525166190Sjeff * The actual idle process. 2526166190Sjeff */ 2527166190Sjeffvoid 2528166190Sjeffsched_idletd(void *dummy) 2529166190Sjeff{ 2530166190Sjeff struct thread *td; 2531171482Sjeff struct tdq *tdq; 2532178277Sjeff int switchcnt; 2533178277Sjeff int i; 2534166190Sjeff 2535191643Sjeff mtx_assert(&Giant, MA_NOTOWNED); 2536166190Sjeff td = curthread; 2537171482Sjeff tdq = TDQ_SELF(); 2538171482Sjeff for (;;) { 2539171482Sjeff#ifdef SMP 2540178277Sjeff if (tdq_idled(tdq) == 0) 2541178277Sjeff continue; 2542171482Sjeff#endif 2543178277Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2544178277Sjeff /* 2545178277Sjeff * If we're switching very frequently, spin while checking 2546178277Sjeff * for load rather than entering a low power state that 2547191643Sjeff * may require an IPI. However, don't do any busy 2548191643Sjeff * loops while on SMT machines as this simply steals 2549191643Sjeff * cycles from cores doing useful work. 2550178277Sjeff */ 2551191676Sjeff if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 2552178277Sjeff for (i = 0; i < sched_idlespins; i++) { 2553178277Sjeff if (tdq->tdq_load) 2554178277Sjeff break; 2555178277Sjeff cpu_spinwait(); 2556178277Sjeff } 2557178277Sjeff } 2558191643Sjeff switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2559212416Smav if (tdq->tdq_load == 0) { 2560212416Smav tdq->tdq_cpu_idle = 1; 2561212416Smav if (tdq->tdq_load == 0) { 2562212541Smav cpu_idle(switchcnt > sched_idlespinthresh * 4); 2563212416Smav tdq->tdq_switchcnt++; 2564212416Smav } 2565212416Smav tdq->tdq_cpu_idle = 0; 2566212416Smav } 2567178277Sjeff if (tdq->tdq_load) { 2568178277Sjeff thread_lock(td); 2569178277Sjeff mi_switch(SW_VOL | SWT_IDLE, NULL); 2570178277Sjeff thread_unlock(td); 2571178277Sjeff } 2572171482Sjeff } 2573166190Sjeff} 2574166190Sjeff 2575170293Sjeff/* 2576170293Sjeff * A CPU is entering for the first time or a thread is exiting. 2577170293Sjeff */ 2578170293Sjeffvoid 2579170293Sjeffsched_throw(struct thread *td) 2580170293Sjeff{ 2581172411Sjeff struct thread *newtd; 2582171482Sjeff struct tdq *tdq; 2583171482Sjeff 2584171482Sjeff tdq = TDQ_SELF(); 2585170293Sjeff if (td == NULL) { 2586171482Sjeff /* Correct spinlock nesting and acquire the correct lock. */ 2587171482Sjeff TDQ_LOCK(tdq); 2588170293Sjeff spinlock_exit(); 2589170293Sjeff } else { 2590171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2591177435Sjeff tdq_load_rem(tdq, td); 2592174629Sjeff lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object); 2593170293Sjeff } 2594170293Sjeff KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count")); 2595172411Sjeff newtd = choosethread(); 2596172411Sjeff TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd; 2597170293Sjeff PCPU_SET(switchtime, cpu_ticks()); 2598170293Sjeff PCPU_SET(switchticks, ticks); 2599172411Sjeff cpu_throw(td, newtd); /* doesn't return */ 2600170293Sjeff} 2601170293Sjeff 2602171482Sjeff/* 2603171482Sjeff * This is called from fork_exit(). Just acquire the correct locks and 2604171482Sjeff * let fork do the rest of the work. 2605171482Sjeff */ 2606170293Sjeffvoid 2607170600Sjeffsched_fork_exit(struct thread *td) 2608170293Sjeff{ 2609171482Sjeff struct td_sched *ts; 2610171482Sjeff struct tdq *tdq; 2611171482Sjeff int cpuid; 2612170293Sjeff 2613170293Sjeff /* 2614170293Sjeff * Finish setting up thread glue so that it begins execution in a 2615171482Sjeff * non-nested critical section with the scheduler lock held. 2616170293Sjeff */ 2617171482Sjeff cpuid = PCPU_GET(cpuid); 2618171482Sjeff tdq = TDQ_CPU(cpuid); 2619171482Sjeff ts = td->td_sched; 2620171482Sjeff if (TD_IS_IDLETHREAD(td)) 2621171482Sjeff td->td_lock = TDQ_LOCKPTR(tdq); 2622171482Sjeff MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2623171482Sjeff td->td_oncpu = cpuid; 2624172411Sjeff TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 2625174629Sjeff lock_profile_obtain_lock_success( 2626174629Sjeff &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__); 2627170293Sjeff} 2628170293Sjeff 2629187357Sjeff/* 2630187357Sjeff * Create on first use to catch odd startup conditons. 2631187357Sjeff */ 2632187357Sjeffchar * 2633187357Sjeffsched_tdname(struct thread *td) 2634187357Sjeff{ 2635187357Sjeff#ifdef KTR 2636187357Sjeff struct td_sched *ts; 2637187357Sjeff 2638187357Sjeff ts = td->td_sched; 2639187357Sjeff if (ts->ts_name[0] == '\0') 2640187357Sjeff snprintf(ts->ts_name, sizeof(ts->ts_name), 2641187357Sjeff "%s tid %d", td->td_name, td->td_tid); 2642187357Sjeff return (ts->ts_name); 2643187357Sjeff#else 2644187357Sjeff return (td->td_name); 2645187357Sjeff#endif 2646187357Sjeff} 2647187357Sjeff 2648184439Sivoras#ifdef SMP 2649184439Sivoras 2650184439Sivoras/* 2651184439Sivoras * Build the CPU topology dump string. Is recursively called to collect 2652184439Sivoras * the topology tree. 2653184439Sivoras */ 2654184439Sivorasstatic int 2655184439Sivorassysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 2656184439Sivoras int indent) 2657184439Sivoras{ 2658222813Sattilio char cpusetbuf[CPUSETBUFSIZ]; 2659184439Sivoras int i, first; 2660184439Sivoras 2661184439Sivoras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 2662212821Savg "", 1 + indent / 2, cg->cg_level); 2663222813Sattilio sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"%s\">", indent, "", 2664222813Sattilio cg->cg_count, cpusetobj_strprint(cpusetbuf, &cg->cg_mask)); 2665184439Sivoras first = TRUE; 2666184439Sivoras for (i = 0; i < MAXCPU; i++) { 2667222813Sattilio if (CPU_ISSET(i, &cg->cg_mask)) { 2668184439Sivoras if (!first) 2669184439Sivoras sbuf_printf(sb, ", "); 2670184439Sivoras else 2671184439Sivoras first = FALSE; 2672184439Sivoras sbuf_printf(sb, "%d", i); 2673184439Sivoras } 2674184439Sivoras } 2675184439Sivoras sbuf_printf(sb, "</cpu>\n"); 2676184439Sivoras 2677184439Sivoras if (cg->cg_flags != 0) { 2678210117Sivoras sbuf_printf(sb, "%*s <flags>", indent, ""); 2679184439Sivoras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 2680208982Sivoras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 2681208983Sivoras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2682208983Sivoras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 2683191643Sjeff if ((cg->cg_flags & CG_FLAG_SMT) != 0) 2684208983Sivoras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 2685210117Sivoras sbuf_printf(sb, "</flags>\n"); 2686184439Sivoras } 2687184439Sivoras 2688184439Sivoras if (cg->cg_children > 0) { 2689184439Sivoras sbuf_printf(sb, "%*s <children>\n", indent, ""); 2690184439Sivoras for (i = 0; i < cg->cg_children; i++) 2691184439Sivoras sysctl_kern_sched_topology_spec_internal(sb, 2692184439Sivoras &cg->cg_child[i], indent+2); 2693184439Sivoras sbuf_printf(sb, "%*s </children>\n", indent, ""); 2694184439Sivoras } 2695184439Sivoras sbuf_printf(sb, "%*s</group>\n", indent, ""); 2696184439Sivoras return (0); 2697184439Sivoras} 2698184439Sivoras 2699184439Sivoras/* 2700184439Sivoras * Sysctl handler for retrieving topology dump. It's a wrapper for 2701184439Sivoras * the recursive sysctl_kern_smp_topology_spec_internal(). 2702184439Sivoras */ 2703184439Sivorasstatic int 2704184439Sivorassysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 2705184439Sivoras{ 2706184439Sivoras struct sbuf *topo; 2707184439Sivoras int err; 2708184439Sivoras 2709184439Sivoras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 2710184439Sivoras 2711184570Sivoras topo = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND); 2712184439Sivoras if (topo == NULL) 2713184439Sivoras return (ENOMEM); 2714184439Sivoras 2715184439Sivoras sbuf_printf(topo, "<groups>\n"); 2716184439Sivoras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 2717184439Sivoras sbuf_printf(topo, "</groups>\n"); 2718184439Sivoras 2719184439Sivoras if (err == 0) { 2720184439Sivoras sbuf_finish(topo); 2721184439Sivoras err = SYSCTL_OUT(req, sbuf_data(topo), sbuf_len(topo)); 2722184439Sivoras } 2723184439Sivoras sbuf_delete(topo); 2724184439Sivoras return (err); 2725184439Sivoras} 2726214510Sdavidxu 2727184439Sivoras#endif 2728184439Sivoras 2729177435SjeffSYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2730171482SjeffSYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 2731165762Sjeff "Scheduler name"); 2732171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 2733171482Sjeff "Slice size for timeshare threads"); 2734171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 2735171482Sjeff "Interactivity score threshold"); 2736171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh, 2737171482Sjeff 0,"Min priority for preemption, lower priorities have greater precedence"); 2738177085SjeffSYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 2739177085Sjeff 0,"Controls whether static kernel priorities are assigned to sleeping threads."); 2740178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 2741178277Sjeff 0,"Number of times idle will spin waiting for new work."); 2742178277SjeffSYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, &sched_idlespinthresh, 2743178277Sjeff 0,"Threshold before we will permit idle spinning."); 2744166108Sjeff#ifdef SMP 2745171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 2746171482Sjeff "Number of hz ticks to keep thread affinity for"); 2747171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 2748171482Sjeff "Enables the long-term load balancer"); 2749172409SjeffSYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 2750172409Sjeff &balance_interval, 0, 2751172409Sjeff "Average frequency in stathz ticks to run the long-term balancer"); 2752171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0, 2753171482Sjeff "Steals work from another hyper-threaded core on idle"); 2754171482SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 2755171482Sjeff "Attempts to steal work from other cores before idling"); 2756171506SjeffSYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 2757171506Sjeff "Minimum load on remote cpu before we'll steal"); 2758184439Sivoras 2759184439Sivoras/* Retrieve SMP topology */ 2760184439SivorasSYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 2761184439Sivoras CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 2762184439Sivoras "XML dump of detected CPU topology"); 2763214510Sdavidxu 2764166108Sjeff#endif 2765165762Sjeff 2766172264Sjeff/* ps compat. All cpu percentages from ULE are weighted. */ 2767172293Sjeffstatic int ccpu = 0; 2768165762SjeffSYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 2769