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*28828Skarels * @(#)kern_clock.c 6.16 (Berkeley) 05/27/86 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 2326265Skarels #if defined(vax) 249751Ssam #include "../vax/mtpr.h" 2526265Skarels #include "../vax/clock.h" 269751Ssam #endif 279751Ssam 2810291Smckusick #ifdef GPROF 2917088Sbloom #include "gprof.h" 3010291Smckusick #endif 3110291Smckusick 328124Sroot /* 338124Sroot * Clock handling routines. 348124Sroot * 3511392Ssam * This code is written to operate with two timers which run 3611392Ssam * independently of each other. The main clock, running at hz 3711392Ssam * times per second, is used to do scheduling and timeout calculations. 3811392Ssam * The second timer does resource utilization estimation statistically 3911392Ssam * based on the state of the machine phz times a second. Both functions 4011392Ssam * can be performed by a single clock (ie hz == phz), however the 4111392Ssam * statistics will be much more prone to errors. Ideally a machine 4211392Ssam * would have separate clocks measuring time spent in user state, system 4311392Ssam * state, interrupt state, and idle state. These clocks would allow a non- 4411392Ssam * approximate measure of resource utilization. 458124Sroot */ 461559Sbill 478124Sroot /* 488124Sroot * TODO: 4912747Ssam * time of day, system/user timing, timeouts, profiling on separate timers 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) { \ 5717007Smckusick register struct timeval *tp = (t); \ 5817007Smckusick \ 5917007Smckusick tp->tv_usec += (usec); \ 6017007Smckusick if (tp->tv_usec >= 1000000) { \ 6117007Smckusick tp->tv_usec -= 1000000; \ 6217007Smckusick tp->tv_sec++; \ 6317007Smckusick } \ 6417007Smckusick } 6517007Smckusick 668124Sroot /* 6711392Ssam * The hz hardware interval timer. 6811392Ssam * We update the events relating to real time. 6911392Ssam * If this timer is also being used to gather statistics, 7011392Ssam * we run through the statistics gathering routine as well. 718124Sroot */ 722609Swnj /*ARGSUSED*/ 732442Swnj hardclock(pc, ps) 742450Swnj caddr_t pc; 758944Sroot int ps; 769Sbill { 772768Swnj register struct callout *p1; 788097Sroot register struct proc *p; 7924524Sbloom register int s; 8016172Skarels int needsoft = 0; 81*28828Skarels extern int timedelta, tickdelta; 829Sbill 838124Sroot /* 848124Sroot * Update real-time timeout queue. 858124Sroot * At front of queue are some number of events which are ``due''. 868124Sroot * The time to these is <= 0 and if negative represents the 878124Sroot * number of ticks which have passed since it was supposed to happen. 888124Sroot * The rest of the q elements (times > 0) are events yet to happen, 898124Sroot * where the time for each is given as a delta from the previous. 908124Sroot * Decrementing just the first of these serves to decrement the time 918124Sroot * to all events. 928124Sroot */ 9312747Ssam p1 = calltodo.c_next; 9412747Ssam while (p1) { 9512747Ssam if (--p1->c_time > 0) 9612747Ssam break; 9716172Skarels needsoft = 1; 9812747Ssam if (p1->c_time == 0) 9912747Ssam break; 10012747Ssam p1 = p1->c_next; 10112747Ssam } 102138Sbill 1038124Sroot /* 1048124Sroot * Charge the time out based on the mode the cpu is in. 1058124Sroot * Here again we fudge for the lack of proper interval timers 1068124Sroot * assuming that the current state has been around at least 1078124Sroot * one tick. 1088124Sroot */ 1099Sbill if (USERMODE(ps)) { 11016172Skarels if (u.u_prof.pr_scale) 11116172Skarels needsoft = 1; 1128124Sroot /* 1138124Sroot * CPU was in user state. Increment 1148124Sroot * user time counter, and process process-virtual time 1159604Ssam * interval timer. 1168124Sroot */ 11717007Smckusick BUMPTIME(&u.u_ru.ru_utime, tick); 1188097Sroot if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && 1198097Sroot itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) 1208097Sroot psignal(u.u_procp, SIGVTALRM); 1219Sbill } else { 1228124Sroot /* 12324524Sbloom * CPU was in system state. 1248124Sroot */ 12526265Skarels if (!noproc) 12617007Smckusick BUMPTIME(&u.u_ru.ru_stime, tick); 1279Sbill } 1288097Sroot 1298124Sroot /* 13010388Ssam * If the cpu is currently scheduled to a process, then 13110388Ssam * charge it with resource utilization for a tick, updating 13210388Ssam * statistics which run in (user+system) virtual time, 13310388Ssam * such as the cpu time limit and profiling timers. 13410388Ssam * This assumes that the current process has been running 13510388Ssam * the entire last tick. 13610388Ssam */ 13718585Skarels if (noproc == 0) { 13810388Ssam if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > 13910388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur) { 14010388Ssam psignal(u.u_procp, SIGXCPU); 14110388Ssam if (u.u_rlimit[RLIMIT_CPU].rlim_cur < 14210388Ssam u.u_rlimit[RLIMIT_CPU].rlim_max) 14310388Ssam u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; 14410388Ssam } 14510388Ssam if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && 14610388Ssam itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) 14710388Ssam psignal(u.u_procp, SIGPROF); 14810388Ssam s = u.u_procp->p_rssize; 14926265Skarels u.u_ru.ru_idrss += s; 15026265Skarels #ifdef notdef 15126265Skarels u.u_ru.ru_isrss += 0; /* XXX (haven't got this) */ 15226265Skarels #endif 15310388Ssam if (u.u_procp->p_textp) { 15410388Ssam register int xrss = u.u_procp->p_textp->x_rssize; 15510388Ssam 15610388Ssam s += xrss; 15710388Ssam u.u_ru.ru_ixrss += xrss; 15810388Ssam } 15910388Ssam if (s > u.u_ru.ru_maxrss) 16010388Ssam u.u_ru.ru_maxrss = s; 16110388Ssam } 16210388Ssam 16310388Ssam /* 1648124Sroot * We adjust the priority of the current process. 1658124Sroot * The priority of a process gets worse as it accumulates 1668124Sroot * CPU time. The cpu usage estimator (p_cpu) is increased here 1678124Sroot * and the formula for computing priorities (in kern_synch.c) 1688124Sroot * will compute a different value each time the p_cpu increases 1698124Sroot * by 4. The cpu usage estimator ramps up quite quickly when 1708124Sroot * the process is running (linearly), and decays away exponentially, 1718124Sroot * at a rate which is proportionally slower when the system is 1728124Sroot * busy. The basic principal is that the system will 90% forget 1738124Sroot * that a process used a lot of CPU time in 5*loadav seconds. 1748124Sroot * This causes the system to favor processes which haven't run 1758124Sroot * much recently, and to round-robin among other processes. 1768124Sroot */ 1779Sbill if (!noproc) { 1788097Sroot p = u.u_procp; 1798097Sroot p->p_cpticks++; 1808097Sroot if (++p->p_cpu == 0) 1818097Sroot p->p_cpu--; 1828124Sroot if ((p->p_cpu&3) == 0) { 1838097Sroot (void) setpri(p); 1848097Sroot if (p->p_pri >= PUSER) 1858097Sroot p->p_pri = p->p_usrpri; 1869Sbill } 1879Sbill } 1888124Sroot 1898124Sroot /* 19011392Ssam * If the alternate clock has not made itself known then 19111392Ssam * we must gather the statistics. 19211392Ssam */ 19311392Ssam if (phz == 0) 19411392Ssam gatherstats(pc, ps); 19511392Ssam 19611392Ssam /* 1978124Sroot * Increment the time-of-day, and schedule 1988124Sroot * processing of the callouts at a very low cpu priority, 1998124Sroot * so we don't keep the relatively high clock interrupt 2008124Sroot * priority any longer than necessary. 2018124Sroot */ 202*28828Skarels if (timedelta == 0) 20317356Skarels BUMPTIME(&time, tick) 20417356Skarels else { 20517356Skarels register delta; 20617356Skarels 207*28828Skarels if (timedelta < 0) { 208*28828Skarels delta = tick - tickdelta; 209*28828Skarels timedelta += tickdelta; 21017356Skarels } else { 211*28828Skarels delta = tick + tickdelta; 212*28828Skarels timedelta -= tickdelta; 21317356Skarels } 21417356Skarels BUMPTIME(&time, delta); 21517356Skarels } 21616525Skarels if (needsoft) { 21716525Skarels if (BASEPRI(ps)) { 21816525Skarels /* 21916525Skarels * Save the overhead of a software interrupt; 22016525Skarels * it will happen as soon as we return, so do it now. 22116525Skarels */ 22216525Skarels (void) splsoftclock(); 22316525Skarels softclock(pc, ps); 22416525Skarels } else 22516525Skarels setsoftclock(); 22616525Skarels } 2272442Swnj } 2282442Swnj 22915191Ssam int dk_ndrive = DK_NDRIVE; 2308124Sroot /* 23111392Ssam * Gather statistics on resource utilization. 23211392Ssam * 23311392Ssam * We make a gross assumption: that the system has been in the 23411392Ssam * state it is in (user state, kernel state, interrupt state, 23511392Ssam * or idle state) for the entire last time interval, and 23611392Ssam * update statistics accordingly. 23711392Ssam */ 23812747Ssam /*ARGSUSED*/ 23911392Ssam gatherstats(pc, ps) 24011392Ssam caddr_t pc; 24111392Ssam int ps; 24211392Ssam { 24326265Skarels register int cpstate, s; 24411392Ssam 24511392Ssam /* 24611392Ssam * Determine what state the cpu is in. 24711392Ssam */ 24811392Ssam if (USERMODE(ps)) { 24911392Ssam /* 25011392Ssam * CPU was in user state. 25111392Ssam */ 25211392Ssam if (u.u_procp->p_nice > NZERO) 25311392Ssam cpstate = CP_NICE; 25411392Ssam else 25511392Ssam cpstate = CP_USER; 25611392Ssam } else { 25711392Ssam /* 25811392Ssam * CPU was in system state. If profiling kernel 25924524Sbloom * increment a counter. If no process is running 26024524Sbloom * then this is a system tick if we were running 26124524Sbloom * at a non-zero IPL (in a driver). If a process is running, 26224524Sbloom * then we charge it with system time even if we were 26324524Sbloom * at a non-zero IPL, since the system often runs 26424524Sbloom * this way during processing of system calls. 26524524Sbloom * This is approximate, but the lack of true interval 26624524Sbloom * timers makes doing anything else difficult. 26711392Ssam */ 26811392Ssam cpstate = CP_SYS; 26911392Ssam if (noproc && BASEPRI(ps)) 27011392Ssam cpstate = CP_IDLE; 27111392Ssam #ifdef GPROF 27211392Ssam s = pc - s_lowpc; 27311392Ssam if (profiling < 2 && s < s_textsize) 27411392Ssam kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 27511392Ssam #endif 27611392Ssam } 27711392Ssam /* 27811392Ssam * We maintain statistics shown by user-level statistics 27911392Ssam * programs: the amount of time in each cpu state, and 28011392Ssam * the amount of time each of DK_NDRIVE ``drives'' is busy. 28111392Ssam */ 28211392Ssam cp_time[cpstate]++; 28311392Ssam for (s = 0; s < DK_NDRIVE; s++) 28426265Skarels if (dk_busy & (1 << s)) 28511392Ssam dk_time[s]++; 28611392Ssam } 28711392Ssam 28811392Ssam /* 2898124Sroot * Software priority level clock interrupt. 2908124Sroot * Run periodic events from timeout queue. 2918124Sroot */ 2922609Swnj /*ARGSUSED*/ 2932442Swnj softclock(pc, ps) 2942450Swnj caddr_t pc; 2958944Sroot int ps; 2962442Swnj { 2972442Swnj 2988097Sroot for (;;) { 2998124Sroot register struct callout *p1; 3008124Sroot register caddr_t arg; 3018124Sroot register int (*func)(); 3028124Sroot register int a, s; 3038124Sroot 30426265Skarels s = splhigh(); 3058097Sroot if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { 3068097Sroot splx(s); 3078097Sroot break; 3082442Swnj } 3098124Sroot arg = p1->c_arg; func = p1->c_func; a = p1->c_time; 3108097Sroot calltodo.c_next = p1->c_next; 3118097Sroot p1->c_next = callfree; 3128097Sroot callfree = p1; 3139157Ssam splx(s); 3148112Sroot (*func)(arg, a); 3152442Swnj } 3169604Ssam /* 31713127Ssam * If trapped user-mode and profiling, give it 31813127Ssam * a profiling tick. 3199604Ssam */ 32013127Ssam if (USERMODE(ps)) { 32113127Ssam register struct proc *p = u.u_procp; 32213127Ssam 32313127Ssam if (u.u_prof.pr_scale) { 32413127Ssam p->p_flag |= SOWEUPC; 32513127Ssam aston(); 32613127Ssam } 32713127Ssam /* 32813127Ssam * Check to see if process has accumulated 32913127Ssam * more than 10 minutes of user time. If so 33013127Ssam * reduce priority to give others a chance. 33113127Ssam */ 33213127Ssam if (p->p_uid && p->p_nice == NZERO && 33313127Ssam u.u_ru.ru_utime.tv_sec > 10 * 60) { 33413127Ssam p->p_nice = NZERO+4; 33513127Ssam (void) setpri(p); 33613127Ssam p->p_pri = p->p_usrpri; 33713127Ssam } 3389604Ssam } 3399Sbill } 3409Sbill 3419Sbill /* 34212747Ssam * Arrange that (*fun)(arg) is called in t/hz seconds. 34312747Ssam */ 34412747Ssam timeout(fun, arg, t) 3452450Swnj int (*fun)(); 3462450Swnj caddr_t arg; 34712747Ssam register int t; 3489Sbill { 3493542Swnj register struct callout *p1, *p2, *pnew; 35026265Skarels register int s = splhigh(); 3519Sbill 35218282Smckusick if (t <= 0) 35312747Ssam t = 1; 3543542Swnj pnew = callfree; 3553542Swnj if (pnew == NULL) 3563542Swnj panic("timeout table overflow"); 3573542Swnj callfree = pnew->c_next; 3583542Swnj pnew->c_arg = arg; 3593542Swnj pnew->c_func = fun; 3603542Swnj for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) 3619742Ssam if (p2->c_time > 0) 3629742Ssam t -= p2->c_time; 3633542Swnj p1->c_next = pnew; 3643542Swnj pnew->c_next = p2; 3653542Swnj pnew->c_time = t; 3663542Swnj if (p2) 3673542Swnj p2->c_time -= t; 3689Sbill splx(s); 3699Sbill } 3707305Ssam 3717305Ssam /* 3727305Ssam * untimeout is called to remove a function timeout call 3737305Ssam * from the callout structure. 3747305Ssam */ 3758097Sroot untimeout(fun, arg) 3767305Ssam int (*fun)(); 3777305Ssam caddr_t arg; 3787305Ssam { 3797305Ssam register struct callout *p1, *p2; 3807305Ssam register int s; 3817305Ssam 38226265Skarels s = splhigh(); 3837305Ssam for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { 3847305Ssam if (p2->c_func == fun && p2->c_arg == arg) { 3858112Sroot if (p2->c_next && p2->c_time > 0) 3867305Ssam p2->c_next->c_time += p2->c_time; 3877305Ssam p1->c_next = p2->c_next; 3887305Ssam p2->c_next = callfree; 3897305Ssam callfree = p2; 3907305Ssam break; 3917305Ssam } 3927305Ssam } 3937305Ssam splx(s); 3947305Ssam } 3958112Sroot 3968124Sroot /* 3978124Sroot * Compute number of hz until specified time. 3988124Sroot * Used to compute third argument to timeout() from an 3998124Sroot * absolute time. 4008124Sroot */ 4018112Sroot hzto(tv) 4028112Sroot struct timeval *tv; 4038112Sroot { 4048124Sroot register long ticks; 4058124Sroot register long sec; 40626265Skarels int s = splhigh(); 4078112Sroot 4088124Sroot /* 4098124Sroot * If number of milliseconds will fit in 32 bit arithmetic, 4108124Sroot * then compute number of milliseconds to time and scale to 4118124Sroot * ticks. Otherwise just compute number of hz in time, rounding 4128124Sroot * times greater than representible to maximum value. 4138124Sroot * 4148124Sroot * Delta times less than 25 days can be computed ``exactly''. 4158124Sroot * Maximum value for any timeout in 10ms ticks is 250 days. 4168124Sroot */ 4178124Sroot sec = tv->tv_sec - time.tv_sec; 4188124Sroot if (sec <= 0x7fffffff / 1000 - 1000) 4198124Sroot ticks = ((tv->tv_sec - time.tv_sec) * 1000 + 4208124Sroot (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); 4218124Sroot else if (sec <= 0x7fffffff / hz) 4228124Sroot ticks = sec * hz; 4238124Sroot else 4248124Sroot ticks = 0x7fffffff; 4258112Sroot splx(s); 4268112Sroot return (ticks); 4278112Sroot } 42812747Ssam 42912747Ssam profil() 43012747Ssam { 43112747Ssam register struct a { 43212747Ssam short *bufbase; 43312747Ssam unsigned bufsize; 43412747Ssam unsigned pcoffset; 43512747Ssam unsigned pcscale; 43612747Ssam } *uap = (struct a *)u.u_ap; 43712747Ssam register struct uprof *upp = &u.u_prof; 43812747Ssam 43912747Ssam upp->pr_base = uap->bufbase; 44012747Ssam upp->pr_size = uap->bufsize; 44112747Ssam upp->pr_off = uap->pcoffset; 44212747Ssam upp->pr_scale = uap->pcscale; 44312747Ssam } 44412747Ssam 44526265Skarels #ifdef COMPAT 44612747Ssam opause() 44712747Ssam { 44812747Ssam 44912747Ssam for (;;) 45012747Ssam sleep((caddr_t)&u, PSLEP); 45112747Ssam } 45226265Skarels #endif 453