2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
67 #include <machine/spl.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/resourcevar.h>
73 #include <sys/kernel.h>
74 #include <sys/resource.h>
75 #include <sys/proc_internal.h>
77 #include <sys/sysctl.h>
83 #include <kern/thread.h>
85 #include <kern/assert.h>
86 #include <mach/boolean.h>
88 #include <kern/thread_call.h>
90 void bsd_uprofil(struct time_value
*syst
, user_addr_t pc
);
91 void get_procrustime(time_value_t
*tv
);
92 int sysctl_clockrate(user_addr_t where
, size_t *sizep
);
93 int tvtohz(struct timeval
*tv
);
94 extern void psignal_sigprof(struct proc
*);
95 extern void psignal_vtalarm(struct proc
*);
96 extern void psignal_xcpu(struct proc
*);
99 * Clock handling routines.
101 * This code is written to operate with two timers which run
102 * independently of each other. The main clock, running at hz
103 * times per second, is used to do scheduling and timeout calculations.
104 * The second timer does resource utilization estimation statistically
105 * based on the state of the machine phz times a second. Both functions
106 * can be performed by a single clock (ie hz == phz), however the
107 * statistics will be much more prone to errors. Ideally a machine
108 * would have separate clocks measuring time spent in user state, system
109 * state, interrupt state, and idle state. These clocks would allow a non-
110 * approximate measure of resource utilization.
114 * The hz hardware interval timer.
115 * We update the events relating to real time.
116 * If this timer is also being used to gather statistics,
117 * we run through the statistics gathering routine as well.
120 int hz
= 100; /* GET RID OF THIS !!! */
121 int tick
= (1000000 / 100); /* GET RID OF THIS !!! */
123 int bsd_hardclockinit
= 0;
136 register struct proc
*p
;
137 register thread_t thread
;
138 int nusecs
= numticks
* tick
;
141 if (!bsd_hardclockinit
)
144 if (bsd_hardclockinit
< 0) {
148 thread
= current_thread();
150 * Charge the time out based on the mode the cpu is in.
151 * Here again we fudge for the lack of proper interval timers
152 * assuming that the current state has been around at least
155 p
= (struct proc
*)current_proc();
156 if (p
&& ((p
->p_flag
& P_WEXIT
) == 0)) {
158 if (p
->p_stats
&& p
->p_stats
->p_prof
.pr_scale
) {
159 p
->p_flag
|= P_OWEUPC
;
164 * CPU was in user state. Increment
165 * user time counter, and process process-virtual time
169 timerisset(&p
->p_stats
->p_timer
[ITIMER_VIRTUAL
].it_value
) &&
170 !itimerdecr(&p
->p_stats
->p_timer
[ITIMER_VIRTUAL
], nusecs
)) {
172 /* does psignal(p, SIGVTALRM) in a thread context */
173 thread_call_func((thread_call_func_t
)psignal_vtalarm
, p
, FALSE
);
178 * If the cpu is currently scheduled to a process, then
179 * charge it with resource utilization for a tick, updating
180 * statistics which run in (user+system) virtual time,
181 * such as the cpu time limit and profiling timers.
182 * This assumes that the current process has been running
183 * the entire last tick.
185 if (!is_thread_idle(thread
)) {
187 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
!= RLIM_INFINITY
) {
188 time_value_t sys_time
, user_time
;
190 thread_read_times(thread
, &user_time
, &sys_time
);
191 if ((sys_time
.seconds
+ user_time
.seconds
+ 1) >
192 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
) {
194 /* does psignal(p, SIGXCPU) in a thread context */
195 thread_call_func((thread_call_func_t
)psignal_xcpu
, p
, FALSE
);
197 if (p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
<
198 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_max
)
199 p
->p_limit
->pl_rlimit
[RLIMIT_CPU
].rlim_cur
+= 5;
202 if (timerisset(&p
->p_stats
->p_timer
[ITIMER_PROF
].it_value
) &&
203 !itimerdecr(&p
->p_stats
->p_timer
[ITIMER_PROF
], nusecs
)) {
205 /* does psignal(p, SIGPROF) in a thread context */
206 thread_call_func((thread_call_func_t
)psignal_sigprof
, p
, FALSE
);
213 * Gather some statistics.
215 gatherstats(usermode
, pc
);
220 * Gather some statistics.
229 __unused boolean_t usermode
,
237 struct gmonparam
*p
= &_gmonparam
;
239 if (p
->state
== GMON_PROF_ON
) {
243 if (s
< p
->textsize
) {
244 s
/= (HISTFRACTION
* sizeof(*p
->kcount
));
254 * Kernel timeout services.
260 * fcn: function to call
261 * param: parameter to pass to function
262 * interval: timeout interval, in hz.
272 clock_interval_to_deadline(interval
, NSEC_PER_SEC
/ hz
, &deadline
);
273 thread_call_func_delayed((thread_call_func_t
)fcn
, param
, deadline
);
281 register timeout_fcn_t fcn
,
282 register void *param
)
284 thread_call_func_cancel((thread_call_func_t
)fcn
, param
, FALSE
);
291 * fcn: function to call
292 * param: parameter to pass to function
293 * ts: timeout interval, in timespec
301 uint64_t deadline
= 0;
303 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
304 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &deadline
);
305 clock_absolutetime_interval_to_deadline( deadline
, &deadline
);
307 thread_call_func_delayed((thread_call_func_t
)fcn
, param
, deadline
);
315 register timeout_fcn_t fcn
,
316 register void *param
)
318 thread_call_func_cancel((thread_call_func_t
)fcn
, param
, FALSE
);
323 * Compute number of hz until specified time.
324 * Used to compute third argument to timeout() from an
337 * If number of milliseconds will fit in 32 bit arithmetic,
338 * then compute number of milliseconds to time and scale to
339 * ticks. Otherwise just compute number of hz in time, rounding
340 * times greater than representible to maximum value.
342 * Delta times less than 25 days can be computed ``exactly''.
343 * Maximum value for any timeout in 10ms ticks is 250 days.
345 sec
= tv
->tv_sec
- now
.tv_sec
;
346 if (sec
<= 0x7fffffff / 1000 - 1000)
347 ticks
= ((tv
->tv_sec
- now
.tv_sec
) * 1000 +
348 (tv
->tv_usec
- now
.tv_usec
) / 1000)
350 else if (sec
<= 0x7fffffff / hz
)
359 * Return information about system clocks.
362 sysctl_clockrate(user_addr_t where
, size_t *sizep
)
364 struct clockinfo clkinfo
;
367 * Construct clockinfo structure.
373 return sysctl_rdstruct(where
, sizep
, USER_ADDR_NULL
, &clkinfo
, sizeof(clkinfo
));
378 * Compute number of ticks in the specified amount of time.
381 tvtohz(struct timeval
*tv
)
383 register unsigned long ticks
;
384 register long sec
, usec
;
387 * If the number of usecs in the whole seconds part of the time
388 * difference fits in a long, then the total number of usecs will
389 * fit in an unsigned long. Compute the total and convert it to
390 * ticks, rounding up and adding 1 to allow for the current tick
391 * to expire. Rounding also depends on unsigned long arithmetic
394 * Otherwise, if the number of ticks in the whole seconds part of
395 * the time difference fits in a long, then convert the parts to
396 * ticks separately and add, using similar rounding methods and
397 * overflow avoidance. This method would work in the previous
398 * case but it is slightly slower and assumes that hz is integral.
400 * Otherwise, round the time difference down to the maximum
401 * representable value.
403 * If ints have 32 bits, then the maximum value for any timeout in
404 * 10ms ticks is 248 days.
418 printf("tvotohz: negative time difference %ld sec %ld usec\n",
422 } else if (sec
<= LONG_MAX
/ 1000000)
423 ticks
= (sec
* 1000000 + (unsigned long)usec
+ (tick
- 1))
425 else if (sec
<= LONG_MAX
/ hz
)
427 + ((unsigned long)usec
+ (tick
- 1)) / tick
+ 1;
437 * Start profiling on a process.
439 * Kernel profiling passes kernel_proc which never exits and hence
440 * keeps the profile clock running constantly.
444 register struct proc
*p
;
446 if ((p
->p_flag
& P_PROFIL
) == 0)
447 p
->p_flag
|= P_PROFIL
;
451 * Stop profiling on a process.
455 register struct proc
*p
;
457 if (p
->p_flag
& P_PROFIL
)
458 p
->p_flag
&= ~P_PROFIL
;
462 bsd_uprofil(struct time_value
*syst
, user_addr_t pc
)
464 struct proc
*p
= current_proc();
471 if ( !(p
->p_flag
& P_PROFIL
))
474 st
.tv_sec
= syst
->seconds
;
475 st
.tv_usec
= syst
->microseconds
;
477 tv
= &(p
->p_stats
->p_ru
.ru_stime
);
479 ticks
= ((tv
->tv_sec
- st
.tv_sec
) * 1000 +
480 (tv
->tv_usec
- st
.tv_usec
) / 1000) /
483 addupc_task(p
, pc
, ticks
);
487 get_procrustime(time_value_t
*tv
)
489 struct proc
*p
= current_proc();
494 if ( !(p
->p_flag
& P_PROFIL
))
497 st
= p
->p_stats
->p_ru
.ru_stime
;
499 tv
->seconds
= st
.tv_sec
;
500 tv
->microseconds
= st
.tv_usec
;