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
fcl::time::duration
std::chrono::system_clock::duration duration
Representation of a time duration.
Definition: time.h:55
fcl::detail::Profiler::TimeInfo::total
time::duration total
Total time counted.
Definition: detail/profiler.h:155
fcl::detail::Profiler::PerThread::avg
std::map< std::string, AvgInfo > avg
The stored averages.
Definition: detail/profiler.h:196
fcl::detail::Profiler::AvgInfo::parts
unsigned long int parts
Number of times a value was added to this structure.
Definition: detail/profiler.h:186
fcl::detail::Profiler::AvgInfo
Information maintained about averaged values.
Definition: detail/profiler.h:177
fcl::detail::Profiler::data_
std::map< std::thread::id, PerThread > data_
Definition: detail/profiler.h:205
fcl::detail::Profiler::prof_
Profiler & prof_
Definition: detail/profiler.h:226
fcl::detail::Profiler::wasRunning_
bool wasRunning_
Definition: detail/profiler.h:245
fcl::detail::Profiler::AvgInfo::totalSqr
double totalSqr
The sub of squares of the values to average.
Definition: detail/profiler.h:183
fcl::detail::Profiler::name_
std::string name_
Definition: detail/profiler.h:225
fcl::detail::Profiler::TimeInfo::shortest
time::duration shortest
The shortest counted time interval.
Definition: detail/profiler.h:158
fcl::detail::Profiler::TimeInfo::start
time::point start
The point in time when counting time started.
Definition: detail/profiler.h:167
name
std::string name
Definition: test_sphere_box.cpp:167
fcl::detail::Profiler::AvgInfo::total
double total
The sum of the values to average.
Definition: detail/profiler.h:180
fcl::detail::Profiler::tinfo_
TimeInfo tinfo_
Definition: detail/profiler.h:206
fcl::detail::Profiler::running_
bool running_
Definition: detail/profiler.h:207
time.h
fcl::detail::Profiler::Instance
static Profiler & Instance(void)
Return an instance of the class.
Definition: profiler.cpp:44
fcl::detail::Profiler::PerThread::time
std::map< std::string, TimeInfo > time
The amount of time spent in various places.
Definition: detail/profiler.h:199
fcl::detail::Profiler::TimeInfo::parts
unsigned long int parts
Number of times a chunk of time was added to this structure.
Definition: detail/profiler.h:164
fcl::detail::Profiler::TimeInfo
Information about time spent in a section of the code.
Definition: detail/profiler.h:150
fcl::time::point
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: time.h:52
fcl::detail::Profiler::printOnDestroy_
bool printOnDestroy_
Definition: detail/profiler.h:208
fcl::detail::Profiler::PerThread::events
std::map< std::string, unsigned long int > events
The stored events.
Definition: detail/profiler.h:193
fcl::detail::Profiler::lock_
std::mutex lock_
Definition: detail/profiler.h:204
fcl::detail::Profiler::PerThread
Information to be maintained for each thread.
Definition: detail/profiler.h:190
fcl::detail::Profiler
This is a simple thread-safe tool for counting time spent in various chunks of code....
Definition: detail/profiler.h:62
fcl::detail::Profiler::TimeInfo::longest
time::duration longest
The longest counted time interval.
Definition: detail/profiler.h:161
fcl
Main namespace.
Definition: broadphase_bruteforce-inl.h:45


fcl
Author(s):
autogenerated on Tue Dec 5 2023 03:40:48