Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Fix EventPipeBuffer to use ClrVirtualAlloc instead of malloc (#28038)
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhang authored May 13, 2020
1 parent b825061 commit b174565
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/vm/eventpipebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ EventPipeBuffer::EventPipeBuffer(unsigned int bufferSize, EventPipeThread* pWrit
m_state = EventPipeBufferState::WRITABLE;
m_pWriterThread = pWriterThread;
m_eventSequenceNumber = eventSequenceNumber;
m_pBuffer = new BYTE[bufferSize];
// Use ClrVirtualAlloc instead of malloc to allocate buffer to avoid potential internal fragmentation in the native CRT heap.
// (See https://github.com/dotnet/runtime/pull/35924 and https://github.com/microsoft/ApplicationInsights-dotnet/issues/1678 for more details)
m_pBuffer = (BYTE*)ClrVirtualAlloc(NULL, bufferSize, MEM_COMMIT, PAGE_READWRITE);
memset(m_pBuffer, 0, bufferSize);
m_pLimit = m_pBuffer + bufferSize;
m_pCurrent = GetNextAlignedAddress(m_pBuffer);
Expand All @@ -47,7 +49,7 @@ EventPipeBuffer::~EventPipeBuffer()
}
CONTRACTL_END;

delete[] m_pBuffer;
ClrVirtualFree(m_pBuffer, 0, MEM_RELEASE);
}

bool EventPipeBuffer::WriteEvent(Thread *pThread, EventPipeSession &session, EventPipeEvent &event, EventPipeEventPayload &payload, LPCGUID pActivityId, LPCGUID pRelatedActivityId, StackContents *pStack)
Expand Down
6 changes: 6 additions & 0 deletions src/vm/eventpipebuffermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ EventPipeBuffer* EventPipeBufferManager::AllocateBufferForThread(EventPipeThread
const unsigned int maxBufferSize = 1024 * 1024;
bufferSize = Min(bufferSize, maxBufferSize);

// Make the buffer size fit into with pagesize-aligned block,
// since ClrVirtualAlloc expects page-aligned sizes to be passed
// as arguments (see ctor of EventPipeBuffer)
bufferSize = (bufferSize + g_SystemInfo.dwAllocationGranularity - 1) & ~static_cast<unsigned int>(g_SystemInfo.dwAllocationGranularity - 1);


// EX_TRY is used here as opposed to new (nothrow) because
// the constructor also allocates a private buffer, which
// could throw, and cannot be easily checked
Expand Down

0 comments on commit b174565

Please sign in to comment.