From 80afa776607a21895f207babb8722c04414590e9 Mon Sep 17 00:00:00 2001 From: sdong Date: Fri, 10 Jun 2022 16:34:01 -0700 Subject: [PATCH] Run fadvise with mmap file (#10142) 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 | 43 +++++++++++++++++++++++-------------------- env/io_posix.h | 8 ++++---- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/env/io_posix.cc b/env/io_posix.cc index ef4975de5..ac8b3999d 100644 --- a/env/io_posix.cc +++ b/env/io_posix.cc @@ -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; diff --git a/env/io_posix.h b/env/io_posix.h index 0438163a3..644abfd9c 100644 --- a/env/io_posix.h +++ b/env/io_posix.h @@ -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 {