Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 8c53a36effa1c4bbdbf94d37afd66e19593844f1 Mon Sep 17 00:00:00 2001
- From: sekrit-twc <noreply@example.com>
- Date: Sat, 24 Mar 2018 14:43:51 -0700
- Subject: [PATCH] Allocate buffers with huge pages.
- ---
- src/core/vscore.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++---
- 1 file changed, 71 insertions(+), 4 deletions(-)
- diff --git a/src/core/vscore.cpp b/src/core/vscore.cpp
- index 13573b8..c570b5f 100644
- --- a/src/core/vscore.cpp
- +++ b/src/core/vscore.cpp
- @@ -238,6 +238,62 @@ void VSVariant::initStorage(VSVType t) {
- ///////////////
- +static bool do_enable_hugepages()
- +{
- + HANDLE token = INVALID_HANDLE_VALUE;
- + TOKEN_PRIVILEGES priv = {};
- +
- + if (!(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)))
- + return false;
- +
- + if (!(LookupPrivilegeValue(nullptr, SE_LOCK_MEMORY_NAME, &priv.Privileges[0].Luid))) {
- + CloseHandle(token);
- + return false;
- + }
- +
- + priv.PrivilegeCount = 1;
- + priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- +
- + if (!(AdjustTokenPrivileges(token, FALSE, &priv, 0, nullptr, 0))) {
- + CloseHandle(token);
- + return false;
- + }
- +
- + CloseHandle(token);
- + return true;
- +}
- +
- +static bool enable_hugepages()
- +{
- + static const bool enabled = do_enable_hugepages();
- + return enabled;
- +}
- +
- +static uint8_t *alloc_large_page(size_t bytes)
- +{
- + if (!enable_hugepages())
- + return nullptr;
- +
- + static const size_t pagesize = GetLargePageMinimum();
- +
- + if (bytes % pagesize)
- + bytes += pagesize - bytes % pagesize;
- +
- + void *buf = VirtualAllocEx(GetCurrentProcess(), nullptr, bytes, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
- + if (!buf)
- + vsFatal("memory allocation failed");
- + return (uint8_t *)buf;
- +}
- +
- +static void free_large_page(void *ptr)
- +{
- + if (!enable_hugepages()) {
- + vs_aligned_free(ptr);
- + return;
- + }
- + VirtualFreeEx(GetCurrentProcess(), ptr, 0, MEM_RELEASE);
- +}
- +
- void MemoryUse::add(size_t bytes) {
- used.fetch_add(bytes);
- }
- @@ -260,8 +316,14 @@ uint8_t *MemoryUse::allocBuffer(size_t bytes) {
- }
- }
- - uint8_t *buf = vs_aligned_malloc<uint8_t>(VSFrame::alignment + bytes, VSFrame::alignment);
- - memcpy(buf, &bytes, sizeof(bytes));
- + uint8_t *buf = nullptr;
- +
- + if (bytes >= 2UL * (1UL << 20))
- + buf = alloc_large_page(bytes);
- + if (!buf)
- + buf = vs_aligned_malloc<uint8_t>(VSFrame::alignment + bytes, VSFrame::alignment);
- +
- + memcpy(buf, &bytes, sizeof(bytes));
- return buf + VSFrame::alignment;
- }
- @@ -279,8 +341,13 @@ void MemoryUse::freeBuffer(uint8_t *buf) {
- std::advance(iter, randSrc(generator));
- assert(unusedBufferSize >= iter->first);
- unusedBufferSize -= iter->first;
- - vs_aligned_free(iter->second);
- - buffers.erase(iter);
- +
- + if (iter->first >= 2UL * (1UL << 20))
- + free_large_page(iter->second);
- + else
- + vs_aligned_free(iter->second);
- +
- + buffers.erase(iter);
- }
- }
- --
- 2.13.2.windows.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement