sched_4bsd.c revision 105127
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 105127 2002-10-14 20:34:31Z 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 54104964Sjeff 55104964Sjeffstatic int sched_quantum; /* Roundrobin scheduling quantum in ticks. */ 56104964Sjeff#define SCHED_QUANTUM (hz / 10); /* Default sched quantum */ 57104964Sjeff 58104964Sjeffstatic struct callout schedcpu_callout; 59104964Sjeffstatic struct callout roundrobin_callout; 60104964Sjeff 61104964Sjeffstatic void roundrobin(void *arg); 62104964Sjeffstatic void schedcpu(void *arg); 63104964Sjeffstatic void sched_setup(void *dummy); 64104964Sjeffstatic void maybe_resched(struct thread *td); 65104964Sjeffstatic void updatepri(struct ksegrp *kg); 66104964Sjeffstatic void resetpriority(struct ksegrp *kg); 67104964Sjeff 68104964SjeffSYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL) 69104964Sjeff 70104964Sjeff/* 71104964Sjeff * Global run queue. 72104964Sjeff */ 73104964Sjeffstatic struct runq runq; 74104964SjeffSYSINIT(runq, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, runq_init, &runq) 75104964Sjeff 76104964Sjeffstatic int 77104964Sjeffsysctl_kern_quantum(SYSCTL_HANDLER_ARGS) 78104964Sjeff{ 79104964Sjeff int error, new_val; 80104964Sjeff 81104964Sjeff new_val = sched_quantum * tick; 82104964Sjeff error = sysctl_handle_int(oidp, &new_val, 0, req); 83104964Sjeff if (error != 0 || req->newptr == NULL) 84104964Sjeff return (error); 85104964Sjeff if (new_val < tick) 86104964Sjeff return (EINVAL); 87104964Sjeff sched_quantum = new_val / tick; 88104964Sjeff hogticks = 2 * sched_quantum; 89104964Sjeff return (0); 90104964Sjeff} 91104964Sjeff 92104964SjeffSYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW, 93104964Sjeff 0, sizeof sched_quantum, sysctl_kern_quantum, "I", 94104964Sjeff "Roundrobin scheduling quantum in microseconds"); 95104964Sjeff 96104964Sjeff/* 97104964Sjeff * Arrange to reschedule if necessary, taking the priorities and 98104964Sjeff * schedulers into account. 99104964Sjeff */ 100104964Sjeffstatic void 101104964Sjeffmaybe_resched(struct thread *td) 102104964Sjeff{ 103104964Sjeff 104104964Sjeff mtx_assert(&sched_lock, MA_OWNED); 105104964Sjeff if (td->td_priority < curthread->td_priority) 106104964Sjeff curthread->td_kse->ke_flags |= KEF_NEEDRESCHED; 107104964Sjeff} 108104964Sjeff 109104964Sjeff/* 110104964Sjeff * Force switch among equal priority processes every 100ms. 111104964Sjeff * We don't actually need to force a context switch of the current process. 112104964Sjeff * The act of firing the event triggers a context switch to softclock() and 113104964Sjeff * then switching back out again which is equivalent to a preemption, thus 114104964Sjeff * no further work is needed on the local CPU. 115104964Sjeff */ 116104964Sjeff/* ARGSUSED */ 117104964Sjeffstatic void 118104964Sjeffroundrobin(void *arg) 119104964Sjeff{ 120104964Sjeff 121104964Sjeff#ifdef SMP 122104964Sjeff mtx_lock_spin(&sched_lock); 123104964Sjeff forward_roundrobin(); 124104964Sjeff mtx_unlock_spin(&sched_lock); 125104964Sjeff#endif 126104964Sjeff 127104964Sjeff callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL); 128104964Sjeff} 129104964Sjeff 130104964Sjeff/* 131104964Sjeff * Constants for digital decay and forget: 132104964Sjeff * 90% of (p_estcpu) usage in 5 * loadav time 133104964Sjeff * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 134104964Sjeff * Note that, as ps(1) mentions, this can let percentages 135104964Sjeff * total over 100% (I've seen 137.9% for 3 processes). 136104964Sjeff * 137104964Sjeff * Note that schedclock() updates p_estcpu and p_cpticks asynchronously. 138104964Sjeff * 139104964Sjeff * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds. 140104964Sjeff * That is, the system wants to compute a value of decay such 141104964Sjeff * that the following for loop: 142104964Sjeff * for (i = 0; i < (5 * loadavg); i++) 143104964Sjeff * p_estcpu *= decay; 144104964Sjeff * will compute 145104964Sjeff * p_estcpu *= 0.1; 146104964Sjeff * for all values of loadavg: 147104964Sjeff * 148104964Sjeff * Mathematically this loop can be expressed by saying: 149104964Sjeff * decay ** (5 * loadavg) ~= .1 150104964Sjeff * 151104964Sjeff * The system computes decay as: 152104964Sjeff * decay = (2 * loadavg) / (2 * loadavg + 1) 153104964Sjeff * 154104964Sjeff * We wish to prove that the system's computation of decay 155104964Sjeff * will always fulfill the equation: 156104964Sjeff * decay ** (5 * loadavg) ~= .1 157104964Sjeff * 158104964Sjeff * If we compute b as: 159104964Sjeff * b = 2 * loadavg 160104964Sjeff * then 161104964Sjeff * decay = b / (b + 1) 162104964Sjeff * 163104964Sjeff * We now need to prove two things: 164104964Sjeff * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 165104964Sjeff * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 166104964Sjeff * 167104964Sjeff * Facts: 168104964Sjeff * For x close to zero, exp(x) =~ 1 + x, since 169104964Sjeff * exp(x) = 0! + x**1/1! + x**2/2! + ... . 170104964Sjeff * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 171104964Sjeff * For x close to zero, ln(1+x) =~ x, since 172104964Sjeff * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 173104964Sjeff * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 174104964Sjeff * ln(.1) =~ -2.30 175104964Sjeff * 176104964Sjeff * Proof of (1): 177104964Sjeff * Solve (factor)**(power) =~ .1 given power (5*loadav): 178104964Sjeff * solving for factor, 179104964Sjeff * ln(factor) =~ (-2.30/5*loadav), or 180104964Sjeff * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 181104964Sjeff * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 182104964Sjeff * 183104964Sjeff * Proof of (2): 184104964Sjeff * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 185104964Sjeff * solving for power, 186104964Sjeff * power*ln(b/(b+1)) =~ -2.30, or 187104964Sjeff * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 188104964Sjeff * 189104964Sjeff * Actual power values for the implemented algorithm are as follows: 190104964Sjeff * loadav: 1 2 3 4 191104964Sjeff * power: 5.68 10.32 14.94 19.55 192104964Sjeff */ 193104964Sjeff 194104964Sjeff/* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 195104964Sjeff#define loadfactor(loadav) (2 * (loadav)) 196104964Sjeff#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 197104964Sjeff 198104964Sjeff/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 199104964Sjeffstatic fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 200104964SjeffSYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 201104964Sjeff 202104964Sjeff/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */ 203104964Sjeffstatic int fscale __unused = FSCALE; 204104964SjeffSYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, ""); 205104964Sjeff 206104964Sjeff/* 207104964Sjeff * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 208104964Sjeff * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 209104964Sjeff * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 210104964Sjeff * 211104964Sjeff * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 212104964Sjeff * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 213104964Sjeff * 214104964Sjeff * If you don't want to bother with the faster/more-accurate formula, you 215104964Sjeff * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 216104964Sjeff * (more general) method of calculating the %age of CPU used by a process. 217104964Sjeff */ 218104964Sjeff#define CCPU_SHIFT 11 219104964Sjeff 220104964Sjeff/* 221104964Sjeff * Recompute process priorities, every hz ticks. 222104964Sjeff * MP-safe, called without the Giant mutex. 223104964Sjeff */ 224104964Sjeff/* ARGSUSED */ 225104964Sjeffstatic void 226104964Sjeffschedcpu(void *arg) 227104964Sjeff{ 228104964Sjeff register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 229104964Sjeff struct thread *td; 230104964Sjeff struct proc *p; 231104964Sjeff struct kse *ke; 232104964Sjeff struct ksegrp *kg; 233104964Sjeff int realstathz; 234104964Sjeff int awake; 235104964Sjeff 236104964Sjeff realstathz = stathz ? stathz : hz; 237104964Sjeff sx_slock(&allproc_lock); 238104964Sjeff FOREACH_PROC_IN_SYSTEM(p) { 239104964Sjeff mtx_lock_spin(&sched_lock); 240104964Sjeff p->p_swtime++; 241104964Sjeff FOREACH_KSEGRP_IN_PROC(p, kg) { 242104964Sjeff awake = 0; 243104964Sjeff FOREACH_KSE_IN_GROUP(kg, ke) { 244104964Sjeff /* 245104964Sjeff * Increment time in/out of memory and sleep 246104964Sjeff * time (if sleeping). We ignore overflow; 247104964Sjeff * with 16-bit int's (remember them?) 248104964Sjeff * overflow takes 45 days. 249104964Sjeff */ 250104964Sjeff /* 251104964Sjeff * The kse slptimes are not touched in wakeup 252104964Sjeff * because the thread may not HAVE a KSE. 253104964Sjeff */ 254104964Sjeff if (ke->ke_state == KES_ONRUNQ) { 255104964Sjeff awake = 1; 256104964Sjeff ke->ke_flags &= ~KEF_DIDRUN; 257104964Sjeff } else if ((ke->ke_state == KES_THREAD) && 258104964Sjeff (TD_IS_RUNNING(ke->ke_thread))) { 259104964Sjeff awake = 1; 260104964Sjeff /* Do not clear KEF_DIDRUN */ 261104964Sjeff } else if (ke->ke_flags & KEF_DIDRUN) { 262104964Sjeff awake = 1; 263104964Sjeff ke->ke_flags &= ~KEF_DIDRUN; 264104964Sjeff } 265104964Sjeff 266104964Sjeff /* 267104964Sjeff * pctcpu is only for ps? 268104964Sjeff * Do it per kse.. and add them up at the end? 269104964Sjeff * XXXKSE 270104964Sjeff */ 271104964Sjeff ke->ke_pctcpu 272104964Sjeff = (ke->ke_pctcpu * ccpu) >> FSHIFT; 273104964Sjeff /* 274104964Sjeff * If the kse has been idle the entire second, 275104964Sjeff * stop recalculating its priority until 276104964Sjeff * it wakes up. 277104964Sjeff */ 278104964Sjeff if (ke->ke_cpticks == 0) 279104964Sjeff continue; 280104964Sjeff#if (FSHIFT >= CCPU_SHIFT) 281104964Sjeff ke->ke_pctcpu += (realstathz == 100) ? 282104964Sjeff ((fixpt_t) ke->ke_cpticks) << 283104964Sjeff (FSHIFT - CCPU_SHIFT) : 284104964Sjeff 100 * (((fixpt_t) ke->ke_cpticks) << 285104964Sjeff (FSHIFT - CCPU_SHIFT)) / realstathz; 286104964Sjeff#else 287104964Sjeff ke->ke_pctcpu += ((FSCALE - ccpu) * 288104964Sjeff (ke->ke_cpticks * FSCALE / realstathz)) >> 289104964Sjeff FSHIFT; 290104964Sjeff#endif 291104964Sjeff ke->ke_cpticks = 0; 292104964Sjeff } /* end of kse loop */ 293104964Sjeff /* 294104964Sjeff * If there are ANY running threads in this KSEGRP, 295104964Sjeff * then don't count it as sleeping. 296104964Sjeff */ 297104964Sjeff if (awake) { 298104964Sjeff if (kg->kg_slptime > 1) { 299104964Sjeff /* 300104964Sjeff * In an ideal world, this should not 301104964Sjeff * happen, because whoever woke us 302104964Sjeff * up from the long sleep should have 303104964Sjeff * unwound the slptime and reset our 304104964Sjeff * priority before we run at the stale 305104964Sjeff * priority. Should KASSERT at some 306104964Sjeff * point when all the cases are fixed. 307104964Sjeff */ 308104964Sjeff updatepri(kg); 309104964Sjeff } 310104964Sjeff kg->kg_slptime = 0; 311104964Sjeff } else { 312104964Sjeff kg->kg_slptime++; 313104964Sjeff } 314104964Sjeff if (kg->kg_slptime > 1) 315104964Sjeff continue; 316104964Sjeff kg->kg_estcpu = decay_cpu(loadfac, kg->kg_estcpu); 317104964Sjeff resetpriority(kg); 318104964Sjeff FOREACH_THREAD_IN_GROUP(kg, td) { 319104964Sjeff if (td->td_priority >= PUSER) { 320105127Sjulian sched_prio(td, kg->kg_user_pri); 321104964Sjeff } 322104964Sjeff } 323104964Sjeff } /* end of ksegrp loop */ 324104964Sjeff mtx_unlock_spin(&sched_lock); 325104964Sjeff } /* end of process loop */ 326104964Sjeff sx_sunlock(&allproc_lock); 327104964Sjeff wakeup(&lbolt); 328104964Sjeff callout_reset(&schedcpu_callout, hz, schedcpu, NULL); 329104964Sjeff} 330104964Sjeff 331104964Sjeff/* 332104964Sjeff * Recalculate the priority of a process after it has slept for a while. 333104964Sjeff * For all load averages >= 1 and max p_estcpu of 255, sleeping for at 334104964Sjeff * least six times the loadfactor will decay p_estcpu to zero. 335104964Sjeff */ 336104964Sjeffstatic void 337104964Sjeffupdatepri(struct ksegrp *kg) 338104964Sjeff{ 339104964Sjeff register unsigned int newcpu; 340104964Sjeff register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 341104964Sjeff 342104964Sjeff newcpu = kg->kg_estcpu; 343104964Sjeff if (kg->kg_slptime > 5 * loadfac) 344104964Sjeff kg->kg_estcpu = 0; 345104964Sjeff else { 346104964Sjeff kg->kg_slptime--; /* the first time was done in schedcpu */ 347104964Sjeff while (newcpu && --kg->kg_slptime) 348104964Sjeff newcpu = decay_cpu(loadfac, newcpu); 349104964Sjeff kg->kg_estcpu = newcpu; 350104964Sjeff } 351104964Sjeff resetpriority(kg); 352104964Sjeff} 353104964Sjeff 354104964Sjeff/* 355104964Sjeff * Compute the priority of a process when running in user mode. 356104964Sjeff * Arrange to reschedule if the resulting priority is better 357104964Sjeff * than that of the current process. 358104964Sjeff */ 359104964Sjeffstatic void 360104964Sjeffresetpriority(struct ksegrp *kg) 361104964Sjeff{ 362104964Sjeff register unsigned int newpriority; 363104964Sjeff struct thread *td; 364104964Sjeff 365104964Sjeff mtx_lock_spin(&sched_lock); 366104964Sjeff if (kg->kg_pri_class == PRI_TIMESHARE) { 367104964Sjeff newpriority = PUSER + kg->kg_estcpu / INVERSE_ESTCPU_WEIGHT + 368104964Sjeff NICE_WEIGHT * (kg->kg_nice - PRIO_MIN); 369104964Sjeff newpriority = min(max(newpriority, PRI_MIN_TIMESHARE), 370104964Sjeff PRI_MAX_TIMESHARE); 371104964Sjeff kg->kg_user_pri = newpriority; 372104964Sjeff } 373104964Sjeff FOREACH_THREAD_IN_GROUP(kg, td) { 374104964Sjeff maybe_resched(td); /* XXXKSE silly */ 375104964Sjeff } 376104964Sjeff mtx_unlock_spin(&sched_lock); 377104964Sjeff} 378104964Sjeff 379104964Sjeff/* ARGSUSED */ 380104964Sjeffstatic void 381104964Sjeffsched_setup(void *dummy) 382104964Sjeff{ 383104964Sjeff if (sched_quantum == 0) 384104964Sjeff sched_quantum = SCHED_QUANTUM; 385104964Sjeff hogticks = 2 * sched_quantum; 386104964Sjeff 387104964Sjeff callout_init(&schedcpu_callout, 1); 388104964Sjeff callout_init(&roundrobin_callout, 0); 389104964Sjeff 390104964Sjeff /* Kick off timeout driven events by calling first time. */ 391104964Sjeff roundrobin(NULL); 392104964Sjeff schedcpu(NULL); 393104964Sjeff} 394104964Sjeff 395104964Sjeff/* External interfaces start here */ 396104964Sjeffint 397104964Sjeffsched_runnable(void) 398104964Sjeff{ 399104964Sjeff return runq_check(&runq); 400104964Sjeff} 401104964Sjeff 402104964Sjeffint 403104964Sjeffsched_rr_interval(void) 404104964Sjeff{ 405104964Sjeff if (sched_quantum == 0) 406104964Sjeff sched_quantum = SCHED_QUANTUM; 407104964Sjeff return (sched_quantum); 408104964Sjeff} 409104964Sjeff 410104964Sjeff/* 411104964Sjeff * We adjust the priority of the current process. The priority of 412104964Sjeff * a process gets worse as it accumulates CPU time. The cpu usage 413104964Sjeff * estimator (p_estcpu) is increased here. resetpriority() will 414104964Sjeff * compute a different priority each time p_estcpu increases by 415104964Sjeff * INVERSE_ESTCPU_WEIGHT 416104964Sjeff * (until MAXPRI is reached). The cpu usage estimator ramps up 417104964Sjeff * quite quickly when the process is running (linearly), and decays 418104964Sjeff * away exponentially, at a rate which is proportionally slower when 419104964Sjeff * the system is busy. The basic principle is that the system will 420104964Sjeff * 90% forget that the process used a lot of CPU time in 5 * loadav 421104964Sjeff * seconds. This causes the system to favor processes which haven't 422104964Sjeff * run much recently, and to round-robin among other processes. 423104964Sjeff */ 424104964Sjeffvoid 425104964Sjeffsched_clock(struct thread *td) 426104964Sjeff{ 427104964Sjeff struct kse *ke; 428104964Sjeff struct ksegrp *kg; 429104964Sjeff 430104964Sjeff KASSERT((td != NULL), ("schedclock: null thread pointer")); 431104964Sjeff ke = td->td_kse; 432104964Sjeff kg = td->td_ksegrp; 433104964Sjeff ke->ke_cpticks++; 434104964Sjeff kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + 1); 435104964Sjeff if ((kg->kg_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) { 436104964Sjeff resetpriority(kg); 437104964Sjeff if (td->td_priority >= PUSER) 438104964Sjeff td->td_priority = kg->kg_user_pri; 439104964Sjeff } 440104964Sjeff} 441104964Sjeff/* 442104964Sjeff * charge childs scheduling cpu usage to parent. 443104964Sjeff * 444104964Sjeff * XXXKSE assume only one thread & kse & ksegrp keep estcpu in each ksegrp. 445104964Sjeff * Charge it to the ksegrp that did the wait since process estcpu is sum of 446104964Sjeff * all ksegrps, this is strictly as expected. Assume that the child process 447104964Sjeff * aggregated all the estcpu into the 'built-in' ksegrp. 448104964Sjeff */ 449104964Sjeffvoid 450104964Sjeffsched_exit(struct ksegrp *kg, struct ksegrp *child) 451104964Sjeff{ 452104964Sjeff kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + child->kg_estcpu); 453104964Sjeff} 454104964Sjeff 455104964Sjeffvoid 456104964Sjeffsched_fork(struct ksegrp *kg, struct ksegrp *child) 457104964Sjeff{ 458104964Sjeff /* 459104964Sjeff * set priority of child to be that of parent. 460104964Sjeff * XXXKSE this needs redefining.. 461104964Sjeff */ 462104964Sjeff child->kg_estcpu = kg->kg_estcpu; 463104964Sjeff} 464104964Sjeff 465104964Sjeffvoid 466104964Sjeffsched_nice(struct ksegrp *kg, int nice) 467104964Sjeff{ 468104964Sjeff kg->kg_nice = nice; 469104964Sjeff resetpriority(kg); 470104964Sjeff} 471104964Sjeff 472105127Sjulian/* 473105127Sjulian * Adjust the priority of a thread. 474105127Sjulian * This may include moving the thread within the KSEGRP, 475105127Sjulian * changing the assignment of a kse to the thread, 476105127Sjulian * and moving a KSE in the system run queue. 477105127Sjulian */ 478104964Sjeffvoid 479104964Sjeffsched_prio(struct thread *td, u_char prio) 480104964Sjeff{ 481104964Sjeff 482104964Sjeff if (TD_ON_RUNQ(td)) { 483105127Sjulian adjustrunqueue(td, prio); 484105127Sjulian } else { 485105127Sjulian td->td_priority = prio; 486104964Sjeff } 487104964Sjeff} 488104964Sjeff 489104964Sjeffvoid 490104964Sjeffsched_sleep(struct thread *td, u_char prio) 491104964Sjeff{ 492104964Sjeff td->td_ksegrp->kg_slptime = 0; 493104964Sjeff td->td_priority = prio; 494104964Sjeff} 495104964Sjeff 496104964Sjeffvoid 497104964Sjeffsched_switchin(struct thread *td) 498104964Sjeff{ 499104964Sjeff td->td_kse->ke_oncpu = PCPU_GET(cpuid); 500104964Sjeff} 501104964Sjeff 502104964Sjeffvoid 503104964Sjeffsched_switchout(struct thread *td) 504104964Sjeff{ 505104964Sjeff struct kse *ke; 506104964Sjeff struct proc *p; 507104964Sjeff 508104964Sjeff ke = td->td_kse; 509104964Sjeff p = td->td_proc; 510104964Sjeff 511104964Sjeff KASSERT((ke->ke_state == KES_THREAD), ("mi_switch: kse state?")); 512104964Sjeff 513104964Sjeff td->td_lastcpu = ke->ke_oncpu; 514105127Sjulian td->td_last_kse = ke; 515104964Sjeff ke->ke_oncpu = NOCPU; 516104964Sjeff ke->ke_flags &= ~KEF_NEEDRESCHED; 517104964Sjeff /* 518104964Sjeff * At the last moment, if this thread is still marked RUNNING, 519104964Sjeff * then put it back on the run queue as it has not been suspended 520104964Sjeff * or stopped or any thing else similar. 521104964Sjeff */ 522104964Sjeff if (TD_IS_RUNNING(td)) { 523104964Sjeff /* Put us back on the run queue (kse and all). */ 524104964Sjeff setrunqueue(td); 525104964Sjeff } else if (p->p_flag & P_KSES) { 526104964Sjeff /* 527104964Sjeff * We will not be on the run queue. So we must be 528104964Sjeff * sleeping or similar. As it's available, 529104964Sjeff * someone else can use the KSE if they need it. 530104964Sjeff * (If bound LOANING can still occur). 531104964Sjeff */ 532104964Sjeff kse_reassign(ke); 533104964Sjeff } 534104964Sjeff} 535104964Sjeff 536104964Sjeffvoid 537104964Sjeffsched_wakeup(struct thread *td) 538104964Sjeff{ 539104964Sjeff struct ksegrp *kg; 540104964Sjeff 541104964Sjeff kg = td->td_ksegrp; 542104964Sjeff if (kg->kg_slptime > 1) 543104964Sjeff updatepri(kg); 544104964Sjeff kg->kg_slptime = 0; 545104964Sjeff setrunqueue(td); 546104964Sjeff maybe_resched(td); 547104964Sjeff} 548104964Sjeff 549104964Sjeffvoid 550104964Sjeffsched_add(struct kse *ke) 551104964Sjeff{ 552104964Sjeff mtx_assert(&sched_lock, MA_OWNED); 553104964Sjeff KASSERT((ke->ke_thread != NULL), ("runq_add: No thread on KSE")); 554104964Sjeff KASSERT((ke->ke_thread->td_kse != NULL), 555104964Sjeff ("runq_add: No KSE on thread")); 556104964Sjeff KASSERT(ke->ke_state != KES_ONRUNQ, 557104964Sjeff ("runq_add: kse %p (%s) already in run queue", ke, 558104964Sjeff ke->ke_proc->p_comm)); 559104964Sjeff KASSERT(ke->ke_proc->p_sflag & PS_INMEM, 560104964Sjeff ("runq_add: process swapped out")); 561104964Sjeff ke->ke_ksegrp->kg_runq_kses++; 562104964Sjeff ke->ke_state = KES_ONRUNQ; 563104964Sjeff 564104964Sjeff runq_add(&runq, ke); 565104964Sjeff} 566104964Sjeff 567104964Sjeffvoid 568104964Sjeffsched_rem(struct kse *ke) 569104964Sjeff{ 570104964Sjeff KASSERT(ke->ke_proc->p_sflag & PS_INMEM, 571104964Sjeff ("runq_remove: process swapped out")); 572104964Sjeff KASSERT((ke->ke_state == KES_ONRUNQ), ("KSE not on run queue")); 573104964Sjeff mtx_assert(&sched_lock, MA_OWNED); 574104964Sjeff 575104964Sjeff runq_remove(&runq, ke); 576104964Sjeff ke->ke_state = KES_THREAD; 577104964Sjeff ke->ke_ksegrp->kg_runq_kses--; 578104964Sjeff} 579104964Sjeff 580104964Sjeffstruct kse * 581104964Sjeffsched_choose(void) 582104964Sjeff{ 583104964Sjeff struct kse *ke; 584104964Sjeff 585104964Sjeff ke = runq_choose(&runq); 586104964Sjeff 587104964Sjeff if (ke != NULL) { 588104964Sjeff runq_remove(&runq, ke); 589104964Sjeff ke->ke_state = KES_THREAD; 590104964Sjeff 591104964Sjeff KASSERT((ke->ke_thread != NULL), 592104964Sjeff ("runq_choose: No thread on KSE")); 593104964Sjeff KASSERT((ke->ke_thread->td_kse != NULL), 594104964Sjeff ("runq_choose: No KSE on thread")); 595104964Sjeff KASSERT(ke->ke_proc->p_sflag & PS_INMEM, 596104964Sjeff ("runq_choose: process swapped out")); 597104964Sjeff } 598104964Sjeff return (ke); 599104964Sjeff} 600104964Sjeff 601104964Sjeffvoid 602104964Sjeffsched_userret(struct thread *td) 603104964Sjeff{ 604104964Sjeff struct ksegrp *kg; 605104964Sjeff /* 606104964Sjeff * XXX we cheat slightly on the locking here to avoid locking in 607104964Sjeff * the usual case. Setting td_priority here is essentially an 608104964Sjeff * incomplete workaround for not setting it properly elsewhere. 609104964Sjeff * Now that some interrupt handlers are threads, not setting it 610104964Sjeff * properly elsewhere can clobber it in the window between setting 611104964Sjeff * it here and returning to user mode, so don't waste time setting 612104964Sjeff * it perfectly here. 613104964Sjeff */ 614104964Sjeff kg = td->td_ksegrp; 615104964Sjeff if (td->td_priority != kg->kg_user_pri) { 616104964Sjeff mtx_lock_spin(&sched_lock); 617104964Sjeff td->td_priority = kg->kg_user_pri; 618104964Sjeff mtx_unlock_spin(&sched_lock); 619104964Sjeff } 620104964Sjeff} 621