sched_4bsd.c revision 113339
1104964Sjeff/*- 2104964Sjeff * Copyright (c) 1982, 1986, 1990, 1991, 1993 3104964Sjeff * The Regents of the University of California. All rights reserved. 4104964Sjeff * (c) UNIX System Laboratories, Inc. 5104964Sjeff * All or some portions of this file are derived from material licensed 6104964Sjeff * to the University of California by American Telephone and Telegraph 7104964Sjeff * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8104964Sjeff * the permission of UNIX System Laboratories, Inc. 9104964Sjeff * 10104964Sjeff * Redistribution and use in source and binary forms, with or without 11104964Sjeff * modification, are permitted provided that the following conditions 12104964Sjeff * are met: 13104964Sjeff * 1. Redistributions of source code must retain the above copyright 14104964Sjeff * notice, this list of conditions and the following disclaimer. 15104964Sjeff * 2. Redistributions in binary form must reproduce the above copyright 16104964Sjeff * notice, this list of conditions and the following disclaimer in the 17104964Sjeff * documentation and/or other materials provided with the distribution. 18104964Sjeff * 3. All advertising materials mentioning features or use of this software 19104964Sjeff * must display the following acknowledgement: 20104964Sjeff * This product includes software developed by the University of 21104964Sjeff * California, Berkeley and its contributors. 22104964Sjeff * 4. Neither the name of the University nor the names of its contributors 23104964Sjeff * may be used to endorse or promote products derived from this software 24104964Sjeff * without specific prior written permission. 25104964Sjeff * 26104964Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27104964Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28104964Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29104964Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30104964Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31104964Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32104964Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33104964Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34104964Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35104964Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36104964Sjeff * SUCH DAMAGE. 37104964Sjeff * 38104964Sjeff * $FreeBSD: head/sys/kern/sched_4bsd.c 113339 2003-04-10 17:35:44Z julian $ 39104964Sjeff */ 40104964Sjeff 41104964Sjeff#include <sys/param.h> 42104964Sjeff#include <sys/systm.h> 43104964Sjeff#include <sys/kernel.h> 44104964Sjeff#include <sys/ktr.h> 45104964Sjeff#include <sys/lock.h> 46104964Sjeff#include <sys/mutex.h> 47104964Sjeff#include <sys/proc.h> 48104964Sjeff#include <sys/resourcevar.h> 49104964Sjeff#include <sys/sched.h> 50104964Sjeff#include <sys/smp.h> 51104964Sjeff#include <sys/sysctl.h> 52104964Sjeff#include <sys/sx.h> 53104964Sjeff 54107135Sjeff/* 55107135Sjeff * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in 56107135Sjeff * the range 100-256 Hz (approximately). 57107135Sjeff */ 58107135Sjeff#define ESTCPULIM(e) \ 59107135Sjeff min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \ 60107135Sjeff RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1) 61107135Sjeff#define INVERSE_ESTCPU_WEIGHT 8 /* 1 / (priorities per estcpu level). */ 62107135Sjeff#define NICE_WEIGHT 1 /* Priorities per nice level. */ 63107135Sjeff 64109145Sjeffstruct ke_sched { 65109145Sjeff int ske_cpticks; /* (j) Ticks of cpu time. */ 66109145Sjeff}; 67109145Sjeff 68109145Sjeffstruct ke_sched ke_sched; 69109145Sjeff 70109145Sjeffstruct ke_sched *kse0_sched = &ke_sched; 71107126Sjeffstruct kg_sched *ksegrp0_sched = NULL; 72107126Sjeffstruct p_sched *proc0_sched = NULL; 73107126Sjeffstruct td_sched *thread0_sched = NULL; 74104964Sjeff 75104964Sjeffstatic int sched_quantum; /* Roundrobin scheduling quantum in ticks. */ 76112535Smux#define SCHED_QUANTUM (hz / 10) /* Default sched quantum */ 77104964Sjeff 78104964Sjeffstatic struct callout schedcpu_callout; 79104964Sjeffstatic struct callout roundrobin_callout; 80104964Sjeff 81104964Sjeffstatic void roundrobin(void *arg); 82104964Sjeffstatic void schedcpu(void *arg); 83104964Sjeffstatic void sched_setup(void *dummy); 84104964Sjeffstatic void maybe_resched(struct thread *td); 85104964Sjeffstatic void updatepri(struct ksegrp *kg); 86104964Sjeffstatic void resetpriority(struct ksegrp *kg); 87104964Sjeff 88104964SjeffSYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL) 89104964Sjeff 90104964Sjeff/* 91104964Sjeff * Global run queue. 92104964Sjeff */ 93104964Sjeffstatic struct runq runq; 94104964SjeffSYSINIT(runq, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, runq_init, &runq) 95104964Sjeff 96104964Sjeffstatic int 97104964Sjeffsysctl_kern_quantum(SYSCTL_HANDLER_ARGS) 98104964Sjeff{ 99104964Sjeff int error, new_val; 100104964Sjeff 101104964Sjeff new_val = sched_quantum * tick; 102104964Sjeff error = sysctl_handle_int(oidp, &new_val, 0, req); 103104964Sjeff if (error != 0 || req->newptr == NULL) 104104964Sjeff return (error); 105104964Sjeff if (new_val < tick) 106104964Sjeff return (EINVAL); 107104964Sjeff sched_quantum = new_val / tick; 108104964Sjeff hogticks = 2 * sched_quantum; 109104964Sjeff return (0); 110104964Sjeff} 111104964Sjeff 112104964SjeffSYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW, 113104964Sjeff 0, sizeof sched_quantum, sysctl_kern_quantum, "I", 114104964Sjeff "Roundrobin scheduling quantum in microseconds"); 115104964Sjeff 116104964Sjeff/* 117104964Sjeff * Arrange to reschedule if necessary, taking the priorities and 118104964Sjeff * schedulers into account. 119104964Sjeff */ 120104964Sjeffstatic void 121104964Sjeffmaybe_resched(struct thread *td) 122104964Sjeff{ 123104964Sjeff 124104964Sjeff mtx_assert(&sched_lock, MA_OWNED); 125108338Sjulian if (td->td_priority < curthread->td_priority && curthread->td_kse) 126111032Sjulian curthread->td_flags |= TDF_NEEDRESCHED; 127104964Sjeff} 128104964Sjeff 129104964Sjeff/* 130104964Sjeff * Force switch among equal priority processes every 100ms. 131104964Sjeff * We don't actually need to force a context switch of the current process. 132104964Sjeff * The act of firing the event triggers a context switch to softclock() and 133104964Sjeff * then switching back out again which is equivalent to a preemption, thus 134104964Sjeff * no further work is needed on the local CPU. 135104964Sjeff */ 136104964Sjeff/* ARGSUSED */ 137104964Sjeffstatic void 138104964Sjeffroundrobin(void *arg) 139104964Sjeff{ 140104964Sjeff 141104964Sjeff#ifdef SMP 142104964Sjeff mtx_lock_spin(&sched_lock); 143104964Sjeff forward_roundrobin(); 144104964Sjeff mtx_unlock_spin(&sched_lock); 145104964Sjeff#endif 146104964Sjeff 147104964Sjeff callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL); 148104964Sjeff} 149104964Sjeff 150104964Sjeff/* 151104964Sjeff * Constants for digital decay and forget: 152104964Sjeff * 90% of (p_estcpu) usage in 5 * loadav time 153104964Sjeff * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 154104964Sjeff * Note that, as ps(1) mentions, this can let percentages 155104964Sjeff * total over 100% (I've seen 137.9% for 3 processes). 156104964Sjeff * 157104964Sjeff * Note that schedclock() updates p_estcpu and p_cpticks asynchronously. 158104964Sjeff * 159104964Sjeff * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds. 160104964Sjeff * That is, the system wants to compute a value of decay such 161104964Sjeff * that the following for loop: 162104964Sjeff * for (i = 0; i < (5 * loadavg); i++) 163104964Sjeff * p_estcpu *= decay; 164104964Sjeff * will compute 165104964Sjeff * p_estcpu *= 0.1; 166104964Sjeff * for all values of loadavg: 167104964Sjeff * 168104964Sjeff * Mathematically this loop can be expressed by saying: 169104964Sjeff * decay ** (5 * loadavg) ~= .1 170104964Sjeff * 171104964Sjeff * The system computes decay as: 172104964Sjeff * decay = (2 * loadavg) / (2 * loadavg + 1) 173104964Sjeff * 174104964Sjeff * We wish to prove that the system's computation of decay 175104964Sjeff * will always fulfill the equation: 176104964Sjeff * decay ** (5 * loadavg) ~= .1 177104964Sjeff * 178104964Sjeff * If we compute b as: 179104964Sjeff * b = 2 * loadavg 180104964Sjeff * then 181104964Sjeff * decay = b / (b + 1) 182104964Sjeff * 183104964Sjeff * We now need to prove two things: 184104964Sjeff * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 185104964Sjeff * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 186104964Sjeff * 187104964Sjeff * Facts: 188104964Sjeff * For x close to zero, exp(x) =~ 1 + x, since 189104964Sjeff * exp(x) = 0! + x**1/1! + x**2/2! + ... . 190104964Sjeff * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 191104964Sjeff * For x close to zero, ln(1+x) =~ x, since 192104964Sjeff * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 193104964Sjeff * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 194104964Sjeff * ln(.1) =~ -2.30 195104964Sjeff * 196104964Sjeff * Proof of (1): 197104964Sjeff * Solve (factor)**(power) =~ .1 given power (5*loadav): 198104964Sjeff * solving for factor, 199104964Sjeff * ln(factor) =~ (-2.30/5*loadav), or 200104964Sjeff * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 201104964Sjeff * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 202104964Sjeff * 203104964Sjeff * Proof of (2): 204104964Sjeff * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 205104964Sjeff * solving for power, 206104964Sjeff * power*ln(b/(b+1)) =~ -2.30, or 207104964Sjeff * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 208104964Sjeff * 209104964Sjeff * Actual power values for the implemented algorithm are as follows: 210104964Sjeff * loadav: 1 2 3 4 211104964Sjeff * power: 5.68 10.32 14.94 19.55 212104964Sjeff */ 213104964Sjeff 214104964Sjeff/* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 215104964Sjeff#define loadfactor(loadav) (2 * (loadav)) 216104964Sjeff#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 217104964Sjeff 218104964Sjeff/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 219104964Sjeffstatic fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 220104964SjeffSYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 221104964Sjeff 222104964Sjeff/* 223104964Sjeff * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 224104964Sjeff * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 225104964Sjeff * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 226104964Sjeff * 227104964Sjeff * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 228104964Sjeff * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 229104964Sjeff * 230104964Sjeff * If you don't want to bother with the faster/more-accurate formula, you 231104964Sjeff * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 232104964Sjeff * (more general) method of calculating the %age of CPU used by a process. 233104964Sjeff */ 234104964Sjeff#define CCPU_SHIFT 11 235104964Sjeff 236104964Sjeff/* 237104964Sjeff * Recompute process priorities, every hz ticks. 238104964Sjeff * MP-safe, called without the Giant mutex. 239104964Sjeff */ 240104964Sjeff/* ARGSUSED */ 241104964Sjeffstatic void 242104964Sjeffschedcpu(void *arg) 243104964Sjeff{ 244104964Sjeff register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 245104964Sjeff struct thread *td; 246104964Sjeff struct proc *p; 247104964Sjeff struct kse *ke; 248104964Sjeff struct ksegrp *kg; 249104964Sjeff int realstathz; 250104964Sjeff int awake; 251104964Sjeff 252104964Sjeff realstathz = stathz ? stathz : hz; 253104964Sjeff sx_slock(&allproc_lock); 254104964Sjeff FOREACH_PROC_IN_SYSTEM(p) { 255104964Sjeff mtx_lock_spin(&sched_lock); 256104964Sjeff p->p_swtime++; 257104964Sjeff FOREACH_KSEGRP_IN_PROC(p, kg) { 258104964Sjeff awake = 0; 259104964Sjeff FOREACH_KSE_IN_GROUP(kg, ke) { 260104964Sjeff /* 261104964Sjeff * Increment time in/out of memory and sleep 262104964Sjeff * time (if sleeping). We ignore overflow; 263104964Sjeff * with 16-bit int's (remember them?) 264104964Sjeff * overflow takes 45 days. 265104964Sjeff */ 266104964Sjeff /* 267104964Sjeff * The kse slptimes are not touched in wakeup 268104964Sjeff * because the thread may not HAVE a KSE. 269104964Sjeff */ 270104964Sjeff if (ke->ke_state == KES_ONRUNQ) { 271104964Sjeff awake = 1; 272104964Sjeff ke->ke_flags &= ~KEF_DIDRUN; 273104964Sjeff } else if ((ke->ke_state == KES_THREAD) && 274104964Sjeff (TD_IS_RUNNING(ke->ke_thread))) { 275104964Sjeff awake = 1; 276104964Sjeff /* Do not clear KEF_DIDRUN */ 277104964Sjeff } else if (ke->ke_flags & KEF_DIDRUN) { 278104964Sjeff awake = 1; 279104964Sjeff ke->ke_flags &= ~KEF_DIDRUN; 280104964Sjeff } 281104964Sjeff 282104964Sjeff /* 283104964Sjeff * pctcpu is only for ps? 284104964Sjeff * Do it per kse.. and add them up at the end? 285104964Sjeff * XXXKSE 286104964Sjeff */ 287109157Sjeff ke->ke_pctcpu 288109157Sjeff = (ke->ke_pctcpu * ccpu) >> 289109145Sjeff FSHIFT; 290104964Sjeff /* 291104964Sjeff * If the kse has been idle the entire second, 292104964Sjeff * stop recalculating its priority until 293104964Sjeff * it wakes up. 294104964Sjeff */ 295109145Sjeff if (ke->ke_sched->ske_cpticks == 0) 296104964Sjeff continue; 297104964Sjeff#if (FSHIFT >= CCPU_SHIFT) 298109157Sjeff ke->ke_pctcpu += (realstathz == 100) 299109145Sjeff ? ((fixpt_t) ke->ke_sched->ske_cpticks) << 300104964Sjeff (FSHIFT - CCPU_SHIFT) : 301109145Sjeff 100 * (((fixpt_t) ke->ke_sched->ske_cpticks) 302109145Sjeff << (FSHIFT - CCPU_SHIFT)) / realstathz; 303104964Sjeff#else 304109157Sjeff ke->ke_pctcpu += ((FSCALE - ccpu) * 305109145Sjeff (ke->ke_sched->ske_cpticks * 306109145Sjeff FSCALE / realstathz)) >> FSHIFT; 307104964Sjeff#endif 308109145Sjeff ke->ke_sched->ske_cpticks = 0; 309104964Sjeff } /* end of kse loop */ 310104964Sjeff /* 311104964Sjeff * If there are ANY running threads in this KSEGRP, 312104964Sjeff * then don't count it as sleeping. 313104964Sjeff */ 314104964Sjeff if (awake) { 315104964Sjeff if (kg->kg_slptime > 1) { 316104964Sjeff /* 317104964Sjeff * In an ideal world, this should not 318104964Sjeff * happen, because whoever woke us 319104964Sjeff * up from the long sleep should have 320104964Sjeff * unwound the slptime and reset our 321104964Sjeff * priority before we run at the stale 322104964Sjeff * priority. Should KASSERT at some 323104964Sjeff * point when all the cases are fixed. 324104964Sjeff */ 325104964Sjeff updatepri(kg); 326104964Sjeff } 327104964Sjeff kg->kg_slptime = 0; 328104964Sjeff } else { 329104964Sjeff kg->kg_slptime++; 330104964Sjeff } 331104964Sjeff if (kg->kg_slptime > 1) 332104964Sjeff continue; 333104964Sjeff kg->kg_estcpu = decay_cpu(loadfac, kg->kg_estcpu); 334104964Sjeff resetpriority(kg); 335104964Sjeff FOREACH_THREAD_IN_GROUP(kg, td) { 336104964Sjeff if (td->td_priority >= PUSER) { 337105127Sjulian sched_prio(td, kg->kg_user_pri); 338104964Sjeff } 339104964Sjeff } 340104964Sjeff } /* end of ksegrp loop */ 341104964Sjeff mtx_unlock_spin(&sched_lock); 342104964Sjeff } /* end of process loop */ 343104964Sjeff sx_sunlock(&allproc_lock); 344104964Sjeff callout_reset(&schedcpu_callout, hz, schedcpu, NULL); 345104964Sjeff} 346104964Sjeff 347104964Sjeff/* 348104964Sjeff * Recalculate the priority of a process after it has slept for a while. 349104964Sjeff * For all load averages >= 1 and max p_estcpu of 255, sleeping for at 350104964Sjeff * least six times the loadfactor will decay p_estcpu to zero. 351104964Sjeff */ 352104964Sjeffstatic void 353104964Sjeffupdatepri(struct ksegrp *kg) 354104964Sjeff{ 355104964Sjeff register unsigned int newcpu; 356104964Sjeff register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 357104964Sjeff 358104964Sjeff newcpu = kg->kg_estcpu; 359104964Sjeff if (kg->kg_slptime > 5 * loadfac) 360104964Sjeff kg->kg_estcpu = 0; 361104964Sjeff else { 362104964Sjeff kg->kg_slptime--; /* the first time was done in schedcpu */ 363104964Sjeff while (newcpu && --kg->kg_slptime) 364104964Sjeff newcpu = decay_cpu(loadfac, newcpu); 365104964Sjeff kg->kg_estcpu = newcpu; 366104964Sjeff } 367104964Sjeff resetpriority(kg); 368104964Sjeff} 369104964Sjeff 370104964Sjeff/* 371104964Sjeff * Compute the priority of a process when running in user mode. 372104964Sjeff * Arrange to reschedule if the resulting priority is better 373104964Sjeff * than that of the current process. 374104964Sjeff */ 375104964Sjeffstatic void 376104964Sjeffresetpriority(struct ksegrp *kg) 377104964Sjeff{ 378104964Sjeff register unsigned int newpriority; 379104964Sjeff struct thread *td; 380104964Sjeff 381104964Sjeff mtx_lock_spin(&sched_lock); 382104964Sjeff if (kg->kg_pri_class == PRI_TIMESHARE) { 383104964Sjeff newpriority = PUSER + kg->kg_estcpu / INVERSE_ESTCPU_WEIGHT + 384104964Sjeff NICE_WEIGHT * (kg->kg_nice - PRIO_MIN); 385104964Sjeff newpriority = min(max(newpriority, PRI_MIN_TIMESHARE), 386104964Sjeff PRI_MAX_TIMESHARE); 387104964Sjeff kg->kg_user_pri = newpriority; 388104964Sjeff } 389104964Sjeff FOREACH_THREAD_IN_GROUP(kg, td) { 390104964Sjeff maybe_resched(td); /* XXXKSE silly */ 391104964Sjeff } 392104964Sjeff mtx_unlock_spin(&sched_lock); 393104964Sjeff} 394104964Sjeff 395104964Sjeff/* ARGSUSED */ 396104964Sjeffstatic void 397104964Sjeffsched_setup(void *dummy) 398104964Sjeff{ 399104964Sjeff if (sched_quantum == 0) 400104964Sjeff sched_quantum = SCHED_QUANTUM; 401104964Sjeff hogticks = 2 * sched_quantum; 402104964Sjeff 403104964Sjeff callout_init(&schedcpu_callout, 1); 404104964Sjeff callout_init(&roundrobin_callout, 0); 405104964Sjeff 406104964Sjeff /* Kick off timeout driven events by calling first time. */ 407104964Sjeff roundrobin(NULL); 408104964Sjeff schedcpu(NULL); 409104964Sjeff} 410104964Sjeff 411104964Sjeff/* External interfaces start here */ 412104964Sjeffint 413104964Sjeffsched_runnable(void) 414104964Sjeff{ 415104964Sjeff return runq_check(&runq); 416104964Sjeff} 417104964Sjeff 418104964Sjeffint 419104964Sjeffsched_rr_interval(void) 420104964Sjeff{ 421104964Sjeff if (sched_quantum == 0) 422104964Sjeff sched_quantum = SCHED_QUANTUM; 423104964Sjeff return (sched_quantum); 424104964Sjeff} 425104964Sjeff 426104964Sjeff/* 427104964Sjeff * We adjust the priority of the current process. The priority of 428104964Sjeff * a process gets worse as it accumulates CPU time. The cpu usage 429104964Sjeff * estimator (p_estcpu) is increased here. resetpriority() will 430104964Sjeff * compute a different priority each time p_estcpu increases by 431104964Sjeff * INVERSE_ESTCPU_WEIGHT 432104964Sjeff * (until MAXPRI is reached). The cpu usage estimator ramps up 433104964Sjeff * quite quickly when the process is running (linearly), and decays 434104964Sjeff * away exponentially, at a rate which is proportionally slower when 435104964Sjeff * the system is busy. The basic principle is that the system will 436104964Sjeff * 90% forget that the process used a lot of CPU time in 5 * loadav 437104964Sjeff * seconds. This causes the system to favor processes which haven't 438104964Sjeff * run much recently, and to round-robin among other processes. 439104964Sjeff */ 440104964Sjeffvoid 441104964Sjeffsched_clock(struct thread *td) 442104964Sjeff{ 443104964Sjeff struct kse *ke; 444104964Sjeff struct ksegrp *kg; 445104964Sjeff 446104964Sjeff KASSERT((td != NULL), ("schedclock: null thread pointer")); 447104964Sjeff ke = td->td_kse; 448104964Sjeff kg = td->td_ksegrp; 449109145Sjeff ke->ke_sched->ske_cpticks++; 450104964Sjeff kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + 1); 451104964Sjeff if ((kg->kg_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) { 452104964Sjeff resetpriority(kg); 453104964Sjeff if (td->td_priority >= PUSER) 454104964Sjeff td->td_priority = kg->kg_user_pri; 455104964Sjeff } 456104964Sjeff} 457104964Sjeff/* 458104964Sjeff * charge childs scheduling cpu usage to parent. 459104964Sjeff * 460104964Sjeff * XXXKSE assume only one thread & kse & ksegrp keep estcpu in each ksegrp. 461104964Sjeff * Charge it to the ksegrp that did the wait since process estcpu is sum of 462104964Sjeff * all ksegrps, this is strictly as expected. Assume that the child process 463104964Sjeff * aggregated all the estcpu into the 'built-in' ksegrp. 464104964Sjeff */ 465104964Sjeffvoid 466104964Sjeffsched_exit(struct ksegrp *kg, struct ksegrp *child) 467104964Sjeff{ 468104964Sjeff kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + child->kg_estcpu); 469104964Sjeff} 470104964Sjeff 471104964Sjeffvoid 472104964Sjeffsched_fork(struct ksegrp *kg, struct ksegrp *child) 473104964Sjeff{ 474109145Sjeff struct kse *ke; 475109145Sjeff 476104964Sjeff /* 477104964Sjeff * set priority of child to be that of parent. 478104964Sjeff * XXXKSE this needs redefining.. 479104964Sjeff */ 480104964Sjeff child->kg_estcpu = kg->kg_estcpu; 481109145Sjeff 482109145Sjeff /* Set up scheduler specific data */ 483109145Sjeff ke = FIRST_KSE_IN_KSEGRP(kg); 484109145Sjeff ke->ke_sched->ske_cpticks = 0; 485104964Sjeff} 486104964Sjeff 487104964Sjeffvoid 488104964Sjeffsched_nice(struct ksegrp *kg, int nice) 489104964Sjeff{ 490104964Sjeff kg->kg_nice = nice; 491104964Sjeff resetpriority(kg); 492104964Sjeff} 493104964Sjeff 494105127Sjulian/* 495105127Sjulian * Adjust the priority of a thread. 496105127Sjulian * This may include moving the thread within the KSEGRP, 497105127Sjulian * changing the assignment of a kse to the thread, 498105127Sjulian * and moving a KSE in the system run queue. 499105127Sjulian */ 500104964Sjeffvoid 501104964Sjeffsched_prio(struct thread *td, u_char prio) 502104964Sjeff{ 503104964Sjeff 504104964Sjeff if (TD_ON_RUNQ(td)) { 505105127Sjulian adjustrunqueue(td, prio); 506105127Sjulian } else { 507105127Sjulian td->td_priority = prio; 508104964Sjeff } 509104964Sjeff} 510104964Sjeff 511104964Sjeffvoid 512104964Sjeffsched_sleep(struct thread *td, u_char prio) 513104964Sjeff{ 514104964Sjeff td->td_ksegrp->kg_slptime = 0; 515104964Sjeff td->td_priority = prio; 516104964Sjeff} 517104964Sjeff 518104964Sjeffvoid 519104964Sjeffsched_switchin(struct thread *td) 520104964Sjeff{ 521113339Sjulian td->td_oncpu = PCPU_GET(cpuid); 522104964Sjeff} 523104964Sjeff 524104964Sjeffvoid 525104964Sjeffsched_switchout(struct thread *td) 526104964Sjeff{ 527104964Sjeff struct kse *ke; 528104964Sjeff struct proc *p; 529104964Sjeff 530104964Sjeff ke = td->td_kse; 531104964Sjeff p = td->td_proc; 532104964Sjeff 533104964Sjeff KASSERT((ke->ke_state == KES_THREAD), ("mi_switch: kse state?")); 534104964Sjeff 535113339Sjulian td->td_lastcpu = td->td_oncpu; 536105127Sjulian td->td_last_kse = ke; 537113339Sjulian td->td_oncpu = NOCPU; 538111032Sjulian td->td_flags &= ~TDF_NEEDRESCHED; 539104964Sjeff /* 540104964Sjeff * At the last moment, if this thread is still marked RUNNING, 541104964Sjeff * then put it back on the run queue as it has not been suspended 542104964Sjeff * or stopped or any thing else similar. 543104964Sjeff */ 544104964Sjeff if (TD_IS_RUNNING(td)) { 545104964Sjeff /* Put us back on the run queue (kse and all). */ 546104964Sjeff setrunqueue(td); 547111585Sjulian } else if (p->p_flag & P_THREADED) { 548104964Sjeff /* 549104964Sjeff * We will not be on the run queue. So we must be 550104964Sjeff * sleeping or similar. As it's available, 551104964Sjeff * someone else can use the KSE if they need it. 552104964Sjeff */ 553104964Sjeff kse_reassign(ke); 554104964Sjeff } 555104964Sjeff} 556104964Sjeff 557104964Sjeffvoid 558104964Sjeffsched_wakeup(struct thread *td) 559104964Sjeff{ 560104964Sjeff struct ksegrp *kg; 561104964Sjeff 562104964Sjeff kg = td->td_ksegrp; 563104964Sjeff if (kg->kg_slptime > 1) 564104964Sjeff updatepri(kg); 565104964Sjeff kg->kg_slptime = 0; 566104964Sjeff setrunqueue(td); 567104964Sjeff maybe_resched(td); 568104964Sjeff} 569104964Sjeff 570104964Sjeffvoid 571104964Sjeffsched_add(struct kse *ke) 572104964Sjeff{ 573104964Sjeff mtx_assert(&sched_lock, MA_OWNED); 574104964Sjeff KASSERT((ke->ke_thread != NULL), ("runq_add: No thread on KSE")); 575104964Sjeff KASSERT((ke->ke_thread->td_kse != NULL), 576104964Sjeff ("runq_add: No KSE on thread")); 577104964Sjeff KASSERT(ke->ke_state != KES_ONRUNQ, 578104964Sjeff ("runq_add: kse %p (%s) already in run queue", ke, 579104964Sjeff ke->ke_proc->p_comm)); 580104964Sjeff KASSERT(ke->ke_proc->p_sflag & PS_INMEM, 581104964Sjeff ("runq_add: process swapped out")); 582104964Sjeff ke->ke_ksegrp->kg_runq_kses++; 583104964Sjeff ke->ke_state = KES_ONRUNQ; 584104964Sjeff 585104964Sjeff runq_add(&runq, ke); 586104964Sjeff} 587104964Sjeff 588104964Sjeffvoid 589104964Sjeffsched_rem(struct kse *ke) 590104964Sjeff{ 591104964Sjeff KASSERT(ke->ke_proc->p_sflag & PS_INMEM, 592104964Sjeff ("runq_remove: process swapped out")); 593104964Sjeff KASSERT((ke->ke_state == KES_ONRUNQ), ("KSE not on run queue")); 594104964Sjeff mtx_assert(&sched_lock, MA_OWNED); 595104964Sjeff 596104964Sjeff runq_remove(&runq, ke); 597104964Sjeff ke->ke_state = KES_THREAD; 598104964Sjeff ke->ke_ksegrp->kg_runq_kses--; 599104964Sjeff} 600104964Sjeff 601104964Sjeffstruct kse * 602104964Sjeffsched_choose(void) 603104964Sjeff{ 604104964Sjeff struct kse *ke; 605104964Sjeff 606104964Sjeff ke = runq_choose(&runq); 607104964Sjeff 608104964Sjeff if (ke != NULL) { 609104964Sjeff runq_remove(&runq, ke); 610104964Sjeff ke->ke_state = KES_THREAD; 611104964Sjeff 612104964Sjeff KASSERT((ke->ke_thread != NULL), 613104964Sjeff ("runq_choose: No thread on KSE")); 614104964Sjeff KASSERT((ke->ke_thread->td_kse != NULL), 615104964Sjeff ("runq_choose: No KSE on thread")); 616104964Sjeff KASSERT(ke->ke_proc->p_sflag & PS_INMEM, 617104964Sjeff ("runq_choose: process swapped out")); 618104964Sjeff } 619104964Sjeff return (ke); 620104964Sjeff} 621104964Sjeff 622104964Sjeffvoid 623104964Sjeffsched_userret(struct thread *td) 624104964Sjeff{ 625104964Sjeff struct ksegrp *kg; 626104964Sjeff /* 627104964Sjeff * XXX we cheat slightly on the locking here to avoid locking in 628104964Sjeff * the usual case. Setting td_priority here is essentially an 629104964Sjeff * incomplete workaround for not setting it properly elsewhere. 630104964Sjeff * Now that some interrupt handlers are threads, not setting it 631104964Sjeff * properly elsewhere can clobber it in the window between setting 632104964Sjeff * it here and returning to user mode, so don't waste time setting 633104964Sjeff * it perfectly here. 634104964Sjeff */ 635104964Sjeff kg = td->td_ksegrp; 636104964Sjeff if (td->td_priority != kg->kg_user_pri) { 637104964Sjeff mtx_lock_spin(&sched_lock); 638104964Sjeff td->td_priority = kg->kg_user_pri; 639104964Sjeff mtx_unlock_spin(&sched_lock); 640104964Sjeff } 641104964Sjeff} 642107126Sjeff 643107126Sjeffint 644107126Sjeffsched_sizeof_kse(void) 645107126Sjeff{ 646109145Sjeff return (sizeof(struct kse) + sizeof(struct ke_sched)); 647107126Sjeff} 648107126Sjeffint 649107126Sjeffsched_sizeof_ksegrp(void) 650107126Sjeff{ 651107126Sjeff return (sizeof(struct ksegrp)); 652107126Sjeff} 653107126Sjeffint 654107126Sjeffsched_sizeof_proc(void) 655107126Sjeff{ 656107126Sjeff return (sizeof(struct proc)); 657107126Sjeff} 658107126Sjeffint 659107126Sjeffsched_sizeof_thread(void) 660107126Sjeff{ 661107126Sjeff return (sizeof(struct thread)); 662107126Sjeff} 663107137Sjeff 664107137Sjefffixpt_t 665107137Sjeffsched_pctcpu(struct kse *ke) 666107137Sjeff{ 667109157Sjeff return (ke->ke_pctcpu); 668107137Sjeff} 669