diff options
| author | Stefan Boberg <[email protected]> | 2024-11-25 09:56:23 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-11-25 09:56:23 +0100 |
| commit | 8b8de92e51db4cc4c1727712c736dcba5f79d369 (patch) | |
| tree | 1f58edaaad389837a7652daebab246125762240e /src/zencore/iobuffer.cpp | |
| parent | 5.5.13 (diff) | |
| download | zen-8b8de92e51db4cc4c1727712c736dcba5f79d369.tar.xz zen-8b8de92e51db4cc4c1727712c736dcba5f79d369.zip | |
Insights-compatible memory tracking (#214)
This change introduces support for tracing of memory allocation activity. The code is ported from UE5, and Unreal Insights can be used to analyze the output. This is currently only fully supported on Windows, but will be extended to Mac/Linux in the near future.
To activate full memory tracking, pass `--trace=memory` on the commandline alongside `--tracehost=<ip>` or `-tracefile=<path>`. For more control over how much detail is traced you can instead pass some combination of `callstack`, `memtag`, `memalloc` instead. In practice, `--trace=memory` is an alias for `--trace=callstack,memtag,memalloc`). For convenience we also support `--trace=memory_light` which omits call stacks.
This change also introduces multiple memory allocators, which may be selected via command-line option `--malloc=<allocator>`:
* `mimalloc` - mimalloc (default, same as before)
* `rpmalloc` - rpmalloc is another high performance allocator for multithreaded applications which may be a better option than mimalloc (to be evaluated). Due to toolchain limitations this is currently only supported on Windows.
* `stomp` - an allocator intended to be used during development/debugging to help track down memory issues such as use-after-free or out-of-bounds access. Currently only supported on Windows.
* `ansi` - fallback to default system allocator
Diffstat (limited to 'src/zencore/iobuffer.cpp')
| -rw-r--r-- | src/zencore/iobuffer.cpp | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/src/zencore/iobuffer.cpp b/src/zencore/iobuffer.cpp index 51f380c34..d6d02eb0b 100644 --- a/src/zencore/iobuffer.cpp +++ b/src/zencore/iobuffer.cpp @@ -8,6 +8,8 @@ #include <zencore/iohash.h> #include <zencore/logging.h> #include <zencore/memory.h> +#include <zencore/memory/llm.h> +#include <zencore/memory/memory.h> #include <zencore/testing.h> #include <zencore/thread.h> #include <zencore/trace.h> @@ -15,12 +17,6 @@ #include <memory.h> #include <system_error> -#if ZEN_USE_MIMALLOC -ZEN_THIRD_PARTY_INCLUDES_START -# include <mimalloc.h> -ZEN_THIRD_PARTY_INCLUDES_END -#endif - #if ZEN_PLATFORM_WINDOWS # include <zencore/windows.h> #else @@ -43,39 +39,39 @@ namespace zen { void IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) const { -#if ZEN_USE_MIMALLOC - void* Ptr = mi_aligned_alloc(Alignment, RoundUp(InSize, Alignment)); - m_Flags.fetch_or(kIoBufferAlloc, std::memory_order_relaxed); -#else + UE_MEMSCOPE(ELLMTag::IoBufferMemory); + void* Ptr = Memory::Alloc(InSize, Alignment); -#endif if (!Ptr) { ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", InSize, Alignment)); } + m_DataPtr = Ptr; } void IoBufferCore::FreeBuffer() { - if (!m_DataPtr) + if (m_DataPtr) { - return; + Memory::Free(const_cast<void*>(m_DataPtr)); + m_DataPtr = nullptr; } +} - const uint32_t LocalFlags = m_Flags.load(std::memory_order_relaxed); - -#if ZEN_USE_MIMALLOC - if (LocalFlags & kIoBufferAlloc) - { - return mi_free(const_cast<void*>(m_DataPtr)); - } -#endif +void* +IoBufferCore::operator new(size_t Size) +{ + UE_MEMSCOPE(ELLMTag::IoBufferCore); + return Memory::Malloc(Size); +} - ZEN_UNUSED(LocalFlags); - return Memory::Free(const_cast<void*>(m_DataPtr)); +void +IoBufferCore::operator delete(void* Ptr) +{ + Memory::Free(Ptr); } ////////////////////////////////////////////////////////////////////////// @@ -104,10 +100,9 @@ IoBufferCore::IoBufferCore(size_t InSize, size_t Alignment) IoBufferCore::~IoBufferCore() { - if (IsOwnedByThis() && m_DataPtr) + if (IsOwnedByThis()) { FreeBuffer(); - m_DataPtr = nullptr; } } @@ -567,7 +562,7 @@ IoBufferBuilder::ReadFromFileMaybe(const IoBuffer& InBuffer) Error = zen::GetLastError(); } #else - int Fd = int(intptr_t(FileRef.FileHandle)); + int Fd = int(intptr_t(FileRef.FileHandle)); ssize_t ReadResult = pread(Fd, OutBuffer.MutableData(), size_t(NumberOfBytesToRead), off_t(FileOffset)); if (ReadResult != -1) { @@ -635,7 +630,7 @@ IoBufferBuilder::MakeFromFile(const std::filesystem::path& FileName, uint64_t Of DataFile.GetSize((ULONGLONG&)FileSize); #else int Flags = O_RDONLY | O_CLOEXEC; - int Fd = open(FileName.c_str(), Flags); + int Fd = open(FileName.c_str(), Flags); if (Fd < 0) { return {}; @@ -704,7 +699,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const std::filesystem::path& FileName) Handle = DataFile.Detach(); #else - int Fd = open(FileName.native().c_str(), O_RDONLY); + int Fd = open(FileName.native().c_str(), O_RDONLY); if (Fd < 0) { return {}; |