profiler.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #pragma once
38 
39 #define MOVEIT_ENABLE_PROFILING 1
40 
41 #ifndef MOVEIT_ENABLE_PROFILING
42 
46 #ifdef NDEBUG
47 #define MOVEIT_ENABLE_PROFILING 0
48 #else
49 #define MOVEIT_ENABLE_PROFILING 1
50 #endif
51 
52 #endif
53 
54 #if MOVEIT_ENABLE_PROFILING
55 
56 #include <map>
57 #include <string>
58 #include <iostream>
59 #include <boost/thread.hpp>
60 #include <boost/noncopyable.hpp>
61 #include <boost/date_time/posix_time/posix_time.hpp>
62 
63 namespace moveit
64 {
65 namespace tools
66 {
72 class Profiler : private boost::noncopyable
73 {
74 public:
78  {
79  public:
81  ScopedBlock(const std::string& name, Profiler& prof = Profiler::instance()) : name_(name), prof_(prof)
82  {
83  prof_.begin(name);
84  }
85 
87  {
88  prof_.end(name_);
89  }
90 
91  private:
92  std::string name_;
94  };
95 
99  {
100  public:
103  {
104  if (!wasRunning_)
105  prof_.start();
106  }
107 
109  {
110  if (!wasRunning_)
111  prof_.stop();
112  }
113 
114  private:
117  };
118 
120  static Profiler& instance();
121 
124  Profiler(bool printOnDestroy = false, bool autoStart = false) : running_(false), printOnDestroy_(printOnDestroy)
125  {
126  if (autoStart)
127  start();
128  }
129 
132  {
133  if (printOnDestroy_ && !data_.empty())
134  status();
135  }
136 
138  static void Start() // NOLINT(readability-identifier-naming)
139  {
140  instance().start();
141  }
142 
144  static void Stop() // NOLINT(readability-identifier-naming)
145  {
146  instance().stop();
147  }
148 
150  static void Clear() // NOLINT(readability-identifier-naming)
151  {
152  instance().clear();
153  }
154 
156  void start();
157 
159  void stop();
160 
162  void clear();
163 
165  static void Event(const std::string& name, const unsigned int times = 1) // NOLINT(readability-identifier-naming)
166  {
167  instance().event(name, times);
168  }
169 
171  void event(const std::string& name, const unsigned int times = 1);
172 
174  static void Average(const std::string& name, const double value) // NOLINT(readability-identifier-naming)
175  {
176  instance().average(name, value);
177  }
178 
180  void average(const std::string& name, const double value);
181 
183  static void Begin(const std::string& name) // NOLINT(readability-identifier-naming)
184  {
185  instance().begin(name);
186  }
187 
189  static void End(const std::string& name) // NOLINT(readability-identifier-naming)
190  {
191  instance().end(name);
192  }
193 
195  void begin(const std::string& name);
196 
198  void end(const std::string& name);
199 
203  static void Status(std::ostream& out = std::cout, bool merge = true) // NOLINT(readability-identifier-naming)
204  {
205  instance().status(out, merge);
206  }
207 
211  void status(std::ostream& out = std::cout, bool merge = true);
212 
215  static void Console() // NOLINT(readability-identifier-naming)
216  {
217  instance().console();
218  }
219 
222  void console();
223 
225  bool running() const
226  {
227  return running_;
228  }
229 
231  static bool Running() // NOLINT(readability-identifier-naming)
232  {
233  return instance().running();
234  }
235 
236 private:
238  struct TimeInfo
239  {
241  : total(0, 0, 0, 0), shortest(boost::posix_time::pos_infin), longest(boost::posix_time::neg_infin), parts(0)
242  {
243  }
244 
246  boost::posix_time::time_duration total;
247 
249  boost::posix_time::time_duration shortest;
250 
252  boost::posix_time::time_duration longest;
253 
255  unsigned long int parts;
256 
258  boost::posix_time::ptime start;
259 
261  void set()
262  {
263  start = boost::posix_time::microsec_clock::universal_time();
264  }
265 
267  void update()
268  {
269  const boost::posix_time::time_duration& dt = boost::posix_time::microsec_clock::universal_time() - start;
270  if (dt > longest)
271  longest = dt;
272  if (dt < shortest)
273  shortest = dt;
274  total = total + dt;
275  ++parts;
276  }
277  };
278 
280  struct AvgInfo
281  {
283  double total;
284 
286  double totalSqr;
287 
289  unsigned long int parts;
290  };
291 
293  struct PerThread
294  {
296  std::map<std::string, unsigned long int> events;
297 
299  std::map<std::string, AvgInfo> avg;
300 
302  std::map<std::string, TimeInfo> time;
303  };
304 
305  void printThreadInfo(std::ostream& out, const PerThread& data);
306 
307  boost::mutex lock_;
308  std::map<boost::thread::id, PerThread> data_;
310  bool running_;
312 };
313 } // namespace tools
314 } // namespace moveit
315 
316 #else
317 
318 #include <string>
319 #include <iostream>
320 
321 /* If profiling is disabled, provide empty implementations for the
322  public functions */
323 namespace moveit
324 {
325 namespace tools
326 {
327 class Profiler
328 {
329 public:
330  class ScopedBlock
331  {
332  public:
333  ScopedBlock(const std::string&, Profiler& = Profiler::instance())
334  {
335  }
336 
337  ~ScopedBlock(void)
338  {
339  }
340  };
341 
342  class ScopedStart
343  {
344  public:
346  {
347  }
348 
349  ~ScopedStart(void)
350  {
351  }
352  };
353 
354  static Profiler& instance(void);
355 
356  Profiler(bool = true, bool = true)
357  {
358  }
359 
360  ~Profiler(void)
361  {
362  }
363 
364  static void Start(void)
365  {
366  }
367 
368  static void Stop(void)
369  {
370  }
371 
372  static void Clear(void)
373  {
374  }
375 
376  void start(void)
377  {
378  }
379 
380  void stop(void)
381  {
382  }
383 
384  void clear(void)
385  {
386  }
387 
388  static void Event(const std::string&, const unsigned int = 1)
389  {
390  }
391 
392  void event(const std::string&, const unsigned int = 1)
393  {
394  }
395 
396  static void Average(const std::string&, const double)
397  {
398  }
399 
400  void average(const std::string&, const double)
401  {
402  }
403 
404  static void Begin(const std::string&)
405  {
406  }
407 
408  static void End(const std::string&)
409  {
410  }
411 
412  void begin(const std::string&)
413  {
414  }
415 
416  void end(const std::string&)
417  {
418  }
419 
420  static void Status(std::ostream& = std::cout, bool = true)
421  {
422  }
423 
424  void status(std::ostream& = std::cout, bool = true)
425  {
426  }
427 
428  static void Console(void)
429  {
430  }
431 
432  void console(void)
433  {
434  }
435 
436  bool running(void) const
437  {
438  return false;
439  }
440 
441  static bool Running(void)
442  {
443  return false;
444  }
445 };
446 } // namespace tools
447 } // namespace moveit
448 
449 #endif
moveit::tools::Profiler::running_
bool running_
Definition: profiler.h:310
moveit::tools::Profiler::end
void end(const std::string &name)
Stop counting time for a specific chunk of code.
moveit::tools::Profiler::Status
static void Status(std::ostream &out=std::cout, bool merge=true)
Print the status of the profiled code chunks and events. Optionally, computation done by different th...
Definition: profiler.h:203
moveit::tools::Profiler::Begin
static void Begin(const std::string &name)
Begin counting time for a specific chunk of code.
Definition: profiler.h:183
moveit::tools::Profiler::Average
static void Average(const std::string &name, const double value)
Maintain the average of a specific value.
Definition: profiler.h:174
moveit::tools::Profiler::instance
static Profiler & instance()
Return an instance of the class.
moveit::tools::Profiler::TimeInfo::update
void update()
Add the counted time to the total time.
Definition: profiler.h:267
moveit::tools::Profiler::AvgInfo::totalSqr
double totalSqr
The sub of squares of the values to average.
Definition: profiler.h:286
moveit::tools::Profiler::event
void event(const std::string &name, const unsigned int times=1)
Count a specific event for a number of times.
moveit::tools::Profiler::TimeInfo
Information about time spent in a section of the code.
Definition: profiler.h:238
moveit::tools::Profiler::AvgInfo::parts
unsigned long int parts
Number of times a value was added to this structure.
Definition: profiler.h:289
moveit::tools::Profiler::Stop
static void Stop()
Stop counting time.
Definition: profiler.h:144
boost
moveit::tools::Profiler::ScopedStart
This instance will call Profiler::start() when constructed and Profiler::stop() when it goes out of s...
Definition: profiler.h:98
moveit::tools::Profiler::TimeInfo::total
boost::posix_time::time_duration total
Total time counted.
Definition: profiler.h:246
moveit::tools::Profiler::Console
static void Console()
Print the status of the profiled code chunks and events to the console (using msg::Console)
Definition: profiler.h:215
moveit::tools::Profiler::begin
void begin(const std::string &name)
Begin counting time for a specific chunk of code.
moveit::tools::Profiler::printThreadInfo
void printThreadInfo(std::ostream &out, const PerThread &data)
moveit::tools::Profiler
Definition: profiler.h:72
moveit::tools::Profiler::ScopedStart::ScopedStart
ScopedStart(Profiler &prof=Profiler::instance())
Take as argument the profiler instance to operate on (prof)
Definition: profiler.h:102
moveit::tools::Profiler::start
void start()
Start counting time.
moveit::tools::Profiler::ScopedBlock::ScopedBlock
ScopedBlock(const std::string &name, Profiler &prof=Profiler::instance())
Start counting time for the block named name of the profiler prof.
Definition: profiler.h:81
moveit::tools::Profiler::Start
static void Start()
Start counting time.
Definition: profiler.h:138
moveit::tools::Profiler::Profiler
Profiler(bool printOnDestroy=false, bool autoStart=false)
Constructor. It is allowed to separately instantiate this class (not only as a singleton)
Definition: profiler.h:124
name
std::string name
moveit::tools::Profiler::TimeInfo::TimeInfo
TimeInfo()
Definition: profiler.h:240
moveit::tools::Profiler::printOnDestroy_
bool printOnDestroy_
Definition: profiler.h:311
moveit::tools::Profiler::ScopedStart::prof_
Profiler & prof_
Definition: profiler.h:115
moveit::tools::Profiler::TimeInfo::longest
boost::posix_time::time_duration longest
The longest counted time interval.
Definition: profiler.h:252
moveit::tools::Profiler::ScopedStart::~ScopedStart
~ScopedStart()
Definition: profiler.h:108
moveit::tools::Profiler::status
void status(std::ostream &out=std::cout, bool merge=true)
Print the status of the profiled code chunks and events. Optionally, computation done by different th...
moveit::tools::Profiler::console
void console()
Print the status of the profiled code chunks and events to the console (using msg::Console)
moveit::tools::Profiler::PerThread::events
std::map< std::string, unsigned long int > events
The stored events.
Definition: profiler.h:296
moveit::tools::Profiler::PerThread::avg
std::map< std::string, AvgInfo > avg
The stored averages.
Definition: profiler.h:299
moveit::tools::Profiler::running
bool running() const
Check if the profiler is counting time or not.
Definition: profiler.h:225
moveit::tools::Profiler::data_
std::map< boost::thread::id, PerThread > data_
Definition: profiler.h:308
moveit::tools::Profiler::~Profiler
~Profiler()
Destructor.
Definition: profiler.h:131
moveit::tools::Profiler::AvgInfo::total
double total
The sum of the values to average.
Definition: profiler.h:283
moveit::tools::Profiler::ScopedStart::wasRunning_
bool wasRunning_
Definition: profiler.h:116
moveit::tools::Profiler::PerThread
Information to be maintained for each thread.
Definition: profiler.h:293
moveit::tools::Profiler::TimeInfo::parts
unsigned long int parts
Number of times a chunk of time was added to this structure.
Definition: profiler.h:255
moveit
Main namespace for MoveIt.
Definition: background_processing.h:46
moveit::tools::Profiler::average
void average(const std::string &name, const double value)
Maintain the average of a specific value.
moveit::tools::Profiler::ScopedBlock::prof_
Profiler & prof_
Definition: profiler.h:93
moveit::tools::Profiler::PerThread::time
std::map< std::string, TimeInfo > time
The amount of time spent in various places.
Definition: profiler.h:302
moveit::tools::Profiler::ScopedBlock
This instance will call Profiler::begin() when constructed and Profiler::end() when it goes out of sc...
Definition: profiler.h:77
moveit::tools::Profiler::lock_
boost::mutex lock_
Definition: profiler.h:307
moveit::tools::Profiler::stop
void stop()
Stop counting time.
moveit::tools::Profiler::ScopedBlock::name_
std::string name_
Definition: profiler.h:92
moveit::tools::Profiler::End
static void End(const std::string &name)
Stop counting time for a specific chunk of code.
Definition: profiler.h:189
moveit::tools::Profiler::Running
static bool Running()
Check if the profiler is counting time or not.
Definition: profiler.h:231
moveit::tools::Profiler::AvgInfo
Information maintained about averaged values.
Definition: profiler.h:280
moveit::tools::Profiler::ScopedBlock::~ScopedBlock
~ScopedBlock()
Definition: profiler.h:86
moveit::tools::Profiler::Event
static void Event(const std::string &name, const unsigned int times=1)
Count a specific event for a number of times.
Definition: profiler.h:165
moveit::tools::Profiler::TimeInfo::shortest
boost::posix_time::time_duration shortest
The shortest counted time interval.
Definition: profiler.h:249
moveit::tools::Profiler::clear
void clear()
Clear counted time and events.
moveit::tools::Profiler::TimeInfo::set
void set()
Begin counting time.
Definition: profiler.h:261
moveit::tools::Profiler::TimeInfo::start
boost::posix_time::ptime start
The point in time when counting time started.
Definition: profiler.h:258
moveit::tools::Profiler::Clear
static void Clear()
Clear counted time and events.
Definition: profiler.h:150
moveit::tools::Profiler::tinfo_
TimeInfo tinfo_
Definition: profiler.h:309


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Sat Dec 21 2024 03:23:42