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
main
sdong 3 years ago committed by Facebook GitHub Bot
parent 1777e5f7e9
commit 80afa77660
  1. 43
      env/io_posix.cc
  2. 8
      env/io_posix.h

43
env/io_posix.cc vendored

@ -88,6 +88,24 @@ int Fadvise(int fd, off_t offset, size_t len, int advice) {
#endif #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 { namespace {
// On MacOS (and probably *BSD), the posix write and pwrite calls do not support // 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()) { if (use_direct_io()) {
return; return;
} }
switch (pattern) { FadviseForHint(fd_, 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;
}
} }
IOStatus PosixRandomAccessFile::InvalidateCache(size_t offset, size_t length) { IOStatus PosixRandomAccessFile::InvalidateCache(size_t offset, size_t length) {
@ -982,6 +981,10 @@ IOStatus PosixMmapReadableFile::Read(uint64_t offset, size_t n,
return s; return s;
} }
void PosixMmapReadableFile::Hint(AccessPattern pattern) {
FadviseForHint(fd_, pattern);
}
IOStatus PosixMmapReadableFile::InvalidateCache(size_t offset, size_t length) { IOStatus PosixMmapReadableFile::InvalidateCache(size_t offset, size_t length) {
#ifndef OS_LINUX #ifndef OS_LINUX
(void)offset; (void)offset;

8
env/io_posix.h vendored

@ -352,10 +352,10 @@ class PosixMmapReadableFile : public FSRandomAccessFile {
PosixMmapReadableFile(const int fd, const std::string& fname, void* base, PosixMmapReadableFile(const int fd, const std::string& fname, void* base,
size_t length, const EnvOptions& options); size_t length, const EnvOptions& options);
virtual ~PosixMmapReadableFile(); virtual ~PosixMmapReadableFile();
virtual IOStatus Read(uint64_t offset, size_t n, const IOOptions& opts, IOStatus Read(uint64_t offset, size_t n, const IOOptions& opts, Slice* result,
Slice* result, char* scratch, char* scratch, IODebugContext* dbg) const override;
IODebugContext* dbg) const override; void Hint(AccessPattern pattern) override;
virtual IOStatus InvalidateCache(size_t offset, size_t length) override; IOStatus InvalidateCache(size_t offset, size_t length) override;
}; };
class PosixMmapFile : public FSWritableFile { class PosixMmapFile : public FSWritableFile {

Loading…
Cancel
Save