Profiler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #pragma once
  2. #include "DebugCommon.h"
  3. #include "WinDebugger.h"
  4. #include "BeefySysLib/util/CritSect.h"
  5. #include "BeefySysLib/util/Hash.h"
  6. #include "BeefySysLib/util/BumpAllocator.h"
  7. NS_BF_DBG_BEGIN
  8. template <typename T>
  9. class ProfileDataList
  10. {
  11. public:
  12. typedef T data_type;
  13. public:
  14. T* mData;
  15. int mSize;
  16. bool operator==(const ProfileDataList& rhs)
  17. {
  18. if (mSize != rhs.mSize)
  19. return false;
  20. return memcmp(mData, rhs.mData, mSize * sizeof(T)) == 0;
  21. }
  22. };
  23. template <typename T>
  24. struct ProfileDataListHash
  25. {
  26. size_t operator()(const ProfileDataList<T>& val) const
  27. {
  28. return (size_t)Beefy::Hash64((void*)val.mData, val.mSize * sizeof(T));
  29. }
  30. };
  31. template <typename T>
  32. struct ProfileDataListEquals
  33. {
  34. bool operator()(const ProfileDataList<T>& lhs, const ProfileDataList<T>& rhs) const
  35. {
  36. if (lhs.mSize != rhs.mSize)
  37. return false;
  38. return memcmp(lhs.mData, rhs.mData, lhs.mSize * sizeof(T)) == 0;
  39. }
  40. };
  41. class ProfileAddrEntry : public ProfileDataList<addr_target>
  42. {
  43. public:
  44. //int mSampleCount;
  45. int mEntryIdx;
  46. public:
  47. ProfileAddrEntry()
  48. {
  49. mEntryIdx = -1;
  50. //mSampleCount = 0;
  51. }
  52. };
  53. //typedef std::unordered_set<ProfileAddrEntry, ProfileDataListHash<addr_target>, ProfileDataListEquals<addr_target> > ProfileAddrEntrySet;
  54. typedef HashSet<ProfileAddrEntry> ProfileAddrEntrySet;
  55. class ProfileProcId
  56. {
  57. public:
  58. String mProcName;
  59. bool mIsIdle;
  60. };
  61. class ProfileProdIdEntry
  62. {
  63. public:
  64. ProfileProcId* mProcId;
  65. bool operator==(const ProfileProdIdEntry& rhs)
  66. {
  67. return rhs.mProcId->mProcName == mProcId->mProcName;
  68. }
  69. };
  70. class ProfileProcEntry : public ProfileDataList<ProfileProcId*>
  71. {
  72. public:
  73. int mSampleCount;
  74. int mEntryIdx;
  75. bool mUsed;
  76. public:
  77. ProfileProcEntry()
  78. {
  79. mEntryIdx = -1;
  80. mSampleCount = 0;
  81. mUsed = false;
  82. }
  83. };
  84. //typedef std::unordered_set<ProfileProcEntry, ProfileDataListHash<ProfileProcId*>, ProfileDataListEquals<ProfileProcId*> > ProfileProcEntrySet;
  85. typedef HashSet<ProfileProcEntry> ProfileProcEntrySet;
  86. class ProfileThreadInfo
  87. {
  88. public:
  89. Array<int> mProfileAddrEntries;
  90. int mTotalSamples;
  91. int mTotalIdleSamples;
  92. String mName;
  93. public:
  94. ProfileThreadInfo()
  95. {
  96. mTotalSamples = 0;
  97. mTotalIdleSamples = 0;
  98. }
  99. };
  100. class DbgProfiler : public Profiler
  101. {
  102. public:
  103. WinDebugger* mDebugger;
  104. volatile bool mIsRunning;
  105. bool mWantsClear;
  106. SyncEvent mShutdownEvent;
  107. bool mNeedsProcessing;
  108. uint32 mStartTick;
  109. uint32 mEndTick;
  110. int mTotalVirtualSamples;
  111. int mTotalActualSamples;
  112. int mTotalActiveSamplingMS;
  113. BumpAllocator mAlloc;
  114. Dictionary<uint, ProfileThreadInfo*> mThreadInfo;
  115. Array<uint> mThreadIdList;
  116. Dictionary<addr_target, bool> mStackHeadCheckMap;
  117. ProfileAddrEntrySet mProfileAddrEntrySet;
  118. Array<ProfileAddrEntry*> mProfileAddrEntries;
  119. Array<ProfileAddrEntry> mPendingProfileEntries;
  120. ProfileProcEntrySet mProfileProcEntrySet;
  121. Array<ProfileProcEntry*> mProfileProcEntries;
  122. Array<int> mProfileAddrToProcMap;
  123. Dictionary<void*, ProfileProcId*> mProcMap; // Keyed on either DwSubprogram or DwSymbol. Multiple pointers can reference the same ProfileProcId (in the case of inlined functions, for example)
  124. HashSet<ProfileProdIdEntry> mUniqueProcSet;
  125. HashSet<String> mIdleSymbolNames;
  126. public:
  127. void ThreadProc();
  128. void AddEntries(String& str, Array<ProfileProcEntry*>& procEntries, int rangeStart, int rangeEnd, int stackIdx, ProfileProcId* findProc);
  129. template <typename T>
  130. typename T::key_type* AddToSet(T& map, typename T::key_type::data_type* data, int size)
  131. {
  132. typedef T::key_type::data_type DataType;
  133. typename T::key_type entry;
  134. entry.mData = data;
  135. entry.mSize = size;
  136. typename T::key_type* entryPtr;
  137. if (map.TryAdd(entry, &entryPtr))
  138. {
  139. entryPtr->mData = (DataType*)mAlloc.AllocBytes(sizeof(DataType) * size);
  140. memcpy(entryPtr->mData, data, sizeof(DataType) * size);
  141. }
  142. return entryPtr;
  143. }
  144. public:
  145. void HandlePendingEntries();
  146. void Process();
  147. void DoClear();
  148. ProfileProcId* Get(const StringImpl& str, bool* outIsNew = NULL);
  149. public:
  150. DbgProfiler(WinDebugger* debugger);
  151. ~DbgProfiler();
  152. void Start() override;
  153. void Stop() override;
  154. void Clear() override;
  155. bool IsSampling() override { return mIsRunning; }
  156. String GetOverview() override;
  157. String GetThreadList() override;
  158. String GetCallTree(int threadId, bool reverse) override;
  159. };
  160. NS_BF_DBG_END
  161. namespace std
  162. {
  163. template <>
  164. struct hash<NS_BF_DBG::ProfileProdIdEntry>
  165. {
  166. size_t operator()(const NS_BF_DBG::ProfileProdIdEntry& entry) const
  167. {
  168. return std::hash<Beefy::String>()(entry.mProcId->mProcName);
  169. }
  170. };
  171. template <>
  172. struct hash<NS_BF_DBG::ProfileProcEntry>
  173. {
  174. size_t operator()(const NS_BF_DBG::ProfileProcEntry& val) const
  175. {
  176. return (size_t)Beefy::Hash64((void*)val.mData, val.mSize * sizeof(NS_BF_DBG::ProfileProcId*));
  177. }
  178. };
  179. template <>
  180. struct hash<NS_BF_DBG::ProfileAddrEntry>
  181. {
  182. size_t operator()(const NS_BF_DBG::ProfileAddrEntry& val) const
  183. {
  184. return (size_t)Beefy::Hash64((void*)val.mData, val.mSize * sizeof(addr_target));
  185. }
  186. };
  187. template <typename T>
  188. struct hash<NS_BF_DBG::ProfileDataList<T> >
  189. {
  190. size_t operator()(const NS_BF_DBG::ProfileDataList<T>& val) const
  191. {
  192. return (size_t)Beefy::Hash64((void*)val.mData, val.mSize * sizeof(T));
  193. }
  194. };
  195. }