1 | /* |
2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License |
8 | * Version 2.0 (the 'License'). You may not use this file except in |
9 | * compliance with the License. The rights granted to you under the License |
10 | * may not be used to create, or enable the creation or redistribution of, |
11 | * unlawful or unlicensed copies of an Apple operating system, or to |
12 | * circumvent, violate, or enable the circumvention or violation of, any |
13 | * terms of an Apple operating system software license agreement. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and |
24 | * limitations under the License. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ |
29 | /*- |
30 | * Copyright (c) 1982, 1986, 1991, 1993 |
31 | * The Regents of the University of California. All rights reserved. |
32 | * (c) UNIX System Laboratories, Inc. |
33 | * All or some portions of this file are derived from material licensed |
34 | * to the University of California by American Telephone and Telegraph |
35 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
36 | * the permission of UNIX System Laboratories, Inc. |
37 | * |
38 | * Redistribution and use in source and binary forms, with or without |
39 | * modification, are permitted provided that the following conditions |
40 | * are met: |
41 | * 1. Redistributions of source code must retain the above copyright |
42 | * notice, this list of conditions and the following disclaimer. |
43 | * 2. Redistributions in binary form must reproduce the above copyright |
44 | * notice, this list of conditions and the following disclaimer in the |
45 | * documentation and/or other materials provided with the distribution. |
46 | * 3. All advertising materials mentioning features or use of this software |
47 | * must display the following acknowledgement: |
48 | * This product includes software developed by the University of |
49 | * California, Berkeley and its contributors. |
50 | * 4. Neither the name of the University nor the names of its contributors |
51 | * may be used to endorse or promote products derived from this software |
52 | * without specific prior written permission. |
53 | * |
54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
64 | * SUCH DAMAGE. |
65 | * |
66 | * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 |
67 | */ |
68 | /* |
69 | * HISTORY |
70 | */ |
71 | |
72 | #include <sys/param.h> |
73 | #include <sys/systm.h> |
74 | #include <sys/time.h> |
75 | #include <sys/resourcevar.h> |
76 | #include <sys/kernel.h> |
77 | #include <sys/resource.h> |
78 | #include <sys/proc_internal.h> |
79 | #include <sys/vm.h> |
80 | #include <sys/sysctl.h> |
81 | |
82 | #ifdef GPROF |
83 | #include <sys/gmon.h> |
84 | #endif |
85 | |
86 | #include <kern/thread.h> |
87 | #include <kern/ast.h> |
88 | #include <kern/assert.h> |
89 | #include <mach/boolean.h> |
90 | |
91 | #include <kern/thread_call.h> |
92 | |
93 | void bsd_uprofil(struct time_value *syst, user_addr_t pc); |
94 | int tvtohz(struct timeval *tv); |
95 | |
96 | /* |
97 | * Clock handling routines. |
98 | * |
99 | * This code is written to operate with two timers which run |
100 | * independently of each other. The main clock, running at hz |
101 | * times per second, is used to do scheduling and timeout calculations. |
102 | * The second timer does resource utilization estimation statistically |
103 | * based on the state of the machine phz times a second. Both functions |
104 | * can be performed by a single clock (ie hz == phz), however the |
105 | * statistics will be much more prone to errors. Ideally a machine |
106 | * would have separate clocks measuring time spent in user state, system |
107 | * state, interrupt state, and idle state. These clocks would allow a non- |
108 | * approximate measure of resource utilization. |
109 | */ |
110 | |
111 | /* |
112 | * The hz hardware interval timer. |
113 | */ |
114 | |
115 | int hz = 100; /* GET RID OF THIS !!! */ |
116 | int tick = (1000000 / 100); /* GET RID OF THIS !!! */ |
117 | |
118 | /* |
119 | * Kernel timeout services. |
120 | */ |
121 | |
122 | /* |
123 | * Set a timeout. |
124 | * |
125 | * fcn: function to call |
126 | * param: parameter to pass to function |
127 | * interval: timeout interval, in hz. |
128 | */ |
129 | void |
130 | timeout( |
131 | timeout_fcn_t fcn, |
132 | void *param, |
133 | int interval) |
134 | { |
135 | uint64_t deadline; |
136 | |
137 | clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline); |
138 | thread_call_func_delayed((thread_call_func_t)fcn, param, deadline); |
139 | } |
140 | |
141 | /* |
142 | * Set a timeout with leeway. |
143 | * |
144 | * fcn: function to call |
145 | * param: parameter to pass to function |
146 | * interval: timeout interval, in hz. |
147 | * leeway_interval: leeway interval, in hz. |
148 | */ |
149 | void |
150 | timeout_with_leeway( |
151 | timeout_fcn_t fcn, |
152 | void *param, |
153 | int interval, |
154 | int leeway_interval) |
155 | { |
156 | uint64_t deadline; |
157 | uint64_t leeway; |
158 | |
159 | clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline); |
160 | |
161 | clock_interval_to_absolutetime_interval(leeway_interval, NSEC_PER_SEC / hz, &leeway); |
162 | |
163 | thread_call_func_delayed_with_leeway((thread_call_func_t)fcn, param, deadline, leeway, THREAD_CALL_DELAY_LEEWAY); |
164 | } |
165 | |
166 | /* |
167 | * Cancel a timeout. |
168 | * Deprecated because it's very inefficient. |
169 | * Switch to an allocated thread call instead. |
170 | */ |
171 | void |
172 | untimeout( |
173 | timeout_fcn_t fcn, |
174 | void *param) |
175 | { |
176 | thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE); |
177 | } |
178 | |
179 | |
180 | /* |
181 | * Set a timeout. |
182 | * |
183 | * fcn: function to call |
184 | * param: parameter to pass to function |
185 | * ts: timeout interval, in timespec |
186 | */ |
187 | void |
188 | bsd_timeout( |
189 | timeout_fcn_t fcn, |
190 | void *param, |
191 | struct timespec *ts) |
192 | { |
193 | uint64_t deadline = 0; |
194 | |
195 | if (ts && (ts->tv_sec || ts->tv_nsec)) { |
196 | nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec, &deadline ); |
197 | clock_absolutetime_interval_to_deadline( deadline, &deadline ); |
198 | } |
199 | thread_call_func_delayed((thread_call_func_t)fcn, param, deadline); |
200 | } |
201 | |
202 | /* |
203 | * Cancel a timeout. |
204 | * Deprecated because it's very inefficient. |
205 | * Switch to an allocated thread call instead. |
206 | */ |
207 | void |
208 | bsd_untimeout( |
209 | timeout_fcn_t fcn, |
210 | void *param) |
211 | { |
212 | thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE); |
213 | } |
214 | |
215 | |
216 | /* |
217 | * Compute number of hz until specified time. |
218 | * Used to compute third argument to timeout() from an |
219 | * absolute time. |
220 | */ |
221 | int |
222 | hzto(struct timeval *tv) |
223 | { |
224 | struct timeval now; |
225 | long ticks; |
226 | long sec; |
227 | |
228 | microtime(&now); |
229 | /* |
230 | * If number of milliseconds will fit in 32 bit arithmetic, |
231 | * then compute number of milliseconds to time and scale to |
232 | * ticks. Otherwise just compute number of hz in time, rounding |
233 | * times greater than representible to maximum value. |
234 | * |
235 | * Delta times less than 25 days can be computed ``exactly''. |
236 | * Maximum value for any timeout in 10ms ticks is 250 days. |
237 | */ |
238 | sec = tv->tv_sec - now.tv_sec; |
239 | if (sec <= 0x7fffffff / 1000 - 1000) |
240 | ticks = ((tv->tv_sec - now.tv_sec) * 1000 + |
241 | (tv->tv_usec - now.tv_usec) / 1000) |
242 | / (tick / 1000); |
243 | else if (sec <= 0x7fffffff / hz) |
244 | ticks = sec * hz; |
245 | else |
246 | ticks = 0x7fffffff; |
247 | |
248 | return (ticks); |
249 | } |
250 | |
251 | /* |
252 | * Return information about system clocks. |
253 | */ |
254 | static int |
255 | sysctl_clockrate |
256 | (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req) |
257 | { |
258 | struct clockinfo clkinfo = { |
259 | .hz = hz, |
260 | .tick = tick, |
261 | .tickadj = 0, |
262 | .stathz = hz, |
263 | .profhz = hz, |
264 | }; |
265 | |
266 | return sysctl_io_opaque(req, &clkinfo, sizeof(clkinfo), NULL); |
267 | } |
268 | |
269 | SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, |
270 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
271 | 0, 0, sysctl_clockrate, "S,clockinfo" , "" ); |
272 | |
273 | |
274 | /* |
275 | * Compute number of ticks in the specified amount of time. |
276 | */ |
277 | int |
278 | tvtohz(struct timeval *tv) |
279 | { |
280 | unsigned long ticks; |
281 | long sec, usec; |
282 | |
283 | /* |
284 | * If the number of usecs in the whole seconds part of the time |
285 | * difference fits in a long, then the total number of usecs will |
286 | * fit in an unsigned long. Compute the total and convert it to |
287 | * ticks, rounding up and adding 1 to allow for the current tick |
288 | * to expire. Rounding also depends on unsigned long arithmetic |
289 | * to avoid overflow. |
290 | * |
291 | * Otherwise, if the number of ticks in the whole seconds part of |
292 | * the time difference fits in a long, then convert the parts to |
293 | * ticks separately and add, using similar rounding methods and |
294 | * overflow avoidance. This method would work in the previous |
295 | * case but it is slightly slower and assumes that hz is integral. |
296 | * |
297 | * Otherwise, round the time difference down to the maximum |
298 | * representable value. |
299 | * |
300 | * If ints have 32 bits, then the maximum value for any timeout in |
301 | * 10ms ticks is 248 days. |
302 | */ |
303 | sec = tv->tv_sec; |
304 | usec = tv->tv_usec; |
305 | if (usec < 0) { |
306 | sec--; |
307 | usec += 1000000; |
308 | } |
309 | if (sec < 0) { |
310 | #ifdef DIAGNOSTIC |
311 | if (usec > 0) { |
312 | sec++; |
313 | usec -= 1000000; |
314 | } |
315 | printf("tvotohz: negative time difference %ld sec %ld usec\n" , |
316 | sec, usec); |
317 | #endif |
318 | ticks = 1; |
319 | } else if (sec <= LONG_MAX / 1000000) |
320 | ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) |
321 | / tick + 1; |
322 | else if (sec <= LONG_MAX / hz) |
323 | ticks = sec * hz |
324 | + ((unsigned long)usec + (tick - 1)) / tick + 1; |
325 | else |
326 | ticks = LONG_MAX; |
327 | if (ticks > INT_MAX) |
328 | ticks = INT_MAX; |
329 | return ((int)ticks); |
330 | } |
331 | |
332 | |
333 | /* |
334 | * Start profiling on a process. |
335 | * |
336 | * Kernel profiling passes kernel_proc which never exits and hence |
337 | * keeps the profile clock running constantly. |
338 | */ |
339 | void |
340 | startprofclock(struct proc *p) |
341 | { |
342 | if ((p->p_flag & P_PROFIL) == 0) |
343 | OSBitOrAtomic(P_PROFIL, &p->p_flag); |
344 | } |
345 | |
346 | /* |
347 | * Stop profiling on a process. |
348 | */ |
349 | void |
350 | stopprofclock(struct proc *p) |
351 | { |
352 | if (p->p_flag & P_PROFIL) |
353 | OSBitAndAtomic(~((uint32_t)P_PROFIL), &p->p_flag); |
354 | } |
355 | |
356 | /* TBD locking user profiling is not resolved yet */ |
357 | void |
358 | bsd_uprofil(struct time_value *syst, user_addr_t pc) |
359 | { |
360 | struct proc *p = current_proc(); |
361 | int ticks; |
362 | struct timeval *tv; |
363 | struct timeval st; |
364 | |
365 | if (p == NULL) |
366 | return; |
367 | if ( !(p->p_flag & P_PROFIL)) |
368 | return; |
369 | |
370 | st.tv_sec = syst->seconds; |
371 | st.tv_usec = syst->microseconds; |
372 | |
373 | tv = &(p->p_stats->p_ru.ru_stime); |
374 | |
375 | ticks = ((tv->tv_sec - st.tv_sec) * 1000 + |
376 | (tv->tv_usec - st.tv_usec) / 1000) / |
377 | (tick / 1000); |
378 | if (ticks) |
379 | addupc_task(p, pc, ticks); |
380 | } |
381 | |
382 | /* TBD locking user profiling is not resolved yet */ |
383 | void |
384 | get_procrustime(time_value_t *tv) |
385 | { |
386 | struct proc *p = current_proc(); |
387 | struct timeval st; |
388 | |
389 | if (p == NULL) |
390 | return; |
391 | if ( !(p->p_flag & P_PROFIL)) |
392 | return; |
393 | |
394 | //proc_lock(p); |
395 | st = p->p_stats->p_ru.ru_stime; |
396 | //proc_unlock(p); |
397 | |
398 | tv->seconds = st.tv_sec; |
399 | tv->microseconds = st.tv_usec; |
400 | } |
401 | |