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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019