From 6616e4d629e568cd8fbe1592320d630029548e84 Mon Sep 17 00:00:00 2001 From: Aaron Gao Date: Wed, 26 Apr 2017 14:21:04 -0700 Subject: [PATCH] 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 --- env/io_posix.cc | 22 +++++++++++++++++++++- env/io_posix.h | 3 +++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/env/io_posix.cc b/env/io_posix.cc index 41b120402..68cb70d23 100644 --- a/env/io_posix.cc +++ b/env/io_posix.cc @@ -38,7 +38,7 @@ namespace rocksdb { // 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) { #ifdef OS_LINUX return posix_fadvise(fd, offset, len, advice); @@ -337,6 +337,26 @@ Status PosixRandomAccessFile::Read(uint64_t offset, size_t n, Slice* result, return s; } +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(offset); + advice.ra_count = static_cast(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 { return PosixHelper::GetUniqueIdFromFile(fd_, id, max_size); diff --git a/env/io_posix.h b/env/io_posix.h index 351d6d7cb..af59939a9 100644 --- a/env/io_posix.h +++ b/env/io_posix.h @@ -79,6 +79,9 @@ class PosixRandomAccessFile : public RandomAccessFile { virtual Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const override; + + 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; #endif