xref: /freebsd/sys/kern/kern_clock.c (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
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 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/callout.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/ktr.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/resource.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
57 #include <sys/signalvar.h>
58 #include <sys/smp.h>
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62 #include <sys/sysctl.h>
63 #include <sys/bus.h>
64 #include <sys/interrupt.h>
65 #include <sys/limits.h>
66 #include <sys/timetc.h>
67 
68 #include <machine/cpu.h>
69 
70 #ifdef GPROF
71 #include <sys/gmon.h>
72 #endif
73 
74 #ifdef DEVICE_POLLING
75 extern void hardclock_device_poll(void);
76 #endif /* DEVICE_POLLING */
77 
78 static void initclocks(void *dummy);
79 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
80 
81 /* Some of these don't belong here, but it's easiest to concentrate them. */
82 long cp_time[CPUSTATES];
83 
84 SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
85     "LU", "CPU time statistics");
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 int	profprocs;
122 int	ticks;
123 int	psratio;
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 	cpu_initclocks();
140 
141 	/*
142 	 * Compute profhz/stathz, and fix profhz if needed.
143 	 */
144 	i = stathz ? stathz : hz;
145 	if (profhz == 0)
146 		profhz = i;
147 	psratio = profhz / i;
148 }
149 
150 /*
151  * Each time the real-time timer fires, this function is called on all CPUs.
152  * Note that hardclock() calls hardclock_process() for the boot CPU, so only
153  * the other CPUs in the system need to call this function.
154  */
155 void
156 hardclock_process(frame)
157 	register struct clockframe *frame;
158 {
159 	struct pstats *pstats;
160 	struct thread *td = curthread;
161 	struct proc *p = td->td_proc;
162 
163 	/*
164 	 * Run current process's virtual and profile time, as needed.
165 	 */
166 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
167 	if (p->p_flag & P_SA) {
168 		/* XXXKSE What to do? */
169 	} else {
170 		pstats = p->p_stats;
171 		if (CLKF_USERMODE(frame) &&
172 		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
173 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
174 			p->p_sflag |= PS_ALRMPEND;
175 			td->td_flags |= TDF_ASTPENDING;
176 		}
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;
180 			td->td_flags |= TDF_ASTPENDING;
181 		}
182 	}
183 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
184 }
185 
186 /*
187  * The real-time timer, interrupting hz times per second.
188  */
189 void
190 hardclock(frame)
191 	register struct clockframe *frame;
192 {
193 	int need_softclock = 0;
194 
195 	CTR0(KTR_CLK, "hardclock fired");
196 	hardclock_process(frame);
197 
198 	tc_ticktock();
199 	/*
200 	 * If no separate statistics clock is available, run it from here.
201 	 *
202 	 * XXX: this only works for UP
203 	 */
204 	if (stathz == 0) {
205 		profclock(frame);
206 		statclock(frame);
207 	}
208 
209 #ifdef DEVICE_POLLING
210 	hardclock_device_poll();	/* this is very short and quick */
211 #endif /* DEVICE_POLLING */
212 
213 	/*
214 	 * Process callouts at a very low cpu priority, so we don't keep the
215 	 * relatively high clock interrupt priority any longer than necessary.
216 	 */
217 	mtx_lock_spin_flags(&callout_lock, MTX_QUIET);
218 	ticks++;
219 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
220 		need_softclock = 1;
221 	} else if (softticks + 1 == ticks)
222 		++softticks;
223 	mtx_unlock_spin_flags(&callout_lock, MTX_QUIET);
224 
225 	/*
226 	 * swi_sched acquires sched_lock, so we don't want to call it with
227 	 * callout_lock held; incorrect locking order.
228 	 */
229 	if (need_softclock)
230 		swi_sched(softclock_ih, 0);
231 }
232 
233 /*
234  * Compute number of ticks in the specified amount of time.
235  */
236 int
237 tvtohz(tv)
238 	struct timeval *tv;
239 {
240 	register unsigned long ticks;
241 	register long sec, usec;
242 
243 	/*
244 	 * If the number of usecs in the whole seconds part of the time
245 	 * difference fits in a long, then the total number of usecs will
246 	 * fit in an unsigned long.  Compute the total and convert it to
247 	 * ticks, rounding up and adding 1 to allow for the current tick
248 	 * to expire.  Rounding also depends on unsigned long arithmetic
249 	 * to avoid overflow.
250 	 *
251 	 * Otherwise, if the number of ticks in the whole seconds part of
252 	 * the time difference fits in a long, then convert the parts to
253 	 * ticks separately and add, using similar rounding methods and
254 	 * overflow avoidance.  This method would work in the previous
255 	 * case but it is slightly slower and assumes that hz is integral.
256 	 *
257 	 * Otherwise, round the time difference down to the maximum
258 	 * representable value.
259 	 *
260 	 * If ints have 32 bits, then the maximum value for any timeout in
261 	 * 10ms ticks is 248 days.
262 	 */
263 	sec = tv->tv_sec;
264 	usec = tv->tv_usec;
265 	if (usec < 0) {
266 		sec--;
267 		usec += 1000000;
268 	}
269 	if (sec < 0) {
270 #ifdef DIAGNOSTIC
271 		if (usec > 0) {
272 			sec++;
273 			usec -= 1000000;
274 		}
275 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
276 		       sec, usec);
277 #endif
278 		ticks = 1;
279 	} else if (sec <= LONG_MAX / 1000000)
280 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
281 			/ tick + 1;
282 	else if (sec <= LONG_MAX / hz)
283 		ticks = sec * hz
284 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
285 	else
286 		ticks = LONG_MAX;
287 	if (ticks > INT_MAX)
288 		ticks = INT_MAX;
289 	return ((int)ticks);
290 }
291 
292 /*
293  * Start profiling on a process.
294  *
295  * Kernel profiling passes proc0 which never exits and hence
296  * keeps the profile clock running constantly.
297  */
298 void
299 startprofclock(p)
300 	register struct proc *p;
301 {
302 
303 	/*
304 	 * XXX; Right now sched_lock protects statclock(), but perhaps
305 	 * it should be protected later on by a time_lock, which would
306 	 * cover psdiv, etc. as well.
307 	 */
308 	PROC_LOCK_ASSERT(p, MA_OWNED);
309 	if (p->p_flag & P_STOPPROF)
310 		return;
311 	if ((p->p_flag & P_PROFIL) == 0) {
312 		mtx_lock_spin(&sched_lock);
313 		p->p_flag |= P_PROFIL;
314 		if (++profprocs == 1)
315 			cpu_startprofclock();
316 		mtx_unlock_spin(&sched_lock);
317 	}
318 }
319 
320 /*
321  * Stop profiling on a process.
322  */
323 void
324 stopprofclock(p)
325 	register struct proc *p;
326 {
327 
328 	PROC_LOCK_ASSERT(p, MA_OWNED);
329 	if (p->p_flag & P_PROFIL) {
330 		if (p->p_profthreads != 0) {
331 			p->p_flag |= P_STOPPROF;
332 			while (p->p_profthreads != 0)
333 				msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
334 				    "stopprof", NULL);
335 			p->p_flag &= ~P_STOPPROF;
336 		}
337 		mtx_lock_spin(&sched_lock);
338 		p->p_flag &= ~P_PROFIL;
339 		if (--profprocs == 0)
340 			cpu_stopprofclock();
341 		mtx_unlock_spin(&sched_lock);
342 	}
343 }
344 
345 /*
346  * Statistics clock.  Grab profile sample, and if divider reaches 0,
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.
350  * This should be called by all active processors.
351  */
352 void
353 statclock(frame)
354 	register struct clockframe *frame;
355 {
356 	struct pstats *pstats;
357 	struct rusage *ru;
358 	struct vmspace *vm;
359 	struct thread *td;
360 	struct kse *ke;
361 	struct proc *p;
362 	long rss;
363 
364 	td = curthread;
365 	p = td->td_proc;
366 
367 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
368 	ke = td->td_kse;
369 	if (CLKF_USERMODE(frame)) {
370 		/*
371 		 * Charge the time as appropriate.
372 		 */
373 		if (p->p_flag & P_SA)
374 			thread_statclock(1);
375 		p->p_uticks++;
376 		if (ke->ke_ksegrp->kg_nice > NZERO)
377 			cp_time[CP_NICE]++;
378 		else
379 			cp_time[CP_USER]++;
380 	} else {
381 		/*
382 		 * Came from kernel mode, so we were:
383 		 * - handling an interrupt,
384 		 * - doing syscall or trap work on behalf of the current
385 		 *   user process, or
386 		 * - spinning in the idle loop.
387 		 * Whichever it is, charge the time as appropriate.
388 		 * Note that we charge interrupts to the current process,
389 		 * regardless of whether they are ``for'' that process,
390 		 * so that we know how much of its real time was spent
391 		 * in ``non-process'' (i.e., interrupt) work.
392 		 */
393 		if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) {
394 			p->p_iticks++;
395 			cp_time[CP_INTR]++;
396 		} else {
397 			if (p->p_flag & P_SA)
398 				thread_statclock(0);
399 			td->td_sticks++;
400 			p->p_sticks++;
401 			if (p != PCPU_GET(idlethread)->td_proc)
402 				cp_time[CP_SYS]++;
403 			else
404 				cp_time[CP_IDLE]++;
405 		}
406 	}
407 
408 	sched_clock(ke);
409 
410 	/* Update resource usage integrals and maximums. */
411 	if ((pstats = p->p_stats) != NULL &&
412 	    (ru = &pstats->p_ru) != NULL &&
413 	    (vm = p->p_vmspace) != NULL) {
414 		ru->ru_ixrss += pgtok(vm->vm_tsize);
415 		ru->ru_idrss += pgtok(vm->vm_dsize);
416 		ru->ru_isrss += pgtok(vm->vm_ssize);
417 		rss = pgtok(vmspace_resident_count(vm));
418 		if (ru->ru_maxrss < rss)
419 			ru->ru_maxrss = rss;
420 	}
421 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
422 }
423 
424 void
425 profclock(frame)
426 	register struct clockframe *frame;
427 {
428 	struct thread *td;
429 #ifdef GPROF
430 	struct gmonparam *g;
431 	int i;
432 #endif
433 
434 	td = curthread;
435 	if (CLKF_USERMODE(frame)) {
436 		/*
437 		 * Came from user mode; CPU was in user state.
438 		 * If this process is being profiled, record the tick.
439 		 * if there is no related user location yet, don't
440 		 * bother trying to count it.
441 		 */
442 		td = curthread;
443 		if (td->td_proc->p_flag & P_PROFIL)
444 			addupc_intr(td, CLKF_PC(frame), 1);
445 	}
446 #ifdef GPROF
447 	else {
448 		/*
449 		 * Kernel statistics are just like addupc_intr, only easier.
450 		 */
451 		g = &_gmonparam;
452 		if (g->state == GMON_PROF_ON) {
453 			i = CLKF_PC(frame) - g->lowpc;
454 			if (i < g->textsize) {
455 				i /= HISTFRACTION * sizeof(*g->kcount);
456 				g->kcount[i]++;
457 			}
458 		}
459 	}
460 #endif
461 }
462 
463 /*
464  * Return information about system clocks.
465  */
466 static int
467 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
468 {
469 	struct clockinfo clkinfo;
470 	/*
471 	 * Construct clockinfo structure.
472 	 */
473 	bzero(&clkinfo, sizeof(clkinfo));
474 	clkinfo.hz = hz;
475 	clkinfo.tick = tick;
476 	clkinfo.profhz = profhz;
477 	clkinfo.stathz = stathz ? stathz : hz;
478 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
479 }
480 
481 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
482 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
483 	"Rate and period of various kernel clocks");
484