EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stopwatch.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Stopwatch.h
1 // @(#)root/base:$Name: $:$Id: Stopwatch.h,v 1.2 2008/02/25 13:53:06 ppost Exp $
2 // Author: Fons Rademakers 11/10/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef _Stopwatch
13 #define _Stopwatch
14 
15 
17 // //
18 // TStopwatch //
19 // //
20 // Stopwatch class. This class returns the real and cpu time between //
21 // the start and stop events. //
22 // //
24 
25 
26 class Stopwatch {
27 
28 private:
30 
31  double fStartRealTime; //wall clock start time
32  double fStopRealTime; //wall clock stop time
33  double fStartCpuTime; //cpu start time
34  double fStopCpuTime; //cpu stop time
35  double fTotalCpuTime; //total cpu time
36  double fTotalRealTime; //total real time
37  EState fState; //stopwatch state
38  int fCounter; //number of times the stopwatch was started
39 
40  inline static double GetRealTime();
41  inline static double GetCPUTime();
42 
43 public:
44  inline Stopwatch();
45  inline void Start(int reset = 1);
46  inline void Stop();
47  inline void Continue();
48  inline int Counter() const { return fCounter; }
49  inline double RealTime();
50  inline void Reset() { ResetCpuTime(); ResetRealTime(); }
51  inline void ResetCpuTime(double time = 0) { Stop(); fTotalCpuTime = time; }
52  inline void ResetRealTime(double time = 0) { Stop(); fTotalRealTime = time; }
53  inline double CpuTime();
54 };
55 
56 
57 //______________________________________________________________________________
58 
59 #ifndef _WIN32
60 #define R__UNIX
61 #endif
62 
63 #if defined(R__UNIX)
64 # include <sys/time.h>
65 # include <sys/times.h>
66 # include <unistd.h>
67 static double gTicks = 0;
68 #elif defined(R__VMS)
69 # include <time.h>
70 # include <unistd.h>
71 static double gTicks = 1000;
72 #elif defined(_WIN32)
73 # include <sys/types.h>
74 # include <sys/timeb.h>
75 # include <windows.h>
76 # undef min // unfortunately, <windows.h> defines min/max as macros
77 # undef max
78 //# include "TError.h"
79 const double gTicks = 1.0e-7;
80 static __int64 gTicksQPC = -1; // < 0 means "not yet initialized"
81 //# include "Windows4Root.h"
82 #endif
83 
84 
86 {
87  // Create a stopwatch and start it.
88 
89 #ifdef R__UNIX
90  if (!gTicks)
91  gTicks = (double)sysconf(_SC_CLK_TCK);
92 #endif
93 #ifdef _WIN32
94  if( gTicksQPC < 0 ) {
95  LARGE_INTEGER freq;
96  QueryPerformanceFrequency( &freq );
97  gTicksQPC = (double)freq.QuadPart;
98  }
99 #endif
100 
101  Start();
102 }
103 
104 //______________________________________________________________________________
105 inline void Stopwatch::Start(int reset)
106 {
107  // Start the stopwatch. If reset is kTRUE reset the stopwatch before
108  // starting it (including the stopwatch counter).
109  // Use kFALSE to continue timing after a Stop() without
110  // resetting the stopwatch.
111 
112  if (reset) {
113  fState = kUndefined;
114  fTotalCpuTime = 0;
115  fTotalRealTime = 0;
116  fCounter = 0;
117  }
118  if (fState != kRunning) {
121  }
122  fState = kRunning;
123  fCounter++;
124 }
125 
126 //______________________________________________________________________________
127 inline void Stopwatch::Stop()
128 {
129  // Stop the stopwatch.
130 
133 
134  if (fState == kRunning) {
137  }
138  fState = kStopped;
139 }
140 
141 //______________________________________________________________________________
142 inline void Stopwatch::Continue()
143 {
144  // Resume a stopped stopwatch. The stopwatch continues counting from the last
145  // Start() onwards (this is like the laptimer function).
146 
147  if (fState == kUndefined){
148  //cout<< "stopwatch not started"<<endl;
149  return;
150  }
151  if (fState == kStopped) {
154  }
155 
156  fState = kRunning;
157 }
158 
159 //______________________________________________________________________________
160 inline double Stopwatch::RealTime()
161 {
162  // Return the realtime passed between the start and stop events. If the
163  // stopwatch was still running stop it first.
164 
165  if (fState == kUndefined){
166  //cout<<"stopwatch not started"<<endl;
167  return 0;
168  }
169  if (fState == kRunning)
170  Stop();
171 
172  return fTotalRealTime;
173 }
174 
175 //______________________________________________________________________________
176 inline double Stopwatch::CpuTime()
177 {
178  // Return the cputime passed between the start and stop events. If the
179  // stopwatch was still running stop it first.
180 
181  if (fState == kUndefined){
182  //cout<<"stopwatch not started"<<endl;
183  return 0;
184  }
185  if (fState == kRunning)
186  Stop();
187 
188  return fTotalCpuTime;
189 }
190 
191 //______________________________________________________________________________
192 inline double Stopwatch::GetRealTime()
193 {
194 #if defined(R__UNIX)
195  struct timeval tp;
196  gettimeofday(&tp, 0);
197  return tp.tv_sec + (tp.tv_usec)*1.e-6;
198 #elif defined(_WIN32)
199  LARGE_INTEGER counter;
200  QueryPerformanceCounter( &counter );
201  return (double)counter.QuadPart / gTicksQPC;
202 #else
203  return 0;
204 #endif
205 }
206 
207 //______________________________________________________________________________
208 inline double Stopwatch::GetCPUTime()
209 {
210  // Private static method returning system CPU time.
211 
212 #if defined(R__UNIX)
213  struct tms cpt;
214  times(&cpt);
215  return (double)(cpt.tms_utime+cpt.tms_stime) / gTicks;
216 #elif defined(R__VMS)
217  return (double)clock() / gTicks;
218 #elif defined(_WIN32)
219  OSVERSIONINFO OsVersionInfo;
220 
221  // Value Platform
222  //----------------------------------------------------
223  // VER_PLATFORM_WIN32s Win32s on Windows 3.1
224  // VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95
225  // VER_PLATFORM_WIN32_NT Windows NT
226  //
227  OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
228  GetVersionEx(&OsVersionInfo);
229  if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
230  DWORD ret;
231  FILETIME ftCreate, // when the process was created
232  ftExit; // when the process exited
233 
234  union {
235  FILETIME ftFileTime;
236  __int64 ftInt64;
237  } ftKernel; // time the process has spent in kernel mode
238 
239  union {
240  FILETIME ftFileTime;
241  __int64 ftInt64;
242  } ftUser; // time the process has spent in user mode
243 
244  HANDLE hProcess = GetCurrentProcess();
245  ret = GetProcessTimes (hProcess, &ftCreate, &ftExit,
246  &ftKernel.ftFileTime,
247  &ftUser.ftFileTime);
248  if (ret != TRUE) {
249  ret = GetLastError ();
250  //cout<<" Error on GetProcessTimes 0x%lx"<<endl;
251  return 0;
252  }
253 
254  // Process times are returned in a 64-bit structure, as the number of
255  // 100 nanosecond ticks since 1 January 1601. User mode and kernel mode
256  // times for this process are in separate 64-bit structures.
257  // To convert to floating point seconds, we will:
258  //
259  // Convert sum of high 32-bit quantities to 64-bit int
260 
261  return (double) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
262  } else
263  return GetRealTime();
264 #endif
265 }
266 
267 #endif