internal_stats.cc 26.5 KB
Newer Older
I
Igor Canadi 已提交
1 2 3 4 5 6 7 8 9
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
// 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 <algorithm>
I
Igor Canadi 已提交
17
#include <vector>
18
#include "db/column_family.h"
19

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

namespace rocksdb {

25
#ifndef ROCKSDB_LITE
L
Lei Jin 已提交
26 27 28 29
namespace {
const double kMB = 1048576.0;
const double kGB = kMB * 1024;

30
void PrintLevelStatsHeader(char* buf, size_t len, const std::string& cf_name) {
L
Lei Jin 已提交
31 32
  snprintf(
      buf, len,
33
      "\n** Compaction Stats [%s] **\n"
34
      "Level    Files   Size(MB) Score Read(GB)  Rn(GB) Rnp1(GB) "
35 36
      "Write(GB) Wnew(GB) Moved(GB) W-Amp Rd(MB/s) Wr(MB/s) "
      "Comp(sec) Comp(cnt) Avg(sec) "
37
      "Stall(cnt)  KeyIn KeyDrop\n"
L
Lei Jin 已提交
38
      "--------------------------------------------------------------------"
39
      "-----------------------------------------------------------"
40
      "--------------------------------------\n",
41
      cf_name.c_str());
L
Lei Jin 已提交
42 43 44
}

void PrintLevelStats(char* buf, size_t len, const std::string& name,
45
    int num_files, int being_compacted, double total_file_size, double score,
46
    double w_amp, uint64_t stalls,
L
Lei Jin 已提交
47
    const InternalStats::CompactionStats& stats) {
48 49 50 51
  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;
L
Lei Jin 已提交
52
  double elapsed = (stats.micros + 1) / 1000000.0;
53 54 55
  std::string num_input_records = NumberToHumanString(stats.num_input_records);
  std::string num_dropped_records =
      NumberToHumanString(stats.num_dropped_records);
L
Lei Jin 已提交
56 57

  snprintf(buf, len,
58
           "%4s %6d/%-3d %8.0f %5.1f " /* Level, Files, Size(MB), Score */
S
sdong 已提交
59 60 61 62 63
           "%8.1f "                    /* Read(GB) */
           "%7.1f "                    /* Rn(GB) */
           "%8.1f "                    /* Rnp1(GB) */
           "%9.1f "                    /* Write(GB) */
           "%8.1f "                    /* Wnew(GB) */
64
           "%9.1f "                    /* Moved(GB) */
S
sdong 已提交
65 66 67
           "%5.1f "                    /* W-Amp */
           "%8.1f "                    /* Rd(MB/s) */
           "%8.1f "                    /* Wr(MB/s) */
68
           "%9.0f "                    /* Comp(sec) */
S
sdong 已提交
69 70 71 72
           "%9d "                      /* Comp(cnt) */
           "%8.3f "                    /* Avg(sec) */
           "%10" PRIu64
           " "      /* Stall(cnt) */
73 74
           "%7s "   /* KeyIn */
           "%6s\n", /* KeyDrop */
S
sdong 已提交
75
           name.c_str(), num_files, being_compacted, total_file_size / kMB,
76 77
           score, bytes_read / kGB, stats.bytes_read_non_output_levels / kGB,
           stats.bytes_read_output_level / kGB, stats.bytes_written / kGB,
78 79
           bytes_new / kGB, stats.bytes_moved / kGB,
           w_amp, bytes_read / kMB / elapsed,
80 81
           stats.bytes_written / kMB / elapsed, stats.micros / 1000000.0,
           stats.count,
S
sdong 已提交
82
           stats.count == 0 ? 0 : stats.micros / 1000000.0 / stats.count,
83
           stalls,
84
           num_input_records.c_str(), num_dropped_records.c_str());
L
Lei Jin 已提交
85 86 87
}
}

88 89 90
static const std::string rocksdb_prefix = "rocksdb.";

static const std::string num_files_at_level_prefix = "num-files-at-level";
91
static const std::string allstats = "stats";
92 93 94 95 96
static const std::string sstables = "sstables";
static const std::string cfstats = "cfstats";
static const std::string dbstats = "dbstats";
static const std::string levelstats = "levelstats";
static const std::string num_immutable_mem_table = "num-immutable-mem-table";
97 98
static const std::string num_immutable_mem_table_flushed =
    "num-immutable-mem-table-flushed";
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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";
static const std::string cur_size_all_mem_tables = "cur-size-all-mem-tables";
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";
static const std::string base_level = "base-level";

const std::string DB::Properties::kNumFilesAtLevelPrefix =
                      rocksdb_prefix + num_files_at_level_prefix;
125
const std::string DB::Properties::kStats = rocksdb_prefix + allstats;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
const std::string DB::Properties::kSSTables = rocksdb_prefix + sstables;
const std::string DB::Properties::kCFStats = rocksdb_prefix + cfstats;
const std::string DB::Properties::kDBStats = rocksdb_prefix + dbstats;
const std::string DB::Properties::kNumImmutableMemTable =
                      rocksdb_prefix + num_immutable_mem_table;
const std::string DB::Properties::kMemTableFlushPending =
                      rocksdb_prefix + mem_table_flush_pending;
const std::string DB::Properties::kCompactionPending =
                      rocksdb_prefix + compaction_pending;
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 =
                      rocksdb_prefix + cur_size_all_mem_tables;
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;

162 163 164 165
DBPropertyType GetPropertyType(const Slice& property, bool* is_int_property,
                               bool* need_out_of_mutex) {
  assert(is_int_property != nullptr);
  assert(need_out_of_mutex != nullptr);
I
Igor Canadi 已提交
166
  Slice in = property;
167
  Slice prefix(rocksdb_prefix);
168
  *need_out_of_mutex = false;
169
  *is_int_property = false;
S
sdong 已提交
170 171 172
  if (!in.starts_with(prefix)) {
    return kUnknown;
  }
I
Igor Canadi 已提交
173 174
  in.remove_prefix(prefix.size());

175
  if (in.starts_with(num_files_at_level_prefix)) {
176
    return kNumFilesAtLevel;
177
  } else if (in == levelstats) {
178
    return kLevelStats;
179
  } else if (in == allstats) {
180
    return kStats;
181
  } else if (in == cfstats) {
182
    return kCFStats;
183
  } else if (in == dbstats) {
184
    return kDBStats;
185
  } else if (in == sstables) {
186
    return kSsTables;
187 188 189
  }

  *is_int_property = true;
190
  if (in == num_immutable_mem_table) {
191
    return kNumImmutableMemTable;
192 193
  } else if (in == num_immutable_mem_table_flushed) {
    return kNumImmutableMemTableFlushed;
194
  } else if (in == mem_table_flush_pending) {
195
    return kMemtableFlushPending;
196
  } else if (in == compaction_pending) {
197
    return kCompactionPending;
198
  } else if (in == background_errors) {
199
    return kBackgroundErrors;
200
  } else if (in == cur_size_active_mem_table) {
201
    return kCurSizeActiveMemTable;
202
  } else if (in == cur_size_all_mem_tables) {
I
Igor Canadi 已提交
203
    return kCurSizeAllMemTables;
204
  } else if (in == num_entries_active_mem_table) {
205
    return kNumEntriesInMutableMemtable;
206
  } else if (in == num_entries_imm_mem_tables) {
207
    return kNumEntriesInImmutableMemtable;
208
  } else if (in == num_deletes_active_mem_table) {
209
    return kNumDeletesInMutableMemtable;
210
  } else if (in == num_deletes_imm_mem_tables) {
211
    return kNumDeletesInImmutableMemtable;
212
  } else if (in == estimate_num_keys) {
S
sdong 已提交
213
    return kEstimatedNumKeys;
214
  } else if (in == estimate_table_readers_mem) {
215 216
    *need_out_of_mutex = true;
    return kEstimatedUsageByTableReaders;
217
  } else if (in == is_file_deletions_enabled) {
218
    return kIsFileDeletionEnabled;
219
  } else if (in == num_snapshots) {
220
    return kNumSnapshots;
221
  } else if (in == oldest_snapshot_time) {
222
    return kOldestSnapshotTime;
223
  } else if (in == num_live_versions) {
224
    return kNumLiveVersions;
225
  } else if (in == base_level) {
226
    return kBaseLevel;
227 228 229 230
  }
  return kUnknown;
}

231 232 233 234 235 236
bool InternalStats::GetIntPropertyOutOfMutex(DBPropertyType property_type,
                                             Version* version,
                                             uint64_t* value) const {
  assert(value != nullptr);
  if (property_type != kEstimatedUsageByTableReaders) {
    return false;
237
  }
238 239 240 241 242 243 244
  if (version == nullptr) {
    *value = 0;
  } else {
    *value = version->GetMemoryUsageByTableReaders();
  }
  return true;
}
245

246 247 248 249
bool InternalStats::GetStringProperty(DBPropertyType property_type,
                                      const Slice& property,
                                      std::string* value) {
  assert(value != nullptr);
S
sdong 已提交
250
  auto* current = cfd_->current();
S
sdong 已提交
251
  const auto* vstorage = current->storage_info();
252 253 254 255 256 257 258 259 260 261 262 263
  Slice in = property;

  switch (property_type) {
    case kNumFilesAtLevel: {
      in.remove_prefix(strlen("rocksdb.num-files-at-level"));
      uint64_t level;
      bool ok = ConsumeDecimalNumber(&in, &level) && in.empty();
      if (!ok || (int)level >= number_levels_) {
        return false;
      } else {
        char buf[100];
        snprintf(buf, sizeof(buf), "%d",
S
sdong 已提交
264
                 vstorage->NumLevelFiles(static_cast<int>(level)));
265 266 267
        *value = buf;
        return true;
      }
I
Igor Canadi 已提交
268
    }
269 270 271 272 273 274
    case kLevelStats: {
      char buf[1000];
      snprintf(buf, sizeof(buf),
               "Level Files Size(MB)\n"
               "--------------------\n");
      value->append(buf);
I
Igor Canadi 已提交
275

276 277
      for (int level = 0; level < number_levels_; level++) {
        snprintf(buf, sizeof(buf), "%3d %8d %8.0f\n", level,
S
sdong 已提交
278 279
                 vstorage->NumLevelFiles(level),
                 vstorage->NumLevelBytes(level) / kMB);
I
Igor Canadi 已提交
280 281
        value->append(buf);
      }
282
      return true;
I
Igor Canadi 已提交
283
    }
284
    case kStats: {
285
      if (!GetStringProperty(kCFStats, DB::Properties::kCFStats, value)) {
286
        return false;
287
      }
288
      if (!GetStringProperty(kDBStats, DB::Properties::kDBStats, value)) {
289
        return false;
L
Lei Jin 已提交
290
      }
291 292 293
      return true;
    }
    case kCFStats: {
294
      DumpCFStats(value);
295 296 297 298
      return true;
    }
    case kDBStats: {
      DumpDBStats(value);
299 300 301 302 303
      return true;
    }
    case kSsTables:
      *value = current->DebugString();
      return true;
304 305 306 307 308 309
    default:
      return false;
  }
}

bool InternalStats::GetIntProperty(DBPropertyType property_type,
310
                                   uint64_t* value, DBImpl* db) const {
311
  db->mutex_.AssertHeld();
S
sdong 已提交
312
  const auto* vstorage = cfd_->current()->storage_info();
313 314

  switch (property_type) {
315
    case kNumImmutableMemTable:
316 317 318 319
      *value = cfd_->imm()->NumNotFlushed();
      return true;
    case kNumImmutableMemTableFlushed:
      *value = cfd_->imm()->NumFlushed();
320
      return true;
321
    case kMemtableFlushPending:
322
      // Return number of mem tables that are ready to flush (made immutable)
323
      *value = (cfd_->imm()->IsFlushPending() ? 1 : 0);
324
      return true;
325
    case kCompactionPending:
326 327
      // 1 if the system already determines at least one compacdtion is needed.
      // 0 otherwise,
Y
Yueh-Hsuan Chiang 已提交
328
      *value = (cfd_->compaction_picker()->NeedsCompaction(vstorage) ? 1 : 0);
329
      return true;
330 331
    case kBackgroundErrors:
      // Accumulated number of  errors in background flushes or compactions.
332
      *value = GetBackgroundErrorCount();
333
      return true;
334 335
    case kCurSizeActiveMemTable:
      // Current size of the active memtable
336
      *value = cfd_->mem()->ApproximateMemoryUsage();
337
      return true;
I
Igor Canadi 已提交
338 339 340 341 342
    case kCurSizeAllMemTables:
      // Current size of the active memtable + immutable memtables
      *value = cfd_->mem()->ApproximateMemoryUsage() +
               cfd_->imm()->ApproximateMemoryUsage();
      return true;
343
    case kNumEntriesInMutableMemtable:
I
Igor Canadi 已提交
344
      // Current number of entires in the active memtable
345
      *value = cfd_->mem()->num_entries();
346 347
      return true;
    case kNumEntriesInImmutableMemtable:
I
Igor Canadi 已提交
348
      // Current number of entries in the immutable memtables
349
      *value = cfd_->imm()->current()->GetTotalNumEntries();
350
      return true;
351 352 353 354 355 356 357 358
    case kNumDeletesInMutableMemtable:
      // Current number of entires in the active memtable
      *value = cfd_->mem()->num_deletes();
      return true;
    case kNumDeletesInImmutableMemtable:
      // Current number of entries in the immutable memtables
      *value = cfd_->imm()->current()->GetTotalNumDeletes();
      return true;
S
sdong 已提交
359 360 361
    case kEstimatedNumKeys:
      // Estimate number of entries in the column family:
      // Use estimated entries in tables + total entries in memtables.
362 363 364 365 366
      *value = cfd_->mem()->num_entries() +
               cfd_->imm()->current()->GetTotalNumEntries() -
               (cfd_->mem()->num_deletes() +
                cfd_->imm()->current()->GetTotalNumDeletes()) *
                   2 +
S
sdong 已提交
367
               vstorage->GetEstimatedActiveKeys();
S
sdong 已提交
368
      return true;
369 370 371 372 373 374
    case kNumSnapshots:
      *value = db->snapshots().count();
      return true;
    case kOldestSnapshotTime:
      *value = static_cast<uint64_t>(db->snapshots().GetOldestSnapshotTime());
      return true;
375 376 377
    case kNumLiveVersions:
      *value = cfd_->GetNumLiveVersions();
      return true;
I
Igor Canadi 已提交
378
#ifndef ROCKSDB_LITE
379 380 381
    case kIsFileDeletionEnabled:
      *value = db->IsFileDeletionsEnabled();
      return true;
I
Igor Canadi 已提交
382
#endif
383 384 385
    case kBaseLevel:
      *value = vstorage->base_level();
      return true;
386 387 388
    default:
      return false;
  }
I
Igor Canadi 已提交
389 390
}

391 392 393 394 395 396 397 398 399 400 401
void InternalStats::DumpDBStats(std::string* value) {
  char buf[1000];
  // DB-level stats, only available from default column family
  double seconds_up = (env_->NowMicros() - started_at_ + 1) / 1000000.0;
  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
  uint64_t user_bytes_written = db_stats_[InternalStats::BYTES_WRITTEN];
S
sdong 已提交
402
  uint64_t num_keys_written = db_stats_[InternalStats::NUMBER_KEYS_WRITTEN];
403 404 405 406 407
  uint64_t write_other = db_stats_[InternalStats::WRITE_DONE_BY_OTHER];
  uint64_t write_self = db_stats_[InternalStats::WRITE_DONE_BY_SELF];
  uint64_t wal_bytes = db_stats_[InternalStats::WAL_FILE_BYTES];
  uint64_t wal_synced = db_stats_[InternalStats::WAL_FILE_SYNCED];
  uint64_t write_with_wal = db_stats_[InternalStats::WRITE_WITH_WAL];
S
sdong 已提交
408
  uint64_t write_stall_micros = db_stats_[InternalStats::WRITE_STALL_MICROS];
409 410 411 412
  uint64_t compact_bytes_read = 0;
  uint64_t compact_bytes_write = 0;
  uint64_t compact_micros = 0;

413 414 415
  const int kHumanMicrosLen = 32;
  char human_micros[kHumanMicrosLen];

416
  // Data
S
sdong 已提交
417 418 419 420 421 422 423 424
  // writes: total number of write requests.
  // keys: total number of key updates issued by all the write requests
  // batches: number of group commits issued to the DB. Each group can contain
  //          one or more writes.
  // so writes/keys is the average number of put in multi-put or put
  // writes/batches is the average group commit size.
  //
  // The format is the same for interval stats.
425
  snprintf(buf, sizeof(buf),
426 427 428 429 430
           "Cumulative writes: %s writes, %s keys, %s batches, "
           "%.1f writes per batch, ingest: %.2f GB, %.2f MB/s\n",
           NumberToHumanString(write_other + write_self).c_str(),
           NumberToHumanString(num_keys_written).c_str(),
           NumberToHumanString(write_self).c_str(),
431
           (write_other + write_self) / static_cast<double>(write_self + 1),
432
           user_bytes_written / kGB, user_bytes_written / kMB / seconds_up);
433 434 435
  value->append(buf);
  // WAL
  snprintf(buf, sizeof(buf),
436 437 438 439
           "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(),
440
           write_with_wal / static_cast<double>(wal_synced + 1),
441 442
           wal_bytes / kGB, wal_bytes / kMB / seconds_up);
  value->append(buf);
443 444
  // Compact
  for (int level = 0; level < number_levels_; level++) {
445 446
    compact_bytes_read += comp_stats_[level].bytes_read_output_level +
                          comp_stats_[level].bytes_read_non_output_levels;
447 448 449 450 451 452 453 454 455 456 457 458
    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 / 1000000.0);
  value->append(buf);
459 460 461 462 463 464 465
  // 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));
466 467 468 469 470
  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 已提交
471 472
  uint64_t interval_num_keys_written =
      num_keys_written - db_stats_snapshot_.num_keys_written;
473
  snprintf(buf, sizeof(buf),
474 475 476 477 478 479
           "Interval writes: %s writes, %s keys, %s batches, "
           "%.1f writes per batch, ingest: %.2f MB, %.2f MB/s\n",
           NumberToHumanString(
               interval_write_other + interval_write_self).c_str(),
           NumberToHumanString(interval_num_keys_written).c_str(),
           NumberToHumanString(interval_write_self).c_str(),
480 481
           static_cast<double>(interval_write_other + interval_write_self) /
               (interval_write_self + 1),
S
sdong 已提交
482
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB,
483 484
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB /
               std::max(interval_seconds_up, 0.001)),
485 486 487 488 489 490 491 492
  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),
493 494 495 496
           "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(),
497 498
           interval_write_with_wal /
              static_cast<double>(interval_wal_synced + 1),
499 500 501 502
           interval_wal_bytes / kGB,
           interval_wal_bytes / kMB / std::max(interval_seconds_up, 0.001));
  value->append(buf);

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  // Compaction
  uint64_t interval_compact_bytes_write =
      compact_bytes_write - db_stats_snapshot_.compact_bytes_write;
  uint64_t interval_compact_bytes_read =
      compact_bytes_read - db_stats_snapshot_.compact_bytes_read;
  uint64_t interval_compact_micros =
      compact_micros - db_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 / 1000000.0);
  value->append(buf);

523 524 525 526 527 528 529 530 531 532
  // 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));
533 534 535 536 537 538
  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 已提交
539
  db_stats_snapshot_.num_keys_written = num_keys_written;
540 541 542
  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 已提交
543
  db_stats_snapshot_.write_stall_micros = write_stall_micros;
544 545 546
  db_stats_snapshot_.compact_bytes_write = compact_bytes_write;
  db_stats_snapshot_.compact_bytes_read = compact_bytes_read;
  db_stats_snapshot_.compact_micros = compact_micros;
547 548
}

549
void InternalStats::DumpCFStats(std::string* value) {
S
sdong 已提交
550
  const VersionStorageInfo* vstorage = cfd_->current()->storage_info();
551 552

  int num_levels_to_check =
553
      (cfd_->ioptions()->compaction_style != kCompactionStyleFIFO)
554
          ? vstorage->num_levels() - 1
555
          : 1;
S
sdong 已提交
556

557 558 559 560
  // Compaction scores are sorted base on its value. Restore them to the
  // level order
  std::vector<double> compaction_score(number_levels_, 0);
  for (int i = 0; i < num_levels_to_check; ++i) {
561 562
    compaction_score[vstorage->CompactionScoreLevel(i)] =
        vstorage->CompactionScore(i);
563 564 565
  }
  // Count # of files being compacted for each level
  std::vector<int> files_being_compacted(number_levels_, 0);
566
  for (int level = 0; level < number_levels_; ++level) {
567
    for (auto* f : vstorage->LevelFiles(level)) {
568 569 570 571 572 573 574 575
      if (f->being_compacted) {
        ++files_being_compacted[level];
      }
    }
  }

  char buf[1000];
  // Per-ColumnFamily stats
576
  PrintLevelStatsHeader(buf, sizeof(buf), cfd_->GetName());
577 578 579 580 581 582 583 584 585 586
  value->append(buf);

  CompactionStats stats_sum(0);
  int total_files = 0;
  int total_files_being_compacted = 0;
  double total_file_size = 0;
  uint64_t total_slowdown_count_soft = 0;
  uint64_t total_slowdown_count_hard = 0;
  uint64_t total_stall_count = 0;
  for (int level = 0; level < number_levels_; level++) {
S
sdong 已提交
587
    int files = vstorage->NumLevelFiles(level);
588 589 590 591 592 593 594 595 596 597 598
    total_files += files;
    total_files_being_compacted += files_being_compacted[level];
    if (comp_stats_[level].micros > 0 || files > 0) {
      uint64_t stalls = level == 0 ?
        (cf_stats_count_[LEVEL0_SLOWDOWN] +
         cf_stats_count_[LEVEL0_NUM_FILES] +
         cf_stats_count_[MEMTABLE_COMPACTION])
        : (stall_leveln_slowdown_count_soft_[level] +
           stall_leveln_slowdown_count_hard_[level]);

      stats_sum.Add(comp_stats_[level]);
S
sdong 已提交
599
      total_file_size += vstorage->NumLevelBytes(level);
600 601 602
      total_stall_count += stalls;
      total_slowdown_count_soft += stall_leveln_slowdown_count_soft_[level];
      total_slowdown_count_hard += stall_leveln_slowdown_count_hard_[level];
603 604 605 606
      double w_amp =
          (comp_stats_[level].bytes_read_non_output_levels == 0) ? 0.0
          : static_cast<double>(comp_stats_[level].bytes_written) /
            comp_stats_[level].bytes_read_non_output_levels;
607
      PrintLevelStats(buf, sizeof(buf), "L" + ToString(level), files,
S
sdong 已提交
608 609
                      files_being_compacted[level],
                      vstorage->NumLevelBytes(level), compaction_score[level],
610
                      w_amp, stalls, comp_stats_[level]);
611 612 613 614 615 616 617 618
      value->append(buf);
    }
  }
  uint64_t curr_ingest = cf_stats_value_[BYTES_FLUSHED];
  // Cumulative summary
  double w_amp = stats_sum.bytes_written / static_cast<double>(curr_ingest + 1);
  // Stats summary across levels
  PrintLevelStats(buf, sizeof(buf), "Sum", total_files,
619
      total_files_being_compacted, total_file_size, 0, w_amp,
620
      total_stall_count, stats_sum);
621 622 623 624 625 626 627 628
  value->append(buf);
  // Interval summary
  uint64_t interval_ingest =
      curr_ingest - cf_stats_snapshot_.ingest_bytes + 1;
  CompactionStats interval_stats(stats_sum);
  interval_stats.Subtract(cf_stats_snapshot_.comp_stats);
  w_amp = interval_stats.bytes_written / static_cast<double>(interval_ingest);
  PrintLevelStats(buf, sizeof(buf), "Int", 0, 0, 0, 0,
629 630
      w_amp, total_stall_count - cf_stats_snapshot_.stall_count,
      interval_stats);
631 632 633
  value->append(buf);

  snprintf(buf, sizeof(buf),
634
           "Flush(GB): cumulative %.3f, interval %.3f\n",
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
           curr_ingest / kGB, interval_ingest / kGB);
  value->append(buf);

  snprintf(buf, sizeof(buf),
           "Stalls(count): %" PRIu64 " level0_slowdown, "
           "%" PRIu64 " level0_numfiles, %" PRIu64 " memtable_compaction, "
           "%" PRIu64 " leveln_slowdown_soft, "
           "%" PRIu64 " leveln_slowdown_hard\n",
           cf_stats_count_[LEVEL0_SLOWDOWN],
           cf_stats_count_[LEVEL0_NUM_FILES],
           cf_stats_count_[MEMTABLE_COMPACTION],
           total_slowdown_count_soft, total_slowdown_count_hard);
  value->append(buf);

  cf_stats_snapshot_.ingest_bytes = curr_ingest;
  cf_stats_snapshot_.comp_stats = stats_sum;
  cf_stats_snapshot_.stall_count = total_stall_count;
}

654 655 656 657 658 659 660 661 662 663

#else

DBPropertyType GetPropertyType(const Slice& property, bool* is_int_property,
                               bool* need_out_of_mutex) {
  return kUnknown;
}

#endif  // !ROCKSDB_LITE

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