From c847a317277c12999bb2fd66a83fb739dcd4c364 Mon Sep 17 00:00:00 2001 From: Dhruba Borthakur Date: Tue, 4 Dec 2012 09:47:05 -0800 Subject: [PATCH] Print compaction score for every compaction run. Summary: A compaction is picked based on its score. It is useful to print the compaction score in the LOG because it aids in debugging. If one looks at the logs, one can find out why a compaction was preferred over another. Test Plan: make clean check Differential Revision: https://reviews.facebook.net/D7137 --- db/db_impl.cc | 3 ++- db/version_set.cc | 8 +++++--- db/version_set.h | 6 +++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 5bffb8cda..e1a215416 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1349,11 +1349,12 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { int64_t imm_micros = 0; // Micros spent doing imm_ compactions Log(options_.info_log, - "Compacting %d@%d + %d@%d files, compaction slots available %d", + "Compacting %d@%d + %d@%d files, score %.2f slots available %d", compact->compaction->num_input_files(0), compact->compaction->level(), compact->compaction->num_input_files(1), compact->compaction->level() + 1, + compact->compaction->score(), options_.max_background_compactions - bg_compaction_scheduled_); char scratch[256]; compact->compaction->Summary(scratch, sizeof(scratch)); diff --git a/db/version_set.cc b/db/version_set.cc index ea5fa013e..4852148d6 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -1787,7 +1787,7 @@ uint64_t VersionSet::SizeBeingCompacted(int level) { return total; } -Compaction* VersionSet::PickCompactionBySize(int level) { +Compaction* VersionSet::PickCompactionBySize(int level, double score) { Compaction* c = NULL; // level 0 files are overlapping. So we cannot pick more @@ -1802,6 +1802,7 @@ Compaction* VersionSet::PickCompactionBySize(int level) { assert(level+1 < NumberLevels()); c = new Compaction(level, MaxFileSizeForLevel(level), MaxGrandParentOverlapBytes(level), NumberLevels()); + c->score_ = score; // Pick the largest file in this level that is not already // being compacted @@ -1876,7 +1877,7 @@ Compaction* VersionSet::PickCompaction() { current_->compaction_score_[i-1]); level = current_->compaction_level_[i]; if ((current_->compaction_score_[i] >= 1)) { - c = PickCompactionBySize(level); + c = PickCompactionBySize(level, current_->compaction_score_[i]); if (c != NULL) { break; } @@ -2076,7 +2077,8 @@ Compaction::Compaction(int level, uint64_t target_file_size, seen_key_(false), overlapped_bytes_(0), base_index_(-1), - parent_index_(-1) { + parent_index_(-1), + score_(0) { edit_ = new VersionEdit(number_levels_); level_ptrs_ = new size_t[number_levels_]; for (int i = 0; i < number_levels_; i++) { diff --git a/db/version_set.h b/db/version_set.h index 96d1c9bb4..b008d50e7 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -345,7 +345,7 @@ class VersionSet { // For the specfied level, pick a compaction. // Returns NULL if there is no compaction to be done. - Compaction* PickCompactionBySize(int level); + Compaction* PickCompactionBySize(int level, double score); // Free up the files that were participated in a compaction void ReleaseCompactionFiles(Compaction* c, Status status); @@ -502,6 +502,9 @@ class Compaction { void Summary(char* output, int len); + // Return the score that was used to pick this compaction run. + double score() const { return score_; } + private: friend class Version; friend class VersionSet; @@ -531,6 +534,7 @@ class Compaction { // and grandparent files int base_index_; // index of the file in files_[level_] int parent_index_; // index of some file with same range in files_[level_+1] + double score_; // score that was used to pick this compaction. // State for implementing IsBaseLevelForKey