xref: /freebsd/sys/kern/kern_clock.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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 curthread 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(td, user)
163 	struct thread *td;
164 	int user;
165 {
166 	struct pstats *pstats;
167 	struct proc *p = td->td_proc;
168 
169 	/*
170 	 * Run current process's virtual and profile time, as needed.
171 	 */
172 	mtx_assert(&sched_lock, MA_OWNED);
173 	if (p->p_flag & P_KSES) {
174 		/* XXXKSE What to do? */
175 	} else {
176 		pstats = p->p_stats;
177 		if (user &&
178 		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
179 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
180 			p->p_sflag |= PS_ALRMPEND;
181 			td->td_kse->ke_flags |= KEF_ASTPENDING;
182 		}
183 		if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
184 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) {
185 			p->p_sflag |= PS_PROFPEND;
186 			td->td_kse->ke_flags |= KEF_ASTPENDING;
187 		}
188 	}
189 }
190 
191 /*
192  * The real-time timer, interrupting hz times per second.
193  */
194 void
195 hardclock(frame)
196 	register struct clockframe *frame;
197 {
198 	int need_softclock = 0;
199 
200 	CTR0(KTR_INTR, "hardclock fired");
201 	mtx_lock_spin(&sched_lock);
202 	hardclock_process(curthread, CLKF_USERMODE(frame));
203 	mtx_unlock_spin(&sched_lock);
204 
205 	/*
206 	 * If no separate statistics clock is available, run it from here.
207 	 *
208 	 * XXX: this only works for UP
209 	 */
210 	if (stathz == 0)
211 		statclock(frame);
212 
213 	tc_windup();
214 
215 	/*
216 	 * Process callouts at a very low cpu priority, so we don't keep the
217 	 * relatively high clock interrupt priority any longer than necessary.
218 	 */
219 	mtx_lock_spin(&callout_lock);
220 	ticks++;
221 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
222 		need_softclock = 1;
223 	} else if (softticks + 1 == ticks)
224 		++softticks;
225 	mtx_unlock_spin(&callout_lock);
226 
227 	/*
228 	 * swi_sched acquires sched_lock, so we don't want to call it with
229 	 * callout_lock held; incorrect locking order.
230 	 */
231 	if (need_softclock)
232 		swi_sched(softclock_ih, SWI_NOSWITCH);
233 }
234 
235 /*
236  * Compute number of ticks in the specified amount of time.
237  */
238 int
239 tvtohz(tv)
240 	struct timeval *tv;
241 {
242 	register unsigned long ticks;
243 	register long sec, usec;
244 
245 	/*
246 	 * If the number of usecs in the whole seconds part of the time
247 	 * difference fits in a long, then the total number of usecs will
248 	 * fit in an unsigned long.  Compute the total and convert it to
249 	 * ticks, rounding up and adding 1 to allow for the current tick
250 	 * to expire.  Rounding also depends on unsigned long arithmetic
251 	 * to avoid overflow.
252 	 *
253 	 * Otherwise, if the number of ticks in the whole seconds part of
254 	 * the time difference fits in a long, then convert the parts to
255 	 * ticks separately and add, using similar rounding methods and
256 	 * overflow avoidance.  This method would work in the previous
257 	 * case but it is slightly slower and assumes that hz is integral.
258 	 *
259 	 * Otherwise, round the time difference down to the maximum
260 	 * representable value.
261 	 *
262 	 * If ints have 32 bits, then the maximum value for any timeout in
263 	 * 10ms ticks is 248 days.
264 	 */
265 	sec = tv->tv_sec;
266 	usec = tv->tv_usec;
267 	if (usec < 0) {
268 		sec--;
269 		usec += 1000000;
270 	}
271 	if (sec < 0) {
272 #ifdef DIAGNOSTIC
273 		if (usec > 0) {
274 			sec++;
275 			usec -= 1000000;
276 		}
277 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
278 		       sec, usec);
279 #endif
280 		ticks = 1;
281 	} else if (sec <= LONG_MAX / 1000000)
282 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
283 			/ tick + 1;
284 	else if (sec <= LONG_MAX / hz)
285 		ticks = sec * hz
286 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
287 	else
288 		ticks = LONG_MAX;
289 	if (ticks > INT_MAX)
290 		ticks = INT_MAX;
291 	return ((int)ticks);
292 }
293 
294 /*
295  * Start profiling on a process.
296  *
297  * Kernel profiling passes proc0 which never exits and hence
298  * keeps the profile clock running constantly.
299  */
300 void
301 startprofclock(p)
302 	register struct proc *p;
303 {
304 	int s;
305 
306 	/*
307 	 * XXX; Right now sched_lock protects statclock(), but perhaps
308 	 * it should be protected later on by a time_lock, which would
309 	 * cover psdiv, etc. as well.
310 	 */
311 	mtx_lock_spin(&sched_lock);
312 	if ((p->p_sflag & PS_PROFIL) == 0) {
313 		p->p_sflag |= PS_PROFIL;
314 		if (++profprocs == 1 && stathz != 0) {
315 			s = splstatclock();
316 			psdiv = pscnt = psratio;
317 			setstatclockrate(profhz);
318 			splx(s);
319 		}
320 	}
321 	mtx_unlock_spin(&sched_lock);
322 }
323 
324 /*
325  * Stop profiling on a process.
326  */
327 void
328 stopprofclock(p)
329 	register struct proc *p;
330 {
331 	int s;
332 
333 	mtx_lock_spin(&sched_lock);
334 	if (p->p_sflag & PS_PROFIL) {
335 		p->p_sflag &= ~PS_PROFIL;
336 		if (--profprocs == 0 && stathz != 0) {
337 			s = splstatclock();
338 			psdiv = pscnt = 1;
339 			setstatclockrate(stathz);
340 			splx(s);
341 		}
342 	}
343 	mtx_unlock_spin(&sched_lock);
344 }
345 
346 /*
347  * Do process and kernel statistics.  Most of the statistics are only
348  * used by user-level statistics programs.  The main exceptions are
349  * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.  This function
350  * should be called by all CPUs in the system for each statistics clock
351  * interrupt.  See the description of hardclock_process for more detail on
352  * this function's relationship to statclock.
353  */
354 void
355 statclock_process(ke, pc, user)
356 	struct kse *ke;
357 	register_t pc;
358 	int user;
359 {
360 #ifdef GPROF
361 	struct gmonparam *g;
362 	int i;
363 #endif
364 	struct pstats *pstats;
365 	long rss;
366 	struct rusage *ru;
367 	struct vmspace *vm;
368 	struct proc *p = ke->ke_proc;
369 	struct thread *td = ke->ke_thread; /* current thread */
370 
371 	KASSERT(ke == curthread->td_kse, ("statclock_process: td != curthread"));
372 	mtx_assert(&sched_lock, MA_OWNED);
373 	if (user) {
374 		/*
375 		 * Came from user mode; CPU was in user state.
376 		 * If this process is being profiled, record the tick.
377 		 */
378 		if (p->p_sflag & PS_PROFIL)
379 			addupc_intr(ke, pc, 1);
380 		if (pscnt < psdiv)
381 			return;
382 		/*
383 		 * Charge the time as appropriate.
384 		 */
385 		ke->ke_uticks++;
386 		if (ke->ke_ksegrp->kg_nice > NZERO)
387 			cp_time[CP_NICE]++;
388 		else
389 			cp_time[CP_USER]++;
390 	} else {
391 #ifdef GPROF
392 		/*
393 		 * Kernel statistics are just like addupc_intr, only easier.
394 		 */
395 		g = &_gmonparam;
396 		if (g->state == GMON_PROF_ON) {
397 			i = pc - g->lowpc;
398 			if (i < g->textsize) {
399 				i /= HISTFRACTION * sizeof(*g->kcount);
400 				g->kcount[i]++;
401 			}
402 		}
403 #endif
404 		if (pscnt < psdiv)
405 			return;
406 		/*
407 		 * Came from kernel mode, so we were:
408 		 * - handling an interrupt,
409 		 * - doing syscall or trap work on behalf of the current
410 		 *   user process, or
411 		 * - spinning in the idle loop.
412 		 * Whichever it is, charge the time as appropriate.
413 		 * Note that we charge interrupts to the current process,
414 		 * regardless of whether they are ``for'' that process,
415 		 * so that we know how much of its real time was spent
416 		 * in ``non-process'' (i.e., interrupt) work.
417 		 */
418 		if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) {
419 			ke->ke_iticks++;
420 			cp_time[CP_INTR]++;
421 		} else {
422 			ke->ke_sticks++;
423 			if (p != PCPU_GET(idlethread)->td_proc)
424 				cp_time[CP_SYS]++;
425 			else
426 				cp_time[CP_IDLE]++;
427 		}
428 	}
429 
430 	schedclock(ke->ke_thread);
431 
432 	/* Update resource usage integrals and maximums. */
433 	if ((pstats = p->p_stats) != NULL &&
434 	    (ru = &pstats->p_ru) != NULL &&
435 	    (vm = p->p_vmspace) != NULL) {
436 		ru->ru_ixrss += pgtok(vm->vm_tsize);
437 		ru->ru_idrss += pgtok(vm->vm_dsize);
438 		ru->ru_isrss += pgtok(vm->vm_ssize);
439 		rss = pgtok(vmspace_resident_count(vm));
440 		if (ru->ru_maxrss < rss)
441 			ru->ru_maxrss = rss;
442 	}
443 }
444 
445 /*
446  * Statistics clock.  Grab profile sample, and if divider reaches 0,
447  * do process and kernel statistics.  Most of the statistics are only
448  * used by user-level statistics programs.  The main exceptions are
449  * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
450  */
451 void
452 statclock(frame)
453 	register struct clockframe *frame;
454 {
455 
456 	CTR0(KTR_INTR, "statclock fired");
457 	mtx_lock_spin(&sched_lock);
458 	if (--pscnt == 0)
459 		pscnt = psdiv;
460 	statclock_process(curthread->td_kse, CLKF_PC(frame), CLKF_USERMODE(frame));
461 	mtx_unlock_spin(&sched_lock);
462 }
463 
464 /*
465  * Return information about system clocks.
466  */
467 static int
468 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
469 {
470 	struct clockinfo clkinfo;
471 	/*
472 	 * Construct clockinfo structure.
473 	 */
474 	clkinfo.hz = hz;
475 	clkinfo.tick = tick;
476 	clkinfo.tickadj = tickadj;
477 	clkinfo.profhz = profhz;
478 	clkinfo.stathz = stathz ? stathz : hz;
479 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
480 }
481 
482 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
483 	0, 0, sysctl_kern_clockrate, "S,clockinfo","");
484