]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_clock.c
xnu-344.21.74.tar.gz
[apple/xnu.git] / bsd / kern / kern_clock.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*-
27 * Copyright (c) 1982, 1986, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
64 */
65 /*
66 * HISTORY
67 */
68
69 #include <machine/spl.h>
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/time.h>
74 #include <sys/resourcevar.h>
75 #include <sys/kernel.h>
76 #include <sys/resource.h>
77 #include <sys/proc.h>
78 #include <sys/vm.h>
79
80 #ifdef GPROF
81 #include <sys/gmon.h>
82 #endif
83
84 #include <kern/thread.h>
85 #include <kern/ast.h>
86 #include <kern/assert.h>
87 #include <mach/boolean.h>
88
89 #include <kern/thread_call.h>
90
91 /*
92 * Clock handling routines.
93 *
94 * This code is written to operate with two timers which run
95 * independently of each other. The main clock, running at hz
96 * times per second, is used to do scheduling and timeout calculations.
97 * The second timer does resource utilization estimation statistically
98 * based on the state of the machine phz times a second. Both functions
99 * can be performed by a single clock (ie hz == phz), however the
100 * statistics will be much more prone to errors. Ideally a machine
101 * would have separate clocks measuring time spent in user state, system
102 * state, interrupt state, and idle state. These clocks would allow a non-
103 * approximate measure of resource utilization.
104 */
105
106 /*
107 * The hz hardware interval timer.
108 * We update the events relating to real time.
109 * If this timer is also being used to gather statistics,
110 * we run through the statistics gathering routine as well.
111 */
112
113 int bsd_hardclockinit = 0;
114 /*ARGSUSED*/
115 void
116 bsd_hardclock(usermode, pc, numticks)
117 boolean_t usermode;
118 caddr_t pc;
119 int numticks;
120 {
121 register struct proc *p;
122 register thread_t thread;
123 int nusecs = numticks * tick;
124
125 if (!bsd_hardclockinit)
126 return;
127
128 /*
129 * Increment the time-of-day.
130 */
131 microtime(&time);
132
133 if (bsd_hardclockinit < 0) {
134 return;
135 }
136
137 thread = current_thread();
138 /*
139 * Charge the time out based on the mode the cpu is in.
140 * Here again we fudge for the lack of proper interval timers
141 * assuming that the current state has been around at least
142 * one tick.
143 */
144 p = (struct proc *)current_proc();
145 if (p && ((p->p_flag & P_WEXIT) == NULL)) {
146 if (usermode) {
147 if (p->p_stats && p->p_stats->p_prof.pr_scale) {
148 p->p_flag |= P_OWEUPC;
149 astbsd_on();
150 }
151
152 /*
153 * CPU was in user state. Increment
154 * user time counter, and process process-virtual time
155 * interval timer.
156 */
157 if (p->p_stats &&
158 timerisset(&p->p_stats->p_timer[ITIMER_VIRTUAL].it_value) &&
159 !itimerdecr(&p->p_stats->p_timer[ITIMER_VIRTUAL], nusecs)) {
160 extern void psignal_vtalarm(struct proc *);
161
162 /* does psignal(p, SIGVTALRM) in a thread context */
163 thread_call_func(psignal_vtalarm, p, FALSE);
164 }
165 }
166
167 /*
168 * If the cpu is currently scheduled to a process, then
169 * charge it with resource utilization for a tick, updating
170 * statistics which run in (user+system) virtual time,
171 * such as the cpu time limit and profiling timers.
172 * This assumes that the current process has been running
173 * the entire last tick.
174 */
175 if (!is_thread_idle(thread)) {
176 if (p->p_limit &&
177 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
178 time_value_t sys_time, user_time;
179
180 thread_read_times(thread, &user_time, &sys_time);
181 if ((sys_time.seconds + user_time.seconds + 1) >
182 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur) {
183 extern void psignal_xcpu(struct proc *);
184
185 /* does psignal(p, SIGXCPU) in a thread context */
186 thread_call_func(psignal_xcpu, p, FALSE);
187
188 if (p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur <
189 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_max)
190 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur += 5;
191 }
192 }
193 if (timerisset(&p->p_stats->p_timer[ITIMER_PROF].it_value) &&
194 !itimerdecr(&p->p_stats->p_timer[ITIMER_PROF], nusecs)) {
195 extern void psignal_sigprof(struct proc *);
196
197 /* does psignal(p, SIGPROF) in a thread context */
198 thread_call_func(psignal_sigprof, p, FALSE);
199 }
200 }
201 }
202
203 #ifdef GPROF
204 /*
205 * Gather some statistics.
206 */
207 gatherstats(usermode, pc);
208 #endif
209 }
210
211 /*
212 * Gather some statistics.
213 */
214 /*ARGSUSED*/
215 void
216 gatherstats(
217 boolean_t usermode,
218 caddr_t pc)
219 {
220 #ifdef GPROF
221 if (!usermode) {
222 struct gmonparam *p = &_gmonparam;
223
224 if (p->state == GMON_PROF_ON) {
225 register int s;
226
227 s = pc - p->lowpc;
228 if (s < p->textsize) {
229 s /= (HISTFRACTION * sizeof(*p->kcount));
230 p->kcount[s]++;
231 }
232 }
233 }
234 #endif
235 }
236
237
238 /*
239 * Kernel timeout services.
240 */
241
242 /*
243 * Set a timeout.
244 *
245 * fcn: function to call
246 * param: parameter to pass to function
247 * interval: timeout interval, in hz.
248 */
249 void
250 timeout(
251 timeout_fcn_t fcn,
252 void *param,
253 int interval)
254 {
255 uint64_t deadline;
256
257 clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline);
258 thread_call_func_delayed((thread_call_func_t)fcn, param, deadline);
259 }
260
261 /*
262 * Cancel a timeout.
263 */
264 void
265 untimeout(
266 register timeout_fcn_t fcn,
267 register void *param)
268 {
269 thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE);
270 }
271
272
273
274 /*
275 * Compute number of hz until specified time.
276 * Used to compute third argument to timeout() from an
277 * absolute time.
278 */
279 hzto(tv)
280 struct timeval *tv;
281 {
282 struct timeval now;
283 register long ticks;
284 register long sec;
285
286 microtime(&now);
287 /*
288 * If number of milliseconds will fit in 32 bit arithmetic,
289 * then compute number of milliseconds to time and scale to
290 * ticks. Otherwise just compute number of hz in time, rounding
291 * times greater than representible to maximum value.
292 *
293 * Delta times less than 25 days can be computed ``exactly''.
294 * Maximum value for any timeout in 10ms ticks is 250 days.
295 */
296 sec = tv->tv_sec - now.tv_sec;
297 if (sec <= 0x7fffffff / 1000 - 1000)
298 ticks = ((tv->tv_sec - now.tv_sec) * 1000 +
299 (tv->tv_usec - now.tv_usec) / 1000)
300 / (tick / 1000);
301 else if (sec <= 0x7fffffff / hz)
302 ticks = sec * hz;
303 else
304 ticks = 0x7fffffff;
305
306 return (ticks);
307 }
308
309 /*
310 * Return information about system clocks.
311 */
312 int
313 sysctl_clockrate(where, sizep)
314 register char *where;
315 size_t *sizep;
316 {
317 struct clockinfo clkinfo;
318
319 /*
320 * Construct clockinfo structure.
321 */
322 clkinfo.hz = hz;
323 clkinfo.tick = tick;
324 clkinfo.profhz = hz;
325 clkinfo.stathz = hz;
326 return sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo));
327 }
328
329
330 /*
331 * Compute number of ticks in the specified amount of time.
332 */
333 int
334 tvtohz(tv)
335 struct timeval *tv;
336 {
337 register unsigned long ticks;
338 register long sec, usec;
339
340 /*
341 * If the number of usecs in the whole seconds part of the time
342 * difference fits in a long, then the total number of usecs will
343 * fit in an unsigned long. Compute the total and convert it to
344 * ticks, rounding up and adding 1 to allow for the current tick
345 * to expire. Rounding also depends on unsigned long arithmetic
346 * to avoid overflow.
347 *
348 * Otherwise, if the number of ticks in the whole seconds part of
349 * the time difference fits in a long, then convert the parts to
350 * ticks separately and add, using similar rounding methods and
351 * overflow avoidance. This method would work in the previous
352 * case but it is slightly slower and assumes that hz is integral.
353 *
354 * Otherwise, round the time difference down to the maximum
355 * representable value.
356 *
357 * If ints have 32 bits, then the maximum value for any timeout in
358 * 10ms ticks is 248 days.
359 */
360 sec = tv->tv_sec;
361 usec = tv->tv_usec;
362 if (usec < 0) {
363 sec--;
364 usec += 1000000;
365 }
366 if (sec < 0) {
367 #ifdef DIAGNOSTIC
368 if (usec > 0) {
369 sec++;
370 usec -= 1000000;
371 }
372 printf("tvotohz: negative time difference %ld sec %ld usec\n",
373 sec, usec);
374 #endif
375 ticks = 1;
376 } else if (sec <= LONG_MAX / 1000000)
377 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
378 / tick + 1;
379 else if (sec <= LONG_MAX / hz)
380 ticks = sec * hz
381 + ((unsigned long)usec + (tick - 1)) / tick + 1;
382 else
383 ticks = LONG_MAX;
384 if (ticks > INT_MAX)
385 ticks = INT_MAX;
386 return ((int)ticks);
387 }
388
389
390 /*
391 * Start profiling on a process.
392 *
393 * Kernel profiling passes kernel_proc which never exits and hence
394 * keeps the profile clock running constantly.
395 */
396 void
397 startprofclock(p)
398 register struct proc *p;
399 {
400 if ((p->p_flag & P_PROFIL) == 0)
401 p->p_flag |= P_PROFIL;
402 }
403
404 /*
405 * Stop profiling on a process.
406 */
407 void
408 stopprofclock(p)
409 register struct proc *p;
410 {
411 if (p->p_flag & P_PROFIL)
412 p->p_flag &= ~P_PROFIL;
413 }
414
415 void
416 bsd_uprofil(struct time_value *syst, unsigned int pc)
417 {
418 struct proc *p = current_proc();
419 int ticks;
420 struct timeval *tv;
421 struct timeval st;
422
423 if (p == NULL)
424 return;
425 if ( !(p->p_flag & P_PROFIL))
426 return;
427
428 st.tv_sec = syst->seconds;
429 st.tv_usec = syst->microseconds;
430
431 tv = &(p->p_stats->p_ru.ru_stime);
432
433 ticks = ((tv->tv_sec - st.tv_sec) * 1000 +
434 (tv->tv_usec - st.tv_usec) / 1000) /
435 (tick / 1000);
436 if (ticks)
437 addupc_task(p, pc, ticks);
438 }
439
440 void
441 get_procrustime(time_value_t *tv)
442 {
443 struct proc *p = current_proc();
444 struct timeval st;
445
446 if (p == NULL)
447 return;
448 if ( !(p->p_flag & P_PROFIL))
449 return;
450
451 st = p->p_stats->p_ru.ru_stime;
452
453 tv->seconds = st.tv_sec;
454 tv->microseconds = st.tv_usec;
455 }