7#include "ProfilingDebugging/TagTrace.h"
9#ifndef PLATFORM_SUPPORTS_LLM
10#define PLATFORM_SUPPORTS_LLM 1
17#ifdef ENABLE_LOW_LEVEL_MEM_TRACKER
18#error ENABLE_LOW_LEVEL_MEM_TRACKER is now a derived define that should not be defined separately. Define LLM_ENABLED_IN_CONFIG (build environment only) or LLM_ENABLED_ON_PLATFORM (build environment or c++ header) instead.
32#ifndef LLM_ALLOW_ASSETS_TAGS
33 #define LLM_ALLOW_ASSETS_TAGS 0
40#ifndef LLM_ALLOW_STATS
41 #define LLM_ALLOW_STATS 0
45#define LLM_ENABLED_STAT_TAGS (LLM_ALLOW_STATS || LLM_ALLOW_ASSETS_TAGS)
47#include "Containers/Array.h"
48#include "Containers/ArrayView.h"
49#include "HAL/CriticalSection.h"
50#include "HAL/PlatformCrt.h"
51#include "HAL/PlatformMisc.h"
52#include "Templates/AlignmentTemplates.h"
53#include "Templates/UnrealTemplate.h"
54#include "UObject/NameTypes.h"
55#include "UObject/UnrealNames.h"
61namespace UE::LLMPrivate
64bool HandleAssert(
bool bLog,
const TCHAR* Format, ...);
67template <
typename Type>
68bool TrueOnFirstCallOnly(
const Type&)
70 static bool bValue =
true;
78#if !USING_CODE_ANALYSIS
79 #define LLMTrueOnFirstCallOnly UE::LLMPrivate::TrueOnFirstCallOnly([]{})
81 #define LLMTrueOnFirstCallOnly false
84#define LLMCheckMessage(expr) TEXT("LLM check failed: %s [File:%s] [Line: %d]\r\n"), TEXT(#expr), TEXT(__FILE__), __LINE__
85#define LLMCheckfMessage(expr, format) TEXT("LLM check failed: %s [File:%s] [Line: %d]\r\n") format TEXT("\r\n"), TEXT(#expr), TEXT(__FILE__), __LINE__
86#define LLMEnsureMessage(expr) TEXT("LLM ensure failed: %s [File:%s] [Line: %d]\r\n"), TEXT(#expr), TEXT(__FILE__), __LINE__
88#define LLMCheck(expr) do { if (UNLIKELY(!(expr))) { UE::LLMPrivate::HandleAssert(true, LLMCheckMessage(expr)); FPlatformMisc::RaiseException(1
); } } while(false)
89#define LLMCheckf(expr,format,...) do { if (UNLIKELY(!(expr))) { UE::LLMPrivate::HandleAssert(true, LLMCheckfMessage(expr, format), ##__VA_ARGS__); FPlatformMisc::RaiseException(1
); } } while(false)
90#define LLMEnsure(expr) (LIKELY(!!(expr)) || UE::LLMPrivate::HandleAssert(LLMTrueOnFirstCallOnly, LLMEnsureMessage(expr)))
95#define LLMCheckf(expr,...)
96#define LLMEnsure(expr) (!!(expr))
100#define LLM_TAG_TYPE uint8
105#define LLM_MEMORY_OVERHEAD (600LL
*1024
*1024
)
108
109
110enum class ELLMTracker : uint8
119
120
121enum class ELLMTagSet : uint8
131#define LLM_ENUM_GENERIC_TAGS(macro)
132 macro(Untagged, "Untagged", NAME_None, NAME_None, -1
)
133 macro(Paused, "Paused", NAME_None, NAME_None, -1
)
134 macro(Total, "Total", GET_STATFNAME(STAT_TotalLLM), GET_STATFNAME(STAT_TrackedTotalSummaryLLM), -1
)
135 macro(Untracked, "Untracked", GET_STATFNAME(STAT_UntrackedLLM), GET_STATFNAME(STAT_TrackedTotalSummaryLLM), -1
)
136 macro(PlatformTotal, "Total", GET_STATFNAME(STAT_PlatformTotalLLM), NAME_None, -1
)
137 macro(TrackedTotal, "TrackedTotal", GET_STATFNAME(STAT_TrackedTotalLLM), GET_STATFNAME(STAT_TrackedTotalSummaryLLM), -1
)
138 macro(UntaggedTotal, "Untagged", GET_STATFNAME(STAT_UntaggedTotalLLM), NAME_None, -1
)
139 macro(WorkingSetSize, "WorkingSetSize", GET_STATFNAME(STAT_WorkingSetSizeLLM), GET_STATFNAME(STAT_TrackedTotalSummaryLLM), -1
)
140 macro(PagefileUsed, "PagefileUsed", GET_STATFNAME(STAT_PagefileUsedLLM), GET_STATFNAME(STAT_TrackedTotalSummaryLLM), -1
)
141 macro(PlatformTrackedTotal, "TrackedTotal", GET_STATFNAME(STAT_PlatformTrackedTotalLLM), NAME_None, -1
)
142 macro(PlatformUntaggedTotal, "Untagged", GET_STATFNAME(STAT_PlatformUntaggedTotalLLM), NAME_None, -1
)
143 macro(PlatformUntracked, "Untracked", GET_STATFNAME(STAT_PlatformUntrackedLLM), NAME_None, -1
)
144 macro(PlatformOverhead, "LLMOverhead", GET_STATFNAME(STAT_PlatformOverheadLLM), NAME_None, -1
)
145 macro(PlatformOSAvailable, "OSAvailable", GET_STATFNAME(STAT_PlatformOSAvailableLLM), NAME_None, -1
)
146 macro(FMalloc, "FMalloc", GET_STATFNAME(STAT_FMallocLLM), NAME_None, -1
)
147 macro(FMallocUnused, "FMallocUnused", GET_STATFNAME(STAT_FMallocUnusedLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
148 macro(ThreadStack, "ThreadStack", GET_STATFNAME(STAT_ThreadStackLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
149 macro(ThreadStackPlatform, "ThreadStack", GET_STATFNAME(STAT_ThreadStackPlatformLLM), NAME_None, -1
)
150 macro(ProgramSizePlatform, "ProgramSize", GET_STATFNAME(STAT_ProgramSizePlatformLLM), NAME_None, -1
)
151 macro(ProgramSize, "ProgramSize", GET_STATFNAME(STAT_ProgramSizeLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
152 macro(BackupOOMMemoryPoolPlatform, "OOMBackupPool", GET_STATFNAME(STAT_OOMBackupPoolPlatformLLM), NAME_None, -1
)
153 macro(BackupOOMMemoryPool, "OOMBackupPool", GET_STATFNAME(STAT_OOMBackupPoolLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
154 macro(GenericPlatformMallocCrash, "GenericPlatformMallocCrash", GET_STATFNAME(STAT_GenericPlatformMallocCrashLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
155 macro(GenericPlatformMallocCrashPlatform, "GenericPlatformMallocCrash", GET_STATFNAME(STAT_GenericPlatformMallocCrashPlatformLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
157 macro(EngineMisc, "EngineMisc", GET_STATFNAME(STAT_EngineMiscLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
159 macro(TaskGraphTasksMisc, "TaskGraphMiscTasks", GET_STATFNAME(STAT_TaskGraphTasksMiscLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
160 macro(LinearAllocator, "LinearAllocator", GET_STATFNAME(STAT_LinearAllocatorLLM), NAME_None, -1
)
161 macro(Audio, "Audio", GET_STATFNAME(STAT_AudioLLM), GET_STATFNAME(STAT_AudioSummaryLLM), -1
)
162 macro(AudioMisc, "AudioMisc", GET_STATFNAME(STAT_AudioMiscLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
163 macro(AudioSoundWaves, "AudioSoundWaves", GET_STATFNAME(STAT_AudioSoundWavesLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
164 macro(AudioSoundWaveProxies, "AudioSoundWaveProxies", GET_STATFNAME(STAT_AudioSoundWaveProxiesLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
165 macro(AudioMixer, "AudioMixer", GET_STATFNAME(STAT_AudioMixerLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
166 macro(AudioMixerPlugins, "AudioMixerPlugins", GET_STATFNAME(STAT_AudioMixerPluginsLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
167 macro(AudioPrecache, "AudioPrecache", GET_STATFNAME(STAT_AudioPrecacheLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
168 macro(AudioDecompress, "AudioDecompress", GET_STATFNAME(STAT_AudioDecompressLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
169 macro(AudioRealtimePrecache, "AudioRealtimePrecache", GET_STATFNAME(STAT_AudioRealtimePrecacheLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
170 macro(AudioFullDecompress, "AudioFullDecompress", GET_STATFNAME(STAT_AudioFullDecompressLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
171 macro(AudioStreamCache, "AudioStreamCache", GET_STATFNAME(STAT_AudioStreamCacheLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
172 macro(AudioStreamCacheCompressedData, "AudioStreamCacheCompressedData",GET_STATFNAME(STAT_AudioStreamCacheCompressedDataLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
173 macro(AudioSynthesis, "AudioSynthesis", GET_STATFNAME(STAT_AudioSynthesisLLM), GET_STATFNAME(STAT_AudioSummaryLLM), ELLMTag::Audio)
174 macro(RealTimeCommunications, "RealTimeCommunications", GET_STATFNAME(STAT_RealTimeCommunicationsLLM), NAME_None, -1
)
175 macro(FName, "FName", GET_STATFNAME(STAT_FNameLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
176 macro(Networking, "Networking", GET_STATFNAME(STAT_NetworkingLLM), GET_STATFNAME(STAT_NetworkingSummaryLLM), -1
)
177 macro(Meshes, "Meshes", GET_STATFNAME(STAT_MeshesLLM), GET_STATFNAME(STAT_MeshesSummaryLLM), -1
)
178 macro(Stats, "Stats", GET_STATFNAME(STAT_StatsLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
179 macro(Shaders, "Shaders", GET_STATFNAME(STAT_ShadersLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
180 macro(PSO, "PSO", GET_STATFNAME(STAT_PSOLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
181 macro(Textures, "Textures", GET_STATFNAME(STAT_TexturesLLM), GET_STATFNAME(STAT_TexturesSummaryLLM), -1
)
182 macro(TextureMetaData, "TextureMetaData", GET_STATFNAME(STAT_TextureMetaDataLLM), GET_STATFNAME(STAT_TexturesSummaryLLM), -1
)
183 macro(VirtualTextureSystem, "VirtualTextureSystem", GET_STATFNAME(STAT_VirtualTextureSystemLLM), GET_STATFNAME(STAT_TexturesSummaryLLM), -1
)
184 macro(RenderTargets, "RenderTargets", GET_STATFNAME(STAT_RenderTargetsLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
185 macro(SceneRender, "SceneRender", GET_STATFNAME(STAT_SceneRenderLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
186 macro(RHIMisc, "RHIMisc", GET_STATFNAME(STAT_RHIMiscLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
187 macro(AsyncLoading, "AsyncLoading", GET_STATFNAME(STAT_AsyncLoadingLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
192 macro(UObject, "UObject", GET_STATFNAME(STAT_UObjectLLM), GET_STATFNAME(STAT_UObjectSummaryLLM), -1
)
193 macro(Animation, "Animation", GET_STATFNAME(STAT_AnimationLLM), GET_STATFNAME(STAT_AnimationSummaryLLM), -1
)
195 macro(StaticMesh, "StaticMesh", GET_STATFNAME(STAT_StaticMeshLLM), GET_STATFNAME(STAT_StaticMeshSummaryLLM), ELLMTag::Meshes)
196 macro(Materials, "Materials", GET_STATFNAME(STAT_MaterialsLLM), GET_STATFNAME(STAT_MaterialsSummaryLLM), -1
)
197 macro(Particles, "Particles", GET_STATFNAME(STAT_ParticlesLLM), GET_STATFNAME(STAT_ParticlesSummaryLLM), -1
)
198 macro(Niagara, "Niagara", GET_STATFNAME(STAT_NiagaraLLM), GET_STATFNAME(STAT_NiagaraSummaryLLM), -1
)
199 macro(GPUSort, "GPUSort", GET_STATFNAME(STAT_GPUSortLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
200 macro(GC, "GC", GET_STATFNAME(STAT_GCLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
201 macro(UI, "UI", GET_STATFNAME(STAT_UILLM), GET_STATFNAME(STAT_UISummaryLLM), -1
)
202 macro(NavigationRecast, "NavigationRecast", GET_STATFNAME(STAT_NavigationRecastLLM), GET_STATFNAME(STAT_NavigationSummaryLLM), -1
)
203 macro(Physics, "Physics", GET_STATFNAME(STAT_PhysicsLLM), GET_STATFNAME(STAT_PhysicsSummaryLLM), -1
)
204 macro(PhysX, "PhysX", GET_STATFNAME(STAT_PhysXLLM), GET_STATFNAME(STAT_PhysXSummaryLLM), ELLMTag::Physics)
205 macro(PhysXGeometry, "PhysXGeometry", GET_STATFNAME(STAT_PhysXGeometryLLM), GET_STATFNAME(STAT_PhysXSummaryLLM), ELLMTag::Physics)
206 macro(PhysXTrimesh, "PhysXTrimesh", GET_STATFNAME(STAT_PhysXTrimeshLLM), GET_STATFNAME(STAT_PhysXSummaryLLM), ELLMTag::Physics)
207 macro(PhysXConvex, "PhysXConvex", GET_STATFNAME(STAT_PhysXConvexLLM), GET_STATFNAME(STAT_PhysXSummaryLLM), ELLMTag::Physics)
208 macro(PhysXAllocator, "PhysXAllocator", GET_STATFNAME(STAT_PhysXAllocatorLLM), GET_STATFNAME(STAT_PhysXSummaryLLM), ELLMTag::Physics)
209 macro(PhysXLandscape, "PhysXLandscape", GET_STATFNAME(STAT_PhysXLandscapeLLM), GET_STATFNAME(STAT_PhysXSummaryLLM), ELLMTag::Physics)
210 macro(Chaos, "Chaos", GET_STATFNAME(STAT_ChaosLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
211 macro(ChaosGeometry, "ChaosGeometry", GET_STATFNAME(STAT_ChaosGeometryLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
212 macro(ChaosAcceleration, "ChaosAcceleration", GET_STATFNAME(STAT_ChaosAccelerationLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
213 macro(ChaosParticles, "ChaosParticles", GET_STATFNAME(STAT_ChaosParticlesLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
214 macro(ChaosLandscape, "ChaosLandscape", GET_STATFNAME(STAT_ChaosLandscapeLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
215 macro(ChaosTrimesh, "ChaosTrimesh", GET_STATFNAME(STAT_ChaosTrimeshLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
216 macro(ChaosConvex, "ChaosConvex", GET_STATFNAME(STAT_ChaosConvexLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
217 macro(ChaosScene, "ChaosScene", GET_STATFNAME(STAT_ChaosSceneLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
218 macro(ChaosUpdate, "ChaosUpdate", GET_STATFNAME(STAT_ChaosUpdateLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
219 macro(ChaosActor, "ChaosActor", GET_STATFNAME(STAT_ChaosActorLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
220 macro(ChaosBody, "ChaosBody", GET_STATFNAME(STAT_ChaosBodyLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
221 macro(ChaosConstraint, "ChaosConstraint", GET_STATFNAME(STAT_ChaosConstraintLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
222 macro(ChaosMaterial, "ChaosMaterial", GET_STATFNAME(STAT_ChaosMaterialLLM), GET_STATFNAME(STAT_ChaosSummaryLLM), ELLMTag::Physics)
223 macro(EnginePreInitMemory, "EnginePreInit", GET_STATFNAME(STAT_EnginePreInitLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
224 macro(EngineInitMemory, "EngineInit", GET_STATFNAME(STAT_EngineInitLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
225 macro(RenderingThreadMemory, "RenderingThread", GET_STATFNAME(STAT_RenderingThreadLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
226 macro(LoadMapMisc, "LoadMapMisc", GET_STATFNAME(STAT_LoadMapMiscLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
227 macro(StreamingManager, "StreamingManager", GET_STATFNAME(STAT_StreamingManagerLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
228 macro(GraphicsPlatform, "Graphics", GET_STATFNAME(STAT_GraphicsPlatformLLM), NAME_None, -1
)
229 macro(FileSystem, "FileSystem", GET_STATFNAME(STAT_FileSystemLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
230 macro(Localization, "Localization", GET_STATFNAME(STAT_LocalizationLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
231 macro(AssetRegistry, "AssetRegistry", GET_STATFNAME(STAT_AssetRegistryLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
232 macro(ConfigSystem, "ConfigSystem", GET_STATFNAME(STAT_ConfigSystemLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
233 macro(InitUObject, "InitUObject", GET_STATFNAME(STAT_InitUObjectLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
234 macro(VideoRecording, "VideoRecording", GET_STATFNAME(STAT_VideoRecordingLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
235 macro(Replays, "Replays", GET_STATFNAME(STAT_ReplaysLLM), GET_STATFNAME(STAT_NetworkingSummaryLLM), ELLMTag::Networking)
236 macro(MaterialInstance, "MaterialInstance", GET_STATFNAME(STAT_MaterialInstanceLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
237 macro(SkeletalMesh, "SkeletalMesh", GET_STATFNAME(STAT_SkeletalMeshLLM), GET_STATFNAME(STAT_EngineSummaryLLM), ELLMTag::Meshes)
238 macro(InstancedMesh, "InstancedMesh", GET_STATFNAME(STAT_InstancedMeshLLM), GET_STATFNAME(STAT_EngineSummaryLLM), ELLMTag::Meshes)
239 macro(Landscape, "Landscape", GET_STATFNAME(STAT_LandscapeLLM), GET_STATFNAME(STAT_EngineSummaryLLM), ELLMTag::Meshes)
240 macro(CsvProfiler, "CsvProfiler", GET_STATFNAME(STAT_CsvProfilerLLM), GET_STATFNAME(STAT_EngineSummaryLLM), -1
)
241 macro(MediaStreaming, "MediaStreaming", GET_STATFNAME(STAT_MediaStreamingLLM), GET_STATFNAME(STAT_MediaStreamingSummaryLLM), -1
)
242 macro(ElectraPlayer, "ElectraPlayer", GET_STATFNAME(STAT_ElectraPlayerLLM), GET_STATFNAME(STAT_MediaStreamingSummaryLLM), ELLMTag::MediaStreaming)
243 macro(WMFPlayer, "WMFPlayer", GET_STATFNAME(STAT_WMFPlayerLLM), GET_STATFNAME(STAT_MediaStreamingSummaryLLM), ELLMTag::MediaStreaming)
244 macro(PlatformMMIO, "MMIO", GET_STATFNAME(STAT_PlatformMMIOLLM), NAME_None, -1
)
245 macro(PlatformVM, "Virtual Memory", GET_STATFNAME(STAT_PlatformVMLLM), NAME_None, -1
)
246 macro(CustomName, "CustomName", GET_STATFNAME(STAT_CustomName), NAME_None, -1
)
249
250
251enum class ELLMTag : LLM_TAG_TYPE
253#define LLM_ENUM(Enum,Str,Stat,Group,Parent) Enum,
254 LLM_ENUM_GENERIC_TAGS(LLM_ENUM)
261 PlatformTagStart = 111,
262 PlatformTagEnd = 149,
266 ProjectTagStart = 150,
271static_assert( ELLMTag::GenericTagCount <= ELLMTag::PlatformTagStart,
272 "too many LLM tags defined -- Instead of adding a new tag and updating the limits, please use the LLM_DECLARE_TAG macros below");
274static constexpr uint32 LLM_TAG_COUNT = 256;
275static constexpr uint32 LLM_CUSTOM_TAG_START = (int32)ELLMTag::PlatformTagStart;
276static constexpr uint32 LLM_CUSTOM_TAG_END = (int32)ELLMTag::ProjectTagEnd;
277static constexpr uint32 LLM_CUSTOM_TAG_COUNT = LLM_CUSTOM_TAG_END + 1 - LLM_CUSTOM_TAG_START;
280
281
282
283enum class ELLMAllocType
292extern const ANSICHAR* LLMGetTagNameANSI(ELLMTag Tag);
293extern const TCHAR* LLMGetTagName(ELLMTag Tag);
294UE_DEPRECATED(4.27,
"This function was an unused implementation detail; contact Epic if you need to keep its functionality.")
295extern FName LLMGetTagStatGroup(ELLMTag Tag);
296UE_DEPRECATED(4.27,
"This function was an unused implementation detail; contact Epic if you need to keep its functionality.")
297extern FName LLMGetTagStat(ELLMTag Tag);
300
301
303#define LLM_IF_ENABLED(x) if (!FLowLevelMemTracker::bIsDisabled) { x; }
304#define SCOPE_NAME PREPROCESSOR_JOIN(LLMScope,__LINE__)
311
312
313#define LLM_SCOPE(Tag) FLLMScope SCOPE_NAME(Tag, false , ELLMTagSet::None, ELLMTracker::Default);
315#define LLM_TAGSET_SCOPE(Tag, TagSet) FLLMScope SCOPE_NAME(Tag, false , TagSet, ELLMTracker::Default);
316#define LLM_SCOPE_BYNAME(Tag) static FName PREPROCESSOR_JOIN(LLMScope_Name,__LINE__)(Tag);
317 FLLMScope SCOPE_NAME(PREPROCESSOR_JOIN(LLMScope_Name,__LINE__), false , ELLMTagSet::None, ELLMTracker::Default);
318 UE_MEMSCOPE(PREPROCESSOR_JOIN(LLMScope_Name,__LINE__));
319#define LLM_SCOPE_BYTAG(TagDeclName) FLLMScope SCOPE_NAME(PREPROCESSOR_JOIN(LLMTagDeclaration_, TagDeclName).GetUniqueName(), false , ELLMTagSet::None, ELLMTracker::Default);
320 UE_MEMSCOPE(PREPROCESSOR_JOIN(LLMTagDeclaration_,TagDeclName).GetUniqueName());
321#define LLM_SCOPE_RENDER_RESOURCE(Tag) static const FString PREPROCESSOR_JOIN(LLMScope_NamePrefix,__LINE__)(TEXT("RenderResources."));
322 FName PREPROCESSOR_JOIN(LLMScope_Name,__LINE__)(PREPROCESSOR_JOIN(LLMScope_NamePrefix, __LINE__) + (Tag ? Tag : TEXT("Unknown")));
323 FLLMScope SCOPE_NAME(PREPROCESSOR_JOIN(LLMScope_Name,__LINE__), false , ELLMTagSet::Assets, ELLMTracker::Default, false );
324#define LLM_PLATFORM_SCOPE(Tag) FLLMScope SCOPE_NAME(Tag, false , ELLMTagSet::None, ELLMTracker::Platform);
325#define LLM_PLATFORM_SCOPE_BYNAME(Tag) static FName PREPROCESSOR_JOIN(LLMScope_Name,__LINE__)(Tag);
326 FLLMScope SCOPE_NAME(PREPROCESSOR_JOIN(LLMLLMScope_NameScope,__LINE__), false , ELLMTagSet::None, ELLMTracker::Platform);
327#define LLM_PLATFORM_SCOPE_BYTAG(TagDeclName) FLLMScope SCOPE_NAME(PREPROCESSOR_JOIN(LLMTagDeclaration_, TagDeclName).GetUniqueName(), false , ELLMTagSet::None, ELLMTracker::Platform);
330
331
332#define LLM_SCOPED_PAUSE_TRACKING(AllocType) FLLMPauseScope SCOPE_NAME(ELLMTag::Untagged, false , 0
, ELLMTracker::Max, AllocType);
333#define LLM_SCOPED_PAUSE_TRACKING_FOR_TRACKER(Tracker, AllocType) FLLMPauseScope SCOPE_NAME(ELLMTag::Untagged, false , 0
, Tracker, AllocType);
334#define LLM_SCOPED_PAUSE_TRACKING_WITH_ENUM_AND_AMOUNT(Tag, Amount, Tracker, AllocType) FLLMPauseScope SCOPE_NAME(Tag, false , Amount, Tracker, AllocType);
337
338
339#define LLM_REALLOC_SCOPE(Ptr) FLLMScopeFromPtr SCOPE_NAME(Ptr, ELLMTracker::Default);
340#define LLM_REALLOC_PLATFORM_SCOPE(Ptr) FLLMScopeFromPtr SCOPE_NAME(Ptr, ELLMTracker::Platform);
343
344
345#define LLM_DUMP_TAG() FLowLevelMemTracker::Get().DumpTag(ELLMTracker::Default,__FILE__,__LINE__)
346#define LLM_DUMP_PLATFORM_TAG() FLowLevelMemTracker::Get().DumpTag(ELLMTracker::Platform,__FILE__,__LINE__)
349
350
351
352
353
354
355
356
357
358
359
360
361#define LLM_DEFINE_TAG(UniqueNameWithUnderscores, ...) FLLMTagDeclaration PREPROCESSOR_JOIN(LLMTagDeclaration_, UniqueNameWithUnderscores)(TEXT(#UniqueNameWithUnderscores), ##__VA_ARGS__)
364
365
366
367
368
369
370
371#define LLM_DECLARE_TAG(UniqueNameWithUnderscores) extern FLLMTagDeclaration PREPROCESSOR_JOIN(LLMTagDeclaration_, UniqueNameWithUnderscores)
372#define LLM_DECLARE_TAG_API(UniqueNameWithUnderscores, ModuleAPI) extern ModuleAPI FLLMTagDeclaration PREPROCESSOR_JOIN(LLMTagDeclaration_, UniqueNameWithUnderscores)
375#define LLM_TAG_NAME(UniqueNameWithUnderscores) (PREPROCESSOR_JOIN(LLMTagDeclaration_, UniqueNameWithUnderscores).GetUniqueName())
378
379
380
381
382#define LLM_DEFINE_BOOTSTRAP_TAG(UniqueNameWithUnderscores, ...)
383 FLLMTagDeclaration& PREPROCESSOR_JOIN(GetLLMTagDeclaration_, UniqueNameWithUnderscores)()
385 static FLLMTagDeclaration PREPROCESSOR_JOIN(LLMTagDeclaration_, UniqueNameWithUnderscores)(TEXT(#UniqueNameWithUnderscores), ##__VA_ARGS__);
386 return PREPROCESSOR_JOIN(LLMTagDeclaration_, UniqueNameWithUnderscores);
388#define LLM_SCOPE_BY_BOOTSTRAP_TAG(TagDeclName) FLLMScope SCOPE_NAME(PREPROCESSOR_JOIN(GetLLMTagDeclaration_, TagDeclName)().GetUniqueName(), false , ELLMTagSet::None, ELLMTracker::Default);
389 UE_MEMSCOPE(PREPROCESSOR_JOIN(GetLLMTagDeclaration_,TagDeclName)().GetUniqueName());
390#define LLM_DECLARE_BOOTSTRAP_TAG(UniqueNameWithUnderscores) extern FLLMTagDeclaration& PREPROCESSOR_JOIN(GetLLMTagDeclaration_, UniqueNameWithUnderscores)();
391#define LLM_DECLARE_BOOTSTRAP_TAG_API(UniqueNameWithUnderscores, ModuleAPI) extern ModuleAPI FLLMTagDeclaration& PREPROCESSOR_JOIN(GetLLMTagDeclaration_, UniqueNameWithUnderscores)();
393typedef void*(*LLMAllocFunction)(size_t);
394typedef void(*LLMFreeFunction)(
void*, size_t);
396class FLLMTagDeclaration;
398struct FLLMTagSetAllocationFilter;
400namespace UE::LLMPrivate
403class FLLMCsvProfilerWriter;
405class FLLMThreadState;
406class FLLMTraceWriter;
410class FTagDataNameMap;
412namespace AllocatorPrivate
418
419
420
427 static FLLMAllocator*& Get();
429 void Initialise(LLMAllocFunction InAlloc, LLMFreeFunction InFree, int32 InPageSize);
431 void* Alloc(size_t Size);
432 void* Malloc(size_t Size);
433 void Free(
void* Ptr, size_t Size);
434 void* Realloc(
void* Ptr, size_t OldSize, size_t NewSize);
435 int64 GetTotal()
const;
437 template <
typename T,
typename... ArgsType>
438 T* New(ArgsType&&... Args)
440 T* Ptr =
reinterpret_cast<T*>(Alloc(
sizeof(T)));
441 new (Ptr) T(Forward<ArgsType>(Args)...);
445 template <
typename T>
451 Free(Ptr,
sizeof(T));
455 LLMAllocFunction GetPlatformAlloc()
457 return PlatformAlloc;
459 LLMFreeFunction GetPlatformFree()
465 void* AllocPages(size_t Size);
466 void FreePages(
void* Ptr, size_t Size);
467 int32 GetBinIndex(size_t Size)
const;
469 FCriticalSection CriticalSection;
470 LLMAllocFunction PlatformAlloc;
471 LLMFreeFunction PlatformFree;
472 AllocatorPrivate::FBin* Bins;
477 friend struct UE::LLMPrivate::AllocatorPrivate::FPage;
478 friend struct UE::LLMPrivate::AllocatorPrivate::FBin;
481enum class ETagReferenceSource
493struct UE_DEPRECATED(4.27,
"FLLMCustomTag was an implementation detail that has been modified, switch to FLLMTagInfo or to your own local struct") FLLMCustomTag
498 FName SummaryStatName;
506 FName SummaryStatName;
507 int32 ParentTag = -1;
511class FLowLevelMemTracker
516 inline static FLowLevelMemTracker& Get()
519 return *TrackerInstance;
524 static FLowLevelMemTracker& Construct();
526 static bool IsEnabled();
529
530
531
532 void ProcessCommandLine(
const TCHAR* CmdLine);
535 uint64 GetTotalTrackedMemory(ELLMTracker Tracker);
538
539
540
541
542
543
544
545
546
547
548 void OnLowLevelAlloc(ELLMTracker Tracker,
const void* Ptr, uint64 Size, ELLMTag DefaultTag = ELLMTag::Untagged,
549 ELLMAllocType AllocType = ELLMAllocType::None,
bool bTrackInMemPro =
true);
550 void OnLowLevelAlloc(ELLMTracker Tracker,
const void* Ptr, uint64 Size, FName DefaultTag,
551 ELLMAllocType AllocType = ELLMAllocType::None,
bool bTrackInMemPro =
true);
554
555
556
557
558
559
560
561
562 void OnLowLevelFree(ELLMTracker Tracker,
const void* Ptr,
563 ELLMAllocType AllocType = ELLMAllocType::None,
bool bTrackInMemPro =
true);
566
567
568
569
570
571
572
573
574 void OnLowLevelChangeInMemoryUse(ELLMTracker Tracker, int64 DeltaMemory, ELLMTag DefaultTag = ELLMTag::Untagged, ELLMAllocType AllocType = ELLMAllocType::None);
575 void OnLowLevelChangeInMemoryUse(ELLMTracker Tracker, int64 DeltaMemory, FName DefaultTag, ELLMAllocType AllocType = ELLMAllocType::None);
578 void OnLowLevelAllocMoved(ELLMTracker Tracker,
const void* Dest,
const void* Source,
579 ELLMAllocType AllocType = ELLMAllocType::None);
582 void UpdateStatsPerFrame(
const TCHAR* LogName=
nullptr);
587 void SetProgramSize(uint64 InProgramSize);
590 bool Exec(
const TCHAR* Cmd, FOutputDevice& Ar);
593 bool IsTagSetActive(ELLMTagSet Set);
596 bool ShouldReduceThreads();
599 UE_DEPRECATED(4.27,
"Tags have been changed to FNames and the old ELLMTag is now only the top-level coarse tag. Use GetActivateTagData instead to get the current Tag instead of its toplevel parent.")
600 int64 GetActiveTag(ELLMTracker Tracker);
603 const UE::LLMPrivate::FTagData* GetActiveTagData(ELLMTracker Tracker, ELLMTagSet TagSet = ELLMTagSet::None);
606 void RegisterPlatformTag(int32 Tag,
const TCHAR* Name, FName StatName, FName SummaryStatName, int32 ParentTag = -1);
607 void RegisterProjectTag(int32 Tag,
const TCHAR* Name, FName StatName, FName SummaryStatName, int32 ParentTag = -1);
610 TArray<
const UE::LLMPrivate::FTagData*> GetTrackedTags(ELLMTagSet TagSet = ELLMTagSet::None);
613 TArray<
const UE::LLMPrivate::FTagData*> GetTrackedTags(ELLMTracker Tracker, ELLMTagSet TagSet = ELLMTagSet::None);
615 void GetTrackedTagsNamesWithAmount(TMap<FName, uint64>& TagsNamesWithAmount, ELLMTracker Tracker, ELLMTagSet TagSet);
616 void GetTrackedTagsNamesWithAmountFiltered(TMap<FName, uint64>& TagsNamesWithAmount, ELLMTracker Tracker, ELLMTagSet TagSet, TArray<FLLMTagSetAllocationFilter>& Filters);
619 bool FindTagByName(
const TCHAR* Name, uint64& OutTag, ELLMTagSet InTagSet = ELLMTagSet::None)
const;
621 UE_DEPRECATED(4.27,
"Use FindTagDisplayName instead")
622 const TCHAR* FindTagName(uint64 Tag)
const;
625 FName FindTagDisplayName(uint64 Tag)
const;
628 FName GetTagDisplayName(
const UE::LLMPrivate::FTagData* TagData)
const;
631 FString GetTagDisplayPathName(
const UE::LLMPrivate::FTagData* TagData)
const;
634 FName GetTagUniqueName(
const UE::LLMPrivate::FTagData* TagData)
const;
637 int64 GetTagAmountForTracker(ELLMTracker Tracker, ELLMTag Tag,
bool bPeakAmount =
false);
640 int64 GetTagAmountForTracker(ELLMTracker Tracker,
const UE::LLMPrivate::FTagData* TagData,
bool bPeakAmount =
false);
642 int64 GetTagAmountForTracker(ELLMTracker Tracker, FName Tag, ELLMTagSet TagSet,
bool bPeakAmount =
false);
645 void SetTagAmountForTracker(ELLMTracker Tracker, ELLMTag Tag, int64 Amount,
bool bAddToTotal);
648 uint64 DumpTag(ELLMTracker Tracker,
const char* FileName,
int LineNumber);
651 void PublishDataSingleFrame();
653 enum class EDumpFormat
658 void DumpToLog(EDumpFormat DumpFormat = EDumpFormat::PlainText, FOutputDevice* OutputDevice =
nullptr);
663 FLowLevelMemTracker();
665 ~FLowLevelMemTracker();
668
669
670
671
672
673 void BootstrapInitialise();
675 bool IsBootstrapping()
const {
return bIsBootstrapping; }
679 void InitialiseProgramSize();
681 class UE::LLMPrivate::FLLMTracker* GetTracker(ELLMTracker Tracker);
685 void SortTags(UE::LLMPrivate::FTagDataArray*& OutOldTagDatas);
686 void PublishDataPerFrame(
const TCHAR* LogName);
688 void RegisterCustomTagInternal(int32 Tag, ELLMTagSet TagSet,
const TCHAR* Name, FName StatName, FName SummaryStatName, int32 ParentTag = -1);
690
691
692
693 void BootstrapTagDatas();
695 void FinishInitialise();
696 void InitialiseTagDatas_SetLLMTagNames();
697 void InitialiseTagDatas_FinishRegister();
698 void InitialiseTagDatas();
699 void ClearTagDatas();
700 void RegisterTagDeclaration(FLLMTagDeclaration& TagDeclaration);
701 UE::LLMPrivate::FTagData& RegisterTagData(FName Name, FName DisplayName, FName ParentName, FName StatName, FName SummaryStatName,
bool bHasEnumTag, ELLMTag EnumTag,
bool bIsStatTag, UE::LLMPrivate::ETagReferenceSource ReferenceSource, ELLMTagSet TagSet = ELLMTagSet::None);
703 void FinishConstruct(UE::LLMPrivate::FTagData* TagData, UE::LLMPrivate::ETagReferenceSource ReferenceSource);
704 void ReportDuplicateTagName(UE::LLMPrivate::FTagData* TagData, UE::LLMPrivate::ETagReferenceSource ReferenceSource);
706 const UE::LLMPrivate::FTagData* FindOrAddTagData(ELLMTag EnumTag, UE::LLMPrivate::ETagReferenceSource ReferenceSource = UE::LLMPrivate::ETagReferenceSource::FunctionAPI);
707 const UE::LLMPrivate::FTagData* FindOrAddTagData(FName Name, ELLMTagSet TagSet,
bool bIsStatData=
false, UE::LLMPrivate::ETagReferenceSource ReferenceSource = UE::LLMPrivate::ETagReferenceSource::FunctionAPI);
708 const UE::LLMPrivate::FTagData* FindTagData(ELLMTag EnumTag, UE::LLMPrivate::ETagReferenceSource ReferenceSource = UE::LLMPrivate::ETagReferenceSource::FunctionAPI);
709 const UE::LLMPrivate::FTagData* FindTagData(FName Name, ELLMTagSet TagSet, UE::LLMPrivate::ETagReferenceSource ReferenceSource = UE::LLMPrivate::ETagReferenceSource::FunctionAPI);
711 friend class FLLMPauseScope;
712 friend class FLLMScope;
713 friend class FLLMScopeFromPtr;
714 friend class UE::LLMPrivate::FLLMCsvWriter;
715 friend class UE::LLMPrivate::FLLMTracker;
716 friend class UE::LLMPrivate::FLLMThreadState;
717 friend class UE::LLMPrivate::FLLMTraceWriter;
718 friend class UE::LLMPrivate::FLLMCsvProfilerWriter;
719 friend void GlobalRegisterTagDeclaration(FLLMTagDeclaration& TagDeclaration);
721 UE::LLMPrivate::FLLMAllocator Allocator;
723 UE::LLMPrivate::FTagDataArray* TagDatas;
725 UE::LLMPrivate::FTagDataNameMap* TagDataNameMap;
727 UE::LLMPrivate::FTagData** TagDataEnumMap;
729 UE::LLMPrivate::FLLMTracker* Trackers[
static_cast<int32>(ELLMTracker::Max)];
731 mutable FRWLock TagDataLock;
732 FCriticalSection UpdateLock;
735 int64 MemoryUsageCurrentOverhead;
736 int64 MemoryUsagePlatformTotalUntracked;
738 bool ActiveSets[(int32)ELLMTagSet::Max];
740 bool bFirstTimeUpdating;
742 bool bCsvWriterEnabled;
743 bool bTraceWriterEnabled;
744 bool bInitialisedTracking;
745 bool bIsBootstrapping;
746 bool bFullyInitialised;
747 bool bConfigurationComplete;
750 bool bPublishSingleFrame;
752 static FLowLevelMemTracker* TrackerInstance;
754 static bool bIsDisabled;
761 FLLMScope(FName TagName,
bool bIsStatTag, ELLMTagSet InTagSet, ELLMTracker InTracker,
bool bOverride =
true)
763 if (!FLowLevelMemTracker::bIsDisabled)
765 Init(TagName, bIsStatTag, InTagSet, InTracker, bOverride);
768 FLLMScope(ELLMTag TagEnum,
bool bIsStatTag, ELLMTagSet InTagSet, ELLMTracker InTracker,
bool bOverride =
true)
770 if (!FLowLevelMemTracker::bIsDisabled)
772 Init(TagEnum, bIsStatTag, InTagSet, InTracker, bOverride);
777 FLLMScope(
const UE::LLMPrivate::FTagData* TagData,
bool bIsStatTag, ELLMTagSet Set, ELLMTracker Tracker,
bool bOverride =
true)
779 if (!FLowLevelMemTracker::bIsDisabled)
781 Init(TagData, bIsStatTag, Set, Tracker, bOverride);
794 void Init(FName TagName,
bool bIsStatTag, ELLMTagSet InTagSet, ELLMTracker InTracker,
bool bOverride =
true);
795 void Init(ELLMTag TagEnum,
bool bIsStatTag, ELLMTagSet InTagSet, ELLMTracker InTracker,
bool bOverride =
true);
796 void Init(
const UE::LLMPrivate::FTagData* TagData,
bool bIsStatTag, ELLMTagSet InTagSet, ELLMTracker InTracker,
bool bOverride =
true);
799 ELLMTracker Tracker{};
800 bool bEnabled =
false;
808 FLLMPauseScope(FName TagName,
bool bIsStatTag, uint64 Amount, ELLMTracker TrackerToPause, ELLMAllocType InAllocType);
809 FLLMPauseScope(ELLMTag TagEnum,
bool bIsStatTag, uint64 Amount, ELLMTracker TrackerToPause, ELLMAllocType InAllocType);
812 void Init(FName TagName, ELLMTag EnumTag,
bool bIsEnumTag,
bool bIsStatTag, uint64 Amount,
813 ELLMTracker TrackerToPause, ELLMAllocType InAllocType);
814 ELLMTracker PausedTracker;
815 ELLMAllocType AllocType;
820class FLLMScopeFromPtr
823 FLLMScopeFromPtr(
void* Ptr, ELLMTracker Tracker);
827 bool bEnabled[
static_cast<int32>(ELLMTagSet::Max)];
830 inline void DisableAll()
832 FMemory::Memzero(bEnabled);
837class FLLMTagDeclaration
840 FLLMTagDeclaration(
const TCHAR* InCPPName,
const FName InDisplayName=NAME_None, FName InParentTagName = NAME_None, FName InStatName = NAME_None, FName InSummaryStatName = NAME_None, ELLMTagSet TagSet = ELLMTagSet::None);
841 FName GetUniqueName()
const {
return UniqueName; }
843 typedef void (*FCreationCallback)(FLLMTagDeclaration&);
846 static void AddCreationCallback(FCreationCallback InCallback);
847 static void ClearCreationCallbacks();
848 static TArrayView<FCreationCallback> GetCreationCallbacks();
849 static FLLMTagDeclaration* GetList();
854 void ConstructUniqueName();
856 const TCHAR* CPPName;
861 FName SummaryStatName;
863 FLLMTagDeclaration* Next =
nullptr;
865 friend class FLowLevelMemTracker;
866 friend class FTagTrace;
870struct FLLMTagSetAllocationFilter
881#define LLM_IF_ENABLED(...)
882#define LLM_SCOPE(...)
883#define LLM_TAGSET_SCOPE(...)
884#define LLM_SCOPE_BYNAME(...)
885#define LLM_SCOPE_BYTAG(...)
886#define LLM_SCOPE_RENDER_RESOURCE(...)
887#define LLM_PLATFORM_SCOPE(...)
888#define LLM_PLATFORM_SCOPE_BYNAME(...)
889#define LLM_PLATFORM_SCOPE_BYTAG(...)
890#define LLM_REALLOC_SCOPE(...)
891#define LLM_REALLOC_PLATFORM_SCOPE(...)
892#define LLM_SCOPED_PAUSE_TRACKING(...)
893#define LLM_SCOPED_PAUSE_TRACKING_FOR_TRACKER(...)
894#define LLM_SCOPED_PAUSE_TRACKING_WITH_ENUM_AND_AMOUNT(...)
895#define LLM_DUMP_TAG()
896#define LLM_DUMP_PLATFORM_TAG()
897#define LLM_DEFINE_TAG(...)
898#define LLM_DECLARE_TAG(...)
899#define LLM_DECLARE_TAG_API(...)
900#define LLM_DEFINE_BOOTSTRAP_TAG(...)
901#define LLM_SCOPE_BY_BOOTSTRAP_TAG(...)
902#define LLM_DECLARE_BOOTSTRAP_TAG(...)
903#define LLM_DECLARE_BOOTSTRAP_TAG_API(...)
#define PLATFORM_SUPPORTS_LLM
#define ENABLE_LOW_LEVEL_MEM_TRACKER
#define LLM_ENABLED_ON_PLATFORM
#define LLM_ENABLED_IN_CONFIG