fw4spl
Profiling.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2004-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWCORE_PROFILING_HPP__
8 #define __FWCORE_PROFILING_HPP__
9 
10 #include "fwCore/HiResTimer.hpp"
11 #include "fwCore/spyLog.hpp"
12 
13 // Define FW_PROFILING_DISABLED before including this header if you need to disable profiling output
14 
15 namespace fwCore
16 {
17 
22 {
23 public:
24  fwProfileScope(const char* label) :
25  m_label(label)
26  {
27  m_timer.start();
28  }
29 
31  {
32  m_timer.stop();
33 
34  std::stringstream log;
35  log << "TIMER : " << m_label << " = " << m_timer.getElapsedTimeInMilliSec() << " ms.";
36  ::fwCore::log::SpyLogger::getSpyLogger().info(log.str(), __FILE__, __LINE__);
37  }
38 
42  const char* m_label;
43 };
44 
49 {
50 public:
51  fwProfileFrameTimer(double interval) :
52  m_interval(interval),
53  m_average(0),
54  m_count(0)
55  {
56  m_timer.start();
57  }
58 
59  bool tick()
60  {
61  m_average = m_timer.getElapsedTimeInMilliSec() / ++m_count;
62  return m_timer.getElapsedTimeInMilliSec() >= m_interval * 1000;
63  }
64 
65  bool tick(::fwCore::HiResClock::HiResClockType time)
66  {
67  m_average = (m_average * m_count + time) / (m_count + 1);
68  ++m_count;
69  return m_timer.getElapsedTimeInMilliSec() >= m_interval * 1000;
70  }
71 
72  void reset()
73  {
74  m_count = 0;
75  m_average = 0;
76  m_timer.reset();
77  }
78 
82  double m_interval;
84  double m_average;
86  unsigned int m_count;
87 };
88 
93 {
94 public:
95  fwProfileScopeAvg(const char* label, fwProfileFrameTimer& frameTimer) :
96  m_label(label),
97  m_frameTimer(frameTimer)
98  {
99  m_timer.start();
100  }
101 
103  {
104  m_timer.stop();
105 
106  if( m_frameTimer.tick(m_timer.getElapsedTimeInMilliSec()) )
107  {
108  std::stringstream log;
109  log << "TIMER (avg over " << m_frameTimer.m_interval << "s) : " << m_label <<
110  " = " << m_frameTimer.m_average << " ms.";
111  ::fwCore::log::SpyLogger::getSpyLogger().info(log.str(), __FILE__, __LINE__);
112  m_frameTimer.reset();
113  }
114  }
115 
119  const char* m_label;
122 };
123 
128 {
129 public:
130  fwProfileFrame(const char* label, fwProfileFrameTimer& frameTimer) :
131  m_label(label),
132  m_frameTimer(frameTimer)
133  {
134  std::stringstream log;
135  log << "FRAME : " << m_label << " = " << m_frameTimer.m_timer.getElapsedTimeInMilliSec() << " ms.";
136  ::fwCore::log::SpyLogger::getSpyLogger().info(log.str(), __FILE__, __LINE__);
137  m_frameTimer.reset();
138  }
139 
140  ~fwProfileFrame()
141  {
142  }
143 
145  const char* m_label;
148 };
149 
154 {
155 public:
156  fwProfileFrameAvg(const char* label, fwProfileFrameTimer& frameTimer) :
157  m_label(label),
158  m_frameTimer(frameTimer)
159  {
160  if( m_frameTimer.tick() )
161  {
162  std::stringstream log;
163  log << "FRAME (avg over " << m_frameTimer.m_interval << "s) : " << m_label <<
164  " = " << m_frameTimer.m_average << " ms.";
165  ::fwCore::log::SpyLogger::getSpyLogger().info(log.str(), __FILE__, __LINE__);
166  m_frameTimer.reset();
167  }
168  }
169 
171  {
172  }
173 
175  const char* m_label;
178 };
179 
180 #ifndef FW_PROFILING_DISABLED
181 
183 #define FW_PROFILE(_label) \
184  ::fwCore::fwProfileScope BOOST_PP_CAT( profiler, __LINE__ ) (_label);
185 
187 #define FW_PROFILE_AVG(_label, interval) \
188  static ::fwCore::fwProfileFrameTimer BOOST_PP_CAT( frameTimer, __LINE__ ) (interval); \
189  ::fwCore::fwProfileScopeAvg BOOST_PP_CAT( profiler, __LINE__ ) (_label, BOOST_PP_CAT( frameTimer, __LINE__ ));
190 
192 #define FW_PROFILE_FRAME(_label) \
193  static ::fwCore::fwProfileFrameTimer BOOST_PP_CAT( frameTimer, __LINE__ ) (0); \
194  ::fwCore::fwProfileFrame BOOST_PP_CAT( profiler, __LINE__ ) (_label, BOOST_PP_CAT( frameTimer, __LINE__ ));
195 
197 #define FW_PROFILE_FRAME_AVG(_label, interval) \
198  static ::fwCore::fwProfileFrameTimer BOOST_PP_CAT( frameTimer, __LINE__ ) (interval); \
199  ::fwCore::fwProfileFrameAvg BOOST_PP_CAT( profiler, __LINE__ ) (_label, BOOST_PP_CAT( frameTimer, __LINE__ ));
200 
201 #else // FW_PROFILING_DISABLED
202 
203 #define FW_PROFILE(_label)
204 #define FW_PROFILE_AVG(_label, interval)
205 #define FW_PROFILE_FRAME(_label)
206 #define FW_PROFILE_FRAME_AVG(_label, interval)
207 
208 #endif // FW_PROFILING_DISABLED
209 
210 } //namespace fwCore
211 
212 
213 #endif // __FWCORE_PROFILING_HPP__
This class provide a timer (stopwatch). HiResTimer is able to measure the elapsed time with a few mic...
Definition: HiResTimer.hpp:28
fwProfileFrameTimer & m_frameTimer
Timer used to get the elapsed time between two profiling scopes.
Definition: Profiling.hpp:121
double m_interval
Timer label.
Definition: Profiling.hpp:82
const char * m_label
Timer label.
Definition: Profiling.hpp:42
FWCORE_API void stop()
Stop the timer. stop() will not reset the timer.
Definition: HiResTimer.cpp:32
fwProfileFrameTimer & m_frameTimer
Timer used to get the elapsed time between two profiling scopes.
Definition: Profiling.hpp:177
This class holds a timer. It displays elapsed time at destruction.
Definition: Profiling.hpp:21
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16
fwProfileFrameTimer & m_frameTimer
Timer used to get the elapsed time between two profiling scopes.
Definition: Profiling.hpp:147
double HiResClockType
Type returned by HiResClock Functions.
Definition: HiResClock.hpp:29
FWCORE_API::fwCore::HiResClock::HiResClockType getElapsedTimeInMilliSec()
Definition: HiResTimer.cpp:66
const char * m_label
Timer label.
Definition: Profiling.hpp:175
This class is used to compute the elapsed time between two profiling scopes every N seconds...
Definition: Profiling.hpp:153
This class is used to compute the elapsed time between two profiling scopes.
Definition: Profiling.hpp:127
This class holds a timer. It displays elapsed time at destruction every N seconds.
Definition: Profiling.hpp:92
::fwCore::HiResTimer m_timer
Timer.
Definition: Profiling.hpp:80
::fwCore::HiResTimer m_timer
Timer.
Definition: Profiling.hpp:117
const char * m_label
Timer label.
Definition: Profiling.hpp:145
const char * m_label
Timer label.
Definition: Profiling.hpp:119
FWCORE_API void reset(::fwCore::HiResClock::HiResClockType initial_value=0.)
Reset the timer and leave it in the same state it was (started or stopped).
Definition: HiResTimer.cpp:39
FWCORE_API void start()
Start the timer.
Definition: HiResTimer.cpp:24
unsigned int m_count
Number of calls, used to compute the average.
Definition: Profiling.hpp:86
This class holds a timer. It allows to compute the elapsed time between two profiling scopes...
Definition: Profiling.hpp:48
::fwCore::HiResTimer m_timer
Timer.
Definition: Profiling.hpp:40
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
double m_average
Actual elapsed time average.
Definition: Profiling.hpp:84