1*1399Sbill /* 10/14/12 3.20 kern_clock.c */ 29Sbill 39Sbill #include "../h/param.h" 49Sbill #include "../h/systm.h" 5329Sbill #include "../h/dk.h" 69Sbill #include "../h/callo.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" 199Sbill 209Sbill #define SCHMAG 9/10 219Sbill 22*1399Sbill /* 23*1399Sbill * Constant for decay filter for cpu usage. 24*1399Sbill */ 25*1399Sbill double ccpu = 0.93550698503161773774; /* exp(-1/15) */ 269Sbill 279Sbill /* 28*1399Sbill * Clock is called straight from 299Sbill * the real time clock interrupt. 309Sbill * 319Sbill * Functions: 329Sbill * implement callouts 339Sbill * maintain user/system times 349Sbill * maintain date 359Sbill * profile 369Sbill * lightning bolt wakeup (every second) 379Sbill * alarm clock signals 389Sbill * jab the scheduler 399Sbill */ 409Sbill #ifdef KPROF 41104Sbill unsigned short kcount[20000]; 429Sbill #endif 439Sbill 44115Sbill /* 45115Sbill * We handle regular calls to the dh and dz silo input processors 46115Sbill * without using timeouts to save a little time. 47115Sbill */ 48142Sbill int rintvl = 0; /* every 1/60'th of sec check receivers */ 49115Sbill int rcnt; 50115Sbill 519Sbill clock(pc, ps) 529Sbill caddr_t pc; 539Sbill { 549Sbill register struct callo *p1, *p2; 559Sbill register struct proc *pp; 569Sbill register int s; 57305Sbill int a, cpstate; 589Sbill 599Sbill /* 609Sbill * reprime clock 619Sbill */ 629Sbill clkreld(); 639Sbill 649Sbill /* 659Sbill * callouts 669Sbill * else update first non-zero time 679Sbill */ 689Sbill 699Sbill if(callout[0].c_func == NULL) 709Sbill goto out; 719Sbill p2 = &callout[0]; 729Sbill while(p2->c_time<=0 && p2->c_func!=NULL) 739Sbill p2++; 749Sbill p2->c_time--; 759Sbill 769Sbill /* 779Sbill * if ps is high, just return 789Sbill */ 799Sbill if (BASEPRI(ps)) 809Sbill goto out; 819Sbill 829Sbill /* 839Sbill * callout 849Sbill */ 859Sbill 869Sbill if(callout[0].c_time <= 0) { 879Sbill p1 = &callout[0]; 889Sbill while(p1->c_func != 0 && p1->c_time <= 0) { 899Sbill (*p1->c_func)(p1->c_arg); 909Sbill p1++; 919Sbill } 929Sbill p2 = &callout[0]; 939Sbill while(p2->c_func = p1->c_func) { 949Sbill p2->c_time = p1->c_time; 959Sbill p2->c_arg = p1->c_arg; 969Sbill p1++; 979Sbill p2++; 989Sbill } 999Sbill } 1009Sbill 1019Sbill /* 1029Sbill * lightning bolt time-out 1039Sbill * and time of day 1049Sbill */ 1059Sbill out: 106138Sbill 107138Sbill /* 108138Sbill * In order to not take input character interrupts to use 109138Sbill * the input silo on DZ's we have to guarantee to echo 110138Sbill * characters regularly. This means that we have to 111138Sbill * call the timer routines predictably. Since blocking 112138Sbill * in these routines is at spl5(), we have to make spl5() 113138Sbill * really spl6() blocking off the clock to put this code 114138Sbill * here. Note also that it is critical that we run spl5() 115138Sbill * (i.e. really spl6()) in the receiver interrupt routines 116138Sbill * so we can't enter them recursively and transpose characters. 117138Sbill */ 118138Sbill if (rcnt >= rintvl) { 119138Sbill dhtimer(); 120138Sbill dztimer(); 121138Sbill rcnt = 0; 122138Sbill } else 123138Sbill rcnt++; 1249Sbill if (!noproc) { 1259Sbill s = u.u_procp->p_rssize; 1269Sbill u.u_vm.vm_idsrss += s; 1279Sbill if (u.u_procp->p_textp) { 1289Sbill register int xrss = u.u_procp->p_textp->x_rssize; 1299Sbill 1309Sbill s += xrss; 1319Sbill u.u_vm.vm_ixrss += xrss; 1329Sbill } 1339Sbill if (s > u.u_vm.vm_maxrss) 1349Sbill u.u_vm.vm_maxrss = s; 135375Sbill if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/HZ > u.u_limit[LIM_CPU]) { 136375Sbill psignal(u.u_procp, SIGXCPU); 137375Sbill if (u.u_limit[LIM_CPU] < INFINITY - 5) 138375Sbill u.u_limit[LIM_CPU] += 5; 139375Sbill } 1409Sbill } 1419Sbill if (USERMODE(ps)) { 1429Sbill u.u_vm.vm_utime++; 1439Sbill if(u.u_procp->p_nice > NZERO) 144305Sbill cpstate = CP_NICE; 145305Sbill else 146305Sbill cpstate = CP_USER; 1479Sbill } else { 148305Sbill cpstate = CP_SYS; 1499Sbill if (noproc) 150305Sbill cpstate = CP_IDLE; 1519Sbill else 1529Sbill u.u_vm.vm_stime++; 1539Sbill } 154305Sbill dk_time[cpstate][dk_busy&(DK_NSTATES-1)]++; 1559Sbill if (!noproc) { 1569Sbill pp = u.u_procp; 157*1399Sbill pp->p_cpticks++; 1589Sbill if(++pp->p_cpu == 0) 1599Sbill pp->p_cpu--; 1609Sbill if(pp->p_cpu % 16 == 0) { 161125Sbill (void) setpri(pp); 1629Sbill if (pp->p_pri >= PUSER) 1639Sbill pp->p_pri = pp->p_usrpri; 1649Sbill } 1659Sbill } 1669Sbill ++lbolt; 1679Sbill if (lbolt % (HZ/4) == 0) { 1689Sbill vmpago(); 1699Sbill runrun++; 1709Sbill } 1719Sbill if (lbolt >= HZ) { 172877Sbill extern int hangcnt; 173877Sbill 1749Sbill if (BASEPRI(ps)) 1759Sbill return; 1769Sbill lbolt -= HZ; 1779Sbill ++time; 178125Sbill (void) spl1(); 179877Sbill /* 180877Sbill * machdep.c:unhang uses hangcnt to make sure uba 181877Sbill * doesn't forget to interrupt (this has been observed). 182877Sbill * This prevents an accumulation of < 5 second uba failures 183877Sbill * from summing to a uba reset. 184877Sbill */ 185877Sbill if (hangcnt) 186877Sbill hangcnt--; 1879Sbill runrun++; 1889Sbill wakeup((caddr_t)&lbolt); 1899Sbill for(pp = &proc[0]; pp < &proc[NPROC]; pp++) 190928Sbill if (pp->p_stat && pp->p_stat!=SZOMB) { 1919Sbill if(pp->p_time != 127) 1929Sbill pp->p_time++; 1939Sbill if(pp->p_clktim) 1949Sbill if(--pp->p_clktim == 0) 195101Sbill if (pp->p_flag & STIMO) { 196101Sbill s = spl6(); 197204Sbill switch (pp->p_stat) { 198204Sbill 199204Sbill case SSLEEP: 200101Sbill setrun(pp); 201204Sbill break; 202204Sbill 203204Sbill case SSTOP: 204204Sbill unsleep(pp); 205204Sbill break; 206204Sbill } 207101Sbill pp->p_flag &= ~STIMO; 208101Sbill splx(s); 209101Sbill } else 210166Sbill psignal(pp, SIGALRM); 2119Sbill if(pp->p_stat==SSLEEP||pp->p_stat==SSTOP) 2129Sbill if (pp->p_slptime != 127) 2139Sbill pp->p_slptime++; 214*1399Sbill if (pp->p_flag&SLOAD) 215*1399Sbill pp->p_pctcpu = ccpu * pp->p_pctcpu + 216*1399Sbill (1.0 - ccpu) * (pp->p_cpticks/(float)HZ); 217*1399Sbill pp->p_cpticks = 0; 2189Sbill a = (pp->p_cpu & 0377)*SCHMAG + pp->p_nice - NZERO; 2199Sbill if(a < 0) 2209Sbill a = 0; 2219Sbill if(a > 255) 2229Sbill a = 255; 2239Sbill pp->p_cpu = a; 224125Sbill (void) setpri(pp); 2259Sbill s = spl6(); 2269Sbill if(pp->p_pri >= PUSER) { 2279Sbill if ((pp != u.u_procp || noproc) && 2289Sbill pp->p_stat == SRUN && 2299Sbill (pp->p_flag & SLOAD) && 2309Sbill pp->p_pri != pp->p_usrpri) { 2319Sbill remrq(pp); 2329Sbill pp->p_pri = pp->p_usrpri; 2339Sbill setrq(pp); 2349Sbill } else 2359Sbill pp->p_pri = pp->p_usrpri; 2369Sbill } 2379Sbill splx(s); 2389Sbill } 2399Sbill vmmeter(); 2409Sbill if(runin!=0) { 2419Sbill runin = 0; 2429Sbill wakeup((caddr_t)&runin); 2439Sbill } 2449Sbill /* 2459Sbill * If there are pages that have been cleaned, 2469Sbill * jolt the pageout daemon to process them. 2479Sbill * We do this here so that these pages will be 2489Sbill * freed if there is an abundance of memory and the 2499Sbill * daemon would not be awakened otherwise. 2509Sbill */ 2519Sbill if (bclnlist != NULL) 2529Sbill wakeup((caddr_t)&proc[2]); 2539Sbill if (USERMODE(ps)) { 2549Sbill pp = u.u_procp; 255362Sbill #ifdef ERNIE 2569Sbill if (pp->p_uid) 2579Sbill if (pp->p_nice == NZERO && u.u_vm.vm_utime > 600 * HZ) 2589Sbill pp->p_nice = NZERO+4; 259125Sbill (void) setpri(pp); 2609Sbill pp->p_pri = pp->p_usrpri; 261362Sbill #endif 2629Sbill } 2639Sbill } 264277Sbill if (!BASEPRI(ps)) 265277Sbill unhang(); 2669Sbill if (USERMODE(ps)) { 2679Sbill /* 2689Sbill * We do this last since it 2699Sbill * may block on a page fault in user space. 2709Sbill */ 2719Sbill if (u.u_prof.pr_scale) 2729Sbill addupc(pc, &u.u_prof, 1); 2739Sbill } 2749Sbill #ifdef KPROF 2759Sbill else if (!noproc) { 276104Sbill register int indx = ((int)pc & 0x7fffffff) / 4; 2779Sbill 2789Sbill if (indx >= 0 && indx < 20000) 279104Sbill if (++kcount[indx] == 0) 280104Sbill --kcount[indx]; 2819Sbill } 2829Sbill #endif 2839Sbill } 2849Sbill 2859Sbill /* 2869Sbill * timeout is called to arrange that 2879Sbill * fun(arg) is called in tim/HZ seconds. 2889Sbill * An entry is sorted into the callout 2899Sbill * structure. The time in each structure 2909Sbill * entry is the number of HZ's more 2919Sbill * than the previous entry. 2929Sbill * In this way, decrementing the 2939Sbill * first entry has the effect of 2949Sbill * updating all entries. 2959Sbill * 2969Sbill * The panic is there because there is nothing 2979Sbill * intelligent to be done if an entry won't fit. 2989Sbill */ 2999Sbill timeout(fun, arg, tim) 3009Sbill int (*fun)(); 3019Sbill caddr_t arg; 3029Sbill { 3039Sbill register struct callo *p1, *p2; 3049Sbill register int t; 3059Sbill int s; 3069Sbill 3079Sbill t = tim; 3089Sbill p1 = &callout[0]; 3099Sbill s = spl7(); 3109Sbill while(p1->c_func != 0 && p1->c_time <= t) { 3119Sbill t -= p1->c_time; 3129Sbill p1++; 3139Sbill } 3149Sbill if (p1 >= &callout[NCALL-1]) 3159Sbill panic("Timeout table overflow"); 3169Sbill p1->c_time -= t; 3179Sbill p2 = p1; 3189Sbill while(p2->c_func != 0) 3199Sbill p2++; 3209Sbill while(p2 >= p1) { 3219Sbill (p2+1)->c_time = p2->c_time; 3229Sbill (p2+1)->c_func = p2->c_func; 3239Sbill (p2+1)->c_arg = p2->c_arg; 3249Sbill p2--; 3259Sbill } 3269Sbill p1->c_time = t; 3279Sbill p1->c_func = fun; 3289Sbill p1->c_arg = arg; 3299Sbill splx(s); 3309Sbill } 331