kern_synch.c revision 38551
1/*- 2 * Copyright (c) 1982, 1986, 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95 39 * $Id: kern_synch.c,v 1.61 1998/07/15 02:32:10 bde Exp $ 40 */ 41 42#include "opt_ktrace.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/proc.h> 47#include <sys/kernel.h> 48#include <sys/signalvar.h> 49#include <sys/resourcevar.h> 50#include <sys/vmmeter.h> 51#include <sys/sysctl.h> 52#include <vm/vm.h> 53#include <vm/vm_extern.h> 54#ifdef KTRACE 55#include <sys/uio.h> 56#include <sys/ktrace.h> 57#endif 58 59#include <machine/cpu.h> 60#ifdef SMP 61#include <machine/smp.h> 62#endif 63#include <machine/limits.h> /* for UCHAR_MAX = typeof(p_priority)_MAX */ 64 65static void rqinit __P((void *)); 66SYSINIT(runqueue, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, rqinit, NULL) 67 68u_char curpriority; /* usrpri of curproc */ 69int lbolt; /* once a second sleep address */ 70 71static void endtsleep __P((void *)); 72static void roundrobin __P((void *arg)); 73static void schedcpu __P((void *arg)); 74static void updatepri __P((struct proc *p)); 75 76#define MAXIMUM_SCHEDULE_QUANTUM (1000000) /* arbitrary limit */ 77#ifndef DEFAULT_SCHEDULE_QUANTUM 78#define DEFAULT_SCHEDULE_QUANTUM 10 79#endif 80static int quantum = DEFAULT_SCHEDULE_QUANTUM; /* default value */ 81 82static int 83sysctl_kern_quantum SYSCTL_HANDLER_ARGS 84{ 85 int error; 86 int new_val = quantum; 87 88 new_val = quantum; 89 error = sysctl_handle_int(oidp, &new_val, 0, req); 90 if (error == 0) { 91 if ((new_val > 0) && (new_val < MAXIMUM_SCHEDULE_QUANTUM)) { 92 quantum = new_val; 93 } else { 94 error = EINVAL; 95 } 96 } 97 return (error); 98} 99 100SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW, 101 0, sizeof quantum, sysctl_kern_quantum, "I", ""); 102 103/* maybe_resched: Decide if you need to reschedule or not 104 * taking the priorities and schedulers into account. 105 */ 106static void maybe_resched(struct proc *chk) 107{ 108 struct proc *p = curproc; /* XXX */ 109 110 /* 111 * Compare priorities if the new process is on the same scheduler, 112 * otherwise the one on the more realtimeish scheduler wins. 113 * 114 * XXX idle scheduler still broken because proccess stays on idle 115 * scheduler during waits (such as when getting FS locks). If a 116 * standard process becomes runaway cpu-bound, the system can lockup 117 * due to idle-scheduler processes in wakeup never getting any cpu. 118 */ 119 if (p == 0 || 120 (chk->p_priority < curpriority && RTP_PRIO_BASE(p->p_rtprio.type) == RTP_PRIO_BASE(chk->p_rtprio.type)) || 121 RTP_PRIO_BASE(chk->p_rtprio.type) < RTP_PRIO_BASE(p->p_rtprio.type) 122 ) { 123 need_resched(); 124 } 125} 126 127#define ROUNDROBIN_INTERVAL (hz / quantum) 128int roundrobin_interval(void) 129{ 130 return ROUNDROBIN_INTERVAL; 131} 132 133/* 134 * Force switch among equal priority processes every 100ms. 135 */ 136/* ARGSUSED */ 137static void 138roundrobin(arg) 139 void *arg; 140{ 141 struct proc *p = curproc; /* XXX */ 142 143#ifdef SMP 144 need_resched(); 145 forward_roundrobin(); 146#else 147 if (p == 0 || RTP_PRIO_NEED_RR(p->p_rtprio.type)) 148 need_resched(); 149#endif 150 151 timeout(roundrobin, NULL, ROUNDROBIN_INTERVAL); 152} 153 154/* 155 * Constants for digital decay and forget: 156 * 90% of (p_estcpu) usage in 5 * loadav time 157 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 158 * Note that, as ps(1) mentions, this can let percentages 159 * total over 100% (I've seen 137.9% for 3 processes). 160 * 161 * Note that statclock() updates p_estcpu and p_cpticks asynchronously. 162 * 163 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds. 164 * That is, the system wants to compute a value of decay such 165 * that the following for loop: 166 * for (i = 0; i < (5 * loadavg); i++) 167 * p_estcpu *= decay; 168 * will compute 169 * p_estcpu *= 0.1; 170 * for all values of loadavg: 171 * 172 * Mathematically this loop can be expressed by saying: 173 * decay ** (5 * loadavg) ~= .1 174 * 175 * The system computes decay as: 176 * decay = (2 * loadavg) / (2 * loadavg + 1) 177 * 178 * We wish to prove that the system's computation of decay 179 * will always fulfill the equation: 180 * decay ** (5 * loadavg) ~= .1 181 * 182 * If we compute b as: 183 * b = 2 * loadavg 184 * then 185 * decay = b / (b + 1) 186 * 187 * We now need to prove two things: 188 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 189 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 190 * 191 * Facts: 192 * For x close to zero, exp(x) =~ 1 + x, since 193 * exp(x) = 0! + x**1/1! + x**2/2! + ... . 194 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 195 * For x close to zero, ln(1+x) =~ x, since 196 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 197 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 198 * ln(.1) =~ -2.30 199 * 200 * Proof of (1): 201 * Solve (factor)**(power) =~ .1 given power (5*loadav): 202 * solving for factor, 203 * ln(factor) =~ (-2.30/5*loadav), or 204 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 205 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 206 * 207 * Proof of (2): 208 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 209 * solving for power, 210 * power*ln(b/(b+1)) =~ -2.30, or 211 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 212 * 213 * Actual power values for the implemented algorithm are as follows: 214 * loadav: 1 2 3 4 215 * power: 5.68 10.32 14.94 19.55 216 */ 217 218/* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 219#define loadfactor(loadav) (2 * (loadav)) 220#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 221 222/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 223static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 224SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 225 226/* kernel uses `FSCALE', user uses `fscale' */ 227static int fscale = FSCALE; 228SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, ""); 229 230/* 231 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 232 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 233 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 234 * 235 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 236 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 237 * 238 * If you don't want to bother with the faster/more-accurate formula, you 239 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 240 * (more general) method of calculating the %age of CPU used by a process. 241 */ 242#define CCPU_SHIFT 11 243 244/* 245 * Recompute process priorities, every hz ticks. 246 */ 247/* ARGSUSED */ 248static void 249schedcpu(arg) 250 void *arg; 251{ 252 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 253 register struct proc *p; 254 register int s; 255 register unsigned int newcpu; 256 257 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 258 /* 259 * Increment time in/out of memory and sleep time 260 * (if sleeping). We ignore overflow; with 16-bit int's 261 * (remember them?) overflow takes 45 days. 262 */ 263 p->p_swtime++; 264 if (p->p_stat == SSLEEP || p->p_stat == SSTOP) 265 p->p_slptime++; 266 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT; 267 /* 268 * If the process has slept the entire second, 269 * stop recalculating its priority until it wakes up. 270 */ 271 if (p->p_slptime > 1) 272 continue; 273 s = splhigh(); /* prevent state changes and protect run queue */ 274 /* 275 * p_pctcpu is only for ps. 276 */ 277#if (FSHIFT >= CCPU_SHIFT) 278 p->p_pctcpu += (hz == 100)? 279 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT): 280 100 * (((fixpt_t) p->p_cpticks) 281 << (FSHIFT - CCPU_SHIFT)) / hz; 282#else 283 p->p_pctcpu += ((FSCALE - ccpu) * 284 (p->p_cpticks * FSCALE / hz)) >> FSHIFT; 285#endif 286 p->p_cpticks = 0; 287 newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice; 288 p->p_estcpu = min(newcpu, UCHAR_MAX); 289 resetpriority(p); 290 if (p->p_priority >= PUSER) { 291#define PPQ (128 / NQS) /* priorities per queue */ 292 if ((p != curproc) && 293#ifdef SMP 294 (u_char)p->p_oncpu == 0xff && /* idle */ 295#endif 296 p->p_stat == SRUN && 297 (p->p_flag & P_INMEM) && 298 (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) { 299 remrq(p); 300 p->p_priority = p->p_usrpri; 301 setrunqueue(p); 302 } else 303 p->p_priority = p->p_usrpri; 304 } 305 splx(s); 306 } 307 vmmeter(); 308 wakeup((caddr_t)&lbolt); 309 timeout(schedcpu, (void *)0, hz); 310} 311 312/* 313 * Recalculate the priority of a process after it has slept for a while. 314 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at 315 * least six times the loadfactor will decay p_estcpu to zero. 316 */ 317static void 318updatepri(p) 319 register struct proc *p; 320{ 321 register unsigned int newcpu = p->p_estcpu; 322 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 323 324 if (p->p_slptime > 5 * loadfac) 325 p->p_estcpu = 0; 326 else { 327 p->p_slptime--; /* the first time was done in schedcpu */ 328 while (newcpu && --p->p_slptime) 329 newcpu = (int) decay_cpu(loadfac, newcpu); 330 p->p_estcpu = min(newcpu, UCHAR_MAX); 331 } 332 resetpriority(p); 333} 334 335/* 336 * We're only looking at 7 bits of the address; everything is 337 * aligned to 4, lots of things are aligned to greater powers 338 * of 2. Shift right by 8, i.e. drop the bottom 256 worth. 339 */ 340#define TABLESIZE 128 341static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE]; 342#define LOOKUP(x) (((intptr_t)(x) >> 8) & (TABLESIZE - 1)) 343 344/* 345 * During autoconfiguration or after a panic, a sleep will simply 346 * lower the priority briefly to allow interrupts, then return. 347 * The priority to be used (safepri) is machine-dependent, thus this 348 * value is initialized and maintained in the machine-dependent layers. 349 * This priority will typically be 0, or the lowest priority 350 * that is safe for use on the interrupt stack; it can be made 351 * higher to block network software interrupts after panics. 352 */ 353int safepri; 354 355void 356sleepinit() 357{ 358 int i; 359 360 for (i = 0; i < TABLESIZE; i++) 361 TAILQ_INIT(&slpque[i]); 362} 363 364/* 365 * General sleep call. Suspends the current process until a wakeup is 366 * performed on the specified identifier. The process will then be made 367 * runnable with the specified priority. Sleeps at most timo/hz seconds 368 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 369 * before and after sleeping, else signals are not checked. Returns 0 if 370 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 371 * signal needs to be delivered, ERESTART is returned if the current system 372 * call should be restarted if possible, and EINTR is returned if the system 373 * call should be interrupted by the signal (return EINTR). 374 */ 375int 376tsleep(ident, priority, wmesg, timo) 377 void *ident; 378 int priority, timo; 379 const char *wmesg; 380{ 381 struct proc *p = curproc; 382 int s, sig, catch = priority & PCATCH; 383 struct callout_handle thandle; 384 385#ifdef KTRACE 386 if (KTRPOINT(p, KTR_CSW)) 387 ktrcsw(p->p_tracep, 1, 0); 388#endif 389 s = splhigh(); 390 if (cold || panicstr) { 391 /* 392 * After a panic, or during autoconfiguration, 393 * just give interrupts a chance, then just return; 394 * don't run any other procs or panic below, 395 * in case this is the idle process and already asleep. 396 */ 397 splx(safepri); 398 splx(s); 399 return (0); 400 } 401#ifdef DIAGNOSTIC 402 if(p == NULL) 403 panic("tsleep1"); 404 if (ident == NULL || p->p_stat != SRUN) 405 panic("tsleep"); 406 /* XXX This is not exhaustive, just the most common case */ 407 if ((p->p_procq.tqe_prev != NULL) && (*p->p_procq.tqe_prev == p)) 408 panic("sleeping process already on another queue"); 409#endif 410 p->p_wchan = ident; 411 p->p_wmesg = wmesg; 412 p->p_slptime = 0; 413 p->p_priority = priority & PRIMASK; 414 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_procq); 415 if (timo) 416 thandle = timeout(endtsleep, (void *)p, timo); 417 /* 418 * We put ourselves on the sleep queue and start our timeout 419 * before calling CURSIG, as we could stop there, and a wakeup 420 * or a SIGCONT (or both) could occur while we were stopped. 421 * A SIGCONT would cause us to be marked as SSLEEP 422 * without resuming us, thus we must be ready for sleep 423 * when CURSIG is called. If the wakeup happens while we're 424 * stopped, p->p_wchan will be 0 upon return from CURSIG. 425 */ 426 if (catch) { 427 p->p_flag |= P_SINTR; 428 if ((sig = CURSIG(p))) { 429 if (p->p_wchan) 430 unsleep(p); 431 p->p_stat = SRUN; 432 goto resume; 433 } 434 if (p->p_wchan == 0) { 435 catch = 0; 436 goto resume; 437 } 438 } else 439 sig = 0; 440 p->p_stat = SSLEEP; 441 p->p_stats->p_ru.ru_nvcsw++; 442 mi_switch(); 443resume: 444 curpriority = p->p_usrpri; 445 splx(s); 446 p->p_flag &= ~P_SINTR; 447 if (p->p_flag & P_TIMEOUT) { 448 p->p_flag &= ~P_TIMEOUT; 449 if (sig == 0) { 450#ifdef KTRACE 451 if (KTRPOINT(p, KTR_CSW)) 452 ktrcsw(p->p_tracep, 0, 0); 453#endif 454 return (EWOULDBLOCK); 455 } 456 } else if (timo) 457 untimeout(endtsleep, (void *)p, thandle); 458 if (catch && (sig != 0 || (sig = CURSIG(p)))) { 459#ifdef KTRACE 460 if (KTRPOINT(p, KTR_CSW)) 461 ktrcsw(p->p_tracep, 0, 0); 462#endif 463 if (p->p_sigacts->ps_sigintr & sigmask(sig)) 464 return (EINTR); 465 return (ERESTART); 466 } 467#ifdef KTRACE 468 if (KTRPOINT(p, KTR_CSW)) 469 ktrcsw(p->p_tracep, 0, 0); 470#endif 471 return (0); 472} 473 474/* 475 * Implement timeout for tsleep. 476 * If process hasn't been awakened (wchan non-zero), 477 * set timeout flag and undo the sleep. If proc 478 * is stopped, just unsleep so it will remain stopped. 479 */ 480static void 481endtsleep(arg) 482 void *arg; 483{ 484 register struct proc *p; 485 int s; 486 487 p = (struct proc *)arg; 488 s = splhigh(); 489 if (p->p_wchan) { 490 if (p->p_stat == SSLEEP) 491 setrunnable(p); 492 else 493 unsleep(p); 494 p->p_flag |= P_TIMEOUT; 495 } 496 splx(s); 497} 498 499/* 500 * Remove a process from its wait queue 501 */ 502void 503unsleep(p) 504 register struct proc *p; 505{ 506 int s; 507 508 s = splhigh(); 509 if (p->p_wchan) { 510 TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_procq); 511 p->p_wchan = 0; 512 } 513 splx(s); 514} 515 516/* 517 * Make all processes sleeping on the specified identifier runnable. 518 */ 519void 520wakeup(ident) 521 register void *ident; 522{ 523 register struct slpquehead *qp; 524 register struct proc *p; 525 int s; 526 527 s = splhigh(); 528 qp = &slpque[LOOKUP(ident)]; 529restart: 530 for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) { 531#ifdef DIAGNOSTIC 532 if (p->p_stat != SSLEEP && p->p_stat != SSTOP) 533 panic("wakeup"); 534#endif 535 if (p->p_wchan == ident) { 536 TAILQ_REMOVE(qp, p, p_procq); 537 p->p_wchan = 0; 538 if (p->p_stat == SSLEEP) { 539 /* OPTIMIZED EXPANSION OF setrunnable(p); */ 540 if (p->p_slptime > 1) 541 updatepri(p); 542 p->p_slptime = 0; 543 p->p_stat = SRUN; 544 if (p->p_flag & P_INMEM) { 545 setrunqueue(p); 546 maybe_resched(p); 547 } else { 548 p->p_flag |= P_SWAPINREQ; 549 wakeup((caddr_t)&proc0); 550 } 551 /* END INLINE EXPANSION */ 552 goto restart; 553 } 554 } 555 } 556 splx(s); 557} 558 559/* 560 * Make a process sleeping on the specified identifier runnable. 561 * May wake more than one process if a target prcoess is currently 562 * swapped out. 563 */ 564void 565wakeup_one(ident) 566 register void *ident; 567{ 568 register struct slpquehead *qp; 569 register struct proc *p; 570 int s; 571 572 s = splhigh(); 573 qp = &slpque[LOOKUP(ident)]; 574 575 for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) { 576#ifdef DIAGNOSTIC 577 if (p->p_stat != SSLEEP && p->p_stat != SSTOP) 578 panic("wakeup_one"); 579#endif 580 if (p->p_wchan == ident) { 581 TAILQ_REMOVE(qp, p, p_procq); 582 p->p_wchan = 0; 583 if (p->p_stat == SSLEEP) { 584 /* OPTIMIZED EXPANSION OF setrunnable(p); */ 585 if (p->p_slptime > 1) 586 updatepri(p); 587 p->p_slptime = 0; 588 p->p_stat = SRUN; 589 if (p->p_flag & P_INMEM) { 590 setrunqueue(p); 591 maybe_resched(p); 592 break; 593 } else { 594 p->p_flag |= P_SWAPINREQ; 595 wakeup((caddr_t)&proc0); 596 } 597 /* END INLINE EXPANSION */ 598 } 599 } 600 } 601 splx(s); 602} 603 604/* 605 * The machine independent parts of mi_switch(). 606 * Must be called at splstatclock() or higher. 607 */ 608void 609mi_switch() 610{ 611 register struct proc *p = curproc; /* XXX */ 612 register struct rlimit *rlim; 613 int x; 614 615 /* 616 * XXX this spl is almost unnecessary. It is partly to allow for 617 * sloppy callers that don't do it (issignal() via CURSIG() is the 618 * main offender). It is partly to work around a bug in the i386 619 * cpu_switch() (the ipl is not preserved). We ran for years 620 * without it. I think there was only a interrupt latency problem. 621 * The main caller, tsleep(), does an splx() a couple of instructions 622 * after calling here. The buggy caller, issignal(), usually calls 623 * here at spl0() and sometimes returns at splhigh(). The process 624 * then runs for a little too long at splhigh(). The ipl gets fixed 625 * when the process returns to user mode (or earlier). 626 * 627 * It would probably be better to always call here at spl0(). Callers 628 * are prepared to give up control to another process, so they must 629 * be prepared to be interrupted. The clock stuff here may not 630 * actually need splstatclock(). 631 */ 632 x = splstatclock(); 633 634#ifdef SIMPLELOCK_DEBUG 635 if (p->p_simple_locks) 636 printf("sleep: holding simple lock\n"); 637#endif 638 /* 639 * Compute the amount of time during which the current 640 * process was running, and add that to its total so far. 641 */ 642 microuptime(&switchtime); 643 p->p_runtime += (switchtime.tv_usec - p->p_switchtime.tv_usec) + 644 (switchtime.tv_sec - p->p_switchtime.tv_sec) * (int64_t)1000000; 645 646 /* 647 * Check if the process exceeds its cpu resource allocation. 648 * If over max, kill it. 649 */ 650 if (p->p_stat != SZOMB && p->p_runtime > p->p_limit->p_cpulimit) { 651 rlim = &p->p_rlimit[RLIMIT_CPU]; 652 if (p->p_runtime / (rlim_t)1000000 >= rlim->rlim_max) { 653 killproc(p, "exceeded maximum CPU limit"); 654 } else { 655 psignal(p, SIGXCPU); 656 if (rlim->rlim_cur < rlim->rlim_max) { 657 /* XXX: we should make a private copy */ 658 rlim->rlim_cur += 5; 659 } 660 } 661 } 662 663 /* 664 * Pick a new current process and record its start time. 665 */ 666 cnt.v_swtch++; 667 cpu_switch(p); 668 if (switchtime.tv_sec) 669 p->p_switchtime = switchtime; 670 else 671 microuptime(&p->p_switchtime); 672 splx(x); 673} 674 675/* 676 * Initialize the (doubly-linked) run queues 677 * to be empty. 678 */ 679/* ARGSUSED*/ 680static void 681rqinit(dummy) 682 void *dummy; 683{ 684 register int i; 685 686 for (i = 0; i < NQS; i++) { 687 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 688 rtqs[i].ph_link = rtqs[i].ph_rlink = (struct proc *)&rtqs[i]; 689 idqs[i].ph_link = idqs[i].ph_rlink = (struct proc *)&idqs[i]; 690 } 691} 692 693/* 694 * Change process state to be runnable, 695 * placing it on the run queue if it is in memory, 696 * and awakening the swapper if it isn't in memory. 697 */ 698void 699setrunnable(p) 700 register struct proc *p; 701{ 702 register int s; 703 704 s = splhigh(); 705 switch (p->p_stat) { 706 case 0: 707 case SRUN: 708 case SZOMB: 709 default: 710 panic("setrunnable"); 711 case SSTOP: 712 case SSLEEP: 713 unsleep(p); /* e.g. when sending signals */ 714 break; 715 716 case SIDL: 717 break; 718 } 719 p->p_stat = SRUN; 720 if (p->p_flag & P_INMEM) 721 setrunqueue(p); 722 splx(s); 723 if (p->p_slptime > 1) 724 updatepri(p); 725 p->p_slptime = 0; 726 if ((p->p_flag & P_INMEM) == 0) { 727 p->p_flag |= P_SWAPINREQ; 728 wakeup((caddr_t)&proc0); 729 } 730 else 731 maybe_resched(p); 732} 733 734/* 735 * Compute the priority of a process when running in user mode. 736 * Arrange to reschedule if the resulting priority is better 737 * than that of the current process. 738 */ 739void 740resetpriority(p) 741 register struct proc *p; 742{ 743 register unsigned int newpriority; 744 745 if (p->p_rtprio.type == RTP_PRIO_NORMAL) { 746 newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice; 747 newpriority = min(newpriority, MAXPRI); 748 p->p_usrpri = newpriority; 749 } 750 maybe_resched(p); 751} 752 753/* ARGSUSED */ 754static void sched_setup __P((void *dummy)); 755static void 756sched_setup(dummy) 757 void *dummy; 758{ 759 /* Kick off timeout driven events by calling first time. */ 760 roundrobin(NULL); 761 schedcpu(NULL); 762} 763SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL) 764 765