]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/timer.h
xnu-1504.7.4.tar.gz
[apple/xnu.git] / osfmk / kern / timer.h
index 776fb5d76c47b5f49981f14a0aef17439c5db7f1..abbfcb5e398e3e5441817079a027f836cbfcde88 100644 (file)
@@ -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@
 #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.
- */
-
-#define TIMER_RATE     1000000
-#define TIMER_HIGH_UNIT        TIMER_RATE
-#undef TIMER_ADJUST
-
-#else  /* STAT_TIME */
-/*
- *     Machine dependent definitions based on hardware support.
- */
-
-#include <machine/timer.h>
-
-#endif /* STAT_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;
+#if    !STAT_TIME
+       uint64_t        tstamp;
+#endif /* STAT_TIME */
+#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);
 
-/* Switch to a new timer */
-extern void            timer_switch(
-                               timer_t         new_timer);
-#endif /* STAT_TIME */
+#include <kern/macro_help.h>
 
-/* Initialize timer module */
-extern void            init_timers(void);
+/* Advance a timer by a 32 bit value */
+#define TIMER_BUMP(timer, ticks)                                                               \
+MACRO_BEGIN                                                                                                            \
+       uint32_t        old_low, low;                                                                   \
+                                                                                                                               \
+       old_low = (timer)->low_bits;                                                            \
+       low = old_low + (ticks);                                                                        \
+       if (low < old_low)                                                                                      \
+               timer_update((timer), (timer)->high_bits + 1, low);             \
+       else                                                                                                            \
+               (timer)->low_bits = low;                                                                \
+MACRO_END
 
-/*
- * Initializes a single timer.
- */
-extern void            timer_init(
-                               timer_t this_timer);
+#define timer_start(timer, tstamp)
+#define timer_stop(timer, tstamp)
+#define timer_switch(timer, tstamp, new_timer)
+#define thread_timer_event(tstamp, new_timer)
 
-/* Normalize timer value */
-extern void            timer_normalize(
-                               timer_t timer);
+#else  /* STAT_TIME */
 
-/* Read value of timer into tv */
-extern void            timer_read(
-                               timer_t timer,
-                               time_value_t *tv);
+#define        TIMER_BUMP(timer, ticks)
 
-/* Read thread times */
-extern void            thread_read_times(
-                               thread_t        thread,
-                               time_value_t    *user_time_p,
-                               time_value_t    *system_time_p);
+/* Start a timer by setting the timestamp */
+extern void            timer_start(
+                                       timer_t         timer,
+                                       uint64_t        tstamp);
 
-/* Compute timer difference */
-extern unsigned                timer_delta(
-                               timer_t         timer,
-                               timer_save_t    save);
+/* Stop a timer by updating from the timestamp */
+extern void            timer_stop(
+                                       timer_t         timer,
+                                       uint64_t        tstamp);
 
-#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
+/* Update the timer and start a new one */
+extern void            timer_switch(
+                                       timer_t         timer,
+                                       uint64_t        tstamp,
+                                       timer_t         new_timer);
 
-#else  /* STAT_TIME */
-/*
- *     Exported hardware interface to timers
- */
-/* Time trap entry */
-extern void            time_trap_uentry(
-                               unsigned        ts);
+/* Update the thread timer at an event */
+extern void            thread_timer_event(
+                                       uint64_t        tstamp,
+                                       timer_t         new_timer);
 
-/* Time trap exit */
-extern void            time_trap_uexit(
-                               unsigned        ts);
+#endif /* STAT_TIME */
 
-/* Time interrupt entry */
-extern timer_t         time_int_entry(
-                               unsigned        ts,
-                               timer_t         new_timer);
+/* Initialize a timer */
+extern void            timer_init(
+                                       timer_t         timer);
 
-/* Time interrrupt exit */
-extern void            time_int_exit(
-                               unsigned        ts,
-                               timer_t         old_timer);
+/* Update a saved timer value and return delta to current value */
+extern uint64_t        timer_delta(
+                                       timer_t         timer,
+                                       uint64_t        *save);
 
-#endif /* STAT_TIME */
+/* Advance a timer by a 64 bit value */
+extern void            timer_advance(
+                                       timer_t         timer,
+                                       uint64_t        delta);
 
 /*
- *     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().
+ *     Exported hardware interface to timers
  */
 
-#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_ */