From 2aa33f2a36838ca04b6215efc62affc44cef9d36 Mon Sep 17 00:00:00 2001 From: Brannon King Date: Thu, 29 Aug 2019 08:56:10 -0600 Subject: [PATCH] reduced max open files on levelDB --- src/dbwrapper.cpp | 6 +++--- src/leveldb/include/leveldb/env.h | 3 +++ src/leveldb/table/format.cc | 18 +++++++++--------- src/leveldb/util/env_posix.cc | 20 ++++++++++++-------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 725d7aa1c..501ce9a82 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -80,14 +80,14 @@ static void SetMaxOpenFiles(leveldb::Options *options) { // implementation that does not use extra file descriptors (the fds are // closed after being mmap'ed). // - // Increasing the value beyond the default is dangerous because LevelDB will - // fall back to a non-mmap implementation when the file count is too large. + // Increasing the value beyond the nmap count is dangerous because LevelDB will + // fall back to a non-mmap implementation when the file count is too large (thus contending select()). // On 32-bit Unix host we should decrease the value because the handles use // up real fds, and we want to avoid fd exhaustion issues. // // See PR #12495 for further discussion. - int default_open_files = options->max_open_files; + int default_open_files = 400; #ifndef WIN32 if (sizeof(void*) < 8) { options->max_open_files = 64; diff --git a/src/leveldb/include/leveldb/env.h b/src/leveldb/include/leveldb/env.h index 275d441ea..5a40c1b14 100644 --- a/src/leveldb/include/leveldb/env.h +++ b/src/leveldb/include/leveldb/env.h @@ -221,6 +221,9 @@ class RandomAccessFile { // Get a name for the file, only for error reporting virtual std::string GetName() const = 0; + virtual char* AllocateScratch(std::size_t size) const { return new char[size]; }; + virtual void DeallocateScratch(char* pointer) const { delete[] pointer; }; + private: // No copying allowed RandomAccessFile(const RandomAccessFile&); diff --git a/src/leveldb/table/format.cc b/src/leveldb/table/format.cc index 285e1c0de..7edee0cf0 100644 --- a/src/leveldb/table/format.cc +++ b/src/leveldb/table/format.cc @@ -73,15 +73,15 @@ Status ReadBlock(RandomAccessFile* file, // Read the block contents as well as the type/crc footer. // See table_builder.cc for the code that built this structure. size_t n = static_cast(handle.size()); - char* buf = new char[n + kBlockTrailerSize]; + char* buf = file->AllocateScratch(n + kBlockTrailerSize); Slice contents; Status s = file->Read(handle.offset(), n + kBlockTrailerSize, &contents, buf); if (!s.ok()) { - delete[] buf; + file->DeallocateScratch(buf); return s; } if (contents.size() != n + kBlockTrailerSize) { - delete[] buf; + file->DeallocateScratch(buf); return Status::Corruption("truncated block read", file->GetName()); } @@ -91,7 +91,7 @@ Status ReadBlock(RandomAccessFile* file, const uint32_t crc = crc32c::Unmask(DecodeFixed32(data + n + 1)); const uint32_t actual = crc32c::Value(data, n + 1); if (actual != crc) { - delete[] buf; + file->DeallocateScratch(buf); s = Status::Corruption("block checksum mismatch", file->GetName()); return s; } @@ -103,7 +103,7 @@ Status ReadBlock(RandomAccessFile* file, // File implementation gave us pointer to some other data. // Use it directly under the assumption that it will be live // while the file is open. - delete[] buf; + file->DeallocateScratch(buf); result->data = Slice(data, n); result->heap_allocated = false; result->cachable = false; // Do not double-cache @@ -118,23 +118,23 @@ Status ReadBlock(RandomAccessFile* file, case kSnappyCompression: { size_t ulength = 0; if (!port::Snappy_GetUncompressedLength(data, n, &ulength)) { - delete[] buf; + file->DeallocateScratch(buf); return Status::Corruption("corrupted compressed block contents", file->GetName()); } char* ubuf = new char[ulength]; if (!port::Snappy_Uncompress(data, n, ubuf)) { - delete[] buf; + file->DeallocateScratch(buf); delete[] ubuf; return Status::Corruption("corrupted compressed block contents", file->GetName()); } - delete[] buf; + file->DeallocateScratch(buf); result->data = Slice(ubuf, ulength); result->heap_allocated = true; result->cachable = true; break; } default: - delete[] buf; + file->DeallocateScratch(buf); return Status::Corruption("bad block type", file->GetName()); } diff --git a/src/leveldb/util/env_posix.cc b/src/leveldb/util/env_posix.cc index f77918313..80dbb1c42 100644 --- a/src/leveldb/util/env_posix.cc +++ b/src/leveldb/util/env_posix.cc @@ -162,6 +162,7 @@ class PosixRandomAccessFile: public RandomAccessFile { } Status s; + assert(scratch); ssize_t r = pread(fd, scratch, n, static_cast(offset)); *result = Slice(scratch, (r < 0) ? 0 : r); if (r < 0) { @@ -188,19 +189,19 @@ class PosixMmapReadableFile: public RandomAccessFile { public: // base[0,length-1] contains the mmapped contents of the file. - PosixMmapReadableFile(const std::string& fname, void* base, size_t length, + PosixMmapReadableFile(std::string fname, void* base, size_t length, Limiter* limiter) - : filename_(fname), mmapped_region_(base), length_(length), + : filename_(std::move(fname)), mmapped_region_(base), length_(length), limiter_(limiter) { } - virtual ~PosixMmapReadableFile() { + ~PosixMmapReadableFile() override { munmap(mmapped_region_, length_); limiter_->Release(); } - virtual Status Read(uint64_t offset, size_t n, Slice* result, - char* scratch) const { + Status Read(uint64_t offset, size_t n, Slice* result, + char* scratch) const override { Status s; if (offset + n > length_) { *result = Slice(); @@ -211,7 +212,10 @@ class PosixMmapReadableFile: public RandomAccessFile { return s; } - virtual std::string GetName() const { return filename_; } + std::string GetName() const override { return filename_; } + + char* AllocateScratch(std::size_t size) const override { return nullptr; } + void DeallocateScratch(char* pointer) const override { assert(pointer == nullptr); } }; class PosixWritableFile : public WritableFile { @@ -585,8 +589,8 @@ static int MaxMmaps() { if (mmap_limit >= 0) { return mmap_limit; } - // Up to 4096 mmaps for 64-bit binaries; none for smaller pointer sizes. - mmap_limit = sizeof(void*) >= 8 ? 4096 : 0; + // Up to 400 mmaps for 64-bit binaries (800MB); none for smaller pointer sizes. + mmap_limit = sizeof(void*) >= 8 ? 400 : 0; return mmap_limit; }