Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
TraceTimer.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* TraceMessage.h (C) 2000-2008 */
9/* */
10/* Timer for trace message. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_TRACETIMER_H
13#define ARCANE_UTILS_TRACETIMER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17namespace Arcane
18{
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23#define DAY_TRACE_TIMER
24
25#include <cassert>
26
27#if defined(MPI_TRACE_TIMER)
28#elif defined(DAY_TRACE_TIMER)
29#elif defined(CPU_TRACE_TIMER)
30#elif defined(SCPU_TRACE_TIMER)
31#else
32#error "TraceTimer type not defined"
33/* MPI_TRACE_TIMER : uses the MPI_Wtime function from MPI
34 * DAY_TRACE_TIMER : measures the elapsed time (** SVr4, BSD 4.3 **)
35 * CPU_TRACE_TIMER : measures the number of CPU 'ticks' (** SVR4, SVID, POSIX, X/OPEN, BSD 4.3 **)
36 * : (it can distinguish the process itself from its children and system calls)
37 * : for more info see 'man times'
38 * SCPU_TRACE_TIMER : like CPU_TRACE_TIMER but includes system and child times
39 *
40 * We can also use
41 * return ((double) clock())/CLOCKS_PER_SEC;
42 * which counts consumed cycles and is limited to 72min (32bit arch) [need <time.h>]
43 * or getrusage which resembles times() but provides more parameters
44 */
45#endif
46
47#if defined(MPI_TRACE_TIMER)
48#include "mpi.h"
49class InnerTimer_MPI
50{
51 protected:
52
53 double systemTime() const
54 {
55 return MPI_Wtime();
56 }
57
58 public:
59
60 static const char* type() { return "MPI Timer"; }
61};
62#endif /* MPI_TRACE_TIMER */
63
64#if defined(DAY_TRACE_TIMER)
66{
67 protected:
68
69 double systemTime()
70 {
71 return platform::getRealTime();
72 }
73
74 public:
75
76 static const char* type() { return "Day Timer"; }
77};
78#endif /* DAY_TRACE_TIMER */
79
80#if defined(CPU_TRACE_TIMER)
81#include <unistd.h> // sysconf()
82#include <sys/times.h> // times()
83class InnerTimer_CPU
84{
85 protected:
86
87 struct tms tp;
88 double systemTime()
89 {
90 static const long _CLK_TCK = sysconf(_SC_CLK_TCK);
91 times(&tp);
92 return static_cast<double>(tp.tms_utime) / _CLK_TCK; // user only
93 }
94
95 public:
96
97 static const char* type() { return "Cpu Timer"; }
98};
99#endif /* CPU_TRACE_TIMER */
100
101#if defined(SCPU_TRACE_TIMER)
102#include <unistd.h> // sysconf()
103#include <sys/times.h> // times()
104class InnerTimer_SysCPU
105{
106 protected:
107
108 struct tms tp;
109 double systemTime()
110 {
111 static const long _CLK_TCK = sysconf(_SC_CLK_TCK);
112 return static_cast<double>(times(&tp)) / _CLK_TCK; // user+system+children
113 }
114
115 public:
116
117 static const char* type() { return "SysCpu Timer"; }
118};
119#endif /* SCPU_TRACE_TIMER */
120
121template <typename Model>
122class TraceTimerT : public Model
123{
124 public:
125
126 enum ClockState
127 {
128 init,
129 stopped,
130 running
131 };
132
133 private:
134
135 //! Timer State
136 ClockState state;
137
138 //! Initial and last time
139 double t0, t1;
140
141 //! Cumulate time
142 double total;
143
144 public:
145
146 //! New timer
147 /*! Autostart by default */
148 TraceTimerT(const bool _start = true)
149 : state(init)
150 , t0(0)
151 , t1(0)
152 , total(0)
153 {
154 if (_start)
155 start();
156 }
157
158 //! reset timer
159 void reset()
160 {
161 state = init;
162 total = 0;
163 t0 = t1 = 0;
164 }
165
166 //! start the timer or restart without cumulate
167 void start()
168 {
169 state = running;
170 t0 = this->systemTime();
171 }
172
173 //! start or restart the timer and cumuluate
174 /*! Usefull for starting the count of a new partial time and keep the cumulative timer */
175 void restart()
176 {
177 if (state == running) {
178 t1 = this->systemTime();
179 total += t1 - t0;
180 }
181 state = running;
182 t0 = this->systemTime();
183 }
184
185 //! stop timer
186 double stop()
187 {
188 assert(state == running);
189 state = stopped;
190 t1 = this->systemTime();
191 total += t1 - t0;
192 return t1 - t0;
193 }
194
195 //! return state of timer
196 ClockState getState() const
197 {
198 return state;
199 }
200
201 //! get partial time
202 double getTime()
203 {
204 assert(state != init);
205 if (state == running)
206 t1 = this->systemTime();
207 return t1 - t0;
208 }
209
210 //! get total time
212 {
213 assert(state != init);
214 if (state == running)
215 return total + this->systemTime() - t0;
216 // si state stopped
217 assert(state == stopped);
218 return total;
219 }
220};
221
222#if defined(MPI_TRACE_TIMER)
223typedef TraceTimerT<InnerTimer_MPI> TraceTimer;
224#elif defined(DAY_TRACE_TIMER)
225typedef TraceTimerT<InnerTimer_DAY> TraceTimer;
226#elif defined(CPU_TRACE_TIMER)
227typedef TraceTimerT<InnerTimer_CPU> TraceTimer;
228#elif defined(SCPU_TRACE_TIMER)
229typedef TraceTimerT<InnerTimer_SysCPU> TraceTimer;
230#endif
231
232/*---------------------------------------------------------------------------*/
233/*---------------------------------------------------------------------------*/
234
235} // namespace Arcane
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
240#endif /* ARCANE_UTILS_TRACETIMER_H */
double getCumulTime()
get total time
Definition TraceTimer.h:211
double getTime()
get partial time
Definition TraceTimer.h:202
void start()
start the timer or restart without cumulate
Definition TraceTimer.h:167
void restart()
start or restart the timer and cumuluate
Definition TraceTimer.h:175
TraceTimerT(const bool _start=true)
New timer.
Definition TraceTimer.h:148
ClockState getState() const
return state of timer
Definition TraceTimer.h:196
void reset()
reset timer
Definition TraceTimer.h:159
double stop()
stop timer
Definition TraceTimer.h:186
Real getRealTime()
Real time used in seconds.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --