CARLA
Profiler.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #ifndef LIBCARLA_ENABLE_PROFILER
10 # define CARLA_PROFILE_SCOPE(context, profiler_name)
11 # define CARLA_PROFILE_FPS(context, profiler_name)
12 #else
13 
14 #include "carla/StopWatch.h"
15 
16 #include <algorithm>
17 #include <limits>
18 #include <string>
19 
20 namespace carla {
21 namespace profiler {
22 namespace detail {
23 
24  class ProfilerData {
25  public:
26 
27  explicit ProfilerData(std::string name, bool print_fps = false)
28  : _name(std::move(name)),
29  _print_fps(print_fps) {}
30 
31  ~ProfilerData();
32 
33  void Annotate(const StopWatch &stop_watch) {
34  const auto elapsed_microseconds = stop_watch.GetElapsedTime<std::chrono::microseconds>();
35  ++_count;
36  _total_microseconds += elapsed_microseconds;
37  _max_elapsed = std::max(elapsed_microseconds, _max_elapsed);
38  _min_elapsed = std::min(elapsed_microseconds, _min_elapsed);
39  }
40 
41  float average() const {
42  return ms(_total_microseconds) / static_cast<float>(_count);
43  }
44 
45  float maximum() const {
46  return ms(_max_elapsed);
47  }
48 
49  float minimum() const {
50  return ms(_min_elapsed);
51  }
52 
53  private:
54 
55  static inline float ms(size_t microseconds) {
56  return 1e-3f * static_cast<float>(microseconds);
57  }
58 
59  static inline float fps(float milliseconds) {
60  return milliseconds > 0.0f ? (1e3f / milliseconds) : std::numeric_limits<float>::max();
61  }
62 
63  const std::string _name;
64 
65  const bool _print_fps;
66 
67  size_t _count = 0u;
68 
69  size_t _total_microseconds = 0u;
70 
71  size_t _max_elapsed = 0u;
72 
73  size_t _min_elapsed = std::numeric_limits<size_t>::max();
74  };
75 
76  class ScopedProfiler {
77  public:
78 
79  explicit ScopedProfiler(ProfilerData &parent) : _profiler(parent) {}
80 
81  ~ScopedProfiler() {
82  _stop_watch.Stop();
83  _profiler.Annotate(_stop_watch);
84  }
85 
86  private:
87 
88  ProfilerData &_profiler;
89 
90  StopWatch _stop_watch;
91  };
92 
93 } // namespace detail
94 } // namespace profiler
95 } // namespace carla
96 
97 #ifdef LIBCARLA_WITH_GTEST
98 # define LIBCARLA_GTEST_GET_TEST_NAME() std::string(::testing::UnitTest::GetInstance()->current_test_info()->name())
99 #else
100 # define LIBCARLA_GTEST_GET_TEST_NAME() std::string("")
101 #endif // LIBCARLA_WITH_GTEST
102 
103 #define CARLA_PROFILE_SCOPE(context, profiler_name) \
104  static thread_local ::carla::profiler::detail::ProfilerData carla_profiler_ ## context ## _ ## profiler_name ## _data( \
105  LIBCARLA_GTEST_GET_TEST_NAME() + "." #context "." #profiler_name); \
106  ::carla::profiler::detail::ScopedProfiler carla_profiler_ ## context ## _ ## profiler_name ## _scoped_profiler( \
107  carla_profiler_ ## context ## _ ## profiler_name ## _data);
108 
109 #define CARLA_PROFILE_FPS(context, profiler_name) \
110  { \
111  static thread_local ::carla::StopWatch stop_watch; \
112  stop_watch.Stop(); \
113  static thread_local bool first_time = true; \
114  if (!first_time) { \
115  static thread_local ::carla::profiler::detail::ProfilerData profiler_data( \
116  LIBCARLA_GTEST_GET_TEST_NAME() + "." #context "." #profiler_name, true); \
117  profiler_data.Annotate(stop_watch); \
118  } else { \
119  first_time = false; \
120  } \
121  stop_watch.Restart(); \
122  }
123 
124 #endif // LIBCARLA_ENABLE_PROFILER
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
detail::StopWatchTmpl< std::chrono::steady_clock > StopWatch
Definition: StopWatch.h:58
double min(double v1, double v2)
Definition: Simplify.h:294