]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/misc/AliL3Stopwatch.cxx
Merged HLT tag v1-2 with ALIROOT tag v3-09-Release.
[u/mrichter/AliRoot.git] / HLT / misc / AliL3Stopwatch.cxx
1 // @(#) $Id$
2
3 // Author: C. Loizides <mailto:loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright &copy ALICE HLT Group
5
6 #include "AliL3Stopwatch.h"
7
8 #ifdef no_root
9
10 #include "AliL3StandardIncludes.h"
11
12 #if GCCVERSION == 3
13 using namespace std;
14 #endif
15
16
17 /** \class AliL3Stopwatch
18 <pre>
19 //----------------------------------------------------
20 // AliL3Stopwatch
21 //
22 // Stopwatch class. This class returns the real and cpu time between   
23 // the start and stop events (taken from Root)
24 </pre>
25 */
26
27
28 ClassImp(AliL3Stopwatch)
29
30 clock_t AliL3Stopwatch::gTicks = 0;
31
32 AliL3Stopwatch::AliL3Stopwatch()
33 {
34   // Create a stopwatch and start it.
35   if (!gTicks) gTicks = (clock_t)sysconf(_SC_CLK_TCK);
36   fState         = kUndefined;
37   fTotalCpuTime  = 0;
38   fTotalRealTime = 0;
39   fCounter       = 0;
40   Start();
41 }
42
43 AliL3Stopwatch::~AliL3Stopwatch()
44 {
45 }
46
47 void AliL3Stopwatch::Start(Bool_t reset)
48 {
49   // Start the stopwatch. If reset is kTRUE reset the stopwatch before
50   // starting it (including the stopwatch counter).
51   // Use kFALSE to continue timing after a Stop() without
52   // resetting the stopwatch.
53
54   if (reset) {
55     fTotalCpuTime  = 0;
56       fTotalRealTime = 0;
57       fCounter       = 0;
58   }
59   if (fState != kRunning) {
60     fStartRealTime = GetRealTime();
61     fStartCpuTime  = GetCPUTime();
62   }
63   fState = kRunning;
64   fCounter++;
65 }
66
67 void AliL3Stopwatch::Stop()
68 {
69    // Stop the stopwatch.
70    fStopRealTime = GetRealTime();
71    fStopCpuTime  = GetCPUTime();
72    if (fState == kRunning) {
73      fTotalCpuTime  += fStopCpuTime  - fStartCpuTime;
74      fTotalRealTime += fStopRealTime - fStartRealTime;
75    }
76    fState = kStopped;
77 }
78
79 void AliL3Stopwatch::Continue()
80 {
81   // Resume a stopped stopwatch. The stopwatch continues counting from the last
82   // Start() onwards (this is like the laptimer function).
83   
84   if (fState == kUndefined){
85     cerr << "Error in AliL3Stopwatch::Continue! Stopwatch not started." << endl;
86     return;
87   }
88
89   if (fState == kStopped) {
90     fTotalCpuTime  -= fStopCpuTime  - fStartCpuTime;
91     fTotalRealTime -= fStopRealTime - fStartRealTime;
92   }
93
94   fState = kRunning;
95 }
96
97 Double_t AliL3Stopwatch::RealTime()
98 {
99   // Return the realtime passed between the start and stop events. If the
100   // stopwatch was still running stop it first.
101   
102   if (fState == kUndefined){
103     cerr << "Error in AliL3Stopwatch::RealTime! Stopwatch not started." << endl;
104     return -1.0;
105   }
106
107   if (fState == kRunning)
108     Stop();
109
110   return fTotalRealTime;
111 }
112
113 Double_t AliL3Stopwatch::CpuTime()
114 {
115   // Return the cputime passed between the start and stop events. If the
116   // stopwatch was still running stop it first.
117   
118   if (fState == kUndefined){
119     cerr << "Error in AliL3Stopwatch::CpuTime! Stopwatch not started." << endl;
120     return -1.0;
121   }
122   if (fState == kRunning)
123     Stop();
124
125   return fTotalCpuTime;
126 }
127
128 Double_t AliL3Stopwatch::GetRealTime()
129 {
130   struct tms cpt;
131   Double_t trt =  (Double_t)times(&cpt);
132   return trt / (Double_t)gTicks;
133 }
134
135 Double_t AliL3Stopwatch::GetCPUTime()
136 {
137    struct tms cpt;
138    times(&cpt);
139    return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
140 }
141
142 //______________________________________________________________________________
143 void AliL3Stopwatch::Print(Char_t *opt) const
144 {
145    // Print the real and cpu time passed between the start and stop events.
146    // and the number of times (slices) this TStopwatch was called
147    // (if this number > 1)
148
149    Double_t  realt = ((AliL3Stopwatch*)(this))->RealTime();
150
151    Int_t  hours = Int_t(realt / 3600);
152    realt -= hours * 3600;
153    Int_t  min   = Int_t(realt / 60);
154    realt -= min * 60;
155    Int_t  sec   = Int_t(realt);
156    Int_t counter = Counter();
157    if (counter <= 1 )
158       printf("Real time %d:%d:%d, CP time %.3f", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime());
159    else
160       printf("Real time %d:%d:%d, CP time %.3f, %d slices", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime(),counter);
161 }
162
163 #endif