]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_clock.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_clock.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*-
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
67 */
68/*
69 * HISTORY
70 */
71
72#include <machine/spl.h>
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/time.h>
1c79356b
A
77#include <sys/resourcevar.h>
78#include <sys/kernel.h>
79#include <sys/resource.h>
91447636 80#include <sys/proc_internal.h>
1c79356b 81#include <sys/vm.h>
91447636 82#include <sys/sysctl.h>
1c79356b
A
83
84#ifdef GPROF
85#include <sys/gmon.h>
86#endif
87
88#include <kern/thread.h>
89#include <kern/ast.h>
90#include <kern/assert.h>
91#include <mach/boolean.h>
92
93#include <kern/thread_call.h>
94
91447636
A
95void bsd_uprofil(struct time_value *syst, user_addr_t pc);
96void get_procrustime(time_value_t *tv);
97int sysctl_clockrate(user_addr_t where, size_t *sizep);
98int tvtohz(struct timeval *tv);
99extern void psignal_sigprof(struct proc *);
100extern void psignal_vtalarm(struct proc *);
101extern void psignal_xcpu(struct proc *);
102
1c79356b
A
103/*
104 * Clock handling routines.
105 *
106 * This code is written to operate with two timers which run
107 * independently of each other. The main clock, running at hz
108 * times per second, is used to do scheduling and timeout calculations.
109 * The second timer does resource utilization estimation statistically
110 * based on the state of the machine phz times a second. Both functions
111 * can be performed by a single clock (ie hz == phz), however the
112 * statistics will be much more prone to errors. Ideally a machine
113 * would have separate clocks measuring time spent in user state, system
114 * state, interrupt state, and idle state. These clocks would allow a non-
115 * approximate measure of resource utilization.
116 */
117
118/*
119 * The hz hardware interval timer.
120 * We update the events relating to real time.
121 * If this timer is also being used to gather statistics,
122 * we run through the statistics gathering routine as well.
123 */
124
91447636
A
125int hz = 100; /* GET RID OF THIS !!! */
126int tick = (1000000 / 100); /* GET RID OF THIS !!! */
127
1c79356b
A
128int bsd_hardclockinit = 0;
129/*ARGSUSED*/
130void
91447636
A
131bsd_hardclock(
132 boolean_t usermode,
133#ifdef GPROF
134 caddr_t pc,
135#else
136 __unused caddr_t pc,
137#endif
138 int numticks
139 )
1c79356b
A
140{
141 register struct proc *p;
1c79356b
A
142 register thread_t thread;
143 int nusecs = numticks * tick;
55e303ae 144 struct timeval tv;
1c79356b
A
145
146 if (!bsd_hardclockinit)
147 return;
148
9bccf70c
A
149 if (bsd_hardclockinit < 0) {
150 return;
151 }
152
91447636 153 thread = current_thread();
1c79356b
A
154 /*
155 * Charge the time out based on the mode the cpu is in.
156 * Here again we fudge for the lack of proper interval timers
157 * assuming that the current state has been around at least
158 * one tick.
159 */
0b4e3aa0 160 p = (struct proc *)current_proc();
91447636 161 if (p && ((p->p_flag & P_WEXIT) == 0)) {
9bccf70c 162 if (usermode) {
1c79356b
A
163 if (p->p_stats && p->p_stats->p_prof.pr_scale) {
164 p->p_flag |= P_OWEUPC;
9bccf70c
A
165 astbsd_on();
166 }
167
168 /*
169 * CPU was in user state. Increment
170 * user time counter, and process process-virtual time
171 * interval timer.
172 */
173 if (p->p_stats &&
174 timerisset(&p->p_stats->p_timer[ITIMER_VIRTUAL].it_value) &&
175 !itimerdecr(&p->p_stats->p_timer[ITIMER_VIRTUAL], nusecs)) {
9bccf70c
A
176
177 /* does psignal(p, SIGVTALRM) in a thread context */
55e303ae 178 thread_call_func((thread_call_func_t)psignal_vtalarm, p, FALSE);
1c79356b
A
179 }
180 }
181
182 /*
9bccf70c
A
183 * If the cpu is currently scheduled to a process, then
184 * charge it with resource utilization for a tick, updating
185 * statistics which run in (user+system) virtual time,
186 * such as the cpu time limit and profiling timers.
187 * This assumes that the current process has been running
188 * the entire last tick.
1c79356b 189 */
9bccf70c
A
190 if (!is_thread_idle(thread)) {
191 if (p->p_limit &&
192 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
193 time_value_t sys_time, user_time;
1c79356b 194
9bccf70c
A
195 thread_read_times(thread, &user_time, &sys_time);
196 if ((sys_time.seconds + user_time.seconds + 1) >
197 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur) {
1c79356b 198
9bccf70c 199 /* does psignal(p, SIGXCPU) in a thread context */
55e303ae 200 thread_call_func((thread_call_func_t)psignal_xcpu, p, FALSE);
1c79356b 201
9bccf70c
A
202 if (p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur <
203 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_max)
204 p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur += 5;
205 }
1c79356b 206 }
9bccf70c
A
207 if (timerisset(&p->p_stats->p_timer[ITIMER_PROF].it_value) &&
208 !itimerdecr(&p->p_stats->p_timer[ITIMER_PROF], nusecs)) {
1c79356b 209
9bccf70c 210 /* does psignal(p, SIGPROF) in a thread context */
55e303ae 211 thread_call_func((thread_call_func_t)psignal_sigprof, p, FALSE);
9bccf70c
A
212 }
213 }
1c79356b
A
214 }
215
9bccf70c 216#ifdef GPROF
1c79356b 217 /*
9bccf70c 218 * Gather some statistics.
1c79356b
A
219 */
220 gatherstats(usermode, pc);
9bccf70c 221#endif
1c79356b
A
222}
223
224/*
9bccf70c 225 * Gather some statistics.
1c79356b
A
226 */
227/*ARGSUSED*/
228void
9bccf70c 229gatherstats(
91447636
A
230#ifdef GPROF
231 boolean_t usermode,
232 caddr_t pc
233#else
234 __unused boolean_t usermode,
235 __unused caddr_t pc
236#endif
237 )
238
1c79356b 239{
1c79356b 240#ifdef GPROF
9bccf70c
A
241 if (!usermode) {
242 struct gmonparam *p = &_gmonparam;
1c79356b 243
1c79356b 244 if (p->state == GMON_PROF_ON) {
9bccf70c
A
245 register int s;
246
1c79356b
A
247 s = pc - p->lowpc;
248 if (s < p->textsize) {
249 s /= (HISTFRACTION * sizeof(*p->kcount));
250 p->kcount[s]++;
251 }
252 }
1c79356b 253 }
9bccf70c 254#endif
1c79356b
A
255}
256
257
258/*
259 * Kernel timeout services.
260 */
261
262/*
263 * Set a timeout.
264 *
265 * fcn: function to call
266 * param: parameter to pass to function
267 * interval: timeout interval, in hz.
268 */
269void
270timeout(
271 timeout_fcn_t fcn,
272 void *param,
273 int interval)
274{
0b4e3aa0 275 uint64_t deadline;
1c79356b
A
276
277 clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline);
278 thread_call_func_delayed((thread_call_func_t)fcn, param, deadline);
279}
280
281/*
282 * Cancel a timeout.
283 */
284void
285untimeout(
286 register timeout_fcn_t fcn,
287 register void *param)
288{
289 thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE);
290}
291
292
91447636
A
293/*
294 * Set a timeout.
295 *
296 * fcn: function to call
297 * param: parameter to pass to function
298 * ts: timeout interval, in timespec
299 */
300void
301bsd_timeout(
302 timeout_fcn_t fcn,
303 void *param,
304 struct timespec *ts)
305{
306 uint64_t deadline = 0;
307
308 if (ts && (ts->tv_sec || ts->tv_nsec)) {
309 nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec, &deadline );
310 clock_absolutetime_interval_to_deadline( deadline, &deadline );
311 }
312 thread_call_func_delayed((thread_call_func_t)fcn, param, deadline);
313}
314
315/*
316 * Cancel a timeout.
317 */
318void
319bsd_untimeout(
320 register timeout_fcn_t fcn,
321 register void *param)
322{
323 thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE);
324}
325
1c79356b
A
326
327/*
328 * Compute number of hz until specified time.
329 * Used to compute third argument to timeout() from an
330 * absolute time.
331 */
91447636 332int
1c79356b
A
333hzto(tv)
334 struct timeval *tv;
335{
9bccf70c 336 struct timeval now;
1c79356b
A
337 register long ticks;
338 register long sec;
9bccf70c
A
339
340 microtime(&now);
1c79356b
A
341 /*
342 * If number of milliseconds will fit in 32 bit arithmetic,
343 * then compute number of milliseconds to time and scale to
344 * ticks. Otherwise just compute number of hz in time, rounding
345 * times greater than representible to maximum value.
346 *
347 * Delta times less than 25 days can be computed ``exactly''.
348 * Maximum value for any timeout in 10ms ticks is 250 days.
349 */
9bccf70c 350 sec = tv->tv_sec - now.tv_sec;
1c79356b 351 if (sec <= 0x7fffffff / 1000 - 1000)
9bccf70c
A
352 ticks = ((tv->tv_sec - now.tv_sec) * 1000 +
353 (tv->tv_usec - now.tv_usec) / 1000)
1c79356b
A
354 / (tick / 1000);
355 else if (sec <= 0x7fffffff / hz)
356 ticks = sec * hz;
357 else
358 ticks = 0x7fffffff;
1c79356b 359
9bccf70c 360 return (ticks);
1c79356b 361}
1c79356b
A
362
363/*
364 * Return information about system clocks.
365 */
366int
91447636 367sysctl_clockrate(user_addr_t where, size_t *sizep)
1c79356b
A
368{
369 struct clockinfo clkinfo;
370
371 /*
372 * Construct clockinfo structure.
373 */
374 clkinfo.hz = hz;
375 clkinfo.tick = tick;
376 clkinfo.profhz = hz;
377 clkinfo.stathz = hz;
91447636 378 return sysctl_rdstruct(where, sizep, USER_ADDR_NULL, &clkinfo, sizeof(clkinfo));
1c79356b
A
379}
380
381
382/*
383 * Compute number of ticks in the specified amount of time.
384 */
385int
91447636 386tvtohz(struct timeval *tv)
1c79356b
A
387{
388 register unsigned long ticks;
389 register long sec, usec;
390
391 /*
392 * If the number of usecs in the whole seconds part of the time
393 * difference fits in a long, then the total number of usecs will
394 * fit in an unsigned long. Compute the total and convert it to
395 * ticks, rounding up and adding 1 to allow for the current tick
396 * to expire. Rounding also depends on unsigned long arithmetic
397 * to avoid overflow.
398 *
399 * Otherwise, if the number of ticks in the whole seconds part of
400 * the time difference fits in a long, then convert the parts to
401 * ticks separately and add, using similar rounding methods and
402 * overflow avoidance. This method would work in the previous
403 * case but it is slightly slower and assumes that hz is integral.
404 *
405 * Otherwise, round the time difference down to the maximum
406 * representable value.
407 *
408 * If ints have 32 bits, then the maximum value for any timeout in
409 * 10ms ticks is 248 days.
410 */
411 sec = tv->tv_sec;
412 usec = tv->tv_usec;
413 if (usec < 0) {
414 sec--;
415 usec += 1000000;
416 }
417 if (sec < 0) {
418#ifdef DIAGNOSTIC
419 if (usec > 0) {
420 sec++;
421 usec -= 1000000;
422 }
423 printf("tvotohz: negative time difference %ld sec %ld usec\n",
424 sec, usec);
425#endif
426 ticks = 1;
427 } else if (sec <= LONG_MAX / 1000000)
428 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
429 / tick + 1;
430 else if (sec <= LONG_MAX / hz)
431 ticks = sec * hz
432 + ((unsigned long)usec + (tick - 1)) / tick + 1;
433 else
434 ticks = LONG_MAX;
435 if (ticks > INT_MAX)
436 ticks = INT_MAX;
437 return ((int)ticks);
438}
439
440
441/*
442 * Start profiling on a process.
443 *
444 * Kernel profiling passes kernel_proc which never exits and hence
445 * keeps the profile clock running constantly.
446 */
447void
448startprofclock(p)
449 register struct proc *p;
450{
451 if ((p->p_flag & P_PROFIL) == 0)
452 p->p_flag |= P_PROFIL;
453}
454
455/*
456 * Stop profiling on a process.
457 */
458void
459stopprofclock(p)
460 register struct proc *p;
461{
462 if (p->p_flag & P_PROFIL)
463 p->p_flag &= ~P_PROFIL;
464}
465
466void
91447636 467bsd_uprofil(struct time_value *syst, user_addr_t pc)
1c79356b
A
468{
469struct proc *p = current_proc();
470int ticks;
471struct timeval *tv;
472struct timeval st;
473
474 if (p == NULL)
475 return;
476 if ( !(p->p_flag & P_PROFIL))
477 return;
478
479 st.tv_sec = syst->seconds;
480 st.tv_usec = syst->microseconds;
481
482 tv = &(p->p_stats->p_ru.ru_stime);
483
484 ticks = ((tv->tv_sec - st.tv_sec) * 1000 +
485 (tv->tv_usec - st.tv_usec) / 1000) /
486 (tick / 1000);
487 if (ticks)
488 addupc_task(p, pc, ticks);
489}
490
491void
492get_procrustime(time_value_t *tv)
493{
494 struct proc *p = current_proc();
495 struct timeval st;
496
497 if (p == NULL)
498 return;
499 if ( !(p->p_flag & P_PROFIL))
500 return;
501
502 st = p->p_stats->p_ru.ru_stime;
503
504 tv->seconds = st.tv_sec;
505 tv->microseconds = st.tv_usec;
506}