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
mrpt::system::CTimeLogger::TCallStats::mean_t
double mean_t
Definition: system/CTimeLogger.h:77
mrpt::system::CTimeLogger::TCallStats
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
Definition: system/CTimeLogger.h:74
exceptions.h
mrpt::system::global_profiler_getref
mrpt::system::CTimeLogger & global_profiler_getref() noexcept
Definition: CTimeLogger.cpp:42
mrpt::system::CTimeLogger::TCallData::TCallData
TCallData()
Definition: CTimeLogger.cpp:274
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::system::CTimeLogger::TCallData::mean_t
double mean_t
Definition: system/CTimeLogger.h:56
mrpt::system::CTimeLogger
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X),...
Definition: system/CTimeLogger.h:43
mrpt::containers::ts_hash_map
ts_hash_map()
< Default constructor *‍/
Definition: ts_hash_map.h:186
mrpt::system::CTimeLogger::TCallStats::max_t
double max_t
Definition: system/CTimeLogger.h:77
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::system::CTimeLogger::TCallData::last_t
double last_t
Definition: system/CTimeLogger.h:56
mrpt::system::CTimeLogger::disable
void disable()
Definition: system/CTimeLogger.h:107
mrpt::system::CTimeLogger::registerUserMeasure
void registerUserMeasure(const char *event_name, const double value)
Definition: CTimeLogger.cpp:252
mrpt::system::CTimeLogger::enter
void enter(const char *func_name)
Start of a named section.
Definition: system/CTimeLogger.h:116
mrpt::system::CTimeLogger::do_leave
double do_leave(const char *func_name)
Definition: CTimeLogger.cpp:222
mrpt::system::CTimeLogger::dumpAllStats
void dumpAllStats(const size_t column_width=80) const
Dump all stats through the COutputLogger interface.
Definition: CTimeLogger.cpp:207
mrpt::system::CTimeLogger::clear
void clear(bool deep_clear=false)
Resets all stats.
Definition: CTimeLogger.cpp:99
mrpt::system::CTimeLogger::m_enabled
bool m_enabled
Definition: system/CTimeLogger.h:47
mrpt::system::CTimeLogger::leave
double leave(const char *func_name)
End of a named section.
Definition: system/CTimeLogger.h:122
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::system::CTimeLogger::~CTimeLogger
virtual ~CTimeLogger()
Default constructor.
Definition: CTimeLogger.cpp:61
ts_hash_map.h
mrpt::system::CTimeLogger::TCallStats::last_t
double last_t
Definition: system/CTimeLogger.h:77
mrpt::system::CTimeLogger::m_data
TDataMap m_data
Definition: system/CTimeLogger.h:66
mrpt::system::CTimeLogger::TCallData::max_t
double max_t
Definition: system/CTimeLogger.h:56
COutputLogger.h
mrpt::system::CTimeLogger::do_enter
void do_enter(const char *func_name)
Definition: CTimeLogger.cpp:212
mrpt::system::CTimeLoggerEntry::m_logger
CTimeLogger & m_logger
Definition: system/CTimeLogger.h:155
mrpt::system::CTimeLogger::TCallStats::total_t
double total_t
Definition: system/CTimeLogger.h:77
mrpt::system::CTimeLogger::TDataMap
mrpt::containers::ts_hash_map< std::string, TCallData, 1, 10 > TDataMap
Definition: system/CTimeLogger.h:65
mrpt::system::CTimeLogger::TCallData::min_t
double min_t
Definition: system/CTimeLogger.h:56
mrpt::system::CTimeLogger::TCallData::open_calls
std::stack< double, std::vector< double > > open_calls
Definition: system/CTimeLogger.h:57
mrpt::system::CTimeLoggerEntry::CTimeLoggerEntry
CTimeLoggerEntry(const CTimeLogger &logger, const char *section_name)
Definition: CTimeLogger.cpp:296
mrpt::system::CTimeLogger::isEnabled
bool isEnabled() const
Definition: system/CTimeLogger.h:108
mrpt::system::CTimeLogger::TCallData::has_time_units
bool has_time_units
Definition: system/CTimeLogger.h:58
mrpt::system::global_profiler_leave
void global_profiler_leave(const char *func_name) noexcept
Definition: CTimeLogger.cpp:47
mrpt::system::CTimeLogger::m_tictac
CTicTac m_tictac
Definition: system/CTimeLogger.h:46
mrpt::system::CTimeLogger::setName
void setName(const std::string &name)
Definition: system/CTimeLogger.h:114
mrpt::system::COutputLogger
Versatile class for consistent logging and management of output messages.
Definition: system/COutputLogger.h:117
mrpt::system::CTimeLoggerEntry
A safe way to call enter() and leave() of a mrpt::system::CTimeLogger upon construction and destructi...
Definition: system/CTimeLogger.h:151
mrpt::system::CTimeLogger::TCallData::n_calls
size_t n_calls
Definition: system/CTimeLogger.h:55
mrpt::system::CTimeLogger::getLastTime
double getLastTime(const std::string &name) const
Return the last execution time of the given "section", or 0 if it hasn't ever been called "enter" wit...
Definition: CTimeLogger.cpp:287
mrpt::system::CTimeLogger::CTimeLogger
CTimeLogger(bool enabled=true, const std::string &name="")
Definition: CTimeLogger.cpp:54
mrpt::system::CTimeLogger::getStats
void getStats(std::map< std::string, TCallStats > &out_stats) const
Returns all the current stats as a map: section_name => stats.
Definition: CTimeLogger.cpp:121
mrpt::system::CTimeLogger::enable
void enable(bool enabled=true)
Definition: system/CTimeLogger.h:106
mrpt::system::CTimeLogger::saveToCSVFile
void saveToCSVFile(const std::string &csv_file) const
Dump all stats to a Comma Separated Values (CSV) file.
Definition: CTimeLogger.cpp:191
CTicTac.h
value
GLsizei const GLfloat * value
Definition: glext.h:4117
mrpt::system::CTimeLogger::TCallStats::n_calls
size_t n_calls
Definition: system/CTimeLogger.h:76
mrpt::system::CTimeLogger::TCallStats::min_t
double min_t
Definition: system/CTimeLogger.h:77
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::CTimeLoggerEntry::~CTimeLoggerEntry
~CTimeLoggerEntry()
Definition: CTimeLogger.cpp:302
mrpt::system::CTimeLogger::m_name
std::string m_name
Definition: system/CTimeLogger.h:48
mrpt::system::CTimeLogger::TCallData
Data of all the calls:
Definition: system/CTimeLogger.h:51
mrpt::system::CTimeLoggerEntry::m_section_name
const char * m_section_name
Definition: system/CTimeLogger.h:156
mrpt::system::CTimeLogger::getStatsAsText
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
Definition: CTimeLogger.cpp:136
mrpt::system::CTimeLogger::operator=
CTimeLogger & operator=(const CTimeLogger &o)
Definition: CTimeLogger.cpp:75
mrpt::system::CTimeLogger::getMeanTime
double getMeanTime(const std::string &name) const
Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" wit...
Definition: CTimeLogger.cpp:279
mrpt::system::global_profiler_enter
void global_profiler_enter(const char *func_name) noexcept
Definition: CTimeLogger.cpp:43



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at mié 12 jul 2023 10:03:34 CEST