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