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> 643db720fdSDavid Xu #include <sys/umtx.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. */ 1993d7f4117SAlexander Motin #define TDF_SLICEEND TDF_SCHED2 /* Thread time slice is over. */ 2003d7f4117SAlexander Motin 20135e6168fSJeff Roberson /* 202e7d50326SJeff Roberson * tickincr: Converts a stathz tick into a hz domain scaled by 203e7d50326SJeff Roberson * the shift factor. Without the shift the error rate 204e7d50326SJeff Roberson * due to rounding would be unacceptably high. 205e7d50326SJeff Roberson * realstathz: stathz is sometimes 0 and run off of hz. 206e7d50326SJeff Roberson * sched_slice: Runtime of each thread before rescheduling. 207ae7a6b38SJeff Roberson * preempt_thresh: Priority threshold for preemption and remote IPIs. 20835e6168fSJeff Roberson */ 20961322a0aSAlexander Motin static int __read_mostly sched_interact = SCHED_INTERACT_THRESH; 21061322a0aSAlexander Motin static int __read_mostly tickincr = 8 << SCHED_TICK_SHIFT; 21161322a0aSAlexander Motin static int __read_mostly realstathz = 127; /* reset during boot. */ 21261322a0aSAlexander Motin static int __read_mostly sched_slice = 10; /* reset during boot. */ 21361322a0aSAlexander Motin static int __read_mostly sched_slice_min = 1; /* reset during boot. */ 21402e2d6b4SJeff Roberson #ifdef PREEMPTION 21502e2d6b4SJeff Roberson #ifdef FULL_PREEMPTION 21661322a0aSAlexander Motin static int __read_mostly preempt_thresh = PRI_MAX_IDLE; 21702e2d6b4SJeff Roberson #else 21861322a0aSAlexander Motin static int __read_mostly preempt_thresh = PRI_MIN_KERN; 21902e2d6b4SJeff Roberson #endif 22002e2d6b4SJeff Roberson #else 22161322a0aSAlexander Motin static int __read_mostly preempt_thresh = 0; 22202e2d6b4SJeff Roberson #endif 22361322a0aSAlexander Motin static int __read_mostly static_boost = PRI_MIN_BATCH; 22461322a0aSAlexander Motin static int __read_mostly sched_idlespins = 10000; 22561322a0aSAlexander Motin static int __read_mostly sched_idlespinthresh = -1; 226ae7a6b38SJeff Roberson 22735e6168fSJeff Roberson /* 228ae7a6b38SJeff Roberson * tdq - per processor runqs and statistics. All fields are protected by the 229ae7a6b38SJeff Roberson * tdq_lock. The load and lowpri may be accessed without to avoid excess 230ae7a6b38SJeff Roberson * locking in sched_pickcpu(); 23135e6168fSJeff Roberson */ 232ad1e7d28SJulian Elischer struct tdq { 23339f819e2SJim Harris /* 23439f819e2SJim Harris * Ordered to improve efficiency of cpu_search() and switch(). 23539f819e2SJim Harris * tdq_lock is padded to avoid false sharing with tdq_load and 23639f819e2SJim Harris * tdq_cpu_idle. 23739f819e2SJim Harris */ 2384ceaf45dSAttilio Rao struct mtx_padalign tdq_lock; /* run queue lock. */ 23973daf66fSJeff Roberson struct cpu_group *tdq_cg; /* Pointer to cpu topology. */ 2401690c6c1SJeff Roberson volatile int tdq_load; /* Aggregate load. */ 2419f9ad565SAlexander Motin volatile int tdq_cpu_idle; /* cpu_idle() is active. */ 24273daf66fSJeff Roberson int tdq_sysload; /* For loadavg, !ITHD load. */ 24397e9382dSDon Lewis volatile int tdq_transferable; /* Transferable thread count. */ 24497e9382dSDon Lewis volatile short tdq_switchcnt; /* Switches this tick. */ 24597e9382dSDon Lewis volatile short tdq_oldswitchcnt; /* Switches last tick. */ 24673daf66fSJeff Roberson u_char tdq_lowpri; /* Lowest priority thread. */ 2477789ab32SMark Johnston u_char tdq_owepreempt; /* Remote preemption pending. */ 24873daf66fSJeff Roberson u_char tdq_idx; /* Current insert index. */ 24973daf66fSJeff Roberson u_char tdq_ridx; /* Current removal index. */ 250018ff686SJeff Roberson int tdq_id; /* cpuid. */ 251e7d50326SJeff Roberson struct runq tdq_realtime; /* real-time run queue. */ 252ae7a6b38SJeff Roberson struct runq tdq_timeshare; /* timeshare run queue. */ 253ae7a6b38SJeff Roberson struct runq tdq_idle; /* Queue of IDLE threads. */ 2548f51ad55SJeff Roberson char tdq_name[TDQ_NAME_LEN]; 2558f51ad55SJeff Roberson #ifdef KTR 2568f51ad55SJeff Roberson char tdq_loadname[TDQ_LOADNAME_LEN]; 2578f51ad55SJeff Roberson #endif 258ae7a6b38SJeff Roberson } __aligned(64); 25935e6168fSJeff Roberson 2601690c6c1SJeff Roberson /* Idle thread states and config. */ 2611690c6c1SJeff Roberson #define TDQ_RUNNING 1 2621690c6c1SJeff Roberson #define TDQ_IDLE 2 2637b8bfa0dSJeff Roberson 26480f86c9fSJeff Roberson #ifdef SMP 26561322a0aSAlexander Motin struct cpu_group __read_mostly *cpu_top; /* CPU topology */ 2667b8bfa0dSJeff Roberson 26762fa74d9SJeff Roberson #define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000)) 26862fa74d9SJeff Roberson #define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity)) 2697b8bfa0dSJeff Roberson 2707b8bfa0dSJeff Roberson /* 2717b8bfa0dSJeff Roberson * Run-time tunables. 2727b8bfa0dSJeff Roberson */ 27328994a58SJeff Roberson static int rebalance = 1; 2747fcf154aSJeff Roberson static int balance_interval = 128; /* Default set in sched_initticks(). */ 27561322a0aSAlexander Motin static int __read_mostly affinity; 27661322a0aSAlexander Motin static int __read_mostly steal_idle = 1; 27761322a0aSAlexander Motin static int __read_mostly steal_thresh = 2; 27861322a0aSAlexander Motin static int __read_mostly always_steal = 0; 27961322a0aSAlexander Motin static int __read_mostly trysteal_limit = 2; 28080f86c9fSJeff Roberson 28135e6168fSJeff Roberson /* 282d2ad694cSJeff Roberson * One thread queue per processor. 28335e6168fSJeff Roberson */ 28461322a0aSAlexander Motin static struct tdq __read_mostly *balance_tdq; 2857fcf154aSJeff Roberson static int balance_ticks; 286018ff686SJeff Roberson DPCPU_DEFINE_STATIC(struct tdq, tdq); 2872bf95012SAndrew Turner DPCPU_DEFINE_STATIC(uint32_t, randomval); 288dc03363dSJeff Roberson 289018ff686SJeff Roberson #define TDQ_SELF() ((struct tdq *)PCPU_GET(sched)) 290018ff686SJeff Roberson #define TDQ_CPU(x) (DPCPU_ID_PTR((x), tdq)) 291018ff686SJeff Roberson #define TDQ_ID(x) ((x)->tdq_id) 29280f86c9fSJeff Roberson #else /* !SMP */ 293ad1e7d28SJulian Elischer static struct tdq tdq_cpu; 294dc03363dSJeff Roberson 29536b36916SJeff Roberson #define TDQ_ID(x) (0) 296ad1e7d28SJulian Elischer #define TDQ_SELF() (&tdq_cpu) 297ad1e7d28SJulian Elischer #define TDQ_CPU(x) (&tdq_cpu) 2980a016a05SJeff Roberson #endif 29935e6168fSJeff Roberson 300ae7a6b38SJeff Roberson #define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type)) 301ae7a6b38SJeff Roberson #define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t))) 302ae7a6b38SJeff Roberson #define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f)) 303ae7a6b38SJeff Roberson #define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t))) 3044ceaf45dSAttilio Rao #define TDQ_LOCKPTR(t) ((struct mtx *)(&(t)->tdq_lock)) 305ae7a6b38SJeff Roberson 3068460a577SJohn Birrell static void sched_priority(struct thread *); 30721381d1bSJeff Roberson static void sched_thread_priority(struct thread *, u_char); 3088460a577SJohn Birrell static int sched_interact_score(struct thread *); 3098460a577SJohn Birrell static void sched_interact_update(struct thread *); 3108460a577SJohn Birrell static void sched_interact_fork(struct thread *); 3117295465eSAlexander Motin static void sched_pctcpu_update(struct td_sched *, int); 31235e6168fSJeff Roberson 3135d7ef00cSJeff Roberson /* Operations on per processor queues */ 3149727e637SJeff Roberson static struct thread *tdq_choose(struct tdq *); 315018ff686SJeff Roberson static void tdq_setup(struct tdq *, int i); 3169727e637SJeff Roberson static void tdq_load_add(struct tdq *, struct thread *); 3179727e637SJeff Roberson static void tdq_load_rem(struct tdq *, struct thread *); 3189727e637SJeff Roberson static __inline void tdq_runq_add(struct tdq *, struct thread *, int); 3199727e637SJeff Roberson static __inline void tdq_runq_rem(struct tdq *, struct thread *); 320ff256d9cSJeff Roberson static inline int sched_shouldpreempt(int, int, int); 321ad1e7d28SJulian Elischer void tdq_print(int cpu); 322e7d50326SJeff Roberson static void runq_print(struct runq *rq); 323ae7a6b38SJeff Roberson static void tdq_add(struct tdq *, struct thread *, int); 3245d7ef00cSJeff Roberson #ifdef SMP 32597e9382dSDon Lewis static struct thread *tdq_move(struct tdq *, struct tdq *); 326ad1e7d28SJulian Elischer static int tdq_idled(struct tdq *); 32727ee18adSRyan Stone static void tdq_notify(struct tdq *, struct thread *); 3289727e637SJeff Roberson static struct thread *tdq_steal(struct tdq *, int); 3299727e637SJeff Roberson static struct thread *runq_steal(struct runq *, int); 3309727e637SJeff Roberson static int sched_pickcpu(struct thread *, int); 3317fcf154aSJeff Roberson static void sched_balance(void); 33262fa74d9SJeff Roberson static int sched_balance_pair(struct tdq *, struct tdq *); 3339727e637SJeff Roberson static inline struct tdq *sched_setcpu(struct thread *, int, int); 334ae7a6b38SJeff Roberson static inline void thread_unblock_switch(struct thread *, struct mtx *); 33507095abfSIvan Voras static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS); 33607095abfSIvan Voras static int sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, 33707095abfSIvan Voras struct cpu_group *cg, int indent); 3385d7ef00cSJeff Roberson #endif 3395d7ef00cSJeff Roberson 340e7d50326SJeff Roberson static void sched_setup(void *dummy); 341237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL); 342e7d50326SJeff Roberson 343e7d50326SJeff Roberson static void sched_initticks(void *dummy); 344237fdd78SRobert Watson SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, 345237fdd78SRobert Watson NULL); 346e7d50326SJeff Roberson 347b3e9e682SRyan Stone SDT_PROVIDER_DEFINE(sched); 348b3e9e682SRyan Stone 349d9fae5abSAndriy Gapon SDT_PROBE_DEFINE3(sched, , , change__pri, "struct thread *", 350b3e9e682SRyan Stone "struct proc *", "uint8_t"); 351d9fae5abSAndriy Gapon SDT_PROBE_DEFINE3(sched, , , dequeue, "struct thread *", 352b3e9e682SRyan Stone "struct proc *", "void *"); 353d9fae5abSAndriy Gapon SDT_PROBE_DEFINE4(sched, , , enqueue, "struct thread *", 354b3e9e682SRyan Stone "struct proc *", "void *", "int"); 355d9fae5abSAndriy Gapon SDT_PROBE_DEFINE4(sched, , , lend__pri, "struct thread *", 356b3e9e682SRyan Stone "struct proc *", "uint8_t", "struct thread *"); 357d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , load__change, "int", "int"); 358d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , off__cpu, "struct thread *", 359b3e9e682SRyan Stone "struct proc *"); 360d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(sched, , , on__cpu); 361d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(sched, , , remain__cpu); 362d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , surrender, "struct thread *", 363b3e9e682SRyan Stone "struct proc *"); 364b3e9e682SRyan Stone 3650567b6ccSWarner Losh /* 366ae7a6b38SJeff Roberson * Print the threads waiting on a run-queue. 367ae7a6b38SJeff Roberson */ 368e7d50326SJeff Roberson static void 369e7d50326SJeff Roberson runq_print(struct runq *rq) 370e7d50326SJeff Roberson { 371e7d50326SJeff Roberson struct rqhead *rqh; 3729727e637SJeff Roberson struct thread *td; 373e7d50326SJeff Roberson int pri; 374e7d50326SJeff Roberson int j; 375e7d50326SJeff Roberson int i; 376e7d50326SJeff Roberson 377e7d50326SJeff Roberson for (i = 0; i < RQB_LEN; i++) { 378e7d50326SJeff Roberson printf("\t\trunq bits %d 0x%zx\n", 379e7d50326SJeff Roberson i, rq->rq_status.rqb_bits[i]); 380e7d50326SJeff Roberson for (j = 0; j < RQB_BPW; j++) 381e7d50326SJeff Roberson if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 382e7d50326SJeff Roberson pri = j + (i << RQB_L2BPW); 383e7d50326SJeff Roberson rqh = &rq->rq_queues[pri]; 3849727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 385e7d50326SJeff Roberson printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 3869727e637SJeff Roberson td, td->td_name, td->td_priority, 3879727e637SJeff Roberson td->td_rqindex, pri); 388e7d50326SJeff Roberson } 389e7d50326SJeff Roberson } 390e7d50326SJeff Roberson } 391e7d50326SJeff Roberson } 392e7d50326SJeff Roberson 393ae7a6b38SJeff Roberson /* 394ae7a6b38SJeff Roberson * Print the status of a per-cpu thread queue. Should be a ddb show cmd. 395ae7a6b38SJeff Roberson */ 39615dc847eSJeff Roberson void 397ad1e7d28SJulian Elischer tdq_print(int cpu) 39815dc847eSJeff Roberson { 399ad1e7d28SJulian Elischer struct tdq *tdq; 40015dc847eSJeff Roberson 401ad1e7d28SJulian Elischer tdq = TDQ_CPU(cpu); 40215dc847eSJeff Roberson 403c47f202bSJeff Roberson printf("tdq %d:\n", TDQ_ID(tdq)); 40462fa74d9SJeff Roberson printf("\tlock %p\n", TDQ_LOCKPTR(tdq)); 40562fa74d9SJeff Roberson printf("\tLock name: %s\n", tdq->tdq_name); 406d2ad694cSJeff Roberson printf("\tload: %d\n", tdq->tdq_load); 4071690c6c1SJeff Roberson printf("\tswitch cnt: %d\n", tdq->tdq_switchcnt); 4081690c6c1SJeff Roberson printf("\told switch cnt: %d\n", tdq->tdq_oldswitchcnt); 409e7d50326SJeff Roberson printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 4103f872f85SJeff Roberson printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 4111690c6c1SJeff Roberson printf("\tload transferable: %d\n", tdq->tdq_transferable); 4121690c6c1SJeff Roberson printf("\tlowest priority: %d\n", tdq->tdq_lowpri); 413e7d50326SJeff Roberson printf("\trealtime runq:\n"); 414e7d50326SJeff Roberson runq_print(&tdq->tdq_realtime); 415e7d50326SJeff Roberson printf("\ttimeshare runq:\n"); 416e7d50326SJeff Roberson runq_print(&tdq->tdq_timeshare); 417e7d50326SJeff Roberson printf("\tidle runq:\n"); 418e7d50326SJeff Roberson runq_print(&tdq->tdq_idle); 41915dc847eSJeff Roberson } 42015dc847eSJeff Roberson 421ff256d9cSJeff Roberson static inline int 422ff256d9cSJeff Roberson sched_shouldpreempt(int pri, int cpri, int remote) 423ff256d9cSJeff Roberson { 424ff256d9cSJeff Roberson /* 425ff256d9cSJeff Roberson * If the new priority is not better than the current priority there is 426ff256d9cSJeff Roberson * nothing to do. 427ff256d9cSJeff Roberson */ 428ff256d9cSJeff Roberson if (pri >= cpri) 429ff256d9cSJeff Roberson return (0); 430ff256d9cSJeff Roberson /* 431ff256d9cSJeff Roberson * Always preempt idle. 432ff256d9cSJeff Roberson */ 433ff256d9cSJeff Roberson if (cpri >= PRI_MIN_IDLE) 434ff256d9cSJeff Roberson return (1); 435ff256d9cSJeff Roberson /* 436ff256d9cSJeff Roberson * If preemption is disabled don't preempt others. 437ff256d9cSJeff Roberson */ 438ff256d9cSJeff Roberson if (preempt_thresh == 0) 439ff256d9cSJeff Roberson return (0); 440ff256d9cSJeff Roberson /* 441ff256d9cSJeff Roberson * Preempt if we exceed the threshold. 442ff256d9cSJeff Roberson */ 443ff256d9cSJeff Roberson if (pri <= preempt_thresh) 444ff256d9cSJeff Roberson return (1); 445ff256d9cSJeff Roberson /* 44612d56c0fSJohn Baldwin * If we're interactive or better and there is non-interactive 44712d56c0fSJohn Baldwin * or worse running preempt only remote processors. 448ff256d9cSJeff Roberson */ 44912d56c0fSJohn Baldwin if (remote && pri <= PRI_MAX_INTERACT && cpri > PRI_MAX_INTERACT) 450ff256d9cSJeff Roberson return (1); 451ff256d9cSJeff Roberson return (0); 452ff256d9cSJeff Roberson } 453ff256d9cSJeff Roberson 454ae7a6b38SJeff Roberson /* 455ae7a6b38SJeff Roberson * Add a thread to the actual run-queue. Keeps transferable counts up to 456ae7a6b38SJeff Roberson * date with what is actually on the run-queue. Selects the correct 457ae7a6b38SJeff Roberson * queue position for timeshare threads. 458ae7a6b38SJeff Roberson */ 459155b9987SJeff Roberson static __inline void 4609727e637SJeff Roberson tdq_runq_add(struct tdq *tdq, struct thread *td, int flags) 461155b9987SJeff Roberson { 4629727e637SJeff Roberson struct td_sched *ts; 463c143ac21SJeff Roberson u_char pri; 464c143ac21SJeff Roberson 465ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 46661a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 46773daf66fSJeff Roberson 4689727e637SJeff Roberson pri = td->td_priority; 46993ccd6bfSKonstantin Belousov ts = td_get_sched(td); 4709727e637SJeff Roberson TD_SET_RUNQ(td); 4719727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td)) { 472d2ad694cSJeff Roberson tdq->tdq_transferable++; 473ad1e7d28SJulian Elischer ts->ts_flags |= TSF_XFERABLE; 47480f86c9fSJeff Roberson } 47512d56c0fSJohn Baldwin if (pri < PRI_MIN_BATCH) { 476c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_realtime; 47712d56c0fSJohn Baldwin } else if (pri <= PRI_MAX_BATCH) { 478c143ac21SJeff Roberson ts->ts_runq = &tdq->tdq_timeshare; 47912d56c0fSJohn Baldwin KASSERT(pri <= PRI_MAX_BATCH && pri >= PRI_MIN_BATCH, 480e7d50326SJeff Roberson ("Invalid priority %d on timeshare runq", pri)); 481e7d50326SJeff Roberson /* 482e7d50326SJeff Roberson * This queue contains only priorities between MIN and MAX 483e7d50326SJeff Roberson * realtime. Use the whole queue to represent these values. 484e7d50326SJeff Roberson */ 485c47f202bSJeff Roberson if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) { 48616705791SAndriy Gapon pri = RQ_NQS * (pri - PRI_MIN_BATCH) / PRI_BATCH_RANGE; 487e7d50326SJeff Roberson pri = (pri + tdq->tdq_idx) % RQ_NQS; 4883f872f85SJeff Roberson /* 4893f872f85SJeff Roberson * This effectively shortens the queue by one so we 4903f872f85SJeff Roberson * can have a one slot difference between idx and 4913f872f85SJeff Roberson * ridx while we wait for threads to drain. 4923f872f85SJeff Roberson */ 4933f872f85SJeff Roberson if (tdq->tdq_ridx != tdq->tdq_idx && 4943f872f85SJeff Roberson pri == tdq->tdq_ridx) 4954499aff6SJeff Roberson pri = (unsigned char)(pri - 1) % RQ_NQS; 496e7d50326SJeff Roberson } else 4973f872f85SJeff Roberson pri = tdq->tdq_ridx; 4989727e637SJeff Roberson runq_add_pri(ts->ts_runq, td, pri, flags); 499c143ac21SJeff Roberson return; 500e7d50326SJeff Roberson } else 50173daf66fSJeff Roberson ts->ts_runq = &tdq->tdq_idle; 5029727e637SJeff Roberson runq_add(ts->ts_runq, td, flags); 50373daf66fSJeff Roberson } 50473daf66fSJeff Roberson 50573daf66fSJeff Roberson /* 506ae7a6b38SJeff Roberson * Remove a thread from a run-queue. This typically happens when a thread 507ae7a6b38SJeff Roberson * is selected to run. Running threads are not on the queue and the 508ae7a6b38SJeff Roberson * transferable count does not reflect them. 509ae7a6b38SJeff Roberson */ 510155b9987SJeff Roberson static __inline void 5119727e637SJeff Roberson tdq_runq_rem(struct tdq *tdq, struct thread *td) 512155b9987SJeff Roberson { 5139727e637SJeff Roberson struct td_sched *ts; 5149727e637SJeff Roberson 51593ccd6bfSKonstantin Belousov ts = td_get_sched(td); 516ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 51761a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 518ae7a6b38SJeff Roberson KASSERT(ts->ts_runq != NULL, 5199727e637SJeff Roberson ("tdq_runq_remove: thread %p null ts_runq", td)); 520ad1e7d28SJulian Elischer if (ts->ts_flags & TSF_XFERABLE) { 521d2ad694cSJeff Roberson tdq->tdq_transferable--; 522ad1e7d28SJulian Elischer ts->ts_flags &= ~TSF_XFERABLE; 52380f86c9fSJeff Roberson } 5243f872f85SJeff Roberson if (ts->ts_runq == &tdq->tdq_timeshare) { 5253f872f85SJeff Roberson if (tdq->tdq_idx != tdq->tdq_ridx) 5269727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, &tdq->tdq_ridx); 527e7d50326SJeff Roberson else 5289727e637SJeff Roberson runq_remove_idx(ts->ts_runq, td, NULL); 5293f872f85SJeff Roberson } else 5309727e637SJeff Roberson runq_remove(ts->ts_runq, td); 531155b9987SJeff Roberson } 532155b9987SJeff Roberson 533ae7a6b38SJeff Roberson /* 534ae7a6b38SJeff Roberson * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load 535ae7a6b38SJeff Roberson * for this thread to the referenced thread queue. 536ae7a6b38SJeff Roberson */ 537a8949de2SJeff Roberson static void 5389727e637SJeff Roberson tdq_load_add(struct tdq *tdq, struct thread *td) 5395d7ef00cSJeff Roberson { 540ae7a6b38SJeff Roberson 541ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 54261a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 54303d17db7SJeff Roberson 544d2ad694cSJeff Roberson tdq->tdq_load++; 5451b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 546d2ad694cSJeff Roberson tdq->tdq_sysload++; 5478f51ad55SJeff Roberson KTR_COUNTER0(KTR_SCHED, "load", tdq->tdq_loadname, tdq->tdq_load); 548d9fae5abSAndriy Gapon SDT_PROBE2(sched, , , load__change, (int)TDQ_ID(tdq), tdq->tdq_load); 5495d7ef00cSJeff Roberson } 55015dc847eSJeff Roberson 551ae7a6b38SJeff Roberson /* 552ae7a6b38SJeff Roberson * Remove the load from a thread that is transitioning to a sleep state or 553ae7a6b38SJeff Roberson * exiting. 554ae7a6b38SJeff Roberson */ 555a8949de2SJeff Roberson static void 5569727e637SJeff Roberson tdq_load_rem(struct tdq *tdq, struct thread *td) 5575d7ef00cSJeff Roberson { 558ae7a6b38SJeff Roberson 559ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 56061a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 561ae7a6b38SJeff Roberson KASSERT(tdq->tdq_load != 0, 562c47f202bSJeff Roberson ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq))); 56303d17db7SJeff Roberson 564d2ad694cSJeff Roberson tdq->tdq_load--; 5651b9d701fSAttilio Rao if ((td->td_flags & TDF_NOLOAD) == 0) 56603d17db7SJeff 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); 56915dc847eSJeff Roberson } 57015dc847eSJeff Roberson 571356500a3SJeff Roberson /* 5725e5c3873SJeff Roberson * Bound timeshare latency by decreasing slice size as load increases. We 5735e5c3873SJeff Roberson * consider the maximum latency as the sum of the threads waiting to run 5745e5c3873SJeff Roberson * aside from curthread and target no more than sched_slice latency but 5755e5c3873SJeff Roberson * no less than sched_slice_min runtime. 5765e5c3873SJeff Roberson */ 5775e5c3873SJeff Roberson static inline int 5785e5c3873SJeff Roberson tdq_slice(struct tdq *tdq) 5795e5c3873SJeff Roberson { 5805e5c3873SJeff Roberson int load; 5815e5c3873SJeff Roberson 5825e5c3873SJeff Roberson /* 5835e5c3873SJeff Roberson * It is safe to use sys_load here because this is called from 5845e5c3873SJeff Roberson * contexts where timeshare threads are running and so there 5855e5c3873SJeff Roberson * cannot be higher priority load in the system. 5865e5c3873SJeff Roberson */ 5875e5c3873SJeff Roberson load = tdq->tdq_sysload - 1; 5885e5c3873SJeff Roberson if (load >= SCHED_SLICE_MIN_DIVISOR) 5895e5c3873SJeff Roberson return (sched_slice_min); 5905e5c3873SJeff Roberson if (load <= 1) 5915e5c3873SJeff Roberson return (sched_slice); 5925e5c3873SJeff Roberson return (sched_slice / load); 5935e5c3873SJeff Roberson } 5945e5c3873SJeff Roberson 5955e5c3873SJeff Roberson /* 59662fa74d9SJeff Roberson * Set lowpri to its exact value by searching the run-queue and 59762fa74d9SJeff Roberson * evaluating curthread. curthread may be passed as an optimization. 598356500a3SJeff Roberson */ 59922bf7d9aSJeff Roberson static void 60062fa74d9SJeff Roberson tdq_setlowpri(struct tdq *tdq, struct thread *ctd) 60162fa74d9SJeff Roberson { 60262fa74d9SJeff Roberson struct thread *td; 60362fa74d9SJeff Roberson 60462fa74d9SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 60562fa74d9SJeff Roberson if (ctd == NULL) 60662fa74d9SJeff Roberson ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread; 6079727e637SJeff Roberson td = tdq_choose(tdq); 6089727e637SJeff Roberson if (td == NULL || td->td_priority > ctd->td_priority) 60962fa74d9SJeff Roberson tdq->tdq_lowpri = ctd->td_priority; 61062fa74d9SJeff Roberson else 61162fa74d9SJeff Roberson tdq->tdq_lowpri = td->td_priority; 61262fa74d9SJeff Roberson } 61362fa74d9SJeff Roberson 61462fa74d9SJeff Roberson #ifdef SMP 6159129dd59SPedro F. Giffuni /* 6169129dd59SPedro F. Giffuni * We need some randomness. Implement a classic Linear Congruential 6179129dd59SPedro F. Giffuni * Generator X_{n+1}=(aX_n+c) mod m. These values are optimized for 6189129dd59SPedro F. Giffuni * m = 2^32, a = 69069 and c = 5. We only return the upper 16 bits 6199129dd59SPedro F. Giffuni * of the random state (in the low bits of our answer) to keep 6209129dd59SPedro F. Giffuni * the maximum randomness. 6219129dd59SPedro F. Giffuni */ 6229129dd59SPedro F. Giffuni static uint32_t 6239129dd59SPedro F. Giffuni sched_random(void) 6249129dd59SPedro F. Giffuni { 6259129dd59SPedro F. Giffuni uint32_t *rndptr; 6269129dd59SPedro F. Giffuni 6279129dd59SPedro F. Giffuni rndptr = DPCPU_PTR(randomval); 6289129dd59SPedro F. Giffuni *rndptr = *rndptr * 69069 + 5; 6299129dd59SPedro F. Giffuni 6309129dd59SPedro F. Giffuni return (*rndptr >> 16); 6319129dd59SPedro F. Giffuni } 6329129dd59SPedro F. Giffuni 63362fa74d9SJeff Roberson struct cpu_search { 634*aefe0a8cSAlexander Motin cpuset_t *cs_mask; 63536acfc65SAlexander Motin u_int cs_prefer; 63636acfc65SAlexander Motin int cs_pri; /* Min priority for low. */ 63736acfc65SAlexander Motin int cs_limit; /* Max load for low, min load for high. */ 638*aefe0a8cSAlexander Motin }; 639*aefe0a8cSAlexander Motin 640*aefe0a8cSAlexander Motin struct cpu_search_res { 64136acfc65SAlexander Motin int cs_cpu; 64236acfc65SAlexander Motin int cs_load; 64362fa74d9SJeff Roberson }; 64462fa74d9SJeff Roberson 64562fa74d9SJeff Roberson /* 646*aefe0a8cSAlexander Motin * Search the tree of cpu_groups for the lowest or highest loaded CPU. 647*aefe0a8cSAlexander Motin * These routines actually compare the load on all paths through the tree 648*aefe0a8cSAlexander Motin * and find the least loaded cpu on the least loaded path, which may differ 649*aefe0a8cSAlexander Motin * from the least loaded cpu in the system. This balances work among caches 650*aefe0a8cSAlexander Motin * and buses. 65162fa74d9SJeff Roberson */ 652*aefe0a8cSAlexander Motin static int 653*aefe0a8cSAlexander Motin cpu_search_lowest(const struct cpu_group *cg, const struct cpu_search *s, 654*aefe0a8cSAlexander Motin struct cpu_search_res *r) 65562fa74d9SJeff Roberson { 656*aefe0a8cSAlexander Motin struct cpu_search_res lr; 65736acfc65SAlexander Motin struct tdq *tdq; 658*aefe0a8cSAlexander Motin int c, bload, l, load, total; 65962fa74d9SJeff Roberson 66036acfc65SAlexander Motin total = 0; 661*aefe0a8cSAlexander Motin bload = INT_MAX; 662*aefe0a8cSAlexander Motin r->cs_cpu = -1; 66336acfc65SAlexander Motin 664*aefe0a8cSAlexander Motin /* Loop through children CPU groups if there are any. */ 665*aefe0a8cSAlexander Motin if (cg->cg_children > 0) { 666*aefe0a8cSAlexander Motin for (c = cg->cg_children - 1; c >= 0; c--) { 667*aefe0a8cSAlexander Motin load = cpu_search_lowest(&cg->cg_child[c], s, &lr); 66836acfc65SAlexander Motin total += load; 669*aefe0a8cSAlexander Motin if (lr.cs_cpu >= 0 && (load < bload || 670*aefe0a8cSAlexander Motin (load == bload && lr.cs_load < r->cs_load))) { 671*aefe0a8cSAlexander Motin bload = load; 672*aefe0a8cSAlexander Motin r->cs_cpu = lr.cs_cpu; 673*aefe0a8cSAlexander Motin r->cs_load = lr.cs_load; 67436acfc65SAlexander Motin } 67536acfc65SAlexander Motin } 67662fa74d9SJeff Roberson return (total); 67762fa74d9SJeff Roberson } 67862fa74d9SJeff Roberson 679*aefe0a8cSAlexander Motin /* Loop through children CPUs otherwise. */ 680*aefe0a8cSAlexander Motin for (c = cg->cg_last; c >= cg->cg_first; c--) { 681*aefe0a8cSAlexander Motin if (!CPU_ISSET(c, &cg->cg_mask)) 682*aefe0a8cSAlexander Motin continue; 683*aefe0a8cSAlexander Motin tdq = TDQ_CPU(c); 684*aefe0a8cSAlexander Motin l = tdq->tdq_load; 685*aefe0a8cSAlexander Motin load = l * 256; 686*aefe0a8cSAlexander Motin if (c == s->cs_prefer) 687*aefe0a8cSAlexander Motin load -= 128; 688*aefe0a8cSAlexander Motin total += load; 689*aefe0a8cSAlexander Motin if (l > s->cs_limit || tdq->tdq_lowpri <= s->cs_pri || 690*aefe0a8cSAlexander Motin !CPU_ISSET(c, s->cs_mask)) 691*aefe0a8cSAlexander Motin continue; 692*aefe0a8cSAlexander Motin load -= sched_random() % 128; 693*aefe0a8cSAlexander Motin if (load < bload) { 694*aefe0a8cSAlexander Motin bload = load; 695*aefe0a8cSAlexander Motin r->cs_cpu = c; 696*aefe0a8cSAlexander Motin } 697*aefe0a8cSAlexander Motin } 698*aefe0a8cSAlexander Motin r->cs_load = bload; 699*aefe0a8cSAlexander Motin return (total); 70062fa74d9SJeff Roberson } 70162fa74d9SJeff Roberson 702*aefe0a8cSAlexander Motin static int 703*aefe0a8cSAlexander Motin cpu_search_highest(const struct cpu_group *cg, const struct cpu_search *s, 704*aefe0a8cSAlexander Motin struct cpu_search_res *r) 70562fa74d9SJeff Roberson { 706*aefe0a8cSAlexander Motin struct cpu_search_res lr; 707*aefe0a8cSAlexander Motin struct tdq *tdq; 708*aefe0a8cSAlexander Motin int c, bload, l, load, total; 709*aefe0a8cSAlexander Motin 710*aefe0a8cSAlexander Motin total = 0; 711*aefe0a8cSAlexander Motin bload = INT_MIN; 712*aefe0a8cSAlexander Motin r->cs_cpu = -1; 713*aefe0a8cSAlexander Motin 714*aefe0a8cSAlexander Motin /* Loop through children CPU groups if there are any. */ 715*aefe0a8cSAlexander Motin if (cg->cg_children > 0) { 716*aefe0a8cSAlexander Motin for (c = cg->cg_children - 1; c >= 0; c--) { 717*aefe0a8cSAlexander Motin load = cpu_search_highest(&cg->cg_child[c], s, &lr); 718*aefe0a8cSAlexander Motin total += load; 719*aefe0a8cSAlexander Motin if (lr.cs_cpu >= 0 && (load > bload || 720*aefe0a8cSAlexander Motin (load == bload && lr.cs_load > r->cs_load))) { 721*aefe0a8cSAlexander Motin bload = load; 722*aefe0a8cSAlexander Motin r->cs_cpu = lr.cs_cpu; 723*aefe0a8cSAlexander Motin r->cs_load = lr.cs_load; 724*aefe0a8cSAlexander Motin } 725*aefe0a8cSAlexander Motin } 726*aefe0a8cSAlexander Motin return (total); 72762fa74d9SJeff Roberson } 72862fa74d9SJeff Roberson 729*aefe0a8cSAlexander Motin /* Loop through children CPUs otherwise. */ 730*aefe0a8cSAlexander Motin for (c = cg->cg_last; c >= cg->cg_first; c--) { 731*aefe0a8cSAlexander Motin if (!CPU_ISSET(c, &cg->cg_mask)) 732*aefe0a8cSAlexander Motin continue; 733*aefe0a8cSAlexander Motin tdq = TDQ_CPU(c); 734*aefe0a8cSAlexander Motin l = tdq->tdq_load; 735*aefe0a8cSAlexander Motin load = l * 256; 736*aefe0a8cSAlexander Motin total += load; 737*aefe0a8cSAlexander Motin if (l < s->cs_limit || !tdq->tdq_transferable || 738*aefe0a8cSAlexander Motin !CPU_ISSET(c, s->cs_mask)) 739*aefe0a8cSAlexander Motin continue; 740*aefe0a8cSAlexander Motin load -= sched_random() % 128; 741*aefe0a8cSAlexander Motin if (load > bload) { 742*aefe0a8cSAlexander Motin bload = load; 743*aefe0a8cSAlexander Motin r->cs_cpu = c; 744*aefe0a8cSAlexander Motin } 745*aefe0a8cSAlexander Motin } 746*aefe0a8cSAlexander Motin r->cs_load = bload; 747*aefe0a8cSAlexander Motin return (total); 74862fa74d9SJeff Roberson } 74962fa74d9SJeff Roberson 75062fa74d9SJeff Roberson /* 75162fa74d9SJeff Roberson * Find the cpu with the least load via the least loaded path that has a 75262fa74d9SJeff Roberson * lowpri greater than pri pri. A pri of -1 indicates any priority is 75362fa74d9SJeff Roberson * acceptable. 75462fa74d9SJeff Roberson */ 75562fa74d9SJeff Roberson static inline int 756*aefe0a8cSAlexander Motin sched_lowest(const struct cpu_group *cg, cpuset_t *mask, int pri, int maxload, 75736acfc65SAlexander Motin int prefer) 75862fa74d9SJeff Roberson { 759*aefe0a8cSAlexander Motin struct cpu_search s; 760*aefe0a8cSAlexander Motin struct cpu_search_res r; 76162fa74d9SJeff Roberson 762*aefe0a8cSAlexander Motin s.cs_prefer = prefer; 763*aefe0a8cSAlexander Motin s.cs_mask = mask; 764*aefe0a8cSAlexander Motin s.cs_pri = pri; 765*aefe0a8cSAlexander Motin s.cs_limit = maxload; 766*aefe0a8cSAlexander Motin cpu_search_lowest(cg, &s, &r); 767*aefe0a8cSAlexander Motin return (r.cs_cpu); 76862fa74d9SJeff Roberson } 76962fa74d9SJeff Roberson 77062fa74d9SJeff Roberson /* 77162fa74d9SJeff Roberson * Find the cpu with the highest load via the highest loaded path. 77262fa74d9SJeff Roberson */ 77362fa74d9SJeff Roberson static inline int 774*aefe0a8cSAlexander Motin sched_highest(const struct cpu_group *cg, cpuset_t *mask, int minload) 77562fa74d9SJeff Roberson { 776*aefe0a8cSAlexander Motin struct cpu_search s; 777*aefe0a8cSAlexander Motin struct cpu_search_res r; 77862fa74d9SJeff Roberson 779*aefe0a8cSAlexander Motin s.cs_mask = mask; 780*aefe0a8cSAlexander Motin s.cs_limit = minload; 781*aefe0a8cSAlexander Motin cpu_search_highest(cg, &s, &r); 782*aefe0a8cSAlexander Motin return (r.cs_cpu); 78362fa74d9SJeff Roberson } 78462fa74d9SJeff Roberson 78562fa74d9SJeff Roberson static void 78662fa74d9SJeff Roberson sched_balance_group(struct cpu_group *cg) 78762fa74d9SJeff Roberson { 788018ff686SJeff Roberson struct tdq *tdq; 78936acfc65SAlexander Motin cpuset_t hmask, lmask; 79036acfc65SAlexander Motin int high, low, anylow; 79162fa74d9SJeff Roberson 79236acfc65SAlexander Motin CPU_FILL(&hmask); 79362fa74d9SJeff Roberson for (;;) { 794*aefe0a8cSAlexander Motin high = sched_highest(cg, &hmask, 2); 79536acfc65SAlexander Motin /* Stop if there is no more CPU with transferrable threads. */ 79636acfc65SAlexander Motin if (high == -1) 79762fa74d9SJeff Roberson break; 79836acfc65SAlexander Motin CPU_CLR(high, &hmask); 79936acfc65SAlexander Motin CPU_COPY(&hmask, &lmask); 80036acfc65SAlexander Motin /* Stop if there is no more CPU left for low. */ 80136acfc65SAlexander Motin if (CPU_EMPTY(&lmask)) 80262fa74d9SJeff Roberson break; 80336acfc65SAlexander Motin anylow = 1; 804018ff686SJeff Roberson tdq = TDQ_CPU(high); 80536acfc65SAlexander Motin nextlow: 806*aefe0a8cSAlexander Motin low = sched_lowest(cg, &lmask, -1, tdq->tdq_load - 1, high); 80736acfc65SAlexander Motin /* Stop if we looked well and found no less loaded CPU. */ 80836acfc65SAlexander Motin if (anylow && low == -1) 80936acfc65SAlexander Motin break; 81036acfc65SAlexander Motin /* Go to next high if we found no less loaded CPU. */ 81136acfc65SAlexander Motin if (low == -1) 81236acfc65SAlexander Motin continue; 81336acfc65SAlexander Motin /* Transfer thread from high to low. */ 814018ff686SJeff Roberson if (sched_balance_pair(tdq, TDQ_CPU(low))) { 81536acfc65SAlexander Motin /* CPU that got thread can no longer be a donor. */ 81636acfc65SAlexander Motin CPU_CLR(low, &hmask); 81736acfc65SAlexander Motin } else { 81862fa74d9SJeff Roberson /* 81936acfc65SAlexander Motin * If failed, then there is no threads on high 82036acfc65SAlexander Motin * that can run on this low. Drop low from low 82136acfc65SAlexander Motin * mask and look for different one. 82262fa74d9SJeff Roberson */ 82336acfc65SAlexander Motin CPU_CLR(low, &lmask); 82436acfc65SAlexander Motin anylow = 0; 82536acfc65SAlexander Motin goto nextlow; 82662fa74d9SJeff Roberson } 82736acfc65SAlexander Motin } 82862fa74d9SJeff Roberson } 82962fa74d9SJeff Roberson 83062fa74d9SJeff Roberson static void 83162375ca8SEd Schouten sched_balance(void) 832356500a3SJeff Roberson { 8337fcf154aSJeff Roberson struct tdq *tdq; 834356500a3SJeff Roberson 8350567b6ccSWarner Losh balance_ticks = max(balance_interval / 2, 1) + 836b250ad34SWarner Losh (sched_random() % balance_interval); 8377fcf154aSJeff Roberson tdq = TDQ_SELF(); 8387fcf154aSJeff Roberson TDQ_UNLOCK(tdq); 83962fa74d9SJeff Roberson sched_balance_group(cpu_top); 8407fcf154aSJeff Roberson TDQ_LOCK(tdq); 841cac77d04SJeff Roberson } 84286f8ae96SJeff Roberson 843ae7a6b38SJeff Roberson /* 844ae7a6b38SJeff Roberson * Lock two thread queues using their address to maintain lock order. 845ae7a6b38SJeff Roberson */ 846ae7a6b38SJeff Roberson static void 847ae7a6b38SJeff Roberson tdq_lock_pair(struct tdq *one, struct tdq *two) 848ae7a6b38SJeff Roberson { 849ae7a6b38SJeff Roberson if (one < two) { 850ae7a6b38SJeff Roberson TDQ_LOCK(one); 851ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(two, MTX_DUPOK); 852ae7a6b38SJeff Roberson } else { 853ae7a6b38SJeff Roberson TDQ_LOCK(two); 854ae7a6b38SJeff Roberson TDQ_LOCK_FLAGS(one, MTX_DUPOK); 855ae7a6b38SJeff Roberson } 856ae7a6b38SJeff Roberson } 857ae7a6b38SJeff Roberson 858ae7a6b38SJeff Roberson /* 8597fcf154aSJeff Roberson * Unlock two thread queues. Order is not important here. 8607fcf154aSJeff Roberson */ 8617fcf154aSJeff Roberson static void 8627fcf154aSJeff Roberson tdq_unlock_pair(struct tdq *one, struct tdq *two) 8637fcf154aSJeff Roberson { 8647fcf154aSJeff Roberson TDQ_UNLOCK(one); 8657fcf154aSJeff Roberson TDQ_UNLOCK(two); 8667fcf154aSJeff Roberson } 8677fcf154aSJeff Roberson 8687fcf154aSJeff Roberson /* 869ae7a6b38SJeff Roberson * Transfer load between two imbalanced thread queues. 870ae7a6b38SJeff Roberson */ 87162fa74d9SJeff Roberson static int 872ad1e7d28SJulian Elischer sched_balance_pair(struct tdq *high, struct tdq *low) 873cac77d04SJeff Roberson { 87497e9382dSDon Lewis struct thread *td; 875880bf8b9SMarius Strobl int cpu; 876cac77d04SJeff Roberson 877ae7a6b38SJeff Roberson tdq_lock_pair(high, low); 87897e9382dSDon Lewis td = NULL; 879155b9987SJeff Roberson /* 88097e9382dSDon Lewis * Transfer a thread from high to low. 881155b9987SJeff Roberson */ 88236acfc65SAlexander Motin if (high->tdq_transferable != 0 && high->tdq_load > low->tdq_load && 88397e9382dSDon Lewis (td = tdq_move(high, low)) != NULL) { 884a5423ea3SJeff Roberson /* 88597e9382dSDon Lewis * In case the target isn't the current cpu notify it of the 88697e9382dSDon Lewis * new load, possibly sending an IPI to force it to reschedule. 887a5423ea3SJeff Roberson */ 888880bf8b9SMarius Strobl cpu = TDQ_ID(low); 889880bf8b9SMarius Strobl if (cpu != PCPU_GET(cpuid)) 89097e9382dSDon Lewis tdq_notify(low, td); 891ae7a6b38SJeff Roberson } 8927fcf154aSJeff Roberson tdq_unlock_pair(high, low); 89397e9382dSDon Lewis return (td != NULL); 894356500a3SJeff Roberson } 895356500a3SJeff Roberson 896ae7a6b38SJeff Roberson /* 897ae7a6b38SJeff Roberson * Move a thread from one thread queue to another. 898ae7a6b38SJeff Roberson */ 89997e9382dSDon Lewis static struct thread * 900ae7a6b38SJeff Roberson tdq_move(struct tdq *from, struct tdq *to) 901356500a3SJeff Roberson { 902ae7a6b38SJeff Roberson struct thread *td; 903ae7a6b38SJeff Roberson struct tdq *tdq; 904ae7a6b38SJeff Roberson int cpu; 905356500a3SJeff Roberson 9067fcf154aSJeff Roberson TDQ_LOCK_ASSERT(from, MA_OWNED); 9077fcf154aSJeff Roberson TDQ_LOCK_ASSERT(to, MA_OWNED); 9087fcf154aSJeff Roberson 909ad1e7d28SJulian Elischer tdq = from; 910ae7a6b38SJeff Roberson cpu = TDQ_ID(to); 9119727e637SJeff Roberson td = tdq_steal(tdq, cpu); 9129727e637SJeff Roberson if (td == NULL) 91397e9382dSDon Lewis return (NULL); 91461a74c5cSJeff Roberson 915ae7a6b38SJeff Roberson /* 91661a74c5cSJeff Roberson * Although the run queue is locked the thread may be 91761a74c5cSJeff Roberson * blocked. We can not set the lock until it is unblocked. 918ae7a6b38SJeff Roberson */ 91961a74c5cSJeff Roberson thread_lock_block_wait(td); 920ae7a6b38SJeff Roberson sched_rem(td); 92161a74c5cSJeff Roberson THREAD_LOCKPTR_ASSERT(td, TDQ_LOCKPTR(from)); 922ae7a6b38SJeff Roberson td->td_lock = TDQ_LOCKPTR(to); 92361a74c5cSJeff Roberson td_get_sched(td)->ts_cpu = cpu; 924ae7a6b38SJeff Roberson tdq_add(to, td, SRQ_YIELDING); 92561a74c5cSJeff Roberson 92697e9382dSDon Lewis return (td); 927356500a3SJeff Roberson } 92822bf7d9aSJeff Roberson 929ae7a6b38SJeff Roberson /* 930ae7a6b38SJeff Roberson * This tdq has idled. Try to steal a thread from another cpu and switch 931ae7a6b38SJeff Roberson * to it. 932ae7a6b38SJeff Roberson */ 93380f86c9fSJeff Roberson static int 934ad1e7d28SJulian Elischer tdq_idled(struct tdq *tdq) 93522bf7d9aSJeff Roberson { 93662fa74d9SJeff Roberson struct cpu_group *cg; 937ad1e7d28SJulian Elischer struct tdq *steal; 938c76ee827SJeff Roberson cpuset_t mask; 93997e9382dSDon Lewis int cpu, switchcnt; 94080f86c9fSJeff Roberson 94197e9382dSDon Lewis if (smp_started == 0 || steal_idle == 0 || tdq->tdq_cg == NULL) 94288f530ccSJeff Roberson return (1); 943c76ee827SJeff Roberson CPU_FILL(&mask); 944c76ee827SJeff Roberson CPU_CLR(PCPU_GET(cpuid), &mask); 94597e9382dSDon Lewis restart: 94697e9382dSDon Lewis switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 94797e9382dSDon Lewis for (cg = tdq->tdq_cg; ; ) { 948*aefe0a8cSAlexander Motin cpu = sched_highest(cg, &mask, steal_thresh); 94997e9382dSDon Lewis /* 95097e9382dSDon Lewis * We were assigned a thread but not preempted. Returning 95197e9382dSDon Lewis * 0 here will cause our caller to switch to it. 95297e9382dSDon Lewis */ 95397e9382dSDon Lewis if (tdq->tdq_load) 95497e9382dSDon Lewis return (0); 95562fa74d9SJeff Roberson if (cpu == -1) { 95662fa74d9SJeff Roberson cg = cg->cg_parent; 95797e9382dSDon Lewis if (cg == NULL) 95897e9382dSDon Lewis return (1); 95980f86c9fSJeff Roberson continue; 9607b8bfa0dSJeff Roberson } 9617b8bfa0dSJeff Roberson steal = TDQ_CPU(cpu); 96297e9382dSDon Lewis /* 96397e9382dSDon Lewis * The data returned by sched_highest() is stale and 96497e9382dSDon Lewis * the chosen CPU no longer has an eligible thread. 96597e9382dSDon Lewis * 96697e9382dSDon Lewis * Testing this ahead of tdq_lock_pair() only catches 96797e9382dSDon Lewis * this situation about 20% of the time on an 8 core 96897e9382dSDon Lewis * 16 thread Ryzen 7, but it still helps performance. 96997e9382dSDon Lewis */ 97097e9382dSDon Lewis if (steal->tdq_load < steal_thresh || 97197e9382dSDon Lewis steal->tdq_transferable == 0) 97297e9382dSDon Lewis goto restart; 9737fcf154aSJeff Roberson tdq_lock_pair(tdq, steal); 97497e9382dSDon Lewis /* 97597e9382dSDon Lewis * We were assigned a thread while waiting for the locks. 97697e9382dSDon Lewis * Switch to it now instead of stealing a thread. 97797e9382dSDon Lewis */ 97897e9382dSDon Lewis if (tdq->tdq_load) 97997e9382dSDon Lewis break; 98097e9382dSDon Lewis /* 98197e9382dSDon Lewis * The data returned by sched_highest() is stale and 98297e9382dSDon Lewis * the chosen CPU no longer has an eligible thread, or 98397e9382dSDon Lewis * we were preempted and the CPU loading info may be out 98497e9382dSDon Lewis * of date. The latter is rare. In either case restart 98597e9382dSDon Lewis * the search. 98697e9382dSDon Lewis */ 98797e9382dSDon Lewis if (steal->tdq_load < steal_thresh || 98897e9382dSDon Lewis steal->tdq_transferable == 0 || 98997e9382dSDon Lewis switchcnt != tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt) { 9907fcf154aSJeff Roberson tdq_unlock_pair(tdq, steal); 99197e9382dSDon Lewis goto restart; 99262fa74d9SJeff Roberson } 99362fa74d9SJeff Roberson /* 99497e9382dSDon Lewis * Steal the thread and switch to it. 99562fa74d9SJeff Roberson */ 99697e9382dSDon Lewis if (tdq_move(steal, tdq) != NULL) 99797e9382dSDon Lewis break; 99897e9382dSDon Lewis /* 99997e9382dSDon Lewis * We failed to acquire a thread even though it looked 100097e9382dSDon Lewis * like one was available. This could be due to affinity 100197e9382dSDon Lewis * restrictions or for other reasons. Loop again after 100297e9382dSDon Lewis * removing this CPU from the set. The restart logic 100397e9382dSDon Lewis * above does not restore this CPU to the set due to the 100497e9382dSDon Lewis * likelyhood of failing here again. 100597e9382dSDon Lewis */ 100697e9382dSDon Lewis CPU_CLR(cpu, &mask); 100762fa74d9SJeff Roberson tdq_unlock_pair(tdq, steal); 100880f86c9fSJeff Roberson } 1009ae7a6b38SJeff Roberson TDQ_UNLOCK(steal); 1010686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IDLE); 10117b8bfa0dSJeff Roberson return (0); 101222bf7d9aSJeff Roberson } 101322bf7d9aSJeff Roberson 1014ae7a6b38SJeff Roberson /* 1015ae7a6b38SJeff Roberson * Notify a remote cpu of new work. Sends an IPI if criteria are met. 1016ae7a6b38SJeff Roberson */ 101722bf7d9aSJeff Roberson static void 101827ee18adSRyan Stone tdq_notify(struct tdq *tdq, struct thread *td) 101922bf7d9aSJeff Roberson { 102002f0ff6dSJohn Baldwin struct thread *ctd; 102127ee18adSRyan Stone int pri; 10227b8bfa0dSJeff Roberson int cpu; 102322bf7d9aSJeff Roberson 10247789ab32SMark Johnston if (tdq->tdq_owepreempt) 1025ff256d9cSJeff Roberson return; 102627ee18adSRyan Stone cpu = td_get_sched(td)->ts_cpu; 102727ee18adSRyan Stone pri = td->td_priority; 102802f0ff6dSJohn Baldwin ctd = pcpu_find(cpu)->pc_curthread; 102902f0ff6dSJohn Baldwin if (!sched_shouldpreempt(pri, ctd->td_priority, 1)) 10306b2f763fSJeff Roberson return; 103179654969SAlexander Motin 103279654969SAlexander Motin /* 1033ae9e9b4fSAlexander Motin * Make sure that our caller's earlier update to tdq_load is 1034ae9e9b4fSAlexander Motin * globally visible before we read tdq_cpu_idle. Idle thread 103579654969SAlexander Motin * accesses both of them without locks, and the order is important. 103679654969SAlexander Motin */ 1037e8677f38SKonstantin Belousov atomic_thread_fence_seq_cst(); 103879654969SAlexander Motin 103902f0ff6dSJohn Baldwin if (TD_IS_IDLETHREAD(ctd)) { 10401690c6c1SJeff Roberson /* 10416c47aaaeSJeff Roberson * If the MD code has an idle wakeup routine try that before 10426c47aaaeSJeff Roberson * falling back to IPI. 10436c47aaaeSJeff Roberson */ 10449f9ad565SAlexander Motin if (!tdq->tdq_cpu_idle || cpu_idle_wakeup(cpu)) 10456c47aaaeSJeff Roberson return; 10461690c6c1SJeff Roberson } 10477789ab32SMark Johnston 10487789ab32SMark Johnston /* 10497789ab32SMark Johnston * The run queues have been updated, so any switch on the remote CPU 10507789ab32SMark Johnston * will satisfy the preemption request. 10517789ab32SMark Johnston */ 10527789ab32SMark Johnston tdq->tdq_owepreempt = 1; 1053d9d8d144SJohn Baldwin ipi_cpu(cpu, IPI_PREEMPT); 105422bf7d9aSJeff Roberson } 105522bf7d9aSJeff Roberson 1056ae7a6b38SJeff Roberson /* 1057ae7a6b38SJeff Roberson * Steals load from a timeshare queue. Honors the rotating queue head 1058ae7a6b38SJeff Roberson * index. 1059ae7a6b38SJeff Roberson */ 10609727e637SJeff Roberson static struct thread * 106162fa74d9SJeff Roberson runq_steal_from(struct runq *rq, int cpu, u_char start) 1062ae7a6b38SJeff Roberson { 1063ae7a6b38SJeff Roberson struct rqbits *rqb; 1064ae7a6b38SJeff Roberson struct rqhead *rqh; 106536acfc65SAlexander Motin struct thread *td, *first; 1066ae7a6b38SJeff Roberson int bit; 1067ae7a6b38SJeff Roberson int i; 1068ae7a6b38SJeff Roberson 1069ae7a6b38SJeff Roberson rqb = &rq->rq_status; 1070ae7a6b38SJeff Roberson bit = start & (RQB_BPW -1); 107136acfc65SAlexander Motin first = NULL; 1072ae7a6b38SJeff Roberson again: 1073ae7a6b38SJeff Roberson for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) { 1074ae7a6b38SJeff Roberson if (rqb->rqb_bits[i] == 0) 1075ae7a6b38SJeff Roberson continue; 10768bc713f6SJeff Roberson if (bit == 0) 10778bc713f6SJeff Roberson bit = RQB_FFS(rqb->rqb_bits[i]); 10788bc713f6SJeff Roberson for (; bit < RQB_BPW; bit++) { 10798bc713f6SJeff Roberson if ((rqb->rqb_bits[i] & (1ul << bit)) == 0) 1080ae7a6b38SJeff Roberson continue; 10818bc713f6SJeff Roberson rqh = &rq->rq_queues[bit + (i << RQB_L2BPW)]; 10829727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) { 10839727e637SJeff Roberson if (first && THREAD_CAN_MIGRATE(td) && 10849727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 10859727e637SJeff Roberson return (td); 108636acfc65SAlexander Motin first = td; 1087ae7a6b38SJeff Roberson } 1088ae7a6b38SJeff Roberson } 10898bc713f6SJeff Roberson } 1090ae7a6b38SJeff Roberson if (start != 0) { 1091ae7a6b38SJeff Roberson start = 0; 1092ae7a6b38SJeff Roberson goto again; 1093ae7a6b38SJeff Roberson } 1094ae7a6b38SJeff Roberson 109536acfc65SAlexander Motin if (first && THREAD_CAN_MIGRATE(first) && 109636acfc65SAlexander Motin THREAD_CAN_SCHED(first, cpu)) 109736acfc65SAlexander Motin return (first); 1098ae7a6b38SJeff Roberson return (NULL); 1099ae7a6b38SJeff Roberson } 1100ae7a6b38SJeff Roberson 1101ae7a6b38SJeff Roberson /* 1102ae7a6b38SJeff Roberson * Steals load from a standard linear queue. 1103ae7a6b38SJeff Roberson */ 11049727e637SJeff Roberson static struct thread * 110562fa74d9SJeff Roberson runq_steal(struct runq *rq, int cpu) 110622bf7d9aSJeff Roberson { 110722bf7d9aSJeff Roberson struct rqhead *rqh; 110822bf7d9aSJeff Roberson struct rqbits *rqb; 11099727e637SJeff Roberson struct thread *td; 111022bf7d9aSJeff Roberson int word; 111122bf7d9aSJeff Roberson int bit; 111222bf7d9aSJeff Roberson 111322bf7d9aSJeff Roberson rqb = &rq->rq_status; 111422bf7d9aSJeff Roberson for (word = 0; word < RQB_LEN; word++) { 111522bf7d9aSJeff Roberson if (rqb->rqb_bits[word] == 0) 111622bf7d9aSJeff Roberson continue; 111722bf7d9aSJeff Roberson for (bit = 0; bit < RQB_BPW; bit++) { 1118a2640c9bSPeter Wemm if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 111922bf7d9aSJeff Roberson continue; 112022bf7d9aSJeff Roberson rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 11219727e637SJeff Roberson TAILQ_FOREACH(td, rqh, td_runq) 11229727e637SJeff Roberson if (THREAD_CAN_MIGRATE(td) && 11239727e637SJeff Roberson THREAD_CAN_SCHED(td, cpu)) 11249727e637SJeff Roberson return (td); 112522bf7d9aSJeff Roberson } 112622bf7d9aSJeff Roberson } 112722bf7d9aSJeff Roberson return (NULL); 112822bf7d9aSJeff Roberson } 112922bf7d9aSJeff Roberson 1130ae7a6b38SJeff Roberson /* 1131ae7a6b38SJeff Roberson * Attempt to steal a thread in priority order from a thread queue. 1132ae7a6b38SJeff Roberson */ 11339727e637SJeff Roberson static struct thread * 113462fa74d9SJeff Roberson tdq_steal(struct tdq *tdq, int cpu) 113522bf7d9aSJeff Roberson { 11369727e637SJeff Roberson struct thread *td; 113722bf7d9aSJeff Roberson 1138ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 11399727e637SJeff Roberson if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL) 11409727e637SJeff Roberson return (td); 11419727e637SJeff Roberson if ((td = runq_steal_from(&tdq->tdq_timeshare, 11429727e637SJeff Roberson cpu, tdq->tdq_ridx)) != NULL) 11439727e637SJeff Roberson return (td); 114462fa74d9SJeff Roberson return (runq_steal(&tdq->tdq_idle, cpu)); 114522bf7d9aSJeff Roberson } 114680f86c9fSJeff Roberson 1147ae7a6b38SJeff Roberson /* 1148ae7a6b38SJeff Roberson * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the 11497fcf154aSJeff Roberson * current lock and returns with the assigned queue locked. 1150ae7a6b38SJeff Roberson */ 1151ae7a6b38SJeff Roberson static inline struct tdq * 11529727e637SJeff Roberson sched_setcpu(struct thread *td, int cpu, int flags) 115380f86c9fSJeff Roberson { 11549727e637SJeff Roberson 1155ae7a6b38SJeff Roberson struct tdq *tdq; 115661a74c5cSJeff Roberson struct mtx *mtx; 115780f86c9fSJeff Roberson 11589727e637SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1159ae7a6b38SJeff Roberson tdq = TDQ_CPU(cpu); 116093ccd6bfSKonstantin Belousov td_get_sched(td)->ts_cpu = cpu; 11619727e637SJeff Roberson /* 11629727e637SJeff Roberson * If the lock matches just return the queue. 11639727e637SJeff Roberson */ 116461a74c5cSJeff Roberson if (td->td_lock == TDQ_LOCKPTR(tdq)) { 116561a74c5cSJeff Roberson KASSERT((flags & SRQ_HOLD) == 0, 116661a74c5cSJeff Roberson ("sched_setcpu: Invalid lock for SRQ_HOLD")); 1167ae7a6b38SJeff Roberson return (tdq); 1168ae7a6b38SJeff Roberson } 116961a74c5cSJeff Roberson 117080f86c9fSJeff Roberson /* 1171ae7a6b38SJeff Roberson * The hard case, migration, we need to block the thread first to 1172ae7a6b38SJeff Roberson * prevent order reversals with other cpus locks. 11737b8bfa0dSJeff Roberson */ 1174b0b9dee5SAttilio Rao spinlock_enter(); 117561a74c5cSJeff Roberson mtx = thread_lock_block(td); 117661a74c5cSJeff Roberson if ((flags & SRQ_HOLD) == 0) 117761a74c5cSJeff Roberson mtx_unlock_spin(mtx); 1178ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1179ae7a6b38SJeff Roberson thread_lock_unblock(td, TDQ_LOCKPTR(tdq)); 1180b0b9dee5SAttilio Rao spinlock_exit(); 1181ae7a6b38SJeff Roberson return (tdq); 118280f86c9fSJeff Roberson } 11832454aaf5SJeff Roberson 11848df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_intrbind, "Soft interrupt binding"); 11858df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_idle_affinity, "Picked idle cpu based on affinity"); 11868df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_affinity, "Picked cpu based on affinity"); 11878df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_lowest, "Selected lowest load"); 11888df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_local, "Migrated to current cpu"); 11898df78c41SJeff Roberson SCHED_STAT_DEFINE(pickcpu_migration, "Selection may have caused migration"); 11908df78c41SJeff Roberson 1191ae7a6b38SJeff Roberson static int 11929727e637SJeff Roberson sched_pickcpu(struct thread *td, int flags) 1193ae7a6b38SJeff Roberson { 119436acfc65SAlexander Motin struct cpu_group *cg, *ccg; 11959727e637SJeff Roberson struct td_sched *ts; 1196ae7a6b38SJeff Roberson struct tdq *tdq; 1197*aefe0a8cSAlexander Motin cpuset_t *mask; 1198c9205e35SAlexander Motin int cpu, pri, self, intr; 11997b8bfa0dSJeff Roberson 120062fa74d9SJeff Roberson self = PCPU_GET(cpuid); 120193ccd6bfSKonstantin Belousov ts = td_get_sched(td); 1202efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(ts->ts_cpu), ("sched_pickcpu: Start scheduler on " 1203efe67753SNathan Whitehorn "absent CPU %d for thread %s.", ts->ts_cpu, td->td_name)); 12047b8bfa0dSJeff Roberson if (smp_started == 0) 12057b8bfa0dSJeff Roberson return (self); 120628994a58SJeff Roberson /* 120728994a58SJeff Roberson * Don't migrate a running thread from sched_switch(). 120828994a58SJeff Roberson */ 120962fa74d9SJeff Roberson if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td)) 121062fa74d9SJeff Roberson return (ts->ts_cpu); 12117b8bfa0dSJeff Roberson /* 121262fa74d9SJeff Roberson * Prefer to run interrupt threads on the processors that generate 121362fa74d9SJeff Roberson * the interrupt. 12147b8bfa0dSJeff Roberson */ 121562fa74d9SJeff Roberson if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) && 1216c9205e35SAlexander Motin curthread->td_intr_nesting_level) { 1217c55dc51cSAlexander Motin tdq = TDQ_SELF(); 1218c55dc51cSAlexander Motin if (tdq->tdq_lowpri >= PRI_MIN_IDLE) { 1219c55dc51cSAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 1220c55dc51cSAlexander Motin return (self); 1221c55dc51cSAlexander Motin } 122262fa74d9SJeff Roberson ts->ts_cpu = self; 1223c9205e35SAlexander Motin intr = 1; 1224c55dc51cSAlexander Motin cg = tdq->tdq_cg; 1225c55dc51cSAlexander Motin goto llc; 1226c55dc51cSAlexander Motin } else { 1227c9205e35SAlexander Motin intr = 0; 1228c55dc51cSAlexander Motin tdq = TDQ_CPU(ts->ts_cpu); 1229c55dc51cSAlexander Motin cg = tdq->tdq_cg; 1230c55dc51cSAlexander Motin } 12317b8bfa0dSJeff Roberson /* 123236acfc65SAlexander Motin * If the thread can run on the last cpu and the affinity has not 12330127914cSEric van Gyzen * expired and it is idle, run it there. 12347b8bfa0dSJeff Roberson */ 123536acfc65SAlexander Motin if (THREAD_CAN_SCHED(td, ts->ts_cpu) && 123636acfc65SAlexander Motin tdq->tdq_lowpri >= PRI_MIN_IDLE && 123736acfc65SAlexander Motin SCHED_AFFINITY(ts, CG_SHARE_L2)) { 1238c55dc51cSAlexander Motin if (cg->cg_flags & CG_FLAG_THREAD) { 1239176dd236SAlexander Motin /* Check all SMT threads for being idle. */ 1240*aefe0a8cSAlexander Motin for (cpu = cg->cg_first; cpu <= cg->cg_last; cpu++) { 1241176dd236SAlexander Motin if (CPU_ISSET(cpu, &cg->cg_mask) && 1242176dd236SAlexander Motin TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE) 124362fa74d9SJeff Roberson break; 1244*aefe0a8cSAlexander Motin } 1245*aefe0a8cSAlexander Motin if (cpu > cg->cg_last) { 1246176dd236SAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 1247176dd236SAlexander Motin return (ts->ts_cpu); 124836acfc65SAlexander Motin } 1249176dd236SAlexander Motin } else { 125036acfc65SAlexander Motin SCHED_STAT_INC(pickcpu_idle_affinity); 125136acfc65SAlexander Motin return (ts->ts_cpu); 125236acfc65SAlexander Motin } 125336acfc65SAlexander Motin } 1254c55dc51cSAlexander Motin llc: 125536acfc65SAlexander Motin /* 125636acfc65SAlexander Motin * Search for the last level cache CPU group in the tree. 1257c9205e35SAlexander Motin * Skip SMT, identical groups and caches with expired affinity. 1258c9205e35SAlexander Motin * Interrupt threads affinity is explicit and never expires. 125936acfc65SAlexander Motin */ 126036acfc65SAlexander Motin for (ccg = NULL; cg != NULL; cg = cg->cg_parent) { 126136acfc65SAlexander Motin if (cg->cg_flags & CG_FLAG_THREAD) 126236acfc65SAlexander Motin continue; 1263c9205e35SAlexander Motin if (cg->cg_children == 1 || cg->cg_count == 1) 1264c9205e35SAlexander Motin continue; 1265c9205e35SAlexander Motin if (cg->cg_level == CG_SHARE_NONE || 1266c9205e35SAlexander Motin (!intr && !SCHED_AFFINITY(ts, cg->cg_level))) 126736acfc65SAlexander Motin continue; 126836acfc65SAlexander Motin ccg = cg; 126936acfc65SAlexander Motin } 1270c9205e35SAlexander Motin /* Found LLC shared by all CPUs, so do a global search. */ 1271c9205e35SAlexander Motin if (ccg == cpu_top) 1272c9205e35SAlexander Motin ccg = NULL; 127362fa74d9SJeff Roberson cpu = -1; 1274*aefe0a8cSAlexander Motin mask = &td->td_cpuset->cs_mask; 1275c9205e35SAlexander Motin pri = td->td_priority; 1276c9205e35SAlexander Motin /* 1277c9205e35SAlexander Motin * Try hard to keep interrupts within found LLC. Search the LLC for 1278c9205e35SAlexander Motin * the least loaded CPU we can run now. For NUMA systems it should 1279c9205e35SAlexander Motin * be within target domain, and it also reduces scheduling overhead. 1280c9205e35SAlexander Motin */ 1281c9205e35SAlexander Motin if (ccg != NULL && intr) { 1282c9205e35SAlexander Motin cpu = sched_lowest(ccg, mask, pri, INT_MAX, ts->ts_cpu); 1283c9205e35SAlexander Motin if (cpu >= 0) 1284c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_intrbind); 1285c9205e35SAlexander Motin } else 1286c9205e35SAlexander Motin /* Search the LLC for the least loaded idle CPU we can run now. */ 1287c9205e35SAlexander Motin if (ccg != NULL) { 1288c9205e35SAlexander Motin cpu = sched_lowest(ccg, mask, max(pri, PRI_MAX_TIMESHARE), 128936acfc65SAlexander Motin INT_MAX, ts->ts_cpu); 1290c9205e35SAlexander Motin if (cpu >= 0) 1291c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_affinity); 1292c9205e35SAlexander Motin } 1293c9205e35SAlexander Motin /* Search globally for the least loaded CPU we can run now. */ 1294c9205e35SAlexander Motin if (cpu < 0) { 129536acfc65SAlexander Motin cpu = sched_lowest(cpu_top, mask, pri, INT_MAX, ts->ts_cpu); 1296c9205e35SAlexander Motin if (cpu >= 0) 1297c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_lowest); 1298c9205e35SAlexander Motin } 1299c9205e35SAlexander Motin /* Search globally for the least loaded CPU. */ 1300c9205e35SAlexander Motin if (cpu < 0) { 130136acfc65SAlexander Motin cpu = sched_lowest(cpu_top, mask, -1, INT_MAX, ts->ts_cpu); 1302c9205e35SAlexander Motin if (cpu >= 0) 1303c9205e35SAlexander Motin SCHED_STAT_INC(pickcpu_lowest); 1304c9205e35SAlexander Motin } 1305bb3dfc6aSAlexander Motin KASSERT(cpu >= 0, ("sched_pickcpu: Failed to find a cpu.")); 1306efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(cpu), ("sched_pickcpu: Picked absent CPU %d.", cpu)); 130762fa74d9SJeff Roberson /* 130862fa74d9SJeff Roberson * Compare the lowest loaded cpu to current cpu. 130962fa74d9SJeff Roberson */ 1310018ff686SJeff Roberson tdq = TDQ_CPU(cpu); 1311018ff686SJeff Roberson if (THREAD_CAN_SCHED(td, self) && TDQ_SELF()->tdq_lowpri > pri && 1312018ff686SJeff Roberson tdq->tdq_lowpri < PRI_MIN_IDLE && 1313018ff686SJeff Roberson TDQ_SELF()->tdq_load <= tdq->tdq_load + 1) { 13148df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_local); 131562fa74d9SJeff Roberson cpu = self; 1316c9205e35SAlexander Motin } 13178df78c41SJeff Roberson if (cpu != ts->ts_cpu) 13188df78c41SJeff Roberson SCHED_STAT_INC(pickcpu_migration); 1319ae7a6b38SJeff Roberson return (cpu); 132080f86c9fSJeff Roberson } 132162fa74d9SJeff Roberson #endif 132222bf7d9aSJeff Roberson 132322bf7d9aSJeff Roberson /* 132422bf7d9aSJeff Roberson * Pick the highest priority task we have and return it. 13250c0a98b2SJeff Roberson */ 13269727e637SJeff Roberson static struct thread * 1327ad1e7d28SJulian Elischer tdq_choose(struct tdq *tdq) 13285d7ef00cSJeff Roberson { 13299727e637SJeff Roberson struct thread *td; 13305d7ef00cSJeff Roberson 1331ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 13329727e637SJeff Roberson td = runq_choose(&tdq->tdq_realtime); 13339727e637SJeff Roberson if (td != NULL) 13349727e637SJeff Roberson return (td); 13359727e637SJeff Roberson td = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 13369727e637SJeff Roberson if (td != NULL) { 133712d56c0fSJohn Baldwin KASSERT(td->td_priority >= PRI_MIN_BATCH, 1338e7d50326SJeff Roberson ("tdq_choose: Invalid priority on timeshare queue %d", 13399727e637SJeff Roberson td->td_priority)); 13409727e637SJeff Roberson return (td); 134115dc847eSJeff Roberson } 13429727e637SJeff Roberson td = runq_choose(&tdq->tdq_idle); 13439727e637SJeff Roberson if (td != NULL) { 13449727e637SJeff Roberson KASSERT(td->td_priority >= PRI_MIN_IDLE, 1345e7d50326SJeff Roberson ("tdq_choose: Invalid priority on idle queue %d", 13469727e637SJeff Roberson td->td_priority)); 13479727e637SJeff Roberson return (td); 1348e7d50326SJeff Roberson } 1349e7d50326SJeff Roberson 1350e7d50326SJeff Roberson return (NULL); 1351245f3abfSJeff Roberson } 13520a016a05SJeff Roberson 1353ae7a6b38SJeff Roberson /* 1354ae7a6b38SJeff Roberson * Initialize a thread queue. 1355ae7a6b38SJeff Roberson */ 13560a016a05SJeff Roberson static void 1357018ff686SJeff Roberson tdq_setup(struct tdq *tdq, int id) 13580a016a05SJeff Roberson { 1359ae7a6b38SJeff Roberson 1360c47f202bSJeff Roberson if (bootverbose) 1361018ff686SJeff Roberson printf("ULE: setup cpu %d\n", id); 1362e7d50326SJeff Roberson runq_init(&tdq->tdq_realtime); 1363e7d50326SJeff Roberson runq_init(&tdq->tdq_timeshare); 1364d2ad694cSJeff Roberson runq_init(&tdq->tdq_idle); 1365018ff686SJeff Roberson tdq->tdq_id = id; 136662fa74d9SJeff Roberson snprintf(tdq->tdq_name, sizeof(tdq->tdq_name), 136762fa74d9SJeff Roberson "sched lock %d", (int)TDQ_ID(tdq)); 136861a74c5cSJeff Roberson mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock", MTX_SPIN); 13698f51ad55SJeff Roberson #ifdef KTR 13708f51ad55SJeff Roberson snprintf(tdq->tdq_loadname, sizeof(tdq->tdq_loadname), 13718f51ad55SJeff Roberson "CPU %d load", (int)TDQ_ID(tdq)); 13728f51ad55SJeff Roberson #endif 13730a016a05SJeff Roberson } 13740a016a05SJeff Roberson 1375c47f202bSJeff Roberson #ifdef SMP 1376c47f202bSJeff Roberson static void 1377c47f202bSJeff Roberson sched_setup_smp(void) 1378c47f202bSJeff Roberson { 1379c47f202bSJeff Roberson struct tdq *tdq; 1380c47f202bSJeff Roberson int i; 1381c47f202bSJeff Roberson 138262fa74d9SJeff Roberson cpu_top = smp_topo(); 13833aa6d94eSJohn Baldwin CPU_FOREACH(i) { 1384018ff686SJeff Roberson tdq = DPCPU_ID_PTR(i, tdq); 1385018ff686SJeff Roberson tdq_setup(tdq, i); 138662fa74d9SJeff Roberson tdq->tdq_cg = smp_topo_find(cpu_top, i); 138762fa74d9SJeff Roberson if (tdq->tdq_cg == NULL) 138862fa74d9SJeff Roberson panic("Can't find cpu group for %d\n", i); 1389c47f202bSJeff Roberson } 1390018ff686SJeff Roberson PCPU_SET(sched, DPCPU_PTR(tdq)); 139162fa74d9SJeff Roberson balance_tdq = TDQ_SELF(); 1392c47f202bSJeff Roberson } 1393c47f202bSJeff Roberson #endif 1394c47f202bSJeff Roberson 1395ae7a6b38SJeff Roberson /* 1396ae7a6b38SJeff Roberson * Setup the thread queues and initialize the topology based on MD 1397ae7a6b38SJeff Roberson * information. 1398ae7a6b38SJeff Roberson */ 139935e6168fSJeff Roberson static void 140035e6168fSJeff Roberson sched_setup(void *dummy) 140135e6168fSJeff Roberson { 1402ae7a6b38SJeff Roberson struct tdq *tdq; 1403c47f202bSJeff Roberson 14040ec896fdSJeff Roberson #ifdef SMP 1405c47f202bSJeff Roberson sched_setup_smp(); 1406749d01b0SJeff Roberson #else 1407018ff686SJeff Roberson tdq_setup(TDQ_SELF(), 0); 1408356500a3SJeff Roberson #endif 1409018ff686SJeff Roberson tdq = TDQ_SELF(); 1410ae7a6b38SJeff Roberson 1411ae7a6b38SJeff Roberson /* Add thread0's load since it's running. */ 1412ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 1413e1504695SJeff Roberson thread0.td_lock = TDQ_LOCKPTR(tdq); 14149727e637SJeff Roberson tdq_load_add(tdq, &thread0); 141562fa74d9SJeff Roberson tdq->tdq_lowpri = thread0.td_priority; 1416ae7a6b38SJeff Roberson TDQ_UNLOCK(tdq); 141735e6168fSJeff Roberson } 141835e6168fSJeff Roberson 1419ae7a6b38SJeff Roberson /* 1420579895dfSAlexander Motin * This routine determines time constants after stathz and hz are setup. 1421ae7a6b38SJeff Roberson */ 1422a1d4fe69SDavid Xu /* ARGSUSED */ 1423a1d4fe69SDavid Xu static void 1424a1d4fe69SDavid Xu sched_initticks(void *dummy) 1425a1d4fe69SDavid Xu { 1426ae7a6b38SJeff Roberson int incr; 1427ae7a6b38SJeff Roberson 1428a1d4fe69SDavid Xu realstathz = stathz ? stathz : hz; 14295e5c3873SJeff Roberson sched_slice = realstathz / SCHED_SLICE_DEFAULT_DIVISOR; 14305e5c3873SJeff Roberson sched_slice_min = sched_slice / SCHED_SLICE_MIN_DIVISOR; 143137f4e025SAlexander Motin hogticks = imax(1, (2 * hz * sched_slice + realstathz / 2) / 143237f4e025SAlexander Motin realstathz); 1433a1d4fe69SDavid Xu 1434a1d4fe69SDavid Xu /* 1435e7d50326SJeff Roberson * tickincr is shifted out by 10 to avoid rounding errors due to 14363f872f85SJeff Roberson * hz not being evenly divisible by stathz on all platforms. 1437e7d50326SJeff Roberson */ 1438ae7a6b38SJeff Roberson incr = (hz << SCHED_TICK_SHIFT) / realstathz; 1439e7d50326SJeff Roberson /* 1440e7d50326SJeff Roberson * This does not work for values of stathz that are more than 1441e7d50326SJeff Roberson * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1442a1d4fe69SDavid Xu */ 1443ae7a6b38SJeff Roberson if (incr == 0) 1444ae7a6b38SJeff Roberson incr = 1; 1445ae7a6b38SJeff Roberson tickincr = incr; 14467b8bfa0dSJeff Roberson #ifdef SMP 14479862717aSJeff Roberson /* 14487fcf154aSJeff Roberson * Set the default balance interval now that we know 14497fcf154aSJeff Roberson * what realstathz is. 14507fcf154aSJeff Roberson */ 14517fcf154aSJeff Roberson balance_interval = realstathz; 1452290d9060SDon Lewis balance_ticks = balance_interval; 14537b8bfa0dSJeff Roberson affinity = SCHED_AFFINITY_DEFAULT; 14547b8bfa0dSJeff Roberson #endif 1455b3f40a41SAlexander Motin if (sched_idlespinthresh < 0) 14562c27cb3aSAlexander Motin sched_idlespinthresh = 2 * max(10000, 6 * hz) / realstathz; 1457a1d4fe69SDavid Xu } 1458a1d4fe69SDavid Xu 145935e6168fSJeff Roberson /* 1460ae7a6b38SJeff Roberson * This is the core of the interactivity algorithm. Determines a score based 1461ae7a6b38SJeff Roberson * on past behavior. It is the ratio of sleep time to run time scaled to 1462ae7a6b38SJeff Roberson * a [0, 100] integer. This is the voluntary sleep time of a process, which 1463ae7a6b38SJeff Roberson * differs from the cpu usage because it does not account for time spent 1464ae7a6b38SJeff Roberson * waiting on a run-queue. Would be prettier if we had floating point. 146557031f79SGeorge V. Neville-Neil * 146657031f79SGeorge V. Neville-Neil * When a thread's sleep time is greater than its run time the 146757031f79SGeorge V. Neville-Neil * calculation is: 146857031f79SGeorge V. Neville-Neil * 146957031f79SGeorge V. Neville-Neil * scaling factor 147057031f79SGeorge V. Neville-Neil * interactivity score = --------------------- 147157031f79SGeorge V. Neville-Neil * sleep time / run time 147257031f79SGeorge V. Neville-Neil * 147357031f79SGeorge V. Neville-Neil * 147457031f79SGeorge V. Neville-Neil * When a thread's run time is greater than its sleep time the 147557031f79SGeorge V. Neville-Neil * calculation is: 147657031f79SGeorge V. Neville-Neil * 147757031f79SGeorge V. Neville-Neil * scaling factor 147843521b46Swiklam * interactivity score = 2 * scaling factor - --------------------- 147957031f79SGeorge V. Neville-Neil * run time / sleep time 1480ae7a6b38SJeff Roberson */ 1481ae7a6b38SJeff Roberson static int 1482ae7a6b38SJeff Roberson sched_interact_score(struct thread *td) 1483ae7a6b38SJeff Roberson { 1484ae7a6b38SJeff Roberson struct td_sched *ts; 1485ae7a6b38SJeff Roberson int div; 1486ae7a6b38SJeff Roberson 148793ccd6bfSKonstantin Belousov ts = td_get_sched(td); 1488ae7a6b38SJeff Roberson /* 1489ae7a6b38SJeff Roberson * The score is only needed if this is likely to be an interactive 1490ae7a6b38SJeff Roberson * task. Don't go through the expense of computing it if there's 1491ae7a6b38SJeff Roberson * no chance. 1492ae7a6b38SJeff Roberson */ 1493ae7a6b38SJeff Roberson if (sched_interact <= SCHED_INTERACT_HALF && 1494ae7a6b38SJeff Roberson ts->ts_runtime >= ts->ts_slptime) 1495ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1496ae7a6b38SJeff Roberson 1497ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1498ae7a6b38SJeff Roberson div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF); 1499ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF + 1500ae7a6b38SJeff Roberson (SCHED_INTERACT_HALF - (ts->ts_slptime / div))); 1501ae7a6b38SJeff Roberson } 1502ae7a6b38SJeff Roberson if (ts->ts_slptime > ts->ts_runtime) { 1503ae7a6b38SJeff Roberson div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF); 1504ae7a6b38SJeff Roberson return (ts->ts_runtime / div); 1505ae7a6b38SJeff Roberson } 1506ae7a6b38SJeff Roberson /* runtime == slptime */ 1507ae7a6b38SJeff Roberson if (ts->ts_runtime) 1508ae7a6b38SJeff Roberson return (SCHED_INTERACT_HALF); 1509ae7a6b38SJeff Roberson 1510ae7a6b38SJeff Roberson /* 1511ae7a6b38SJeff Roberson * This can happen if slptime and runtime are 0. 1512ae7a6b38SJeff Roberson */ 1513ae7a6b38SJeff Roberson return (0); 1514ae7a6b38SJeff Roberson 1515ae7a6b38SJeff Roberson } 1516ae7a6b38SJeff Roberson 1517ae7a6b38SJeff Roberson /* 151835e6168fSJeff Roberson * Scale the scheduling priority according to the "interactivity" of this 151935e6168fSJeff Roberson * process. 152035e6168fSJeff Roberson */ 152115dc847eSJeff Roberson static void 15228460a577SJohn Birrell sched_priority(struct thread *td) 152335e6168fSJeff Roberson { 1524e7d50326SJeff Roberson int score; 152535e6168fSJeff Roberson int pri; 152635e6168fSJeff Roberson 1527c9a8cba4SJohn Baldwin if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 152815dc847eSJeff Roberson return; 1529e7d50326SJeff Roberson /* 1530e7d50326SJeff Roberson * If the score is interactive we place the thread in the realtime 1531e7d50326SJeff Roberson * queue with a priority that is less than kernel and interrupt 1532e7d50326SJeff Roberson * priorities. These threads are not subject to nice restrictions. 1533e7d50326SJeff Roberson * 1534ae7a6b38SJeff Roberson * Scores greater than this are placed on the normal timeshare queue 1535e7d50326SJeff Roberson * where the priority is partially decided by the most recent cpu 1536e7d50326SJeff Roberson * utilization and the rest is decided by nice value. 1537a5423ea3SJeff Roberson * 1538a5423ea3SJeff Roberson * The nice value of the process has a linear effect on the calculated 1539a5423ea3SJeff Roberson * score. Negative nice values make it easier for a thread to be 1540a5423ea3SJeff Roberson * considered interactive. 1541e7d50326SJeff Roberson */ 1542a0f15352SJohn Baldwin score = imax(0, sched_interact_score(td) + td->td_proc->p_nice); 1543e7d50326SJeff Roberson if (score < sched_interact) { 154412d56c0fSJohn Baldwin pri = PRI_MIN_INTERACT; 154512d56c0fSJohn Baldwin pri += ((PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) / 154678920008SJohn Baldwin sched_interact) * score; 154712d56c0fSJohn Baldwin KASSERT(pri >= PRI_MIN_INTERACT && pri <= PRI_MAX_INTERACT, 15489a93305aSJeff Roberson ("sched_priority: invalid interactive priority %d score %d", 15499a93305aSJeff Roberson pri, score)); 1550e7d50326SJeff Roberson } else { 1551e7d50326SJeff Roberson pri = SCHED_PRI_MIN; 155293ccd6bfSKonstantin Belousov if (td_get_sched(td)->ts_ticks) 155393ccd6bfSKonstantin Belousov pri += min(SCHED_PRI_TICKS(td_get_sched(td)), 15545457fa23SJohn Baldwin SCHED_PRI_RANGE - 1); 1555e7d50326SJeff Roberson pri += SCHED_PRI_NICE(td->td_proc->p_nice); 155612d56c0fSJohn Baldwin KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH, 1557ae7a6b38SJeff Roberson ("sched_priority: invalid priority %d: nice %d, " 1558ae7a6b38SJeff Roberson "ticks %d ftick %d ltick %d tick pri %d", 155993ccd6bfSKonstantin Belousov pri, td->td_proc->p_nice, td_get_sched(td)->ts_ticks, 156093ccd6bfSKonstantin Belousov td_get_sched(td)->ts_ftick, td_get_sched(td)->ts_ltick, 156193ccd6bfSKonstantin Belousov SCHED_PRI_TICKS(td_get_sched(td)))); 1562e7d50326SJeff Roberson } 15638460a577SJohn Birrell sched_user_prio(td, pri); 156435e6168fSJeff Roberson 156515dc847eSJeff Roberson return; 156635e6168fSJeff Roberson } 156735e6168fSJeff Roberson 156835e6168fSJeff Roberson /* 1569d322132cSJeff Roberson * This routine enforces a maximum limit on the amount of scheduling history 1570ae7a6b38SJeff Roberson * kept. It is called after either the slptime or runtime is adjusted. This 1571ae7a6b38SJeff Roberson * function is ugly due to integer math. 1572d322132cSJeff Roberson */ 15734b60e324SJeff Roberson static void 15748460a577SJohn Birrell sched_interact_update(struct thread *td) 15754b60e324SJeff Roberson { 1576155b6ca1SJeff Roberson struct td_sched *ts; 15779a93305aSJeff Roberson u_int sum; 15783f741ca1SJeff Roberson 157993ccd6bfSKonstantin Belousov ts = td_get_sched(td); 1580ae7a6b38SJeff Roberson sum = ts->ts_runtime + ts->ts_slptime; 1581d322132cSJeff Roberson if (sum < SCHED_SLP_RUN_MAX) 1582d322132cSJeff Roberson return; 1583d322132cSJeff Roberson /* 1584155b6ca1SJeff Roberson * This only happens from two places: 1585155b6ca1SJeff Roberson * 1) We have added an unusual amount of run time from fork_exit. 1586155b6ca1SJeff Roberson * 2) We have added an unusual amount of sleep time from sched_sleep(). 1587155b6ca1SJeff Roberson */ 1588155b6ca1SJeff Roberson if (sum > SCHED_SLP_RUN_MAX * 2) { 1589ae7a6b38SJeff Roberson if (ts->ts_runtime > ts->ts_slptime) { 1590ae7a6b38SJeff Roberson ts->ts_runtime = SCHED_SLP_RUN_MAX; 1591ae7a6b38SJeff Roberson ts->ts_slptime = 1; 1592155b6ca1SJeff Roberson } else { 1593ae7a6b38SJeff Roberson ts->ts_slptime = SCHED_SLP_RUN_MAX; 1594ae7a6b38SJeff Roberson ts->ts_runtime = 1; 1595155b6ca1SJeff Roberson } 1596155b6ca1SJeff Roberson return; 1597155b6ca1SJeff Roberson } 1598155b6ca1SJeff Roberson /* 1599d322132cSJeff Roberson * If we have exceeded by more than 1/5th then the algorithm below 1600d322132cSJeff Roberson * will not bring us back into range. Dividing by two here forces 16012454aaf5SJeff Roberson * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1602d322132cSJeff Roberson */ 160337a35e4aSJeff Roberson if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1604ae7a6b38SJeff Roberson ts->ts_runtime /= 2; 1605ae7a6b38SJeff Roberson ts->ts_slptime /= 2; 1606d322132cSJeff Roberson return; 1607d322132cSJeff Roberson } 1608ae7a6b38SJeff Roberson ts->ts_runtime = (ts->ts_runtime / 5) * 4; 1609ae7a6b38SJeff Roberson ts->ts_slptime = (ts->ts_slptime / 5) * 4; 1610d322132cSJeff Roberson } 1611d322132cSJeff Roberson 1612ae7a6b38SJeff Roberson /* 1613ae7a6b38SJeff Roberson * Scale back the interactivity history when a child thread is created. The 1614ae7a6b38SJeff Roberson * history is inherited from the parent but the thread may behave totally 1615ae7a6b38SJeff Roberson * differently. For example, a shell spawning a compiler process. We want 1616ae7a6b38SJeff Roberson * to learn that the compiler is behaving badly very quickly. 1617ae7a6b38SJeff Roberson */ 1618d322132cSJeff Roberson static void 16198460a577SJohn Birrell sched_interact_fork(struct thread *td) 1620d322132cSJeff Roberson { 162193ccd6bfSKonstantin Belousov struct td_sched *ts; 1622d322132cSJeff Roberson int ratio; 1623d322132cSJeff Roberson int sum; 1624d322132cSJeff Roberson 162593ccd6bfSKonstantin Belousov ts = td_get_sched(td); 162693ccd6bfSKonstantin Belousov sum = ts->ts_runtime + ts->ts_slptime; 1627d322132cSJeff Roberson if (sum > SCHED_SLP_RUN_FORK) { 1628d322132cSJeff Roberson ratio = sum / SCHED_SLP_RUN_FORK; 162993ccd6bfSKonstantin Belousov ts->ts_runtime /= ratio; 163093ccd6bfSKonstantin Belousov ts->ts_slptime /= ratio; 16314b60e324SJeff Roberson } 16324b60e324SJeff Roberson } 16334b60e324SJeff Roberson 163415dc847eSJeff Roberson /* 1635ae7a6b38SJeff Roberson * Called from proc0_init() to setup the scheduler fields. 1636ed062c8dSJulian Elischer */ 1637ed062c8dSJulian Elischer void 1638ed062c8dSJulian Elischer schedinit(void) 1639ed062c8dSJulian Elischer { 164093ccd6bfSKonstantin Belousov struct td_sched *ts0; 1641e7d50326SJeff Roberson 1642ed062c8dSJulian Elischer /* 164393ccd6bfSKonstantin Belousov * Set up the scheduler specific parts of thread0. 1644ed062c8dSJulian Elischer */ 164593ccd6bfSKonstantin Belousov ts0 = td_get_sched(&thread0); 164693ccd6bfSKonstantin Belousov ts0->ts_ltick = ticks; 164793ccd6bfSKonstantin Belousov ts0->ts_ftick = ticks; 164893ccd6bfSKonstantin Belousov ts0->ts_slice = 0; 16491408b84aSHans Petter Selasky ts0->ts_cpu = curcpu; /* set valid CPU number */ 1650ed062c8dSJulian Elischer } 1651ed062c8dSJulian Elischer 1652ed062c8dSJulian Elischer /* 165315dc847eSJeff Roberson * This is only somewhat accurate since given many processes of the same 165415dc847eSJeff Roberson * priority they will switch when their slices run out, which will be 1655e7d50326SJeff Roberson * at most sched_slice stathz ticks. 165615dc847eSJeff Roberson */ 165735e6168fSJeff Roberson int 165835e6168fSJeff Roberson sched_rr_interval(void) 165935e6168fSJeff Roberson { 1660e7d50326SJeff Roberson 1661579895dfSAlexander Motin /* Convert sched_slice from stathz to hz. */ 166237f4e025SAlexander Motin return (imax(1, (sched_slice * hz + realstathz / 2) / realstathz)); 166335e6168fSJeff Roberson } 166435e6168fSJeff Roberson 1665ae7a6b38SJeff Roberson /* 1666ae7a6b38SJeff Roberson * Update the percent cpu tracking information when it is requested or 1667ae7a6b38SJeff Roberson * the total history exceeds the maximum. We keep a sliding history of 1668ae7a6b38SJeff Roberson * tick counts that slowly decays. This is less precise than the 4BSD 1669ae7a6b38SJeff Roberson * mechanism since it happens with less regular and frequent events. 1670ae7a6b38SJeff Roberson */ 167122bf7d9aSJeff Roberson static void 16727295465eSAlexander Motin sched_pctcpu_update(struct td_sched *ts, int run) 167335e6168fSJeff Roberson { 16747295465eSAlexander Motin int t = ticks; 1675e7d50326SJeff Roberson 167678133024SMark Johnston /* 167778133024SMark Johnston * The signed difference may be negative if the thread hasn't run for 167878133024SMark Johnston * over half of the ticks rollover period. 167978133024SMark Johnston */ 168078133024SMark Johnston if ((u_int)(t - ts->ts_ltick) >= SCHED_TICK_TARG) { 1681ad1e7d28SJulian Elischer ts->ts_ticks = 0; 16827295465eSAlexander Motin ts->ts_ftick = t - SCHED_TICK_TARG; 16837295465eSAlexander Motin } else if (t - ts->ts_ftick >= SCHED_TICK_MAX) { 16847295465eSAlexander Motin ts->ts_ticks = (ts->ts_ticks / (ts->ts_ltick - ts->ts_ftick)) * 16857295465eSAlexander Motin (ts->ts_ltick - (t - SCHED_TICK_TARG)); 16867295465eSAlexander Motin ts->ts_ftick = t - SCHED_TICK_TARG; 16877295465eSAlexander Motin } 16887295465eSAlexander Motin if (run) 16897295465eSAlexander Motin ts->ts_ticks += (t - ts->ts_ltick) << SCHED_TICK_SHIFT; 16907295465eSAlexander Motin ts->ts_ltick = t; 169135e6168fSJeff Roberson } 169235e6168fSJeff Roberson 1693ae7a6b38SJeff Roberson /* 1694ae7a6b38SJeff Roberson * Adjust the priority of a thread. Move it to the appropriate run-queue 1695ae7a6b38SJeff Roberson * if necessary. This is the back-end for several priority related 1696ae7a6b38SJeff Roberson * functions. 1697ae7a6b38SJeff Roberson */ 1698e7d50326SJeff Roberson static void 1699f5c157d9SJohn Baldwin sched_thread_priority(struct thread *td, u_char prio) 170035e6168fSJeff Roberson { 1701ad1e7d28SJulian Elischer struct td_sched *ts; 170273daf66fSJeff Roberson struct tdq *tdq; 170373daf66fSJeff Roberson int oldpri; 170435e6168fSJeff Roberson 17058f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "prio", 17068f51ad55SJeff Roberson "prio:%d", td->td_priority, "new prio:%d", prio, 17078f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(curthread)); 1708d9fae5abSAndriy Gapon SDT_PROBE3(sched, , , change__pri, td, td->td_proc, prio); 1709e87fc7cfSAndriy Gapon if (td != curthread && prio < td->td_priority) { 17108f51ad55SJeff Roberson KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread), 17118f51ad55SJeff Roberson "lend prio", "prio:%d", td->td_priority, "new prio:%d", 17128f51ad55SJeff Roberson prio, KTR_ATTR_LINKED, sched_tdname(td)); 1713d9fae5abSAndriy Gapon SDT_PROBE4(sched, , , lend__pri, td, td->td_proc, prio, 1714b3e9e682SRyan Stone curthread); 17158f51ad55SJeff Roberson } 171693ccd6bfSKonstantin Belousov ts = td_get_sched(td); 17177b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1718f5c157d9SJohn Baldwin if (td->td_priority == prio) 1719f5c157d9SJohn Baldwin return; 17203f741ca1SJeff Roberson /* 17213f741ca1SJeff Roberson * If the priority has been elevated due to priority 17223f741ca1SJeff Roberson * propagation, we may have to move ourselves to a new 1723e7d50326SJeff Roberson * queue. This could be optimized to not re-add in some 1724e7d50326SJeff Roberson * cases. 1725f2b74cbfSJeff Roberson */ 17266d55b3ecSJeff Roberson if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1727e7d50326SJeff Roberson sched_rem(td); 1728e7d50326SJeff Roberson td->td_priority = prio; 172961a74c5cSJeff Roberson sched_add(td, SRQ_BORROWING | SRQ_HOLDTD); 173073daf66fSJeff Roberson return; 173173daf66fSJeff Roberson } 17326d55b3ecSJeff Roberson /* 17336d55b3ecSJeff Roberson * If the thread is currently running we may have to adjust the lowpri 17346d55b3ecSJeff Roberson * information so other cpus are aware of our current priority. 17356d55b3ecSJeff Roberson */ 17366d55b3ecSJeff Roberson if (TD_IS_RUNNING(td)) { 1737ae7a6b38SJeff Roberson tdq = TDQ_CPU(ts->ts_cpu); 173862fa74d9SJeff Roberson oldpri = td->td_priority; 17393f741ca1SJeff Roberson td->td_priority = prio; 174062fa74d9SJeff Roberson if (prio < tdq->tdq_lowpri) 174162fa74d9SJeff Roberson tdq->tdq_lowpri = prio; 174262fa74d9SJeff Roberson else if (tdq->tdq_lowpri == oldpri) 174362fa74d9SJeff Roberson tdq_setlowpri(tdq, td); 17446d55b3ecSJeff Roberson return; 174573daf66fSJeff Roberson } 17466d55b3ecSJeff Roberson td->td_priority = prio; 1747ae7a6b38SJeff Roberson } 174835e6168fSJeff Roberson 1749f5c157d9SJohn Baldwin /* 1750f5c157d9SJohn Baldwin * Update a thread's priority when it is lent another thread's 1751f5c157d9SJohn Baldwin * priority. 1752f5c157d9SJohn Baldwin */ 1753f5c157d9SJohn Baldwin void 1754f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio) 1755f5c157d9SJohn Baldwin { 1756f5c157d9SJohn Baldwin 1757f5c157d9SJohn Baldwin td->td_flags |= TDF_BORROWING; 1758f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1759f5c157d9SJohn Baldwin } 1760f5c157d9SJohn Baldwin 1761f5c157d9SJohn Baldwin /* 1762f5c157d9SJohn Baldwin * Restore a thread's priority when priority propagation is 1763f5c157d9SJohn Baldwin * over. The prio argument is the minimum priority the thread 1764f5c157d9SJohn Baldwin * needs to have to satisfy other possible priority lending 1765f5c157d9SJohn Baldwin * requests. If the thread's regular priority is less 1766f5c157d9SJohn Baldwin * important than prio, the thread will keep a priority boost 1767f5c157d9SJohn Baldwin * of prio. 1768f5c157d9SJohn Baldwin */ 1769f5c157d9SJohn Baldwin void 1770f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio) 1771f5c157d9SJohn Baldwin { 1772f5c157d9SJohn Baldwin u_char base_pri; 1773f5c157d9SJohn Baldwin 1774f5c157d9SJohn Baldwin if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1775f5c157d9SJohn Baldwin td->td_base_pri <= PRI_MAX_TIMESHARE) 17768460a577SJohn Birrell base_pri = td->td_user_pri; 1777f5c157d9SJohn Baldwin else 1778f5c157d9SJohn Baldwin base_pri = td->td_base_pri; 1779f5c157d9SJohn Baldwin if (prio >= base_pri) { 1780f5c157d9SJohn Baldwin td->td_flags &= ~TDF_BORROWING; 1781f5c157d9SJohn Baldwin sched_thread_priority(td, base_pri); 1782f5c157d9SJohn Baldwin } else 1783f5c157d9SJohn Baldwin sched_lend_prio(td, prio); 1784f5c157d9SJohn Baldwin } 1785f5c157d9SJohn Baldwin 1786ae7a6b38SJeff Roberson /* 1787ae7a6b38SJeff Roberson * Standard entry for setting the priority to an absolute value. 1788ae7a6b38SJeff Roberson */ 1789f5c157d9SJohn Baldwin void 1790f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio) 1791f5c157d9SJohn Baldwin { 1792f5c157d9SJohn Baldwin u_char oldprio; 1793f5c157d9SJohn Baldwin 1794f5c157d9SJohn Baldwin /* First, update the base priority. */ 1795f5c157d9SJohn Baldwin td->td_base_pri = prio; 1796f5c157d9SJohn Baldwin 1797f5c157d9SJohn Baldwin /* 179850aaa791SJohn Baldwin * If the thread is borrowing another thread's priority, don't 1799f5c157d9SJohn Baldwin * ever lower the priority. 1800f5c157d9SJohn Baldwin */ 1801f5c157d9SJohn Baldwin if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1802f5c157d9SJohn Baldwin return; 1803f5c157d9SJohn Baldwin 1804f5c157d9SJohn Baldwin /* Change the real priority. */ 1805f5c157d9SJohn Baldwin oldprio = td->td_priority; 1806f5c157d9SJohn Baldwin sched_thread_priority(td, prio); 1807f5c157d9SJohn Baldwin 1808f5c157d9SJohn Baldwin /* 1809f5c157d9SJohn Baldwin * If the thread is on a turnstile, then let the turnstile update 1810f5c157d9SJohn Baldwin * its state. 1811f5c157d9SJohn Baldwin */ 1812f5c157d9SJohn Baldwin if (TD_ON_LOCK(td) && oldprio != prio) 1813f5c157d9SJohn Baldwin turnstile_adjust(td, oldprio); 1814f5c157d9SJohn Baldwin } 1815f5c157d9SJohn Baldwin 1816ae7a6b38SJeff Roberson /* 1817ae7a6b38SJeff Roberson * Set the base user priority, does not effect current running priority. 1818ae7a6b38SJeff Roberson */ 181935e6168fSJeff Roberson void 18208460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio) 18213db720fdSDavid Xu { 18223db720fdSDavid Xu 18238460a577SJohn Birrell td->td_base_user_pri = prio; 1824acbe332aSDavid Xu if (td->td_lend_user_pri <= prio) 1825fc6c30f6SJulian Elischer return; 18268460a577SJohn Birrell td->td_user_pri = prio; 18273db720fdSDavid Xu } 18283db720fdSDavid Xu 18293db720fdSDavid Xu void 18303db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio) 18313db720fdSDavid Xu { 18323db720fdSDavid Xu 1833435806d3SDavid Xu THREAD_LOCK_ASSERT(td, MA_OWNED); 1834acbe332aSDavid Xu td->td_lend_user_pri = prio; 1835c8e368a9SDavid Xu td->td_user_pri = min(prio, td->td_base_user_pri); 1836c8e368a9SDavid Xu if (td->td_priority > td->td_user_pri) 1837c8e368a9SDavid Xu sched_prio(td, td->td_user_pri); 1838c8e368a9SDavid Xu else if (td->td_priority != td->td_user_pri) 1839c8e368a9SDavid Xu td->td_flags |= TDF_NEEDRESCHED; 1840435806d3SDavid Xu } 18413db720fdSDavid Xu 1842ac97da9aSMateusz Guzik /* 1843ac97da9aSMateusz Guzik * Like the above but first check if there is anything to do. 1844ac97da9aSMateusz Guzik */ 1845ac97da9aSMateusz Guzik void 1846ac97da9aSMateusz Guzik sched_lend_user_prio_cond(struct thread *td, u_char prio) 1847ac97da9aSMateusz Guzik { 1848ac97da9aSMateusz Guzik 1849ac97da9aSMateusz Guzik if (td->td_lend_user_pri != prio) 1850ac97da9aSMateusz Guzik goto lend; 1851ac97da9aSMateusz Guzik if (td->td_user_pri != min(prio, td->td_base_user_pri)) 1852ac97da9aSMateusz Guzik goto lend; 1853b77594bbSMateusz Guzik if (td->td_priority != td->td_user_pri) 1854ac97da9aSMateusz Guzik goto lend; 1855ac97da9aSMateusz Guzik return; 1856ac97da9aSMateusz Guzik 1857ac97da9aSMateusz Guzik lend: 1858ac97da9aSMateusz Guzik thread_lock(td); 1859ac97da9aSMateusz Guzik sched_lend_user_prio(td, prio); 1860ac97da9aSMateusz Guzik thread_unlock(td); 1861ac97da9aSMateusz Guzik } 1862ac97da9aSMateusz Guzik 18634c8a8cfcSKonstantin Belousov #ifdef SMP 1864ae7a6b38SJeff Roberson /* 186597e9382dSDon Lewis * This tdq is about to idle. Try to steal a thread from another CPU before 186697e9382dSDon Lewis * choosing the idle thread. 186797e9382dSDon Lewis */ 186897e9382dSDon Lewis static void 186997e9382dSDon Lewis tdq_trysteal(struct tdq *tdq) 187097e9382dSDon Lewis { 187197e9382dSDon Lewis struct cpu_group *cg; 187297e9382dSDon Lewis struct tdq *steal; 187397e9382dSDon Lewis cpuset_t mask; 187497e9382dSDon Lewis int cpu, i; 187597e9382dSDon Lewis 187697e9382dSDon Lewis if (smp_started == 0 || trysteal_limit == 0 || tdq->tdq_cg == NULL) 187797e9382dSDon Lewis return; 187897e9382dSDon Lewis CPU_FILL(&mask); 187997e9382dSDon Lewis CPU_CLR(PCPU_GET(cpuid), &mask); 188097e9382dSDon Lewis /* We don't want to be preempted while we're iterating. */ 188197e9382dSDon Lewis spinlock_enter(); 188297e9382dSDon Lewis TDQ_UNLOCK(tdq); 188397e9382dSDon Lewis for (i = 1, cg = tdq->tdq_cg; ; ) { 1884*aefe0a8cSAlexander Motin cpu = sched_highest(cg, &mask, steal_thresh); 188597e9382dSDon Lewis /* 188697e9382dSDon Lewis * If a thread was added while interrupts were disabled don't 188797e9382dSDon Lewis * steal one here. 188897e9382dSDon Lewis */ 188997e9382dSDon Lewis if (tdq->tdq_load > 0) { 189097e9382dSDon Lewis TDQ_LOCK(tdq); 189197e9382dSDon Lewis break; 189297e9382dSDon Lewis } 189397e9382dSDon Lewis if (cpu == -1) { 189497e9382dSDon Lewis i++; 189597e9382dSDon Lewis cg = cg->cg_parent; 189697e9382dSDon Lewis if (cg == NULL || i > trysteal_limit) { 189797e9382dSDon Lewis TDQ_LOCK(tdq); 189897e9382dSDon Lewis break; 189997e9382dSDon Lewis } 190097e9382dSDon Lewis continue; 190197e9382dSDon Lewis } 190297e9382dSDon Lewis steal = TDQ_CPU(cpu); 190397e9382dSDon Lewis /* 190497e9382dSDon Lewis * The data returned by sched_highest() is stale and 190597e9382dSDon Lewis * the chosen CPU no longer has an eligible thread. 190697e9382dSDon Lewis */ 190797e9382dSDon Lewis if (steal->tdq_load < steal_thresh || 190897e9382dSDon Lewis steal->tdq_transferable == 0) 190997e9382dSDon Lewis continue; 191097e9382dSDon Lewis tdq_lock_pair(tdq, steal); 191197e9382dSDon Lewis /* 191297e9382dSDon Lewis * If we get to this point, unconditonally exit the loop 191397e9382dSDon Lewis * to bound the time spent in the critcal section. 191497e9382dSDon Lewis * 191597e9382dSDon Lewis * If a thread was added while interrupts were disabled don't 191697e9382dSDon Lewis * steal one here. 191797e9382dSDon Lewis */ 191897e9382dSDon Lewis if (tdq->tdq_load > 0) { 191997e9382dSDon Lewis TDQ_UNLOCK(steal); 192097e9382dSDon Lewis break; 192197e9382dSDon Lewis } 192297e9382dSDon Lewis /* 192397e9382dSDon Lewis * The data returned by sched_highest() is stale and 192497e9382dSDon Lewis * the chosen CPU no longer has an eligible thread. 192597e9382dSDon Lewis */ 192697e9382dSDon Lewis if (steal->tdq_load < steal_thresh || 192797e9382dSDon Lewis steal->tdq_transferable == 0) { 192897e9382dSDon Lewis TDQ_UNLOCK(steal); 192997e9382dSDon Lewis break; 193097e9382dSDon Lewis } 193197e9382dSDon Lewis /* 193297e9382dSDon Lewis * If we fail to acquire one due to affinity restrictions, 193397e9382dSDon Lewis * bail out and let the idle thread to a more complete search 193497e9382dSDon Lewis * outside of a critical section. 193597e9382dSDon Lewis */ 193697e9382dSDon Lewis if (tdq_move(steal, tdq) == NULL) { 193797e9382dSDon Lewis TDQ_UNLOCK(steal); 193897e9382dSDon Lewis break; 193997e9382dSDon Lewis } 194097e9382dSDon Lewis TDQ_UNLOCK(steal); 194197e9382dSDon Lewis break; 194297e9382dSDon Lewis } 194397e9382dSDon Lewis spinlock_exit(); 194497e9382dSDon Lewis } 19454c8a8cfcSKonstantin Belousov #endif 194697e9382dSDon Lewis 194797e9382dSDon Lewis /* 1948c47f202bSJeff Roberson * Handle migration from sched_switch(). This happens only for 1949c47f202bSJeff Roberson * cpu binding. 1950c47f202bSJeff Roberson */ 1951c47f202bSJeff Roberson static struct mtx * 1952c47f202bSJeff Roberson sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags) 1953c47f202bSJeff Roberson { 1954c47f202bSJeff Roberson struct tdq *tdn; 1955c47f202bSJeff Roberson 1956686bcb5cSJeff Roberson KASSERT(THREAD_CAN_MIGRATE(td) || 1957686bcb5cSJeff Roberson (td_get_sched(td)->ts_flags & TSF_BOUND) != 0, 1958686bcb5cSJeff Roberson ("Thread %p shouldn't migrate", td)); 1959efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(td_get_sched(td)->ts_cpu), ("sched_switch_migrate: " 1960efe67753SNathan Whitehorn "thread %s queued on absent CPU %d.", td->td_name, 1961efe67753SNathan Whitehorn td_get_sched(td)->ts_cpu)); 196293ccd6bfSKonstantin Belousov tdn = TDQ_CPU(td_get_sched(td)->ts_cpu); 1963c47f202bSJeff Roberson #ifdef SMP 19649727e637SJeff Roberson tdq_load_rem(tdq, td); 1965c47f202bSJeff Roberson /* 1966686bcb5cSJeff Roberson * Do the lock dance required to avoid LOR. We have an 1967686bcb5cSJeff Roberson * extra spinlock nesting from sched_switch() which will 1968686bcb5cSJeff Roberson * prevent preemption while we're holding neither run-queue lock. 1969c47f202bSJeff Roberson */ 1970686bcb5cSJeff Roberson TDQ_UNLOCK(tdq); 1971686bcb5cSJeff Roberson TDQ_LOCK(tdn); 1972c47f202bSJeff Roberson tdq_add(tdn, td, flags); 197327ee18adSRyan Stone tdq_notify(tdn, td); 1974c47f202bSJeff Roberson TDQ_UNLOCK(tdn); 1975686bcb5cSJeff Roberson TDQ_LOCK(tdq); 1976c47f202bSJeff Roberson #endif 1977c47f202bSJeff Roberson return (TDQ_LOCKPTR(tdn)); 1978c47f202bSJeff Roberson } 1979c47f202bSJeff Roberson 1980c47f202bSJeff Roberson /* 198161a74c5cSJeff Roberson * thread_lock_unblock() that does not assume td_lock is blocked. 1982ae7a6b38SJeff Roberson */ 1983ae7a6b38SJeff Roberson static inline void 1984ae7a6b38SJeff Roberson thread_unblock_switch(struct thread *td, struct mtx *mtx) 1985ae7a6b38SJeff Roberson { 1986ae7a6b38SJeff Roberson atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock, 1987ae7a6b38SJeff Roberson (uintptr_t)mtx); 1988ae7a6b38SJeff Roberson } 1989ae7a6b38SJeff Roberson 1990ae7a6b38SJeff Roberson /* 1991ae7a6b38SJeff Roberson * Switch threads. This function has to handle threads coming in while 1992ae7a6b38SJeff Roberson * blocked for some reason, running, or idle. It also must deal with 1993ae7a6b38SJeff Roberson * migrating a thread from one queue to another as running threads may 1994ae7a6b38SJeff Roberson * be assigned elsewhere via binding. 1995ae7a6b38SJeff Roberson */ 19963db720fdSDavid Xu void 1997686bcb5cSJeff Roberson sched_switch(struct thread *td, int flags) 199835e6168fSJeff Roberson { 1999686bcb5cSJeff Roberson struct thread *newtd; 2000c02bbb43SJeff Roberson struct tdq *tdq; 2001ad1e7d28SJulian Elischer struct td_sched *ts; 2002ae7a6b38SJeff Roberson struct mtx *mtx; 2003c47f202bSJeff Roberson int srqflag; 20043d7f4117SAlexander Motin int cpuid, preempted; 200535e6168fSJeff Roberson 20067b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 200735e6168fSJeff Roberson 2008ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 2009018ff686SJeff Roberson tdq = TDQ_SELF(); 201093ccd6bfSKonstantin Belousov ts = td_get_sched(td); 20117295465eSAlexander Motin sched_pctcpu_update(ts, 1); 2012ae7a6b38SJeff Roberson ts->ts_rltick = ticks; 2013060563ecSJulian Elischer td->td_lastcpu = td->td_oncpu; 2014ad9dadc4SAndriy Gapon preempted = (td->td_flags & TDF_SLICEEND) == 0 && 2015ad9dadc4SAndriy Gapon (flags & SW_PREEMPT) != 0; 20163d7f4117SAlexander Motin td->td_flags &= ~(TDF_NEEDRESCHED | TDF_SLICEEND); 201777918643SStephan Uphoff td->td_owepreempt = 0; 20187789ab32SMark Johnston tdq->tdq_owepreempt = 0; 20192c27cb3aSAlexander Motin if (!TD_IS_IDLETHREAD(td)) 20201690c6c1SJeff Roberson tdq->tdq_switchcnt++; 20217789ab32SMark Johnston 2022b11fdad0SJeff Roberson /* 2023686bcb5cSJeff Roberson * Always block the thread lock so we can drop the tdq lock early. 2024b11fdad0SJeff Roberson */ 2025686bcb5cSJeff Roberson mtx = thread_lock_block(td); 2026686bcb5cSJeff Roberson spinlock_enter(); 2027486a9414SJulian Elischer if (TD_IS_IDLETHREAD(td)) { 2028686bcb5cSJeff Roberson MPASS(mtx == TDQ_LOCKPTR(tdq)); 2029bf0acc27SJohn Baldwin TD_SET_CAN_RUN(td); 20307b20fb19SJeff Roberson } else if (TD_IS_RUNNING(td)) { 2031686bcb5cSJeff Roberson MPASS(mtx == TDQ_LOCKPTR(tdq)); 20323d7f4117SAlexander Motin srqflag = preempted ? 2033598b368dSJeff Roberson SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 2034c47f202bSJeff Roberson SRQ_OURSELF|SRQ_YIELDING; 2035ba4932b5SMatthew D Fleming #ifdef SMP 20360f7a0ebdSMatthew D Fleming if (THREAD_CAN_MIGRATE(td) && !THREAD_CAN_SCHED(td, ts->ts_cpu)) 20370f7a0ebdSMatthew D Fleming ts->ts_cpu = sched_pickcpu(td, 0); 2038ba4932b5SMatthew D Fleming #endif 2039c47f202bSJeff Roberson if (ts->ts_cpu == cpuid) 20409727e637SJeff Roberson tdq_runq_add(tdq, td, srqflag); 2041686bcb5cSJeff Roberson else 2042c47f202bSJeff Roberson mtx = sched_switch_migrate(tdq, td, srqflag); 2043ae7a6b38SJeff Roberson } else { 2044ae7a6b38SJeff Roberson /* This thread must be going to sleep. */ 204561a74c5cSJeff Roberson if (mtx != TDQ_LOCKPTR(tdq)) { 204661a74c5cSJeff Roberson mtx_unlock_spin(mtx); 204761a74c5cSJeff Roberson TDQ_LOCK(tdq); 204861a74c5cSJeff Roberson } 20499727e637SJeff Roberson tdq_load_rem(tdq, td); 20504c8a8cfcSKonstantin Belousov #ifdef SMP 205197e9382dSDon Lewis if (tdq->tdq_load == 0) 205297e9382dSDon Lewis tdq_trysteal(tdq); 20534c8a8cfcSKonstantin Belousov #endif 2054ae7a6b38SJeff Roberson } 2055afa0a46cSAndriy Gapon 2056afa0a46cSAndriy Gapon #if (KTR_COMPILE & KTR_SCHED) != 0 2057afa0a46cSAndriy Gapon if (TD_IS_IDLETHREAD(td)) 2058afa0a46cSAndriy Gapon KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "idle", 2059afa0a46cSAndriy Gapon "prio:%d", td->td_priority); 2060afa0a46cSAndriy Gapon else 2061afa0a46cSAndriy Gapon KTR_STATE3(KTR_SCHED, "thread", sched_tdname(td), KTDSTATE(td), 2062afa0a46cSAndriy Gapon "prio:%d", td->td_priority, "wmesg:\"%s\"", td->td_wmesg, 2063afa0a46cSAndriy Gapon "lockname:\"%s\"", td->td_lockname); 2064afa0a46cSAndriy Gapon #endif 2065afa0a46cSAndriy Gapon 2066ae7a6b38SJeff Roberson /* 2067ae7a6b38SJeff Roberson * We enter here with the thread blocked and assigned to the 2068ae7a6b38SJeff Roberson * appropriate cpu run-queue or sleep-queue and with the current 2069ae7a6b38SJeff Roberson * thread-queue locked. 2070ae7a6b38SJeff Roberson */ 2071ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED); 20722454aaf5SJeff Roberson newtd = choosethread(); 2073686bcb5cSJeff Roberson sched_pctcpu_update(td_get_sched(newtd), 0); 2074686bcb5cSJeff Roberson TDQ_UNLOCK(tdq); 2075686bcb5cSJeff Roberson 2076ae7a6b38SJeff Roberson /* 2077ae7a6b38SJeff Roberson * Call the MD code to switch contexts if necessary. 2078ae7a6b38SJeff Roberson */ 2079ebccf1e3SJoseph Koshy if (td != newtd) { 2080ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 2081ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 2082ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 2083ebccf1e3SJoseph Koshy #endif 2084d9fae5abSAndriy Gapon SDT_PROBE2(sched, , , off__cpu, newtd, newtd->td_proc); 20856f5f25e5SJohn Birrell 20866f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS 20876f5f25e5SJohn Birrell /* 20886f5f25e5SJohn Birrell * If DTrace has set the active vtime enum to anything 20896f5f25e5SJohn Birrell * other than INACTIVE (0), then it should have set the 20906f5f25e5SJohn Birrell * function to call. 20916f5f25e5SJohn Birrell */ 20926f5f25e5SJohn Birrell if (dtrace_vtime_active) 20936f5f25e5SJohn Birrell (*dtrace_vtime_switch_func)(newtd); 20946f5f25e5SJohn Birrell #endif 2095686bcb5cSJeff Roberson td->td_oncpu = NOCPU; 2096ae7a6b38SJeff Roberson cpu_switch(td, newtd, mtx); 2097a89c2c8cSMark Johnston cpuid = td->td_oncpu = PCPU_GET(cpuid); 2098b3e9e682SRyan Stone 2099d9fae5abSAndriy Gapon SDT_PROBE0(sched, , , on__cpu); 2100ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 2101ebccf1e3SJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 2102ebccf1e3SJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 2103ebccf1e3SJoseph Koshy #endif 2104b3e9e682SRyan Stone } else { 2105ae7a6b38SJeff Roberson thread_unblock_switch(td, mtx); 2106d9fae5abSAndriy Gapon SDT_PROBE0(sched, , , remain__cpu); 2107b3e9e682SRyan Stone } 2108686bcb5cSJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, 2109686bcb5cSJeff Roberson ("invalid count %d", curthread->td_md.md_spinlock_count)); 2110afa0a46cSAndriy Gapon 2111afa0a46cSAndriy Gapon KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "running", 2112afa0a46cSAndriy Gapon "prio:%d", td->td_priority); 211335e6168fSJeff Roberson } 211435e6168fSJeff Roberson 2115ae7a6b38SJeff Roberson /* 2116ae7a6b38SJeff Roberson * Adjust thread priorities as a result of a nice request. 2117ae7a6b38SJeff Roberson */ 211835e6168fSJeff Roberson void 2119fa885116SJulian Elischer sched_nice(struct proc *p, int nice) 212035e6168fSJeff Roberson { 212135e6168fSJeff Roberson struct thread *td; 212235e6168fSJeff Roberson 2123fa885116SJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 2124e7d50326SJeff Roberson 2125fa885116SJulian Elischer p->p_nice = nice; 21268460a577SJohn Birrell FOREACH_THREAD_IN_PROC(p, td) { 21277b20fb19SJeff Roberson thread_lock(td); 21288460a577SJohn Birrell sched_priority(td); 2129e7d50326SJeff Roberson sched_prio(td, td->td_base_user_pri); 21307b20fb19SJeff Roberson thread_unlock(td); 213135e6168fSJeff Roberson } 2132fa885116SJulian Elischer } 213335e6168fSJeff Roberson 2134ae7a6b38SJeff Roberson /* 2135ae7a6b38SJeff Roberson * Record the sleep time for the interactivity scorer. 2136ae7a6b38SJeff Roberson */ 213735e6168fSJeff Roberson void 2138c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int prio) 213935e6168fSJeff Roberson { 2140e7d50326SJeff Roberson 21417b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 214235e6168fSJeff Roberson 214354b0e65fSJeff Roberson td->td_slptick = ticks; 214417c4c356SKonstantin Belousov if (TD_IS_SUSPENDED(td) || prio >= PSOCK) 2145c5aa6b58SJeff Roberson td->td_flags |= TDF_CANSWAP; 21462dc29adbSJohn Baldwin if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE) 21472dc29adbSJohn Baldwin return; 21480502fe2eSJeff Roberson if (static_boost == 1 && prio) 2149c5aa6b58SJeff Roberson sched_prio(td, prio); 21500502fe2eSJeff Roberson else if (static_boost && td->td_priority > static_boost) 21510502fe2eSJeff Roberson sched_prio(td, static_boost); 215235e6168fSJeff Roberson } 215335e6168fSJeff Roberson 2154ae7a6b38SJeff Roberson /* 2155ae7a6b38SJeff Roberson * Schedule a thread to resume execution and record how long it voluntarily 2156ae7a6b38SJeff Roberson * slept. We also update the pctcpu, interactivity, and priority. 215761a74c5cSJeff Roberson * 215861a74c5cSJeff Roberson * Requires the thread lock on entry, drops on exit. 2159ae7a6b38SJeff Roberson */ 216035e6168fSJeff Roberson void 216161a74c5cSJeff Roberson sched_wakeup(struct thread *td, int srqflags) 216235e6168fSJeff Roberson { 216314618990SJeff Roberson struct td_sched *ts; 2164ae7a6b38SJeff Roberson int slptick; 2165e7d50326SJeff Roberson 21667b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 216793ccd6bfSKonstantin Belousov ts = td_get_sched(td); 2168c5aa6b58SJeff Roberson td->td_flags &= ~TDF_CANSWAP; 216961a74c5cSJeff Roberson 217035e6168fSJeff Roberson /* 2171e7d50326SJeff Roberson * If we slept for more than a tick update our interactivity and 2172e7d50326SJeff Roberson * priority. 217335e6168fSJeff Roberson */ 217454b0e65fSJeff Roberson slptick = td->td_slptick; 217554b0e65fSJeff Roberson td->td_slptick = 0; 2176ae7a6b38SJeff Roberson if (slptick && slptick != ticks) { 21777295465eSAlexander Motin ts->ts_slptime += (ticks - slptick) << SCHED_TICK_SHIFT; 21788460a577SJohn Birrell sched_interact_update(td); 21797295465eSAlexander Motin sched_pctcpu_update(ts, 0); 2180f1e8dc4aSJeff Roberson } 21815e5c3873SJeff Roberson /* 21825e5c3873SJeff Roberson * Reset the slice value since we slept and advanced the round-robin. 21835e5c3873SJeff Roberson */ 21845e5c3873SJeff Roberson ts->ts_slice = 0; 218561a74c5cSJeff Roberson sched_add(td, SRQ_BORING | srqflags); 218635e6168fSJeff Roberson } 218735e6168fSJeff Roberson 218835e6168fSJeff Roberson /* 218935e6168fSJeff Roberson * Penalize the parent for creating a new child and initialize the child's 219035e6168fSJeff Roberson * priority. 219135e6168fSJeff Roberson */ 219235e6168fSJeff Roberson void 21938460a577SJohn Birrell sched_fork(struct thread *td, struct thread *child) 219415dc847eSJeff Roberson { 21957b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 219693ccd6bfSKonstantin Belousov sched_pctcpu_update(td_get_sched(td), 1); 2197ad1e7d28SJulian Elischer sched_fork_thread(td, child); 2198e7d50326SJeff Roberson /* 2199e7d50326SJeff Roberson * Penalize the parent and child for forking. 2200e7d50326SJeff Roberson */ 2201e7d50326SJeff Roberson sched_interact_fork(child); 2202e7d50326SJeff Roberson sched_priority(child); 220393ccd6bfSKonstantin Belousov td_get_sched(td)->ts_runtime += tickincr; 2204e7d50326SJeff Roberson sched_interact_update(td); 2205e7d50326SJeff Roberson sched_priority(td); 2206ad1e7d28SJulian Elischer } 2207ad1e7d28SJulian Elischer 2208ae7a6b38SJeff Roberson /* 2209ae7a6b38SJeff Roberson * Fork a new thread, may be within the same process. 2210ae7a6b38SJeff Roberson */ 2211ad1e7d28SJulian Elischer void 2212ad1e7d28SJulian Elischer sched_fork_thread(struct thread *td, struct thread *child) 2213ad1e7d28SJulian Elischer { 2214ad1e7d28SJulian Elischer struct td_sched *ts; 2215ad1e7d28SJulian Elischer struct td_sched *ts2; 22165e5c3873SJeff Roberson struct tdq *tdq; 22178460a577SJohn Birrell 22185e5c3873SJeff Roberson tdq = TDQ_SELF(); 22198b16c208SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2220e7d50326SJeff Roberson /* 2221e7d50326SJeff Roberson * Initialize child. 2222e7d50326SJeff Roberson */ 222393ccd6bfSKonstantin Belousov ts = td_get_sched(td); 222493ccd6bfSKonstantin Belousov ts2 = td_get_sched(child); 222592de34dfSJohn Baldwin child->td_oncpu = NOCPU; 222692de34dfSJohn Baldwin child->td_lastcpu = NOCPU; 22275e5c3873SJeff Roberson child->td_lock = TDQ_LOCKPTR(tdq); 22288b16c208SJeff Roberson child->td_cpuset = cpuset_ref(td->td_cpuset); 22293f289c3fSJeff Roberson child->td_domain.dr_policy = td->td_cpuset->cs_domain; 2230ad1e7d28SJulian Elischer ts2->ts_cpu = ts->ts_cpu; 22318b16c208SJeff Roberson ts2->ts_flags = 0; 2232e7d50326SJeff Roberson /* 223322d19207SJohn Baldwin * Grab our parents cpu estimation information. 2234e7d50326SJeff Roberson */ 2235ad1e7d28SJulian Elischer ts2->ts_ticks = ts->ts_ticks; 2236ad1e7d28SJulian Elischer ts2->ts_ltick = ts->ts_ltick; 2237ad1e7d28SJulian Elischer ts2->ts_ftick = ts->ts_ftick; 223822d19207SJohn Baldwin /* 223922d19207SJohn Baldwin * Do not inherit any borrowed priority from the parent. 224022d19207SJohn Baldwin */ 224122d19207SJohn Baldwin child->td_priority = child->td_base_pri; 2242e7d50326SJeff Roberson /* 2243e7d50326SJeff Roberson * And update interactivity score. 2244e7d50326SJeff Roberson */ 2245ae7a6b38SJeff Roberson ts2->ts_slptime = ts->ts_slptime; 2246ae7a6b38SJeff Roberson ts2->ts_runtime = ts->ts_runtime; 22475e5c3873SJeff Roberson /* Attempt to quickly learn interactivity. */ 22485e5c3873SJeff Roberson ts2->ts_slice = tdq_slice(tdq) - sched_slice_min; 22498f51ad55SJeff Roberson #ifdef KTR 22508f51ad55SJeff Roberson bzero(ts2->ts_name, sizeof(ts2->ts_name)); 22518f51ad55SJeff Roberson #endif 225215dc847eSJeff Roberson } 225315dc847eSJeff Roberson 2254ae7a6b38SJeff Roberson /* 2255ae7a6b38SJeff Roberson * Adjust the priority class of a thread. 2256ae7a6b38SJeff Roberson */ 225715dc847eSJeff Roberson void 22588460a577SJohn Birrell sched_class(struct thread *td, int class) 225915dc847eSJeff Roberson { 226015dc847eSJeff Roberson 22617b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 22628460a577SJohn Birrell if (td->td_pri_class == class) 226315dc847eSJeff Roberson return; 22648460a577SJohn Birrell td->td_pri_class = class; 226535e6168fSJeff Roberson } 226635e6168fSJeff Roberson 226735e6168fSJeff Roberson /* 226835e6168fSJeff Roberson * Return some of the child's priority and interactivity to the parent. 226935e6168fSJeff Roberson */ 227035e6168fSJeff Roberson void 2271fc6c30f6SJulian Elischer sched_exit(struct proc *p, struct thread *child) 227235e6168fSJeff Roberson { 2273e7d50326SJeff Roberson struct thread *td; 2274141ad61cSJeff Roberson 22758f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "proc exit", 2276cd39bb09SXin LI "prio:%d", child->td_priority); 2277374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 2278e7d50326SJeff Roberson td = FIRST_THREAD_IN_PROC(p); 2279e7d50326SJeff Roberson sched_exit_thread(td, child); 2280ad1e7d28SJulian Elischer } 2281ad1e7d28SJulian Elischer 2282ae7a6b38SJeff Roberson /* 2283ae7a6b38SJeff Roberson * Penalize another thread for the time spent on this one. This helps to 2284ae7a6b38SJeff Roberson * worsen the priority and interactivity of processes which schedule batch 2285ae7a6b38SJeff Roberson * jobs such as make. This has little effect on the make process itself but 2286ae7a6b38SJeff Roberson * causes new processes spawned by it to receive worse scores immediately. 2287ae7a6b38SJeff Roberson */ 2288ad1e7d28SJulian Elischer void 2289fc6c30f6SJulian Elischer sched_exit_thread(struct thread *td, struct thread *child) 2290ad1e7d28SJulian Elischer { 2291fc6c30f6SJulian Elischer 22928f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "thread exit", 2293cd39bb09SXin LI "prio:%d", child->td_priority); 2294e7d50326SJeff Roberson /* 2295e7d50326SJeff Roberson * Give the child's runtime to the parent without returning the 2296e7d50326SJeff Roberson * sleep time as a penalty to the parent. This causes shells that 2297e7d50326SJeff Roberson * launch expensive things to mark their children as expensive. 2298e7d50326SJeff Roberson */ 22997b20fb19SJeff Roberson thread_lock(td); 230093ccd6bfSKonstantin Belousov td_get_sched(td)->ts_runtime += td_get_sched(child)->ts_runtime; 2301fc6c30f6SJulian Elischer sched_interact_update(td); 2302e7d50326SJeff Roberson sched_priority(td); 23037b20fb19SJeff Roberson thread_unlock(td); 2304ad1e7d28SJulian Elischer } 2305ad1e7d28SJulian Elischer 2306ff256d9cSJeff Roberson void 2307ff256d9cSJeff Roberson sched_preempt(struct thread *td) 2308ff256d9cSJeff Roberson { 2309ff256d9cSJeff Roberson struct tdq *tdq; 2310686bcb5cSJeff Roberson int flags; 2311ff256d9cSJeff Roberson 2312b3e9e682SRyan Stone SDT_PROBE2(sched, , , surrender, td, td->td_proc); 2313b3e9e682SRyan Stone 2314ff256d9cSJeff Roberson thread_lock(td); 2315ff256d9cSJeff Roberson tdq = TDQ_SELF(); 2316ff256d9cSJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2317ff256d9cSJeff Roberson if (td->td_priority > tdq->tdq_lowpri) { 2318686bcb5cSJeff Roberson if (td->td_critnest == 1) { 23198df78c41SJeff Roberson flags = SW_INVOL | SW_PREEMPT; 2320686bcb5cSJeff Roberson flags |= TD_IS_IDLETHREAD(td) ? SWT_REMOTEWAKEIDLE : 2321686bcb5cSJeff Roberson SWT_REMOTEPREEMPT; 2322686bcb5cSJeff Roberson mi_switch(flags); 2323686bcb5cSJeff Roberson /* Switch dropped thread lock. */ 2324686bcb5cSJeff Roberson return; 2325686bcb5cSJeff Roberson } 2326ff256d9cSJeff Roberson td->td_owepreempt = 1; 23277789ab32SMark Johnston } else { 23287789ab32SMark Johnston tdq->tdq_owepreempt = 0; 2329ff256d9cSJeff Roberson } 2330ff256d9cSJeff Roberson thread_unlock(td); 2331ff256d9cSJeff Roberson } 2332ff256d9cSJeff Roberson 2333ae7a6b38SJeff Roberson /* 2334ae7a6b38SJeff Roberson * Fix priorities on return to user-space. Priorities may be elevated due 2335ae7a6b38SJeff Roberson * to static priorities in msleep() or similar. 2336ae7a6b38SJeff Roberson */ 2337ad1e7d28SJulian Elischer void 233828240885SMateusz Guzik sched_userret_slowpath(struct thread *td) 2339ad1e7d28SJulian Elischer { 234028240885SMateusz Guzik 23417b20fb19SJeff Roberson thread_lock(td); 2342ad1e7d28SJulian Elischer td->td_priority = td->td_user_pri; 2343ad1e7d28SJulian Elischer td->td_base_pri = td->td_user_pri; 234462fa74d9SJeff Roberson tdq_setlowpri(TDQ_SELF(), td); 23457b20fb19SJeff Roberson thread_unlock(td); 2346ad1e7d28SJulian Elischer } 234735e6168fSJeff Roberson 2348ae7a6b38SJeff Roberson /* 2349ae7a6b38SJeff Roberson * Handle a stathz tick. This is really only relevant for timeshare 2350ae7a6b38SJeff Roberson * threads. 2351ae7a6b38SJeff Roberson */ 235235e6168fSJeff Roberson void 2353c3cccf95SJeff Roberson sched_clock(struct thread *td, int cnt) 235435e6168fSJeff Roberson { 2355ad1e7d28SJulian Elischer struct tdq *tdq; 2356ad1e7d28SJulian Elischer struct td_sched *ts; 235735e6168fSJeff Roberson 2358ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 23593f872f85SJeff Roberson tdq = TDQ_SELF(); 23607fcf154aSJeff Roberson #ifdef SMP 23617fcf154aSJeff Roberson /* 23627fcf154aSJeff Roberson * We run the long term load balancer infrequently on the first cpu. 23637fcf154aSJeff Roberson */ 2364c3cccf95SJeff Roberson if (balance_tdq == tdq && smp_started != 0 && rebalance != 0 && 2365c3cccf95SJeff Roberson balance_ticks != 0) { 2366c3cccf95SJeff Roberson balance_ticks -= cnt; 2367c3cccf95SJeff Roberson if (balance_ticks <= 0) 23687fcf154aSJeff Roberson sched_balance(); 23697fcf154aSJeff Roberson } 23707fcf154aSJeff Roberson #endif 23713f872f85SJeff Roberson /* 23721690c6c1SJeff Roberson * Save the old switch count so we have a record of the last ticks 23731690c6c1SJeff Roberson * activity. Initialize the new switch count based on our load. 23741690c6c1SJeff Roberson * If there is some activity seed it to reflect that. 23751690c6c1SJeff Roberson */ 23761690c6c1SJeff Roberson tdq->tdq_oldswitchcnt = tdq->tdq_switchcnt; 23776c47aaaeSJeff Roberson tdq->tdq_switchcnt = tdq->tdq_load; 23781690c6c1SJeff Roberson /* 23793f872f85SJeff Roberson * Advance the insert index once for each tick to ensure that all 23803f872f85SJeff Roberson * threads get a chance to run. 23813f872f85SJeff Roberson */ 23823f872f85SJeff Roberson if (tdq->tdq_idx == tdq->tdq_ridx) { 23833f872f85SJeff Roberson tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 23843f872f85SJeff Roberson if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 23853f872f85SJeff Roberson tdq->tdq_ridx = tdq->tdq_idx; 23863f872f85SJeff Roberson } 238793ccd6bfSKonstantin Belousov ts = td_get_sched(td); 23887295465eSAlexander Motin sched_pctcpu_update(ts, 1); 2389c3cccf95SJeff Roberson if ((td->td_pri_class & PRI_FIFO_BIT) || TD_IS_IDLETHREAD(td)) 2390a8949de2SJeff Roberson return; 2391c3cccf95SJeff Roberson 2392c9a8cba4SJohn Baldwin if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) { 2393a8949de2SJeff Roberson /* 2394fd0b8c78SJeff Roberson * We used a tick; charge it to the thread so 2395fd0b8c78SJeff Roberson * that we can compute our interactivity. 239615dc847eSJeff Roberson */ 2397c3cccf95SJeff Roberson td_get_sched(td)->ts_runtime += tickincr * cnt; 23988460a577SJohn Birrell sched_interact_update(td); 239973daf66fSJeff Roberson sched_priority(td); 2400fd0b8c78SJeff Roberson } 2401579895dfSAlexander Motin 240235e6168fSJeff Roberson /* 2403579895dfSAlexander Motin * Force a context switch if the current thread has used up a full 2404579895dfSAlexander Motin * time slice (default is 100ms). 240535e6168fSJeff Roberson */ 2406c3cccf95SJeff Roberson ts->ts_slice += cnt; 2407c3cccf95SJeff Roberson if (ts->ts_slice >= tdq_slice(tdq)) { 24085e5c3873SJeff Roberson ts->ts_slice = 0; 24093d7f4117SAlexander Motin td->td_flags |= TDF_NEEDRESCHED | TDF_SLICEEND; 241035e6168fSJeff Roberson } 2411579895dfSAlexander Motin } 241235e6168fSJeff Roberson 2413ccd0ec40SKonstantin Belousov u_int 2414ccd0ec40SKonstantin Belousov sched_estcpu(struct thread *td __unused) 2415ae7a6b38SJeff Roberson { 2416ae7a6b38SJeff Roberson 2417ccd0ec40SKonstantin Belousov return (0); 2418ae7a6b38SJeff Roberson } 2419ae7a6b38SJeff Roberson 2420ae7a6b38SJeff Roberson /* 2421ae7a6b38SJeff Roberson * Return whether the current CPU has runnable tasks. Used for in-kernel 2422ae7a6b38SJeff Roberson * cooperative idle threads. 2423ae7a6b38SJeff Roberson */ 242435e6168fSJeff Roberson int 242535e6168fSJeff Roberson sched_runnable(void) 242635e6168fSJeff Roberson { 2427ad1e7d28SJulian Elischer struct tdq *tdq; 2428b90816f1SJeff Roberson int load; 242935e6168fSJeff Roberson 2430b90816f1SJeff Roberson load = 1; 2431b90816f1SJeff Roberson 2432ad1e7d28SJulian Elischer tdq = TDQ_SELF(); 24333f741ca1SJeff Roberson if ((curthread->td_flags & TDF_IDLETD) != 0) { 2434d2ad694cSJeff Roberson if (tdq->tdq_load > 0) 24353f741ca1SJeff Roberson goto out; 24363f741ca1SJeff Roberson } else 2437d2ad694cSJeff Roberson if (tdq->tdq_load - 1 > 0) 2438b90816f1SJeff Roberson goto out; 2439b90816f1SJeff Roberson load = 0; 2440b90816f1SJeff Roberson out: 2441b90816f1SJeff Roberson return (load); 244235e6168fSJeff Roberson } 244335e6168fSJeff Roberson 2444ae7a6b38SJeff Roberson /* 2445ae7a6b38SJeff Roberson * Choose the highest priority thread to run. The thread is removed from 2446ae7a6b38SJeff Roberson * the run-queue while running however the load remains. For SMP we set 2447ae7a6b38SJeff Roberson * the tdq in the global idle bitmask if it idles here. 2448ae7a6b38SJeff Roberson */ 24497a5e5e2aSJeff Roberson struct thread * 2450c9f25d8fSJeff Roberson sched_choose(void) 2451c9f25d8fSJeff Roberson { 24529727e637SJeff Roberson struct thread *td; 2453ae7a6b38SJeff Roberson struct tdq *tdq; 2454ae7a6b38SJeff Roberson 2455ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2456ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 24579727e637SJeff Roberson td = tdq_choose(tdq); 24589727e637SJeff Roberson if (td) { 24599727e637SJeff Roberson tdq_runq_rem(tdq, td); 24600502fe2eSJeff Roberson tdq->tdq_lowpri = td->td_priority; 24619727e637SJeff Roberson return (td); 246235e6168fSJeff Roberson } 24630502fe2eSJeff Roberson tdq->tdq_lowpri = PRI_MAX_IDLE; 246462fa74d9SJeff Roberson return (PCPU_GET(idlethread)); 24657a5e5e2aSJeff Roberson } 24667a5e5e2aSJeff Roberson 2467ae7a6b38SJeff Roberson /* 2468ae7a6b38SJeff Roberson * Set owepreempt if necessary. Preemption never happens directly in ULE, 2469ae7a6b38SJeff Roberson * we always request it once we exit a critical section. 2470ae7a6b38SJeff Roberson */ 2471ae7a6b38SJeff Roberson static inline void 2472ae7a6b38SJeff Roberson sched_setpreempt(struct thread *td) 24737a5e5e2aSJeff Roberson { 24747a5e5e2aSJeff Roberson struct thread *ctd; 24757a5e5e2aSJeff Roberson int cpri; 24767a5e5e2aSJeff Roberson int pri; 24777a5e5e2aSJeff Roberson 2478ff256d9cSJeff Roberson THREAD_LOCK_ASSERT(curthread, MA_OWNED); 2479ff256d9cSJeff Roberson 24807a5e5e2aSJeff Roberson ctd = curthread; 24817a5e5e2aSJeff Roberson pri = td->td_priority; 24827a5e5e2aSJeff Roberson cpri = ctd->td_priority; 2483ff256d9cSJeff Roberson if (pri < cpri) 2484ff256d9cSJeff Roberson ctd->td_flags |= TDF_NEEDRESCHED; 2485879e0604SMateusz Guzik if (KERNEL_PANICKED() || pri >= cpri || cold || TD_IS_INHIBITED(ctd)) 2486ae7a6b38SJeff Roberson return; 2487ff256d9cSJeff Roberson if (!sched_shouldpreempt(pri, cpri, 0)) 2488ae7a6b38SJeff Roberson return; 24897a5e5e2aSJeff Roberson ctd->td_owepreempt = 1; 249035e6168fSJeff Roberson } 249135e6168fSJeff Roberson 2492ae7a6b38SJeff Roberson /* 249373daf66fSJeff Roberson * Add a thread to a thread queue. Select the appropriate runq and add the 249473daf66fSJeff Roberson * thread to it. This is the internal function called when the tdq is 249573daf66fSJeff Roberson * predetermined. 2496ae7a6b38SJeff Roberson */ 249735e6168fSJeff Roberson void 2498ae7a6b38SJeff Roberson tdq_add(struct tdq *tdq, struct thread *td, int flags) 249935e6168fSJeff Roberson { 2500c9f25d8fSJeff Roberson 2501ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 250261a74c5cSJeff Roberson THREAD_LOCK_BLOCKED_ASSERT(td, MA_OWNED); 25037a5e5e2aSJeff Roberson KASSERT((td->td_inhibitors == 0), 25047a5e5e2aSJeff Roberson ("sched_add: trying to run inhibited thread")); 25057a5e5e2aSJeff Roberson KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)), 25067a5e5e2aSJeff Roberson ("sched_add: bad thread state")); 2507b61ce5b0SJeff Roberson KASSERT(td->td_flags & TDF_INMEM, 2508b61ce5b0SJeff Roberson ("sched_add: thread swapped out")); 2509ae7a6b38SJeff Roberson 2510ae7a6b38SJeff Roberson if (td->td_priority < tdq->tdq_lowpri) 2511ae7a6b38SJeff Roberson tdq->tdq_lowpri = td->td_priority; 25129727e637SJeff Roberson tdq_runq_add(tdq, td, flags); 25139727e637SJeff Roberson tdq_load_add(tdq, td); 2514ae7a6b38SJeff Roberson } 2515ae7a6b38SJeff Roberson 2516ae7a6b38SJeff Roberson /* 2517ae7a6b38SJeff Roberson * Select the target thread queue and add a thread to it. Request 2518ae7a6b38SJeff Roberson * preemption or IPI a remote processor if required. 251961a74c5cSJeff Roberson * 252061a74c5cSJeff Roberson * Requires the thread lock on entry, drops on exit. 2521ae7a6b38SJeff Roberson */ 2522ae7a6b38SJeff Roberson void 2523ae7a6b38SJeff Roberson sched_add(struct thread *td, int flags) 2524ae7a6b38SJeff Roberson { 2525ae7a6b38SJeff Roberson struct tdq *tdq; 25267b8bfa0dSJeff Roberson #ifdef SMP 2527ae7a6b38SJeff Roberson int cpu; 2528ae7a6b38SJeff Roberson #endif 25298f51ad55SJeff Roberson 25308f51ad55SJeff Roberson KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add", 25318f51ad55SJeff Roberson "prio:%d", td->td_priority, KTR_ATTR_LINKED, 25328f51ad55SJeff Roberson sched_tdname(curthread)); 25338f51ad55SJeff Roberson KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup", 25348f51ad55SJeff Roberson KTR_ATTR_LINKED, sched_tdname(td)); 2535b3e9e682SRyan Stone SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL, 2536b3e9e682SRyan Stone flags & SRQ_PREEMPTED); 2537ae7a6b38SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2538ae7a6b38SJeff Roberson /* 2539ae7a6b38SJeff Roberson * Recalculate the priority before we select the target cpu or 2540ae7a6b38SJeff Roberson * run-queue. 2541ae7a6b38SJeff Roberson */ 2542ae7a6b38SJeff Roberson if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 2543ae7a6b38SJeff Roberson sched_priority(td); 2544ae7a6b38SJeff Roberson #ifdef SMP 2545ae7a6b38SJeff Roberson /* 2546ae7a6b38SJeff Roberson * Pick the destination cpu and if it isn't ours transfer to the 2547ae7a6b38SJeff Roberson * target cpu. 2548ae7a6b38SJeff Roberson */ 25499727e637SJeff Roberson cpu = sched_pickcpu(td, flags); 25509727e637SJeff Roberson tdq = sched_setcpu(td, cpu, flags); 2551ae7a6b38SJeff Roberson tdq_add(tdq, td, flags); 255261a74c5cSJeff Roberson if (cpu != PCPU_GET(cpuid)) 255327ee18adSRyan Stone tdq_notify(tdq, td); 255461a74c5cSJeff Roberson else if (!(flags & SRQ_YIELDING)) 255561a74c5cSJeff Roberson sched_setpreempt(td); 2556ae7a6b38SJeff Roberson #else 2557ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2558ae7a6b38SJeff Roberson /* 2559ae7a6b38SJeff Roberson * Now that the thread is moving to the run-queue, set the lock 2560ae7a6b38SJeff Roberson * to the scheduler's lock. 2561ae7a6b38SJeff Roberson */ 2562e4894505SMark Johnston if (td->td_lock != TDQ_LOCKPTR(tdq)) { 2563e4894505SMark Johnston TDQ_LOCK(tdq); 256461a74c5cSJeff Roberson if ((flags & SRQ_HOLD) != 0) 256561a74c5cSJeff Roberson td->td_lock = TDQ_LOCKPTR(tdq); 256661a74c5cSJeff Roberson else 2567ae7a6b38SJeff Roberson thread_lock_set(td, TDQ_LOCKPTR(tdq)); 2568e4894505SMark Johnston } 2569ae7a6b38SJeff Roberson tdq_add(tdq, td, flags); 2570ae7a6b38SJeff Roberson if (!(flags & SRQ_YIELDING)) 2571ae7a6b38SJeff Roberson sched_setpreempt(td); 257261a74c5cSJeff Roberson #endif 257361a74c5cSJeff Roberson if (!(flags & SRQ_HOLDTD)) 257461a74c5cSJeff Roberson thread_unlock(td); 257535e6168fSJeff Roberson } 257635e6168fSJeff Roberson 2577ae7a6b38SJeff Roberson /* 2578ae7a6b38SJeff Roberson * Remove a thread from a run-queue without running it. This is used 2579ae7a6b38SJeff Roberson * when we're stealing a thread from a remote queue. Otherwise all threads 2580ae7a6b38SJeff Roberson * exit by calling sched_exit_thread() and sched_throw() themselves. 2581ae7a6b38SJeff Roberson */ 258235e6168fSJeff Roberson void 25837cf90fb3SJeff Roberson sched_rem(struct thread *td) 258435e6168fSJeff Roberson { 2585ad1e7d28SJulian Elischer struct tdq *tdq; 25867cf90fb3SJeff Roberson 25878f51ad55SJeff Roberson KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "runq rem", 25888f51ad55SJeff Roberson "prio:%d", td->td_priority); 2589b3e9e682SRyan Stone SDT_PROBE3(sched, , , dequeue, td, td->td_proc, NULL); 259093ccd6bfSKonstantin Belousov tdq = TDQ_CPU(td_get_sched(td)->ts_cpu); 2591ae7a6b38SJeff Roberson TDQ_LOCK_ASSERT(tdq, MA_OWNED); 2592ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 25937a5e5e2aSJeff Roberson KASSERT(TD_ON_RUNQ(td), 2594ad1e7d28SJulian Elischer ("sched_rem: thread not on run queue")); 25959727e637SJeff Roberson tdq_runq_rem(tdq, td); 25969727e637SJeff Roberson tdq_load_rem(tdq, td); 25977a5e5e2aSJeff Roberson TD_SET_CAN_RUN(td); 259862fa74d9SJeff Roberson if (td->td_priority == tdq->tdq_lowpri) 259962fa74d9SJeff Roberson tdq_setlowpri(tdq, NULL); 260035e6168fSJeff Roberson } 260135e6168fSJeff Roberson 2602ae7a6b38SJeff Roberson /* 2603ae7a6b38SJeff Roberson * Fetch cpu utilization information. Updates on demand. 2604ae7a6b38SJeff Roberson */ 260535e6168fSJeff Roberson fixpt_t 26067cf90fb3SJeff Roberson sched_pctcpu(struct thread *td) 260735e6168fSJeff Roberson { 260835e6168fSJeff Roberson fixpt_t pctcpu; 2609ad1e7d28SJulian Elischer struct td_sched *ts; 261035e6168fSJeff Roberson 261135e6168fSJeff Roberson pctcpu = 0; 261293ccd6bfSKonstantin Belousov ts = td_get_sched(td); 261335e6168fSJeff Roberson 26143da35a0aSJohn Baldwin THREAD_LOCK_ASSERT(td, MA_OWNED); 26157295465eSAlexander Motin sched_pctcpu_update(ts, TD_IS_RUNNING(td)); 2616ad1e7d28SJulian Elischer if (ts->ts_ticks) { 261735e6168fSJeff Roberson int rtick; 261835e6168fSJeff Roberson 261935e6168fSJeff Roberson /* How many rtick per second ? */ 2620e7d50326SJeff Roberson rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 2621e7d50326SJeff Roberson pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 262235e6168fSJeff Roberson } 262335e6168fSJeff Roberson 262435e6168fSJeff Roberson return (pctcpu); 262535e6168fSJeff Roberson } 262635e6168fSJeff Roberson 262762fa74d9SJeff Roberson /* 262862fa74d9SJeff Roberson * Enforce affinity settings for a thread. Called after adjustments to 262962fa74d9SJeff Roberson * cpumask. 263062fa74d9SJeff Roberson */ 2631885d51a3SJeff Roberson void 2632885d51a3SJeff Roberson sched_affinity(struct thread *td) 2633885d51a3SJeff Roberson { 263462fa74d9SJeff Roberson #ifdef SMP 263562fa74d9SJeff Roberson struct td_sched *ts; 263662fa74d9SJeff Roberson 263762fa74d9SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 263893ccd6bfSKonstantin Belousov ts = td_get_sched(td); 263962fa74d9SJeff Roberson if (THREAD_CAN_SCHED(td, ts->ts_cpu)) 264062fa74d9SJeff Roberson return; 264153a6c8b3SJeff Roberson if (TD_ON_RUNQ(td)) { 264253a6c8b3SJeff Roberson sched_rem(td); 2643d8d5f036SJeff Roberson sched_add(td, SRQ_BORING | SRQ_HOLDTD); 264453a6c8b3SJeff Roberson return; 264553a6c8b3SJeff Roberson } 264662fa74d9SJeff Roberson if (!TD_IS_RUNNING(td)) 264762fa74d9SJeff Roberson return; 264862fa74d9SJeff Roberson /* 26490f7a0ebdSMatthew D Fleming * Force a switch before returning to userspace. If the 26500f7a0ebdSMatthew D Fleming * target thread is not running locally send an ipi to force 26510f7a0ebdSMatthew D Fleming * the issue. 265262fa74d9SJeff Roberson */ 2653a8103ae8SJohn Baldwin td->td_flags |= TDF_NEEDRESCHED; 26540f7a0ebdSMatthew D Fleming if (td != curthread) 26550f7a0ebdSMatthew D Fleming ipi_cpu(ts->ts_cpu, IPI_PREEMPT); 265662fa74d9SJeff Roberson #endif 2657885d51a3SJeff Roberson } 2658885d51a3SJeff Roberson 2659ae7a6b38SJeff Roberson /* 2660ae7a6b38SJeff Roberson * Bind a thread to a target cpu. 2661ae7a6b38SJeff Roberson */ 26629bacd788SJeff Roberson void 26639bacd788SJeff Roberson sched_bind(struct thread *td, int cpu) 26649bacd788SJeff Roberson { 2665ad1e7d28SJulian Elischer struct td_sched *ts; 26669bacd788SJeff Roberson 2667c47f202bSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED); 26681d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_bind: can only bind curthread")); 266993ccd6bfSKonstantin Belousov ts = td_get_sched(td); 26706b2f763fSJeff Roberson if (ts->ts_flags & TSF_BOUND) 2671c95d2db2SJeff Roberson sched_unbind(td); 26720f7a0ebdSMatthew D Fleming KASSERT(THREAD_CAN_MIGRATE(td), ("%p must be migratable", td)); 2673ad1e7d28SJulian Elischer ts->ts_flags |= TSF_BOUND; 26746b2f763fSJeff Roberson sched_pin(); 267580f86c9fSJeff Roberson if (PCPU_GET(cpuid) == cpu) 26769bacd788SJeff Roberson return; 26776b2f763fSJeff Roberson ts->ts_cpu = cpu; 26789bacd788SJeff Roberson /* When we return from mi_switch we'll be on the correct cpu. */ 2679686bcb5cSJeff Roberson mi_switch(SW_VOL); 2680686bcb5cSJeff Roberson thread_lock(td); 26819bacd788SJeff Roberson } 26829bacd788SJeff Roberson 2683ae7a6b38SJeff Roberson /* 2684ae7a6b38SJeff Roberson * Release a bound thread. 2685ae7a6b38SJeff Roberson */ 26869bacd788SJeff Roberson void 26879bacd788SJeff Roberson sched_unbind(struct thread *td) 26889bacd788SJeff Roberson { 2689e7d50326SJeff Roberson struct td_sched *ts; 2690e7d50326SJeff Roberson 26917b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 26921d7830edSJohn Baldwin KASSERT(td == curthread, ("sched_unbind: can only bind curthread")); 269393ccd6bfSKonstantin Belousov ts = td_get_sched(td); 26946b2f763fSJeff Roberson if ((ts->ts_flags & TSF_BOUND) == 0) 26956b2f763fSJeff Roberson return; 2696e7d50326SJeff Roberson ts->ts_flags &= ~TSF_BOUND; 2697e7d50326SJeff Roberson sched_unpin(); 26989bacd788SJeff Roberson } 26999bacd788SJeff Roberson 270035e6168fSJeff Roberson int 2701ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td) 2702ebccf1e3SJoseph Koshy { 27037b20fb19SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 270493ccd6bfSKonstantin Belousov return (td_get_sched(td)->ts_flags & TSF_BOUND); 2705ebccf1e3SJoseph Koshy } 2706ebccf1e3SJoseph Koshy 2707ae7a6b38SJeff Roberson /* 2708ae7a6b38SJeff Roberson * Basic yield call. 2709ae7a6b38SJeff Roberson */ 271036ec198bSDavid Xu void 271136ec198bSDavid Xu sched_relinquish(struct thread *td) 271236ec198bSDavid Xu { 27137b20fb19SJeff Roberson thread_lock(td); 2714686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_RELINQUISH); 271536ec198bSDavid Xu } 271636ec198bSDavid Xu 2717ae7a6b38SJeff Roberson /* 2718ae7a6b38SJeff Roberson * Return the total system load. 2719ae7a6b38SJeff Roberson */ 2720ebccf1e3SJoseph Koshy int 272133916c36SJeff Roberson sched_load(void) 272233916c36SJeff Roberson { 272333916c36SJeff Roberson #ifdef SMP 272433916c36SJeff Roberson int total; 272533916c36SJeff Roberson int i; 272633916c36SJeff Roberson 272733916c36SJeff Roberson total = 0; 27283aa6d94eSJohn Baldwin CPU_FOREACH(i) 272962fa74d9SJeff Roberson total += TDQ_CPU(i)->tdq_sysload; 273033916c36SJeff Roberson return (total); 273133916c36SJeff Roberson #else 2732d2ad694cSJeff Roberson return (TDQ_SELF()->tdq_sysload); 273333916c36SJeff Roberson #endif 273433916c36SJeff Roberson } 273533916c36SJeff Roberson 273633916c36SJeff Roberson int 273735e6168fSJeff Roberson sched_sizeof_proc(void) 273835e6168fSJeff Roberson { 273935e6168fSJeff Roberson return (sizeof(struct proc)); 274035e6168fSJeff Roberson } 274135e6168fSJeff Roberson 274235e6168fSJeff Roberson int 274335e6168fSJeff Roberson sched_sizeof_thread(void) 274435e6168fSJeff Roberson { 274535e6168fSJeff Roberson return (sizeof(struct thread) + sizeof(struct td_sched)); 274635e6168fSJeff Roberson } 2747b41f1452SDavid Xu 274809c8a4ccSJeff Roberson #ifdef SMP 274909c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) \ 275009c8a4ccSJeff Roberson ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0) 275109c8a4ccSJeff Roberson #else 275209c8a4ccSJeff Roberson #define TDQ_IDLESPIN(tdq) 1 275309c8a4ccSJeff Roberson #endif 275409c8a4ccSJeff Roberson 27557a5e5e2aSJeff Roberson /* 27567a5e5e2aSJeff Roberson * The actual idle process. 27577a5e5e2aSJeff Roberson */ 27587a5e5e2aSJeff Roberson void 27597a5e5e2aSJeff Roberson sched_idletd(void *dummy) 27607a5e5e2aSJeff Roberson { 27617a5e5e2aSJeff Roberson struct thread *td; 2762ae7a6b38SJeff Roberson struct tdq *tdq; 27632c27cb3aSAlexander Motin int oldswitchcnt, switchcnt; 27641690c6c1SJeff Roberson int i; 27657a5e5e2aSJeff Roberson 27667b55ab05SJeff Roberson mtx_assert(&Giant, MA_NOTOWNED); 27677a5e5e2aSJeff Roberson td = curthread; 2768ae7a6b38SJeff Roberson tdq = TDQ_SELF(); 2769ba96d2d8SJohn Baldwin THREAD_NO_SLEEPING(); 27702c27cb3aSAlexander Motin oldswitchcnt = -1; 2771ae7a6b38SJeff Roberson for (;;) { 27722c27cb3aSAlexander Motin if (tdq->tdq_load) { 27732c27cb3aSAlexander Motin thread_lock(td); 2774686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IDLE); 27752c27cb3aSAlexander Motin } 27762c27cb3aSAlexander Motin switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 2777ae7a6b38SJeff Roberson #ifdef SMP 277897e9382dSDon Lewis if (always_steal || switchcnt != oldswitchcnt) { 27792c27cb3aSAlexander Motin oldswitchcnt = switchcnt; 27801690c6c1SJeff Roberson if (tdq_idled(tdq) == 0) 27811690c6c1SJeff Roberson continue; 27822c27cb3aSAlexander Motin } 27831690c6c1SJeff Roberson switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 27842fd4047fSAlexander Motin #else 27852fd4047fSAlexander Motin oldswitchcnt = switchcnt; 27862fd4047fSAlexander Motin #endif 27871690c6c1SJeff Roberson /* 27881690c6c1SJeff Roberson * If we're switching very frequently, spin while checking 27891690c6c1SJeff Roberson * for load rather than entering a low power state that 27907b55ab05SJeff Roberson * may require an IPI. However, don't do any busy 27917b55ab05SJeff Roberson * loops while on SMT machines as this simply steals 27927b55ab05SJeff Roberson * cycles from cores doing useful work. 27931690c6c1SJeff Roberson */ 279409c8a4ccSJeff Roberson if (TDQ_IDLESPIN(tdq) && switchcnt > sched_idlespinthresh) { 27951690c6c1SJeff Roberson for (i = 0; i < sched_idlespins; i++) { 27961690c6c1SJeff Roberson if (tdq->tdq_load) 27971690c6c1SJeff Roberson break; 27981690c6c1SJeff Roberson cpu_spinwait(); 27991690c6c1SJeff Roberson } 28001690c6c1SJeff Roberson } 28012c27cb3aSAlexander Motin 28022c27cb3aSAlexander Motin /* If there was context switch during spin, restart it. */ 28036c47aaaeSJeff Roberson switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 28042c27cb3aSAlexander Motin if (tdq->tdq_load != 0 || switchcnt != oldswitchcnt) 28052c27cb3aSAlexander Motin continue; 28062c27cb3aSAlexander Motin 28072c27cb3aSAlexander Motin /* Run main MD idle handler. */ 28089f9ad565SAlexander Motin tdq->tdq_cpu_idle = 1; 280979654969SAlexander Motin /* 281079654969SAlexander Motin * Make sure that tdq_cpu_idle update is globally visible 281179654969SAlexander Motin * before cpu_idle() read tdq_load. The order is important 281279654969SAlexander Motin * to avoid race with tdq_notify. 281379654969SAlexander Motin */ 2814e8677f38SKonstantin Belousov atomic_thread_fence_seq_cst(); 281597e9382dSDon Lewis /* 281697e9382dSDon Lewis * Checking for again after the fence picks up assigned 281797e9382dSDon Lewis * threads often enough to make it worthwhile to do so in 281897e9382dSDon Lewis * order to avoid calling cpu_idle(). 281997e9382dSDon Lewis */ 282097e9382dSDon Lewis if (tdq->tdq_load != 0) { 282197e9382dSDon Lewis tdq->tdq_cpu_idle = 0; 282297e9382dSDon Lewis continue; 282397e9382dSDon Lewis } 28242c27cb3aSAlexander Motin cpu_idle(switchcnt * 4 > sched_idlespinthresh); 28259f9ad565SAlexander Motin tdq->tdq_cpu_idle = 0; 28262c27cb3aSAlexander Motin 28272c27cb3aSAlexander Motin /* 28282c27cb3aSAlexander Motin * Account thread-less hardware interrupts and 28292c27cb3aSAlexander Motin * other wakeup reasons equal to context switches. 28302c27cb3aSAlexander Motin */ 28312c27cb3aSAlexander Motin switchcnt = tdq->tdq_switchcnt + tdq->tdq_oldswitchcnt; 28322c27cb3aSAlexander Motin if (switchcnt != oldswitchcnt) 28332c27cb3aSAlexander Motin continue; 28342c27cb3aSAlexander Motin tdq->tdq_switchcnt++; 28352c27cb3aSAlexander Motin oldswitchcnt++; 2836ae7a6b38SJeff Roberson } 2837b41f1452SDavid Xu } 2838e7d50326SJeff Roberson 28397b20fb19SJeff Roberson /* 28407b20fb19SJeff Roberson * A CPU is entering for the first time or a thread is exiting. 28417b20fb19SJeff Roberson */ 28427b20fb19SJeff Roberson void 28437b20fb19SJeff Roberson sched_throw(struct thread *td) 28447b20fb19SJeff Roberson { 284559c68134SJeff Roberson struct thread *newtd; 2846ae7a6b38SJeff Roberson struct tdq *tdq; 2847ae7a6b38SJeff Roberson 28481eb13fceSJeff Roberson if (__predict_false(td == NULL)) { 2849018ff686SJeff Roberson #ifdef SMP 2850018ff686SJeff Roberson PCPU_SET(sched, DPCPU_PTR(tdq)); 2851018ff686SJeff Roberson #endif 2852ae7a6b38SJeff Roberson /* Correct spinlock nesting and acquire the correct lock. */ 2853018ff686SJeff Roberson tdq = TDQ_SELF(); 2854ae7a6b38SJeff Roberson TDQ_LOCK(tdq); 28557b20fb19SJeff Roberson spinlock_exit(); 28567e3a96eaSJohn Baldwin PCPU_SET(switchtime, cpu_ticks()); 28577e3a96eaSJohn Baldwin PCPU_SET(switchticks, ticks); 2858e1504695SJeff Roberson PCPU_GET(idlethread)->td_lock = TDQ_LOCKPTR(tdq); 28597b20fb19SJeff Roberson } else { 2860018ff686SJeff Roberson tdq = TDQ_SELF(); 2861686bcb5cSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 2862686bcb5cSJeff Roberson THREAD_LOCKPTR_ASSERT(td, TDQ_LOCKPTR(tdq)); 28639727e637SJeff Roberson tdq_load_rem(tdq, td); 286492de34dfSJohn Baldwin td->td_lastcpu = td->td_oncpu; 286592de34dfSJohn Baldwin td->td_oncpu = NOCPU; 28661eb13fceSJeff Roberson thread_lock_block(td); 28677b20fb19SJeff Roberson } 286859c68134SJeff Roberson newtd = choosethread(); 2869686bcb5cSJeff Roberson spinlock_enter(); 2870686bcb5cSJeff Roberson TDQ_UNLOCK(tdq); 2871686bcb5cSJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, 2872686bcb5cSJeff Roberson ("invalid count %d", curthread->td_md.md_spinlock_count)); 28731eb13fceSJeff Roberson /* doesn't return */ 28741eb13fceSJeff Roberson if (__predict_false(td == NULL)) 287559c68134SJeff Roberson cpu_throw(td, newtd); /* doesn't return */ 28761eb13fceSJeff Roberson else 28771eb13fceSJeff Roberson cpu_switch(td, newtd, TDQ_LOCKPTR(tdq)); 28787b20fb19SJeff Roberson } 28797b20fb19SJeff Roberson 2880ae7a6b38SJeff Roberson /* 2881ae7a6b38SJeff Roberson * This is called from fork_exit(). Just acquire the correct locks and 2882ae7a6b38SJeff Roberson * let fork do the rest of the work. 2883ae7a6b38SJeff Roberson */ 28847b20fb19SJeff Roberson void 2885fe54587fSJeff Roberson sched_fork_exit(struct thread *td) 28867b20fb19SJeff Roberson { 2887ae7a6b38SJeff Roberson struct tdq *tdq; 2888ae7a6b38SJeff Roberson int cpuid; 28897b20fb19SJeff Roberson 28907b20fb19SJeff Roberson /* 28917b20fb19SJeff Roberson * Finish setting up thread glue so that it begins execution in a 2892ae7a6b38SJeff Roberson * non-nested critical section with the scheduler lock held. 28937b20fb19SJeff Roberson */ 2894686bcb5cSJeff Roberson KASSERT(curthread->td_md.md_spinlock_count == 1, 2895686bcb5cSJeff Roberson ("invalid count %d", curthread->td_md.md_spinlock_count)); 2896ae7a6b38SJeff Roberson cpuid = PCPU_GET(cpuid); 2897018ff686SJeff Roberson tdq = TDQ_SELF(); 2898686bcb5cSJeff Roberson TDQ_LOCK(tdq); 2899686bcb5cSJeff Roberson spinlock_exit(); 2900ae7a6b38SJeff Roberson MPASS(td->td_lock == TDQ_LOCKPTR(tdq)); 2901ae7a6b38SJeff Roberson td->td_oncpu = cpuid; 290228ef18b8SAndriy Gapon KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "running", 290328ef18b8SAndriy Gapon "prio:%d", td->td_priority); 290428ef18b8SAndriy Gapon SDT_PROBE0(sched, , , on__cpu); 29057b20fb19SJeff Roberson } 29067b20fb19SJeff Roberson 29078f51ad55SJeff Roberson /* 29088f51ad55SJeff Roberson * Create on first use to catch odd startup conditons. 29098f51ad55SJeff Roberson */ 29108f51ad55SJeff Roberson char * 29118f51ad55SJeff Roberson sched_tdname(struct thread *td) 29128f51ad55SJeff Roberson { 29138f51ad55SJeff Roberson #ifdef KTR 29148f51ad55SJeff Roberson struct td_sched *ts; 29158f51ad55SJeff Roberson 291693ccd6bfSKonstantin Belousov ts = td_get_sched(td); 29178f51ad55SJeff Roberson if (ts->ts_name[0] == '\0') 29188f51ad55SJeff Roberson snprintf(ts->ts_name, sizeof(ts->ts_name), 29198f51ad55SJeff Roberson "%s tid %d", td->td_name, td->td_tid); 29208f51ad55SJeff Roberson return (ts->ts_name); 29218f51ad55SJeff Roberson #else 29228f51ad55SJeff Roberson return (td->td_name); 29238f51ad55SJeff Roberson #endif 29248f51ad55SJeff Roberson } 29258f51ad55SJeff Roberson 292644ad5475SJohn Baldwin #ifdef KTR 292744ad5475SJohn Baldwin void 292844ad5475SJohn Baldwin sched_clear_tdname(struct thread *td) 292944ad5475SJohn Baldwin { 293044ad5475SJohn Baldwin struct td_sched *ts; 293144ad5475SJohn Baldwin 293293ccd6bfSKonstantin Belousov ts = td_get_sched(td); 293344ad5475SJohn Baldwin ts->ts_name[0] = '\0'; 293444ad5475SJohn Baldwin } 293544ad5475SJohn Baldwin #endif 293644ad5475SJohn Baldwin 293707095abfSIvan Voras #ifdef SMP 293807095abfSIvan Voras 293907095abfSIvan Voras /* 294007095abfSIvan Voras * Build the CPU topology dump string. Is recursively called to collect 294107095abfSIvan Voras * the topology tree. 294207095abfSIvan Voras */ 294307095abfSIvan Voras static int 294407095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(struct sbuf *sb, struct cpu_group *cg, 294507095abfSIvan Voras int indent) 294607095abfSIvan Voras { 294771a19bdcSAttilio Rao char cpusetbuf[CPUSETBUFSIZ]; 294807095abfSIvan Voras int i, first; 294907095abfSIvan Voras 295007095abfSIvan Voras sbuf_printf(sb, "%*s<group level=\"%d\" cache-level=\"%d\">\n", indent, 295119b8a6dbSAndriy Gapon "", 1 + indent / 2, cg->cg_level); 295271a19bdcSAttilio Rao sbuf_printf(sb, "%*s <cpu count=\"%d\" mask=\"%s\">", indent, "", 295371a19bdcSAttilio Rao cg->cg_count, cpusetobj_strprint(cpusetbuf, &cg->cg_mask)); 295407095abfSIvan Voras first = TRUE; 2955*aefe0a8cSAlexander Motin for (i = cg->cg_first; i <= cg->cg_last; i++) { 295671a19bdcSAttilio Rao if (CPU_ISSET(i, &cg->cg_mask)) { 295707095abfSIvan Voras if (!first) 295807095abfSIvan Voras sbuf_printf(sb, ", "); 295907095abfSIvan Voras else 296007095abfSIvan Voras first = FALSE; 296107095abfSIvan Voras sbuf_printf(sb, "%d", i); 296207095abfSIvan Voras } 296307095abfSIvan Voras } 296407095abfSIvan Voras sbuf_printf(sb, "</cpu>\n"); 296507095abfSIvan Voras 296607095abfSIvan Voras if (cg->cg_flags != 0) { 2967611daf7eSIvan Voras sbuf_printf(sb, "%*s <flags>", indent, ""); 296807095abfSIvan Voras if ((cg->cg_flags & CG_FLAG_HTT) != 0) 29695368befbSIvan Voras sbuf_printf(sb, "<flag name=\"HTT\">HTT group</flag>"); 2970a401f2d0SIvan Voras if ((cg->cg_flags & CG_FLAG_THREAD) != 0) 2971a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"THREAD\">THREAD group</flag>"); 29727b55ab05SJeff Roberson if ((cg->cg_flags & CG_FLAG_SMT) != 0) 2973a401f2d0SIvan Voras sbuf_printf(sb, "<flag name=\"SMT\">SMT group</flag>"); 297407095abfSIvan Voras sbuf_printf(sb, "</flags>\n"); 2975611daf7eSIvan Voras } 297607095abfSIvan Voras 297707095abfSIvan Voras if (cg->cg_children > 0) { 297807095abfSIvan Voras sbuf_printf(sb, "%*s <children>\n", indent, ""); 297907095abfSIvan Voras for (i = 0; i < cg->cg_children; i++) 298007095abfSIvan Voras sysctl_kern_sched_topology_spec_internal(sb, 298107095abfSIvan Voras &cg->cg_child[i], indent+2); 298207095abfSIvan Voras sbuf_printf(sb, "%*s </children>\n", indent, ""); 298307095abfSIvan Voras } 298407095abfSIvan Voras sbuf_printf(sb, "%*s</group>\n", indent, ""); 298507095abfSIvan Voras return (0); 298607095abfSIvan Voras } 298707095abfSIvan Voras 298807095abfSIvan Voras /* 298907095abfSIvan Voras * Sysctl handler for retrieving topology dump. It's a wrapper for 299007095abfSIvan Voras * the recursive sysctl_kern_smp_topology_spec_internal(). 299107095abfSIvan Voras */ 299207095abfSIvan Voras static int 299307095abfSIvan Voras sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS) 299407095abfSIvan Voras { 299507095abfSIvan Voras struct sbuf *topo; 299607095abfSIvan Voras int err; 299707095abfSIvan Voras 299807095abfSIvan Voras KASSERT(cpu_top != NULL, ("cpu_top isn't initialized")); 299907095abfSIvan Voras 3000b97fa22cSIan Lepore topo = sbuf_new_for_sysctl(NULL, NULL, 512, req); 300107095abfSIvan Voras if (topo == NULL) 300207095abfSIvan Voras return (ENOMEM); 300307095abfSIvan Voras 300407095abfSIvan Voras sbuf_printf(topo, "<groups>\n"); 300507095abfSIvan Voras err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1); 300607095abfSIvan Voras sbuf_printf(topo, "</groups>\n"); 300707095abfSIvan Voras 300807095abfSIvan Voras if (err == 0) { 3009b97fa22cSIan Lepore err = sbuf_finish(topo); 301007095abfSIvan Voras } 301107095abfSIvan Voras sbuf_delete(topo); 301207095abfSIvan Voras return (err); 301307095abfSIvan Voras } 3014b67cc292SDavid Xu 301507095abfSIvan Voras #endif 301607095abfSIvan Voras 3017579895dfSAlexander Motin static int 3018579895dfSAlexander Motin sysctl_kern_quantum(SYSCTL_HANDLER_ARGS) 3019579895dfSAlexander Motin { 3020579895dfSAlexander Motin int error, new_val, period; 3021579895dfSAlexander Motin 3022579895dfSAlexander Motin period = 1000000 / realstathz; 3023579895dfSAlexander Motin new_val = period * sched_slice; 3024579895dfSAlexander Motin error = sysctl_handle_int(oidp, &new_val, 0, req); 3025579895dfSAlexander Motin if (error != 0 || req->newptr == NULL) 3026579895dfSAlexander Motin return (error); 3027579895dfSAlexander Motin if (new_val <= 0) 3028579895dfSAlexander Motin return (EINVAL); 302937f4e025SAlexander Motin sched_slice = imax(1, (new_val + period / 2) / period); 30305e5c3873SJeff Roberson sched_slice_min = sched_slice / SCHED_SLICE_MIN_DIVISOR; 303137f4e025SAlexander Motin hogticks = imax(1, (2 * hz * sched_slice + realstathz / 2) / 303237f4e025SAlexander Motin realstathz); 3033579895dfSAlexander Motin return (0); 3034579895dfSAlexander Motin } 3035579895dfSAlexander Motin 30367029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 30377029da5cSPawel Biernacki "Scheduler"); 3038ae7a6b38SJeff Roberson SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0, 3039e7d50326SJeff Roberson "Scheduler name"); 30407029da5cSPawel Biernacki SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, 30417029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0, 30427029da5cSPawel Biernacki sysctl_kern_quantum, "I", 304337f4e025SAlexander Motin "Quantum for timeshare threads in microseconds"); 3044ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, 304537f4e025SAlexander Motin "Quantum for timeshare threads in stathz ticks"); 3046ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, 3047ae7a6b38SJeff Roberson "Interactivity score threshold"); 304837f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, 304937f4e025SAlexander Motin &preempt_thresh, 0, 305037f4e025SAlexander Motin "Maximal (lowest) priority for preemption"); 305137f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, static_boost, CTLFLAG_RW, &static_boost, 0, 305237f4e025SAlexander Motin "Assign static kernel priorities to sleeping threads"); 305337f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, idlespins, CTLFLAG_RW, &sched_idlespins, 0, 305437f4e025SAlexander Motin "Number of times idle thread will spin waiting for new work"); 305537f4e025SAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW, 305637f4e025SAlexander Motin &sched_idlespinthresh, 0, 305737f4e025SAlexander Motin "Threshold before we will permit idle thread spinning"); 30587b8bfa0dSJeff Roberson #ifdef SMP 3059ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0, 3060ae7a6b38SJeff Roberson "Number of hz ticks to keep thread affinity for"); 3061ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, 3062ae7a6b38SJeff Roberson "Enables the long-term load balancer"); 30637fcf154aSJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW, 30647fcf154aSJeff Roberson &balance_interval, 0, 3065579895dfSAlexander Motin "Average period in stathz ticks to run the long-term balancer"); 3066ae7a6b38SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0, 3067ae7a6b38SJeff Roberson "Attempts to steal work from other cores before idling"); 306828994a58SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0, 306937f4e025SAlexander Motin "Minimum load on remote CPU before we'll steal"); 307097e9382dSDon Lewis SYSCTL_INT(_kern_sched, OID_AUTO, trysteal_limit, CTLFLAG_RW, &trysteal_limit, 307197e9382dSDon Lewis 0, "Topological distance limit for stealing threads in sched_switch()"); 307297e9382dSDon Lewis SYSCTL_INT(_kern_sched, OID_AUTO, always_steal, CTLFLAG_RW, &always_steal, 0, 307397e9382dSDon Lewis "Always run the stealer from the idle thread"); 307407095abfSIvan Voras SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING | 3075c69a1a50SMateusz Guzik CTLFLAG_MPSAFE | CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec, "A", 307607095abfSIvan Voras "XML dump of detected CPU topology"); 30777b8bfa0dSJeff Roberson #endif 3078e7d50326SJeff Roberson 307954b0e65fSJeff Roberson /* ps compat. All cpu percentages from ULE are weighted. */ 3080a5423ea3SJeff Roberson static int ccpu = 0; 3081b05ca429SPawel Biernacki SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, 3082b05ca429SPawel Biernacki "Decay factor used for updating %CPU in 4BSD scheduler"); 3083