1*4527Swnj /* kern_clock.c 4.26 81/10/16 */ 29Sbill 39Sbill #include "../h/param.h" 49Sbill #include "../h/systm.h" 5329Sbill #include "../h/dk.h" 62768Swnj #include "../h/callout.h" 79Sbill #include "../h/seg.h" 89Sbill #include "../h/dir.h" 99Sbill #include "../h/user.h" 109Sbill #include "../h/proc.h" 119Sbill #include "../h/reg.h" 129Sbill #include "../h/psl.h" 139Sbill #include "../h/vm.h" 149Sbill #include "../h/buf.h" 159Sbill #include "../h/text.h" 16877Sbill #include "../h/vlimit.h" 17877Sbill #include "../h/mtpr.h" 18877Sbill #include "../h/clock.h" 192689Swnj #include "../h/cpu.h" 209Sbill 213511Sroot #include "bk.h" 221943Swnj #include "dh.h" 231943Swnj #include "dz.h" 241559Sbill 259Sbill /* 262442Swnj * Hardclock is called straight from 279Sbill * the real time clock interrupt. 282442Swnj * We limit the work we do at real clock interrupt time to: 292442Swnj * reloading clock 302442Swnj * decrementing time to callouts 312442Swnj * recording cpu time usage 322450Swnj * modifying priority of current process 332442Swnj * arrange for soft clock interrupt 342442Swnj * kernel pc profiling 359Sbill * 363110Swnj * At software (softclock) interrupt time we: 379Sbill * implement callouts 389Sbill * maintain date 399Sbill * lightning bolt wakeup (every second) 409Sbill * alarm clock signals 419Sbill * jab the scheduler 422442Swnj * 432442Swnj * On the vax softclock interrupts are implemented by 442442Swnj * software interrupts. Note that we may have multiple softclock 452442Swnj * interrupts compressed into one (due to excessive interrupt load), 462442Swnj * but that hardclock interrupts should never be lost. 479Sbill */ 48*4527Swnj int kcounts[20000]; 49*4527Swnj int kprof = 1; 509Sbill 512609Swnj /*ARGSUSED*/ 522442Swnj hardclock(pc, ps) 532450Swnj caddr_t pc; 549Sbill { 552768Swnj register struct callout *p1; 569Sbill register struct proc *pp; 572442Swnj register int s, cpstate; 589Sbill 599Sbill /* 609Sbill * reprime clock 619Sbill */ 629Sbill clkreld(); 639Sbill 649Sbill /* 652442Swnj * update callout times 669Sbill */ 673542Swnj for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next) 683542Swnj ; 693542Swnj if (p1) 703542Swnj p1->c_time--; 71138Sbill 72138Sbill /* 732442Swnj * Maintain iostat and per-process cpu statistics 74138Sbill */ 759Sbill if (!noproc) { 769Sbill s = u.u_procp->p_rssize; 779Sbill u.u_vm.vm_idsrss += s; 789Sbill if (u.u_procp->p_textp) { 799Sbill register int xrss = u.u_procp->p_textp->x_rssize; 809Sbill 819Sbill s += xrss; 829Sbill u.u_vm.vm_ixrss += xrss; 839Sbill } 849Sbill if (s > u.u_vm.vm_maxrss) 859Sbill u.u_vm.vm_maxrss = s; 862768Swnj if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/hz > u.u_limit[LIM_CPU]) { 87375Sbill psignal(u.u_procp, SIGXCPU); 88375Sbill if (u.u_limit[LIM_CPU] < INFINITY - 5) 89375Sbill u.u_limit[LIM_CPU] += 5; 90375Sbill } 919Sbill } 923110Swnj /* 933110Swnj * Update iostat information. 943110Swnj */ 959Sbill if (USERMODE(ps)) { 969Sbill u.u_vm.vm_utime++; 979Sbill if(u.u_procp->p_nice > NZERO) 98305Sbill cpstate = CP_NICE; 99305Sbill else 100305Sbill cpstate = CP_USER; 1019Sbill } else { 102*4527Swnj int k = ((int)pc & 0x7fffffff) / 8; 103*4527Swnj if (k < 20000) 104*4527Swnj kcounts[k]++; 105305Sbill cpstate = CP_SYS; 1069Sbill if (noproc) 107305Sbill cpstate = CP_IDLE; 1089Sbill else 1099Sbill u.u_vm.vm_stime++; 1109Sbill } 1111408Sbill cp_time[cpstate]++; 1122442Swnj for (s = 0; s < DK_NDRIVE; s++) 1132442Swnj if (dk_busy&(1<<s)) 1142442Swnj dk_time[s]++; 1153110Swnj /* 1163110Swnj * Adjust priority of current process. 1173110Swnj */ 1189Sbill if (!noproc) { 1199Sbill pp = u.u_procp; 1201399Sbill pp->p_cpticks++; 1219Sbill if(++pp->p_cpu == 0) 1229Sbill pp->p_cpu--; 1233876Swnj if(pp->p_cpu % 4 == 0) { 124125Sbill (void) setpri(pp); 1259Sbill if (pp->p_pri >= PUSER) 1269Sbill pp->p_pri = pp->p_usrpri; 1279Sbill } 1289Sbill } 1293110Swnj /* 1303110Swnj * Time moves on. 1313110Swnj */ 1329Sbill ++lbolt; 1332689Swnj #if VAX780 1343110Swnj /* 1353110Swnj * On 780's, impelement a fast UBA watcher, 1363110Swnj * to make sure uba's don't get stuck. 1373110Swnj */ 1382872Swnj if (cpu == VAX_780 && panicstr == 0 && !BASEPRI(ps)) 1392442Swnj unhang(); 1402442Swnj #endif 1413110Swnj /* 1423110Swnj * Schedule a software interrupt for the rest 1433110Swnj * of clock activities. 1443110Swnj */ 1452442Swnj setsoftclock(); 1462442Swnj } 1472442Swnj 1482442Swnj /* 1493876Swnj * The digital decay cpu usage priority assignment is scaled to run in 1503876Swnj * time as expanded by the 1 minute load average. Each second we 1513876Swnj * multiply the the previous cpu usage estimate by 1523876Swnj * nrscale*avenrun[0] 1533876Swnj * The following relates the load average to the period over which 1543876Swnj * cpu usage is 90% forgotten: 1553876Swnj * loadav 1 5 seconds 1563876Swnj * loadav 5 24 seconds 1573876Swnj * loadav 10 47 seconds 1583876Swnj * loadav 20 93 seconds 1593876Swnj * This is a great improvement on the previous algorithm which 1603876Swnj * decayed the priorities by a constant, and decayed away all knowledge 1613876Swnj * of previous activity in about 20 seconds. Under heavy load, 1623876Swnj * the previous algorithm degenerated to round-robin with poor response 1633876Swnj * time when there was a high load average. 1642442Swnj */ 1653984Sroot #undef ave 1663876Swnj #define ave(a,b) ((int)(((int)(a*b))/(b+1))) 1673876Swnj int nrscale = 2; 1683876Swnj double avenrun[]; 1693110Swnj 1703110Swnj /* 1713110Swnj * Constant for decay filter for cpu usage field 1723110Swnj * in process table (used by ps au). 1733110Swnj */ 1742442Swnj double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 1752442Swnj 1762442Swnj /* 1772442Swnj * Software clock interrupt. 1783110Swnj * This routine runs at lower priority than device interrupts. 1792442Swnj */ 1802609Swnj /*ARGSUSED*/ 1812442Swnj softclock(pc, ps) 1822450Swnj caddr_t pc; 1832442Swnj { 1843615Sroot register struct callout *p1; 1852442Swnj register struct proc *pp; 1862442Swnj register int a, s; 1873542Swnj caddr_t arg; 1883542Swnj int (*func)(); 1892442Swnj 1902442Swnj /* 1912872Swnj * Perform callouts (but not after panic's!) 1922442Swnj */ 1933542Swnj if (panicstr == 0) { 1943542Swnj for (;;) { 1953542Swnj s = spl7(); 1964250Swnj if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 1974250Swnj splx(s); 1983542Swnj break; 1994250Swnj } 2003542Swnj calltodo.c_next = p1->c_next; 2013542Swnj arg = p1->c_arg; 2023542Swnj func = p1->c_func; 2033542Swnj p1->c_next = callfree; 2043542Swnj callfree = p1; 2053542Swnj (void) splx(s); 2063542Swnj (*func)(arg); 2072442Swnj } 2082442Swnj } 2092442Swnj 2102442Swnj /* 2112442Swnj * Drain silos. 2122442Swnj */ 2132647Swnj #if NDH > 0 2142442Swnj s = spl5(); dhtimer(); splx(s); 2152442Swnj #endif 2162647Swnj #if NDZ > 0 2172442Swnj s = spl5(); dztimer(); splx(s); 2182442Swnj #endif 2192442Swnj 2202442Swnj /* 2212450Swnj * If idling and processes are waiting to swap in, 2222450Swnj * check on them. 2232450Swnj */ 2242450Swnj if (noproc && runin) { 2252450Swnj runin = 0; 2262450Swnj wakeup((caddr_t)&runin); 2272450Swnj } 2282450Swnj 2292450Swnj /* 2303876Swnj * Run paging daemon every 1/4 sec. 2312442Swnj */ 2322768Swnj if (lbolt % (hz/4) == 0) { 2339Sbill vmpago(); 2343876Swnj } 2353876Swnj 2363876Swnj /* 2373876Swnj * Reschedule every 1/10 sec. 2383876Swnj */ 2393876Swnj if (lbolt % (hz/10) == 0) { 2409Sbill runrun++; 2412442Swnj aston(); 2429Sbill } 2432442Swnj 2442442Swnj /* 2452442Swnj * Lightning bolt every second: 2462442Swnj * sleep timeouts 2472442Swnj * process priority recomputation 2482442Swnj * process %cpu averaging 2492442Swnj * virtual memory metering 2502442Swnj * kick swapper if processes want in 2512442Swnj */ 2522768Swnj if (lbolt >= hz) { 2532872Swnj /* 2543110Swnj * This doesn't mean much on VAX since we run at 2552872Swnj * software interrupt time... if hardclock() 2562872Swnj * calls softclock() directly, it prevents 2572872Swnj * this code from running when the priority 2582872Swnj * was raised when the clock interrupt occurred. 2592872Swnj */ 2609Sbill if (BASEPRI(ps)) 2619Sbill return; 2622872Swnj 2632872Swnj /* 2642872Swnj * If we didn't run a few times because of 2652872Swnj * long blockage at high ipl, we don't 2662872Swnj * really want to run this code several times, 2672872Swnj * so squish out all multiples of hz here. 2682872Swnj */ 2692872Swnj time += lbolt / hz; 2702872Swnj lbolt %= hz; 2712872Swnj 2722872Swnj /* 2732872Swnj * Wakeup lightning bolt sleepers. 2742872Swnj * Processes sleep on lbolt to wait 2752872Swnj * for short amounts of time (e.g. 1 second). 2762872Swnj */ 2779Sbill wakeup((caddr_t)&lbolt); 2782872Swnj 2792872Swnj /* 2802872Swnj * Recompute process priority and process 2812872Swnj * sleep() system calls as well as internal 2822872Swnj * sleeps with timeouts (tsleep() kernel routine). 2832872Swnj */ 2842872Swnj for (pp = proc; pp < procNPROC; pp++) 285928Sbill if (pp->p_stat && pp->p_stat!=SZOMB) { 2862872Swnj /* 2872872Swnj * Increase resident time, to max of 127 seconds 2882872Swnj * (it is kept in a character.) For 2892872Swnj * loaded processes this is time in core; for 2902872Swnj * swapped processes, this is time on drum. 2912872Swnj */ 2922872Swnj if (pp->p_time != 127) 2939Sbill pp->p_time++; 2942872Swnj /* 2952872Swnj * If process has clock counting down, and it 2962872Swnj * expires, set it running (if this is a tsleep()), 2972872Swnj * or give it an SIGALRM (if the user process 2982872Swnj * is using alarm signals. 2992872Swnj */ 3002872Swnj if (pp->p_clktim && --pp->p_clktim == 0) 3012872Swnj if (pp->p_flag & STIMO) { 3022872Swnj s = spl6(); 3032872Swnj switch (pp->p_stat) { 304204Sbill 3052872Swnj case SSLEEP: 3062872Swnj setrun(pp); 3072872Swnj break; 308204Sbill 3092872Swnj case SSTOP: 3102872Swnj unsleep(pp); 3112872Swnj break; 3122872Swnj } 3132872Swnj pp->p_flag &= ~STIMO; 3142872Swnj splx(s); 3152872Swnj } else 3162872Swnj psignal(pp, SIGALRM); 3172872Swnj /* 3182872Swnj * If process is blocked, increment computed 3192872Swnj * time blocked. This is used in swap scheduling. 3202872Swnj */ 3212872Swnj if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP) 3229Sbill if (pp->p_slptime != 127) 3239Sbill pp->p_slptime++; 3242872Swnj /* 3252872Swnj * Update digital filter estimation of process 3262872Swnj * cpu utilization for loaded processes. 3272872Swnj */ 3281399Sbill if (pp->p_flag&SLOAD) 3291399Sbill pp->p_pctcpu = ccpu * pp->p_pctcpu + 3302768Swnj (1.0 - ccpu) * (pp->p_cpticks/(float)hz); 3312872Swnj /* 3322872Swnj * Recompute process priority. The number p_cpu 3332872Swnj * is a weighted estimate of cpu time consumed. 3342872Swnj * A process which consumes cpu time has this 3352872Swnj * increase regularly. We here decrease it by 3363876Swnj * a fraction based on load average giving a digital 3373876Swnj * decay filter which damps out in about 5 seconds 3383876Swnj * when seconds are measured in time expanded by the 3393876Swnj * load average. 3402872Swnj * 3412872Swnj * If a process is niced, then the nice directly 3422872Swnj * affects the new priority. The final priority 3432872Swnj * is in the range 0 to 255, to fit in a character. 3442872Swnj */ 3451399Sbill pp->p_cpticks = 0; 3463876Swnj a = ave((pp->p_cpu & 0377), avenrun[0]*nrscale) + 3473876Swnj pp->p_nice - NZERO; 3482872Swnj if (a < 0) 3499Sbill a = 0; 3502872Swnj if (a > 255) 3519Sbill a = 255; 3529Sbill pp->p_cpu = a; 353125Sbill (void) setpri(pp); 3542872Swnj /* 3552872Swnj * Now have computed new process priority 3562872Swnj * in p->p_usrpri. Carefully change p->p_pri. 3572872Swnj * A process is on a run queue associated with 3582872Swnj * this priority, so we must block out process 3592872Swnj * state changes during the transition. 3602872Swnj */ 3619Sbill s = spl6(); 3622872Swnj if (pp->p_pri >= PUSER) { 3639Sbill if ((pp != u.u_procp || noproc) && 3649Sbill pp->p_stat == SRUN && 3659Sbill (pp->p_flag & SLOAD) && 3669Sbill pp->p_pri != pp->p_usrpri) { 3679Sbill remrq(pp); 3689Sbill pp->p_pri = pp->p_usrpri; 3699Sbill setrq(pp); 3709Sbill } else 3719Sbill pp->p_pri = pp->p_usrpri; 3729Sbill } 3739Sbill splx(s); 3749Sbill } 3752872Swnj 3762872Swnj /* 3772872Swnj * Perform virtual memory metering. 3782872Swnj */ 3799Sbill vmmeter(); 3802872Swnj 3812872Swnj /* 3822872Swnj * If the swap process is trying to bring 3832872Swnj * a process in, have it look again to see 3842872Swnj * if it is possible now. 3852872Swnj */ 3862872Swnj if (runin!=0) { 3879Sbill runin = 0; 3889Sbill wakeup((caddr_t)&runin); 3899Sbill } 3902872Swnj 3919Sbill /* 3929Sbill * If there are pages that have been cleaned, 3939Sbill * jolt the pageout daemon to process them. 3949Sbill * We do this here so that these pages will be 3959Sbill * freed if there is an abundance of memory and the 3969Sbill * daemon would not be awakened otherwise. 3979Sbill */ 3989Sbill if (bclnlist != NULL) 3999Sbill wakeup((caddr_t)&proc[2]); 4002872Swnj 4012872Swnj /* 4022872Swnj * If the trap occurred from usermode, 4032872Swnj * then check to see if it has now been 4042872Swnj * running more than 10 minutes of user time 4052872Swnj * and should thus run with reduced priority 4062872Swnj * to give other processes a chance. 4072872Swnj */ 4089Sbill if (USERMODE(ps)) { 4099Sbill pp = u.u_procp; 4102872Swnj if (pp->p_uid && pp->p_nice == NZERO && 4112872Swnj u.u_vm.vm_utime > 600 * hz) 4122872Swnj pp->p_nice = NZERO+4; 413125Sbill (void) setpri(pp); 4149Sbill pp->p_pri = pp->p_usrpri; 4159Sbill } 4169Sbill } 4172872Swnj /* 4182872Swnj * If trapped user-mode, give it a profiling tick. 4192872Swnj */ 4202442Swnj if (USERMODE(ps) && u.u_prof.pr_scale) { 4212442Swnj u.u_procp->p_flag |= SOWEUPC; 4222442Swnj aston(); 4239Sbill } 4249Sbill } 4259Sbill 4269Sbill /* 4273110Swnj * Timeout is called to arrange that 4282768Swnj * fun(arg) is called in tim/hz seconds. 4293542Swnj * An entry is linked into the callout 4303110Swnj * structure. The time in each structure 4312768Swnj * entry is the number of hz's more 4329Sbill * than the previous entry. 4339Sbill * In this way, decrementing the 4349Sbill * first entry has the effect of 4359Sbill * updating all entries. 4369Sbill * 4379Sbill * The panic is there because there is nothing 4389Sbill * intelligent to be done if an entry won't fit. 4399Sbill */ 4409Sbill timeout(fun, arg, tim) 4412450Swnj int (*fun)(); 4422450Swnj caddr_t arg; 4439Sbill { 4443542Swnj register struct callout *p1, *p2, *pnew; 4459Sbill register int t; 4469Sbill int s; 4479Sbill 4483446Sroot /* DEBUGGING CODE */ 4493446Sroot int ttrstrt(); 4503446Sroot 4513446Sroot if (fun == ttrstrt && arg == 0) 4523446Sroot panic("timeout ttrstr arg"); 4533446Sroot /* END DEBUGGING CODE */ 4549Sbill t = tim; 4559Sbill s = spl7(); 4563542Swnj pnew = callfree; 4573542Swnj if (pnew == NULL) 4583542Swnj panic("timeout table overflow"); 4593542Swnj callfree = pnew->c_next; 4603542Swnj pnew->c_arg = arg; 4613542Swnj pnew->c_func = fun; 4623542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 4633542Swnj t -= p2->c_time; 4643542Swnj p1->c_next = pnew; 4653542Swnj pnew->c_next = p2; 4663542Swnj pnew->c_time = t; 4673542Swnj if (p2) 4683542Swnj p2->c_time -= t; 4699Sbill splx(s); 4709Sbill } 471