Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
7.2 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // ETW (Event Tracing for Windows) profiling helpers.
  4. // This allows easy insertion of Generic Event markers into ETW/xperf tracing
  5. // which then aids in analyzing the traces and finding performance problems.
  6. // The usage patterns are to use ETWBegin and ETWEnd (typically through the
  7. // convenience class CETWScope) to bracket time-consuming operations. In addition
  8. // ETWFrameMark marks the beginning of each frame, and ETWMark can be used to
  9. // mark other notable events. More event types and providers can be added as needed.
  10. // When recording xperf profiles add Valve-Main+Valve-FrameRate to the list of
  11. // user-mode providers and be sure to register the providers with this sequence
  12. // of commands:
  13. // xcopy /y game\bin\tier0.dll %temp%
  14. // wevtutil um src\tier0\ValveETWProvider.man
  15. // wevtutil im src\tier0\ValveETWProvider.man
  16. //
  17. //===============================================================================
  18. #ifndef ETWPROF_H
  19. #define ETWPROF_H
  20. #if defined( COMPILER_MSVC )
  21. #pragma once
  22. #endif
  23. #include "tier0/platform.h"
  24. #ifdef IS_WINDOWS_PC
  25. // ETW support should be compiled in for all Windows PC platforms. It isn't
  26. // supported on Windows XP but that is determined at run-time.
  27. #define ETW_MARKS_ENABLED
  28. #endif
  29. #ifdef ETW_MARKS_ENABLED
  30. // This returns true if VTrace is running and tracing is enabled.
  31. // More exactly it returns true if the VALVE_MAIN provider is enabled.
  32. PLATFORM_INTERFACE bool ETWIsTracingEnabled();
  33. // Insert a single event to mark a point in an ETW trace. The return value is a 64-bit
  34. // time stamp.
  35. PLATFORM_INTERFACE int64 ETWMark( const char *pMessage );
  36. // Optionally do full printf formatting of the mark string. This will be more expensive,
  37. // but only when tracing is enabled.
  38. PLATFORM_INTERFACE void ETWMarkPrintf( PRINTF_FORMAT_STRING const char *pMessage, ... ) FMTFUNCTION( 1, 2 );
  39. // Optionally specify one to four floats. They will show up in separate columns in
  40. // summary tables to allow sorting and easier transfer to spreadsheets.
  41. PLATFORM_INTERFACE void ETWMark1F( const char *pMessage, float data1 );
  42. PLATFORM_INTERFACE void ETWMark2F( const char *pMessage, float data1, float data2 );
  43. PLATFORM_INTERFACE void ETWMark3F( const char *pMessage, float data1, float data2, float data3 );
  44. PLATFORM_INTERFACE void ETWMark4F( const char *pMessage, float data1, float data2, float data3, float data4 );
  45. // Optionally specify one to four ints. They will show up in separate columns in
  46. // summary tables to allow sorting and easier transfer to spreadsheets.
  47. PLATFORM_INTERFACE void ETWMark1I( const char *pMessage, int data1 );
  48. PLATFORM_INTERFACE void ETWMark2I( const char *pMessage, int data1, int data2 );
  49. PLATFORM_INTERFACE void ETWMark3I( const char *pMessage, int data1, int data2, int data3 );
  50. PLATFORM_INTERFACE void ETWMark4I( const char *pMessage, int data1, int data2, int data3, int data4 );
  51. // Optionally specify one to two strings. They will show up in separate columns in
  52. // summary tables to allow sorting and easier transfer to spreadsheets.
  53. PLATFORM_INTERFACE void ETWMark1S( const char *pMessage, const char* data1 );
  54. PLATFORM_INTERFACE void ETWMark2S( const char *pMessage, const char* data1, const char* data2 );
  55. // Insert a begin event to mark the start of some work. The return value is a 64-bit
  56. // time stamp which should be passed to the corresponding ETWEnd function.
  57. PLATFORM_INTERFACE int64 ETWBegin( const char *pMessage );
  58. // Insert a paired end event to mark the end of some work.
  59. PLATFORM_INTERFACE int64 ETWEnd( const char *pMessage, int64 nStartTime );
  60. // Mark the start of the next render frame. bIsServerProcess must be passed
  61. // in consistently for a particular process.
  62. PLATFORM_INTERFACE void ETWRenderFrameMark( bool bIsServerProcess );
  63. // Mark the start of the next simulation frame. bIsServerProcess must be passed
  64. // in consistently for a particular process.
  65. PLATFORM_INTERFACE void ETWSimFrameMark( bool bIsServerProcess );
  66. // Return the frame number recorded in the ETW trace -- useful for synchronizing
  67. // other profile information to the ETW trace.
  68. PLATFORM_INTERFACE int ETWGetRenderFrameNumber();
  69. PLATFORM_INTERFACE void ETWMouseDown( int nWhichButton, int nX, int nY );
  70. PLATFORM_INTERFACE void ETWMouseUp( int nWhichButton, int nX, int nY );
  71. PLATFORM_INTERFACE void ETWMouseMove( int nX, int nY );
  72. PLATFORM_INTERFACE void ETWMouseWheel( int nWheelDelta, int nX, int nY );
  73. PLATFORM_INTERFACE void ETWKeyDown( int nScanCode, int nVirtualCode, const char *pChar );
  74. PLATFORM_INTERFACE void ETWSendPacket( const char *pTo, int nWireSize, int nOutSequenceNR, int nOutSequenceNrAck );
  75. PLATFORM_INTERFACE void ETWThrottled();
  76. PLATFORM_INTERFACE void ETWReadPacket( const char *pFrom, int nWireSize, int nInSequenceNR, int nOutSequenceNRAck );
  77. // This class calls the ETW Begin and End functions in order to insert a
  78. // pair of events to bracket some work.
  79. class CETWScope
  80. {
  81. public:
  82. CETWScope( const char *pMessage )
  83. : m_pMessage( pMessage )
  84. {
  85. m_nStartTime = ETWBegin( pMessage );
  86. }
  87. ~CETWScope()
  88. {
  89. ETWEnd( m_pMessage, m_nStartTime );
  90. }
  91. private:
  92. // Private and unimplemented to disable copying.
  93. CETWScope( const CETWScope& rhs );
  94. CETWScope& operator=( const CETWScope& rhs );
  95. const char* m_pMessage;
  96. int64 m_nStartTime;
  97. };
  98. #else
  99. inline bool ETWIsTracingEnabled() { return false; }
  100. inline int64 ETWMark( const char* ) { return 0; }
  101. inline void ETWMarkPrintf( const char *, ... ) { return; }
  102. inline void ETWMark1F( const char *, float ) { }
  103. inline void ETWMark2F( const char *, float , float ) { }
  104. inline void ETWMark3F( const char *, float , float , float ) { }
  105. inline void ETWMark4F( const char *, float , float , float , float ) { }
  106. inline void ETWMark1I( const char *, int ) { }
  107. inline void ETWMark2I( const char *, int , int ) { }
  108. inline void ETWMark3I( const char *, int , int , int ) { }
  109. inline void ETWMark4I( const char *, int , int , int , int ) { }
  110. // Optionally specify one to two strings. They will show up in separate columns in
  111. // summary tables to allow sorting and easier transfer to spreadsheets.
  112. inline void ETWMark1S( const char *, const char* ) { }
  113. inline void ETWMark2S( const char *, const char* , const char* ) { }
  114. inline int64 ETWBegin( const char* ) { return 0; }
  115. inline int64 ETWEnd( const char*, int64 ) { return 0; }
  116. inline void ETWRenderFrameMark( bool ) {}
  117. inline void ETWSimFrameMark( bool ) {}
  118. inline int ETWGetRenderFrameNumber() { return 0; }
  119. inline void ETWMouseDown( int nWhichButton, int nX, int nY ) {}
  120. inline void ETWMouseUp( int nWhichButton, int nX, int nY ) {}
  121. inline void ETWMouseMove( int nX, int nY ) {}
  122. inline void ETWMouseWheel( int nWheelDelta, int nX, int nY ) {}
  123. inline void ETWKeyDown( int nScanCode, int nVirtualCode, const char *pChar ) {}
  124. inline void ETWSendPacket( const char *pTo, int nWireSize, int nOutSequenceNR, int nOutSequenceNrAck ) {}
  125. inline void ETWThrottled() {}
  126. inline void ETWReadPacket( const char *pFrom, int nWireSize, int nInSequenceNR, int nOutSequenceNRAck ) {}
  127. // This class calls the ETW Begin and End functions in order to insert a
  128. // pair of events to bracket some work.
  129. class CETWScope
  130. {
  131. public:
  132. CETWScope( const char* )
  133. {
  134. }
  135. private:
  136. // Private and unimplemented to disable copying.
  137. CETWScope( const CETWScope& rhs );
  138. CETWScope& operator=( const CETWScope& rhs );
  139. };
  140. #endif
  141. #endif // ETWPROF_H