From 49412d93e27ebb69eccddc966c7101f69abb6e65 Mon Sep 17 00:00:00 2001 From: Aaron Gao Date: Thu, 4 May 2017 01:21:31 -0700 Subject: [PATCH] fix memory alignment with logical sector size Summary: we align the buffer with logical sector size and should not test it with page size, which is usually 4k. Closes https://github.com/facebook/rocksdb/pull/2245 Differential Revision: D5001842 Pulled By: lightmark fbshipit-source-id: a7135fcf6351c6db363e8908956b1e193a4a6291 --- env/io_posix.cc | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/env/io_posix.cc b/env/io_posix.cc index 2fc7ad533..b978885b1 100644 --- a/env/io_posix.cc +++ b/env/io_posix.cc @@ -115,18 +115,13 @@ size_t GetLogicalBufferSize(int __attribute__((__unused__)) fd) { */ #ifndef NDEBUG namespace { -#ifdef OS_LINUX -const size_t kPageSize = sysconf(_SC_PAGESIZE); -#else -const size_t kPageSize = 4 * 1024; -#endif bool IsSectorAligned(const size_t off, size_t sector_size) { return off % sector_size == 0; } -static bool IsPageAligned(const void* ptr) { - return uintptr_t(ptr) % (kPageSize) == 0; +bool IsSectorAligned(const void* ptr, size_t sector_size) { + return uintptr_t(ptr) % sector_size == 0; } } @@ -182,6 +177,11 @@ Status PosixSequentialFile::Read(size_t n, Slice* result, char* scratch) { Status PosixSequentialFile::PositionedRead(uint64_t offset, size_t n, Slice* result, char* scratch) { + if (use_direct_io()) { + assert(IsSectorAligned(offset, GetRequiredBufferAlignment())); + assert(IsSectorAligned(n, GetRequiredBufferAlignment())); + assert(IsSectorAligned(scratch, GetRequiredBufferAlignment())); + } Status s; ssize_t r = -1; size_t left = n; @@ -306,6 +306,11 @@ PosixRandomAccessFile::~PosixRandomAccessFile() { close(fd_); } Status PosixRandomAccessFile::Read(uint64_t offset, size_t n, Slice* result, char* scratch) const { + if (use_direct_io()) { + assert(IsSectorAligned(offset, GetRequiredBufferAlignment())); + assert(IsSectorAligned(n, GetRequiredBufferAlignment())); + assert(IsSectorAligned(scratch, GetRequiredBufferAlignment())); + } Status s; ssize_t r = -1; size_t left = n; @@ -704,9 +709,10 @@ PosixWritableFile::~PosixWritableFile() { } Status PosixWritableFile::Append(const Slice& data) { - assert(!use_direct_io() || - (IsSectorAligned(data.size(), GetRequiredBufferAlignment()) && - IsPageAligned(data.data()))); + if (use_direct_io()) { + assert(IsSectorAligned(data.size(), GetRequiredBufferAlignment())); + assert(IsSectorAligned(data.data(), GetRequiredBufferAlignment())); + } const char* src = data.data(); size_t left = data.size(); while (left != 0) { @@ -725,10 +731,11 @@ Status PosixWritableFile::Append(const Slice& data) { } Status PosixWritableFile::PositionedAppend(const Slice& data, uint64_t offset) { - assert(use_direct_io() && - IsSectorAligned(offset, GetRequiredBufferAlignment()) && - IsSectorAligned(data.size(), GetRequiredBufferAlignment()) && - IsPageAligned(data.data())); + if (use_direct_io()) { + assert(IsSectorAligned(offset, GetRequiredBufferAlignment())); + assert(IsSectorAligned(data.size(), GetRequiredBufferAlignment())); + assert(IsSectorAligned(data.data(), GetRequiredBufferAlignment())); + } assert(offset <= std::numeric_limits::max()); const char* src = data.data(); size_t left = data.size();