xref: /original-bsd/sys/kern/kern_clock.c (revision e037b36a)
1 /*-
2  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_clock.c	7.24 (Berkeley) 07/16/92
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "dkstat.h"
13 #include "callout.h"
14 #include "kernel.h"
15 #include "proc.h"
16 #include "resourcevar.h"
17 
18 #include "machine/cpu.h"
19 
20 #ifdef GPROF
21 #include "gmon.h"
22 extern u_short *kcount;
23 #endif
24 
25 /*
26  * Clock handling routines.
27  *
28  * This code is written to operate with two timers that run independently of
29  * each other.  The main clock, running hz times per second, is used to keep
30  * track of real time.  The second timer handles kernel and user profiling,
31  * and does resource use estimation.  If the second timer is programmable,
32  * it is randomized to avoid aliasing between the two clocks.  For example,
33  * the randomization prevents an adversary from always giving up the cpu
34  * just before its quantum expires.  Otherwise, it would never accumulate
35  * cpu ticks.  The mean frequency of the second timer is stathz.
36  *
37  * If no second timer exists, stathz will be zero; in this case we drive
38  * profiling and statistics off the main clock.  This WILL NOT be accurate;
39  * do not do it unless absolutely necessary.
40  *
41  * The statistics clock may (or may not) be run at a higher rate while
42  * profiling.  This profile clock runs at profhz.  We require that profhz
43  * be an integral multiple of stathz.
44  *
45  * If the statistics clock is running fast, it must be divided by the ratio
46  * profhz/stathz for statistics.  (For profiling, every tick counts.)
47  */
48 
49 /*
50  * TODO:
51  *	allocate more timeout table slots when table overflows.
52  */
53 
54 /*
55  * Bump a timeval by a small number of usec's.
56  */
57 #define BUMPTIME(t, usec) { \
58 	register volatile struct timeval *tp = (t); \
59 	register long us; \
60  \
61 	tp->tv_usec = us = tp->tv_usec + (usec); \
62 	if (us >= 1000000) { \
63 		tp->tv_usec = us - 1000000; \
64 		tp->tv_sec++; \
65 	} \
66 }
67 
68 int	stathz;
69 int	profhz;
70 int	profprocs;
71 static int psratio, psdiv, pscnt;	/* prof => stat divider */
72 
73 volatile struct	timeval time;
74 volatile struct	timeval mono_time;
75 
76 /*
77  * Initialize clock frequencies and start both clocks running.
78  */
79 void
80 initclocks()
81 {
82 	register int i;
83 
84 	/*
85 	 * Set divisors to 1 (normal case) and let the machine-specific
86 	 * code do its bit.
87 	 */
88 	psdiv = pscnt = 1;
89 	cpu_initclocks();
90 
91 	/*
92 	 * Compute profhz/stathz, and fix profhz if needed.
93 	 */
94 	i = stathz ? stathz : hz;
95 	if (profhz == 0)
96 		profhz = i;
97 	psratio = profhz / i;
98 }
99 
100 /*
101  * The real-time timer, interrupting hz times per second.
102  */
103 void
104 hardclock(frame)
105 	register struct clockframe *frame;
106 {
107 	register struct callout *p1;
108 	register struct proc *p;
109 	register int delta, needsoft;
110 	extern int tickdelta;
111 	extern long timedelta;
112 
113 	/*
114 	 * Update real-time timeout queue.
115 	 * At front of queue are some number of events which are ``due''.
116 	 * The time to these is <= 0 and if negative represents the
117 	 * number of ticks which have passed since it was supposed to happen.
118 	 * The rest of the q elements (times > 0) are events yet to happen,
119 	 * where the time for each is given as a delta from the previous.
120 	 * Decrementing just the first of these serves to decrement the time
121 	 * to all events.
122 	 */
123 	needsoft = 0;
124 	for (p1 = calltodo.c_next; p1 != NULL; p1 = p1->c_next) {
125 		if (--p1->c_time > 0)
126 			break;
127 		needsoft = 1;
128 		if (p1->c_time == 0)
129 			break;
130 	}
131 
132 	p = curproc;
133 	if (p) {
134 		register struct pstats *pstats;
135 
136 		/*
137 		 * Run current process's virtual and profile time, as needed.
138 		 */
139 		pstats = p->p_stats;
140 		if (CLKF_USERMODE(frame) &&
141 		    timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
142 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
143 			psignal(p, SIGVTALRM);
144 		if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
145 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
146 			psignal(p, SIGPROF);
147 	}
148 
149 	/*
150 	 * If no separate statistics clock is available, run it from here.
151 	 */
152 	if (stathz == 0)
153 		statclock(frame);
154 
155 	/*
156 	 * Increment the time-of-day.  The increment is just ``tick'' unless
157 	 * we are still adjusting the clock; see adjtime().
158 	 */
159 	if (timedelta == 0)
160 		delta = tick;
161 	else {
162 		delta = tick + tickdelta;
163 		timedelta -= tickdelta;
164 	}
165 	BUMPTIME(&time, delta);
166 	BUMPTIME(&mono_time, delta);
167 
168 	/*
169 	 * Process callouts at a very low cpu priority, so we don't keep the
170 	 * relatively high clock interrupt priority any longer than necessary.
171 	 */
172 	if (needsoft) {
173 		if (CLKF_BASEPRI(frame)) {
174 			/*
175 			 * Save the overhead of a software interrupt;
176 			 * it will happen as soon as we return, so do it now.
177 			 */
178 			(void)splsoftclock();
179 			softclock();
180 		} else
181 			setsoftclock();
182 	}
183 }
184 
185 /*
186  * Software (low priority) clock interrupt.
187  * Run periodic events from timeout queue.
188  */
189 /*ARGSUSED*/
190 void
191 softclock()
192 {
193 	register struct callout *c;
194 	register void *arg;
195 	register void (*func) __P((void *));
196 	register int s;
197 
198 	s = splhigh();
199 	while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
200 		func = c->c_func;
201 		arg = c->c_arg;
202 		calltodo.c_next = c->c_next;
203 		c->c_next = callfree;
204 		callfree = c;
205 		splx(s);
206 		(*func)(arg);
207 		(void) splhigh();
208 	}
209 	splx(s);
210 }
211 
212 /*
213  * Arrange that (*func)(arg) is called in t/hz seconds.
214  */
215 void
216 timeout(func, arg, t)
217 	void (*func) __P((void *));
218 	void *arg;
219 	register int t;
220 {
221 	register struct callout *p1, *p2, *pnew;
222 	register int s;
223 
224 	s = splhigh();
225 	if (t <= 0)
226 		t = 1;
227 	pnew = callfree;
228 	if (pnew == NULL)
229 		panic("timeout table overflow");
230 	callfree = pnew->c_next;
231 	pnew->c_arg = arg;
232 	pnew->c_func = func;
233 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
234 		if (p2->c_time > 0)
235 			t -= p2->c_time;
236 	p1->c_next = pnew;
237 	pnew->c_next = p2;
238 	pnew->c_time = t;
239 	if (p2)
240 		p2->c_time -= t;
241 	splx(s);
242 }
243 
244 /*
245  * untimeout is called to remove a function timeout call
246  * from the callout structure.
247  */
248 void
249 untimeout(func, arg)
250 	void (*func) __P((void *));
251 	void *arg;
252 {
253 	register struct callout *p1, *p2;
254 	register int s;
255 
256 	s = splhigh();
257 	for (p1 = &calltodo; (p2 = p1->c_next) != NULL; p1 = p2) {
258 		if (p2->c_func == func && p2->c_arg == arg) {
259 			if (p2->c_next && p2->c_time > 0)
260 				p2->c_next->c_time += p2->c_time;
261 			p1->c_next = p2->c_next;
262 			p2->c_next = callfree;
263 			callfree = p2;
264 			break;
265 		}
266 	}
267 	splx(s);
268 }
269 
270 /*
271  * Compute number of hz until specified time.
272  * Used to compute third argument to timeout() from an
273  * absolute time.
274  */
275 int
276 hzto(tv)
277 	struct timeval *tv;
278 {
279 	register long ticks, sec;
280 	int s;
281 
282 	/*
283 	 * If number of milliseconds will fit in 32 bit arithmetic,
284 	 * then compute number of milliseconds to time and scale to
285 	 * ticks.  Otherwise just compute number of hz in time, rounding
286 	 * times greater than representible to maximum value.
287 	 *
288 	 * Delta times less than 25 days can be computed ``exactly''.
289 	 * Maximum value for any timeout in 10ms ticks is 250 days.
290 	 */
291 	s = splhigh();
292 	sec = tv->tv_sec - time.tv_sec;
293 	if (sec <= 0x7fffffff / 1000 - 1000)
294 		ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
295 			(tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
296 	else if (sec <= 0x7fffffff / hz)
297 		ticks = sec * hz;
298 	else
299 		ticks = 0x7fffffff;
300 	splx(s);
301 	return (ticks);
302 }
303 
304 /*
305  * Start profiling on a process.
306  *
307  * Kernel profiling passes proc0 which never exits and hence
308  * keeps the profile clock running constantly.
309  */
310 void
311 startprofclock(p)
312 	register struct proc *p;
313 {
314 	int s;
315 
316 	if ((p->p_flag & SPROFIL) == 0) {
317 		p->p_flag |= SPROFIL;
318 		if (++profprocs == 1 && stathz != 0) {
319 			s = splstatclock();
320 			psdiv = pscnt = psratio;
321 			setstatclockrate(profhz);
322 			splx(s);
323 		}
324 	}
325 }
326 
327 /*
328  * Stop profiling on a process.
329  */
330 void
331 stopprofclock(p)
332 	register struct proc *p;
333 {
334 	int s;
335 
336 	if (p->p_flag & SPROFIL) {
337 		p->p_flag &= ~SPROFIL;
338 		if (--profprocs == 0 && stathz != 0) {
339 			s = splstatclock();
340 			psdiv = pscnt = 1;
341 			setstatclockrate(stathz);
342 			splx(s);
343 		}
344 	}
345 }
346 
347 int	dk_ndrive = DK_NDRIVE;
348 
349 /*
350  * Statistics clock.  Grab profile sample, and if divider reaches 0,
351  * do process and kernel statistics.
352  */
353 void
354 statclock(frame)
355 	register struct clockframe *frame;
356 {
357 #ifdef GPROF
358 	register struct gmonparam *g;
359 #endif
360 	register struct proc *p;
361 	register int i;
362 
363 	if (CLKF_USERMODE(frame)) {
364 		p = curproc;
365 		if (p->p_flag & SPROFIL)
366 			addupc_intr(p, CLKF_PC(frame), 1);
367 		if (--pscnt > 0)
368 			return;
369 		/*
370 		 * Came from user mode; CPU was in user state.
371 		 * If this process is being profiled record the tick.
372 		 */
373 		p->p_uticks++;
374 		if (p->p_nice > NZERO)
375 			cp_time[CP_NICE]++;
376 		else
377 			cp_time[CP_USER]++;
378 	} else {
379 #ifdef GPROF
380 		/*
381 		 * Kernel statistics are just like addupc_intr, only easier.
382 		 */
383 		g = &_gmonparam;
384 		if (g->state == GMON_PROF_ON) {
385 			i = CLKF_PC(frame) - g->lowpc;
386 			if (i < g->textsize)
387 				kcount[i / (HISTFRACTION * sizeof(*kcount))]++;
388 		}
389 #endif
390 		if (--pscnt > 0)
391 			return;
392 		/*
393 		 * Came from kernel mode, so we were:
394 		 * - handling an interrupt,
395 		 * - doing syscall or trap work on behalf of the current
396 		 *   user process, or
397 		 * - spinning in the idle loop.
398 		 * Whichever it is, charge the time as appropriate.
399 		 * Note that we charge interrupts to the current process,
400 		 * regardless of whether they are ``for'' that process,
401 		 * so that we know how much of its real time was spent
402 		 * in ``non-process'' (i.e., interrupt) work.
403 		 */
404 		p = curproc;
405 		if (CLKF_INTR(frame)) {
406 			if (p != NULL)
407 				p->p_iticks++;
408 			cp_time[CP_INTR]++;
409 		} else if (p != NULL) {
410 			p->p_sticks++;
411 			cp_time[CP_SYS]++;
412 		} else
413 			cp_time[CP_IDLE]++;
414 	}
415 	pscnt = psdiv;
416 
417 	/*
418 	 * We maintain statistics shown by user-level statistics
419 	 * programs:  the amount of time in each cpu state, and
420 	 * the amount of time each of DK_NDRIVE ``drives'' is busy.
421 	 *
422 	 * XXX	should either run linked list of drives, or (better)
423 	 *	grab timestamps in the start & done code.
424 	 */
425 	for (i = 0; i < DK_NDRIVE; i++)
426 		if (dk_busy & (1 << i))
427 			dk_time[i]++;
428 
429 	/*
430 	 * We adjust the priority of the current process.
431 	 * The priority of a process gets worse as it accumulates
432 	 * CPU time.  The cpu usage estimator (p_cpu) is increased here
433 	 * and the formula for computing priorities (in kern_synch.c)
434 	 * will compute a different value each time the p_cpu increases
435 	 * by 4.  The cpu usage estimator ramps up quite quickly when
436 	 * the process is running (linearly), and decays away
437 	 * exponentially, at a rate which is proportionally slower
438 	 * when the system is busy.  The basic principal is that the
439 	 * system will 90% forget that a process used a lot of CPU
440 	 * time in 5*loadav seconds.  This causes the system to favor
441 	 * processes which haven't run much recently, and to
442 	 * round-robin among other processes.
443 	 */
444 	if (p != NULL) {
445 		p->p_cpticks++;
446 		if (++p->p_cpu == 0)
447 			p->p_cpu--;
448 		if ((p->p_cpu & 3) == 0) {
449 			setpri(p);
450 			if (p->p_pri >= PUSER)
451 				p->p_pri = p->p_usrpri;
452 		}
453 	}
454 }
455 
456 /*
457  * Return information about system clocks.
458  */
459 /* ARGSUSED */
460 kinfo_clockrate(op, where, acopysize, arg, aneeded)
461 	int op;
462 	register char *where;
463 	int *acopysize, arg, *aneeded;
464 {
465 	int buflen, error;
466 	struct clockinfo clockinfo;
467 
468 	*aneeded = sizeof(clockinfo);
469 	if (where == NULL)
470 		return (0);
471 	/*
472 	 * Check for enough buffering.
473 	 */
474 	buflen = *acopysize;
475 	if (buflen < sizeof(clockinfo)) {
476 		*acopysize = 0;
477 		return (0);
478 	}
479 	/*
480 	 * Copyout clockinfo structure.
481 	 */
482 	clockinfo.hz = hz;
483 	clockinfo.tick = tick;
484 	clockinfo.profhz = profhz;
485 	clockinfo.stathz = stathz ? stathz : hz;
486 	if (error = copyout((caddr_t)&clockinfo, where, sizeof(clockinfo)))
487 		return (error);
488 	*acopysize = sizeof(clockinfo);
489 	return (0);
490 }
491