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

CTBtraceClock.cxx

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00013 
00014 #include    "CTB.hxx"
00015 #include    "CTBtraceClock.hxx"
00016 
00017 #include    "CTBsystem.hxx"
00018 #include    "CTBprintf.hxx"
00019 
00074 //------------------------------------------+-----------------------------------
00076 
00077 CTBtraceClock::CTBtraceClock()              // default constructor
00078 {
00079   Init(0,false,false);
00080 }
00081 
00082 //------------------------------------------+-----------------------------------
00084 
00089 CTBtraceClock::CTBtraceClock(const char* c_text, bool b_start, bool b_print)
00090 {
00091   Init(c_text,b_start,b_print);
00092 }         
00093 
00094 //------------------------------------------+-----------------------------------
00096 
00101 CTBtraceClock::~CTBtraceClock()
00102 {
00103   if (mb_autoprint) ToStream();             // print on destruct if requested
00104 }
00105 
00106 //------------------------------------------+-----------------------------------
00108 
00109 void CTBtraceClock::Start()
00110 {
00111   if (!mb_started) {
00112     md_cpustart   = CTBsystem::CpuTime();
00113     md_elastart   = CTBsystem::ElapsedTime();
00114     mi_intervals += 1;
00115     mb_started    = true;
00116   }
00117 }
00118 
00119 //------------------------------------------+-----------------------------------
00121 
00122 void CTBtraceClock::Stop()
00123 {
00124   if (mb_started) {
00125     Update(e_cputime|e_elatime);
00126     mb_started  = false;
00127   }
00128 }
00129 
00130 //------------------------------------------+-----------------------------------
00132 
00133 void CTBtraceClock::Reset()
00134 {
00135   md_cputime    = 0.;
00136   md_elatime    = 0.;
00137   mi_intervals  = 0;
00138   if (mb_started) {
00139     md_cpustart   = CTBsystem::CpuTime();
00140     md_elastart   = CTBsystem::ElapsedTime();
00141     mi_intervals += 1;
00142   }
00143 }
00144 
00145 //------------------------------------------+-----------------------------------
00147 
00158 void CTBtraceClock::ToStream(ostream& os)
00159 {
00160   Update(e_cputime|e_elatime);
00161 
00162   os << CTBprintf(mc_text,"-s",30) << "  CPU ";
00163   if (mi_intervals > 0) os << CTBprintf(md_cputime,"f",9,3);
00164   else                  os << "    -.---";
00165   os << "s  elapsed ";
00166   if (mi_intervals > 0) os << CTBprintf(md_elatime,"f",9,3);
00167   else                  os << "     -.--";
00168   os << "s";
00169   if (md_elatime > 0.)
00170     os << "  (" << CTBprintf(100.*md_cputime/md_elatime,"f",6,2) << "%)";
00171   os << endl;
00172 }
00173 
00174 //------------------------------------------+-----------------------------------
00176 
00177 void CTBtraceClock::Init(const char* c_text, bool b_start, bool b_print)
00178 {
00179   mc_text       = c_text ? c_text : "";     // convert null -> ""
00180   mb_started    = false;
00181   mb_autoprint  = b_print;
00182   md_cputime    = 0.;
00183   md_elatime    = 0.;
00184   mi_intervals  = 0;
00185   if (b_start) Start();
00186 }
00187 
00188 //------------------------------------------+-----------------------------------
00190 
00191 void CTBtraceClock::Update(int i_flags)
00192 {
00193   if (mb_started) {
00194     if (i_flags & e_cputime) {
00195       double d_cpustop = CTBsystem::CpuTime();
00196       double d_dtime   = d_cpustop - md_cpustart;
00197 
00198       if (d_dtime > 1.e-6) {
00199         md_cputime  += d_dtime;
00200         md_cpustart  = d_cpustop;
00201       }
00202     }
00203     if (i_flags & e_elatime) {
00204       double d_elastop = CTBsystem::ElapsedTime();
00205       double d_dtime   = d_elastop - md_elastart;
00206 
00207       if (d_dtime > 1.e-6) {
00208         md_elatime  += d_dtime;
00209         md_elastart  = d_elastop;
00210       }
00211     }
00212   }
00213 }
00214 
00215 // Notes:
00216 //  1. The `if (d_dtime > 1.e6)' conditions above seem redundant, but they are
00217 //     needed to suppress a strange rounding error effect on Intel systems.
00218 //     The FPU registers are 80 bit, so the result of CpuTime() is calculated
00219 //     and passed as 80 bit value. However it's stored in the member variables
00220 //     as 64 bit number, thus truncated.
00221 //     Due to this, one can get a non-zero but very small d_dtime for two calls
00222 //     within the same CPU or clock time tick ! The if makes sure, that there
00223 //     are no spurious 1.e-8 sec elapesed CPU times in cases were less the
00224 //     time span was less than one clock tick.
00225 
00226 //------------------------------------------+-----------------------------------
00227 #if (defined(CTB__OutLine) || defined(CTBtraceClock__OutLine))
00228 #define inline
00229 #include "CTBtraceClock.icc"
00230 #undef  inline
00231 #endif

Generated at Fri Oct 24 18:11:30 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001