AL_USDMaya  0.29.4
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CodeTimings.h
1 //
2 // Copyright 2017 Animal Logic
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.//
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #pragma once
17 #include <unordered_map>
18 #include <string>
19 #include <ostream>
20 #include <ctime>
21 #include <cassert>
22 #include <stdint.h>
23 #include <time.h>
24 
25 namespace AL {
26 namespace usdmaya {
27 
28 //----------------------------------------------------------------------------------------------------------------------
31 //----------------------------------------------------------------------------------------------------------------------
32 const uint32_t MAX_TIMESTAMP_STACK_SIZE = 16;
33 
34 //----------------------------------------------------------------------------------------------------------------------
37 //----------------------------------------------------------------------------------------------------------------------
39 {
40  friend class Profiler;
41 public:
42 
48  const std::string sectionName,
49  const std::string filePath,
50  const size_t lineNumber)
51  : m_sectionName(sectionName), m_filePath(filePath), m_lineNumber(lineNumber),
52  m_hash(((std::hash<std::string>()(filePath) << 1) ^ (std::hash<size_t>()(lineNumber) << 1)) ^ std::hash<std::string>()(sectionName))
53  {}
54 
58  inline bool operator == (const ProfilerSectionTag& rhs) const
59  {
60  return m_hash == rhs.m_hash &&
61  m_sectionName == rhs.m_sectionName &&
62  m_filePath == rhs.m_filePath &&
63  m_lineNumber == rhs.m_lineNumber;
64  }
65 
68  inline size_t hash() const
69  { return m_hash;}
70 
71 private:
72  const std::string m_sectionName;
73  const std::string m_filePath;
74  const size_t m_lineNumber;
75  const size_t m_hash;
76 };
77 
78 //----------------------------------------------------------------------------------------------------------------------
99 //----------------------------------------------------------------------------------------------------------------------
101 {
102  friend class Profiler;
103 public:
104 
108  inline ProfilerSectionPath(const AL::usdmaya::ProfilerSectionTag* const top, const ProfilerSectionPath* const parent = 0)
109  : m_top(top), m_parent(parent), m_hash(m_top->hash() ^ (m_parent ? m_parent->hash() : 0))
110  {}
111 
115  inline bool operator == (const ProfilerSectionPath& rhs) const
116  { return (m_hash == rhs.m_hash && m_top == rhs.m_top && m_parent == rhs.m_parent); }
117 
120  inline size_t hash() const
121  { return m_hash;}
122 
123 private:
124  const ProfilerSectionTag* const m_top;
125  const ProfilerSectionPath* const m_parent;
126  const size_t m_hash;
127 };
128 } // usdmaya
129 } // AL
130 
131 //----------------------------------------------------------------------------------------------------------------------
132 #ifndef AL_GENERATING_DOCS
133 namespace std {
134 template<> struct hash<AL::usdmaya::ProfilerSectionTag> {
135  inline size_t operator()(const AL::usdmaya::ProfilerSectionTag& k) const {
136  return k.hash();
137  }
138 };
139 template<> struct hash<AL::usdmaya::ProfilerSectionPath> {
140  inline size_t operator()(const AL::usdmaya::ProfilerSectionPath& k) const {
141  return k.hash();
142  }
143 };
144 } // std
145 #endif
146 
147 //----------------------------------------------------------------------------------------------------------------------
148 namespace AL {
149 namespace usdmaya {
150 //----------------------------------------------------------------------------------------------------------------------
183 //----------------------------------------------------------------------------------------------------------------------
184 class Profiler
185 {
186 public:
187 
190  static void printReport(std::ostream& os);
191 
193  static inline void clearAll()
194  {
195  assert(m_stackPos == 0);
196  m_map.clear();
197  }
198 
201  static void pushTime(const ProfilerSectionTag* entry);
202 
204  static void popTime();
205 
206 private:
207 
208  typedef std::unordered_map<ProfilerSectionPath, timespec> ProfilerSectionPathLUT;
209  typedef ProfilerSectionPathLUT::const_iterator iter_t;
210  static inline bool compareTimeStamps(const iter_t, const iter_t);
211  static void print(std::ostream& os, iter_t, const ProfilerSectionPathLUT&, uint32_t, double);
212 
213  struct ProfilerSectionStackNode
214  {
215  timespec m_time;
216  const ProfilerSectionTag* m_entry;
217  ProfilerSectionPathLUT::iterator m_path;
218  };
219 
220  static inline timespec timeDiff(const timespec startTime, const timespec endTime)
221  {
222  timespec ts;
223  if (endTime.tv_nsec < startTime.tv_nsec)
224  {
225  ts.tv_sec = endTime.tv_sec - 1 - startTime.tv_sec;
226  ts.tv_nsec = 1000000000 + endTime.tv_nsec - startTime.tv_nsec;
227  }
228  else
229  {
230  ts.tv_sec = endTime.tv_sec - startTime.tv_sec;
231  ts.tv_nsec = endTime.tv_nsec - startTime.tv_nsec;
232  }
233  return ts;
234  }
235  static inline timespec timeAdd(timespec t1, timespec t2)
236  {
237  timespec ts;
238  int32_t sec = t2.tv_sec + t1.tv_sec;
239  int32_t nsec = t2.tv_nsec + t1.tv_nsec;
240  if (nsec >= 1000000000)
241  {
242  nsec -= 1000000000;
243  sec++;
244  }
245  ts.tv_sec = sec;
246  ts.tv_nsec = nsec;
247  return ts;
248  }
249 
250  static ProfilerSectionStackNode m_timeStack[MAX_TIMESTAMP_STACK_SIZE];
251  static uint32_t m_stackPos;
252  static ProfilerSectionPathLUT m_map;
253 };
254 
255 //----------------------------------------------------------------------------------------------------------------------
256 } // usdmaya
257 } // AL
258 //----------------------------------------------------------------------------------------------------------------------
259 
262 #define AL_BEGIN_PROFILE_SECTION(TimedSection) \
263  { \
264  static const AL::usdmaya::ProfilerSectionTag __entry(#TimedSection, __FILE__, __LINE__); \
265  AL::usdmaya::Profiler::pushTime(&__entry); \
266  }
267 
270 #define AL_END_PROFILE_SECTION() \
271  { AL::usdmaya::Profiler::popTime(); }
272 
273 
size_t hash() const
return the hash of this class
Definition: CodeTimings.h:68
static void pushTime(const ProfilerSectionTag *entry)
do not call directly. Use the AL_BEGIN_PROFILE_SECTION macro
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:120
This class provides a static hash that should be unique for a line within a specific function...
Definition: CodeTimings.h:38
ProfilerSectionTag(const std::string sectionName, const std::string filePath, const size_t lineNumber)
ctor
Definition: CodeTimings.h:47
static void printReport(std::ostream &os)
call to output the report
static void clearAll()
call to clear internal timers
Definition: CodeTimings.h:193
ProfilerSectionPath(const AL::usdmaya::ProfilerSectionTag *const top, const ProfilerSectionPath *const parent=0)
ctor
Definition: CodeTimings.h:108
bool operator==(const ProfilerSectionPath &rhs) const
equality operator
Definition: CodeTimings.h:115
const uint32_t MAX_TIMESTAMP_STACK_SIZE
Definition: CodeTimings.h:32
bool operator==(const ProfilerSectionTag &rhs) const
equality operator
Definition: CodeTimings.h:58
This class represents a path made up of AL::usdmaya::ProfilerSectionTag's. It is used so that we can ...
Definition: CodeTimings.h:100
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:184