xref: /netbsd-src/sys/kern/kern_clock.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: kern_clock.c,v 1.131 2012/12/02 01:05:16 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Charles M. Hannum.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1982, 1986, 1991, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  * (c) UNIX System Laboratories, Inc.
39  * All or some portions of this file are derived from material licensed
40  * to the University of California by American Telephone and Telegraph
41  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
42  * the permission of UNIX System Laboratories, Inc.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.131 2012/12/02 01:05:16 chs Exp $");
73 
74 #include "opt_dtrace.h"
75 #include "opt_ntp.h"
76 #include "opt_perfctrs.h"
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/callout.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/resourcevar.h>
84 #include <sys/signalvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/timex.h>
87 #include <sys/sched.h>
88 #include <sys/time.h>
89 #include <sys/timetc.h>
90 #include <sys/cpu.h>
91 #include <sys/atomic.h>
92 
93 #ifdef GPROF
94 #include <sys/gmon.h>
95 #endif
96 
97 #ifdef KDTRACE_HOOKS
98 #include <sys/dtrace_bsd.h>
99 #include <sys/cpu.h>
100 
101 cyclic_clock_func_t	cyclic_clock_func[MAXCPUS];
102 #endif
103 
104 /*
105  * Clock handling routines.
106  *
107  * This code is written to operate with two timers that run independently of
108  * each other.  The main clock, running hz times per second, is used to keep
109  * track of real time.  The second timer handles kernel and user profiling,
110  * and does resource use estimation.  If the second timer is programmable,
111  * it is randomized to avoid aliasing between the two clocks.  For example,
112  * the randomization prevents an adversary from always giving up the CPU
113  * just before its quantum expires.  Otherwise, it would never accumulate
114  * CPU ticks.  The mean frequency of the second timer is stathz.
115  *
116  * If no second timer exists, stathz will be zero; in this case we drive
117  * profiling and statistics off the main clock.  This WILL NOT be accurate;
118  * do not do it unless absolutely necessary.
119  *
120  * The statistics clock may (or may not) be run at a higher rate while
121  * profiling.  This profile clock runs at profhz.  We require that profhz
122  * be an integral multiple of stathz.
123  *
124  * If the statistics clock is running fast, it must be divided by the ratio
125  * profhz/stathz for statistics.  (For profiling, every tick counts.)
126  */
127 
128 int	stathz;
129 int	profhz;
130 int	profsrc;
131 int	schedhz;
132 int	profprocs;
133 int	hardclock_ticks;
134 static int hardscheddiv; /* hard => sched divider (used if schedhz == 0) */
135 static int psdiv;			/* prof => stat divider */
136 int	psratio;			/* ratio: prof / stat */
137 
138 static u_int get_intr_timecount(struct timecounter *);
139 
140 static struct timecounter intr_timecounter = {
141 	get_intr_timecount,	/* get_timecount */
142 	0,			/* no poll_pps */
143 	~0u,			/* counter_mask */
144 	0,		        /* frequency */
145 	"clockinterrupt",	/* name */
146 	0,			/* quality - minimum implementation level for a clock */
147 	NULL,			/* prev */
148 	NULL,			/* next */
149 };
150 
151 static u_int
152 get_intr_timecount(struct timecounter *tc)
153 {
154 
155 	return (u_int)hardclock_ticks;
156 }
157 
158 /*
159  * Initialize clock frequencies and start both clocks running.
160  */
161 void
162 initclocks(void)
163 {
164 	int i;
165 
166 	/*
167 	 * Set divisors to 1 (normal case) and let the machine-specific
168 	 * code do its bit.
169 	 */
170 	psdiv = 1;
171 	/*
172 	 * provide minimum default time counter
173 	 * will only run at interrupt resolution
174 	 */
175 	intr_timecounter.tc_frequency = hz;
176 	tc_init(&intr_timecounter);
177 	cpu_initclocks();
178 
179 	/*
180 	 * Compute profhz and stathz, fix profhz if needed.
181 	 */
182 	i = stathz ? stathz : hz;
183 	if (profhz == 0)
184 		profhz = i;
185 	psratio = profhz / i;
186 	if (schedhz == 0) {
187 		/* 16Hz is best */
188 		hardscheddiv = hz / 16;
189 		if (hardscheddiv <= 0)
190 			panic("hardscheddiv");
191 	}
192 
193 }
194 
195 /*
196  * The real-time timer, interrupting hz times per second.
197  */
198 void
199 hardclock(struct clockframe *frame)
200 {
201 	struct lwp *l;
202 	struct cpu_info *ci;
203 
204 	ci = curcpu();
205 	l = ci->ci_data.cpu_onproc;
206 
207 	timer_tick(l, CLKF_USERMODE(frame));
208 
209 	/*
210 	 * If no separate statistics clock is available, run it from here.
211 	 */
212 	if (stathz == 0)
213 		statclock(frame);
214 	/*
215 	 * If no separate schedclock is provided, call it here
216 	 * at about 16 Hz.
217 	 */
218 	if (schedhz == 0) {
219 		if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
220 			schedclock(l);
221 			ci->ci_schedstate.spc_schedticks = hardscheddiv;
222 		}
223 	}
224 	if ((--ci->ci_schedstate.spc_ticks) <= 0)
225 		sched_tick(ci);
226 
227 	if (CPU_IS_PRIMARY(ci)) {
228 		hardclock_ticks++;
229 		tc_ticktock();
230 	}
231 
232 	/*
233 	 * Update real-time timeout queue.
234 	 */
235 	callout_hardclock();
236 
237 #ifdef KDTRACE_HOOKS
238 	cyclic_clock_func_t func = cyclic_clock_func[cpu_index(ci)];
239 	if (func) {
240 		(*func)((struct clockframe *)frame);
241 	}
242 #endif
243 }
244 
245 /*
246  * Start profiling on a process.
247  *
248  * Kernel profiling passes proc0 which never exits and hence
249  * keeps the profile clock running constantly.
250  */
251 void
252 startprofclock(struct proc *p)
253 {
254 
255 	KASSERT(mutex_owned(&p->p_stmutex));
256 
257 	if ((p->p_stflag & PST_PROFIL) == 0) {
258 		p->p_stflag |= PST_PROFIL;
259 		/*
260 		 * This is only necessary if using the clock as the
261 		 * profiling source.
262 		 */
263 		if (++profprocs == 1 && stathz != 0)
264 			psdiv = psratio;
265 	}
266 }
267 
268 /*
269  * Stop profiling on a process.
270  */
271 void
272 stopprofclock(struct proc *p)
273 {
274 
275 	KASSERT(mutex_owned(&p->p_stmutex));
276 
277 	if (p->p_stflag & PST_PROFIL) {
278 		p->p_stflag &= ~PST_PROFIL;
279 		/*
280 		 * This is only necessary if using the clock as the
281 		 * profiling source.
282 		 */
283 		if (--profprocs == 0 && stathz != 0)
284 			psdiv = 1;
285 	}
286 }
287 
288 #if defined(PERFCTRS)
289 /*
290  * Independent profiling "tick" in case we're using a separate
291  * clock or profiling event source.  Currently, that's just
292  * performance counters--hence the wrapper.
293  */
294 void
295 proftick(struct clockframe *frame)
296 {
297 #ifdef GPROF
298         struct gmonparam *g;
299         intptr_t i;
300 #endif
301 	struct lwp *l;
302 	struct proc *p;
303 
304 	l = curcpu()->ci_data.cpu_onproc;
305 	p = (l ? l->l_proc : NULL);
306 	if (CLKF_USERMODE(frame)) {
307 		mutex_spin_enter(&p->p_stmutex);
308 		if (p->p_stflag & PST_PROFIL)
309 			addupc_intr(l, CLKF_PC(frame));
310 		mutex_spin_exit(&p->p_stmutex);
311 	} else {
312 #ifdef GPROF
313 		g = &_gmonparam;
314 		if (g->state == GMON_PROF_ON) {
315 			i = CLKF_PC(frame) - g->lowpc;
316 			if (i < g->textsize) {
317 				i /= HISTFRACTION * sizeof(*g->kcount);
318 				g->kcount[i]++;
319 			}
320 		}
321 #endif
322 #ifdef LWP_PC
323 		if (p != NULL && (p->p_stflag & PST_PROFIL) != 0)
324 			addupc_intr(l, LWP_PC(l));
325 #endif
326 	}
327 }
328 #endif
329 
330 void
331 schedclock(struct lwp *l)
332 {
333 	if ((l->l_flag & LW_IDLE) != 0)
334 		return;
335 
336 	sched_schedclock(l);
337 }
338 
339 /*
340  * Statistics clock.  Grab profile sample, and if divider reaches 0,
341  * do process and kernel statistics.
342  */
343 void
344 statclock(struct clockframe *frame)
345 {
346 #ifdef GPROF
347 	struct gmonparam *g;
348 	intptr_t i;
349 #endif
350 	struct cpu_info *ci = curcpu();
351 	struct schedstate_percpu *spc = &ci->ci_schedstate;
352 	struct proc *p;
353 	struct lwp *l;
354 
355 	/*
356 	 * Notice changes in divisor frequency, and adjust clock
357 	 * frequency accordingly.
358 	 */
359 	if (spc->spc_psdiv != psdiv) {
360 		spc->spc_psdiv = psdiv;
361 		spc->spc_pscnt = psdiv;
362 		if (psdiv == 1) {
363 			setstatclockrate(stathz);
364 		} else {
365 			setstatclockrate(profhz);
366 		}
367 	}
368 	l = ci->ci_data.cpu_onproc;
369 	if ((l->l_flag & LW_IDLE) != 0) {
370 		/*
371 		 * don't account idle lwps as swapper.
372 		 */
373 		p = NULL;
374 	} else {
375 		p = l->l_proc;
376 		mutex_spin_enter(&p->p_stmutex);
377 	}
378 
379 	if (CLKF_USERMODE(frame)) {
380 		if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
381 			addupc_intr(l, CLKF_PC(frame));
382 		if (--spc->spc_pscnt > 0) {
383 			mutex_spin_exit(&p->p_stmutex);
384 			return;
385 		}
386 
387 		/*
388 		 * Came from user mode; CPU was in user state.
389 		 * If this process is being profiled record the tick.
390 		 */
391 		p->p_uticks++;
392 		if (p->p_nice > NZERO)
393 			spc->spc_cp_time[CP_NICE]++;
394 		else
395 			spc->spc_cp_time[CP_USER]++;
396 	} else {
397 #ifdef GPROF
398 		/*
399 		 * Kernel statistics are just like addupc_intr, only easier.
400 		 */
401 		g = &_gmonparam;
402 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
403 			i = CLKF_PC(frame) - g->lowpc;
404 			if (i < g->textsize) {
405 				i /= HISTFRACTION * sizeof(*g->kcount);
406 				g->kcount[i]++;
407 			}
408 		}
409 #endif
410 #ifdef LWP_PC
411 		if (p != NULL && profsrc == PROFSRC_CLOCK &&
412 		    (p->p_stflag & PST_PROFIL)) {
413 			addupc_intr(l, LWP_PC(l));
414 		}
415 #endif
416 		if (--spc->spc_pscnt > 0) {
417 			if (p != NULL)
418 				mutex_spin_exit(&p->p_stmutex);
419 			return;
420 		}
421 		/*
422 		 * Came from kernel mode, so we were:
423 		 * - handling an interrupt,
424 		 * - doing syscall or trap work on behalf of the current
425 		 *   user process, or
426 		 * - spinning in the idle loop.
427 		 * Whichever it is, charge the time as appropriate.
428 		 * Note that we charge interrupts to the current process,
429 		 * regardless of whether they are ``for'' that process,
430 		 * so that we know how much of its real time was spent
431 		 * in ``non-process'' (i.e., interrupt) work.
432 		 */
433 		if (CLKF_INTR(frame) || (curlwp->l_pflag & LP_INTR) != 0) {
434 			if (p != NULL) {
435 				p->p_iticks++;
436 			}
437 			spc->spc_cp_time[CP_INTR]++;
438 		} else if (p != NULL) {
439 			p->p_sticks++;
440 			spc->spc_cp_time[CP_SYS]++;
441 		} else {
442 			spc->spc_cp_time[CP_IDLE]++;
443 		}
444 	}
445 	spc->spc_pscnt = psdiv;
446 
447 	if (p != NULL) {
448 		atomic_inc_uint(&l->l_cpticks);
449 		mutex_spin_exit(&p->p_stmutex);
450 	}
451 }
452