CmProfiler.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "CmProfiler.h"
  2. #include "CmMath.h"
  3. namespace CamelotFramework
  4. {
  5. const UINT32 Profiler::NUM_SAVED_FRAMES = 200;
  6. Profiler::Profiler()
  7. :mSavedReports(nullptr), mCPUProfiler(nullptr), mNextReportIdx(0)
  8. {
  9. #if CM_PROFILING_ENABLED
  10. mCPUProfiler = cm_new<CPUProfiler>();
  11. #endif
  12. mSavedReports = cm_newN<ProfilerReport>(NUM_SAVED_FRAMES);
  13. }
  14. Profiler::~Profiler()
  15. {
  16. if(mCPUProfiler != nullptr)
  17. cm_delete(mCPUProfiler);
  18. if(mSavedReports != nullptr)
  19. cm_deleteN(mSavedReports, NUM_SAVED_FRAMES);
  20. }
  21. void Profiler::update()
  22. {
  23. #if CM_PROFILING_ENABLED
  24. mSavedReports[mNextReportIdx].cpuReport = mCPUProfiler->generateReport();
  25. mCPUProfiler->reset();
  26. mNextReportIdx = (mNextReportIdx + 1) % NUM_SAVED_FRAMES;
  27. #endif
  28. }
  29. const ProfilerReport& Profiler::getReport(UINT32 idx) const
  30. {
  31. idx = Math::Clamp(idx, 0U, (UINT32)(NUM_SAVED_FRAMES - 1));
  32. UINT32 reportIdx = mNextReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
  33. reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
  34. return mSavedReports[reportIdx];
  35. }
  36. Profiler& gProfiler()
  37. {
  38. return Profiler::instance();
  39. }
  40. }