123366Smckusick /* 223366Smckusick * Copyright (c) 1982 Regents of the University of California. 323366Smckusick * All rights reserved. The Berkeley software License Agreement 423366Smckusick * specifies the terms and conditions for redistribution. 523366Smckusick * 6*24524Sbloom * @(#)kern_clock.c 6.14 (Berkeley) 09/04/85 723366Smckusick */ 89Sbill 99751Ssam #include "../machine/reg.h" 109751Ssam #include "../machine/psl.h" 119751Ssam 1217088Sbloom #include "param.h" 1317088Sbloom #include "systm.h" 1417088Sbloom #include "dk.h" 1517088Sbloom #include "callout.h" 1617088Sbloom #include "dir.h" 1717088Sbloom #include "user.h" 1817088Sbloom #include "kernel.h" 1917088Sbloom #include "proc.h" 2017088Sbloom #include "vm.h" 2117088Sbloom #include "text.h" 229Sbill 239751Ssam #ifdef vax 249751Ssam #include "../vax/mtpr.h" 259751Ssam #endif 269751Ssam 2710291Smckusick #ifdef GPROF 2817088Sbloom #include "gprof.h" 2910291Smckusick #endif 3010291Smckusick 318124Sroot /* 328124Sroot * Clock handling routines. 338124Sroot * 3411392Ssam * This code is written to operate with two timers which run 3511392Ssam * independently of each other. The main clock, running at hz 3611392Ssam * times per second, is used to do scheduling and timeout calculations. 3711392Ssam * The second timer does resource utilization estimation statistically 3811392Ssam * based on the state of the machine phz times a second. Both functions 3911392Ssam * can be performed by a single clock (ie hz == phz), however the 4011392Ssam * statistics will be much more prone to errors. Ideally a machine 4111392Ssam * would have separate clocks measuring time spent in user state, system 4211392Ssam * state, interrupt state, and idle state. These clocks would allow a non- 4311392Ssam * approximate measure of resource utilization. 448124Sroot */ 451559Sbill 468124Sroot /* 478124Sroot * TODO: 4812747Ssam * time of day, system/user timing, timeouts, profiling on separate timers 4912747Ssam * allocate more timeout table slots when table overflows. 508124Sroot */ 5117007Smckusick #ifdef notdef 5217007Smckusick /* 5317007Smckusick * Bump a timeval by a small number of usec's. 5417007Smckusick */ 5517007Smckusick bumptime(tp, usec) 5617007Smckusick register struct timeval *tp; 5717007Smckusick int usec; 5817007Smckusick { 599Sbill 6017007Smckusick tp->tv_usec += usec; 6117007Smckusick if (tp->tv_usec >= 1000000) { 6217007Smckusick tp->tv_usec -= 1000000; 6317007Smckusick tp->tv_sec++; 6417007Smckusick } 6517007Smckusick } 6617007Smckusick #endif notdef 6717007Smckusick #define BUMPTIME(t, usec) { \ 6817007Smckusick register struct timeval *tp = (t); \ 6917007Smckusick \ 7017007Smckusick tp->tv_usec += (usec); \ 7117007Smckusick if (tp->tv_usec >= 1000000) { \ 7217007Smckusick tp->tv_usec -= 1000000; \ 7317007Smckusick tp->tv_sec++; \ 7417007Smckusick } \ 7517007Smckusick } 7617007Smckusick 778124Sroot /* 7811392Ssam * The hz hardware interval timer. 7911392Ssam * We update the events relating to real time. 8011392Ssam * If this timer is also being used to gather statistics, 8111392Ssam * we run through the statistics gathering routine as well. 828124Sroot */ 832609Swnj /*ARGSUSED*/ 842442Swnj hardclock(pc, ps) 852450Swnj caddr_t pc; 868944Sroot int ps; 879Sbill { 882768Swnj register struct callout *p1; 898097Sroot register struct proc *p; 90*24524Sbloom register int s; 9116172Skarels int needsoft = 0; 9217356Skarels extern int adjtimedelta, tickadj; 939Sbill 948124Sroot /* 958124Sroot * Update real-time timeout queue. 968124Sroot * At front of queue are some number of events which are ``due''. 978124Sroot * The time to these is <= 0 and if negative represents the 988124Sroot * number of ticks which have passed since it was supposed to happen. 998124Sroot * The rest of the q elements (times > 0) are events yet to happen, 1008124Sroot * where the time for each is given as a delta from the previous. 1018124Sroot * Decrementing just the first of these serves to decrement the time 1028124Sroot * to all events. 1038124Sroot */ 10412747Ssam p1 = calltodo.c_next; 10512747Ssam while (p1) { 10612747Ssam if (--p1->c_time > 0) 10712747Ssam break; 10816172Skarels needsoft = 1; 10912747Ssam if (p1->c_time == 0) 11012747Ssam break; 11112747Ssam p1 = p1->c_next; 11212747Ssam } 113138Sbill 1148124Sroot /* 1158124Sroot * Charge the time out based on the mode the cpu is in. 1168124Sroot * Here again we fudge for the lack of proper interval timers 1178124Sroot * assuming that the current state has been around at least 1188124Sroot * one tick. 1198124Sroot */ 1209Sbill if (USERMODE(ps)) { 12116172Skarels if (u.u_prof.pr_scale) 12216172Skarels needsoft = 1; 1238124Sroot /* 1248124Sroot * CPU was in user state. Increment 1258124Sroot * user time counter, and process process-virtual time 1269604Ssam * interval timer. 1278124Sroot */ 12817007Smckusick BUMPTIME(&u.u_ru.ru_utime, tick); 1298097Sroot if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && 1308097Sroot itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) 1318097Sroot psignal(u.u_procp, SIGVTALRM); 1329Sbill } else { 1338124Sroot /* 134*24524Sbloom * CPU was in system state. 1358124Sroot */ 136*24524Sbloom if (! noproc) { 13717007Smckusick BUMPTIME(&u.u_ru.ru_stime, tick); 1388028Sroot } 1399Sbill } 1408097Sroot 1418124Sroot /* 14210388Ssam * If the cpu is currently scheduled to a process, then 14310388Ssam * charge it with resource utilization for a tick, updating 14410388Ssam * statistics which run in (user+system) virtual time, 14510388Ssam * such as the cpu time limit and profiling timers. 14610388Ssam * This assumes that the current process has been running 14710388Ssam * the entire last tick. 14810388Ssam */ 14918585Skarels if (noproc == 0) { 15010388Ssam if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > 15110388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur) { 15210388Ssam psignal(u.u_procp, SIGXCPU); 15310388Ssam if (u.u_rlimit[RLIMIT_CPU].rlim_cur < 15410388Ssam u.u_rlimit[RLIMIT_CPU].rlim_max) 15510388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; 15610388Ssam } 15710388Ssam if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && 15810388Ssam itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) 15910388Ssam psignal(u.u_procp, SIGPROF); 16010388Ssam s = u.u_procp->p_rssize; 16110388Ssam u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */ 16210388Ssam if (u.u_procp->p_textp) { 16310388Ssam register int xrss = u.u_procp->p_textp->x_rssize; 16410388Ssam 16510388Ssam s += xrss; 16610388Ssam u.u_ru.ru_ixrss += xrss; 16710388Ssam } 16810388Ssam if (s > u.u_ru.ru_maxrss) 16910388Ssam u.u_ru.ru_maxrss = s; 17010388Ssam } 17110388Ssam 17210388Ssam /* 1738124Sroot * We adjust the priority of the current process. 1748124Sroot * The priority of a process gets worse as it accumulates 1758124Sroot * CPU time. The cpu usage estimator (p_cpu) is increased here 1768124Sroot * and the formula for computing priorities (in kern_synch.c) 1778124Sroot * will compute a different value each time the p_cpu increases 1788124Sroot * by 4. The cpu usage estimator ramps up quite quickly when 1798124Sroot * the process is running (linearly), and decays away exponentially, 1808124Sroot * at a rate which is proportionally slower when the system is 1818124Sroot * busy. The basic principal is that the system will 90% forget 1828124Sroot * that a process used a lot of CPU time in 5*loadav seconds. 1838124Sroot * This causes the system to favor processes which haven't run 1848124Sroot * much recently, and to round-robin among other processes. 1858124Sroot */ 1869Sbill if (!noproc) { 1878097Sroot p = u.u_procp; 1888097Sroot p->p_cpticks++; 1898097Sroot if (++p->p_cpu == 0) 1908097Sroot p->p_cpu--; 1918124Sroot if ((p->p_cpu&3) == 0) { 1928097Sroot (void) setpri(p); 1938097Sroot if (p->p_pri >= PUSER) 1948097Sroot p->p_pri = p->p_usrpri; 1959Sbill } 1969Sbill } 1978124Sroot 1988124Sroot /* 19911392Ssam * If the alternate clock has not made itself known then 20011392Ssam * we must gather the statistics. 20111392Ssam */ 20211392Ssam if (phz == 0) 20311392Ssam gatherstats(pc, ps); 20411392Ssam 20511392Ssam /* 2068124Sroot * Increment the time-of-day, and schedule 2078124Sroot * processing of the callouts at a very low cpu priority, 2088124Sroot * so we don't keep the relatively high clock interrupt 2098124Sroot * priority any longer than necessary. 2108124Sroot */ 21117356Skarels if (adjtimedelta == 0) 21217356Skarels BUMPTIME(&time, tick) 21317356Skarels else { 21417356Skarels register delta; 21517356Skarels 21617356Skarels if (adjtimedelta < 0) { 21717356Skarels delta = tick - tickadj; 21817356Skarels adjtimedelta += tickadj; 21917356Skarels } else { 22017356Skarels delta = tick + tickadj; 22117356Skarels adjtimedelta -= tickadj; 22217356Skarels } 22317356Skarels BUMPTIME(&time, delta); 22417356Skarels } 22516525Skarels if (needsoft) { 22616525Skarels if (BASEPRI(ps)) { 22716525Skarels /* 22816525Skarels * Save the overhead of a software interrupt; 22916525Skarels * it will happen as soon as we return, so do it now. 23016525Skarels */ 23116525Skarels (void) splsoftclock(); 23216525Skarels softclock(pc, ps); 23316525Skarels } else 23416525Skarels setsoftclock(); 23516525Skarels } 2362442Swnj } 2372442Swnj 23815191Ssam int dk_ndrive = DK_NDRIVE; 2398124Sroot /* 24011392Ssam * Gather statistics on resource utilization. 24111392Ssam * 24211392Ssam * We make a gross assumption: that the system has been in the 24311392Ssam * state it is in (user state, kernel state, interrupt state, 24411392Ssam * or idle state) for the entire last time interval, and 24511392Ssam * update statistics accordingly. 24611392Ssam */ 24712747Ssam /*ARGSUSED*/ 24811392Ssam gatherstats(pc, ps) 24911392Ssam caddr_t pc; 25011392Ssam int ps; 25111392Ssam { 25211392Ssam int cpstate, s; 25311392Ssam 25411392Ssam /* 25511392Ssam * Determine what state the cpu is in. 25611392Ssam */ 25711392Ssam if (USERMODE(ps)) { 25811392Ssam /* 25911392Ssam * CPU was in user state. 26011392Ssam */ 26111392Ssam if (u.u_procp->p_nice > NZERO) 26211392Ssam cpstate = CP_NICE; 26311392Ssam else 26411392Ssam cpstate = CP_USER; 26511392Ssam } else { 26611392Ssam /* 26711392Ssam * CPU was in system state. If profiling kernel 268*24524Sbloom * increment a counter. If no process is running 269*24524Sbloom * then this is a system tick if we were running 270*24524Sbloom * at a non-zero IPL (in a driver). If a process is running, 271*24524Sbloom * then we charge it with system time even if we were 272*24524Sbloom * at a non-zero IPL, since the system often runs 273*24524Sbloom * this way during processing of system calls. 274*24524Sbloom * This is approximate, but the lack of true interval 275*24524Sbloom * timers makes doing anything else difficult. 27611392Ssam */ 27711392Ssam cpstate = CP_SYS; 27811392Ssam if (noproc && BASEPRI(ps)) 27911392Ssam cpstate = CP_IDLE; 28011392Ssam #ifdef GPROF 28111392Ssam s = pc - s_lowpc; 28211392Ssam if (profiling < 2 && s < s_textsize) 28311392Ssam kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 28411392Ssam #endif 28511392Ssam } 28611392Ssam /* 28711392Ssam * We maintain statistics shown by user-level statistics 28811392Ssam * programs: the amount of time in each cpu state, and 28911392Ssam * the amount of time each of DK_NDRIVE ``drives'' is busy. 29011392Ssam */ 29111392Ssam cp_time[cpstate]++; 29211392Ssam for (s = 0; s < DK_NDRIVE; s++) 29311392Ssam if (dk_busy&(1<<s)) 29411392Ssam dk_time[s]++; 29511392Ssam } 29611392Ssam 29711392Ssam /* 2988124Sroot * Software priority level clock interrupt. 2998124Sroot * Run periodic events from timeout queue. 3008124Sroot */ 3012609Swnj /*ARGSUSED*/ 3022442Swnj softclock(pc, ps) 3032450Swnj caddr_t pc; 3048944Sroot int ps; 3052442Swnj { 3062442Swnj 3078097Sroot for (;;) { 3088124Sroot register struct callout *p1; 3098124Sroot register caddr_t arg; 3108124Sroot register int (*func)(); 3118124Sroot register int a, s; 3128124Sroot 3138097Sroot s = spl7(); 3148097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 3158097Sroot splx(s); 3168097Sroot break; 3172442Swnj } 3188124Sroot arg = p1->c_arg; func = p1->c_func; a = p1->c_time; 3198097Sroot calltodo.c_next = p1->c_next; 3208097Sroot p1->c_next = callfree; 3218097Sroot callfree = p1; 3229157Ssam splx(s); 3238112Sroot (*func)(arg, a); 3242442Swnj } 3259604Ssam /* 32613127Ssam * If trapped user-mode and profiling, give it 32713127Ssam * a profiling tick. 3289604Ssam */ 32913127Ssam if (USERMODE(ps)) { 33013127Ssam register struct proc *p = u.u_procp; 33113127Ssam 33213127Ssam if (u.u_prof.pr_scale) { 33313127Ssam p->p_flag |= SOWEUPC; 33413127Ssam aston(); 33513127Ssam } 33613127Ssam /* 33713127Ssam * Check to see if process has accumulated 33813127Ssam * more than 10 minutes of user time. If so 33913127Ssam * reduce priority to give others a chance. 34013127Ssam */ 34113127Ssam if (p->p_uid && p->p_nice == NZERO && 34213127Ssam u.u_ru.ru_utime.tv_sec > 10 * 60) { 34313127Ssam p->p_nice = NZERO+4; 34413127Ssam (void) setpri(p); 34513127Ssam p->p_pri = p->p_usrpri; 34613127Ssam } 3479604Ssam } 3489Sbill } 3499Sbill 3509Sbill /* 35112747Ssam * Arrange that (*fun)(arg) is called in t/hz seconds. 35212747Ssam */ 35312747Ssam timeout(fun, arg, t) 3542450Swnj int (*fun)(); 3552450Swnj caddr_t arg; 35612747Ssam register int t; 3579Sbill { 3583542Swnj register struct callout *p1, *p2, *pnew; 35912747Ssam register int s = spl7(); 3609Sbill 36118282Smckusick if (t <= 0) 36212747Ssam t = 1; 3633542Swnj pnew = callfree; 3643542Swnj if (pnew == NULL) 3653542Swnj panic("timeout table overflow"); 3663542Swnj callfree = pnew->c_next; 3673542Swnj pnew->c_arg = arg; 3683542Swnj pnew->c_func = fun; 3693542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 3709742Ssam if (p2->c_time > 0) 3719742Ssam t -= p2->c_time; 3723542Swnj p1->c_next = pnew; 3733542Swnj pnew->c_next = p2; 3743542Swnj pnew->c_time = t; 3753542Swnj if (p2) 3763542Swnj p2->c_time -= t; 3779Sbill splx(s); 3789Sbill } 3797305Ssam 3807305Ssam /* 3817305Ssam * untimeout is called to remove a function timeout call 3827305Ssam * from the callout structure. 3837305Ssam */ 3848097Sroot untimeout(fun, arg) 3857305Ssam int (*fun)(); 3867305Ssam caddr_t arg; 3877305Ssam { 3887305Ssam register struct callout *p1, *p2; 3897305Ssam register int s; 3907305Ssam 3917305Ssam s = spl7(); 3927305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 3937305Ssam if (p2->c_func == fun && p2->c_arg == arg) { 3948112Sroot if (p2->c_next && p2->c_time > 0) 3957305Ssam p2->c_next->c_time += p2->c_time; 3967305Ssam p1->c_next = p2->c_next; 3977305Ssam p2->c_next = callfree; 3987305Ssam callfree = p2; 3997305Ssam break; 4007305Ssam } 4017305Ssam } 4027305Ssam splx(s); 4037305Ssam } 4048112Sroot 4058124Sroot /* 4068124Sroot * Compute number of hz until specified time. 4078124Sroot * Used to compute third argument to timeout() from an 4088124Sroot * absolute time. 4098124Sroot */ 4108112Sroot hzto(tv) 4118112Sroot struct timeval *tv; 4128112Sroot { 4138124Sroot register long ticks; 4148124Sroot register long sec; 4158112Sroot int s = spl7(); 4168112Sroot 4178124Sroot /* 4188124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 4198124Sroot * then compute number of milliseconds to time and scale to 4208124Sroot * ticks. Otherwise just compute number of hz in time, rounding 4218124Sroot * times greater than representible to maximum value. 4228124Sroot * 4238124Sroot * Delta times less than 25 days can be computed ``exactly''. 4248124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 4258124Sroot */ 4268124Sroot sec = tv->tv_sec - time.tv_sec; 4278124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 4288124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 4298124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 4308124Sroot else if (sec <= 0x7fffffff / hz) 4318124Sroot ticks = sec * hz; 4328124Sroot else 4338124Sroot ticks = 0x7fffffff; 4348112Sroot splx(s); 4358112Sroot return (ticks); 4368112Sroot } 43712747Ssam 43812747Ssam profil() 43912747Ssam { 44012747Ssam register struct a { 44112747Ssam short *bufbase; 44212747Ssam unsigned bufsize; 44312747Ssam unsigned pcoffset; 44412747Ssam unsigned pcscale; 44512747Ssam } *uap = (struct a *)u.u_ap; 44612747Ssam register struct uprof *upp = &u.u_prof; 44712747Ssam 44812747Ssam upp->pr_base = uap->bufbase; 44912747Ssam upp->pr_size = uap->bufsize; 45012747Ssam upp->pr_off = uap->pcoffset; 45112747Ssam upp->pr_scale = uap->pcscale; 45212747Ssam } 45312747Ssam 45412747Ssam opause() 45512747Ssam { 45612747Ssam 45712747Ssam for (;;) 45812747Ssam sleep((caddr_t)&u, PSLEP); 45912747Ssam } 460