[PATCH] D116366: [Support] Add MemoryBuffer::dontNeedIfMmap

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 29 01:09:33 PST 2021


MaskRay created this revision.
MaskRay added reviewers: aganea, Bigcheese, rnk, tejohnson.
Herald added subscribers: dexonsmith, hiraditya.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This is to be used by Dxxxxx


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116366

Files:
  llvm/include/llvm/Support/FileSystem.h
  llvm/include/llvm/Support/MemoryBuffer.h
  llvm/lib/Support/MemoryBuffer.cpp
  llvm/lib/Support/Unix/Path.inc
  llvm/lib/Support/Windows/Path.inc


Index: llvm/lib/Support/Windows/Path.inc
===================================================================
--- llvm/lib/Support/Windows/Path.inc
+++ llvm/lib/Support/Windows/Path.inc
@@ -959,6 +959,8 @@
   }
 }
 
+void mapped_file_region::dontNeedImpl() {}
+
 int mapped_file_region::alignment() {
   SYSTEM_INFO SysInfo;
   ::GetSystemInfo(&SysInfo);
Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -870,6 +870,11 @@
     ::munmap(Mapping, Size);
 }
 
+void mapped_file_region::dontNeedImpl() {
+  if (Mapping)
+    ::madvise(Mapping, Size, MADV_DONTNEED);
+}
+
 int mapped_file_region::alignment() {
   return Process::getPageSizeEstimate();
 }
Index: llvm/lib/Support/MemoryBuffer.cpp
===================================================================
--- llvm/lib/Support/MemoryBuffer.cpp
+++ llvm/lib/Support/MemoryBuffer.cpp
@@ -220,6 +220,8 @@
   MemoryBuffer::BufferKind getBufferKind() const override {
     return MemoryBuffer::MemoryBuffer_MMap;
   }
+
+  void dontNeedIfMmap() override { MFR.dontNeed(); }
 };
 } // namespace
 
Index: llvm/include/llvm/Support/MemoryBuffer.h
===================================================================
--- llvm/include/llvm/Support/MemoryBuffer.h
+++ llvm/include/llvm/Support/MemoryBuffer.h
@@ -74,6 +74,12 @@
   /// from.
   virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
 
+  /// For MemoryBuffer_MMap, mark the buffer as unused for an unspecified amount
+  /// of time and the kernel can free resources associated with it. Further
+  /// access is supported but may be expensive. This calls
+  /// madvise(MADV_DONTNEED) on Linux.
+  virtual void dontNeedIfMmap() {}
+
   /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
   /// if successful, otherwise returning null.
   ///
Index: llvm/include/llvm/Support/FileSystem.h
===================================================================
--- llvm/include/llvm/Support/FileSystem.h
+++ llvm/include/llvm/Support/FileSystem.h
@@ -1279,6 +1279,7 @@
   }
 
   void unmapImpl();
+  void dontNeedImpl();
 
   std::error_code init(sys::fs::file_t FD, uint64_t Offset, mapmode Mode);
 
@@ -1308,6 +1309,7 @@
     unmapImpl();
     copyFrom(mapped_file_region());
   }
+  void dontNeed() { dontNeedImpl(); }
 
   size_t size() const;
   char *data() const;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116366.396502.patch
Type: text/x-patch
Size: 2447 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211229/e6ab5483/attachment.bin>


More information about the llvm-commits mailing list