kern_synch.c revision 40648
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.62 1998/08/26 05:27:42 dillon 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 142#ifdef SMP 143 need_resched(); 144 forward_roundrobin(); 145#else 146 if (p == 0 || RTP_PRIO_NEED_RR(p->p_rtprio.type)) 147 need_resched(); 148#endif 149 150 timeout(roundrobin, NULL, ROUNDROBIN_INTERVAL); 151} 152 153/* 154 * Constants for digital decay and forget: 155 * 90% of (p_estcpu) usage in 5 * loadav time 156 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 157 * Note that, as ps(1) mentions, this can let percentages 158 * total over 100% (I've seen 137.9% for 3 processes). 159 * 160 * Note that statclock() updates p_estcpu and p_cpticks asynchronously. 161 * 162 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds. 163 * That is, the system wants to compute a value of decay such 164 * that the following for loop: 165 * for (i = 0; i < (5 * loadavg); i++) 166 * p_estcpu *= decay; 167 * will compute 168 * p_estcpu *= 0.1; 169 * for all values of loadavg: 170 * 171 * Mathematically this loop can be expressed by saying: 172 * decay ** (5 * loadavg) ~= .1 173 * 174 * The system computes decay as: 175 * decay = (2 * loadavg) / (2 * loadavg + 1) 176 * 177 * We wish to prove that the system's computation of decay 178 * will always fulfill the equation: 179 * decay ** (5 * loadavg) ~= .1 180 * 181 * If we compute b as: 182 * b = 2 * loadavg 183 * then 184 * decay = b / (b + 1) 185 * 186 * We now need to prove two things: 187 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 188 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 189 * 190 * Facts: 191 * For x close to zero, exp(x) =~ 1 + x, since 192 * exp(x) = 0! + x**1/1! + x**2/2! + ... . 193 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 194 * For x close to zero, ln(1+x) =~ x, since 195 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 196 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 197 * ln(.1) =~ -2.30 198 * 199 * Proof of (1): 200 * Solve (factor)**(power) =~ .1 given power (5*loadav): 201 * solving for factor, 202 * ln(factor) =~ (-2.30/5*loadav), or 203 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 204 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 205 * 206 * Proof of (2): 207 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 208 * solving for power, 209 * power*ln(b/(b+1)) =~ -2.30, or 210 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 211 * 212 * Actual power values for the implemented algorithm are as follows: 213 * loadav: 1 2 3 4 214 * power: 5.68 10.32 14.94 19.55 215 */ 216 217/* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 218#define loadfactor(loadav) (2 * (loadav)) 219#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 220 221/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 222static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 223SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 224 225/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */ 226static int fscale __unused = FSCALE; 227SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, ""); 228 229/* 230 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 231 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 232 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 233 * 234 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 235 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 236 * 237 * If you don't want to bother with the faster/more-accurate formula, you 238 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 239 * (more general) method of calculating the %age of CPU used by a process. 240 */ 241#define CCPU_SHIFT 11 242 243/* 244 * Recompute process priorities, every hz ticks. 245 */ 246/* ARGSUSED */ 247static void 248schedcpu(arg) 249 void *arg; 250{ 251 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 252 register struct proc *p; 253 register int s; 254 register unsigned int newcpu; 255 256 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 257 /* 258 * Increment time in/out of memory and sleep time 259 * (if sleeping). We ignore overflow; with 16-bit int's 260 * (remember them?) overflow takes 45 days. 261 */ 262 p->p_swtime++; 263 if (p->p_stat == SSLEEP || p->p_stat == SSTOP) 264 p->p_slptime++; 265 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT; 266 /* 267 * If the process has slept the entire second, 268 * stop recalculating its priority until it wakes up. 269 */ 270 if (p->p_slptime > 1) 271 continue; 272 s = splhigh(); /* prevent state changes and protect run queue */ 273 /* 274 * p_pctcpu is only for ps. 275 */ 276#if (FSHIFT >= CCPU_SHIFT) 277 p->p_pctcpu += (hz == 100)? 278 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT): 279 100 * (((fixpt_t) p->p_cpticks) 280 << (FSHIFT - CCPU_SHIFT)) / hz; 281#else 282 p->p_pctcpu += ((FSCALE - ccpu) * 283 (p->p_cpticks * FSCALE / hz)) >> FSHIFT; 284#endif 285 p->p_cpticks = 0; 286 newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice; 287 p->p_estcpu = min(newcpu, UCHAR_MAX); 288 resetpriority(p); 289 if (p->p_priority >= PUSER) { 290#define PPQ (128 / NQS) /* priorities per queue */ 291 if ((p != curproc) && 292#ifdef SMP 293 (u_char)p->p_oncpu == 0xff && /* idle */ 294#endif 295 p->p_stat == SRUN && 296 (p->p_flag & P_INMEM) && 297 (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) { 298 remrq(p); 299 p->p_priority = p->p_usrpri; 300 setrunqueue(p); 301 } else 302 p->p_priority = p->p_usrpri; 303 } 304 splx(s); 305 } 306 vmmeter(); 307 wakeup((caddr_t)&lbolt); 308 timeout(schedcpu, (void *)0, hz); 309} 310 311/* 312 * Recalculate the priority of a process after it has slept for a while. 313 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at 314 * least six times the loadfactor will decay p_estcpu to zero. 315 */ 316static void 317updatepri(p) 318 register struct proc *p; 319{ 320 register unsigned int newcpu = p->p_estcpu; 321 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 322 323 if (p->p_slptime > 5 * loadfac) 324 p->p_estcpu = 0; 325 else { 326 p->p_slptime--; /* the first time was done in schedcpu */ 327 while (newcpu && --p->p_slptime) 328 newcpu = (int) decay_cpu(loadfac, newcpu); 329 p->p_estcpu = min(newcpu, UCHAR_MAX); 330 } 331 resetpriority(p); 332} 333 334/* 335 * We're only looking at 7 bits of the address; everything is 336 * aligned to 4, lots of things are aligned to greater powers 337 * of 2. Shift right by 8, i.e. drop the bottom 256 worth. 338 */ 339#define TABLESIZE 128 340static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE]; 341#define LOOKUP(x) (((intptr_t)(x) >> 8) & (TABLESIZE - 1)) 342 343/* 344 * During autoconfiguration or after a panic, a sleep will simply 345 * lower the priority briefly to allow interrupts, then return. 346 * The priority to be used (safepri) is machine-dependent, thus this 347 * value is initialized and maintained in the machine-dependent layers. 348 * This priority will typically be 0, or the lowest priority 349 * that is safe for use on the interrupt stack; it can be made 350 * higher to block network software interrupts after panics. 351 */ 352int safepri; 353 354void 355sleepinit() 356{ 357 int i; 358 359 for (i = 0; i < TABLESIZE; i++) 360 TAILQ_INIT(&slpque[i]); 361} 362 363/* 364 * General sleep call. Suspends the current process until a wakeup is 365 * performed on the specified identifier. The process will then be made 366 * runnable with the specified priority. Sleeps at most timo/hz seconds 367 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 368 * before and after sleeping, else signals are not checked. Returns 0 if 369 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 370 * signal needs to be delivered, ERESTART is returned if the current system 371 * call should be restarted if possible, and EINTR is returned if the system 372 * call should be interrupted by the signal (return EINTR). 373 */ 374int 375tsleep(ident, priority, wmesg, timo) 376 void *ident; 377 int priority, timo; 378 const char *wmesg; 379{ 380 struct proc *p = curproc; 381 int s, sig, catch = priority & PCATCH; 382 struct callout_handle thandle; 383 384#ifdef KTRACE 385 if (KTRPOINT(p, KTR_CSW)) 386 ktrcsw(p->p_tracep, 1, 0); 387#endif 388 s = splhigh(); 389 if (cold || panicstr) { 390 /* 391 * After a panic, or during autoconfiguration, 392 * just give interrupts a chance, then just return; 393 * don't run any other procs or panic below, 394 * in case this is the idle process and already asleep. 395 */ 396 splx(safepri); 397 splx(s); 398 return (0); 399 } 400#ifdef DIAGNOSTIC 401 if(p == NULL) 402 panic("tsleep1"); 403 if (ident == NULL || p->p_stat != SRUN) 404 panic("tsleep"); 405 /* XXX This is not exhaustive, just the most common case */ 406 if ((p->p_procq.tqe_prev != NULL) && (*p->p_procq.tqe_prev == p)) 407 panic("sleeping process already on another queue"); 408#endif 409 p->p_wchan = ident; 410 p->p_wmesg = wmesg; 411 p->p_slptime = 0; 412 p->p_priority = priority & PRIMASK; 413 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_procq); 414 if (timo) 415 thandle = timeout(endtsleep, (void *)p, timo); 416 /* 417 * We put ourselves on the sleep queue and start our timeout 418 * before calling CURSIG, as we could stop there, and a wakeup 419 * or a SIGCONT (or both) could occur while we were stopped. 420 * A SIGCONT would cause us to be marked as SSLEEP 421 * without resuming us, thus we must be ready for sleep 422 * when CURSIG is called. If the wakeup happens while we're 423 * stopped, p->p_wchan will be 0 upon return from CURSIG. 424 */ 425 if (catch) { 426 p->p_flag |= P_SINTR; 427 if ((sig = CURSIG(p))) { 428 if (p->p_wchan) 429 unsleep(p); 430 p->p_stat = SRUN; 431 goto resume; 432 } 433 if (p->p_wchan == 0) { 434 catch = 0; 435 goto resume; 436 } 437 } else 438 sig = 0; 439 p->p_stat = SSLEEP; 440 p->p_stats->p_ru.ru_nvcsw++; 441 mi_switch(); 442resume: 443 curpriority = p->p_usrpri; 444 splx(s); 445 p->p_flag &= ~P_SINTR; 446 if (p->p_flag & P_TIMEOUT) { 447 p->p_flag &= ~P_TIMEOUT; 448 if (sig == 0) { 449#ifdef KTRACE 450 if (KTRPOINT(p, KTR_CSW)) 451 ktrcsw(p->p_tracep, 0, 0); 452#endif 453 return (EWOULDBLOCK); 454 } 455 } else if (timo) 456 untimeout(endtsleep, (void *)p, thandle); 457 if (catch && (sig != 0 || (sig = CURSIG(p)))) { 458#ifdef KTRACE 459 if (KTRPOINT(p, KTR_CSW)) 460 ktrcsw(p->p_tracep, 0, 0); 461#endif 462 if (p->p_sigacts->ps_sigintr & sigmask(sig)) 463 return (EINTR); 464 return (ERESTART); 465 } 466#ifdef KTRACE 467 if (KTRPOINT(p, KTR_CSW)) 468 ktrcsw(p->p_tracep, 0, 0); 469#endif 470 return (0); 471} 472 473/* 474 * Implement timeout for tsleep. 475 * If process hasn't been awakened (wchan non-zero), 476 * set timeout flag and undo the sleep. If proc 477 * is stopped, just unsleep so it will remain stopped. 478 */ 479static void 480endtsleep(arg) 481 void *arg; 482{ 483 register struct proc *p; 484 int s; 485 486 p = (struct proc *)arg; 487 s = splhigh(); 488 if (p->p_wchan) { 489 if (p->p_stat == SSLEEP) 490 setrunnable(p); 491 else 492 unsleep(p); 493 p->p_flag |= P_TIMEOUT; 494 } 495 splx(s); 496} 497 498/* 499 * Remove a process from its wait queue 500 */ 501void 502unsleep(p) 503 register struct proc *p; 504{ 505 int s; 506 507 s = splhigh(); 508 if (p->p_wchan) { 509 TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_procq); 510 p->p_wchan = 0; 511 } 512 splx(s); 513} 514 515/* 516 * Make all processes sleeping on the specified identifier runnable. 517 */ 518void 519wakeup(ident) 520 register void *ident; 521{ 522 register struct slpquehead *qp; 523 register struct proc *p; 524 int s; 525 526 s = splhigh(); 527 qp = &slpque[LOOKUP(ident)]; 528restart: 529 for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) { 530#ifdef DIAGNOSTIC 531 if (p->p_stat != SSLEEP && p->p_stat != SSTOP) 532 panic("wakeup"); 533#endif 534 if (p->p_wchan == ident) { 535 TAILQ_REMOVE(qp, p, p_procq); 536 p->p_wchan = 0; 537 if (p->p_stat == SSLEEP) { 538 /* OPTIMIZED EXPANSION OF setrunnable(p); */ 539 if (p->p_slptime > 1) 540 updatepri(p); 541 p->p_slptime = 0; 542 p->p_stat = SRUN; 543 if (p->p_flag & P_INMEM) { 544 setrunqueue(p); 545 maybe_resched(p); 546 } else { 547 p->p_flag |= P_SWAPINREQ; 548 wakeup((caddr_t)&proc0); 549 } 550 /* END INLINE EXPANSION */ 551 goto restart; 552 } 553 } 554 } 555 splx(s); 556} 557 558/* 559 * Make a process sleeping on the specified identifier runnable. 560 * May wake more than one process if a target prcoess is currently 561 * swapped out. 562 */ 563void 564wakeup_one(ident) 565 register void *ident; 566{ 567 register struct slpquehead *qp; 568 register struct proc *p; 569 int s; 570 571 s = splhigh(); 572 qp = &slpque[LOOKUP(ident)]; 573 574 for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) { 575#ifdef DIAGNOSTIC 576 if (p->p_stat != SSLEEP && p->p_stat != SSTOP) 577 panic("wakeup_one"); 578#endif 579 if (p->p_wchan == ident) { 580 TAILQ_REMOVE(qp, p, p_procq); 581 p->p_wchan = 0; 582 if (p->p_stat == SSLEEP) { 583 /* OPTIMIZED EXPANSION OF setrunnable(p); */ 584 if (p->p_slptime > 1) 585 updatepri(p); 586 p->p_slptime = 0; 587 p->p_stat = SRUN; 588 if (p->p_flag & P_INMEM) { 589 setrunqueue(p); 590 maybe_resched(p); 591 break; 592 } else { 593 p->p_flag |= P_SWAPINREQ; 594 wakeup((caddr_t)&proc0); 595 } 596 /* END INLINE EXPANSION */ 597 } 598 } 599 } 600 splx(s); 601} 602 603/* 604 * The machine independent parts of mi_switch(). 605 * Must be called at splstatclock() or higher. 606 */ 607void 608mi_switch() 609{ 610 register struct proc *p = curproc; /* XXX */ 611 register struct rlimit *rlim; 612 int x; 613 614 /* 615 * XXX this spl is almost unnecessary. It is partly to allow for 616 * sloppy callers that don't do it (issignal() via CURSIG() is the 617 * main offender). It is partly to work around a bug in the i386 618 * cpu_switch() (the ipl is not preserved). We ran for years 619 * without it. I think there was only a interrupt latency problem. 620 * The main caller, tsleep(), does an splx() a couple of instructions 621 * after calling here. The buggy caller, issignal(), usually calls 622 * here at spl0() and sometimes returns at splhigh(). The process 623 * then runs for a little too long at splhigh(). The ipl gets fixed 624 * when the process returns to user mode (or earlier). 625 * 626 * It would probably be better to always call here at spl0(). Callers 627 * are prepared to give up control to another process, so they must 628 * be prepared to be interrupted. The clock stuff here may not 629 * actually need splstatclock(). 630 */ 631 x = splstatclock(); 632 633#ifdef SIMPLELOCK_DEBUG 634 if (p->p_simple_locks) 635 printf("sleep: holding simple lock\n"); 636#endif 637 /* 638 * Compute the amount of time during which the current 639 * process was running, and add that to its total so far. 640 */ 641 microuptime(&switchtime); 642 p->p_runtime += (switchtime.tv_usec - p->p_switchtime.tv_usec) + 643 (switchtime.tv_sec - p->p_switchtime.tv_sec) * (int64_t)1000000; 644 645 /* 646 * Check if the process exceeds its cpu resource allocation. 647 * If over max, kill it. 648 */ 649 if (p->p_stat != SZOMB && p->p_runtime > p->p_limit->p_cpulimit) { 650 rlim = &p->p_rlimit[RLIMIT_CPU]; 651 if (p->p_runtime / (rlim_t)1000000 >= rlim->rlim_max) { 652 killproc(p, "exceeded maximum CPU limit"); 653 } else { 654 psignal(p, SIGXCPU); 655 if (rlim->rlim_cur < rlim->rlim_max) { 656 /* XXX: we should make a private copy */ 657 rlim->rlim_cur += 5; 658 } 659 } 660 } 661 662 /* 663 * Pick a new current process and record its start time. 664 */ 665 cnt.v_swtch++; 666 cpu_switch(p); 667 if (switchtime.tv_sec) 668 p->p_switchtime = switchtime; 669 else 670 microuptime(&p->p_switchtime); 671 splx(x); 672} 673 674/* 675 * Initialize the (doubly-linked) run queues 676 * to be empty. 677 */ 678/* ARGSUSED*/ 679static void 680rqinit(dummy) 681 void *dummy; 682{ 683 register int i; 684 685 for (i = 0; i < NQS; i++) { 686 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 687 rtqs[i].ph_link = rtqs[i].ph_rlink = (struct proc *)&rtqs[i]; 688 idqs[i].ph_link = idqs[i].ph_rlink = (struct proc *)&idqs[i]; 689 } 690} 691 692/* 693 * Change process state to be runnable, 694 * placing it on the run queue if it is in memory, 695 * and awakening the swapper if it isn't in memory. 696 */ 697void 698setrunnable(p) 699 register struct proc *p; 700{ 701 register int s; 702 703 s = splhigh(); 704 switch (p->p_stat) { 705 case 0: 706 case SRUN: 707 case SZOMB: 708 default: 709 panic("setrunnable"); 710 case SSTOP: 711 case SSLEEP: 712 unsleep(p); /* e.g. when sending signals */ 713 break; 714 715 case SIDL: 716 break; 717 } 718 p->p_stat = SRUN; 719 if (p->p_flag & P_INMEM) 720 setrunqueue(p); 721 splx(s); 722 if (p->p_slptime > 1) 723 updatepri(p); 724 p->p_slptime = 0; 725 if ((p->p_flag & P_INMEM) == 0) { 726 p->p_flag |= P_SWAPINREQ; 727 wakeup((caddr_t)&proc0); 728 } 729 else 730 maybe_resched(p); 731} 732 733/* 734 * Compute the priority of a process when running in user mode. 735 * Arrange to reschedule if the resulting priority is better 736 * than that of the current process. 737 */ 738void 739resetpriority(p) 740 register struct proc *p; 741{ 742 register unsigned int newpriority; 743 744 if (p->p_rtprio.type == RTP_PRIO_NORMAL) { 745 newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice; 746 newpriority = min(newpriority, MAXPRI); 747 p->p_usrpri = newpriority; 748 } 749 maybe_resched(p); 750} 751 752/* ARGSUSED */ 753static void sched_setup __P((void *dummy)); 754static void 755sched_setup(dummy) 756 void *dummy; 757{ 758 /* Kick off timeout driven events by calling first time. */ 759 roundrobin(NULL); 760 schedcpu(NULL); 761} 762SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL) 763 764