]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2018 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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 | ||
1c79356b A |
72 | #include <sys/param.h> |
73 | #include <sys/systm.h> | |
74 | #include <sys/time.h> | |
1c79356b A |
75 | #include <sys/resourcevar.h> |
76 | #include <sys/kernel.h> | |
77 | #include <sys/resource.h> | |
91447636 | 78 | #include <sys/proc_internal.h> |
1c79356b | 79 | #include <sys/vm.h> |
91447636 | 80 | #include <sys/sysctl.h> |
1c79356b | 81 | |
1c79356b A |
82 | #include <kern/thread.h> |
83 | #include <kern/ast.h> | |
84 | #include <kern/assert.h> | |
85 | #include <mach/boolean.h> | |
86 | ||
87 | #include <kern/thread_call.h> | |
88 | ||
91447636 | 89 | void bsd_uprofil(struct time_value *syst, user_addr_t pc); |
91447636 | 90 | int tvtohz(struct timeval *tv); |
91447636 | 91 | |
1c79356b A |
92 | /* |
93 | * Clock handling routines. | |
94 | * | |
95 | * This code is written to operate with two timers which run | |
96 | * independently of each other. The main clock, running at hz | |
97 | * times per second, is used to do scheduling and timeout calculations. | |
98 | * The second timer does resource utilization estimation statistically | |
99 | * based on the state of the machine phz times a second. Both functions | |
0a7de745 | 100 | * can be performed by a single clock (ie hz == phz), however the |
1c79356b A |
101 | * statistics will be much more prone to errors. Ideally a machine |
102 | * would have separate clocks measuring time spent in user state, system | |
103 | * state, interrupt state, and idle state. These clocks would allow a non- | |
104 | * approximate measure of resource utilization. | |
105 | */ | |
106 | ||
107 | /* | |
108 | * The hz hardware interval timer. | |
1c79356b A |
109 | */ |
110 | ||
91447636 A |
111 | int hz = 100; /* GET RID OF THIS !!! */ |
112 | int tick = (1000000 / 100); /* GET RID OF THIS !!! */ | |
113 | ||
1c79356b A |
114 | /* |
115 | * Kernel timeout services. | |
116 | */ | |
117 | ||
118 | /* | |
119 | * Set a timeout. | |
120 | * | |
121 | * fcn: function to call | |
122 | * param: parameter to pass to function | |
123 | * interval: timeout interval, in hz. | |
124 | */ | |
125 | void | |
126 | timeout( | |
0a7de745 A |
127 | timeout_fcn_t fcn, |
128 | void *param, | |
129 | int interval) | |
1c79356b | 130 | { |
0a7de745 | 131 | uint64_t deadline; |
1c79356b A |
132 | |
133 | clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline); | |
134 | thread_call_func_delayed((thread_call_func_t)fcn, param, deadline); | |
135 | } | |
136 | ||
39236c6e A |
137 | /* |
138 | * Set a timeout with leeway. | |
139 | * | |
140 | * fcn: function to call | |
141 | * param: parameter to pass to function | |
142 | * interval: timeout interval, in hz. | |
143 | * leeway_interval: leeway interval, in hz. | |
144 | */ | |
145 | void | |
146 | timeout_with_leeway( | |
0a7de745 A |
147 | timeout_fcn_t fcn, |
148 | void *param, | |
149 | int interval, | |
150 | int leeway_interval) | |
39236c6e | 151 | { |
0a7de745 A |
152 | uint64_t deadline; |
153 | uint64_t leeway; | |
39236c6e A |
154 | |
155 | clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline); | |
156 | ||
157 | clock_interval_to_absolutetime_interval(leeway_interval, NSEC_PER_SEC / hz, &leeway); | |
158 | ||
159 | thread_call_func_delayed_with_leeway((thread_call_func_t)fcn, param, deadline, leeway, THREAD_CALL_DELAY_LEEWAY); | |
160 | } | |
161 | ||
1c79356b A |
162 | /* |
163 | * Cancel a timeout. | |
5ba3f43e A |
164 | * Deprecated because it's very inefficient. |
165 | * Switch to an allocated thread call instead. | |
1c79356b A |
166 | */ |
167 | void | |
168 | untimeout( | |
0a7de745 A |
169 | timeout_fcn_t fcn, |
170 | void *param) | |
1c79356b A |
171 | { |
172 | thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE); | |
173 | } | |
174 | ||
175 | ||
91447636 A |
176 | /* |
177 | * Set a timeout. | |
178 | * | |
179 | * fcn: function to call | |
180 | * param: parameter to pass to function | |
181 | * ts: timeout interval, in timespec | |
182 | */ | |
183 | void | |
184 | bsd_timeout( | |
0a7de745 A |
185 | timeout_fcn_t fcn, |
186 | void *param, | |
91447636 A |
187 | struct timespec *ts) |
188 | { | |
0a7de745 | 189 | uint64_t deadline = 0; |
91447636 A |
190 | |
191 | if (ts && (ts->tv_sec || ts->tv_nsec)) { | |
0a7de745 | 192 | nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec, &deadline ); |
91447636 A |
193 | clock_absolutetime_interval_to_deadline( deadline, &deadline ); |
194 | } | |
195 | thread_call_func_delayed((thread_call_func_t)fcn, param, deadline); | |
196 | } | |
197 | ||
198 | /* | |
199 | * Cancel a timeout. | |
5ba3f43e A |
200 | * Deprecated because it's very inefficient. |
201 | * Switch to an allocated thread call instead. | |
91447636 A |
202 | */ |
203 | void | |
204 | bsd_untimeout( | |
0a7de745 A |
205 | timeout_fcn_t fcn, |
206 | void *param) | |
91447636 A |
207 | { |
208 | thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE); | |
209 | } | |
210 | ||
1c79356b A |
211 | |
212 | /* | |
213 | * Compute number of hz until specified time. | |
214 | * Used to compute third argument to timeout() from an | |
215 | * absolute time. | |
216 | */ | |
91447636 | 217 | int |
2d21ac55 | 218 | hzto(struct timeval *tv) |
1c79356b | 219 | { |
9bccf70c | 220 | struct timeval now; |
2d21ac55 A |
221 | long ticks; |
222 | long sec; | |
9bccf70c A |
223 | |
224 | microtime(&now); | |
1c79356b A |
225 | /* |
226 | * If number of milliseconds will fit in 32 bit arithmetic, | |
227 | * then compute number of milliseconds to time and scale to | |
228 | * ticks. Otherwise just compute number of hz in time, rounding | |
229 | * times greater than representible to maximum value. | |
230 | * | |
231 | * Delta times less than 25 days can be computed ``exactly''. | |
232 | * Maximum value for any timeout in 10ms ticks is 250 days. | |
233 | */ | |
9bccf70c | 234 | sec = tv->tv_sec - now.tv_sec; |
0a7de745 | 235 | if (sec <= 0x7fffffff / 1000 - 1000) { |
9bccf70c | 236 | ticks = ((tv->tv_sec - now.tv_sec) * 1000 + |
0a7de745 A |
237 | (tv->tv_usec - now.tv_usec) / 1000) |
238 | / (tick / 1000); | |
239 | } else if (sec <= 0x7fffffff / hz) { | |
1c79356b | 240 | ticks = sec * hz; |
0a7de745 | 241 | } else { |
1c79356b | 242 | ticks = 0x7fffffff; |
0a7de745 | 243 | } |
1c79356b | 244 | |
0a7de745 | 245 | return ticks; |
1c79356b | 246 | } |
1c79356b A |
247 | |
248 | /* | |
249 | * Return information about system clocks. | |
250 | */ | |
2d21ac55 A |
251 | static int |
252 | sysctl_clockrate | |
253 | (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req) | |
1c79356b | 254 | { |
5ba3f43e A |
255 | struct clockinfo clkinfo = { |
256 | .hz = hz, | |
257 | .tick = tick, | |
258 | .tickadj = 0, | |
259 | .stathz = hz, | |
260 | .profhz = hz, | |
261 | }; | |
1c79356b | 262 | |
2d21ac55 | 263 | return sysctl_io_opaque(req, &clkinfo, sizeof(clkinfo), NULL); |
1c79356b A |
264 | } |
265 | ||
2d21ac55 | 266 | SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, |
0a7de745 A |
267 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
268 | 0, 0, sysctl_clockrate, "S,clockinfo", ""); | |
2d21ac55 | 269 | |
1c79356b A |
270 | |
271 | /* | |
272 | * Compute number of ticks in the specified amount of time. | |
273 | */ | |
274 | int | |
91447636 | 275 | tvtohz(struct timeval *tv) |
1c79356b | 276 | { |
2d21ac55 A |
277 | unsigned long ticks; |
278 | long sec, usec; | |
1c79356b A |
279 | |
280 | /* | |
281 | * If the number of usecs in the whole seconds part of the time | |
282 | * difference fits in a long, then the total number of usecs will | |
283 | * fit in an unsigned long. Compute the total and convert it to | |
284 | * ticks, rounding up and adding 1 to allow for the current tick | |
285 | * to expire. Rounding also depends on unsigned long arithmetic | |
286 | * to avoid overflow. | |
287 | * | |
288 | * Otherwise, if the number of ticks in the whole seconds part of | |
289 | * the time difference fits in a long, then convert the parts to | |
290 | * ticks separately and add, using similar rounding methods and | |
291 | * overflow avoidance. This method would work in the previous | |
292 | * case but it is slightly slower and assumes that hz is integral. | |
293 | * | |
294 | * Otherwise, round the time difference down to the maximum | |
295 | * representable value. | |
296 | * | |
297 | * If ints have 32 bits, then the maximum value for any timeout in | |
298 | * 10ms ticks is 248 days. | |
299 | */ | |
300 | sec = tv->tv_sec; | |
301 | usec = tv->tv_usec; | |
302 | if (usec < 0) { | |
303 | sec--; | |
304 | usec += 1000000; | |
305 | } | |
306 | if (sec < 0) { | |
307 | #ifdef DIAGNOSTIC | |
308 | if (usec > 0) { | |
309 | sec++; | |
310 | usec -= 1000000; | |
311 | } | |
312 | printf("tvotohz: negative time difference %ld sec %ld usec\n", | |
0a7de745 | 313 | sec, usec); |
1c79356b A |
314 | #endif |
315 | ticks = 1; | |
0a7de745 | 316 | } else if (sec <= LONG_MAX / 1000000) { |
1c79356b | 317 | ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) |
0a7de745 A |
318 | / tick + 1; |
319 | } else if (sec <= LONG_MAX / hz) { | |
1c79356b | 320 | ticks = sec * hz |
0a7de745 A |
321 | + ((unsigned long)usec + (tick - 1)) / tick + 1; |
322 | } else { | |
1c79356b | 323 | ticks = LONG_MAX; |
0a7de745 A |
324 | } |
325 | if (ticks > INT_MAX) { | |
1c79356b | 326 | ticks = INT_MAX; |
0a7de745 A |
327 | } |
328 | return (int)ticks; | |
1c79356b A |
329 | } |
330 | ||
2d21ac55 | 331 | /* TBD locking user profiling is not resolved yet */ |
1c79356b A |
332 | void |
333 | get_procrustime(time_value_t *tv) | |
334 | { | |
335 | struct proc *p = current_proc(); | |
336 | struct timeval st; | |
337 | ||
0a7de745 | 338 | if (p == NULL) { |
1c79356b | 339 | return; |
0a7de745 A |
340 | } |
341 | if (!(p->p_flag & P_PROFIL)) { | |
342 | return; | |
343 | } | |
1c79356b | 344 | |
2d21ac55 | 345 | //proc_lock(p); |
1c79356b | 346 | st = p->p_stats->p_ru.ru_stime; |
2d21ac55 | 347 | //proc_unlock(p); |
0a7de745 | 348 | |
1c79356b A |
349 | tv->seconds = st.tv_sec; |
350 | tv->microseconds = st.tv_usec; | |
351 | } |