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.

126 lines
2.6 KiB

  1. //===== Copyright � 1996-2007, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // Simple data structure keeping track
  6. //
  7. // $NoKeywords: $
  8. //
  9. //===========================================================================//
  10. #ifndef PERFSTATS_H
  11. #define PERFSTATS_H
  12. #include "tier0/fasttimer.h"
  13. //-----------------------------------------------------------------------------
  14. //
  15. // Slot ID for which we will be collecting performance stats
  16. //
  17. enum PerfStatsSlot_t
  18. {
  19. PERF_STATS_SLOT_MAINTHREAD, // stats collected in CPerfStatsData::Tick()
  20. PERF_STATS_SLOT_MAINTHREAD_NOWAIT, // time spent in the main thread - time spent in EndFrame
  21. PERF_STATS_SLOT_RENDERTHREAD,
  22. PERF_STATS_SLOT_END_FRAME,
  23. PERF_STATS_SLOT_FORCE_HARDWARE_SYNC,
  24. PERF_STATS_SLOT_MAX
  25. };
  26. //-----------------------------------------------------------------------------
  27. //
  28. // PerfStatsSlotData struct
  29. // Contains performance stats for a single slot
  30. //
  31. class CPerfStatsSlotData
  32. {
  33. public:
  34. void Reset()
  35. {
  36. m_pszName = NULL;
  37. m_CurrFrameTime.Init();
  38. m_PrevFrameTime.Init();
  39. m_AccTotalTime.Init();
  40. }
  41. void ResetFrameStats()
  42. {
  43. m_CurrFrameTime.Init();
  44. }
  45. void StartTimer( const char* pszName )
  46. {
  47. m_pszName = pszName;
  48. m_Timer.Start();
  49. }
  50. void EndTimer( void )
  51. {
  52. m_Timer.End();
  53. const CCycleCount& duration = m_Timer.GetDuration();
  54. m_CurrFrameTime += duration;
  55. m_AccTotalTime += duration;
  56. }
  57. const char* m_pszName;
  58. CFastTimer m_Timer;
  59. CCycleCount m_CurrFrameTime; // Accumulated time over a single frame
  60. CCycleCount m_PrevFrameTime; // This is used to display cl_showfps
  61. CCycleCount m_AccTotalTime; // Total accumulated time
  62. };
  63. //-----------------------------------------------------------------------------
  64. //
  65. // PerfStatsData struct
  66. //
  67. class PLATFORM_CLASS CPerfStatsData
  68. {
  69. public:
  70. CPerfStatsData();
  71. void Tick();
  72. void Reset();
  73. uint64 m_nFrames;
  74. CPerfStatsSlotData m_Slots[PERF_STATS_SLOT_MAX];
  75. };
  76. PLATFORM_INTERFACE CPerfStatsData g_PerfStats;
  77. //-----------------------------------------------------------------------------
  78. //
  79. // Helper class that times whatever block of code it's in
  80. // and collect data for the given slot
  81. //
  82. class CPerfStatsScope
  83. {
  84. public:
  85. CPerfStatsScope( const char* pszName, PerfStatsSlot_t slotId ) : m_SlotData( NULL )
  86. {
  87. if ( slotId < PERF_STATS_SLOT_MAX )
  88. {
  89. m_SlotData = &g_PerfStats.m_Slots[slotId];
  90. m_SlotData->StartTimer( pszName );
  91. }
  92. }
  93. ~CPerfStatsScope()
  94. {
  95. if ( m_SlotData )
  96. {
  97. m_SlotData->EndTimer();
  98. }
  99. }
  100. private:
  101. CPerfStatsSlotData* m_SlotData;
  102. };
  103. #define PERF_STATS_BLOCK( name, slot ) CPerfStatsScope _perfStatsScope##__LINE__( name, slot );
  104. #endif // PERFSTATS_H