kern_synch.c revision 14525
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.19 1996/03/03 23:04:10 hsu 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/buf.h> 49#include <sys/signalvar.h> 50#include <sys/resourcevar.h> 51#include <sys/signalvar.h> 52#include <sys/vmmeter.h> 53#include <vm/vm.h> 54#include <vm/vm_param.h> 55#include <vm/vm_extern.h> 56#ifdef KTRACE 57#include <sys/ktrace.h> 58#endif 59 60#include <machine/cpu.h> 61 62static void rqinit __P((void *)); 63SYSINIT(runqueue, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, rqinit, NULL) 64 65u_char curpriority; /* usrpri of curproc */ 66int lbolt; /* once a second sleep address */ 67 68extern void endtsleep __P((void *)); 69extern void updatepri __P((struct proc *p)); 70 71/* 72 * Force switch among equal priority processes every 100ms. 73 */ 74/* ARGSUSED */ 75void 76roundrobin(arg) 77 void *arg; 78{ 79 80 need_resched(); 81 timeout(roundrobin, NULL, hz / 10); 82} 83 84/* 85 * Constants for digital decay and forget: 86 * 90% of (p_estcpu) usage in 5 * loadav time 87 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 88 * Note that, as ps(1) mentions, this can let percentages 89 * total over 100% (I've seen 137.9% for 3 processes). 90 * 91 * Note that hardclock updates p_estcpu and p_cpticks independently. 92 * 93 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds. 94 * That is, the system wants to compute a value of decay such 95 * that the following for loop: 96 * for (i = 0; i < (5 * loadavg); i++) 97 * p_estcpu *= decay; 98 * will compute 99 * p_estcpu *= 0.1; 100 * for all values of loadavg: 101 * 102 * Mathematically this loop can be expressed by saying: 103 * decay ** (5 * loadavg) ~= .1 104 * 105 * The system computes decay as: 106 * decay = (2 * loadavg) / (2 * loadavg + 1) 107 * 108 * We wish to prove that the system's computation of decay 109 * will always fulfill the equation: 110 * decay ** (5 * loadavg) ~= .1 111 * 112 * If we compute b as: 113 * b = 2 * loadavg 114 * then 115 * decay = b / (b + 1) 116 * 117 * We now need to prove two things: 118 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 119 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 120 * 121 * Facts: 122 * For x close to zero, exp(x) =~ 1 + x, since 123 * exp(x) = 0! + x**1/1! + x**2/2! + ... . 124 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 125 * For x close to zero, ln(1+x) =~ x, since 126 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 127 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 128 * ln(.1) =~ -2.30 129 * 130 * Proof of (1): 131 * Solve (factor)**(power) =~ .1 given power (5*loadav): 132 * solving for factor, 133 * ln(factor) =~ (-2.30/5*loadav), or 134 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 135 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 136 * 137 * Proof of (2): 138 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 139 * solving for power, 140 * power*ln(b/(b+1)) =~ -2.30, or 141 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 142 * 143 * Actual power values for the implemented algorithm are as follows: 144 * loadav: 1 2 3 4 145 * power: 5.68 10.32 14.94 19.55 146 */ 147 148/* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 149#define loadfactor(loadav) (2 * (loadav)) 150#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 151 152/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 153fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 154 155/* 156 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 157 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 158 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 159 * 160 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 161 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 162 * 163 * If you dont want to bother with the faster/more-accurate formula, you 164 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 165 * (more general) method of calculating the %age of CPU used by a process. 166 */ 167#define CCPU_SHIFT 11 168 169/* 170 * Recompute process priorities, every hz ticks. 171 */ 172/* ARGSUSED */ 173void 174schedcpu(arg) 175 void *arg; 176{ 177 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 178 register struct proc *p; 179 register int s; 180 register unsigned int newcpu; 181 182 wakeup((caddr_t)&lbolt); 183 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 184 /* 185 * Increment time in/out of memory and sleep time 186 * (if sleeping). We ignore overflow; with 16-bit int's 187 * (remember them?) overflow takes 45 days. 188 */ 189 p->p_swtime++; 190 if (p->p_stat == SSLEEP || p->p_stat == SSTOP) 191 p->p_slptime++; 192 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT; 193 /* 194 * If the process has slept the entire second, 195 * stop recalculating its priority until it wakes up. 196 */ 197 if (p->p_slptime > 1) 198 continue; 199 s = splstatclock(); /* prevent state changes */ 200 /* 201 * p_pctcpu is only for ps. 202 */ 203#if (FSHIFT >= CCPU_SHIFT) 204 p->p_pctcpu += (hz == 100)? 205 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT): 206 100 * (((fixpt_t) p->p_cpticks) 207 << (FSHIFT - CCPU_SHIFT)) / hz; 208#else 209 p->p_pctcpu += ((FSCALE - ccpu) * 210 (p->p_cpticks * FSCALE / hz)) >> FSHIFT; 211#endif 212 p->p_cpticks = 0; 213 newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice; 214 p->p_estcpu = min(newcpu, UCHAR_MAX); 215 resetpriority(p); 216 if (p->p_priority >= PUSER) { 217#define PPQ (128 / NQS) /* priorities per queue */ 218 if ((p != curproc) && 219 p->p_stat == SRUN && 220 (p->p_flag & P_INMEM) && 221 (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) { 222 remrq(p); 223 p->p_priority = p->p_usrpri; 224 setrunqueue(p); 225 } else 226 p->p_priority = p->p_usrpri; 227 } 228 splx(s); 229 } 230 vmmeter(); 231 timeout(schedcpu, (void *)0, hz); 232} 233 234/* 235 * Recalculate the priority of a process after it has slept for a while. 236 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at 237 * least six times the loadfactor will decay p_estcpu to zero. 238 */ 239void 240updatepri(p) 241 register struct proc *p; 242{ 243 register unsigned int newcpu = p->p_estcpu; 244 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 245 246 if (p->p_slptime > 5 * loadfac) 247 p->p_estcpu = 0; 248 else { 249 p->p_slptime--; /* the first time was done in schedcpu */ 250 while (newcpu && --p->p_slptime) 251 newcpu = (int) decay_cpu(loadfac, newcpu); 252 p->p_estcpu = min(newcpu, UCHAR_MAX); 253 } 254 resetpriority(p); 255} 256 257/* 258 * We're only looking at 7 bits of the address; everything is 259 * aligned to 4, lots of things are aligned to greater powers 260 * of 2. Shift right by 8, i.e. drop the bottom 256 worth. 261 */ 262#define TABLESIZE 128 263#define LOOKUP(x) (((long)(x) >> 8) & (TABLESIZE - 1)) 264struct slpque { 265 struct proc *sq_head; 266 struct proc **sq_tailp; 267} slpque[TABLESIZE]; 268 269/* 270 * During autoconfiguration or after a panic, a sleep will simply 271 * lower the priority briefly to allow interrupts, then return. 272 * The priority to be used (safepri) is machine-dependent, thus this 273 * value is initialized and maintained in the machine-dependent layers. 274 * This priority will typically be 0, or the lowest priority 275 * that is safe for use on the interrupt stack; it can be made 276 * higher to block network software interrupts after panics. 277 */ 278int safepri; 279 280/* 281 * General sleep call. Suspends the current process until a wakeup is 282 * performed on the specified identifier. The process will then be made 283 * runnable with the specified priority. Sleeps at most timo/hz seconds 284 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 285 * before and after sleeping, else signals are not checked. Returns 0 if 286 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 287 * signal needs to be delivered, ERESTART is returned if the current system 288 * call should be restarted if possible, and EINTR is returned if the system 289 * call should be interrupted by the signal (return EINTR). 290 */ 291int 292tsleep(ident, priority, wmesg, timo) 293 void *ident; 294 int priority, timo; 295 char *wmesg; 296{ 297 register struct proc *p = curproc; 298 register struct slpque *qp; 299 register s; 300 int sig, catch = priority & PCATCH; 301 302#ifdef KTRACE 303 if (KTRPOINT(p, KTR_CSW)) 304 ktrcsw(p->p_tracep, 1, 0); 305#endif 306 s = splhigh(); 307 if (cold || panicstr) { 308 /* 309 * After a panic, or during autoconfiguration, 310 * just give interrupts a chance, then just return; 311 * don't run any other procs or panic below, 312 * in case this is the idle process and already asleep. 313 */ 314 splx(safepri); 315 splx(s); 316 return (0); 317 } 318#ifdef DIAGNOSTIC 319 if (ident == NULL || p->p_stat != SRUN || p->p_back) 320 panic("tsleep"); 321#endif 322 p->p_wchan = ident; 323 p->p_wmesg = wmesg; 324 p->p_slptime = 0; 325 p->p_priority = priority & PRIMASK; 326 qp = &slpque[LOOKUP(ident)]; 327 if (qp->sq_head == 0) 328 qp->sq_head = p; 329 else 330 *qp->sq_tailp = p; 331 *(qp->sq_tailp = &p->p_forw) = 0; 332 if (timo) 333 timeout(endtsleep, (void *)p, timo); 334 /* 335 * We put ourselves on the sleep queue and start our timeout 336 * before calling CURSIG, as we could stop there, and a wakeup 337 * or a SIGCONT (or both) could occur while we were stopped. 338 * A SIGCONT would cause us to be marked as SSLEEP 339 * without resuming us, thus we must be ready for sleep 340 * when CURSIG is called. If the wakeup happens while we're 341 * stopped, p->p_wchan will be 0 upon return from CURSIG. 342 */ 343 if (catch) { 344 p->p_flag |= P_SINTR; 345 if ((sig = CURSIG(p))) { 346 if (p->p_wchan) 347 unsleep(p); 348 p->p_stat = SRUN; 349 goto resume; 350 } 351 if (p->p_wchan == 0) { 352 catch = 0; 353 goto resume; 354 } 355 } else 356 sig = 0; 357 p->p_stat = SSLEEP; 358 p->p_stats->p_ru.ru_nvcsw++; 359 mi_switch(); 360resume: 361 curpriority = p->p_usrpri; 362 splx(s); 363 p->p_flag &= ~P_SINTR; 364 if (p->p_flag & P_TIMEOUT) { 365 p->p_flag &= ~P_TIMEOUT; 366 if (sig == 0) { 367#ifdef KTRACE 368 if (KTRPOINT(p, KTR_CSW)) 369 ktrcsw(p->p_tracep, 0, 0); 370#endif 371 return (EWOULDBLOCK); 372 } 373 } else if (timo) 374 untimeout(endtsleep, (void *)p); 375 if (catch && (sig != 0 || (sig = CURSIG(p)))) { 376#ifdef KTRACE 377 if (KTRPOINT(p, KTR_CSW)) 378 ktrcsw(p->p_tracep, 0, 0); 379#endif 380 if (p->p_sigacts->ps_sigintr & sigmask(sig)) 381 return (EINTR); 382 return (ERESTART); 383 } 384#ifdef KTRACE 385 if (KTRPOINT(p, KTR_CSW)) 386 ktrcsw(p->p_tracep, 0, 0); 387#endif 388 return (0); 389} 390 391/* 392 * Implement timeout for tsleep. 393 * If process hasn't been awakened (wchan non-zero), 394 * set timeout flag and undo the sleep. If proc 395 * is stopped, just unsleep so it will remain stopped. 396 */ 397void 398endtsleep(arg) 399 void *arg; 400{ 401 register struct proc *p; 402 int s; 403 404 p = (struct proc *)arg; 405 s = splhigh(); 406 if (p->p_wchan) { 407 if (p->p_stat == SSLEEP) 408 setrunnable(p); 409 else 410 unsleep(p); 411 p->p_flag |= P_TIMEOUT; 412 } 413 splx(s); 414} 415 416/* 417 * Short-term, non-interruptable sleep. 418 */ 419void 420sleep(ident, priority) 421 void *ident; 422 int priority; 423{ 424 register struct proc *p = curproc; 425 register struct slpque *qp; 426 register s; 427 428#ifdef DIAGNOSTIC 429 if (priority > PZERO) { 430 printf("sleep called with priority %d > PZERO, wchan: %p\n", 431 priority, ident); 432 panic("old sleep"); 433 } 434#endif 435 s = splhigh(); 436 if (cold || panicstr) { 437 /* 438 * After a panic, or during autoconfiguration, 439 * just give interrupts a chance, then just return; 440 * don't run any other procs or panic below, 441 * in case this is the idle process and already asleep. 442 */ 443 splx(safepri); 444 splx(s); 445 return; 446 } 447#ifdef DIAGNOSTIC 448 if (ident == NULL || p->p_stat != SRUN || p->p_back) 449 panic("sleep"); 450#endif 451 p->p_wchan = ident; 452 p->p_wmesg = NULL; 453 p->p_slptime = 0; 454 p->p_priority = priority; 455 qp = &slpque[LOOKUP(ident)]; 456 if (qp->sq_head == 0) 457 qp->sq_head = p; 458 else 459 *qp->sq_tailp = p; 460 *(qp->sq_tailp = &p->p_forw) = 0; 461 p->p_stat = SSLEEP; 462 p->p_stats->p_ru.ru_nvcsw++; 463#ifdef KTRACE 464 if (KTRPOINT(p, KTR_CSW)) 465 ktrcsw(p->p_tracep, 1, 0); 466#endif 467 mi_switch(); 468#ifdef KTRACE 469 if (KTRPOINT(p, KTR_CSW)) 470 ktrcsw(p->p_tracep, 0, 0); 471#endif 472 curpriority = p->p_usrpri; 473 splx(s); 474} 475 476/* 477 * Remove a process from its wait queue 478 */ 479void 480unsleep(p) 481 register struct proc *p; 482{ 483 register struct slpque *qp; 484 register struct proc **hp; 485 int s; 486 487 s = splhigh(); 488 if (p->p_wchan) { 489 hp = &(qp = &slpque[LOOKUP(p->p_wchan)])->sq_head; 490 while (*hp != p) 491 hp = &(*hp)->p_forw; 492 *hp = p->p_forw; 493 if (qp->sq_tailp == &p->p_forw) 494 qp->sq_tailp = hp; 495 p->p_wchan = 0; 496 } 497 splx(s); 498} 499 500/* 501 * Make all processes sleeping on the specified identifier runnable. 502 */ 503void 504wakeup(ident) 505 register void *ident; 506{ 507 register struct slpque *qp; 508 register struct proc *p, **q; 509 int s; 510 511 s = splhigh(); 512 qp = &slpque[LOOKUP(ident)]; 513restart: 514 for (q = &qp->sq_head; *q; ) { 515 p = *q; 516#ifdef DIAGNOSTIC 517 if (p->p_back || (p->p_stat != SSLEEP && p->p_stat != SSTOP)) 518 panic("wakeup"); 519#endif 520 if (p->p_wchan == ident) { 521 p->p_wchan = 0; 522 *q = p->p_forw; 523 if (qp->sq_tailp == &p->p_forw) 524 qp->sq_tailp = q; 525 if (p->p_stat == SSLEEP) { 526 /* OPTIMIZED EXPANSION OF setrunnable(p); */ 527 if (p->p_slptime > 1) 528 updatepri(p); 529 p->p_slptime = 0; 530 p->p_stat = SRUN; 531 if (p->p_flag & P_INMEM) 532 setrunqueue(p); 533 /* 534 * Since curpriority is a user priority, 535 * p->p_priority is always better than 536 * curpriority. 537 */ 538 if ((p->p_flag & P_INMEM) == 0) 539 wakeup((caddr_t)&proc0); 540 else 541 need_resched(); 542 /* END INLINE EXPANSION */ 543 goto restart; 544 } 545 } else 546 q = &p->p_forw; 547 } 548 splx(s); 549} 550 551/* 552 * The machine independent parts of mi_switch(). 553 * Must be called at splstatclock() or higher. 554 */ 555void 556mi_switch() 557{ 558 register struct proc *p = curproc; /* XXX */ 559 register struct rlimit *rlim; 560 register long s, u; 561 struct timeval tv; 562 563#ifdef DEBUG 564 if (p->p_simple_locks) 565 panic("sleep: holding simple lock"); 566#endif 567 /* 568 * Compute the amount of time during which the current 569 * process was running, and add that to its total so far. 570 */ 571 microtime(&tv); 572 u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec); 573 s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec); 574 if (u < 0) { 575 u += 1000000; 576 s--; 577 } else if (u >= 1000000) { 578 u -= 1000000; 579 s++; 580 } 581 p->p_rtime.tv_usec = u; 582 p->p_rtime.tv_sec = s; 583 584 /* 585 * Check if the process exceeds its cpu resource allocation. 586 * If over max, kill it. In any case, if it has run for more 587 * than 10 minutes, reduce priority to give others a chance. 588 */ 589 if (p->p_stat != SZOMB) { 590 rlim = &p->p_rlimit[RLIMIT_CPU]; 591 if (s >= rlim->rlim_cur) { 592 if (s >= rlim->rlim_max) 593 killproc(p, "exceeded maximum CPU limit"); 594 else { 595 psignal(p, SIGXCPU); 596 if (rlim->rlim_cur < rlim->rlim_max) 597 rlim->rlim_cur += 5; 598 } 599 } 600 if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) { 601 p->p_nice = NZERO + 4; 602 resetpriority(p); 603 } 604 } 605 606 /* 607 * Pick a new current process and record its start time. 608 */ 609 cnt.v_swtch++; 610 cpu_switch(p); 611 microtime(&runtime); 612} 613 614/* 615 * Initialize the (doubly-linked) run queues 616 * to be empty. 617 */ 618/* ARGSUSED*/ 619static void 620rqinit(dummy) 621 void *dummy; 622{ 623 register int i; 624 625 for (i = 0; i < NQS; i++) { 626 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 627 rtqs[i].ph_link = rtqs[i].ph_rlink = (struct proc *)&rtqs[i]; 628 idqs[i].ph_link = idqs[i].ph_rlink = (struct proc *)&idqs[i]; 629 } 630} 631 632/* 633 * Change process state to be runnable, 634 * placing it on the run queue if it is in memory, 635 * and awakening the swapper if it isn't in memory. 636 */ 637void 638setrunnable(p) 639 register struct proc *p; 640{ 641 register int s; 642 643 s = splhigh(); 644 switch (p->p_stat) { 645 case 0: 646 case SRUN: 647 case SZOMB: 648 default: 649 panic("setrunnable"); 650 case SSTOP: 651 case SSLEEP: 652 unsleep(p); /* e.g. when sending signals */ 653 break; 654 655 case SIDL: 656 break; 657 } 658 p->p_stat = SRUN; 659 if (p->p_flag & P_INMEM) 660 setrunqueue(p); 661 splx(s); 662 if (p->p_slptime > 1) 663 updatepri(p); 664 p->p_slptime = 0; 665 if ((p->p_flag & P_INMEM) == 0) 666 wakeup((caddr_t)&proc0); 667 else if (p->p_priority < curpriority) 668 need_resched(); 669} 670 671/* 672 * Compute the priority of a process when running in user mode. 673 * Arrange to reschedule if the resulting priority is better 674 * than that of the current process. 675 */ 676void 677resetpriority(p) 678 register struct proc *p; 679{ 680 register unsigned int newpriority; 681 682 if (p->p_rtprio.type == RTP_PRIO_NORMAL) { 683 newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice; 684 newpriority = min(newpriority, MAXPRI); 685 p->p_usrpri = newpriority; 686 if (newpriority < curpriority) 687 need_resched(); 688 } else { 689 need_resched(); 690 } 691} 692