]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commitdiff
Run fadvise with mmap file (#10142)
authorsdong <siying.d@fb.com>
Fri, 10 Jun 2022 23:34:01 +0000 (16:34 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Fri, 10 Jun 2022 23:34:01 +0000 (16:34 -0700)
Summary:
Right now with mmap file, we don't run fadvise following users' requests. There is no reason for that so this diff does that.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/10142

Test Plan:
A simple readrandom against files with page cache dropped shows latency improvement from 7.8 us to 2.8:

./db_bench -use_existing_db --benchmarks=readrandom --num=100

Reviewed By: anand1976

Differential Revision: D37074975

fbshipit-source-id: ccc72bcac1b5fd634eb8fa2b6a5d9afe332e0bf6

env/io_posix.cc
env/io_posix.h

index ef4975de5f3cae7a491e2d3eb9d283d35b919018..ac8b3999d8905472117a7f5c27ef20f7130df8fb 100644 (file)
@@ -88,6 +88,24 @@ int Fadvise(int fd, off_t offset, size_t len, int advice) {
 #endif
 }
 
+int FadviseForHint(int fd, FSRandomAccessFile::AccessPattern pattern) {
+  switch (pattern) {
+    case FSRandomAccessFile::AccessPattern::kNormal:
+      return Fadvise(fd, 0, 0, POSIX_FADV_NORMAL);
+    case FSRandomAccessFile::AccessPattern::kRandom:
+      return Fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
+    case FSRandomAccessFile::AccessPattern::kSequential:
+      return Fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
+    case FSRandomAccessFile::AccessPattern::kWillNeed:
+      return Fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
+    case FSRandomAccessFile::AccessPattern::kWontNeed:
+      return Fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
+    default:
+      assert(false);
+      return 1;
+  }
+}
+
 namespace {
 
 // On MacOS (and probably *BSD), the posix write and pwrite calls do not support
@@ -821,26 +839,7 @@ void PosixRandomAccessFile::Hint(AccessPattern pattern) {
   if (use_direct_io()) {
     return;
   }
-  switch (pattern) {
-    case kNormal:
-      Fadvise(fd_, 0, 0, POSIX_FADV_NORMAL);
-      break;
-    case kRandom:
-      Fadvise(fd_, 0, 0, POSIX_FADV_RANDOM);
-      break;
-    case kSequential:
-      Fadvise(fd_, 0, 0, POSIX_FADV_SEQUENTIAL);
-      break;
-    case kWillNeed:
-      Fadvise(fd_, 0, 0, POSIX_FADV_WILLNEED);
-      break;
-    case kWontNeed:
-      Fadvise(fd_, 0, 0, POSIX_FADV_DONTNEED);
-      break;
-    default:
-      assert(false);
-      break;
-  }
+  FadviseForHint(fd_, pattern);
 }
 
 IOStatus PosixRandomAccessFile::InvalidateCache(size_t offset, size_t length) {
@@ -982,6 +981,10 @@ IOStatus PosixMmapReadableFile::Read(uint64_t offset, size_t n,
   return s;
 }
 
+void PosixMmapReadableFile::Hint(AccessPattern pattern) {
+  FadviseForHint(fd_, pattern);
+}
+
 IOStatus PosixMmapReadableFile::InvalidateCache(size_t offset, size_t length) {
 #ifndef OS_LINUX
   (void)offset;
index 0438163a36f748fae6b5065f02c95e310db8b768..644abfd9c5ea0370abb5bb8a6ec848c8af62cda3 100644 (file)
@@ -352,10 +352,10 @@ class PosixMmapReadableFile : public FSRandomAccessFile {
   PosixMmapReadableFile(const int fd, const std::string& fname, void* base,
                         size_t length, const EnvOptions& options);
   virtual ~PosixMmapReadableFile();
-  virtual IOStatus Read(uint64_t offset, size_t n, const IOOptions& opts,
-                        Slice* result, char* scratch,
-                        IODebugContext* dbg) const override;
-  virtual IOStatus InvalidateCache(size_t offset, size_t length) override;
+  IOStatus Read(uint64_t offset, size_t n, const IOOptions& opts, Slice* result,
+                char* scratch, IODebugContext* dbg) const override;
+  void Hint(AccessPattern pattern) override;
+  IOStatus InvalidateCache(size_t offset, size_t length) override;
 };
 
 class PosixMmapFile : public FSWritableFile {