UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PlatformMisc.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "CoreTypes.h"
6#include COMPILED_PLATFORM_HEADER(PlatformMisc.h)
8
9#ifndef UE_DEBUG_BREAK
10#error UE_DEBUG_BREAK is not defined for this platform
11#endif
12
13#ifndef PLATFORM_USES_ANSI_STRING_FOR_EXTERNAL_PROFILING
14#error PLATFORM_USES_ANSI_STRING_FOR_EXTERNAL_PROFILING is not defined.
15#endif
16
17#ifndef PLATFORM_LIMIT_PROFILER_UNIQUE_NAMED_EVENTS
18#if defined(FRAMEPRO_ENABLED) && FRAMEPRO_ENABLED
19 // If framepro is enabled, we need to limit the number of unique events
20 // This define prevents us emitting "Frame N" events, etc
21 #define PLATFORM_LIMIT_PROFILER_UNIQUE_NAMED_EVENTS 1
22#else
23 #define PLATFORM_LIMIT_PROFILER_UNIQUE_NAMED_EVENTS 0
24#endif
25#endif
26
27#ifndef PLATFORM_EMPTY_BASES
28#define PLATFORM_EMPTY_BASES
29#endif
30
31#ifndef ALLOW_NAMED_EVENTS_IN_TEST
32#define ALLOW_NAMED_EVENTS_IN_TEST 1 // enabled in Test by default to facilitate auto perf testing
33#endif
34
35// Master switch for scoped named events
36#define ENABLE_NAMED_EVENTS (!UE_BUILD_SHIPPING && (!UE_BUILD_TEST || ALLOW_NAMED_EVENTS_IN_TEST))
37
38#if PLATFORM_USES_ANSI_STRING_FOR_EXTERNAL_PROFILING
39 #define NAMED_EVENT_STR(x) x
40#else
41 #define NAMED_EVENT_STR(x) TEXT(x)
42#endif
43
44#if ENABLE_NAMED_EVENTS
45
47{
48public:
49 FScopedNamedEventConditional(const struct FColor& Color, const TCHAR* Text, bool bCondition)
50 : bStarted(bCondition)
51 {
52 if (bCondition)
53 {
55 }
56 }
57
58 FScopedNamedEventConditional(const struct FColor& Color, const ANSICHAR* Text, bool bCondition)
59 : bStarted(bCondition)
60 {
61 if (bCondition)
62 {
64 }
65 }
66
68 {
69 if (bStarted)
70 {
72 }
73 }
74
75private:
76 bool bStarted;
77};
78
98
99//
100// Scoped named event class for constant (compile-time) strings literals.
101//
102// BeginNamedEventStatic works the same as BeginNamedEvent, but should only be passed a compile-time string literal.
103// Some platform profilers can optimize the case where strings for certain events are constant.
104//
106{
107public:
108
110 {
111#if PLATFORM_IMPLEMENTS_BeginNamedEventStatic
112 FPlatformMisc::BeginNamedEventStatic(Color, Text);
113#else
115#endif
116 }
117
119 {
120#if PLATFORM_IMPLEMENTS_BeginNamedEventStatic
121 FPlatformMisc::BeginNamedEventStatic(Color, Text);
122#else
124#endif
125 }
126
131};
132
133// Conditional version of FScopedNamedEventStatic
135{
136public:
137
138 FScopedNamedEventConditionalStatic(const struct FColor& Color, const TCHAR* Text, bool bCondition)
139 : bStarted(bCondition)
140 {
141 if (bCondition)
142 {
143#if PLATFORM_IMPLEMENTS_BeginNamedEventStatic
144 FPlatformMisc::BeginNamedEventStatic(Color, Text);
145#else
147#endif
148 }
149 }
150
151 FScopedNamedEventConditionalStatic(const struct FColor& Color, const ANSICHAR* Text, bool bCondition)
152 : bStarted(bCondition)
153 {
154 if (bCondition)
155 {
156#if PLATFORM_IMPLEMENTS_BeginNamedEventStatic
157 FPlatformMisc::BeginNamedEventStatic(Color, Text);
158#else
160#endif
161 }
162 }
163
165 {
166 if (bStarted)
167 {
169 }
170 }
171
172private:
173 bool bStarted;
174};
175
176
177// Lightweight scoped named event separate from stats system. Will be available in test builds.
178// Events cost profiling overhead so use them judiciously in final code.
179
180#define SCOPED_NAMED_EVENT(Name, Color)\
181 FScopedNamedEventStatic ANONYMOUS_VARIABLE(NamedEvent_##Name##_)(Color, NAMED_EVENT_STR(#Name));\
182 TRACE_CPUPROFILER_EVENT_SCOPE(Name);
183
184#define SCOPED_NAMED_EVENT_FSTRING(Text, Color)\
185 FScopedNamedEvent ANONYMOUS_VARIABLE(NamedEvent_)(Color, *Text);\
186 TRACE_CPUPROFILER_EVENT_SCOPE_TEXT(*Text);
187
188#define SCOPED_NAMED_EVENT_TCHAR(Text, Color)\
189 FScopedNamedEvent ANONYMOUS_VARIABLE(NamedEvent_)(Color, Text);\
190 TRACE_CPUPROFILER_EVENT_SCOPE_TEXT(Text);
191
192#define SCOPED_NAMED_EVENT_TEXT(Text, Color)\
193 FScopedNamedEventStatic ANONYMOUS_VARIABLE(NamedEvent_)(Color, NAMED_EVENT_STR(Text));\
194 TRACE_CPUPROFILER_EVENT_SCOPE_STR(Text);
195
196#define SCOPED_NAMED_EVENT_F(Format, Color, ...)\
197 FScopedNamedEvent ANONYMOUS_VARIABLE(NamedEvent_)(Color, *FString::Printf(Format, __VA_ARGS__));\
198 TRACE_CPUPROFILER_EVENT_SCOPE_TEXT(*FString::Printf(Format, __VA_ARGS__));
199
200#define SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL(Text, Color, bCondition)\
201 FScopedNamedEventConditional ANONYMOUS_VARIABLE(NamedEvent_)(Color, Text, (bCondition));\
202 TRACE_CPUPROFILER_EVENT_SCOPE_TEXT_CONDITIONAL(Text, (bCondition));
203
204#else
205
206#define SCOPED_NAMED_EVENT(Name, Color) TRACE_CPUPROFILER_EVENT_SCOPE(Name);
207#define SCOPED_NAMED_EVENT_FSTRING(Text, Color) TRACE_CPUPROFILER_EVENT_SCOPE_TEXT(*Text);
208#define SCOPED_NAMED_EVENT_TCHAR(Text, Color) TRACE_CPUPROFILER_EVENT_SCOPE_TEXT(Text);
209#define SCOPED_NAMED_EVENT_TEXT(Text, Color) TRACE_CPUPROFILER_EVENT_SCOPE_STR(Text);
210#define SCOPED_NAMED_EVENT_F(Format, Color, ...) TRACE_CPUPROFILER_EVENT_SCOPE_TEXT(*FString::Printf(Format, __VA_ARGS__));
211#define SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL(Text, Color, bCondition) TRACE_CPUPROFILER_EVENT_SCOPE_TEXT_CONDITIONAL(Text, (bCondition));
212
213#endif
214
215#define SCOPED_PROFILER_COLOR(...) UE_DEPRECATED_MACRO(5.5, "SCOPED_PROFILER_COLOR is deprecated and there is no replacement.")
216
217// For timing OnEnterBackground tasks. This can be time sensitive on some platforms
232
233// Note: we don't use ANONYMOUS_VARIABLE here because we might want to view the event in a crash dump watch window
234#define SCOPED_ENTER_BACKGROUND_EVENT(Name) \
235 FScopedEnterBackgroundEvent EnterBackgroundEvent_##Name##_(TEXT(#Name)); \
236 QUICK_SCOPE_CYCLE_COUNTER(Name);
237
238
239#ifdef PLATFORM_COMPILER_IWYU
240
241// There are limitations to what IWYU can read out of the ast. decltype() inside unused template parameter default initializer is one example
242// In those cases we need to give IWYU something it can use to know what is needed
243namespace UE::Core::Private
244{
245 template <typename T, bool IsSameTypes = true> struct IwyuTestSize { enum { Value = 0 }; };
246 template <typename T> struct IwyuTestSize<T, false> { enum { Value = sizeof(T) }; };
247}
248#define IWYU_MARKUP_IMPLICIT_CAST(From, To) UE::Core::Private::IwyuTestSize<From, std::is_same<typename std::remove_cv<To>::type, typename std::remove_cv<From>::type>::value>::Value
249
250#else
251#define IWYU_MARKUP_IMPLICIT_CAST(From, To)
252#endif
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition PlatformMisc.h:219
FScopedEnterBackgroundEvent(const TCHAR *Text)
Definition PlatformMisc.h:222
~FScopedEnterBackgroundEvent()
Definition PlatformMisc.h:227
Definition PlatformMisc.h:135
FScopedNamedEventConditionalStatic(const struct FColor &Color, const ANSICHAR *Text, bool bCondition)
Definition PlatformMisc.h:151
~FScopedNamedEventConditionalStatic()
Definition PlatformMisc.h:164
FScopedNamedEventConditionalStatic(const struct FColor &Color, const TCHAR *Text, bool bCondition)
Definition PlatformMisc.h:138
Definition PlatformMisc.h:47
FScopedNamedEventConditional(const struct FColor &Color, const TCHAR *Text, bool bCondition)
Definition PlatformMisc.h:49
FScopedNamedEventConditional(const struct FColor &Color, const ANSICHAR *Text, bool bCondition)
Definition PlatformMisc.h:58
~FScopedNamedEventConditional()
Definition PlatformMisc.h:67
Definition PlatformMisc.h:106
FScopedNamedEventStatic(const struct FColor &Color, const TCHAR *Text)
Definition PlatformMisc.h:109
~FScopedNamedEventStatic()
Definition PlatformMisc.h:127
FScopedNamedEventStatic(const struct FColor &Color, const ANSICHAR *Text)
Definition PlatformMisc.h:118
Definition PlatformMisc.h:80
~FScopedNamedEvent()
Definition PlatformMisc.h:93
FScopedNamedEvent(const struct FColor &Color, const ANSICHAR *Text)
Definition PlatformMisc.h:88
FScopedNamedEvent(const struct FColor &Color, const TCHAR *Text)
Definition PlatformMisc.h:83
implementation
Definition PlayInEditorLoadingScope.h:8
@ false
Definition radaudio_common.h:23
Definition Color.h:486
static void EndEnterBackgroundEvent()
Definition GenericPlatformMisc.h:950
static FORCEINLINE void BeginNamedEvent(const struct FColor &Color, const TCHAR *Text)
Definition GenericPlatformMisc.h:920
static void BeginEnterBackgroundEvent(const TCHAR *Text)
Definition GenericPlatformMisc.h:949
static FORCEINLINE void EndNamedEvent()
Definition GenericPlatformMisc.h:926