internal_stats.cc 49.8 KB
Newer Older
S
Siying Dong 已提交
1 2 3
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
I
Igor Canadi 已提交
4 5 6 7 8 9
//
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// 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 "db/internal_stats.h"
L
liuhuahang 已提交
10 11

#ifndef __STDC_FORMAT_MACROS
I
Igor Canadi 已提交
12
#define __STDC_FORMAT_MACROS
L
liuhuahang 已提交
13 14
#endif

I
Igor Canadi 已提交
15
#include <inttypes.h>
16
#include <string>
17
#include <algorithm>
18
#include <utility>
I
Igor Canadi 已提交
19
#include <vector>
20
#include "db/column_family.h"
21

22
#include "db/db_impl.h"
23
#include "util/string_util.h"
I
Igor Canadi 已提交
24 25 26

namespace rocksdb {

27
#ifndef ROCKSDB_LITE
28 29 30 31 32 33

const std::map<LevelStatType, LevelStat> InternalStats::compaction_level_stats =
    {
        {LevelStatType::NUM_FILES, LevelStat{"NumFiles", "Files"}},
        {LevelStatType::COMPACTED_FILES,
         LevelStat{"CompactedFiles", "CompactedFiles"}},
34
        {LevelStatType::SIZE_BYTES, LevelStat{"SizeBytes", "Size"}},
35
        {LevelStatType::SCORE, LevelStat{"Score", "Score"}},
36 37 38 39 40 41
        {LevelStatType::READ_GB, LevelStat{"ReadGB", "Read(GB)"}},
        {LevelStatType::RN_GB, LevelStat{"RnGB", "Rn(GB)"}},
        {LevelStatType::RNP1_GB, LevelStat{"Rnp1GB", "Rnp1(GB)"}},
        {LevelStatType::WRITE_GB, LevelStat{"WriteGB", "Write(GB)"}},
        {LevelStatType::W_NEW_GB, LevelStat{"WnewGB", "Wnew(GB)"}},
        {LevelStatType::MOVED_GB, LevelStat{"MovedGB", "Moved(GB)"}},
42
        {LevelStatType::WRITE_AMP, LevelStat{"WriteAmp", "W-Amp"}},
43 44 45 46 47
        {LevelStatType::READ_MBPS, LevelStat{"ReadMBps", "Rd(MB/s)"}},
        {LevelStatType::WRITE_MBPS, LevelStat{"WriteMBps", "Wr(MB/s)"}},
        {LevelStatType::COMP_SEC, LevelStat{"CompSec", "Comp(sec)"}},
        {LevelStatType::COMP_COUNT, LevelStat{"CompCount", "Comp(cnt)"}},
        {LevelStatType::AVG_SEC, LevelStat{"AvgSec", "Avg(sec)"}},
48 49 50 51
        {LevelStatType::KEY_IN, LevelStat{"KeyIn", "KeyIn"}},
        {LevelStatType::KEY_DROP, LevelStat{"KeyDrop", "KeyDrop"}},
};

L
Lei Jin 已提交
52 53 54
namespace {
const double kMB = 1048576.0;
const double kGB = kMB * 1024;
55
const double kMicrosInSec = 1000000.0;
L
Lei Jin 已提交
56

57
void PrintLevelStatsHeader(char* buf, size_t len, const std::string& cf_name) {
58 59 60 61 62 63 64
  int written_size =
      snprintf(buf, len, "\n** Compaction Stats [%s] **\n", cf_name.c_str());
  auto hdr = [](LevelStatType t) {
    return InternalStats::compaction_level_stats.at(t).header_name.c_str();
  };
  int line_size = snprintf(
      buf + written_size, len - written_size,
65
      "Level    %s   %s     %s %s  %s %s %s %s %s %s %s %s %s %s %s %s %s\n",
66
      // Note that we skip COMPACTED_FILES and merge it with Files column
67
      hdr(LevelStatType::NUM_FILES), hdr(LevelStatType::SIZE_BYTES),
68 69 70 71 72 73 74 75 76 77 78 79
      hdr(LevelStatType::SCORE), hdr(LevelStatType::READ_GB),
      hdr(LevelStatType::RN_GB), hdr(LevelStatType::RNP1_GB),
      hdr(LevelStatType::WRITE_GB), hdr(LevelStatType::W_NEW_GB),
      hdr(LevelStatType::MOVED_GB), hdr(LevelStatType::WRITE_AMP),
      hdr(LevelStatType::READ_MBPS), hdr(LevelStatType::WRITE_MBPS),
      hdr(LevelStatType::COMP_SEC), hdr(LevelStatType::COMP_COUNT),
      hdr(LevelStatType::AVG_SEC), hdr(LevelStatType::KEY_IN),
      hdr(LevelStatType::KEY_DROP));

  written_size += line_size;
  snprintf(buf + written_size, len - written_size, "%s\n",
           std::string(line_size, '-').c_str());
L
Lei Jin 已提交
80 81
}

82 83 84 85
void PrepareLevelStats(std::map<LevelStatType, double>* level_stats,
                       int num_files, int being_compacted,
                       double total_file_size, double score, double w_amp,
                       const InternalStats::CompactionStats& stats) {
86 87 88 89
  uint64_t bytes_read =
      stats.bytes_read_non_output_levels + stats.bytes_read_output_level;
  int64_t bytes_new =
      stats.bytes_written - stats.bytes_read_output_level;
90
  double elapsed = (stats.micros + 1) / kMicrosInSec;
L
Lei Jin 已提交
91

92 93
  (*level_stats)[LevelStatType::NUM_FILES] = num_files;
  (*level_stats)[LevelStatType::COMPACTED_FILES] = being_compacted;
94
  (*level_stats)[LevelStatType::SIZE_BYTES] = total_file_size;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  (*level_stats)[LevelStatType::SCORE] = score;
  (*level_stats)[LevelStatType::READ_GB] = bytes_read / kGB;
  (*level_stats)[LevelStatType::RN_GB] =
      stats.bytes_read_non_output_levels / kGB;
  (*level_stats)[LevelStatType::RNP1_GB] = stats.bytes_read_output_level / kGB;
  (*level_stats)[LevelStatType::WRITE_GB] = stats.bytes_written / kGB;
  (*level_stats)[LevelStatType::W_NEW_GB] = bytes_new / kGB;
  (*level_stats)[LevelStatType::MOVED_GB] = stats.bytes_moved / kGB;
  (*level_stats)[LevelStatType::WRITE_AMP] = w_amp;
  (*level_stats)[LevelStatType::READ_MBPS] = bytes_read / kMB / elapsed;
  (*level_stats)[LevelStatType::WRITE_MBPS] =
      stats.bytes_written / kMB / elapsed;
  (*level_stats)[LevelStatType::COMP_SEC] = stats.micros / kMicrosInSec;
  (*level_stats)[LevelStatType::COMP_COUNT] = stats.count;
  (*level_stats)[LevelStatType::AVG_SEC] =
      stats.count == 0 ? 0 : stats.micros / kMicrosInSec / stats.count;
  (*level_stats)[LevelStatType::KEY_IN] =
      static_cast<double>(stats.num_input_records);
  (*level_stats)[LevelStatType::KEY_DROP] =
      static_cast<double>(stats.num_dropped_records);
}

void PrintLevelStats(char* buf, size_t len, const std::string& name,
                     const std::map<LevelStatType, double>& stat_value) {
L
Lei Jin 已提交
119
  snprintf(buf, len,
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
           "%4s "      /*  Level */
           "%6d/%-3d " /*  Files */
           "%8s "      /*  Size */
           "%5.1f "    /*  Score */
           "%8.1f "    /*  Read(GB) */
           "%7.1f "    /*  Rn(GB) */
           "%8.1f "    /*  Rnp1(GB) */
           "%9.1f "    /*  Write(GB) */
           "%8.1f "    /*  Wnew(GB) */
           "%9.1f "    /*  Moved(GB) */
           "%5.1f "    /*  W-Amp */
           "%8.1f "    /*  Rd(MB/s) */
           "%8.1f "    /*  Wr(MB/s) */
           "%9.0f "    /*  Comp(sec) */
           "%9d "      /*  Comp(cnt) */
           "%8.3f "    /*  Avg(sec) */
           "%7s "      /*  KeyIn */
           "%6s\n",    /*  KeyDrop */
138
           name.c_str(),
139 140
           static_cast<int>(stat_value.at(LevelStatType::NUM_FILES)),
           static_cast<int>(stat_value.at(LevelStatType::COMPACTED_FILES)),
141 142 143
           BytesToHumanString(
               static_cast<uint64_t>(stat_value.at(LevelStatType::SIZE_BYTES)))
               .c_str(),
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
           stat_value.at(LevelStatType::SCORE),
           stat_value.at(LevelStatType::READ_GB),
           stat_value.at(LevelStatType::RN_GB),
           stat_value.at(LevelStatType::RNP1_GB),
           stat_value.at(LevelStatType::WRITE_GB),
           stat_value.at(LevelStatType::W_NEW_GB),
           stat_value.at(LevelStatType::MOVED_GB),
           stat_value.at(LevelStatType::WRITE_AMP),
           stat_value.at(LevelStatType::READ_MBPS),
           stat_value.at(LevelStatType::WRITE_MBPS),
           stat_value.at(LevelStatType::COMP_SEC),
           static_cast<int>(stat_value.at(LevelStatType::COMP_COUNT)),
           stat_value.at(LevelStatType::AVG_SEC),
           NumberToHumanString(
               static_cast<std::int64_t>(stat_value.at(LevelStatType::KEY_IN)))
               .c_str(),
           NumberToHumanString(static_cast<std::int64_t>(
                                   stat_value.at(LevelStatType::KEY_DROP)))
               .c_str());
}

void PrintLevelStats(char* buf, size_t len, const std::string& name,
                     int num_files, int being_compacted, double total_file_size,
                     double score, double w_amp,
                     const InternalStats::CompactionStats& stats) {
  std::map<LevelStatType, double> level_stats;
  PrepareLevelStats(&level_stats, num_files, being_compacted, total_file_size,
                    score, w_amp, stats);
  PrintLevelStats(buf, len, name, level_stats);
L
Lei Jin 已提交
173
}
174 175 176 177 178 179 180 181 182 183 184 185 186

// Assumes that trailing numbers represent an optional argument. This requires
// property names to not end with numbers.
std::pair<Slice, Slice> GetPropertyNameAndArg(const Slice& property) {
  Slice name = property, arg = property;
  size_t sfx_len = 0;
  while (sfx_len < property.size() &&
         isdigit(property[property.size() - sfx_len - 1])) {
    ++sfx_len;
  }
  name.remove_suffix(sfx_len);
  arg.remove_prefix(property.size() - sfx_len);
  return {name, arg};
L
Lei Jin 已提交
187
}
188
}  // anonymous namespace
L
Lei Jin 已提交
189

190 191 192
static const std::string rocksdb_prefix = "rocksdb.";

static const std::string num_files_at_level_prefix = "num-files-at-level";
193 194
static const std::string compression_ratio_at_level_prefix =
    "compression-ratio-at-level";
195
static const std::string allstats = "stats";
196 197
static const std::string sstables = "sstables";
static const std::string cfstats = "cfstats";
198 199 200
static const std::string cfstats_no_file_histogram =
    "cfstats-no-file-histogram";
static const std::string cf_file_histogram = "cf-file-histogram";
201 202 203
static const std::string dbstats = "dbstats";
static const std::string levelstats = "levelstats";
static const std::string num_immutable_mem_table = "num-immutable-mem-table";
204 205
static const std::string num_immutable_mem_table_flushed =
    "num-immutable-mem-table-flushed";
206 207 208 209 210
static const std::string mem_table_flush_pending = "mem-table-flush-pending";
static const std::string compaction_pending = "compaction-pending";
static const std::string background_errors = "background-errors";
static const std::string cur_size_active_mem_table =
                          "cur-size-active-mem-table";
211 212
static const std::string cur_size_all_mem_tables = "cur-size-all-mem-tables";
static const std::string size_all_mem_tables = "size-all-mem-tables";
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static const std::string num_entries_active_mem_table =
                          "num-entries-active-mem-table";
static const std::string num_entries_imm_mem_tables =
                          "num-entries-imm-mem-tables";
static const std::string num_deletes_active_mem_table =
                          "num-deletes-active-mem-table";
static const std::string num_deletes_imm_mem_tables =
                          "num-deletes-imm-mem-tables";
static const std::string estimate_num_keys = "estimate-num-keys";
static const std::string estimate_table_readers_mem =
                          "estimate-table-readers-mem";
static const std::string is_file_deletions_enabled =
                          "is-file-deletions-enabled";
static const std::string num_snapshots = "num-snapshots";
static const std::string oldest_snapshot_time = "oldest-snapshot-time";
static const std::string num_live_versions = "num-live-versions";
229 230
static const std::string current_version_number =
    "current-super-version-number";
A
Andres Notzli 已提交
231
static const std::string estimate_live_data_size = "estimate-live-data-size";
232
static const std::string min_log_number_to_keep = "min-log-number-to-keep";
233
static const std::string base_level = "base-level";
234
static const std::string total_sst_files_size = "total-sst-files-size";
235 236
static const std::string estimate_pending_comp_bytes =
    "estimate-pending-compaction-bytes";
237 238 239 240
static const std::string aggregated_table_properties =
    "aggregated-table-properties";
static const std::string aggregated_table_properties_at_level =
    aggregated_table_properties + "-at-level";
241 242
static const std::string num_running_compactions = "num-running-compactions";
static const std::string num_running_flushes = "num-running-flushes";
243 244 245
static const std::string actual_delayed_write_rate =
    "actual-delayed-write-rate";
static const std::string is_write_stopped = "is-write-stopped";
246 247 248

const std::string DB::Properties::kNumFilesAtLevelPrefix =
                      rocksdb_prefix + num_files_at_level_prefix;
249 250
const std::string DB::Properties::kCompressionRatioAtLevelPrefix =
                      rocksdb_prefix + compression_ratio_at_level_prefix;
251
const std::string DB::Properties::kStats = rocksdb_prefix + allstats;
252 253
const std::string DB::Properties::kSSTables = rocksdb_prefix + sstables;
const std::string DB::Properties::kCFStats = rocksdb_prefix + cfstats;
254 255 256 257
const std::string DB::Properties::kCFStatsNoFileHistogram =
    rocksdb_prefix + cfstats_no_file_histogram;
const std::string DB::Properties::kCFFileHistogram =
    rocksdb_prefix + cf_file_histogram;
258
const std::string DB::Properties::kDBStats = rocksdb_prefix + dbstats;
259
const std::string DB::Properties::kLevelStats = rocksdb_prefix + levelstats;
260 261
const std::string DB::Properties::kNumImmutableMemTable =
                      rocksdb_prefix + num_immutable_mem_table;
262
const std::string DB::Properties::kNumImmutableMemTableFlushed =
263
    rocksdb_prefix + num_immutable_mem_table_flushed;
264 265 266 267
const std::string DB::Properties::kMemTableFlushPending =
                      rocksdb_prefix + mem_table_flush_pending;
const std::string DB::Properties::kCompactionPending =
                      rocksdb_prefix + compaction_pending;
268 269 270 271
const std::string DB::Properties::kNumRunningCompactions =
    rocksdb_prefix + num_running_compactions;
const std::string DB::Properties::kNumRunningFlushes =
    rocksdb_prefix + num_running_flushes;
272 273 274 275 276
const std::string DB::Properties::kBackgroundErrors =
                      rocksdb_prefix + background_errors;
const std::string DB::Properties::kCurSizeActiveMemTable =
                      rocksdb_prefix + cur_size_active_mem_table;
const std::string DB::Properties::kCurSizeAllMemTables =
277
    rocksdb_prefix + cur_size_all_mem_tables;
278 279
const std::string DB::Properties::kSizeAllMemTables =
    rocksdb_prefix + size_all_mem_tables;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
const std::string DB::Properties::kNumEntriesActiveMemTable =
                      rocksdb_prefix + num_entries_active_mem_table;
const std::string DB::Properties::kNumEntriesImmMemTables =
                      rocksdb_prefix + num_entries_imm_mem_tables;
const std::string DB::Properties::kNumDeletesActiveMemTable =
                      rocksdb_prefix + num_deletes_active_mem_table;
const std::string DB::Properties::kNumDeletesImmMemTables =
                      rocksdb_prefix + num_deletes_imm_mem_tables;
const std::string DB::Properties::kEstimateNumKeys =
                      rocksdb_prefix + estimate_num_keys;
const std::string DB::Properties::kEstimateTableReadersMem =
                      rocksdb_prefix + estimate_table_readers_mem;
const std::string DB::Properties::kIsFileDeletionsEnabled =
                      rocksdb_prefix + is_file_deletions_enabled;
const std::string DB::Properties::kNumSnapshots =
                      rocksdb_prefix + num_snapshots;
const std::string DB::Properties::kOldestSnapshotTime =
                      rocksdb_prefix + oldest_snapshot_time;
const std::string DB::Properties::kNumLiveVersions =
                      rocksdb_prefix + num_live_versions;
300
const std::string DB::Properties::kCurrentSuperVersionNumber =
301
    rocksdb_prefix + current_version_number;
A
Andres Notzli 已提交
302 303
const std::string DB::Properties::kEstimateLiveDataSize =
                      rocksdb_prefix + estimate_live_data_size;
304 305
const std::string DB::Properties::kMinLogNumberToKeep =
    rocksdb_prefix + min_log_number_to_keep;
306 307
const std::string DB::Properties::kTotalSstFilesSize =
                      rocksdb_prefix + total_sst_files_size;
308
const std::string DB::Properties::kBaseLevel = rocksdb_prefix + base_level;
309 310
const std::string DB::Properties::kEstimatePendingCompactionBytes =
    rocksdb_prefix + estimate_pending_comp_bytes;
311 312 313 314
const std::string DB::Properties::kAggregatedTableProperties =
    rocksdb_prefix + aggregated_table_properties;
const std::string DB::Properties::kAggregatedTablePropertiesAtLevel =
    rocksdb_prefix + aggregated_table_properties_at_level;
315 316 317 318
const std::string DB::Properties::kActualDelayedWriteRate =
    rocksdb_prefix + actual_delayed_write_rate;
const std::string DB::Properties::kIsWriteStopped =
    rocksdb_prefix + is_write_stopped;
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333
const std::unordered_map<std::string, DBPropertyInfo>
    InternalStats::ppt_name_to_info = {
        {DB::Properties::kNumFilesAtLevelPrefix,
         {false, &InternalStats::HandleNumFilesAtLevel, nullptr, nullptr}},
        {DB::Properties::kCompressionRatioAtLevelPrefix,
         {false, &InternalStats::HandleCompressionRatioAtLevelPrefix, nullptr,
          nullptr}},
        {DB::Properties::kLevelStats,
         {false, &InternalStats::HandleLevelStats, nullptr, nullptr}},
        {DB::Properties::kStats,
         {false, &InternalStats::HandleStats, nullptr, nullptr}},
        {DB::Properties::kCFStats,
         {false, &InternalStats::HandleCFStats, nullptr,
          &InternalStats::HandleCFMapStats}},
334 335 336 337 338
        {DB::Properties::kCFStatsNoFileHistogram,
         {false, &InternalStats::HandleCFStatsNoFileHistogram, nullptr,
          nullptr}},
        {DB::Properties::kCFFileHistogram,
         {false, &InternalStats::HandleCFFileHistogram, nullptr, nullptr}},
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        {DB::Properties::kDBStats,
         {false, &InternalStats::HandleDBStats, nullptr, nullptr}},
        {DB::Properties::kSSTables,
         {false, &InternalStats::HandleSsTables, nullptr, nullptr}},
        {DB::Properties::kAggregatedTableProperties,
         {false, &InternalStats::HandleAggregatedTableProperties, nullptr,
          nullptr}},
        {DB::Properties::kAggregatedTablePropertiesAtLevel,
         {false, &InternalStats::HandleAggregatedTablePropertiesAtLevel,
          nullptr, nullptr}},
        {DB::Properties::kNumImmutableMemTable,
         {false, nullptr, &InternalStats::HandleNumImmutableMemTable, nullptr}},
        {DB::Properties::kNumImmutableMemTableFlushed,
         {false, nullptr, &InternalStats::HandleNumImmutableMemTableFlushed,
          nullptr}},
        {DB::Properties::kMemTableFlushPending,
         {false, nullptr, &InternalStats::HandleMemTableFlushPending, nullptr}},
        {DB::Properties::kCompactionPending,
         {false, nullptr, &InternalStats::HandleCompactionPending, nullptr}},
        {DB::Properties::kBackgroundErrors,
         {false, nullptr, &InternalStats::HandleBackgroundErrors, nullptr}},
        {DB::Properties::kCurSizeActiveMemTable,
         {false, nullptr, &InternalStats::HandleCurSizeActiveMemTable,
          nullptr}},
        {DB::Properties::kCurSizeAllMemTables,
         {false, nullptr, &InternalStats::HandleCurSizeAllMemTables, nullptr}},
        {DB::Properties::kSizeAllMemTables,
         {false, nullptr, &InternalStats::HandleSizeAllMemTables, nullptr}},
        {DB::Properties::kNumEntriesActiveMemTable,
         {false, nullptr, &InternalStats::HandleNumEntriesActiveMemTable,
          nullptr}},
        {DB::Properties::kNumEntriesImmMemTables,
         {false, nullptr, &InternalStats::HandleNumEntriesImmMemTables,
          nullptr}},
        {DB::Properties::kNumDeletesActiveMemTable,
         {false, nullptr, &InternalStats::HandleNumDeletesActiveMemTable,
          nullptr}},
        {DB::Properties::kNumDeletesImmMemTables,
         {false, nullptr, &InternalStats::HandleNumDeletesImmMemTables,
          nullptr}},
        {DB::Properties::kEstimateNumKeys,
         {false, nullptr, &InternalStats::HandleEstimateNumKeys, nullptr}},
        {DB::Properties::kEstimateTableReadersMem,
         {true, nullptr, &InternalStats::HandleEstimateTableReadersMem,
          nullptr}},
        {DB::Properties::kIsFileDeletionsEnabled,
         {false, nullptr, &InternalStats::HandleIsFileDeletionsEnabled,
          nullptr}},
        {DB::Properties::kNumSnapshots,
         {false, nullptr, &InternalStats::HandleNumSnapshots, nullptr}},
        {DB::Properties::kOldestSnapshotTime,
         {false, nullptr, &InternalStats::HandleOldestSnapshotTime, nullptr}},
        {DB::Properties::kNumLiveVersions,
         {false, nullptr, &InternalStats::HandleNumLiveVersions, nullptr}},
        {DB::Properties::kCurrentSuperVersionNumber,
         {false, nullptr, &InternalStats::HandleCurrentSuperVersionNumber,
          nullptr}},
        {DB::Properties::kEstimateLiveDataSize,
         {true, nullptr, &InternalStats::HandleEstimateLiveDataSize, nullptr}},
398 399
        {DB::Properties::kMinLogNumberToKeep,
         {false, nullptr, &InternalStats::HandleMinLogNumberToKeep, nullptr}},
400 401 402 403 404 405 406 407 408 409 410 411
        {DB::Properties::kBaseLevel,
         {false, nullptr, &InternalStats::HandleBaseLevel, nullptr}},
        {DB::Properties::kTotalSstFilesSize,
         {false, nullptr, &InternalStats::HandleTotalSstFilesSize, nullptr}},
        {DB::Properties::kEstimatePendingCompactionBytes,
         {false, nullptr, &InternalStats::HandleEstimatePendingCompactionBytes,
          nullptr}},
        {DB::Properties::kNumRunningFlushes,
         {false, nullptr, &InternalStats::HandleNumRunningFlushes, nullptr}},
        {DB::Properties::kNumRunningCompactions,
         {false, nullptr, &InternalStats::HandleNumRunningCompactions,
          nullptr}},
412 413 414 415 416
        {DB::Properties::kActualDelayedWriteRate,
         {false, nullptr, &InternalStats::HandleActualDelayedWriteRate,
          nullptr}},
        {DB::Properties::kIsWriteStopped,
         {false, nullptr, &InternalStats::HandleIsWriteStopped, nullptr}},
417 418 419 420 421 422 423
};

const DBPropertyInfo* GetPropertyInfo(const Slice& property) {
  std::string ppt_name = GetPropertyNameAndArg(property).first.ToString();
  auto ppt_info_iter = InternalStats::ppt_name_to_info.find(ppt_name);
  if (ppt_info_iter == InternalStats::ppt_name_to_info.end()) {
    return nullptr;
424
  }
425 426
  return &ppt_info_iter->second;
}
427

428 429 430 431 432 433 434
bool InternalStats::GetStringProperty(const DBPropertyInfo& property_info,
                                      const Slice& property,
                                      std::string* value) {
  assert(value != nullptr);
  assert(property_info.handle_string != nullptr);
  Slice arg = GetPropertyNameAndArg(property).second;
  return (this->*(property_info.handle_string))(value, arg);
435 436
}

437
bool InternalStats::GetMapProperty(const DBPropertyInfo& property_info,
438
                                   const Slice& property,
439 440 441 442 443 444
                                   std::map<std::string, double>* value) {
  assert(value != nullptr);
  assert(property_info.handle_map != nullptr);
  return (this->*(property_info.handle_map))(value);
}

445 446
bool InternalStats::GetIntProperty(const DBPropertyInfo& property_info,
                                   uint64_t* value, DBImpl* db) {
447
  assert(value != nullptr);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  assert(property_info.handle_int != nullptr &&
         !property_info.need_out_of_mutex);
  db->mutex_.AssertHeld();
  return (this->*(property_info.handle_int))(value, db, nullptr /* version */);
}

bool InternalStats::GetIntPropertyOutOfMutex(
    const DBPropertyInfo& property_info, Version* version, uint64_t* value) {
  assert(value != nullptr);
  assert(property_info.handle_int != nullptr &&
         property_info.need_out_of_mutex);
  return (this->*(property_info.handle_int))(value, nullptr /* db */, version);
}

bool InternalStats::HandleNumFilesAtLevel(std::string* value, Slice suffix) {
  uint64_t level;
A
Andres Notzli 已提交
464
  const auto* vstorage = cfd_->current()->storage_info();
465 466 467 468 469 470 471 472 473 474 475 476
  bool ok = ConsumeDecimalNumber(&suffix, &level) && suffix.empty();
  if (!ok || static_cast<int>(level) >= number_levels_) {
    return false;
  } else {
    char buf[100];
    snprintf(buf, sizeof(buf), "%d",
             vstorage->NumLevelFiles(static_cast<int>(level)));
    *value = buf;
    return true;
  }
}

477 478 479 480 481 482 483 484 485 486 487 488 489
bool InternalStats::HandleCompressionRatioAtLevelPrefix(std::string* value,
                                                        Slice suffix) {
  uint64_t level;
  const auto* vstorage = cfd_->current()->storage_info();
  bool ok = ConsumeDecimalNumber(&suffix, &level) && suffix.empty();
  if (!ok || level >= static_cast<uint64_t>(number_levels_)) {
    return false;
  }
  *value = ToString(
      vstorage->GetEstimatedCompressionRatioAtLevel(static_cast<int>(level)));
  return true;
}

490
bool InternalStats::HandleLevelStats(std::string* value, Slice suffix) {
491 492 493 494 495 496
  char buf[1000];
  const auto* vstorage = cfd_->current()->storage_info();
  snprintf(buf, sizeof(buf),
           "Level Files Size(MB)\n"
           "--------------------\n");
  value->append(buf);
A
Andres Notzli 已提交
497

498 499 500 501 502
  for (int level = 0; level < number_levels_; level++) {
    snprintf(buf, sizeof(buf), "%3d %8d %8.0f\n", level,
             vstorage->NumLevelFiles(level),
             vstorage->NumLevelBytes(level) / kMB);
    value->append(buf);
503
  }
504
  return true;
505
}
506

507 508 509 510 511 512 513 514 515 516
bool InternalStats::HandleStats(std::string* value, Slice suffix) {
  if (!HandleCFStats(value, suffix)) {
    return false;
  }
  if (!HandleDBStats(value, suffix)) {
    return false;
  }
  return true;
}

517 518 519 520 521
bool InternalStats::HandleCFMapStats(std::map<std::string, double>* cf_stats) {
  DumpCFMapStats(cf_stats);
  return true;
}

522
bool InternalStats::HandleCFStats(std::string* value, Slice suffix) {
523 524 525 526
  DumpCFStats(value);
  return true;
}

527
bool InternalStats::HandleCFStatsNoFileHistogram(std::string* value,
528
                                                 Slice suffix) {
529 530 531 532
  DumpCFStatsNoFileHistogram(value);
  return true;
}

533
bool InternalStats::HandleCFFileHistogram(std::string* value, Slice suffix) {
534 535 536 537
  DumpCFFileHistogram(value);
  return true;
}

538
bool InternalStats::HandleDBStats(std::string* value, Slice suffix) {
539 540 541 542
  DumpDBStats(value);
  return true;
}

543
bool InternalStats::HandleSsTables(std::string* value, Slice suffix) {
S
sdong 已提交
544
  auto* current = cfd_->current();
545
  *value = current->DebugString(true, true);
546 547
  return true;
}
I
Igor Canadi 已提交
548

549
bool InternalStats::HandleAggregatedTableProperties(std::string* value,
550
                                                    Slice suffix) {
551 552 553 554
  std::shared_ptr<const TableProperties> tp;
  auto s = cfd_->current()->GetAggregatedTableProperties(&tp);
  if (!s.ok()) {
    return false;
555
  }
556 557
  *value = tp->ToString();
  return true;
558 559
}

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
bool InternalStats::HandleAggregatedTablePropertiesAtLevel(std::string* value,
                                                           Slice suffix) {
  uint64_t level;
  bool ok = ConsumeDecimalNumber(&suffix, &level) && suffix.empty();
  if (!ok || static_cast<int>(level) >= number_levels_) {
    return false;
  }
  std::shared_ptr<const TableProperties> tp;
  auto s = cfd_->current()->GetAggregatedTableProperties(
      &tp, static_cast<int>(level));
  if (!s.ok()) {
    return false;
  }
  *value = tp->ToString();
  return true;
}

577 578
bool InternalStats::HandleNumImmutableMemTable(uint64_t* value, DBImpl* db,
                                               Version* version) {
579 580 581 582 583
  *value = cfd_->imm()->NumNotFlushed();
  return true;
}

bool InternalStats::HandleNumImmutableMemTableFlushed(uint64_t* value,
584 585
                                                      DBImpl* db,
                                                      Version* version) {
586 587 588 589
  *value = cfd_->imm()->NumFlushed();
  return true;
}

590 591
bool InternalStats::HandleMemTableFlushPending(uint64_t* value, DBImpl* db,
                                               Version* version) {
592 593 594 595 596 597
  // Return number of mem tables that are ready to flush (made immutable)
  *value = (cfd_->imm()->IsFlushPending() ? 1 : 0);
  return true;
}

bool InternalStats::HandleNumRunningFlushes(uint64_t* value, DBImpl* db,
598
                                            Version* version) {
599 600 601 602
  *value = db->num_running_flushes();
  return true;
}

603 604
bool InternalStats::HandleCompactionPending(uint64_t* value, DBImpl* db,
                                            Version* version) {
605 606
  // 1 if the system already determines at least one compaction is needed.
  // 0 otherwise,
S
sdong 已提交
607
  const auto* vstorage = cfd_->current()->storage_info();
608 609 610
  *value = (cfd_->compaction_picker()->NeedsCompaction(vstorage) ? 1 : 0);
  return true;
}
611

612
bool InternalStats::HandleNumRunningCompactions(uint64_t* value, DBImpl* db,
613
                                                Version* version) {
614 615 616 617
  *value = db->num_running_compactions_;
  return true;
}

618 619
bool InternalStats::HandleBackgroundErrors(uint64_t* value, DBImpl* db,
                                           Version* version) {
620 621 622 623 624
  // Accumulated number of  errors in background flushes or compactions.
  *value = GetBackgroundErrorCount();
  return true;
}

625 626
bool InternalStats::HandleCurSizeActiveMemTable(uint64_t* value, DBImpl* db,
                                                Version* version) {
627 628 629 630 631
  // Current size of the active memtable
  *value = cfd_->mem()->ApproximateMemoryUsage();
  return true;
}

632 633
bool InternalStats::HandleCurSizeAllMemTables(uint64_t* value, DBImpl* db,
                                              Version* version) {
634 635 636 637 638 639
  // Current size of the active memtable + immutable memtables
  *value = cfd_->mem()->ApproximateMemoryUsage() +
           cfd_->imm()->ApproximateUnflushedMemTablesMemoryUsage();
  return true;
}

640 641
bool InternalStats::HandleSizeAllMemTables(uint64_t* value, DBImpl* db,
                                           Version* version) {
642 643 644 645 646
  *value = cfd_->mem()->ApproximateMemoryUsage() +
           cfd_->imm()->ApproximateMemoryUsage();
  return true;
}

647 648
bool InternalStats::HandleNumEntriesActiveMemTable(uint64_t* value, DBImpl* db,
                                                   Version* version) {
649 650 651 652 653
  // Current number of entires in the active memtable
  *value = cfd_->mem()->num_entries();
  return true;
}

654 655
bool InternalStats::HandleNumEntriesImmMemTables(uint64_t* value, DBImpl* db,
                                                 Version* version) {
656 657 658 659 660
  // Current number of entries in the immutable memtables
  *value = cfd_->imm()->current()->GetTotalNumEntries();
  return true;
}

661 662
bool InternalStats::HandleNumDeletesActiveMemTable(uint64_t* value, DBImpl* db,
                                                   Version* version) {
663 664 665 666 667
  // Current number of entires in the active memtable
  *value = cfd_->mem()->num_deletes();
  return true;
}

668 669
bool InternalStats::HandleNumDeletesImmMemTables(uint64_t* value, DBImpl* db,
                                                 Version* version) {
670 671 672 673 674
  // Current number of entries in the immutable memtables
  *value = cfd_->imm()->current()->GetTotalNumDeletes();
  return true;
}

675 676
bool InternalStats::HandleEstimateNumKeys(uint64_t* value, DBImpl* db,
                                          Version* version) {
677 678 679
  // Estimate number of entries in the column family:
  // Use estimated entries in tables + total entries in memtables.
  const auto* vstorage = cfd_->current()->storage_info();
680 681 682 683 684 685 686 687
  uint64_t estimate_keys = cfd_->mem()->num_entries() +
                           cfd_->imm()->current()->GetTotalNumEntries() +
                           vstorage->GetEstimatedActiveKeys();
  uint64_t estimate_deletes =
      cfd_->mem()->num_deletes() + cfd_->imm()->current()->GetTotalNumDeletes();
  *value = estimate_keys > estimate_deletes * 2
               ? estimate_keys - (estimate_deletes * 2)
               : 0;
688 689 690 691
  return true;
}

bool InternalStats::HandleNumSnapshots(uint64_t* value, DBImpl* db,
692
                                       Version* version) {
693 694 695 696 697
  *value = db->snapshots().count();
  return true;
}

bool InternalStats::HandleOldestSnapshotTime(uint64_t* value, DBImpl* db,
698
                                             Version* version) {
699 700 701 702
  *value = static_cast<uint64_t>(db->snapshots().GetOldestSnapshotTime());
  return true;
}

703 704
bool InternalStats::HandleNumLiveVersions(uint64_t* value, DBImpl* db,
                                          Version* version) {
705 706 707 708
  *value = cfd_->GetNumLiveVersions();
  return true;
}

709 710
bool InternalStats::HandleCurrentSuperVersionNumber(uint64_t* value, DBImpl* db,
                                                    Version* version) {
711 712 713 714
  *value = cfd_->GetSuperVersionNumber();
  return true;
}

715
bool InternalStats::HandleIsFileDeletionsEnabled(uint64_t* value, DBImpl* db,
716
                                                 Version* version) {
717 718 719 720
  *value = db->IsFileDeletionsEnabled();
  return true;
}

721 722
bool InternalStats::HandleBaseLevel(uint64_t* value, DBImpl* db,
                                    Version* version) {
723 724 725 726 727
  const auto* vstorage = cfd_->current()->storage_info();
  *value = vstorage->base_level();
  return true;
}

728 729
bool InternalStats::HandleTotalSstFilesSize(uint64_t* value, DBImpl* db,
                                            Version* version) {
730 731 732 733 734
  *value = cfd_->GetTotalSstFilesSize();
  return true;
}

bool InternalStats::HandleEstimatePendingCompactionBytes(uint64_t* value,
735 736
                                                         DBImpl* db,
                                                         Version* version) {
737 738 739 740 741
  const auto* vstorage = cfd_->current()->storage_info();
  *value = vstorage->estimated_compaction_needed_bytes();
  return true;
}

742
bool InternalStats::HandleEstimateTableReadersMem(uint64_t* value, DBImpl* db,
743 744 745 746 747
                                                  Version* version) {
  *value = (version == nullptr) ? 0 : version->GetMemoryUsageByTableReaders();
  return true;
}

748 749
bool InternalStats::HandleEstimateLiveDataSize(uint64_t* value, DBImpl* db,
                                               Version* version) {
750 751 752
  const auto* vstorage = cfd_->current()->storage_info();
  *value = vstorage->EstimateLiveDataSize();
  return true;
I
Igor Canadi 已提交
753 754
}

755
bool InternalStats::HandleMinLogNumberToKeep(uint64_t* value, DBImpl* db,
756
                                             Version* version) {
757 758 759 760
  *value = db->MinLogNumberToKeep();
  return true;
}

761
bool InternalStats::HandleActualDelayedWriteRate(uint64_t* value, DBImpl* db,
762
                                                 Version* version) {
763 764 765 766 767 768 769 770 771 772
  const WriteController& wc = db->write_controller();
  if (!wc.NeedsDelay()) {
    *value = 0;
  } else {
    *value = wc.delayed_write_rate();
  }
  return true;
}

bool InternalStats::HandleIsWriteStopped(uint64_t* value, DBImpl* db,
773
                                         Version* version) {
774 775 776 777
  *value = db->write_controller().IsStopped() ? 1 : 0;
  return true;
}

778 779 780
void InternalStats::DumpDBStats(std::string* value) {
  char buf[1000];
  // DB-level stats, only available from default column family
781
  double seconds_up = (env_->NowMicros() - started_at_ + 1) / kMicrosInSec;
782 783 784 785 786 787
  double interval_seconds_up = seconds_up - db_stats_snapshot_.seconds_up;
  snprintf(buf, sizeof(buf),
           "\n** DB Stats **\nUptime(secs): %.1f total, %.1f interval\n",
           seconds_up, interval_seconds_up);
  value->append(buf);
  // Cumulative
788 789 790 791 792 793 794 795
  uint64_t user_bytes_written = GetDBStats(InternalStats::BYTES_WRITTEN);
  uint64_t num_keys_written = GetDBStats(InternalStats::NUMBER_KEYS_WRITTEN);
  uint64_t write_other = GetDBStats(InternalStats::WRITE_DONE_BY_OTHER);
  uint64_t write_self = GetDBStats(InternalStats::WRITE_DONE_BY_SELF);
  uint64_t wal_bytes = GetDBStats(InternalStats::WAL_FILE_BYTES);
  uint64_t wal_synced = GetDBStats(InternalStats::WAL_FILE_SYNCED);
  uint64_t write_with_wal = GetDBStats(InternalStats::WRITE_WITH_WAL);
  uint64_t write_stall_micros = GetDBStats(InternalStats::WRITE_STALL_MICROS);
796

797 798 799
  const int kHumanMicrosLen = 32;
  char human_micros[kHumanMicrosLen];

800
  // Data
S
sdong 已提交
801 802
  // writes: total number of write requests.
  // keys: total number of key updates issued by all the write requests
803 804
  // commit groups: number of group commits issued to the DB. Each group can
  //                contain one or more writes.
S
sdong 已提交
805
  // so writes/keys is the average number of put in multi-put or put
806
  // writes/groups is the average group commit size.
S
sdong 已提交
807 808
  //
  // The format is the same for interval stats.
809
  snprintf(buf, sizeof(buf),
810 811
           "Cumulative writes: %s writes, %s keys, %s commit groups, "
           "%.1f writes per commit group, ingest: %.2f GB, %.2f MB/s\n",
812 813 814
           NumberToHumanString(write_other + write_self).c_str(),
           NumberToHumanString(num_keys_written).c_str(),
           NumberToHumanString(write_self).c_str(),
815
           (write_other + write_self) / static_cast<double>(write_self + 1),
816
           user_bytes_written / kGB, user_bytes_written / kMB / seconds_up);
817 818 819
  value->append(buf);
  // WAL
  snprintf(buf, sizeof(buf),
820 821 822 823
           "Cumulative WAL: %s writes, %s syncs, "
           "%.2f writes per sync, written: %.2f GB, %.2f MB/s\n",
           NumberToHumanString(write_with_wal).c_str(),
           NumberToHumanString(wal_synced).c_str(),
824
           write_with_wal / static_cast<double>(wal_synced + 1),
825 826 827 828 829 830 831 832 833
           wal_bytes / kGB, wal_bytes / kMB / seconds_up);
  value->append(buf);
  // Stall
  AppendHumanMicros(write_stall_micros, human_micros, kHumanMicrosLen, true);
  snprintf(buf, sizeof(buf),
           "Cumulative stall: %s, %.1f percent\n",
           human_micros,
           // 10000 = divide by 1M to get secs, then multiply by 100 for pct
           write_stall_micros / 10000.0 / std::max(seconds_up, 0.001));
834 835 836 837 838
  value->append(buf);

  // Interval
  uint64_t interval_write_other = write_other - db_stats_snapshot_.write_other;
  uint64_t interval_write_self = write_self - db_stats_snapshot_.write_self;
S
sdong 已提交
839 840
  uint64_t interval_num_keys_written =
      num_keys_written - db_stats_snapshot_.num_keys_written;
841
  snprintf(buf, sizeof(buf),
842 843
           "Interval writes: %s writes, %s keys, %s commit groups, "
           "%.1f writes per commit group, ingest: %.2f MB, %.2f MB/s\n",
844 845 846 847
           NumberToHumanString(
               interval_write_other + interval_write_self).c_str(),
           NumberToHumanString(interval_num_keys_written).c_str(),
           NumberToHumanString(interval_write_self).c_str(),
848 849
           static_cast<double>(interval_write_other + interval_write_self) /
               (interval_write_self + 1),
S
sdong 已提交
850
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB,
851 852
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB /
               std::max(interval_seconds_up, 0.001)),
853 854 855 856 857 858 859 860
  value->append(buf);

  uint64_t interval_write_with_wal =
      write_with_wal - db_stats_snapshot_.write_with_wal;
  uint64_t interval_wal_synced = wal_synced - db_stats_snapshot_.wal_synced;
  uint64_t interval_wal_bytes = wal_bytes - db_stats_snapshot_.wal_bytes;

  snprintf(buf, sizeof(buf),
861 862 863 864
           "Interval WAL: %s writes, %s syncs, "
           "%.2f writes per sync, written: %.2f MB, %.2f MB/s\n",
           NumberToHumanString(interval_write_with_wal).c_str(),
           NumberToHumanString(interval_wal_synced).c_str(),
865 866
           interval_write_with_wal /
              static_cast<double>(interval_wal_synced + 1),
867 868 869 870 871 872 873 874 875 876 877 878 879 880
           interval_wal_bytes / kGB,
           interval_wal_bytes / kMB / std::max(interval_seconds_up, 0.001));
  value->append(buf);

  // Stall
  AppendHumanMicros(
      write_stall_micros - db_stats_snapshot_.write_stall_micros,
      human_micros, kHumanMicrosLen, true);
  snprintf(buf, sizeof(buf),
           "Interval stall: %s, %.1f percent\n",
           human_micros,
           // 10000 = divide by 1M to get secs, then multiply by 100 for pct
           (write_stall_micros - db_stats_snapshot_.write_stall_micros) /
               10000.0 / std::max(interval_seconds_up, 0.001));
881 882 883 884 885 886
  value->append(buf);

  db_stats_snapshot_.seconds_up = seconds_up;
  db_stats_snapshot_.ingest_bytes = user_bytes_written;
  db_stats_snapshot_.write_other = write_other;
  db_stats_snapshot_.write_self = write_self;
S
sdong 已提交
887
  db_stats_snapshot_.num_keys_written = num_keys_written;
888 889 890
  db_stats_snapshot_.wal_bytes = wal_bytes;
  db_stats_snapshot_.wal_synced = wal_synced;
  db_stats_snapshot_.write_with_wal = write_with_wal;
S
sdong 已提交
891
  db_stats_snapshot_.write_stall_micros = write_stall_micros;
892 893
}

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
/**
 * Dump Compaction Level stats to a map of stat name to value in double.
 * The level in stat name is represented with a prefix "Lx" where "x"
 * is the level number. A special level "Sum" represents the sum of a stat
 * for all levels.
 */
void InternalStats::DumpCFMapStats(std::map<std::string, double>* cf_stats) {
  CompactionStats compaction_stats_sum(0);
  std::map<int, std::map<LevelStatType, double>> levels_stats;
  DumpCFMapStats(&levels_stats, &compaction_stats_sum);
  for (auto const& level_ent : levels_stats) {
    auto level_str =
        level_ent.first == -1 ? "Sum" : "L" + ToString(level_ent.first);
    for (auto const& stat_ent : level_ent.second) {
      auto stat_type = stat_ent.first;
      auto key_str =
          level_str + "." +
          InternalStats::compaction_level_stats.at(stat_type).property_name;
      (*cf_stats)[key_str] = stat_ent.second;
    }
  }
}

917
void InternalStats::DumpCFMapStats(
918 919
    std::map<int, std::map<LevelStatType, double>>* levels_stats,
    CompactionStats* compaction_stats_sum) {
S
sdong 已提交
920
  const VersionStorageInfo* vstorage = cfd_->current()->storage_info();
921 922

  int num_levels_to_check =
923
      (cfd_->ioptions()->compaction_style != kCompactionStyleFIFO)
924
          ? vstorage->num_levels() - 1
925
          : 1;
S
sdong 已提交
926

927
  // Compaction scores are sorted based on its value. Restore them to the
928 929 930
  // level order
  std::vector<double> compaction_score(number_levels_, 0);
  for (int i = 0; i < num_levels_to_check; ++i) {
931 932
    compaction_score[vstorage->CompactionScoreLevel(i)] =
        vstorage->CompactionScore(i);
933 934 935
  }
  // Count # of files being compacted for each level
  std::vector<int> files_being_compacted(number_levels_, 0);
936
  for (int level = 0; level < number_levels_; ++level) {
937
    for (auto* f : vstorage->LevelFiles(level)) {
938 939 940 941 942 943 944 945 946
      if (f->being_compacted) {
        ++files_being_compacted[level];
      }
    }
  }

  int total_files = 0;
  int total_files_being_compacted = 0;
  double total_file_size = 0;
947 948 949
  uint64_t flush_ingest = cf_stats_value_[BYTES_FLUSHED];
  uint64_t add_file_ingest = cf_stats_value_[BYTES_INGESTED_ADD_FILE];
  uint64_t curr_ingest = flush_ingest + add_file_ingest;
950
  for (int level = 0; level < number_levels_; level++) {
S
sdong 已提交
951
    int files = vstorage->NumLevelFiles(level);
952 953 954
    total_files += files;
    total_files_being_compacted += files_being_compacted[level];
    if (comp_stats_[level].micros > 0 || files > 0) {
955
      compaction_stats_sum->Add(comp_stats_[level]);
S
sdong 已提交
956
      total_file_size += vstorage->NumLevelBytes(level);
957 958 959 960 961 962
      uint64_t input_bytes;
      if (level == 0) {
        input_bytes = curr_ingest;
      } else {
        input_bytes = comp_stats_[level].bytes_read_non_output_levels;
      }
963
      double w_amp =
964
          (input_bytes == 0)
965 966
              ? 0.0
              : static_cast<double>(comp_stats_[level].bytes_written) /
967
                    input_bytes;
968 969 970 971 972
      std::map<LevelStatType, double> level_stats;
      PrepareLevelStats(&level_stats, files, files_being_compacted[level],
                        static_cast<double>(vstorage->NumLevelBytes(level)),
                        compaction_score[level], w_amp, comp_stats_[level]);
      (*levels_stats)[level] = level_stats;
973 974
    }
  }
975 976 977 978 979 980 981 982 983 984 985
  // Cumulative summary
  double w_amp = compaction_stats_sum->bytes_written /
                 static_cast<double>(curr_ingest + 1);
  // Stats summary across levels
  std::map<LevelStatType, double> sum_stats;
  PrepareLevelStats(&sum_stats, total_files, total_files_being_compacted,
                    total_file_size, 0, w_amp, *compaction_stats_sum);
  (*levels_stats)[-1] = sum_stats;  //  -1 is for the Sum level
}

void InternalStats::DumpCFStats(std::string* value) {
986 987 988 989 990
  DumpCFStatsNoFileHistogram(value);
  DumpCFFileHistogram(value);
}

void InternalStats::DumpCFStatsNoFileHistogram(std::string* value) {
J
Jonathan Lee 已提交
991
  char buf[2000];
992 993 994 995 996 997 998
  // Per-ColumnFamily stats
  PrintLevelStatsHeader(buf, sizeof(buf), cfd_->GetName());
  value->append(buf);

  // Print stats for each level
  std::map<int, std::map<LevelStatType, double>> levels_stats;
  CompactionStats compaction_stats_sum(0);
999 1000
  DumpCFMapStats(&levels_stats, &compaction_stats_sum);
  for (int l = 0; l < number_levels_; ++l) {
1001 1002 1003 1004 1005
    if (levels_stats.find(l) != levels_stats.end()) {
      PrintLevelStats(buf, sizeof(buf), "L" + ToString(l), levels_stats[l]);
      value->append(buf);
    }
  }
1006

1007 1008 1009 1010 1011 1012
  // Print sum of level stats
  PrintLevelStats(buf, sizeof(buf), "Sum", levels_stats[-1]);
  value->append(buf);

  uint64_t flush_ingest = cf_stats_value_[BYTES_FLUSHED];
  uint64_t add_file_ingest = cf_stats_value_[BYTES_INGESTED_ADD_FILE];
1013 1014 1015 1016
  uint64_t ingest_files_addfile = cf_stats_value_[INGESTED_NUM_FILES_TOTAL];
  uint64_t ingest_l0_files_addfile =
      cf_stats_value_[INGESTED_LEVEL0_NUM_FILES_TOTAL];
  uint64_t ingest_keys_addfile = cf_stats_value_[INGESTED_NUM_KEYS_TOTAL];
1017
  // Cumulative summary
1018 1019 1020 1021 1022
  uint64_t total_stall_count =
      cf_stats_count_[LEVEL0_SLOWDOWN_TOTAL] +
      cf_stats_count_[LEVEL0_NUM_FILES_TOTAL] +
      cf_stats_count_[SOFT_PENDING_COMPACTION_BYTES_LIMIT] +
      cf_stats_count_[HARD_PENDING_COMPACTION_BYTES_LIMIT] +
1023
      cf_stats_count_[MEMTABLE_COMPACTION] + cf_stats_count_[MEMTABLE_SLOWDOWN];
1024
  // Interval summary
1025 1026 1027
  uint64_t interval_flush_ingest =
      flush_ingest - cf_stats_snapshot_.ingest_bytes_flush;
  uint64_t interval_add_file_inget =
1028
      add_file_ingest - cf_stats_snapshot_.ingest_bytes_addfile;
1029
  uint64_t interval_ingest =
1030
      interval_flush_ingest + interval_add_file_inget + 1;
1031
  CompactionStats interval_stats(compaction_stats_sum);
1032
  interval_stats.Subtract(cf_stats_snapshot_.comp_stats);
1033 1034
  double w_amp =
      interval_stats.bytes_written / static_cast<double>(interval_ingest);
1035
  PrintLevelStats(buf, sizeof(buf), "Int", 0, 0, 0, 0, w_amp, interval_stats);
1036 1037
  value->append(buf);

1038 1039 1040 1041 1042
  double seconds_up = (env_->NowMicros() - started_at_ + 1) / kMicrosInSec;
  double interval_seconds_up = seconds_up - cf_stats_snapshot_.seconds_up;
  snprintf(buf, sizeof(buf), "Uptime(secs): %.1f total, %.1f interval\n",
           seconds_up, interval_seconds_up);
  value->append(buf);
1043 1044
  snprintf(buf, sizeof(buf), "Flush(GB): cumulative %.3f, interval %.3f\n",
           flush_ingest / kGB, interval_flush_ingest / kGB);
1045
  value->append(buf);
1046 1047
  snprintf(buf, sizeof(buf), "AddFile(GB): cumulative %.3f, interval %.3f\n",
           add_file_ingest / kGB, interval_add_file_inget / kGB);
1048 1049
  value->append(buf);

1050 1051
  uint64_t interval_ingest_files_addfile =
      ingest_files_addfile - cf_stats_snapshot_.ingest_files_addfile;
I
Islam AbdelRahman 已提交
1052 1053
  snprintf(buf, sizeof(buf), "AddFile(Total Files): cumulative %" PRIu64
                             ", interval %" PRIu64 "\n",
1054 1055 1056 1057 1058 1059
           ingest_files_addfile, interval_ingest_files_addfile);
  value->append(buf);

  uint64_t interval_ingest_l0_files_addfile =
      ingest_l0_files_addfile - cf_stats_snapshot_.ingest_l0_files_addfile;
  snprintf(buf, sizeof(buf),
I
Islam AbdelRahman 已提交
1060
           "AddFile(L0 Files): cumulative %" PRIu64 ", interval %" PRIu64 "\n",
1061 1062 1063 1064 1065
           ingest_l0_files_addfile, interval_ingest_l0_files_addfile);
  value->append(buf);

  uint64_t interval_ingest_keys_addfile =
      ingest_keys_addfile - cf_stats_snapshot_.ingest_keys_addfile;
I
Islam AbdelRahman 已提交
1066 1067
  snprintf(buf, sizeof(buf),
           "AddFile(Keys): cumulative %" PRIu64 ", interval %" PRIu64 "\n",
1068 1069 1070
           ingest_keys_addfile, interval_ingest_keys_addfile);
  value->append(buf);

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  // Compact
  uint64_t compact_bytes_read = 0;
  uint64_t compact_bytes_write = 0;
  uint64_t compact_micros = 0;
  for (int level = 0; level < number_levels_; level++) {
    compact_bytes_read += comp_stats_[level].bytes_read_output_level +
                          comp_stats_[level].bytes_read_non_output_levels;
    compact_bytes_write += comp_stats_[level].bytes_written;
    compact_micros += comp_stats_[level].micros;
  }

  snprintf(buf, sizeof(buf),
           "Cumulative compaction: %.2f GB write, %.2f MB/s write, "
           "%.2f GB read, %.2f MB/s read, %.1f seconds\n",
           compact_bytes_write / kGB, compact_bytes_write / kMB / seconds_up,
           compact_bytes_read / kGB, compact_bytes_read / kMB / seconds_up,
           compact_micros / kMicrosInSec);
  value->append(buf);

  // Compaction interval
  uint64_t interval_compact_bytes_write =
      compact_bytes_write - cf_stats_snapshot_.compact_bytes_write;
  uint64_t interval_compact_bytes_read =
      compact_bytes_read - cf_stats_snapshot_.compact_bytes_read;
  uint64_t interval_compact_micros =
      compact_micros - cf_stats_snapshot_.compact_micros;

  snprintf(
      buf, sizeof(buf),
      "Interval compaction: %.2f GB write, %.2f MB/s write, "
      "%.2f GB read, %.2f MB/s read, %.1f seconds\n",
      interval_compact_bytes_write / kGB,
      interval_compact_bytes_write / kMB / std::max(interval_seconds_up, 0.001),
      interval_compact_bytes_read / kGB,
      interval_compact_bytes_read / kMB / std::max(interval_seconds_up, 0.001),
      interval_compact_micros / kMicrosInSec);
  value->append(buf);
  cf_stats_snapshot_.compact_bytes_write = compact_bytes_write;
  cf_stats_snapshot_.compact_bytes_read = compact_bytes_read;
  cf_stats_snapshot_.compact_micros = compact_micros;

1112 1113 1114 1115 1116 1117 1118 1119 1120
  snprintf(buf, sizeof(buf), "Stalls(count): %" PRIu64
                             " level0_slowdown, "
                             "%" PRIu64
                             " level0_slowdown_with_compaction, "
                             "%" PRIu64
                             " level0_numfiles, "
                             "%" PRIu64
                             " level0_numfiles_with_compaction, "
                             "%" PRIu64
1121
                             " stop for pending_compaction_bytes, "
1122
                             "%" PRIu64
1123
                             " slowdown for pending_compaction_bytes, "
1124
                             "%" PRIu64
1125
                             " memtable_compaction, "
1126 1127
                             "%" PRIu64
                             " memtable_slowdown, "
1128
                             "interval %" PRIu64 " total count\n",
1129 1130 1131 1132
           cf_stats_count_[LEVEL0_SLOWDOWN_TOTAL],
           cf_stats_count_[LEVEL0_SLOWDOWN_WITH_COMPACTION],
           cf_stats_count_[LEVEL0_NUM_FILES_TOTAL],
           cf_stats_count_[LEVEL0_NUM_FILES_WITH_COMPACTION],
1133
           cf_stats_count_[HARD_PENDING_COMPACTION_BYTES_LIMIT],
1134 1135
           cf_stats_count_[SOFT_PENDING_COMPACTION_BYTES_LIMIT],
           cf_stats_count_[MEMTABLE_COMPACTION],
1136
           cf_stats_count_[MEMTABLE_SLOWDOWN],
1137
           total_stall_count - cf_stats_snapshot_.stall_count);
1138 1139
  value->append(buf);

1140
  cf_stats_snapshot_.seconds_up = seconds_up;
1141
  cf_stats_snapshot_.ingest_bytes_flush = flush_ingest;
1142 1143 1144 1145
  cf_stats_snapshot_.ingest_bytes_addfile = add_file_ingest;
  cf_stats_snapshot_.ingest_files_addfile = ingest_files_addfile;
  cf_stats_snapshot_.ingest_l0_files_addfile = ingest_l0_files_addfile;
  cf_stats_snapshot_.ingest_keys_addfile = ingest_keys_addfile;
1146
  cf_stats_snapshot_.comp_stats = compaction_stats_sum;
1147 1148 1149
  cf_stats_snapshot_.stall_count = total_stall_count;
}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
void InternalStats::DumpCFFileHistogram(std::string* value) {
  char buf[2000];
  snprintf(buf, sizeof(buf),
           "\n** File Read Latency Histogram By Level [%s] **\n",
           cfd_->GetName().c_str());
  value->append(buf);

  for (int level = 0; level < number_levels_; level++) {
    if (!file_read_latency_[level].Empty()) {
      char buf2[5000];
      snprintf(buf2, sizeof(buf2),
               "** Level %d read latency histogram (micros):\n%s\n", level,
               file_read_latency_[level].ToString().c_str());
      value->append(buf2);
    }
  }
}
1167 1168 1169

#else

1170
const DBPropertyInfo* GetPropertyInfo(const Slice& property) { return nullptr; }
1171 1172 1173

#endif  // !ROCKSDB_LITE

I
Igor Canadi 已提交
1174
}  // namespace rocksdb