Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4ProfileTimer/TGo4ProfileTimer.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4ProfileTimer.h"
00017 
00018 #include <iostream.h>
00019 #include <string.h>
00020 #include <unistd.h>
00021 
00022 ClassImp(TGo4ProfileTimer)
00023 
00024 // start: set begin and last time
00025 // stop: set last and end time
00026 // stop() ... stop() ... stop()
00027 // elapsed_rusage(ru): resource usage between last start()/stop() and now (end)
00028 // elapsed_time(et):   time elapsed from start() (begin) to stop() (end)
00029 
00030 
00031 
00032 Int_t TGo4ProfileTimer::Start(void)
00033 {
00034   gettimeofday (&this->fxBeginTime, 0);
00035   this->fxLastTime = this->fxBeginTime;
00036   getrusage (RUSAGE_SELF, &this->fxBeginUsage);
00037   this->fxLastUsage = this->fxBeginUsage;
00038   return 0;
00039 }
00040 
00041 
00042 Int_t TGo4ProfileTimer::Stop(void)
00043 {
00044   this->fxLastTime = this->fxEndTime;
00045   gettimeofday (&this->fxEndTime, 0);
00046   this->fxLastUsage = this->fxEndUsage;
00047   getrusage (RUSAGE_SELF, &this->fxEndUsage);
00048 
00049   return 0;
00050 }
00051 
00052 // Terminate the interval timer.
00053 
00054 TGo4ProfileTimer::~TGo4ProfileTimer (void)
00055 {
00056 }
00057 
00058 void
00059 TGo4ProfileTimer::Dump (void) const
00060 {
00061 }
00062 
00063 // Give short timing resume
00064 
00065 void
00066 TGo4ProfileTimer::Resume (void)
00067 {
00068   int rc;
00069   TGo4ElapsedTime et;
00070   TGo4ProfileTimer::Rusage ru;
00071 
00072   // between begin and end
00073   rc = this->ElapsedTime(et);
00074   // between last stop() and end
00075   this->ElapsedRusage(ru);
00076 
00077   cout << "\nResource Usage: Timing [s]\n";
00078   cout << "Real:\t" << et.fdRealTime << endl;
00079   //cout << "\nUser:\t" << et.fdUserTime << endl;
00080   cout << "User:\t" << (float)(ru.ru_utime.tv_sec + (float)ru.ru_utime.tv_usec/1000000.)  << endl;
00081   //cout << "\nSystem:\t" << et.fdSystemTime << endl;
00082   cout << "System:\t" << (float)(ru.ru_stime.tv_sec + (float)ru.ru_stime.tv_usec/1000000.)   << endl;
00083   cout << "\n";
00084 }
00085 
00086 
00087 // Give short memory resume
00088 
00089 void
00090 TGo4ProfileTimer::Memory (void)
00091 {
00092   TGo4ProfileTimer::Rusage ru;
00093 
00094   // between last stop() and end
00095   this->ElapsedRusage(ru);
00096 
00097   cout << "\nResource Usage: Memory etc.\n";
00098   cout << "maximum resident set size     \t";
00099   cout << ru.ru_maxrss << endl;
00100   cout << "integral shared memory size   \t";
00101   cout << ru.ru_ixrss << endl;
00102   cout << "integral unshared data        \t";
00103   cout << ru.ru_idrss << endl;
00104   cout << "integral unshared stack       \t";
00105   cout << ru.ru_isrss << endl;
00106   cout << "page reclaims - total vmfaults\t";
00107   cout << ru.ru_minflt << endl;
00108   cout << "page faults                   \t";
00109   cout << ru.ru_majflt << endl;
00110   cout << "swaps                         \t";
00111   cout << ru.ru_nswap << endl;
00112   cout << "block input operations        \t";
00113   cout << ru.ru_inblock << endl;
00114   cout << "block output operations       \t";
00115   cout << ru.ru_oublock << endl;
00116   cout << "messages sent                 \t";
00117   cout << ru.ru_msgsnd << endl;
00118   cout << "messages received             \t";
00119   cout << ru.ru_msgrcv << endl;
00120   cout << "signals received              \t";
00121   cout << ru.ru_nsignals << endl;
00122   cout << "voluntary context switches    \t";
00123   cout << ru.ru_nvcsw << endl;
00124   cout << "involuntary context switches  \t";
00125   cout << ru.ru_nivcsw << endl;
00126   cout << "\n";
00127 }
00128 
00129 
00130 // Initialize interval timer.
00131 
00132 TGo4ProfileTimer::TGo4ProfileTimer (void)
00133 {
00134   memset (&this->fxEndUsage, 0, sizeof this->fxEndUsage);
00135   memset (&this->fxBeginUsage, 0, sizeof this->fxBeginUsage);
00136   memset (&this->fxLastUsage, 0, sizeof this->fxLastUsage);
00137 
00138   memset (&this->fxBeginTime, 0, sizeof this->fxBeginTime);
00139   memset (&this->fxEndTime, 0, sizeof this->fxEndTime);
00140   memset (&this->fxLastTime, 0, sizeof this->fxLastTime);
00141 
00142   char buf[20];
00143   snprintf (buf,19, "/proc/%d", getpid ());
00144 
00145   cout << "Profile_Timer created for (" << buf << ")" << endl;
00146 }
00147 
00148 // Return the resource utilization.
00149 
00150 void
00151 TGo4ProfileTimer::GetRusage (TGo4ProfileTimer::Rusage &usage)
00152 {
00153   usage = this->fxEndUsage;
00154 }
00155 
00156 // Compute the amount of resource utilization since the start time.
00157 
00158 void
00159 TGo4ProfileTimer::ElapsedRusage (TGo4ProfileTimer::Rusage &usage)
00160 {
00161   //  maximum resident set size
00162   usage.ru_maxrss = this->fxEndUsage.ru_maxrss - this->fxLastUsage.ru_maxrss;
00163   // integral shared memory size
00164   usage.ru_ixrss = this->fxEndUsage.ru_ixrss - this->fxLastUsage.ru_ixrss;
00165   // integral unshared data
00166   usage.ru_idrss = this->fxEndUsage.ru_idrss - this->fxLastUsage.ru_idrss;
00167   // integral unshared stack
00168   usage.ru_isrss = this->fxEndUsage.ru_isrss - this->fxLastUsage.ru_isrss;
00169   // page reclaims - total vmfaults
00170   usage.ru_minflt = this->fxEndUsage.ru_minflt - this->fxLastUsage.ru_minflt;
00171   // page faults
00172   usage.ru_majflt = this->fxEndUsage.ru_majflt - this->fxLastUsage.ru_majflt;
00173   // swaps
00174   usage.ru_nswap = this->fxEndUsage.ru_nswap - this->fxLastUsage.ru_nswap;
00175   // block input operations
00176   usage.ru_inblock = this->fxEndUsage.ru_inblock -
00177                        this->fxLastUsage.ru_inblock;
00178   // block output operations
00179   usage.ru_oublock = this->fxEndUsage.ru_oublock -
00180                        this->fxLastUsage.ru_oublock;
00181   // messages sent
00182   usage.ru_msgsnd = this->fxEndUsage.ru_msgsnd - this->fxLastUsage.ru_msgsnd;
00183   // messages received
00184   usage.ru_msgrcv = this->fxEndUsage.ru_msgrcv - this->fxLastUsage.ru_msgrcv;
00185   // signals received
00186   usage.ru_nsignals = this->fxEndUsage.ru_nsignals -
00187                         this->fxLastUsage.ru_nsignals;
00188   // voluntary context switches
00189   usage.ru_nvcsw = this->fxEndUsage.ru_nvcsw - this->fxLastUsage.ru_nvcsw;
00190   // involuntary context switches
00191   usage.ru_nivcsw = this->fxEndUsage.ru_nivcsw - this->fxLastUsage.ru_nivcsw;
00192   this->Subtract (usage.ru_utime, this->fxEndUsage.ru_utime, this->fxLastUsage.ru_utime);
00193   this->Subtract (usage.ru_stime, this->fxEndUsage.ru_stime, this->fxLastUsage.ru_stime);
00194 }
00195 
00196 // Compute the elapsed time.
00197 
00198 void
00199 TGo4ProfileTimer::ComputeTimes (TGo4ElapsedTime &et)
00200 {
00201   timeval td;
00202 
00203   //Profile_Timer::Rusage &end = this->end_usage_;
00204   //Profile_Timer::Rusage &begin = this->begin_usage_;
00205 
00206   this->Subtract (td, this->fxEndTime, this->fxBeginTime);
00207   et.fdRealTime = td.tv_sec + ((double) td.tv_usec) / ONE_SECOND_IN_USECS;
00208 
00209   this->Subtract (td, this->fxEndUsage.ru_utime, this->fxBeginUsage.ru_utime);
00210   et.fdUserTime = td.tv_sec + ((double) td.tv_usec) / ONE_SECOND_IN_USECS;
00211 
00212   this->Subtract (td, this->fxEndUsage.ru_stime,  this->fxBeginUsage.ru_stime);
00213   et.fdSystemTime = td.tv_sec + ((double) td.tv_usec) / ONE_SECOND_IN_USECS;
00214 }
00215 
00216 // Determine the difference between T1 and T2.
00217 
00218 void
00219 TGo4ProfileTimer::Subtract (timeval &tdiff, timeval &t1, timeval &t0)
00220 {
00221   tdiff.tv_sec  = t1.tv_sec - t0.tv_sec;
00222   tdiff.tv_usec = t1.tv_usec - t0.tv_usec;
00223 
00224   // Normalize the time.
00225 
00226   while (tdiff.tv_usec < 0)
00227     {
00228       tdiff.tv_sec--;
00229       tdiff.tv_usec += ONE_SECOND_IN_USECS;
00230     }
00231 }
00232 
00233 // Compute the amount of time that has elapsed between start and stop.
00234 
00235 Int_t
00236 TGo4ProfileTimer::ElapsedTime (TGo4ElapsedTime &et)
00237 {
00238   this->ComputeTimes (et);
00239   return 0;
00240 }
00241 TGo4ProfileTimer::TGo4ElapsedTime::TGo4ElapsedTime(): fdRealTime(0.), fdUserTime(0.), fdSystemTime(0.)
00242 {
00243 
00244 }
00245 
00246 
00247 
00248 
00249 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:56:04 2005 for Go4-v2.10-5 by doxygen1.2.15