MRPT  1.9.9
CTimeLogger.cpp
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 
10 #include "system-precomp.h" // Precompiled headers
11 
14 #include <mrpt/core/bits_math.h>
15 #include <iostream>
16 #include <fstream>
17 
18 using namespace mrpt;
19 using namespace mrpt::system;
20 using namespace std;
21 
23 {
24  MyGlobalProfiler() : mrpt::system::CTimeLogger("MRPT_global_profiler") {}
26  {
27  if (!m_data.empty())
28  {
29  const std::string sFil("mrpt-global-profiler.csv");
30  this->saveToCSVFile(sFil);
31  std::cout << "[MRPT global profiler] Write stats to: " << sFil
32  << std::endl;
33  }
34  }
35 };
37 
38 namespace mrpt::system
39 {
41 void global_profiler_enter(const char* func_name) noexcept
42 {
43  global_profiler.enter(func_name);
44 }
45 void global_profiler_leave(const char* func_name) noexcept
46 {
47  global_profiler.leave(func_name);
48 }
49 }
50 
52  bool enabled /*=true*/, const std::string& name /*=""*/)
53  : COutputLogger("CTimeLogger"), m_tictac(), m_enabled(enabled), m_name(name)
54 {
55  m_tictac.Tic();
56 }
57 
59 {
60  // Dump all stats:
61  if (!m_data.empty()) // If logging is disabled, do nothing...
62  dumpAllStats();
63 }
64 
66  : COutputLogger(o),
67  m_enabled(o.m_enabled),
68  m_name(o.m_name),
69  m_data(o.m_data)
70 {
71 }
73 {
74  COutputLogger::operator=(o);
75  m_enabled = o.m_enabled;
76  m_name = o.m_name;
77  m_data = o.m_data;
78  return *this;
79 }
81  : COutputLogger(o),
82  m_enabled(o.m_enabled),
83  m_name(o.m_name),
84  m_data(o.m_data)
85 {
86 }
88 {
89  COutputLogger::operator=(o);
90  m_enabled = o.m_enabled;
91  m_name = o.m_name;
92  m_data = o.m_data;
93  return *this;
94 }
95 
96 void CTimeLogger::clear(bool deep_clear)
97 {
98  if (deep_clear)
99  m_data.clear();
100  else
101  {
102  for (auto& e : m_data) e.second = TCallData();
103  }
104 }
105 
107 {
108  std::string ret;
109 
110  for (size_t p = 0; p < s.size(); p += len)
111  {
112  ret += rightPad(s.c_str() + p, len, true);
113  if (p + len < s.size()) ret += "\n";
114  }
115  return ret;
116 }
117 
118 void CTimeLogger::getStats(std::map<std::string, TCallStats>& out_stats) const
119 {
120  out_stats.clear();
121  for (const auto e : m_data)
122  {
123  TCallStats& cs = out_stats[e.first];
124  cs.min_t = e.second.min_t;
125  cs.max_t = e.second.max_t;
126  cs.total_t = e.second.mean_t;
127  cs.mean_t = e.second.n_calls ? e.second.mean_t / e.second.n_calls : 0;
128  cs.n_calls = e.second.n_calls;
129  cs.last_t = e.second.last_t;
130  }
131 }
132 
133 std::string CTimeLogger::getStatsAsText(const size_t column_width) const
134 {
135  std::string stats_text;
136  std::string name_tmp = m_name.size() ? " " + m_name + ": " : " ";
137  std::string mrpt_string = "MRPT CTimeLogger report ";
138 
139  std::string top_header(name_tmp + mrpt_string);
140  // append dashes to the header to reach column_width
141  {
142  int space_to_fill = top_header.size() < column_width
143  ? (column_width - top_header.size()) / 2
144  : 2;
145  std::string dashes_half(space_to_fill, '-');
146  top_header = dashes_half + top_header + dashes_half;
147  if (dashes_half.size() % 2)
148  { // what if column_width-top_header.size() is odd?
149  top_header += '-';
150  }
151  }
152 
153  std::string middle_header(
154  " FUNCTION #CALLS MIN.T MEAN.T "
155  "MAX.T TOTAL ");
156  std::string bottom_header(column_width, '-');
157 
158  stats_text += top_header + "\n";
159  stats_text += middle_header + "\n";
160  stats_text += bottom_header + "\n";
161 
162  // for all the timed sections
163  for (const auto i : m_data)
164  {
165  const string sMinT = unitsFormat(i.second.min_t, 1, false);
166  const string sMaxT = unitsFormat(i.second.max_t, 1, false);
167  const string sTotalT = unitsFormat(i.second.mean_t, 1, false);
168  const string sMeanT = unitsFormat(
169  i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0, 1,
170  false);
171 
172  stats_text += format(
173  "%s %7u %6s%c %6s%c %6s%c %6s%c\n",
174  aux_format_string_multilines(i.first, 39).c_str(),
175  static_cast<unsigned int>(i.second.n_calls), sMinT.c_str(),
176  i.second.has_time_units ? 's' : ' ', sMeanT.c_str(),
177  i.second.has_time_units ? 's' : ' ', sMaxT.c_str(),
178  i.second.has_time_units ? 's' : ' ', sTotalT.c_str(),
179  i.second.has_time_units ? 's' : ' ');
180  }
181 
182  std::string footer(top_header);
183  stats_text += footer + "\n";
184 
185  return stats_text;
186 }
187 
188 void CTimeLogger::saveToCSVFile(const std::string& csv_file) const
189 {
190  std::string s;
191  s += "FUNCTION, #CALLS, LAST.T, MIN.T, MEAN.T, MAX.T, TOTAL.T\n";
192  for (const auto& i : m_data)
193  {
194  s += format(
195  "\"%s\",\"%7u\",\"%e\",\"%e\",\"%e\",\"%e\",\"%e\"\n",
196  i.first.c_str(), static_cast<unsigned int>(i.second.n_calls),
197  i.second.last_t, i.second.min_t,
198  i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0,
199  i.second.max_t, i.second.mean_t);
200  }
201  std::ofstream(csv_file) << s;
202 }
203 
204 void CTimeLogger::dumpAllStats(const size_t column_width) const
205 {
206  MRPT_LOG_INFO_STREAM("dumpAllStats:\n" << getStatsAsText(column_width));
207 }
208 
209 void CTimeLogger::do_enter(const char* func_name)
210 {
211  const string s = func_name;
212  TCallData& d = m_data[s];
213 
214  d.n_calls++;
215  d.open_calls.push(0); // Dummy value, it'll be written below
216  d.open_calls.top() = m_tictac.Tac(); // to avoid possible delays.
217 }
218 
219 double CTimeLogger::do_leave(const char* func_name)
220 {
221  const double tim = m_tictac.Tac();
222 
223  const string s = func_name;
224  TCallData& d = m_data[s];
225 
226  if (!d.open_calls.empty())
227  {
228  const double At = tim - d.open_calls.top();
229  d.open_calls.pop();
230 
231  d.last_t = At;
232  d.mean_t += At;
233  if (d.n_calls == 1)
234  {
235  d.min_t = At;
236  d.max_t = At;
237  }
238  else
239  {
240  mrpt::keep_min(d.min_t, At);
241  mrpt::keep_max(d.max_t, At);
242  }
243  return At;
244  }
245  else
246  return 0; // This shouldn't happen!
247 }
248 
250  const char* event_name, const double value)
251 {
252  if (!m_enabled) return;
253  const string s = event_name;
254  TCallData& d = m_data[s];
255 
256  d.has_time_units = false;
257  d.last_t = value;
258  d.mean_t += value;
259  if (++d.n_calls == 1)
260  {
261  d.min_t = value;
262  d.max_t = value;
263  }
264  else
265  {
268  }
269 }
270 
272  : n_calls(0), min_t(0), max_t(0), mean_t(0), last_t(0), has_time_units(true)
273 {
274 }
275 
277 {
279  if (it == m_data.end())
280  return 0;
281  else
282  return it->second.n_calls ? it->second.mean_t / it->second.n_calls : 0;
283 }
285 {
287  if (it == m_data.end())
288  return 0;
289  else
290  return it->second.last_t;
291 }
292 
294  const CTimeLogger& logger, const char* section_name)
295  : m_logger(const_cast<CTimeLogger&>(logger)), m_section_name(section_name)
296 {
298 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
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.
STL namespace.
mrpt::system::CTimeLogger & global_profiler_getref() noexcept
Definition: CTimeLogger.cpp:40
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
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.
GLdouble s
Definition: glext.h:3676
GLenum GLsizei len
Definition: glext.h:4712
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
MyGlobalProfiler global_profiler
Definition: CTimeLogger.cpp:36
std::stack< double, std::vector< double > > open_calls
void global_profiler_enter(const char *func_name) noexcept
Definition: CTimeLogger.cpp:41
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
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...
#define MRPT_LOG_INFO_STREAM(__CONTENTS)
CTimeLogger & operator=(const CTimeLogger &o)
Definition: CTimeLogger.cpp:72
void global_profiler_leave(const char *func_name) noexcept
Definition: CTimeLogger.cpp:45
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string unitsFormat(const double val, int nDecimalDigits=2, bool middle_space=true)
This function implements formatting with the appropriate SI metric unit prefix: 1e-12->&#39;p&#39;, 1e-9->&#39;n&#39;, 1e-6->&#39;u&#39;, 1e-3->&#39;m&#39;, 1->&#39;&#39;, 1e3->&#39;K&#39;, 1e6->&#39;M&#39;, 1e9->&#39;G&#39;, 1e12->&#39;T&#39;.
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.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
std::string aux_format_string_multilines(const std::string &s, const size_t len)
GLuint const GLchar * name
Definition: glext.h:4054
~CTimeLoggerEntry()
std::string rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger=false)
Enlarge the string with spaces up to the given length.
GLsizei const GLfloat * value
Definition: glext.h:4117
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
GLfloat GLfloat p
Definition: glext.h:6305
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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