1*7315Ssam /* kern_clock.c 4.32 82/06/27 */ 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" 205247Sroot #include "../h/protosw.h" 219Sbill 223511Sroot #include "bk.h" 231943Swnj #include "dh.h" 241943Swnj #include "dz.h" 257305Ssam #include "ps.h" 261559Sbill 279Sbill /* 282442Swnj * Hardclock is called straight from 299Sbill * the real time clock interrupt. 302442Swnj * We limit the work we do at real clock interrupt time to: 312442Swnj * reloading clock 322442Swnj * decrementing time to callouts 332442Swnj * recording cpu time usage 342450Swnj * modifying priority of current process 352442Swnj * arrange for soft clock interrupt 362442Swnj * kernel pc profiling 379Sbill * 383110Swnj * At software (softclock) interrupt time we: 399Sbill * implement callouts 409Sbill * maintain date 419Sbill * lightning bolt wakeup (every second) 429Sbill * alarm clock signals 439Sbill * jab the scheduler 442442Swnj * 452442Swnj * On the vax softclock interrupts are implemented by 462442Swnj * software interrupts. Note that we may have multiple softclock 472442Swnj * interrupts compressed into one (due to excessive interrupt load), 482442Swnj * but that hardclock interrupts should never be lost. 499Sbill */ 504968Swnj #ifdef KPROF 514527Swnj int kcounts[20000]; 524968Swnj #endif 539Sbill 545247Sroot /* 555247Sroot * Protoslow is like lbolt, but for slow protocol timeouts, counting 565247Sroot * up to (hz/PR_SLOWHZ), then causing a pfslowtimo(). 575247Sroot * Protofast is like lbolt, but for fast protocol timeouts, counting 585247Sroot * up to (hz/PR_FASTHZ), then causing a pffasttimo(). 595247Sroot */ 605247Sroot int protoslow; 615247Sroot int protofast; 625247Sroot 632609Swnj /*ARGSUSED*/ 642442Swnj hardclock(pc, ps) 652450Swnj caddr_t pc; 669Sbill { 672768Swnj register struct callout *p1; 689Sbill register struct proc *pp; 692442Swnj register int s, cpstate; 709Sbill 719Sbill /* 729Sbill * reprime clock 739Sbill */ 749Sbill clkreld(); 759Sbill 767305Ssam #if NPS > 0 779Sbill /* 787305Ssam * sync referesh of picture system 797305Ssam */ 807305Ssam psextsync(pc, ps); 817305Ssam #endif 827305Ssam 837305Ssam /* 842442Swnj * update callout times 859Sbill */ 863542Swnj for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next) 873542Swnj ; 883542Swnj if (p1) 893542Swnj p1->c_time--; 90138Sbill 91138Sbill /* 922442Swnj * Maintain iostat and per-process cpu statistics 93138Sbill */ 949Sbill if (!noproc) { 959Sbill s = u.u_procp->p_rssize; 969Sbill u.u_vm.vm_idsrss += s; 979Sbill if (u.u_procp->p_textp) { 989Sbill register int xrss = u.u_procp->p_textp->x_rssize; 999Sbill 1009Sbill s += xrss; 1019Sbill u.u_vm.vm_ixrss += xrss; 1029Sbill } 1039Sbill if (s > u.u_vm.vm_maxrss) 1049Sbill u.u_vm.vm_maxrss = s; 1052768Swnj if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/hz > u.u_limit[LIM_CPU]) { 106375Sbill psignal(u.u_procp, SIGXCPU); 107375Sbill if (u.u_limit[LIM_CPU] < INFINITY - 5) 108375Sbill u.u_limit[LIM_CPU] += 5; 109375Sbill } 1109Sbill } 1113110Swnj /* 1123110Swnj * Update iostat information. 1133110Swnj */ 1149Sbill if (USERMODE(ps)) { 1159Sbill u.u_vm.vm_utime++; 1169Sbill if(u.u_procp->p_nice > NZERO) 117305Sbill cpstate = CP_NICE; 118305Sbill else 119305Sbill cpstate = CP_USER; 1209Sbill } else { 1214968Swnj #ifdef KPROF 1224968Swnj int k = ((int)pc & 0x7fffffff) / 8; 1234968Swnj if (k < 20000) 1244968Swnj kcounts[k]++; 1254968Swnj #endif 126305Sbill cpstate = CP_SYS; 127*7315Ssam if (noproc) { 128*7315Ssam if ((ps&PSL_IPL) != 0) 129*7315Ssam cpstate = CP_IDLE; 130*7315Ssam } else 1319Sbill u.u_vm.vm_stime++; 1329Sbill } 1331408Sbill cp_time[cpstate]++; 1342442Swnj for (s = 0; s < DK_NDRIVE; s++) 1352442Swnj if (dk_busy&(1<<s)) 1362442Swnj dk_time[s]++; 1373110Swnj /* 1383110Swnj * Adjust priority of current process. 1393110Swnj */ 1409Sbill if (!noproc) { 1419Sbill pp = u.u_procp; 1421399Sbill pp->p_cpticks++; 1439Sbill if(++pp->p_cpu == 0) 1449Sbill pp->p_cpu--; 1453876Swnj if(pp->p_cpu % 4 == 0) { 146125Sbill (void) setpri(pp); 1479Sbill if (pp->p_pri >= PUSER) 1489Sbill pp->p_pri = pp->p_usrpri; 1499Sbill } 1509Sbill } 1513110Swnj /* 1523110Swnj * Time moves on. 1533110Swnj */ 1549Sbill ++lbolt; 1555247Sroot 1565247Sroot /* 1575247Sroot * Time moves on for protocols. 1585247Sroot */ 1595264Swnj --protoslow; --protofast; 1605247Sroot 1612689Swnj #if VAX780 1623110Swnj /* 1633110Swnj * On 780's, impelement a fast UBA watcher, 1643110Swnj * to make sure uba's don't get stuck. 1653110Swnj */ 1662872Swnj if (cpu == VAX_780 && panicstr == 0 && !BASEPRI(ps)) 1672442Swnj unhang(); 1682442Swnj #endif 1693110Swnj /* 1703110Swnj * Schedule a software interrupt for the rest 1713110Swnj * of clock activities. 1723110Swnj */ 1732442Swnj setsoftclock(); 1742442Swnj } 1752442Swnj 1762442Swnj /* 1773876Swnj * The digital decay cpu usage priority assignment is scaled to run in 1783876Swnj * time as expanded by the 1 minute load average. Each second we 1793876Swnj * multiply the the previous cpu usage estimate by 1803876Swnj * nrscale*avenrun[0] 1813876Swnj * The following relates the load average to the period over which 1823876Swnj * cpu usage is 90% forgotten: 1833876Swnj * loadav 1 5 seconds 1843876Swnj * loadav 5 24 seconds 1853876Swnj * loadav 10 47 seconds 1863876Swnj * loadav 20 93 seconds 1873876Swnj * This is a great improvement on the previous algorithm which 1883876Swnj * decayed the priorities by a constant, and decayed away all knowledge 1893876Swnj * of previous activity in about 20 seconds. Under heavy load, 1903876Swnj * the previous algorithm degenerated to round-robin with poor response 1913876Swnj * time when there was a high load average. 1922442Swnj */ 1933984Sroot #undef ave 1943876Swnj #define ave(a,b) ((int)(((int)(a*b))/(b+1))) 1953876Swnj int nrscale = 2; 1963876Swnj double avenrun[]; 1973110Swnj 1983110Swnj /* 1993110Swnj * Constant for decay filter for cpu usage field 2003110Swnj * in process table (used by ps au). 2013110Swnj */ 2022442Swnj double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 2032442Swnj 2042442Swnj /* 2052442Swnj * Software clock interrupt. 2063110Swnj * This routine runs at lower priority than device interrupts. 2072442Swnj */ 2082609Swnj /*ARGSUSED*/ 2092442Swnj softclock(pc, ps) 2102450Swnj caddr_t pc; 2112442Swnj { 2123615Sroot register struct callout *p1; 2132442Swnj register struct proc *pp; 2142442Swnj register int a, s; 2153542Swnj caddr_t arg; 2163542Swnj int (*func)(); 2172442Swnj 2182442Swnj /* 2192872Swnj * Perform callouts (but not after panic's!) 2202442Swnj */ 2213542Swnj if (panicstr == 0) { 2223542Swnj for (;;) { 2233542Swnj s = spl7(); 2244250Swnj if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 2254250Swnj splx(s); 2263542Swnj break; 2274250Swnj } 2283542Swnj calltodo.c_next = p1->c_next; 2293542Swnj arg = p1->c_arg; 2303542Swnj func = p1->c_func; 2313542Swnj p1->c_next = callfree; 2323542Swnj callfree = p1; 2333542Swnj (void) splx(s); 2343542Swnj (*func)(arg); 2352442Swnj } 2362442Swnj } 2372442Swnj 2382442Swnj /* 2392442Swnj * Drain silos. 2402442Swnj */ 2412647Swnj #if NDH > 0 2422442Swnj s = spl5(); dhtimer(); splx(s); 2432442Swnj #endif 2442647Swnj #if NDZ > 0 2452442Swnj s = spl5(); dztimer(); splx(s); 2462442Swnj #endif 2472442Swnj 2482442Swnj /* 2492450Swnj * If idling and processes are waiting to swap in, 2502450Swnj * check on them. 2512450Swnj */ 2522450Swnj if (noproc && runin) { 2532450Swnj runin = 0; 2542450Swnj wakeup((caddr_t)&runin); 2552450Swnj } 2562450Swnj 2572450Swnj /* 2583876Swnj * Run paging daemon every 1/4 sec. 2592442Swnj */ 2602768Swnj if (lbolt % (hz/4) == 0) { 2619Sbill vmpago(); 2623876Swnj } 2633876Swnj 2643876Swnj /* 2653876Swnj * Reschedule every 1/10 sec. 2663876Swnj */ 2673876Swnj if (lbolt % (hz/10) == 0) { 2689Sbill runrun++; 2692442Swnj aston(); 2709Sbill } 2712442Swnj 2722442Swnj /* 2735247Sroot * Run network slow and fast timeouts. 2745247Sroot */ 2755264Swnj if (protofast <= 0) { 2765264Swnj protofast = hz / PR_FASTHZ; 2775247Sroot pffasttimo(); 2785264Swnj } 2795264Swnj if (protoslow <= 0) { 2805264Swnj protoslow = hz / PR_SLOWHZ; 2815247Sroot pfslowtimo(); 2825264Swnj } 2835247Sroot 2845247Sroot /* 2852442Swnj * Lightning bolt every second: 2862442Swnj * sleep timeouts 2872442Swnj * process priority recomputation 2882442Swnj * process %cpu averaging 2892442Swnj * virtual memory metering 2902442Swnj * kick swapper if processes want in 2912442Swnj */ 2922768Swnj if (lbolt >= hz) { 2932872Swnj /* 2943110Swnj * This doesn't mean much on VAX since we run at 2952872Swnj * software interrupt time... if hardclock() 2962872Swnj * calls softclock() directly, it prevents 2972872Swnj * this code from running when the priority 2982872Swnj * was raised when the clock interrupt occurred. 2992872Swnj */ 3009Sbill if (BASEPRI(ps)) 3019Sbill return; 3022872Swnj 3032872Swnj /* 3042872Swnj * If we didn't run a few times because of 3052872Swnj * long blockage at high ipl, we don't 3062872Swnj * really want to run this code several times, 3072872Swnj * so squish out all multiples of hz here. 3082872Swnj */ 309*7315Ssam s = spl6(); 310*7315Ssam time += lbolt / hz; lbolt %= hz; 311*7315Ssam splx(s); 3122872Swnj 3132872Swnj /* 3142872Swnj * Wakeup lightning bolt sleepers. 3152872Swnj * Processes sleep on lbolt to wait 3162872Swnj * for short amounts of time (e.g. 1 second). 3172872Swnj */ 3189Sbill wakeup((caddr_t)&lbolt); 3192872Swnj 3202872Swnj /* 3212872Swnj * Recompute process priority and process 3222872Swnj * sleep() system calls as well as internal 3232872Swnj * sleeps with timeouts (tsleep() kernel routine). 3242872Swnj */ 3252872Swnj for (pp = proc; pp < procNPROC; pp++) 326928Sbill if (pp->p_stat && pp->p_stat!=SZOMB) { 3272872Swnj /* 3282872Swnj * Increase resident time, to max of 127 seconds 3292872Swnj * (it is kept in a character.) For 3302872Swnj * loaded processes this is time in core; for 3312872Swnj * swapped processes, this is time on drum. 3322872Swnj */ 3332872Swnj if (pp->p_time != 127) 3349Sbill pp->p_time++; 3352872Swnj /* 3362872Swnj * If process has clock counting down, and it 3372872Swnj * expires, set it running (if this is a tsleep()), 3382872Swnj * or give it an SIGALRM (if the user process 3392872Swnj * is using alarm signals. 3402872Swnj */ 3412872Swnj if (pp->p_clktim && --pp->p_clktim == 0) 3422872Swnj if (pp->p_flag & STIMO) { 3432872Swnj s = spl6(); 3442872Swnj switch (pp->p_stat) { 345204Sbill 3462872Swnj case SSLEEP: 3472872Swnj setrun(pp); 3482872Swnj break; 349204Sbill 3502872Swnj case SSTOP: 3512872Swnj unsleep(pp); 3522872Swnj break; 3532872Swnj } 3542872Swnj pp->p_flag &= ~STIMO; 3552872Swnj splx(s); 3562872Swnj } else 3572872Swnj psignal(pp, SIGALRM); 3582872Swnj /* 3592872Swnj * If process is blocked, increment computed 3602872Swnj * time blocked. This is used in swap scheduling. 3612872Swnj */ 3622872Swnj if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP) 3639Sbill if (pp->p_slptime != 127) 3649Sbill pp->p_slptime++; 3652872Swnj /* 3662872Swnj * Update digital filter estimation of process 3672872Swnj * cpu utilization for loaded processes. 3682872Swnj */ 3691399Sbill if (pp->p_flag&SLOAD) 3701399Sbill pp->p_pctcpu = ccpu * pp->p_pctcpu + 3712768Swnj (1.0 - ccpu) * (pp->p_cpticks/(float)hz); 3722872Swnj /* 3732872Swnj * Recompute process priority. The number p_cpu 3742872Swnj * is a weighted estimate of cpu time consumed. 3752872Swnj * A process which consumes cpu time has this 3762872Swnj * increase regularly. We here decrease it by 3773876Swnj * a fraction based on load average giving a digital 3783876Swnj * decay filter which damps out in about 5 seconds 3793876Swnj * when seconds are measured in time expanded by the 3803876Swnj * load average. 3812872Swnj * 3822872Swnj * If a process is niced, then the nice directly 3832872Swnj * affects the new priority. The final priority 3842872Swnj * is in the range 0 to 255, to fit in a character. 3852872Swnj */ 3861399Sbill pp->p_cpticks = 0; 3873876Swnj a = ave((pp->p_cpu & 0377), avenrun[0]*nrscale) + 3883876Swnj pp->p_nice - NZERO; 3892872Swnj if (a < 0) 3909Sbill a = 0; 3912872Swnj if (a > 255) 3929Sbill a = 255; 3939Sbill pp->p_cpu = a; 394125Sbill (void) setpri(pp); 3952872Swnj /* 3962872Swnj * Now have computed new process priority 3972872Swnj * in p->p_usrpri. Carefully change p->p_pri. 3982872Swnj * A process is on a run queue associated with 3992872Swnj * this priority, so we must block out process 4002872Swnj * state changes during the transition. 4012872Swnj */ 4029Sbill s = spl6(); 4032872Swnj if (pp->p_pri >= PUSER) { 4049Sbill if ((pp != u.u_procp || noproc) && 4059Sbill pp->p_stat == SRUN && 4069Sbill (pp->p_flag & SLOAD) && 4079Sbill pp->p_pri != pp->p_usrpri) { 4089Sbill remrq(pp); 4099Sbill pp->p_pri = pp->p_usrpri; 4109Sbill setrq(pp); 4119Sbill } else 4129Sbill pp->p_pri = pp->p_usrpri; 4139Sbill } 4149Sbill splx(s); 4159Sbill } 4162872Swnj 4172872Swnj /* 4182872Swnj * Perform virtual memory metering. 4192872Swnj */ 4209Sbill vmmeter(); 4212872Swnj 4222872Swnj /* 4232872Swnj * If the swap process is trying to bring 4242872Swnj * a process in, have it look again to see 4252872Swnj * if it is possible now. 4262872Swnj */ 4272872Swnj if (runin!=0) { 4289Sbill runin = 0; 4299Sbill wakeup((caddr_t)&runin); 4309Sbill } 4312872Swnj 4329Sbill /* 4339Sbill * If there are pages that have been cleaned, 4349Sbill * jolt the pageout daemon to process them. 4359Sbill * We do this here so that these pages will be 4369Sbill * freed if there is an abundance of memory and the 4379Sbill * daemon would not be awakened otherwise. 4389Sbill */ 4399Sbill if (bclnlist != NULL) 4409Sbill wakeup((caddr_t)&proc[2]); 4412872Swnj 4422872Swnj /* 4432872Swnj * If the trap occurred from usermode, 4442872Swnj * then check to see if it has now been 4452872Swnj * running more than 10 minutes of user time 4462872Swnj * and should thus run with reduced priority 4472872Swnj * to give other processes a chance. 4482872Swnj */ 4499Sbill if (USERMODE(ps)) { 4509Sbill pp = u.u_procp; 4512872Swnj if (pp->p_uid && pp->p_nice == NZERO && 4522872Swnj u.u_vm.vm_utime > 600 * hz) 4532872Swnj pp->p_nice = NZERO+4; 454125Sbill (void) setpri(pp); 4559Sbill pp->p_pri = pp->p_usrpri; 4569Sbill } 4579Sbill } 4582872Swnj /* 4592872Swnj * If trapped user-mode, give it a profiling tick. 4602872Swnj */ 4612442Swnj if (USERMODE(ps) && u.u_prof.pr_scale) { 4622442Swnj u.u_procp->p_flag |= SOWEUPC; 4632442Swnj aston(); 4649Sbill } 4659Sbill } 4669Sbill 4679Sbill /* 4683110Swnj * Timeout is called to arrange that 4692768Swnj * fun(arg) is called in tim/hz seconds. 4703542Swnj * An entry is linked into the callout 4713110Swnj * structure. The time in each structure 4722768Swnj * entry is the number of hz's more 4739Sbill * than the previous entry. 4749Sbill * In this way, decrementing the 4759Sbill * first entry has the effect of 4769Sbill * updating all entries. 4779Sbill * 4789Sbill * The panic is there because there is nothing 4799Sbill * intelligent to be done if an entry won't fit. 4809Sbill */ 4819Sbill timeout(fun, arg, tim) 4822450Swnj int (*fun)(); 4832450Swnj caddr_t arg; 4849Sbill { 4853542Swnj register struct callout *p1, *p2, *pnew; 4869Sbill register int t; 4879Sbill int s; 4889Sbill 4893446Sroot /* DEBUGGING CODE */ 4903446Sroot int ttrstrt(); 4913446Sroot 4923446Sroot if (fun == ttrstrt && arg == 0) 4933446Sroot panic("timeout ttrstr arg"); 4943446Sroot /* END DEBUGGING CODE */ 4959Sbill t = tim; 4969Sbill s = spl7(); 4973542Swnj pnew = callfree; 4983542Swnj if (pnew == NULL) 4993542Swnj panic("timeout table overflow"); 5003542Swnj callfree = pnew->c_next; 5013542Swnj pnew->c_arg = arg; 5023542Swnj pnew->c_func = fun; 5033542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 5043542Swnj t -= p2->c_time; 5053542Swnj p1->c_next = pnew; 5063542Swnj pnew->c_next = p2; 5073542Swnj pnew->c_time = t; 5083542Swnj if (p2) 5093542Swnj p2->c_time -= t; 5109Sbill splx(s); 5119Sbill } 5127305Ssam 5137305Ssam /* 5147305Ssam * untimeout is called to remove a function timeout call 5157305Ssam * from the callout structure. 5167305Ssam */ 5177305Ssam untimeout (fun, arg) 5187305Ssam int (*fun)(); 5197305Ssam caddr_t arg; 5207305Ssam { 5217305Ssam 5227305Ssam register struct callout *p1, *p2; 5237305Ssam register int s; 5247305Ssam 5257305Ssam s = spl7(); 5267305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 5277305Ssam if (p2->c_func == fun && p2->c_arg == arg) { 5287305Ssam if (p2->c_next) 5297305Ssam p2->c_next->c_time += p2->c_time; 5307305Ssam p1->c_next = p2->c_next; 5317305Ssam p2->c_next = callfree; 5327305Ssam callfree = p2; 5337305Ssam break; 5347305Ssam } 5357305Ssam } 5367305Ssam splx(s); 5377305Ssam } 538