Main MRPT website > C++ reference for MRPT 1.5.7
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 enter(X)-leave(X), among other stats.
26  * The results can be dumped to cout or to Visual Studio's output panel.
27  * Recursive methods are supported with no problems, that is, calling "enter(X) 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 user-provided parameters via the method CTimeLogger::registerUserMeasure()
31  *
32  * Cost of the profiler itself (measured on MSVC2015, Windows 10, Intel i5-2310 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_base_grp
40  */
41  class BASE_IMPEXP CTimeLogger : public mrpt::utils::COutputLogger
42  {
43  private:
45  bool m_enabled;
47 
48  //! Data of all the calls:
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  protected:
59  typedef mrpt::utils::ts_hash_map<std::string, TCallData, 1 /* bytes hash */, 10 /* allowed hash collisions */> TDataMap; // Was: std::map<std::string,TCallData>
61 
62  void do_enter( const char *func_name );
63  double do_leave( const char *func_name );
64 
65  public:
66  /** Data of each call section: # of calls, minimum, maximum, average and overall execution time (in seconds) \sa getStats */
68  {
69  size_t n_calls;
70  double min_t,max_t,mean_t,total_t,last_t;
71  };
72 
73  CTimeLogger(bool enabled=true, const std::string& name=""); //! Default constructor
74  virtual ~CTimeLogger(); //!< Destructor
75 
76  // We must define these 4 because of the definition of a virtual dtor (compiler will not generate the defaults)
77  CTimeLogger(const CTimeLogger&o);
78  CTimeLogger &operator =(const CTimeLogger&o);
79 #if MRPT_HAS_CXX11
81  CTimeLogger &operator =(CTimeLogger&&o);
82 #endif
83 
84  std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
85  void getStats(std::map<std::string,TCallStats> &out_stats) const; //!< Returns all the current stats as a map: section_name => stats. \sa getStatsAsText, dumpAllStats, saveToCVSFile
86  void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the COutputLogger interface. \sa getStatsAsText, saveToCVSFile
87  void clear(bool deep_clear=false); //!< Resets all stats. By default (deep_clear=false), all section names are remembered (not freed) so the cost of creating upon the first next call is avoided.
88  void enable(bool enabled = true) { m_enabled = enabled; }
89  void disable() { m_enabled = false; }
90  bool isEnabled() const { return m_enabled;}
91  void saveToCSVFile(const std::string &csv_file) const; //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
92  void registerUserMeasure(const char *event_name, const double value);
93 
94  void setName(const std::string& name) { m_name = name; }
95 
96  /** Start of a named section \sa enter */
97  inline void enter( const char *func_name ) {
98  if (m_enabled)
99  do_enter(func_name);
100  }
101  /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
102  inline double leave( const char *func_name ) {
103  return m_enabled ? do_leave(func_name) : 0;
104  }
105  /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
106  double getMeanTime(const std::string &name) const;
107  /** Return the last execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
108  double getLastTime(const std::string &name) const;
109  }; // End of class def.
110 
111 
112  /** A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destruction of
113  * this auxiliary object, making sure that leave() will be called upon exceptions, etc.
114  * Usage:
115  * \code
116  * CTimeLogger logger;
117  * // ...
118  * { // Start of scope to be monitorized
119  * CTimeLoggerEntry tle(logger,"operation-name");
120  *
121  * // do whatever
122  *
123  * } // End of scope
124  * \endcode
125  * \ingroup mrpt_base_grp
126  */
128  {
129  CTimeLoggerEntry(const CTimeLogger &logger, const char*section_name );
130  ~CTimeLoggerEntry();
132  const char *m_section_name;
133  };
134 
135 
136  /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
137  @{ */
138  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
139  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
141  /** @} */
142 
143  } // End of namespace
144 } // End of namespace
145 #endif
ts_hash_map()
< Default constructor */
Definition: ts_hash_map.h:109
#define MRPT_NO_THROWS
C++11 noexcept: Used after member declarations.
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
void enable(bool enabled=true)
Definition: CTimeLogger.h:88
mrpt::utils::CTimeLogger BASE_IMPEXP & global_profiler_getref() MRPT_NO_THROWS
Definition: CTimeLogger.cpp:44
mrpt::utils::ts_hash_map< std::string, TCallData, 1, 10 > TDataMap
Definition: CTimeLogger.h:59
Data of all the calls:
Definition: CTimeLogger.h:49
This class implements a high-performance stopwatch.
Definition: CTicTac.h:24
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
Definition: CTimeLogger.h:67
bool isEnabled() const
Definition: CTimeLogger.h:90
GLsizei const GLchar ** string
Definition: glext.h:3919
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::stack< double, std::vector< double > > open_calls
Definition: CTimeLogger.h:55
A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destructio...
Definition: CTimeLogger.h:127
void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS
Definition: CTimeLogger.cpp:48
GLuint const GLchar * name
Definition: glext.h:3891
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
Definition: CTimeLogger.h:41
double leave(const char *func_name)
End of a named section.
Definition: CTimeLogger.h:102
const char * m_section_name
Definition: CTimeLogger.h:132
void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS
Definition: CTimeLogger.cpp:51
void setName(const std::string &name)
Definition: CTimeLogger.h:94
CTimeLogger & m_logger
Definition: CTimeLogger.h:131
GLsizei const GLfloat * value
Definition: glext.h:3929
void enter(const char *func_name)
Start of a named section.
Definition: CTimeLogger.h:97



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019