47 #ifndef _MIRA_PROFILER_H_ 48 #define _MIRA_PROFILER_H_ 50 #ifdef _MSC_VER // Microsoft Visual C++ 52 #pragma intrinsic(__rdtsc) 59 #include <boost/optional.hpp> 73 #ifdef MIRA_PROFILER_ENABLED 74 # define MIRA_PROFILE_BEGIN(id) _MIRA_PROFILE_BEGIN(id) 75 # define MIRA_PROFILE_END(id) _MIRA_PROFILE_END (id) 76 # define MIRA_PROFILE_SCOPE(id) _MIRA_PROFILE_SCOPE(id) 78 # define MIRA_PROFILE_BEGIN(id) 79 # define MIRA_PROFILE_END(id) 80 # define MIRA_PROFILE_SCOPE(id) 84 #ifndef MIRA_PROFILER_LEVEL 85 # define MIRA_PROFILER_LEVEL 3 88 #if MIRA_PROFILER_LEVEL >= 1 89 # define MIRA_PROFILE_BEGIN1(id) MIRA_PROFILE_BEGIN(id) 90 # define MIRA_PROFILE_END1(id) MIRA_PROFILE_END(id) 91 # define MIRA_PROFILE_SCOPE1(id) MIRA_PROFILE_SCOPE(id) 93 # define MIRA_PROFILE_BEGIN1(id) 94 # define MIRA_PROFILE_END1(id) 95 # define MIRA_PROFILE_SCOPE1(id) 98 #if MIRA_PROFILER_LEVEL >= 2 99 # define MIRA_PROFILE_BEGIN2(id) MIRA_PROFILE_BEGIN(id) 100 # define MIRA_PROFILE_END2(id) MIRA_PROFILE_END(id) 101 # define MIRA_PROFILE_SCOPE2(id) MIRA_PROFILE_SCOPE(id) 103 # define MIRA_PROFILE_BEGIN2(id) 104 # define MIRA_PROFILE_END2(id) 105 # define MIRA_PROFILE_SCOPE2(id) 108 #if MIRA_PROFILER_LEVEL >= 3 109 # define MIRA_PROFILE_BEGIN3(id) MIRA_PROFILE_BEGIN(id) 110 # define MIRA_PROFILE_END3(id) MIRA_PROFILE_END(id) 111 # define MIRA_PROFILE_SCOPE3(id) MIRA_PROFILE_SCOPE(id) 113 # define MIRA_PROFILE_BEGIN3(id) 114 # define MIRA_PROFILE_END3(id) 115 # define MIRA_PROFILE_SCOPE3(id) 121 #define __MIRA_PROFILE_BEGIN(id) \ 122 static mira::Profiler::Node* _node_##id = \ 123 mira::Profiler::instance().newProfileNode(#id,__FILE__, __LINE__); \ 124 mira::Profiler::Node* _parent_##id = \ 125 mira::Profiler::instance().beginHierarchy(_node_##id,__FILE__, __LINE__);\ 126 uint64 _start_##id = mira::Profiler::getCycleCount(); 128 #define _MIRA_PROFILE_BEGIN(id) __MIRA_PROFILE_BEGIN(id) 130 #define __MIRA_PROFILE_END(id) \ 131 uint64 _duration_##id = mira::Profiler::getCycleCount() - _start_##id; \ 132 mira::Profiler::instance().endHierarchy(_node_##id, _parent_##id, \ 133 _duration_##id, __FILE__, __LINE__); 135 #define _MIRA_PROFILE_END(id) __MIRA_PROFILE_END(id) 137 #define __MIRA_PROFILE_SCOPE(id) \ 138 static mira::Profiler::Node* _node_##id = \ 139 mira::Profiler::instance().newProfileNode(#id,__FILE__, __LINE__); \ 140 mira::Profiler::Scope _scope_##id(_node_##id,__FILE__, __LINE__); 142 #define _MIRA_PROFILE_SCOPE(id) __MIRA_PROFILE_SCOPE(id) 217 ChildLink() : count(0), totalCycles(0), avgCycles(0.0), M2cycles(0.0), child(NULL) {}
232 Node() : id(-1), filename(NULL), line(0), count(0), totalCycles(0), avgCycles(0.0), M2cycles(0.0) {}
241 const char* filename;
256 std::vector<ChildLink> children;
273 const std::string& dotCommand=
"",
274 const std::string& imgFormat=
"png") {
286 double getCPUSpeed();
294 static uint64 getCycleCount();
305 Node* newProfileNode(
const std::string& name,
306 const char* filename, uint32 line);
311 Node* beginHierarchy(Node* node,
const char* filename, uint32 line);
317 void endHierarchy(Node* node, Node* prevNode, uint64 cycles,
318 const char* filename, uint32 line);
328 Scope(Node* node,
const char* filename, uint32 line) :
329 mNode(node), mFilename(filename), mLine(line),
330 mParent(instance().beginHierarchy(node,filename, line)),
331 mStart(getCycleCount()) {}
335 uint64 duration = getCycleCount() - mStart;
336 instance().endHierarchy(mNode, mParent, duration, mFilename, mLine);
341 const char* mFilename;
349 typedef std::map<std::string, Node*> NodeMap;
352 boost::optional<double> mCPUSpeed;
360 int32 mNextProfileID;
363 friend struct ThreadInfo;
365 typedef boost::shared_ptr<ThreadInfo> ThreadInfoPtr;
366 boost::thread_specific_ptr<ThreadInfoPtr> mThreadInfo;
367 std::vector<ThreadInfoPtr> mThreads;
369 static void threadInfoCleanupFn(ThreadInfo* t);
371 ThreadInfo* getThreadInfo();
375 void writeReportInternal(
const std::string& directory,
376 std::string dotCommand,
377 const std::string& imgFormat);
379 ReportNode* buildReportNode(Report* report,
const Node* node);
380 void buildThreadReport(Report* report,
const Node* node);
390 return cycles = __rdtsc();
395 mov DWORD PTR[cycles] , eax
396 mov DWORD PTR[cycles+4], edx
402 # if defined(MIRA_ARCH_X86) 403 # if defined(MIRA_ARCH64) 406 asm volatile (
"rdtsc" :
"=a"(lo),
"=d"(hi));
407 return ( (uint64)lo) | (((uint64)hi)<<32 );
411 asm volatile (
"rdtsc" :
"=A" (cycles));
414 # elif defined(MIRA_ARCH_ARM) 415 # if defined(MIRA_ARCH64) 418 asm volatile (
"mrs %0, cntvct_el0" :
"=r" (cycles));
Typedefs for OS independent basic data types.
void setCPUSpeed(double speed)
Overwrites the calibrated CPU speed with the specified value (in Hz!).
Definition: Profiler.h:289
Implementation of Spinlock.
~Scope()
Definition: Profiler.h:333
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:544
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
static void writeReport(const std::string &directory, const std::string &dotCommand="", const std::string &imgFormat="png")
Definition: Profiler.h:272
Time and Duration wrapper class.
Provided for convenience.
Definition: Singleton.h:572
Scope(Node *node, const char *filename, uint32 line)
Definition: Profiler.h:328
The main Profiler class.
Definition: Profiler.h:206
Includes, defines and functions for threads.
A singleton class that can be freely configured using policies that control the creation, instantiation, lifetime and thread-safety.
Definition: Profiler.h:326
static uint64 getCycleCount()
Definition: Profiler.h:385
A spinlock is similar to a mutex and allows thread synchronization of critical sections.
Definition: Spinlock.h:68