xref: /freebsd/sys/kern/kern_clock.c (revision 0fddbf874719b9bd50cf66ac26d1140bb3f2be69)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
39  * $FreeBSD$
40  */
41 
42 #include "opt_ntp.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/dkstat.h>
47 #include <sys/callout.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/proc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/smp.h>
55 #include <sys/timetc.h>
56 #include <sys/timepps.h>
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_map.h>
60 #include <sys/sysctl.h>
61 #include <sys/bus.h>
62 #include <sys/interrupt.h>
63 
64 #include <machine/cpu.h>
65 #include <machine/limits.h>
66 
67 #ifdef GPROF
68 #include <sys/gmon.h>
69 #endif
70 
71 
72 static void initclocks __P((void *dummy));
73 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
74 
75 /* Some of these don't belong here, but it's easiest to concentrate them. */
76 long cp_time[CPUSTATES];
77 
78 SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
79     "LU", "CPU time statistics");
80 
81 long tk_cancc;
82 long tk_nin;
83 long tk_nout;
84 long tk_rawcc;
85 
86 /*
87  * Clock handling routines.
88  *
89  * This code is written to operate with two timers that run independently of
90  * each other.
91  *
92  * The main timer, running hz times per second, is used to trigger interval
93  * timers, timeouts and rescheduling as needed.
94  *
95  * The second timer handles kernel and user profiling,
96  * and does resource use estimation.  If the second timer is programmable,
97  * it is randomized to avoid aliasing between the two clocks.  For example,
98  * the randomization prevents an adversary from always giving up the cpu
99  * just before its quantum expires.  Otherwise, it would never accumulate
100  * cpu ticks.  The mean frequency of the second timer is stathz.
101  *
102  * If no second timer exists, stathz will be zero; in this case we drive
103  * profiling and statistics off the main clock.  This WILL NOT be accurate;
104  * do not do it unless absolutely necessary.
105  *
106  * The statistics clock may (or may not) be run at a higher rate while
107  * profiling.  This profile clock runs at profhz.  We require that profhz
108  * be an integral multiple of stathz.
109  *
110  * If the statistics clock is running fast, it must be divided by the ratio
111  * profhz/stathz for statistics.  (For profiling, every tick counts.)
112  *
113  * Time-of-day is maintained using a "timecounter", which may or may
114  * not be related to the hardware generating the above mentioned
115  * interrupts.
116  */
117 
118 int	stathz;
119 int	profhz;
120 static int profprocs;
121 int	ticks;
122 static int psdiv, pscnt;		/* prof => stat divider */
123 int	psratio;			/* ratio: prof / stat */
124 
125 /*
126  * Initialize clock frequencies and start both clocks running.
127  */
128 /* ARGSUSED*/
129 static void
130 initclocks(dummy)
131 	void *dummy;
132 {
133 	register int i;
134 
135 	/*
136 	 * Set divisors to 1 (normal case) and let the machine-specific
137 	 * code do its bit.
138 	 */
139 	psdiv = pscnt = 1;
140 	cpu_initclocks();
141 
142 	/*
143 	 * Compute profhz/stathz, and fix profhz if needed.
144 	 */
145 	i = stathz ? stathz : hz;
146 	if (profhz == 0)
147 		profhz = i;
148 	psratio = profhz / i;
149 }
150 
151 /*
152  * Each time the real-time timer fires, this function is called on all CPUs
153  * with each CPU passing in its curproc as the first argument.  If possible
154  * a nice optimization in the future would be to allow the CPU receiving the
155  * actual real-time timer interrupt to call this function on behalf of the
156  * other CPUs rather than sending an IPI to all other CPUs so that they
157  * can call this function.  Note that hardclock() calls hardclock_process()
158  * for the CPU receiving the timer interrupt, so only the other CPUs in the
159  * system need to call this function (or have it called on their behalf.
160  */
161 void
162 hardclock_process(p, user)
163 	struct proc *p;
164 	int user;
165 {
166 	struct pstats *pstats;
167 
168 	/*
169 	 * Run current process's virtual and profile time, as needed.
170 	 */
171 	mtx_assert(&sched_lock, MA_OWNED);
172 	pstats = p->p_stats;
173 	if (user &&
174 	    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
175 	    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
176 		p->p_sflag |= PS_ALRMPEND | PS_ASTPENDING;
177 	if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
178 	    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
179 		p->p_sflag |= PS_PROFPEND | PS_ASTPENDING;
180 }
181 
182 /*
183  * The real-time timer, interrupting hz times per second.
184  */
185 void
186 hardclock(frame)
187 	register struct clockframe *frame;
188 {
189 	int need_softclock = 0;
190 
191 	CTR0(KTR_INTR, "hardclock fired");
192 	mtx_lock_spin(&sched_lock);
193 	hardclock_process(curproc, CLKF_USERMODE(frame));
194 	mtx_unlock_spin(&sched_lock);
195 
196 	/*
197 	 * If no separate statistics clock is available, run it from here.
198 	 *
199 	 * XXX: this only works for UP
200 	 */
201 	if (stathz == 0)
202 		statclock(frame);
203 
204 	tc_windup();
205 
206 	/*
207 	 * Process callouts at a very low cpu priority, so we don't keep the
208 	 * relatively high clock interrupt priority any longer than necessary.
209 	 */
210 	mtx_lock_spin(&callout_lock);
211 	ticks++;
212 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
213 		need_softclock = 1;
214 	} else if (softticks + 1 == ticks)
215 		++softticks;
216 	mtx_unlock_spin(&callout_lock);
217 
218 	/*
219 	 * swi_sched acquires sched_lock, so we don't want to call it with
220 	 * callout_lock held; incorrect locking order.
221 	 */
222 	if (need_softclock)
223 		swi_sched(softclock_ih, SWI_NOSWITCH);
224 }
225 
226 /*
227  * Compute number of ticks in the specified amount of time.
228  */
229 int
230 tvtohz(tv)
231 	struct timeval *tv;
232 {
233 	register unsigned long ticks;
234 	register long sec, usec;
235 
236 	/*
237 	 * If the number of usecs in the whole seconds part of the time
238 	 * difference fits in a long, then the total number of usecs will
239 	 * fit in an unsigned long.  Compute the total and convert it to
240 	 * ticks, rounding up and adding 1 to allow for the current tick
241 	 * to expire.  Rounding also depends on unsigned long arithmetic
242 	 * to avoid overflow.
243 	 *
244 	 * Otherwise, if the number of ticks in the whole seconds part of
245 	 * the time difference fits in a long, then convert the parts to
246 	 * ticks separately and add, using similar rounding methods and
247 	 * overflow avoidance.  This method would work in the previous
248 	 * case but it is slightly slower and assumes that hz is integral.
249 	 *
250 	 * Otherwise, round the time difference down to the maximum
251 	 * representable value.
252 	 *
253 	 * If ints have 32 bits, then the maximum value for any timeout in
254 	 * 10ms ticks is 248 days.
255 	 */
256 	sec = tv->tv_sec;
257 	usec = tv->tv_usec;
258 	if (usec < 0) {
259 		sec--;
260 		usec += 1000000;
261 	}
262 	if (sec < 0) {
263 #ifdef DIAGNOSTIC
264 		if (usec > 0) {
265 			sec++;
266 			usec -= 1000000;
267 		}
268 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
269 		       sec, usec);
270 #endif
271 		ticks = 1;
272 	} else if (sec <= LONG_MAX / 1000000)
273 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
274 			/ tick + 1;
275 	else if (sec <= LONG_MAX / hz)
276 		ticks = sec * hz
277 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
278 	else
279 		ticks = LONG_MAX;
280 	if (ticks > INT_MAX)
281 		ticks = INT_MAX;
282 	return ((int)ticks);
283 }
284 
285 /*
286  * Start profiling on a process.
287  *
288  * Kernel profiling passes proc0 which never exits and hence
289  * keeps the profile clock running constantly.
290  */
291 void
292 startprofclock(p)
293 	register struct proc *p;
294 {
295 	int s;
296 
297 	/*
298 	 * XXX; Right now sched_lock protects statclock(), but perhaps
299 	 * it should be protected later on by a time_lock, which would
300 	 * cover psdiv, etc. as well.
301 	 */
302 	mtx_lock_spin(&sched_lock);
303 	if ((p->p_sflag & PS_PROFIL) == 0) {
304 		p->p_sflag |= PS_PROFIL;
305 		if (++profprocs == 1 && stathz != 0) {
306 			s = splstatclock();
307 			psdiv = pscnt = psratio;
308 			setstatclockrate(profhz);
309 			splx(s);
310 		}
311 	}
312 	mtx_unlock_spin(&sched_lock);
313 }
314 
315 /*
316  * Stop profiling on a process.
317  */
318 void
319 stopprofclock(p)
320 	register struct proc *p;
321 {
322 	int s;
323 
324 	mtx_lock_spin(&sched_lock);
325 	if (p->p_sflag & PS_PROFIL) {
326 		p->p_sflag &= ~PS_PROFIL;
327 		if (--profprocs == 0 && stathz != 0) {
328 			s = splstatclock();
329 			psdiv = pscnt = 1;
330 			setstatclockrate(stathz);
331 			splx(s);
332 		}
333 	}
334 	mtx_unlock_spin(&sched_lock);
335 }
336 
337 /*
338  * Do process and kernel statistics.  Most of the statistics are only
339  * used by user-level statistics programs.  The main exceptions are
340  * p->p_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.  This function
341  * should be called by all CPUs in the system for each statistics clock
342  * interrupt.  See the description of hardclock_process for more detail on
343  * this function's relationship to statclock.
344  */
345 void
346 statclock_process(p, pc, user)
347 	struct proc *p;
348 	register_t pc;
349 	int user;
350 {
351 #ifdef GPROF
352 	struct gmonparam *g;
353 	int i;
354 #endif
355 	struct pstats *pstats;
356 	long rss;
357 	struct rusage *ru;
358 	struct vmspace *vm;
359 
360 	KASSERT(p == curproc, ("statclock_process: p != curproc"));
361 	mtx_assert(&sched_lock, MA_OWNED);
362 	if (user) {
363 		/*
364 		 * Came from user mode; CPU was in user state.
365 		 * If this process is being profiled, record the tick.
366 		 */
367 		if (p->p_sflag & PS_PROFIL)
368 			addupc_intr(p, pc, 1);
369 		if (pscnt < psdiv)
370 			return;
371 		/*
372 		 * Charge the time as appropriate.
373 		 */
374 		p->p_uticks++;
375 		if (p->p_nice > NZERO)
376 			cp_time[CP_NICE]++;
377 		else
378 			cp_time[CP_USER]++;
379 	} else {
380 #ifdef GPROF
381 		/*
382 		 * Kernel statistics are just like addupc_intr, only easier.
383 		 */
384 		g = &_gmonparam;
385 		if (g->state == GMON_PROF_ON) {
386 			i = pc - g->lowpc;
387 			if (i < g->textsize) {
388 				i /= HISTFRACTION * sizeof(*g->kcount);
389 				g->kcount[i]++;
390 			}
391 		}
392 #endif
393 		if (pscnt < psdiv)
394 			return;
395 		/*
396 		 * Came from kernel mode, so we were:
397 		 * - handling an interrupt,
398 		 * - doing syscall or trap work on behalf of the current
399 		 *   user process, or
400 		 * - spinning in the idle loop.
401 		 * Whichever it is, charge the time as appropriate.
402 		 * Note that we charge interrupts to the current process,
403 		 * regardless of whether they are ``for'' that process,
404 		 * so that we know how much of its real time was spent
405 		 * in ``non-process'' (i.e., interrupt) work.
406 		 */
407 		if ((p->p_ithd != NULL) || p->p_intr_nesting_level >= 2) {
408 			p->p_iticks++;
409 			cp_time[CP_INTR]++;
410 		} else {
411 			p->p_sticks++;
412 			if (p != PCPU_GET(idleproc))
413 				cp_time[CP_SYS]++;
414 			else
415 				cp_time[CP_IDLE]++;
416 		}
417 	}
418 
419 	schedclock(p);
420 
421 	/* Update resource usage integrals and maximums. */
422 	if ((pstats = p->p_stats) != NULL &&
423 	    (ru = &pstats->p_ru) != NULL &&
424 	    (vm = p->p_vmspace) != NULL) {
425 		ru->ru_ixrss += pgtok(vm->vm_tsize);
426 		ru->ru_idrss += pgtok(vm->vm_dsize);
427 		ru->ru_isrss += pgtok(vm->vm_ssize);
428 		rss = pgtok(vmspace_resident_count(vm));
429 		if (ru->ru_maxrss < rss)
430 			ru->ru_maxrss = rss;
431 	}
432 }
433 
434 /*
435  * Statistics clock.  Grab profile sample, and if divider reaches 0,
436  * do process and kernel statistics.  Most of the statistics are only
437  * used by user-level statistics programs.  The main exceptions are
438  * p->p_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
439  */
440 void
441 statclock(frame)
442 	register struct clockframe *frame;
443 {
444 
445 	CTR0(KTR_INTR, "statclock fired");
446 	mtx_lock_spin(&sched_lock);
447 	if (--pscnt == 0)
448 		pscnt = psdiv;
449 	statclock_process(curproc, CLKF_PC(frame), CLKF_USERMODE(frame));
450 	mtx_unlock_spin(&sched_lock);
451 }
452 
453 /*
454  * Return information about system clocks.
455  */
456 static int
457 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
458 {
459 	struct clockinfo clkinfo;
460 	/*
461 	 * Construct clockinfo structure.
462 	 */
463 	clkinfo.hz = hz;
464 	clkinfo.tick = tick;
465 	clkinfo.tickadj = tickadj;
466 	clkinfo.profhz = profhz;
467 	clkinfo.stathz = stathz ? stathz : hz;
468 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
469 }
470 
471 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
472 	0, 0, sysctl_kern_clockrate, "S,clockinfo","");
473