Profile_Timer.cpp

Go to the documentation of this file.
00001 // Profile_Timer.cpp,v 4.39 2006/05/30 11:11:09 jwillemsen Exp
00002 
00003 #include "ace/Profile_Timer.h"
00004 
00005 #if !defined (__ACE_INLINE__)
00006 # include "ace/Profile_Timer.inl"
00007 #endif /* __ACE_INLINE__ */
00008 
00009 #include "ace/Log_Msg.h"
00010 #include "ace/OS_NS_string.h"
00011 
00012 ACE_RCSID(ace, Profile_Timer, "Profile_Timer.cpp,v 4.39 2006/05/30 11:11:09 jwillemsen Exp")
00013 
00014 #if (defined (ACE_HAS_PRUSAGE_T) || defined (ACE_HAS_GETRUSAGE)) && !defined (ACE_WIN32)
00015 
00016 #include "ace/OS_NS_stdio.h"
00017 
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019 
00020 ACE_ALLOC_HOOK_DEFINE(ACE_Profile_Timer)
00021 
00022 
00023 void
00024 ACE_Profile_Timer::dump (void) const
00025 {
00026 #if defined (ACE_HAS_DUMP)
00027   ACE_TRACE ("ACE_Profile_Timer::dump");
00028 #endif /* ACE_HAS_DUMP */
00029 }
00030 
00031 // Initialize interval timer.
00032 
00033 ACE_Profile_Timer::ACE_Profile_Timer (void)
00034 {
00035   ACE_TRACE ("ACE_Profile_Timer::ACE_Profile_Timer");
00036   ACE_OS::memset (&this->end_usage_, 0, sizeof this->end_usage_);
00037   ACE_OS::memset (&this->begin_usage_, 0, sizeof this->begin_usage_);
00038   ACE_OS::memset (&this->last_usage_, 0, sizeof this->last_usage_);
00039 
00040 #  if defined (ACE_HAS_PRUSAGE_T)
00041   ACE_OS::memset (&this->last_usage_, 0, sizeof this->last_usage_);
00042   char buf[20];
00043   ACE_OS::sprintf (buf, "/proc/%d", static_cast<int> (ACE_OS::getpid ()));
00044 
00045   this->proc_handle_ = ACE_OS::open (buf, O_RDONLY, 0);
00046   if (this->proc_handle_ == -1)
00047     ACE_ERROR ((LM_ERROR,
00048                 ACE_LIB_TEXT ("%p\n"),
00049                 buf));
00050 #  elif defined (ACE_HAS_GETRUSAGE)
00051   ACE_OS::memset (&this->begin_time_, 0, sizeof this->begin_time_);
00052   ACE_OS::memset (&this->end_time_, 0, sizeof this->end_time_);
00053   ACE_OS::memset (&this->last_time_, 0, sizeof this->last_time_);
00054 #  endif /* ACE_HAS_PRUSAGE_T */
00055 }
00056 
00057 // Terminate the interval timer.
00058 ACE_Profile_Timer::~ACE_Profile_Timer (void)
00059 {
00060   ACE_TRACE ("ACE_Profile_Timer::~ACE_Profile_Timer");
00061 #  if defined (ACE_HAS_PRUSAGE_T)
00062   if (ACE_OS::close (this->proc_handle_) == -1)
00063     ACE_ERROR ((LM_ERROR,
00064                 ACE_LIB_TEXT ("ACE_Profile_Timer::~ACE_Profile_Timer")));
00065 #  endif /* ACE_HAS_PRUSAGE_T */
00066 }
00067 
00068 // Return the resource utilization.
00069 
00070 void
00071 ACE_Profile_Timer::get_rusage (ACE_Profile_Timer::Rusage &usage)
00072 {
00073   ACE_TRACE ("ACE_Profile_Timer::get_rusage");
00074   usage = this->end_usage_;
00075 }
00076 
00077 #  if defined (ACE_HAS_PRUSAGE_T)
00078 
00079 // Compute the amount of resource utilization since the start time.
00080 
00081 void
00082 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage &rusage)
00083 {
00084   ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
00085   rusage.pr_lwpid =
00086     this->end_usage_.pr_lwpid - this->last_usage_.pr_lwpid;
00087   rusage.pr_count =
00088     this->end_usage_.pr_count - this->last_usage_.pr_count;
00089   rusage.pr_minf  =
00090     this->end_usage_.pr_minf - this->last_usage_.pr_minf;
00091   rusage.pr_majf  =
00092     this->end_usage_.pr_majf - this->last_usage_.pr_majf;
00093   rusage.pr_inblk =
00094     this->end_usage_.pr_inblk - this->last_usage_.pr_inblk;
00095   rusage.pr_oublk =
00096     this->end_usage_.pr_oublk - this->last_usage_.pr_oublk;
00097   rusage.pr_msnd =
00098     this->end_usage_.pr_msnd - this->last_usage_.pr_msnd;
00099   rusage.pr_mrcv =
00100     this->end_usage_.pr_mrcv - this->last_usage_.pr_mrcv;
00101   rusage.pr_sigs =
00102     this->end_usage_.pr_sigs - this->last_usage_.pr_sigs;
00103   this->subtract (rusage.pr_wtime,
00104                   this->end_usage_.pr_wtime,
00105                   this->last_usage_.pr_wtime);
00106   this->subtract (rusage.pr_ltime,
00107                   this->end_usage_.pr_ltime,
00108                   this->last_usage_.pr_ltime);
00109   this->subtract (rusage.pr_slptime,
00110                   this->end_usage_.pr_slptime,
00111                   this->last_usage_.pr_slptime);
00112   rusage.pr_vctx  =
00113     this->end_usage_.pr_vctx - this->last_usage_.pr_vctx;
00114   rusage.pr_ictx  =
00115     this->end_usage_.pr_ictx - this->last_usage_.pr_ictx;
00116   rusage.pr_sysc  =
00117     this->end_usage_.pr_sysc - this->last_usage_.pr_sysc;
00118   rusage.pr_ioch  =
00119     this->end_usage_.pr_ioch - this->last_usage_.pr_ioch;
00120 }
00121 
00122 // Compute the elapsed time.
00123 
00124 void
00125 ACE_Profile_Timer::compute_times (ACE_Elapsed_Time &et)
00126 {
00127   ACE_TRACE ("ACE_Profile_Timer::compute_times");
00128   timespec_t td;
00129 
00130   ACE_Profile_Timer::Rusage &end = this->end_usage_;
00131   ACE_Profile_Timer::Rusage &begin = this->begin_usage_;
00132 
00133   this->subtract (td, end.pr_tstamp, begin.pr_tstamp);
00134   // Convert nanoseconds into seconds.
00135   et.real_time = td.tv_sec + ((double) td.tv_nsec) / ACE_ONE_SECOND_IN_NSECS;
00136   this->subtract (td, end.pr_utime, begin.pr_utime);
00137   // Convert nanoseconds into seconds.
00138   et.user_time = td.tv_sec + ((double) td.tv_nsec) / ACE_ONE_SECOND_IN_NSECS;
00139   this->subtract (td, end.pr_stime,  begin.pr_stime);
00140   // Convert nanoseconds into seconds.
00141   et.system_time = td.tv_sec + ((double) td.tv_nsec) / ACE_ONE_SECOND_IN_NSECS;
00142 }
00143 
00144 // Determine the difference between T1 and T2.
00145 
00146 void
00147 ACE_Profile_Timer::subtract (timespec_t &tdiff, timespec_t &t1, timespec_t &t0)
00148 {
00149   ACE_TRACE ("ACE_Profile_Timer::subtract");
00150   tdiff.tv_sec  = t1.tv_sec - t0.tv_sec;
00151   tdiff.tv_nsec = t1.tv_nsec - t0.tv_nsec;
00152 
00153   // Normalize the time.
00154 
00155   while (tdiff.tv_nsec < 0)
00156     {
00157       tdiff.tv_sec--;
00158       tdiff.tv_nsec += ACE_ONE_SECOND_IN_NSECS;
00159     }
00160 }
00161 
00162 #  elif defined (ACE_HAS_GETRUSAGE)
00163 // Compute the amount of resource utilization since the start time.
00164 
00165 void
00166 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage &usage)
00167 {
00168   ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
00169 #    if !defined (ACE_HAS_LIMITED_RUSAGE_T)
00170   // integral shared memory size
00171   usage.ru_ixrss =
00172     this->end_usage_.ru_ixrss - this->last_usage_.ru_ixrss;
00173   // integral unshared data "
00174   usage.ru_idrss =
00175     this->end_usage_.ru_idrss - this->last_usage_.ru_idrss;
00176   // integral unshared stack "
00177   usage.ru_isrss =
00178     this->end_usage_.ru_isrss - this->last_usage_.ru_isrss;
00179   // page reclaims - total vmfaults
00180   usage.ru_minflt =
00181     this->end_usage_.ru_minflt - this->last_usage_.ru_minflt;
00182   // page faults
00183   usage.ru_majflt =
00184     this->end_usage_.ru_majflt - this->last_usage_.ru_majflt;
00185   // swaps
00186   usage.ru_nswap =
00187     this->end_usage_.ru_nswap - this->last_usage_.ru_nswap;
00188   // block input operations
00189   usage.ru_inblock =
00190     this->end_usage_.ru_inblock - this->last_usage_.ru_inblock;
00191   // block output operations
00192   usage.ru_oublock =
00193     this->end_usage_.ru_oublock - this->last_usage_.ru_oublock;
00194   // messages sent
00195   usage.ru_msgsnd =
00196     this->end_usage_.ru_msgsnd - this->last_usage_.ru_msgsnd;
00197   // messages received
00198   usage.ru_msgrcv =
00199     this->end_usage_.ru_msgrcv - this->last_usage_.ru_msgrcv;
00200   // signals received
00201   usage.ru_nsignals =
00202     this->end_usage_.ru_nsignals - this->last_usage_.ru_nsignals;
00203   // voluntary context switches
00204   usage.ru_nvcsw =
00205     this->end_usage_.ru_nvcsw - this->last_usage_.ru_nvcsw;
00206   // involuntary context switches
00207   usage.ru_nivcsw =
00208     this->end_usage_.ru_nivcsw - this->last_usage_.ru_nivcsw;
00209   this->subtract (usage.ru_utime,
00210                   this->end_usage_.ru_utime,
00211                   this->last_usage_.ru_utime);
00212   this->subtract (usage.ru_stime,
00213                   this->end_usage_.ru_stime,
00214                   this->last_usage_.ru_stime);
00215 #    else
00216   ACE_UNUSED_ARG(usage);
00217 #    endif /* ACE_HAS_LIMITED_RUSAGE_T */
00218 }
00219 
00220 void
00221 ACE_Profile_Timer::compute_times (ACE_Elapsed_Time &et)
00222 {
00223   ACE_TRACE ("ACE_Profile_Timer::compute_times");
00224 
00225   timeval td;
00226 
00227   this->subtract (td, this->end_time_, this->begin_time_);
00228   et.real_time = td.tv_sec + ((double) td.tv_usec) / ACE_ONE_SECOND_IN_USECS;
00229 
00230   this->subtract (td, this->end_usage_.ru_utime, this->begin_usage_.ru_utime);
00231   et.user_time = td.tv_sec + ((double) td.tv_usec) / ACE_ONE_SECOND_IN_USECS;
00232 
00233   this->subtract (td, this->end_usage_.ru_stime,  this->begin_usage_.ru_stime);
00234   et.system_time = td.tv_sec + ((double) td.tv_usec) / ACE_ONE_SECOND_IN_USECS;
00235 }
00236 
00237 // Determine the difference between T1 and T2.
00238 
00239 void
00240 ACE_Profile_Timer::subtract (timeval &tdiff, timeval &t1, timeval &t0)
00241 {
00242   ACE_TRACE ("ACE_Profile_Timer::subtract");
00243   tdiff.tv_sec  = t1.tv_sec - t0.tv_sec;
00244   tdiff.tv_usec = t1.tv_usec - t0.tv_usec;
00245 
00246   // Normalize the time.
00247 
00248   while (tdiff.tv_usec < 0)
00249     {
00250       tdiff.tv_sec--;
00251       tdiff.tv_usec += ACE_ONE_SECOND_IN_USECS;
00252     }
00253 }
00254 
00255 #  endif /* ACE_HAS_PRUSAGE_T */
00256 
00257 // Compute the amount of time that has elapsed between start and stop.
00258 
00259 int
00260 ACE_Profile_Timer::elapsed_time (ACE_Elapsed_Time &et)
00261 {
00262   ACE_TRACE ("ACE_Profile_Timer::elapsed_time");
00263   this->compute_times (et);
00264   return 0;
00265 }
00266 
00267 ACE_END_VERSIONED_NAMESPACE_DECL
00268 
00269 #elif defined (ACE_WIN32) /* defined (ACE_HAS_PRUSAGE_T) || defined (ACE_HAS_GETRUSAGE) */
00270 
00271 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00272 
00273 void
00274 ACE_Profile_Timer::dump (void) const
00275 {
00276 #if defined (ACE_HAS_DUMP)
00277   ACE_TRACE ("ACE_Profile_Timer::dump");
00278   timer_.dump ();
00279 #endif /* ACE_HAS_DUMP */
00280 }
00281 
00282 // Initialize interval timer.
00283 ACE_Profile_Timer::ACE_Profile_Timer (void)
00284   : timer_ ()
00285 {
00286   ACE_TRACE ("ACE_Profile_Timer::ACE_Profile_Timer");
00287 #  if defined (ACE_HAS_GETRUSAGE)
00288 
00289   ACE_OS::memset (&this->end_usage_, 0, sizeof this->end_usage_);
00290   ACE_OS::memset (&this->begin_usage_, 0, sizeof this->begin_usage_);
00291   ACE_OS::memset (&this->last_usage_, 0, sizeof this->last_usage_);
00292 
00293   ACE_OS::memset (&this->begin_time_, 0, sizeof this->begin_time_);
00294   ACE_OS::memset (&this->end_time_, 0, sizeof this->end_time_);
00295   ACE_OS::memset (&this->last_time_, 0, sizeof this->last_time_);
00296 #  endif /* ACE_HAS_GETRUSAGE */
00297 }
00298 
00299 int
00300 ACE_Profile_Timer::elapsed_time (ACE_Elapsed_Time &et)
00301 {
00302   ACE_TRACE ("ACE_Profile_Timer::elapsed_time");
00303 
00304   ACE_hrtime_t delta_t; // nanoseconds
00305   timer_.elapsed_time (delta_t);
00306 #  if defined (ACE_LACKS_LONGLONG_T)
00307   et.real_time = delta_t / (double) ACE_ONE_SECOND_IN_NSECS;
00308 #  else
00309   et.real_time = (__int64) delta_t / (double) ACE_ONE_SECOND_IN_NSECS;
00310 #  endif /* ACE_LACKS_LONGLONG_T */
00311 #  if defined (ACE_HAS_GETRUSAGE)
00312   ACE_Time_Value atv = ACE_Time_Value (this->end_usage_.ru_utime)
00313                        - ACE_Time_Value (this->begin_usage_.ru_utime);
00314   et.user_time = atv.sec () + ((double) atv.usec ()) / ACE_ONE_SECOND_IN_USECS;
00315 
00316   atv = ACE_Time_Value (this->end_usage_.ru_stime)
00317         - ACE_Time_Value (this->begin_usage_.ru_stime);
00318   et.system_time = atv.sec () + ((double) atv.usec ()) / ACE_ONE_SECOND_IN_USECS;
00319 #  else /* ACE_HAS_GETRUSAGE */
00320   et.user_time = 0;
00321   et.system_time = 0;
00322 #  endif /* ACE_HAS_GETRUSAGE */
00323 
00324   return 0;
00325 }
00326 
00327 // Return the resource utilization.
00328 
00329 void
00330 ACE_Profile_Timer::get_rusage (ACE_Profile_Timer::Rusage &usage)
00331 {
00332   ACE_TRACE ("ACE_Profile_Timer::get_rusage");
00333 #  if defined (ACE_HAS_GETRUSAGE)
00334   usage = this->end_usage_;
00335 #  else /* ACE_HAS_GETRUSAGE */
00336   usage = 0;
00337 #  endif /* ACE_HAS_GETRUSAGE */
00338 }
00339 
00340 // Compute the amount of resource utilization since the start time.
00341 
00342 void
00343 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage &usage)
00344 {
00345   ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
00346 
00347 #  if defined (ACE_HAS_GETRUSAGE)
00348   // Use ACE_Time_Value's as intermediate because the type of ru_utime can
00349   // be multiple types and using the - operator is not safe when this are
00350   // 64bit FILETIMEs on Windows
00351   ACE_Time_Value end_ru_utime (this->end_usage_.ru_utime);
00352   ACE_Time_Value begin_ru_utime (this->begin_usage_.ru_utime);
00353   usage.ru_utime = end_ru_utime - begin_ru_utime;
00354 
00355   ACE_Time_Value end_ru_stime (this->end_usage_.ru_stime);
00356   ACE_Time_Value begin_ru_stime (this->begin_usage_.ru_stime);
00357   usage.ru_stime = end_ru_stime - begin_ru_stime;
00358 #  else /* ACE_HAS_GETRUSAGE */
00359   usage = 0;
00360 #  endif /* ACE_HAS_GETRUSAGE */
00361 }
00362 
00363 #  if defined (ACE_HAS_GETRUSAGE)
00364 // Determine the difference between T1 and T2.
00365 
00366 void
00367 ACE_Profile_Timer::subtract (timeval &tdiff, timeval &t1, timeval &t0)
00368 {
00369   ACE_TRACE ("ACE_Profile_Timer::subtract");
00370   tdiff.tv_sec  = t1.tv_sec - t0.tv_sec;
00371   tdiff.tv_usec = t1.tv_usec - t0.tv_usec;
00372 
00373   // Normalize the time.
00374 
00375   while (tdiff.tv_usec < 0)
00376     {
00377       tdiff.tv_sec--;
00378       tdiff.tv_usec += ACE_ONE_SECOND_IN_USECS;
00379     }
00380 }
00381 #  endif /* ACE_HAS_GETRUSAGE */
00382 
00383 ACE_END_VERSIONED_NAMESPACE_DECL
00384 
00385 #else
00386 
00387 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00388 
00389 void
00390 ACE_Profile_Timer::dump (void) const
00391 {
00392 #if defined (ACE_HAS_DUMP)
00393   ACE_TRACE ("ACE_Profile_Timer::dump");
00394   timer_.dump ();
00395 #endif /* ACE_HAS_DUMP */
00396 }
00397 
00398 ACE_Profile_Timer::ACE_Profile_Timer (void)
00399   : timer_ ()
00400 {
00401   ACE_TRACE ("ACE_Profile_Timer::ACE_Profile_Timer");
00402 }
00403 
00404 int
00405 ACE_Profile_Timer::elapsed_time (ACE_Elapsed_Time &et)
00406 {
00407   ACE_TRACE ("ACE_Profile_Timer::elapsed_time");
00408 
00409   ACE_hrtime_t delta_t; /* nanoseconds */
00410   timer_.elapsed_time (delta_t);
00411 
00412   et.real_time = delta_t / (double) ACE_ONE_SECOND_IN_NSECS;
00413 
00414   et.user_time = 0;
00415   et.system_time = 0;
00416 
00417   return 0;
00418 }
00419 
00420 void
00421 ACE_Profile_Timer::get_rusage (ACE_Profile_Timer::Rusage &usage)
00422 {
00423   ACE_TRACE ("ACE_Profile_Timer::get_rusage");
00424   usage = 0;
00425 }
00426 
00427 
00428 void
00429 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage &usage)
00430 {
00431   ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
00432   usage = 0;
00433 }
00434 
00435 ACE_END_VERSIONED_NAMESPACE_DECL
00436 
00437 #endif /* defined (ACE_HAS_PRUSAGE_T) ||
00438           defined (ACE_HAS_GETRUSAGE) && !defined (ACE_WIN32) */

Generated on Thu Nov 9 09:42:00 2006 for ACE by doxygen 1.3.6