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 {