]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_clock.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / kern_clock.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ff6e181a
A
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
11 * file.
1c79356b 12 *
ff6e181a
A
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
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
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.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24/*-
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.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
62 */
63/*
64 * HISTORY
65 */
66
67#include <machine/spl.h>
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/time.h>
1c79356b
A
72#include <sys/resourcevar.h>
73#include <sys/kernel.h>
74#include <sys/resource.h>
91447636 75#include <sys/proc_internal.h>
1c79356b 76#include <sys/vm.h>
91447636 77#include <sys/sysctl.h>
1c79356b
A
78
79#ifdef GPROF
80#include <sys/gmon.h>
81#endif
82
83#include <kern/thread.h>
84#include <kern/ast.h>
85#include <kern/assert.h>
86#include <mach/boolean.h>
87
88#include <kern/thread_call.h>
89
91447636
A
90void bsd_uprofil(struct time_value *syst, user_addr_t pc);
91void get_procrustime(time_value_t *tv);
92int sysctl_clockrate(user_addr_t where, size_t *sizep);
93int tvtohz(struct timeval *tv);
94extern void psignal_sigprof(struct proc *);
95extern void psignal_vtalarm(struct proc *);
96extern void psignal_xcpu(struct proc *);
97
1c79356b
A
98/*
99 * Clock handling routines.
100 *
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.
111 */
112
113/*
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.
118 */
119
91447636
A
120int hz = 100; /* GET RID OF THIS !!! */
121int tick = (1000000 / 100); /* GET RID OF THIS !!! */
122
1c79356b
A
123int bsd_hardclockinit = 0;
124/*ARGSUSED*/
125void
91447636
A
126bsd_hardclock(
127 boolean_t usermode,
128#ifdef GPROF
129 caddr_t pc,
130#else
131 __unused caddr_t pc,
132#endif
133 int numticks
134 )
1c79356b
A
135{
136 register struct proc *p;
1c79356b
A
137 register thread_t thread;
138 int nusecs = numticks * tick;
55e303ae 139 struct timeval tv;
1c79356b
A
140
141 if (!bsd_hardclockinit)
142 return;
143
9bccf70c
A
144 if (bsd_hardclockinit < 0) {
145 return;
146 }
147
91447636 148 thread = current_thread();
1c79356b
A
149 /*
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
153 * one tick.
154 */
0b4e3aa0 155 p = (struct proc *)current_proc();
91447636 156 if (p && ((p->p_flag & P_WEXIT) == 0)) {
9bccf70c 157 if (usermode) {
1c79356b
A
158 if (p->p_stats && p->p_stats->p_prof.pr_scale) {
159 p->p_flag |= P_OWEUPC;
9bccf70c
A
160 astbsd_on();
161 }
162
163 /*
164 * CPU was in user state. Increment
165 * user time counter, and process process-virtual time
166 * interval timer.
167 */
168 if (p->p_stats &&
169 timerisset(&p->p_stats->p_timer[ITIMER_VIRTUAL].it_value) &&
170 !itimerdecr(&p->p_stats->p_timer[ITIMER_VIRTUAL], nusecs)) {
9bccf70c
A
171
172 /* does psignal(p, SIGVTALRM) in a thread context */
55e303ae 173 thread_call_func((thread_call_func_t)psignal_vtalarm, p, FALSE);
1c79356b
A
174 }
175 }
176
177 /*
9bccf70c
A
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.
1c79356b 184 */
9bccf70c
A
185 if (!is_thread_idle(thread)) {
186 if (p->p_limit &&
187 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
188 time_value_t sys_time, user_time;
1c79356b 189
9bccf70c
A
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) {
1c79356b 193
9bccf70c 194 /* does psignal(p, SIGXCPU) in a thread context */
55e303ae 195 thread_call_func((thread_call_func_t)psignal_xcpu, p, FALSE);
1c79356b 196
9bccf70c
A
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;
200 }
1c79356b 201 }
9bccf70c
A
202 if (timerisset(&p->p_stats->p_timer[ITIMER_PROF].it_value) &&
203 !itimerdecr(&p->p_stats->p_timer[ITIMER_PROF], nusecs)) {
1c79356b 204
9bccf70c 205 /* does psignal(p, SIGPROF) in a thread context */
55e303ae 206 thread_call_func((thread_call_func_t)psignal_sigprof, p, FALSE);
9bccf70c
A
207 }
208 }
1c79356b
A
209 }
210
9bccf70c 211#ifdef GPROF
1c79356b 212 /*
9bccf70c 213 * Gather some statistics.
1c79356b
A
214 */
215 gatherstats(usermode, pc);
9bccf70c 216#endif
1c79356b
A
217}
218
219/*
9bccf70c 220 * Gather some statistics.
1c79356b
A
221 */
222/*ARGSUSED*/
223void
9bccf70c 224gatherstats(
91447636
A
225#ifdef GPROF
226 boolean_t usermode,
227 caddr_t pc
228#else
229 __unused boolean_t usermode,
230 __unused caddr_t pc
231#endif
232 )
233
1c79356b 234{
1c79356b 235#ifdef GPROF
9bccf70c
A
236 if (!usermode) {
237 struct gmonparam *p = &_gmonparam;
1c79356b 238
1c79356b 239 if (p->state == GMON_PROF_ON) {
9bccf70c
A
240 register int s;
241
1c79356b
A
242 s = pc - p->lowpc;
243 if (s < p->textsize) {
244 s /= (HISTFRACTION * sizeof(*p->kcount));
245 p->kcount[s]++;
246 }
247 }
1c79356b 248 }
9bccf70c 249#endif
1c79356b
A
250}
251
252
253/*
254 * Kernel timeout services.
255 */
256
257/*
258 * Set a timeout.
259 *
260 * fcn: function to call
261 * param: parameter to pass to function
262 * interval: timeout interval, in hz.
263 */
264void
265timeout(
266 timeout_fcn_t fcn,
267 void *param,
268 int interval)
269{
0b4e3aa0 270 uint64_t deadline;
1c79356b
A
271
272 clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline);
273 thread_call_func_delayed((thread_call_func_t)fcn, param, deadline);
274}
275
276/*
277 * Cancel a timeout.
278 */
279void
280untimeout(
281 register timeout_fcn_t fcn,
282 register void *param)
283{
284 thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE);
285}
286
287
91447636
A
288/*
289 * Set a timeout.
290 *
291 * fcn: function to call
292 * param: parameter to pass to function
293 * ts: timeout interval, in timespec
294 */
295void
296bsd_timeout(
297 timeout_fcn_t fcn,
298 void *param,
299 struct timespec *ts)
300{
301 uint64_t deadline = 0;
302
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 );
306 }
307 thread_call_func_delayed((thread_call_func_t)fcn, param, deadline);
308}
309
310/*
311 * Cancel a timeout.
312 */
313void
314bsd_untimeout(
315 register timeout_fcn_t fcn,
316 register void *param)
317{
318 thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE);
319}
320
1c79356b
A
321
322/*
323 * Compute number of hz until specified time.
324 * Used to compute third argument to timeout() from an
325 * absolute time.
326 */
91447636 327int
1c79356b
A
328hzto(tv)
329 struct timeval *tv;
330{
9bccf70c 331 struct timeval now;
1c79356b
A
332 register long ticks;
333 register long sec;
9bccf70c
A
334
335 microtime(&now);
1c79356b
A
336 /*
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.
341 *
342 * Delta times less than 25 days can be computed ``exactly''.
343 * Maximum value for any timeout in 10ms ticks is 250 days.
344 */
9bccf70c 345 sec = tv->tv_sec - now.tv_sec;
1c79356b 346 if (sec <= 0x7fffffff / 1000 - 1000)
9bccf70c
A
347 ticks = ((tv->tv_sec - now.tv_sec) * 1000 +
348 (tv->tv_usec - now.tv_usec) / 1000)
1c79356b
A
349 / (tick / 1000);
350 else if (sec <= 0x7fffffff / hz)
351 ticks = sec * hz;
352 else
353 ticks = 0x7fffffff;
1c79356b 354
9bccf70c 355 return (ticks);
1c79356b 356}
1c79356b
A
357
358/*
359 * Return information about system clocks.
360 */
361int
91447636 362sysctl_clockrate(user_addr_t where, size_t *sizep)
1c79356b
A
363{
364 struct clockinfo clkinfo;
365
366 /*
367 * Construct clockinfo structure.
368 */
369 clkinfo.hz = hz;
370 clkinfo.tick = tick;
371 clkinfo.profhz = hz;
372 clkinfo.stathz = hz;
91447636 373 return sysctl_rdstruct(where, sizep, USER_ADDR_NULL, &clkinfo, sizeof(clkinfo));
1c79356b
A
374}
375
376
377/*
378 * Compute number of ticks in the specified amount of time.
379 */
380int
91447636 381tvtohz(struct timeval *tv)
1c79356b
A
382{
383 register unsigned long ticks;
384 register long sec, usec;
385
386 /*
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
392 * to avoid overflow.
393 *
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.
399 *
400 * Otherwise, round the time difference down to the maximum
401 * representable value.
402 *
403 * If ints have 32 bits, then the maximum value for any timeout in
404 * 10ms ticks is 248 days.
405 */
406 sec = tv->tv_sec;
407 usec = tv->tv_usec;
408 if (usec < 0) {
409 sec--;
410 usec += 1000000;
411 }
412 if (sec < 0) {
413#ifdef DIAGNOSTIC
414 if (usec > 0) {
415 sec++;
416 usec -= 1000000;
417 }
418 printf("tvotohz: negative time difference %ld sec %ld usec\n",
419 sec, usec);
420#endif
421 ticks = 1;
422 } else if (sec <= LONG_MAX / 1000000)
423 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
424 / tick + 1;
425 else if (sec <= LONG_MAX / hz)
426 ticks = sec * hz
427 + ((unsigned long)usec + (tick - 1)) / tick + 1;
428 else
429 ticks = LONG_MAX;
430 if (ticks > INT_MAX)
431 ticks = INT_MAX;
432 return ((int)ticks);
433}
434
435
436/*
437 * Start profiling on a process.
438 *
439 * Kernel profiling passes kernel_proc which never exits and hence
440 * keeps the profile clock running constantly.
441 */
442void
443startprofclock(p)
444 register struct proc *p;
445{
446 if ((p->p_flag & P_PROFIL) == 0)
447 p->p_flag |= P_PROFIL;
448}
449
450/*
451 * Stop profiling on a process.
452 */
453void
454stopprofclock(p)
455 register struct proc *p;
456{
457 if (p->p_flag & P_PROFIL)
458 p->p_flag &= ~P_PROFIL;
459}
460
461void
91447636 462bsd_uprofil(struct time_value *syst, user_addr_t pc)
1c79356b
A
463{
464struct proc *p = current_proc();
465int ticks;
466struct timeval *tv;
467struct timeval st;
468
469 if (p == NULL)
470 return;
471 if ( !(p->p_flag & P_PROFIL))
472 return;
473
474 st.tv_sec = syst->seconds;
475 st.tv_usec = syst->microseconds;
476
477 tv = &(p->p_stats->p_ru.ru_stime);
478
479 ticks = ((tv->tv_sec - st.tv_sec) * 1000 +
480 (tv->tv_usec - st.tv_usec) / 1000) /
481 (tick / 1000);
482 if (ticks)
483 addupc_task(p, pc, ticks);
484}
485
486void
487get_procrustime(time_value_t *tv)
488{
489 struct proc *p = current_proc();
490 struct timeval st;
491
492 if (p == NULL)
493 return;
494 if ( !(p->p_flag & P_PROFIL))
495 return;
496
497 st = p->p_stats->p_ru.ru_stime;
498
499 tv->seconds = st.tv_sec;
500 tv->microseconds = st.tv_usec;
501}