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