123366Smckusick /* 229086Smckusick * Copyright (c) 1982, 1986 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*43402Smckusick * @(#)kern_clock.c 7.8 (Berkeley) 06/21/90 723366Smckusick */ 89Sbill 917088Sbloom #include "param.h" 1017088Sbloom #include "systm.h" 1129946Skarels #include "dkstat.h" 1217088Sbloom #include "callout.h" 13*43402Smckusick #include "syscontext.h" 1417088Sbloom #include "kernel.h" 1517088Sbloom #include "proc.h" 1617088Sbloom #include "vm.h" 1717088Sbloom #include "text.h" 189Sbill 1937493Smckusick #include "machine/reg.h" 2037493Smckusick #include "machine/psl.h" 2135406Skarels 2229946Skarels #if defined(vax) || defined(tahoe) 2337493Smckusick #include "machine/mtpr.h" 2437493Smckusick #include "machine/clock.h" 259751Ssam #endif 2641566Smckusick #if defined(hp300) 2741566Smckusick #include "machine/mtpr.h" 2841566Smckusick #endif 299751Ssam 3010291Smckusick #ifdef GPROF 3117088Sbloom #include "gprof.h" 3210291Smckusick #endif 3310291Smckusick 348124Sroot /* 358124Sroot * Clock handling routines. 368124Sroot * 3711392Ssam * This code is written to operate with two timers which run 3811392Ssam * independently of each other. The main clock, running at hz 3911392Ssam * times per second, is used to do scheduling and timeout calculations. 4011392Ssam * The second timer does resource utilization estimation statistically 4111392Ssam * based on the state of the machine phz times a second. Both functions 4211392Ssam * can be performed by a single clock (ie hz == phz), however the 4311392Ssam * statistics will be much more prone to errors. Ideally a machine 4411392Ssam * would have separate clocks measuring time spent in user state, system 4511392Ssam * state, interrupt state, and idle state. These clocks would allow a non- 4611392Ssam * approximate measure of resource utilization. 478124Sroot */ 481559Sbill 498124Sroot /* 508124Sroot * TODO: 5112747Ssam * time of day, system/user timing, timeouts, profiling on separate timers 5212747Ssam * allocate more timeout table slots when table overflows. 538124Sroot */ 5426265Skarels 5517007Smckusick /* 5617007Smckusick * Bump a timeval by a small number of usec's. 5717007Smckusick */ 5817007Smckusick #define BUMPTIME(t, usec) { \ 5917007Smckusick register struct timeval *tp = (t); \ 6017007Smckusick \ 6117007Smckusick tp->tv_usec += (usec); \ 6217007Smckusick if (tp->tv_usec >= 1000000) { \ 6317007Smckusick tp->tv_usec -= 1000000; \ 6417007Smckusick tp->tv_sec++; \ 6517007Smckusick } \ 6617007Smckusick } 6717007Smckusick 688124Sroot /* 6911392Ssam * The hz hardware interval timer. 7011392Ssam * We update the events relating to real time. 7111392Ssam * If this timer is also being used to gather statistics, 7211392Ssam * we run through the statistics gathering routine as well. 738124Sroot */ 742609Swnj /*ARGSUSED*/ 752442Swnj hardclock(pc, ps) 762450Swnj caddr_t pc; 778944Sroot int ps; 789Sbill { 792768Swnj register struct callout *p1; 8040674Smarc register struct proc *p = u.u_procp; 8124524Sbloom register int s; 8216172Skarels int needsoft = 0; 8328947Skarels extern int tickdelta; 8428947Skarels extern long timedelta; 859Sbill 868124Sroot /* 878124Sroot * Update real-time timeout queue. 888124Sroot * At front of queue are some number of events which are ``due''. 898124Sroot * The time to these is <= 0 and if negative represents the 908124Sroot * number of ticks which have passed since it was supposed to happen. 918124Sroot * The rest of the q elements (times > 0) are events yet to happen, 928124Sroot * where the time for each is given as a delta from the previous. 938124Sroot * Decrementing just the first of these serves to decrement the time 948124Sroot * to all events. 958124Sroot */ 9612747Ssam p1 = calltodo.c_next; 9712747Ssam while (p1) { 9812747Ssam if (--p1->c_time > 0) 9912747Ssam break; 10016172Skarels needsoft = 1; 10112747Ssam if (p1->c_time == 0) 10212747Ssam break; 10312747Ssam p1 = p1->c_next; 10412747Ssam } 105138Sbill 1068124Sroot /* 1078124Sroot * Charge the time out based on the mode the cpu is in. 1088124Sroot * Here again we fudge for the lack of proper interval timers 1098124Sroot * assuming that the current state has been around at least 1108124Sroot * one tick. 1118124Sroot */ 1129Sbill if (USERMODE(ps)) { 11316172Skarels if (u.u_prof.pr_scale) 11416172Skarels needsoft = 1; 1158124Sroot /* 1168124Sroot * CPU was in user state. Increment 1178124Sroot * user time counter, and process process-virtual time 1189604Ssam * interval timer. 1198124Sroot */ 12040674Smarc BUMPTIME(&p->p_utime, tick); 1218097Sroot if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && 1228097Sroot itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) 12340674Smarc psignal(p, SIGVTALRM); 1249Sbill } else { 1258124Sroot /* 12624524Sbloom * CPU was in system state. 1278124Sroot */ 12826265Skarels if (!noproc) 12940674Smarc BUMPTIME(&p->p_stime, tick); 1309Sbill } 1318097Sroot 1328124Sroot /* 13310388Ssam * If the cpu is currently scheduled to a process, then 13410388Ssam * charge it with resource utilization for a tick, updating 13510388Ssam * statistics which run in (user+system) virtual time, 13610388Ssam * such as the cpu time limit and profiling timers. 13710388Ssam * This assumes that the current process has been running 13810388Ssam * the entire last tick. 13910388Ssam */ 14018585Skarels if (noproc == 0) { 14140674Smarc if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) > 14210388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur) { 14340674Smarc psignal(p, SIGXCPU); 14410388Ssam if (u.u_rlimit[RLIMIT_CPU].rlim_cur < 14510388Ssam u.u_rlimit[RLIMIT_CPU].rlim_max) 14610388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; 14710388Ssam } 14810388Ssam if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && 14910388Ssam itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) 15040674Smarc psignal(p, SIGPROF); 15140674Smarc s = p->p_rssize; 15226265Skarels u.u_ru.ru_idrss += s; 15326265Skarels #ifdef notdef 15426265Skarels u.u_ru.ru_isrss += 0; /* XXX (haven't got this) */ 15526265Skarels #endif 15640674Smarc if (p->p_textp) { 15740674Smarc register int xrss = p->p_textp->x_rssize; 15810388Ssam 15910388Ssam s += xrss; 16010388Ssam u.u_ru.ru_ixrss += xrss; 16110388Ssam } 16210388Ssam if (s > u.u_ru.ru_maxrss) 16310388Ssam u.u_ru.ru_maxrss = s; 16410388Ssam } 16510388Ssam 16610388Ssam /* 1678124Sroot * We adjust the priority of the current process. 1688124Sroot * The priority of a process gets worse as it accumulates 1698124Sroot * CPU time. The cpu usage estimator (p_cpu) is increased here 1708124Sroot * and the formula for computing priorities (in kern_synch.c) 1718124Sroot * will compute a different value each time the p_cpu increases 1728124Sroot * by 4. The cpu usage estimator ramps up quite quickly when 1738124Sroot * the process is running (linearly), and decays away exponentially, 1748124Sroot * at a rate which is proportionally slower when the system is 1758124Sroot * busy. The basic principal is that the system will 90% forget 1768124Sroot * that a process used a lot of CPU time in 5*loadav seconds. 1778124Sroot * This causes the system to favor processes which haven't run 1788124Sroot * much recently, and to round-robin among other processes. 1798124Sroot */ 1809Sbill if (!noproc) { 1818097Sroot p->p_cpticks++; 1828097Sroot if (++p->p_cpu == 0) 1838097Sroot p->p_cpu--; 1848124Sroot if ((p->p_cpu&3) == 0) { 1858097Sroot (void) setpri(p); 1868097Sroot if (p->p_pri >= PUSER) 1878097Sroot p->p_pri = p->p_usrpri; 1889Sbill } 1899Sbill } 1908124Sroot 1918124Sroot /* 19211392Ssam * If the alternate clock has not made itself known then 19311392Ssam * we must gather the statistics. 19411392Ssam */ 19511392Ssam if (phz == 0) 19611392Ssam gatherstats(pc, ps); 19711392Ssam 19811392Ssam /* 1998124Sroot * Increment the time-of-day, and schedule 2008124Sroot * processing of the callouts at a very low cpu priority, 2018124Sroot * so we don't keep the relatively high clock interrupt 2028124Sroot * priority any longer than necessary. 2038124Sroot */ 20428828Skarels if (timedelta == 0) 20517356Skarels BUMPTIME(&time, tick) 20617356Skarels else { 20717356Skarels register delta; 20817356Skarels 20928828Skarels if (timedelta < 0) { 21028828Skarels delta = tick - tickdelta; 21128828Skarels timedelta += tickdelta; 21217356Skarels } else { 21328828Skarels delta = tick + tickdelta; 21428828Skarels timedelta -= tickdelta; 21517356Skarels } 21617356Skarels BUMPTIME(&time, delta); 21717356Skarels } 21816525Skarels if (needsoft) { 21916525Skarels if (BASEPRI(ps)) { 22016525Skarels /* 22116525Skarels * Save the overhead of a software interrupt; 22216525Skarels * it will happen as soon as we return, so do it now. 22316525Skarels */ 22416525Skarels (void) splsoftclock(); 22516525Skarels softclock(pc, ps); 22616525Skarels } else 22716525Skarels setsoftclock(); 22816525Skarels } 2292442Swnj } 2302442Swnj 23115191Ssam int dk_ndrive = DK_NDRIVE; 2328124Sroot /* 23311392Ssam * Gather statistics on resource utilization. 23411392Ssam * 23511392Ssam * We make a gross assumption: that the system has been in the 23611392Ssam * state it is in (user state, kernel state, interrupt state, 23711392Ssam * or idle state) for the entire last time interval, and 23811392Ssam * update statistics accordingly. 23911392Ssam */ 24012747Ssam /*ARGSUSED*/ 24111392Ssam gatherstats(pc, ps) 24211392Ssam caddr_t pc; 24311392Ssam int ps; 24411392Ssam { 24526265Skarels register int cpstate, s; 24611392Ssam 24711392Ssam /* 24811392Ssam * Determine what state the cpu is in. 24911392Ssam */ 25011392Ssam if (USERMODE(ps)) { 25111392Ssam /* 25211392Ssam * CPU was in user state. 25311392Ssam */ 25411392Ssam if (u.u_procp->p_nice > NZERO) 25511392Ssam cpstate = CP_NICE; 25611392Ssam else 25711392Ssam cpstate = CP_USER; 25811392Ssam } else { 25911392Ssam /* 26011392Ssam * CPU was in system state. If profiling kernel 26124524Sbloom * increment a counter. If no process is running 26224524Sbloom * then this is a system tick if we were running 26324524Sbloom * at a non-zero IPL (in a driver). If a process is running, 26424524Sbloom * then we charge it with system time even if we were 26524524Sbloom * at a non-zero IPL, since the system often runs 26624524Sbloom * this way during processing of system calls. 26724524Sbloom * This is approximate, but the lack of true interval 26824524Sbloom * timers makes doing anything else difficult. 26911392Ssam */ 27011392Ssam cpstate = CP_SYS; 27111392Ssam if (noproc && BASEPRI(ps)) 27211392Ssam cpstate = CP_IDLE; 27311392Ssam #ifdef GPROF 27411392Ssam s = pc - s_lowpc; 27511392Ssam if (profiling < 2 && s < s_textsize) 27611392Ssam kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 27711392Ssam #endif 27811392Ssam } 27911392Ssam /* 28011392Ssam * We maintain statistics shown by user-level statistics 28111392Ssam * programs: the amount of time in each cpu state, and 28211392Ssam * the amount of time each of DK_NDRIVE ``drives'' is busy. 28311392Ssam */ 28411392Ssam cp_time[cpstate]++; 28511392Ssam for (s = 0; s < DK_NDRIVE; s++) 28629946Skarels if (dk_busy&(1<<s)) 28711392Ssam dk_time[s]++; 28811392Ssam } 28911392Ssam 29011392Ssam /* 2918124Sroot * Software priority level clock interrupt. 2928124Sroot * Run periodic events from timeout queue. 2938124Sroot */ 2942609Swnj /*ARGSUSED*/ 2952442Swnj softclock(pc, ps) 2962450Swnj caddr_t pc; 2978944Sroot int ps; 2982442Swnj { 2992442Swnj 3008097Sroot for (;;) { 3018124Sroot register struct callout *p1; 3028124Sroot register caddr_t arg; 3038124Sroot register int (*func)(); 3048124Sroot register int a, s; 3058124Sroot 30626265Skarels s = splhigh(); 3078097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 3088097Sroot splx(s); 3098097Sroot break; 3102442Swnj } 3118124Sroot arg = p1->c_arg; func = p1->c_func; a = p1->c_time; 3128097Sroot calltodo.c_next = p1->c_next; 3138097Sroot p1->c_next = callfree; 3148097Sroot callfree = p1; 3159157Ssam splx(s); 3168112Sroot (*func)(arg, a); 3172442Swnj } 3189604Ssam /* 31913127Ssam * If trapped user-mode and profiling, give it 32013127Ssam * a profiling tick. 3219604Ssam */ 32213127Ssam if (USERMODE(ps)) { 32313127Ssam register struct proc *p = u.u_procp; 32413127Ssam 32513127Ssam if (u.u_prof.pr_scale) { 32613127Ssam p->p_flag |= SOWEUPC; 32713127Ssam aston(); 32813127Ssam } 32913127Ssam /* 33013127Ssam * Check to see if process has accumulated 33113127Ssam * more than 10 minutes of user time. If so 33213127Ssam * reduce priority to give others a chance. 33313127Ssam */ 33413127Ssam if (p->p_uid && p->p_nice == NZERO && 33540674Smarc p->p_utime.tv_sec > 10 * 60) { 33613127Ssam p->p_nice = NZERO+4; 33713127Ssam (void) setpri(p); 33813127Ssam p->p_pri = p->p_usrpri; 33913127Ssam } 3409604Ssam } 3419Sbill } 3429Sbill 3439Sbill /* 34412747Ssam * Arrange that (*fun)(arg) is called in t/hz seconds. 34512747Ssam */ 34612747Ssam timeout(fun, arg, t) 3472450Swnj int (*fun)(); 3482450Swnj caddr_t arg; 34912747Ssam register int t; 3509Sbill { 3513542Swnj register struct callout *p1, *p2, *pnew; 35226265Skarels register int s = splhigh(); 3539Sbill 35418282Smckusick if (t <= 0) 35512747Ssam t = 1; 3563542Swnj pnew = callfree; 3573542Swnj if (pnew == NULL) 3583542Swnj panic("timeout table overflow"); 3593542Swnj callfree = pnew->c_next; 3603542Swnj pnew->c_arg = arg; 3613542Swnj pnew->c_func = fun; 3623542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 3639742Ssam if (p2->c_time > 0) 3649742Ssam t -= p2->c_time; 3653542Swnj p1->c_next = pnew; 3663542Swnj pnew->c_next = p2; 3673542Swnj pnew->c_time = t; 3683542Swnj if (p2) 3693542Swnj p2->c_time -= t; 3709Sbill splx(s); 3719Sbill } 3727305Ssam 3737305Ssam /* 3747305Ssam * untimeout is called to remove a function timeout call 3757305Ssam * from the callout structure. 3767305Ssam */ 3778097Sroot untimeout(fun, arg) 3787305Ssam int (*fun)(); 3797305Ssam caddr_t arg; 3807305Ssam { 3817305Ssam register struct callout *p1, *p2; 3827305Ssam register int s; 3837305Ssam 38426265Skarels s = splhigh(); 3857305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 3867305Ssam if (p2->c_func == fun && p2->c_arg == arg) { 3878112Sroot if (p2->c_next && p2->c_time > 0) 3887305Ssam p2->c_next->c_time += p2->c_time; 3897305Ssam p1->c_next = p2->c_next; 3907305Ssam p2->c_next = callfree; 3917305Ssam callfree = p2; 3927305Ssam break; 3937305Ssam } 3947305Ssam } 3957305Ssam splx(s); 3967305Ssam } 3978112Sroot 3988124Sroot /* 3998124Sroot * Compute number of hz until specified time. 4008124Sroot * Used to compute third argument to timeout() from an 4018124Sroot * absolute time. 4028124Sroot */ 4038112Sroot hzto(tv) 4048112Sroot struct timeval *tv; 4058112Sroot { 4068124Sroot register long ticks; 4078124Sroot register long sec; 40826265Skarels int s = splhigh(); 4098112Sroot 4108124Sroot /* 4118124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 4128124Sroot * then compute number of milliseconds to time and scale to 4138124Sroot * ticks. Otherwise just compute number of hz in time, rounding 4148124Sroot * times greater than representible to maximum value. 4158124Sroot * 4168124Sroot * Delta times less than 25 days can be computed ``exactly''. 4178124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 4188124Sroot */ 4198124Sroot sec = tv->tv_sec - time.tv_sec; 4208124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 4218124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 4228124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 4238124Sroot else if (sec <= 0x7fffffff / hz) 4248124Sroot ticks = sec * hz; 4258124Sroot else 4268124Sroot ticks = 0x7fffffff; 4278112Sroot splx(s); 4288112Sroot return (ticks); 4298112Sroot } 43012747Ssam 431*43402Smckusick /* ARGSUSED */ 432*43402Smckusick profil(p, uap, retval) 433*43402Smckusick struct proc *p; 434*43402Smckusick register struct args { 43512747Ssam short *bufbase; 43612747Ssam unsigned bufsize; 43712747Ssam unsigned pcoffset; 43812747Ssam unsigned pcscale; 439*43402Smckusick } *uap; 440*43402Smckusick int *retval; 441*43402Smckusick { 44212747Ssam register struct uprof *upp = &u.u_prof; 44312747Ssam 44412747Ssam upp->pr_base = uap->bufbase; 44512747Ssam upp->pr_size = uap->bufsize; 44612747Ssam upp->pr_off = uap->pcoffset; 44712747Ssam upp->pr_scale = uap->pcscale; 448*43402Smckusick RETURN (0); 44912747Ssam } 450