Arcane  v4.1.1.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Timer.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* Timer.cc (C) 2000-2025 */
9/* */
10/* Gestion d'un timer. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/PlatformUtils.h"
15#include "arcane/utils/String.h"
16#include "arcane/utils/TraceInfo.h"
17#include "arcane/utils/FatalErrorException.h"
18#include "arcane/utils/ITraceMng.h"
19#include "arcane/utils/Iostream.h"
20
21#include "arcane/core/Timer.h"
22#include "arcane/core/ISubDomain.h"
23#include "arcane/core/ITimerMng.h"
24#include "arcane/core/ITimeStats.h"
25
26#include "arccore/trace/internal/TimeMetric.h"
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arcane
32{
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
47: m_timer_mng(tm)
48, m_type(type)
50, m_is_activated(false)
52, m_total_time(0.0)
53, m_name(name)
54, m_start_time(0.0)
55{
56}
57
58/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60
62~Timer()
63{
64}
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
70start()
71{
73 ARCANE_FATAL("Timer is already activated");
74
75 //\todo Get time
77 m_timer_mng->beginTimer(this);
78
80 m_is_activated = true;
81}
82
83/*---------------------------------------------------------------------------*/
84/*---------------------------------------------------------------------------*/
85
87stop()
88{
89 if (m_is_activated){
90 m_activation_time = m_timer_mng->endTimer(this);
91 m_is_activated = false;
93 }
94 else
95 ARCANE_FATAL("Timer is not activated");
96 return m_activation_time;
97}
98
100reset()
101{
102 m_is_activated = false;
103 m_activation_time = 0.0;
104 m_total_time = 0.0;
105
106}
107
108/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117void Timer::Action::
118_init()
119{
120 if (m_stats)
121 if (!m_stats->isGathering())
122 m_stats = 0;
123 if (m_stats)
124 m_stats->beginAction(m_action_name);
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130Timer::Action::
131Action(ISubDomain* sub_domain,const String& action_name,bool print_time)
132: m_stats(0)
133, m_action_name(action_name)
134, m_print_time(print_time)
135{
136 if (sub_domain)
137 m_stats = sub_domain->timeStats();
138 _init();
139}
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
144Timer::Action::
145Action(ITimeStats* stats,const String& action_name,bool print_time)
146: m_stats(stats)
147, m_action_name(action_name)
148, m_print_time(print_time)
149{
150 _init();
151}
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156Timer::Action::
157~Action()
158{
159 if (m_stats)
160 m_stats->endAction(m_action_name,m_print_time);
161}
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169void Timer::Phase::
170_init()
171{
172 if (m_stats)
173 if (!m_stats->isGathering())
174 m_stats = 0;
175 if (m_stats)
176 m_stats->beginPhase(m_phase_type);
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182Timer::Phase::
183Phase(ISubDomain* sub_domain,eTimePhase pt)
184: m_stats(0)
185, m_phase_type(pt)
186{
187 if (sub_domain)
188 m_stats = sub_domain->timeStats();
189 _init();
190}
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
194
195Timer::Phase::
196Phase(ITimeStats* stats,eTimePhase pt)
197: m_stats(stats)
198, m_phase_type(pt)
199{
200 _init();
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206Timer::Phase::
207~Phase()
208{
209 if (m_stats)
210 m_stats->endPhase(m_phase_type);
211}
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216/*---------------------------------------------------------------------------*/
217/*---------------------------------------------------------------------------*/
218
219Timer::SimplePrinter::
220SimplePrinter(ITraceMng* tm,const String& msg)
221: m_trace_mng(tm)
222, m_begin_time(0.0)
223, m_is_active(true)
224, m_message(msg)
225{
226 _init();
227}
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
231
232Timer::SimplePrinter::
233SimplePrinter(ITraceMng* tm,const String& msg,bool is_active)
234: m_trace_mng(tm)
235, m_begin_time(0.0)
236, m_is_active(is_active)
237, m_message(msg)
238{
239 _init();
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245Timer::SimplePrinter::
246~SimplePrinter()
247{
248 if (m_is_active){
249 Real end_time = platform::getRealTime();
250 Real diff_time = end_time - m_begin_time;
251 m_trace_mng->info() << m_message << " time=" << diff_time;
252 }
253}
254
255/*---------------------------------------------------------------------------*/
256/*---------------------------------------------------------------------------*/
257
258void Timer::SimplePrinter::
259_init()
260{
261 if (m_is_active)
262 m_begin_time = platform::getRealTime();
263}
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268TimeMetricAction Timer::
269phaseAction(ITimeStats* s,eTimePhase phase)
270{
271 ITimeMetricCollector* c = nullptr;
272 if (s)
273 c = s->metricCollector();
274 if (!c)
275 return TimeMetricAction();
276 return TimeMetricAction(c,TimeMetricActionBuildInfo(String(),phase));
277}
278
279/*---------------------------------------------------------------------------*/
280/*---------------------------------------------------------------------------*/
281} // namespace Arcane
282
283/*---------------------------------------------------------------------------*/
284/*---------------------------------------------------------------------------*/
285
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Interface du gestionnaire d'un sous-domaine.
Definition ISubDomain.h:74
Interface gérant les statistiques sur les temps d'exécution.
Definition ITimeStats.h:43
virtual bool isGathering() const =0
Indique si les statistiques sont actives.
Interface d'un gestionnaire de timer.
Definition ITimerMng.h:49
Interface du gestionnaire de traces.
Chaîne de caractères unicode.
ITimeStats * m_stats
Gestionnaire de sous-domaine.
Definition Timer.h:136
const String & name() const
Retourne le nom du timer.
Definition Timer.h:213
eTimerType m_type
Type du timer.
Definition Timer.h:242
Real m_activation_time
Temps passé lors de la dernière activation.
Definition Timer.h:245
Integer m_nb_activated
Nombre de fois que le timer a été activé
Definition Timer.h:243
String m_name
Nom du timer.
Definition Timer.h:247
ITimerMng * timerMng() const
Gestionnaire associé à ce timer.
Definition Timer.h:231
Real stop()
Désactive le timer.
Definition Timer.cc:87
eTimerType
Type du timer.
Definition Timer.h:67
~Timer()
Libère les ressources.
Definition Timer.cc:62
ITimerMng * m_timer_mng
Gestionnaire de timer.
Definition Timer.h:241
eTimerType type() const
Retourne le type du temps utilisé
Definition Timer.h:225
Timer(ISubDomain *sd, const String &name, eTimerType type)
Construit un timer.
Definition Timer.cc:37
void reset()
Remet à zéro les compteurs de temps.
Definition Timer.cc:100
Real m_start_time
Temps du début de la dernière activation.
Definition Timer.h:248
bool m_is_activated
true si le timer est actif
Definition Timer.h:244
void start()
Active le timer.
Definition Timer.cc:70
Real m_total_time
Temps total passé dans le timer.
Definition Timer.h:246
ARCCORE_BASE_EXPORT Real getRealTime()
Temps Real utilisé en secondes.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
double Real
Type représentant un réel.
eTimePhase
Phase d'une action temporelle.