Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
TraceTimer.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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) 2008 */
9/* */
10/* Timer pour message de trace. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_TRACETIMER_H
13#define ARCANE_UTILS_TRACETIMER_H
14
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18ARCANE_BEGIN_NAMESPACE
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 : utilise la fonction MPI_Wtime de MPI
34 * DAY_TRACE_TIMER : mesure le temps qui passe (** SVr4, BSD 4.3 **)
35 * CPU_TRACE_TIMER : mesure le nombre de 'ticks' CPU (** SVR4, SVID, POSIX, X/OPEN, BSD 4.3 **)
36 * : (on peut distinguer le processus même de ses enfants et appels système)
37 * : pour plus d'infos voir 'man times'
38 * SCPU_TRACE_TIMER : comme CPU_TRACE_TIMER mais inclus les temps système et des enfants
39 *
40 * On peut aussi utiliser
41 * return ((double) clock())/CLOCKS_PER_SEC;
42 * qui compte les cycles consommés et limite à 72min (arch 32bits) [need <time.h>]
43 * ou getrusage qui ressemble à times() mais fourni plus de paramètres
44 */
45#endif
46
47#if defined(MPI_TRACE_TIMER)
48#include "mpi.h"
49class InnerTimer_MPI {
50protected:
51 double systemTime() const {
52 return MPI_Wtime();
53 }
54public:
55 static const char * type() { return "MPI Timer"; }
56};
57#endif /* MPI_TRACE_TIMER */
58
59#if defined(DAY_TRACE_TIMER)
61protected:
62 double systemTime() {
63 return platform::getRealTime();
64 }
65public:
66 static const char * type() { return "Day Timer"; }
67};
68#endif /* DAY_TRACE_TIMER */
69
70#if defined(CPU_TRACE_TIMER)
71#include <unistd.h> // sysconf()
72#include <sys/times.h> // times()
73class InnerTimer_CPU {
74protected:
75 struct tms tp;
76 double systemTime() {
77 static const long _CLK_TCK = sysconf(_SC_CLK_TCK);
78 times(&tp);
79 return static_cast<double>(tp.tms_utime)/_CLK_TCK; // user only
80 }
81public:
82 static const char * type() { return "Cpu Timer"; }
83};
84#endif /* CPU_TRACE_TIMER */
85
86#if defined(SCPU_TRACE_TIMER)
87#include <unistd.h> // sysconf()
88#include <sys/times.h> // times()
89class InnerTimer_SysCPU {
90protected:
91 struct tms tp;
92 double systemTime() {
93 static const long _CLK_TCK = sysconf(_SC_CLK_TCK);
94 return static_cast<double>(times(&tp))/_CLK_TCK; // user+system+children
95 }
96public:
97 static const char * type() { return "SysCpu Timer"; }
98};
99#endif /* SCPU_TRACE_TIMER */
100
101
102template<typename Model>
103class TraceTimerT : public Model {
104public:
105 enum ClockState { init, stopped, running };
106private:
107 //! Timer State
108 ClockState state;
109
110 //! Initial and last time
111 double t0,t1;
112
113 //! Cumulate time
114 double total;
115public:
116 //! New timer
117 /*! Autostart by default */
118 TraceTimerT(const bool _start = true)
119 : state(init), t0(0), t1(0), total(0) {
120 if (_start) start();
121 }
122
123 //! reset timer
124 void reset() {
125 state = init;
126 total = 0;
127 t0 = t1 = 0;
128 }
129
130 //! start the timer or restart without cumulate
131 void start() {
132 state = running;
133 t0 = this->systemTime();
134 }
135
136 //! start or restart the timer and cumuluate
137 /*! Usefull for starting the count of a new partial time and keep the cumulative timer */
138 void restart() {
139 if (state == running) {
140 t1 = this->systemTime();
141 total += t1 - t0;
142 }
143 state = running;
144 t0 = this->systemTime();
145 }
146
147 //! stop timer
148 double stop() {
149 assert(state == running);
150 state = stopped;
151 t1 = this->systemTime();
152 total += t1 - t0;
153 return t1 - t0;
154 }
155
156 //! return state of timer
157 ClockState getState() const {
158 return state;
159 }
160
161 //! get partial time
162 double getTime() {
163 assert(state != init);
164 if (state == running)
165 t1 = this->systemTime();
166 return t1 - t0;
167 }
168
169 //! get total time
170 double getCumulTime() {
171 assert(state != init);
172 if (state == running)
173 return total + this->systemTime() - t0;
174 // si state stopped
175 assert(state == stopped);
176 return total;
177 }
178};
179
180#if defined(MPI_TRACE_TIMER)
181typedef TraceTimerT<InnerTimer_MPI> TraceTimer;
182#elif defined(DAY_TRACE_TIMER)
183typedef TraceTimerT<InnerTimer_DAY> TraceTimer;
184#elif defined(CPU_TRACE_TIMER)
185typedef TraceTimerT<InnerTimer_CPU> TraceTimer;
186#elif defined(SCPU_TRACE_TIMER)
187typedef TraceTimerT<InnerTimer_SysCPU> TraceTimer;
188#endif
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
192
193ARCANE_END_NAMESPACE
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
197
198#endif /* ARCANE_UTILS_TRACETIMER_H */
double getCumulTime()
get total time
Definition TraceTimer.h:170
double getTime()
get partial time
Definition TraceTimer.h:162
void start()
start the timer or restart without cumulate
Definition TraceTimer.h:131
void restart()
start or restart the timer and cumuluate
Definition TraceTimer.h:138
TraceTimerT(const bool _start=true)
New timer.
Definition TraceTimer.h:118
ClockState getState() const
return state of timer
Definition TraceTimer.h:157
void reset()
reset timer
Definition TraceTimer.h:124
double stop()
stop timer
Definition TraceTimer.h:148