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