2 #include "ace/Global_Macros.h"
4 #if defined (ACE_WIN32)
5 # include "ace/OS_NS_sys_time.h"
8 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
10 /// Be very careful before changing the calculations inside
11 /// ACE_High_Res_Timer. The precision matters and we are using integer
12 /// calculations not floating point.
14 ACE_High_Res_Timer::hrtime_to_tv (ACE_Time_Value &tv,
15 const ACE_hrtime_t hrt)
17 #if !defined (ACE_WIN32)
18 // The following are based on the units of global_scale_factor_
19 // being 1/microsecond. Therefore, dividing by it converts
20 // clock ticks to microseconds.
21 tv.sec ((time_t) (hrt / (ACE_UINT32) ACE_HR_SCALE_CONVERSION /
22 global_scale_factor ()));
24 // hrt = (tv.sec * ACE_ONE_SECOND_IN_USECS + tv.usec) * global_scale_factor_
25 // tv.usec = hrt / global_scale_factor_ - tv.sec * ACE_ONE_SECOND_IN_USECS
26 // That first term will be lossy, so factor out global_scale_factor_:
27 // tv.usec = (hrt - tv.sec * ACE_ONE_SECOND_IN_USECS * global_scale_factor_)/
28 // global_scale_factor
29 ACE_hrtime_t tmp = tv.sec ();
30 tmp *= ((ACE_UINT32) ACE_HR_SCALE_CONVERSION * global_scale_factor ());
31 tv.usec ((suseconds_t) ((hrt - tmp) / global_scale_factor ()));
33 // This a higher-precision version, specific for Windows systems
34 // The following are based on the units of global_scale_factor_
35 // being 1/microsecond. Therefore, dividing by it converts
36 // clock ticks to microseconds.
37 tv.sec ((time_t) (hrt / global_scale_factor () ));
39 // Calculate usec, first calculate the seconds in hrtime
40 ACE_High_Res_Timer::global_scale_factor_type tmp = tv.sec ();
41 tmp *= global_scale_factor ();
42 tv.usec ((suseconds_t) ((hrt - tmp) * ACE_HR_SCALE_CONVERSION / global_scale_factor ()));
47 ACE_INLINE ACE_Time_Value
48 ACE_High_Res_Timer::gettimeofday (const ACE_OS::ACE_HRTimer_Op op)
50 #if defined (ACE_WIN32)
51 // Get the global scale factor if there isn't one yet.
52 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
53 ACE_High_Res_Timer::global_scale_factor ();
55 // If there isn't a high-res timer, use gettimeofday ();
56 if (ACE_High_Res_Timer::global_scale_factor_status_ == -1)
57 return ACE_OS::gettimeofday ();
58 #endif /* ACE_WIN32 */
61 ACE_High_Res_Timer::hrtime_to_tv (tv, ACE_OS::gethrtime (op));
65 /// Get the current high res timer as the time of day. This is intended
66 /// to be used for a gettimeofday replacement in ACE_Timer_Queue and
67 /// derived classes so the timers will be based on high res timers rather
68 /// than wall clock time. It uses the ACE_High_Res_Timer::gettimeofday
69 /// function, which is deprecated. If it gets removed, please move the
70 /// code down here, intact.
71 ACE_INLINE ACE_Time_Value
72 ACE_High_Res_Timer::gettimeofday_hr ()
74 return ACE_High_Res_Timer::gettimeofday ();
78 ACE_INLINE ACE_hrtime_t
79 ACE_High_Res_Timer::gettime (const ACE_OS::ACE_HRTimer_Op op)
81 #if defined (ACE_WIN32)
82 // Get the global scale factor if there isn't one yet.
83 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
84 ACE_High_Res_Timer::global_scale_factor ();
86 // If there isn't a high-res timer, use gettimeofday ();
87 if (ACE_High_Res_Timer::global_scale_factor_status_ == -1)
89 ACE_Time_Value const tv = ACE_OS::gettimeofday ();
90 // Return the time in microseconds because the global_scale_factor_
92 return tv.sec () * ACE_ONE_SECOND_IN_USECS + tv.usec ();
94 #endif /* ACE_WIN32 */
96 return ACE_OS::gethrtime (op);
99 ACE_INLINE ACE_hrtime_t
100 ACE_High_Res_Timer::elapsed_hrtime (const ACE_hrtime_t end,
101 const ACE_hrtime_t start)
105 return (~start + 1 + end); // Wrapped-around counter diff
109 ACE_High_Res_Timer::~ACE_High_Res_Timer ()
114 ACE_High_Res_Timer::start (const ACE_OS::ACE_HRTimer_Op op)
116 ACE_TRACE ("ACE_High_Res_Timer::start");
117 this->start_ = ACE_High_Res_Timer::gettime (op);
121 ACE_High_Res_Timer::stop (const ACE_OS::ACE_HRTimer_Op op)
123 ACE_TRACE ("ACE_High_Res_Timer::stop");
124 this->end_ = ACE_High_Res_Timer::gettime (op);
128 ACE_High_Res_Timer::start_incr (const ACE_OS::ACE_HRTimer_Op op)
130 ACE_TRACE ("ACE_High_Res_Timer::start_incr");
131 this->start_incr_ = ACE_High_Res_Timer::gettime (op);
135 ACE_High_Res_Timer::stop_incr (const ACE_OS::ACE_HRTimer_Op op)
137 ACE_TRACE ("ACE_High_Res_Timer::stop_incr");
139 ACE_High_Res_Timer::elapsed_hrtime (ACE_High_Res_Timer::gettime (op),
144 ACE_High_Res_Timer::elapsed_microseconds (ACE_hrtime_t &usecs) const
147 #if !defined (ACE_WIN32)
148 ACE_hrtime_t elapsed = ACE_High_Res_Timer::elapsed_hrtime (this->end_,
150 usecs = (ACE_hrtime_t) (elapsed / global_scale_factor ());
152 usecs = (ACE_High_Res_Timer::elapsed_hrtime (this->end_, this->start_) *
153 ACE_HR_SCALE_CONVERSION) /
154 global_scale_factor ();
159 ACE_High_Res_Timer::global_scale_factor (
160 ACE_High_Res_Timer::global_scale_factor_type gsf)
162 global_scale_factor_ = gsf;
165 ACE_END_VERSIONED_NAMESPACE_DECL