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