123366Smckusick /* 247546Skarels * Copyright (c) 1982, 1986, 1991 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*48979Skarels * @(#)kern_clock.c 7.13 (Berkeley) 05/03/91 723366Smckusick */ 89Sbill 917088Sbloom #include "param.h" 1017088Sbloom #include "systm.h" 1129946Skarels #include "dkstat.h" 1217088Sbloom #include "callout.h" 1317088Sbloom #include "kernel.h" 1417088Sbloom #include "proc.h" 15*48979Skarels #include "resourcevar.h" 169Sbill 1747546Skarels #include "machine/cpu.h" 1835406Skarels 1910291Smckusick #ifdef GPROF 2017088Sbloom #include "gprof.h" 2110291Smckusick #endif 2210291Smckusick 238124Sroot /* 248124Sroot * Clock handling routines. 258124Sroot * 2611392Ssam * This code is written to operate with two timers which run 2711392Ssam * independently of each other. The main clock, running at hz 2811392Ssam * times per second, is used to do scheduling and timeout calculations. 2911392Ssam * The second timer does resource utilization estimation statistically 3011392Ssam * based on the state of the machine phz times a second. Both functions 3111392Ssam * can be performed by a single clock (ie hz == phz), however the 3211392Ssam * statistics will be much more prone to errors. Ideally a machine 3311392Ssam * would have separate clocks measuring time spent in user state, system 3411392Ssam * state, interrupt state, and idle state. These clocks would allow a non- 3511392Ssam * approximate measure of resource utilization. 368124Sroot */ 371559Sbill 388124Sroot /* 398124Sroot * TODO: 4012747Ssam * time of day, system/user timing, timeouts, profiling on separate timers 4112747Ssam * allocate more timeout table slots when table overflows. 428124Sroot */ 4326265Skarels 4417007Smckusick /* 4517007Smckusick * Bump a timeval by a small number of usec's. 4617007Smckusick */ 4717007Smckusick #define BUMPTIME(t, usec) { \ 4817007Smckusick register struct timeval *tp = (t); \ 4917007Smckusick \ 5017007Smckusick tp->tv_usec += (usec); \ 5117007Smckusick if (tp->tv_usec >= 1000000) { \ 5217007Smckusick tp->tv_usec -= 1000000; \ 5317007Smckusick tp->tv_sec++; \ 5417007Smckusick } \ 5517007Smckusick } 5617007Smckusick 578124Sroot /* 5811392Ssam * The hz hardware interval timer. 5911392Ssam * We update the events relating to real time. 6011392Ssam * If this timer is also being used to gather statistics, 6111392Ssam * we run through the statistics gathering routine as well. 628124Sroot */ 6344774Swilliam hardclock(frame) 6447546Skarels clockframe frame; 659Sbill { 662768Swnj register struct callout *p1; 6747546Skarels register struct proc *p = curproc; 68*48979Skarels register struct pstats *pstats; 6924524Sbloom register int s; 7016172Skarels int needsoft = 0; 7128947Skarels extern int tickdelta; 7228947Skarels extern long timedelta; 739Sbill 748124Sroot /* 758124Sroot * Update real-time timeout queue. 768124Sroot * At front of queue are some number of events which are ``due''. 778124Sroot * The time to these is <= 0 and if negative represents the 788124Sroot * number of ticks which have passed since it was supposed to happen. 798124Sroot * The rest of the q elements (times > 0) are events yet to happen, 808124Sroot * where the time for each is given as a delta from the previous. 818124Sroot * Decrementing just the first of these serves to decrement the time 828124Sroot * to all events. 838124Sroot */ 8412747Ssam p1 = calltodo.c_next; 8512747Ssam while (p1) { 8612747Ssam if (--p1->c_time > 0) 8712747Ssam break; 8816172Skarels needsoft = 1; 8912747Ssam if (p1->c_time == 0) 9012747Ssam break; 9112747Ssam p1 = p1->c_next; 9212747Ssam } 93138Sbill 948124Sroot /* 95*48979Skarels * Curproc (now in p) is null if no process is running. 96*48979Skarels * We assume that curproc is set in user mode! 97*48979Skarels */ 98*48979Skarels if (p) 99*48979Skarels pstats = p->p_stats; 100*48979Skarels /* 1018124Sroot * Charge the time out based on the mode the cpu is in. 1028124Sroot * Here again we fudge for the lack of proper interval timers 1038124Sroot * assuming that the current state has been around at least 1048124Sroot * one tick. 1058124Sroot */ 10647546Skarels if (CLKF_USERMODE(&frame)) { 10747546Skarels if (pstats->p_prof.pr_scale) 10816172Skarels needsoft = 1; 1098124Sroot /* 1108124Sroot * CPU was in user state. Increment 1118124Sroot * user time counter, and process process-virtual time 1129604Ssam * interval timer. 1138124Sroot */ 11440674Smarc BUMPTIME(&p->p_utime, tick); 11547546Skarels if (timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) && 11647546Skarels itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 11740674Smarc psignal(p, SIGVTALRM); 1189Sbill } else { 1198124Sroot /* 12024524Sbloom * CPU was in system state. 1218124Sroot */ 122*48979Skarels if (p) 12340674Smarc BUMPTIME(&p->p_stime, tick); 1249Sbill } 1258097Sroot 1268124Sroot /* 12710388Ssam * If the cpu is currently scheduled to a process, then 12810388Ssam * charge it with resource utilization for a tick, updating 12910388Ssam * statistics which run in (user+system) virtual time, 13010388Ssam * such as the cpu time limit and profiling timers. 13110388Ssam * This assumes that the current process has been running 13210388Ssam * the entire last tick. 13310388Ssam */ 134*48979Skarels if (p) { 13540674Smarc if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) > 13647546Skarels p->p_rlimit[RLIMIT_CPU].rlim_cur) { 13740674Smarc psignal(p, SIGXCPU); 13847546Skarels if (p->p_rlimit[RLIMIT_CPU].rlim_cur < 13947546Skarels p->p_rlimit[RLIMIT_CPU].rlim_max) 14047546Skarels p->p_rlimit[RLIMIT_CPU].rlim_cur += 5; 14110388Ssam } 14247546Skarels if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) && 14347546Skarels itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 14440674Smarc psignal(p, SIGPROF); 14510388Ssam 14647546Skarels /* 14747546Skarels * We adjust the priority of the current process. 14847546Skarels * The priority of a process gets worse as it accumulates 14947546Skarels * CPU time. The cpu usage estimator (p_cpu) is increased here 15047546Skarels * and the formula for computing priorities (in kern_synch.c) 15147546Skarels * will compute a different value each time the p_cpu increases 15247546Skarels * by 4. The cpu usage estimator ramps up quite quickly when 15347546Skarels * the process is running (linearly), and decays away 15447546Skarels * exponentially, * at a rate which is proportionally slower 15547546Skarels * when the system is busy. The basic principal is that the 15647546Skarels * system will 90% forget that a process used a lot of CPU 15747546Skarels * time in 5*loadav seconds. This causes the system to favor 15847546Skarels * processes which haven't run much recently, and to 15947546Skarels * round-robin among other processes. 16047546Skarels */ 1618097Sroot p->p_cpticks++; 1628097Sroot if (++p->p_cpu == 0) 1638097Sroot p->p_cpu--; 1648124Sroot if ((p->p_cpu&3) == 0) { 16547546Skarels setpri(p); 1668097Sroot if (p->p_pri >= PUSER) 1678097Sroot p->p_pri = p->p_usrpri; 1689Sbill } 1699Sbill } 1708124Sroot 1718124Sroot /* 17211392Ssam * If the alternate clock has not made itself known then 17311392Ssam * we must gather the statistics. 17411392Ssam */ 17511392Ssam if (phz == 0) 17647546Skarels gatherstats(&frame); 17711392Ssam 17811392Ssam /* 1798124Sroot * Increment the time-of-day, and schedule 1808124Sroot * processing of the callouts at a very low cpu priority, 1818124Sroot * so we don't keep the relatively high clock interrupt 1828124Sroot * priority any longer than necessary. 1838124Sroot */ 18428828Skarels if (timedelta == 0) 18517356Skarels BUMPTIME(&time, tick) 18617356Skarels else { 18717356Skarels register delta; 18817356Skarels 18928828Skarels if (timedelta < 0) { 19028828Skarels delta = tick - tickdelta; 19128828Skarels timedelta += tickdelta; 19217356Skarels } else { 19328828Skarels delta = tick + tickdelta; 19428828Skarels timedelta -= tickdelta; 19517356Skarels } 19617356Skarels BUMPTIME(&time, delta); 19717356Skarels } 19816525Skarels if (needsoft) { 19947546Skarels if (CLKF_BASEPRI(&frame)) { 20016525Skarels /* 20116525Skarels * Save the overhead of a software interrupt; 20216525Skarels * it will happen as soon as we return, so do it now. 20316525Skarels */ 20416525Skarels (void) splsoftclock(); 20544774Swilliam softclock(frame); 20616525Skarels } else 20716525Skarels setsoftclock(); 20816525Skarels } 2092442Swnj } 2102442Swnj 21115191Ssam int dk_ndrive = DK_NDRIVE; 2128124Sroot /* 21311392Ssam * Gather statistics on resource utilization. 21411392Ssam * 21511392Ssam * We make a gross assumption: that the system has been in the 21611392Ssam * state it is in (user state, kernel state, interrupt state, 21711392Ssam * or idle state) for the entire last time interval, and 21811392Ssam * update statistics accordingly. 21911392Ssam */ 22047546Skarels gatherstats(framep) 22147546Skarels clockframe *framep; 22211392Ssam { 22326265Skarels register int cpstate, s; 22411392Ssam 22511392Ssam /* 22611392Ssam * Determine what state the cpu is in. 22711392Ssam */ 22847546Skarels if (CLKF_USERMODE(framep)) { 22911392Ssam /* 23011392Ssam * CPU was in user state. 23111392Ssam */ 23247546Skarels if (curproc->p_nice > NZERO) 23311392Ssam cpstate = CP_NICE; 23411392Ssam else 23511392Ssam cpstate = CP_USER; 23611392Ssam } else { 23711392Ssam /* 23811392Ssam * CPU was in system state. If profiling kernel 23924524Sbloom * increment a counter. If no process is running 24024524Sbloom * then this is a system tick if we were running 24124524Sbloom * at a non-zero IPL (in a driver). If a process is running, 24224524Sbloom * then we charge it with system time even if we were 24324524Sbloom * at a non-zero IPL, since the system often runs 24424524Sbloom * this way during processing of system calls. 24524524Sbloom * This is approximate, but the lack of true interval 24624524Sbloom * timers makes doing anything else difficult. 24711392Ssam */ 24811392Ssam cpstate = CP_SYS; 249*48979Skarels if (curproc == NULL && CLKF_BASEPRI(framep)) 25011392Ssam cpstate = CP_IDLE; 25111392Ssam #ifdef GPROF 25247546Skarels s = CLKF_PC(framep) - s_lowpc; 25311392Ssam if (profiling < 2 && s < s_textsize) 25411392Ssam kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 25511392Ssam #endif 25611392Ssam } 25711392Ssam /* 25811392Ssam * We maintain statistics shown by user-level statistics 25911392Ssam * programs: the amount of time in each cpu state, and 26011392Ssam * the amount of time each of DK_NDRIVE ``drives'' is busy. 26111392Ssam */ 26211392Ssam cp_time[cpstate]++; 26311392Ssam for (s = 0; s < DK_NDRIVE; s++) 26429946Skarels if (dk_busy&(1<<s)) 26511392Ssam dk_time[s]++; 26611392Ssam } 26711392Ssam 26811392Ssam /* 2698124Sroot * Software priority level clock interrupt. 2708124Sroot * Run periodic events from timeout queue. 2718124Sroot */ 2722609Swnj /*ARGSUSED*/ 27344774Swilliam softclock(frame) 27447546Skarels clockframe frame; 2752442Swnj { 2762442Swnj 2778097Sroot for (;;) { 2788124Sroot register struct callout *p1; 2798124Sroot register caddr_t arg; 2808124Sroot register int (*func)(); 2818124Sroot register int a, s; 2828124Sroot 28326265Skarels s = splhigh(); 2848097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 2858097Sroot splx(s); 2868097Sroot break; 2872442Swnj } 2888124Sroot arg = p1->c_arg; func = p1->c_func; a = p1->c_time; 2898097Sroot calltodo.c_next = p1->c_next; 2908097Sroot p1->c_next = callfree; 2918097Sroot callfree = p1; 2929157Ssam splx(s); 2938112Sroot (*func)(arg, a); 2942442Swnj } 2959604Ssam /* 29613127Ssam * If trapped user-mode and profiling, give it 29713127Ssam * a profiling tick. 2989604Ssam */ 29947546Skarels if (CLKF_USERMODE(&frame)) { 30047546Skarels register struct proc *p = curproc; 30113127Ssam 30247546Skarels if (p->p_stats->p_prof.pr_scale) 30347546Skarels profile_tick(p, &frame); 30413127Ssam /* 30513127Ssam * Check to see if process has accumulated 30613127Ssam * more than 10 minutes of user time. If so 30713127Ssam * reduce priority to give others a chance. 30813127Ssam */ 30947546Skarels if (p->p_ucred->cr_uid && p->p_nice == NZERO && 31040674Smarc p->p_utime.tv_sec > 10 * 60) { 31147546Skarels p->p_nice = NZERO + 4; 31247546Skarels setpri(p); 31313127Ssam p->p_pri = p->p_usrpri; 31413127Ssam } 3159604Ssam } 3169Sbill } 3179Sbill 3189Sbill /* 31947546Skarels * Arrange that (*func)(arg) is called in t/hz seconds. 32012747Ssam */ 32147546Skarels timeout(func, arg, t) 32247546Skarels int (*func)(); 3232450Swnj caddr_t arg; 32412747Ssam register int t; 3259Sbill { 3263542Swnj register struct callout *p1, *p2, *pnew; 32726265Skarels register int s = splhigh(); 3289Sbill 32918282Smckusick if (t <= 0) 33012747Ssam t = 1; 3313542Swnj pnew = callfree; 3323542Swnj if (pnew == NULL) 3333542Swnj panic("timeout table overflow"); 3343542Swnj callfree = pnew->c_next; 3353542Swnj pnew->c_arg = arg; 33647546Skarels pnew->c_func = func; 3373542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 3389742Ssam if (p2->c_time > 0) 3399742Ssam t -= p2->c_time; 3403542Swnj p1->c_next = pnew; 3413542Swnj pnew->c_next = p2; 3423542Swnj pnew->c_time = t; 3433542Swnj if (p2) 3443542Swnj p2->c_time -= t; 3459Sbill splx(s); 3469Sbill } 3477305Ssam 3487305Ssam /* 3497305Ssam * untimeout is called to remove a function timeout call 3507305Ssam * from the callout structure. 3517305Ssam */ 35247546Skarels untimeout(func, arg) 35347546Skarels int (*func)(); 3547305Ssam caddr_t arg; 3557305Ssam { 3567305Ssam register struct callout *p1, *p2; 3577305Ssam register int s; 3587305Ssam 35926265Skarels s = splhigh(); 3607305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 36147546Skarels if (p2->c_func == func && p2->c_arg == arg) { 3628112Sroot if (p2->c_next && p2->c_time > 0) 3637305Ssam p2->c_next->c_time += p2->c_time; 3647305Ssam p1->c_next = p2->c_next; 3657305Ssam p2->c_next = callfree; 3667305Ssam callfree = p2; 3677305Ssam break; 3687305Ssam } 3697305Ssam } 3707305Ssam splx(s); 3717305Ssam } 3728112Sroot 3738124Sroot /* 3748124Sroot * Compute number of hz until specified time. 3758124Sroot * Used to compute third argument to timeout() from an 3768124Sroot * absolute time. 3778124Sroot */ 3788112Sroot hzto(tv) 3798112Sroot struct timeval *tv; 3808112Sroot { 3818124Sroot register long ticks; 3828124Sroot register long sec; 38326265Skarels int s = splhigh(); 3848112Sroot 3858124Sroot /* 3868124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 3878124Sroot * then compute number of milliseconds to time and scale to 3888124Sroot * ticks. Otherwise just compute number of hz in time, rounding 3898124Sroot * times greater than representible to maximum value. 3908124Sroot * 3918124Sroot * Delta times less than 25 days can be computed ``exactly''. 3928124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 3938124Sroot */ 3948124Sroot sec = tv->tv_sec - time.tv_sec; 3958124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 3968124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 3978124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 3988124Sroot else if (sec <= 0x7fffffff / hz) 3998124Sroot ticks = sec * hz; 4008124Sroot else 4018124Sroot ticks = 0x7fffffff; 4028112Sroot splx(s); 4038112Sroot return (ticks); 4048112Sroot } 40512747Ssam 40643402Smckusick /* ARGSUSED */ 40743402Smckusick profil(p, uap, retval) 40843402Smckusick struct proc *p; 40943402Smckusick register struct args { 41012747Ssam short *bufbase; 41112747Ssam unsigned bufsize; 41212747Ssam unsigned pcoffset; 41312747Ssam unsigned pcscale; 41443402Smckusick } *uap; 41543402Smckusick int *retval; 41643402Smckusick { 41747546Skarels register struct uprof *upp = &p->p_stats->p_prof; 41812747Ssam 41912747Ssam upp->pr_base = uap->bufbase; 42012747Ssam upp->pr_size = uap->bufsize; 42112747Ssam upp->pr_off = uap->pcoffset; 42212747Ssam upp->pr_scale = uap->pcscale; 42344404Skarels return (0); 42412747Ssam } 425