FCL  0.6.0
Flexible Collision Library
profiler.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * 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 
38 #ifndef FCL_COMMON_DETAIL_PROFILER_H
39 #define FCL_COMMON_DETAIL_PROFILER_H
40 
41 #include <algorithm>
42 #include <chrono>
43 #include <iostream>
44 #include <map>
45 #include <cmath>
46 #include <mutex>
47 #include <sstream>
48 #include <string>
49 #include <thread>
50 #include <vector>
51 #include "fcl/common/time.h"
52 
53 namespace fcl {
54 namespace detail {
55 
61 class Profiler
62 {
63 public:
64  // non-copyable
65  Profiler(const Profiler&) = delete;
66  Profiler& operator=(const Profiler&) = delete;
67 
70  class ScopedBlock;
71 
76  class ScopedStart;
77 
79  static Profiler& Instance(void);
80 
83  Profiler(bool printOnDestroy = false, bool autoStart = false);
84 
86  ~Profiler(void);
87 
89  static void Start(void);
90 
92  static void Stop(void);
93 
95  static void Clear(void);
96 
98  void start(void);
99 
101  void stop(void);
102 
104  void clear(void);
105 
107  static void Event(const std::string& name, const unsigned int times = 1);
108 
110  void event(const std::string &name, const unsigned int times = 1);
111 
113  static void Average(const std::string& name, const double value);
114 
116  void average(const std::string &name, const double value);
117 
119  static void Begin(const std::string &name);
120 
122  static void End(const std::string &name);
123 
125  void begin(const std::string &name);
126 
128  void end(const std::string &name);
129 
133  static void Status(std::ostream &out = std::cout, bool merge = true);
134 
138  void status(std::ostream &out = std::cout, bool merge = true);
139 
141  bool running(void) const;
142 
144  static bool Running(void);
145 
146 private:
147 
149  struct TimeInfo
150  {
151  TimeInfo(void);
152 
154  time::duration total;
155 
157  time::duration shortest;
158 
160  time::duration longest;
161 
163  unsigned long int parts;
164 
167 
169  void set(void);
170 
172  void update(void);
173  };
174 
176  struct AvgInfo
177  {
179  double total;
180 
182  double totalSqr;
183 
185  unsigned long int parts;
186  };
187 
189  struct PerThread
190  {
192  std::map<std::string, unsigned long int> events;
193 
195  std::map<std::string, AvgInfo> avg;
196 
198  std::map<std::string, TimeInfo> time;
199  };
200 
201  void printThreadInfo(std::ostream &out, const PerThread &data);
202 
203  std::mutex lock_;
204  std::map<std::thread::id, PerThread> data_;
205  TimeInfo tinfo_;
206  bool running_;
207  bool printOnDestroy_;
208 
209 };
210 
214 {
215 public:
218  ScopedBlock(const std::string &name, Profiler &prof = Profiler::Instance());
219 
220  ~ScopedBlock(void);
221 
222 private:
223 
224  std::string name_;
225  Profiler &prof_;
226 };
227 
233 {
234 public:
235 
238 
239  ~ScopedStart(void);
240 
241 private:
242 
243  Profiler &prof_;
244  bool wasRunning_;
245 };
246 
247 } // namespace detail
248 } // namespace fcl
249 
250 #endif // #ifndef FCL_COMMON_DETAIL_PROFILER_H
std::chrono::system_clock::duration duration
Representation of a time duration.
Definition: time.h:54
This instance will call Profiler::start() when constructed and Profiler::stop() when it goes out of s...
Definition: profiler.h:232
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
void clear(void)
Clear counted time and events.
Definition: profiler.cpp:108
static bool Running(void)
Check if the profiler is counting time or not.
Definition: profiler.cpp:235
static void Begin(const std::string &name)
Begin counting time for a specific chunk of code.
Definition: profiler.cpp:150
This instance will call Profiler::begin() when constructed and Profiler::end() when it goes out of sc...
Definition: profiler.h:213
static void End(const std::string &name)
Stop counting time for a specific chunk of code.
Definition: profiler.cpp:156
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.cpp:178
static void Event(const std::string &name, const unsigned int times=1)
Count a specific event for a number of times.
Definition: profiler.cpp:119
static Profiler & Instance(void)
Return an instance of the class.
Definition: profiler.cpp:44
void average(const std::string &name, const double value)
Maintain the average of a specific value.
Definition: profiler.cpp:139
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.cpp:184
void event(const std::string &name, const unsigned int times=1)
Count a specific event for a number of times.
Definition: profiler.cpp:125
static void Start(void)
Start counting time.
Definition: profiler.cpp:66
void stop(void)
Stop counting time.
Definition: profiler.cpp:96
~Profiler(void)
Destructor.
Definition: profiler.cpp:59
static void Clear(void)
Clear counted time and events.
Definition: profiler.cpp:78
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: time.h:51
void end(const std::string &name)
Stop counting time for a specific chunk of code.
Definition: profiler.cpp:170
static void Average(const std::string &name, const double value)
Maintain the average of a specific value.
Definition: profiler.cpp:133
bool running(void) const
Check if the profiler is counting time or not.
Definition: profiler.cpp:229
void start(void)
Start counting time.
Definition: profiler.cpp:84
This is a simple thread-safe tool for counting time spent in various chunks of code. This is different from external profiling tools in that it allows the user to count time spent in various bits of code (sub-function granularity) or count how many times certain pieces of code are executed.
Definition: profiler.h:61
void begin(const std::string &name)
Begin counting time for a specific chunk of code.
Definition: profiler.cpp:162
static void Stop(void)
Stop counting time.
Definition: profiler.cpp:72