149594Sbostic /*- 249594Sbostic * Copyright (c) 1982, 1986, 1991 The Regents of the University of California. 349594Sbostic * All rights reserved. 423366Smckusick * 549594Sbostic * %sccs.include.redist.c% 649594Sbostic * 7*52951Smckusick * @(#)kern_clock.c 7.18 (Berkeley) 03/15/92 823366Smckusick */ 99Sbill 1017088Sbloom #include "param.h" 1117088Sbloom #include "systm.h" 1229946Skarels #include "dkstat.h" 1317088Sbloom #include "callout.h" 1417088Sbloom #include "kernel.h" 1517088Sbloom #include "proc.h" 1648979Skarels #include "resourcevar.h" 179Sbill 1847546Skarels #include "machine/cpu.h" 1935406Skarels 2010291Smckusick #ifdef GPROF 2117088Sbloom #include "gprof.h" 2210291Smckusick #endif 2310291Smckusick 248124Sroot /* 258124Sroot * Clock handling routines. 268124Sroot * 2711392Ssam * This code is written to operate with two timers which run 2811392Ssam * independently of each other. The main clock, running at hz 2911392Ssam * times per second, is used to do scheduling and timeout calculations. 3011392Ssam * The second timer does resource utilization estimation statistically 3111392Ssam * based on the state of the machine phz times a second. Both functions 3211392Ssam * can be performed by a single clock (ie hz == phz), however the 3311392Ssam * statistics will be much more prone to errors. Ideally a machine 3411392Ssam * would have separate clocks measuring time spent in user state, system 3511392Ssam * state, interrupt state, and idle state. These clocks would allow a non- 3611392Ssam * approximate measure of resource utilization. 378124Sroot */ 381559Sbill 398124Sroot /* 408124Sroot * TODO: 4112747Ssam * time of day, system/user timing, timeouts, profiling on separate timers 4212747Ssam * allocate more timeout table slots when table overflows. 438124Sroot */ 4426265Skarels 4517007Smckusick /* 4617007Smckusick * Bump a timeval by a small number of usec's. 4717007Smckusick */ 4817007Smckusick #define BUMPTIME(t, usec) { \ 4917007Smckusick register struct timeval *tp = (t); \ 5017007Smckusick \ 5117007Smckusick tp->tv_usec += (usec); \ 5217007Smckusick if (tp->tv_usec >= 1000000) { \ 5317007Smckusick tp->tv_usec -= 1000000; \ 5417007Smckusick tp->tv_sec++; \ 5517007Smckusick } \ 5617007Smckusick } 5717007Smckusick 588124Sroot /* 5911392Ssam * The hz hardware interval timer. 6011392Ssam * We update the events relating to real time. 6111392Ssam * If this timer is also being used to gather statistics, 6211392Ssam * we run through the statistics gathering routine as well. 638124Sroot */ 6444774Swilliam hardclock(frame) 6547546Skarels clockframe frame; 669Sbill { 672768Swnj register struct callout *p1; 6847546Skarels register struct proc *p = curproc; 6948979Skarels register struct pstats *pstats; 7024524Sbloom register int s; 7116172Skarels int needsoft = 0; 72*52951Smckusick time_t secs; 7328947Skarels extern int tickdelta; 7428947Skarels extern long timedelta; 759Sbill 768124Sroot /* 778124Sroot * Update real-time timeout queue. 788124Sroot * At front of queue are some number of events which are ``due''. 798124Sroot * The time to these is <= 0 and if negative represents the 808124Sroot * number of ticks which have passed since it was supposed to happen. 818124Sroot * The rest of the q elements (times > 0) are events yet to happen, 828124Sroot * where the time for each is given as a delta from the previous. 838124Sroot * Decrementing just the first of these serves to decrement the time 848124Sroot * to all events. 858124Sroot */ 8612747Ssam p1 = calltodo.c_next; 8712747Ssam while (p1) { 8812747Ssam if (--p1->c_time > 0) 8912747Ssam break; 9016172Skarels needsoft = 1; 9112747Ssam if (p1->c_time == 0) 9212747Ssam break; 9312747Ssam p1 = p1->c_next; 9412747Ssam } 95138Sbill 968124Sroot /* 9748979Skarels * Curproc (now in p) is null if no process is running. 9848979Skarels * We assume that curproc is set in user mode! 9948979Skarels */ 10048979Skarels if (p) 10148979Skarels pstats = p->p_stats; 10248979Skarels /* 1038124Sroot * Charge the time out based on the mode the cpu is in. 1048124Sroot * Here again we fudge for the lack of proper interval timers 1058124Sroot * assuming that the current state has been around at least 1068124Sroot * one tick. 1078124Sroot */ 10847546Skarels if (CLKF_USERMODE(&frame)) { 10947546Skarels if (pstats->p_prof.pr_scale) 11016172Skarels needsoft = 1; 1118124Sroot /* 1128124Sroot * CPU was in user state. Increment 1138124Sroot * user time counter, and process process-virtual time 1149604Ssam * interval timer. 1158124Sroot */ 11640674Smarc BUMPTIME(&p->p_utime, tick); 11747546Skarels if (timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) && 11847546Skarels itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 11940674Smarc psignal(p, SIGVTALRM); 1209Sbill } else { 1218124Sroot /* 12224524Sbloom * CPU was in system state. 1238124Sroot */ 12448979Skarels if (p) 12540674Smarc BUMPTIME(&p->p_stime, tick); 1269Sbill } 1278097Sroot 1288124Sroot /* 12910388Ssam * If the cpu is currently scheduled to a process, then 13010388Ssam * charge it with resource utilization for a tick, updating 13110388Ssam * statistics which run in (user+system) virtual time, 13210388Ssam * such as the cpu time limit and profiling timers. 13310388Ssam * This assumes that the current process has been running 13410388Ssam * the entire last tick. 13510388Ssam */ 13648979Skarels if (p) { 137*52951Smckusick secs = p->p_utime.tv_sec + p->p_stime.tv_sec + 1; 138*52951Smckusick if (secs > p->p_rlimit[RLIMIT_CPU].rlim_cur) { 139*52951Smckusick if (secs > p->p_rlimit[RLIMIT_CPU].rlim_max) 140*52951Smckusick psignal(p, SIGKILL); 141*52951Smckusick else { 142*52951Smckusick psignal(p, SIGXCPU); 143*52951Smckusick if (p->p_rlimit[RLIMIT_CPU].rlim_cur < 144*52951Smckusick p->p_rlimit[RLIMIT_CPU].rlim_max) 145*52951Smckusick p->p_rlimit[RLIMIT_CPU].rlim_cur += 5; 146*52951Smckusick } 14710388Ssam } 14847546Skarels if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) && 14947546Skarels itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 15040674Smarc psignal(p, SIGPROF); 15110388Ssam 15247546Skarels /* 15347546Skarels * We adjust the priority of the current process. 15447546Skarels * The priority of a process gets worse as it accumulates 15547546Skarels * CPU time. The cpu usage estimator (p_cpu) is increased here 15647546Skarels * and the formula for computing priorities (in kern_synch.c) 15747546Skarels * will compute a different value each time the p_cpu increases 15847546Skarels * by 4. The cpu usage estimator ramps up quite quickly when 15947546Skarels * the process is running (linearly), and decays away 16047546Skarels * exponentially, * at a rate which is proportionally slower 16147546Skarels * when the system is busy. The basic principal is that the 16247546Skarels * system will 90% forget that a process used a lot of CPU 16347546Skarels * time in 5*loadav seconds. This causes the system to favor 16447546Skarels * processes which haven't run much recently, and to 16547546Skarels * round-robin among other processes. 16647546Skarels */ 1678097Sroot p->p_cpticks++; 1688097Sroot if (++p->p_cpu == 0) 1698097Sroot p->p_cpu--; 1708124Sroot if ((p->p_cpu&3) == 0) { 17147546Skarels setpri(p); 1728097Sroot if (p->p_pri >= PUSER) 1738097Sroot p->p_pri = p->p_usrpri; 1749Sbill } 1759Sbill } 1768124Sroot 1778124Sroot /* 17811392Ssam * If the alternate clock has not made itself known then 17911392Ssam * we must gather the statistics. 18011392Ssam */ 18111392Ssam if (phz == 0) 18247546Skarels gatherstats(&frame); 18311392Ssam 18411392Ssam /* 1858124Sroot * Increment the time-of-day, and schedule 1868124Sroot * processing of the callouts at a very low cpu priority, 1878124Sroot * so we don't keep the relatively high clock interrupt 1888124Sroot * priority any longer than necessary. 1898124Sroot */ 19028828Skarels if (timedelta == 0) 19117356Skarels BUMPTIME(&time, tick) 19217356Skarels else { 19317356Skarels register delta; 19417356Skarels 19528828Skarels if (timedelta < 0) { 19628828Skarels delta = tick - tickdelta; 19728828Skarels timedelta += tickdelta; 19817356Skarels } else { 19928828Skarels delta = tick + tickdelta; 20028828Skarels timedelta -= tickdelta; 20117356Skarels } 20217356Skarels BUMPTIME(&time, delta); 20317356Skarels } 20416525Skarels if (needsoft) { 20547546Skarels if (CLKF_BASEPRI(&frame)) { 20616525Skarels /* 20716525Skarels * Save the overhead of a software interrupt; 20816525Skarels * it will happen as soon as we return, so do it now. 20916525Skarels */ 21016525Skarels (void) splsoftclock(); 21144774Swilliam softclock(frame); 21216525Skarels } else 21316525Skarels setsoftclock(); 21416525Skarels } 2152442Swnj } 2162442Swnj 21715191Ssam int dk_ndrive = DK_NDRIVE; 2188124Sroot /* 21911392Ssam * Gather statistics on resource utilization. 22011392Ssam * 22111392Ssam * We make a gross assumption: that the system has been in the 22211392Ssam * state it is in (user state, kernel state, interrupt state, 22311392Ssam * or idle state) for the entire last time interval, and 22411392Ssam * update statistics accordingly. 22511392Ssam */ 22647546Skarels gatherstats(framep) 22747546Skarels clockframe *framep; 22811392Ssam { 22926265Skarels register int cpstate, s; 23011392Ssam 23111392Ssam /* 23211392Ssam * Determine what state the cpu is in. 23311392Ssam */ 23447546Skarels if (CLKF_USERMODE(framep)) { 23511392Ssam /* 23611392Ssam * CPU was in user state. 23711392Ssam */ 23847546Skarels if (curproc->p_nice > NZERO) 23911392Ssam cpstate = CP_NICE; 24011392Ssam else 24111392Ssam cpstate = CP_USER; 24211392Ssam } else { 24311392Ssam /* 24411392Ssam * CPU was in system state. If profiling kernel 24524524Sbloom * increment a counter. If no process is running 24624524Sbloom * then this is a system tick if we were running 24724524Sbloom * at a non-zero IPL (in a driver). If a process is running, 24824524Sbloom * then we charge it with system time even if we were 24924524Sbloom * at a non-zero IPL, since the system often runs 25024524Sbloom * this way during processing of system calls. 25124524Sbloom * This is approximate, but the lack of true interval 25224524Sbloom * timers makes doing anything else difficult. 25311392Ssam */ 25411392Ssam cpstate = CP_SYS; 25548979Skarels if (curproc == NULL && CLKF_BASEPRI(framep)) 25611392Ssam cpstate = CP_IDLE; 25711392Ssam #ifdef GPROF 25847546Skarels s = CLKF_PC(framep) - s_lowpc; 25911392Ssam if (profiling < 2 && s < s_textsize) 26011392Ssam kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 26111392Ssam #endif 26211392Ssam } 26311392Ssam /* 26411392Ssam * We maintain statistics shown by user-level statistics 26511392Ssam * programs: the amount of time in each cpu state, and 26611392Ssam * the amount of time each of DK_NDRIVE ``drives'' is busy. 26711392Ssam */ 26811392Ssam cp_time[cpstate]++; 26911392Ssam for (s = 0; s < DK_NDRIVE; s++) 27029946Skarels if (dk_busy&(1<<s)) 27111392Ssam dk_time[s]++; 27211392Ssam } 27311392Ssam 27411392Ssam /* 2758124Sroot * Software priority level clock interrupt. 2768124Sroot * Run periodic events from timeout queue. 2778124Sroot */ 2782609Swnj /*ARGSUSED*/ 27944774Swilliam softclock(frame) 28047546Skarels clockframe frame; 2812442Swnj { 2822442Swnj 2838097Sroot for (;;) { 2848124Sroot register struct callout *p1; 2858124Sroot register caddr_t arg; 2868124Sroot register int (*func)(); 2878124Sroot register int a, s; 2888124Sroot 28926265Skarels s = splhigh(); 2908097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 2918097Sroot splx(s); 2928097Sroot break; 2932442Swnj } 2948124Sroot arg = p1->c_arg; func = p1->c_func; a = p1->c_time; 2958097Sroot calltodo.c_next = p1->c_next; 2968097Sroot p1->c_next = callfree; 2978097Sroot callfree = p1; 2989157Ssam splx(s); 2998112Sroot (*func)(arg, a); 3002442Swnj } 3019604Ssam /* 30213127Ssam * If trapped user-mode and profiling, give it 30313127Ssam * a profiling tick. 3049604Ssam */ 30547546Skarels if (CLKF_USERMODE(&frame)) { 30647546Skarels register struct proc *p = curproc; 30713127Ssam 30847546Skarels if (p->p_stats->p_prof.pr_scale) 30947546Skarels profile_tick(p, &frame); 31013127Ssam /* 31113127Ssam * Check to see if process has accumulated 31213127Ssam * more than 10 minutes of user time. If so 31313127Ssam * reduce priority to give others a chance. 31413127Ssam */ 31547546Skarels if (p->p_ucred->cr_uid && p->p_nice == NZERO && 31640674Smarc p->p_utime.tv_sec > 10 * 60) { 31747546Skarels p->p_nice = NZERO + 4; 31847546Skarels setpri(p); 31913127Ssam p->p_pri = p->p_usrpri; 32013127Ssam } 3219604Ssam } 3229Sbill } 3239Sbill 3249Sbill /* 32547546Skarels * Arrange that (*func)(arg) is called in t/hz seconds. 32612747Ssam */ 32747546Skarels timeout(func, arg, t) 32847546Skarels int (*func)(); 3292450Swnj caddr_t arg; 33012747Ssam register int t; 3319Sbill { 3323542Swnj register struct callout *p1, *p2, *pnew; 33326265Skarels register int s = splhigh(); 3349Sbill 33518282Smckusick if (t <= 0) 33612747Ssam t = 1; 3373542Swnj pnew = callfree; 3383542Swnj if (pnew == NULL) 3393542Swnj panic("timeout table overflow"); 3403542Swnj callfree = pnew->c_next; 3413542Swnj pnew->c_arg = arg; 34247546Skarels pnew->c_func = func; 3433542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 3449742Ssam if (p2->c_time > 0) 3459742Ssam t -= p2->c_time; 3463542Swnj p1->c_next = pnew; 3473542Swnj pnew->c_next = p2; 3483542Swnj pnew->c_time = t; 3493542Swnj if (p2) 3503542Swnj p2->c_time -= t; 3519Sbill splx(s); 3529Sbill } 3537305Ssam 3547305Ssam /* 3557305Ssam * untimeout is called to remove a function timeout call 3567305Ssam * from the callout structure. 3577305Ssam */ 35847546Skarels untimeout(func, arg) 35947546Skarels int (*func)(); 3607305Ssam caddr_t arg; 3617305Ssam { 3627305Ssam register struct callout *p1, *p2; 3637305Ssam register int s; 3647305Ssam 36526265Skarels s = splhigh(); 3667305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 36747546Skarels if (p2->c_func == func && p2->c_arg == arg) { 3688112Sroot if (p2->c_next && p2->c_time > 0) 3697305Ssam p2->c_next->c_time += p2->c_time; 3707305Ssam p1->c_next = p2->c_next; 3717305Ssam p2->c_next = callfree; 3727305Ssam callfree = p2; 3737305Ssam break; 3747305Ssam } 3757305Ssam } 3767305Ssam splx(s); 3777305Ssam } 3788112Sroot 3798124Sroot /* 3808124Sroot * Compute number of hz until specified time. 3818124Sroot * Used to compute third argument to timeout() from an 3828124Sroot * absolute time. 3838124Sroot */ 3848112Sroot hzto(tv) 3858112Sroot struct timeval *tv; 3868112Sroot { 3878124Sroot register long ticks; 3888124Sroot register long sec; 38926265Skarels int s = splhigh(); 3908112Sroot 3918124Sroot /* 3928124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 3938124Sroot * then compute number of milliseconds to time and scale to 3948124Sroot * ticks. Otherwise just compute number of hz in time, rounding 3958124Sroot * times greater than representible to maximum value. 3968124Sroot * 3978124Sroot * Delta times less than 25 days can be computed ``exactly''. 3988124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 3998124Sroot */ 4008124Sroot sec = tv->tv_sec - time.tv_sec; 4018124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 4028124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 4038124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 4048124Sroot else if (sec <= 0x7fffffff / hz) 4058124Sroot ticks = sec * hz; 4068124Sroot else 4078124Sroot ticks = 0x7fffffff; 4088112Sroot splx(s); 4098112Sroot return (ticks); 4108112Sroot } 41152668Smckusick 41252668Smckusick /* 41352668Smckusick * Return information about system clocks. 41452668Smckusick */ 41552668Smckusick /* ARGSUSED */ 41652668Smckusick kinfo_clockrate(op, where, acopysize, arg, aneeded) 41752668Smckusick int op; 41852668Smckusick register char *where; 41952668Smckusick int *acopysize, arg, *aneeded; 42052668Smckusick { 42152668Smckusick int buflen, error; 42252668Smckusick struct clockinfo clockinfo; 42352668Smckusick 42452668Smckusick *aneeded = sizeof(clockinfo); 42552668Smckusick if (where == NULL) 42652668Smckusick return (0); 42752668Smckusick /* 42852668Smckusick * Check for enough buffering. 42952668Smckusick */ 43052668Smckusick buflen = *acopysize; 43152668Smckusick if (buflen < sizeof(clockinfo)) { 43252668Smckusick *acopysize = 0; 43352668Smckusick return (0); 43452668Smckusick } 43552668Smckusick /* 43652668Smckusick * Copyout clockinfo structure. 43752668Smckusick */ 43852668Smckusick clockinfo.hz = hz; 43952668Smckusick clockinfo.phz = phz; 44052668Smckusick clockinfo.tick = tick; 44152668Smckusick clockinfo.profhz = profhz; 44252668Smckusick if (error = copyout((caddr_t)&clockinfo, where, sizeof(clockinfo))) 44352668Smckusick return (error); 44452668Smckusick *acopysize = sizeof(clockinfo); 44552668Smckusick return (0); 44652668Smckusick } 447