3 // $Id: High_Res_Timer.inl 81138 2008-03-28 09:18:15Z johnnyw $
5 #include "ace/Global_Macros.h"
7 #if defined (ACE_WIN32)
8 # include "ace/OS_NS_sys_time.h"
11 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
13 // Be very careful before changing the calculations inside
14 // ACE_High_Res_Timer. The precision matters and we are using integer
15 // calculations not floating point. Also look closely at the emulated 64
16 // bit int class (inside Basic_Types{h,i,cpp} before changing
17 // anything. It's operator/ only returns 32 bits not 64 bits, among
21 ACE_High_Res_Timer::hrtime_to_tv (ACE_Time_Value &tv,
22 const ACE_hrtime_t hrt)
24 // The following are based on the units of global_scale_factor_
25 // being 1/microsecond. Therefore, dividing by it converts
26 // clock ticks to microseconds.
27 tv.sec ((long) (hrt / (ACE_UINT32) ACE_HR_SCALE_CONVERSION /
28 global_scale_factor ()));
30 // Calculate usec in a manner that's compatible with ACE_U_LongLong.
31 // hrt = (tv.sec * ACE_ONE_SECOND_IN_USECS + tv.usec) * global_scale_factor_
32 // tv.usec = hrt / global_scale_factor_ - tv.sec * ACE_ONE_SECOND_IN_USECS
33 // That first term will be lossy, so factor out global_scale_factor_:
34 // tv.usec = (hrt - tv.sec * ACE_ONE_SECOND_IN_USECS * global_scale_factor_)/
35 // global_scale_factor
36 ACE_hrtime_t tmp = tv.sec ();
37 tmp *= ((ACE_UINT32) ACE_HR_SCALE_CONVERSION * global_scale_factor ());
38 tv.usec ((long) ((hrt - tmp) / global_scale_factor ()));
42 ACE_INLINE ACE_Time_Value
43 ACE_High_Res_Timer::gettimeofday (const ACE_OS::ACE_HRTimer_Op op)
45 #if defined (ACE_WIN32)
46 // Get the global scale factor if there isn't one yet.
47 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
48 ACE_High_Res_Timer::global_scale_factor ();
50 // If there isn't a high-res timer, use gettimeofday ();
51 if (ACE_High_Res_Timer::global_scale_factor_status_ == -1)
52 return ACE_OS::gettimeofday ();
53 #endif /* ACE_WIN32 */
56 ACE_High_Res_Timer::hrtime_to_tv (tv, ACE_OS::gethrtime (op));
61 // Get the current high res timer as the time of day. This is intended
62 // to be used for a gettimeofday replacement in ACE_Timer_Queue and
63 // derived classes so the timers will bebased on high res timers rather
64 // than wall clock time. It uses the ACE_High_Res_Timer::gettimeofday
65 // function, which is deprecated. If it gets removed, please move the
66 // code down here, intact.
67 ACE_INLINE ACE_Time_Value
68 ACE_High_Res_Timer::gettimeofday_hr (void)
70 return ACE_High_Res_Timer::gettimeofday ();
74 ACE_INLINE ACE_hrtime_t
75 ACE_High_Res_Timer::gettime (const ACE_OS::ACE_HRTimer_Op op)
77 #if defined (ACE_WIN32)
78 // Get the global scale factor if there isn't one yet.
79 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
80 ACE_High_Res_Timer::global_scale_factor ();
82 // If there isn't a high-res timer, use gettimeofday ();
83 if (ACE_High_Res_Timer::global_scale_factor_status_ == -1)
85 ACE_Time_Value tv = ACE_OS::gettimeofday ();
86 // Return the time in microseconds because the global_scale_factor_
88 return tv.sec () * ACE_ONE_SECOND_IN_USECS + tv.usec ();
90 #endif /* ACE_WIN32 */
92 return ACE_OS::gethrtime (op);
95 ACE_INLINE ACE_hrtime_t
96 ACE_High_Res_Timer::elapsed_hrtime (const ACE_hrtime_t end,
97 const ACE_hrtime_t start)
101 return (~start + 1 + end); // Wrapped-around counter diff
105 ACE_High_Res_Timer::~ACE_High_Res_Timer (void)
110 ACE_High_Res_Timer::start (const ACE_OS::ACE_HRTimer_Op op)
112 ACE_TRACE ("ACE_High_Res_Timer::start");
113 this->start_ = ACE_High_Res_Timer::gettime (op);
117 ACE_High_Res_Timer::stop (const ACE_OS::ACE_HRTimer_Op op)
119 ACE_TRACE ("ACE_High_Res_Timer::stop");
120 this->end_ = ACE_High_Res_Timer::gettime (op);
124 ACE_High_Res_Timer::start_incr (const ACE_OS::ACE_HRTimer_Op op)
126 ACE_TRACE ("ACE_High_Res_Timer::start_incr");
127 this->start_incr_ = ACE_High_Res_Timer::gettime (op);
131 ACE_High_Res_Timer::stop_incr (const ACE_OS::ACE_HRTimer_Op op)
133 ACE_TRACE ("ACE_High_Res_Timer::stop_incr");
135 ACE_High_Res_Timer::elapsed_hrtime (ACE_High_Res_Timer::gettime (op),
140 ACE_High_Res_Timer::elapsed_microseconds (ACE_hrtime_t &usecs) const
142 ACE_hrtime_t elapsed = ACE_High_Res_Timer::elapsed_hrtime (this->end_,
144 usecs = (ACE_hrtime_t) (elapsed / global_scale_factor ());
148 ACE_High_Res_Timer::global_scale_factor (ACE_UINT32 gsf)
150 global_scale_factor_ = gsf;
153 ACE_END_VERSIONED_NAMESPACE_DECL