149594Sbostic /*- 2*63170Sbostic * Copyright (c) 1982, 1986, 1991, 1993 3*63170Sbostic * The Regents of the University of California. All rights reserved. 423366Smckusick * 549594Sbostic * %sccs.include.redist.c% 649594Sbostic * 7*63170Sbostic * @(#)kern_clock.c 8.1 (Berkeley) 06/10/93 823366Smckusick */ 99Sbill 1056517Sbostic #include <sys/param.h> 1156517Sbostic #include <sys/systm.h> 1256517Sbostic #include <sys/dkstat.h> 1356517Sbostic #include <sys/callout.h> 1456517Sbostic #include <sys/kernel.h> 1556517Sbostic #include <sys/proc.h> 1656517Sbostic #include <sys/resourcevar.h> 179Sbill 1856517Sbostic #include <machine/cpu.h> 1935406Skarels 2010291Smckusick #ifdef GPROF 2156517Sbostic #include <sys/gmon.h> 2210291Smckusick #endif 2310291Smckusick 248124Sroot /* 258124Sroot * Clock handling routines. 268124Sroot * 2754791Storek * This code is written to operate with two timers that run independently of 2854791Storek * each other. The main clock, running hz times per second, is used to keep 2954791Storek * track of real time. The second timer handles kernel and user profiling, 3054791Storek * and does resource use estimation. If the second timer is programmable, 3154791Storek * it is randomized to avoid aliasing between the two clocks. For example, 3254791Storek * the randomization prevents an adversary from always giving up the cpu 3354791Storek * just before its quantum expires. Otherwise, it would never accumulate 3454791Storek * cpu ticks. The mean frequency of the second timer is stathz. 3554791Storek * 3654791Storek * If no second timer exists, stathz will be zero; in this case we drive 3754791Storek * profiling and statistics off the main clock. This WILL NOT be accurate; 3854791Storek * do not do it unless absolutely necessary. 3954791Storek * 4054791Storek * The statistics clock may (or may not) be run at a higher rate while 4154791Storek * profiling. This profile clock runs at profhz. We require that profhz 4254791Storek * be an integral multiple of stathz. 4354791Storek * 4454791Storek * If the statistics clock is running fast, it must be divided by the ratio 4554791Storek * profhz/stathz for statistics. (For profiling, every tick counts.) 468124Sroot */ 471559Sbill 488124Sroot /* 498124Sroot * TODO: 5012747Ssam * allocate more timeout table slots when table overflows. 518124Sroot */ 5226265Skarels 5317007Smckusick /* 5417007Smckusick * Bump a timeval by a small number of usec's. 5517007Smckusick */ 5617007Smckusick #define BUMPTIME(t, usec) { \ 5754791Storek register volatile struct timeval *tp = (t); \ 5854791Storek register long us; \ 5917007Smckusick \ 6054791Storek tp->tv_usec = us = tp->tv_usec + (usec); \ 6154791Storek if (us >= 1000000) { \ 6254791Storek tp->tv_usec = us - 1000000; \ 6317007Smckusick tp->tv_sec++; \ 6417007Smckusick } \ 6517007Smckusick } 6617007Smckusick 6754124Smckusick int stathz; 6853011Ssklower int profhz; 6954138Smckusick int profprocs; 7056338Ssklower int ticks; 7156317Shibler static int psdiv, pscnt; /* prof => stat divider */ 7256855Storek int psratio; /* ratio: prof / stat */ 7354791Storek 7454791Storek volatile struct timeval time; 7554791Storek volatile struct timeval mono_time; 7654791Storek 778124Sroot /* 7854791Storek * Initialize clock frequencies and start both clocks running. 798124Sroot */ 8054791Storek void 8154791Storek initclocks() 8254791Storek { 8354791Storek register int i; 8454791Storek 8554791Storek /* 8654791Storek * Set divisors to 1 (normal case) and let the machine-specific 8754791Storek * code do its bit. 8854791Storek */ 8954791Storek psdiv = pscnt = 1; 9054791Storek cpu_initclocks(); 9154791Storek 9254791Storek /* 9354791Storek * Compute profhz/stathz, and fix profhz if needed. 9454791Storek */ 9554791Storek i = stathz ? stathz : hz; 9654791Storek if (profhz == 0) 9754791Storek profhz = i; 9854791Storek psratio = profhz / i; 9954791Storek } 10054791Storek 10154791Storek /* 10254791Storek * The real-time timer, interrupting hz times per second. 10354791Storek */ 10454791Storek void 10544774Swilliam hardclock(frame) 10654791Storek register struct clockframe *frame; 1079Sbill { 1082768Swnj register struct callout *p1; 10954791Storek register struct proc *p; 11055294Storek register int delta, needsoft; 11128947Skarels extern int tickdelta; 11228947Skarels extern long timedelta; 1139Sbill 1148124Sroot /* 1158124Sroot * Update real-time timeout queue. 1168124Sroot * At front of queue are some number of events which are ``due''. 1178124Sroot * The time to these is <= 0 and if negative represents the 1188124Sroot * number of ticks which have passed since it was supposed to happen. 1198124Sroot * The rest of the q elements (times > 0) are events yet to happen, 1208124Sroot * where the time for each is given as a delta from the previous. 1218124Sroot * Decrementing just the first of these serves to decrement the time 1228124Sroot * to all events. 1238124Sroot */ 12454791Storek needsoft = 0; 12554791Storek for (p1 = calltodo.c_next; p1 != NULL; p1 = p1->c_next) { 12612747Ssam if (--p1->c_time > 0) 12712747Ssam break; 12816172Skarels needsoft = 1; 12912747Ssam if (p1->c_time == 0) 13012747Ssam break; 13112747Ssam } 132138Sbill 13354791Storek p = curproc; 13454791Storek if (p) { 13554791Storek register struct pstats *pstats; 13654791Storek 1378124Sroot /* 13854791Storek * Run current process's virtual and profile time, as needed. 1398124Sroot */ 14054791Storek pstats = p->p_stats; 14154791Storek if (CLKF_USERMODE(frame) && 14254791Storek timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) && 14347546Skarels itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 14440674Smarc psignal(p, SIGVTALRM); 14547546Skarels if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) && 14647546Skarels itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 14740674Smarc psignal(p, SIGPROF); 1489Sbill } 1498124Sroot 1508124Sroot /* 15154791Storek * If no separate statistics clock is available, run it from here. 15211392Ssam */ 15354124Smckusick if (stathz == 0) 15454791Storek statclock(frame); 15511392Ssam 15611392Ssam /* 15755294Storek * Increment the time-of-day. The increment is just ``tick'' unless 15855294Storek * we are still adjusting the clock; see adjtime(). 1598124Sroot */ 16056338Ssklower ticks++; 16155294Storek if (timedelta == 0) 16255294Storek delta = tick; 16355294Storek else { 16455294Storek delta = tick + tickdelta; 16555294Storek timedelta -= tickdelta; 16617356Skarels } 16755294Storek BUMPTIME(&time, delta); 16855294Storek BUMPTIME(&mono_time, delta); 16954791Storek 17054791Storek /* 17154791Storek * Process callouts at a very low cpu priority, so we don't keep the 17254791Storek * relatively high clock interrupt priority any longer than necessary. 17354791Storek */ 17416525Skarels if (needsoft) { 17554791Storek if (CLKF_BASEPRI(frame)) { 17616525Skarels /* 17716525Skarels * Save the overhead of a software interrupt; 17816525Skarels * it will happen as soon as we return, so do it now. 17916525Skarels */ 18054791Storek (void)splsoftclock(); 18154791Storek softclock(); 18216525Skarels } else 18316525Skarels setsoftclock(); 18416525Skarels } 1852442Swnj } 1862442Swnj 1878124Sroot /* 18854791Storek * Software (low priority) clock interrupt. 1898124Sroot * Run periodic events from timeout queue. 1908124Sroot */ 1912609Swnj /*ARGSUSED*/ 19254791Storek void 19354791Storek softclock() 1942442Swnj { 19554791Storek register struct callout *c; 19654791Storek register void *arg; 19754791Storek register void (*func) __P((void *)); 19854791Storek register int s; 1992442Swnj 20054791Storek s = splhigh(); 20154791Storek while ((c = calltodo.c_next) != NULL && c->c_time <= 0) { 20254791Storek func = c->c_func; 20354791Storek arg = c->c_arg; 20454791Storek calltodo.c_next = c->c_next; 20554791Storek c->c_next = callfree; 20654791Storek callfree = c; 2079157Ssam splx(s); 20854791Storek (*func)(arg); 20954791Storek (void) splhigh(); 2102442Swnj } 21154791Storek splx(s); 2129Sbill } 2139Sbill 2149Sbill /* 21547546Skarels * Arrange that (*func)(arg) is called in t/hz seconds. 21612747Ssam */ 21754791Storek void 21847546Skarels timeout(func, arg, t) 21954791Storek void (*func) __P((void *)); 22054791Storek void *arg; 22112747Ssam register int t; 2229Sbill { 2233542Swnj register struct callout *p1, *p2, *pnew; 22454791Storek register int s; 2259Sbill 22654791Storek s = splhigh(); 22718282Smckusick if (t <= 0) 22812747Ssam t = 1; 2293542Swnj pnew = callfree; 2303542Swnj if (pnew == NULL) 2313542Swnj panic("timeout table overflow"); 2323542Swnj callfree = pnew->c_next; 2333542Swnj pnew->c_arg = arg; 23447546Skarels pnew->c_func = func; 2353542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 2369742Ssam if (p2->c_time > 0) 2379742Ssam t -= p2->c_time; 2383542Swnj p1->c_next = pnew; 2393542Swnj pnew->c_next = p2; 2403542Swnj pnew->c_time = t; 2413542Swnj if (p2) 2423542Swnj p2->c_time -= t; 2439Sbill splx(s); 2449Sbill } 2457305Ssam 2467305Ssam /* 2477305Ssam * untimeout is called to remove a function timeout call 2487305Ssam * from the callout structure. 2497305Ssam */ 25054791Storek void 25147546Skarels untimeout(func, arg) 25254791Storek void (*func) __P((void *)); 25354791Storek void *arg; 2547305Ssam { 2557305Ssam register struct callout *p1, *p2; 2567305Ssam register int s; 2577305Ssam 25826265Skarels s = splhigh(); 25954791Storek for (p1 = &calltodo; (p2 = p1->c_next) != NULL; p1 = p2) { 26047546Skarels if (p2->c_func == func && p2->c_arg == arg) { 2618112Sroot if (p2->c_next && p2->c_time > 0) 2627305Ssam p2->c_next->c_time += p2->c_time; 2637305Ssam p1->c_next = p2->c_next; 2647305Ssam p2->c_next = callfree; 2657305Ssam callfree = p2; 2667305Ssam break; 2677305Ssam } 2687305Ssam } 2697305Ssam splx(s); 2707305Ssam } 2718112Sroot 2728124Sroot /* 2738124Sroot * Compute number of hz until specified time. 2748124Sroot * Used to compute third argument to timeout() from an 2758124Sroot * absolute time. 2768124Sroot */ 27754791Storek int 2788112Sroot hzto(tv) 2798112Sroot struct timeval *tv; 2808112Sroot { 28154791Storek register long ticks, sec; 28254791Storek int s; 2838112Sroot 2848124Sroot /* 2858124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 2868124Sroot * then compute number of milliseconds to time and scale to 2878124Sroot * ticks. Otherwise just compute number of hz in time, rounding 2888124Sroot * times greater than representible to maximum value. 2898124Sroot * 2908124Sroot * Delta times less than 25 days can be computed ``exactly''. 2918124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 2928124Sroot */ 29354791Storek s = splhigh(); 2948124Sroot sec = tv->tv_sec - time.tv_sec; 2958124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 2968124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 2978124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 2988124Sroot else if (sec <= 0x7fffffff / hz) 2998124Sroot ticks = sec * hz; 3008124Sroot else 3018124Sroot ticks = 0x7fffffff; 3028112Sroot splx(s); 3038112Sroot return (ticks); 3048112Sroot } 30552668Smckusick 30652668Smckusick /* 30754791Storek * Start profiling on a process. 30854791Storek * 30954791Storek * Kernel profiling passes proc0 which never exits and hence 31054791Storek * keeps the profile clock running constantly. 31154791Storek */ 31254791Storek void 31354791Storek startprofclock(p) 31454791Storek register struct proc *p; 31554791Storek { 31654791Storek int s; 31754791Storek 31854791Storek if ((p->p_flag & SPROFIL) == 0) { 31954791Storek p->p_flag |= SPROFIL; 32054791Storek if (++profprocs == 1 && stathz != 0) { 32154791Storek s = splstatclock(); 32254791Storek psdiv = pscnt = psratio; 32354791Storek setstatclockrate(profhz); 32454791Storek splx(s); 32554791Storek } 32654791Storek } 32754791Storek } 32854791Storek 32954791Storek /* 33054791Storek * Stop profiling on a process. 33154791Storek */ 33254791Storek void 33354791Storek stopprofclock(p) 33454791Storek register struct proc *p; 33554791Storek { 33654791Storek int s; 33754791Storek 33854791Storek if (p->p_flag & SPROFIL) { 33954791Storek p->p_flag &= ~SPROFIL; 34054791Storek if (--profprocs == 0 && stathz != 0) { 34154791Storek s = splstatclock(); 34254791Storek psdiv = pscnt = 1; 34354791Storek setstatclockrate(stathz); 34454791Storek splx(s); 34554791Storek } 34654791Storek } 34754791Storek } 34854791Storek 34954791Storek int dk_ndrive = DK_NDRIVE; 35054791Storek 35154791Storek /* 35254791Storek * Statistics clock. Grab profile sample, and if divider reaches 0, 35354791Storek * do process and kernel statistics. 35454791Storek */ 35554791Storek void 35654791Storek statclock(frame) 35754791Storek register struct clockframe *frame; 35854791Storek { 35954791Storek #ifdef GPROF 36054791Storek register struct gmonparam *g; 36154791Storek #endif 36254791Storek register struct proc *p; 36354791Storek register int i; 36454791Storek 36554791Storek if (CLKF_USERMODE(frame)) { 36654791Storek p = curproc; 36754791Storek if (p->p_flag & SPROFIL) 36854791Storek addupc_intr(p, CLKF_PC(frame), 1); 36954791Storek if (--pscnt > 0) 37054791Storek return; 37154791Storek /* 37254791Storek * Came from user mode; CPU was in user state. 37354791Storek * If this process is being profiled record the tick. 37454791Storek */ 37554791Storek p->p_uticks++; 37654791Storek if (p->p_nice > NZERO) 37754791Storek cp_time[CP_NICE]++; 37854791Storek else 37954791Storek cp_time[CP_USER]++; 38054791Storek } else { 38154791Storek #ifdef GPROF 38254791Storek /* 38354791Storek * Kernel statistics are just like addupc_intr, only easier. 38454791Storek */ 38554791Storek g = &_gmonparam; 38654791Storek if (g->state == GMON_PROF_ON) { 38754791Storek i = CLKF_PC(frame) - g->lowpc; 38859204Smckusick if (i < g->textsize) { 38959204Smckusick i /= HISTFRACTION * sizeof(*g->kcount); 39059204Smckusick g->kcount[i]++; 39159204Smckusick } 39254791Storek } 39354791Storek #endif 39454791Storek if (--pscnt > 0) 39554791Storek return; 39654791Storek /* 39754791Storek * Came from kernel mode, so we were: 39854791Storek * - handling an interrupt, 39954791Storek * - doing syscall or trap work on behalf of the current 40054791Storek * user process, or 40154791Storek * - spinning in the idle loop. 40254791Storek * Whichever it is, charge the time as appropriate. 40354791Storek * Note that we charge interrupts to the current process, 40454791Storek * regardless of whether they are ``for'' that process, 40554791Storek * so that we know how much of its real time was spent 40654791Storek * in ``non-process'' (i.e., interrupt) work. 40754791Storek */ 40854791Storek p = curproc; 40954791Storek if (CLKF_INTR(frame)) { 41054791Storek if (p != NULL) 41154791Storek p->p_iticks++; 41254791Storek cp_time[CP_INTR]++; 41354791Storek } else if (p != NULL) { 41454791Storek p->p_sticks++; 41554791Storek cp_time[CP_SYS]++; 41654791Storek } else 41754791Storek cp_time[CP_IDLE]++; 41854791Storek } 41954791Storek pscnt = psdiv; 42054791Storek 42154791Storek /* 42254791Storek * We maintain statistics shown by user-level statistics 42354791Storek * programs: the amount of time in each cpu state, and 42454791Storek * the amount of time each of DK_NDRIVE ``drives'' is busy. 42554791Storek * 42654791Storek * XXX should either run linked list of drives, or (better) 42754791Storek * grab timestamps in the start & done code. 42854791Storek */ 42954791Storek for (i = 0; i < DK_NDRIVE; i++) 43054791Storek if (dk_busy & (1 << i)) 43154791Storek dk_time[i]++; 43254791Storek 43354791Storek /* 43454791Storek * We adjust the priority of the current process. 43554791Storek * The priority of a process gets worse as it accumulates 43654791Storek * CPU time. The cpu usage estimator (p_cpu) is increased here 43754791Storek * and the formula for computing priorities (in kern_synch.c) 43854791Storek * will compute a different value each time the p_cpu increases 43954791Storek * by 4. The cpu usage estimator ramps up quite quickly when 44054791Storek * the process is running (linearly), and decays away 44154791Storek * exponentially, at a rate which is proportionally slower 44254791Storek * when the system is busy. The basic principal is that the 44354791Storek * system will 90% forget that a process used a lot of CPU 44454791Storek * time in 5*loadav seconds. This causes the system to favor 44554791Storek * processes which haven't run much recently, and to 44654791Storek * round-robin among other processes. 44754791Storek */ 44854791Storek if (p != NULL) { 44954791Storek p->p_cpticks++; 45054791Storek if (++p->p_cpu == 0) 45154791Storek p->p_cpu--; 45254791Storek if ((p->p_cpu & 3) == 0) { 45354791Storek setpri(p); 45454791Storek if (p->p_pri >= PUSER) 45554791Storek p->p_pri = p->p_usrpri; 45654791Storek } 45754791Storek } 45854791Storek } 45954791Storek 46054791Storek /* 46152668Smckusick * Return information about system clocks. 46252668Smckusick */ 46357840Smckusick sysctl_clockrate(where, sizep) 46452668Smckusick register char *where; 46558464Sbostic size_t *sizep; 46652668Smckusick { 46757840Smckusick struct clockinfo clkinfo; 46852668Smckusick 46952668Smckusick /* 47057840Smckusick * Construct clockinfo structure. 47152668Smckusick */ 47257840Smckusick clkinfo.hz = hz; 47357840Smckusick clkinfo.tick = tick; 47457840Smckusick clkinfo.profhz = profhz; 47557840Smckusick clkinfo.stathz = stathz ? stathz : hz; 47657840Smckusick return (sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo))); 47752668Smckusick } 478