AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
CodeTimings.h
1 #pragma once
2 #include <unordered_map>
3 #include <string>
4 #include <ostream>
5 #include <ctime>
6 #include <cassert>
7 
8 namespace AL {
9 namespace maya {
10 
11 
12 //----------------------------------------------------------------------------------------------------------------------
15 //----------------------------------------------------------------------------------------------------------------------
16 const uint32_t MAX_TIMESTAMP_STACK_SIZE = 16;
17 
18 //----------------------------------------------------------------------------------------------------------------------
21 //----------------------------------------------------------------------------------------------------------------------
23 {
24  friend class Profiler;
25 public:
26 
32  const std::string sectionName,
33  const std::string filePath,
34  const size_t lineNumber)
35  : m_sectionName(sectionName), m_filePath(filePath), m_lineNumber(lineNumber),
36  m_hash(((std::hash<std::string>()(filePath) << 1) ^ (std::hash<size_t>()(lineNumber) << 1)) ^ std::hash<std::string>()(sectionName))
37  {}
38 
42  inline bool operator == (const ProfilerSectionTag& rhs) const
43  {
44  return m_hash == rhs.m_hash &&
45  m_sectionName == rhs.m_sectionName &&
46  m_filePath == rhs.m_filePath &&
47  m_lineNumber == rhs.m_lineNumber;
48  }
49 
52  inline size_t hash() const
53  { return m_hash;}
54 
55 private:
56  const std::string m_sectionName;
57  const std::string m_filePath;
58  const size_t m_lineNumber;
59  const size_t m_hash;
60 };
61 
62 //----------------------------------------------------------------------------------------------------------------------
83 //----------------------------------------------------------------------------------------------------------------------
85 {
86  friend class Profiler;
87 public:
88 
92  inline ProfilerSectionPath(const ProfilerSectionTag* const top, const ProfilerSectionPath* const parent = 0)
93  : m_top(top), m_parent(parent), m_hash(m_top->hash() ^ (m_parent ? m_parent->hash() : 0))
94  {}
95 
99  inline bool operator == (const ProfilerSectionPath& rhs) const
100  { return (m_hash == rhs.m_hash && m_top == rhs.m_top && m_parent == rhs.m_parent); }
101 
104  inline size_t hash() const
105  { return m_hash;}
106 
107 private:
108  const ProfilerSectionTag* const m_top;
109  const ProfilerSectionPath* const m_parent;
110  const size_t m_hash;
111 };
112 } // maya
113 } // AL
114 
115 //----------------------------------------------------------------------------------------------------------------------
116 #ifndef AL_GENERATING_DOCS
117 namespace std {
118 template<> struct hash<AL::maya::ProfilerSectionTag> {
119  inline size_t operator()(const AL::maya::ProfilerSectionTag& k) const {
120  return k.hash();
121  }
122 };
123 template<> struct hash<AL::maya::ProfilerSectionPath> {
124  inline size_t operator()(const AL::maya::ProfilerSectionPath& k) const {
125  return k.hash();
126  }
127 };
128 } // std
129 #endif
130 
131 //----------------------------------------------------------------------------------------------------------------------
132 namespace AL {
133 namespace maya {
134 //----------------------------------------------------------------------------------------------------------------------
167 //----------------------------------------------------------------------------------------------------------------------
168 class Profiler
169 {
170 public:
171 
174  static void printReport(std::ostream& os);
175 
177  static inline void clearAll()
178  {
179  assert(m_stackPos == 0);
180  m_map.clear();
181  }
182 
185  static void pushTime(const ProfilerSectionTag* entry);
186 
188  static void popTime();
189 
190 private:
191 
192  typedef std::unordered_map<ProfilerSectionPath, timespec> ProfilerSectionPathLUT;
193  typedef ProfilerSectionPathLUT::const_iterator iter_t;
194  static inline bool compareTimeStamps(const iter_t, const iter_t);
195  static void print(std::ostream& os, iter_t, const ProfilerSectionPathLUT&, uint32_t, double);
196 
197  struct ProfilerSectionStackNode
198  {
199  timespec m_time;
200  const ProfilerSectionTag* m_entry;
201  ProfilerSectionPathLUT::iterator m_path;
202  };
203 
204  static inline timespec timeDiff(const timespec startTime, const timespec endTime)
205  {
206  if (endTime.tv_nsec < startTime.tv_nsec)
207  {
208  return (timespec) {
209  tv_sec: endTime.tv_sec - 1 - startTime.tv_sec,
210  tv_nsec: 1000000000 + endTime.tv_nsec - startTime.tv_nsec
211  };
212  }
213  return (timespec) {
214  tv_sec: endTime.tv_sec - startTime.tv_sec,
215  tv_nsec: endTime.tv_nsec - startTime.tv_nsec
216  };
217  }
218  static inline timespec timeAdd(timespec t1, timespec t2)
219  {
220  int32_t sec = t2.tv_sec + t1.tv_sec;
221  int32_t nsec = t2.tv_nsec + t1.tv_nsec;
222  if (nsec >= 1000000000)
223  {
224  nsec -= 1000000000;
225  sec++;
226  }
227  return (timespec){ tv_sec: sec, tv_nsec: nsec };
228  }
229 
230  static ProfilerSectionStackNode m_timeStack[MAX_TIMESTAMP_STACK_SIZE];
231  static int32_t m_stackPos;
232  static ProfilerSectionPathLUT m_map;
233 };
234 
235 //----------------------------------------------------------------------------------------------------------------------
236 } // maya
237 } // AL
238 //----------------------------------------------------------------------------------------------------------------------
239 
242 #define AL_BEGIN_PROFILE_SECTION(TimedSection) \
243  { \
244  static const AL::maya::ProfilerSectionTag __entry(#TimedSection, __FILE__, __LINE__); \
245  AL::maya::Profiler::pushTime(&__entry); \
246  }
247 
250 #define AL_END_PROFILE_SECTION() \
251  { AL::maya::Profiler::popTime(); }
252 
253 
static void printReport(std::ostream &os)
call to output the report
This class implements a very simple incode profiler. This profiler is NOT thread safe. It is mainly used to get some basic stats on the where the bottlenecks are during a file import/export operation. A simple example of usage:
Definition: CodeTimings.h:168
const uint32_t MAX_TIMESTAMP_STACK_SIZE
Definition: CodeTimings.h:16
size_t hash() const
return the hash of this class
Definition: CodeTimings.h:104
bool operator==(const ProfilerSectionPath &rhs) const
equality operator
Definition: CodeTimings.h:99
This class provides a static hash that should be unique for a line within a specific function...
Definition: CodeTimings.h:22
ProfilerSectionPath(const ProfilerSectionTag *const top, const ProfilerSectionPath *const parent=0)
ctor
Definition: CodeTimings.h:92
ProfilerSectionTag(const std::string sectionName, const std::string filePath, const size_t lineNumber)
ctor
Definition: CodeTimings.h:31
static void pushTime(const ProfilerSectionTag *entry)
do not call directly. Use the AL_BEGIN_PROFILE_SECTION macro
static void clearAll()
call to clear internal timers
Definition: CodeTimings.h:177
This class represents a path made up of ProfilerSectionTag's. It is used so that we can distinguish b...
Definition: CodeTimings.h:84
static void popTime()
do not call directly. Use the AL_END_PROFILE_SECTION macro
size_t hash() const
return the hash of this class
Definition: CodeTimings.h:52
bool operator==(const ProfilerSectionTag &rhs) const
equality operator
Definition: CodeTimings.h:42