| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include "CmProfiler.h"
- #include "CmMath.h"
- namespace CamelotFramework
- {
- const UINT32 Profiler::NUM_SAVED_FRAMES = 200;
- Profiler::Profiler()
- :mSavedSimReports(nullptr), mCPUProfiler(nullptr), mNextSimReportIdx(0),
- mSavedCoreReports(nullptr), mNextCoreReportIdx(0)
- {
- #if CM_PROFILING_ENABLED
- mCPUProfiler = cm_new<CPUProfiler, ProfilerAlloc>();
- #endif
- mSavedSimReports = cm_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
- mSavedCoreReports = cm_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
- }
- Profiler::~Profiler()
- {
- if(mCPUProfiler != nullptr)
- cm_delete<ProfilerAlloc>(mCPUProfiler);
- if(mSavedSimReports != nullptr)
- cm_deleteN<ProfilerAlloc>(mSavedSimReports, NUM_SAVED_FRAMES);
- if(mSavedCoreReports != nullptr)
- cm_deleteN<ProfilerAlloc>(mSavedCoreReports, NUM_SAVED_FRAMES);
- }
- void Profiler::update()
- {
- #if CM_PROFILING_ENABLED
- mSavedSimReports[mNextSimReportIdx].cpuReport = mCPUProfiler->generateReport();
- mCPUProfiler->reset();
- mNextSimReportIdx = (mNextSimReportIdx + 1) % NUM_SAVED_FRAMES;
- #endif
- }
- void Profiler::updateCore()
- {
- #if CM_PROFILING_ENABLED
- CM_LOCK_MUTEX(mSync);
- mSavedCoreReports[mNextCoreReportIdx].cpuReport = mCPUProfiler->generateReport();
- mCPUProfiler->reset();
- mNextCoreReportIdx = (mNextCoreReportIdx + 1) % NUM_SAVED_FRAMES;
- #endif
- }
- const ProfilerReport& Profiler::getReport(ProfiledThread thread, UINT32 idx) const
- {
- idx = Math::clamp(idx, 0U, (UINT32)(NUM_SAVED_FRAMES - 1));
- if(thread == ProfiledThread::Core)
- {
- CM_LOCK_MUTEX(mSync);
- UINT32 reportIdx = mNextCoreReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
- reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
- return mSavedCoreReports[reportIdx];
- }
- else
- {
- UINT32 reportIdx = mNextSimReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
- reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
- return mSavedSimReports[reportIdx];
- }
- }
- Profiler& gProfiler()
- {
- return Profiler::instance();
- }
- }
|