detail/profiler.h
Go to the documentation of this file.
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 #include "fcl/export.h"
53 
54 namespace fcl {
55 namespace detail {
56 
62 class FCL_EXPORT Profiler
63 {
64 public:
65  // non-copyable
66  Profiler(const Profiler&) = delete;
67  Profiler& operator=(const Profiler&) = delete;
68 
71  class FCL_EXPORT ScopedBlock;
72 
77  class FCL_EXPORT ScopedStart;
78 
80  static Profiler& Instance(void);
81 
84  Profiler(bool printOnDestroy = false, bool autoStart = false);
85 
87  ~Profiler(void);
88 
90  static void Start(void);
91 
93  static void Stop(void);
94 
96  static void Clear(void);
97 
99  void start(void);
100 
102  void stop(void);
103 
105  void clear(void);
106 
108  static void Event(const std::string& name, const unsigned int times = 1);
109 
111  void event(const std::string &name, const unsigned int times = 1);
112 
114  static void Average(const std::string& name, const double value);
115 
117  void average(const std::string &name, const double value);
118 
120  static void Begin(const std::string &name);
121 
123  static void End(const std::string &name);
124 
126  void begin(const std::string &name);
127 
129  void end(const std::string &name);
130 
134  static void Status(std::ostream &out = std::cout, bool merge = true);
135 
139  void status(std::ostream &out = std::cout, bool merge = true);
140 
142  bool running(void) const;
143 
145  static bool Running(void);
146 
147 private:
148 
150  struct TimeInfo
151  {
152  TimeInfo(void);
153 
156 
159 
162 
164  unsigned long int parts;
165 
168 
170  void set(void);
171 
173  void update(void);
174  };
175 
177  struct AvgInfo
178  {
180  double total;
181 
183  double totalSqr;
184 
186  unsigned long int parts;
187  };
188 
190  struct PerThread
191  {
193  std::map<std::string, unsigned long int> events;
194 
196  std::map<std::string, AvgInfo> avg;
197 
199  std::map<std::string, TimeInfo> time;
200  };
201 
202  void printThreadInfo(std::ostream &out, const PerThread &data);
203 
204  std::mutex lock_;
205  std::map<std::thread::id, PerThread> data_;
207  bool running_;
209 
210 };
211 
214 class FCL_EXPORT Profiler::ScopedBlock
215 {
216 public:
219  ScopedBlock(const std::string &name, Profiler &prof = Profiler::Instance());
220 
221  ~ScopedBlock(void);
222 
223 private:
224 
225  std::string name_;
227 };
228 
233 class FCL_EXPORT Profiler::ScopedStart
234 {
235 public:
236 
238  ScopedStart(Profiler &prof = Profiler::Instance());
239 
240  ~ScopedStart(void);
241 
242 private:
243 
244  Profiler &prof_;
246 };
247 
248 } // namespace detail
249 } // namespace fcl
250 
251 #endif // #ifndef FCL_COMMON_DETAIL_PROFILER_H
time::point start
The point in time when counting time started.
std::chrono::system_clock::duration duration
Representation of a time duration.
Definition: time.h:55
unsigned long int parts
Number of times a value was added to this structure.
time::duration shortest
The shortest counted time interval.
Information about time spent in a section of the code.
Main namespace.
std::map< std::string, unsigned long int > events
The stored events.
unsigned long int parts
Number of times a chunk of time was added to this structure.
double total
The sum of the values to average.
Information maintained about averaged values.
static Profiler & Instance(void)
Return an instance of the class.
Definition: profiler.cpp:44
std::string name
time::duration longest
The longest counted time interval.
std::map< std::string, AvgInfo > avg
The stored averages.
std::map< std::string, TimeInfo > time
The amount of time spent in various places.
double totalSqr
The sub of squares of the values to average.
Information to be maintained for each thread.
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: time.h:52
time::duration total
Total time counted.
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.
std::map< std::thread::id, PerThread > data_


fcl_catkin
Author(s):
autogenerated on Thu Mar 23 2023 03:00:18