FreeBSD/Linux Kernel Cross Reference
sys/kern/timer.h
1 /*
2 * Mach Operating System
3 * Copyright (c) 1993-1987 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * HISTORY
28 * $Log: timer.h,v $
29 * Revision 2.6 93/11/17 17:33:00 dbg
30 * If no machine-dependent timer is used: maintain timer in seconds
31 * and nanoseconds, and use microseconds (or the nearest binary
32 * approximation thereof) for the scheduling units.
33 * [93/01/25 dbg]
34 *
35 * Revision 2.5 92/05/21 17:17:07 jfriedl
36 * Appended 'U' to constants that would otherwise be signed.
37 * [92/05/16 jfriedl]
38 *
39 * Revision 2.4 91/05/14 16:50:00 mrt
40 * Correcting copyright
41 *
42 * Revision 2.3 91/02/05 17:31:10 mrt
43 * Changed to new Mach copyright
44 * [91/02/01 16:21:08 mrt]
45 *
46 * Revision 2.2 90/08/07 18:00:13 rpd
47 * Picked up new timer representation.
48 *
49 * Revision 2.1 89/08/03 15:57:34 rwd
50 * Created.
51 *
52 * 27-Oct-88 David Black (dlb) at Carnegie-Mellon University
53 * Added TIMER_LOW_FULL mask to check for possible overflow of
54 * low_bits field. Removed TIMER_SERVICE flag.
55 *
56 * Revision 2.3 88/08/24 02:49:17 mwyoung
57 * Adjusted include file references.
58 * [88/08/17 02:25:43 mwyoung]
59 *
60 *
61 * 7-Apr-88 David Black (dlb) at Carnegie-Mellon University
62 * Added interface definitions and null routines for STAT_TIME.
63 *
64 * 19-Feb-88 David Black (dlb) at Carnegie-Mellon University
65 * Renamed fields, added timer_save structure.
66 *
67 * 31-May-87 Avadis Tevanian (avie) at Carnegie-Mellon University
68 * machine/machtimer.h -> machine/timer.h
69 *
70 * 23-Feb-87 David L. Black (dlb) at Carnegie-Mellon University
71 * Created.
72 */
73
74 #ifndef _KERN_TIMER_H_
75 #define _KERN_TIMER_H_
76
77 #include <cpus.h>
78 #include <stat_time.h>
79
80 #include <mach/time_spec.h>
81 #include <kern/macro_help.h>
82
83 #if STAT_TIME
84 /*
85 * Statistical timer definitions - use nanoseconds in timer, seconds
86 * in high unit field. Use microseconds as scheduling unit: or,
87 * since we use a shift, 10**9/1024.
88 */
89
90 /*
91 * Timers themselves are counted in seconds and nanoseconds.
92 */
93 #define TIMER_HIGH_UNIT NANOSEC_PER_SEC
94
95 /*
96 * USAGE_RATE is the unit for cpu_usage and sched_usage.
97 * It is nanoseconds >> 10 (10**9 / 1024).
98 */
99 #define USAGE_RATE (TIMER_HIGH_UNIT >> 10)
100 /* (10**9)/(2**10) == 976562.5 */
101
102 #define TIMER_LOW_TO_USAGE(nanos) ((nanos) >> 10)
103 #define TIMER_HIGH_TO_USAGE(secs) ((secs) * USAGE_RATE)
104
105 /* (((secs)*TIMER_HIGH_UNIT)>>10), but this will overflow */
106
107 #undef TIMER_TO_TIMESPEC /* timers == time_specs */
108
109 #else /* STAT_TIME */
110 /*
111 * Machine dependent definitions based on hardware support.
112 */
113
114 #include <machine/timer.h>
115
116 #endif /* STAT_TIME */
117
118 /*
119 * Definitions for accurate timers. high_bits_check is a copy of
120 * high_bits that allows reader to verify that values read are ok.
121 */
122
123 struct timer {
124 unsigned int low_bits;
125 unsigned int high_bits;
126 unsigned int high_bits_check;
127 unsigned int tstamp;
128 };
129
130 typedef struct timer timer_data_t;
131 typedef struct timer *timer_t;
132
133 /*
134 * Mask to check if low_bits is in danger of overflowing
135 */
136
137 #define TIMER_LOW_FULL 0x80000000U /* assumes 32 bits! */
138
139 /*
140 * Kernel timers and current timer array. [Exported]
141 */
142
143 extern timer_t current_timer[NCPUS];
144 extern timer_data_t kernel_timer[NCPUS];
145
146 /*
147 * save structure for timer readings. This is used to save timer
148 * readings for elapsed time computations.
149 */
150
151 struct timer_save {
152 unsigned int low;
153 unsigned int high;
154 };
155
156 typedef struct timer_save timer_save_data_t, *timer_save_t;
157
158 /*
159 * Exported kernel interface to timers
160 */
161
162 extern void init_timers(void);
163
164 #if STAT_TIME
165 #define start_timer(timer)
166 #define timer_switch(timer)
167 #else /* STAT_TIME */
168 extern void start_timer(timer_t timer);
169 extern void timer_switch(timer_t new_timer);
170 #endif /* STAT_TIME */
171
172 extern void timer_init(
173 timer_t timer);
174
175 extern void timer_read(
176 timer_t timer,
177 time_spec_t *tv);
178
179 extern void thread_read_times(
180 thread_t thread,
181 time_spec_t *user_time_p,
182 time_spec_t *system_time_p);
183
184 extern unsigned int timer_delta(
185 timer_t timer,
186 timer_save_t save);
187
188 #if STAT_TIME
189 /*
190 * Macro to bump timer values.
191 */
192 #define timer_bump(timer, nsec) \
193 MACRO_BEGIN \
194 (timer)->low_bits += nsec; \
195 if ((timer)->low_bits & TIMER_LOW_FULL) { \
196 timer_normalize(timer); \
197 } \
198 MACRO_END
199
200 extern void
201 timer_normalize(timer_t);
202
203 #else /* STAT_TIME */
204 /*
205 * Exported hardware interface to timers
206 */
207 extern void time_trap_uentry(unsigned int timestamp);
208 extern void time_trap_uexit(unsigned int timestamp);
209 extern timer_t time_int_entry(unsigned int timestamp, timer_t new_timer);
210 extern void time_int_exit(unsigned int timestamp, timer_t old_timer);
211 #endif /* STAT_TIME */
212
213 /*
214 * TIMER_DELTA finds the difference between a timer and a saved value,
215 * and updates the saved value. Look at high_bits check field after
216 * reading low because that's the first written by a normalize
217 * operation; this isn't necessary for current usage because
218 * this macro is only used when the timer can't be normalized:
219 * thread is not running, or running thread calls it on itself at
220 * splsched().
221 */
222
223 #define TIMER_DELTA(timer, save, result) \
224 MACRO_BEGIN \
225 register unsigned int temp; \
226 \
227 temp = (timer).low_bits; \
228 if ((save).high != (timer).high_bits_check) { \
229 result += timer_delta(&(timer), &(save)); \
230 } \
231 else { \
232 result += TIMER_LOW_TO_USAGE(temp - (save).low); \
233 (save).low = temp; \
234 } \
235 MACRO_END
236
237 #endif /* _KERN_TIMER_H_ */
Cache object: e612cc69124c595f0b29a9bb8645e37d
|