BsProfilingManager.cpp 2.2 KB

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