unrealengine4/Plugins/RealSense/Source/RealSense/Private/Profiler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #if defined(PROF_ENABLED)
4 
5 #define PROF_OPTION_QPC
6 #define PROF_OPTION_THREADSAFE
7 
8 #define INIT_PROFILER for(;;) { HiresTimer::Init(); Profiler::InitInstance(); break; }
9 #define SHUT_PROFILER for(;;) { Profiler::GetInstance()->LogSummary(); Profiler::DestroyInstance(); break; }
10 #define NAMED_PROFILER(name) ScopedProfiler named_profiler(PROF_TEXT(name), PROF_TEXT(PROF_UNIQ_NAME))
11 #define SCOPED_PROFILER ScopedProfiler scoped_profiler(PROF_TEXT(__FUNCTION__), PROF_TEXT(PROF_UNIQ_NAME))
12 
13 #if defined(_WIN32) || defined(_WIN64)
14  #define PROF_PLATFORM_WINDOWS
15 #endif
16 
17 #include <vector>
18 #include <algorithm>
19 
20 #if !defined(PROF_PLATFORM_WINDOWS)
21  #include <sys/time.h>
22  #include <time.h>
23 #endif
24 
25 #if !defined(PROF_CHAR)
26  #define PROF_CHAR char
27 #endif
28 
29 #if !defined(PROF_TEXT)
30  #define PROF_TEXT(str) str
31 #endif
32 
33 #if !defined(PROF_LOG)
34  #define PROF_LOG printf
35 #endif
36 
37 #if !defined(PROF_MAP_IMPL)
38  #define PROF_MAP_IMPL std::unordered_map
39  #include <unordered_map>
40 #endif
41 
42 #if defined(PROF_OPTION_THREADSAFE)
43  #if !defined(PROF_MUTEX_IMPL)
44  #include <mutex>
45  #define PROF_MUTEX_IMPL std::mutex
46  #define PROF_SCOPED_MUTEX_IMPL std::lock_guard<std::mutex>
47  #endif
48  #define PROF_MUTEX_DECL PROF_MUTEX_IMPL m_mutex
49  #define PROF_LOCK PROF_SCOPED_MUTEX_IMPL _scoped(m_mutex)
50 #else
51  #define PROF_MUTEX_DECL
52  #define PROF_LOCK
53 #endif
54 
55 #define PROF_STRINGIZE1(x) PROF_STRINGIZE2(x)
56 #define PROF_STRINGIZE2(x) #x
57 #define PROF_UNIQ_NAME __FILE__ "_" PROF_STRINGIZE1(__LINE__)
58 
59 #define PROF_SAFE_DELETE(ptr) { if (ptr) { delete ptr; ptr = nullptr; } }
60 
61 #define PROF_NO_COPY(type_)\
62  private:\
63  type_(const type_&); \
64  void operator=(const type_&)
65 
66 // HiresTimer
67 
68 class HiresTimer
69 {
70 public:
71  static int Init();
72  static double GetTicks();
73 };
74 
75 // ProfilerSection
76 
77 class ProfilerSection
78 {
79  PROF_NO_COPY(ProfilerSection);
80 
81  friend class Profiler;
82  friend class ScopedProfiler;
83 
84 private:
85 
86  inline ProfilerSection(const PROF_CHAR* name, const PROF_CHAR* uniq) :
87  m_name(name), m_uniq(uniq), m_sum(0), m_count(0) {}
88 
89  inline void Update(double delta) { m_sum += delta, m_count += 1; }
90  inline void ResetCounters() { m_sum = 0, m_count = 0; }
91 
92  inline const PROF_CHAR* GetName() const { return m_name; }
93  inline const PROF_CHAR* GetUniq() const { return m_uniq; }
94  inline double GetSum() const { return m_sum; }
95  inline unsigned int GetCount() const { return m_count; }
96  inline double GetAvg() const { return (m_count > 0 ? (m_sum / (double)m_count) : 0); }
97 
98 private:
99 
100  const PROF_CHAR* m_name;
101  const PROF_CHAR* m_uniq;
102  double m_sum;
103  unsigned int m_count;
104 };
105 
106 // Profiler
107 
108 class Profiler
109 {
110  PROF_NO_COPY(Profiler);
111 
112 public:
113 
114  enum SortMode
115  {
116  SORT_SUM = 0,
117  SORT_AVG
118  };
119 
120  static void InitInstance();
121  static void DestroyInstance();
122  static Profiler* GetInstance();
123 
124  Profiler() {}
125  ~Profiler() { Release(); }
126 
127  ProfilerSection* BeginSection(const PROF_CHAR* name, const PROF_CHAR* uniq);
128  void EndSection(ProfilerSection* section, double delta);
129  void ResetCounters();
130 
131  void LogSummary(SortMode sortMode = SORT_SUM);
132  void Release();
133 
134 private:
135 
136  PROF_MUTEX_DECL;
137 
138  typedef PROF_MAP_IMPL<const PROF_CHAR*, ProfilerSection*> SectionMap;
139  SectionMap m_sections;
140 };
141 
142 // ScopedProfiler
143 
144 class ScopedProfiler
145 {
146  PROF_NO_COPY(ScopedProfiler);
147 
148 public:
149 
150  inline ScopedProfiler(const PROF_CHAR* name, const PROF_CHAR* uniq)
151  {
152  m_section = Profiler::GetInstance()->BeginSection(name, uniq);
153  m_ticks = HiresTimer::GetTicks();
154  }
155 
156  inline ~ScopedProfiler()
157  {
158  const double ticks = HiresTimer::GetTicks();
159  const double delta = (ticks - m_ticks);
160  Profiler::GetInstance()->EndSection(m_section, delta);
161  }
162 
163 private:
164 
165  ProfilerSection* m_section;
166  double m_ticks;
167 };
168 
169 #endif // PROF_ENABLED
GLuint const GLchar * name


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39