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 #ifndef MOVEIT_PROFILER_
38 #define MOVEIT_PROFILER_
39 
40 #define MOVEIT_ENABLE_PROFILING 1
41 
42 #ifndef MOVEIT_ENABLE_PROFILING
43 
47 #ifdef NDEBUG
48 #define MOVEIT_ENABLE_PROFILING 0
49 #else
50 #define MOVEIT_ENABLE_PROFILING 1
51 #endif
52 
53 #endif
54 
55 #if MOVEIT_ENABLE_PROFILING
56 
57 #include <map>
58 #include <string>
59 #include <iostream>
60 #include <boost/thread.hpp>
61 #include <boost/noncopyable.hpp>
62 #include <boost/date_time/posix_time/posix_time.hpp>
63 
64 namespace moveit
65 {
66 namespace tools
67 {
73 class Profiler : private boost::noncopyable
74 {
75 public:
79  {
80  public:
82  ScopedBlock(const std::string& name, Profiler& prof = Profiler::Instance()) : name_(name), prof_(prof)
83  {
84  prof_.begin(name);
85  }
86 
88  {
89  prof_.end(name_);
90  }
91 
92  private:
93  std::string name_;
95  };
96 
100  {
101  public:
103  ScopedStart(Profiler& prof = Profiler::Instance()) : prof_(prof), wasRunning_(prof_.running())
104  {
105  if (!wasRunning_)
106  prof_.start();
107  }
108 
110  {
111  if (!wasRunning_)
112  prof_.stop();
113  }
114 
115  private:
118  };
119 
121  static Profiler& Instance(void);
122 
125  Profiler(bool printOnDestroy = false, bool autoStart = false) : running_(false), printOnDestroy_(printOnDestroy)
126  {
127  if (autoStart)
128  start();
129  }
130 
132  ~Profiler(void)
133  {
134  if (printOnDestroy_ && !data_.empty())
135  status();
136  }
137 
139  static void Start(void)
140  {
141  Instance().start();
142  }
143 
145  static void Stop(void)
146  {
147  Instance().stop();
148  }
149 
151  static void Clear(void)
152  {
153  Instance().clear();
154  }
155 
157  void start(void);
158 
160  void stop(void);
161 
163  void clear(void);
164 
166  static void Event(const std::string& name, const unsigned int times = 1)
167  {
168  Instance().event(name, times);
169  }
170 
172  void event(const std::string& name, const unsigned int times = 1);
173 
175  static void Average(const std::string& name, const double value)
176  {
177  Instance().average(name, value);
178  }
179 
181  void average(const std::string& name, const double value);
182 
184  static void Begin(const std::string& name)
185  {
186  Instance().begin(name);
187  }
188 
190  static void End(const std::string& name)
191  {
192  Instance().end(name);
193  }
194 
196  void begin(const std::string& name);
197 
199  void end(const std::string& name);
200 
204  static void Status(std::ostream& out = std::cout, bool merge = true)
205  {
206  Instance().status(out, merge);
207  }
208 
212  void status(std::ostream& out = std::cout, bool merge = true);
213 
216  static void Console(void)
217  {
218  Instance().console();
219  }
220 
223  void console(void);
224 
226  bool running(void) const
227  {
228  return running_;
229  }
230 
232  static bool Running(void)
233  {
234  return Instance().running();
235  }
236 
237 private:
239  struct TimeInfo
240  {
241  TimeInfo(void)
242  : total(0, 0, 0, 0), shortest(boost::posix_time::pos_infin), longest(boost::posix_time::neg_infin), parts(0)
243  {
244  }
245 
247  boost::posix_time::time_duration total;
248 
250  boost::posix_time::time_duration shortest;
251 
253  boost::posix_time::time_duration longest;
254 
256  unsigned long int parts;
257 
259  boost::posix_time::ptime start;
260 
262  void set(void)
263  {
264  start = boost::posix_time::microsec_clock::universal_time();
265  }
266 
268  void update(void)
269  {
270  const boost::posix_time::time_duration& dt = boost::posix_time::microsec_clock::universal_time() - start;
271  if (dt > longest)
272  longest = dt;
273  if (dt < shortest)
274  shortest = dt;
275  total = total + dt;
276  ++parts;
277  }
278  };
279 
281  struct AvgInfo
282  {
284  double total;
285 
287  double totalSqr;
288 
290  unsigned long int parts;
291  };
292 
294  struct PerThread
295  {
297  std::map<std::string, unsigned long int> events;
298 
300  std::map<std::string, AvgInfo> avg;
301 
303  std::map<std::string, TimeInfo> time;
304  };
305 
306  void printThreadInfo(std::ostream& out, const PerThread& data);
307 
308  boost::mutex lock_;
309  std::map<boost::thread::id, PerThread> data_;
311  bool running_;
313 };
314 }
315 }
316 
317 #else
318 
319 #include <string>
320 #include <iostream>
321 
322 /* If profiling is disabled, provide empty implementations for the
323  public functions */
324 namespace moveit
325 {
326 namespace tools
327 {
328 class Profiler
329 {
330 public:
331  class ScopedBlock
332  {
333  public:
334  ScopedBlock(const std::string&, Profiler& = Profiler::Instance())
335  {
336  }
337 
338  ~ScopedBlock(void)
339  {
340  }
341  };
342 
343  class ScopedStart
344  {
345  public:
347  {
348  }
349 
350  ~ScopedStart(void)
351  {
352  }
353  };
354 
355  static Profiler& Instance(void);
356 
357  Profiler(bool = true, bool = true)
358  {
359  }
360 
361  ~Profiler(void)
362  {
363  }
364 
365  static void Start(void)
366  {
367  }
368 
369  static void Stop(void)
370  {
371  }
372 
373  static void Clear(void)
374  {
375  }
376 
377  void start(void)
378  {
379  }
380 
381  void stop(void)
382  {
383  }
384 
385  void clear(void)
386  {
387  }
388 
389  static void Event(const std::string&, const unsigned int = 1)
390  {
391  }
392 
393  void event(const std::string&, const unsigned int = 1)
394  {
395  }
396 
397  static void Average(const std::string&, const double)
398  {
399  }
400 
401  void average(const std::string&, const double)
402  {
403  }
404 
405  static void Begin(const std::string&)
406  {
407  }
408 
409  static void End(const std::string&)
410  {
411  }
412 
413  void begin(const std::string&)
414  {
415  }
416 
417  void end(const std::string&)
418  {
419  }
420 
421  static void Status(std::ostream& = std::cout, bool = true)
422  {
423  }
424 
425  void status(std::ostream& = std::cout, bool = true)
426  {
427  }
428 
429  static void Console(void)
430  {
431  }
432 
433  void console(void)
434  {
435  }
436 
437  bool running(void) const
438  {
439  return false;
440  }
441 
442  static bool Running(void)
443  {
444  return false;
445  }
446 };
447 }
448 }
449 
450 #endif
451 
452 #endif
Information to be maintained for each thread.
Definition: profiler.h:294
static void Stop(void)
Stop counting time.
Definition: profiler.h:145
void begin(const std::string &name)
Begin counting time for a specific chunk of code.
std::map< std::string, TimeInfo > time
The amount of time spent in various places.
Definition: profiler.h:303
void start(void)
Start counting time.
static void Start(void)
Start counting time.
Definition: profiler.h:139
double totalSqr
The sub of squares of the values to average.
Definition: profiler.h:287
Profiler(bool printOnDestroy=false, bool autoStart=false)
Constructor. It is allowed to separately instantiate this class (not only as a singleton) ...
Definition: profiler.h:125
unsigned long int parts
Number of times a chunk of time was added to this structure.
Definition: profiler.h:256
static void Console(void)
Print the status of the profiled code chunks and events to the console (using msg::Console) ...
Definition: profiler.h:216
void console(void)
Print the status of the profiled code chunks and events to the console (using msg::Console) ...
static void Average(const std::string &name, const double value)
Maintain the average of a specific value.
Definition: profiler.h:175
static void Begin(const std::string &name)
Begin counting time for a specific chunk of code.
Definition: profiler.h:184
ScopedStart(Profiler &prof=Profiler::Instance())
Take as argument the profiler instance to operate on (prof)
Definition: profiler.h:103
bool running(void) const
Check if the profiler is counting time or not.
Definition: profiler.h:226
void update(void)
Add the counted time to the total time.
Definition: profiler.h:268
void end(const std::string &name)
Stop counting time for a specific chunk of code.
static void End(const std::string &name)
Stop counting time for a specific chunk of code.
Definition: profiler.h:190
std::map< std::string, unsigned long int > events
The stored events.
Definition: profiler.h:297
void printThreadInfo(std::ostream &out, const PerThread &data)
void stop(void)
Stop counting time.
boost::posix_time::ptime start
The point in time when counting time started.
Definition: profiler.h:259
static Profiler & Instance(void)
Return an instance of the class.
This instance will call Profiler::start() when constructed and Profiler::stop() when it goes out of s...
Definition: profiler.h:99
Information maintained about averaged values.
Definition: profiler.h:281
boost::posix_time::time_duration total
Total time counted.
Definition: profiler.h:247
ScopedBlock(const std::string &name, Profiler &prof=Profiler::Instance())
Start counting time for the block named name of the profiler prof.
Definition: profiler.h:82
static void Event(const std::string &name, const unsigned int times=1)
Count a specific event for a number of times.
Definition: profiler.h:166
boost::posix_time::time_duration shortest
The shortest counted time interval.
Definition: profiler.h:250
unsigned long int parts
Number of times a value was added to this structure.
Definition: profiler.h:290
static bool Running(void)
Check if the profiler is counting time or not.
Definition: profiler.h:232
boost::posix_time::time_duration longest
The longest counted time interval.
Definition: profiler.h:253
~Profiler(void)
Destructor.
Definition: profiler.h:132
This instance will call Profiler::begin() when constructed and Profiler::end() when it goes out of sc...
Definition: profiler.h:78
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...
void average(const std::string &name, const double value)
Maintain the average of a specific value.
Main namespace for MoveIt!
static void Clear(void)
Clear counted time and events.
Definition: profiler.h:151
std::map< std::string, AvgInfo > avg
The stored averages.
Definition: profiler.h:300
boost::mutex lock_
Definition: profiler.h:308
void clear(void)
Clear counted time and events.
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:204
void event(const std::string &name, const unsigned int times=1)
Count a specific event for a number of times.
std::map< boost::thread::id, PerThread > data_
Definition: profiler.h:309
Information about time spent in a section of the code.
Definition: profiler.h:239
double total
The sum of the values to average.
Definition: profiler.h:284


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Thu Oct 18 2018 02:47:09