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.

154 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Real-Time Hierarchical Telemetry Profiling
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VPROF_TELEMETRY_H
  8. #define VPROF_TELEMETRY_H
  9. #if !defined( MAKE_VPC )
  10. #if !defined( RAD_TELEMETRY_DISABLED ) && ( defined( IS_WINDOWS_PC ) || defined( _LINUX ) )
  11. // Rad Telemetry profiling is enabled on Win32 and Win64.
  12. #define RAD_TELEMETRY_ENABLED
  13. #endif
  14. #endif // !MAKE_VPC
  15. #if !defined( RAD_TELEMETRY_ENABLED )
  16. //
  17. // Kill all tmZone() macros, etc.
  18. //
  19. #include "tmapi_dummy.h"
  20. inline void TelemetryTick() {}
  21. inline void TelemetrySetLevel( unsigned int Level ) {}
  22. #define TelemetrySetLockName( _ctx, _location, _description )
  23. class CTelemetryLock
  24. {
  25. public:
  26. CTelemetryLock(void *plocation, const char *description) {}
  27. ~CTelemetryLock() {}
  28. void Locked() {}
  29. void Unlocked() {}
  30. };
  31. class CTelemetrySpikeDetector
  32. {
  33. public:
  34. CTelemetrySpikeDetector( const char *msg, uint32 threshold = 50 ) {}
  35. ~CTelemetrySpikeDetector() { }
  36. };
  37. #define TM_ZONE_DEFAULT( context )
  38. #define TM_ZONE_DEFAULT_PARAM( context, string_param )
  39. #else
  40. //
  41. // Telemetry is enabled. Include the telemetry header.
  42. //
  43. #include "../../thirdparty/telemetry/include/telemetry.h"
  44. // Different versions of radbase.h define RADCOPYRIGHT to different values. So undef that here.
  45. #undef RADCOPYRIGHT
  46. struct TelemetryData
  47. {
  48. HTELEMETRY tmContext[32];
  49. float flRDTSCToMilliSeconds; // Conversion from tmFastTime() (rdtsc) to milliseconds.
  50. uint32 FrameCount; // Count of frames to capture before turning off.
  51. char ServerAddress[128]; // Server name to connect to.
  52. int playbacktick; // GetPlaybackTick() value from demo file (or 0 if not playing a demo).
  53. uint32 DemoTickStart; // Start telemetry on demo tick #
  54. uint32 DemoTickEnd; // End telemetry on demo tick #
  55. uint32 Level; // Current Telemetry level (Use TelemetrySetLevel to modify)
  56. };
  57. PLATFORM_INTERFACE TelemetryData g_Telemetry;
  58. PLATFORM_INTERFACE void TelemetryTick();
  59. PLATFORM_INTERFACE void TelemetrySetLevel( unsigned int Level );
  60. #define TELEMETRY_LEVEL0 g_Telemetry.tmContext[0] // high level tmZone()
  61. #define TELEMETRY_LEVEL1 g_Telemetry.tmContext[1] // lower level tmZone(), tmZoneFiltered()
  62. #define TELEMETRY_LEVEL2 g_Telemetry.tmContext[2] // VPROF_0
  63. #define TELEMETRY_LEVEL3 g_Telemetry.tmContext[3] // VPROF_1
  64. #define TELEMETRY_LEVEL4 g_Telemetry.tmContext[4] // VPROF_2
  65. #define TELEMETRY_LEVEL5 g_Telemetry.tmContext[5] // VPROF_3
  66. #define TELEMETRY_LEVEL6 g_Telemetry.tmContext[6] // VPROF_4
  67. #define TM_ZONE_DEFAULT( context ) tmZone(context, TMZF_NONE, __FUNCTION__ )
  68. #define TM_ZONE_DEFAULT_PARAM( context, string_param ) tmZone(context, TMZF_NONE, "%s( %s )", __FUNCTION__ , tmDynamicString( context, (string_param) ) )
  69. #define TelemetrySetLockName( _ctx, _location, _description ) \
  70. do \
  71. { \
  72. static bool s_bNameSet = false; \
  73. if( _ctx && !s_bNameSet ) \
  74. { \
  75. tmLockName( _ctx, _location, _description ); \
  76. s_bNameSet = true; \
  77. } \
  78. } while( 0 )
  79. class CTelemetryLock
  80. {
  81. public:
  82. CTelemetryLock(void *plocation, const char *description)
  83. {
  84. m_plocation = (const char *)plocation;
  85. m_description = description;
  86. TelemetrySetLockName( TELEMETRY_LEVEL1, m_plocation, m_description );
  87. tmTryLock( TELEMETRY_LEVEL1, m_plocation, "%s", m_description );
  88. }
  89. ~CTelemetryLock()
  90. {
  91. Unlocked();
  92. }
  93. void Locked()
  94. {
  95. tmEndTryLock( TELEMETRY_LEVEL1, m_plocation, TMLR_SUCCESS );
  96. tmSetLockState( TELEMETRY_LEVEL1, m_plocation, TMLS_LOCKED, "%s Locked", m_description );
  97. }
  98. void Unlocked()
  99. {
  100. if( m_plocation )
  101. {
  102. tmSetLockState( TELEMETRY_LEVEL1, m_plocation, TMLS_RELEASED, "%s Released", m_description );
  103. m_plocation = NULL;
  104. }
  105. }
  106. public:
  107. const char *m_plocation;
  108. const char *m_description;
  109. };
  110. class CTelemetrySpikeDetector
  111. {
  112. public:
  113. // Spews Telemetry message when threshold hit (in milliseconds.)
  114. CTelemetrySpikeDetector( const char *msg, float threshold = 5 ) :
  115. m_message( msg ), m_threshold( threshold ), time0( tmFastTime() ) {}
  116. ~CTelemetrySpikeDetector()
  117. {
  118. float time = ( tmFastTime() - time0 ) * g_Telemetry.flRDTSCToMilliSeconds;
  119. if( time >= m_threshold )
  120. {
  121. tmMessage( TELEMETRY_LEVEL0, TMMF_ICON_NOTE | TMMF_SEVERITY_WARNING, "(source/spike)%s %.2fms %t", m_message, time, tmSendCallStack( TELEMETRY_LEVEL0, 0 ) );
  122. }
  123. }
  124. private:
  125. TmU64 time0;
  126. float m_threshold;
  127. const char *m_message;
  128. };
  129. #endif // RAD_TELEMETRY_ENABLED
  130. #endif // VPROF_TELEMETRY_H