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