2
0

BsProfilingManager.cpp 1.9 KB

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