| 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 | #pragma once |
| 4 | |
| 5 | #include "BsCorePrerequisites.h" |
| 6 | #include "Utility/BsModule.h" |
| 7 | #include "Profiling/BsProfilerCPU.h" |
| 8 | |
| 9 | namespace bs |
| 10 | { |
| 11 | /** @addtogroup Profiling-Internal |
| 12 | * @{ |
| 13 | */ |
| 14 | |
| 15 | /** Contains data about a profiling session. */ |
| 16 | struct ProfilerReport |
| 17 | { |
| 18 | CPUProfilerReport cpuReport; |
| 19 | }; |
| 20 | |
| 21 | /** Type of thread used by the profiler. */ |
| 22 | enum class ProfiledThread |
| 23 | { |
| 24 | Sim, |
| 25 | Core |
| 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * Tracks CPU profiling information with each frame for sim and core threads. |
| 30 | * |
| 31 | * @note Sim thread only unless specified otherwise. |
| 32 | */ |
| 33 | class BS_CORE_EXPORT ProfilingManager : public Module<ProfilingManager> |
| 34 | { |
| 35 | public: |
| 36 | ProfilingManager(); |
| 37 | ~ProfilingManager(); |
| 38 | |
| 39 | /** Called every frame. */ |
| 40 | void _update(); |
| 41 | |
| 42 | /** |
| 43 | * Called every frame from the core thread. |
| 44 | * |
| 45 | * @note Core thread only. |
| 46 | */ |
| 47 | void _updateCore(); |
| 48 | |
| 49 | /** |
| 50 | * Returns a profiler report for the specified frame, for the specified thread. |
| 51 | * |
| 52 | * @param[in] thread Thread for which to retrieve the profiler report. |
| 53 | * @param[in] idx Profiler report index, ranging [0, NUM_SAVED_FRAMES]. 0 always returns the latest report. |
| 54 | * Increasing indexes return reports for older and older frames. Out of range indexes will be |
| 55 | * clamped. |
| 56 | * |
| 57 | * @note |
| 58 | * Profiler reports get updated every frame. Oldest reports that no longer fit in the saved reports buffer are |
| 59 | * discarded. |
| 60 | */ |
| 61 | const ProfilerReport& getReport(ProfiledThread thread, UINT32 idx = 0) const; |
| 62 | |
| 63 | private: |
| 64 | static const UINT32 NUM_SAVED_FRAMES; |
| 65 | ProfilerReport* mSavedSimReports = nullptr; |
| 66 | UINT32 mNextSimReportIdx = 0; |
| 67 | |
| 68 | ProfilerReport* mSavedCoreReports = nullptr; |
| 69 | UINT32 mNextCoreReportIdx = 0; |
| 70 | |
| 71 | mutable Mutex mSync; |
| 72 | }; |
| 73 | |
| 74 | /** Easy way to access ProfilingManager. */ |
| 75 | BS_CORE_EXPORT ProfilingManager& gProfiler(); |
| 76 | |
| 77 | /** @} */ |
| 78 | } |