add prefetch to PosixRandomAccessFile in buffered io

Summary:
Every time after a compaction/flush finish, we issue user reads to put the table into block cache which includes a couple of IO that read footer, index blocks, meta block, etc. So we implement Prefetch here to reduce IO.
Closes https://github.com/facebook/rocksdb/pull/2196

Differential Revision: D4931782

Pulled By: lightmark

fbshipit-source-id: 5a13d58dcab209964352322217193bbf7ff78149
This commit is contained in:
Aaron Gao 2017-04-26 14:21:04 -07:00 committed by Yi Wu
parent 1530f38baa
commit 6d29d8b3fa
2 changed files with 26 additions and 3 deletions

24
env/io_posix.cc vendored
View File

@ -37,7 +37,7 @@
namespace rocksdb { namespace rocksdb {
// A wrapper for fadvise, if the platform doesn't support fadvise, // A wrapper for fadvise, if the platform doesn't support fadvise,
// it will simply return Status::NotSupport. // it will simply return 0.
int Fadvise(int fd, off_t offset, size_t len, int advice) { int Fadvise(int fd, off_t offset, size_t len, int advice) {
#ifdef OS_LINUX #ifdef OS_LINUX
return posix_fadvise(fd, offset, len, advice); return posix_fadvise(fd, offset, len, advice);
@ -336,7 +336,27 @@ Status PosixRandomAccessFile::Read(uint64_t offset, size_t n, Slice* result,
return s; return s;
} }
#if defined(OS_LINUX) || defined(OS_MACOSX) Status PosixRandomAccessFile::Prefetch(uint64_t offset, size_t n) {
Status s;
if (!use_direct_io()) {
ssize_t r = 0;
#ifdef OS_LINUX
r = readahead(fd_, offset, n);
#endif
#ifdef OS_MACOSX
radvisory advice;
advice.ra_offset = static_cast<off_t>(offset);
advice.ra_count = static_cast<int>(n);
r = fcntl(fd_, F_RDADVISE, &advice);
#endif
if (r == -1) {
s = IOError(filename_, errno);
}
}
return s;
}
#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_AIX)
size_t PosixRandomAccessFile::GetUniqueId(char* id, size_t max_size) const { size_t PosixRandomAccessFile::GetUniqueId(char* id, size_t max_size) const {
return PosixHelper::GetUniqueIdFromFile(fd_, id, max_size); return PosixHelper::GetUniqueIdFromFile(fd_, id, max_size);
} }

5
env/io_posix.h vendored
View File

@ -79,7 +79,10 @@ class PosixRandomAccessFile : public RandomAccessFile {
virtual Status Read(uint64_t offset, size_t n, Slice* result, virtual Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const override; char* scratch) const override;
#if defined(OS_LINUX) || defined(OS_MACOSX)
virtual Status Prefetch(uint64_t offset, size_t n) override;
#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_AIX)
virtual size_t GetUniqueId(char* id, size_t max_size) const override; virtual size_t GetUniqueId(char* id, size_t max_size) const override;
#endif #endif
virtual void Hint(AccessPattern pattern) override; virtual void Hint(AccessPattern pattern) override;