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 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
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 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
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
00176 break;
00177 }
00178 #endif
00179 }
00180
00181
00182
00183 ACE_OS::fclose (cpuinfo);
00184 }
00185
00186 return scale_factor;
00187 }
00188 #endif
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
00200 if (ACE_High_Res_Timer::global_scale_factor_status_ == 0)
00201 {
00202
00203
00204 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00205 *ACE_Static_Object_Lock::instance (), 0));
00206
00207
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
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
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
00235
00236 # if !defined (ACE_WIN32)
00237 if (ACE_High_Res_Timer::global_scale_factor_ == 1u)
00238
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
00246
00247
00248
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
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
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
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 #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
00434 ACE_hrtime_t total_nanoseconds;
00435 this->elapsed_time (total_nanoseconds);
00436
00437
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
00476 ACE_hrtime_t total_nanoseconds;
00477 this->elapsed_time (total_nanoseconds);
00478
00479
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
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
00532 return -1;
00533 }
00534
00535 ACE_END_VERSIONED_NAMESPACE_DECL