Main MRPT website > C++ reference for MRPT 1.9.9
CTimeLogger.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CTimeLogger_H
10 #define CTimeLogger_H
11 
12 #include <mrpt/utils/CTicTac.h>
15 #include <mrpt/utils/mrpt_macros.h>
16 #include <mrpt/utils/ts_hash_map.h>
17 #include <vector>
18 #include <stack>
19 //#include <map>
20 
21 namespace mrpt
22 {
23 namespace utils
24 {
25 /** A versatile "profiler" that logs the time spent within each pair of calls to
26  * enter(X)-leave(X), among other stats.
27  * The results can be dumped to cout or to Visual Studio's output panel.
28  * Recursive methods are supported with no problems, that is, calling "enter(X)
29  * enter(X) ... leave(X) leave(X)".
30  * `enter()`/`leave()` are thread-safe.
31  *
32  * This class can be also used to monitorize min/mean/max/total stats of any
33  * user-provided parameters via the method CTimeLogger::registerUserMeasure()
34  *
35  * Cost of the profiler itself (measured on MSVC2015, Windows 10, Intel i5-2310
36  * 2.9GHz):
37  * - `enter()`: average 445 ns
38  * - `leave()`: average 316 ns
39  *
40  * \sa CTimeLoggerEntry
41  *
42  * \note The default behavior is dumping all the information at destruction.
43  * \ingroup mrpt_base_grp
44  */
45 class CTimeLogger : public mrpt::utils::COutputLogger
46 {
47  private:
49  bool m_enabled;
51 
52  //! Data of all the calls:
53  struct TCallData
54  {
55  TCallData();
56 
57  size_t n_calls;
58  double min_t, max_t, mean_t, last_t;
59  std::stack<double, std::vector<double>> open_calls;
61  };
62 
63  protected:
64  typedef mrpt::utils::ts_hash_map<std::string, TCallData, 1 /* bytes hash */,
65  10 /* allowed hash collisions */>
66  TDataMap; // Was: std::map<std::string,TCallData>
68 
69  void do_enter(const char* func_name);
70  double do_leave(const char* func_name);
71 
72  public:
73  /** Data of each call section: # of calls, minimum, maximum, average and
74  * overall execution time (in seconds) \sa getStats */
75  struct TCallStats
76  {
77  size_t n_calls;
79  };
80 
82  bool enabled = true,
83  const std::string& name = ""); //! Default constructor
84  /** Destructor */
85  virtual ~CTimeLogger();
86 
87  // We must define these 4 because of the definition of a virtual dtor
88  // (compiler will not generate the defaults)
89  CTimeLogger(const CTimeLogger& o);
93 
94  /** Dump all stats to a multi-line text string. \sa dumpAllStats,
95  * saveToCVSFile */
96  std::string getStatsAsText(const size_t column_width = 80) const;
97  /** Returns all the current stats as a map: section_name => stats. \sa
98  * getStatsAsText, dumpAllStats, saveToCVSFile */
99  void getStats(std::map<std::string, TCallStats>& out_stats) const;
100  /** Dump all stats through the COutputLogger interface. \sa getStatsAsText,
101  * saveToCVSFile */
102  void dumpAllStats(const size_t column_width = 80) const;
103  /** Resets all stats. By default (deep_clear=false), all section names are
104  * remembered (not freed) so the cost of creating upon the first next call
105  * is avoided. */
106  void clear(bool deep_clear = false);
107  void enable(bool enabled = true) { m_enabled = enabled; }
108  void disable() { m_enabled = false; }
109  bool isEnabled() const { return m_enabled; }
110  /** Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
111  */
112  void saveToCSVFile(const std::string& csv_file) const;
113  void registerUserMeasure(const char* event_name, const double value);
114 
115  void setName(const std::string& name) { m_name = name; }
116  /** Start of a named section \sa enter */
117  inline void enter(const char* func_name)
118  {
119  if (m_enabled) do_enter(func_name);
120  }
121  /** End of a named section \return The ellapsed time, in seconds or 0 if
122  * disabled. \sa enter */
123  inline double leave(const char* func_name)
124  {
125  return m_enabled ? do_leave(func_name) : 0;
126  }
127  /** Return the mean execution time of the given "section", or 0 if it hasn't
128  * ever been called "enter" with that section name */
129  double getMeanTime(const std::string& name) const;
130  /** Return the last execution time of the given "section", or 0 if it hasn't
131  * ever been called "enter" with that section name */
132  double getLastTime(const std::string& name) const;
133 }; // End of class def.
134 
135 /** A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon
136  * construction and destruction of
137  * this auxiliary object, making sure that leave() will be called upon
138  * exceptions, etc.
139  * Usage:
140  * \code
141  * CTimeLogger logger;
142  * // ...
143  * { // Start of scope to be monitorized
144  * CTimeLoggerEntry tle(logger,"operation-name");
145  *
146  * // do whatever
147  *
148  * } // End of scope
149  * \endcode
150  * \ingroup mrpt_base_grp
151  */
153 {
154  CTimeLoggerEntry(const CTimeLogger& logger, const char* section_name);
157  const char* m_section_name;
158 };
159 
160 /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END
161  macros.
162  @{ */
163 void global_profiler_enter(const char* func_name) noexcept;
164 void global_profiler_leave(const char* func_name) noexcept;
166 /** @} */
167 
168 } // End of namespace
169 } // End of namespace
170 #endif
ts_hash_map()
< Default constructor */
Definition: ts_hash_map.h:187
void getStats(std::map< std::string, TCallStats > &out_stats) const
Returns all the current stats as a map: section_name => stats.
std::stack< double, std::vector< double > > open_calls
Definition: CTimeLogger.h:59
void global_profiler_leave(const char *func_name) noexcept
Definition: CTimeLogger.cpp:48
void enable(bool enabled=true)
Definition: CTimeLogger.h:107
void clear(bool deep_clear=false)
Resets all stats.
mrpt::utils::ts_hash_map< std::string, TCallData, 1, 10 > TDataMap
Definition: CTimeLogger.h:66
CTimeLogger & operator=(const CTimeLogger &o)
Definition: CTimeLogger.cpp:76
Data of all the calls:
Definition: CTimeLogger.h:53
CTimeLogger(bool enabled=true, const std::string &name="")
Definition: CTimeLogger.cpp:55
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
This class implements a high-performance stopwatch.
Definition: CTicTac.h:23
void saveToCSVFile(const std::string &csv_file) const
Dump all stats to a Comma Separated Values (CSV) file.
double do_leave(const char *func_name)
void do_enter(const char *func_name)
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
Definition: CTimeLogger.h:75
double getLastTime(const std::string &name) const
Return the last execution time of the given "section", or 0 if it hasn&#39;t ever been called "enter" wit...
virtual ~CTimeLogger()
Default constructor.
Definition: CTimeLogger.cpp:62
GLsizei const GLchar ** string
Definition: glext.h:4101
CTimeLoggerEntry(const CTimeLogger &logger, const char *section_name)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void global_profiler_enter(const char *func_name) noexcept
Definition: CTimeLogger.cpp:44
~CTimeLoggerEntry()
A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destructio...
Definition: CTimeLogger.h:152
double getMeanTime(const std::string &name) const
Return the mean execution time of the given "section", or 0 if it hasn&#39;t ever been called "enter" wit...
GLuint const GLchar * name
Definition: glext.h:4054
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
Definition: CTimeLogger.h:45
double leave(const char *func_name)
End of a named section.
Definition: CTimeLogger.h:123
const char * m_section_name
Definition: CTimeLogger.h:157
void setName(const std::string &name)
Definition: CTimeLogger.h:115
void registerUserMeasure(const char *event_name, const double value)
CTimeLogger & m_logger
Definition: CTimeLogger.h:156
GLsizei const GLfloat * value
Definition: glext.h:4117
void enter(const char *func_name)
Start of a named section.
Definition: CTimeLogger.h:117
void dumpAllStats(const size_t column_width=80) const
Dump all stats through the COutputLogger interface.
mrpt::utils::CTimeLogger & global_profiler_getref() noexcept
Definition: CTimeLogger.cpp:43



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019