13 // #define MILLISECOND_RESOLUTION to track time with greater precision
14 // If not defined the resolution is to the second only
16 #define MILLISECOND_RESOLUTION
17 #ifdef MILLISECOND_RESOLUTION
18 double gTicks
= 1.0e-4; // for millisecond resolution
20 double gTicks
= 1.0e-7; // for second resolution
23 Stopwatch::Stopwatch() {
26 if (!gTicks
) gTicks
= (clock_t)sysconf(_SC_CLK_TCK
);
31 mCreatedStack
= PR_FALSE
;
32 mSavedStates
= nsnull
;
36 Stopwatch::~Stopwatch() {
39 while ((state
= (EState
*) mSavedStates
->Pop())) {
46 void Stopwatch::Start(PRBool reset
) {
51 if (fState
!= kRunning
) {
53 fStartRealTime
= GetRealTime();
54 fStartCpuTime
= GetCPUTime();
57 fStartRealTime
= (double)times(&cpt
) / gTicks
;
58 fStartCpuTime
= (double)(cpt
.tms_utime
+cpt
.tms_stime
) / gTicks
;
64 void Stopwatch::Stop() {
67 fStopRealTime
= GetRealTime();
68 fStopCpuTime
= GetCPUTime();
71 fStopRealTime
= (double)times(&cpt
) / gTicks
;
72 fStopCpuTime
= (double)(cpt
.tms_utime
+cpt
.tms_stime
) / gTicks
;
74 if (fState
== kRunning
) {
75 fTotalCpuTime
+= fStopCpuTime
- fStartCpuTime
;
76 fTotalRealTime
+= fStopRealTime
- fStartRealTime
;
82 void Stopwatch::SaveState() {
84 mSavedStates
= new nsDeque(nsnull
);
87 mCreatedStack
= PR_TRUE
;
89 EState
* state
= new EState();
92 mSavedStates
->PushFront((void*) state
);
96 void Stopwatch::RestoreState() {
97 EState
* state
= nsnull
;
98 state
= (EState
*) mSavedStates
->Pop();
100 if (*state
== kRunning
&& fState
== kStopped
)
102 else if (*state
== kStopped
&& fState
== kRunning
)
107 NS_WARNING("Stopwatch::RestoreState(): The saved state stack is empty.\n");
111 void Stopwatch::Continue() {
113 if (fState
!= kUndefined
) {
115 if (fState
== kStopped
) {
116 fTotalCpuTime
-= fStopCpuTime
- fStartCpuTime
;
117 fTotalRealTime
-= fStopRealTime
- fStartRealTime
;
125 // NOTE: returns seconds regardless of the state of the MILLISECOND_RESOLUTION #define
127 double Stopwatch::RealTime() {
129 if (fState
!= kUndefined
) {
130 if (fState
== kRunning
)
134 #ifdef MILLISECOND_RESOLUTION
135 return fTotalRealTime
/1000;
137 return fTotalRealTime
;
141 // NOTE: returns milliseconds regardless of the state of the MILLISECOND_RESOLUTION #define
143 double Stopwatch::RealTimeInMilliseconds() {
145 if (fState
!= kUndefined
) {
146 if (fState
== kRunning
)
150 #ifdef MILLISECOND_RESOLUTION
151 return fTotalRealTime
;
153 return fTotalRealTime
* 1000; // we don;t have milliseconds, so fake it
157 // NOTE: returns seconds regardless of the state of the MILLISECOND_RESOLUTION define
159 double Stopwatch::CpuTime() {
160 if (fState
!= kUndefined
) {
162 if (fState
== kRunning
)
166 #ifdef MILLISECOND_RESOLUTION
167 return fTotalCpuTime
/ 1000; // adjust from milliseconds to seconds
169 return fTotalCpuTime
;
174 double Stopwatch::GetRealTime(){
176 // return(double)clock() / gTicks;
177 return(double)clock() / 1000000L;
178 #elif defined(R__UNIX)
180 return (double)times(&cpt
) / gTicks
;
181 #elif defined(R__VMS)
182 return(double)clock()/gTicks
;
184 union {FILETIME ftFileTime
;
186 } ftRealTime
; // time the process has spent in kernel mode
189 SystemTimeToFileTime(&st
,&ftRealTime
.ftFileTime
);
190 return (double)ftRealTime
.ftInt64
* gTicks
;
195 double Stopwatch::GetCPUTime(){
197 // return(double)clock() / gTicks;
198 return(double)clock();
199 #elif defined(R__UNIX)
202 return (double)(cpt
.tms_utime
+cpt
.tms_stime
) / gTicks
;
203 #elif defined(R__VMS)
204 return(double)clock()/gTicks
;
210 FILETIME ftCreate
, // when the process was created
211 ftExit
; // when the process exited
213 union {FILETIME ftFileTime
;
215 } ftKernel
; // time the process has spent in kernel mode
217 union {FILETIME ftFileTime
;
219 } ftUser
; // time the process has spent in user mode
221 HANDLE hProcess
= GetCurrentProcess();
222 ret
= GetProcessTimes (hProcess
, &ftCreate
, &ftExit
,
223 &ftKernel
.ftFileTime
,
226 ret
= GetLastError ();
228 printf("%s 0x%lx\n"," Error on GetProcessTimes", (int)ret
);
233 * Process times are returned in a 64-bit structure, as the number of
234 * 100 nanosecond ticks since 1 January 1601. User mode and kernel mode
235 * times for this process are in separate 64-bit structures.
236 * To convert to floating point seconds, we will:
238 * Convert sum of high 32-bit quantities to 64-bit int
241 return (double) (ftKernel
.ftInt64
+ ftUser
.ftInt64
) * gTicks
;
247 void Stopwatch::Print(void) {
248 // Print the real and cpu time passed between the start and stop events.
250 double realt
= RealTimeInMilliseconds();
252 int hours
= int(realt
/ 3600000);
253 realt
-= hours
* 3600000;
254 int min
= int(realt
/ 60000);
255 realt
-= min
* 60000;
256 int sec
= int(realt
/1000);
258 #ifdef MOZ_PERF_METRICS
260 RAPTOR_STOPWATCH_TRACE(("Real time %d:%d:%d.%d, CP time %.3f\n", hours
, min
, sec
, ms
, CpuTime()));
263 printf("Real time %d:%d:%d.%d, CP time %.3f\n", hours
, min
, sec
, ms
, CpuTime());