High_Res_Timer.inl

Go to the documentation of this file.
00001 // -*- C++ -*- */
00002 //
00003 // $Id: High_Res_Timer.inl 76988 2007-02-09 21:45:26Z kitty $
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,
00057                                     ACE_OS::gethrtime (op));
00058   return tv;
00059 }
00060 
00061 
00062 // Get the current high res timer as the time of day. This is intended
00063 // to be used for a gettimeofday replacement in ACE_Timer_Queue and
00064 // derived classes so the timers will bebased on high res timers rather
00065 // than wall clock time. It uses the ACE_High_Res_Timer::gettimeofday
00066 // function, which is deprecated. If it gets removed, please move the
00067 // code down here, intact.
00068 ACE_INLINE ACE_Time_Value
00069 ACE_High_Res_Timer::gettimeofday_hr (void)
00070 {
00071   return ACE_High_Res_Timer::gettimeofday ();
00072 }
00073 
00074 
00075 ACE_INLINE ACE_hrtime_t
00076 ACE_High_Res_Timer::gettime (const ACE_OS::ACE_HRTimer_Op op)
00077 {
00078 #if defined (ACE_WIN32)
00079   // Get the global scale factor if there isn't one yet.
00080   if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
00081     ACE_High_Res_Timer::global_scale_factor ();
00082 
00083   // If there isn't a high-res timer, use gettimeofday ();
00084   if (ACE_High_Res_Timer::global_scale_factor_status_ == -1)
00085     {
00086       ACE_Time_Value tv = ACE_OS::gettimeofday ();
00087       // Return the time in microseconds because the global_scale_factor_
00088       // is 1.
00089       return tv.sec () * ACE_ONE_SECOND_IN_USECS + tv.usec ();
00090     }
00091 #endif /* ACE_WIN32 */
00092 
00093   return ACE_OS::gethrtime (op);
00094 }
00095 
00096 ACE_INLINE ACE_hrtime_t
00097 ACE_High_Res_Timer::elapsed_hrtime (const ACE_hrtime_t end,
00098                                     const ACE_hrtime_t start)
00099 {
00100   if (end > start)
00101     return end - start;
00102   return (~start + 1 + end);     // Wrapped-around counter diff
00103 }
00104 
00105 ACE_INLINE
00106 ACE_High_Res_Timer::~ACE_High_Res_Timer (void)
00107 {
00108 }
00109 
00110 ACE_INLINE void
00111 ACE_High_Res_Timer::start (const ACE_OS::ACE_HRTimer_Op op)
00112 {
00113   ACE_TRACE ("ACE_High_Res_Timer::start");
00114   this->start_ = ACE_High_Res_Timer::gettime (op);
00115 }
00116 
00117 ACE_INLINE void
00118 ACE_High_Res_Timer::stop (const ACE_OS::ACE_HRTimer_Op op)
00119 {
00120   ACE_TRACE ("ACE_High_Res_Timer::stop");
00121   this->end_ = ACE_High_Res_Timer::gettime (op);
00122 }
00123 
00124 ACE_INLINE void
00125 ACE_High_Res_Timer::start_incr (const ACE_OS::ACE_HRTimer_Op op)
00126 {
00127   ACE_TRACE ("ACE_High_Res_Timer::start_incr");
00128   this->start_incr_ = ACE_High_Res_Timer::gettime (op);
00129 }
00130 
00131 ACE_INLINE void
00132 ACE_High_Res_Timer::stop_incr (const ACE_OS::ACE_HRTimer_Op op)
00133 {
00134   ACE_TRACE ("ACE_High_Res_Timer::stop_incr");
00135   this->total_ +=
00136     ACE_High_Res_Timer::elapsed_hrtime (ACE_High_Res_Timer::gettime (op),
00137                                         this->start_incr_);
00138 }
00139 
00140 ACE_INLINE void
00141 ACE_High_Res_Timer::elapsed_microseconds (ACE_hrtime_t &usecs) const
00142 {
00143   ACE_hrtime_t elapsed = ACE_High_Res_Timer::elapsed_hrtime (this->end_,
00144                                                              this->start_);
00145   usecs = (ACE_hrtime_t) (elapsed / global_scale_factor ());
00146 }
00147 
00148 ACE_INLINE void
00149 ACE_High_Res_Timer::global_scale_factor (ACE_UINT32 gsf)
00150 {
00151   global_scale_factor_ = gsf;
00152 }
00153 
00154 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 12:05:28 2008 for ACE by doxygen 1.3.6