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