]>
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
;
126 if (!bsd_hardclockinit
)
130 * Increment the time-of-day.
135 if (bsd_hardclockinit
< 0) {
139 thread
= current_act();
141 * Charge the time out based on the mode the cpu is in.
142 * Here again we fudge for the lack of proper interval timers
143 * assuming that the current state has been around at least
146 p
= (struct proc
*)current_proc();
147 if (p
&& ((p
->p_flag
& P_WEXIT
) == NULL
)) {
149 if (p
->p_stats
&& p
->p_stats
->p_prof
.pr_scale
) {
150 p
->p_flag
|= P_OWEUPC
;
155 * CPU was in user state. Increment
156 * user time counter, and process process-virtual time
160 timerisset(&p
->p_stats
->p_timer
[ITIMER_VIRTUAL
].it_value
) &&
161 !itimerdecr(&p
->p_stats
->p_timer
[ITIMER_VIRTUAL
], nusecs
)) {
162 extern void psignal_vtalarm(struct proc
*);
164 /* does psignal(p, SIGVTALRM) in a thread context */
165 thread_call_func((thread_call_func_t
)psignal_vtalarm
, p
, FALSE
);
170 * If the cpu is currently scheduled to a process, then
171 * charge it with resource utilization for a tick, updating
172 * statistics which run in (user+system) virtual time,
173 * such as the cpu time limit and profiling timers.
174 * This assumes that the current process has been running
175 * the entire last tick.
177 if (!is_thread_idle(thread
)) {
179 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
!= RLIM_INFINITY
) {
180 time_value_t sys_time
, user_time
;
182 thread_read_times(thread
, &user_time
, &sys_time
);
183 if ((sys_time
.seconds
+ user_time
.seconds
+ 1) >
184 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
) {
185 extern void psignal_xcpu(struct proc
*);
187 /* does psignal(p, SIGXCPU) in a thread context */
188 thread_call_func((thread_call_func_t
)psignal_xcpu
, p
, FALSE
);
190 if (p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
<
191 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_max
)
192 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
+= 5;
195 if (timerisset(&p
->p_stats
->p_timer
[ITIMER_PROF
].it_value
) &&
196 !itimerdecr(&p
->p_stats
->p_timer
[ITIMER_PROF
], nusecs
)) {
197 extern void psignal_sigprof(struct proc
*);
199 /* does psignal(p, SIGPROF) in a thread context */
200 thread_call_func((thread_call_func_t
)psignal_sigprof
, p
, FALSE
);
207 * Gather some statistics.
209 gatherstats(usermode
, pc
);
214 * Gather some statistics.
224 struct gmonparam
*p
= &_gmonparam
;
226 if (p
->state
== GMON_PROF_ON
) {
230 if (s
< p
->textsize
) {
231 s
/= (HISTFRACTION
* sizeof(*p
->kcount
));
241 * Kernel timeout services.
247 * fcn: function to call
248 * param: parameter to pass to function
249 * interval: timeout interval, in hz.
259 clock_interval_to_deadline(interval
, NSEC_PER_SEC
/ hz
, &deadline
);
260 thread_call_func_delayed((thread_call_func_t
)fcn
, param
, deadline
);
268 register timeout_fcn_t fcn
,
269 register void *param
)
271 thread_call_func_cancel((thread_call_func_t
)fcn
, param
, FALSE
);
277 * Compute number of hz until specified time.
278 * Used to compute third argument to timeout() from an
290 * If number of milliseconds will fit in 32 bit arithmetic,
291 * then compute number of milliseconds to time and scale to
292 * ticks. Otherwise just compute number of hz in time, rounding
293 * times greater than representible to maximum value.
295 * Delta times less than 25 days can be computed ``exactly''.
296 * Maximum value for any timeout in 10ms ticks is 250 days.
298 sec
= tv
->tv_sec
- now
.tv_sec
;
299 if (sec
<= 0x7fffffff / 1000 - 1000)
300 ticks
= ((tv
->tv_sec
- now
.tv_sec
) * 1000 +
301 (tv
->tv_usec
- now
.tv_usec
) / 1000)
303 else if (sec
<= 0x7fffffff / hz
)
312 * Return information about system clocks.
315 sysctl_clockrate(where
, sizep
)
316 register char *where
;
319 struct clockinfo clkinfo
;
322 * Construct clockinfo structure.
328 return sysctl_rdstruct(where
, sizep
, NULL
, &clkinfo
, sizeof(clkinfo
));
333 * Compute number of ticks in the specified amount of time.
339 register unsigned long ticks
;
340 register long sec
, usec
;
343 * If the number of usecs in the whole seconds part of the time
344 * difference fits in a long, then the total number of usecs will
345 * fit in an unsigned long. Compute the total and convert it to
346 * ticks, rounding up and adding 1 to allow for the current tick
347 * to expire. Rounding also depends on unsigned long arithmetic
350 * Otherwise, if the number of ticks in the whole seconds part of
351 * the time difference fits in a long, then convert the parts to
352 * ticks separately and add, using similar rounding methods and
353 * overflow avoidance. This method would work in the previous
354 * case but it is slightly slower and assumes that hz is integral.
356 * Otherwise, round the time difference down to the maximum
357 * representable value.
359 * If ints have 32 bits, then the maximum value for any timeout in
360 * 10ms ticks is 248 days.
374 printf("tvotohz: negative time difference %ld sec %ld usec\n",
378 } else if (sec
<= LONG_MAX
/ 1000000)
379 ticks
= (sec
* 1000000 + (unsigned long)usec
+ (tick
- 1))
381 else if (sec
<= LONG_MAX
/ hz
)
383 + ((unsigned long)usec
+ (tick
- 1)) / tick
+ 1;
393 * Start profiling on a process.
395 * Kernel profiling passes kernel_proc which never exits and hence
396 * keeps the profile clock running constantly.
400 register struct proc
*p
;
402 if ((p
->p_flag
& P_PROFIL
) == 0)
403 p
->p_flag
|= P_PROFIL
;
407 * Stop profiling on a process.
411 register struct proc
*p
;
413 if (p
->p_flag
& P_PROFIL
)
414 p
->p_flag
&= ~P_PROFIL
;
418 bsd_uprofil(struct time_value
*syst
, unsigned int pc
)
420 struct proc
*p
= current_proc();
427 if ( !(p
->p_flag
& P_PROFIL
))
430 st
.tv_sec
= syst
->seconds
;
431 st
.tv_usec
= syst
->microseconds
;
433 tv
= &(p
->p_stats
->p_ru
.ru_stime
);
435 ticks
= ((tv
->tv_sec
- st
.tv_sec
) * 1000 +
436 (tv
->tv_usec
- st
.tv_usec
) / 1000) /
439 addupc_task(p
, pc
, ticks
);
443 get_procrustime(time_value_t
*tv
)
445 struct proc
*p
= current_proc();
450 if ( !(p
->p_flag
& P_PROFIL
))
453 st
= p
->p_stats
->p_ru
.ru_stime
;
455 tv
->seconds
= st
.tv_sec
;
456 tv
->microseconds
= st
.tv_usec
;