CmProfiler.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmCPUProfiler.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Shortcut for profiling a single function call.
  9. */
  10. #define PROFILE_CALL(call, name) \
  11. BansheeEngine::gProfiler().beginSample(##name##); \
  12. call; \
  13. BansheeEngine::gProfiler().endSample(##name##);
  14. /**
  15. * @brief Contains data about a profiling session.
  16. */
  17. struct ProfilerReport
  18. {
  19. CPUProfilerReport cpuReport;
  20. };
  21. /**
  22. * @brief Type of thread used by the profiler.
  23. */
  24. enum class ProfiledThread
  25. {
  26. Sim,
  27. Core
  28. };
  29. /**
  30. * @brief Provides performance measuring methods for the CPU.
  31. */
  32. class BS_CORE_EXPORT Profiler : public Module<Profiler>
  33. {
  34. public:
  35. Profiler();
  36. ~Profiler();
  37. /**
  38. * @copydoc CPUProfiler::beginThread
  39. */
  40. void beginThread(const ProfilerString& name)
  41. {
  42. #if CM_PROFILING_ENABLED
  43. mCPUProfiler->beginThread(name);
  44. #endif
  45. }
  46. /**
  47. * @copydoc CPUProfiler::endThread
  48. */
  49. void endThread()
  50. {
  51. #if CM_PROFILING_ENABLED
  52. mCPUProfiler->endThread();
  53. #endif
  54. }
  55. /**
  56. * @copydoc CPUProfiler::beginSample
  57. */
  58. void beginSample(const ProfilerString& name)
  59. {
  60. #if CM_PROFILING_ENABLED
  61. mCPUProfiler->beginSample(name);
  62. #endif
  63. }
  64. /**
  65. * @copydoc CPUProfiler::endSample
  66. */
  67. void endSample(const ProfilerString& name)
  68. {
  69. #if CM_PROFILING_ENABLED
  70. mCPUProfiler->endSample(name);
  71. #endif
  72. }
  73. /**
  74. * @copydoc CPUProfiler::beginSamplePrecise
  75. */
  76. void beginSamplePrecise(const ProfilerString& name)
  77. {
  78. #if CM_PROFILING_ENABLED
  79. mCPUProfiler->beginSamplePrecise(name);
  80. #endif
  81. }
  82. /**
  83. * @copydoc CPUProfiler::endSamplePrecise
  84. */
  85. void endSamplePrecise(const ProfilerString& name)
  86. {
  87. #if CM_PROFILING_ENABLED
  88. mCPUProfiler->endSamplePrecise(name);
  89. #endif
  90. }
  91. /**
  92. * @brief Called every frame.
  93. *
  94. * @note Internal method.
  95. */
  96. void _update();
  97. /**
  98. * @brief Called every frame from the core thread.
  99. *
  100. * @note Internal method. Only call from core thread.
  101. */
  102. void _updateCore();
  103. /**
  104. * @brief Returns a profiler report for the specified frame, for the specified thread.
  105. *
  106. * @param Profiler report index, ranging [0, NUM_SAVED_FRAMES]. 0 always returns the latest
  107. * report. Increasing indexes return reports for older and older frames. Out of range
  108. * indexes will be clamped.
  109. *
  110. * @note Profiler reports get updated every frame. Oldest reports that no longer fit in the saved reports buffer
  111. * are discarded.
  112. */
  113. const ProfilerReport& getReport(ProfiledThread thread, UINT32 idx = 0) const;
  114. private:
  115. static const UINT32 NUM_SAVED_FRAMES;
  116. ProfilerReport* mSavedSimReports;
  117. UINT32 mNextSimReportIdx;
  118. ProfilerReport* mSavedCoreReports;
  119. UINT32 mNextCoreReportIdx;
  120. CPUProfiler* mCPUProfiler;
  121. CM_MUTEX(mSync);
  122. };
  123. /**
  124. * @brief Quick way to access the profiler.
  125. */
  126. BS_CORE_EXPORT Profiler& gProfiler();
  127. }