00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "ace/High_Res_Timer.h"
00011
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/High_Res_Timer.inl"
00014 #endif
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 89483 2010-03-15 09:48:01Z 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
00034
00035
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
00047
00048
00049
00050 ACE_UINT32 ACE_High_Res_Timer::global_scale_factor_ = 1u;
00051
00052 ACE_END_VERSIONED_NAMESPACE_DECL
00053
00054 #else
00055
00056
00057
00058 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00059
00060
00061
00062
00063 ACE_UINT32 ACE_High_Res_Timer::global_scale_factor_ = 1000u;
00064
00065 ACE_END_VERSIONED_NAMESPACE_DECL
00066 #endif
00067
00068
00069
00070 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00071
00072
00073
00074
00075 int ACE_High_Res_Timer::global_scale_factor_status_ = 0;
00076
00077
00078 #if defined (linux)
00079
00080 ACE_UINT32
00081 ACE_High_Res_Timer::get_cpuinfo (void)
00082 {
00083 ACE_UINT32 scale_factor = 1u;
00084
00085
00086
00087
00088
00089
00090 #if !defined (__alpha__)
00091 bool supported = false;
00092 #endif
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
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
00126 if (::sscanf (buf, "cpu : %s\n", arg) == 1)
00127 {
00128
00129
00130 if (ACE_OS::strncmp (arg,
00131 "Alpha",
00132 5) == 0)
00133 {
00134 supported = true;
00135 }
00136 }
00137
00138 else if (!supported
00139 && ::sscanf (buf, "model name : Pentium %s\n", arg) == 1)
00140 {
00141
00142
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
00154
00155
00156 if (mhertz > 0.0)
00157 {
00158 scale_factor = (ACE_UINT32) (mhertz + 0.5);
00159 break;
00160 }
00161 }
00162 else if (::sscanf (buf, "bogomips : %lf\n", &bmips) == 1
00163 || ::sscanf (buf, "BogoMIPS : %lf\n", &bmips) == 1)
00164 {
00165 if (supported)
00166 {
00167 scale_factor = (ACE_UINT32) (bmips + 0.5);
00168
00169 }
00170 #if 0
00171 else
00172 {
00173 ACE_DEBUG ((LM_DEBUG,
00174 ACE_TEXT ("\nThe BogoMIPS metric is not supported on this platform"
00175 "\n\tReport the results of the clock calibration and"
00176 "\n\tthe contents of /proc/cpuinfo to the ace-users mailing list")));
00177 }
00178 #endif
00179 break;
00180 }
00181 #endif
00182 }
00183
00184
00185
00186 ACE_OS::fclose (cpuinfo);
00187 }
00188
00189 return scale_factor;
00190 }
00191 #endif
00192
00193 ACE_UINT32
00194 ACE_High_Res_Timer::global_scale_factor (void)
00195 {
00196 #if (defined (ACE_WIN32) || defined (ACE_HAS_POWERPC_TIMER) || \
00197 defined (ACE_HAS_PENTIUM) || defined (ACE_HAS_ALPHA_TIMER)) && \
00198 !defined (ACE_HAS_HI_RES_TIMER) && \
00199 (defined (ACE_WIN32) || \
00200 defined (ghs) || defined (__GNUG__) || \
00201 defined (__INTEL_COMPILER))
00202
00203 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
00204 {
00205
00206
00207 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00208 *ACE_Static_Object_Lock::instance (), 0));
00209
00210
00211 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
00212 {
00213 # if defined (ACE_WIN32)
00214 LARGE_INTEGER freq;
00215 if (::QueryPerformanceFrequency (&freq))
00216 {
00217
00218 # if defined (ACE_LACKS_LONGLONG_T)
00219 ACE_UINT64 uint64_freq(freq.u.LowPart, (ACE_UINT32) freq.u.HighPart);
00220 ACE_High_Res_Timer::global_scale_factor
00221 (uint64_freq / (ACE_UINT32) ACE_ONE_SECOND_IN_USECS);
00222 # else
00223 ACE_High_Res_Timer::global_scale_factor
00224 (static_cast<unsigned int> (freq.QuadPart / ACE_HR_SCALE_CONVERSION));
00225 # endif // (ACE_LACKS_LONGLONG_T)
00226
00227 ACE_High_Res_Timer::global_scale_factor_status_ = 1;
00228 }
00229 else
00230
00231 ACE_High_Res_Timer::global_scale_factor_status_ = -1;
00232
00233 return ACE_High_Res_Timer::global_scale_factor_;
00234
00235 # elif defined (linux)
00236 ACE_High_Res_Timer::global_scale_factor (ACE_High_Res_Timer::get_cpuinfo ());
00237 # endif
00238
00239 # if !defined (ACE_WIN32)
00240 if (ACE_High_Res_Timer::global_scale_factor_ <= 1u)
00241
00242 ACE_High_Res_Timer::calibrate ();
00243 # endif // (ACE_WIN32)
00244 }
00245 }
00246
00247 ACE_High_Res_Timer::global_scale_factor_status_ = 1;
00248 #endif
00249
00250
00251
00252
00253 return ACE_High_Res_Timer::global_scale_factor_;
00254 }
00255
00256 ACE_High_Res_Timer::ACE_High_Res_Timer (void)
00257 {
00258 ACE_TRACE ("ACE_High_Res_Timer::ACE_High_Res_Timer");
00259
00260 this->reset ();
00261
00262
00263 (void) global_scale_factor ();
00264 }
00265
00266 ACE_UINT32
00267 ACE_High_Res_Timer::calibrate (const ACE_UINT32 usec,
00268 const u_int iterations)
00269 {
00270 const ACE_Time_Value sleep_time (0, usec);
00271 ACE_Stats delta_hrtime;
00272
00273 ACE_Stats actual_sleeps;
00274
00275 for (u_int i = 0;
00276 i < iterations;
00277 ++i)
00278 {
00279 ACE_Time_Value const actual_start = ACE_OS::gettimeofday ();
00280 ACE_hrtime_t const start = ACE_OS::gethrtime ();
00281 ACE_OS::sleep (sleep_time);
00282 ACE_hrtime_t const stop = ACE_OS::gethrtime ();
00283 ACE_Time_Value const actual_delta =
00284 ACE_OS::gettimeofday () - actual_start;
00285
00286
00287 delta_hrtime.sample (ACE_Utils::truncate_cast<ACE_INT32> (stop - start));
00288 actual_sleeps.sample (actual_delta.msec () * 100u);
00289 }
00290
00291
00292
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
00300 const ACE_UINT32 scale_factor =
00301 (ticks.whole () / actual_sleep.whole () + 5) /
00302 10u ;
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
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
00342 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00343 #endif
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
00366
00367
00368 void
00369 ACE_High_Res_Timer::elapsed_time (struct timespec &elapsed_time) const
00370 {
00371
00372
00373
00374
00375
00376
00377
00378
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
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
00390 elapsed_time.tv_nsec = (time_t) ((useconds % ACE_ONE_SECOND_IN_USECS) * 1000u + nseconds);
00391 }
00392 #endif
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
00404
00405
00406
00407 nanoseconds = ACE_High_Res_Timer::elapsed_hrtime (this->end_, this->start_)
00408 * (1024000u / ACE_High_Res_Timer::global_scale_factor ());
00409
00410 nanoseconds = nanoseconds >> 10;
00411
00412
00413 }
00414
00415 void
00416 ACE_High_Res_Timer::elapsed_time_incr (ACE_hrtime_t &nanoseconds) const
00417 {
00418
00419 nanoseconds = this->total_
00420 * (1024000u / ACE_High_Res_Timer::global_scale_factor ());
00421
00422 nanoseconds = nanoseconds >> 10;
00423 }
00424
00425 void
00426 ACE_High_Res_Timer::print_ave (const ACE_TCHAR *str,
00427 const int count,
00428 ACE_HANDLE handle) const
00429 {
00430 ACE_TRACE ("ACE_High_Res_Timer::print_ave");
00431
00432
00433 ACE_hrtime_t total_nanoseconds;
00434 this->elapsed_time (total_nanoseconds);
00435
00436
00437 u_long total_secs =
00438 static_cast<u_long> (total_nanoseconds / (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00439 ACE_UINT32 extra_nsecs =
00440 static_cast<ACE_UINT32> (total_nanoseconds % (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00441
00442 ACE_TCHAR buf[100];
00443 if (count > 1)
00444 {
00445 ACE_hrtime_t avg_nsecs = total_nanoseconds / (ACE_UINT32) count;
00446 ACE_OS::sprintf (buf,
00447 ACE_TEXT (" count = %d, total (secs %lu, usecs %u), avg usecs = %lu\n"),
00448 count,
00449 total_secs,
00450 (extra_nsecs + 500u) / 1000u,
00451 (u_long) ((avg_nsecs + 500u) / 1000u));
00452 }
00453 else
00454 ACE_OS::sprintf (buf,
00455 ACE_TEXT (" total %3lu.%06lu secs\n"),
00456 total_secs,
00457 (extra_nsecs + 500lu) / 1000lu);
00458
00459 ACE_OS::write (handle,
00460 str,
00461 ACE_OS::strlen (str));
00462 ACE_OS::write (handle,
00463 buf,
00464 ACE_OS::strlen (buf));
00465 }
00466
00467 void
00468 ACE_High_Res_Timer::print_total (const ACE_TCHAR *str,
00469 const int count,
00470 ACE_HANDLE handle) const
00471 {
00472 ACE_TRACE ("ACE_High_Res_Timer::print_total");
00473
00474
00475 ACE_hrtime_t total_nanoseconds;
00476 this->elapsed_time (total_nanoseconds);
00477
00478
00479 u_long total_secs =
00480 static_cast<u_long> (total_nanoseconds / (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00481 ACE_UINT32 extra_nsecs =
00482 static_cast<ACE_UINT32> (total_nanoseconds % (ACE_UINT32) ACE_ONE_SECOND_IN_NSECS);
00483
00484 ACE_TCHAR buf[100];
00485 if (count > 1)
00486 {
00487 ACE_hrtime_t avg_nsecs = this->total_ / (ACE_UINT32) count;
00488
00489 ACE_OS::sprintf (buf,
00490 ACE_TEXT (" count = %d, total (secs %lu, usecs %u), avg usecs = %lu\n"),
00491 count,
00492 total_secs,
00493 (extra_nsecs + 500u) / 1000u,
00494 (u_long) ((avg_nsecs + 500u) / 1000u));
00495 }
00496 else
00497 ACE_OS::sprintf (buf,
00498 ACE_TEXT (" total %3lu.%06u secs\n"),
00499 total_secs,
00500 (extra_nsecs + 500u) / 1000u);
00501
00502 ACE_OS::write (handle,
00503 str,
00504 ACE_OS::strlen (str));
00505 ACE_OS::write (handle,
00506 buf,
00507 ACE_OS::strlen (buf));
00508 }
00509
00510 int
00511 ACE_High_Res_Timer::get_env_global_scale_factor (const ACE_TCHAR *env)
00512 {
00513 if (env != 0)
00514 {
00515 const char *env_value = ACE_OS::getenv (ACE_TEXT_ALWAYS_CHAR (env));
00516 if (env_value != 0)
00517 {
00518 int const value = ACE_OS::atoi (env_value);
00519 if (value > 0)
00520 {
00521 ACE_High_Res_Timer::global_scale_factor (value);
00522 return 0;
00523 }
00524 }
00525 }
00526
00527 return -1;
00528 }
00529
00530 ACE_END_VERSIONED_NAMESPACE_DECL