Main MRPT website > C++ reference
MRPT logo
CTimeLogger.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7  | Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8  | Copyright (c) 2012-2013, University of Almeria |
9  | All rights reserved. |
10  | |
11  | Redistribution and use in source and binary forms, with or without |
12  | modification, are permitted provided that the following conditions are |
13  | met: |
14  | * Redistributions of source code must retain the above copyright |
15  | notice, this list of conditions and the following disclaimer. |
16  | * Redistributions in binary form must reproduce the above copyright |
17  | notice, this list of conditions and the following disclaimer in the |
18  | documentation and/or other materials provided with the distribution. |
19  | * Neither the name of the copyright holders nor the |
20  | names of its contributors may be used to endorse or promote products |
21  | derived from this software without specific prior written permission.|
22  | |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24  | 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25  | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26  | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29  | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31  | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32  | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33  | POSSIBILITY OF SUCH DAMAGE. |
34  +---------------------------------------------------------------------------+ */
35 #ifndef CTimeLogger_H
36 #define CTimeLogger_H
37 
38 #include <mrpt/utils/CTicTac.h>
40 
41 #include <stack>
42 
43 namespace mrpt
44 {
45  namespace utils
46  {
47  using namespace std;
48 
49  /** A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
50  * The results can be dumped to cout or to Visual Studio's output panel.
51  * Recursive methods are supported with no problems, that is, calling "enter(X) enter(X) ... leave(X) leave(X)".
52  *
53  * This class can be also used to monitorize min/mean/max/total stats of any user-provided parameters via the method CTimeLogger::registerUserMeasure()
54  *
55  * \sa CTimeLoggerEntry
56  *
57  * \note The default behavior is dumping all the information at destruction.
58  * \ingroup mrpt_base_grp
59  */
61  {
62  private:
64  bool m_enabled;
65 
66  //! Data of all the calls:
67  struct TCallData
68  {
69  TCallData();
70 
71  size_t n_calls;
72  double min_t,max_t,mean_t;
73  stack<double,vector<double> > open_calls;
75  };
76 
77  map<string,TCallData> m_data;
78 
79  void do_enter( const char *func_name );
80  double do_leave( const char *func_name );
81 
82  public:
83  /** Data of each call section: # of calls, minimum, maximum, average and overall execution time (in seconds) \sa getStats */
84  struct TCallStats
85  {
86  size_t n_calls;
87  double min_t,max_t,mean_t,total_t;
88  };
89 
90  CTimeLogger(bool enabled = true); //! Default constructor
91  virtual ~CTimeLogger(); //!< Destructor
92  std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
93  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
94  void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the CDebugOutputCapable interface. \sa getStatsAsText, saveToCVSFile
95  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.
96  void enable(bool enabled = true) { m_enabled = enabled; }
97  void disable() { m_enabled = false; }
98  void saveToCSVFile(const std::string &csv_file) const; //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
99  void registerUserMeasure(const char *event_name, const double value);
100 
101  /** Start of a named section \sa enter */
102  inline void enter( const char *func_name ) {
103  if (m_enabled)
104  do_enter(func_name);
105  }
106  /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
107  inline double leave( const char *func_name ) {
108  return m_enabled ? do_leave(func_name) : 0;
109  }
110  /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
111  double getMeanTime(const std::string &name) const;
112  }; // End of class def.
113 
114 
115  /** A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destruction of
116  * this auxiliary object, making sure that leave() will be called upon exceptions, etc.
117  * Usage:
118  * \code
119  * CTimeLogger logger;
120  * // ...
121  * { // Start of scope to be monitorized
122  * CTimeLoggerEntry tle(logger,"operation-name");
123  *
124  * // do whatever
125  *
126  * } // End of scope
127  * \endcode
128  * \ingroup mrpt_base_grp
129  */
131  {
132  CTimeLoggerEntry(CTimeLogger &logger, const char*section_name );
133  ~CTimeLoggerEntry();
135  const char *m_section_name;
136  };
137 
138 
139  /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
140  @{ */
142  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
143  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
144  /** @} */
145 
146  } // End of namespace
147 } // End of namespace
148 #endif
#define MRPT_NO_THROWS
Used after member declarations.
STL namespace.
void enable(bool enabled=true)
Definition: CTimeLogger.h:96
stack< double, vector< double > > open_calls
Definition: CTimeLogger.h:73
Data of all the calls:
Definition: CTimeLogger.h:67
This class implements a high-performance stopwatch.
Definition: CTicTac.h:49
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
Definition: CTimeLogger.h:84
map< string, TCallData > m_data
Definition: CTimeLogger.h:77
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destructio...
Definition: CTimeLogger.h:130
CTimeLogger BASE_IMPEXP global_profiler
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
Definition: CTimeLogger.h:60
double leave(const char *func_name)
End of a named section.
Definition: CTimeLogger.h:107
const char * m_section_name
Definition: CTimeLogger.h:135
void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS
CTimeLogger & m_logger
Definition: CTimeLogger.h:134
void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS
void enter(const char *func_name)
Start of a named section.
Definition: CTimeLogger.h:102
This base class provides a common printf-like method to send debug information to std::cout...



Page generated by Doxygen 1.8.14 for MRPT 1.0.2 SVN: at lun oct 28 00:52:41 CET 2019 Hosted on:
SourceForge.net Logo