/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
*
- * @APPLE_LICENSE_HEADER_START@
- *
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License"). You may not use this file except in compliance with the
- * License. Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
- *
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ *
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License.
- *
- * @APPLE_LICENSE_HEADER_END@
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* @OSF_COPYRIGHT@
*/
-/*
+/*
* Mach Operating System
* Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
* All Rights Reserved.
- *
+ *
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
- *
+ *
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
+ *
* Carnegie Mellon requests users of this software to return to
- *
+ *
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
- *
+ *
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
*/
-#ifndef _KERN_TIMER_H_
+#ifndef _KERN_TIMER_H_
#define _KERN_TIMER_H_
-#include <cpus.h>
-#include <stat_time.h>
-
-#include <kern/macro_help.h>
#include <kern/kern_types.h>
-#if STAT_TIME
/*
- * Statistical timer definitions - use microseconds in timer, seconds
- * in high unit field. No adjustment needed to convert to time_value_t
- * as a result. Service timers once an hour.
+ * Some platforms have very expensive timebase routines. An optimization
+ * is to avoid switching timers on kernel exit/entry, which results in all
+ * time billed to the system timer. However, when exposed to userspace,
+ * we report as user time to indicate that work was done on behalf of
+ * userspace.
+ *
+ * Although this policy is implemented as a global variable, we snapshot it
+ * at key points in the thread structure (when the thread is locked and
+ * executing in the kernel) to avoid imbalances.
*/
+extern int precise_user_kernel_time;
-#define TIMER_RATE 1000000
-#define TIMER_HIGH_UNIT TIMER_RATE
-#undef TIMER_ADJUST
-
-#else /* STAT_TIME */
/*
- * Machine dependent definitions based on hardware support.
+ * thread must be locked, or be the current executing thread, so that
+ * it doesn't transition from user to kernel while updating the
+ * thread-local value (or in kernel debugger context). In the future,
+ * we make take into account task-level or thread-level policy.
*/
-
-#include <machine/timer.h>
-
-#endif /* STAT_TIME */
+#define use_precise_user_kernel_time(thread) (precise_user_kernel_time)
/*
- * Definitions for accurate timers. high_bits_check is a copy of
- * high_bits that allows reader to verify that values read are ok.
+ * Definitions for high resolution timers.
*/
-struct timer {
- unsigned low_bits;
- unsigned high_bits;
- unsigned high_bits_check;
- unsigned tstamp;
-};
+#if __LP64__
+#define TIMER_ALIGNMENT
+#else
+#define TIMER_ALIGNMENT __attribute__((packed, aligned(4)))
+#endif
-typedef struct timer timer_data_t;
-typedef struct timer *timer_t;
+struct timer {
+ uint64_t tstamp;
+#if defined(__LP64__)
+ uint64_t all_bits;
+#else /* defined(__LP64__) */
+ /* A check word on the high portion allows atomic updates. */
+ uint32_t low_bits;
+ uint32_t high_bits;
+ uint32_t high_bits_check;
+#endif /* !defined(__LP64__) */
+} TIMER_ALIGNMENT;
+
+typedef struct timer timer_data_t, *timer_t;
/*
- * Mask to check if low_bits is in danger of overflowing
+ * Initialize the `timer`.
*/
-
-#define TIMER_LOW_FULL 0x80000000
+void timer_init(timer_t timer);
/*
- * Kernel timers and current timer array. [Exported]
+ * Start the `timer` at time `tstamp`.
*/
-
-extern timer_t current_timer[NCPUS];
-extern timer_data_t kernel_timer[NCPUS];
+void timer_start(timer_t timer, uint64_t tstamp);
/*
- * save structure for timer readings. This is used to save timer
- * readings for elapsed time computations.
+ * Stop the `timer` and update it with time `tstamp`.
*/
-
-struct timer_save {
- unsigned low;
- unsigned high;
-};
-
-typedef struct timer_save timer_save_data_t, *timer_save_t;
+void timer_stop(timer_t timer, uint64_t tstamp);
/*
- * Exported kernel interface to timers
+ * Update the `timer` at time `tstamp`, leaving it running.
*/
-
-#if STAT_TIME
-#define start_timer(timer)
-#define timer_switch(timer)
-#else /* STAT_TIME */
-/* Start timer for this cpu */
-extern void start_timer(
- timer_t timer);
-
-/* Switch to a new timer */
-extern void timer_switch(
- timer_t new_timer);
-#endif /* STAT_TIME */
-
-/* Initialize timer module */
-extern void init_timers(void);
+void timer_update(timer_t timer, uint64_t tstamp);
/*
- * Initializes a single timer.
+ * Update the `timer` with time `tstamp` and start `new_timer`.
*/
-extern void timer_init(
- timer_t this_timer);
-
-/* Normalize timer value */
-extern void timer_normalize(
- timer_t timer);
-
-/* Read value of timer into tv */
-extern void timer_read(
- timer_t timer,
- time_value_t *tv);
-
-/* Read thread times */
-extern void thread_read_times(
- thread_t thread,
- time_value_t *user_time_p,
- time_value_t *system_time_p);
-
-/* Compute timer difference */
-extern unsigned timer_delta(
- timer_t timer,
- timer_save_t save);
-
-#if STAT_TIME
-/*
- * Macro to bump timer values.
- */
-#define timer_bump(timer, usec) \
-MACRO_BEGIN \
- (timer)->low_bits += usec; \
- if ((timer)->low_bits & TIMER_LOW_FULL) { \
- timer_normalize(timer); \
- } \
-MACRO_END
-
-#else /* STAT_TIME */
+void timer_switch(timer_t timer, uint64_t tstamp, timer_t new_timer);
+
/*
- * Exported hardware interface to timers
+ * Update the thread timer at an "event," like a context switch, at time
+ * `tstamp`. This stops the current timer and starts the `new_timer` running.
+ *
+ * Must be called with interrupts disabled.
*/
-/* Time trap entry */
-extern void time_trap_uentry(
- unsigned ts);
-
-/* Time trap exit */
-extern void time_trap_uexit(
- unsigned ts);
-
-/* Time interrupt entry */
-extern timer_t time_int_entry(
- unsigned ts,
- timer_t new_timer);
-
-/* Time interrrupt exit */
-extern void time_int_exit(
- unsigned ts,
- timer_t old_timer);
-
-#endif /* STAT_TIME */
+void processor_timer_switch_thread(uint64_t tstamp, timer_t new_timer);
/*
- * TIMER_DELTA finds the difference between a timer and a saved value,
- * and updates the saved value. Look at high_bits check field after
- * reading low because that's the first written by a normalize
- * operation; this isn't necessary for current usage because
- * this macro is only used when the timer can't be normalized:
- * thread is not running, or running thread calls it on itself at
- * splsched().
+ * Return the difference between the `timer` and a previous value pointed to by
+ * `prev_in_cur_out`. Store the current value of the timer to
+ * `prev_in_cur_out`.
*/
+uint64_t timer_delta(timer_t timer, uint64_t *prev_in_cur_out);
-#define TIMER_DELTA(timer, save, result) \
-MACRO_BEGIN \
- register unsigned temp; \
- \
- temp = (timer).low_bits; \
- if ((save).high != (timer).high_bits_check) { \
- result += timer_delta(&(timer), &(save)); \
- } \
- else { \
- result += temp - (save).low; \
- (save).low = temp; \
- } \
-MACRO_END
-
-#endif /* _KERN_TIMER_H_ */
+/*
+ * Read the accumulated time of `timer`.
+ */
+#if defined(__LP64__)
+static inline
+uint64_t
+timer_grab(timer_t timer)
+{
+ return timer->all_bits;
+}
+#else /* defined(__LP64__) */
+uint64_t timer_grab(timer_t timer);
+#endif /* !defined(__LP64__) */
+
+#endif /* _KERN_TIMER_H_ */