Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Timer.cc
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/* Timer.cc (C) 2000-2025 */
9/* */
10/* Timer management. */
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
38: Timer(sd->timerMng(), name, type)
39{
40}
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
116void Timer::Action::
117_init()
118{
119 if (m_stats)
120 if (!m_stats->isGathering())
121 m_stats = 0;
122 if (m_stats)
123 m_stats->beginAction(m_action_name);
124}
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
129Timer::Action::
130Action(ISubDomain* sub_domain, const String& action_name, bool print_time)
131: m_stats(0)
132, m_action_name(action_name)
133, m_print_time(print_time)
134{
135 if (sub_domain)
136 m_stats = sub_domain->timeStats();
137 _init();
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143Timer::Action::
144Action(ITimeStats* stats, const String& action_name, bool print_time)
145: m_stats(stats)
146, m_action_name(action_name)
147, m_print_time(print_time)
148{
149 _init();
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
155Timer::Action::
156~Action()
157{
158 if (m_stats)
159 m_stats->endAction(m_action_name, m_print_time);
160}
161
162/*---------------------------------------------------------------------------*/
163/*---------------------------------------------------------------------------*/
164
165/*---------------------------------------------------------------------------*/
166/*---------------------------------------------------------------------------*/
167
168void Timer::Phase::
169_init()
170{
171 if (m_stats)
172 if (!m_stats->isGathering())
173 m_stats = 0;
174 if (m_stats)
175 m_stats->beginPhase(m_phase_type);
176}
177
178/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180
181Timer::Phase::
182Phase(ISubDomain* sub_domain, eTimePhase pt)
183: m_stats(0)
184, m_phase_type(pt)
185{
186 if (sub_domain)
187 m_stats = sub_domain->timeStats();
188 _init();
189}
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
193
194Timer::Phase::
195Phase(ITimeStats* stats, eTimePhase pt)
196: m_stats(stats)
197, m_phase_type(pt)
198{
199 _init();
200}
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
204
205Timer::Phase::
206~Phase()
207{
208 if (m_stats)
209 m_stats->endPhase(m_phase_type);
210}
211
212/*---------------------------------------------------------------------------*/
213/*---------------------------------------------------------------------------*/
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218Timer::SimplePrinter::
219SimplePrinter(ITraceMng* tm, const String& msg)
220: m_trace_mng(tm)
221, m_begin_time(0.0)
222, m_is_active(true)
223, m_message(msg)
224{
225 _init();
226}
227
228/*---------------------------------------------------------------------------*/
229/*---------------------------------------------------------------------------*/
230
231Timer::SimplePrinter::
232SimplePrinter(ITraceMng* tm, const String& msg, bool is_active)
233: m_trace_mng(tm)
234, m_begin_time(0.0)
235, m_is_active(is_active)
236, m_message(msg)
237{
238 _init();
239}
240
241/*---------------------------------------------------------------------------*/
242/*---------------------------------------------------------------------------*/
243
244Timer::SimplePrinter::
245~SimplePrinter()
246{
247 if (m_is_active) {
248 Real end_time = platform::getRealTime();
249 Real diff_time = end_time - m_begin_time;
250 m_trace_mng->info() << m_message << " time=" << diff_time;
251 }
252}
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257void Timer::SimplePrinter::
258_init()
259{
260 if (m_is_active)
261 m_begin_time = platform::getRealTime();
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267TimeMetricAction Timer::
268phaseAction(ITimeStats* s, eTimePhase phase)
269{
270 ITimeMetricCollector* c = nullptr;
271 if (s)
272 c = s->metricCollector();
273 if (!c)
274 return TimeMetricAction();
275 return TimeMetricAction(c, TimeMetricActionBuildInfo(String(), phase));
276}
277
278/*---------------------------------------------------------------------------*/
279/*---------------------------------------------------------------------------*/
280
281} // namespace Arcane
282
283/*---------------------------------------------------------------------------*/
284/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Interface of the subdomain manager.
Definition ISubDomain.h:75
Interface managing execution time statistics.
Definition ITimeStats.h:44
virtual bool isGathering() const =0
Indicates if statistics are active.
Interface of a timer manager.
Definition ITimerMng.h:50
ITimeStats * m_stats
Sub-domain manager.
Definition Timer.h:153
const String & name() const
Returns the name of the timer.
Definition Timer.h:237
eTimerType m_type
Timer type.
Definition Timer.h:271
Real m_activation_time
Time spent during the last activation.
Definition Timer.h:274
Integer m_nb_activated
Number of times the timer has been activated.
Definition Timer.h:272
String m_name
Timer name.
Definition Timer.h:276
ITimerMng * timerMng() const
Manager associated with this timer.
Definition Timer.h:255
Real stop()
Deactivates the timer.
Definition Timer.cc:87
eTimerType
Timer type.
Definition Timer.h:68
~Timer()
Frees resources.
Definition Timer.cc:62
ITimerMng * m_timer_mng
Timer manager.
Definition Timer.h:270
eTimerType type() const
Returns the type of time used.
Definition Timer.h:249
Timer(ISubDomain *sd, const String &name, eTimerType type)
Constructs a timer.
Definition Timer.cc:37
void reset()
Resets the time counters.
Definition Timer.cc:100
Real m_start_time
Time of the start of the last activation.
Definition Timer.h:277
bool m_is_activated
true if the timer is active
Definition Timer.h:273
void start()
Activates the timer.
Definition Timer.cc:70
Real m_total_time
Total time spent in the timer.
Definition Timer.h:275
Real getRealTime()
Real time used in seconds.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
double Real
Type representing a real number.
eTimePhase
Phase of a temporal action.