1*17007Smckusick /* kern_clock.c 6.8 84/08/22 */ 29Sbill 39751Ssam #include "../machine/reg.h" 49751Ssam #include "../machine/psl.h" 59751Ssam 69Sbill #include "../h/param.h" 79Sbill #include "../h/systm.h" 8329Sbill #include "../h/dk.h" 92768Swnj #include "../h/callout.h" 109Sbill #include "../h/dir.h" 119Sbill #include "../h/user.h" 128028Sroot #include "../h/kernel.h" 139Sbill #include "../h/proc.h" 149Sbill #include "../h/vm.h" 159Sbill #include "../h/text.h" 169Sbill 179751Ssam #ifdef vax 189751Ssam #include "../vax/mtpr.h" 199751Ssam #endif 209751Ssam 2110291Smckusick #ifdef GPROF 2210291Smckusick #include "../h/gprof.h" 2310291Smckusick #endif 2410291Smckusick 258124Sroot /* 268124Sroot * Clock handling routines. 278124Sroot * 2811392Ssam * This code is written to operate with two timers which run 2911392Ssam * independently of each other. The main clock, running at hz 3011392Ssam * times per second, is used to do scheduling and timeout calculations. 3111392Ssam * The second timer does resource utilization estimation statistically 3211392Ssam * based on the state of the machine phz times a second. Both functions 3311392Ssam * can be performed by a single clock (ie hz == phz), however the 3411392Ssam * statistics will be much more prone to errors. Ideally a machine 3511392Ssam * would have separate clocks measuring time spent in user state, system 3611392Ssam * state, interrupt state, and idle state. These clocks would allow a non- 3711392Ssam * approximate measure of resource utilization. 388124Sroot */ 391559Sbill 408124Sroot /* 418124Sroot * TODO: 4212747Ssam * time of day, system/user timing, timeouts, profiling on separate timers 4312747Ssam * allocate more timeout table slots when table overflows. 448124Sroot */ 45*17007Smckusick #ifdef notdef 46*17007Smckusick /* 47*17007Smckusick * Bump a timeval by a small number of usec's. 48*17007Smckusick */ 49*17007Smckusick bumptime(tp, usec) 50*17007Smckusick register struct timeval *tp; 51*17007Smckusick int usec; 52*17007Smckusick { 539Sbill 54*17007Smckusick tp->tv_usec += usec; 55*17007Smckusick if (tp->tv_usec >= 1000000) { 56*17007Smckusick tp->tv_usec -= 1000000; 57*17007Smckusick tp->tv_sec++; 58*17007Smckusick } 59*17007Smckusick } 60*17007Smckusick #endif notdef 61*17007Smckusick #define BUMPTIME(t, usec) { \ 62*17007Smckusick register struct timeval *tp = (t); \ 63*17007Smckusick \ 64*17007Smckusick tp->tv_usec += (usec); \ 65*17007Smckusick if (tp->tv_usec >= 1000000) { \ 66*17007Smckusick tp->tv_usec -= 1000000; \ 67*17007Smckusick tp->tv_sec++; \ 68*17007Smckusick } \ 69*17007Smckusick } 70*17007Smckusick 718124Sroot /* 7211392Ssam * The hz hardware interval timer. 7311392Ssam * We update the events relating to real time. 7411392Ssam * If this timer is also being used to gather statistics, 7511392Ssam * we run through the statistics gathering routine as well. 768124Sroot */ 772609Swnj /*ARGSUSED*/ 782442Swnj hardclock(pc, ps) 792450Swnj caddr_t pc; 808944Sroot int ps; 819Sbill { 822768Swnj register struct callout *p1; 838097Sroot register struct proc *p; 842442Swnj register int s, cpstate; 8516172Skarels int needsoft = 0; 869Sbill 878124Sroot /* 888124Sroot * Update real-time timeout queue. 898124Sroot * At front of queue are some number of events which are ``due''. 908124Sroot * The time to these is <= 0 and if negative represents the 918124Sroot * number of ticks which have passed since it was supposed to happen. 928124Sroot * The rest of the q elements (times > 0) are events yet to happen, 938124Sroot * where the time for each is given as a delta from the previous. 948124Sroot * Decrementing just the first of these serves to decrement the time 958124Sroot * to all events. 968124Sroot */ 9712747Ssam p1 = calltodo.c_next; 9812747Ssam while (p1) { 9912747Ssam if (--p1->c_time > 0) 10012747Ssam break; 10116172Skarels needsoft = 1; 10212747Ssam if (p1->c_time == 0) 10312747Ssam break; 10412747Ssam p1 = p1->c_next; 10512747Ssam } 106138Sbill 1078124Sroot /* 1088124Sroot * Charge the time out based on the mode the cpu is in. 1098124Sroot * Here again we fudge for the lack of proper interval timers 1108124Sroot * assuming that the current state has been around at least 1118124Sroot * one tick. 1128124Sroot */ 1139Sbill if (USERMODE(ps)) { 11416172Skarels if (u.u_prof.pr_scale) 11516172Skarels needsoft = 1; 1168124Sroot /* 1178124Sroot * CPU was in user state. Increment 1188124Sroot * user time counter, and process process-virtual time 1199604Ssam * interval timer. 1208124Sroot */ 121*17007Smckusick BUMPTIME(&u.u_ru.ru_utime, tick); 1228097Sroot if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && 1238097Sroot itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) 1248097Sroot psignal(u.u_procp, SIGVTALRM); 1258028Sroot if (u.u_procp->p_nice > NZERO) 126305Sbill cpstate = CP_NICE; 127305Sbill else 128305Sbill cpstate = CP_USER; 1299Sbill } else { 1308124Sroot /* 1318124Sroot * CPU was in system state. If profiling kernel 1328124Sroot * increment a counter. If no process is running 1338124Sroot * then this is a system tick if we were running 1348124Sroot * at a non-zero IPL (in a driver). If a process is running, 1358124Sroot * then we charge it with system time even if we were 1368124Sroot * at a non-zero IPL, since the system often runs 1378124Sroot * this way during processing of system calls. 1388124Sroot * This is approximate, but the lack of true interval 1398124Sroot * timers makes doing anything else difficult. 1408124Sroot */ 141305Sbill cpstate = CP_SYS; 1427315Ssam if (noproc) { 1438944Sroot if (BASEPRI(ps)) 1447315Ssam cpstate = CP_IDLE; 1458028Sroot } else { 146*17007Smckusick BUMPTIME(&u.u_ru.ru_stime, tick); 1478028Sroot } 1489Sbill } 1498097Sroot 1508124Sroot /* 15110388Ssam * If the cpu is currently scheduled to a process, then 15210388Ssam * charge it with resource utilization for a tick, updating 15310388Ssam * statistics which run in (user+system) virtual time, 15410388Ssam * such as the cpu time limit and profiling timers. 15510388Ssam * This assumes that the current process has been running 15610388Ssam * the entire last tick. 15710388Ssam */ 15810388Ssam if (noproc == 0 && cpstate != CP_IDLE) { 15910388Ssam if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > 16010388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur) { 16110388Ssam psignal(u.u_procp, SIGXCPU); 16210388Ssam if (u.u_rlimit[RLIMIT_CPU].rlim_cur < 16310388Ssam u.u_rlimit[RLIMIT_CPU].rlim_max) 16410388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; 16510388Ssam } 16610388Ssam if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && 16710388Ssam itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) 16810388Ssam psignal(u.u_procp, SIGPROF); 16910388Ssam s = u.u_procp->p_rssize; 17010388Ssam u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */ 17110388Ssam if (u.u_procp->p_textp) { 17210388Ssam register int xrss = u.u_procp->p_textp->x_rssize; 17310388Ssam 17410388Ssam s += xrss; 17510388Ssam u.u_ru.ru_ixrss += xrss; 17610388Ssam } 17710388Ssam if (s > u.u_ru.ru_maxrss) 17810388Ssam u.u_ru.ru_maxrss = s; 17910388Ssam } 18010388Ssam 18110388Ssam /* 1828124Sroot * We adjust the priority of the current process. 1838124Sroot * The priority of a process gets worse as it accumulates 1848124Sroot * CPU time. The cpu usage estimator (p_cpu) is increased here 1858124Sroot * and the formula for computing priorities (in kern_synch.c) 1868124Sroot * will compute a different value each time the p_cpu increases 1878124Sroot * by 4. The cpu usage estimator ramps up quite quickly when 1888124Sroot * the process is running (linearly), and decays away exponentially, 1898124Sroot * at a rate which is proportionally slower when the system is 1908124Sroot * busy. The basic principal is that the system will 90% forget 1918124Sroot * that a process used a lot of CPU time in 5*loadav seconds. 1928124Sroot * This causes the system to favor processes which haven't run 1938124Sroot * much recently, and to round-robin among other processes. 1948124Sroot */ 1959Sbill if (!noproc) { 1968097Sroot p = u.u_procp; 1978097Sroot p->p_cpticks++; 1988097Sroot if (++p->p_cpu == 0) 1998097Sroot p->p_cpu--; 2008124Sroot if ((p->p_cpu&3) == 0) { 2018097Sroot (void) setpri(p); 2028097Sroot if (p->p_pri >= PUSER) 2038097Sroot p->p_pri = p->p_usrpri; 2049Sbill } 2059Sbill } 2068124Sroot 2078124Sroot /* 20811392Ssam * If the alternate clock has not made itself known then 20911392Ssam * we must gather the statistics. 21011392Ssam */ 21111392Ssam if (phz == 0) 21211392Ssam gatherstats(pc, ps); 21311392Ssam 21411392Ssam /* 2158124Sroot * Increment the time-of-day, and schedule 2168124Sroot * processing of the callouts at a very low cpu priority, 2178124Sroot * so we don't keep the relatively high clock interrupt 2188124Sroot * priority any longer than necessary. 2198124Sroot */ 220*17007Smckusick BUMPTIME(&time, tick); 22116525Skarels if (needsoft) { 22216525Skarels if (BASEPRI(ps)) { 22316525Skarels /* 22416525Skarels * Save the overhead of a software interrupt; 22516525Skarels * it will happen as soon as we return, so do it now. 22616525Skarels */ 22716525Skarels (void) splsoftclock(); 22816525Skarels softclock(pc, ps); 22916525Skarels } else 23016525Skarels setsoftclock(); 23116525Skarels } 2322442Swnj } 2332442Swnj 23415191Ssam int dk_ndrive = DK_NDRIVE; 2358124Sroot /* 23611392Ssam * Gather statistics on resource utilization. 23711392Ssam * 23811392Ssam * We make a gross assumption: that the system has been in the 23911392Ssam * state it is in (user state, kernel state, interrupt state, 24011392Ssam * or idle state) for the entire last time interval, and 24111392Ssam * update statistics accordingly. 24211392Ssam */ 24312747Ssam /*ARGSUSED*/ 24411392Ssam gatherstats(pc, ps) 24511392Ssam caddr_t pc; 24611392Ssam int ps; 24711392Ssam { 24811392Ssam int cpstate, s; 24911392Ssam 25011392Ssam /* 25111392Ssam * Determine what state the cpu is in. 25211392Ssam */ 25311392Ssam if (USERMODE(ps)) { 25411392Ssam /* 25511392Ssam * CPU was in user state. 25611392Ssam */ 25711392Ssam if (u.u_procp->p_nice > NZERO) 25811392Ssam cpstate = CP_NICE; 25911392Ssam else 26011392Ssam cpstate = CP_USER; 26111392Ssam } else { 26211392Ssam /* 26311392Ssam * CPU was in system state. If profiling kernel 26411392Ssam * increment a counter. 26511392Ssam */ 26611392Ssam cpstate = CP_SYS; 26711392Ssam if (noproc && BASEPRI(ps)) 26811392Ssam cpstate = CP_IDLE; 26911392Ssam #ifdef GPROF 27011392Ssam s = pc - s_lowpc; 27111392Ssam if (profiling < 2 && s < s_textsize) 27211392Ssam kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 27311392Ssam #endif 27411392Ssam } 27511392Ssam /* 27611392Ssam * We maintain statistics shown by user-level statistics 27711392Ssam * programs: the amount of time in each cpu state, and 27811392Ssam * the amount of time each of DK_NDRIVE ``drives'' is busy. 27911392Ssam */ 28011392Ssam cp_time[cpstate]++; 28111392Ssam for (s = 0; s < DK_NDRIVE; s++) 28211392Ssam if (dk_busy&(1<<s)) 28311392Ssam dk_time[s]++; 28411392Ssam } 28511392Ssam 28611392Ssam /* 2878124Sroot * Software priority level clock interrupt. 2888124Sroot * Run periodic events from timeout queue. 2898124Sroot */ 2902609Swnj /*ARGSUSED*/ 2912442Swnj softclock(pc, ps) 2922450Swnj caddr_t pc; 2938944Sroot int ps; 2942442Swnj { 2952442Swnj 2968097Sroot for (;;) { 2978124Sroot register struct callout *p1; 2988124Sroot register caddr_t arg; 2998124Sroot register int (*func)(); 3008124Sroot register int a, s; 3018124Sroot 3028097Sroot s = spl7(); 3038097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 3048097Sroot splx(s); 3058097Sroot break; 3062442Swnj } 3078124Sroot arg = p1->c_arg; func = p1->c_func; a = p1->c_time; 3088097Sroot calltodo.c_next = p1->c_next; 3098097Sroot p1->c_next = callfree; 3108097Sroot callfree = p1; 3119157Ssam splx(s); 3128112Sroot (*func)(arg, a); 3132442Swnj } 3149604Ssam /* 31513127Ssam * If trapped user-mode and profiling, give it 31613127Ssam * a profiling tick. 3179604Ssam */ 31813127Ssam if (USERMODE(ps)) { 31913127Ssam register struct proc *p = u.u_procp; 32013127Ssam 32113127Ssam if (u.u_prof.pr_scale) { 32213127Ssam p->p_flag |= SOWEUPC; 32313127Ssam aston(); 32413127Ssam } 32513127Ssam /* 32613127Ssam * Check to see if process has accumulated 32713127Ssam * more than 10 minutes of user time. If so 32813127Ssam * reduce priority to give others a chance. 32913127Ssam */ 33013127Ssam if (p->p_uid && p->p_nice == NZERO && 33113127Ssam u.u_ru.ru_utime.tv_sec > 10 * 60) { 33213127Ssam p->p_nice = NZERO+4; 33313127Ssam (void) setpri(p); 33413127Ssam p->p_pri = p->p_usrpri; 33513127Ssam } 3369604Ssam } 3379Sbill } 3389Sbill 3399Sbill /* 34012747Ssam * Arrange that (*fun)(arg) is called in t/hz seconds. 34112747Ssam */ 34212747Ssam timeout(fun, arg, t) 3432450Swnj int (*fun)(); 3442450Swnj caddr_t arg; 34512747Ssam register int t; 3469Sbill { 3473542Swnj register struct callout *p1, *p2, *pnew; 34812747Ssam register int s = spl7(); 3499Sbill 35012747Ssam if (t == 0) 35112747Ssam t = 1; 3523542Swnj pnew = callfree; 3533542Swnj if (pnew == NULL) 3543542Swnj panic("timeout table overflow"); 3553542Swnj callfree = pnew->c_next; 3563542Swnj pnew->c_arg = arg; 3573542Swnj pnew->c_func = fun; 3583542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 3599742Ssam if (p2->c_time > 0) 3609742Ssam t -= p2->c_time; 3613542Swnj p1->c_next = pnew; 3623542Swnj pnew->c_next = p2; 3633542Swnj pnew->c_time = t; 3643542Swnj if (p2) 3653542Swnj p2->c_time -= t; 3669Sbill splx(s); 3679Sbill } 3687305Ssam 3697305Ssam /* 3707305Ssam * untimeout is called to remove a function timeout call 3717305Ssam * from the callout structure. 3727305Ssam */ 3738097Sroot untimeout(fun, arg) 3747305Ssam int (*fun)(); 3757305Ssam caddr_t arg; 3767305Ssam { 3777305Ssam register struct callout *p1, *p2; 3787305Ssam register int s; 3797305Ssam 3807305Ssam s = spl7(); 3817305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 3827305Ssam if (p2->c_func == fun && p2->c_arg == arg) { 3838112Sroot if (p2->c_next && p2->c_time > 0) 3847305Ssam p2->c_next->c_time += p2->c_time; 3857305Ssam p1->c_next = p2->c_next; 3867305Ssam p2->c_next = callfree; 3877305Ssam callfree = p2; 3887305Ssam break; 3897305Ssam } 3907305Ssam } 3917305Ssam splx(s); 3927305Ssam } 3938112Sroot 3948124Sroot /* 3958124Sroot * Compute number of hz until specified time. 3968124Sroot * Used to compute third argument to timeout() from an 3978124Sroot * absolute time. 3988124Sroot */ 3998112Sroot hzto(tv) 4008112Sroot struct timeval *tv; 4018112Sroot { 4028124Sroot register long ticks; 4038124Sroot register long sec; 4048112Sroot int s = spl7(); 4058112Sroot 4068124Sroot /* 4078124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 4088124Sroot * then compute number of milliseconds to time and scale to 4098124Sroot * ticks. Otherwise just compute number of hz in time, rounding 4108124Sroot * times greater than representible to maximum value. 4118124Sroot * 4128124Sroot * Delta times less than 25 days can be computed ``exactly''. 4138124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 4148124Sroot */ 4158124Sroot sec = tv->tv_sec - time.tv_sec; 4168124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 4178124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 4188124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 4198124Sroot else if (sec <= 0x7fffffff / hz) 4208124Sroot ticks = sec * hz; 4218124Sroot else 4228124Sroot ticks = 0x7fffffff; 4238112Sroot splx(s); 4248112Sroot return (ticks); 4258112Sroot } 42612747Ssam 42712747Ssam profil() 42812747Ssam { 42912747Ssam register struct a { 43012747Ssam short *bufbase; 43112747Ssam unsigned bufsize; 43212747Ssam unsigned pcoffset; 43312747Ssam unsigned pcscale; 43412747Ssam } *uap = (struct a *)u.u_ap; 43512747Ssam register struct uprof *upp = &u.u_prof; 43612747Ssam 43712747Ssam upp->pr_base = uap->bufbase; 43812747Ssam upp->pr_size = uap->bufsize; 43912747Ssam upp->pr_off = uap->pcoffset; 44012747Ssam upp->pr_scale = uap->pcscale; 44112747Ssam } 44212747Ssam 44312747Ssam opause() 44412747Ssam { 44512747Ssam 44612747Ssam for (;;) 44712747Ssam sleep((caddr_t)&u, PSLEP); 44812747Ssam } 449