]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
66 #include <machine/spl.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/resourcevar.h>
72 #include <sys/kernel.h>
73 #include <sys/resource.h>
81 #include <kern/thread.h>
83 #include <kern/assert.h>
84 #include <mach/boolean.h>
86 #include <kern/thread_call.h>
89 * Clock handling routines.
91 * This code is written to operate with two timers which run
92 * independently of each other. The main clock, running at hz
93 * times per second, is used to do scheduling and timeout calculations.
94 * The second timer does resource utilization estimation statistically
95 * based on the state of the machine phz times a second. Both functions
96 * can be performed by a single clock (ie hz == phz), however the
97 * statistics will be much more prone to errors. Ideally a machine
98 * would have separate clocks measuring time spent in user state, system
99 * state, interrupt state, and idle state. These clocks would allow a non-
100 * approximate measure of resource utilization.
104 * The hz hardware interval timer.
105 * We update the events relating to real time.
106 * If this timer is also being used to gather statistics,
107 * we run through the statistics gathering routine as well.
110 int bsd_hardclockinit
= 0;
113 bsd_hardclock(usermode
, pc
, numticks
)
118 register struct proc
*p
;
119 register thread_t thread
;
120 int nusecs
= numticks
* tick
;
123 if (!bsd_hardclockinit
)
127 * Increment the time-of-day.
132 if (bsd_hardclockinit
< 0) {
136 thread
= current_act();
138 * Charge the time out based on the mode the cpu is in.
139 * Here again we fudge for the lack of proper interval timers
140 * assuming that the current state has been around at least
143 p
= (struct proc
*)current_proc();
144 if (p
&& ((p
->p_flag
& P_WEXIT
) == NULL
)) {
146 if (p
->p_stats
&& p
->p_stats
->p_prof
.pr_scale
) {
147 p
->p_flag
|= P_OWEUPC
;
152 * CPU was in user state. Increment
153 * user time counter, and process process-virtual time
157 timerisset(&p
->p_stats
->p_timer
[ITIMER_VIRTUAL
].it_value
) &&
158 !itimerdecr(&p
->p_stats
->p_timer
[ITIMER_VIRTUAL
], nusecs
)) {
159 extern void psignal_vtalarm(struct proc
*);
161 /* does psignal(p, SIGVTALRM) in a thread context */
162 thread_call_func((thread_call_func_t
)psignal_vtalarm
, p
, FALSE
);
167 * If the cpu is currently scheduled to a process, then
168 * charge it with resource utilization for a tick, updating
169 * statistics which run in (user+system) virtual time,
170 * such as the cpu time limit and profiling timers.
171 * This assumes that the current process has been running
172 * the entire last tick.
174 if (!is_thread_idle(thread
)) {
176 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
!= RLIM_INFINITY
) {
177 time_value_t sys_time
, user_time
;
179 thread_read_times(thread
, &user_time
, &sys_time
);
180 if ((sys_time
.seconds
+ user_time
.seconds
+ 1) >
181 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
) {
182 extern void psignal_xcpu(struct proc
*);
184 /* does psignal(p, SIGXCPU) in a thread context */
185 thread_call_func((thread_call_func_t
)psignal_xcpu
, p
, FALSE
);
187 if (p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
<
188 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_max
)
189 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
+= 5;
192 if (timerisset(&p
->p_stats
->p_timer
[ITIMER_PROF
].it_value
) &&
193 !itimerdecr(&p
->p_stats
->p_timer
[ITIMER_PROF
], nusecs
)) {
194 extern void psignal_sigprof(struct proc
*);
196 /* does psignal(p, SIGPROF) in a thread context */
197 thread_call_func((thread_call_func_t
)psignal_sigprof
, p
, FALSE
);
204 * Gather some statistics.
206 gatherstats(usermode
, pc
);
211 * Gather some statistics.
221 struct gmonparam
*p
= &_gmonparam
;
223 if (p
->state
== GMON_PROF_ON
) {
227 if (s
< p
->textsize
) {
228 s
/= (HISTFRACTION
* sizeof(*p
->kcount
));
238 * Kernel timeout services.
244 * fcn: function to call
245 * param: parameter to pass to function
246 * interval: timeout interval, in hz.
256 clock_interval_to_deadline(interval
, NSEC_PER_SEC
/ hz
, &deadline
);
257 thread_call_func_delayed((thread_call_func_t
)fcn
, param
, deadline
);
265 register timeout_fcn_t fcn
,
266 register void *param
)
268 thread_call_func_cancel((thread_call_func_t
)fcn
, param
, FALSE
);
274 * Compute number of hz until specified time.
275 * Used to compute third argument to timeout() from an
287 * If number of milliseconds will fit in 32 bit arithmetic,
288 * then compute number of milliseconds to time and scale to
289 * ticks. Otherwise just compute number of hz in time, rounding
290 * times greater than representible to maximum value.
292 * Delta times less than 25 days can be computed ``exactly''.
293 * Maximum value for any timeout in 10ms ticks is 250 days.
295 sec
= tv
->tv_sec
- now
.tv_sec
;
296 if (sec
<= 0x7fffffff / 1000 - 1000)
297 ticks
= ((tv
->tv_sec
- now
.tv_sec
) * 1000 +
298 (tv
->tv_usec
- now
.tv_usec
) / 1000)
300 else if (sec
<= 0x7fffffff / hz
)
309 * Return information about system clocks.
312 sysctl_clockrate(where
, sizep
)
313 register char *where
;
316 struct clockinfo clkinfo
;
319 * Construct clockinfo structure.
325 return sysctl_rdstruct(where
, sizep
, NULL
, &clkinfo
, sizeof(clkinfo
));
330 * Compute number of ticks in the specified amount of time.
336 register unsigned long ticks
;
337 register long sec
, usec
;
340 * If the number of usecs in the whole seconds part of the time
341 * difference fits in a long, then the total number of usecs will
342 * fit in an unsigned long. Compute the total and convert it to
343 * ticks, rounding up and adding 1 to allow for the current tick
344 * to expire. Rounding also depends on unsigned long arithmetic
347 * Otherwise, if the number of ticks in the whole seconds part of
348 * the time difference fits in a long, then convert the parts to
349 * ticks separately and add, using similar rounding methods and
350 * overflow avoidance. This method would work in the previous
351 * case but it is slightly slower and assumes that hz is integral.
353 * Otherwise, round the time difference down to the maximum
354 * representable value.
356 * If ints have 32 bits, then the maximum value for any timeout in
357 * 10ms ticks is 248 days.
371 printf("tvotohz: negative time difference %ld sec %ld usec\n",
375 } else if (sec
<= LONG_MAX
/ 1000000)
376 ticks
= (sec
* 1000000 + (unsigned long)usec
+ (tick
- 1))
378 else if (sec
<= LONG_MAX
/ hz
)
380 + ((unsigned long)usec
+ (tick
- 1)) / tick
+ 1;
390 * Start profiling on a process.
392 * Kernel profiling passes kernel_proc which never exits and hence
393 * keeps the profile clock running constantly.
397 register struct proc
*p
;
399 if ((p
->p_flag
& P_PROFIL
) == 0)
400 p
->p_flag
|= P_PROFIL
;
404 * Stop profiling on a process.
408 register struct proc
*p
;
410 if (p
->p_flag
& P_PROFIL
)
411 p
->p_flag
&= ~P_PROFIL
;
415 bsd_uprofil(struct time_value
*syst
, unsigned int pc
)
417 struct proc
*p
= current_proc();
424 if ( !(p
->p_flag
& P_PROFIL
))
427 st
.tv_sec
= syst
->seconds
;
428 st
.tv_usec
= syst
->microseconds
;
430 tv
= &(p
->p_stats
->p_ru
.ru_stime
);
432 ticks
= ((tv
->tv_sec
- st
.tv_sec
) * 1000 +
433 (tv
->tv_usec
- st
.tv_usec
) / 1000) /
436 addupc_task(p
, pc
, ticks
);
440 get_procrustime(time_value_t
*tv
)
442 struct proc
*p
= current_proc();
447 if ( !(p
->p_flag
& P_PROFIL
))
450 st
= p
->p_stats
->p_ru
.ru_stime
;
452 tv
->seconds
= st
.tv_sec
;
453 tv
->microseconds
= st
.tv_usec
;