xref: /netbsd-src/sys/kern/kern_clock.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: kern_clock.c,v 1.122 2008/04/28 20:24:02 martin 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.122 2008/04/28 20:24:02 martin Exp $");
73 
74 #include "opt_ntp.h"
75 #include "opt_multiprocessor.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 #include <uvm/uvm_extern.h>
94 
95 #ifdef GPROF
96 #include <sys/gmon.h>
97 #endif
98 
99 /*
100  * Clock handling routines.
101  *
102  * This code is written to operate with two timers that run independently of
103  * each other.  The main clock, running hz times per second, is used to keep
104  * track of real time.  The second timer handles kernel and user profiling,
105  * and does resource use estimation.  If the second timer is programmable,
106  * it is randomized to avoid aliasing between the two clocks.  For example,
107  * the randomization prevents an adversary from always giving up the CPU
108  * just before its quantum expires.  Otherwise, it would never accumulate
109  * CPU ticks.  The mean frequency of the second timer is stathz.
110  *
111  * If no second timer exists, stathz will be zero; in this case we drive
112  * profiling and statistics off the main clock.  This WILL NOT be accurate;
113  * do not do it unless absolutely necessary.
114  *
115  * The statistics clock may (or may not) be run at a higher rate while
116  * profiling.  This profile clock runs at profhz.  We require that profhz
117  * be an integral multiple of stathz.
118  *
119  * If the statistics clock is running fast, it must be divided by the ratio
120  * profhz/stathz for statistics.  (For profiling, every tick counts.)
121  */
122 
123 int	stathz;
124 int	profhz;
125 int	profsrc;
126 int	schedhz;
127 int	profprocs;
128 int	hardclock_ticks;
129 static int hardscheddiv; /* hard => sched divider (used if schedhz == 0) */
130 static int psdiv;			/* prof => stat divider */
131 int	psratio;			/* ratio: prof / stat */
132 
133 static u_int get_intr_timecount(struct timecounter *);
134 
135 static struct timecounter intr_timecounter = {
136 	get_intr_timecount,	/* get_timecount */
137 	0,			/* no poll_pps */
138 	~0u,			/* counter_mask */
139 	0,		        /* frequency */
140 	"clockinterrupt",	/* name */
141 	0,			/* quality - minimum implementation level for a clock */
142 	NULL,			/* prev */
143 	NULL,			/* next */
144 };
145 
146 static u_int
147 get_intr_timecount(struct timecounter *tc)
148 {
149 
150 	return (u_int)hardclock_ticks;
151 }
152 
153 /*
154  * Initialize clock frequencies and start both clocks running.
155  */
156 void
157 initclocks(void)
158 {
159 	int i;
160 
161 	/*
162 	 * Set divisors to 1 (normal case) and let the machine-specific
163 	 * code do its bit.
164 	 */
165 	psdiv = 1;
166 	/*
167 	 * provide minimum default time counter
168 	 * will only run at interrupt resolution
169 	 */
170 	intr_timecounter.tc_frequency = hz;
171 	tc_init(&intr_timecounter);
172 	cpu_initclocks();
173 
174 	/*
175 	 * Compute profhz and stathz, fix profhz if needed.
176 	 */
177 	i = stathz ? stathz : hz;
178 	if (profhz == 0)
179 		profhz = i;
180 	psratio = profhz / i;
181 	if (schedhz == 0) {
182 		/* 16Hz is best */
183 		hardscheddiv = hz / 16;
184 		if (hardscheddiv <= 0)
185 			panic("hardscheddiv");
186 	}
187 
188 }
189 
190 /*
191  * The real-time timer, interrupting hz times per second.
192  */
193 void
194 hardclock(struct clockframe *frame)
195 {
196 	struct lwp *l;
197 	struct cpu_info *ci;
198 
199 	ci = curcpu();
200 	l = ci->ci_data.cpu_onproc;
201 
202 	timer_tick(l, CLKF_USERMODE(frame));
203 
204 	/*
205 	 * If no separate statistics clock is available, run it from here.
206 	 */
207 	if (stathz == 0)
208 		statclock(frame);
209 	/*
210 	 * If no separate schedclock is provided, call it here
211 	 * at about 16 Hz.
212 	 */
213 	if (schedhz == 0) {
214 		if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
215 			schedclock(l);
216 			ci->ci_schedstate.spc_schedticks = hardscheddiv;
217 		}
218 	}
219 	if ((--ci->ci_schedstate.spc_ticks) <= 0)
220 		sched_tick(ci);
221 
222 #if defined(MULTIPROCESSOR)
223 	if (CPU_IS_PRIMARY(ci))
224 #endif
225 	{
226 		hardclock_ticks++;
227 		tc_ticktock();
228 	}
229 
230 	/*
231 	 * Update real-time timeout queue.  Callouts are processed at a
232 	 * very low CPU priority, so we don't keep the relatively high
233 	 * clock interrupt priority any longer than necessary.
234 	 */
235 	callout_hardclock();
236 }
237 
238 /*
239  * Start profiling on a process.
240  *
241  * Kernel profiling passes proc0 which never exits and hence
242  * keeps the profile clock running constantly.
243  */
244 void
245 startprofclock(struct proc *p)
246 {
247 
248 	KASSERT(mutex_owned(&p->p_stmutex));
249 
250 	if ((p->p_stflag & PST_PROFIL) == 0) {
251 		p->p_stflag |= PST_PROFIL;
252 		/*
253 		 * This is only necessary if using the clock as the
254 		 * profiling source.
255 		 */
256 		if (++profprocs == 1 && stathz != 0)
257 			psdiv = psratio;
258 	}
259 }
260 
261 /*
262  * Stop profiling on a process.
263  */
264 void
265 stopprofclock(struct proc *p)
266 {
267 
268 	KASSERT(mutex_owned(&p->p_stmutex));
269 
270 	if (p->p_stflag & PST_PROFIL) {
271 		p->p_stflag &= ~PST_PROFIL;
272 		/*
273 		 * This is only necessary if using the clock as the
274 		 * profiling source.
275 		 */
276 		if (--profprocs == 0 && stathz != 0)
277 			psdiv = 1;
278 	}
279 }
280 
281 #if defined(PERFCTRS)
282 /*
283  * Independent profiling "tick" in case we're using a separate
284  * clock or profiling event source.  Currently, that's just
285  * performance counters--hence the wrapper.
286  */
287 void
288 proftick(struct clockframe *frame)
289 {
290 #ifdef GPROF
291         struct gmonparam *g;
292         intptr_t i;
293 #endif
294 	struct lwp *l;
295 	struct proc *p;
296 
297 	l = curcpu()->ci_data.cpu_onproc;
298 	p = (l ? l->l_proc : NULL);
299 	if (CLKF_USERMODE(frame)) {
300 		mutex_spin_enter(&p->p_stmutex);
301 		if (p->p_stflag & PST_PROFIL)
302 			addupc_intr(l, CLKF_PC(frame));
303 		mutex_spin_exit(&p->p_stmutex);
304 	} else {
305 #ifdef GPROF
306 		g = &_gmonparam;
307 		if (g->state == GMON_PROF_ON) {
308 			i = CLKF_PC(frame) - g->lowpc;
309 			if (i < g->textsize) {
310 				i /= HISTFRACTION * sizeof(*g->kcount);
311 				g->kcount[i]++;
312 			}
313 		}
314 #endif
315 #ifdef LWP_PC
316 		if (p != NULL && (p->p_stflag & PST_PROFIL) != 0)
317 			addupc_intr(l, LWP_PC(l));
318 #endif
319 	}
320 }
321 #endif
322 
323 void
324 schedclock(struct lwp *l)
325 {
326 	struct cpu_info *ci;
327 
328 	ci = l->l_cpu;
329 
330 	/* Accumulate syscall and context switch counts. */
331 	atomic_add_int((unsigned *)&uvmexp.swtch, ci->ci_data.cpu_nswtch);
332 	ci->ci_data.cpu_nswtch = 0;
333 	atomic_add_int((unsigned *)&uvmexp.syscalls, ci->ci_data.cpu_nsyscall);
334 	ci->ci_data.cpu_nsyscall = 0;
335 
336 	if ((l->l_flag & LW_IDLE) != 0)
337 		return;
338 
339 	sched_schedclock(l);
340 }
341 
342 /*
343  * Statistics clock.  Grab profile sample, and if divider reaches 0,
344  * do process and kernel statistics.
345  */
346 void
347 statclock(struct clockframe *frame)
348 {
349 #ifdef GPROF
350 	struct gmonparam *g;
351 	intptr_t i;
352 #endif
353 	struct cpu_info *ci = curcpu();
354 	struct schedstate_percpu *spc = &ci->ci_schedstate;
355 	struct proc *p;
356 	struct lwp *l;
357 
358 	/*
359 	 * Notice changes in divisor frequency, and adjust clock
360 	 * frequency accordingly.
361 	 */
362 	if (spc->spc_psdiv != psdiv) {
363 		spc->spc_psdiv = psdiv;
364 		spc->spc_pscnt = psdiv;
365 		if (psdiv == 1) {
366 			setstatclockrate(stathz);
367 		} else {
368 			setstatclockrate(profhz);
369 		}
370 	}
371 	l = ci->ci_data.cpu_onproc;
372 	if ((l->l_flag & LW_IDLE) != 0) {
373 		/*
374 		 * don't account idle lwps as swapper.
375 		 */
376 		p = NULL;
377 	} else {
378 		p = l->l_proc;
379 		mutex_spin_enter(&p->p_stmutex);
380 	}
381 
382 	if (CLKF_USERMODE(frame)) {
383 		if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
384 			addupc_intr(l, CLKF_PC(frame));
385 		if (--spc->spc_pscnt > 0) {
386 			mutex_spin_exit(&p->p_stmutex);
387 			return;
388 		}
389 
390 		/*
391 		 * Came from user mode; CPU was in user state.
392 		 * If this process is being profiled record the tick.
393 		 */
394 		p->p_uticks++;
395 		if (p->p_nice > NZERO)
396 			spc->spc_cp_time[CP_NICE]++;
397 		else
398 			spc->spc_cp_time[CP_USER]++;
399 	} else {
400 #ifdef GPROF
401 		/*
402 		 * Kernel statistics are just like addupc_intr, only easier.
403 		 */
404 		g = &_gmonparam;
405 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
406 			i = CLKF_PC(frame) - g->lowpc;
407 			if (i < g->textsize) {
408 				i /= HISTFRACTION * sizeof(*g->kcount);
409 				g->kcount[i]++;
410 			}
411 		}
412 #endif
413 #ifdef LWP_PC
414 		if (p != NULL && profsrc == PROFSRC_CLOCK &&
415 		    (p->p_stflag & PST_PROFIL)) {
416 			addupc_intr(l, LWP_PC(l));
417 		}
418 #endif
419 		if (--spc->spc_pscnt > 0) {
420 			if (p != NULL)
421 				mutex_spin_exit(&p->p_stmutex);
422 			return;
423 		}
424 		/*
425 		 * Came from kernel mode, so we were:
426 		 * - handling an interrupt,
427 		 * - doing syscall or trap work on behalf of the current
428 		 *   user process, or
429 		 * - spinning in the idle loop.
430 		 * Whichever it is, charge the time as appropriate.
431 		 * Note that we charge interrupts to the current process,
432 		 * regardless of whether they are ``for'' that process,
433 		 * so that we know how much of its real time was spent
434 		 * in ``non-process'' (i.e., interrupt) work.
435 		 */
436 		if (CLKF_INTR(frame) || (curlwp->l_pflag & LP_INTR) != 0) {
437 			if (p != NULL) {
438 				p->p_iticks++;
439 			}
440 			spc->spc_cp_time[CP_INTR]++;
441 		} else if (p != NULL) {
442 			p->p_sticks++;
443 			spc->spc_cp_time[CP_SYS]++;
444 		} else {
445 			spc->spc_cp_time[CP_IDLE]++;
446 		}
447 	}
448 	spc->spc_pscnt = psdiv;
449 
450 	if (p != NULL) {
451 		++l->l_cpticks;
452 		mutex_spin_exit(&p->p_stmutex);
453 	}
454 }
455