1*3615Sroot /* kern_clock.c 4.21 81/04/28 */ 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 */ 489Sbill 492609Swnj /*ARGSUSED*/ 502442Swnj hardclock(pc, ps) 512450Swnj caddr_t pc; 529Sbill { 532768Swnj register struct callout *p1; 549Sbill register struct proc *pp; 552442Swnj register int s, cpstate; 569Sbill 579Sbill /* 589Sbill * reprime clock 599Sbill */ 609Sbill clkreld(); 619Sbill 629Sbill /* 632442Swnj * update callout times 649Sbill */ 653542Swnj for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next) 663542Swnj ; 673542Swnj if (p1) 683542Swnj p1->c_time--; 69138Sbill 70138Sbill /* 712442Swnj * Maintain iostat and per-process cpu statistics 72138Sbill */ 739Sbill if (!noproc) { 749Sbill s = u.u_procp->p_rssize; 759Sbill u.u_vm.vm_idsrss += s; 769Sbill if (u.u_procp->p_textp) { 779Sbill register int xrss = u.u_procp->p_textp->x_rssize; 789Sbill 799Sbill s += xrss; 809Sbill u.u_vm.vm_ixrss += xrss; 819Sbill } 829Sbill if (s > u.u_vm.vm_maxrss) 839Sbill u.u_vm.vm_maxrss = s; 842768Swnj if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/hz > u.u_limit[LIM_CPU]) { 85375Sbill psignal(u.u_procp, SIGXCPU); 86375Sbill if (u.u_limit[LIM_CPU] < INFINITY - 5) 87375Sbill u.u_limit[LIM_CPU] += 5; 88375Sbill } 899Sbill } 903110Swnj /* 913110Swnj * Update iostat information. 923110Swnj */ 939Sbill if (USERMODE(ps)) { 949Sbill u.u_vm.vm_utime++; 959Sbill if(u.u_procp->p_nice > NZERO) 96305Sbill cpstate = CP_NICE; 97305Sbill else 98305Sbill cpstate = CP_USER; 999Sbill } else { 100305Sbill cpstate = CP_SYS; 1019Sbill if (noproc) 102305Sbill cpstate = CP_IDLE; 1039Sbill else 1049Sbill u.u_vm.vm_stime++; 1059Sbill } 1061408Sbill cp_time[cpstate]++; 1072442Swnj for (s = 0; s < DK_NDRIVE; s++) 1082442Swnj if (dk_busy&(1<<s)) 1092442Swnj dk_time[s]++; 1103110Swnj /* 1113110Swnj * Adjust priority of current process. 1123110Swnj */ 1139Sbill if (!noproc) { 1149Sbill pp = u.u_procp; 1151399Sbill pp->p_cpticks++; 1169Sbill if(++pp->p_cpu == 0) 1179Sbill pp->p_cpu--; 1189Sbill if(pp->p_cpu % 16 == 0) { 119125Sbill (void) setpri(pp); 1209Sbill if (pp->p_pri >= PUSER) 1219Sbill pp->p_pri = pp->p_usrpri; 1229Sbill } 1239Sbill } 1243110Swnj /* 1253110Swnj * Time moves on. 1263110Swnj */ 1279Sbill ++lbolt; 1282689Swnj #if VAX780 1293110Swnj /* 1303110Swnj * On 780's, impelement a fast UBA watcher, 1313110Swnj * to make sure uba's don't get stuck. 1323110Swnj */ 1332872Swnj if (cpu == VAX_780 && panicstr == 0 && !BASEPRI(ps)) 1342442Swnj unhang(); 1352442Swnj #endif 1363110Swnj /* 1373110Swnj * Schedule a software interrupt for the rest 1383110Swnj * of clock activities. 1393110Swnj */ 1402442Swnj setsoftclock(); 1412442Swnj } 1422442Swnj 1432442Swnj /* 1443110Swnj * SCHMAG is the constant in the digital decay cpu 1453110Swnj * usage priority assignment. Each second we multiply 1463110Swnj * the previous cpu usage estimate by SCHMAG. At 9/10 1473110Swnj * it tends to decay away all knowledge of previous activity 1483110Swnj * in about 10 seconds. 1492442Swnj */ 1503110Swnj #define SCHMAG 9/10 1513110Swnj 1523110Swnj /* 1533110Swnj * Constant for decay filter for cpu usage field 1543110Swnj * in process table (used by ps au). 1553110Swnj */ 1562442Swnj double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 1572442Swnj 1582442Swnj /* 1592442Swnj * Software clock interrupt. 1603110Swnj * This routine runs at lower priority than device interrupts. 1612442Swnj */ 1622609Swnj /*ARGSUSED*/ 1632442Swnj softclock(pc, ps) 1642450Swnj caddr_t pc; 1652442Swnj { 166*3615Sroot register struct callout *p1; 1672442Swnj register struct proc *pp; 1682442Swnj register int a, s; 1693542Swnj caddr_t arg; 1703542Swnj int (*func)(); 1712442Swnj 1722442Swnj /* 1732872Swnj * Perform callouts (but not after panic's!) 1742442Swnj */ 1753542Swnj if (panicstr == 0) { 1763542Swnj for (;;) { 1773542Swnj s = spl7(); 1783542Swnj if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) 1793542Swnj break; 1803542Swnj calltodo.c_next = p1->c_next; 1813542Swnj arg = p1->c_arg; 1823542Swnj func = p1->c_func; 1833542Swnj p1->c_next = callfree; 1843542Swnj callfree = p1; 1853542Swnj (void) splx(s); 1863542Swnj (*func)(arg); 1872442Swnj } 1882442Swnj } 1892442Swnj 1902442Swnj /* 1912442Swnj * Drain silos. 1922442Swnj */ 1933511Sroot #if NBK > 0 1942647Swnj #if NDH > 0 1952442Swnj s = spl5(); dhtimer(); splx(s); 1962442Swnj #endif 1972647Swnj #if NDZ > 0 1982442Swnj s = spl5(); dztimer(); splx(s); 1992442Swnj #endif 2003511Sroot #endif 2012442Swnj 2022442Swnj /* 2032450Swnj * If idling and processes are waiting to swap in, 2042450Swnj * check on them. 2052450Swnj */ 2062450Swnj if (noproc && runin) { 2072450Swnj runin = 0; 2082450Swnj wakeup((caddr_t)&runin); 2092450Swnj } 2102450Swnj 2112450Swnj /* 2122442Swnj * Run paging daemon and reschedule every 1/4 sec. 2132442Swnj */ 2142768Swnj if (lbolt % (hz/4) == 0) { 2159Sbill vmpago(); 2169Sbill runrun++; 2172442Swnj aston(); 2189Sbill } 2192442Swnj 2202442Swnj /* 2212442Swnj * Lightning bolt every second: 2222442Swnj * sleep timeouts 2232442Swnj * process priority recomputation 2242442Swnj * process %cpu averaging 2252442Swnj * virtual memory metering 2262442Swnj * kick swapper if processes want in 2272442Swnj */ 2282768Swnj if (lbolt >= hz) { 2292872Swnj /* 2303110Swnj * This doesn't mean much on VAX since we run at 2312872Swnj * software interrupt time... if hardclock() 2322872Swnj * calls softclock() directly, it prevents 2332872Swnj * this code from running when the priority 2342872Swnj * was raised when the clock interrupt occurred. 2352872Swnj */ 2369Sbill if (BASEPRI(ps)) 2379Sbill return; 2382872Swnj 2392872Swnj /* 2402872Swnj * If we didn't run a few times because of 2412872Swnj * long blockage at high ipl, we don't 2422872Swnj * really want to run this code several times, 2432872Swnj * so squish out all multiples of hz here. 2442872Swnj */ 2452872Swnj time += lbolt / hz; 2462872Swnj lbolt %= hz; 2472872Swnj 2482872Swnj /* 2492872Swnj * Wakeup lightning bolt sleepers. 2502872Swnj * Processes sleep on lbolt to wait 2512872Swnj * for short amounts of time (e.g. 1 second). 2522872Swnj */ 2539Sbill wakeup((caddr_t)&lbolt); 2542872Swnj 2552872Swnj /* 2562872Swnj * Recompute process priority and process 2572872Swnj * sleep() system calls as well as internal 2582872Swnj * sleeps with timeouts (tsleep() kernel routine). 2592872Swnj */ 2602872Swnj for (pp = proc; pp < procNPROC; pp++) 261928Sbill if (pp->p_stat && pp->p_stat!=SZOMB) { 2622872Swnj /* 2632872Swnj * Increase resident time, to max of 127 seconds 2642872Swnj * (it is kept in a character.) For 2652872Swnj * loaded processes this is time in core; for 2662872Swnj * swapped processes, this is time on drum. 2672872Swnj */ 2682872Swnj if (pp->p_time != 127) 2699Sbill pp->p_time++; 2702872Swnj /* 2712872Swnj * If process has clock counting down, and it 2722872Swnj * expires, set it running (if this is a tsleep()), 2732872Swnj * or give it an SIGALRM (if the user process 2742872Swnj * is using alarm signals. 2752872Swnj */ 2762872Swnj if (pp->p_clktim && --pp->p_clktim == 0) 2772872Swnj if (pp->p_flag & STIMO) { 2782872Swnj s = spl6(); 2792872Swnj switch (pp->p_stat) { 280204Sbill 2812872Swnj case SSLEEP: 2822872Swnj setrun(pp); 2832872Swnj break; 284204Sbill 2852872Swnj case SSTOP: 2862872Swnj unsleep(pp); 2872872Swnj break; 2882872Swnj } 2892872Swnj pp->p_flag &= ~STIMO; 2902872Swnj splx(s); 2912872Swnj } else 2922872Swnj psignal(pp, SIGALRM); 2932872Swnj /* 2942872Swnj * If process is blocked, increment computed 2952872Swnj * time blocked. This is used in swap scheduling. 2962872Swnj */ 2972872Swnj if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP) 2989Sbill if (pp->p_slptime != 127) 2999Sbill pp->p_slptime++; 3002872Swnj /* 3012872Swnj * Update digital filter estimation of process 3022872Swnj * cpu utilization for loaded processes. 3032872Swnj */ 3041399Sbill if (pp->p_flag&SLOAD) 3051399Sbill pp->p_pctcpu = ccpu * pp->p_pctcpu + 3062768Swnj (1.0 - ccpu) * (pp->p_cpticks/(float)hz); 3072872Swnj /* 3082872Swnj * Recompute process priority. The number p_cpu 3092872Swnj * is a weighted estimate of cpu time consumed. 3102872Swnj * A process which consumes cpu time has this 3112872Swnj * increase regularly. We here decrease it by 3122872Swnj * a fraction (SCHMAG is 90%), giving a digital 3132872Swnj * decay filter which damps out in about 10 seconds. 3142872Swnj * 3152872Swnj * If a process is niced, then the nice directly 3162872Swnj * affects the new priority. The final priority 3172872Swnj * is in the range 0 to 255, to fit in a character. 3182872Swnj */ 3191399Sbill pp->p_cpticks = 0; 3209Sbill a = (pp->p_cpu & 0377)*SCHMAG + pp->p_nice - NZERO; 3212872Swnj if (a < 0) 3229Sbill a = 0; 3232872Swnj if (a > 255) 3249Sbill a = 255; 3259Sbill pp->p_cpu = a; 326125Sbill (void) setpri(pp); 3272872Swnj /* 3282872Swnj * Now have computed new process priority 3292872Swnj * in p->p_usrpri. Carefully change p->p_pri. 3302872Swnj * A process is on a run queue associated with 3312872Swnj * this priority, so we must block out process 3322872Swnj * state changes during the transition. 3332872Swnj */ 3349Sbill s = spl6(); 3352872Swnj if (pp->p_pri >= PUSER) { 3369Sbill if ((pp != u.u_procp || noproc) && 3379Sbill pp->p_stat == SRUN && 3389Sbill (pp->p_flag & SLOAD) && 3399Sbill pp->p_pri != pp->p_usrpri) { 3409Sbill remrq(pp); 3419Sbill pp->p_pri = pp->p_usrpri; 3429Sbill setrq(pp); 3439Sbill } else 3449Sbill pp->p_pri = pp->p_usrpri; 3459Sbill } 3469Sbill splx(s); 3479Sbill } 3482872Swnj 3492872Swnj /* 3502872Swnj * Perform virtual memory metering. 3512872Swnj */ 3529Sbill vmmeter(); 3532872Swnj 3542872Swnj /* 3552872Swnj * If the swap process is trying to bring 3562872Swnj * a process in, have it look again to see 3572872Swnj * if it is possible now. 3582872Swnj */ 3592872Swnj if (runin!=0) { 3609Sbill runin = 0; 3619Sbill wakeup((caddr_t)&runin); 3629Sbill } 3632872Swnj 3649Sbill /* 3659Sbill * If there are pages that have been cleaned, 3669Sbill * jolt the pageout daemon to process them. 3679Sbill * We do this here so that these pages will be 3689Sbill * freed if there is an abundance of memory and the 3699Sbill * daemon would not be awakened otherwise. 3709Sbill */ 3719Sbill if (bclnlist != NULL) 3729Sbill wakeup((caddr_t)&proc[2]); 3732872Swnj 3742872Swnj /* 3752872Swnj * If the trap occurred from usermode, 3762872Swnj * then check to see if it has now been 3772872Swnj * running more than 10 minutes of user time 3782872Swnj * and should thus run with reduced priority 3792872Swnj * to give other processes a chance. 3802872Swnj */ 3819Sbill if (USERMODE(ps)) { 3829Sbill pp = u.u_procp; 3832872Swnj if (pp->p_uid && pp->p_nice == NZERO && 3842872Swnj u.u_vm.vm_utime > 600 * hz) 3852872Swnj pp->p_nice = NZERO+4; 386125Sbill (void) setpri(pp); 3879Sbill pp->p_pri = pp->p_usrpri; 3889Sbill } 3899Sbill } 3902872Swnj /* 3912872Swnj * If trapped user-mode, give it a profiling tick. 3922872Swnj */ 3932442Swnj if (USERMODE(ps) && u.u_prof.pr_scale) { 3942442Swnj u.u_procp->p_flag |= SOWEUPC; 3952442Swnj aston(); 3969Sbill } 3979Sbill } 3989Sbill 3999Sbill /* 4003110Swnj * Timeout is called to arrange that 4012768Swnj * fun(arg) is called in tim/hz seconds. 4023542Swnj * An entry is linked into the callout 4033110Swnj * structure. The time in each structure 4042768Swnj * entry is the number of hz's more 4059Sbill * than the previous entry. 4069Sbill * In this way, decrementing the 4079Sbill * first entry has the effect of 4089Sbill * updating all entries. 4099Sbill * 4109Sbill * The panic is there because there is nothing 4119Sbill * intelligent to be done if an entry won't fit. 4129Sbill */ 4139Sbill timeout(fun, arg, tim) 4142450Swnj int (*fun)(); 4152450Swnj caddr_t arg; 4169Sbill { 4173542Swnj register struct callout *p1, *p2, *pnew; 4189Sbill register int t; 4199Sbill int s; 4209Sbill 4213446Sroot /* DEBUGGING CODE */ 4223446Sroot int ttrstrt(); 4233446Sroot 4243446Sroot if (fun == ttrstrt && arg == 0) 4253446Sroot panic("timeout ttrstr arg"); 4263446Sroot /* END DEBUGGING CODE */ 4279Sbill t = tim; 4289Sbill s = spl7(); 4293542Swnj pnew = callfree; 4303542Swnj if (pnew == NULL) 4313542Swnj panic("timeout table overflow"); 4323542Swnj callfree = pnew->c_next; 4333542Swnj pnew->c_arg = arg; 4343542Swnj pnew->c_func = fun; 4353542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 4363542Swnj t -= p2->c_time; 4373542Swnj p1->c_next = pnew; 4383542Swnj pnew->c_next = p2; 4393542Swnj pnew->c_time = t; 4403542Swnj if (p2) 4413542Swnj p2->c_time -= t; 4429Sbill splx(s); 4439Sbill } 444