CmProfiler.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "CmProfiler.h"
  2. #include "CmMath.h"
  3. namespace CamelotFramework
  4. {
  5. const UINT32 Profiler::NUM_SAVED_FRAMES = 200;
  6. Profiler::Profiler()
  7. :mSavedSimReports(nullptr), mCPUProfiler(nullptr), mNextSimReportIdx(0),
  8. mSavedCoreReports(nullptr), mNextCoreReportIdx(0)
  9. {
  10. #if CM_PROFILING_ENABLED
  11. mCPUProfiler = cm_new<CPUProfiler, ProfilerAlloc>();
  12. #endif
  13. mSavedSimReports = cm_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
  14. mSavedCoreReports = cm_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
  15. }
  16. Profiler::~Profiler()
  17. {
  18. if(mCPUProfiler != nullptr)
  19. cm_delete<ProfilerAlloc>(mCPUProfiler);
  20. if(mSavedSimReports != nullptr)
  21. cm_deleteN<ProfilerAlloc>(mSavedSimReports, NUM_SAVED_FRAMES);
  22. if(mSavedCoreReports != nullptr)
  23. cm_deleteN<ProfilerAlloc>(mSavedCoreReports, NUM_SAVED_FRAMES);
  24. }
  25. void Profiler::update()
  26. {
  27. #if CM_PROFILING_ENABLED
  28. mSavedSimReports[mNextSimReportIdx].cpuReport = mCPUProfiler->generateReport();
  29. mCPUProfiler->reset();
  30. mNextSimReportIdx = (mNextSimReportIdx + 1) % NUM_SAVED_FRAMES;
  31. #endif
  32. }
  33. void Profiler::updateCore()
  34. {
  35. #if CM_PROFILING_ENABLED
  36. CM_LOCK_MUTEX(mSync);
  37. mSavedCoreReports[mNextCoreReportIdx].cpuReport = mCPUProfiler->generateReport();
  38. mCPUProfiler->reset();
  39. mNextCoreReportIdx = (mNextCoreReportIdx + 1) % NUM_SAVED_FRAMES;
  40. #endif
  41. }
  42. const ProfilerReport& Profiler::getReport(ProfiledThread thread, UINT32 idx) const
  43. {
  44. idx = Math::clamp(idx, 0U, (UINT32)(NUM_SAVED_FRAMES - 1));
  45. if(thread == ProfiledThread::Core)
  46. {
  47. CM_LOCK_MUTEX(mSync);
  48. UINT32 reportIdx = mNextCoreReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
  49. reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
  50. return mSavedCoreReports[reportIdx];
  51. }
  52. else
  53. {
  54. UINT32 reportIdx = mNextSimReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
  55. reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
  56. return mSavedSimReports[reportIdx];
  57. }
  58. }
  59. Profiler& gProfiler()
  60. {
  61. return Profiler::instance();
  62. }
  63. }