timer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989,1988,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. #ifndef _KERN_TIMER_H_
  27. #define _KERN_TIMER_H_
  28. #include <kern/macros.h>
  29. #if STAT_TIME
  30. /*
  31. * Statistical timer definitions - use microseconds in timer, seconds
  32. * in high unit field. No adjustment needed to convert to time_value_t
  33. * as a result. Service timers once an hour.
  34. */
  35. /*
  36. * TIMER_MAX is needed if a 32-bit rollover timer needs to be adjusted for
  37. * maximum value.
  38. */
  39. #undef TIMER_MAX
  40. /*
  41. * TIMER_RATE is the rate of the timer in ticks per second. It is used to
  42. * calculate percent cpu usage.
  43. */
  44. #define TIMER_RATE 1000000
  45. /*
  46. * TIMER_HIGH_UNIT is the unit for high_bits in terms of low_bits.
  47. * Setting it to TIMER_RATE makes the high unit seconds.
  48. */
  49. #define TIMER_HIGH_UNIT TIMER_RATE
  50. /*
  51. * TIMER_ADJUST is used to adjust the value of a timer after it has been
  52. * copied into a time_value_t. No adjustment is needed if high_bits is in
  53. * seconds.
  54. */
  55. #undef TIMER_ADJUST
  56. /*
  57. * MACHINE_TIMER_ROUTINES should defined if the timer routines are
  58. * implemented in machine-dependent code (e.g. assembly language).
  59. */
  60. #undef MACHINE_TIMER_ROUTINES
  61. #else /* STAT_TIME */
  62. /*
  63. * Machine dependent definitions based on hardware support.
  64. */
  65. #include <machine/timer.h>
  66. #endif /* STAT_TIME */
  67. /*
  68. * Definitions for accurate timers. high_bits_check is a copy of
  69. * high_bits that allows reader to verify that values read are ok.
  70. */
  71. struct timer {
  72. unsigned low_bits;
  73. unsigned high_bits;
  74. unsigned high_bits_check;
  75. unsigned tstamp;
  76. };
  77. typedef struct timer timer_data_t;
  78. //CONFICT:: //typedef struct timer *timer_t;
  79. /*
  80. * Mask to check if low_bits is in danger of overflowing
  81. */
  82. #define TIMER_LOW_FULL 0x80000000U
  83. /*
  84. * Kernel timers and current timer array. [Exported]
  85. */
  86. extern timer_t current_timer[NCPUS];
  87. extern timer_data_t kernel_timer[NCPUS];
  88. /*
  89. * save structure for timer readings. This is used to save timer
  90. * readings for elapsed time computations.
  91. */
  92. struct timer_save {
  93. unsigned low;
  94. unsigned high;
  95. };
  96. typedef struct timer_save timer_save_data_t, *timer_save_t;
  97. /*
  98. * Exported kernel interface to timers
  99. */
  100. #if STAT_TIME
  101. #define start_timer(timer)
  102. #define timer_switch(timer)
  103. #else /* STAT_TIME */
  104. extern void start_timer(timer_t);
  105. extern void timer_switch(timer_t);
  106. #endif /* STAT_TIME */
  107. extern void timer_read(timer_t, time_value_t *);
  108. extern void thread_read_times(thread_t, time_value_t *, time_value_t *);
  109. extern unsigned timer_delta(timer_t, timer_save_t);
  110. extern void timer_normalize(timer_t);
  111. extern void timer_init(timer_t);
  112. #if STAT_TIME
  113. /*
  114. * Macro to bump timer values.
  115. */
  116. #define timer_bump(timer, usec) \
  117. MACRO_BEGIN \
  118. (timer)->low_bits += usec; \
  119. if ((timer)->low_bits & TIMER_LOW_FULL) { \
  120. timer_normalize(timer); \
  121. } \
  122. MACRO_END
  123. #else /* STAT_TIME */
  124. /*
  125. * Exported hardware interface to timers
  126. */
  127. extern void time_trap_uentry(unsigned);
  128. extern void time_trap_uexit(int);
  129. extern timer_t time_int_entry(unsigned, timer_t);
  130. extern void time_int_exit(unsigned, timer_t);
  131. #endif /* STAT_TIME */
  132. /*
  133. * TIMER_DELTA finds the difference between a timer and a saved value,
  134. * and updates the saved value. Look at high_bits check field after
  135. * reading low because that's the first written by a normalize
  136. * operation; this isn't necessary for current usage because
  137. * this macro is only used when the timer can't be normalized:
  138. * thread is not running, or running thread calls it on itself at
  139. * splsched().
  140. */
  141. #define TIMER_DELTA(timer, save, result) \
  142. MACRO_BEGIN \
  143. unsigned temp; \
  144. \
  145. temp = (timer).low_bits; \
  146. if ((save).high != (timer).high_bits_check) { \
  147. result += timer_delta(&(timer), &(save)); \
  148. } \
  149. else { \
  150. result += temp - (save).low; \
  151. (save).low = temp; \
  152. } \
  153. MACRO_END
  154. extern void init_timers(void);
  155. void timer_init(timer_t this_timer);
  156. #endif /* _KERN_TIMER_H_ */