1*8097Sroot /* kern_clock.c 4.38 82/09/06 */ 29Sbill 39Sbill #include "../h/param.h" 49Sbill #include "../h/systm.h" 5329Sbill #include "../h/dk.h" 62768Swnj #include "../h/callout.h" 79Sbill #include "../h/dir.h" 89Sbill #include "../h/user.h" 98028Sroot #include "../h/kernel.h" 109Sbill #include "../h/proc.h" 119Sbill #include "../h/psl.h" 129Sbill #include "../h/vm.h" 139Sbill #include "../h/text.h" 147490Skre #ifdef MUSH 157490Skre #include "../h/quota.h" 167490Skre #include "../h/share.h" 177490Skre #endif 189Sbill 191943Swnj #include "dh.h" 201943Swnj #include "dz.h" 217305Ssam #include "ps.h" 221559Sbill 237388Sroot #ifdef GPROF 247388Sroot extern int profiling; 257388Sroot extern char *s_lowpc; 267388Sroot extern u_long s_textsize; 277388Sroot extern u_short *kcount; 284968Swnj #endif 299Sbill 30*8097Sroot #define bumptime(tp) \ 31*8097Sroot (tp)->tv_usec += tick; \ 32*8097Sroot if ((tp)->tv_usec >= 1000000) { \ 33*8097Sroot (tp)->tv_usec -= 1000000; \ 34*8097Sroot (tp)->tv_sec++; \ 35*8097Sroot } 365247Sroot 372609Swnj /*ARGSUSED*/ 382442Swnj hardclock(pc, ps) 392450Swnj caddr_t pc; 409Sbill { 412768Swnj register struct callout *p1; 42*8097Sroot register struct proc *p; 432442Swnj register int s, cpstate; 447490Skre extern double avenrun[]; 459Sbill 467305Ssam #if NPS > 0 477305Ssam psextsync(pc, ps); 487305Ssam #endif 497305Ssam 50*8097Sroot /* update callout times */ 513542Swnj for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next) 523542Swnj ; 533542Swnj if (p1) 543542Swnj p1->c_time--; 55138Sbill 56*8097Sroot /* charge process for resource usage... statistically! */ 579Sbill if (!noproc) { 589Sbill s = u.u_procp->p_rssize; 59*8097Sroot u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */ 609Sbill if (u.u_procp->p_textp) { 619Sbill register int xrss = u.u_procp->p_textp->x_rssize; 629Sbill 639Sbill s += xrss; 648028Sroot u.u_ru.ru_ixrss += xrss; 659Sbill } 668028Sroot if (s > u.u_ru.ru_maxrss) 678028Sroot u.u_ru.ru_maxrss = s; 688028Sroot if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > 698028Sroot u.u_rlimit[RLIMIT_CPU].rlim_cur) { 70375Sbill psignal(u.u_procp, SIGXCPU); 718028Sroot if (u.u_rlimit[RLIMIT_CPU].rlim_cur < 728028Sroot u.u_rlimit[RLIMIT_CPU].rlim_max) 738028Sroot u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; 74375Sbill } 75*8097Sroot if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && 76*8097Sroot itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) 77*8097Sroot psignal(u.u_procp, SIGPROF); 789Sbill } 79*8097Sroot 80*8097Sroot /* charge for cpu */ 819Sbill if (USERMODE(ps)) { 82*8097Sroot bumptime(&u.u_ru.ru_utime); 83*8097Sroot if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && 84*8097Sroot itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) 85*8097Sroot psignal(u.u_procp, SIGVTALRM); 868028Sroot if (u.u_procp->p_nice > NZERO) 87305Sbill cpstate = CP_NICE; 88305Sbill else 89305Sbill cpstate = CP_USER; 909Sbill } else { 917388Sroot #ifdef GPROF 927388Sroot int k = pc - s_lowpc; 937388Sroot if (profiling < 2 && k < s_textsize) 947388Sroot kcount[k / sizeof (*kcount)]++; 954968Swnj #endif 96305Sbill cpstate = CP_SYS; 977315Ssam if (noproc) { 987315Ssam if ((ps&PSL_IPL) != 0) 997315Ssam cpstate = CP_IDLE; 1008028Sroot } else { 101*8097Sroot bumptime(&u.u_ru.ru_stime); 1028028Sroot } 1039Sbill } 104*8097Sroot 105*8097Sroot /* iostat statistics */ 1061408Sbill cp_time[cpstate]++; 1072442Swnj for (s = 0; s < DK_NDRIVE; s++) 1082442Swnj if (dk_busy&(1<<s)) 1092442Swnj dk_time[s]++; 110*8097Sroot 111*8097Sroot /* adjust priority of current process */ 1129Sbill if (!noproc) { 113*8097Sroot p = u.u_procp; 114*8097Sroot p->p_cpticks++; 115*8097Sroot if (++p->p_cpu == 0) 116*8097Sroot p->p_cpu--; 1177490Skre #ifdef MUSH 118*8097Sroot p->p_quota->q_cost += (p->p_nice > NZERO ? 119*8097Sroot (shconsts.sc_tic * ((2*NZERO)-p->p_nice)) / NZERO : 1207490Skre shconsts.sc_tic) * (((int)avenrun[0]+2)/3); 1217490Skre #endif 122*8097Sroot if (p->p_cpu % 4 == 0) { 123*8097Sroot (void) setpri(p); 124*8097Sroot if (p->p_pri >= PUSER) 125*8097Sroot p->p_pri = p->p_usrpri; 1269Sbill } 1279Sbill } 128*8097Sroot bumptime(&time); 1292442Swnj setsoftclock(); 1302442Swnj } 1312442Swnj 1322609Swnj /*ARGSUSED*/ 1332442Swnj softclock(pc, ps) 1342450Swnj caddr_t pc; 1352442Swnj { 1363615Sroot register struct callout *p1; 1372442Swnj register int a, s; 1383542Swnj caddr_t arg; 1393542Swnj int (*func)(); 1402442Swnj 141*8097Sroot if (panicstr) 142*8097Sroot goto nocallout; 143*8097Sroot for (;;) { 144*8097Sroot s = spl7(); 145*8097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 146*8097Sroot splx(s); 147*8097Sroot break; 1482442Swnj } 149*8097Sroot calltodo.c_next = p1->c_next; 150*8097Sroot arg = p1->c_arg; 151*8097Sroot func = p1->c_func; 152*8097Sroot p1->c_next = callfree; 153*8097Sroot callfree = p1; 154*8097Sroot (void) splx(s); 155*8097Sroot (*func)(arg); 1562442Swnj } 157*8097Sroot nocallout: 1582442Swnj 1592647Swnj #if NDH > 0 1602442Swnj s = spl5(); dhtimer(); splx(s); 1612442Swnj #endif 1622647Swnj #if NDZ > 0 1632442Swnj s = spl5(); dztimer(); splx(s); 1642442Swnj #endif 1652442Swnj 166*8097Sroot /* if nothing to do, try swapin */ 1672450Swnj if (noproc && runin) { 1682450Swnj runin = 0; 1692450Swnj wakeup((caddr_t)&runin); 1702450Swnj } 1719Sbill } 1729Sbill 1739Sbill /* 174*8097Sroot * Arrange that (*fun)(arg) is called in tim/hz seconds. 1759Sbill */ 1769Sbill timeout(fun, arg, tim) 1772450Swnj int (*fun)(); 1782450Swnj caddr_t arg; 179*8097Sroot int tim; 1809Sbill { 1813542Swnj register struct callout *p1, *p2, *pnew; 1829Sbill register int t; 1839Sbill int s; 1849Sbill 1859Sbill t = tim; 1869Sbill s = spl7(); 1873542Swnj pnew = callfree; 1883542Swnj if (pnew == NULL) 1893542Swnj panic("timeout table overflow"); 1903542Swnj callfree = pnew->c_next; 1913542Swnj pnew->c_arg = arg; 1923542Swnj pnew->c_func = fun; 1933542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 1943542Swnj t -= p2->c_time; 1953542Swnj p1->c_next = pnew; 1963542Swnj pnew->c_next = p2; 1973542Swnj pnew->c_time = t; 1983542Swnj if (p2) 1993542Swnj p2->c_time -= t; 2009Sbill splx(s); 2019Sbill } 2027305Ssam 2037814Sroot #ifdef notdef 2047305Ssam /* 2057305Ssam * untimeout is called to remove a function timeout call 2067305Ssam * from the callout structure. 2077305Ssam */ 208*8097Sroot untimeout(fun, arg) 2097305Ssam int (*fun)(); 2107305Ssam caddr_t arg; 2117305Ssam { 2127305Ssam 2137305Ssam register struct callout *p1, *p2; 2147305Ssam register int s; 2157305Ssam 2167305Ssam s = spl7(); 2177305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 2187305Ssam if (p2->c_func == fun && p2->c_arg == arg) { 2197305Ssam if (p2->c_next) 2207305Ssam p2->c_next->c_time += p2->c_time; 2217305Ssam p1->c_next = p2->c_next; 2227305Ssam p2->c_next = callfree; 2237305Ssam callfree = p2; 2247305Ssam break; 2257305Ssam } 2267305Ssam } 2277305Ssam splx(s); 2287305Ssam } 2297814Sroot #endif 230