TStopwatch.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TStopwatch.cxx 35578 2010-09-22 12:40:13Z rdm $
00002 // Author: Fons Rademakers   11/10/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TStopwatch                                                           //
00015 //                                                                      //
00016 // Stopwatch class. This class returns the real and cpu time between    //
00017 // the start and stop events.                                           //
00018 //                                                                      //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "TStopwatch.h"
00022 #include "TTimeStamp.h"
00023 #include "TString.h"
00024 
00025 #if defined(R__UNIX)
00026 #  include <sys/times.h>
00027 #  include <unistd.h>
00028 static Double_t gTicks = 0;
00029 #elif defined(WIN32)
00030 #  include "TError.h"
00031 const Double_t gTicks = 1.0e-7;
00032 #  include "Windows4Root.h"
00033 #endif
00034 
00035 
00036 ClassImp(TStopwatch)
00037 
00038 //______________________________________________________________________________
00039 TStopwatch::TStopwatch()
00040 {
00041    // Create a stopwatch and start it.
00042 
00043 #ifdef R__UNIX
00044    if (gTicks <= 0.0)
00045       gTicks = (Double_t)sysconf(_SC_CLK_TCK);
00046 #endif
00047 
00048    fStopRealTime = 0;
00049    fStopCpuTime  = 0;
00050 
00051    Start();
00052 }
00053 
00054 //______________________________________________________________________________
00055 void TStopwatch::Start(Bool_t reset)
00056 {
00057    // Start the stopwatch. If reset is kTRUE reset the stopwatch before
00058    // starting it (including the stopwatch counter).
00059    // Use kFALSE to continue timing after a Stop() without
00060    // resetting the stopwatch.
00061 
00062    if (reset) {
00063       fState         = kUndefined;
00064       fTotalCpuTime  = 0;
00065       fTotalRealTime = 0;
00066       fCounter       = 0;
00067    }
00068    if (fState != kRunning) {
00069       fStartRealTime = GetRealTime();
00070       fStartCpuTime  = GetCPUTime();
00071    }
00072    fState = kRunning;
00073    fCounter++;
00074 }
00075 
00076 //______________________________________________________________________________
00077 void TStopwatch::Stop()
00078 {
00079    // Stop the stopwatch.
00080 
00081    fStopRealTime = GetRealTime();
00082    fStopCpuTime  = GetCPUTime();
00083 
00084    if (fState == kRunning) {
00085       fTotalCpuTime  += fStopCpuTime  - fStartCpuTime;
00086       fTotalRealTime += fStopRealTime - fStartRealTime;
00087    }
00088    fState = kStopped;
00089 }
00090 
00091 //______________________________________________________________________________
00092 void TStopwatch::Continue()
00093 {
00094    // Resume a stopped stopwatch. The stopwatch continues counting from the last
00095    // Start() onwards (this is like the laptimer function).
00096 
00097    if (fState == kUndefined)
00098       Error("Continue", "stopwatch not started");
00099 
00100    if (fState == kStopped) {
00101       fTotalCpuTime  -= fStopCpuTime  - fStartCpuTime;
00102       fTotalRealTime -= fStopRealTime - fStartRealTime;
00103    }
00104 
00105    fState = kRunning;
00106 }
00107 
00108 //______________________________________________________________________________
00109 Double_t TStopwatch::RealTime()
00110 {
00111    // Return the realtime passed between the start and stop events. If the
00112    // stopwatch was still running stop it first.
00113 
00114    if (fState == kUndefined)
00115       Error("RealTime", "stopwatch not started");
00116 
00117    if (fState == kRunning)
00118       Stop();
00119 
00120    return fTotalRealTime;
00121 }
00122 
00123 //______________________________________________________________________________
00124 Double_t TStopwatch::CpuTime()
00125 {
00126    // Return the cputime passed between the start and stop events. If the
00127    // stopwatch was still running stop it first.
00128 
00129    if (fState == kUndefined)
00130       Error("CpuTime", "stopwatch not started");
00131 
00132    if (fState == kRunning)
00133       Stop();
00134 
00135    return fTotalCpuTime;
00136 }
00137 
00138 //______________________________________________________________________________
00139 Double_t TStopwatch::GetRealTime()
00140 {
00141    // Private static method returning system realtime.
00142 
00143 #if defined(R__UNIX)
00144    return TTimeStamp();
00145 #elif defined(WIN32)
00146    union {
00147       FILETIME ftFileTime;
00148       __int64  ftInt64;
00149    } ftRealTime; // time the process has spent in kernel mode
00150    SYSTEMTIME st;
00151    GetSystemTime(&st);
00152    SystemTimeToFileTime(&st,&ftRealTime.ftFileTime);
00153    return (Double_t)ftRealTime.ftInt64 * gTicks;
00154 #endif
00155 }
00156 
00157 //______________________________________________________________________________
00158 Double_t TStopwatch::GetCPUTime()
00159 {
00160    // Private static method returning system CPU time.
00161 
00162 #if defined(R__UNIX)
00163    struct tms cpt;
00164    times(&cpt);
00165    return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
00166 #elif defined(WIN32)
00167 
00168    OSVERSIONINFO OsVersionInfo;
00169 
00170    //         Value                      Platform
00171    //----------------------------------------------------
00172    //  VER_PLATFORM_WIN32s          Win32s on Windows 3.1
00173    //  VER_PLATFORM_WIN32_WINDOWS   Win32 on Windows 95
00174    //  VER_PLATFORM_WIN32_NT        Windows NT
00175    //
00176    OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
00177    GetVersionEx(&OsVersionInfo);
00178    if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
00179       DWORD       ret;
00180       FILETIME    ftCreate,       // when the process was created
00181                   ftExit;         // when the process exited
00182 
00183       union {
00184          FILETIME ftFileTime;
00185          __int64  ftInt64;
00186       } ftKernel; // time the process has spent in kernel mode
00187 
00188       union {
00189          FILETIME ftFileTime;
00190          __int64  ftInt64;
00191       } ftUser;   // time the process has spent in user mode
00192 
00193       HANDLE hThread = GetCurrentThread();
00194       ret = GetThreadTimes (hThread, &ftCreate, &ftExit,
00195                                      &ftKernel.ftFileTime,
00196                                      &ftUser.ftFileTime);
00197       if (ret != TRUE) {
00198          ret = GetLastError ();
00199          ::Error ("GetCPUTime", " Error on GetProcessTimes 0x%lx", (int)ret);
00200       }
00201 
00202       // Process times are returned in a 64-bit structure, as the number of
00203       // 100 nanosecond ticks since 1 January 1601.  User mode and kernel mode
00204       // times for this process are in separate 64-bit structures.
00205       // To convert to floating point seconds, we will:
00206       //
00207       // Convert sum of high 32-bit quantities to 64-bit int
00208 
00209       return (Double_t) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
00210    } else
00211       return GetRealTime();
00212 #endif
00213 }
00214 
00215 //______________________________________________________________________________
00216 void TStopwatch::Print(Option_t *opt) const
00217 {
00218    // Print the real and cpu time passed between the start and stop events.
00219    // and the number of times (slices) this TStopwatch was called
00220    // (if this number > 1). If opt="m" print out realtime in milli second
00221    // precision. If opt="u" print out realtime in micro second precision.
00222 
00223    Double_t  realt = const_cast<TStopwatch*>(this)->RealTime();
00224    Double_t  cput  = const_cast<TStopwatch*>(this)->CpuTime();
00225 
00226    Int_t  hours = Int_t(realt / 3600);
00227    realt -= hours * 3600;
00228    Int_t  min   = Int_t(realt / 60);
00229    realt -= min * 60;
00230    Int_t  sec   = Int_t(realt);
00231 
00232    if (realt < 0) realt = 0;
00233    if (cput  < 0) cput  = 0;
00234 
00235    if (opt && *opt == 'm') {
00236       if (Counter() > 1) {
00237          Printf("Real time %d:%02d:%06.3f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
00238       } else {
00239          Printf("Real time %d:%02d:%06.3f, CP time %.3f", hours, min, realt, cput);
00240       }
00241    } else if (opt && *opt == 'u') {
00242       if (Counter() > 1) {
00243          Printf("Real time %d:%02d:%09.6f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
00244       } else {
00245          Printf("Real time %d:%02d:%09.6f, CP time %.3f", hours, min, realt, cput);
00246       }
00247    } else {
00248       if (Counter() > 1) {
00249          Printf("Real time %d:%02d:%02d, CP time %.3f, %d slices", hours, min, sec, cput, Counter());
00250       } else {
00251          Printf("Real time %d:%02d:%02d, CP time %.3f", hours, min, sec, cput);
00252       }
00253    }
00254 }

Generated on Tue Jul 5 14:11:23 2011 for ROOT_528-00b_version by  doxygen 1.5.1