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