]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_clock.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
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.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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
63 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
69 #include <machine/spl.h>
71 #include <sys/param.h>
72 #include <sys/systm.h>
74 #include <sys/resourcevar.h>
75 #include <sys/kernel.h>
76 #include <sys/resource.h>
84 #include <kern/thread.h>
86 #include <kern/assert.h>
87 #include <mach/boolean.h>
89 #include <kern/thread_call.h>
92 * Clock handling routines.
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.
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.
113 int bsd_hardclockinit
= 0;
116 bsd_hardclock(usermode
, pc
, numticks
)
121 register struct proc
*p
;
122 register thread_t thread
;
123 int nusecs
= numticks
* tick
;
125 if (!bsd_hardclockinit
)
129 * Increment the time-of-day.
133 if (bsd_hardclockinit
< 0) {
137 thread
= current_thread();
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
144 p
= (struct proc
*)current_proc();
145 if (p
&& ((p
->p_flag
& P_WEXIT
) == NULL
)) {
147 if (p
->p_stats
&& p
->p_stats
->p_prof
.pr_scale
) {
148 p
->p_flag
|= P_OWEUPC
;
153 * CPU was in user state. Increment
154 * user time counter, and process process-virtual time
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
*);
162 /* does psignal(p, SIGVTALRM) in a thread context */
163 thread_call_func(psignal_vtalarm
, p
, FALSE
);
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.
175 if (!is_thread_idle(thread
)) {
177 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
!= RLIM_INFINITY
) {
178 time_value_t sys_time
, user_time
;
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
*);
185 /* does psignal(p, SIGXCPU) in a thread context */
186 thread_call_func(psignal_xcpu
, p
, FALSE
);
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;
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
*);
197 /* does psignal(p, SIGPROF) in a thread context */
198 thread_call_func(psignal_sigprof
, p
, FALSE
);
205 * Gather some statistics.
207 gatherstats(usermode
, pc
);
212 * Gather some statistics.
222 struct gmonparam
*p
= &_gmonparam
;
224 if (p
->state
== GMON_PROF_ON
) {
228 if (s
< p
->textsize
) {
229 s
/= (HISTFRACTION
* sizeof(*p
->kcount
));
239 * Kernel timeout services.
245 * fcn: function to call
246 * param: parameter to pass to function
247 * interval: timeout interval, in hz.
257 clock_interval_to_deadline(interval
, NSEC_PER_SEC
/ hz
, &deadline
);
258 thread_call_func_delayed((thread_call_func_t
)fcn
, param
, deadline
);
266 register timeout_fcn_t fcn
,
267 register void *param
)
269 thread_call_func_cancel((thread_call_func_t
)fcn
, param
, FALSE
);
275 * Compute number of hz until specified time.
276 * Used to compute third argument to timeout() from an
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.
293 * Delta times less than 25 days can be computed ``exactly''.
294 * Maximum value for any timeout in 10ms ticks is 250 days.
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)
301 else if (sec
<= 0x7fffffff / hz
)
310 * Return information about system clocks.
313 sysctl_clockrate(where
, sizep
)
314 register char *where
;
317 struct clockinfo clkinfo
;
320 * Construct clockinfo structure.
326 return sysctl_rdstruct(where
, sizep
, NULL
, &clkinfo
, sizeof(clkinfo
));
331 * Compute number of ticks in the specified amount of time.
337 register unsigned long ticks
;
338 register long sec
, usec
;
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
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.
354 * Otherwise, round the time difference down to the maximum
355 * representable value.
357 * If ints have 32 bits, then the maximum value for any timeout in
358 * 10ms ticks is 248 days.
372 printf("tvotohz: negative time difference %ld sec %ld usec\n",
376 } else if (sec
<= LONG_MAX
/ 1000000)
377 ticks
= (sec
* 1000000 + (unsigned long)usec
+ (tick
- 1))
379 else if (sec
<= LONG_MAX
/ hz
)
381 + ((unsigned long)usec
+ (tick
- 1)) / tick
+ 1;
391 * Start profiling on a process.
393 * Kernel profiling passes kernel_proc which never exits and hence
394 * keeps the profile clock running constantly.
398 register struct proc
*p
;
400 if ((p
->p_flag
& P_PROFIL
) == 0)
401 p
->p_flag
|= P_PROFIL
;
405 * Stop profiling on a process.
409 register struct proc
*p
;
411 if (p
->p_flag
& P_PROFIL
)
412 p
->p_flag
&= ~P_PROFIL
;
416 bsd_uprofil(struct time_value
*syst
, unsigned int pc
)
418 struct proc
*p
= current_proc();
425 if ( !(p
->p_flag
& P_PROFIL
))
428 st
.tv_sec
= syst
->seconds
;
429 st
.tv_usec
= syst
->microseconds
;
431 tv
= &(p
->p_stats
->p_ru
.ru_stime
);
433 ticks
= ((tv
->tv_sec
- st
.tv_sec
) * 1000 +
434 (tv
->tv_usec
- st
.tv_usec
) / 1000) /
437 addupc_task(p
, pc
, ticks
);
441 get_procrustime(time_value_t
*tv
)
443 struct proc
*p
= current_proc();
448 if ( !(p
->p_flag
& P_PROFIL
))
451 st
= p
->p_stats
->p_ru
.ru_stime
;
453 tv
->seconds
= st
.tv_sec
;
454 tv
->microseconds
= st
.tv_usec
;