aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/memtrack/memorytrace.cpp
blob: 065a6f8ecf6bf1afcf7ca4392fb40112be1648f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/memory/memorytrace.h>
#include <zencore/memory/tagtrace.h>

#include "callstacktrace.h"
#include "tracemalloc.h"
#include "vatrace.h"

#include <zencore/commandline.h>
#include <zencore/enumflags.h>
#include <zencore/guardvalue.h>
#include <zencore/intmath.h>
#include <zencore/string.h>
#include <zencore/trace.h>

#include <string.h>

#if ZEN_PLATFORM_WINDOWS
#	include <shellapi.h>
#endif

class FMalloc;

#if ZEN_WITH_TRACE
namespace zen {
UE_TRACE_CHANNEL_DEFINE(MemAllocChannel, "Memory allocations", true)
}
#endif

#if UE_MEMORY_TRACE_ENABLED

////////////////////////////////////////////////////////////////////////////////

namespace zen {

void MemoryTrace_InitTags(FMalloc*);
void MemoryTrace_EnableTracePump();

}  // namespace zen

////////////////////////////////////////////////////////////////////////////////
namespace {
// Controls how often time markers are emitted (must be POW2-1 as this is used as a mask)
constexpr uint32_t MarkerSamplePeriod = 128 - 1;

// Number of shifted bits to SizeLower
constexpr uint32_t SizeShift = 3;

// Counter to track when time marker is emitted
std::atomic<uint32_t> GMarkerCounter(0);

// If enabled also pumps the Trace system itself. Used on process shutdown
// when worker thread has been killed, but memory events still occurs.
bool GDoPumpTrace;

// Temporarily disables any internal operation that causes allocations. Used to
// avoid recursive behaviour when memory tracing needs to allocate memory through
// TraceMalloc.
thread_local bool GDoNotAllocateInTrace;

// Set on initialization; on some platforms we hook allocator functions very early
// before Trace has the ability to allocate memory.
bool GTraceAllowed;
}  // namespace

////////////////////////////////////////////////////////////////////////////////
namespace UE { namespace Trace {
	TRACELOG_API void Update();
}}	// namespace UE::Trace

namespace zen {

////////////////////////////////////////////////////////////////////////////////
UE_TRACE_EVENT_BEGIN(Memory, Init, NoSync | Important)
	UE_TRACE_EVENT_FIELD(uint64_t, PageSize)  // new in UE 5.5
	UE_TRACE_EVENT_FIELD(uint32_t, MarkerPeriod)
	UE_TRACE_EVENT_FIELD(uint8, Version)
	UE_TRACE_EVENT_FIELD(uint8, MinAlignment)
	UE_TRACE_EVENT_FIELD(uint8, SizeShift)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, Marker)
	UE_TRACE_EVENT_FIELD(uint64_t, Cycle)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, Alloc)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint32_t, Size)
	UE_TRACE_EVENT_FIELD(uint8, AlignmentPow2_SizeLower)
	UE_TRACE_EVENT_FIELD(uint8, RootHeap)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, AllocSystem)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint32_t, Size)
	UE_TRACE_EVENT_FIELD(uint8, AlignmentPow2_SizeLower)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, AllocVideo)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint32_t, Size)
	UE_TRACE_EVENT_FIELD(uint8, AlignmentPow2_SizeLower)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, Free)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint8, RootHeap)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, FreeSystem)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, FreeVideo)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, ReallocAlloc)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint32_t, Size)
	UE_TRACE_EVENT_FIELD(uint8, AlignmentPow2_SizeLower)
	UE_TRACE_EVENT_FIELD(uint8, RootHeap)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, ReallocAllocSystem)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint32_t, Size)
	UE_TRACE_EVENT_FIELD(uint8, AlignmentPow2_SizeLower)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, ReallocFree)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint8, RootHeap)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, ReallocFreeSystem)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, MemorySwapOp)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)	 // page fault real address
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint32_t, CompressedSize)
	UE_TRACE_EVENT_FIELD(uint8, SwapOp)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, HeapSpec, NoSync | Important)
	UE_TRACE_EVENT_FIELD(HeapId, Id)
	UE_TRACE_EVENT_FIELD(HeapId, ParentId)
	UE_TRACE_EVENT_FIELD(uint16, Flags)
	UE_TRACE_EVENT_FIELD(UE::Trace::WideString, Name)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, HeapMarkAlloc)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(uint16, Flags)
	UE_TRACE_EVENT_FIELD(HeapId, Heap)
UE_TRACE_EVENT_END()

UE_TRACE_EVENT_BEGIN(Memory, HeapUnmarkAlloc)
	UE_TRACE_EVENT_FIELD(uint64_t, Address)
	UE_TRACE_EVENT_FIELD(uint32_t, CallstackId)
	UE_TRACE_EVENT_FIELD(HeapId, Heap)
UE_TRACE_EVENT_END()

// If the layout of the above events is changed, bump this version number.
// version 1: Initial version (UE 5.0, UE 5.1)
// version 2: Added CallstackId for Free events and also for HeapMarkAlloc, HeapUnmarkAlloc events (UE 5.2).
constexpr uint8 MemoryTraceVersion = 2;

////////////////////////////////////////////////////////////////////////////////
class FMallocWrapper : public FMalloc
{
public:
	FMallocWrapper(FMalloc* InMalloc);

private:
	struct FCookie
	{
		uint64_t Tag  : 16;
		uint64_t Bias : 8;
		uint64_t Size : 40;
	};

	static uint32_t GetActualAlignment(SIZE_T Size, uint32_t Alignment);

	virtual void* Malloc(SIZE_T Size, uint32_t Alignment) override;
	virtual void* Realloc(void* PrevAddress, SIZE_T NewSize, uint32_t Alignment) override;
	virtual void  Free(void* Address) override;
	virtual bool  GetAllocationSize(void* Address, SIZE_T& SizeOut) override { return InnerMalloc->GetAllocationSize(Address, SizeOut); }
	virtual void  OnMallocInitialized() override { InnerMalloc->OnMallocInitialized(); }

	FMalloc* InnerMalloc;
};

////////////////////////////////////////////////////////////////////////////////
FMallocWrapper::FMallocWrapper(FMalloc* InMalloc) : InnerMalloc(InMalloc)
{
}

////////////////////////////////////////////////////////////////////////////////
uint32_t
FMallocWrapper::GetActualAlignment(SIZE_T Size, uint32_t Alignment)
{
	// Defaults; if size is < 16 then alignment is 8 else 16.
	uint32_t DefaultAlignment = 8 << uint32_t(Size >= 16);
	return (Alignment < DefaultAlignment) ? DefaultAlignment : Alignment;
}

////////////////////////////////////////////////////////////////////////////////
void*
FMallocWrapper::Malloc(SIZE_T Size, uint32_t Alignment)
{
	uint32_t ActualAlignment = GetActualAlignment(Size, Alignment);
	void*	 Address		 = InnerMalloc->Malloc(Size, Alignment);

	MemoryTrace_Alloc((uint64_t)Address, Size, ActualAlignment);

	return Address;
}

////////////////////////////////////////////////////////////////////////////////
void*
FMallocWrapper::Realloc(void* PrevAddress, SIZE_T NewSize, uint32_t Alignment)
{
	// This simplifies things and means reallocs trace events are true reallocs
	if (PrevAddress == nullptr)
	{
		return Malloc(NewSize, Alignment);
	}

	MemoryTrace_ReallocFree((uint64_t)PrevAddress);

	void* RetAddress = InnerMalloc->Realloc(PrevAddress, NewSize, Alignment);

	Alignment = GetActualAlignment(NewSize, Alignment);
	MemoryTrace_ReallocAlloc((uint64_t)RetAddress, NewSize, Alignment);

	return RetAddress;
}

////////////////////////////////////////////////////////////////////////////////
void
FMallocWrapper::Free(void* Address)
{
	if (Address == nullptr)
	{
		return;
	}

	MemoryTrace_Free((uint64_t)Address);

	void* InnerAddress = Address;

	return InnerMalloc->Free(InnerAddress);
}

////////////////////////////////////////////////////////////////////////////////
template<class T>
class alignas(alignof(T)) FUndestructed
{
public:
	template<typename... ArgTypes>
	void Construct(ArgTypes... Args)
	{
		::new (Buffer) T(Args...);
		bIsConstructed = true;
	}

	bool IsConstructed() const { return bIsConstructed; }

	T* operator&() { return (T*)Buffer; }
	T* operator->() { return (T*)Buffer; }

protected:
	uint8 Buffer[sizeof(T)];
	bool  bIsConstructed;
};

////////////////////////////////////////////////////////////////////////////////
static FUndestructed<FTraceMalloc> GTraceMalloc;

////////////////////////////////////////////////////////////////////////////////
static EMemoryTraceInit
MemoryTrace_ShouldEnable(const TraceOptions& Options)
{
	EMemoryTraceInit Mode = EMemoryTraceInit::Disabled;

	// Process any command line trace options
	//
	// Note that calls can come into this function before we enter the regular main function
	// and we can therefore not rely on the regular command line parsing for the application

	using namespace std::literals;

	auto ProcessTraceArg = [&](const std::string_view& Arg) {
		if (Arg == "memalloc"sv)
		{
			Mode |= EMemoryTraceInit::AllocEvents;
		}
		else if (Arg == "callstack"sv)
		{
			Mode |= EMemoryTraceInit::Callstacks;
		}
		else if (Arg == "memtag"sv)
		{
			Mode |= EMemoryTraceInit::Tags;
		}
		else if (Arg == "memory"sv)
		{
			Mode |= EMemoryTraceInit::Full;
		}
		else if (Arg == "memory_light"sv)
		{
			Mode |= EMemoryTraceInit::Light;
		}
	};

	IterateCommaSeparatedValue(Options.Channels, ProcessTraceArg);
	return Mode;
}

////////////////////////////////////////////////////////////////////////////////
FMalloc*
MemoryTrace_CreateInternal(FMalloc* InMalloc, EMemoryTraceInit Mode)
{
	using namespace zen;

	// If allocation events are not desired we don't need to do anything, even
	// if user has enabled only callstacks it will be enabled later.
	if (!EnumHasAnyFlags(Mode, EMemoryTraceInit::AllocEvents))
	{
		return InMalloc;
	}

	// Some OSes (i.e. Windows) will terminate all threads except the main
	// one as part of static deinit. However we may receive more memory
	// trace events that would get lost as Trace's worker thread has been
	// terminated. So flush the last remaining memory events trace needs
	// to be updated which we will do that in response to to memory events.
	// We'll use an atexit can to know when Trace is probably no longer
	// getting ticked.
	atexit([]() { MemoryTrace_EnableTracePump(); });

	GTraceMalloc.Construct(InMalloc);

	// Both tag and callstack tracing need to use the wrapped trace malloc
	// so we can break out tracing memory overhead (and not cause recursive behaviour).
	if (EnumHasAnyFlags(Mode, EMemoryTraceInit::Tags))
	{
		MemoryTrace_InitTags(&GTraceMalloc);
	}

	if (EnumHasAnyFlags(Mode, EMemoryTraceInit::Callstacks))
	{
		CallstackTrace_Create(&GTraceMalloc);
	}

	static FUndestructed<FMallocWrapper> SMallocWrapper;
	SMallocWrapper.Construct(InMalloc);

	return &SMallocWrapper;
}

////////////////////////////////////////////////////////////////////////////////
FMalloc*
MemoryTrace_CreateInternal(FMalloc* InMalloc, const TraceOptions& Options)
{
	const EMemoryTraceInit Mode = MemoryTrace_ShouldEnable(Options);
	return MemoryTrace_CreateInternal(InMalloc, Mode);
}

////////////////////////////////////////////////////////////////////////////////
FMalloc*
MemoryTrace_Create(FMalloc* InMalloc, const TraceOptions& Options)
{
	FMalloc* OutMalloc = MemoryTrace_CreateInternal(InMalloc, Options);

	if (OutMalloc != InMalloc)
	{
#	if PLATFORM_SUPPORTS_TRACE_WIN32_VIRTUAL_MEMORY_HOOKS
		FVirtualWinApiHooks::Initialize(false);
#	endif
	}

	return OutMalloc;
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_Initialize()
{
	// At this point we initialized the system to allow tracing.
	GTraceAllowed = true;

	const int MIN_ALIGNMENT = 8;

	UE_TRACE_LOG(Memory, Init, MemAllocChannel)
		<< Init.PageSize(4096) << Init.MarkerPeriod(MarkerSamplePeriod + 1) << Init.Version(MemoryTraceVersion)
		<< Init.MinAlignment(uint8(MIN_ALIGNMENT)) << Init.SizeShift(uint8(SizeShift));

	const HeapId SystemRootHeap = MemoryTrace_RootHeapSpec(u"System memory");
	ZEN_ASSERT(SystemRootHeap == EMemoryTraceRootHeap::SystemMemory);
	const HeapId VideoRootHeap = MemoryTrace_RootHeapSpec(u"Video memory");
	ZEN_ASSERT(VideoRootHeap == EMemoryTraceRootHeap::VideoMemory);

	static_assert((1 << SizeShift) - 1 <= MIN_ALIGNMENT, "Not enough bits to pack size fields");

#	if !UE_MEMORY_TRACE_LATE_INIT
	// On some platforms callstack initialization cannot happen this early in the process. It is initialized
	// in other locations when UE_MEMORY_TRACE_LATE_INIT is defined. Until that point allocations cannot have
	// callstacks.
	CallstackTrace_Initialize();
#	endif
}

void
MemoryTrace_Shutdown()
{
	// Disable any further activity
	GTraceAllowed = false;
}

////////////////////////////////////////////////////////////////////////////////
bool
MemoryTrace_IsActive()
{
	return GTraceAllowed;
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_EnableTracePump()
{
	GDoPumpTrace = true;
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_UpdateInternal()
{
	const uint32_t TheCount = GMarkerCounter.fetch_add(1, std::memory_order_relaxed);
	if ((TheCount & MarkerSamplePeriod) == 0)
	{
		UE_TRACE_LOG(Memory, Marker, MemAllocChannel) << Marker.Cycle(UE::Trace::Private::TimeGetTimestamp());
	}

	if (GDoPumpTrace)
	{
		UE::Trace::Update();
	}
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_Alloc(uint64_t Address, uint64_t Size, uint32_t Alignment, HeapId RootHeap, uint32_t ExternalCallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	ZEN_ASSERT_SLOW(RootHeap < 16);

	const uint32_t AlignmentPow2	   = uint32_t(zen::CountTrailingZeros64(Alignment));
	const uint32_t Alignment_SizeLower = (AlignmentPow2 << SizeShift) | uint32_t(Size & ((1 << SizeShift) - 1));
	const uint32_t CallstackId = ExternalCallstackId ? ExternalCallstackId : GDoNotAllocateInTrace ? 0 : CallstackTrace_GetCurrentId();

	switch (RootHeap)
	{
		case EMemoryTraceRootHeap::SystemMemory:
			{
				UE_TRACE_LOG(Memory, AllocSystem, MemAllocChannel)
					<< AllocSystem.Address(uint64_t(Address)) << AllocSystem.CallstackId(CallstackId)
					<< AllocSystem.Size(uint32_t(Size >> SizeShift)) << AllocSystem.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower));
				break;
			}

		case EMemoryTraceRootHeap::VideoMemory:
			{
				UE_TRACE_LOG(Memory, AllocVideo, MemAllocChannel)
					<< AllocVideo.Address(uint64_t(Address)) << AllocVideo.CallstackId(CallstackId)
					<< AllocVideo.Size(uint32_t(Size >> SizeShift)) << AllocVideo.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower));
				break;
			}

		default:
			{
				UE_TRACE_LOG(Memory, Alloc, MemAllocChannel)
					<< Alloc.Address(uint64_t(Address)) << Alloc.CallstackId(CallstackId) << Alloc.Size(uint32_t(Size >> SizeShift))
					<< Alloc.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower)) << Alloc.RootHeap(uint8(RootHeap));
				break;
			}
	}

	MemoryTrace_UpdateInternal();
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_Free(uint64_t Address, HeapId RootHeap, uint32_t ExternalCallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	ZEN_ASSERT_SLOW(RootHeap < 16);

	const uint32_t CallstackId = ExternalCallstackId ? ExternalCallstackId : GDoNotAllocateInTrace ? 0 : CallstackTrace_GetCurrentId();

	switch (RootHeap)
	{
		case EMemoryTraceRootHeap::SystemMemory:
			{
				UE_TRACE_LOG(Memory, FreeSystem, MemAllocChannel)
					<< FreeSystem.Address(uint64_t(Address)) << FreeSystem.CallstackId(CallstackId);
				break;
			}
		case EMemoryTraceRootHeap::VideoMemory:
			{
				UE_TRACE_LOG(Memory, FreeVideo, MemAllocChannel)
					<< FreeVideo.Address(uint64_t(Address)) << FreeVideo.CallstackId(CallstackId);
				break;
			}
		default:
			{
				UE_TRACE_LOG(Memory, Free, MemAllocChannel)
					<< Free.Address(uint64_t(Address)) << Free.CallstackId(CallstackId) << Free.RootHeap(uint8(RootHeap));
				break;
			}
	}

	MemoryTrace_UpdateInternal();
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_ReallocAlloc(uint64_t Address, uint64_t Size, uint32_t Alignment, HeapId RootHeap, uint32_t ExternalCallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	ZEN_ASSERT_SLOW(RootHeap < 16);

	const uint32_t AlignmentPow2	   = uint32_t(zen::CountTrailingZeros64(Alignment));
	const uint32_t Alignment_SizeLower = (AlignmentPow2 << SizeShift) | uint32_t(Size & ((1 << SizeShift) - 1));
	const uint32_t CallstackId = ExternalCallstackId ? ExternalCallstackId : GDoNotAllocateInTrace ? 0 : CallstackTrace_GetCurrentId();

	switch (RootHeap)
	{
		case EMemoryTraceRootHeap::SystemMemory:
			{
				UE_TRACE_LOG(Memory, ReallocAllocSystem, MemAllocChannel)
					<< ReallocAllocSystem.Address(uint64_t(Address)) << ReallocAllocSystem.CallstackId(CallstackId)
					<< ReallocAllocSystem.Size(uint32_t(Size >> SizeShift))
					<< ReallocAllocSystem.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower));
				break;
			}

		default:
			{
				UE_TRACE_LOG(Memory, ReallocAlloc, MemAllocChannel)
					<< ReallocAlloc.Address(uint64_t(Address)) << ReallocAlloc.CallstackId(CallstackId)
					<< ReallocAlloc.Size(uint32_t(Size >> SizeShift)) << ReallocAlloc.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower))
					<< ReallocAlloc.RootHeap(uint8(RootHeap));
				break;
			}
	}

	MemoryTrace_UpdateInternal();
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_ReallocFree(uint64_t Address, HeapId RootHeap, uint32_t ExternalCallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	ZEN_ASSERT_SLOW(RootHeap < 16);

	const uint32_t CallstackId = ExternalCallstackId ? ExternalCallstackId : GDoNotAllocateInTrace ? 0 : CallstackTrace_GetCurrentId();

	switch (RootHeap)
	{
		case EMemoryTraceRootHeap::SystemMemory:
			{
				UE_TRACE_LOG(Memory, ReallocFreeSystem, MemAllocChannel)
					<< ReallocFreeSystem.Address(uint64_t(Address)) << ReallocFreeSystem.CallstackId(CallstackId);
				break;
			}

		default:
			{
				UE_TRACE_LOG(Memory, ReallocFree, MemAllocChannel)
					<< ReallocFree.Address(uint64_t(Address)) << ReallocFree.CallstackId(CallstackId)
					<< ReallocFree.RootHeap(uint8(RootHeap));
				break;
			}
	}

	MemoryTrace_UpdateInternal();
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_SwapOp(uint64_t PageAddress, EMemoryTraceSwapOperation SwapOperation, uint32_t CompressedSize, uint32_t CallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	UE_TRACE_LOG(Memory, MemorySwapOp, MemAllocChannel)
		<< MemorySwapOp.Address(PageAddress) << MemorySwapOp.CallstackId(CallstackId) << MemorySwapOp.CompressedSize(CompressedSize)
		<< MemorySwapOp.SwapOp((uint8)SwapOperation);

	MemoryTrace_UpdateInternal();
}

////////////////////////////////////////////////////////////////////////////////
HeapId
MemoryTrace_HeapSpec(HeapId ParentId, const char16_t* Name, EMemoryTraceHeapFlags Flags)
{
	if (!GTraceAllowed)
	{
		return 0;
	}

	static std::atomic<HeapId> HeapIdCount(EMemoryTraceRootHeap::EndReserved + 1);	// Reserve indexes for root heaps
	const HeapId			   Id		= HeapIdCount.fetch_add(1);
	const uint32_t			   NameLen	= uint32_t(zen::StringLength(Name));
	const uint32_t			   DataSize = NameLen * sizeof(char16_t);
	ZEN_ASSERT(ParentId < Id);

	UE_TRACE_LOG(Memory, HeapSpec, MemAllocChannel, DataSize)
		<< HeapSpec.Id(Id) << HeapSpec.ParentId(ParentId) << HeapSpec.Name(Name, NameLen) << HeapSpec.Flags(uint16(Flags));

	return Id;
}

////////////////////////////////////////////////////////////////////////////////
HeapId
MemoryTrace_RootHeapSpec(const char16_t* Name, EMemoryTraceHeapFlags Flags)
{
	if (!GTraceAllowed)
	{
		return 0;
	}

	static std::atomic<HeapId> RootHeapCount(0);
	const HeapId			   Id = RootHeapCount.fetch_add(1);
	ZEN_ASSERT(Id <= EMemoryTraceRootHeap::EndReserved);

	const uint32_t NameLen	= uint32_t(zen::StringLength(Name));
	const uint32_t DataSize = NameLen * sizeof(char16_t);

	UE_TRACE_LOG(Memory, HeapSpec, MemAllocChannel, DataSize)
		<< HeapSpec.Id(Id) << HeapSpec.ParentId(HeapId(~0)) << HeapSpec.Name(Name, NameLen)
		<< HeapSpec.Flags(uint16(EMemoryTraceHeapFlags::Root | Flags));

	return Id;
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_MarkAllocAsHeap(uint64_t Address, HeapId Heap, EMemoryTraceHeapAllocationFlags Flags, uint32_t ExternalCallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	const uint32_t CallstackId = ExternalCallstackId ? ExternalCallstackId : GDoNotAllocateInTrace ? 0 : CallstackTrace_GetCurrentId();

	UE_TRACE_LOG(Memory, HeapMarkAlloc, MemAllocChannel)
		<< HeapMarkAlloc.Address(uint64_t(Address)) << HeapMarkAlloc.CallstackId(CallstackId)
		<< HeapMarkAlloc.Flags(uint16(EMemoryTraceHeapAllocationFlags::Heap | Flags)) << HeapMarkAlloc.Heap(Heap);
}

////////////////////////////////////////////////////////////////////////////////
void
MemoryTrace_UnmarkAllocAsHeap(uint64_t Address, HeapId Heap, uint32_t ExternalCallstackId)
{
	if (!GTraceAllowed)
	{
		return;
	}

	const uint32_t CallstackId = ExternalCallstackId ? ExternalCallstackId : GDoNotAllocateInTrace ? 0 : CallstackTrace_GetCurrentId();

	// Sets all flags to zero
	UE_TRACE_LOG(Memory, HeapUnmarkAlloc, MemAllocChannel)
		<< HeapUnmarkAlloc.Address(uint64_t(Address)) << HeapUnmarkAlloc.CallstackId(CallstackId) << HeapUnmarkAlloc.Heap(Heap);
}

}  // namespace zen

#else  // UE_MEMORY_TRACE_ENABLED

/////////////////////////////////////////////////////////////////////////////
bool
MemoryTrace_IsActive()
{
	return false;
}

#endif	// UE_MEMORY_TRACE_ENABLED

namespace zen {

/////////////////////////////////////////////////////////////////////////////
FTraceMalloc::FTraceMalloc(FMalloc* InMalloc)
{
	WrappedMalloc = InMalloc;
}

/////////////////////////////////////////////////////////////////////////////
FTraceMalloc::~FTraceMalloc()
{
}

/////////////////////////////////////////////////////////////////////////////
void*
FTraceMalloc::Malloc(size_t Count, uint32_t Alignment)
{
#if UE_MEMORY_TRACE_ENABLED
	// UE_TRACE_METADATA_CLEAR_SCOPE();
	ZEN_MEMSCOPE(TRACE_TAG);

	void* NewPtr;
	{
		zen::TGuardValue<bool> _(GDoNotAllocateInTrace, true);
		NewPtr = WrappedMalloc->Malloc(Count, Alignment);
	}

	const uint64_t Size				   = Count;
	const uint32_t AlignmentPow2	   = uint32_t(zen::CountTrailingZeros64(Alignment));
	const uint32_t Alignment_SizeLower = (AlignmentPow2 << SizeShift) | uint32_t(Size & ((1 << SizeShift) - 1));

	UE_TRACE_LOG(Memory, Alloc, MemAllocChannel)
		<< Alloc.Address(uint64_t(NewPtr)) << Alloc.CallstackId(0) << Alloc.Size(uint32_t(Size >> SizeShift))
		<< Alloc.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower)) << Alloc.RootHeap(uint8(EMemoryTraceRootHeap::SystemMemory));

	return NewPtr;
#else
	return WrappedMalloc->Malloc(Count, Alignment);
#endif	// UE_MEMORY_TRACE_ENABLED
}

/////////////////////////////////////////////////////////////////////////////
void*
FTraceMalloc::Realloc(void* Original, size_t Count, uint32_t Alignment)
{
#if UE_MEMORY_TRACE_ENABLED
	// UE_TRACE_METADATA_CLEAR_SCOPE();
	ZEN_MEMSCOPE(TRACE_TAG);

	UE_TRACE_LOG(Memory, ReallocFree, MemAllocChannel)
		<< ReallocFree.Address(uint64_t(Original)) << ReallocFree.RootHeap(uint8(EMemoryTraceRootHeap::SystemMemory));

	void* NewPtr;
	{
		zen::TGuardValue<bool> _(GDoNotAllocateInTrace, true);
		NewPtr = WrappedMalloc->Realloc(Original, Count, Alignment);
	}

	const uint64_t Size				   = Count;
	const uint32_t AlignmentPow2	   = uint32_t(zen::CountTrailingZeros64(Alignment));
	const uint32_t Alignment_SizeLower = (AlignmentPow2 << SizeShift) | uint32_t(Size & ((1 << SizeShift) - 1));

	UE_TRACE_LOG(Memory, ReallocAlloc, MemAllocChannel)
		<< ReallocAlloc.Address(uint64_t(NewPtr)) << ReallocAlloc.CallstackId(0) << ReallocAlloc.Size(uint32_t(Size >> SizeShift))
		<< ReallocAlloc.AlignmentPow2_SizeLower(uint8(Alignment_SizeLower))
		<< ReallocAlloc.RootHeap(uint8(EMemoryTraceRootHeap::SystemMemory));

	return NewPtr;
#else
	return WrappedMalloc->Realloc(Original, Count, Alignment);
#endif	// UE_MEMORY_TRACE_ENABLED
}

/////////////////////////////////////////////////////////////////////////////
void
FTraceMalloc::Free(void* Original)
{
#if UE_MEMORY_TRACE_ENABLED
	UE_TRACE_LOG(Memory, Free, MemAllocChannel)
		<< Free.Address(uint64_t(Original)) << Free.RootHeap(uint8(EMemoryTraceRootHeap::SystemMemory));

	{
		zen::TGuardValue<bool> _(GDoNotAllocateInTrace, true);
		WrappedMalloc->Free(Original);
	}
#else
	WrappedMalloc->Free(Original);
#endif	// UE_MEMORY_TRACE_ENABLED
}

}  // namespace zen