High_Res_Timer.cpp

Go to the documentation of this file.
00001 // $Id: High_Res_Timer.cpp 81030 2008-03-20 12:43:29Z johnnyw $
00002 
00003 // Be very carefull before changing the calculations inside
00004 // ACE_High_Res_Timer.  The precision matters and we are using integer
00005 // calculations not floating point.  Also look good at the emulated 64
00006 // bit int class (inside Basic_Types{h,i,cpp} before changing
00007 // anything.  It's operator/ only returns 32 bits not 64 bits, among
00008 // other things.
00009 
00010 #include "ace/High_Res_Timer.h"
00011 
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/High_Res_Timer.inl"
00014 #endif /* __ACE_INLINE__ */
00015 
00016 #include "ace/Stats.h"
00017 #include "ace/OS_NS_stdio.h"
00018 #include "ace/OS_NS_string.h"
00019 #include "ace/OS_NS_sys_time.h"
00020 #include "ace/OS_NS_time.h"
00021 #include "ace/OS_NS_unistd.h"
00022 #include "ace/OS_NS_stdlib.h"
00023 #include "ace/Truncate.h"
00024 
00025 ACE_RCSID(ace, High_Res_Timer, "$Id: High_Res_Timer.cpp 81030 2008-03-20 12:43:29Z johnnyw $")
00026 
00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00028 
00029 ACE_ALLOC_HOOK_DEFINE(ACE_High_Res_Timer)
00030 
00031 ACE_END_VERSIONED_NAMESPACE_DECL
00032 
00033 // For Intel platforms, a scale factor is required for
00034 // ACE_OS::gethrtime.  We'll still set this to one to prevent division
00035 // by zero errors.
00036 #if (defined (ACE_WIN32) || defined (ACE_HAS_POWERPC_TIMER) || \
00037      defined (ACE_HAS_PENTIUM) || defined (ACE_HAS_ALPHA_TIMER)) && \
00038     !defined (ACE_HAS_HI_RES_TIMER)
00039 
00040 # include "ace/Guard_T.h"
00041 # include "ace/Recursive_Thread_Mutex.h"
00042 # include "ace/Object_Manager.h"
00043 
00044 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00045 
00046   // Initialize the global_scale_factor_ to 1.  The first
00047   // ACE_High_Res_Timer instance construction will override this
00048   // value.
00049   /* static */
00050   ACE_UINT32 ACE_High_Res_Timer::global_scale_factor_ = 1u;
00051 
00052 ACE_END_VERSIONED_NAMESPACE_DECL
00053 
00054 #else  /* ! (ACE_WIN32 || ACE_HAS_POWERPC_TIMER || \
00055              ACE_HAS_PENTIUM || ACE_HAS_ALPHA_TIMER)  ||
00056           ACE_HAS_HI_RES_TIMER */
00057 
00058 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00059 
00060   // A scale_factor of 1000 converts nanosecond ticks to microseconds.
00061   // That is, on these platforms, 1 tick == 1 nanosecond.
00062   /* static */
00063   ACE_UINT32 ACE_High_Res_Timer::global_scale_factor_ = 1000u;
00064 
00065 ACE_END_VERSIONED_NAMESPACE_DECL
00066 #endif /* ! (ACE_WIN32 || ACE_HAS_POWERPC_TIMER || \
00067              ACE_HAS_PENTIUM || ACE_HAS_ALPHA_TIMER)  ||
00068           ACE_HAS_HI_RES_TIMER */
00069 
00070 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00071 
00072 // This is used to tell if the global_scale_factor_ has been
00073 // set, and if high resolution timers are supported.
00074 /* static */
00075 int ACE_High_Res_Timer::global_scale_factor_status_ = 0;
00076 
00077 
00078 #if defined (linux)
00079 // Determine the apparent CPU clock speed from /proc/cpuinfo
00080 ACE_UINT32
00081 ACE_High_Res_Timer::get_cpuinfo (void)
00082 {
00083   ACE_UINT32 scale_factor = 1u;
00084 
00085   // Get the BogoMIPS from /proc/cpuinfo.  It works fine on Alpha and
00086   // Pentium Pro.  For other CPUs, it will be necessary to interpret
00087   // the BogoMips, as described in the BogoMips mini-HOWTO.  Note that
00088   // this code assumes an order to the /proc/cpuinfo contents.  The
00089   // BogoMips rating had better come after CPU type and model info.
00090 #if !defined (__alpha__)
00091   bool supported = false;
00092 #endif /* __alpha__ */
00093 
00094   FILE *cpuinfo = ACE_OS::fopen (ACE_TEXT ("/proc/cpuinfo"),
00095                                  ACE_TEXT ("r"));
00096 
00097   if (cpuinfo != 0)
00098     {
00099       char buf[128];
00100 
00101       // ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nReading /proc/cpuinfo...")));
00102 
00103       while (ACE_OS::fgets (buf, sizeof buf, cpuinfo))
00104         {
00105 #if defined (__alpha__)
00106           ACE_UINT32 whole;
00107           ACE_UINT32 fractional;
00108           if (::sscanf (buf,
00109                         "BogoMIPS : %d.%d\n",
00110                         &whole,
00111                         &fractional) == 2
00112               || ::sscanf (buf,
00113                            "bogomips : %d.%d\n",
00114                            &whole,
00115                            &fractional) == 2)
00116             {
00117               scale_factor = whole;
00118               break;
00119             }
00120 #else
00121           double mhertz = 1;
00122           double bmips = 1;
00123           char arg[128];
00124 
00125           // CPU type?
00126           if (::sscanf (buf, "cpu : %s\n", arg) == 1)
00127             {
00128               // If this is an Alpha chip, then the BogoMips rating is
00129               // usable...
00130               if (ACE_OS::strncmp (arg,
00131                                    "Alpha",
00132                                    5) == 0)
00133                 {
00134                   supported = true;
00135                 }
00136             }
00137           // Pentium CPU model?
00138           else if (!supported
00139                    && ::sscanf (buf, "model name : Pentium %s\n", arg) == 1)
00140             {
00141               // But if we don't have the right kind of Intel chip,
00142               // just quit.
00143               if (ACE_OS::strcmp (arg, "II") == 0
00144                   || ACE_OS::strcmp (arg, "III") == 0
00145                   || ACE_OS::strcmp (arg, "IV") == 0
00146                   || ACE_OS::strcmp (arg, "Pro") == 0)
00147                 {
00148                   supported = true;
00149                 }
00150             }
00151           else if (::sscanf (buf, "cpu MHz : %lf\n", &mhertz) == 1)
00152             {
00153               // If the line "cpu MHz : xxx" is present, then it's a
00154               // reliable measure of the CPU speed - according to the
00155               // kernel-source.
00156               scale_factor = (ACE_UINT32) (mhertz + 0.5);
00157               break;
00158             }
00159           else if (::sscanf (buf, "bogomips : %lf\n", &bmips) == 1
00160                    || ::sscanf (buf, "BogoMIPS : %lf\n", &bmips) == 1)
00161             {
00162               if (supported)
00163                 {
00164                   scale_factor = (ACE_UINT32) (bmips + 0.5);
00165                   // ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" setting the clock scale factor to %u"), scale_factor));
00166                 }
00167 #if 0
00168               else
00169                 {
00170                   ACE_DEBUG ((LM_DEBUG,
00171                               ACE_TEXT ("\nThe BogoMIPS metric is not supported on this platform"
00172                                          "\n\tReport the results of the clock calibration and"
00173                                          "\n\tthe contents of /proc/cpuinfo to the ace-users mailing list")));
00174                 }
00175 #endif /* 0 */
00176               break;
00177             }
00178 #endif /* __alpha__ */
00179         }
00180 
00181       // ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (done)\n")));
00182 
00183       ACE_OS::fclose (cpuinfo);
00184     }
00185 
00186   return scale_factor;
00187 }
00188 #endif /* linux */
00189 
00190 ACE_UINT32
00191 ACE_High_Res_Timer::global_scale_factor (void)
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 }
00252 
00253 ACE_High_Res_Timer::ACE_High_Res_Timer (void)
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 }
00262 
00263 ACE_UINT32
00264 ACE_High_Res_Timer::calibrate (const ACE_UINT32 usec,
00265                                const u_int iterations)
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 }
00307 
00308 void
00309 ACE_High_Res_Timer::dump (void) const
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 }
00345 
00346 void
00347 ACE_High_Res_Timer::reset (void)
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 }
00356 
00357 void
00358 ACE_High_Res_Timer::elapsed_time (ACE_Time_Value &tv) const
00359 {
00360   hrtime_to_tv (tv,
00361                 ACE_High_Res_Timer::elapsed_hrtime (this->end_, this->start_));
00362 }
00363 
00364 #if defined (ACE_HAS_POSIX_TIME)
00365 // Note... Win32 does not have ACE_HAS_POSIX_TIME, so the scale factor
00366 // does not need to take into account the different units on Win32.
00367 
00368 void
00369 ACE_High_Res_Timer::elapsed_time (struct timespec &elapsed_time) const
00370 {
00371   // This implementation should be cleaned up.
00372 
00373   // Just grab the nanoseconds.  That is, leave off all values above
00374   // microsecond.  This equation is right!  Don't mess with me!  (It
00375   // first strips off everything but the portion less than 1 usec.
00376   // Then it converts that to nanoseconds by dividing by the scale
00377   // factor to convert to usec, and multiplying by 1000.)  The cast
00378   // avoids a MSVC 4.1 compiler warning about narrowing.
00379   ACE_hrtime_t elapsed =
00380     ACE_High_Res_Timer::elapsed_hrtime (this->end_, this->start_);
00381   u_long nseconds = static_cast<u_long> (elapsed %
00382                                          global_scale_factor () * 1000u /
00383                                          global_scale_factor ());
00384 
00385   // Get just the microseconds (dropping any left over nanoseconds).
00386   ACE_UINT32 useconds = (ACE_UINT32) (elapsed / global_scale_factor ());
00387 
00388   elapsed_time.tv_sec = (time_t) (useconds / ACE_ONE_SECOND_IN_USECS);
00389   // Transforms one second in microseconds into nanoseconds.
00390   elapsed_time.tv_nsec = (time_t) ((useconds % ACE_ONE_SECOND_IN_USECS) * 1000u + nseconds);
00391 }
00392 #endif /* ACE_HAS_POSIX_TIME */
00393 
00394 void
00395 ACE_High_Res_Timer::elapsed_time_incr (ACE_Time_Value &tv) const
00396 {
00397   hrtime_to_tv (tv, total_);
00398 }
00399 
00400 void
00401 ACE_High_Res_Timer::elapsed_time (ACE_hrtime_t &nanoseconds) const
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 }
00414 
00415 void
00416 ACE_High_Res_Timer::elapsed_time_incr (ACE_hrtime_t &nanoseconds) const
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 }
00424 
00425 #if !defined (ACE_HAS_WINCE)
00426 void
00427 ACE_High_Res_Timer::print_ave (const ACE_TCHAR *str,
00428                                const int count,
00429                                ACE_HANDLE handle) const
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 }
00467 
00468 void
00469 ACE_High_Res_Timer::print_total (const ACE_TCHAR *str,
00470                                  const int count,
00471                                  ACE_HANDLE handle) const
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 }
00510 #endif /* !ACE_HAS_WINCE */
00511 
00512 int
00513 ACE_High_Res_Timer::get_env_global_scale_factor (const ACE_TCHAR *env)
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 }
00534 
00535 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:39 2010 for ACE by  doxygen 1.4.7