Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Profile_Timer.cpp

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

Generated on Mon Jun 16 11:20:56 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002