Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. From 8c53a36effa1c4bbdbf94d37afd66e19593844f1 Mon Sep 17 00:00:00 2001
  2. From: sekrit-twc <noreply@example.com>
  3. Date: Sat, 24 Mar 2018 14:43:51 -0700
  4. Subject: [PATCH] Allocate buffers with huge pages.
  5.  
  6. ---
  7. src/core/vscore.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++---
  8. 1 file changed, 71 insertions(+), 4 deletions(-)
  9.  
  10. diff --git a/src/core/vscore.cpp b/src/core/vscore.cpp
  11. index 13573b8..c570b5f 100644
  12. --- a/src/core/vscore.cpp
  13. +++ b/src/core/vscore.cpp
  14. @@ -238,6 +238,62 @@ void VSVariant::initStorage(VSVType t) {
  15.  
  16. ///////////////
  17.  
  18. +static bool do_enable_hugepages()
  19. +{
  20. + HANDLE token = INVALID_HANDLE_VALUE;
  21. + TOKEN_PRIVILEGES priv = {};
  22. +
  23. + if (!(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)))
  24. + return false;
  25. +
  26. + if (!(LookupPrivilegeValue(nullptr, SE_LOCK_MEMORY_NAME, &priv.Privileges[0].Luid))) {
  27. + CloseHandle(token);
  28. + return false;
  29. + }
  30. +
  31. + priv.PrivilegeCount = 1;
  32. + priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  33. +
  34. + if (!(AdjustTokenPrivileges(token, FALSE, &priv, 0, nullptr, 0))) {
  35. + CloseHandle(token);
  36. + return false;
  37. + }
  38. +
  39. + CloseHandle(token);
  40. + return true;
  41. +}
  42. +
  43. +static bool enable_hugepages()
  44. +{
  45. + static const bool enabled = do_enable_hugepages();
  46. + return enabled;
  47. +}
  48. +
  49. +static uint8_t *alloc_large_page(size_t bytes)
  50. +{
  51. + if (!enable_hugepages())
  52. + return nullptr;
  53. +
  54. + static const size_t pagesize = GetLargePageMinimum();
  55. +
  56. + if (bytes % pagesize)
  57. + bytes += pagesize - bytes % pagesize;
  58. +
  59. + void *buf = VirtualAllocEx(GetCurrentProcess(), nullptr, bytes, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
  60. + if (!buf)
  61. + vsFatal("memory allocation failed");
  62. + return (uint8_t *)buf;
  63. +}
  64. +
  65. +static void free_large_page(void *ptr)
  66. +{
  67. + if (!enable_hugepages()) {
  68. + vs_aligned_free(ptr);
  69. + return;
  70. + }
  71. + VirtualFreeEx(GetCurrentProcess(), ptr, 0, MEM_RELEASE);
  72. +}
  73. +
  74. void MemoryUse::add(size_t bytes) {
  75. used.fetch_add(bytes);
  76. }
  77. @@ -260,8 +316,14 @@ uint8_t *MemoryUse::allocBuffer(size_t bytes) {
  78. }
  79. }
  80.  
  81. - uint8_t *buf = vs_aligned_malloc<uint8_t>(VSFrame::alignment + bytes, VSFrame::alignment);
  82. - memcpy(buf, &bytes, sizeof(bytes));
  83. + uint8_t *buf = nullptr;
  84. +
  85. + if (bytes >= 2UL * (1UL << 20))
  86. + buf = alloc_large_page(bytes);
  87. + if (!buf)
  88. + buf = vs_aligned_malloc<uint8_t>(VSFrame::alignment + bytes, VSFrame::alignment);
  89. +
  90. + memcpy(buf, &bytes, sizeof(bytes));
  91. return buf + VSFrame::alignment;
  92. }
  93.  
  94. @@ -279,8 +341,13 @@ void MemoryUse::freeBuffer(uint8_t *buf) {
  95. std::advance(iter, randSrc(generator));
  96. assert(unusedBufferSize >= iter->first);
  97. unusedBufferSize -= iter->first;
  98. - vs_aligned_free(iter->second);
  99. - buffers.erase(iter);
  100. +
  101. + if (iter->first >= 2UL * (1UL << 20))
  102. + free_large_page(iter->second);
  103. + else
  104. + vs_aligned_free(iter->second);
  105. +
  106. + buffers.erase(iter);
  107. }
  108. }
  109.  
  110. --
  111. 2.13.2.windows.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement