Team Fortress 2 Source Code as on 22/4/2020
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.

157 lines
7.0 KiB

  1. //========= Copyright 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. // Insert a single event to mark a point in an ETW trace. The return value is a 64-bit
  31. // time stamp.
  32. PLATFORM_INTERFACE int64 ETWMark( const char *pMessage );
  33. // Optionally do full printf formatting of the mark string. This will be more expensive,
  34. // but only when tracing is enabled.
  35. PLATFORM_INTERFACE void ETWMarkPrintf( PRINTF_FORMAT_STRING const char *pMessage, ... ) FMTFUNCTION( 1, 2 );
  36. // Optionally specify one to four floats. They will show up in separate columns in
  37. // summary tables to allow sorting and easier transfer to spreadsheets.
  38. PLATFORM_INTERFACE void ETWMark1F( const char *pMessage, float data1 );
  39. PLATFORM_INTERFACE void ETWMark2F( const char *pMessage, float data1, float data2 );
  40. PLATFORM_INTERFACE void ETWMark3F( const char *pMessage, float data1, float data2, float data3 );
  41. PLATFORM_INTERFACE void ETWMark4F( const char *pMessage, float data1, float data2, float data3, float data4 );
  42. // Optionally specify one to four ints. They will show up in separate columns in
  43. // summary tables to allow sorting and easier transfer to spreadsheets.
  44. PLATFORM_INTERFACE void ETWMark1I( const char *pMessage, int data1 );
  45. PLATFORM_INTERFACE void ETWMark2I( const char *pMessage, int data1, int data2 );
  46. PLATFORM_INTERFACE void ETWMark3I( const char *pMessage, int data1, int data2, int data3 );
  47. PLATFORM_INTERFACE void ETWMark4I( const char *pMessage, int data1, int data2, int data3, int data4 );
  48. // Optionally specify one to two strings. They will show up in separate columns in
  49. // summary tables to allow sorting and easier transfer to spreadsheets.
  50. PLATFORM_INTERFACE void ETWMark1S( const char *pMessage, const char* data1 );
  51. PLATFORM_INTERFACE void ETWMark2S( const char *pMessage, const char* data1, const char* data2 );
  52. // Insert a begin event to mark the start of some work. The return value is a 64-bit
  53. // time stamp which should be passed to the corresponding ETWEnd function.
  54. PLATFORM_INTERFACE int64 ETWBegin( const char *pMessage );
  55. // Insert a paired end event to mark the end of some work.
  56. PLATFORM_INTERFACE int64 ETWEnd( const char *pMessage, int64 nStartTime );
  57. // Mark the start of the next render frame. bIsServerProcess must be passed
  58. // in consistently for a particular process.
  59. PLATFORM_INTERFACE void ETWRenderFrameMark( bool bIsServerProcess );
  60. // Mark the start of the next simulation frame. bIsServerProcess must be passed
  61. // in consistently for a particular process.
  62. PLATFORM_INTERFACE void ETWSimFrameMark( bool bIsServerProcess );
  63. // Return the frame number recorded in the ETW trace -- useful for synchronizing
  64. // other profile information to the ETW trace.
  65. PLATFORM_INTERFACE int ETWGetRenderFrameNumber();
  66. PLATFORM_INTERFACE void ETWMouseDown( int nWhichButton, int nX, int nY );
  67. PLATFORM_INTERFACE void ETWMouseUp( int nWhichButton, int nX, int nY );
  68. PLATFORM_INTERFACE void ETWMouseMove( int nX, int nY );
  69. PLATFORM_INTERFACE void ETWMouseWheel( int nWheelDelta, int nX, int nY );
  70. PLATFORM_INTERFACE void ETWKeyDown( int nScanCode, int nVirtualCode, const char *pChar );
  71. PLATFORM_INTERFACE void ETWSendPacket( const char *pTo, int nWireSize, int nOutSequenceNR, int nOutSequenceNrAck );
  72. PLATFORM_INTERFACE void ETWThrottled();
  73. PLATFORM_INTERFACE void ETWReadPacket( const char *pFrom, int nWireSize, int nInSequenceNR, int nOutSequenceNRAck );
  74. // This class calls the ETW Begin and End functions in order to insert a
  75. // pair of events to bracket some work.
  76. class CETWScope
  77. {
  78. public:
  79. CETWScope( const char *pMessage )
  80. : m_pMessage( pMessage )
  81. {
  82. m_nStartTime = ETWBegin( pMessage );
  83. }
  84. ~CETWScope()
  85. {
  86. ETWEnd( m_pMessage, m_nStartTime );
  87. }
  88. private:
  89. // Private and unimplemented to disable copying.
  90. CETWScope( const CETWScope& rhs );
  91. CETWScope& operator=( const CETWScope& rhs );
  92. const char* m_pMessage;
  93. int64 m_nStartTime;
  94. };
  95. #else
  96. inline int64 ETWMark( const char* ) { return 0; }
  97. inline void ETWMarkPrintf( const char *, ... ) { return; }
  98. inline void ETWMark1F( const char *, float ) { }
  99. inline void ETWMark2F( const char *, float , float ) { }
  100. inline void ETWMark3F( const char *, float , float , float ) { }
  101. inline void ETWMark4F( const char *, float , float , float , float ) { }
  102. inline void ETWMark1I( const char *, int ) { }
  103. inline void ETWMark2I( const char *, int , int ) { }
  104. inline void ETWMark3I( const char *, int , int , int ) { }
  105. inline void ETWMark4I( const char *, int , int , int , int ) { }
  106. // Optionally specify one to two strings. They will show up in separate columns in
  107. // summary tables to allow sorting and easier transfer to spreadsheets.
  108. inline void ETWMark1S( const char *, const char* ) { }
  109. inline void ETWMark2S( const char *, const char* , const char* ) { }
  110. inline int64 ETWBegin( const char* ) { return 0; }
  111. inline int64 ETWEnd( const char*, int64 ) { return 0; }
  112. inline void ETWRenderFrameMark( bool ) {}
  113. inline void ETWSimFrameMark( bool ) {}
  114. inline int ETWGetRenderFrameNumber() { return 0; }
  115. inline void ETWMouseDown( int nWhichButton, int nX, int nY ) {}
  116. inline void ETWMouseUp( int nWhichButton, int nX, int nY ) {}
  117. inline void ETWMouseMove( int nX, int nY ) {}
  118. inline void ETWMouseWheel( int nWheelDelta, int nX, int nY ) {}
  119. inline void ETWKeyDown( int nScanCode, int nVirtualCode, const char *pChar ) {}
  120. inline void ETWSendPacket( const char *pTo, int nWireSize, int nOutSequenceNR, int nOutSequenceNrAck ) {}
  121. inline void ETWThrottled() {}
  122. inline void ETWReadPacket( const char *pFrom, int nWireSize, int nInSequenceNR, int nOutSequenceNRAck ) {}
  123. // This class calls the ETW Begin and End functions in order to insert a
  124. // pair of events to bracket some work.
  125. class CETWScope
  126. {
  127. public:
  128. CETWScope( const char* )
  129. {
  130. }
  131. private:
  132. // Private and unimplemented to disable copying.
  133. CETWScope( const CETWScope& rhs );
  134. CETWScope& operator=( const CETWScope& rhs );
  135. };
  136. #endif
  137. #endif // ETWPROF_H