00001 // -*- C++ -*- */ 00002 // 00003 // $Id: High_Res_Timer.inl 89483 2010-03-15 09:48:01Z johnnyw $ 00004 00005 #include "ace/Global_Macros.h" 00006 00007 #if defined (ACE_WIN32) 00008 # include "ace/OS_NS_sys_time.h" 00009 #endif /* ACE_WIN32 */ 00010 00011 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00012 00013 // Be very careful before changing the calculations inside 00014 // ACE_High_Res_Timer. The precision matters and we are using integer 00015 // calculations not floating point. Also look closely at the emulated 64 00016 // bit int class (inside Basic_Types{h,i,cpp} before changing 00017 // anything. It's operator/ only returns 32 bits not 64 bits, among 00018 // other things. 00019 00020 ACE_INLINE void 00021 ACE_High_Res_Timer::hrtime_to_tv (ACE_Time_Value &tv, 00022 const ACE_hrtime_t hrt) 00023 { 00024 // The following are based on the units of global_scale_factor_ 00025 // being 1/microsecond. Therefore, dividing by it converts 00026 // clock ticks to microseconds. 00027 tv.sec ((long) (hrt / (ACE_UINT32) ACE_HR_SCALE_CONVERSION / 00028 global_scale_factor ())); 00029 00030 // Calculate usec in a manner that's compatible with ACE_U_LongLong. 00031 // hrt = (tv.sec * ACE_ONE_SECOND_IN_USECS + tv.usec) * global_scale_factor_ 00032 // tv.usec = hrt / global_scale_factor_ - tv.sec * ACE_ONE_SECOND_IN_USECS 00033 // That first term will be lossy, so factor out global_scale_factor_: 00034 // tv.usec = (hrt - tv.sec * ACE_ONE_SECOND_IN_USECS * global_scale_factor_)/ 00035 // global_scale_factor 00036 ACE_hrtime_t tmp = tv.sec (); 00037 tmp *= ((ACE_UINT32) ACE_HR_SCALE_CONVERSION * global_scale_factor ()); 00038 tv.usec ((long) ((hrt - tmp) / global_scale_factor ())); 00039 } 00040 00041 00042 ACE_INLINE ACE_Time_Value 00043 ACE_High_Res_Timer::gettimeofday (const ACE_OS::ACE_HRTimer_Op op) 00044 { 00045 #if defined (ACE_WIN32) 00046 // Get the global scale factor if there isn't one yet. 00047 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0) 00048 ACE_High_Res_Timer::global_scale_factor (); 00049 00050 // If there isn't a high-res timer, use gettimeofday (); 00051 if (ACE_High_Res_Timer::global_scale_factor_status_ == -1) 00052 return ACE_OS::gettimeofday (); 00053 #endif /* ACE_WIN32 */ 00054 00055 ACE_Time_Value tv; 00056 ACE_High_Res_Timer::hrtime_to_tv (tv, ACE_OS::gethrtime (op)); 00057 return tv; 00058 } 00059 00060 00061 // Get the current high res timer as the time of day. This is intended 00062 // to be used for a gettimeofday replacement in ACE_Timer_Queue and 00063 // derived classes so the timers will bebased on high res timers rather 00064 // than wall clock time. It uses the ACE_High_Res_Timer::gettimeofday 00065 // function, which is deprecated. If it gets removed, please move the 00066 // code down here, intact. 00067 ACE_INLINE ACE_Time_Value 00068 ACE_High_Res_Timer::gettimeofday_hr (void) 00069 { 00070 return ACE_High_Res_Timer::gettimeofday (); 00071 } 00072 00073 00074 ACE_INLINE ACE_hrtime_t 00075 ACE_High_Res_Timer::gettime (const ACE_OS::ACE_HRTimer_Op op) 00076 { 00077 #if defined (ACE_WIN32) 00078 // Get the global scale factor if there isn't one yet. 00079 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0) 00080 ACE_High_Res_Timer::global_scale_factor (); 00081 00082 // If there isn't a high-res timer, use gettimeofday (); 00083 if (ACE_High_Res_Timer::global_scale_factor_status_ == -1) 00084 { 00085 ACE_Time_Value const tv = ACE_OS::gettimeofday (); 00086 // Return the time in microseconds because the global_scale_factor_ 00087 // is 1. 00088 return tv.sec () * ACE_ONE_SECOND_IN_USECS + tv.usec (); 00089 } 00090 #endif /* ACE_WIN32 */ 00091 00092 return ACE_OS::gethrtime (op); 00093 } 00094 00095 ACE_INLINE ACE_hrtime_t 00096 ACE_High_Res_Timer::elapsed_hrtime (const ACE_hrtime_t end, 00097 const ACE_hrtime_t start) 00098 { 00099 if (end > start) 00100 return end - start; 00101 return (~start + 1 + end); // Wrapped-around counter diff 00102 } 00103 00104 ACE_INLINE 00105 ACE_High_Res_Timer::~ACE_High_Res_Timer (void) 00106 { 00107 } 00108 00109 ACE_INLINE void 00110 ACE_High_Res_Timer::start (const ACE_OS::ACE_HRTimer_Op op) 00111 { 00112 ACE_TRACE ("ACE_High_Res_Timer::start"); 00113 this->start_ = ACE_High_Res_Timer::gettime (op); 00114 } 00115 00116 ACE_INLINE void 00117 ACE_High_Res_Timer::stop (const ACE_OS::ACE_HRTimer_Op op) 00118 { 00119 ACE_TRACE ("ACE_High_Res_Timer::stop"); 00120 this->end_ = ACE_High_Res_Timer::gettime (op); 00121 } 00122 00123 ACE_INLINE void 00124 ACE_High_Res_Timer::start_incr (const ACE_OS::ACE_HRTimer_Op op) 00125 { 00126 ACE_TRACE ("ACE_High_Res_Timer::start_incr"); 00127 this->start_incr_ = ACE_High_Res_Timer::gettime (op); 00128 } 00129 00130 ACE_INLINE void 00131 ACE_High_Res_Timer::stop_incr (const ACE_OS::ACE_HRTimer_Op op) 00132 { 00133 ACE_TRACE ("ACE_High_Res_Timer::stop_incr"); 00134 this->total_ += 00135 ACE_High_Res_Timer::elapsed_hrtime (ACE_High_Res_Timer::gettime (op), 00136 this->start_incr_); 00137 } 00138 00139 ACE_INLINE void 00140 ACE_High_Res_Timer::elapsed_microseconds (ACE_hrtime_t &usecs) const 00141 { 00142 ACE_hrtime_t elapsed = ACE_High_Res_Timer::elapsed_hrtime (this->end_, 00143 this->start_); 00144 usecs = (ACE_hrtime_t) (elapsed / global_scale_factor ()); 00145 } 00146 00147 ACE_INLINE void 00148 ACE_High_Res_Timer::global_scale_factor (ACE_UINT32 gsf) 00149 { 00150 global_scale_factor_ = gsf; 00151 } 00152 00153 ACE_END_VERSIONED_NAMESPACE_DECL