CmProfiler.h 2.7 KB

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