xref: /freebsd/sys/kern/kern_clock.c (revision 4103b7652d1c438d04b360d0c81bae619b1b8259)
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  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_ntp.h"
45 #include "opt_ddb.h"
46 #include "opt_watchdog.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/callout.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/ktr.h>
54 #include <sys/mutex.h>
55 #include <sys/proc.h>
56 #include <sys/resource.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sched.h>
59 #include <sys/signalvar.h>
60 #include <sys/smp.h>
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_map.h>
64 #include <sys/sysctl.h>
65 #include <sys/bus.h>
66 #include <sys/interrupt.h>
67 #include <sys/limits.h>
68 #include <sys/timetc.h>
69 
70 #include <machine/cpu.h>
71 
72 #ifdef GPROF
73 #include <sys/gmon.h>
74 #endif
75 
76 #ifdef DDB
77 #include <ddb/ddb.h>
78 #endif
79 
80 #ifdef DEVICE_POLLING
81 extern void hardclock_device_poll(void);
82 #endif /* DEVICE_POLLING */
83 
84 static void initclocks(void *dummy);
85 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
86 
87 /* Some of these don't belong here, but it's easiest to concentrate them. */
88 long cp_time[CPUSTATES];
89 
90 SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
91     "LU", "CPU time statistics");
92 
93 #ifdef SW_WATCHDOG
94 #include <sys/watchdog.h>
95 
96 static int watchdog_ticks;
97 static int watchdog_enabled;
98 static void watchdog_fire(void);
99 static void watchdog_config(void *, u_int, int *);
100 #endif /* SW_WATCHDOG */
101 
102 /*
103  * Clock handling routines.
104  *
105  * This code is written to operate with two timers that run independently of
106  * each other.
107  *
108  * The main timer, running hz times per second, is used to trigger interval
109  * timers, timeouts and rescheduling as needed.
110  *
111  * The second timer handles kernel and user profiling,
112  * and does resource use estimation.  If the second timer is programmable,
113  * it is randomized to avoid aliasing between the two clocks.  For example,
114  * the randomization prevents an adversary from always giving up the cpu
115  * just before its quantum expires.  Otherwise, it would never accumulate
116  * cpu ticks.  The mean frequency of the second timer is stathz.
117  *
118  * If no second timer exists, stathz will be zero; in this case we drive
119  * profiling and statistics off the main clock.  This WILL NOT be accurate;
120  * do not do it unless absolutely necessary.
121  *
122  * The statistics clock may (or may not) be run at a higher rate while
123  * profiling.  This profile clock runs at profhz.  We require that profhz
124  * be an integral multiple of stathz.
125  *
126  * If the statistics clock is running fast, it must be divided by the ratio
127  * profhz/stathz for statistics.  (For profiling, every tick counts.)
128  *
129  * Time-of-day is maintained using a "timecounter", which may or may
130  * not be related to the hardware generating the above mentioned
131  * interrupts.
132  */
133 
134 int	stathz;
135 int	profhz;
136 int	profprocs;
137 int	ticks;
138 int	psratio;
139 
140 /*
141  * Initialize clock frequencies and start both clocks running.
142  */
143 /* ARGSUSED*/
144 static void
145 initclocks(dummy)
146 	void *dummy;
147 {
148 	register int i;
149 
150 	/*
151 	 * Set divisors to 1 (normal case) and let the machine-specific
152 	 * code do its bit.
153 	 */
154 	cpu_initclocks();
155 
156 	/*
157 	 * Compute profhz/stathz, and fix profhz if needed.
158 	 */
159 	i = stathz ? stathz : hz;
160 	if (profhz == 0)
161 		profhz = i;
162 	psratio = profhz / i;
163 #ifdef SW_WATCHDOG
164 	EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
165 #endif
166 }
167 
168 /*
169  * Each time the real-time timer fires, this function is called on all CPUs.
170  * Note that hardclock() calls hardclock_process() for the boot CPU, so only
171  * the other CPUs in the system need to call this function.
172  */
173 void
174 hardclock_process(frame)
175 	register struct clockframe *frame;
176 {
177 	struct pstats *pstats;
178 	struct thread *td = curthread;
179 	struct proc *p = td->td_proc;
180 
181 	/*
182 	 * Run current process's virtual and profile time, as needed.
183 	 */
184 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
185 	if (p->p_flag & P_SA) {
186 		/* XXXKSE What to do? */
187 	} else {
188 		pstats = p->p_stats;
189 		if (CLKF_USERMODE(frame) &&
190 		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
191 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
192 			p->p_sflag |= PS_ALRMPEND;
193 			td->td_flags |= TDF_ASTPENDING;
194 		}
195 		if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
196 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) {
197 			p->p_sflag |= PS_PROFPEND;
198 			td->td_flags |= TDF_ASTPENDING;
199 		}
200 	}
201 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
202 }
203 
204 /*
205  * The real-time timer, interrupting hz times per second.
206  */
207 void
208 hardclock(frame)
209 	register struct clockframe *frame;
210 {
211 	int need_softclock = 0;
212 
213 	CTR0(KTR_CLK, "hardclock fired");
214 	hardclock_process(frame);
215 
216 	tc_ticktock();
217 	/*
218 	 * If no separate statistics clock is available, run it from here.
219 	 *
220 	 * XXX: this only works for UP
221 	 */
222 	if (stathz == 0) {
223 		profclock(frame);
224 		statclock(frame);
225 	}
226 
227 #ifdef DEVICE_POLLING
228 	hardclock_device_poll();	/* this is very short and quick */
229 #endif /* DEVICE_POLLING */
230 
231 	/*
232 	 * Process callouts at a very low cpu priority, so we don't keep the
233 	 * relatively high clock interrupt priority any longer than necessary.
234 	 */
235 	mtx_lock_spin_flags(&callout_lock, MTX_QUIET);
236 	ticks++;
237 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
238 		need_softclock = 1;
239 	} else if (softticks + 1 == ticks)
240 		++softticks;
241 	mtx_unlock_spin_flags(&callout_lock, MTX_QUIET);
242 
243 	/*
244 	 * swi_sched acquires sched_lock, so we don't want to call it with
245 	 * callout_lock held; incorrect locking order.
246 	 */
247 	if (need_softclock)
248 		swi_sched(softclock_ih, 0);
249 
250 #ifdef SW_WATCHDOG
251 	if (watchdog_enabled > 0 && --watchdog_ticks <= 0)
252 		watchdog_fire();
253 #endif /* SW_WATCHDOG */
254 }
255 
256 /*
257  * Compute number of ticks in the specified amount of time.
258  */
259 int
260 tvtohz(tv)
261 	struct timeval *tv;
262 {
263 	register unsigned long ticks;
264 	register long sec, usec;
265 
266 	/*
267 	 * If the number of usecs in the whole seconds part of the time
268 	 * difference fits in a long, then the total number of usecs will
269 	 * fit in an unsigned long.  Compute the total and convert it to
270 	 * ticks, rounding up and adding 1 to allow for the current tick
271 	 * to expire.  Rounding also depends on unsigned long arithmetic
272 	 * to avoid overflow.
273 	 *
274 	 * Otherwise, if the number of ticks in the whole seconds part of
275 	 * the time difference fits in a long, then convert the parts to
276 	 * ticks separately and add, using similar rounding methods and
277 	 * overflow avoidance.  This method would work in the previous
278 	 * case but it is slightly slower and assumes that hz is integral.
279 	 *
280 	 * Otherwise, round the time difference down to the maximum
281 	 * representable value.
282 	 *
283 	 * If ints have 32 bits, then the maximum value for any timeout in
284 	 * 10ms ticks is 248 days.
285 	 */
286 	sec = tv->tv_sec;
287 	usec = tv->tv_usec;
288 	if (usec < 0) {
289 		sec--;
290 		usec += 1000000;
291 	}
292 	if (sec < 0) {
293 #ifdef DIAGNOSTIC
294 		if (usec > 0) {
295 			sec++;
296 			usec -= 1000000;
297 		}
298 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
299 		       sec, usec);
300 #endif
301 		ticks = 1;
302 	} else if (sec <= LONG_MAX / 1000000)
303 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
304 			/ tick + 1;
305 	else if (sec <= LONG_MAX / hz)
306 		ticks = sec * hz
307 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
308 	else
309 		ticks = LONG_MAX;
310 	if (ticks > INT_MAX)
311 		ticks = INT_MAX;
312 	return ((int)ticks);
313 }
314 
315 /*
316  * Start profiling on a process.
317  *
318  * Kernel profiling passes proc0 which never exits and hence
319  * keeps the profile clock running constantly.
320  */
321 void
322 startprofclock(p)
323 	register struct proc *p;
324 {
325 
326 	/*
327 	 * XXX; Right now sched_lock protects statclock(), but perhaps
328 	 * it should be protected later on by a time_lock, which would
329 	 * cover psdiv, etc. as well.
330 	 */
331 	PROC_LOCK_ASSERT(p, MA_OWNED);
332 	if (p->p_flag & P_STOPPROF)
333 		return;
334 	if ((p->p_flag & P_PROFIL) == 0) {
335 		mtx_lock_spin(&sched_lock);
336 		p->p_flag |= P_PROFIL;
337 		if (++profprocs == 1)
338 			cpu_startprofclock();
339 		mtx_unlock_spin(&sched_lock);
340 	}
341 }
342 
343 /*
344  * Stop profiling on a process.
345  */
346 void
347 stopprofclock(p)
348 	register struct proc *p;
349 {
350 
351 	PROC_LOCK_ASSERT(p, MA_OWNED);
352 	if (p->p_flag & P_PROFIL) {
353 		if (p->p_profthreads != 0) {
354 			p->p_flag |= P_STOPPROF;
355 			while (p->p_profthreads != 0)
356 				msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
357 				    "stopprof", 0);
358 			p->p_flag &= ~P_STOPPROF;
359 		}
360 		mtx_lock_spin(&sched_lock);
361 		p->p_flag &= ~P_PROFIL;
362 		if (--profprocs == 0)
363 			cpu_stopprofclock();
364 		mtx_unlock_spin(&sched_lock);
365 	}
366 }
367 
368 /*
369  * Statistics clock.  Grab profile sample, and if divider reaches 0,
370  * do process and kernel statistics.  Most of the statistics are only
371  * used by user-level statistics programs.  The main exceptions are
372  * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
373  * This should be called by all active processors.
374  */
375 void
376 statclock(frame)
377 	register struct clockframe *frame;
378 {
379 	struct pstats *pstats;
380 	struct rusage *ru;
381 	struct vmspace *vm;
382 	struct thread *td;
383 	struct proc *p;
384 	long rss;
385 
386 	td = curthread;
387 	p = td->td_proc;
388 
389 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
390 	if (CLKF_USERMODE(frame)) {
391 		/*
392 		 * Charge the time as appropriate.
393 		 */
394 		if (p->p_flag & P_SA)
395 			thread_statclock(1);
396 		p->p_uticks++;
397 		if (td->td_ksegrp->kg_nice > NZERO)
398 			cp_time[CP_NICE]++;
399 		else
400 			cp_time[CP_USER]++;
401 	} else {
402 		/*
403 		 * Came from kernel mode, so we were:
404 		 * - handling an interrupt,
405 		 * - doing syscall or trap work on behalf of the current
406 		 *   user process, or
407 		 * - spinning in the idle loop.
408 		 * Whichever it is, charge the time as appropriate.
409 		 * Note that we charge interrupts to the current process,
410 		 * regardless of whether they are ``for'' that process,
411 		 * so that we know how much of its real time was spent
412 		 * in ``non-process'' (i.e., interrupt) work.
413 		 */
414 		if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) {
415 			p->p_iticks++;
416 			cp_time[CP_INTR]++;
417 		} else {
418 			if (p->p_flag & P_SA)
419 				thread_statclock(0);
420 			td->td_sticks++;
421 			p->p_sticks++;
422 			if (p != PCPU_GET(idlethread)->td_proc)
423 				cp_time[CP_SYS]++;
424 			else
425 				cp_time[CP_IDLE]++;
426 		}
427 	}
428 
429 	sched_clock(td);
430 
431 	/* Update resource usage integrals and maximums. */
432 	if ((pstats = p->p_stats) != NULL &&
433 	    (ru = &pstats->p_ru) != NULL &&
434 	    (vm = p->p_vmspace) != NULL) {
435 		ru->ru_ixrss += pgtok(vm->vm_tsize);
436 		ru->ru_idrss += pgtok(vm->vm_dsize);
437 		ru->ru_isrss += pgtok(vm->vm_ssize);
438 		rss = pgtok(vmspace_resident_count(vm));
439 		if (ru->ru_maxrss < rss)
440 			ru->ru_maxrss = rss;
441 	}
442 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
443 }
444 
445 void
446 profclock(frame)
447 	register struct clockframe *frame;
448 {
449 	struct thread *td;
450 #ifdef GPROF
451 	struct gmonparam *g;
452 	int i;
453 #endif
454 
455 	td = curthread;
456 	if (CLKF_USERMODE(frame)) {
457 		/*
458 		 * Came from user mode; CPU was in user state.
459 		 * If this process is being profiled, record the tick.
460 		 * if there is no related user location yet, don't
461 		 * bother trying to count it.
462 		 */
463 		td = curthread;
464 		if (td->td_proc->p_flag & P_PROFIL)
465 			addupc_intr(td, CLKF_PC(frame), 1);
466 	}
467 #ifdef GPROF
468 	else {
469 		/*
470 		 * Kernel statistics are just like addupc_intr, only easier.
471 		 */
472 		g = &_gmonparam;
473 		if (g->state == GMON_PROF_ON) {
474 			i = CLKF_PC(frame) - g->lowpc;
475 			if (i < g->textsize) {
476 				i /= HISTFRACTION * sizeof(*g->kcount);
477 				g->kcount[i]++;
478 			}
479 		}
480 	}
481 #endif
482 }
483 
484 /*
485  * Return information about system clocks.
486  */
487 static int
488 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
489 {
490 	struct clockinfo clkinfo;
491 	/*
492 	 * Construct clockinfo structure.
493 	 */
494 	bzero(&clkinfo, sizeof(clkinfo));
495 	clkinfo.hz = hz;
496 	clkinfo.tick = tick;
497 	clkinfo.profhz = profhz;
498 	clkinfo.stathz = stathz ? stathz : hz;
499 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
500 }
501 
502 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
503 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
504 	"Rate and period of various kernel clocks");
505 
506 #ifdef SW_WATCHDOG
507 
508 static void
509 watchdog_config(void *unused __unused, u_int cmd, int *err)
510 {
511 	u_int u;
512 
513 	if (cmd) {
514 		u = cmd & WD_INTERVAL;
515 		if (u < WD_TO_1SEC)
516 			return;
517 		watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz;
518 		watchdog_enabled = 1;
519 		*err = 0;
520 	} else {
521 		watchdog_enabled = 0;
522 	}
523 }
524 
525 /*
526  * Handle a watchdog timeout by dumping interrupt information and
527  * then either dropping to DDB or panicing.
528  */
529 static void
530 watchdog_fire(void)
531 {
532 	int nintr;
533 	u_int64_t inttotal;
534 	u_long *curintr;
535 	char *curname;
536 
537 	curintr = intrcnt;
538 	curname = intrnames;
539 	inttotal = 0;
540 	nintr = eintrcnt - intrcnt;
541 
542 	printf("interrupt                   total\n");
543 	while (--nintr >= 0) {
544 		if (*curintr)
545 			printf("%-12s %20lu\n", curname, *curintr);
546 		curname += strlen(curname) + 1;
547 		inttotal += *curintr++;
548 	}
549 	printf("Total        %20ju\n", (uintmax_t)inttotal);
550 
551 #ifdef DDB
552 	db_print_backtrace();
553 	Debugger("watchdog timeout");
554 #else /* !DDB */
555 	panic("watchdog timeout");
556 #endif /* DDB */
557 }
558 
559 #endif /* SW_WATCHDOG */
560