Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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 <vector>
17 #include <stack>
18 #include <map>
19 
20 namespace mrpt
21 {
22  namespace utils
23  {
24  /** A versatile "profiler" that logs the time spent within each pair of calls to 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) enter(X) ... leave(X) leave(X)".
27  *
28  * This class can be also used to monitorize min/mean/max/total stats of any user-provided parameters via the method CTimeLogger::registerUserMeasure()
29  *
30  * \sa CTimeLoggerEntry
31  *
32  * \note The default behavior is dumping all the information at destruction.
33  * \ingroup mrpt_base_grp
34  */
36  {
37  private:
39  bool m_enabled;
40 
41  //! Data of all the calls:
43  {
44  TCallData();
45 
46  size_t n_calls;
47  double min_t,max_t,mean_t;
48  std::stack<double,std::vector<double> > open_calls;
50  };
51 
52  std::map<std::string,TCallData> m_data;
53 
54  void do_enter( const char *func_name );
55  double do_leave( const char *func_name );
56 
57  public:
58  /** Data of each call section: # of calls, minimum, maximum, average and overall execution time (in seconds) \sa getStats */
60  {
61  size_t n_calls;
62  double min_t,max_t,mean_t,total_t;
63  };
64 
65  CTimeLogger(bool enabled = true); //! Default constructor
66  virtual ~CTimeLogger(); //!< Destructor
67  std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
68  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
69  void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the CDebugOutputCapable interface. \sa getStatsAsText, saveToCVSFile
70  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.
71  void enable(bool enabled = true) { m_enabled = enabled; }
72  void disable() { m_enabled = false; }
73  void saveToCSVFile(const std::string &csv_file) const; //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
74  void registerUserMeasure(const char *event_name, const double value);
75 
76  /** Start of a named section \sa enter */
77  inline void enter( const char *func_name ) {
78  if (m_enabled)
79  do_enter(func_name);
80  }
81  /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
82  inline double leave( const char *func_name ) {
83  return m_enabled ? do_leave(func_name) : 0;
84  }
85  /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
86  double getMeanTime(const std::string &name) const;
87  }; // End of class def.
88 
89 
90  /** A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destruction of
91  * this auxiliary object, making sure that leave() will be called upon exceptions, etc.
92  * Usage:
93  * \code
94  * CTimeLogger logger;
95  * // ...
96  * { // Start of scope to be monitorized
97  * CTimeLoggerEntry tle(logger,"operation-name");
98  *
99  * // do whatever
100  *
101  * } // End of scope
102  * \endcode
103  * \ingroup mrpt_base_grp
104  */
106  {
107  CTimeLoggerEntry(CTimeLogger &logger, const char*section_name );
108  ~CTimeLoggerEntry();
110  const char *m_section_name;
111  };
112 
113 
114  /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
115  @{ */
117  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
118  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
119  /** @} */
120 
121  } // End of namespace
122 } // End of namespace
123 #endif
mrpt::utils::CTimeLogger::TCallData
Data of all the calls:
Definition: CTimeLogger.h:42
mrpt::utils::global_profiler_enter
void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS
mrpt::utils::CTimeLogger::m_tictac
CTicTac m_tictac
Definition: CTimeLogger.h:38
mrpt::utils::CDebugOutputCapable
This base class provides a common printf-like method to send debug information to std::cout,...
Definition: CDebugOutputCapable.h:31
CDebugOutputCapable.h
mrpt::utils::CTimeLogger::TCallStats
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
Definition: CTimeLogger.h:59
mrpt::utils::CTimeLogger::leave
double leave(const char *func_name)
End of a named section.
Definition: CTimeLogger.h:82
mrpt::utils::CTimeLogger::m_data
std::map< std::string, TCallData > m_data
Definition: CTimeLogger.h:52
mrpt::utils::CTimeLogger::enter
void enter(const char *func_name)
Start of a named section.
Definition: CTimeLogger.h:77
MRPT_NO_THROWS
#define MRPT_NO_THROWS
Used after member declarations.
Definition: mrpt_macros.h:391
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:16
mrpt::utils::CTimeLogger
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X),...
Definition: CTimeLogger.h:35
mrpt::utils::CTimeLogger::TCallData::has_time_units
bool has_time_units
Definition: CTimeLogger.h:49
mrpt::utils::CTicTac
This class implements a high-performance stopwatch.
Definition: CTicTac.h:24
mrpt::utils::CTimeLoggerEntry::m_logger
CTimeLogger & m_logger
Definition: CTimeLogger.h:109
mrpt::utils::CTimeLoggerEntry::m_section_name
const char * m_section_name
Definition: CTimeLogger.h:110
mrpt_macros.h
compiler_fixes.h
mrpt::utils::CTimeLogger::disable
void disable()
Definition: CTimeLogger.h:72
mrpt::utils::global_profiler
CTimeLogger BASE_IMPEXP global_profiler
mrpt::utils::CTimeLogger::enable
void enable(bool enabled=true)
Definition: CTimeLogger.h:71
mrpt::utils::CTimeLoggerEntry
A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destructio...
Definition: CTimeLogger.h:105
CTicTac.h
mrpt::utils::global_profiler_leave
void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS
mrpt::utils::CTimeLogger::m_enabled
bool m_enabled
Definition: CTimeLogger.h:39
mrpt::utils::CTimeLogger::TCallData::open_calls
std::stack< double, std::vector< double > > open_calls
Definition: CTimeLogger.h:48
mrpt::utils::CTimeLogger::TCallStats::n_calls
size_t n_calls
Definition: CTimeLogger.h:61
mrpt::utils::CTimeLogger::TCallStats::total_t
double total_t
Definition: CTimeLogger.h:62
mrpt::utils::CTimeLogger::TCallData::min_t
double min_t
Definition: CTimeLogger.h:47
mrpt::utils::CTimeLogger::TCallData::n_calls
size_t n_calls
Definition: CTimeLogger.h:46



Page generated by Doxygen 1.8.16 for MRPT 1.4.0 SVN: at Mon Oct 14 23:08:25 UTC 2019