Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MallocReplayProxy.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "HAL/CriticalSection.h"
7#include "HAL/MemoryBase.h"
8#include "HAL/UnrealMemory.h"
9#include "Misc/AssertionMacros.h"
10#include "Misc/ScopeLock.h"
11
12class FOutputDevice;
13class UWorld;
15
16#if !defined(UE_USE_MALLOC_REPLAY_PROXY)
17 // it is always enabled on Linux, but not always added to the malloc stack
18 #define UE_USE_MALLOC_REPLAY_PROXY (PLATFORM_UNIX && !UE_BUILD_SHIPPING)
19#endif // !defined(UE_USE_MALLOC_REPLAY_PROXY)
20
22
23/**
24 * This FMalloc proxy is used as a lighweight way to dump memory allocation for later replaying
25 * (and thus testing different malloc implementations)
26 */
27class FMallocReplayProxy : public FMalloc
28{
29private:
30 /** Malloc we're based on, aka using under the hood */
31 FMalloc* UsedMalloc;
32
33private:
34
35 enum
36 {
37 HistoryCacheSize = 16384
38 };
39
40 /** describes a single operation entry */
41 struct FHistoryEntry
42 {
43 /** We don't need to compare the operations, so we can store this as string */
44 const char* Operation;
45 /** Pointer to memory chunk as returned. */
46 void * PointerOut;
47 /** Pointer to memory chunk as passed in. */
48 void * PointerIn;
49 /** Size as passed in - only valid for malloc/realloc. */
50 SIZE_T Size;
51 /** Alignment as passed in - only valid for malloc/realloc. */
52 uint32 Alignment;
53 };
54
55 /** Size of history not yet dumped to disk */
56 FHistoryEntry HistoryCache[HistoryCacheSize];
57
58 /** Current entry in history */
59 int32 CurrentCacheIdx;
60
61 /** Global operation number (to aid using dump file) */
62 uint64 OperationNumber;
63
64 /** Guards access to history*/
65 FCriticalSection HistoryLock;
66
67 /** Regular file where we save the history to */
68 FILE* HistoryFile;
69
70 /** Adds operation to history*/
71 void AddToHistory(const char *Op, void * PtrOut, void * PtrIn, SIZE_T Size, SIZE_T Alignment)
72 {
73 FScopeLock Lock(&HistoryLock);
74
75 HistoryCache[CurrentCacheIdx].Operation = Op;
76 HistoryCache[CurrentCacheIdx].PointerOut = PtrOut;
77 HistoryCache[CurrentCacheIdx].PointerIn = PtrIn;
78 HistoryCache[CurrentCacheIdx].Size = Size;
79 HistoryCache[CurrentCacheIdx].Alignment = Alignment;
80
81 ++CurrentCacheIdx;
82 if (CurrentCacheIdx > HistoryCacheSize - 1)
83 {
84 DumpHistoryToDisk();
85 }
86 }
87
88 /** Expected to be called while history lock is taken. */
89 void DumpHistoryToDisk();
90
91public:
92 // FMalloc interface begin
93 explicit FMallocReplayProxy(FMalloc* InMalloc);
94
95 virtual ~FMallocReplayProxy();
96
97 virtual void InitializeStatsMetadata() override;
98
99 virtual void* Malloc(SIZE_T Size, uint32 Alignment) override;
100
101 virtual void* Realloc(void* Ptr, SIZE_T NewSize, uint32 Alignment) override;
102
103 virtual void Free(void* Ptr) override;
104
105 virtual SIZE_T QuantizeSize(SIZE_T Count, uint32 Alignment) override;
106
107 virtual void UpdateStats() override;
108
109 virtual void GetAllocatorStats(FGenericMemoryStats& out_Stats) override;
110
111 virtual void DumpAllocatorStats(class FOutputDevice& Ar) override;
112
113 virtual bool IsInternallyThreadSafe() const override;
114
115 virtual bool ValidateHeap() override;
116
117 virtual bool Exec(UWorld* InWorld, const TCHAR* Cmd, FOutputDevice& Ar) override;
118
119 virtual bool GetAllocationSize(void *Original, SIZE_T &SizeOut) override;
120
121 virtual const TCHAR* GetDescriptiveName() override;
122
123 virtual void Trim(bool bTrimThreadCaches) override;
124
125 virtual void SetupTLSCachesOnCurrentThread() override;
126
127 virtual void ClearAndDisableTLSCachesOnCurrentThread() override;
128
129 // FMalloc interface end
130
131 // called by destructor or otherwise, idempotent
132 void CloseHistory();
133};
134
135#endif // UE_USE_MALLOC_REPLAY_PROXY
#define UE_BUILD_SHIPPING
Definition Build.h:4
#define UE_USE_MALLOC_REPLAY_PROXY
#define PLATFORM_UNIX
Definition Platform.h:59