BeefPerf.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #pragma once
  2. #ifndef BP_NOINC
  3. #include "../Common.h"
  4. #include "CritSect.h"
  5. #endif
  6. #include "Dictionary.h"
  7. #include "TLSingleton.h"
  8. #ifdef BF_PLATFORM_WINDOWS
  9. #include <winsock.h>
  10. #else
  11. #include <sys/types.h> /* See NOTES */
  12. #include <sys/socket.h>
  13. typedef int SOCKET;
  14. #endif
  15. enum BpConnectState
  16. {
  17. BpConnectState_NotConnected,
  18. BpConnectState_Connecting,
  19. BpConnectState_Connected,
  20. BpConnectState_Failed
  21. };
  22. #ifndef BP_DISABLED
  23. NS_BF_BEGIN
  24. class Buffer
  25. {
  26. public:
  27. uint8* mPtr;
  28. int mBufSize;
  29. int mDataSize;
  30. public:
  31. Buffer();
  32. ~Buffer();
  33. uint8* Alloc(int size);
  34. void Free();
  35. void Clear();
  36. };
  37. class CircularBuffer
  38. {
  39. public:
  40. // Either points to an internal segment of the CircularBuffer, or if the range is discontinuous then
  41. // it presents a temporary pointer that is later committed
  42. class View
  43. {
  44. public:
  45. uint8* mPtr;
  46. int mSrcIdx;
  47. int mSrcSize;
  48. uint8* mTempBuf;
  49. int mTempBufSize;
  50. CircularBuffer* mCircularBuffer;
  51. public:
  52. View();
  53. ~View();
  54. void Commit(int size = -1);
  55. };
  56. public:
  57. uint8* mBuffer;
  58. int mTail;
  59. int mDataSize;
  60. int mBufSize;
  61. public:
  62. CircularBuffer();
  63. ~CircularBuffer();
  64. void Clear();
  65. void Resize(int newSize);
  66. void GrowReserve(int addSize);
  67. void Grow(int addSize);
  68. void GrowFront(int addSize);
  69. int GetSize();
  70. void MapView(int idx, int len, View& view);
  71. void Read(void* ptr, int idx, int len);
  72. void Write(void* ptr, int idx, int len);
  73. void RemoveFront(int len);
  74. };
  75. class BpContext
  76. {
  77. };
  78. enum BpResult
  79. {
  80. BpResult_Ok = 0,
  81. BpResult_InternalError,
  82. BpResult_AlreadyInitialized
  83. };
  84. #define BP_CHUNKSIZE 4096
  85. #define BP_NUMCHUNKS 8
  86. #define BP_CLIENT_VERSION 2
  87. enum BpCmd
  88. {
  89. BpCmd_Init,
  90. BpCmd_Enter,
  91. BpCmd_EnterDyn,
  92. BpCmd_Leave,
  93. BpCmd_LODSmallEntry,
  94. BpCmd_StrEntry,
  95. BpCmd_ThreadAdd,
  96. BpCmd_ThreadRemove,
  97. BpCmd_SetThread,
  98. BpCmd_ThreadName,
  99. BpCmd_Tick,
  100. BpCmd_PrevTick,
  101. BpCmd_KeepAlive,
  102. BpCmd_ClockInfo,
  103. BpCmd_StreamSplitInfo,
  104. BpCmd_Event,
  105. BpCmd_LODEvent,
  106. BpCmd_Cmd
  107. };
  108. class BpCmdTarget
  109. {
  110. public:
  111. CritSect mCritSect;
  112. Buffer mOutBuffer;
  113. char* mThreadName;
  114. int mCurDynStrIdx;
  115. const char* mDynStrs[64];
  116. int mCurDepth;
  117. public:
  118. BpCmdTarget();
  119. void Disable();
  120. // Note: returned "const char*" is actually an index into a temporary string table
  121. const char* DynamicString(const char* str);
  122. const char* ToStrPtr(const char* str);
  123. void Enter(const char* name);
  124. void Enter(const char* name, va_list vargs);
  125. void EnterF(const char* name, ...);
  126. void Leave();
  127. void Event(const char* name, const char* details);
  128. };
  129. class BpRootCmdTarget : public BpCmdTarget
  130. {
  131. public:
  132. void Init();
  133. void Tick();
  134. void KeepAlive();
  135. void AddThread(int threadId, BfpThreadId nativeThreadId);
  136. };
  137. class BpThreadInfo : public BpCmdTarget
  138. {
  139. public:
  140. BfpThreadId mNativeThreadId;
  141. int mThreadId;
  142. bool mReadyToSend;
  143. bool mHasTerminated;
  144. public:
  145. BpThreadInfo();
  146. ~BpThreadInfo();
  147. void SetThreadName(const char* name);
  148. void RemoveThread();
  149. };
  150. struct BpZoneName
  151. {
  152. public:
  153. int mIdx;
  154. int mSize;
  155. };
  156. class BpManager
  157. {
  158. public:
  159. #ifdef BF_PLATFORM_WINDOWS
  160. HANDLE mMutex;
  161. HANDLE mSharedMemoryFile;
  162. #endif
  163. SOCKET mSocket;
  164. BfpThread* mThread;
  165. String mServerName;
  166. String mSessionName;
  167. String mSessionID;
  168. BpRootCmdTarget mRootCmdTarget;
  169. BpConnectState mConnectState;
  170. SyncEvent mShutdownEvent;
  171. CritSect mCritSect;
  172. Array<BpThreadInfo*> mThreadInfos;
  173. String mClientName;
  174. TLSDtor mTLSDtor;
  175. bool mCollectData;
  176. bool mThreadRunning;
  177. int64 mInitTimeStamp;
  178. uint mInitTickCount;
  179. int mPauseCount;
  180. int mInitCount;
  181. CircularBuffer mOutBuffer;
  182. int mOutBlockSizeLeft;
  183. int mCurThreadId;
  184. Dictionary<const char*, BpZoneName> mZoneNameMap;
  185. BF_TLS_DECLSPEC static BpThreadInfo* sBpThreadInfo;
  186. static BpManager* sBpManager;
  187. int64 mCurTick;
  188. protected:
  189. bool Connect();
  190. void FinishWorkThread();
  191. void ThreadProc();
  192. static void BFP_CALLTYPE ThreadProcThunk(void* ptr);
  193. uint8* StartCmd(uint8 cmd, CircularBuffer::View& view, int maxLen);
  194. void EndCmd(CircularBuffer::View& view, uint8* ptr);
  195. void TrySendData();
  196. void LostConnection();
  197. virtual BpThreadInfo* SlowGetCurThreadInfo();
  198. public:
  199. virtual void* AllocBytes(int size);
  200. virtual void FreeBytes(void*);
  201. public:
  202. BpManager();
  203. ~BpManager();
  204. void Clear();
  205. void SetClientName(const StringImpl& clientName);
  206. BpResult Init(const char* serverName, const char* sessionName);
  207. void RetryConnect();
  208. void Pause();
  209. void Unpause();
  210. void Shutdown();
  211. bool IsDisconnected();
  212. BpContext* CreateContext(const char* name);
  213. void CloseContext();
  214. void Tick();
  215. static BpThreadInfo* GetCurThreadInfo();
  216. static BpManager* Get();
  217. };
  218. NS_BF_END
  219. class BpAutoZone
  220. {
  221. public:
  222. BpAutoZone(const char* name)
  223. {
  224. Beefy::BpManager::GetCurThreadInfo()->Enter(name);
  225. }
  226. ~BpAutoZone()
  227. {
  228. Beefy::BpManager::sBpThreadInfo->Leave();
  229. }
  230. };
  231. class BpAutoZoneF
  232. {
  233. public:
  234. BpAutoZoneF(const char* name, ...)
  235. {
  236. va_list args;
  237. va_start(args, name);
  238. Beefy::BpManager::GetCurThreadInfo()->Enter(name, args);
  239. }
  240. ~BpAutoZoneF()
  241. {
  242. Beefy::BpManager::sBpThreadInfo->Leave();
  243. }
  244. };
  245. #define BP_ENTER(zoneName) BpEnter(name)
  246. #define BP_DYN_STR(str) BpDynStr(str)
  247. // We purposely 'incorrectly' define BP_ZONE as a variadic macro so compilation will fail rather than warn if we attempt to add params
  248. #define BP_TOKENPASTE(x, y) x ## y
  249. #define BP_TOKENPASTE2(x, y) BP_TOKENPASTE(x, y)
  250. #define BP_ZONE(zoneName, ...) BpAutoZone BP_TOKENPASTE2(bpZone_, __COUNTER__)(zoneName, ##__VA_ARGS__)
  251. #define BP_ZONE_F(zoneName, ...) BpAutoZoneF BP_TOKENPASTE2(bpZone_, __COUNTER__)(zoneName, ##__VA_ARGS__)
  252. #define BP_SHUTDOWN() BpShutdown()
  253. #else
  254. #define BP_ENTER(zoneName) do { } while (0)
  255. #define BP_DYN_STR(str) str
  256. #define BP_ZONE(zoneName, ...) do { } while (0)
  257. #define BP_ZONE_F(zoneName, ...) do { } while (0)
  258. #define BP_SHUTDOWN() do { } while (0)
  259. #endif
  260. #ifdef BP_DYNAMIC
  261. #define BP_EXPORT BF_EXPORT
  262. #define BP_CALLTYPE BF_CALLTYPE
  263. #else
  264. #define BP_EXPORT
  265. #define BP_CALLTYPE
  266. #endif
  267. BP_EXPORT void BP_CALLTYPE BpInit(const char* serverName, const char* sessionName);
  268. BP_EXPORT void BP_CALLTYPE BpShutdown();
  269. BP_EXPORT BpConnectState BP_CALLTYPE BpGetConnectState();
  270. BP_EXPORT void BP_CALLTYPE BpRetryConnect();
  271. BP_EXPORT void BP_CALLTYPE BpPause();
  272. BP_EXPORT void BP_CALLTYPE BpUnpause();
  273. BP_EXPORT void BP_CALLTYPE BpSetClientName(const char* clientName);
  274. BP_EXPORT void BP_CALLTYPE BpSetThreadName(const char* threadName);
  275. BP_EXPORT void BP_CALLTYPE BpEnter(const char* zoneName);
  276. BP_EXPORT void BP_CALLTYPE BpEnterF(const char* zoneName, ...);
  277. BP_EXPORT void BP_CALLTYPE BpLeave();
  278. BP_EXPORT void BP_CALLTYPE BpFrameTick();
  279. BP_EXPORT void BP_CALLTYPE BpEvent(const char* name, const char* details);
  280. BP_EXPORT const char* BP_CALLTYPE BpDynStr(const char* str);