3 #if defined(PROF_ENABLED) 5 #define PROF_OPTION_QPC 6 #define PROF_OPTION_THREADSAFE 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)) 13 #if defined(_WIN32) || defined(_WIN64) 14 #define PROF_PLATFORM_WINDOWS 20 #if !defined(PROF_PLATFORM_WINDOWS) 25 #if !defined(PROF_CHAR) 26 #define PROF_CHAR char 29 #if !defined(PROF_TEXT) 30 #define PROF_TEXT(str) str 33 #if !defined(PROF_LOG) 34 #define PROF_LOG printf 37 #if !defined(PROF_MAP_IMPL) 38 #define PROF_MAP_IMPL std::unordered_map 39 #include <unordered_map> 42 #if defined(PROF_OPTION_THREADSAFE) 43 #if !defined(PROF_MUTEX_IMPL) 45 #define PROF_MUTEX_IMPL std::mutex 46 #define PROF_SCOPED_MUTEX_IMPL std::lock_guard<std::mutex> 48 #define PROF_MUTEX_DECL PROF_MUTEX_IMPL m_mutex 49 #define PROF_LOCK PROF_SCOPED_MUTEX_IMPL _scoped(m_mutex) 51 #define PROF_MUTEX_DECL 55 #define PROF_STRINGIZE1(x) PROF_STRINGIZE2(x) 56 #define PROF_STRINGIZE2(x) #x 57 #define PROF_UNIQ_NAME __FILE__ "_" PROF_STRINGIZE1(__LINE__) 59 #define PROF_SAFE_DELETE(ptr) { if (ptr) { delete ptr; ptr = nullptr; } } 61 #define PROF_NO_COPY(type_)\ 63 type_(const type_&); \ 64 void operator=(const type_&) 72 static double GetTicks();
79 PROF_NO_COPY(ProfilerSection);
81 friend class Profiler;
82 friend class ScopedProfiler;
86 inline ProfilerSection(
const PROF_CHAR*
name,
const PROF_CHAR* uniq) :
87 m_name(name), m_uniq(uniq), m_sum(0), m_count(0) {}
89 inline void Update(
double delta) { m_sum += delta, m_count += 1; }
90 inline void ResetCounters() { m_sum = 0, m_count = 0; }
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); }
100 const PROF_CHAR* m_name;
101 const PROF_CHAR* m_uniq;
103 unsigned int m_count;
110 PROF_NO_COPY(Profiler);
120 static void InitInstance();
121 static void DestroyInstance();
122 static Profiler* GetInstance();
125 ~Profiler() { Release(); }
127 ProfilerSection* BeginSection(
const PROF_CHAR*
name,
const PROF_CHAR* uniq);
128 void EndSection(ProfilerSection*
section,
double delta);
129 void ResetCounters();
131 void LogSummary(SortMode sortMode = SORT_SUM);
138 typedef PROF_MAP_IMPL<const PROF_CHAR*, ProfilerSection*> SectionMap;
139 SectionMap m_sections;
146 PROF_NO_COPY(ScopedProfiler);
150 inline ScopedProfiler(
const PROF_CHAR*
name,
const PROF_CHAR* uniq)
152 m_section = Profiler::GetInstance()->BeginSection(name, uniq);
153 m_ticks = HiresTimer::GetTicks();
156 inline ~ScopedProfiler()
158 const double ticks = HiresTimer::GetTicks();
159 const double delta = (ticks - m_ticks);
160 Profiler::GetInstance()->EndSection(m_section, delta);
165 ProfilerSection* m_section;
169 #endif // PROF_ENABLED GLuint const GLchar * name