X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/d7e50217d7adf6e52786a38bcaa4cd698cb9a79e..cc8bc92ae4a8e9f1a1ab61bf83d34ad8150b3405:/osfmk/kern/timer.h diff --git a/osfmk/kern/timer.h b/osfmk/kern/timer.h index 776fb5d76..a353c6c29 100644 --- a/osfmk/kern/timer.h +++ b/osfmk/kern/timer.h @@ -1,16 +1,19 @@ /* - * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2007 Apple Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ - * - * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. + * @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. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. + * 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 @@ -20,7 +23,7 @@ * Please see the License for the specific language governing rights and * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ @@ -56,176 +59,106 @@ #ifndef _KERN_TIMER_H_ #define _KERN_TIMER_H_ -#include -#include - -#include #include -#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 - -#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. A check + * word on the high portion allows atomic updates. */ struct timer { - unsigned low_bits; - unsigned high_bits; - unsigned high_bits_check; - unsigned tstamp; -}; - -typedef struct timer timer_data_t; -typedef struct timer *timer_t; - -/* - * Mask to check if low_bits is in danger of overflowing - */ - -#define TIMER_LOW_FULL 0x80000000 - -/* - * Kernel timers and current timer array. [Exported] - */ - -extern timer_t current_timer[NCPUS]; -extern timer_data_t kernel_timer[NCPUS]; - -/* - * save structure for timer readings. This is used to save timer - * readings for elapsed time computations. - */ - -struct timer_save { - unsigned low; - unsigned high; + uint64_t tstamp; +#if defined(__LP64__) + uint64_t all_bits; +#else + uint32_t low_bits; + uint32_t high_bits; + uint32_t high_bits_check; +#endif }; -typedef struct timer_save timer_save_data_t, *timer_save_t; +typedef struct timer timer_data_t, *timer_t; /* * Exported kernel interface to timers */ -#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); +/* Start a timer by setting the timestamp */ +extern void timer_start( + timer_t timer, + uint64_t tstamp); + +/* Stop a timer by updating from the timestamp */ +extern void timer_stop( + timer_t timer, + uint64_t tstamp); -/* Switch to a new timer */ +/* Update the timer and start a new one */ extern void timer_switch( - timer_t new_timer); -#endif /* STAT_TIME */ + timer_t timer, + uint64_t tstamp, + timer_t new_timer); -/* Initialize timer module */ -extern void init_timers(void); +/* Update the thread timer at an event */ +extern void thread_timer_event( + uint64_t tstamp, + timer_t new_timer); -/* - * Initializes a single timer. - */ +/* Initialize a 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); + timer_t timer); -/* Read thread times */ -extern void thread_read_times( - thread_t thread, - time_value_t *user_time_p, - time_value_t *system_time_p); +/* Update a saved timer value and return delta to current value */ +extern uint64_t timer_delta( + timer_t timer, + uint64_t *save); -/* Compute timer difference */ -extern unsigned timer_delta( - timer_t timer, - timer_save_t save); +/* Advance a timer by a 64 bit value */ +extern void timer_advance( + timer_t timer, + uint64_t delta); -#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 */ /* * Exported hardware interface to timers */ -/* 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 */ - -/* - * 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(). - */ -#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 +/* Read timer value */ +#if defined(__LP64__) +static inline uint64_t timer_grab( + timer_t timer) +{ + return timer->all_bits; +} +#else +extern uint64_t timer_grab( + timer_t timer); + +/* Update timer value */ +extern void timer_update( + timer_t timer, + uint32_t new_high, + uint32_t new_low); +#endif /* defined(__LP64__) */ #endif /* _KERN_TIMER_H_ */