]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
11 | * | |
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 | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
23 | /*- | |
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. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
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. | |
47 | * | |
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 | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 | |
61 | */ | |
62 | /* | |
63 | * HISTORY | |
64 | */ | |
65 | ||
66 | #include <machine/spl.h> | |
67 | ||
68 | #include <sys/param.h> | |
69 | #include <sys/systm.h> | |
70 | #include <sys/time.h> | |
71 | #include <sys/dkstat.h> | |
72 | #include <sys/resourcevar.h> | |
73 | #include <sys/kernel.h> | |
74 | #include <sys/resource.h> | |
75 | #include <sys/proc.h> | |
76 | #include <sys/vm.h> | |
77 | ||
78 | #ifdef GPROF | |
79 | #include <sys/gmon.h> | |
80 | #endif | |
81 | ||
82 | #include <kern/thread.h> | |
83 | #include <kern/ast.h> | |
84 | #include <kern/assert.h> | |
85 | #include <mach/boolean.h> | |
86 | ||
87 | #include <kern/thread_call.h> | |
88 | ||
89 | /* | |
90 | * Clock handling routines. | |
91 | * | |
92 | * This code is written to operate with two timers which run | |
93 | * independently of each other. The main clock, running at hz | |
94 | * times per second, is used to do scheduling and timeout calculations. | |
95 | * The second timer does resource utilization estimation statistically | |
96 | * based on the state of the machine phz times a second. Both functions | |
97 | * can be performed by a single clock (ie hz == phz), however the | |
98 | * statistics will be much more prone to errors. Ideally a machine | |
99 | * would have separate clocks measuring time spent in user state, system | |
100 | * state, interrupt state, and idle state. These clocks would allow a non- | |
101 | * approximate measure of resource utilization. | |
102 | */ | |
103 | ||
104 | /* | |
105 | * The hz hardware interval timer. | |
106 | * We update the events relating to real time. | |
107 | * If this timer is also being used to gather statistics, | |
108 | * we run through the statistics gathering routine as well. | |
109 | */ | |
110 | ||
111 | int bsd_hardclockinit = 0; | |
112 | /*ARGSUSED*/ | |
113 | void | |
114 | bsd_hardclock(usermode, pc, numticks) | |
115 | boolean_t usermode; | |
116 | caddr_t pc; | |
117 | int numticks; | |
118 | { | |
119 | register struct proc *p; | |
120 | register int s; | |
121 | int ticks = numticks; | |
122 | extern int tickdelta; | |
123 | extern long timedelta; | |
124 | register thread_t thread; | |
125 | int nusecs = numticks * tick; | |
126 | ||
127 | if (!bsd_hardclockinit) | |
128 | return; | |
129 | ||
130 | thread = current_thread(); | |
131 | ||
132 | /* | |
133 | * Charge the time out based on the mode the cpu is in. | |
134 | * Here again we fudge for the lack of proper interval timers | |
135 | * assuming that the current state has been around at least | |
136 | * one tick. | |
137 | */ | |
0b4e3aa0 | 138 | p = (struct proc *)current_proc(); |
1c79356b A |
139 | if (p && ((p->p_flag & P_WEXIT) == NULL)) { |
140 | if (usermode) { | |
141 | if (p) { | |
142 | if (p->p_stats && p->p_stats->p_prof.pr_scale) { | |
143 | p->p_flag |= P_OWEUPC; | |
144 | ast_on(AST_BSD); | |
145 | } | |
146 | } | |
147 | ||
148 | /* | |
149 | * CPU was in user state. Increment | |
150 | * user time counter, and process process-virtual time | |
151 | * interval timer. | |
152 | */ | |
153 | if (p->p_stats && | |
154 | timerisset(&p->p_stats->p_timer[ITIMER_VIRTUAL].it_value) && | |
155 | itimerdecr(&p->p_stats->p_timer[ITIMER_VIRTUAL], nusecs) == 0) { | |
156 | extern void psignal_vtalarm(struct proc *); | |
157 | ||
158 | /* does psignal(p, SIGVTALRM) in a thread context */ | |
159 | thread_call_func((thread_call_func_t)psignal_vtalarm, p, FALSE); | |
160 | } | |
161 | } | |
162 | ||
163 | /* | |
164 | * If the cpu is currently scheduled to a process, then | |
165 | * charge it with resource utilization for a tick, updating | |
166 | * statistics which run in (user+system) virtual time, | |
167 | * such as the cpu time limit and profiling timers. | |
168 | * This assumes that the current process has been running | |
169 | * the entire last tick. | |
170 | */ | |
171 | if (p && !(is_thread_idle(thread))) | |
172 | { | |
173 | if (p->p_limit && (p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY)) { | |
174 | time_value_t sys_time, user_time; | |
175 | ||
176 | thread_read_times(thread, &user_time, &sys_time); | |
177 | if ((sys_time.seconds + user_time.seconds + 1) > | |
178 | p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur) { | |
179 | extern void psignal_xcpu(struct proc *); | |
180 | ||
181 | /* does psignal(p, SIGXCPU) in a thread context */ | |
182 | thread_call_func((thread_call_func_t)psignal_xcpu, p, FALSE); | |
183 | ||
184 | if (p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur < | |
185 | p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_max) | |
186 | p->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur += 5; | |
187 | } | |
188 | } | |
189 | if (timerisset(&p->p_stats->p_timer[ITIMER_PROF].it_value) && | |
190 | itimerdecr(&p->p_stats->p_timer[ITIMER_PROF], nusecs) == 0) { | |
191 | extern void psignal_sigprof(struct proc *); | |
192 | ||
193 | /* does psignal(p, SIGPROF) in a thread context */ | |
194 | thread_call_func((thread_call_func_t)psignal_sigprof, p, FALSE); | |
195 | } | |
196 | } | |
197 | ||
198 | /* | |
199 | * Increment the time-of-day, and schedule | |
200 | * processing of the callouts at a very low cpu priority, | |
201 | * so we don't keep the relatively high clock interrupt | |
202 | * priority any longer than necessary. | |
203 | */ | |
204 | ||
205 | /* | |
206 | * Gather the statistics. | |
207 | */ | |
208 | gatherstats(usermode, pc); | |
209 | ||
210 | } | |
211 | if (timedelta != 0) { | |
212 | register delta; | |
213 | clock_res_t nsdelta = tickdelta * NSEC_PER_USEC; | |
214 | ||
215 | if (timedelta < 0) { | |
216 | delta = ticks - tickdelta; | |
217 | timedelta += tickdelta; | |
218 | nsdelta = -nsdelta; | |
219 | } else { | |
220 | delta = ticks + tickdelta; | |
221 | timedelta -= tickdelta; | |
222 | } | |
223 | clock_adjust_calendar(nsdelta); | |
224 | } | |
225 | microtime(&time); | |
226 | } | |
227 | ||
228 | /* | |
229 | * Gather statistics on resource utilization. | |
230 | * | |
231 | * We make a gross assumption: that the system has been in the | |
232 | * state it is in (user state, kernel state, interrupt state, | |
233 | * or idle state) for the entire last time interval, and | |
234 | * update statistics accordingly. | |
235 | */ | |
236 | /*ARGSUSED*/ | |
237 | void | |
238 | gatherstats(usermode, pc) | |
239 | boolean_t usermode; | |
240 | caddr_t pc; | |
241 | { | |
242 | register int cpstate, s; | |
243 | struct proc *proc =current_proc(); | |
244 | #ifdef GPROF | |
245 | struct gmonparam *p = &_gmonparam; | |
246 | #endif | |
247 | ||
248 | /* | |
249 | * Determine what state the cpu is in. | |
250 | */ | |
251 | if (usermode) { | |
252 | /* | |
253 | * CPU was in user state. | |
254 | */ | |
255 | if (proc->p_nice > NZERO) | |
256 | cpstate = CP_NICE; | |
257 | else | |
258 | cpstate = CP_USER; | |
259 | } else { | |
260 | /* | |
261 | * CPU was in system state. If profiling kernel | |
262 | * increment a counter. If no process is running | |
263 | * then this is a system tick if we were running | |
264 | * at a non-zero IPL (in a driver). If a process is running, | |
265 | * then we charge it with system time even if we were | |
266 | * at a non-zero IPL, since the system often runs | |
267 | * this way during processing of system calls. | |
268 | * This is approximate, but the lack of true interval | |
269 | * timers makes doing anything else difficult. | |
270 | */ | |
271 | cpstate = CP_SYS; | |
272 | if (is_thread_idle(current_thread())) | |
273 | cpstate = CP_IDLE; | |
274 | #ifdef GPROF | |
275 | if (p->state == GMON_PROF_ON) { | |
276 | s = pc - p->lowpc; | |
277 | if (s < p->textsize) { | |
278 | s /= (HISTFRACTION * sizeof(*p->kcount)); | |
279 | p->kcount[s]++; | |
280 | } | |
281 | } | |
282 | #endif | |
283 | } | |
284 | /* | |
285 | * We maintain statistics shown by user-level statistics | |
286 | * programs: the amount of time in each cpu state, and | |
287 | * the amount of time each of DK_NDRIVE ``drives'' is busy. | |
288 | */ | |
289 | cp_time[cpstate]++; | |
290 | for (s = 0; s < DK_NDRIVE; s++) | |
291 | if (dk_busy & (1 << s)) | |
292 | dk_time[s]++; | |
293 | } | |
294 | ||
295 | ||
296 | /* | |
297 | * Kernel timeout services. | |
298 | */ | |
299 | ||
300 | /* | |
301 | * Set a timeout. | |
302 | * | |
303 | * fcn: function to call | |
304 | * param: parameter to pass to function | |
305 | * interval: timeout interval, in hz. | |
306 | */ | |
307 | void | |
308 | timeout( | |
309 | timeout_fcn_t fcn, | |
310 | void *param, | |
311 | int interval) | |
312 | { | |
0b4e3aa0 | 313 | uint64_t deadline; |
1c79356b A |
314 | |
315 | clock_interval_to_deadline(interval, NSEC_PER_SEC / hz, &deadline); | |
316 | thread_call_func_delayed((thread_call_func_t)fcn, param, deadline); | |
317 | } | |
318 | ||
319 | /* | |
320 | * Cancel a timeout. | |
321 | */ | |
322 | void | |
323 | untimeout( | |
324 | register timeout_fcn_t fcn, | |
325 | register void *param) | |
326 | { | |
327 | thread_call_func_cancel((thread_call_func_t)fcn, param, FALSE); | |
328 | } | |
329 | ||
330 | ||
331 | ||
332 | /* | |
333 | * Compute number of hz until specified time. | |
334 | * Used to compute third argument to timeout() from an | |
335 | * absolute time. | |
336 | */ | |
337 | hzto(tv) | |
338 | struct timeval *tv; | |
339 | { | |
340 | register long ticks; | |
341 | register long sec; | |
342 | int s = splhigh(); | |
343 | ||
344 | /* | |
345 | * If number of milliseconds will fit in 32 bit arithmetic, | |
346 | * then compute number of milliseconds to time and scale to | |
347 | * ticks. Otherwise just compute number of hz in time, rounding | |
348 | * times greater than representible to maximum value. | |
349 | * | |
350 | * Delta times less than 25 days can be computed ``exactly''. | |
351 | * Maximum value for any timeout in 10ms ticks is 250 days. | |
352 | */ | |
353 | sec = tv->tv_sec - time.tv_sec; | |
354 | if (sec <= 0x7fffffff / 1000 - 1000) | |
355 | ticks = ((tv->tv_sec - time.tv_sec) * 1000 + | |
356 | (tv->tv_usec - time.tv_usec) / 1000) | |
357 | / (tick / 1000); | |
358 | else if (sec <= 0x7fffffff / hz) | |
359 | ticks = sec * hz; | |
360 | else | |
361 | ticks = 0x7fffffff; | |
362 | splx(s); | |
363 | return (ticks); | |
364 | } | |
365 | ||
366 | #if 0 /* [ */ | |
367 | /* | |
368 | * Convert ticks to a timeval | |
369 | */ | |
370 | ticks_to_timeval(ticks, tvp) | |
371 | register long ticks; | |
372 | struct timeval *tvp; | |
373 | { | |
374 | tvp->tv_sec = ticks/hz; | |
375 | tvp->tv_usec = (ticks%hz) * tick; | |
376 | asert(tvp->tv_usec < 1000000); | |
377 | } | |
378 | #endif /* ] */ | |
379 | ||
380 | /* | |
381 | * Return information about system clocks. | |
382 | */ | |
383 | int | |
384 | sysctl_clockrate(where, sizep) | |
385 | register char *where; | |
386 | size_t *sizep; | |
387 | { | |
388 | struct clockinfo clkinfo; | |
389 | ||
390 | /* | |
391 | * Construct clockinfo structure. | |
392 | */ | |
393 | clkinfo.hz = hz; | |
394 | clkinfo.tick = tick; | |
395 | clkinfo.profhz = hz; | |
396 | clkinfo.stathz = hz; | |
397 | return sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo)); | |
398 | } | |
399 | ||
400 | ||
401 | /* | |
402 | * Compute number of ticks in the specified amount of time. | |
403 | */ | |
404 | int | |
405 | tvtohz(tv) | |
406 | struct timeval *tv; | |
407 | { | |
408 | register unsigned long ticks; | |
409 | register long sec, usec; | |
410 | ||
411 | /* | |
412 | * If the number of usecs in the whole seconds part of the time | |
413 | * difference fits in a long, then the total number of usecs will | |
414 | * fit in an unsigned long. Compute the total and convert it to | |
415 | * ticks, rounding up and adding 1 to allow for the current tick | |
416 | * to expire. Rounding also depends on unsigned long arithmetic | |
417 | * to avoid overflow. | |
418 | * | |
419 | * Otherwise, if the number of ticks in the whole seconds part of | |
420 | * the time difference fits in a long, then convert the parts to | |
421 | * ticks separately and add, using similar rounding methods and | |
422 | * overflow avoidance. This method would work in the previous | |
423 | * case but it is slightly slower and assumes that hz is integral. | |
424 | * | |
425 | * Otherwise, round the time difference down to the maximum | |
426 | * representable value. | |
427 | * | |
428 | * If ints have 32 bits, then the maximum value for any timeout in | |
429 | * 10ms ticks is 248 days. | |
430 | */ | |
431 | sec = tv->tv_sec; | |
432 | usec = tv->tv_usec; | |
433 | if (usec < 0) { | |
434 | sec--; | |
435 | usec += 1000000; | |
436 | } | |
437 | if (sec < 0) { | |
438 | #ifdef DIAGNOSTIC | |
439 | if (usec > 0) { | |
440 | sec++; | |
441 | usec -= 1000000; | |
442 | } | |
443 | printf("tvotohz: negative time difference %ld sec %ld usec\n", | |
444 | sec, usec); | |
445 | #endif | |
446 | ticks = 1; | |
447 | } else if (sec <= LONG_MAX / 1000000) | |
448 | ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) | |
449 | / tick + 1; | |
450 | else if (sec <= LONG_MAX / hz) | |
451 | ticks = sec * hz | |
452 | + ((unsigned long)usec + (tick - 1)) / tick + 1; | |
453 | else | |
454 | ticks = LONG_MAX; | |
455 | if (ticks > INT_MAX) | |
456 | ticks = INT_MAX; | |
457 | return ((int)ticks); | |
458 | } | |
459 | ||
460 | ||
461 | /* | |
462 | * Start profiling on a process. | |
463 | * | |
464 | * Kernel profiling passes kernel_proc which never exits and hence | |
465 | * keeps the profile clock running constantly. | |
466 | */ | |
467 | void | |
468 | startprofclock(p) | |
469 | register struct proc *p; | |
470 | { | |
471 | if ((p->p_flag & P_PROFIL) == 0) | |
472 | p->p_flag |= P_PROFIL; | |
473 | } | |
474 | ||
475 | /* | |
476 | * Stop profiling on a process. | |
477 | */ | |
478 | void | |
479 | stopprofclock(p) | |
480 | register struct proc *p; | |
481 | { | |
482 | if (p->p_flag & P_PROFIL) | |
483 | p->p_flag &= ~P_PROFIL; | |
484 | } | |
485 | ||
486 | void | |
487 | bsd_uprofil(struct time_value *syst, unsigned int pc) | |
488 | { | |
489 | struct proc *p = current_proc(); | |
490 | int ticks; | |
491 | struct timeval *tv; | |
492 | struct timeval st; | |
493 | ||
494 | if (p == NULL) | |
495 | return; | |
496 | if ( !(p->p_flag & P_PROFIL)) | |
497 | return; | |
498 | ||
499 | st.tv_sec = syst->seconds; | |
500 | st.tv_usec = syst->microseconds; | |
501 | ||
502 | tv = &(p->p_stats->p_ru.ru_stime); | |
503 | ||
504 | ticks = ((tv->tv_sec - st.tv_sec) * 1000 + | |
505 | (tv->tv_usec - st.tv_usec) / 1000) / | |
506 | (tick / 1000); | |
507 | if (ticks) | |
508 | addupc_task(p, pc, ticks); | |
509 | } | |
510 | ||
511 | void | |
512 | get_procrustime(time_value_t *tv) | |
513 | { | |
514 | struct proc *p = current_proc(); | |
515 | struct timeval st; | |
516 | ||
517 | if (p == NULL) | |
518 | return; | |
519 | if ( !(p->p_flag & P_PROFIL)) | |
520 | return; | |
521 | ||
522 | st = p->p_stats->p_ru.ru_stime; | |
523 | ||
524 | tv->seconds = st.tv_sec; | |
525 | tv->microseconds = st.tv_usec; | |
526 | } |