]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/timer.h
xnu-344.tar.gz
[apple/xnu.git] / osfmk / kern / timer.h
CommitLineData
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/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52
53#ifndef _KERN_TIMER_H_
54#define _KERN_TIMER_H_
55
56#include <cpus.h>
57#include <stat_time.h>
58
59#include <kern/macro_help.h>
60#include <kern/kern_types.h>
61
62#if STAT_TIME
63/*
64 * Statistical timer definitions - use microseconds in timer, seconds
65 * in high unit field. No adjustment needed to convert to time_value_t
66 * as a result. Service timers once an hour.
67 */
68
69#define TIMER_RATE 1000000
70#define TIMER_HIGH_UNIT TIMER_RATE
71#undef TIMER_ADJUST
72
73#else /* STAT_TIME */
74/*
75 * Machine dependent definitions based on hardware support.
76 */
77
78#include <machine/timer.h>
79
80#endif /* STAT_TIME */
81
82/*
83 * Definitions for accurate timers. high_bits_check is a copy of
84 * high_bits that allows reader to verify that values read are ok.
85 */
86
87struct timer {
88 unsigned low_bits;
89 unsigned high_bits;
90 unsigned high_bits_check;
91 unsigned tstamp;
92};
93
94typedef struct timer timer_data_t;
95typedef struct timer *timer_t;
96
97/*
98 * Mask to check if low_bits is in danger of overflowing
99 */
100
101#define TIMER_LOW_FULL 0x80000000
102
103/*
104 * Kernel timers and current timer array. [Exported]
105 */
106
107extern timer_t current_timer[NCPUS];
108extern timer_data_t kernel_timer[NCPUS];
109
110/*
111 * save structure for timer readings. This is used to save timer
112 * readings for elapsed time computations.
113 */
114
115struct timer_save {
116 unsigned low;
117 unsigned high;
118};
119
120typedef struct timer_save timer_save_data_t, *timer_save_t;
121
122/*
123 * Exported kernel interface to timers
124 */
125
126#if STAT_TIME
127#define start_timer(timer)
128#define timer_switch(timer)
129#else /* STAT_TIME */
130/* Start timer for this cpu */
131extern void start_timer(
132 timer_t timer);
133
134/* Switch to a new timer */
135extern void timer_switch(
136 timer_t new_timer);
137#endif /* STAT_TIME */
138
139/* Initialize timer module */
140extern void init_timers(void);
141
142/*
143 * Initializes a single timer.
144 */
145extern void timer_init(
146 timer_t this_timer);
147
148/* Normalize timer value */
149extern void timer_normalize(
150 timer_t timer);
151
152/* Read value of timer into tv */
153extern void timer_read(
154 timer_t timer,
155 time_value_t *tv);
156
157/* Read thread times */
158extern void thread_read_times(
159 thread_t thread,
160 time_value_t *user_time_p,
161 time_value_t *system_time_p);
162
163/* Compute timer difference */
164extern unsigned timer_delta(
165 timer_t timer,
166 timer_save_t save);
167
168#if STAT_TIME
169/*
170 * Macro to bump timer values.
171 */
172#define timer_bump(timer, usec) \
173MACRO_BEGIN \
174 (timer)->low_bits += usec; \
175 if ((timer)->low_bits & TIMER_LOW_FULL) { \
176 timer_normalize(timer); \
177 } \
178MACRO_END
179
180#else /* STAT_TIME */
181/*
182 * Exported hardware interface to timers
183 */
184/* Time trap entry */
185extern void time_trap_uentry(
186 unsigned ts);
187
188/* Time trap exit */
189extern void time_trap_uexit(
190 unsigned ts);
191
192/* Time interrupt entry */
193extern timer_t time_int_entry(
194 unsigned ts,
195 timer_t new_timer);
196
197/* Time interrrupt exit */
198extern void time_int_exit(
199 unsigned ts,
200 timer_t old_timer);
201
202#endif /* STAT_TIME */
203
204/*
205 * TIMER_DELTA finds the difference between a timer and a saved value,
206 * and updates the saved value. Look at high_bits check field after
207 * reading low because that's the first written by a normalize
208 * operation; this isn't necessary for current usage because
209 * this macro is only used when the timer can't be normalized:
210 * thread is not running, or running thread calls it on itself at
211 * splsched().
212 */
213
214#define TIMER_DELTA(timer, save, result) \
215MACRO_BEGIN \
216 register unsigned temp; \
217 \
218 temp = (timer).low_bits; \
219 if ((save).high != (timer).high_bits_check) { \
220 result += timer_delta(&(timer), &(save)); \
221 } \
222 else { \
223 result += temp - (save).low; \
224 (save).low = temp; \
225 } \
226MACRO_END
227
228#endif /* _KERN_TIMER_H_ */