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