diff --git a/db/table_cache.cc b/db/table_cache.cc index d7afa0a557..700db74db4 100644 --- a/db/table_cache.cc +++ b/db/table_cache.cc @@ -100,7 +100,7 @@ Iterator* TableCache::NewIterator(const ReadOptions& options, } if (for_compaction) { - table->SetAccessHintForCompaction(); + table->SetupForCompaction(); } return result; diff --git a/include/leveldb/statistics.h b/include/leveldb/statistics.h index 67b5035c34..9fa5352cee 100644 --- a/include/leveldb/statistics.h +++ b/include/leveldb/statistics.h @@ -103,10 +103,13 @@ enum Histograms { // TIME SPENT IN IO DURING TABLE OPEN TABLE_OPEN_IO_MICROS = 7, DB_MULTIGET = 8, - HISTOGRAM_ENUM_MAX = 9 + READ_BLOCK_COMPACTION_MICROS = 9, + READ_BLOCK_GET_MICROS = 10, + HISTOGRAM_ENUM_MAX = 11 }; const std::vector> HistogramsNameMap = { + // Over 80 char by choice std::make_pair(DB_GET, "rocksdb.db.get.micros"), std::make_pair(DB_WRITE, "rocksdb.db.write.micros"), std::make_pair(COMPACTION_TIME, "rocksdb.compaction.times.micros"), @@ -115,7 +118,9 @@ const std::vector> HistogramsNameMap = { std::make_pair(WAL_FILE_SYNC_MICROS, "rocksdb.wal.file.sync.micros"), std::make_pair(MANIFEST_FILE_SYNC_MICROS, "rocksdb.manifest.file.sync.micros"), std::make_pair(TABLE_OPEN_IO_MICROS, "rocksdb.table.open.io.micros"), - std::make_pair(DB_MULTIGET, "rocksdb.db.multiget.micros") + std::make_pair(DB_MULTIGET, "rocksdb.db.multiget.micros"), + std::make_pair(READ_BLOCK_COMPACTION_MICROS, "rocksdb.read.block.compaction.micros"), + std::make_pair(READ_BLOCK_GET_MICROS, "rocksdb.read.block.get.micros") }; struct HistogramData { diff --git a/table/table.cc b/table/table.cc index 06a472f2a1..5aceebe0f6 100644 --- a/table/table.cc +++ b/table/table.cc @@ -2,18 +2,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. +#include "table/table.h" + #include "leveldb/cache.h" #include "leveldb/comparator.h" #include "leveldb/env.h" #include "leveldb/filter_policy.h" #include "leveldb/options.h" #include "leveldb/statistics.h" + #include "table/block.h" #include "table/filter_block.h" #include "table/format.h" -#include "table/table.h" #include "table/two_level_iterator.h" + #include "util/coding.h" +#include "util/stop_watch.h" namespace leveldb { @@ -141,7 +145,7 @@ Status Table::Open(const Options& options, return s; } -void Table::SetAccessHintForCompaction() { +void Table::SetupForCompaction() { switch (rep_->options.access_hint_on_compaction_start) { case Options::NONE: break; @@ -157,6 +161,7 @@ void Table::SetAccessHintForCompaction() { default: assert(false); } + compaction_optimized_ = true; } void Table::ReadMeta(const Footer& footer) { @@ -229,7 +234,8 @@ static void ReleaseBlock(void* arg, void* h) { Iterator* Table::BlockReader(void* arg, const ReadOptions& options, const Slice& index_value, - bool* didIO) { + bool* didIO, + bool for_compaction) { Table* table = reinterpret_cast(arg); Cache* block_cache = table->rep_->options.block_cache.get(); std::shared_ptr statistics = table->rep_->options.statistics; @@ -259,7 +265,18 @@ Iterator* Table::BlockReader(void* arg, RecordTick(statistics, BLOCK_CACHE_HIT); } else { - s = ReadBlock(table->rep_->file.get(), options, handle, &block, didIO); + Histograms histogram = for_compaction ? + READ_BLOCK_COMPACTION_MICROS : READ_BLOCK_GET_MICROS; + { // block for stop watch + StopWatch sw(table->rep_->options.env, statistics, histogram); + s = ReadBlock( + table->rep_->file.get(), + options, + handle, + &block, + didIO + ); + } if (s.ok()) { if (block->isCachable() && options.fill_cache) { cache_handle = block_cache->Insert( @@ -293,7 +310,7 @@ Iterator* Table::BlockReader(void* arg, const EnvOptions& soptions, const Slice& index_value, bool for_compaction) { - return BlockReader(arg, options, index_value, nullptr); + return BlockReader(arg, options, index_value, nullptr, for_compaction); } Iterator* Table::NewIterator(const ReadOptions& options) const { diff --git a/table/table.h b/table/table.h index 2baf667b5e..a657290b5e 100644 --- a/table/table.h +++ b/table/table.h @@ -64,18 +64,20 @@ class Table { // REQUIRES: key is in this table. bool TEST_KeyInCache(const ReadOptions& options, const Slice& key); - void SetAccessHintForCompaction(); + // Set up the table for Compaction. Might change some parameters with + // posix_fadvise + void SetupForCompaction(); private: struct Rep; Rep* rep_; - - explicit Table(Rep* rep) { rep_ = rep; } + bool compaction_optimized_; + explicit Table(Rep* rep) : compaction_optimized_(false) { rep_ = rep; } static Iterator* BlockReader(void*, const ReadOptions&, const EnvOptions& soptions, const Slice&, bool for_compaction); static Iterator* BlockReader(void*, const ReadOptions&, const Slice&, - bool* didIO); + bool* didIO, bool for_compaction = false); // Calls (*handle_result)(arg, ...) repeatedly, starting with the entry found // after a call to Seek(key), until handle_result returns false.