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