!F (Profiling) (DEV-7030) Rewrite of the profiling system to have a unified interface...
[CRYENGINE.git] / Code / CryEngine / CryCommon / CrySystem / Profilers / ICryProfilingSystem.h
blob118eebb443061c199f70927eb55fbaf09a361bb8
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #pragma once
5 #include <CryMath/Cry_Math.h>
6 #include <CrySystem/ISystem.h>
7 #include <CrySystem/ITimer.h>
9 struct SProfilingSectionDescription;
10 struct SProfilingSection;
11 struct SProfilingMarker;
12 typedef int32 TProfilingCount;
13 typedef int64 TProfilingValue;
15 enum EProfiledSubsystem : uint8
17 PROFILE_RENDERER,
18 PROFILE_3DENGINE,
19 PROFILE_PARTICLE,
20 PROFILE_AI,
21 PROFILE_ANIMATION,
22 PROFILE_MOVIE,
23 PROFILE_ENTITY,
24 PROFILE_FONT,
25 PROFILE_NETWORK,
26 PROFILE_PHYSICS,
27 PROFILE_SCRIPT,
28 PROFILE_SCRIPT_CFUNC,
29 PROFILE_AUDIO,
30 PROFILE_EDITOR,
31 PROFILE_SYSTEM,
32 PROFILE_ACTION,
33 PROFILE_GAME,
34 PROFILE_INPUT,
35 PROFILE_LOADING_ONLY,
37 PROFILE_LAST_SUBSYSTEM //!< Must always be last.
40 struct ICryProfilingSystem
42 //! end the profiling session
43 virtual void Stop() = 0;
44 virtual bool IsStopped() const = 0;
46 virtual void PauseRecording(bool pause) = 0;
47 //! Are we collecting profiling data?
48 virtual bool IsPaused() const = 0;
50 bool IsRunning() const { return !(IsStopped() || IsPaused()); };
52 virtual void StartThread() = 0;
53 virtual void EndThread() = 0;
55 virtual void DescriptionCreated(SProfilingSectionDescription*) = 0;
56 virtual void DescriptionDestroyed(SProfilingSectionDescription*) = 0;
58 virtual bool StartSection(SProfilingSection*) = 0;
59 virtual void EndSection(SProfilingSection*) = 0;
61 virtual void RecordMarker(SProfilingMarker*) = 0;
63 virtual void StartFrame() = 0;
64 virtual void EndFrame() = 0;
65 //! Must be called when something quacks like the end of the frame.
66 virtual void OnSliceAndSleep() = 0;
68 virtual const char* GetModuleName(EProfiledSubsystem) const = 0;
69 virtual const char* GetModuleName(const SProfilingSection*) const = 0;
71 //! get time lost to profiling in milliseconds, may return 0, if not measured
72 virtual float GetProfilingTimeCost() const = 0;
73 virtual float GetAverageProfilingTimeCost() const = 0;
75 protected:
76 ~ICryProfilingSystem() = default;
79 //! Description of a code section we're profiling.
80 struct SProfilingSectionDescription
82 SProfilingSectionDescription(const char* szFilename, const char* szEventname, uint16 line, bool isWaiting, EProfiledSubsystem subsystem)
83 : szFilename(szFilename), szEventname(szEventname), line(line), isWaiting(isWaiting), subsystem(subsystem)
84 , color_argb(0), customData(0)
86 if(gEnv && gEnv->pSystem)
87 gEnv->pSystem->GetProfilingSystem()->DescriptionCreated(this);
90 ~SProfilingSectionDescription()
92 if (gEnv && gEnv->pSystem)
93 gEnv->pSystem->GetProfilingSystem()->DescriptionDestroyed(this);
96 const char* szFilename;
97 const char* szEventname;
98 uint16 line;
99 bool isWaiting;
100 EProfiledSubsystem subsystem;
101 //! color stored with 1B per channel
102 uint32 color_argb;
104 //! can be used by a profiling system to attach its own information
105 mutable uintptr_t customData;
108 struct SProfilingSection
110 SProfilingSection(const SProfilingSectionDescription* pDescription, const char* szDynamicName)
111 : pDescription(pDescription), szDynamicName(szDynamicName), childExcludeValue(0), customData(0) // startValue is set by the profiling system
113 wasStarted = gEnv->startProfilingSection(this);
116 ~SProfilingSection()
118 if(wasStarted)
119 gEnv->endProfilingSection(this);
122 const SProfilingSectionDescription* pDescription;
123 //! optional description of the specific instance, e.g. parameters of called function
124 const char* szDynamicName;
126 TProfilingValue startValue;
127 TProfilingValue childExcludeValue;
129 //! can be used by a profiling system to attach its own information
130 uintptr_t customData;
131 bool wasStarted;
134 struct SProfilingMarkerDescription
136 const char* szFilename;
137 const char* szMarkername;
138 uint32 line;
139 EProfiledSubsystem subsystem;
140 //! color stored with 1B per channel
141 uint32 color_argb;
144 struct SProfilingMarker
146 SProfilingMarker(SProfilingMarkerDescription* pDescription) :
147 pDescription(pDescription), threadId(CryGetCurrentThreadId())
149 gEnv->recordProfilingMarker(this);
152 SProfilingMarkerDescription* pDescription;
153 threadID threadId;
156 #ifdef ENABLE_PROFILING_CODE
158 #define CRYPROF_CAT_(a, b) a ## b
159 #define CRYPROF_CAT(a, b) CRYPROF_CAT_(a, b)
161 #define CRY_PROFILE_THREADSTART if(gEnv->pSystem) gEnv->pSystem->GetProfilingSystem()->StartThread()
162 #define CRY_PROFILE_THREADEND if(gEnv->pSystem) gEnv->pSystem->GetProfilingSystem()->EndThread()
164 // static_assert(is_string_literal(PNAME))
165 #define CRY_PROFILE_SECTION_NEW(SYS, PNAME, SZARG, WAITING) \
166 const static SProfilingSectionDescription CRYPROF_CAT(profEventDesc, __LINE__) (__FILE__, PNAME, __LINE__, WAITING, SYS); \
167 SProfilingSection CRYPROF_CAT(profSection, __LINE__)(&CRYPROF_CAT(profEventDesc, __LINE__), SZARG);
169 #define LOADING_TIME_PROFILE_SECTION CRY_PROFILE_SECTION_NEW(PROFILE_LOADING_ONLY, __FUNC__, nullptr, false)
170 #define LOADING_TIME_PROFILE_SECTION_ARGS(args) CRY_PROFILE_SECTION_NEW(PROFILE_LOADING_ONLY, __FUNC__, args, false)
171 #define LOADING_TIME_PROFILE_SECTION_NAMED(sectionName) CRY_PROFILE_SECTION_NEW(PROFILE_LOADING_ONLY, sectionName, nullptr, false)
172 #define LOADING_TIME_PROFILE_SECTION_NAMED_ARGS(sectionName, args) CRY_PROFILE_SECTION_NEW(PROFILE_LOADING_ONLY, sectionName, args, false)
174 #define CRY_PROFILE_REGION(subsystem, szName) CRY_PROFILE_SECTION_NEW(subsystem, szName, nullptr, false)
175 #define CRY_PROFILE_REGION_ARG(subsystem, szName, argument) CRY_PROFILE_SECTION_NEW(subsystem, szName, argument, false)
176 #define CRY_PROFILE_REGION_WAITING(subsystem, szName) CRY_PROFILE_SECTION_NEW(subsystem, szName, nullptr, true)
178 #define CRY_PROFILE_FUNCTION(subsystem) CRY_PROFILE_SECTION_NEW(subsystem, __FUNC__, nullptr, false)
179 #define CRY_PROFILE_FUNCTION_ARG(subsystem, argument) CRY_PROFILE_SECTION_NEW(subsystem, __FUNC__, argument, false)
180 #define CRY_PROFILE_FUNCTION_WAITING(subsystem) CRY_PROFILE_SECTION_NEW(subsystem, __FUNC__, nullptr, true)
181 #define CRY_PROFILE_SECTION(subsystem, szName) CRY_PROFILE_SECTION_NEW(subsystem, szName, nullptr, false)
182 #define CRY_PROFILE_SECTION_WAITING(subsystem, szName) CRY_PROFILE_SECTION_NEW(subsystem, szName, nullptr, true)
184 #define CRY_PROFILE_MARKER(SYS, PNAME) \
185 static SProfilingMarkerDescription CRYPROF_CAT(profMarkerDesc, __LINE__) = {__FILE__, PNAME, __LINE__, SYS, 0}; \
186 SProfilingMarker CRYPROF_CAT(profMarker, __LINE__)(&CRYPROF_CAT(profMarkerDesc, __LINE__));
188 #else
190 #define CRY_PROFILE_THREADSTART
191 #define CRY_PROFILE_THREADEND
193 #define LOADING_TIME_PROFILE_SECTION
194 #define LOADING_TIME_PROFILE_SECTION_ARGS(args)
195 #define LOADING_TIME_PROFILE_SECTION_NAMED(sectionName)
196 #define LOADING_TIME_PROFILE_SECTION_NAMED_ARGS(sectionName, args)
198 #define CRY_PROFILE_REGION(subsystem, szName)
199 #define CRY_PROFILE_REGION_ARG(subsystem, szName, argument)
200 #define CRY_PROFILE_REGION_WAITING(subsystem, szName)
202 #define CRY_PROFILE_FUNCTION(subsystem)
203 #define CRY_PROFILE_FUNCTION_ARG(subsystem, argument)
204 #define CRY_PROFILE_FUNCTION_WAITING(subsystem)
205 #define CRY_PROFILE_SECTION(subsystem, szName)
206 #define CRY_PROFILE_SECTION_WAITING(subsystem, szName)
208 #define CRY_PROFILE_MARKER(SYS, PNAME)
210 #endif
212 #ifdef ENABLE_LOADING_PROFILER
213 struct SBootprofilerAutoSession
215 SBootprofilerAutoSession(const char* szName) { if (gEnv->pSystem) gEnv->pSystem->StartBootProfilerSession(szName); }
216 ~SBootprofilerAutoSession() { if (gEnv->pSystem) gEnv->pSystem->EndBootProfilerSession(); }
218 # define LOADING_TIME_PROFILE_AUTO_SESSION(NAME) SBootprofilerAutoSession __bootProfSession(NAME)
219 #else
220 # define LOADING_TIME_PROFILE_AUTO_SESSION(NAME)
221 #endif