1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Profiling/BsProfilingManager.h"
4#include "Math/BsMath.h"
5
6namespace bs
7{
8 const UINT32 ProfilingManager::NUM_SAVED_FRAMES = 200;
9
10 ProfilingManager::ProfilingManager()
11 {
12 mSavedSimReports = bs_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
13 mSavedCoreReports = bs_newN<ProfilerReport, ProfilerAlloc>(NUM_SAVED_FRAMES);
14 }
15
16 ProfilingManager::~ProfilingManager()
17 {
18 if(mSavedSimReports != nullptr)
19 bs_deleteN<ProfilerReport, ProfilerAlloc>(mSavedSimReports, NUM_SAVED_FRAMES);
20
21 if(mSavedCoreReports != nullptr)
22 bs_deleteN<ProfilerReport, ProfilerAlloc>(mSavedCoreReports, NUM_SAVED_FRAMES);
23 }
24
25 void ProfilingManager::_update()
26 {
27#if BS_PROFILING_ENABLED
28 mSavedSimReports[mNextSimReportIdx].cpuReport = gProfilerCPU().generateReport();
29
30 gProfilerCPU().reset();
31
32 mNextSimReportIdx = (mNextSimReportIdx + 1) % NUM_SAVED_FRAMES;
33#endif
34 }
35
36 void ProfilingManager::_updateCore()
37 {
38#if BS_PROFILING_ENABLED
39 Lock lock(mSync);
40 mSavedCoreReports[mNextCoreReportIdx].cpuReport = gProfilerCPU().generateReport();
41
42 gProfilerCPU().reset();
43
44 mNextCoreReportIdx = (mNextCoreReportIdx + 1) % NUM_SAVED_FRAMES;
45#endif
46 }
47
48 const ProfilerReport& ProfilingManager::getReport(ProfiledThread thread, UINT32 idx) const
49 {
50 idx = Math::clamp(idx, 0U, (UINT32)(NUM_SAVED_FRAMES - 1));
51
52 if(thread == ProfiledThread::Core)
53 {
54 Lock lock(mSync);
55
56 UINT32 reportIdx = mNextCoreReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
57 reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
58
59 return mSavedCoreReports[reportIdx];
60 }
61 else
62 {
63 UINT32 reportIdx = mNextSimReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
64 reportIdx = (reportIdx) % NUM_SAVED_FRAMES;
65
66 return mSavedSimReports[reportIdx];
67 }
68 }
69
70 ProfilingManager& gProfiler()
71 {
72 return ProfilingManager::instance();
73 }
74}