135e6168fSJeff Roberson /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 4e7d50326SJeff Roberson * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org> 535e6168fSJeff Roberson * All rights reserved. 635e6168fSJeff Roberson * 735e6168fSJeff Roberson * Redistribution and use in source and binary forms, with or without 835e6168fSJeff Roberson * modification, are permitted provided that the following conditions 935e6168fSJeff Roberson * are met: 1035e6168fSJeff Roberson * 1. Redistributions of source code must retain the above copyright 1135e6168fSJeff Roberson * notice unmodified, this list of conditions, and the following 1235e6168fSJeff Roberson * disclaimer. 1335e6168fSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 1435e6168fSJeff Roberson * notice, this list of conditions and the following disclaimer in the 1535e6168fSJeff Roberson * documentation and/or other materials provided with the distribution. 1635e6168fSJeff Roberson * 1735e6168fSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1835e6168fSJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1935e6168fSJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2035e6168fSJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2135e6168fSJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2235e6168fSJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2335e6168fSJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2435e6168fSJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2535e6168fSJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2635e6168fSJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2735e6168fSJeff Roberson */ 2835e6168fSJeff Roberson 29ae7a6b38SJeff Roberson /* 30ae7a6b38SJeff Roberson * This file implements the ULE scheduler. ULE supports independent CPU 31ae7a6b38SJeff Roberson * run queues and fine grain locking. It has superior interactive 32ae7a6b38SJeff Roberson * performance under load even on uni-processor systems. 33ae7a6b38SJeff Roberson * 34ae7a6b38SJeff Roberson * etymology: 35a5423ea3SJeff Roberson * ULE is the last three letters in schedule. It owes its name to a 36ae7a6b38SJeff Roberson * generic user created for a scheduling system by Paul Mikesell at 37ae7a6b38SJeff Roberson * Isilon Systems and a general lack of creativity on the part of the author. 38ae7a6b38SJeff Roberson */ 39ae7a6b38SJeff Roberson 40677b542eSDavid E. O'Brien #include <sys/cdefs.h> 41113dda8aSJeff Roberson __FBSDID("$FreeBSD$"); 42677b542eSDavid E. O'Brien 434da0d332SPeter Wemm #include "opt_hwpmc_hooks.h" 444da0d332SPeter Wemm #include "opt_sched.h" 459923b511SScott Long 4635e6168fSJeff Roberson #include <sys/param.h> 4735e6168fSJeff Roberson #include <sys/systm.h> 482c3490b1SMarcel Moolenaar #include <sys/kdb.h> 4935e6168fSJeff Roberson #include <sys/kernel.h> 5035e6168fSJeff Roberson #include <sys/ktr.h> 51c149e542SAttilio Rao #include <sys/limits.h> 5235e6168fSJeff Roberson #include <sys/lock.h> 5335e6168fSJeff Roberson #include <sys/mutex.h> 5435e6168fSJeff Roberson #include <sys/proc.h> 55245f3abfSJeff Roberson #include <sys/resource.h> 569bacd788SJeff Roberson #include <sys/resourcevar.h> 5735e6168fSJeff Roberson #include <sys/sched.h> 58b3e9e682SRyan Stone #include <sys/sdt.h> 5935e6168fSJeff Roberson #include <sys/smp.h> 6035e6168fSJeff Roberson #include <sys/sx.h> 6135e6168fSJeff Roberson #include <sys/sysctl.h> 6235e6168fSJeff Roberson #include <sys/sysproto.h> 63f5c157d9SJohn Baldwin #include <sys/turnstile.h> 64af29f399SDmitry Chagin #include <sys/umtxvar.h> 6535e6168fSJeff Roberson #include <sys/vmmeter.h> 6662fa74d9SJeff Roberson #include <sys/cpuset.h> 6707095abfSIvan Voras #include <sys/sbuf.h> 6835e6168fSJeff Roberson 69ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 70ebccf1e3SJoseph Koshy #include <sys/pmckern.h> 71ebccf1e3SJoseph Koshy #endif 72ebccf1e3SJoseph Koshy 736f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS 746f5f25e5SJohn Birrell #include <sys/dtrace_bsd.h> 7561322a0aSAlexander Motin int __read_mostly dtrace_vtime_active; 766f5f25e5SJohn Birrell dtrace_vtime_switch_func_t dtrace_vtime_switch_func; 776f5f25e5SJohn Birrell #endif 786f5f25e5SJohn Birrell 7935e6168fSJeff Roberson #include <machine/cpu.h> 8022bf7d9aSJeff Roberson #include <machine/smp.h> 8135e6168fSJeff Roberson 82ae7a6b38SJeff Roberson #define KTR_ULE 0 8314618990SJeff Roberson 840d2cf837SJeff Roberson #define TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX))) 850d2cf837SJeff Roberson #define TDQ_NAME_LEN (sizeof("sched lock ") + sizeof(__XSTRING(MAXCPU))) 866338c579SAttilio Rao #define TDQ_LOADNAME_LEN (sizeof("CPU ") + sizeof(__XSTRING(MAXCPU)) - 1 + sizeof(" load")) 878f51ad55SJeff Roberson 886b2f763fSJeff Roberson /* 89ae7a6b38SJeff Roberson * Thread scheduler specific section. All fields are protected 90ae7a6b38SJeff Roberson * by the thread lock. 91ed062c8dSJulian Elischer */ 92ad1e7d28SJulian Elischer struct td_sched { 93ae7a6b38SJeff Roberson struct runq *ts_runq; /* Run-queue we're queued on. */ 94ae7a6b38SJeff Roberson short ts_flags; /* TSF_* flags. */ 95e77f9fedSAdrian Chadd int ts_cpu; /* CPU that we have affinity for. */ 9673daf66fSJeff Roberson int ts_rltick; /* Real last tick, for affinity. */ 97ae7a6b38SJeff Roberson int ts_slice; /* Ticks of slice remaining. */ 98ae7a6b38SJeff Roberson u_int ts_slptime; /* Number of ticks we vol. slept */ 99ae7a6b38SJeff Roberson u_int ts_runtime; /* Number of ticks we were running */ 100ad1e7d28SJulian Elischer int ts_ltick; /* Last tick that we were running on */ 101ad1e7d28SJulian Elischer int ts_ftick; /* First tick that we were running on */ 102ad1e7d28SJulian Elischer int ts_ticks; /* Tick count */ 1038f51ad55SJeff Roberson #ifdef KTR 1048f51ad55SJeff Roberson char ts_name[TS_NAME_LEN]; 1058f51ad55SJeff Roberson #endif 106ed062c8dSJulian Elischer }; 107ad1e7d28SJulian Elischer /* flags kept in ts_flags */ 1087b8bfa0dSJeff Roberson #define TSF_BOUND 0x0001 /* Thread can not migrate. */ 1097b8bfa0dSJeff Roberson #define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */ 11035e6168fSJeff Roberson 11162fa74d9SJeff Roberson #define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0) 11262fa74d9SJeff Roberson #define THREAD_CAN_SCHED(td, cpu) \ 11362fa74d9SJeff Roberson CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask) 11462fa74d9SJeff Roberson 11593ccd6bfSKonstantin Belousov _Static_assert(sizeof(struct thread) + sizeof(struct td_sched) <= 11693ccd6bfSKonstantin Belousov sizeof(struct thread0_storage), 11793ccd6bfSKonstantin Belousov "increase struct thread0_storage.t0st_sched size"); 11893ccd6bfSKonstantin Belousov 11935e6168fSJeff Roberson /* 12012d56c0fSJohn Baldwin * Priority ranges used for interactive and non-interactive timeshare 1212dc29adbSJohn Baldwin * threads. The timeshare priorities are split up into four ranges. 1222dc29adbSJohn Baldwin * The first range handles interactive threads. The last three ranges 1232dc29adbSJohn Baldwin * (NHALF, x, and NHALF) handle non-interactive threads with the outer 1242dc29adbSJohn Baldwin * ranges supporting nice values. 12512d56c0fSJohn Baldwin */ 1262dc29adbSJohn Baldwin #define PRI_TIMESHARE_RANGE (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE + 1) 1272dc29adbSJohn Baldwin #define PRI_INTERACT_RANGE ((PRI_TIMESHARE_RANGE - SCHED_PRI_NRESV) / 2) 12816705791SAndriy Gapon #define PRI_BATCH_RANGE (PRI_TIMESHARE_RANGE - PRI_INTERACT_RANGE) 1292dc29adbSJohn Baldwin 1302dc29adbSJohn Baldwin #define PRI_MIN_INTERACT PRI_MIN_TIMESHARE 1312dc29adbSJohn Baldwin #define PRI_MAX_INTERACT (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE - 1) 1322dc29adbSJohn Baldwin #define PRI_MIN_BATCH (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE) 13312d56c0fSJohn Baldwin #define PRI_MAX_BATCH PRI_MAX_TIMESHARE 13412d56c0fSJohn Baldwin 13512d56c0fSJohn Baldwin /* 136e7d50326SJeff Roberson * Cpu percentage computation macros and defines. 137e1f89c22SJeff Roberson * 138e7d50326SJeff Roberson * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 139e7d50326SJeff Roberson * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 1408ab80cf0SJeff Roberson * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 141e7d50326SJeff Roberson * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 142e7d50326SJeff Roberson * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 143e7d50326SJeff Roberson * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 14435e6168fSJeff Roberson */ 145e7d50326SJeff Roberson #define SCHED_TICK_SECS 10 146e7d50326SJeff Roberson #define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 1478ab80cf0SJeff Roberson #define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 148e7d50326SJeff Roberson #define SCHED_TICK_SHIFT 10 149e7d50326SJeff Roberson #define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 150eddb4efaSJeff Roberson #define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 15135e6168fSJeff Roberson 15235e6168fSJeff Roberson /* 153e7d50326SJeff Roberson * These macros determine priorities for non-interactive threads. They are 154e7d50326SJeff Roberson * assigned a priority based on their recent cpu utilization as expressed 155e7d50326SJeff Roberson * by the ratio of ticks to the tick total. NHALF priorities at the start 156e7d50326SJeff Roberson * and end of the MIN to MAX timeshare range are only reachable with negative 157e7d50326SJeff Roberson * or positive nice respectively. 158e7d50326SJeff Roberson * 159e7d50326SJeff Roberson * PRI_RANGE: Priority range for utilization dependent priorities. 160e7d50326SJeff Roberson * PRI_NRESV: Number of nice values. 161e7d50326SJeff Roberson * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 162e7d50326SJeff Roberson * PRI_NICE: Determines the part of the priority inherited from nice. 163e7d50326SJeff Roberson */ 164e7d50326SJeff Roberson #define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 165e7d50326SJeff Roberson #define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 16612d56c0fSJohn Baldwin #define SCHED_PRI_MIN (PRI_MIN_BATCH + SCHED_PRI_NHALF) 16712d56c0fSJohn Baldwin #define SCHED_PRI_MAX (PRI_MAX_BATCH - SCHED_PRI_NHALF) 16878920008SJohn Baldwin #define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1) 169e7d50326SJeff Roberson #define SCHED_PRI_TICKS(ts) \ 170e7d50326SJeff Roberson (SCHED_TICK_HZ((ts)) / \ 1711e516cf5SJeff Roberson (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 172e7d50326SJeff Roberson #define SCHED_PRI_NICE(nice) (nice) 173e7d50326SJeff Roberson 174e7d50326SJeff Roberson /* 175e7d50326SJeff Roberson * These determine the interactivity of a process. Interactivity differs from 176e7d50326SJeff Roberson * cpu utilization in that it expresses the voluntary time slept vs time ran 177e7d50326SJeff Roberson * while cpu utilization includes all time not running. This more accurately 178e7d50326SJeff Roberson * models the intent of the thread. 17935e6168fSJeff Roberson * 180407b0157SJeff Roberson * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 181407b0157SJeff Roberson * before throttling back. 182d322132cSJeff Roberson * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 183210491d3SJeff Roberson * INTERACT_MAX: Maximum interactivity value. Smaller is better. 1849f518f20SAttilio Rao * INTERACT_THRESH: Threshold for placement on the current runq. 18535e6168fSJeff Roberson */ 186e7d50326SJeff Roberson #define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 187e7d50326SJeff Roberson #define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 188210491d3SJeff Roberson #define SCHED_INTERACT_MAX (100) 189210491d3SJeff Roberson #define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 1904c9612c6SJeff Roberson #define SCHED_INTERACT_THRESH (30) 191e1f89c22SJeff Roberson 1925e5c3873SJeff Roberson /* 1935e5c3873SJeff Roberson * These parameters determine the slice behavior for batch work. 1945e5c3873SJeff Roberson */ 1955e5c3873SJeff Roberson #define SCHED_SLICE_DEFAULT_DIVISOR 10 /* ~94 ms, 12 stathz ticks. */ 1965e5c3873SJeff Roberson #define SCHED_SLICE_MIN_DIVISOR 6 /* DEFAULT/MIN = ~16 ms. */ 1975e5c3873SJeff Roberson 1983d7f4117SAlexander Motin /* Flags kept in td_flags. */ 199e745d729SAlexander Motin #define TDF_PICKCPU TDF_SCHED0 /* Thread should pick new CPU. */ 2003d7f4117SAlexander Motin #define TDF_SLICEEND TDF_SCHED2 /* Thread time slice is over. */ 2013d7f4117SAlexander Motin 20235e6168fSJeff Roberson /* 203e7d50326SJeff Roberson * tickincr: Converts a stathz tick into a hz domain scaled by 204e7d50326SJeff Roberson * the shift factor. Without the shift the error rate 205e7d50326SJeff Roberson * due to rounding would be unacceptably high. 206e7d50326SJeff Roberson * realstathz: stathz is sometimes 0 and run off of hz. 207e7d50326SJeff Roberson * sched_slice: Runtime of each thread before rescheduling. 208ae7a6b38SJeff Roberson * preempt_thresh: Priority threshold for preemption and remote IPIs. 20935e6168fSJeff Roberson */ 2101c119e17SAlexander Motin static u_int __read_mostly sched_interact = SCHED_INTERACT_THRESH; 21161322a0aSAlexander Motin static int __read_mostly tickincr = 8 << SCHED_TICK_SHIFT; 21261322a0aSAlexander Motin static int __read_mostly realstathz = 127; /* reset during boot. */ 21361322a0aSAlexander Motin static int __read_mostly sched_slice = 10; /* reset during boot. */ 21461322a0aSAlexander Motin static int __read_mostly sched_slice_min = 1; /* reset during boot. */ 21502e2d6b4SJeff Roberson #ifdef PREEMPTION 21602e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION 21761322a0aSAlexander Motin static int __read_mostly preempt_thresh = PRI_MAX_IDLE; 21802e2d6b4SJeff Roberson #else 21961322a0aSAlexander Motin static int __read_mostly preempt_thresh = PRI_MIN_KERN; 22002e2d6b4SJeff Roberson #endif 22102e2d6b4SJeff Roberson #else 22261322a0aSAlexander Motin static int __read_mostly preempt_thresh = 0; 22302e2d6b4SJeff Roberson #endif 22461322a0aSAlexander Motin static int __read_mostly static_boost = PRI_MIN_BATCH; 22561322a0aSAlexander Motin static int __read_mostly sched_idlespins = 10000; 22661322a0aSAlexander Motin static int __read_mostly sched_idlespinthresh = -1; 227ae7a6b38SJeff Roberson 22835e6168fSJeff Roberson /* 22911484ad8SMark Johnston * tdq - per processor runqs and statistics. A mutex synchronizes access to 23011484ad8SMark Johnston * most fields. Some fields are loaded or modified without the mutex. 23111484ad8SMark Johnston * 23211484ad8SMark Johnston * Locking protocols: 23311484ad8SMark Johnston * (c) constant after initialization 23411484ad8SMark Johnston * (f) flag, set with the tdq lock held, cleared on local CPU 23511484ad8SMark Johnston * (l) all accesses are CPU-local 23611484ad8SMark Johnston * (ls) stores are performed by the local CPU, loads may be lockless 23711484ad8SMark Johnston * (t) all accesses are protected by the tdq mutex 23811484ad8SMark Johnston * (ts) stores are serialized by the tdq mutex, loads may be lockless 23935e6168fSJeff Roberson */ 240ad1e7d28SJulian Elischer struct tdq { 24139f819e2SJim Harris /* 24239f819e2SJim Harris * Ordered to improve efficiency of cpu_search() and switch(). 24339f819e2SJim Harris * tdq_lock is padded to avoid false sharing with tdq_load and 24439f819e2SJim Harris * tdq_cpu_idle. 24539f819e2SJim Harris */ 2464ceaf45dSAttilio Rao struct mtx_padalign tdq_lock; /* run queue lock. */ 24711484ad8SMark Johnston struct cpu_group *tdq_cg; /* (c) Pointer to cpu topology. */ 24811484ad8SMark Johnston struct thread *tdq_curthread; /* (t) Current executing thread. */ 24911484ad8SMark Johnston int tdq_load; /* (ts) Aggregate load. */ 25011484ad8SMark Johnston int tdq_sysload; /* (ts) For loadavg, !ITHD load. */ 25111484ad8SMark Johnston int tdq_cpu_idle; /* (ls) cpu_idle() is active. */ 25211484ad8SMark Johnston int tdq_transferable; /* (ts) Transferable thread count. */ 25311484ad8SMark Johnston short tdq_switchcnt; /* (l) Switches this tick. */ 25411484ad8SMark Johnston short tdq_oldswitchcnt; /* (l) Switches last tick. */ 25511484ad8SMark Johnston u_char tdq_lowpri; /* (ts) Lowest priority thread. */ 25611484ad8SMark Johnston u_char tdq_owepreempt; /* (f) Remote preemption pending. */ 25711484ad8SMark Johnston u_char tdq_idx; /* (t) Current insert index. */ 25811484ad8SMark Johnston u_char tdq_ridx; /* (t) Current removal index. */ 25911484ad8SMark Johnston int tdq_id; /* (c) cpuid. */ 26011484ad8SMark Johnston struct runq tdq_realtime; /* (t) real-time run queue. */ 26111484ad8SMark Johnston struct runq tdq_timeshare; /* (t) timeshare run queue. */ 26211484ad8SMark Johnston struct runq tdq_idle; /* (t) Queue of IDLE threads. */ 2638f51ad55SJeff Roberson char tdq_name[TDQ_NAME_LEN]; 2648f51ad55SJeff Roberson #ifdef KTR 2658f51ad55SJeff Roberson char tdq_loadname[TDQ_LOADNAME_LEN]; 2668f51ad55SJeff Roberson #endif 26711484ad8SMark Johnston }; 26835e6168fSJeff Roberson 2691690c6c1SJeff Roberson /* Idle thread states and config. */ 2701690c6c1SJeff Roberson #define TDQ_RUNNING 1 2711690c6c1SJeff Roberson #define TDQ_IDLE 2 2727b8bfa0dSJeff Roberson 27311484ad8SMark Johnston /* Lockless accessors. */ 27411484ad8SMark Johnston #define TDQ_LOAD(tdq) atomic_load_int(&(tdq)->tdq_load) 27511484ad8SMark Johnston #define TDQ_TRANSFERABLE(tdq) atomic_load_int(&(tdq)->tdq_transferable) 27611484ad8SMark Johnston #define TDQ_SWITCHCNT(tdq) (atomic_load_short(&(tdq)->tdq_switchcnt) + \ 27711484ad8SMark Johnston atomic_load_short(&(tdq)->tdq_oldswitchcnt)) 27811484ad8SMark Johnston #define TDQ_SWITCHCNT_INC(tdq) (atomic_store_short(&(tdq)->tdq_switchcnt, \ 27911484ad8SMark Johnston atomic_load_short(&(tdq)->tdq_switchcnt) + 1)) 28011484ad8SMark Johnston 28180f86c9fSJeff Roberson #ifdef SMP 28261322a0aSAlexander Motin struct cpu_group __read_mostly *cpu_top; /* CPU topology */ 2837b8bfa0dSJeff Roberson 28462fa74d9SJeff Roberson #define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 28562fa74d9SJeff Roberson #define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 2867b8bfa0dSJeff Roberson 2877b8bfa0dSJeff Roberson /* 2887b8bfa0dSJeff Roberson * Run-time tunables. 2897b8bfa0dSJeff Roberson */ 29028994a58SJeff Roberson static int rebalance = 1; 2917fcf154aSJeff Roberson static int balance_interval = 128; /* Default set in sched_initticks(). */ 29261322a0aSAlexander Motin static int __read_mostly affinity; 29361322a0aSAlexander Motin static int __read_mostly steal_idle = 1; 29461322a0aSAlexander Motin static int __read_mostly steal_thresh = 2; 29561322a0aSAlexander Motin static int __read_mostly always_steal = 0; 29661322a0aSAlexander Motin static int __read_mostly trysteal_limit = 2; 29780f86c9fSJeff Roberson 29835e6168fSJeff Roberson /* 299d2ad694cSJeff Roberson * One thread queue per processor. 30035e6168fSJeff Roberson */ 30161322a0aSAlexander Motin static struct tdq __read_mostly *balance_tdq; 3027fcf154aSJeff Roberson static int balance_ticks; 303018ff686SJeff Roberson DPCPU_DEFINE_STATIC(struct tdq, tdq); 3042bf95012SAndrew Turner DPCPU_DEFINE_STATIC(uint32_t, randomval); 305dc03363dSJeff Roberson 306018ff686SJeff Roberson #define TDQ_SELF() ((struct tdq *)PCPU_GET(sched)) 307018ff686SJeff Roberson #define TDQ_CPU(x) (DPCPU_ID_PTR((x), tdq)) 308018ff686SJeff Roberson #define TDQ_ID(x) ((x)->tdq_id) 30980f86c9fSJeff Roberson #else /* !SMP */ 310ad1e7d28SJulian Elischer static struct tdq tdq_cpu; 311dc03363dSJeff Roberson 31236b36916SJeff Roberson #define TDQ_ID(x) (0) 313ad1e7d28SJulian Elischer #define TDQ_SELF() (&tdq_cpu) 314ad1e7d28SJulian Elischer #define TDQ_CPU(x) (&tdq_cpu) 3150a016a05SJeff Roberson #endif 31635e6168fSJeff Roberson 317ae7a6b38SJeff Roberson #define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 318ae7a6b38SJeff Roberson #define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 319ae7a6b38SJeff Roberson #define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 3208bb173fbSAlexander Motin #define TDQ_TRYLOCK(t) mtx_trylock_spin(TDQ_LOCKPTR((t))) 3218bb173fbSAlexander Motin #define TDQ_TRYLOCK_FLAGS(t, f) mtx_trylock_spin_flags(TDQ_LOCKPTR((t)), (f)) 322ae7a6b38SJeff Roberson #define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 3234ceaf45dSAttilio Rao #define TDQ_LOCKPTR(t) ((struct mtx *)(&(t)->tdq_lock)) 324ae7a6b38SJeff Roberson 3250927ff78SMark Johnston static void sched_setpreempt(int); 3268460a577SJohn Birrell static void sched_priority(struct thread *); 32721381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char); 3288460a577SJohn Birrell static int sched_interact_score(struct thread *); 3298460a577SJohn Birrell static void sched_interact_update(struct thread *); 3308460a577SJohn Birrell static void sched_interact_fork(struct thread *); 3317295465eSAlexander Motin static void sched_pctcpu_update(struct td_sched *, int); 33235e6168fSJeff Roberson 3335d7ef00cSJeff Roberson /* Operations on per processor queues */ 3349727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *); 335018ff686SJeff Roberson static void tdq_setup(struct tdq *, int i); 3369727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *); 3379727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *); 3389727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int); 3399727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *); 340ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int); 34111484ad8SMark Johnston static void tdq_print(int cpu); 342e7d50326SJeff Roberson static void runq_print(struct runq *rq); 3436d3f74a1SMark Johnston static int tdq_add(struct tdq *, struct thread *, int); 3445d7ef00cSJeff Roberson #ifdef SMP 3456d3f74a1SMark Johnston static int tdq_move(struct tdq *, struct tdq *); 346ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *); 3476d3f74a1SMark Johnston static void tdq_notify(struct tdq *, int lowpri); 3489727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int); 3499727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int); 3509727e637SJeff Roberson static int sched_pickcpu(struct thread *, int); 3517fcf154aSJeff Roberson static void sched_balance(void); 3526d3f74a1SMark Johnston static bool sched_balance_pair(struct tdq *, struct tdq *); 3539727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int); 354ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *); 35507095abfSIvan Voras static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 35607095abfSIvan Voras static int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 35707095abfSIvan Voras struct cpu_group *cg, int indent); 3585d7ef00cSJeff Roberson #endif 3595d7ef00cSJeff Roberson 360e7d50326SJeff Roberson static void sched_setup(void *dummy); 361237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 362e7d50326SJeff Roberson 363e7d50326SJeff Roberson static void sched_initticks(void *dummy); 364237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 365237fdd78SRobert Watson NULL); 366e7d50326SJeff Roberson 367b3e9e682SRyan Stone SDT_PROVIDER_DEFINE(sched); 368b3e9e682SRyan Stone 369d9fae5abSAndriy Gapon SDT_PROBE_DEFINE3(sched, , , change__pri, "struct thread *", 370b3e9e682SRyan Stone "struct proc *", "uint8_t"); 371d9fae5abSAndriy Gapon SDT_PROBE_DEFINE3(sched, , , dequeue, "struct thread *", 372b3e9e682SRyan Stone "struct proc *", "void *"); 373d9fae5abSAndriy Gapon SDT_PROBE_DEFINE4(sched, , , enqueue, "struct thread *", 374b3e9e682SRyan Stone "struct proc *", "void *", "int"); 375d9fae5abSAndriy Gapon SDT_PROBE_DEFINE4(sched, , , lend__pri, "struct thread *", 376b3e9e682SRyan Stone "struct proc *", "uint8_t", "struct thread *"); 377d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , load__change, "int", "int"); 378d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , off__cpu, "struct thread *", 379b3e9e682SRyan Stone "struct proc *"); 380d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(sched, , , on__cpu); 381d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(sched, , , remain__cpu); 382d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , surrender, "struct thread *", 383b3e9e682SRyan Stone "struct proc *"); 384b3e9e682SRyan Stone 3850567b6ccSWarner Losh /* 386ae7a6b38SJeff Roberson * Print the threads waiting on a run-queue. 387ae7a6b38SJeff Roberson */ 388e7d50326SJeff Roberson static void 389e7d50326SJeff Roberson runq_print(struct runq *rq) 390e7d50326SJeff Roberson { 391e7d50326SJeff Roberson struct rqhead *rqh; 3929727e637SJeff Roberson struct thread *td; 393e7d50326SJeff Roberson int pri; 394e7d50326SJeff Roberson int j; 395e7d50326SJeff Roberson int i; 396e7d50326SJeff Roberson 397e7d50326SJeff Roberson for (i = 0; i < RQB_LEN; i++) { 398e7d50326SJeff Roberson printf("\t\trunq bits %d 0x%zx\n", 399e7d50326SJeff Roberson i, rq->rq_status.rqb_bits[i]); 400e7d50326SJeff Roberson for (j = 0; j < RQB_BPW; j++) 401e7d50326SJeff Roberson if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 402e7d50326SJeff Roberson pri = j + (i << RQB_L2BPW); 403e7d50326SJeff Roberson rqh = &rq->rq_queues[pri]; 4049727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 405e7d50326SJeff Roberson printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 4069727e637SJeff Roberson td, td->td_name, td->td_priority, 4079727e637SJeff Roberson td->td_rqindex, pri); 408e7d50326SJeff Roberson } 409e7d50326SJeff Roberson } 410e7d50326SJeff Roberson } 411e7d50326SJeff Roberson } 412e7d50326SJeff Roberson 413ae7a6b38SJeff Roberson /* 414ae7a6b38SJeff Roberson * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 415ae7a6b38SJeff Roberson */ 41611484ad8SMark Johnston static void __unused 417ad1e7d28SJulian Elischer tdq_print(int cpu) 41815dc847eSJeff Roberson { 419ad1e7d28SJulian Elischer struct tdq *tdq; 42015dc847eSJeff Roberson 421ad1e7d28SJulian Elischer tdq = TDQ_CPU(cpu); 42215dc847eSJeff Roberson 423c47f202bSJeff Roberson printf("tdq %d:\n", TDQ_ID(tdq)); 42462fa74d9SJeff Roberson printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 42562fa74d9SJeff Roberson printf("\tLock name: %s\n", tdq->tdq_name); 426d2ad694cSJeff Roberson printf("\tload: %d\n", tdq->tdq_load); 4271690c6c1SJeff Roberson printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 4281690c6c1SJeff Roberson printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 429e7d50326SJeff Roberson printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 4303f872f85SJeff Roberson printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 4311690c6c1SJeff Roberson printf("\tload transferable: %d\n", tdq->tdq_transferable); 4321690c6c1SJeff Roberson printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 433e7d50326SJeff Roberson printf("\trealtime runq:\n"); 434e7d50326SJeff Roberson runq_print(&tdq->tdq_realtime); 435e7d50326SJeff Roberson printf("\ttimeshare runq:\n"); 436e7d50326SJeff Roberson runq_print(&tdq->tdq_timeshare); 437e7d50326SJeff Roberson printf("\tidle runq:\n"); 438e7d50326SJeff Roberson runq_print(&tdq->tdq_idle); 43915dc847eSJeff Roberson } 44015dc847eSJeff Roberson 441ff256d9cSJeff Roberson static inline int 442ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote) 443ff256d9cSJeff Roberson { 444ff256d9cSJeff Roberson /* 445ff256d9cSJeff Roberson * If the new priority is not better than the current priority there is 446ff256d9cSJeff Roberson * nothing to do. 447ff256d9cSJeff Roberson */ 448ff256d9cSJeff Roberson if (pri >= cpri) 449ff256d9cSJeff Roberson return (0); 450ff256d9cSJeff Roberson /* 451ff256d9cSJeff Roberson * Always preempt idle. 452ff256d9cSJeff Roberson */ 453ff256d9cSJeff Roberson if (cpri >= PRI_MIN_IDLE) 454ff256d9cSJeff Roberson return (1); 455ff256d9cSJeff Roberson /* 456ff256d9cSJeff Roberson * If preemption is disabled don't preempt others. 457ff256d9cSJeff Roberson */ 458ff256d9cSJeff Roberson if (preempt_thresh == 0) 459ff256d9cSJeff Roberson return (0); 460ff256d9cSJeff Roberson /* 461ff256d9cSJeff Roberson * Preempt if we exceed the threshold. 462ff256d9cSJeff Roberson */ 463ff256d9cSJeff Roberson if (pri <= preempt_thresh) 464ff256d9cSJeff Roberson return (1); 465ff256d9cSJeff Roberson /* 46612d56c0fSJohn Baldwin * If we're interactive or better and there is non-interactive 46712d56c0fSJohn Baldwin * or worse running preempt only remote processors. 468ff256d9cSJeff Roberson */ 46912d56c0fSJohn Baldwin if (remote && pri <= PRI_MAX_INTERACT && cpri > PRI_MAX_INTERACT) 470ff256d9cSJeff Roberson return (1); 471ff256d9cSJeff Roberson return (0); 472ff256d9cSJeff Roberson } 473ff256d9cSJeff Roberson 474ae7a6b38SJeff Roberson /* 475ae7a6b38SJeff Roberson * Add a thread to the actual run-queue. Keeps transferable counts up to 476ae7a6b38SJeff Roberson * date with what is actually on the run-queue. Selects the correct 477ae7a6b38SJeff Roberson * queue position for timeshare threads. 478ae7a6b38SJeff Roberson */ 479155b9987SJeff Roberson static __inline void 4809727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 481155b9987SJeff Roberson { 4829727e637SJeff Roberson struct td_sched *ts; 483c143ac21SJeff Roberson u_char pri; 484c143ac21SJeff Roberson 485ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 48661a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 48773daf66fSJeff Roberson 4889727e637SJeff Roberson pri = td->td_priority; 48993ccd6bfSKonstantin Belousov ts = td_get_sched(td); 4909727e637SJeff Roberson TD_SET_RUNQ(td); 4919727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td)) { 492d2ad694cSJeff Roberson tdq->tdq_transferable++; 493ad1e7d28SJulian Elischer ts->ts_flags |= TSF_XFERABLE; 49480f86c9fSJeff Roberson } 49512d56c0fSJohn Baldwin if (pri < PRI_MIN_BATCH) { 496c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_realtime; 49712d56c0fSJohn Baldwin } else if (pri <= PRI_MAX_BATCH) { 498c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_timeshare; 49912d56c0fSJohn Baldwin KASSERT(pri <= PRI_MAX_BATCH && pri >= PRI_MIN_BATCH, 500e7d50326SJeff Roberson ("Invalid priority %d on timeshare runq", pri)); 501e7d50326SJeff Roberson /* 502e7d50326SJeff Roberson * This queue contains only priorities between MIN and MAX 503ba71333fSMark Johnston * batch. Use the whole queue to represent these values. 504e7d50326SJeff Roberson */ 505c47f202bSJeff Roberson if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 50616705791SAndriy Gapon pri = RQ_NQS * (pri - PRI_MIN_BATCH) / PRI_BATCH_RANGE; 507e7d50326SJeff Roberson pri = (pri + tdq->tdq_idx) % RQ_NQS; 5083f872f85SJeff Roberson /* 5093f872f85SJeff Roberson * This effectively shortens the queue by one so we 5103f872f85SJeff Roberson * can have a one slot difference between idx and 5113f872f85SJeff Roberson * ridx while we wait for threads to drain. 5123f872f85SJeff Roberson */ 5133f872f85SJeff Roberson if (tdq->tdq_ridx != tdq->tdq_idx && 5143f872f85SJeff Roberson pri == tdq->tdq_ridx) 5154499aff6SJeff Roberson pri = (unsigned char)(pri - 1) % RQ_NQS; 516e7d50326SJeff Roberson } else 5173f872f85SJeff Roberson pri = tdq->tdq_ridx; 5189727e637SJeff Roberson runq_add_pri(ts->ts_runq, td, pri, flags); 519c143ac21SJeff Roberson return; 520e7d50326SJeff Roberson } else 52173daf66fSJeff Roberson ts->ts_runq = &tdq->tdq_idle; 5229727e637SJeff Roberson runq_add(ts->ts_runq, td, flags); 52373daf66fSJeff Roberson } 52473daf66fSJeff Roberson 52573daf66fSJeff Roberson /* 526ae7a6b38SJeff Roberson * Remove a thread from a run-queue. This typically happens when a thread 527ae7a6b38SJeff Roberson * is selected to run. Running threads are not on the queue and the 528ae7a6b38SJeff Roberson * transferable count does not reflect them. 529ae7a6b38SJeff Roberson */ 530155b9987SJeff Roberson static __inline void 5319727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td) 532155b9987SJeff Roberson { 5339727e637SJeff Roberson struct td_sched *ts; 5349727e637SJeff Roberson 53593ccd6bfSKonstantin Belousov ts = td_get_sched(td); 536ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 53761a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 538ae7a6b38SJeff Roberson KASSERT(ts->ts_runq != NULL, 5399727e637SJeff Roberson ("tdq_runq_remove: thread %p null ts_runq", td)); 540ad1e7d28SJulian Elischer if (ts->ts_flags & TSF_XFERABLE) { 541d2ad694cSJeff Roberson tdq->tdq_transferable--; 542ad1e7d28SJulian Elischer ts->ts_flags &= ~TSF_XFERABLE; 54380f86c9fSJeff Roberson } 5443f872f85SJeff Roberson if (ts->ts_runq == &tdq->tdq_timeshare) { 5453f872f85SJeff Roberson if (tdq->tdq_idx != tdq->tdq_ridx) 5469727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 547e7d50326SJeff Roberson else 5489727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, NULL); 5493f872f85SJeff Roberson } else 5509727e637SJeff Roberson runq_remove(ts->ts_runq, td); 551155b9987SJeff Roberson } 552155b9987SJeff Roberson 553ae7a6b38SJeff Roberson /* 554ae7a6b38SJeff Roberson * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 555ae7a6b38SJeff Roberson * for this thread to the referenced thread queue. 556ae7a6b38SJeff Roberson */ 557a8949de2SJeff Roberson static void 5589727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td) 5595d7ef00cSJeff Roberson { 560ae7a6b38SJeff Roberson 561ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 56261a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 56303d17db7SJeff Roberson 564d2ad694cSJeff Roberson tdq->tdq_load++; 5651b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 566d2ad694cSJeff Roberson tdq->tdq_sysload++; 5678f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 568d9fae5abSAndriy Gapon SDT_PROBE2(sched, , , load__change, (int)TDQ_ID(tdq), tdq->tdq_load); 5695d7ef00cSJeff Roberson } 57015dc847eSJeff Roberson 571ae7a6b38SJeff Roberson /* 572ae7a6b38SJeff Roberson * Remove the load from a thread that is transitioning to a sleep state or 573ae7a6b38SJeff Roberson * exiting. 574ae7a6b38SJeff Roberson */ 575a8949de2SJeff Roberson static void 5769727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td) 5775d7ef00cSJeff Roberson { 578ae7a6b38SJeff Roberson 579ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 58061a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 581ae7a6b38SJeff Roberson KASSERT(tdq->tdq_load != 0, 582c47f202bSJeff Roberson ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 58303d17db7SJeff Roberson 584d2ad694cSJeff Roberson tdq->tdq_load--; 5851b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 58603d17db7SJeff Roberson tdq->tdq_sysload--; 5878f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 588d9fae5abSAndriy Gapon SDT_PROBE2(sched, , , load__change, (int)TDQ_ID(tdq), tdq->tdq_load); 58915dc847eSJeff Roberson } 59015dc847eSJeff Roberson 591356500a3SJeff Roberson /* 5925e5c3873SJeff Roberson * Bound timeshare latency by decreasing slice size as load increases. We 5935e5c3873SJeff Roberson * consider the maximum latency as the sum of the threads waiting to run 5945e5c3873SJeff Roberson * aside from curthread and target no more than sched_slice latency but 5955e5c3873SJeff Roberson * no less than sched_slice_min runtime. 5965e5c3873SJeff Roberson */ 5975e5c3873SJeff Roberson static inline int 5985e5c3873SJeff Roberson tdq_slice(struct tdq *tdq) 5995e5c3873SJeff Roberson { 6005e5c3873SJeff Roberson int load; 6015e5c3873SJeff Roberson 6025e5c3873SJeff Roberson /* 6035e5c3873SJeff Roberson * It is safe to use sys_load here because this is called from 6045e5c3873SJeff Roberson * contexts where timeshare threads are running and so there 6055e5c3873SJeff Roberson * cannot be higher priority load in the system. 6065e5c3873SJeff Roberson */ 6075e5c3873SJeff Roberson load = tdq->tdq_sysload - 1; 6085e5c3873SJeff Roberson if (load >= SCHED_SLICE_MIN_DIVISOR) 6095e5c3873SJeff Roberson return (sched_slice_min); 6105e5c3873SJeff Roberson if (load <= 1) 6115e5c3873SJeff Roberson return (sched_slice); 6125e5c3873SJeff Roberson return (sched_slice / load); 6135e5c3873SJeff Roberson } 6145e5c3873SJeff Roberson 6155e5c3873SJeff Roberson /* 61662fa74d9SJeff Roberson * Set lowpri to its exact value by searching the run-queue and 61762fa74d9SJeff Roberson * evaluating curthread. curthread may be passed as an optimization. 618356500a3SJeff Roberson */ 61922bf7d9aSJeff Roberson static void 62062fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd) 62162fa74d9SJeff Roberson { 62262fa74d9SJeff Roberson struct thread *td; 62362fa74d9SJeff Roberson 62462fa74d9SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 62562fa74d9SJeff Roberson if (ctd == NULL) 62611484ad8SMark Johnston ctd = tdq->tdq_curthread; 6279727e637SJeff Roberson td = tdq_choose(tdq); 6289727e637SJeff Roberson if (td == NULL || td->td_priority > ctd->td_priority) 62962fa74d9SJeff Roberson tdq->tdq_lowpri = ctd->td_priority; 63062fa74d9SJeff Roberson else 63162fa74d9SJeff Roberson tdq->tdq_lowpri = td->td_priority; 63262fa74d9SJeff Roberson } 63362fa74d9SJeff Roberson 63462fa74d9SJeff Roberson #ifdef SMP 6359129dd59SPedro F. Giffuni /* 6369129dd59SPedro F. Giffuni * We need some randomness. Implement a classic Linear Congruential 6379129dd59SPedro F. Giffuni * Generator X_{n+1}=(aX_n+c) mod m. These values are optimized for 6389129dd59SPedro F. Giffuni * m = 2^32, a = 69069 and c = 5. We only return the upper 16 bits 6399129dd59SPedro F. Giffuni * of the random state (in the low bits of our answer) to keep 6409129dd59SPedro F. Giffuni * the maximum randomness. 6419129dd59SPedro F. Giffuni */ 6429129dd59SPedro F. Giffuni static uint32_t 6439129dd59SPedro F. Giffuni sched_random(void) 6449129dd59SPedro F. Giffuni { 6459129dd59SPedro F. Giffuni uint32_t *rndptr; 6469129dd59SPedro F. Giffuni 6479129dd59SPedro F. Giffuni rndptr = DPCPU_PTR(randomval); 6489129dd59SPedro F. Giffuni *rndptr = *rndptr * 69069 + 5; 6499129dd59SPedro F. Giffuni 6509129dd59SPedro F. Giffuni return (*rndptr >> 16); 6519129dd59SPedro F. Giffuni } 6529129dd59SPedro F. Giffuni 65362fa74d9SJeff Roberson struct cpu_search { 654e745d729SAlexander Motin cpuset_t *cs_mask; /* The mask of allowed CPUs to choose from. */ 655e745d729SAlexander Motin int cs_prefer; /* Prefer this CPU and groups including it. */ 656e745d729SAlexander Motin int cs_running; /* The thread is now running at cs_prefer. */ 65736acfc65SAlexander Motin int cs_pri; /* Min priority for low. */ 65808063e9fSAlexander Motin int cs_load; /* Max load for low, min load for high. */ 65908063e9fSAlexander Motin int cs_trans; /* Min transferable load for high. */ 660aefe0a8cSAlexander Motin }; 661aefe0a8cSAlexander Motin 662aefe0a8cSAlexander Motin struct cpu_search_res { 66308063e9fSAlexander Motin int csr_cpu; /* The best CPU found. */ 66408063e9fSAlexander Motin int csr_load; /* The load of cs_cpu. */ 66562fa74d9SJeff Roberson }; 66662fa74d9SJeff Roberson 66762fa74d9SJeff Roberson /* 668aefe0a8cSAlexander Motin * Search the tree of cpu_groups for the lowest or highest loaded CPU. 669aefe0a8cSAlexander Motin * These routines actually compare the load on all paths through the tree 670aefe0a8cSAlexander Motin * and find the least loaded cpu on the least loaded path, which may differ 671aefe0a8cSAlexander Motin * from the least loaded cpu in the system. This balances work among caches 672aefe0a8cSAlexander Motin * and buses. 67362fa74d9SJeff Roberson */ 674aefe0a8cSAlexander Motin static int 675aefe0a8cSAlexander Motin cpu_search_lowest(const struct cpu_group *cg, const struct cpu_search *s, 676aefe0a8cSAlexander Motin struct cpu_search_res *r) 67762fa74d9SJeff Roberson { 678aefe0a8cSAlexander Motin struct cpu_search_res lr; 67936acfc65SAlexander Motin struct tdq *tdq; 680e745d729SAlexander Motin int c, bload, l, load, p, total; 68162fa74d9SJeff Roberson 68236acfc65SAlexander Motin total = 0; 683aefe0a8cSAlexander Motin bload = INT_MAX; 68408063e9fSAlexander Motin r->csr_cpu = -1; 68536acfc65SAlexander Motin 686aefe0a8cSAlexander Motin /* Loop through children CPU groups if there are any. */ 687aefe0a8cSAlexander Motin if (cg->cg_children > 0) { 688aefe0a8cSAlexander Motin for (c = cg->cg_children - 1; c >= 0; c--) { 689aefe0a8cSAlexander Motin load = cpu_search_lowest(&cg->cg_child[c], s, &lr); 69036acfc65SAlexander Motin total += load; 691e745d729SAlexander Motin 692e745d729SAlexander Motin /* 693e745d729SAlexander Motin * When balancing do not prefer SMT groups with load >1. 694e745d729SAlexander Motin * It allows round-robin between SMT groups with equal 695e745d729SAlexander Motin * load within parent group for more fair scheduling. 696e745d729SAlexander Motin */ 697e745d729SAlexander Motin if (__predict_false(s->cs_running) && 698e745d729SAlexander Motin (cg->cg_child[c].cg_flags & CG_FLAG_THREAD) && 699e745d729SAlexander Motin load >= 128 && (load & 128) != 0) 700e745d729SAlexander Motin load += 128; 701e745d729SAlexander Motin 70208063e9fSAlexander Motin if (lr.csr_cpu >= 0 && (load < bload || 70308063e9fSAlexander Motin (load == bload && lr.csr_load < r->csr_load))) { 704aefe0a8cSAlexander Motin bload = load; 70508063e9fSAlexander Motin r->csr_cpu = lr.csr_cpu; 70608063e9fSAlexander Motin r->csr_load = lr.csr_load; 70736acfc65SAlexander Motin } 70836acfc65SAlexander Motin } 70962fa74d9SJeff Roberson return (total); 71062fa74d9SJeff Roberson } 71162fa74d9SJeff Roberson 712aefe0a8cSAlexander Motin /* Loop through children CPUs otherwise. */ 713aefe0a8cSAlexander Motin for (c = cg->cg_last; c >= cg->cg_first; c--) { 714aefe0a8cSAlexander Motin if (!CPU_ISSET(c, &cg->cg_mask)) 715aefe0a8cSAlexander Motin continue; 716aefe0a8cSAlexander Motin tdq = TDQ_CPU(c); 71711484ad8SMark Johnston l = TDQ_LOAD(tdq); 718e745d729SAlexander Motin if (c == s->cs_prefer) { 719e745d729SAlexander Motin if (__predict_false(s->cs_running)) 720e745d729SAlexander Motin l--; 721e745d729SAlexander Motin p = 128; 722e745d729SAlexander Motin } else 723e745d729SAlexander Motin p = 0; 724aefe0a8cSAlexander Motin load = l * 256; 725e745d729SAlexander Motin total += load - p; 726e745d729SAlexander Motin 727e745d729SAlexander Motin /* 728e745d729SAlexander Motin * Check this CPU is acceptable. 729e745d729SAlexander Motin * If the threads is already on the CPU, don't look on the TDQ 730e745d729SAlexander Motin * priority, since it can be the priority of the thread itself. 731e745d729SAlexander Motin */ 73211484ad8SMark Johnston if (l > s->cs_load || 73311484ad8SMark Johnston (atomic_load_char(&tdq->tdq_lowpri) <= s->cs_pri && 734e745d729SAlexander Motin (!s->cs_running || c != s->cs_prefer)) || 735aefe0a8cSAlexander Motin !CPU_ISSET(c, s->cs_mask)) 736aefe0a8cSAlexander Motin continue; 737e745d729SAlexander Motin 738e745d729SAlexander Motin /* 739e745d729SAlexander Motin * When balancing do not prefer CPUs with load > 1. 740e745d729SAlexander Motin * It allows round-robin between CPUs with equal load 741e745d729SAlexander Motin * within the CPU group for more fair scheduling. 742e745d729SAlexander Motin */ 743e745d729SAlexander Motin if (__predict_false(s->cs_running) && l > 0) 744e745d729SAlexander Motin p = 0; 745e745d729SAlexander Motin 746aefe0a8cSAlexander Motin load -= sched_random() % 128; 747e745d729SAlexander Motin if (bload > load - p) { 748e745d729SAlexander Motin bload = load - p; 74908063e9fSAlexander Motin r->csr_cpu = c; 75008063e9fSAlexander Motin r->csr_load = load; 751aefe0a8cSAlexander Motin } 752aefe0a8cSAlexander Motin } 753aefe0a8cSAlexander Motin return (total); 75462fa74d9SJeff Roberson } 75562fa74d9SJeff Roberson 756aefe0a8cSAlexander Motin static int 757aefe0a8cSAlexander Motin cpu_search_highest(const struct cpu_group *cg, const struct cpu_search *s, 758aefe0a8cSAlexander Motin struct cpu_search_res *r) 75962fa74d9SJeff Roberson { 760aefe0a8cSAlexander Motin struct cpu_search_res lr; 761aefe0a8cSAlexander Motin struct tdq *tdq; 762aefe0a8cSAlexander Motin int c, bload, l, load, total; 763aefe0a8cSAlexander Motin 764aefe0a8cSAlexander Motin total = 0; 765aefe0a8cSAlexander Motin bload = INT_MIN; 76608063e9fSAlexander Motin r->csr_cpu = -1; 767aefe0a8cSAlexander Motin 768aefe0a8cSAlexander Motin /* Loop through children CPU groups if there are any. */ 769aefe0a8cSAlexander Motin if (cg->cg_children > 0) { 770aefe0a8cSAlexander Motin for (c = cg->cg_children - 1; c >= 0; c--) { 771aefe0a8cSAlexander Motin load = cpu_search_highest(&cg->cg_child[c], s, &lr); 772aefe0a8cSAlexander Motin total += load; 77308063e9fSAlexander Motin if (lr.csr_cpu >= 0 && (load > bload || 77408063e9fSAlexander Motin (load == bload && lr.csr_load > r->csr_load))) { 775aefe0a8cSAlexander Motin bload = load; 77608063e9fSAlexander Motin r->csr_cpu = lr.csr_cpu; 77708063e9fSAlexander Motin r->csr_load = lr.csr_load; 778aefe0a8cSAlexander Motin } 779aefe0a8cSAlexander Motin } 780aefe0a8cSAlexander Motin return (total); 78162fa74d9SJeff Roberson } 78262fa74d9SJeff Roberson 783aefe0a8cSAlexander Motin /* Loop through children CPUs otherwise. */ 784aefe0a8cSAlexander Motin for (c = cg->cg_last; c >= cg->cg_first; c--) { 785aefe0a8cSAlexander Motin if (!CPU_ISSET(c, &cg->cg_mask)) 786aefe0a8cSAlexander Motin continue; 787aefe0a8cSAlexander Motin tdq = TDQ_CPU(c); 78811484ad8SMark Johnston l = TDQ_LOAD(tdq); 789aefe0a8cSAlexander Motin load = l * 256; 790aefe0a8cSAlexander Motin total += load; 791e745d729SAlexander Motin 792e745d729SAlexander Motin /* 793e745d729SAlexander Motin * Check this CPU is acceptable. 794e745d729SAlexander Motin */ 79511484ad8SMark Johnston if (l < s->cs_load || TDQ_TRANSFERABLE(tdq) < s->cs_trans || 796aefe0a8cSAlexander Motin !CPU_ISSET(c, s->cs_mask)) 797aefe0a8cSAlexander Motin continue; 798e745d729SAlexander Motin 799ca34553bSAlexander Motin load -= sched_random() % 256; 800aefe0a8cSAlexander Motin if (load > bload) { 801aefe0a8cSAlexander Motin bload = load; 80208063e9fSAlexander Motin r->csr_cpu = c; 803aefe0a8cSAlexander Motin } 804aefe0a8cSAlexander Motin } 80508063e9fSAlexander Motin r->csr_load = bload; 806aefe0a8cSAlexander Motin return (total); 80762fa74d9SJeff Roberson } 80862fa74d9SJeff Roberson 80962fa74d9SJeff Roberson /* 81062fa74d9SJeff Roberson * Find the cpu with the least load via the least loaded path that has a 81162fa74d9SJeff Roberson * lowpri greater than pri pri. A pri of -1 indicates any priority is 81262fa74d9SJeff Roberson * acceptable. 81362fa74d9SJeff Roberson */ 81462fa74d9SJeff Roberson static inline int 815aefe0a8cSAlexander Motin sched_lowest(const struct cpu_group *cg, cpuset_t *mask, int pri, int maxload, 816e745d729SAlexander Motin int prefer, int running) 81762fa74d9SJeff Roberson { 818aefe0a8cSAlexander Motin struct cpu_search s; 819aefe0a8cSAlexander Motin struct cpu_search_res r; 82062fa74d9SJeff Roberson 821aefe0a8cSAlexander Motin s.cs_prefer = prefer; 822e745d729SAlexander Motin s.cs_running = running; 823aefe0a8cSAlexander Motin s.cs_mask = mask; 824aefe0a8cSAlexander Motin s.cs_pri = pri; 82508063e9fSAlexander Motin s.cs_load = maxload; 826aefe0a8cSAlexander Motin cpu_search_lowest(cg, &s, &r); 82708063e9fSAlexander Motin return (r.csr_cpu); 82862fa74d9SJeff Roberson } 82962fa74d9SJeff Roberson 83062fa74d9SJeff Roberson /* 83162fa74d9SJeff Roberson * Find the cpu with the highest load via the highest loaded path. 83262fa74d9SJeff Roberson */ 83362fa74d9SJeff Roberson static inline int 83408063e9fSAlexander Motin sched_highest(const struct cpu_group *cg, cpuset_t *mask, int minload, 83508063e9fSAlexander Motin int mintrans) 83662fa74d9SJeff Roberson { 837aefe0a8cSAlexander Motin struct cpu_search s; 838aefe0a8cSAlexander Motin struct cpu_search_res r; 83962fa74d9SJeff Roberson 840aefe0a8cSAlexander Motin s.cs_mask = mask; 84108063e9fSAlexander Motin s.cs_load = minload; 84208063e9fSAlexander Motin s.cs_trans = mintrans; 843aefe0a8cSAlexander Motin cpu_search_highest(cg, &s, &r); 84408063e9fSAlexander Motin return (r.csr_cpu); 84562fa74d9SJeff Roberson } 84662fa74d9SJeff Roberson 84762fa74d9SJeff Roberson static void 84862fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg) 84962fa74d9SJeff Roberson { 850018ff686SJeff Roberson struct tdq *tdq; 851e745d729SAlexander Motin struct thread *td; 85236acfc65SAlexander Motin cpuset_t hmask, lmask; 85336acfc65SAlexander Motin int high, low, anylow; 85462fa74d9SJeff Roberson 85536acfc65SAlexander Motin CPU_FILL(&hmask); 85662fa74d9SJeff Roberson for (;;) { 85708063e9fSAlexander Motin high = sched_highest(cg, &hmask, 1, 0); 85836acfc65SAlexander Motin /* Stop if there is no more CPU with transferrable threads. */ 85936acfc65SAlexander Motin if (high == -1) 86062fa74d9SJeff Roberson break; 86136acfc65SAlexander Motin CPU_CLR(high, &hmask); 86236acfc65SAlexander Motin CPU_COPY(&hmask, &lmask); 86336acfc65SAlexander Motin /* Stop if there is no more CPU left for low. */ 86436acfc65SAlexander Motin if (CPU_EMPTY(&lmask)) 86562fa74d9SJeff Roberson break; 866018ff686SJeff Roberson tdq = TDQ_CPU(high); 86711484ad8SMark Johnston if (TDQ_LOAD(tdq) == 1) { 868e745d729SAlexander Motin /* 869e745d729SAlexander Motin * There is only one running thread. We can't move 870e745d729SAlexander Motin * it from here, so tell it to pick new CPU by itself. 871e745d729SAlexander Motin */ 872e745d729SAlexander Motin TDQ_LOCK(tdq); 87311484ad8SMark Johnston td = tdq->tdq_curthread; 874e745d729SAlexander Motin if ((td->td_flags & TDF_IDLETD) == 0 && 875e745d729SAlexander Motin THREAD_CAN_MIGRATE(td)) { 876e745d729SAlexander Motin td->td_flags |= TDF_NEEDRESCHED | TDF_PICKCPU; 877e745d729SAlexander Motin if (high != curcpu) 878e745d729SAlexander Motin ipi_cpu(high, IPI_AST); 879e745d729SAlexander Motin } 880e745d729SAlexander Motin TDQ_UNLOCK(tdq); 881e745d729SAlexander Motin break; 882e745d729SAlexander Motin } 883e745d729SAlexander Motin anylow = 1; 88436acfc65SAlexander Motin nextlow: 88511484ad8SMark Johnston if (TDQ_TRANSFERABLE(tdq) == 0) 886e745d729SAlexander Motin continue; 88711484ad8SMark Johnston low = sched_lowest(cg, &lmask, -1, TDQ_LOAD(tdq) - 1, high, 1); 88836acfc65SAlexander Motin /* Stop if we looked well and found no less loaded CPU. */ 88936acfc65SAlexander Motin if (anylow && low == -1) 89036acfc65SAlexander Motin break; 89136acfc65SAlexander Motin /* Go to next high if we found no less loaded CPU. */ 89236acfc65SAlexander Motin if (low == -1) 89336acfc65SAlexander Motin continue; 89436acfc65SAlexander Motin /* Transfer thread from high to low. */ 895018ff686SJeff Roberson if (sched_balance_pair(tdq, TDQ_CPU(low))) { 89636acfc65SAlexander Motin /* CPU that got thread can no longer be a donor. */ 89736acfc65SAlexander Motin CPU_CLR(low, &hmask); 89836acfc65SAlexander Motin } else { 89962fa74d9SJeff Roberson /* 90036acfc65SAlexander Motin * If failed, then there is no threads on high 90136acfc65SAlexander Motin * that can run on this low. Drop low from low 90236acfc65SAlexander Motin * mask and look for different one. 90362fa74d9SJeff Roberson */ 90436acfc65SAlexander Motin CPU_CLR(low, &lmask); 90536acfc65SAlexander Motin anylow = 0; 90636acfc65SAlexander Motin goto nextlow; 90762fa74d9SJeff Roberson } 90836acfc65SAlexander Motin } 90962fa74d9SJeff Roberson } 91062fa74d9SJeff Roberson 91162fa74d9SJeff Roberson static void 91262375ca8SEd Schouten sched_balance(void) 913356500a3SJeff Roberson { 9147fcf154aSJeff Roberson struct tdq *tdq; 915356500a3SJeff Roberson 9160567b6ccSWarner Losh balance_ticks = max(balance_interval / 2, 1) + 917b250ad34SWarner Losh (sched_random() % balance_interval); 9187fcf154aSJeff Roberson tdq = TDQ_SELF(); 9197fcf154aSJeff Roberson TDQ_UNLOCK(tdq); 92062fa74d9SJeff Roberson sched_balance_group(cpu_top); 9217fcf154aSJeff Roberson TDQ_LOCK(tdq); 922cac77d04SJeff Roberson } 92386f8ae96SJeff Roberson 924ae7a6b38SJeff Roberson /* 925ae7a6b38SJeff Roberson * Lock two thread queues using their address to maintain lock order. 926ae7a6b38SJeff Roberson */ 927ae7a6b38SJeff Roberson static void 928ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two) 929ae7a6b38SJeff Roberson { 930ae7a6b38SJeff Roberson if (one < two) { 931ae7a6b38SJeff Roberson TDQ_LOCK(one); 932ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(two, MTX_DUPOK); 933ae7a6b38SJeff Roberson } else { 934ae7a6b38SJeff Roberson TDQ_LOCK(two); 935ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(one, MTX_DUPOK); 936ae7a6b38SJeff Roberson } 937ae7a6b38SJeff Roberson } 938ae7a6b38SJeff Roberson 939ae7a6b38SJeff Roberson /* 9407fcf154aSJeff Roberson * Unlock two thread queues. Order is not important here. 9417fcf154aSJeff Roberson */ 9427fcf154aSJeff Roberson static void 9437fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two) 9447fcf154aSJeff Roberson { 9457fcf154aSJeff Roberson TDQ_UNLOCK(one); 9467fcf154aSJeff Roberson TDQ_UNLOCK(two); 9477fcf154aSJeff Roberson } 9487fcf154aSJeff Roberson 9497fcf154aSJeff Roberson /* 9506d3f74a1SMark Johnston * Transfer load between two imbalanced thread queues. Returns true if a thread 9516d3f74a1SMark Johnston * was moved between the queues, and false otherwise. 952ae7a6b38SJeff Roberson */ 9536d3f74a1SMark Johnston static bool 954ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low) 955cac77d04SJeff Roberson { 9566d3f74a1SMark Johnston int cpu, lowpri; 9576d3f74a1SMark Johnston bool ret; 958cac77d04SJeff Roberson 9596d3f74a1SMark Johnston ret = false; 960ae7a6b38SJeff Roberson tdq_lock_pair(high, low); 9616d3f74a1SMark Johnston 962155b9987SJeff Roberson /* 96397e9382dSDon Lewis * Transfer a thread from high to low. 964155b9987SJeff Roberson */ 9656d3f74a1SMark Johnston if (high->tdq_transferable != 0 && high->tdq_load > low->tdq_load) { 9666d3f74a1SMark Johnston lowpri = tdq_move(high, low); 9676d3f74a1SMark Johnston if (lowpri != -1) { 968a5423ea3SJeff Roberson /* 9690927ff78SMark Johnston * In case the target isn't the current CPU notify it of 9706d3f74a1SMark Johnston * the new load, possibly sending an IPI to force it to 9710927ff78SMark Johnston * reschedule. Otherwise maybe schedule a preemption. 972a5423ea3SJeff Roberson */ 973880bf8b9SMarius Strobl cpu = TDQ_ID(low); 974880bf8b9SMarius Strobl if (cpu != PCPU_GET(cpuid)) 9756d3f74a1SMark Johnston tdq_notify(low, lowpri); 9760927ff78SMark Johnston else 9770927ff78SMark Johnston sched_setpreempt(low->tdq_lowpri); 9786d3f74a1SMark Johnston ret = true; 9796d3f74a1SMark Johnston } 980ae7a6b38SJeff Roberson } 9817fcf154aSJeff Roberson tdq_unlock_pair(high, low); 9826d3f74a1SMark Johnston return (ret); 983356500a3SJeff Roberson } 984356500a3SJeff Roberson 985ae7a6b38SJeff Roberson /* 9866d3f74a1SMark Johnston * Move a thread from one thread queue to another. Returns -1 if the source 9876d3f74a1SMark Johnston * queue was empty, else returns the maximum priority of all threads in 9886d3f74a1SMark Johnston * the destination queue prior to the addition of the new thread. In the latter 9896d3f74a1SMark Johnston * case, this priority can be used to determine whether an IPI needs to be 9906d3f74a1SMark Johnston * delivered. 991ae7a6b38SJeff Roberson */ 9926d3f74a1SMark Johnston static int 993ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to) 994356500a3SJeff Roberson { 995ae7a6b38SJeff Roberson struct thread *td; 996ae7a6b38SJeff Roberson int cpu; 997356500a3SJeff Roberson 9987fcf154aSJeff Roberson TDQ_LOCK_ASSERT(from, MA_OWNED); 9997fcf154aSJeff Roberson TDQ_LOCK_ASSERT(to, MA_OWNED); 10007fcf154aSJeff Roberson 1001ae7a6b38SJeff Roberson cpu = TDQ_ID(to); 100235dd6d6cSMark Johnston td = tdq_steal(from, cpu); 10039727e637SJeff Roberson if (td == NULL) 10046d3f74a1SMark Johnston return (-1); 100561a74c5cSJeff Roberson 1006ae7a6b38SJeff Roberson /* 100761a74c5cSJeff Roberson * Although the run queue is locked the thread may be 100861a74c5cSJeff Roberson * blocked. We can not set the lock until it is unblocked. 1009ae7a6b38SJeff Roberson */ 101061a74c5cSJeff Roberson thread_lock_block_wait(td); 1011ae7a6b38SJeff Roberson sched_rem(td); 101261a74c5cSJeff Roberson THREAD_LOCKPTR_ASSERT(td, TDQ_LOCKPTR(from)); 1013ae7a6b38SJeff Roberson td->td_lock = TDQ_LOCKPTR(to); 101461a74c5cSJeff Roberson td_get_sched(td)->ts_cpu = cpu; 10156d3f74a1SMark Johnston return (tdq_add(to, td, SRQ_YIELDING)); 1016356500a3SJeff Roberson } 101722bf7d9aSJeff Roberson 1018ae7a6b38SJeff Roberson /* 1019ae7a6b38SJeff Roberson * This tdq has idled. Try to steal a thread from another cpu and switch 1020ae7a6b38SJeff Roberson * to it. 1021ae7a6b38SJeff Roberson */ 102280f86c9fSJeff Roberson static int 1023ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq) 102422bf7d9aSJeff Roberson { 10252668bb2aSAlexander Motin struct cpu_group *cg, *parent; 1026ad1e7d28SJulian Elischer struct tdq *steal; 1027c76ee827SJeff Roberson cpuset_t mask; 10282668bb2aSAlexander Motin int cpu, switchcnt, goup; 102980f86c9fSJeff Roberson 103097e9382dSDon Lewis if (smp_started == 0 || steal_idle == 0 || tdq->tdq_cg == NULL) 103188f530ccSJeff Roberson return (1); 1032c76ee827SJeff Roberson CPU_FILL(&mask); 1033c76ee827SJeff Roberson CPU_CLR(PCPU_GET(cpuid), &mask); 103497e9382dSDon Lewis restart: 103511484ad8SMark Johnston switchcnt = TDQ_SWITCHCNT(tdq); 10362668bb2aSAlexander Motin for (cg = tdq->tdq_cg, goup = 0; ; ) { 103708063e9fSAlexander Motin cpu = sched_highest(cg, &mask, steal_thresh, 1); 103897e9382dSDon Lewis /* 103997e9382dSDon Lewis * We were assigned a thread but not preempted. Returning 104097e9382dSDon Lewis * 0 here will cause our caller to switch to it. 104197e9382dSDon Lewis */ 104211484ad8SMark Johnston if (TDQ_LOAD(tdq)) 104397e9382dSDon Lewis return (0); 10442668bb2aSAlexander Motin 10452668bb2aSAlexander Motin /* 10462668bb2aSAlexander Motin * We found no CPU to steal from in this group. Escalate to 10472668bb2aSAlexander Motin * the parent and repeat. But if parent has only two children 10482668bb2aSAlexander Motin * groups we can avoid searching this group again by searching 10492668bb2aSAlexander Motin * the other one specifically and then escalating two levels. 10502668bb2aSAlexander Motin */ 105162fa74d9SJeff Roberson if (cpu == -1) { 10522668bb2aSAlexander Motin if (goup) { 105362fa74d9SJeff Roberson cg = cg->cg_parent; 10542668bb2aSAlexander Motin goup = 0; 10552668bb2aSAlexander Motin } 10562668bb2aSAlexander Motin parent = cg->cg_parent; 10572668bb2aSAlexander Motin if (parent == NULL) 105897e9382dSDon Lewis return (1); 10592668bb2aSAlexander Motin if (parent->cg_children == 2) { 10602668bb2aSAlexander Motin if (cg == &parent->cg_child[0]) 10612668bb2aSAlexander Motin cg = &parent->cg_child[1]; 10622668bb2aSAlexander Motin else 10632668bb2aSAlexander Motin cg = &parent->cg_child[0]; 10642668bb2aSAlexander Motin goup = 1; 10652668bb2aSAlexander Motin } else 10662668bb2aSAlexander Motin cg = parent; 106780f86c9fSJeff Roberson continue; 10687b8bfa0dSJeff Roberson } 10697b8bfa0dSJeff Roberson steal = TDQ_CPU(cpu); 107097e9382dSDon Lewis /* 107197e9382dSDon Lewis * The data returned by sched_highest() is stale and 107297e9382dSDon Lewis * the chosen CPU no longer has an eligible thread. 107397e9382dSDon Lewis * 107497e9382dSDon Lewis * Testing this ahead of tdq_lock_pair() only catches 107597e9382dSDon Lewis * this situation about 20% of the time on an 8 core 107697e9382dSDon Lewis * 16 thread Ryzen 7, but it still helps performance. 107797e9382dSDon Lewis */ 107811484ad8SMark Johnston if (TDQ_LOAD(steal) < steal_thresh || 107911484ad8SMark Johnston TDQ_TRANSFERABLE(steal) == 0) 108097e9382dSDon Lewis goto restart; 108197e9382dSDon Lewis /* 10828bb173fbSAlexander Motin * Try to lock both queues. If we are assigned a thread while 10838bb173fbSAlexander Motin * waited for the lock, switch to it now instead of stealing. 10848bb173fbSAlexander Motin * If we can't get the lock, then somebody likely got there 10858bb173fbSAlexander Motin * first so continue searching. 108697e9382dSDon Lewis */ 10878bb173fbSAlexander Motin TDQ_LOCK(tdq); 10888bb173fbSAlexander Motin if (tdq->tdq_load > 0) { 10898bb173fbSAlexander Motin mi_switch(SW_VOL | SWT_IDLE); 10908bb173fbSAlexander Motin return (0); 10918bb173fbSAlexander Motin } 10928bb173fbSAlexander Motin if (TDQ_TRYLOCK_FLAGS(steal, MTX_DUPOK) == 0) { 10938bb173fbSAlexander Motin TDQ_UNLOCK(tdq); 10948bb173fbSAlexander Motin CPU_CLR(cpu, &mask); 10958bb173fbSAlexander Motin continue; 10968bb173fbSAlexander Motin } 109797e9382dSDon Lewis /* 109897e9382dSDon Lewis * The data returned by sched_highest() is stale and 109997e9382dSDon Lewis * the chosen CPU no longer has an eligible thread, or 110097e9382dSDon Lewis * we were preempted and the CPU loading info may be out 110197e9382dSDon Lewis * of date. The latter is rare. In either case restart 110297e9382dSDon Lewis * the search. 110397e9382dSDon Lewis */ 110411484ad8SMark Johnston if (TDQ_LOAD(steal) < steal_thresh || 110511484ad8SMark Johnston TDQ_TRANSFERABLE(steal) == 0 || 110611484ad8SMark Johnston switchcnt != TDQ_SWITCHCNT(tdq)) { 11077fcf154aSJeff Roberson tdq_unlock_pair(tdq, steal); 110897e9382dSDon Lewis goto restart; 110962fa74d9SJeff Roberson } 111062fa74d9SJeff Roberson /* 111197e9382dSDon Lewis * Steal the thread and switch to it. 111262fa74d9SJeff Roberson */ 11136d3f74a1SMark Johnston if (tdq_move(steal, tdq) != -1) 111497e9382dSDon Lewis break; 111597e9382dSDon Lewis /* 111697e9382dSDon Lewis * We failed to acquire a thread even though it looked 111797e9382dSDon Lewis * like one was available. This could be due to affinity 111897e9382dSDon Lewis * restrictions or for other reasons. Loop again after 111997e9382dSDon Lewis * removing this CPU from the set. The restart logic 112097e9382dSDon Lewis * above does not restore this CPU to the set due to the 112197e9382dSDon Lewis * likelyhood of failing here again. 112297e9382dSDon Lewis */ 112397e9382dSDon Lewis CPU_CLR(cpu, &mask); 112462fa74d9SJeff Roberson tdq_unlock_pair(tdq, steal); 112580f86c9fSJeff Roberson } 1126ae7a6b38SJeff Roberson TDQ_UNLOCK(steal); 1127686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IDLE); 11287b8bfa0dSJeff Roberson return (0); 112922bf7d9aSJeff Roberson } 113022bf7d9aSJeff Roberson 1131ae7a6b38SJeff Roberson /* 1132ae7a6b38SJeff Roberson * Notify a remote cpu of new work. Sends an IPI if criteria are met. 11336d3f74a1SMark Johnston * 11346d3f74a1SMark Johnston * "lowpri" is the minimum scheduling priority among all threads on 11356d3f74a1SMark Johnston * the queue prior to the addition of the new thread. 1136ae7a6b38SJeff Roberson */ 113722bf7d9aSJeff Roberson static void 11386d3f74a1SMark Johnston tdq_notify(struct tdq *tdq, int lowpri) 113922bf7d9aSJeff Roberson { 11407b8bfa0dSJeff Roberson int cpu; 114122bf7d9aSJeff Roberson 11426d3f74a1SMark Johnston TDQ_LOCK_ASSERT(tdq, MA_OWNED); 11436d3f74a1SMark Johnston KASSERT(tdq->tdq_lowpri <= lowpri, 11446d3f74a1SMark Johnston ("tdq_notify: lowpri %d > tdq_lowpri %d", lowpri, tdq->tdq_lowpri)); 11456d3f74a1SMark Johnston 11467789ab32SMark Johnston if (tdq->tdq_owepreempt) 1147ff256d9cSJeff Roberson return; 11486d3f74a1SMark Johnston 11496d3f74a1SMark Johnston /* 11506d3f74a1SMark Johnston * Check to see if the newly added thread should preempt the one 11516d3f74a1SMark Johnston * currently running. 11526d3f74a1SMark Johnston */ 11536d3f74a1SMark Johnston if (!sched_shouldpreempt(tdq->tdq_lowpri, lowpri, 1)) 11546b2f763fSJeff Roberson return; 115579654969SAlexander Motin 115679654969SAlexander Motin /* 1157ae9e9b4fSAlexander Motin * Make sure that our caller's earlier update to tdq_load is 1158ae9e9b4fSAlexander Motin * globally visible before we read tdq_cpu_idle. Idle thread 115979654969SAlexander Motin * accesses both of them without locks, and the order is important. 116079654969SAlexander Motin */ 1161e8677f38SKonstantin Belousov atomic_thread_fence_seq_cst(); 116279654969SAlexander Motin 11631690c6c1SJeff Roberson /* 11646d3f74a1SMark Johnston * Try to figure out if we can signal the idle thread instead of sending 11656d3f74a1SMark Johnston * an IPI. This check is racy; at worst, we will deliever an IPI 11666d3f74a1SMark Johnston * unnecessarily. 11676c47aaaeSJeff Roberson */ 11686d3f74a1SMark Johnston cpu = TDQ_ID(tdq); 11696d3f74a1SMark Johnston if (TD_IS_IDLETHREAD(tdq->tdq_curthread) && 117011484ad8SMark Johnston (atomic_load_int(&tdq->tdq_cpu_idle) == 0 || cpu_idle_wakeup(cpu))) 11716c47aaaeSJeff Roberson return; 11727789ab32SMark Johnston 11737789ab32SMark Johnston /* 11747789ab32SMark Johnston * The run queues have been updated, so any switch on the remote CPU 11757789ab32SMark Johnston * will satisfy the preemption request. 11767789ab32SMark Johnston */ 11777789ab32SMark Johnston tdq->tdq_owepreempt = 1; 1178d9d8d144SJohn Baldwin ipi_cpu(cpu, IPI_PREEMPT); 117922bf7d9aSJeff Roberson } 118022bf7d9aSJeff Roberson 1181ae7a6b38SJeff Roberson /* 1182ae7a6b38SJeff Roberson * Steals load from a timeshare queue. Honors the rotating queue head 1183ae7a6b38SJeff Roberson * index. 1184ae7a6b38SJeff Roberson */ 11859727e637SJeff Roberson static struct thread * 118662fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start) 1187ae7a6b38SJeff Roberson { 1188ae7a6b38SJeff Roberson struct rqbits *rqb; 1189ae7a6b38SJeff Roberson struct rqhead *rqh; 119036acfc65SAlexander Motin struct thread *td, *first; 1191ae7a6b38SJeff Roberson int bit; 1192ae7a6b38SJeff Roberson int i; 1193ae7a6b38SJeff Roberson 1194ae7a6b38SJeff Roberson rqb = &rq->rq_status; 1195ae7a6b38SJeff Roberson bit = start & (RQB_BPW -1); 119636acfc65SAlexander Motin first = NULL; 1197ae7a6b38SJeff Roberson again: 1198ae7a6b38SJeff Roberson for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 1199ae7a6b38SJeff Roberson if (rqb->rqb_bits[i] == 0) 1200ae7a6b38SJeff Roberson continue; 12018bc713f6SJeff Roberson if (bit == 0) 12028bc713f6SJeff Roberson bit = RQB_FFS(rqb->rqb_bits[i]); 12038bc713f6SJeff Roberson for (; bit < RQB_BPW; bit++) { 12048bc713f6SJeff Roberson if ((rqb->rqb_bits[i] & (1ul << bit)) == 0) 1205ae7a6b38SJeff Roberson continue; 12068bc713f6SJeff Roberson rqh = &rq->rq_queues[bit + (i << RQB_L2BPW)]; 12079727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 1208bd84094aSAlexander Motin if (first) { 1209bd84094aSAlexander Motin if (THREAD_CAN_MIGRATE(td) && 12109727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 12119727e637SJeff Roberson return (td); 1212bd84094aSAlexander Motin } else 121336acfc65SAlexander Motin first = td; 1214ae7a6b38SJeff Roberson } 1215ae7a6b38SJeff Roberson } 12168bc713f6SJeff Roberson } 1217ae7a6b38SJeff Roberson if (start != 0) { 1218ae7a6b38SJeff Roberson start = 0; 1219ae7a6b38SJeff Roberson goto again; 1220ae7a6b38SJeff Roberson } 1221ae7a6b38SJeff Roberson 122236acfc65SAlexander Motin if (first && THREAD_CAN_MIGRATE(first) && 122336acfc65SAlexander Motin THREAD_CAN_SCHED(first, cpu)) 122436acfc65SAlexander Motin return (first); 1225ae7a6b38SJeff Roberson return (NULL); 1226ae7a6b38SJeff Roberson } 1227ae7a6b38SJeff Roberson 1228ae7a6b38SJeff Roberson /* 1229ae7a6b38SJeff Roberson * Steals load from a standard linear queue. 1230ae7a6b38SJeff Roberson */ 12319727e637SJeff Roberson static struct thread * 123262fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu) 123322bf7d9aSJeff Roberson { 123422bf7d9aSJeff Roberson struct rqhead *rqh; 123522bf7d9aSJeff Roberson struct rqbits *rqb; 12369727e637SJeff Roberson struct thread *td; 123722bf7d9aSJeff Roberson int word; 123822bf7d9aSJeff Roberson int bit; 123922bf7d9aSJeff Roberson 124022bf7d9aSJeff Roberson rqb = &rq->rq_status; 124122bf7d9aSJeff Roberson for (word = 0; word < RQB_LEN; word++) { 124222bf7d9aSJeff Roberson if (rqb->rqb_bits[word] == 0) 124322bf7d9aSJeff Roberson continue; 124422bf7d9aSJeff Roberson for (bit = 0; bit < RQB_BPW; bit++) { 1245a2640c9bSPeter Wemm if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 124622bf7d9aSJeff Roberson continue; 124722bf7d9aSJeff Roberson rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 12489727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) 12499727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td) && 12509727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 12519727e637SJeff Roberson return (td); 125222bf7d9aSJeff Roberson } 125322bf7d9aSJeff Roberson } 125422bf7d9aSJeff Roberson return (NULL); 125522bf7d9aSJeff Roberson } 125622bf7d9aSJeff Roberson 1257ae7a6b38SJeff Roberson /* 1258ae7a6b38SJeff Roberson * Attempt to steal a thread in priority order from a thread queue. 1259ae7a6b38SJeff Roberson */ 12609727e637SJeff Roberson static struct thread * 126162fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu) 126222bf7d9aSJeff Roberson { 12639727e637SJeff Roberson struct thread *td; 126422bf7d9aSJeff Roberson 1265ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 12669727e637SJeff Roberson if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 12679727e637SJeff Roberson return (td); 12689727e637SJeff Roberson if ((td = runq_steal_from(&tdq->tdq_timeshare, 12699727e637SJeff Roberson cpu, tdq->tdq_ridx)) != NULL) 12709727e637SJeff Roberson return (td); 127162fa74d9SJeff Roberson return (runq_steal(&tdq->tdq_idle, cpu)); 127222bf7d9aSJeff Roberson } 127380f86c9fSJeff Roberson 1274ae7a6b38SJeff Roberson /* 1275ae7a6b38SJeff Roberson * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 12767fcf154aSJeff Roberson * current lock and returns with the assigned queue locked. 1277ae7a6b38SJeff Roberson */ 1278ae7a6b38SJeff Roberson static inline struct tdq * 12799727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags) 128080f86c9fSJeff Roberson { 12819727e637SJeff Roberson 1282ae7a6b38SJeff Roberson struct tdq *tdq; 128361a74c5cSJeff Roberson struct mtx *mtx; 128480f86c9fSJeff Roberson 12859727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1286ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpu); 128793ccd6bfSKonstantin Belousov td_get_sched(td)->ts_cpu = cpu; 12889727e637SJeff Roberson /* 12899727e637SJeff Roberson * If the lock matches just return the queue. 12909727e637SJeff Roberson */ 129161a74c5cSJeff Roberson if (td->td_lock == TDQ_LOCKPTR(tdq)) { 129261a74c5cSJeff Roberson KASSERT((flags & SRQ_HOLD) == 0, 129361a74c5cSJeff Roberson ("sched_setcpu: Invalid lock for SRQ_HOLD")); 1294ae7a6b38SJeff Roberson return (tdq); 1295ae7a6b38SJeff Roberson } 129661a74c5cSJeff Roberson 129780f86c9fSJeff Roberson /* 1298ae7a6b38SJeff Roberson * The hard case, migration, we need to block the thread first to 1299ae7a6b38SJeff Roberson * prevent order reversals with other cpus locks. 13007b8bfa0dSJeff Roberson */ 1301b0b9dee5SAttilio Rao spinlock_enter(); 130261a74c5cSJeff Roberson mtx = thread_lock_block(td); 130361a74c5cSJeff Roberson if ((flags & SRQ_HOLD) == 0) 130461a74c5cSJeff Roberson mtx_unlock_spin(mtx); 1305ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1306ae7a6b38SJeff Roberson thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1307b0b9dee5SAttilio Rao spinlock_exit(); 1308ae7a6b38SJeff Roberson return (tdq); 130980f86c9fSJeff Roberson } 13102454aaf5SJeff Roberson 13118df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 13128df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 13138df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 13148df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 13158df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 13168df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 13178df78c41SJeff Roberson 1318ae7a6b38SJeff Roberson static int 13199727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags) 1320ae7a6b38SJeff Roberson { 132136acfc65SAlexander Motin struct cpu_group *cg, *ccg; 13229727e637SJeff Roberson struct td_sched *ts; 1323ae7a6b38SJeff Roberson struct tdq *tdq; 1324aefe0a8cSAlexander Motin cpuset_t *mask; 1325e745d729SAlexander Motin int cpu, pri, r, self, intr; 13267b8bfa0dSJeff Roberson 132762fa74d9SJeff Roberson self = PCPU_GET(cpuid); 132893ccd6bfSKonstantin Belousov ts = td_get_sched(td); 1329efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(ts->ts_cpu), ("sched_pickcpu: Start scheduler on " 1330efe67753SNathan Whitehorn "absent CPU %d for thread %s.", ts->ts_cpu, td->td_name)); 13317b8bfa0dSJeff Roberson if (smp_started == 0) 13327b8bfa0dSJeff Roberson return (self); 133328994a58SJeff Roberson /* 133428994a58SJeff Roberson * Don't migrate a running thread from sched_switch(). 133528994a58SJeff Roberson */ 133662fa74d9SJeff Roberson if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 133762fa74d9SJeff Roberson return (ts->ts_cpu); 13387b8bfa0dSJeff Roberson /* 133962fa74d9SJeff Roberson * Prefer to run interrupt threads on the processors that generate 134062fa74d9SJeff Roberson * the interrupt. 13417b8bfa0dSJeff Roberson */ 134262fa74d9SJeff Roberson if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 1343c9205e35SAlexander Motin curthread->td_intr_nesting_level) { 1344c55dc51cSAlexander Motin tdq = TDQ_SELF(); 1345c55dc51cSAlexander Motin if (tdq->tdq_lowpri >= PRI_MIN_IDLE) { 1346c55dc51cSAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 1347c55dc51cSAlexander Motin return (self); 1348c55dc51cSAlexander Motin } 134962fa74d9SJeff Roberson ts->ts_cpu = self; 1350c9205e35SAlexander Motin intr = 1; 1351c55dc51cSAlexander Motin cg = tdq->tdq_cg; 1352c55dc51cSAlexander Motin goto llc; 1353c55dc51cSAlexander Motin } else { 1354c9205e35SAlexander Motin intr = 0; 1355c55dc51cSAlexander Motin tdq = TDQ_CPU(ts->ts_cpu); 1356c55dc51cSAlexander Motin cg = tdq->tdq_cg; 1357c55dc51cSAlexander Motin } 13587b8bfa0dSJeff Roberson /* 135936acfc65SAlexander Motin * If the thread can run on the last cpu and the affinity has not 13600127914cSEric van Gyzen * expired and it is idle, run it there. 13617b8bfa0dSJeff Roberson */ 136236acfc65SAlexander Motin if (THREAD_CAN_SCHED(td, ts->ts_cpu) && 1363*6cbc4cebSMark Johnston atomic_load_char(&tdq->tdq_lowpri) >= PRI_MIN_IDLE && 136436acfc65SAlexander Motin SCHED_AFFINITY(ts, CG_SHARE_L2)) { 1365c55dc51cSAlexander Motin if (cg->cg_flags & CG_FLAG_THREAD) { 1366176dd236SAlexander Motin /* Check all SMT threads for being idle. */ 1367aefe0a8cSAlexander Motin for (cpu = cg->cg_first; cpu <= cg->cg_last; cpu++) { 136811484ad8SMark Johnston pri = 136911484ad8SMark Johnston atomic_load_char(&TDQ_CPU(cpu)->tdq_lowpri); 1370176dd236SAlexander Motin if (CPU_ISSET(cpu, &cg->cg_mask) && 137111484ad8SMark Johnston pri < PRI_MIN_IDLE) 137262fa74d9SJeff Roberson break; 1373aefe0a8cSAlexander Motin } 1374aefe0a8cSAlexander Motin if (cpu > cg->cg_last) { 1375176dd236SAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 1376176dd236SAlexander Motin return (ts->ts_cpu); 137736acfc65SAlexander Motin } 1378176dd236SAlexander Motin } else { 137936acfc65SAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 138036acfc65SAlexander Motin return (ts->ts_cpu); 138136acfc65SAlexander Motin } 138236acfc65SAlexander Motin } 1383c55dc51cSAlexander Motin llc: 138436acfc65SAlexander Motin /* 138536acfc65SAlexander Motin * Search for the last level cache CPU group in the tree. 1386c9205e35SAlexander Motin * Skip SMT, identical groups and caches with expired affinity. 1387c9205e35SAlexander Motin * Interrupt threads affinity is explicit and never expires. 138836acfc65SAlexander Motin */ 138936acfc65SAlexander Motin for (ccg = NULL; cg != NULL; cg = cg->cg_parent) { 139036acfc65SAlexander Motin if (cg->cg_flags & CG_FLAG_THREAD) 139136acfc65SAlexander Motin continue; 1392c9205e35SAlexander Motin if (cg->cg_children == 1 || cg->cg_count == 1) 1393c9205e35SAlexander Motin continue; 1394c9205e35SAlexander Motin if (cg->cg_level == CG_SHARE_NONE || 1395c9205e35SAlexander Motin (!intr && !SCHED_AFFINITY(ts, cg->cg_level))) 139636acfc65SAlexander Motin continue; 139736acfc65SAlexander Motin ccg = cg; 139836acfc65SAlexander Motin } 1399c9205e35SAlexander Motin /* Found LLC shared by all CPUs, so do a global search. */ 1400c9205e35SAlexander Motin if (ccg == cpu_top) 1401c9205e35SAlexander Motin ccg = NULL; 140262fa74d9SJeff Roberson cpu = -1; 1403aefe0a8cSAlexander Motin mask = &td->td_cpuset->cs_mask; 1404c9205e35SAlexander Motin pri = td->td_priority; 1405e745d729SAlexander Motin r = TD_IS_RUNNING(td); 1406c9205e35SAlexander Motin /* 1407c9205e35SAlexander Motin * Try hard to keep interrupts within found LLC. Search the LLC for 1408c9205e35SAlexander Motin * the least loaded CPU we can run now. For NUMA systems it should 1409c9205e35SAlexander Motin * be within target domain, and it also reduces scheduling overhead. 1410c9205e35SAlexander Motin */ 1411c9205e35SAlexander Motin if (ccg != NULL && intr) { 1412e745d729SAlexander Motin cpu = sched_lowest(ccg, mask, pri, INT_MAX, ts->ts_cpu, r); 1413c9205e35SAlexander Motin if (cpu >= 0) 1414c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_intrbind); 1415c9205e35SAlexander Motin } else 1416c9205e35SAlexander Motin /* Search the LLC for the least loaded idle CPU we can run now. */ 1417c9205e35SAlexander Motin if (ccg != NULL) { 1418c9205e35SAlexander Motin cpu = sched_lowest(ccg, mask, max(pri, PRI_MAX_TIMESHARE), 1419e745d729SAlexander Motin INT_MAX, ts->ts_cpu, r); 1420c9205e35SAlexander Motin if (cpu >= 0) 1421c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_affinity); 1422c9205e35SAlexander Motin } 1423c9205e35SAlexander Motin /* Search globally for the least loaded CPU we can run now. */ 1424c9205e35SAlexander Motin if (cpu < 0) { 1425e745d729SAlexander Motin cpu = sched_lowest(cpu_top, mask, pri, INT_MAX, ts->ts_cpu, r); 1426c9205e35SAlexander Motin if (cpu >= 0) 1427c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_lowest); 1428c9205e35SAlexander Motin } 1429c9205e35SAlexander Motin /* Search globally for the least loaded CPU. */ 1430c9205e35SAlexander Motin if (cpu < 0) { 1431e745d729SAlexander Motin cpu = sched_lowest(cpu_top, mask, -1, INT_MAX, ts->ts_cpu, r); 1432c9205e35SAlexander Motin if (cpu >= 0) 1433c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_lowest); 1434c9205e35SAlexander Motin } 1435bb3dfc6aSAlexander Motin KASSERT(cpu >= 0, ("sched_pickcpu: Failed to find a cpu.")); 1436efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(cpu), ("sched_pickcpu: Picked absent CPU %d.", cpu)); 143762fa74d9SJeff Roberson /* 143862fa74d9SJeff Roberson * Compare the lowest loaded cpu to current cpu. 143962fa74d9SJeff Roberson */ 1440018ff686SJeff Roberson tdq = TDQ_CPU(cpu); 1441018ff686SJeff Roberson if (THREAD_CAN_SCHED(td, self) && TDQ_SELF()->tdq_lowpri > pri && 144211484ad8SMark Johnston atomic_load_char(&tdq->tdq_lowpri) < PRI_MIN_IDLE && 144311484ad8SMark Johnston TDQ_LOAD(TDQ_SELF()) <= TDQ_LOAD(tdq) + 1) { 14448df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_local); 144562fa74d9SJeff Roberson cpu = self; 1446c9205e35SAlexander Motin } 14478df78c41SJeff Roberson if (cpu != ts->ts_cpu) 14488df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_migration); 1449ae7a6b38SJeff Roberson return (cpu); 145080f86c9fSJeff Roberson } 145162fa74d9SJeff Roberson #endif 145222bf7d9aSJeff Roberson 145322bf7d9aSJeff Roberson /* 145422bf7d9aSJeff Roberson * Pick the highest priority task we have and return it. 14550c0a98b2SJeff Roberson */ 14569727e637SJeff Roberson static struct thread * 1457ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq) 14585d7ef00cSJeff Roberson { 14599727e637SJeff Roberson struct thread *td; 14605d7ef00cSJeff Roberson 1461ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 14629727e637SJeff Roberson td = runq_choose(&tdq->tdq_realtime); 14639727e637SJeff Roberson if (td != NULL) 14649727e637SJeff Roberson return (td); 14659727e637SJeff Roberson td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 14669727e637SJeff Roberson if (td != NULL) { 146712d56c0fSJohn Baldwin KASSERT(td->td_priority >= PRI_MIN_BATCH, 1468e7d50326SJeff Roberson ("tdq_choose: Invalid priority on timeshare queue %d", 14699727e637SJeff Roberson td->td_priority)); 14709727e637SJeff Roberson return (td); 147115dc847eSJeff Roberson } 14729727e637SJeff Roberson td = runq_choose(&tdq->tdq_idle); 14739727e637SJeff Roberson if (td != NULL) { 14749727e637SJeff Roberson KASSERT(td->td_priority >= PRI_MIN_IDLE, 1475e7d50326SJeff Roberson ("tdq_choose: Invalid priority on idle queue %d", 14769727e637SJeff Roberson td->td_priority)); 14779727e637SJeff Roberson return (td); 1478e7d50326SJeff Roberson } 1479e7d50326SJeff Roberson 1480e7d50326SJeff Roberson return (NULL); 1481245f3abfSJeff Roberson } 14820a016a05SJeff Roberson 1483ae7a6b38SJeff Roberson /* 1484ae7a6b38SJeff Roberson * Initialize a thread queue. 1485ae7a6b38SJeff Roberson */ 14860a016a05SJeff Roberson static void 1487018ff686SJeff Roberson tdq_setup(struct tdq *tdq, int id) 14880a016a05SJeff Roberson { 1489ae7a6b38SJeff Roberson 1490c47f202bSJeff Roberson if (bootverbose) 1491018ff686SJeff Roberson printf("ULE: setup cpu %d\n", id); 1492e7d50326SJeff Roberson runq_init(&tdq->tdq_realtime); 1493e7d50326SJeff Roberson runq_init(&tdq->tdq_timeshare); 1494d2ad694cSJeff Roberson runq_init(&tdq->tdq_idle); 1495018ff686SJeff Roberson tdq->tdq_id = id; 149662fa74d9SJeff Roberson snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 149762fa74d9SJeff Roberson "sched lock %d", (int)TDQ_ID(tdq)); 149861a74c5cSJeff Roberson mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", MTX_SPIN); 14998f51ad55SJeff Roberson #ifdef KTR 15008f51ad55SJeff Roberson snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 15018f51ad55SJeff Roberson "CPU %d load", (int)TDQ_ID(tdq)); 15028f51ad55SJeff Roberson #endif 15030a016a05SJeff Roberson } 15040a016a05SJeff Roberson 1505c47f202bSJeff Roberson #ifdef SMP 1506c47f202bSJeff Roberson static void 1507c47f202bSJeff Roberson sched_setup_smp(void) 1508c47f202bSJeff Roberson { 1509c47f202bSJeff Roberson struct tdq *tdq; 1510c47f202bSJeff Roberson int i; 1511c47f202bSJeff Roberson 151262fa74d9SJeff Roberson cpu_top = smp_topo(); 15133aa6d94eSJohn Baldwin CPU_FOREACH(i) { 1514018ff686SJeff Roberson tdq = DPCPU_ID_PTR(i, tdq); 1515018ff686SJeff Roberson tdq_setup(tdq, i); 151662fa74d9SJeff Roberson tdq->tdq_cg = smp_topo_find(cpu_top, i); 151762fa74d9SJeff Roberson if (tdq->tdq_cg == NULL) 151862fa74d9SJeff Roberson panic("Can't find cpu group for %d\n", i); 1519ca34553bSAlexander Motin DPCPU_ID_SET(i, randomval, i * 69069 + 5); 1520c47f202bSJeff Roberson } 1521018ff686SJeff Roberson PCPU_SET(sched, DPCPU_PTR(tdq)); 152262fa74d9SJeff Roberson balance_tdq = TDQ_SELF(); 1523c47f202bSJeff Roberson } 1524c47f202bSJeff Roberson #endif 1525c47f202bSJeff Roberson 1526ae7a6b38SJeff Roberson /* 1527ae7a6b38SJeff Roberson * Setup the thread queues and initialize the topology based on MD 1528ae7a6b38SJeff Roberson * information. 1529ae7a6b38SJeff Roberson */ 153035e6168fSJeff Roberson static void 153135e6168fSJeff Roberson sched_setup(void *dummy) 153235e6168fSJeff Roberson { 1533ae7a6b38SJeff Roberson struct tdq *tdq; 1534c47f202bSJeff Roberson 15350ec896fdSJeff Roberson #ifdef SMP 1536c47f202bSJeff Roberson sched_setup_smp(); 1537749d01b0SJeff Roberson #else 1538018ff686SJeff Roberson tdq_setup(TDQ_SELF(), 0); 1539356500a3SJeff Roberson #endif 1540018ff686SJeff Roberson tdq = TDQ_SELF(); 1541ae7a6b38SJeff Roberson 1542ae7a6b38SJeff Roberson /* Add thread0's load since it's running. */ 1543ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1544e1504695SJeff Roberson thread0.td_lock = TDQ_LOCKPTR(tdq); 15459727e637SJeff Roberson tdq_load_add(tdq, &thread0); 15466d3f74a1SMark Johnston tdq->tdq_curthread = &thread0; 154762fa74d9SJeff Roberson tdq->tdq_lowpri = thread0.td_priority; 1548ae7a6b38SJeff Roberson TDQ_UNLOCK(tdq); 154935e6168fSJeff Roberson } 155035e6168fSJeff Roberson 1551ae7a6b38SJeff Roberson /* 1552579895dfSAlexander Motin * This routine determines time constants after stathz and hz are setup. 1553ae7a6b38SJeff Roberson */ 1554a1d4fe69SDavid Xu /* ARGSUSED */ 1555a1d4fe69SDavid Xu static void 1556a1d4fe69SDavid Xu sched_initticks(void *dummy) 1557a1d4fe69SDavid Xu { 1558ae7a6b38SJeff Roberson int incr; 1559ae7a6b38SJeff Roberson 1560a1d4fe69SDavid Xu realstathz = stathz ? stathz : hz; 15615e5c3873SJeff Roberson sched_slice = realstathz / SCHED_SLICE_DEFAULT_DIVISOR; 15625e5c3873SJeff Roberson sched_slice_min = sched_slice / SCHED_SLICE_MIN_DIVISOR; 156337f4e025SAlexander Motin hogticks = imax(1, (2 * hz * sched_slice + realstathz / 2) / 156437f4e025SAlexander Motin realstathz); 1565a1d4fe69SDavid Xu 1566a1d4fe69SDavid Xu /* 1567e7d50326SJeff Roberson * tickincr is shifted out by 10 to avoid rounding errors due to 15683f872f85SJeff Roberson * hz not being evenly divisible by stathz on all platforms. 1569e7d50326SJeff Roberson */ 1570ae7a6b38SJeff Roberson incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1571e7d50326SJeff Roberson /* 1572e7d50326SJeff Roberson * This does not work for values of stathz that are more than 1573e7d50326SJeff Roberson * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1574a1d4fe69SDavid Xu */ 1575ae7a6b38SJeff Roberson if (incr == 0) 1576ae7a6b38SJeff Roberson incr = 1; 1577ae7a6b38SJeff Roberson tickincr = incr; 15787b8bfa0dSJeff Roberson #ifdef SMP 15799862717aSJeff Roberson /* 15807fcf154aSJeff Roberson * Set the default balance interval now that we know 15817fcf154aSJeff Roberson * what realstathz is. 15827fcf154aSJeff Roberson */ 15837fcf154aSJeff Roberson balance_interval = realstathz; 1584290d9060SDon Lewis balance_ticks = balance_interval; 15857b8bfa0dSJeff Roberson affinity = SCHED_AFFINITY_DEFAULT; 15867b8bfa0dSJeff Roberson #endif 1587b3f40a41SAlexander Motin if (sched_idlespinthresh < 0) 15882c27cb3aSAlexander Motin sched_idlespinthresh = 2 * max(10000, 6 * hz) / realstathz; 1589a1d4fe69SDavid Xu } 1590a1d4fe69SDavid Xu 159135e6168fSJeff Roberson /* 1592ae7a6b38SJeff Roberson * This is the core of the interactivity algorithm. Determines a score based 1593ae7a6b38SJeff Roberson * on past behavior. It is the ratio of sleep time to run time scaled to 1594ae7a6b38SJeff Roberson * a [0, 100] integer. This is the voluntary sleep time of a process, which 1595ae7a6b38SJeff Roberson * differs from the cpu usage because it does not account for time spent 1596ae7a6b38SJeff Roberson * waiting on a run-queue. Would be prettier if we had floating point. 159757031f79SGeorge V. Neville-Neil * 159857031f79SGeorge V. Neville-Neil * When a thread's sleep time is greater than its run time the 159957031f79SGeorge V. Neville-Neil * calculation is: 160057031f79SGeorge V. Neville-Neil * 160157031f79SGeorge V. Neville-Neil * scaling factor 160257031f79SGeorge V. Neville-Neil * interactivity score = --------------------- 160357031f79SGeorge V. Neville-Neil * sleep time / run time 160457031f79SGeorge V. Neville-Neil * 160557031f79SGeorge V. Neville-Neil * 160657031f79SGeorge V. Neville-Neil * When a thread's run time is greater than its sleep time the 160757031f79SGeorge V. Neville-Neil * calculation is: 160857031f79SGeorge V. Neville-Neil * 160957031f79SGeorge V. Neville-Neil * scaling factor 161043521b46Swiklam * interactivity score = 2 * scaling factor - --------------------- 161157031f79SGeorge V. Neville-Neil * run time / sleep time 1612ae7a6b38SJeff Roberson */ 1613ae7a6b38SJeff Roberson static int 1614ae7a6b38SJeff Roberson sched_interact_score(struct thread *td) 1615ae7a6b38SJeff Roberson { 1616ae7a6b38SJeff Roberson struct td_sched *ts; 1617ae7a6b38SJeff Roberson int div; 1618ae7a6b38SJeff Roberson 161993ccd6bfSKonstantin Belousov ts = td_get_sched(td); 1620ae7a6b38SJeff Roberson /* 1621ae7a6b38SJeff Roberson * The score is only needed if this is likely to be an interactive 1622ae7a6b38SJeff Roberson * task. Don't go through the expense of computing it if there's 1623ae7a6b38SJeff Roberson * no chance. 1624ae7a6b38SJeff Roberson */ 1625ae7a6b38SJeff Roberson if (sched_interact <= SCHED_INTERACT_HALF && 1626ae7a6b38SJeff Roberson ts->ts_runtime >= ts->ts_slptime) 1627ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1628ae7a6b38SJeff Roberson 1629ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1630ae7a6b38SJeff Roberson div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1631ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF + 1632ae7a6b38SJeff Roberson (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1633ae7a6b38SJeff Roberson } 1634ae7a6b38SJeff Roberson if (ts->ts_slptime > ts->ts_runtime) { 1635ae7a6b38SJeff Roberson div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1636ae7a6b38SJeff Roberson return (ts->ts_runtime / div); 1637ae7a6b38SJeff Roberson } 1638ae7a6b38SJeff Roberson /* runtime == slptime */ 1639ae7a6b38SJeff Roberson if (ts->ts_runtime) 1640ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1641ae7a6b38SJeff Roberson 1642ae7a6b38SJeff Roberson /* 1643ae7a6b38SJeff Roberson * This can happen if slptime and runtime are 0. 1644ae7a6b38SJeff Roberson */ 1645ae7a6b38SJeff Roberson return (0); 1646ae7a6b38SJeff Roberson 1647ae7a6b38SJeff Roberson } 1648ae7a6b38SJeff Roberson 1649ae7a6b38SJeff Roberson /* 165035e6168fSJeff Roberson * Scale the scheduling priority according to the "interactivity" of this 165135e6168fSJeff Roberson * process. 165235e6168fSJeff Roberson */ 165315dc847eSJeff Roberson static void 16548460a577SJohn Birrell sched_priority(struct thread *td) 165535e6168fSJeff Roberson { 16561c119e17SAlexander Motin u_int pri, score; 165735e6168fSJeff Roberson 1658c9a8cba4SJohn Baldwin if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 165915dc847eSJeff Roberson return; 1660e7d50326SJeff Roberson /* 1661e7d50326SJeff Roberson * If the score is interactive we place the thread in the realtime 1662e7d50326SJeff Roberson * queue with a priority that is less than kernel and interrupt 1663e7d50326SJeff Roberson * priorities. These threads are not subject to nice restrictions. 1664e7d50326SJeff Roberson * 1665ae7a6b38SJeff Roberson * Scores greater than this are placed on the normal timeshare queue 1666e7d50326SJeff Roberson * where the priority is partially decided by the most recent cpu 1667e7d50326SJeff Roberson * utilization and the rest is decided by nice value. 1668a5423ea3SJeff Roberson * 1669a5423ea3SJeff Roberson * The nice value of the process has a linear effect on the calculated 1670a5423ea3SJeff Roberson * score. Negative nice values make it easier for a thread to be 1671a5423ea3SJeff Roberson * considered interactive. 1672e7d50326SJeff Roberson */ 1673a0f15352SJohn Baldwin score = imax(0, sched_interact_score(td) + td->td_proc->p_nice); 1674e7d50326SJeff Roberson if (score < sched_interact) { 167512d56c0fSJohn Baldwin pri = PRI_MIN_INTERACT; 16761c119e17SAlexander Motin pri += (PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) * score / 16771c119e17SAlexander Motin sched_interact; 167812d56c0fSJohn Baldwin KASSERT(pri >= PRI_MIN_INTERACT && pri <= PRI_MAX_INTERACT, 16791c119e17SAlexander Motin ("sched_priority: invalid interactive priority %u score %u", 16809a93305aSJeff Roberson pri, score)); 1681e7d50326SJeff Roberson } else { 1682e7d50326SJeff Roberson pri = SCHED_PRI_MIN; 168393ccd6bfSKonstantin Belousov if (td_get_sched(td)->ts_ticks) 168493ccd6bfSKonstantin Belousov pri += min(SCHED_PRI_TICKS(td_get_sched(td)), 16855457fa23SJohn Baldwin SCHED_PRI_RANGE - 1); 1686e7d50326SJeff Roberson pri += SCHED_PRI_NICE(td->td_proc->p_nice); 168712d56c0fSJohn Baldwin KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH, 16881c119e17SAlexander Motin ("sched_priority: invalid priority %u: nice %d, " 1689ae7a6b38SJeff Roberson "ticks %d ftick %d ltick %d tick pri %d", 169093ccd6bfSKonstantin Belousov pri, td->td_proc->p_nice, td_get_sched(td)->ts_ticks, 169193ccd6bfSKonstantin Belousov td_get_sched(td)->ts_ftick, td_get_sched(td)->ts_ltick, 169293ccd6bfSKonstantin Belousov SCHED_PRI_TICKS(td_get_sched(td)))); 1693e7d50326SJeff Roberson } 16948460a577SJohn Birrell sched_user_prio(td, pri); 169535e6168fSJeff Roberson 169615dc847eSJeff Roberson return; 169735e6168fSJeff Roberson } 169835e6168fSJeff Roberson 169935e6168fSJeff Roberson /* 1700d322132cSJeff Roberson * This routine enforces a maximum limit on the amount of scheduling history 1701ae7a6b38SJeff Roberson * kept. It is called after either the slptime or runtime is adjusted. This 1702ae7a6b38SJeff Roberson * function is ugly due to integer math. 1703d322132cSJeff Roberson */ 17044b60e324SJeff Roberson static void 17058460a577SJohn Birrell sched_interact_update(struct thread *td) 17064b60e324SJeff Roberson { 1707155b6ca1SJeff Roberson struct td_sched *ts; 17089a93305aSJeff Roberson u_int sum; 17093f741ca1SJeff Roberson 171093ccd6bfSKonstantin Belousov ts = td_get_sched(td); 1711ae7a6b38SJeff Roberson sum = ts->ts_runtime + ts->ts_slptime; 1712d322132cSJeff Roberson if (sum < SCHED_SLP_RUN_MAX) 1713d322132cSJeff Roberson return; 1714d322132cSJeff Roberson /* 1715155b6ca1SJeff Roberson * This only happens from two places: 1716155b6ca1SJeff Roberson * 1) We have added an unusual amount of run time from fork_exit. 1717155b6ca1SJeff Roberson * 2) We have added an unusual amount of sleep time from sched_sleep(). 1718155b6ca1SJeff Roberson */ 1719155b6ca1SJeff Roberson if (sum > SCHED_SLP_RUN_MAX * 2) { 1720ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1721ae7a6b38SJeff Roberson ts->ts_runtime = SCHED_SLP_RUN_MAX; 1722ae7a6b38SJeff Roberson ts->ts_slptime = 1; 1723155b6ca1SJeff Roberson } else { 1724ae7a6b38SJeff Roberson ts->ts_slptime = SCHED_SLP_RUN_MAX; 1725ae7a6b38SJeff Roberson ts->ts_runtime = 1; 1726155b6ca1SJeff Roberson } 1727155b6ca1SJeff Roberson return; 1728155b6ca1SJeff Roberson } 1729155b6ca1SJeff Roberson /* 1730d322132cSJeff Roberson * If we have exceeded by more than 1/5th then the algorithm below 1731d322132cSJeff Roberson * will not bring us back into range. Dividing by two here forces 17322454aaf5SJeff Roberson * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1733d322132cSJeff Roberson */ 173437a35e4aSJeff Roberson if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1735ae7a6b38SJeff Roberson ts->ts_runtime /= 2; 1736ae7a6b38SJeff Roberson ts->ts_slptime /= 2; 1737d322132cSJeff Roberson return; 1738d322132cSJeff Roberson } 1739ae7a6b38SJeff Roberson ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1740ae7a6b38SJeff Roberson ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1741d322132cSJeff Roberson } 1742d322132cSJeff Roberson 1743ae7a6b38SJeff Roberson /* 1744ae7a6b38SJeff Roberson * Scale back the interactivity history when a child thread is created. The 1745ae7a6b38SJeff Roberson * history is inherited from the parent but the thread may behave totally 1746ae7a6b38SJeff Roberson * differently. For example, a shell spawning a compiler process. We want 1747ae7a6b38SJeff Roberson * to learn that the compiler is behaving badly very quickly. 1748ae7a6b38SJeff Roberson */ 1749d322132cSJeff Roberson static void 17508460a577SJohn Birrell sched_interact_fork(struct thread *td) 1751d322132cSJeff Roberson { 175293ccd6bfSKonstantin Belousov struct td_sched *ts; 1753d322132cSJeff Roberson int ratio; 1754d322132cSJeff Roberson int sum; 1755d322132cSJeff Roberson 175693ccd6bfSKonstantin Belousov ts = td_get_sched(td); 175793ccd6bfSKonstantin Belousov sum = ts->ts_runtime + ts->ts_slptime; 1758d322132cSJeff Roberson if (sum > SCHED_SLP_RUN_FORK) { 1759d322132cSJeff Roberson ratio = sum / SCHED_SLP_RUN_FORK; 176093ccd6bfSKonstantin Belousov ts->ts_runtime /= ratio; 176193ccd6bfSKonstantin Belousov ts->ts_slptime /= ratio; 17624b60e324SJeff Roberson } 17634b60e324SJeff Roberson } 17644b60e324SJeff Roberson 176515dc847eSJeff Roberson /* 1766ae7a6b38SJeff Roberson * Called from proc0_init() to setup the scheduler fields. 1767ed062c8dSJulian Elischer */ 1768ed062c8dSJulian Elischer void 1769ed062c8dSJulian Elischer schedinit(void) 1770ed062c8dSJulian Elischer { 177193ccd6bfSKonstantin Belousov struct td_sched *ts0; 1772e7d50326SJeff Roberson 1773ed062c8dSJulian Elischer /* 177493ccd6bfSKonstantin Belousov * Set up the scheduler specific parts of thread0. 1775ed062c8dSJulian Elischer */ 177693ccd6bfSKonstantin Belousov ts0 = td_get_sched(&thread0); 177793ccd6bfSKonstantin Belousov ts0->ts_ltick = ticks; 177893ccd6bfSKonstantin Belousov ts0->ts_ftick = ticks; 177993ccd6bfSKonstantin Belousov ts0->ts_slice = 0; 17801408b84aSHans Petter Selasky ts0->ts_cpu = curcpu; /* set valid CPU number */ 1781ed062c8dSJulian Elischer } 1782ed062c8dSJulian Elischer 1783ed062c8dSJulian Elischer /* 1784589aed00SKyle Evans * schedinit_ap() is needed prior to calling sched_throw(NULL) to ensure that 1785589aed00SKyle Evans * the pcpu requirements are met for any calls in the period between curthread 1786589aed00SKyle Evans * initialization and sched_throw(). One can safely add threads to the queue 1787589aed00SKyle Evans * before sched_throw(), for instance, as long as the thread lock is setup 1788589aed00SKyle Evans * correctly. 1789589aed00SKyle Evans * 1790589aed00SKyle Evans * TDQ_SELF() relies on the below sched pcpu setting; it may be used only 1791589aed00SKyle Evans * after schedinit_ap(). 1792589aed00SKyle Evans */ 1793589aed00SKyle Evans void 1794589aed00SKyle Evans schedinit_ap(void) 1795589aed00SKyle Evans { 1796589aed00SKyle Evans 1797589aed00SKyle Evans #ifdef SMP 1798589aed00SKyle Evans PCPU_SET(sched, DPCPU_PTR(tdq)); 1799589aed00SKyle Evans #endif 1800589aed00SKyle Evans PCPU_GET(idlethread)->td_lock = TDQ_LOCKPTR(TDQ_SELF()); 1801589aed00SKyle Evans } 1802589aed00SKyle Evans 1803589aed00SKyle Evans /* 180415dc847eSJeff Roberson * This is only somewhat accurate since given many processes of the same 180515dc847eSJeff Roberson * priority they will switch when their slices run out, which will be 1806e7d50326SJeff Roberson * at most sched_slice stathz ticks. 180715dc847eSJeff Roberson */ 180835e6168fSJeff Roberson int 180935e6168fSJeff Roberson sched_rr_interval(void) 181035e6168fSJeff Roberson { 1811e7d50326SJeff Roberson 1812579895dfSAlexander Motin /* Convert sched_slice from stathz to hz. */ 181337f4e025SAlexander Motin return (imax(1, (sched_slice * hz + realstathz / 2) / realstathz)); 181435e6168fSJeff Roberson } 181535e6168fSJeff Roberson 1816ae7a6b38SJeff Roberson /* 1817ae7a6b38SJeff Roberson * Update the percent cpu tracking information when it is requested or 1818ae7a6b38SJeff Roberson * the total history exceeds the maximum. We keep a sliding history of 1819ae7a6b38SJeff Roberson * tick counts that slowly decays. This is less precise than the 4BSD 1820ae7a6b38SJeff Roberson * mechanism since it happens with less regular and frequent events. 1821ae7a6b38SJeff Roberson */ 182222bf7d9aSJeff Roberson static void 18237295465eSAlexander Motin sched_pctcpu_update(struct td_sched *ts, int run) 182435e6168fSJeff Roberson { 18257295465eSAlexander Motin int t = ticks; 1826e7d50326SJeff Roberson 182778133024SMark Johnston /* 182878133024SMark Johnston * The signed difference may be negative if the thread hasn't run for 182978133024SMark Johnston * over half of the ticks rollover period. 183078133024SMark Johnston */ 183178133024SMark Johnston if ((u_int)(t - ts->ts_ltick) >= SCHED_TICK_TARG) { 1832ad1e7d28SJulian Elischer ts->ts_ticks = 0; 18337295465eSAlexander Motin ts->ts_ftick = t - SCHED_TICK_TARG; 18347295465eSAlexander Motin } else if (t - ts->ts_ftick >= SCHED_TICK_MAX) { 18357295465eSAlexander Motin ts->ts_ticks = (ts->ts_ticks / (ts->ts_ltick - ts->ts_ftick)) * 18367295465eSAlexander Motin (ts->ts_ltick - (t - SCHED_TICK_TARG)); 18377295465eSAlexander Motin ts->ts_ftick = t - SCHED_TICK_TARG; 18387295465eSAlexander Motin } 18397295465eSAlexander Motin if (run) 18407295465eSAlexander Motin ts->ts_ticks += (t - ts->ts_ltick) << SCHED_TICK_SHIFT; 18417295465eSAlexander Motin ts->ts_ltick = t; 184235e6168fSJeff Roberson } 184335e6168fSJeff Roberson 1844ae7a6b38SJeff Roberson /* 1845ae7a6b38SJeff Roberson * Adjust the priority of a thread. Move it to the appropriate run-queue 1846ae7a6b38SJeff Roberson * if necessary. This is the back-end for several priority related 1847ae7a6b38SJeff Roberson * functions. 1848ae7a6b38SJeff Roberson */ 1849e7d50326SJeff Roberson static void 1850f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio) 185135e6168fSJeff Roberson { 185273daf66fSJeff Roberson struct tdq *tdq; 185373daf66fSJeff Roberson int oldpri; 185435e6168fSJeff Roberson 18558f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 18568f51ad55SJeff Roberson "prio:%d", td->td_priority, "new prio:%d", prio, 18578f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(curthread)); 1858d9fae5abSAndriy Gapon SDT_PROBE3(sched, , , change__pri, td, td->td_proc, prio); 1859e87fc7cfSAndriy Gapon if (td != curthread && prio < td->td_priority) { 18608f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 18618f51ad55SJeff Roberson "lend prio", "prio:%d", td->td_priority, "new prio:%d", 18628f51ad55SJeff Roberson prio, KTR_ATTR_LINKED, sched_tdname(td)); 1863d9fae5abSAndriy Gapon SDT_PROBE4(sched, , , lend__pri, td, td->td_proc, prio, 1864b3e9e682SRyan Stone curthread); 18658f51ad55SJeff Roberson } 18667b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1867f5c157d9SJohn Baldwin if (td->td_priority == prio) 1868f5c157d9SJohn Baldwin return; 18693f741ca1SJeff Roberson /* 18703f741ca1SJeff Roberson * If the priority has been elevated due to priority 18713f741ca1SJeff Roberson * propagation, we may have to move ourselves to a new 1872e7d50326SJeff Roberson * queue. This could be optimized to not re-add in some 1873e7d50326SJeff Roberson * cases. 1874f2b74cbfSJeff Roberson */ 18756d55b3ecSJeff Roberson if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1876e7d50326SJeff Roberson sched_rem(td); 1877e7d50326SJeff Roberson td->td_priority = prio; 187861a74c5cSJeff Roberson sched_add(td, SRQ_BORROWING | SRQ_HOLDTD); 187973daf66fSJeff Roberson return; 188073daf66fSJeff Roberson } 18816d55b3ecSJeff Roberson /* 18826d55b3ecSJeff Roberson * If the thread is currently running we may have to adjust the lowpri 18836d55b3ecSJeff Roberson * information so other cpus are aware of our current priority. 18846d55b3ecSJeff Roberson */ 18856d55b3ecSJeff Roberson if (TD_IS_RUNNING(td)) { 18864aec1984SJohn Baldwin tdq = TDQ_CPU(td_get_sched(td)->ts_cpu); 188762fa74d9SJeff Roberson oldpri = td->td_priority; 18883f741ca1SJeff Roberson td->td_priority = prio; 188962fa74d9SJeff Roberson if (prio < tdq->tdq_lowpri) 189062fa74d9SJeff Roberson tdq->tdq_lowpri = prio; 189162fa74d9SJeff Roberson else if (tdq->tdq_lowpri == oldpri) 189262fa74d9SJeff Roberson tdq_setlowpri(tdq, td); 18936d55b3ecSJeff Roberson return; 189473daf66fSJeff Roberson } 18956d55b3ecSJeff Roberson td->td_priority = prio; 1896ae7a6b38SJeff Roberson } 189735e6168fSJeff Roberson 1898f5c157d9SJohn Baldwin /* 1899f5c157d9SJohn Baldwin * Update a thread's priority when it is lent another thread's 1900f5c157d9SJohn Baldwin * priority. 1901f5c157d9SJohn Baldwin */ 1902f5c157d9SJohn Baldwin void 1903f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio) 1904f5c157d9SJohn Baldwin { 1905f5c157d9SJohn Baldwin 1906f5c157d9SJohn Baldwin td->td_flags |= TDF_BORROWING; 1907f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1908f5c157d9SJohn Baldwin } 1909f5c157d9SJohn Baldwin 1910f5c157d9SJohn Baldwin /* 1911f5c157d9SJohn Baldwin * Restore a thread's priority when priority propagation is 1912f5c157d9SJohn Baldwin * over. The prio argument is the minimum priority the thread 1913f5c157d9SJohn Baldwin * needs to have to satisfy other possible priority lending 1914f5c157d9SJohn Baldwin * requests. If the thread's regular priority is less 1915f5c157d9SJohn Baldwin * important than prio, the thread will keep a priority boost 1916f5c157d9SJohn Baldwin * of prio. 1917f5c157d9SJohn Baldwin */ 1918f5c157d9SJohn Baldwin void 1919f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio) 1920f5c157d9SJohn Baldwin { 1921f5c157d9SJohn Baldwin u_char base_pri; 1922f5c157d9SJohn Baldwin 1923f5c157d9SJohn Baldwin if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1924f5c157d9SJohn Baldwin td->td_base_pri <= PRI_MAX_TIMESHARE) 19258460a577SJohn Birrell base_pri = td->td_user_pri; 1926f5c157d9SJohn Baldwin else 1927f5c157d9SJohn Baldwin base_pri = td->td_base_pri; 1928f5c157d9SJohn Baldwin if (prio >= base_pri) { 1929f5c157d9SJohn Baldwin td->td_flags &= ~TDF_BORROWING; 1930f5c157d9SJohn Baldwin sched_thread_priority(td, base_pri); 1931f5c157d9SJohn Baldwin } else 1932f5c157d9SJohn Baldwin sched_lend_prio(td, prio); 1933f5c157d9SJohn Baldwin } 1934f5c157d9SJohn Baldwin 1935ae7a6b38SJeff Roberson /* 1936ae7a6b38SJeff Roberson * Standard entry for setting the priority to an absolute value. 1937ae7a6b38SJeff Roberson */ 1938f5c157d9SJohn Baldwin void 1939f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio) 1940f5c157d9SJohn Baldwin { 1941f5c157d9SJohn Baldwin u_char oldprio; 1942f5c157d9SJohn Baldwin 1943f5c157d9SJohn Baldwin /* First, update the base priority. */ 1944f5c157d9SJohn Baldwin td->td_base_pri = prio; 1945f5c157d9SJohn Baldwin 1946f5c157d9SJohn Baldwin /* 194750aaa791SJohn Baldwin * If the thread is borrowing another thread's priority, don't 1948f5c157d9SJohn Baldwin * ever lower the priority. 1949f5c157d9SJohn Baldwin */ 1950f5c157d9SJohn Baldwin if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1951f5c157d9SJohn Baldwin return; 1952f5c157d9SJohn Baldwin 1953f5c157d9SJohn Baldwin /* Change the real priority. */ 1954f5c157d9SJohn Baldwin oldprio = td->td_priority; 1955f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1956f5c157d9SJohn Baldwin 1957f5c157d9SJohn Baldwin /* 1958f5c157d9SJohn Baldwin * If the thread is on a turnstile, then let the turnstile update 1959f5c157d9SJohn Baldwin * its state. 1960f5c157d9SJohn Baldwin */ 1961f5c157d9SJohn Baldwin if (TD_ON_LOCK(td) && oldprio != prio) 1962f5c157d9SJohn Baldwin turnstile_adjust(td, oldprio); 1963f5c157d9SJohn Baldwin } 1964f5c157d9SJohn Baldwin 1965ae7a6b38SJeff Roberson /* 1966ae7a6b38SJeff Roberson * Set the base user priority, does not effect current running priority. 1967ae7a6b38SJeff Roberson */ 196835e6168fSJeff Roberson void 19698460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio) 19703db720fdSDavid Xu { 19713db720fdSDavid Xu 19728460a577SJohn Birrell td->td_base_user_pri = prio; 1973acbe332aSDavid Xu if (td->td_lend_user_pri <= prio) 1974fc6c30f6SJulian Elischer return; 19758460a577SJohn Birrell td->td_user_pri = prio; 19763db720fdSDavid Xu } 19773db720fdSDavid Xu 19783db720fdSDavid Xu void 19793db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio) 19803db720fdSDavid Xu { 19813db720fdSDavid Xu 1982435806d3SDavid Xu THREAD_LOCK_ASSERT(td, MA_OWNED); 1983acbe332aSDavid Xu td->td_lend_user_pri = prio; 1984c8e368a9SDavid Xu td->td_user_pri = min(prio, td->td_base_user_pri); 1985c8e368a9SDavid Xu if (td->td_priority > td->td_user_pri) 1986c8e368a9SDavid Xu sched_prio(td, td->td_user_pri); 1987c8e368a9SDavid Xu else if (td->td_priority != td->td_user_pri) 1988c8e368a9SDavid Xu td->td_flags |= TDF_NEEDRESCHED; 1989435806d3SDavid Xu } 19903db720fdSDavid Xu 1991ac97da9aSMateusz Guzik /* 1992ac97da9aSMateusz Guzik * Like the above but first check if there is anything to do. 1993ac97da9aSMateusz Guzik */ 1994ac97da9aSMateusz Guzik void 1995ac97da9aSMateusz Guzik sched_lend_user_prio_cond(struct thread *td, u_char prio) 1996ac97da9aSMateusz Guzik { 1997ac97da9aSMateusz Guzik 1998ac97da9aSMateusz Guzik if (td->td_lend_user_pri != prio) 1999ac97da9aSMateusz Guzik goto lend; 2000ac97da9aSMateusz Guzik if (td->td_user_pri != min(prio, td->td_base_user_pri)) 2001ac97da9aSMateusz Guzik goto lend; 2002b77594bbSMateusz Guzik if (td->td_priority != td->td_user_pri) 2003ac97da9aSMateusz Guzik goto lend; 2004ac97da9aSMateusz Guzik return; 2005ac97da9aSMateusz Guzik 2006ac97da9aSMateusz Guzik lend: 2007ac97da9aSMateusz Guzik thread_lock(td); 2008ac97da9aSMateusz Guzik sched_lend_user_prio(td, prio); 2009ac97da9aSMateusz Guzik thread_unlock(td); 2010ac97da9aSMateusz Guzik } 2011ac97da9aSMateusz Guzik 20124c8a8cfcSKonstantin Belousov #ifdef SMP 2013ae7a6b38SJeff Roberson /* 201497e9382dSDon Lewis * This tdq is about to idle. Try to steal a thread from another CPU before 201597e9382dSDon Lewis * choosing the idle thread. 201697e9382dSDon Lewis */ 201797e9382dSDon Lewis static void 201897e9382dSDon Lewis tdq_trysteal(struct tdq *tdq) 201997e9382dSDon Lewis { 20202668bb2aSAlexander Motin struct cpu_group *cg, *parent; 202197e9382dSDon Lewis struct tdq *steal; 202297e9382dSDon Lewis cpuset_t mask; 20232668bb2aSAlexander Motin int cpu, i, goup; 202497e9382dSDon Lewis 202508063e9fSAlexander Motin if (smp_started == 0 || steal_idle == 0 || trysteal_limit == 0 || 202608063e9fSAlexander Motin tdq->tdq_cg == NULL) 202797e9382dSDon Lewis return; 202897e9382dSDon Lewis CPU_FILL(&mask); 202997e9382dSDon Lewis CPU_CLR(PCPU_GET(cpuid), &mask); 203097e9382dSDon Lewis /* We don't want to be preempted while we're iterating. */ 203197e9382dSDon Lewis spinlock_enter(); 203297e9382dSDon Lewis TDQ_UNLOCK(tdq); 20332668bb2aSAlexander Motin for (i = 1, cg = tdq->tdq_cg, goup = 0; ; ) { 203408063e9fSAlexander Motin cpu = sched_highest(cg, &mask, steal_thresh, 1); 203597e9382dSDon Lewis /* 203697e9382dSDon Lewis * If a thread was added while interrupts were disabled don't 203797e9382dSDon Lewis * steal one here. 203897e9382dSDon Lewis */ 203911484ad8SMark Johnston if (TDQ_LOAD(tdq) > 0) { 204097e9382dSDon Lewis TDQ_LOCK(tdq); 204197e9382dSDon Lewis break; 204297e9382dSDon Lewis } 20432668bb2aSAlexander Motin 20442668bb2aSAlexander Motin /* 20452668bb2aSAlexander Motin * We found no CPU to steal from in this group. Escalate to 20462668bb2aSAlexander Motin * the parent and repeat. But if parent has only two children 20472668bb2aSAlexander Motin * groups we can avoid searching this group again by searching 20482668bb2aSAlexander Motin * the other one specifically and then escalating two levels. 20492668bb2aSAlexander Motin */ 205097e9382dSDon Lewis if (cpu == -1) { 20512668bb2aSAlexander Motin if (goup) { 205297e9382dSDon Lewis cg = cg->cg_parent; 20532668bb2aSAlexander Motin goup = 0; 20542668bb2aSAlexander Motin } 20552668bb2aSAlexander Motin if (++i > trysteal_limit) { 205697e9382dSDon Lewis TDQ_LOCK(tdq); 205797e9382dSDon Lewis break; 205897e9382dSDon Lewis } 20592668bb2aSAlexander Motin parent = cg->cg_parent; 20602668bb2aSAlexander Motin if (parent == NULL) { 20612668bb2aSAlexander Motin TDQ_LOCK(tdq); 20622668bb2aSAlexander Motin break; 20632668bb2aSAlexander Motin } 20642668bb2aSAlexander Motin if (parent->cg_children == 2) { 20652668bb2aSAlexander Motin if (cg == &parent->cg_child[0]) 20662668bb2aSAlexander Motin cg = &parent->cg_child[1]; 20672668bb2aSAlexander Motin else 20682668bb2aSAlexander Motin cg = &parent->cg_child[0]; 20692668bb2aSAlexander Motin goup = 1; 20702668bb2aSAlexander Motin } else 20712668bb2aSAlexander Motin cg = parent; 207297e9382dSDon Lewis continue; 207397e9382dSDon Lewis } 207497e9382dSDon Lewis steal = TDQ_CPU(cpu); 207597e9382dSDon Lewis /* 207697e9382dSDon Lewis * The data returned by sched_highest() is stale and 207797e9382dSDon Lewis * the chosen CPU no longer has an eligible thread. 207815b5c347SGordon Bergling * At this point unconditionally exit the loop to bound 207908063e9fSAlexander Motin * the time spent in the critcal section. 208097e9382dSDon Lewis */ 208111484ad8SMark Johnston if (TDQ_LOAD(steal) < steal_thresh || 208211484ad8SMark Johnston TDQ_TRANSFERABLE(steal) == 0) 208397e9382dSDon Lewis continue; 208497e9382dSDon Lewis /* 20858bb173fbSAlexander Motin * Try to lock both queues. If we are assigned a thread while 20868bb173fbSAlexander Motin * waited for the lock, switch to it now instead of stealing. 20878bb173fbSAlexander Motin * If we can't get the lock, then somebody likely got there 208808063e9fSAlexander Motin * first. 208997e9382dSDon Lewis */ 20908bb173fbSAlexander Motin TDQ_LOCK(tdq); 20918bb173fbSAlexander Motin if (tdq->tdq_load > 0) 209297e9382dSDon Lewis break; 20938bb173fbSAlexander Motin if (TDQ_TRYLOCK_FLAGS(steal, MTX_DUPOK) == 0) 20948bb173fbSAlexander Motin break; 209597e9382dSDon Lewis /* 209697e9382dSDon Lewis * The data returned by sched_highest() is stale and 209797e9382dSDon Lewis * the chosen CPU no longer has an eligible thread. 209897e9382dSDon Lewis */ 209911484ad8SMark Johnston if (TDQ_LOAD(steal) < steal_thresh || 210011484ad8SMark Johnston TDQ_TRANSFERABLE(steal) == 0) { 210197e9382dSDon Lewis TDQ_UNLOCK(steal); 210297e9382dSDon Lewis break; 210397e9382dSDon Lewis } 210497e9382dSDon Lewis /* 210597e9382dSDon Lewis * If we fail to acquire one due to affinity restrictions, 210697e9382dSDon Lewis * bail out and let the idle thread to a more complete search 210797e9382dSDon Lewis * outside of a critical section. 210897e9382dSDon Lewis */ 21096d3f74a1SMark Johnston if (tdq_move(steal, tdq) == -1) { 211097e9382dSDon Lewis TDQ_UNLOCK(steal); 211197e9382dSDon Lewis break; 211297e9382dSDon Lewis } 211397e9382dSDon Lewis TDQ_UNLOCK(steal); 211497e9382dSDon Lewis break; 211597e9382dSDon Lewis } 211697e9382dSDon Lewis spinlock_exit(); 211797e9382dSDon Lewis } 21184c8a8cfcSKonstantin Belousov #endif 211997e9382dSDon Lewis 212097e9382dSDon Lewis /* 2121c47f202bSJeff Roberson * Handle migration from sched_switch(). This happens only for 2122c47f202bSJeff Roberson * cpu binding. 2123c47f202bSJeff Roberson */ 2124c47f202bSJeff Roberson static struct mtx * 2125c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 2126c47f202bSJeff Roberson { 2127c47f202bSJeff Roberson struct tdq *tdn; 21286d3f74a1SMark Johnston int lowpri; 2129c47f202bSJeff Roberson 2130686bcb5cSJeff Roberson KASSERT(THREAD_CAN_MIGRATE(td) || 2131686bcb5cSJeff Roberson (td_get_sched(td)->ts_flags & TSF_BOUND) != 0, 2132686bcb5cSJeff Roberson ("Thread %p shouldn't migrate", td)); 2133efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(td_get_sched(td)->ts_cpu), ("sched_switch_migrate: " 2134efe67753SNathan Whitehorn "thread %s queued on absent CPU %d.", td->td_name, 2135efe67753SNathan Whitehorn td_get_sched(td)->ts_cpu)); 213693ccd6bfSKonstantin Belousov tdn = TDQ_CPU(td_get_sched(td)->ts_cpu); 2137c47f202bSJeff Roberson #ifdef SMP 21389727e637SJeff Roberson tdq_load_rem(tdq, td); 2139c47f202bSJeff Roberson /* 2140686bcb5cSJeff Roberson * Do the lock dance required to avoid LOR. We have an 2141686bcb5cSJeff Roberson * extra spinlock nesting from sched_switch() which will 2142686bcb5cSJeff Roberson * prevent preemption while we're holding neither run-queue lock. 2143c47f202bSJeff Roberson */ 2144686bcb5cSJeff Roberson TDQ_UNLOCK(tdq); 2145686bcb5cSJeff Roberson TDQ_LOCK(tdn); 21466d3f74a1SMark Johnston lowpri = tdq_add(tdn, td, flags); 21476d3f74a1SMark Johnston tdq_notify(tdn, lowpri); 2148c47f202bSJeff Roberson TDQ_UNLOCK(tdn); 2149686bcb5cSJeff Roberson TDQ_LOCK(tdq); 2150c47f202bSJeff Roberson #endif 2151c47f202bSJeff Roberson return (TDQ_LOCKPTR(tdn)); 2152c47f202bSJeff Roberson } 2153c47f202bSJeff Roberson 2154c47f202bSJeff Roberson /* 215561a74c5cSJeff Roberson * thread_lock_unblock() that does not assume td_lock is blocked. 2156ae7a6b38SJeff Roberson */ 2157ae7a6b38SJeff Roberson static inline void 2158ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx) 2159ae7a6b38SJeff Roberson { 2160ae7a6b38SJeff Roberson atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 2161ae7a6b38SJeff Roberson (uintptr_t)mtx); 2162ae7a6b38SJeff Roberson } 2163ae7a6b38SJeff Roberson 2164ae7a6b38SJeff Roberson /* 2165ae7a6b38SJeff Roberson * Switch threads. This function has to handle threads coming in while 2166ae7a6b38SJeff Roberson * blocked for some reason, running, or idle. It also must deal with 2167ae7a6b38SJeff Roberson * migrating a thread from one queue to another as running threads may 2168ae7a6b38SJeff Roberson * be assigned elsewhere via binding. 2169ae7a6b38SJeff Roberson */ 21703db720fdSDavid Xu void 2171686bcb5cSJeff Roberson sched_switch(struct thread *td, int flags) 217235e6168fSJeff Roberson { 2173686bcb5cSJeff Roberson struct thread *newtd; 2174c02bbb43SJeff Roberson struct tdq *tdq; 2175ad1e7d28SJulian Elischer struct td_sched *ts; 2176ae7a6b38SJeff Roberson struct mtx *mtx; 2177c47f202bSJeff Roberson int srqflag; 21788db16699SAlexander Motin int cpuid, preempted; 21798db16699SAlexander Motin #ifdef SMP 21808db16699SAlexander Motin int pickcpu; 21818db16699SAlexander Motin #endif 218235e6168fSJeff Roberson 21837b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 218435e6168fSJeff Roberson 2185ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 2186018ff686SJeff Roberson tdq = TDQ_SELF(); 218793ccd6bfSKonstantin Belousov ts = td_get_sched(td); 21887295465eSAlexander Motin sched_pctcpu_update(ts, 1); 21898db16699SAlexander Motin #ifdef SMP 2190e745d729SAlexander Motin pickcpu = (td->td_flags & TDF_PICKCPU) != 0; 2191e745d729SAlexander Motin if (pickcpu) 2192e745d729SAlexander Motin ts->ts_rltick = ticks - affinity * MAX_CACHE_LEVELS; 2193e745d729SAlexander Motin else 2194ae7a6b38SJeff Roberson ts->ts_rltick = ticks; 21958db16699SAlexander Motin #endif 2196060563ecSJulian Elischer td->td_lastcpu = td->td_oncpu; 2197ad9dadc4SAndriy Gapon preempted = (td->td_flags & TDF_SLICEEND) == 0 && 2198ad9dadc4SAndriy Gapon (flags & SW_PREEMPT) != 0; 2199e745d729SAlexander Motin td->td_flags &= ~(TDF_NEEDRESCHED | TDF_PICKCPU | TDF_SLICEEND); 220077918643SStephan Uphoff td->td_owepreempt = 0; 220111484ad8SMark Johnston atomic_store_char(&tdq->tdq_owepreempt, 0); 22022c27cb3aSAlexander Motin if (!TD_IS_IDLETHREAD(td)) 220311484ad8SMark Johnston TDQ_SWITCHCNT_INC(tdq); 22047789ab32SMark Johnston 2205b11fdad0SJeff Roberson /* 2206686bcb5cSJeff Roberson * Always block the thread lock so we can drop the tdq lock early. 2207b11fdad0SJeff Roberson */ 2208686bcb5cSJeff Roberson mtx = thread_lock_block(td); 2209686bcb5cSJeff Roberson spinlock_enter(); 2210486a9414SJulian Elischer if (TD_IS_IDLETHREAD(td)) { 2211686bcb5cSJeff Roberson MPASS(mtx == TDQ_LOCKPTR(tdq)); 2212bf0acc27SJohn Baldwin TD_SET_CAN_RUN(td); 22137b20fb19SJeff Roberson } else if (TD_IS_RUNNING(td)) { 2214686bcb5cSJeff Roberson MPASS(mtx == TDQ_LOCKPTR(tdq)); 22153d7f4117SAlexander Motin srqflag = preempted ? 2216598b368dSJeff Roberson SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 2217c47f202bSJeff Roberson SRQ_OURSELF|SRQ_YIELDING; 2218ba4932b5SMatthew D Fleming #ifdef SMP 2219e745d729SAlexander Motin if (THREAD_CAN_MIGRATE(td) && (!THREAD_CAN_SCHED(td, ts->ts_cpu) 2220e745d729SAlexander Motin || pickcpu)) 22210f7a0ebdSMatthew D Fleming ts->ts_cpu = sched_pickcpu(td, 0); 2222ba4932b5SMatthew D Fleming #endif 2223c47f202bSJeff Roberson if (ts->ts_cpu == cpuid) 22249727e637SJeff Roberson tdq_runq_add(tdq, td, srqflag); 2225686bcb5cSJeff Roberson else 2226c47f202bSJeff Roberson mtx = sched_switch_migrate(tdq, td, srqflag); 2227ae7a6b38SJeff Roberson } else { 2228ae7a6b38SJeff Roberson /* This thread must be going to sleep. */ 222961a74c5cSJeff Roberson if (mtx != TDQ_LOCKPTR(tdq)) { 223061a74c5cSJeff Roberson mtx_unlock_spin(mtx); 223161a74c5cSJeff Roberson TDQ_LOCK(tdq); 223261a74c5cSJeff Roberson } 22339727e637SJeff Roberson tdq_load_rem(tdq, td); 22344c8a8cfcSKonstantin Belousov #ifdef SMP 223597e9382dSDon Lewis if (tdq->tdq_load == 0) 223697e9382dSDon Lewis tdq_trysteal(tdq); 22374c8a8cfcSKonstantin Belousov #endif 2238ae7a6b38SJeff Roberson } 2239afa0a46cSAndriy Gapon 2240afa0a46cSAndriy Gapon #if (KTR_COMPILE & KTR_SCHED) != 0 2241afa0a46cSAndriy Gapon if (TD_IS_IDLETHREAD(td)) 2242afa0a46cSAndriy Gapon KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "idle", 2243afa0a46cSAndriy Gapon "prio:%d", td->td_priority); 2244afa0a46cSAndriy Gapon else 2245afa0a46cSAndriy Gapon KTR_STATE3(KTR_SCHED, "thread", sched_tdname(td), KTDSTATE(td), 2246afa0a46cSAndriy Gapon "prio:%d", td->td_priority, "wmesg:\"%s\"", td->td_wmesg, 2247afa0a46cSAndriy Gapon "lockname:\"%s\"", td->td_lockname); 2248afa0a46cSAndriy Gapon #endif 2249afa0a46cSAndriy Gapon 2250ae7a6b38SJeff Roberson /* 2251ae7a6b38SJeff Roberson * We enter here with the thread blocked and assigned to the 2252ae7a6b38SJeff Roberson * appropriate cpu run-queue or sleep-queue and with the current 2253ae7a6b38SJeff Roberson * thread-queue locked. 2254ae7a6b38SJeff Roberson */ 2255ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 22566d3f74a1SMark Johnston MPASS(td == tdq->tdq_curthread); 22572454aaf5SJeff Roberson newtd = choosethread(); 2258686bcb5cSJeff Roberson sched_pctcpu_update(td_get_sched(newtd), 0); 2259686bcb5cSJeff Roberson TDQ_UNLOCK(tdq); 2260686bcb5cSJeff Roberson 2261ae7a6b38SJeff Roberson /* 2262ae7a6b38SJeff Roberson * Call the MD code to switch contexts if necessary. 2263ae7a6b38SJeff Roberson */ 2264ebccf1e3SJoseph Koshy if (td != newtd) { 2265ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 2266ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 2267ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 2268ebccf1e3SJoseph Koshy #endif 2269d9fae5abSAndriy Gapon SDT_PROBE2(sched, , , off__cpu, newtd, newtd->td_proc); 22706f5f25e5SJohn Birrell 22716f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS 22726f5f25e5SJohn Birrell /* 22736f5f25e5SJohn Birrell * If DTrace has set the active vtime enum to anything 22746f5f25e5SJohn Birrell * other than INACTIVE (0), then it should have set the 22756f5f25e5SJohn Birrell * function to call. 22766f5f25e5SJohn Birrell */ 22776f5f25e5SJohn Birrell if (dtrace_vtime_active) 22786f5f25e5SJohn Birrell (*dtrace_vtime_switch_func)(newtd); 22796f5f25e5SJohn Birrell #endif 2280686bcb5cSJeff Roberson td->td_oncpu = NOCPU; 2281ae7a6b38SJeff Roberson cpu_switch(td, newtd, mtx); 2282a89c2c8cSMark Johnston cpuid = td->td_oncpu = PCPU_GET(cpuid); 2283b3e9e682SRyan Stone 2284d9fae5abSAndriy Gapon SDT_PROBE0(sched, , , on__cpu); 2285ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 2286ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 2287ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 2288ebccf1e3SJoseph Koshy #endif 2289b3e9e682SRyan Stone } else { 2290ae7a6b38SJeff Roberson thread_unblock_switch(td, mtx); 2291d9fae5abSAndriy Gapon SDT_PROBE0(sched, , , remain__cpu); 2292b3e9e682SRyan Stone } 2293686bcb5cSJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, 2294686bcb5cSJeff Roberson ("invalid count %d", curthread->td_md.md_spinlock_count)); 2295afa0a46cSAndriy Gapon 2296afa0a46cSAndriy Gapon KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "running", 2297afa0a46cSAndriy Gapon "prio:%d", td->td_priority); 229835e6168fSJeff Roberson } 229935e6168fSJeff Roberson 2300ae7a6b38SJeff Roberson /* 2301ae7a6b38SJeff Roberson * Adjust thread priorities as a result of a nice request. 2302ae7a6b38SJeff Roberson */ 230335e6168fSJeff Roberson void 2304fa885116SJulian Elischer sched_nice(struct proc *p, int nice) 230535e6168fSJeff Roberson { 230635e6168fSJeff Roberson struct thread *td; 230735e6168fSJeff Roberson 2308fa885116SJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 2309e7d50326SJeff Roberson 2310fa885116SJulian Elischer p->p_nice = nice; 23118460a577SJohn Birrell FOREACH_THREAD_IN_PROC(p, td) { 23127b20fb19SJeff Roberson thread_lock(td); 23138460a577SJohn Birrell sched_priority(td); 2314e7d50326SJeff Roberson sched_prio(td, td->td_base_user_pri); 23157b20fb19SJeff Roberson thread_unlock(td); 231635e6168fSJeff Roberson } 2317fa885116SJulian Elischer } 231835e6168fSJeff Roberson 2319ae7a6b38SJeff Roberson /* 2320ae7a6b38SJeff Roberson * Record the sleep time for the interactivity scorer. 2321ae7a6b38SJeff Roberson */ 232235e6168fSJeff Roberson void 2323c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio) 232435e6168fSJeff Roberson { 2325e7d50326SJeff Roberson 23267b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 232735e6168fSJeff Roberson 232854b0e65fSJeff Roberson td->td_slptick = ticks; 232917c4c356SKonstantin Belousov if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 2330c5aa6b58SJeff Roberson td->td_flags |= TDF_CANSWAP; 23312dc29adbSJohn Baldwin if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 23322dc29adbSJohn Baldwin return; 23330502fe2eSJeff Roberson if (static_boost == 1 && prio) 2334c5aa6b58SJeff Roberson sched_prio(td, prio); 23350502fe2eSJeff Roberson else if (static_boost && td->td_priority > static_boost) 23360502fe2eSJeff Roberson sched_prio(td, static_boost); 233735e6168fSJeff Roberson } 233835e6168fSJeff Roberson 2339ae7a6b38SJeff Roberson /* 2340ae7a6b38SJeff Roberson * Schedule a thread to resume execution and record how long it voluntarily 2341ae7a6b38SJeff Roberson * slept. We also update the pctcpu, interactivity, and priority. 234261a74c5cSJeff Roberson * 234361a74c5cSJeff Roberson * Requires the thread lock on entry, drops on exit. 2344ae7a6b38SJeff Roberson */ 234535e6168fSJeff Roberson void 234661a74c5cSJeff Roberson sched_wakeup(struct thread *td, int srqflags) 234735e6168fSJeff Roberson { 234814618990SJeff Roberson struct td_sched *ts; 2349ae7a6b38SJeff Roberson int slptick; 2350e7d50326SJeff Roberson 23517b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 235293ccd6bfSKonstantin Belousov ts = td_get_sched(td); 2353c5aa6b58SJeff Roberson td->td_flags &= ~TDF_CANSWAP; 235461a74c5cSJeff Roberson 235535e6168fSJeff Roberson /* 2356e7d50326SJeff Roberson * If we slept for more than a tick update our interactivity and 2357e7d50326SJeff Roberson * priority. 235835e6168fSJeff Roberson */ 235954b0e65fSJeff Roberson slptick = td->td_slptick; 236054b0e65fSJeff Roberson td->td_slptick = 0; 2361ae7a6b38SJeff Roberson if (slptick && slptick != ticks) { 23627295465eSAlexander Motin ts->ts_slptime += (ticks - slptick) << SCHED_TICK_SHIFT; 23638460a577SJohn Birrell sched_interact_update(td); 23647295465eSAlexander Motin sched_pctcpu_update(ts, 0); 2365f1e8dc4aSJeff Roberson } 23665e5c3873SJeff Roberson /* 23675e5c3873SJeff Roberson * Reset the slice value since we slept and advanced the round-robin. 23685e5c3873SJeff Roberson */ 23695e5c3873SJeff Roberson ts->ts_slice = 0; 237061a74c5cSJeff Roberson sched_add(td, SRQ_BORING | srqflags); 237135e6168fSJeff Roberson } 237235e6168fSJeff Roberson 237335e6168fSJeff Roberson /* 237435e6168fSJeff Roberson * Penalize the parent for creating a new child and initialize the child's 237535e6168fSJeff Roberson * priority. 237635e6168fSJeff Roberson */ 237735e6168fSJeff Roberson void 23788460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child) 237915dc847eSJeff Roberson { 23807b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 238193ccd6bfSKonstantin Belousov sched_pctcpu_update(td_get_sched(td), 1); 2382ad1e7d28SJulian Elischer sched_fork_thread(td, child); 2383e7d50326SJeff Roberson /* 2384e7d50326SJeff Roberson * Penalize the parent and child for forking. 2385e7d50326SJeff Roberson */ 2386e7d50326SJeff Roberson sched_interact_fork(child); 2387e7d50326SJeff Roberson sched_priority(child); 238893ccd6bfSKonstantin Belousov td_get_sched(td)->ts_runtime += tickincr; 2389e7d50326SJeff Roberson sched_interact_update(td); 2390e7d50326SJeff Roberson sched_priority(td); 2391ad1e7d28SJulian Elischer } 2392ad1e7d28SJulian Elischer 2393ae7a6b38SJeff Roberson /* 2394ae7a6b38SJeff Roberson * Fork a new thread, may be within the same process. 2395ae7a6b38SJeff Roberson */ 2396ad1e7d28SJulian Elischer void 2397ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child) 2398ad1e7d28SJulian Elischer { 2399ad1e7d28SJulian Elischer struct td_sched *ts; 2400ad1e7d28SJulian Elischer struct td_sched *ts2; 24015e5c3873SJeff Roberson struct tdq *tdq; 24028460a577SJohn Birrell 24035e5c3873SJeff Roberson tdq = TDQ_SELF(); 24048b16c208SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2405e7d50326SJeff Roberson /* 2406e7d50326SJeff Roberson * Initialize child. 2407e7d50326SJeff Roberson */ 240893ccd6bfSKonstantin Belousov ts = td_get_sched(td); 240993ccd6bfSKonstantin Belousov ts2 = td_get_sched(child); 241092de34dfSJohn Baldwin child->td_oncpu = NOCPU; 241192de34dfSJohn Baldwin child->td_lastcpu = NOCPU; 24125e5c3873SJeff Roberson child->td_lock = TDQ_LOCKPTR(tdq); 24138b16c208SJeff Roberson child->td_cpuset = cpuset_ref(td->td_cpuset); 24143f289c3fSJeff Roberson child->td_domain.dr_policy = td->td_cpuset->cs_domain; 2415ad1e7d28SJulian Elischer ts2->ts_cpu = ts->ts_cpu; 24168b16c208SJeff Roberson ts2->ts_flags = 0; 2417e7d50326SJeff Roberson /* 241822d19207SJohn Baldwin * Grab our parents cpu estimation information. 2419e7d50326SJeff Roberson */ 2420ad1e7d28SJulian Elischer ts2->ts_ticks = ts->ts_ticks; 2421ad1e7d28SJulian Elischer ts2->ts_ltick = ts->ts_ltick; 2422ad1e7d28SJulian Elischer ts2->ts_ftick = ts->ts_ftick; 242322d19207SJohn Baldwin /* 242422d19207SJohn Baldwin * Do not inherit any borrowed priority from the parent. 242522d19207SJohn Baldwin */ 242622d19207SJohn Baldwin child->td_priority = child->td_base_pri; 2427e7d50326SJeff Roberson /* 2428e7d50326SJeff Roberson * And update interactivity score. 2429e7d50326SJeff Roberson */ 2430ae7a6b38SJeff Roberson ts2->ts_slptime = ts->ts_slptime; 2431ae7a6b38SJeff Roberson ts2->ts_runtime = ts->ts_runtime; 24325e5c3873SJeff Roberson /* Attempt to quickly learn interactivity. */ 24335e5c3873SJeff Roberson ts2->ts_slice = tdq_slice(tdq) - sched_slice_min; 24348f51ad55SJeff Roberson #ifdef KTR 24358f51ad55SJeff Roberson bzero(ts2->ts_name, sizeof(ts2->ts_name)); 24368f51ad55SJeff Roberson #endif 243715dc847eSJeff Roberson } 243815dc847eSJeff Roberson 2439ae7a6b38SJeff Roberson /* 2440ae7a6b38SJeff Roberson * Adjust the priority class of a thread. 2441ae7a6b38SJeff Roberson */ 244215dc847eSJeff Roberson void 24438460a577SJohn Birrell sched_class(struct thread *td, int class) 244415dc847eSJeff Roberson { 244515dc847eSJeff Roberson 24467b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 24478460a577SJohn Birrell if (td->td_pri_class == class) 244815dc847eSJeff Roberson return; 24498460a577SJohn Birrell td->td_pri_class = class; 245035e6168fSJeff Roberson } 245135e6168fSJeff Roberson 245235e6168fSJeff Roberson /* 245335e6168fSJeff Roberson * Return some of the child's priority and interactivity to the parent. 245435e6168fSJeff Roberson */ 245535e6168fSJeff Roberson void 2456fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child) 245735e6168fSJeff Roberson { 2458e7d50326SJeff Roberson struct thread *td; 2459141ad61cSJeff Roberson 24608f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2461cd39bb09SXin LI "prio:%d", child->td_priority); 2462374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2463e7d50326SJeff Roberson td = FIRST_THREAD_IN_PROC(p); 2464e7d50326SJeff Roberson sched_exit_thread(td, child); 2465ad1e7d28SJulian Elischer } 2466ad1e7d28SJulian Elischer 2467ae7a6b38SJeff Roberson /* 2468ae7a6b38SJeff Roberson * Penalize another thread for the time spent on this one. This helps to 2469ae7a6b38SJeff Roberson * worsen the priority and interactivity of processes which schedule batch 2470ae7a6b38SJeff Roberson * jobs such as make. This has little effect on the make process itself but 2471ae7a6b38SJeff Roberson * causes new processes spawned by it to receive worse scores immediately. 2472ae7a6b38SJeff Roberson */ 2473ad1e7d28SJulian Elischer void 2474fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child) 2475ad1e7d28SJulian Elischer { 2476fc6c30f6SJulian Elischer 24778f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2478cd39bb09SXin LI "prio:%d", child->td_priority); 2479e7d50326SJeff Roberson /* 2480e7d50326SJeff Roberson * Give the child's runtime to the parent without returning the 2481e7d50326SJeff Roberson * sleep time as a penalty to the parent. This causes shells that 2482e7d50326SJeff Roberson * launch expensive things to mark their children as expensive. 2483e7d50326SJeff Roberson */ 24847b20fb19SJeff Roberson thread_lock(td); 248593ccd6bfSKonstantin Belousov td_get_sched(td)->ts_runtime += td_get_sched(child)->ts_runtime; 2486fc6c30f6SJulian Elischer sched_interact_update(td); 2487e7d50326SJeff Roberson sched_priority(td); 24887b20fb19SJeff Roberson thread_unlock(td); 2489ad1e7d28SJulian Elischer } 2490ad1e7d28SJulian Elischer 2491ff256d9cSJeff Roberson void 2492ff256d9cSJeff Roberson sched_preempt(struct thread *td) 2493ff256d9cSJeff Roberson { 2494ff256d9cSJeff Roberson struct tdq *tdq; 2495686bcb5cSJeff Roberson int flags; 2496ff256d9cSJeff Roberson 2497b3e9e682SRyan Stone SDT_PROBE2(sched, , , surrender, td, td->td_proc); 2498b3e9e682SRyan Stone 2499ff256d9cSJeff Roberson thread_lock(td); 2500ff256d9cSJeff Roberson tdq = TDQ_SELF(); 2501ff256d9cSJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2502ff256d9cSJeff Roberson if (td->td_priority > tdq->tdq_lowpri) { 2503686bcb5cSJeff Roberson if (td->td_critnest == 1) { 25048df78c41SJeff Roberson flags = SW_INVOL | SW_PREEMPT; 2505686bcb5cSJeff Roberson flags |= TD_IS_IDLETHREAD(td) ? SWT_REMOTEWAKEIDLE : 2506686bcb5cSJeff Roberson SWT_REMOTEPREEMPT; 2507686bcb5cSJeff Roberson mi_switch(flags); 2508686bcb5cSJeff Roberson /* Switch dropped thread lock. */ 2509686bcb5cSJeff Roberson return; 2510686bcb5cSJeff Roberson } 2511ff256d9cSJeff Roberson td->td_owepreempt = 1; 25127789ab32SMark Johnston } else { 25137789ab32SMark Johnston tdq->tdq_owepreempt = 0; 2514ff256d9cSJeff Roberson } 2515ff256d9cSJeff Roberson thread_unlock(td); 2516ff256d9cSJeff Roberson } 2517ff256d9cSJeff Roberson 2518ae7a6b38SJeff Roberson /* 2519ae7a6b38SJeff Roberson * Fix priorities on return to user-space. Priorities may be elevated due 2520ae7a6b38SJeff Roberson * to static priorities in msleep() or similar. 2521ae7a6b38SJeff Roberson */ 2522ad1e7d28SJulian Elischer void 252328240885SMateusz Guzik sched_userret_slowpath(struct thread *td) 2524ad1e7d28SJulian Elischer { 252528240885SMateusz Guzik 25267b20fb19SJeff Roberson thread_lock(td); 2527ad1e7d28SJulian Elischer td->td_priority = td->td_user_pri; 2528ad1e7d28SJulian Elischer td->td_base_pri = td->td_user_pri; 252962fa74d9SJeff Roberson tdq_setlowpri(TDQ_SELF(), td); 25307b20fb19SJeff Roberson thread_unlock(td); 2531ad1e7d28SJulian Elischer } 253235e6168fSJeff Roberson 2533ae7a6b38SJeff Roberson /* 2534ae7a6b38SJeff Roberson * Handle a stathz tick. This is really only relevant for timeshare 2535ae7a6b38SJeff Roberson * threads. 2536ae7a6b38SJeff Roberson */ 253735e6168fSJeff Roberson void 2538c3cccf95SJeff Roberson sched_clock(struct thread *td, int cnt) 253935e6168fSJeff Roberson { 2540ad1e7d28SJulian Elischer struct tdq *tdq; 2541ad1e7d28SJulian Elischer struct td_sched *ts; 254235e6168fSJeff Roberson 2543ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 25443f872f85SJeff Roberson tdq = TDQ_SELF(); 25457fcf154aSJeff Roberson #ifdef SMP 25467fcf154aSJeff Roberson /* 25477fcf154aSJeff Roberson * We run the long term load balancer infrequently on the first cpu. 25487fcf154aSJeff Roberson */ 2549c3cccf95SJeff Roberson if (balance_tdq == tdq && smp_started != 0 && rebalance != 0 && 2550c3cccf95SJeff Roberson balance_ticks != 0) { 2551c3cccf95SJeff Roberson balance_ticks -= cnt; 2552c3cccf95SJeff Roberson if (balance_ticks <= 0) 25537fcf154aSJeff Roberson sched_balance(); 25547fcf154aSJeff Roberson } 25557fcf154aSJeff Roberson #endif 25563f872f85SJeff Roberson /* 25571690c6c1SJeff Roberson * Save the old switch count so we have a record of the last ticks 25581690c6c1SJeff Roberson * activity. Initialize the new switch count based on our load. 25591690c6c1SJeff Roberson * If there is some activity seed it to reflect that. 25601690c6c1SJeff Roberson */ 25611690c6c1SJeff Roberson tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 25626c47aaaeSJeff Roberson tdq->tdq_switchcnt = tdq->tdq_load; 256311484ad8SMark Johnston 25641690c6c1SJeff Roberson /* 25653f872f85SJeff Roberson * Advance the insert index once for each tick to ensure that all 25663f872f85SJeff Roberson * threads get a chance to run. 25673f872f85SJeff Roberson */ 25683f872f85SJeff Roberson if (tdq->tdq_idx == tdq->tdq_ridx) { 25693f872f85SJeff Roberson tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 25703f872f85SJeff Roberson if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 25713f872f85SJeff Roberson tdq->tdq_ridx = tdq->tdq_idx; 25723f872f85SJeff Roberson } 257393ccd6bfSKonstantin Belousov ts = td_get_sched(td); 25747295465eSAlexander Motin sched_pctcpu_update(ts, 1); 2575c3cccf95SJeff Roberson if ((td->td_pri_class & PRI_FIFO_BIT) || TD_IS_IDLETHREAD(td)) 2576a8949de2SJeff Roberson return; 2577c3cccf95SJeff Roberson 2578c9a8cba4SJohn Baldwin if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) { 2579a8949de2SJeff Roberson /* 2580fd0b8c78SJeff Roberson * We used a tick; charge it to the thread so 2581fd0b8c78SJeff Roberson * that we can compute our interactivity. 258215dc847eSJeff Roberson */ 2583c3cccf95SJeff Roberson td_get_sched(td)->ts_runtime += tickincr * cnt; 25848460a577SJohn Birrell sched_interact_update(td); 258573daf66fSJeff Roberson sched_priority(td); 2586fd0b8c78SJeff Roberson } 2587579895dfSAlexander Motin 258835e6168fSJeff Roberson /* 2589579895dfSAlexander Motin * Force a context switch if the current thread has used up a full 2590579895dfSAlexander Motin * time slice (default is 100ms). 259135e6168fSJeff Roberson */ 2592c3cccf95SJeff Roberson ts->ts_slice += cnt; 2593c3cccf95SJeff Roberson if (ts->ts_slice >= tdq_slice(tdq)) { 25945e5c3873SJeff Roberson ts->ts_slice = 0; 25953d7f4117SAlexander Motin td->td_flags |= TDF_NEEDRESCHED | TDF_SLICEEND; 259635e6168fSJeff Roberson } 2597579895dfSAlexander Motin } 259835e6168fSJeff Roberson 2599ccd0ec40SKonstantin Belousov u_int 2600ccd0ec40SKonstantin Belousov sched_estcpu(struct thread *td __unused) 2601ae7a6b38SJeff Roberson { 2602ae7a6b38SJeff Roberson 2603ccd0ec40SKonstantin Belousov return (0); 2604ae7a6b38SJeff Roberson } 2605ae7a6b38SJeff Roberson 2606ae7a6b38SJeff Roberson /* 2607ae7a6b38SJeff Roberson * Return whether the current CPU has runnable tasks. Used for in-kernel 2608ae7a6b38SJeff Roberson * cooperative idle threads. 2609ae7a6b38SJeff Roberson */ 261035e6168fSJeff Roberson int 261135e6168fSJeff Roberson sched_runnable(void) 261235e6168fSJeff Roberson { 2613ad1e7d28SJulian Elischer struct tdq *tdq; 2614b90816f1SJeff Roberson int load; 261535e6168fSJeff Roberson 2616b90816f1SJeff Roberson load = 1; 2617b90816f1SJeff Roberson 2618ad1e7d28SJulian Elischer tdq = TDQ_SELF(); 26193f741ca1SJeff Roberson if ((curthread->td_flags & TDF_IDLETD) != 0) { 262011484ad8SMark Johnston if (TDQ_LOAD(tdq) > 0) 26213f741ca1SJeff Roberson goto out; 26223f741ca1SJeff Roberson } else 262311484ad8SMark Johnston if (TDQ_LOAD(tdq) - 1 > 0) 2624b90816f1SJeff Roberson goto out; 2625b90816f1SJeff Roberson load = 0; 2626b90816f1SJeff Roberson out: 2627b90816f1SJeff Roberson return (load); 262835e6168fSJeff Roberson } 262935e6168fSJeff Roberson 2630ae7a6b38SJeff Roberson /* 2631ae7a6b38SJeff Roberson * Choose the highest priority thread to run. The thread is removed from 2632ef80894cSMark Johnston * the run-queue while running however the load remains. 2633ae7a6b38SJeff Roberson */ 26347a5e5e2aSJeff Roberson struct thread * 2635c9f25d8fSJeff Roberson sched_choose(void) 2636c9f25d8fSJeff Roberson { 26379727e637SJeff Roberson struct thread *td; 2638ae7a6b38SJeff Roberson struct tdq *tdq; 2639ae7a6b38SJeff Roberson 2640ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2641ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 26429727e637SJeff Roberson td = tdq_choose(tdq); 26436d3f74a1SMark Johnston if (td != NULL) { 26449727e637SJeff Roberson tdq_runq_rem(tdq, td); 26450502fe2eSJeff Roberson tdq->tdq_lowpri = td->td_priority; 26466d3f74a1SMark Johnston } else { 26470502fe2eSJeff Roberson tdq->tdq_lowpri = PRI_MAX_IDLE; 26486d3f74a1SMark Johnston td = PCPU_GET(idlethread); 26496d3f74a1SMark Johnston } 26506d3f74a1SMark Johnston tdq->tdq_curthread = td; 26516d3f74a1SMark Johnston return (td); 26527a5e5e2aSJeff Roberson } 26537a5e5e2aSJeff Roberson 2654ae7a6b38SJeff Roberson /* 26550927ff78SMark Johnston * Set owepreempt if the currently running thread has lower priority than "pri". 26560927ff78SMark Johnston * Preemption never happens directly in ULE, we always request it once we exit a 26570927ff78SMark Johnston * critical section. 2658ae7a6b38SJeff Roberson */ 26590927ff78SMark Johnston static void 26600927ff78SMark Johnston sched_setpreempt(int pri) 26617a5e5e2aSJeff Roberson { 26627a5e5e2aSJeff Roberson struct thread *ctd; 26637a5e5e2aSJeff Roberson int cpri; 2664ff256d9cSJeff Roberson 26657a5e5e2aSJeff Roberson ctd = curthread; 26660927ff78SMark Johnston THREAD_LOCK_ASSERT(ctd, MA_OWNED); 26670927ff78SMark Johnston 26687a5e5e2aSJeff Roberson cpri = ctd->td_priority; 2669ff256d9cSJeff Roberson if (pri < cpri) 2670ff256d9cSJeff Roberson ctd->td_flags |= TDF_NEEDRESCHED; 2671879e0604SMateusz Guzik if (KERNEL_PANICKED() || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2672ae7a6b38SJeff Roberson return; 2673ff256d9cSJeff Roberson if (!sched_shouldpreempt(pri, cpri, 0)) 2674ae7a6b38SJeff Roberson return; 26757a5e5e2aSJeff Roberson ctd->td_owepreempt = 1; 267635e6168fSJeff Roberson } 267735e6168fSJeff Roberson 2678ae7a6b38SJeff Roberson /* 267973daf66fSJeff Roberson * Add a thread to a thread queue. Select the appropriate runq and add the 268073daf66fSJeff Roberson * thread to it. This is the internal function called when the tdq is 268173daf66fSJeff Roberson * predetermined. 2682ae7a6b38SJeff Roberson */ 26836d3f74a1SMark Johnston static int 2684ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags) 268535e6168fSJeff Roberson { 26866d3f74a1SMark Johnston int lowpri; 2687c9f25d8fSJeff Roberson 2688ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 268961a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 26907a5e5e2aSJeff Roberson KASSERT((td->td_inhibitors == 0), 26917a5e5e2aSJeff Roberson ("sched_add: trying to run inhibited thread")); 26927a5e5e2aSJeff Roberson KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 26937a5e5e2aSJeff Roberson ("sched_add: bad thread state")); 2694b61ce5b0SJeff Roberson KASSERT(td->td_flags & TDF_INMEM, 2695b61ce5b0SJeff Roberson ("sched_add: thread swapped out")); 2696ae7a6b38SJeff Roberson 26976d3f74a1SMark Johnston lowpri = tdq->tdq_lowpri; 26986d3f74a1SMark Johnston if (td->td_priority < lowpri) 2699ae7a6b38SJeff Roberson tdq->tdq_lowpri = td->td_priority; 27009727e637SJeff Roberson tdq_runq_add(tdq, td, flags); 27019727e637SJeff Roberson tdq_load_add(tdq, td); 27026d3f74a1SMark Johnston return (lowpri); 2703ae7a6b38SJeff Roberson } 2704ae7a6b38SJeff Roberson 2705ae7a6b38SJeff Roberson /* 2706ae7a6b38SJeff Roberson * Select the target thread queue and add a thread to it. Request 2707ae7a6b38SJeff Roberson * preemption or IPI a remote processor if required. 270861a74c5cSJeff Roberson * 270961a74c5cSJeff Roberson * Requires the thread lock on entry, drops on exit. 2710ae7a6b38SJeff Roberson */ 2711ae7a6b38SJeff Roberson void 2712ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags) 2713ae7a6b38SJeff Roberson { 2714ae7a6b38SJeff Roberson struct tdq *tdq; 27157b8bfa0dSJeff Roberson #ifdef SMP 27166d3f74a1SMark Johnston int cpu, lowpri; 2717ae7a6b38SJeff Roberson #endif 27188f51ad55SJeff Roberson 27198f51ad55SJeff Roberson KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 27208f51ad55SJeff Roberson "prio:%d", td->td_priority, KTR_ATTR_LINKED, 27218f51ad55SJeff Roberson sched_tdname(curthread)); 27228f51ad55SJeff Roberson KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 27238f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(td)); 2724b3e9e682SRyan Stone SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL, 2725b3e9e682SRyan Stone flags & SRQ_PREEMPTED); 2726ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2727ae7a6b38SJeff Roberson /* 2728ae7a6b38SJeff Roberson * Recalculate the priority before we select the target cpu or 2729ae7a6b38SJeff Roberson * run-queue. 2730ae7a6b38SJeff Roberson */ 2731ae7a6b38SJeff Roberson if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2732ae7a6b38SJeff Roberson sched_priority(td); 2733ae7a6b38SJeff Roberson #ifdef SMP 2734ae7a6b38SJeff Roberson /* 2735ae7a6b38SJeff Roberson * Pick the destination cpu and if it isn't ours transfer to the 2736ae7a6b38SJeff Roberson * target cpu. 2737ae7a6b38SJeff Roberson */ 27389727e637SJeff Roberson cpu = sched_pickcpu(td, flags); 27399727e637SJeff Roberson tdq = sched_setcpu(td, cpu, flags); 27406d3f74a1SMark Johnston lowpri = tdq_add(tdq, td, flags); 274161a74c5cSJeff Roberson if (cpu != PCPU_GET(cpuid)) 27426d3f74a1SMark Johnston tdq_notify(tdq, lowpri); 274361a74c5cSJeff Roberson else if (!(flags & SRQ_YIELDING)) 27440927ff78SMark Johnston sched_setpreempt(td->td_priority); 2745ae7a6b38SJeff Roberson #else 2746ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2747ae7a6b38SJeff Roberson /* 2748ae7a6b38SJeff Roberson * Now that the thread is moving to the run-queue, set the lock 2749ae7a6b38SJeff Roberson * to the scheduler's lock. 2750ae7a6b38SJeff Roberson */ 2751e4894505SMark Johnston if (td->td_lock != TDQ_LOCKPTR(tdq)) { 2752e4894505SMark Johnston TDQ_LOCK(tdq); 275361a74c5cSJeff Roberson if ((flags & SRQ_HOLD) != 0) 275461a74c5cSJeff Roberson td->td_lock = TDQ_LOCKPTR(tdq); 275561a74c5cSJeff Roberson else 2756ae7a6b38SJeff Roberson thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2757e4894505SMark Johnston } 27586d3f74a1SMark Johnston (void)tdq_add(tdq, td, flags); 2759ae7a6b38SJeff Roberson if (!(flags & SRQ_YIELDING)) 27600927ff78SMark Johnston sched_setpreempt(td->td_priority); 276161a74c5cSJeff Roberson #endif 276261a74c5cSJeff Roberson if (!(flags & SRQ_HOLDTD)) 276361a74c5cSJeff Roberson thread_unlock(td); 276435e6168fSJeff Roberson } 276535e6168fSJeff Roberson 2766ae7a6b38SJeff Roberson /* 2767ae7a6b38SJeff Roberson * Remove a thread from a run-queue without running it. This is used 2768ae7a6b38SJeff Roberson * when we're stealing a thread from a remote queue. Otherwise all threads 2769ae7a6b38SJeff Roberson * exit by calling sched_exit_thread() and sched_throw() themselves. 2770ae7a6b38SJeff Roberson */ 277135e6168fSJeff Roberson void 27727cf90fb3SJeff Roberson sched_rem(struct thread *td) 277335e6168fSJeff Roberson { 2774ad1e7d28SJulian Elischer struct tdq *tdq; 27757cf90fb3SJeff Roberson 27768f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 27778f51ad55SJeff Roberson "prio:%d", td->td_priority); 2778b3e9e682SRyan Stone SDT_PROBE3(sched, , , dequeue, td, td->td_proc, NULL); 277993ccd6bfSKonstantin Belousov tdq = TDQ_CPU(td_get_sched(td)->ts_cpu); 2780ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2781ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 27827a5e5e2aSJeff Roberson KASSERT(TD_ON_RUNQ(td), 2783ad1e7d28SJulian Elischer ("sched_rem: thread not on run queue")); 27849727e637SJeff Roberson tdq_runq_rem(tdq, td); 27859727e637SJeff Roberson tdq_load_rem(tdq, td); 27867a5e5e2aSJeff Roberson TD_SET_CAN_RUN(td); 278762fa74d9SJeff Roberson if (td->td_priority == tdq->tdq_lowpri) 278862fa74d9SJeff Roberson tdq_setlowpri(tdq, NULL); 278935e6168fSJeff Roberson } 279035e6168fSJeff Roberson 2791ae7a6b38SJeff Roberson /* 2792ae7a6b38SJeff Roberson * Fetch cpu utilization information. Updates on demand. 2793ae7a6b38SJeff Roberson */ 279435e6168fSJeff Roberson fixpt_t 27957cf90fb3SJeff Roberson sched_pctcpu(struct thread *td) 279635e6168fSJeff Roberson { 279735e6168fSJeff Roberson fixpt_t pctcpu; 2798ad1e7d28SJulian Elischer struct td_sched *ts; 279935e6168fSJeff Roberson 280035e6168fSJeff Roberson pctcpu = 0; 280193ccd6bfSKonstantin Belousov ts = td_get_sched(td); 280235e6168fSJeff Roberson 28033da35a0aSJohn Baldwin THREAD_LOCK_ASSERT(td, MA_OWNED); 28047295465eSAlexander Motin sched_pctcpu_update(ts, TD_IS_RUNNING(td)); 2805ad1e7d28SJulian Elischer if (ts->ts_ticks) { 280635e6168fSJeff Roberson int rtick; 280735e6168fSJeff Roberson 280835e6168fSJeff Roberson /* How many rtick per second ? */ 2809e7d50326SJeff Roberson rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2810e7d50326SJeff Roberson pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 281135e6168fSJeff Roberson } 281235e6168fSJeff Roberson 281335e6168fSJeff Roberson return (pctcpu); 281435e6168fSJeff Roberson } 281535e6168fSJeff Roberson 281662fa74d9SJeff Roberson /* 281762fa74d9SJeff Roberson * Enforce affinity settings for a thread. Called after adjustments to 281862fa74d9SJeff Roberson * cpumask. 281962fa74d9SJeff Roberson */ 2820885d51a3SJeff Roberson void 2821885d51a3SJeff Roberson sched_affinity(struct thread *td) 2822885d51a3SJeff Roberson { 282362fa74d9SJeff Roberson #ifdef SMP 282462fa74d9SJeff Roberson struct td_sched *ts; 282562fa74d9SJeff Roberson 282662fa74d9SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 282793ccd6bfSKonstantin Belousov ts = td_get_sched(td); 282862fa74d9SJeff Roberson if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 282962fa74d9SJeff Roberson return; 283053a6c8b3SJeff Roberson if (TD_ON_RUNQ(td)) { 283153a6c8b3SJeff Roberson sched_rem(td); 2832d8d5f036SJeff Roberson sched_add(td, SRQ_BORING | SRQ_HOLDTD); 283353a6c8b3SJeff Roberson return; 283453a6c8b3SJeff Roberson } 283562fa74d9SJeff Roberson if (!TD_IS_RUNNING(td)) 283662fa74d9SJeff Roberson return; 283762fa74d9SJeff Roberson /* 28380f7a0ebdSMatthew D Fleming * Force a switch before returning to userspace. If the 28390f7a0ebdSMatthew D Fleming * target thread is not running locally send an ipi to force 28400f7a0ebdSMatthew D Fleming * the issue. 284162fa74d9SJeff Roberson */ 2842a8103ae8SJohn Baldwin td->td_flags |= TDF_NEEDRESCHED; 28430f7a0ebdSMatthew D Fleming if (td != curthread) 28440f7a0ebdSMatthew D Fleming ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 284562fa74d9SJeff Roberson #endif 2846885d51a3SJeff Roberson } 2847885d51a3SJeff Roberson 2848ae7a6b38SJeff Roberson /* 2849ae7a6b38SJeff Roberson * Bind a thread to a target cpu. 2850ae7a6b38SJeff Roberson */ 28519bacd788SJeff Roberson void 28529bacd788SJeff Roberson sched_bind(struct thread *td, int cpu) 28539bacd788SJeff Roberson { 2854ad1e7d28SJulian Elischer struct td_sched *ts; 28559bacd788SJeff Roberson 2856c47f202bSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 28571d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 285893ccd6bfSKonstantin Belousov ts = td_get_sched(td); 28596b2f763fSJeff Roberson if (ts->ts_flags & TSF_BOUND) 2860c95d2db2SJeff Roberson sched_unbind(td); 28610f7a0ebdSMatthew D Fleming KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2862ad1e7d28SJulian Elischer ts->ts_flags |= TSF_BOUND; 28636b2f763fSJeff Roberson sched_pin(); 286480f86c9fSJeff Roberson if (PCPU_GET(cpuid) == cpu) 28659bacd788SJeff Roberson return; 28666b2f763fSJeff Roberson ts->ts_cpu = cpu; 28679bacd788SJeff Roberson /* When we return from mi_switch we'll be on the correct cpu. */ 2868686bcb5cSJeff Roberson mi_switch(SW_VOL); 2869686bcb5cSJeff Roberson thread_lock(td); 28709bacd788SJeff Roberson } 28719bacd788SJeff Roberson 2872ae7a6b38SJeff Roberson /* 2873ae7a6b38SJeff Roberson * Release a bound thread. 2874ae7a6b38SJeff Roberson */ 28759bacd788SJeff Roberson void 28769bacd788SJeff Roberson sched_unbind(struct thread *td) 28779bacd788SJeff Roberson { 2878e7d50326SJeff Roberson struct td_sched *ts; 2879e7d50326SJeff Roberson 28807b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 28811d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 288293ccd6bfSKonstantin Belousov ts = td_get_sched(td); 28836b2f763fSJeff Roberson if ((ts->ts_flags & TSF_BOUND) == 0) 28846b2f763fSJeff Roberson return; 2885e7d50326SJeff Roberson ts->ts_flags &= ~TSF_BOUND; 2886e7d50326SJeff Roberson sched_unpin(); 28879bacd788SJeff Roberson } 28889bacd788SJeff Roberson 288935e6168fSJeff Roberson int 2890ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td) 2891ebccf1e3SJoseph Koshy { 28927b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 289393ccd6bfSKonstantin Belousov return (td_get_sched(td)->ts_flags & TSF_BOUND); 2894ebccf1e3SJoseph Koshy } 2895ebccf1e3SJoseph Koshy 2896ae7a6b38SJeff Roberson /* 2897ae7a6b38SJeff Roberson * Basic yield call. 2898ae7a6b38SJeff Roberson */ 289936ec198bSDavid Xu void 290036ec198bSDavid Xu sched_relinquish(struct thread *td) 290136ec198bSDavid Xu { 29027b20fb19SJeff Roberson thread_lock(td); 2903686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_RELINQUISH); 290436ec198bSDavid Xu } 290536ec198bSDavid Xu 2906ae7a6b38SJeff Roberson /* 2907ae7a6b38SJeff Roberson * Return the total system load. 2908ae7a6b38SJeff Roberson */ 2909ebccf1e3SJoseph Koshy int 291033916c36SJeff Roberson sched_load(void) 291133916c36SJeff Roberson { 291233916c36SJeff Roberson #ifdef SMP 291333916c36SJeff Roberson int total; 291433916c36SJeff Roberson int i; 291533916c36SJeff Roberson 291633916c36SJeff Roberson total = 0; 29173aa6d94eSJohn Baldwin CPU_FOREACH(i) 291811484ad8SMark Johnston total += atomic_load_int(&TDQ_CPU(i)->tdq_sysload); 291933916c36SJeff Roberson return (total); 292033916c36SJeff Roberson #else 292111484ad8SMark Johnston return (atomic_load_int(&TDQ_SELF()->tdq_sysload)); 292233916c36SJeff Roberson #endif 292333916c36SJeff Roberson } 292433916c36SJeff Roberson 292533916c36SJeff Roberson int 292635e6168fSJeff Roberson sched_sizeof_proc(void) 292735e6168fSJeff Roberson { 292835e6168fSJeff Roberson return (sizeof(struct proc)); 292935e6168fSJeff Roberson } 293035e6168fSJeff Roberson 293135e6168fSJeff Roberson int 293235e6168fSJeff Roberson sched_sizeof_thread(void) 293335e6168fSJeff Roberson { 293435e6168fSJeff Roberson return (sizeof(struct thread) + sizeof(struct td_sched)); 293535e6168fSJeff Roberson } 2936b41f1452SDavid Xu 293709c8a4ccSJeff Roberson #ifdef SMP 293809c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) \ 293909c8a4ccSJeff Roberson ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 294009c8a4ccSJeff Roberson #else 294109c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) 1 294209c8a4ccSJeff Roberson #endif 294309c8a4ccSJeff Roberson 29447a5e5e2aSJeff Roberson /* 29457a5e5e2aSJeff Roberson * The actual idle process. 29467a5e5e2aSJeff Roberson */ 29477a5e5e2aSJeff Roberson void 29487a5e5e2aSJeff Roberson sched_idletd(void *dummy) 29497a5e5e2aSJeff Roberson { 29507a5e5e2aSJeff Roberson struct thread *td; 2951ae7a6b38SJeff Roberson struct tdq *tdq; 29522c27cb3aSAlexander Motin int oldswitchcnt, switchcnt; 29531690c6c1SJeff Roberson int i; 29547a5e5e2aSJeff Roberson 29557b55ab05SJeff Roberson mtx_assert(&Giant, MA_NOTOWNED); 29567a5e5e2aSJeff Roberson td = curthread; 2957ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2958ba96d2d8SJohn Baldwin THREAD_NO_SLEEPING(); 29592c27cb3aSAlexander Motin oldswitchcnt = -1; 2960ae7a6b38SJeff Roberson for (;;) { 296111484ad8SMark Johnston if (TDQ_LOAD(tdq)) { 29622c27cb3aSAlexander Motin thread_lock(td); 2963686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IDLE); 29642c27cb3aSAlexander Motin } 296511484ad8SMark Johnston switchcnt = TDQ_SWITCHCNT(tdq); 2966ae7a6b38SJeff Roberson #ifdef SMP 296797e9382dSDon Lewis if (always_steal || switchcnt != oldswitchcnt) { 29682c27cb3aSAlexander Motin oldswitchcnt = switchcnt; 29691690c6c1SJeff Roberson if (tdq_idled(tdq) == 0) 29701690c6c1SJeff Roberson continue; 29712c27cb3aSAlexander Motin } 297211484ad8SMark Johnston switchcnt = TDQ_SWITCHCNT(tdq); 29732fd4047fSAlexander Motin #else 29742fd4047fSAlexander Motin oldswitchcnt = switchcnt; 29752fd4047fSAlexander Motin #endif 29761690c6c1SJeff Roberson /* 29771690c6c1SJeff Roberson * If we're switching very frequently, spin while checking 29781690c6c1SJeff Roberson * for load rather than entering a low power state that 29797b55ab05SJeff Roberson * may require an IPI. However, don't do any busy 29807b55ab05SJeff Roberson * loops while on SMT machines as this simply steals 29817b55ab05SJeff Roberson * cycles from cores doing useful work. 29821690c6c1SJeff Roberson */ 298309c8a4ccSJeff Roberson if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 29841690c6c1SJeff Roberson for (i = 0; i < sched_idlespins; i++) { 298511484ad8SMark Johnston if (TDQ_LOAD(tdq)) 29861690c6c1SJeff Roberson break; 29871690c6c1SJeff Roberson cpu_spinwait(); 29881690c6c1SJeff Roberson } 29891690c6c1SJeff Roberson } 29902c27cb3aSAlexander Motin 29912c27cb3aSAlexander Motin /* If there was context switch during spin, restart it. */ 299211484ad8SMark Johnston switchcnt = TDQ_SWITCHCNT(tdq); 299311484ad8SMark Johnston if (TDQ_LOAD(tdq) != 0 || switchcnt != oldswitchcnt) 29942c27cb3aSAlexander Motin continue; 29952c27cb3aSAlexander Motin 29962c27cb3aSAlexander Motin /* Run main MD idle handler. */ 299711484ad8SMark Johnston atomic_store_int(&tdq->tdq_cpu_idle, 1); 299879654969SAlexander Motin /* 29996d3f74a1SMark Johnston * Make sure that the tdq_cpu_idle update is globally visible 30006d3f74a1SMark Johnston * before cpu_idle() reads tdq_load. The order is important 30016d3f74a1SMark Johnston * to avoid races with tdq_notify(). 300279654969SAlexander Motin */ 3003e8677f38SKonstantin Belousov atomic_thread_fence_seq_cst(); 300497e9382dSDon Lewis /* 300597e9382dSDon Lewis * Checking for again after the fence picks up assigned 300697e9382dSDon Lewis * threads often enough to make it worthwhile to do so in 300797e9382dSDon Lewis * order to avoid calling cpu_idle(). 300897e9382dSDon Lewis */ 300911484ad8SMark Johnston if (TDQ_LOAD(tdq) != 0) { 301011484ad8SMark Johnston atomic_store_int(&tdq->tdq_cpu_idle, 0); 301197e9382dSDon Lewis continue; 301297e9382dSDon Lewis } 30132c27cb3aSAlexander Motin cpu_idle(switchcnt * 4 > sched_idlespinthresh); 301411484ad8SMark Johnston atomic_store_int(&tdq->tdq_cpu_idle, 0); 30152c27cb3aSAlexander Motin 30162c27cb3aSAlexander Motin /* 30172c27cb3aSAlexander Motin * Account thread-less hardware interrupts and 30182c27cb3aSAlexander Motin * other wakeup reasons equal to context switches. 30192c27cb3aSAlexander Motin */ 302011484ad8SMark Johnston switchcnt = TDQ_SWITCHCNT(tdq); 30212c27cb3aSAlexander Motin if (switchcnt != oldswitchcnt) 30222c27cb3aSAlexander Motin continue; 302311484ad8SMark Johnston TDQ_SWITCHCNT_INC(tdq); 30242c27cb3aSAlexander Motin oldswitchcnt++; 3025ae7a6b38SJeff Roberson } 3026b41f1452SDavid Xu } 3027e7d50326SJeff Roberson 30287b20fb19SJeff Roberson /* 30296a8ea6d1SKyle Evans * sched_throw_grab() chooses a thread from the queue to switch to 30306a8ea6d1SKyle Evans * next. It returns with the tdq lock dropped in a spinlock section to 30316a8ea6d1SKyle Evans * keep interrupts disabled until the CPU is running in a proper threaded 30326a8ea6d1SKyle Evans * context. 30336a8ea6d1SKyle Evans */ 30346a8ea6d1SKyle Evans static struct thread * 30356a8ea6d1SKyle Evans sched_throw_grab(struct tdq *tdq) 30366a8ea6d1SKyle Evans { 30376a8ea6d1SKyle Evans struct thread *newtd; 30386a8ea6d1SKyle Evans 30396a8ea6d1SKyle Evans newtd = choosethread(); 30406a8ea6d1SKyle Evans spinlock_enter(); 30416a8ea6d1SKyle Evans TDQ_UNLOCK(tdq); 30426a8ea6d1SKyle Evans KASSERT(curthread->td_md.md_spinlock_count == 1, 30436a8ea6d1SKyle Evans ("invalid count %d", curthread->td_md.md_spinlock_count)); 30446a8ea6d1SKyle Evans return (newtd); 30456a8ea6d1SKyle Evans } 30466a8ea6d1SKyle Evans 30476a8ea6d1SKyle Evans /* 30486a8ea6d1SKyle Evans * A CPU is entering for the first time. 30496a8ea6d1SKyle Evans */ 30506a8ea6d1SKyle Evans void 30516a8ea6d1SKyle Evans sched_ap_entry(void) 30526a8ea6d1SKyle Evans { 30536a8ea6d1SKyle Evans struct thread *newtd; 30546a8ea6d1SKyle Evans struct tdq *tdq; 30556a8ea6d1SKyle Evans 30566a8ea6d1SKyle Evans tdq = TDQ_SELF(); 30576a8ea6d1SKyle Evans 30586a8ea6d1SKyle Evans /* This should have been setup in schedinit_ap(). */ 30596a8ea6d1SKyle Evans THREAD_LOCKPTR_ASSERT(curthread, TDQ_LOCKPTR(tdq)); 30606a8ea6d1SKyle Evans 30616a8ea6d1SKyle Evans TDQ_LOCK(tdq); 30626a8ea6d1SKyle Evans /* Correct spinlock nesting. */ 30636a8ea6d1SKyle Evans spinlock_exit(); 30646a8ea6d1SKyle Evans PCPU_SET(switchtime, cpu_ticks()); 30656a8ea6d1SKyle Evans PCPU_SET(switchticks, ticks); 30666a8ea6d1SKyle Evans 30676a8ea6d1SKyle Evans newtd = sched_throw_grab(tdq); 30686a8ea6d1SKyle Evans 30696a8ea6d1SKyle Evans /* doesn't return */ 30706a8ea6d1SKyle Evans cpu_throw(NULL, newtd); 30716a8ea6d1SKyle Evans } 30726a8ea6d1SKyle Evans 30736a8ea6d1SKyle Evans /* 30746a8ea6d1SKyle Evans * A thread is exiting. 30757b20fb19SJeff Roberson */ 30767b20fb19SJeff Roberson void 30777b20fb19SJeff Roberson sched_throw(struct thread *td) 30787b20fb19SJeff Roberson { 307959c68134SJeff Roberson struct thread *newtd; 3080ae7a6b38SJeff Roberson struct tdq *tdq; 3081ae7a6b38SJeff Roberson 3082018ff686SJeff Roberson tdq = TDQ_SELF(); 30836a8ea6d1SKyle Evans 30846a8ea6d1SKyle Evans MPASS(td != NULL); 3085686bcb5cSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 3086686bcb5cSJeff Roberson THREAD_LOCKPTR_ASSERT(td, TDQ_LOCKPTR(tdq)); 30876a8ea6d1SKyle Evans 30889727e637SJeff Roberson tdq_load_rem(tdq, td); 308992de34dfSJohn Baldwin td->td_lastcpu = td->td_oncpu; 309092de34dfSJohn Baldwin td->td_oncpu = NOCPU; 30911eb13fceSJeff Roberson thread_lock_block(td); 30926a8ea6d1SKyle Evans 30936a8ea6d1SKyle Evans newtd = sched_throw_grab(tdq); 30946a8ea6d1SKyle Evans 30951eb13fceSJeff Roberson /* doesn't return */ 30961eb13fceSJeff Roberson cpu_switch(td, newtd, TDQ_LOCKPTR(tdq)); 30977b20fb19SJeff Roberson } 30987b20fb19SJeff Roberson 3099ae7a6b38SJeff Roberson /* 3100ae7a6b38SJeff Roberson * This is called from fork_exit(). Just acquire the correct locks and 3101ae7a6b38SJeff Roberson * let fork do the rest of the work. 3102ae7a6b38SJeff Roberson */ 31037b20fb19SJeff Roberson void 3104fe54587fSJeff Roberson sched_fork_exit(struct thread *td) 31057b20fb19SJeff Roberson { 3106ae7a6b38SJeff Roberson struct tdq *tdq; 3107ae7a6b38SJeff Roberson int cpuid; 31087b20fb19SJeff Roberson 31097b20fb19SJeff Roberson /* 31107b20fb19SJeff Roberson * Finish setting up thread glue so that it begins execution in a 3111ae7a6b38SJeff Roberson * non-nested critical section with the scheduler lock held. 31127b20fb19SJeff Roberson */ 3113686bcb5cSJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, 3114686bcb5cSJeff Roberson ("invalid count %d", curthread->td_md.md_spinlock_count)); 3115ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 3116018ff686SJeff Roberson tdq = TDQ_SELF(); 3117686bcb5cSJeff Roberson TDQ_LOCK(tdq); 3118686bcb5cSJeff Roberson spinlock_exit(); 3119ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 3120ae7a6b38SJeff Roberson td->td_oncpu = cpuid; 312128ef18b8SAndriy Gapon KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "running", 312228ef18b8SAndriy Gapon "prio:%d", td->td_priority); 312328ef18b8SAndriy Gapon SDT_PROBE0(sched, , , on__cpu); 31247b20fb19SJeff Roberson } 31257b20fb19SJeff Roberson 31268f51ad55SJeff Roberson /* 312715b5c347SGordon Bergling * Create on first use to catch odd startup conditions. 31288f51ad55SJeff Roberson */ 31298f51ad55SJeff Roberson char * 31308f51ad55SJeff Roberson sched_tdname(struct thread *td) 31318f51ad55SJeff Roberson { 31328f51ad55SJeff Roberson #ifdef KTR 31338f51ad55SJeff Roberson struct td_sched *ts; 31348f51ad55SJeff Roberson 313593ccd6bfSKonstantin Belousov ts = td_get_sched(td); 31368f51ad55SJeff Roberson if (ts->ts_name[0] == '\0') 31378f51ad55SJeff Roberson snprintf(ts->ts_name, sizeof(ts->ts_name), 31388f51ad55SJeff Roberson "%s tid %d", td->td_name, td->td_tid); 31398f51ad55SJeff Roberson return (ts->ts_name); 31408f51ad55SJeff Roberson #else 31418f51ad55SJeff Roberson return (td->td_name); 31428f51ad55SJeff Roberson #endif 31438f51ad55SJeff Roberson } 31448f51ad55SJeff Roberson 314544ad5475SJohn Baldwin #ifdef KTR 314644ad5475SJohn Baldwin void 314744ad5475SJohn Baldwin sched_clear_tdname(struct thread *td) 314844ad5475SJohn Baldwin { 314944ad5475SJohn Baldwin struct td_sched *ts; 315044ad5475SJohn Baldwin 315193ccd6bfSKonstantin Belousov ts = td_get_sched(td); 315244ad5475SJohn Baldwin ts->ts_name[0] = '\0'; 315344ad5475SJohn Baldwin } 315444ad5475SJohn Baldwin #endif 315544ad5475SJohn Baldwin 315607095abfSIvan Voras #ifdef SMP 315707095abfSIvan Voras 315807095abfSIvan Voras /* 315907095abfSIvan Voras * Build the CPU topology dump string. Is recursively called to collect 316007095abfSIvan Voras * the topology tree. 316107095abfSIvan Voras */ 316207095abfSIvan Voras static int 316307095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 316407095abfSIvan Voras int indent) 316507095abfSIvan Voras { 316671a19bdcSAttilio Rao char cpusetbuf[CPUSETBUFSIZ]; 316707095abfSIvan Voras int i, first; 316807095abfSIvan Voras 316907095abfSIvan Voras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 317019b8a6dbSAndriy Gapon "", 1 + indent / 2, cg->cg_level); 317171a19bdcSAttilio Rao sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"%s\">", indent, "", 317271a19bdcSAttilio Rao cg->cg_count, cpusetobj_strprint(cpusetbuf, &cg->cg_mask)); 317307095abfSIvan Voras first = TRUE; 3174aefe0a8cSAlexander Motin for (i = cg->cg_first; i <= cg->cg_last; i++) { 317571a19bdcSAttilio Rao if (CPU_ISSET(i, &cg->cg_mask)) { 317607095abfSIvan Voras if (!first) 317707095abfSIvan Voras sbuf_printf(sb, ", "); 317807095abfSIvan Voras else 317907095abfSIvan Voras first = FALSE; 318007095abfSIvan Voras sbuf_printf(sb, "%d", i); 318107095abfSIvan Voras } 318207095abfSIvan Voras } 318307095abfSIvan Voras sbuf_printf(sb, "</cpu>\n"); 318407095abfSIvan Voras 318507095abfSIvan Voras if (cg->cg_flags != 0) { 3186611daf7eSIvan Voras sbuf_printf(sb, "%*s <flags>", indent, ""); 318707095abfSIvan Voras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 31885368befbSIvan Voras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 3189a401f2d0SIvan Voras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 3190a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 31917b55ab05SJeff Roberson if ((cg->cg_flags & CG_FLAG_SMT) != 0) 3192a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 3193ef50d5fbSAlexander Motin if ((cg->cg_flags & CG_FLAG_NODE) != 0) 3194ef50d5fbSAlexander Motin sbuf_printf(sb, "<flag name=\"NODE\">NUMA node</flag>"); 319507095abfSIvan Voras sbuf_printf(sb, "</flags>\n"); 3196611daf7eSIvan Voras } 319707095abfSIvan Voras 319807095abfSIvan Voras if (cg->cg_children > 0) { 319907095abfSIvan Voras sbuf_printf(sb, "%*s <children>\n", indent, ""); 320007095abfSIvan Voras for (i = 0; i < cg->cg_children; i++) 320107095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(sb, 320207095abfSIvan Voras &cg->cg_child[i], indent+2); 320307095abfSIvan Voras sbuf_printf(sb, "%*s </children>\n", indent, ""); 320407095abfSIvan Voras } 320507095abfSIvan Voras sbuf_printf(sb, "%*s</group>\n", indent, ""); 320607095abfSIvan Voras return (0); 320707095abfSIvan Voras } 320807095abfSIvan Voras 320907095abfSIvan Voras /* 321007095abfSIvan Voras * Sysctl handler for retrieving topology dump. It's a wrapper for 321107095abfSIvan Voras * the recursive sysctl_kern_smp_topology_spec_internal(). 321207095abfSIvan Voras */ 321307095abfSIvan Voras static int 321407095abfSIvan Voras sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 321507095abfSIvan Voras { 321607095abfSIvan Voras struct sbuf *topo; 321707095abfSIvan Voras int err; 321807095abfSIvan Voras 321907095abfSIvan Voras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 322007095abfSIvan Voras 3221b97fa22cSIan Lepore topo = sbuf_new_for_sysctl(NULL, NULL, 512, req); 322207095abfSIvan Voras if (topo == NULL) 322307095abfSIvan Voras return (ENOMEM); 322407095abfSIvan Voras 322507095abfSIvan Voras sbuf_printf(topo, "<groups>\n"); 322607095abfSIvan Voras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 322707095abfSIvan Voras sbuf_printf(topo, "</groups>\n"); 322807095abfSIvan Voras 322907095abfSIvan Voras if (err == 0) { 3230b97fa22cSIan Lepore err = sbuf_finish(topo); 323107095abfSIvan Voras } 323207095abfSIvan Voras sbuf_delete(topo); 323307095abfSIvan Voras return (err); 323407095abfSIvan Voras } 3235b67cc292SDavid Xu 323607095abfSIvan Voras #endif 323707095abfSIvan Voras 3238579895dfSAlexander Motin static int 3239579895dfSAlexander Motin sysctl_kern_quantum(SYSCTL_HANDLER_ARGS) 3240579895dfSAlexander Motin { 3241579895dfSAlexander Motin int error, new_val, period; 3242579895dfSAlexander Motin 3243579895dfSAlexander Motin period = 1000000 / realstathz; 3244579895dfSAlexander Motin new_val = period * sched_slice; 3245579895dfSAlexander Motin error = sysctl_handle_int(oidp, &new_val, 0, req); 3246579895dfSAlexander Motin if (error != 0 || req->newptr == NULL) 3247579895dfSAlexander Motin return (error); 3248579895dfSAlexander Motin if (new_val <= 0) 3249579895dfSAlexander Motin return (EINVAL); 325037f4e025SAlexander Motin sched_slice = imax(1, (new_val + period / 2) / period); 32515e5c3873SJeff Roberson sched_slice_min = sched_slice / SCHED_SLICE_MIN_DIVISOR; 325237f4e025SAlexander Motin hogticks = imax(1, (2 * hz * sched_slice + realstathz / 2) / 325337f4e025SAlexander Motin realstathz); 3254579895dfSAlexander Motin return (0); 3255579895dfSAlexander Motin } 3256579895dfSAlexander Motin 32577029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 32587029da5cSPawel Biernacki "Scheduler"); 3259ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 3260e7d50326SJeff Roberson "Scheduler name"); 32617029da5cSPawel Biernacki SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, 32627029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0, 32637029da5cSPawel Biernacki sysctl_kern_quantum, "I", 326437f4e025SAlexander Motin "Quantum for timeshare threads in microseconds"); 3265ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 326637f4e025SAlexander Motin "Quantum for timeshare threads in stathz ticks"); 32671c119e17SAlexander Motin SYSCTL_UINT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 3268ae7a6b38SJeff Roberson "Interactivity score threshold"); 326937f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, 327037f4e025SAlexander Motin &preempt_thresh, 0, 327137f4e025SAlexander Motin "Maximal (lowest) priority for preemption"); 327237f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 0, 327337f4e025SAlexander Motin "Assign static kernel priorities to sleeping threads"); 327437f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 0, 327537f4e025SAlexander Motin "Number of times idle thread will spin waiting for new work"); 327637f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, 327737f4e025SAlexander Motin &sched_idlespinthresh, 0, 327837f4e025SAlexander Motin "Threshold before we will permit idle thread spinning"); 32797b8bfa0dSJeff Roberson #ifdef SMP 3280ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 3281ae7a6b38SJeff Roberson "Number of hz ticks to keep thread affinity for"); 3282ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 3283ae7a6b38SJeff Roberson "Enables the long-term load balancer"); 32847fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 32857fcf154aSJeff Roberson &balance_interval, 0, 3286579895dfSAlexander Motin "Average period in stathz ticks to run the long-term balancer"); 3287ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 3288ae7a6b38SJeff Roberson "Attempts to steal work from other cores before idling"); 328928994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 329037f4e025SAlexander Motin "Minimum load on remote CPU before we'll steal"); 329197e9382dSDon Lewis SYSCTL_INT(_kern_sched, OID_AUTO, trysteal_limit, CTLFLAG_RW, &trysteal_limit, 329297e9382dSDon Lewis 0, "Topological distance limit for stealing threads in sched_switch()"); 329397e9382dSDon Lewis SYSCTL_INT(_kern_sched, OID_AUTO, always_steal, CTLFLAG_RW, &always_steal, 0, 329497e9382dSDon Lewis "Always run the stealer from the idle thread"); 329507095abfSIvan Voras SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 3296c69a1a50SMateusz Guzik CTLFLAG_MPSAFE | CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 329707095abfSIvan Voras "XML dump of detected CPU topology"); 32987b8bfa0dSJeff Roberson #endif 3299e7d50326SJeff Roberson 330054b0e65fSJeff Roberson /* ps compat. All cpu percentages from ULE are weighted. */ 3301a5423ea3SJeff Roberson static int ccpu = 0; 3302b05ca429SPawel Biernacki SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, 3303b05ca429SPawel Biernacki "Decay factor used for updating %CPU in 4BSD scheduler"); 3304