BsProfilingManager.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Profiling/BsProfilingManager.h"
  4. #include "Math/BsMath.h"
  5. namespace bs
  6. {
  7. const UINT32 ProfilingManager::NUM_SAVED_FRAMES = 200;
  8. ProfilingManager::ProfilingManager()
  9. :mSavedSimReports(nullptr), mNextSimReportIdx(0),
  10. mSavedCoreReports(nullptr), mNextCoreReportIdx(0)
  11. {
  12. mSavedSimReports = bs_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
  13. mSavedCoreReports = bs_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
  14. }
  15. ProfilingManager::~ProfilingManager()
  16. {
  17. if(mSavedSimReports != nullptr)
  18. bs_deleteN<ProfilerReport, ProfilerAlloc>(mSavedSimReports, NUM_SAVED_FRAMES);
  19. if(mSavedCoreReports != nullptr)
  20. bs_deleteN<ProfilerReport, ProfilerAlloc>(mSavedCoreReports, NUM_SAVED_FRAMES);
  21. }
  22. void ProfilingManager::_update()
  23. {
  24. #if BS_PROFILING_ENABLED
  25. mSavedSimReports[mNextSimReportIdx].cpuReport = gProfilerCPU().generateReport();
  26. gProfilerCPU().reset();
  27. mNextSimReportIdx = (mNextSimReportIdx + 1) % NUM_SAVED_FRAMES;
  28. #endif
  29. }
  30. void ProfilingManager::_updateCore()
  31. {
  32. #if BS_PROFILING_ENABLED
  33. Lock lock(mSync);
  34. mSavedCoreReports[mNextCoreReportIdx].cpuReport = gProfilerCPU().generateReport();
  35. gProfilerCPU().reset();
  36. mNextCoreReportIdx = (mNextCoreReportIdx + 1) % NUM_SAVED_FRAMES;
  37. #endif
  38. }
  39. const ProfilerReport& ProfilingManager::getReport(ProfiledThread thread, UINT32 idx) const
  40. {
  41. idx = Math::clamp(idx, 0U, (UINT32)(NUM_SAVED_FRAMES - 1));
  42. if(thread == ProfiledThread::Core)
  43. {
  44. Lock lock(mSync);
  45. UINT32 reportIdx = mNextCoreReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
  46. reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
  47. return mSavedCoreReports[reportIdx];
  48. }
  49. else
  50. {
  51. UINT32 reportIdx = mNextSimReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
  52. reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
  53. return mSavedSimReports[reportIdx];
  54. }
  55. }
  56. ProfilingManager& gProfiler()
  57. {
  58. return ProfilingManager::instance();
  59. }
  60. }