Plasma Engine  2.0
Loading...
Searching...
No Matches
Profiling.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Containers/DynamicArray.h>
5#include <Foundation/Containers/StaticRingBuffer.h>
6#include <Foundation/System/Process.h>
7#include <Foundation/Time/Time.h>
8
10class plThread;
11
16class PL_FOUNDATION_DLL plProfilingScope
17{
18public:
19 plProfilingScope(plStringView sName, const char* szFunctionName, plTime timeout);
21
22protected:
23 plStringView m_sName;
24 const char* m_szFunction;
25 plTime m_BeginTime;
26 plTime m_Timeout;
27};
28
38{
39public:
40 PL_FOUNDATION_DLL plProfilingListScope(plStringView sListName, plStringView sFirstSectionName, const char* szFunctionName);
41 PL_FOUNDATION_DLL ~plProfilingListScope();
42
43 PL_FOUNDATION_DLL static void StartNextSection(plStringView sNextSectionName);
44
45protected:
46 static thread_local plProfilingListScope* s_pCurrentList;
47
48 plProfilingListScope* m_pPreviousList;
49
50 plStringView m_sListName;
51 const char* m_szListFunction;
52 plTime m_ListBeginTime;
53
54 plStringView m_sCurSectionName;
55 plTime m_CurSectionBeginTime;
56};
57
59class PL_FOUNDATION_DLL plProfilingSystem
60{
61public:
63 {
64 plUInt64 m_uiThreadId;
65 plString m_sName;
66 };
67
68 struct CPUScope
69 {
70 PL_DECLARE_POD_TYPE();
71
72 static constexpr plUInt32 NAME_SIZE = 40;
73
74 const char* m_szFunctionName;
75 plTime m_BeginTime;
76 plTime m_EndTime;
77 char m_szName[NAME_SIZE];
78 };
79
81 {
83 plUInt64 m_uiThreadId = 0;
84 };
85
87 struct GPUScope
88 {
89 PL_DECLARE_POD_TYPE();
90
91 static constexpr plUInt32 NAME_SIZE = 48;
92
93 plTime m_BeginTime;
94 plTime m_EndTime;
95 char m_szName[NAME_SIZE];
96 };
97
98 struct PL_FOUNDATION_DLL ProfilingData
99 {
100 plUInt32 m_uiFramesThreadID = 0;
101 plUInt32 m_uiProcessSortIndex = 0;
102 plOsProcessID m_uiProcessID = 0;
103
104 plHybridArray<ThreadInfo, 16> m_ThreadInfos;
105
106 plDynamicArray<CPUScopesBufferFlat> m_AllEventBuffers;
107
108 plUInt64 m_uiFrameCount = 0;
109 plDynamicArray<plTime> m_FrameStartTimes;
110
112
114 plResult Write(plStreamWriter& ref_outputStream) const;
115
116 void Clear();
117
119 static void Merge(ProfilingData& out_merged, plArrayPtr<const ProfilingData*> inputs);
120 };
121
122public:
123 static void Clear();
124
125 static void Capture(plProfilingSystem::ProfilingData& out_capture, bool bClearAfterCapture = false);
126
128 static void SetDiscardThreshold(plTime threshold);
129
130 using ScopeTimeoutDelegate = plDelegate<void(plStringView sName, plStringView sFunctionName, plTime duration)>;
131
134
136 static void StartNewFrame();
137
139 static void AddCPUScope(plStringView sName, const char* szFunctionName, plTime beginTime, plTime endTime, plTime scopeTimeout);
140
142 static plUInt64 GetFrameCount();
143
144private:
145 PL_MAKE_SUBSYSTEM_STARTUP_FRIEND(Foundation, ProfilingSystem);
146 friend plUInt32 RunThread(plThread* pThread);
147
148 static void Initialize();
150 static void Reset();
151
153 static void SetThreadName(plStringView sThreadName);
156 static void RemoveThread();
157
158public:
160 static void InitializeGPUData(plUInt32 uiGpuCount = 1);
161
163 static void AddGPUScope(plStringView sName, plTime beginTime, plTime endTime, plUInt32 uiGpuIndex = 0);
164};
165
166#if PL_ENABLED(PL_USE_PROFILING) || defined(PL_DOCS)
167
177# define PL_PROFILE_SCOPE(ScopeName) \
178 plProfilingScope PL_PP_CONCAT(_plProfilingScope, PL_SOURCE_LINE)(ScopeName, PL_SOURCE_FUNCTION, plTime::MakeZero())
179
185# define PL_PROFILE_SCOPE_WITH_TIMEOUT(ScopeName, Timeout) \
186 plProfilingScope PL_PP_CONCAT(_plProfilingScope, PL_SOURCE_LINE)(ScopeName, PL_SOURCE_FUNCTION, Timeout)
187
199# define PL_PROFILE_LIST_SCOPE(ListName, FirstSectionName) \
200 plProfilingListScope PL_PP_CONCAT(_plProfilingScope, PL_SOURCE_LINE)(ListName, FirstSectionName, PL_SOURCE_FUNCTION)
201
206# define PL_PROFILE_LIST_NEXT_SECTION(NextSectionName) \
207 plProfilingListScope::StartNextSection(NextSectionName)
208
210# define PL_PROFILER_FRAME_MARKER()
211
212#else
213# define PL_PROFILE_SCOPE(ScopeName)
214# define PL_PROFILE_SCOPE_WITH_TIMEOUT(ScopeName, Timeout)
215# define PL_PROFILE_LIST_SCOPE(ListName, FirstSectionName)
216# define PL_PROFILE_LIST_NEXT_SECTION(NextSectionName)
217# define PL_PROFILER_FRAME_MARKER()
218#endif
219
220// Let Tracy override the macros.
221#include <Foundation/Profiling/Profiling_Tracy.h>
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Definition DynamicArray.h:81
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
This class implements a profiling scope similar to plProfilingScope, but with additional sub-scopes w...
Definition Profiling.h:38
This class encapsulates a profiling scope.
Definition Profiling.h:17
Helper functionality of the profiling system.
Definition Profiling.h:60
static plUInt64 GetFrameCount()
Get current frame counter.
static void SetScopeTimeoutCallback(ScopeTimeoutDelegate callback)
Sets a callback that is triggered when a profiling scope takes longer than desired.
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
This class is the base class for platform independent long running threads.
Definition Thread.h:40
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Definition Profiling.h:69
Helper struct to hold GPU profiling data.
Definition Profiling.h:88
Definition Profiling.h:99
Definition Profiling.h:63
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12