ACE_High_Res_Timer Class Reference

A high resolution timer class wrapper that encapsulates OS-specific high-resolution timers, such as those found on Solaris, AIX, Win32/Pentium, and VxWorks. More...

#include <High_Res_Timer.h>

List of all members.

Public Member Functions

 ACE_High_Res_Timer (void)
 Initialize the timer.
 ~ACE_High_Res_Timer (void)
 Destructor.
void reset (void)
 Reinitialize the timer.
void start (const ACE_OS::ACE_HRTimer_Op=ACE_OS::ACE_HRTIMER_GETTIME)
 Start timing.
void stop (const ACE_OS::ACE_HRTimer_Op=ACE_OS::ACE_HRTIMER_GETTIME)
 Stop timing.
void elapsed_time (ACE_Time_Value &tv) const
 Set tv to the number of microseconds elapsed.
void elapsed_time (ACE_hrtime_t &nanoseconds) const
 Set nanoseconds to the number of nanoseconds elapsed.
void elapsed_microseconds (ACE_hrtime_t &usecs) const
 Sets usecs to the elapsed (stop - start) time in microseconds.
void start_incr (const ACE_OS::ACE_HRTimer_Op=ACE_OS::ACE_HRTIMER_GETTIME)
 Start incremental timing.
void stop_incr (const ACE_OS::ACE_HRTimer_Op=ACE_OS::ACE_HRTIMER_GETTIME)
 Stop incremental timing.
void elapsed_time_incr (ACE_Time_Value &tv) const
void elapsed_time_incr (ACE_hrtime_t &nanoseconds) const
void print_total (const ACE_TCHAR *message, const int iterations=1, ACE_HANDLE handle=ACE_STDOUT) const
void print_ave (const ACE_TCHAR *message, const int iterations=1, ACE_HANDLE handle=ACE_STDOUT) const
 Print average time.
void dump (void) const
 Dump the state of an object.

Static Public Member Functions

static void global_scale_factor (ACE_UINT32 gsf)
static ACE_UINT32 global_scale_factor (void)
 Returns the global_scale_factor.
static int get_env_global_scale_factor (const ACE_TCHAR *env=ACE_TEXT("ACE_SCALE_FACTOR"))
static ACE_UINT32 calibrate (const ACE_UINT32 usec=500000, const u_int iterations=10)
static ACE_Time_Value gettimeofday_hr (void)
static ACE_Time_Value gettimeofday (const ACE_OS::ACE_HRTimer_Op=ACE_OS::ACE_HRTIMER_GETTIME)
static void hrtime_to_tv (ACE_Time_Value &tv, const ACE_hrtime_t hrt)
 Converts an hrt to tv using global_scale_factor_.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Static Private Member Functions

static ACE_hrtime_t gettime (const ACE_OS::ACE_HRTimer_Op=ACE_OS::ACE_HRTIMER_GETTIME)
static ACE_hrtime_t elapsed_hrtime (const ACE_hrtime_t end, const ACE_hrtime_t start)

Private Attributes

ACE_hrtime_t start_
 Starting time.
ACE_hrtime_t end_
 Ending time.
ACE_hrtime_t total_
 Total elapsed time.
ACE_hrtime_t start_incr_
 Start time of incremental timing.

Static Private Attributes

static ACE_UINT32 global_scale_factor_ = 1000u
static int global_scale_factor_status_ = 0


Detailed Description

A high resolution timer class wrapper that encapsulates OS-specific high-resolution timers, such as those found on Solaris, AIX, Win32/Pentium, and VxWorks.

Most of the member functions don't return values. The only reason that one would fail is if high-resolution time isn't supported on the platform. To avoid impacting performance and complicating the interface, in that case, <ACE_OS::gettimeofday> is used instead. The global scale factor is required for platforms that have high-resolution timers that return units other than microseconds, such as clock ticks. It is represented as a static u_long, can only be accessed through static methods, and is used by all instances of High Res Timer. The member functions that return or print times use the global scale factor. They divide the "time" that they get from <ACE_OS::gethrtime> by global_scale_factor_ to obtain the time in microseconds. Its units are therefore 1/microsecond. On Windows the global_scale_factor_ units are 1/millisecond. There's a macro <ACE_HR_SCALE_CONVERSION> which gives the units/second. Because it's possible that the units/second changes in the future, it's recommended to use it instead of a "hard coded" solution. Dependend on the platform and used class members, there's a maximum elapsed period before overflow (which is not checked). Look at the documentation with some members functions. On some (most?) implementations it's not recommended to measure "long" timeperiods, because the error's can accumulate fast. This is probably not a problem profiling code, but could be on if the high resolution timer class is used to initiate actions after a "long" timeout. On Solaris, a scale factor of 1000 should be used because its high-resolution timer returns nanoseconds. However, on Intel platforms, we use RDTSC which returns the number of clock ticks since system boot. For a 200MHz cpu, each clock tick is 1/200 of a microsecond; the global_scale_factor_ should therefore be 200 or 200000 if it's in unit/millisecond. On Windows QueryPerformanceCounter() is used, which can be a different implementation depending on the used windows HAL (Hardware Abstraction Layer). On some it uses the PC "timer chip" while it uses RDTSC on others.

Note:
The elapsed time calculations in the print methods use ACE_hrtime_t values. Those methods do _not_ check for overflow!

Gabe <begeddov@proaxis.com> raises this issue regarding <ACE_OS::gethrtime>: on multi-processors, the processor that you query for your <timer.stop> value might not be the one you queried for <timer.start>. Its not clear how much divergence there would be, if any. This issue is not mentioned in the Solaris 2.5.1 gethrtime man page. A RDTSC NOTE: RDTSC is the Intel Pentium read-time stamp counter and is actualy a 64 bit clock cycle counter, which is increased with every cycle. It has a low overhead and can be read within 16 (pentium) or 32 (pentium II,III,...) cycles, but it doesn't serialize the processor, which could give wrong timings when profiling very short code fragments. Problematic is that some power sensitive devices (laptops for example, but probably also embedded devices), do change the cycle rate while running. Some pentiums can run on (at least) two clock frequency's. Another problem arises with multiprocessor computers, there are reports that the different RDTSC's are not always kept in sync. A windows "timer chip" NOTE: (8254-compatible real-time clock) When QueryPerformanceCounter() uses the 8254 it has a frequency off about 1.193 Mhz (or sometimes 3.579 Mhz?) and reading it requires some time (several thousand cycles).

Definition at line 100 of file High_Res_Timer.h.


Constructor & Destructor Documentation

ACE_High_Res_Timer::ACE_High_Res_Timer ( void   ) 

Initialize the timer.

Definition at line 253 of file High_Res_Timer.cpp.

References ACE_TRACE, global_scale_factor(), and reset().

00254 {
00255   ACE_TRACE ("ACE_High_Res_Timer::ACE_High_Res_Timer");
00256 
00257   this->reset ();
00258 
00259   // Make sure that the global scale factor is set.
00260   (void) global_scale_factor ();
00261 }

ACE_INLINE ACE_High_Res_Timer::~ACE_High_Res_Timer ( void   ) 

Destructor.

Definition at line 105 of file High_Res_Timer.inl.

00106 {
00107 }


Member Function Documentation

ACE_UINT32 ACE_High_Res_Timer::calibrate ( const ACE_UINT32  usec = 500000,
const u_int  iterations = 10 
) [static]

Set (and return, for info) the global scale factor by sleeping for usec and counting the number of intervening clock cycles. Average over iterations of usec each. On some platforms, such as Pentiums, this is called automatically during the first ACE_High_Res_Timer construction with the default parameter values. An application can override that by calling calibrate with any desired parameter values _prior_ to constructing the first ACE_High_Res_Timer instance. Beware for platforms that can change the cycle rate on the fly.

Definition at line 264 of file High_Res_Timer.cpp.

References ACE_OS::gethrtime(), ACE_OS::gettimeofday(), global_scale_factor(), ACE_Stats::mean(), ACE_Time_Value::msec(), ACE_Stats::sample(), ACE_OS::sleep(), start(), stop(), and ACE_Stats_Value::whole().

Referenced by global_scale_factor().

00266 {
00267   const ACE_Time_Value sleep_time (0, usec);
00268   ACE_Stats delta_hrtime;
00269   // In units of 100 usec, to avoid overflow.
00270   ACE_Stats actual_sleeps;
00271 
00272   for (u_int i = 0;
00273        i < iterations;
00274        ++i)
00275     {
00276       const ACE_Time_Value actual_start =
00277         ACE_OS::gettimeofday ();
00278       const ACE_hrtime_t start =
00279         ACE_OS::gethrtime ();
00280       ACE_OS::sleep (sleep_time);
00281       const ACE_hrtime_t stop =
00282         ACE_OS::gethrtime ();
00283       const ACE_Time_Value actual_delta =
00284         ACE_OS::gettimeofday () - actual_start;
00285 
00286       // Store the sample.
00287       delta_hrtime.sample (ACE_Utils::truncate_cast<ACE_INT32> (stop - start));
00288       actual_sleeps.sample (actual_delta.msec () * 100u);
00289     }
00290 
00291   // Calculate the mean value of the samples, with no fractional
00292   // precision.  Use it for the global scale factor.
00293   ACE_Stats_Value ticks (0);
00294   delta_hrtime.mean (ticks);
00295 
00296   ACE_Stats_Value actual_sleep (0);
00297   actual_sleeps.mean (actual_sleep);
00298 
00299   // The addition of 5 below rounds instead of truncates.
00300   const ACE_UINT32 scale_factor =
00301     (ticks.whole () / actual_sleep.whole () + 5) /
00302     10u /* usec/100 usec */;
00303   ACE_High_Res_Timer::global_scale_factor (scale_factor);
00304 
00305   return scale_factor;
00306 }

void ACE_High_Res_Timer::dump ( void   )  const

Dump the state of an object.

Definition at line 309 of file High_Res_Timer.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_TEXT, ACE_TRACE, end_, global_scale_factor(), LM_DEBUG, start_, start_incr_, and total_.

Referenced by ACE_Profile_Timer::dump().

00310 {
00311 #if defined (ACE_HAS_DUMP)
00312   ACE_TRACE ("ACE_High_Res_Timer::dump");
00313 
00314   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00315   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nglobal_scale_factor_: %u\n"),
00316              global_scale_factor ()));
00317 #if defined (ACE_LACKS_LONGLONG_T)
00318   ACE_DEBUG ((LM_DEBUG,
00319              ACE_TEXT (":\nstart_.hi ():     %8x; start_.lo ():      %8x;\n")
00320              ACE_TEXT ("end_.hi ():       %8x; end_.lo ():        %8x;\n")
00321              ACE_TEXT ("total_.hi ():     %8x; total_.lo ():      %8x;\n")
00322              ACE_TEXT ("start_incr_.hi () %8x; start_incr_.lo (): %8x;\n"),
00323              start_.hi (), start_.lo (),
00324              end_.hi (), end_.lo (),
00325              total_.hi (), total_.lo (),
00326              start_incr_.hi (), start_incr_.lo ()));
00327 #else  /* ! ACE_LACKS_LONGLONG_T */
00328   ACE_DEBUG ((LM_DEBUG,
00329               ACE_TEXT (":\nstart_.hi ():     %8x; start_.lo ():      %8x;\n")
00330               ACE_TEXT ("end_.hi ():       %8x; end_.lo ():        %8x;\n")
00331               ACE_TEXT ("total_.hi ():     %8x; total_.lo ():      %8x;\n")
00332               ACE_TEXT ("start_incr_.hi () %8x; start_incr_.lo (): %8x;\n"),
00333               static_cast<ACE_UINT32> (start_ >> 32),
00334               static_cast<ACE_UINT32> (start_ & 0xfffffffful),
00335               static_cast<ACE_UINT32> (end_ >> 32),
00336               static_cast<ACE_UINT32> (end_ & 0xfffffffful),
00337               static_cast<ACE_UINT32> (total_ >> 32),
00338               static_cast<ACE_UINT32> (total_ & 0xfffffffful),
00339               static_cast<ACE_UINT32> (start_incr_ >> 32),
00340               static_cast<ACE_UINT32> (start_incr_ & 0xfffffffful)));
00341 #endif /* ! ACE_LACKS_LONGLONG_T */
00342   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00343 #endif /* ACE_HAS_DUMP */
00344 }

ACE_INLINE ACE_hrtime_t ACE_High_Res_Timer::elapsed_hrtime ( const ACE_hrtime_t  end,
const ACE_hrtime_t  start 
) [static, private]

Calculate the difference between two ACE_hrtime_t values. It is assumed that the end time is later than start time, so if end is a smaller value, the time counter has wrapped around.

Definition at line 96 of file High_Res_Timer.inl.

Referenced by elapsed_microseconds(), elapsed_time(), and stop_incr().

00098 {
00099   if (end > start)
00100     return end - start;
00101   return (~start + 1 + end);     // Wrapped-around counter diff
00102 }

ACE_INLINE void ACE_High_Res_Timer::elapsed_microseconds ( ACE_hrtime_t usecs  )  const

Sets usecs to the elapsed (stop - start) time in microseconds.

Will overflow on windows when measuring more than appox. 2^^54 ticks. Is still more than 48 days with a 4 Ghz counter.

Definition at line 140 of file High_Res_Timer.inl.

References elapsed_hrtime(), and global_scale_factor().

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 }

void ACE_High_Res_Timer::elapsed_time ( ACE_hrtime_t nanoseconds  )  const

Set nanoseconds to the number of nanoseconds elapsed.

Will overflow when measuring more than 194 day's.

Definition at line 401 of file High_Res_Timer.cpp.

References elapsed_hrtime(), and global_scale_factor().

00402 {
00403   // Please do _not_ rearrange this equation.  It is carefully
00404   // designed and tested to avoid overflow on machines that don't have
00405   // native 64-bit ints. In particular, division can be a problem.
00406   // For more background on this, please see bugzilla #1024.
00407   nanoseconds = ACE_High_Res_Timer::elapsed_hrtime (this->end_, this->start_)
00408             * (1024000u / ACE_High_Res_Timer::global_scale_factor ());
00409   // Caution - Borland has a problem with >>=, so resist the temptation.
00410   nanoseconds = nanoseconds >> 10;
00411   // Right shift is implemented for non native 64-bit ints
00412   // operator/ only for a 32 bit result !
00413 }

void ACE_High_Res_Timer::elapsed_time ( ACE_Time_Value tv  )  const

Set tv to the number of microseconds elapsed.

Could overflow within hours on windows with emulated 64 bit int's and a fast counter. VC++ and Borland normaly use __int64 and so normaly don't have this problem.

Definition at line 358 of file High_Res_Timer.cpp.

References elapsed_hrtime(), and hrtime_to_tv().

Referenced by ACE_Profile_Timer::elapsed_time(), print_ave(), and print_total().

00359 {
00360   hrtime_to_tv (tv,
00361                 ACE_High_Res_Timer::elapsed_hrtime (this->end_, this->start_));
00362 }

void ACE_High_Res_Timer::elapsed_time_incr ( ACE_hrtime_t nanoseconds  )  const

Set <nsec> to the number of nanoseconds elapsed between all calls to start_incr and stop_incr.

Definition at line 416 of file High_Res_Timer.cpp.

References global_scale_factor(), and total_.

00417 {
00418   // Same as above.
00419   nanoseconds = this->total_
00420             * (1024000u / ACE_High_Res_Timer::global_scale_factor ());
00421   // Caution - Borland has a problem with >>=, so resist the temptation.
00422   nanoseconds = nanoseconds >> 10;
00423 }

void ACE_High_Res_Timer::elapsed_time_incr ( ACE_Time_Value tv  )  const

Set tv to the number of microseconds elapsed between all calls to start_incr and stop_incr.

Definition at line 395 of file High_Res_Timer.cpp.

References hrtime_to_tv(), and total_.

00396 {
00397   hrtime_to_tv (tv, total_);
00398 }

int ACE_High_Res_Timer::get_env_global_scale_factor ( const ACE_TCHAR env = ACE_TEXT("ACE_SCALE_FACTOR")  )  [static]

Sets the global_scale_factor to the value in the env environment variable. Returns 0 on success, -1 on failure.

Note:
If env points to string "0" (value zero), this call will fail. This is basically a no-op on CE because there is no concept of environment variable on CE.

Definition at line 513 of file High_Res_Timer.cpp.

References ACE_TEXT_ALWAYS_CHAR, ACE_OS::atoi(), ACE_OS::getenv(), and global_scale_factor().

00514 {
00515 #if !defined (ACE_HAS_WINCE)
00516   if (env != 0)
00517     {
00518       const char *env_value = ACE_OS::getenv (ACE_TEXT_ALWAYS_CHAR (env));
00519       if (env_value != 0)
00520         {
00521           int const value = ACE_OS::atoi (env_value);
00522           if (value > 0)
00523             {
00524               ACE_High_Res_Timer::global_scale_factor (value);
00525               return 0;
00526             }
00527         }
00528     }
00529 #else
00530   ACE_UNUSED_ARG (env);
00531 #endif /* !ACE_HAS_WINCE */
00532   return -1;
00533 }

ACE_INLINE ACE_hrtime_t ACE_High_Res_Timer::gettime ( const ACE_OS::ACE_HRTimer_Op  = ACE_OS::ACE_HRTIMER_GETTIME  )  [static, private]

For internal use: gets the high-resolution time using <ACE_OS::gethrtime>. Except on platforms that require that the <global_scale_factor_> be set, such as ACE_WIN32, uses the low-resolution clock if the <global_scale_factor_> has not been set.

Definition at line 75 of file High_Res_Timer.inl.

References ACE_ONE_SECOND_IN_USECS, ACE_OS::gethrtime(), ACE_OS::gettimeofday(), global_scale_factor(), global_scale_factor_status_, ACE_Time_Value::sec(), and ACE_Time_Value::usec().

Referenced by start(), start_incr(), stop(), and stop_incr().

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 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 }

ACE_INLINE ACE_Time_Value ACE_High_Res_Timer::gettimeofday ( const ACE_OS::ACE_HRTimer_Op  = ACE_OS::ACE_HRTIMER_GETTIME  )  [static]

Deprecated:
THIS FUNCTION IS DEPRECATED. PLEASE USE <ACE_OS::gettimeofday> INSTEAD! Calls <ACE_High_Res_Timer::hrtime_to_tv> passing <ACE_OS::gethrtime>. This function can be used to parameterize objects such as <ACE_Timer_Queue::gettimeofday>. If <global_scale_factor_> is not set, and we're on a platform that requires <global_scale_factor_> (e.g., Win32), ACE_OS::gettimeofday will be used instead of <ACE_OS::gethrtime>. This allows applications on Intel to use <High_Res_Timer> even when <global_scale_factor> is not set. However, setting the <global_scale_factor_> appropriately will result in the finest resolution possible.

Definition at line 43 of file High_Res_Timer.inl.

References ACE_OS::gethrtime(), ACE_OS::gettimeofday(), global_scale_factor(), global_scale_factor_status_, and hrtime_to_tv().

Referenced by gettimeofday_hr().

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 }

ACE_INLINE ACE_Time_Value ACE_High_Res_Timer::gettimeofday_hr ( void   )  [static]

Get the current "time" as the high resolution counter at this time. This is intended to be useful for supplying to a ACE_Timer_Queue as the gettimeofday function, thereby basing the timer calculations on the high res timer rather than wall clock time.

Definition at line 68 of file High_Res_Timer.inl.

References gettimeofday().

00069 {
00070   return ACE_High_Res_Timer::gettimeofday ();
00071 }

ACE_UINT32 ACE_High_Res_Timer::global_scale_factor ( void   )  [static]

Returns the global_scale_factor.

Definition at line 191 of file High_Res_Timer.cpp.

References ACE_GUARD_RETURN, ACE_HR_SCALE_CONVERSION, ACE_ONE_SECOND_IN_USECS, and calibrate().

Referenced by ACE_High_Res_Timer(), calibrate(), dump(), elapsed_microseconds(), elapsed_time(), elapsed_time_incr(), get_env_global_scale_factor(), gettime(), gettimeofday(), and hrtime_to_tv().

00192 {
00193 #if (defined (ACE_WIN32) || defined (ACE_HAS_POWERPC_TIMER) || \
00194      defined (ACE_HAS_PENTIUM) || defined (ACE_HAS_ALPHA_TIMER)) && \
00195     !defined (ACE_HAS_HI_RES_TIMER) && \
00196     ((defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)) || \
00197      defined (ghs) || defined (__GNUG__) || \
00198      defined (__INTEL_COMPILER))
00199   // Check if the global scale factor needs to be set, and do if so.
00200   if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
00201     {
00202       // Grab ACE's static object lock.  This doesn't have anything to
00203       // do with static objects; it's just a convenient lock to use.
00204       ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00205                                 *ACE_Static_Object_Lock::instance (), 0));
00206 
00207       // Double check
00208       if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
00209         {
00210 #         if defined (ACE_WIN32)
00211             LARGE_INTEGER freq;
00212             if (::QueryPerformanceFrequency (&freq))
00213               {
00214                 // We have a high-res timer
00215 #             if defined (ACE_LACKS_LONGLONG_T)
00216                 ACE_UINT64 uint64_freq(freq.u.LowPart, (ACE_UINT32) freq.u.HighPart);
00217                 ACE_High_Res_Timer::global_scale_factor
00218                   (uint64_freq / (ACE_UINT32) ACE_ONE_SECOND_IN_USECS);
00219 #             else
00220                 ACE_High_Res_Timer::global_scale_factor
00221                   (static_cast<unsigned int> (freq.QuadPart / ACE_HR_SCALE_CONVERSION));
00222 #             endif // (ACE_LACKS_LONGLONG_T)
00223 
00224                 ACE_High_Res_Timer::global_scale_factor_status_ = 1;
00225               }
00226             else
00227               // High-Res timers not supported
00228               ACE_High_Res_Timer::global_scale_factor_status_ = -1;
00229 
00230             return ACE_High_Res_Timer::global_scale_factor_;
00231 
00232 #         elif defined (linux)
00233             ACE_High_Res_Timer::global_scale_factor (ACE_High_Res_Timer::get_cpuinfo ());
00234 #         endif /* ! ACE_WIN32 && ! (linux && __alpha__) */
00235 
00236 #         if !defined (ACE_WIN32)
00237           if (ACE_High_Res_Timer::global_scale_factor_ == 1u)
00238             // Failed to retrieve CPU speed from system, so calculate it.
00239             ACE_High_Res_Timer::calibrate ();
00240 #         endif // (ACE_WIN32)
00241         }
00242     }
00243 
00244   ACE_High_Res_Timer::global_scale_factor_status_ = 1;
00245 #endif /* (ACE_WIN32 || ACE_HAS_POWERPC_TIMER || \
00246            ACE_HAS_PENTIUM || ACE_HAS_ALPHA_TIMER) && \
00247           ! ACE_HAS_HI_RES_TIMER &&
00248           ((WIN32 && ! WINCE) || ghs || __GNUG__) */
00249 
00250   return ACE_High_Res_Timer::global_scale_factor_;
00251 }

ACE_INLINE void ACE_High_Res_Timer::global_scale_factor ( ACE_UINT32  gsf  )  [static]

global_scale_factor_ is set to gsf. All High_Res_Timers use global_scale_factor_. This allows applications to set the scale factor just once for all High_Res_Timers. Check High_Res_Timer.cpp for the default global_scale_factors for several platforms. For many platforms (e.g., Solaris), the global_scale_factor_ is set to 1000 so that <scale_factor> need not be set. Careful, a <scale_factor> of 0 will cause division by zero exceptions. Depending on the platform its units are 1/microsecond or 1/millisecond. Use <ACE_HR_SCALE_CONVERSION> inside calculations instead a hardcoded value.

Definition at line 148 of file High_Res_Timer.inl.

References global_scale_factor_.

00149 {
00150   global_scale_factor_ = gsf;
00151 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE void ACE_High_Res_Timer::hrtime_to_tv ( ACE_Time_Value tv,
const ACE_hrtime_t  hrt 
) [static]

Converts an hrt to tv using global_scale_factor_.

Definition at line 21 of file High_Res_Timer.inl.

References ACE_HR_SCALE_CONVERSION, global_scale_factor(), ACE_Time_Value::sec(), and ACE_Time_Value::usec().

Referenced by elapsed_time(), elapsed_time_incr(), and gettimeofday().

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 }

void ACE_High_Res_Timer::print_ave ( const ACE_TCHAR message,
const int  iterations = 1,
ACE_HANDLE  handle = ACE_STDOUT 
) const

Print average time.

Definition at line 427 of file High_Res_Timer.cpp.

References ACE_ONE_SECOND_IN_NSECS, ACE_TEXT, ACE_TRACE, elapsed_time(), ACE_OS::sprintf(), ACE_OS::strlen(), and ACE_OS::write().

00430 {
00431   ACE_TRACE ("ACE_High_Res_Timer::print_ave");
00432 
00433   // Get the total number of nanoseconds elapsed.
00434   ACE_hrtime_t total_nanoseconds;
00435   this->elapsed_time (total_nanoseconds);
00436 
00437   // Separate to seconds and nanoseconds.
00438   u_long total_secs =
00439     static_cast<u_long> (total_nanoseconds / (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00440   ACE_UINT32 extra_nsecs =
00441     static_cast<ACE_UINT32> (total_nanoseconds % (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00442 
00443   ACE_TCHAR buf[100];
00444   if (count > 1)
00445     {
00446       ACE_hrtime_t avg_nsecs = total_nanoseconds / (ACE_UINT32) count;
00447       ACE_OS::sprintf (buf,
00448                        ACE_TEXT (" count = %d, total (secs %lu, usecs %u), avg usecs = %lu\n"),
00449                        count,
00450                        total_secs,
00451                        (extra_nsecs + 500u) / 1000u,
00452                        (u_long) ((avg_nsecs + 500u) / 1000u));
00453     }
00454   else
00455     ACE_OS::sprintf (buf,
00456                      ACE_TEXT (" total %3lu.%06lu secs\n"),
00457                      total_secs,
00458                      (extra_nsecs + 500lu) / 1000lu);
00459 
00460   ACE_OS::write (handle,
00461                  str,
00462                  ACE_OS::strlen (str));
00463   ACE_OS::write (handle,
00464                  buf,
00465                  ACE_OS::strlen (buf));
00466 }

void ACE_High_Res_Timer::print_total ( const ACE_TCHAR message,
const int  iterations = 1,
ACE_HANDLE  handle = ACE_STDOUT 
) const

Print total time.

Note:
only use <print_total> if incremental timings had been used!

Definition at line 469 of file High_Res_Timer.cpp.

References ACE_ONE_SECOND_IN_NSECS, ACE_TEXT, ACE_TRACE, elapsed_time(), ACE_OS::sprintf(), ACE_OS::strlen(), total_, and ACE_OS::write().

00472 {
00473   ACE_TRACE ("ACE_High_Res_Timer::print_total");
00474 
00475   // Get the total number of nanoseconds elapsed.
00476   ACE_hrtime_t total_nanoseconds;
00477   this->elapsed_time (total_nanoseconds);
00478 
00479   // Separate to seconds and nanoseconds.
00480   u_long total_secs =
00481     (u_long) (total_nanoseconds / (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00482   ACE_UINT32 extra_nsecs =
00483     (ACE_UINT32) (total_nanoseconds % (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00484 
00485   ACE_TCHAR buf[100];
00486   if (count > 1)
00487     {
00488       ACE_hrtime_t avg_nsecs = this->total_ / (ACE_UINT32) count;
00489 
00490       ACE_OS::sprintf (buf,
00491                        ACE_TEXT (" count = %d, total (secs %lu, usecs %u), avg usecs = %lu\n"),
00492                        count,
00493                        total_secs,
00494                        (extra_nsecs + 500u) / 1000u,
00495                        (u_long) ((avg_nsecs + 500u) / 1000u));
00496     }
00497   else
00498     ACE_OS::sprintf (buf,
00499                      ACE_TEXT (" total %3lu.%06u secs\n"),
00500                      total_secs,
00501                      (extra_nsecs + 500u) / 1000u);
00502 
00503   ACE_OS::write (handle,
00504                  str,
00505                  ACE_OS::strlen (str));
00506   ACE_OS::write (handle,
00507                  buf,
00508                  ACE_OS::strlen (buf));
00509 }

void ACE_High_Res_Timer::reset ( void   ) 

Reinitialize the timer.

Definition at line 347 of file High_Res_Timer.cpp.

References ACE_TRACE, end_, start_, start_incr_, and total_.

Referenced by ACE_High_Res_Timer().

00348 {
00349   ACE_TRACE ("ACE_High_Res_Timer::reset");
00350 
00351   this->start_ = 0;
00352   this->end_ = 0;
00353   this->total_ = 0;
00354   this->start_incr_ = 0;
00355 }

ACE_INLINE void ACE_High_Res_Timer::start ( const ACE_OS::ACE_HRTimer_Op  = ACE_OS::ACE_HRTIMER_GETTIME  ) 

Start timing.

Definition at line 110 of file High_Res_Timer.inl.

References ACE_TRACE, gettime(), and start_.

Referenced by calibrate(), and ACE_Profile_Timer::start().

00111 {
00112   ACE_TRACE ("ACE_High_Res_Timer::start");
00113   this->start_ = ACE_High_Res_Timer::gettime (op);
00114 }

ACE_INLINE void ACE_High_Res_Timer::start_incr ( const ACE_OS::ACE_HRTimer_Op  = ACE_OS::ACE_HRTIMER_GETTIME  ) 

Start incremental timing.

Definition at line 124 of file High_Res_Timer.inl.

References ACE_TRACE, gettime(), and start_incr_.

00125 {
00126   ACE_TRACE ("ACE_High_Res_Timer::start_incr");
00127   this->start_incr_ = ACE_High_Res_Timer::gettime (op);
00128 }

ACE_INLINE void ACE_High_Res_Timer::stop ( const ACE_OS::ACE_HRTimer_Op  = ACE_OS::ACE_HRTIMER_GETTIME  ) 

Stop timing.

Definition at line 117 of file High_Res_Timer.inl.

References ACE_TRACE, end_, and gettime().

Referenced by calibrate(), and ACE_Profile_Timer::stop().

00118 {
00119   ACE_TRACE ("ACE_High_Res_Timer::stop");
00120   this->end_ = ACE_High_Res_Timer::gettime (op);
00121 }

ACE_INLINE void ACE_High_Res_Timer::stop_incr ( const ACE_OS::ACE_HRTimer_Op  = ACE_OS::ACE_HRTIMER_GETTIME  ) 

Stop incremental timing.

Definition at line 131 of file High_Res_Timer.inl.

References ACE_TRACE, elapsed_hrtime(), gettime(), and total_.

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 }


Member Data Documentation

ACE_High_Res_Timer::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Definition at line 227 of file High_Res_Timer.h.

ACE_hrtime_t ACE_High_Res_Timer::end_ [private]

Ending time.

Definition at line 287 of file High_Res_Timer.h.

Referenced by dump(), reset(), and stop().

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_END_VERSIONED_NAMESPACE_DECL ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_UINT32 ACE_High_Res_Timer::global_scale_factor_ = 1000u [static, private]

Converts ticks to microseconds. That is, ticks / global_scale_factor_ == microseconds.

Definition at line 297 of file High_Res_Timer.h.

Referenced by global_scale_factor().

ACE_END_VERSIONED_NAMESPACE_DECL ACE_BEGIN_VERSIONED_NAMESPACE_DECL int ACE_High_Res_Timer::global_scale_factor_status_ = 0 [static, private]

Indicates the status of the global scale factor, 0 = hasn't been set 1 = been set -1 = HR timer not supported

Definition at line 305 of file High_Res_Timer.h.

Referenced by gettime(), and gettimeofday().

ACE_hrtime_t ACE_High_Res_Timer::start_ [private]

Starting time.

Definition at line 284 of file High_Res_Timer.h.

Referenced by dump(), reset(), and start().

ACE_hrtime_t ACE_High_Res_Timer::start_incr_ [private]

Start time of incremental timing.

Definition at line 293 of file High_Res_Timer.h.

Referenced by dump(), reset(), and start_incr().

ACE_hrtime_t ACE_High_Res_Timer::total_ [private]

Total elapsed time.

Definition at line 290 of file High_Res_Timer.h.

Referenced by dump(), elapsed_time_incr(), print_total(), reset(), and stop_incr().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:12 2010 for ACE by  doxygen 1.4.7