xref: /freebsd/sys/kern/kern_clock.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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/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/timetc.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 #ifdef DEVICE_POLLING
73 extern void hardclock_device_poll(void);
74 #endif /* DEVICE_POLLING */
75 
76 static void initclocks(void *dummy);
77 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
78 
79 /* Some of these don't belong here, but it's easiest to concentrate them. */
80 long cp_time[CPUSTATES];
81 
82 SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
83     "LU", "CPU time statistics");
84 
85 /*
86  * Clock handling routines.
87  *
88  * This code is written to operate with two timers that run independently of
89  * each other.
90  *
91  * The main timer, running hz times per second, is used to trigger interval
92  * timers, timeouts and rescheduling as needed.
93  *
94  * The second timer handles kernel and user profiling,
95  * and does resource use estimation.  If the second timer is programmable,
96  * it is randomized to avoid aliasing between the two clocks.  For example,
97  * the randomization prevents an adversary from always giving up the cpu
98  * just before its quantum expires.  Otherwise, it would never accumulate
99  * cpu ticks.  The mean frequency of the second timer is stathz.
100  *
101  * If no second timer exists, stathz will be zero; in this case we drive
102  * profiling and statistics off the main clock.  This WILL NOT be accurate;
103  * do not do it unless absolutely necessary.
104  *
105  * The statistics clock may (or may not) be run at a higher rate while
106  * profiling.  This profile clock runs at profhz.  We require that profhz
107  * be an integral multiple of stathz.
108  *
109  * If the statistics clock is running fast, it must be divided by the ratio
110  * profhz/stathz for statistics.  (For profiling, every tick counts.)
111  *
112  * Time-of-day is maintained using a "timecounter", which may or may
113  * not be related to the hardware generating the above mentioned
114  * interrupts.
115  */
116 
117 int	stathz;
118 int	profhz;
119 int	profprocs;
120 int	ticks;
121 int	psratio;
122 
123 /*
124  * Initialize clock frequencies and start both clocks running.
125  */
126 /* ARGSUSED*/
127 static void
128 initclocks(dummy)
129 	void *dummy;
130 {
131 	register int i;
132 
133 	/*
134 	 * Set divisors to 1 (normal case) and let the machine-specific
135 	 * code do its bit.
136 	 */
137 	cpu_initclocks();
138 
139 	/*
140 	 * Compute profhz/stathz, and fix profhz if needed.
141 	 */
142 	i = stathz ? stathz : hz;
143 	if (profhz == 0)
144 		profhz = i;
145 	psratio = profhz / i;
146 }
147 
148 /*
149  * Each time the real-time timer fires, this function is called on all CPUs.
150  * Note that hardclock() calls hardclock_process() for the boot CPU, so only
151  * the other CPUs in the system need to call this function.
152  */
153 void
154 hardclock_process(frame)
155 	register struct clockframe *frame;
156 {
157 	struct pstats *pstats;
158 	struct thread *td = curthread;
159 	struct proc *p = td->td_proc;
160 
161 	/*
162 	 * Run current process's virtual and profile time, as needed.
163 	 */
164 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
165 	if (p->p_flag & P_THREADED) {
166 		/* XXXKSE What to do? */
167 	} else {
168 		pstats = p->p_stats;
169 		if (CLKF_USERMODE(frame) &&
170 		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
171 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
172 			p->p_sflag |= PS_ALRMPEND;
173 			td->td_flags |= TDF_ASTPENDING;
174 		}
175 		if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
176 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) {
177 			p->p_sflag |= PS_PROFPEND;
178 			td->td_flags |= TDF_ASTPENDING;
179 		}
180 	}
181 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
182 }
183 
184 /*
185  * The real-time timer, interrupting hz times per second.
186  */
187 void
188 hardclock(frame)
189 	register struct clockframe *frame;
190 {
191 	int need_softclock = 0;
192 
193 	CTR0(KTR_CLK, "hardclock fired");
194 	hardclock_process(frame);
195 
196 	tc_ticktock();
197 	/*
198 	 * If no separate statistics clock is available, run it from here.
199 	 *
200 	 * XXX: this only works for UP
201 	 */
202 	if (stathz == 0) {
203 		profclock(frame);
204 		statclock(frame);
205 	}
206 
207 #ifdef DEVICE_POLLING
208 	hardclock_device_poll();	/* this is very short and quick */
209 #endif /* DEVICE_POLLING */
210 
211 	/*
212 	 * Process callouts at a very low cpu priority, so we don't keep the
213 	 * relatively high clock interrupt priority any longer than necessary.
214 	 */
215 	mtx_lock_spin_flags(&callout_lock, MTX_QUIET);
216 	ticks++;
217 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
218 		need_softclock = 1;
219 	} else if (softticks + 1 == ticks)
220 		++softticks;
221 	mtx_unlock_spin_flags(&callout_lock, MTX_QUIET);
222 
223 	/*
224 	 * swi_sched acquires sched_lock, so we don't want to call it with
225 	 * callout_lock held; incorrect locking order.
226 	 */
227 	if (need_softclock)
228 		swi_sched(softclock_ih, 0);
229 }
230 
231 /*
232  * Compute number of ticks in the specified amount of time.
233  */
234 int
235 tvtohz(tv)
236 	struct timeval *tv;
237 {
238 	register unsigned long ticks;
239 	register long sec, usec;
240 
241 	/*
242 	 * If the number of usecs in the whole seconds part of the time
243 	 * difference fits in a long, then the total number of usecs will
244 	 * fit in an unsigned long.  Compute the total and convert it to
245 	 * ticks, rounding up and adding 1 to allow for the current tick
246 	 * to expire.  Rounding also depends on unsigned long arithmetic
247 	 * to avoid overflow.
248 	 *
249 	 * Otherwise, if the number of ticks in the whole seconds part of
250 	 * the time difference fits in a long, then convert the parts to
251 	 * ticks separately and add, using similar rounding methods and
252 	 * overflow avoidance.  This method would work in the previous
253 	 * case but it is slightly slower and assumes that hz is integral.
254 	 *
255 	 * Otherwise, round the time difference down to the maximum
256 	 * representable value.
257 	 *
258 	 * If ints have 32 bits, then the maximum value for any timeout in
259 	 * 10ms ticks is 248 days.
260 	 */
261 	sec = tv->tv_sec;
262 	usec = tv->tv_usec;
263 	if (usec < 0) {
264 		sec--;
265 		usec += 1000000;
266 	}
267 	if (sec < 0) {
268 #ifdef DIAGNOSTIC
269 		if (usec > 0) {
270 			sec++;
271 			usec -= 1000000;
272 		}
273 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
274 		       sec, usec);
275 #endif
276 		ticks = 1;
277 	} else if (sec <= LONG_MAX / 1000000)
278 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
279 			/ tick + 1;
280 	else if (sec <= LONG_MAX / hz)
281 		ticks = sec * hz
282 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
283 	else
284 		ticks = LONG_MAX;
285 	if (ticks > INT_MAX)
286 		ticks = INT_MAX;
287 	return ((int)ticks);
288 }
289 
290 /*
291  * Start profiling on a process.
292  *
293  * Kernel profiling passes proc0 which never exits and hence
294  * keeps the profile clock running constantly.
295  */
296 void
297 startprofclock(p)
298 	register struct proc *p;
299 {
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_STOPPROF) {
308 		mtx_unlock_spin(&sched_lock);
309 		return;
310 	}
311 	if ((p->p_sflag & PS_PROFIL) == 0) {
312 		p->p_sflag |= PS_PROFIL;
313 		if (++profprocs == 1)
314 			cpu_startprofclock();
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 
327 	PROC_LOCK_ASSERT(p, MA_OWNED);
328 retry:
329 	mtx_lock_spin(&sched_lock);
330 	if (p->p_sflag & PS_PROFIL) {
331 		if (p->p_profthreads) {
332 			p->p_sflag |= PS_STOPPROF;
333 			mtx_unlock_spin(&sched_lock);
334 			msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
335 			       "stopprof", NULL);
336 			goto retry;
337 		}
338 		p->p_sflag &= ~(PS_PROFIL|PS_STOPPROF);
339 		if (--profprocs == 0)
340 			cpu_stopprofclock();
341 	}
342 	mtx_unlock_spin(&sched_lock);
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_THREADED)
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_THREADED)
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(td);
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_sflag & PS_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