internal_stats.cc 31.1 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 <string>
17
#include <algorithm>
I
Igor Canadi 已提交
18
#include <vector>
19
#include "db/column_family.h"
20

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

namespace rocksdb {

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

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

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

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

86 87 88
static const std::string rocksdb_prefix = "rocksdb.";

static const std::string num_files_at_level_prefix = "num-files-at-level";
89
static const std::string allstats = "stats";
90 91 92 93 94
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";
95 96
static const std::string num_immutable_mem_table_flushed =
    "num-immutable-mem-table-flushed";
97 98 99 100 101
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";
102
static const std::string cur_size_unflushed_mem_tables =
103
    "cur-size-all-mem-tables";
104
static const std::string cur_size_all_mem_tables = "size-all-mem-tables";
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
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";
A
Andres Notzli 已提交
121
static const std::string estimate_live_data_size = "estimate-live-data-size";
122
static const std::string base_level = "base-level";
123
static const std::string total_sst_files_size = "total-sst-files-size";
124 125
static const std::string estimate_pending_comp_bytes =
    "estimate-pending-compaction-bytes";
126 127 128 129
static const std::string aggregated_table_properties =
    "aggregated-table-properties";
static const std::string aggregated_table_properties_at_level =
    aggregated_table_properties + "-at-level";
130 131
static const std::string num_running_compactions = "num-running-compactions";
static const std::string num_running_flushes = "num-running-flushes";
132 133 134

const std::string DB::Properties::kNumFilesAtLevelPrefix =
                      rocksdb_prefix + num_files_at_level_prefix;
135
const std::string DB::Properties::kStats = rocksdb_prefix + allstats;
136 137 138 139 140 141 142 143 144
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;
145 146 147 148
const std::string DB::Properties::kNumRunningCompactions =
    rocksdb_prefix + num_running_compactions;
const std::string DB::Properties::kNumRunningFlushes =
    rocksdb_prefix + num_running_flushes;
149 150 151 152 153
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 =
154 155 156
    rocksdb_prefix + cur_size_unflushed_mem_tables;
const std::string DB::Properties::kSizeAllMemTables =
    rocksdb_prefix + cur_size_all_mem_tables;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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;
A
Andres Notzli 已提交
177 178
const std::string DB::Properties::kEstimateLiveDataSize =
                      rocksdb_prefix + estimate_live_data_size;
179 180
const std::string DB::Properties::kTotalSstFilesSize =
                      rocksdb_prefix + total_sst_files_size;
181 182
const std::string DB::Properties::kEstimatePendingCompactionBytes =
    rocksdb_prefix + estimate_pending_comp_bytes;
183 184 185 186
const std::string DB::Properties::kAggregatedTableProperties =
    rocksdb_prefix + aggregated_table_properties;
const std::string DB::Properties::kAggregatedTablePropertiesAtLevel =
    rocksdb_prefix + aggregated_table_properties_at_level;
187

188 189 190 191
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 已提交
192
  Slice in = property;
193
  Slice prefix(rocksdb_prefix);
194
  *need_out_of_mutex = false;
195
  *is_int_property = false;
S
sdong 已提交
196 197 198
  if (!in.starts_with(prefix)) {
    return kUnknown;
  }
I
Igor Canadi 已提交
199 200
  in.remove_prefix(prefix.size());

201
  if (in.starts_with(num_files_at_level_prefix)) {
202
    return kNumFilesAtLevel;
203
  } else if (in == levelstats) {
204
    return kLevelStats;
205
  } else if (in == allstats) {
206
    return kStats;
207
  } else if (in == cfstats) {
208
    return kCFStats;
209
  } else if (in == dbstats) {
210
    return kDBStats;
211
  } else if (in == sstables) {
212
    return kSsTables;
213 214 215 216
  } else if (in == aggregated_table_properties) {
    return kAggregatedTableProperties;
  } else if (in.starts_with(aggregated_table_properties_at_level)) {
    return kAggregatedTablePropertiesAtLevel;
217 218 219
  }

  *is_int_property = true;
220
  if (in == num_immutable_mem_table) {
221
    return kNumImmutableMemTable;
222 223
  } else if (in == num_immutable_mem_table_flushed) {
    return kNumImmutableMemTableFlushed;
224
  } else if (in == mem_table_flush_pending) {
225
    return kMemtableFlushPending;
226
  } else if (in == compaction_pending) {
227
    return kCompactionPending;
228
  } else if (in == background_errors) {
229
    return kBackgroundErrors;
230
  } else if (in == cur_size_active_mem_table) {
231
    return kCurSizeActiveMemTable;
232
  } else if (in == cur_size_unflushed_mem_tables) {
I
Igor Canadi 已提交
233
    return kCurSizeAllMemTables;
234 235
  } else if (in == cur_size_all_mem_tables) {
    return kSizeAllMemTables;
236
  } else if (in == num_entries_active_mem_table) {
237
    return kNumEntriesInMutableMemtable;
238
  } else if (in == num_entries_imm_mem_tables) {
239
    return kNumEntriesInImmutableMemtable;
240
  } else if (in == num_deletes_active_mem_table) {
241
    return kNumDeletesInMutableMemtable;
242
  } else if (in == num_deletes_imm_mem_tables) {
243
    return kNumDeletesInImmutableMemtable;
244
  } else if (in == estimate_num_keys) {
S
sdong 已提交
245
    return kEstimatedNumKeys;
246
  } else if (in == estimate_table_readers_mem) {
247 248
    *need_out_of_mutex = true;
    return kEstimatedUsageByTableReaders;
249
  } else if (in == is_file_deletions_enabled) {
250
    return kIsFileDeletionEnabled;
251
  } else if (in == num_snapshots) {
252
    return kNumSnapshots;
253
  } else if (in == oldest_snapshot_time) {
254
    return kOldestSnapshotTime;
255
  } else if (in == num_live_versions) {
256
    return kNumLiveVersions;
A
Andres Notzli 已提交
257 258 259
  } else if (in == estimate_live_data_size) {
    *need_out_of_mutex = true;
    return kEstimateLiveDataSize;
260
  } else if (in == base_level) {
261
    return kBaseLevel;
262 263
  } else if (in == total_sst_files_size) {
    return kTotalSstFilesSize;
264 265
  } else if (in == estimate_pending_comp_bytes) {
    return kEstimatePendingCompactionBytes;
266 267 268 269
  } else if (in == num_running_flushes) {
    return kNumRunningFlushes;
  } else if (in == num_running_compactions) {
    return kNumRunningCompactions;
270 271 272 273
  }
  return kUnknown;
}

274 275 276 277
bool InternalStats::GetIntPropertyOutOfMutex(DBPropertyType property_type,
                                             Version* version,
                                             uint64_t* value) const {
  assert(value != nullptr);
A
Andres Notzli 已提交
278 279 280 281 282 283 284 285 286 287 288 289
  const auto* vstorage = cfd_->current()->storage_info();

  switch (property_type) {
    case kEstimatedUsageByTableReaders:
      *value = (version == nullptr) ?
        0 : version->GetMemoryUsageByTableReaders();
      return true;
    case kEstimateLiveDataSize:
      *value = vstorage->EstimateLiveDataSize();
      return true;
    default:
      return false;
290 291
  }
}
292

293 294 295 296
bool InternalStats::GetStringProperty(DBPropertyType property_type,
                                      const Slice& property,
                                      std::string* value) {
  assert(value != nullptr);
S
sdong 已提交
297
  auto* current = cfd_->current();
S
sdong 已提交
298
  const auto* vstorage = current->storage_info();
299 300 301 302 303 304 305 306 307 308 309 310
  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 已提交
311
                 vstorage->NumLevelFiles(static_cast<int>(level)));
312 313 314
        *value = buf;
        return true;
      }
I
Igor Canadi 已提交
315
    }
316 317 318 319 320 321
    case kLevelStats: {
      char buf[1000];
      snprintf(buf, sizeof(buf),
               "Level Files Size(MB)\n"
               "--------------------\n");
      value->append(buf);
I
Igor Canadi 已提交
322

323 324
      for (int level = 0; level < number_levels_; level++) {
        snprintf(buf, sizeof(buf), "%3d %8d %8.0f\n", level,
S
sdong 已提交
325 326
                 vstorage->NumLevelFiles(level),
                 vstorage->NumLevelBytes(level) / kMB);
I
Igor Canadi 已提交
327 328
        value->append(buf);
      }
329
      return true;
I
Igor Canadi 已提交
330
    }
331
    case kStats: {
332
      if (!GetStringProperty(kCFStats, DB::Properties::kCFStats, value)) {
333
        return false;
334
      }
335
      if (!GetStringProperty(kDBStats, DB::Properties::kDBStats, value)) {
336
        return false;
L
Lei Jin 已提交
337
      }
338 339 340
      return true;
    }
    case kCFStats: {
341
      DumpCFStats(value);
342 343 344 345
      return true;
    }
    case kDBStats: {
      DumpDBStats(value);
346 347 348 349 350
      return true;
    }
    case kSsTables:
      *value = current->DebugString();
      return true;
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
    case kAggregatedTableProperties: {
      std::shared_ptr<const TableProperties> tp;
      auto s = cfd_->current()->GetAggregatedTableProperties(&tp);
      if (!s.ok()) {
        return false;
      }
      *value = tp->ToString();
      return true;
    }
    case kAggregatedTablePropertiesAtLevel: {
      in.remove_prefix(
          DB::Properties::kAggregatedTablePropertiesAtLevel.length());
      uint64_t level;
      bool ok = ConsumeDecimalNumber(&in, &level) && in.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;
    }
377 378 379 380 381 382
    default:
      return false;
  }
}

bool InternalStats::GetIntProperty(DBPropertyType property_type,
383
                                   uint64_t* value, DBImpl* db) const {
384
  db->mutex_.AssertHeld();
S
sdong 已提交
385
  const auto* vstorage = cfd_->current()->storage_info();
386 387

  switch (property_type) {
388
    case kNumImmutableMemTable:
389 390 391 392
      *value = cfd_->imm()->NumNotFlushed();
      return true;
    case kNumImmutableMemTableFlushed:
      *value = cfd_->imm()->NumFlushed();
393
      return true;
394
    case kMemtableFlushPending:
395
      // Return number of mem tables that are ready to flush (made immutable)
396
      *value = (cfd_->imm()->IsFlushPending() ? 1 : 0);
397
      return true;
398 399 400
    case kNumRunningFlushes:
      *value = db->num_running_flushes();
      return true;
401
    case kCompactionPending:
402
      // 1 if the system already determines at least one compaction is needed.
403
      // 0 otherwise,
Y
Yueh-Hsuan Chiang 已提交
404
      *value = (cfd_->compaction_picker()->NeedsCompaction(vstorage) ? 1 : 0);
405
      return true;
406 407 408
    case kNumRunningCompactions:
      *value = db->num_running_compactions_;
      return true;
409 410
    case kBackgroundErrors:
      // Accumulated number of  errors in background flushes or compactions.
411
      *value = GetBackgroundErrorCount();
412
      return true;
413 414
    case kCurSizeActiveMemTable:
      // Current size of the active memtable
415
      *value = cfd_->mem()->ApproximateMemoryUsage();
416
      return true;
I
Igor Canadi 已提交
417 418
    case kCurSizeAllMemTables:
      // Current size of the active memtable + immutable memtables
419 420 421 422
      *value = cfd_->mem()->ApproximateMemoryUsage() +
               cfd_->imm()->ApproximateUnflushedMemTablesMemoryUsage();
      return true;
    case kSizeAllMemTables:
I
Igor Canadi 已提交
423 424 425
      *value = cfd_->mem()->ApproximateMemoryUsage() +
               cfd_->imm()->ApproximateMemoryUsage();
      return true;
426
    case kNumEntriesInMutableMemtable:
I
Igor Canadi 已提交
427
      // Current number of entires in the active memtable
428
      *value = cfd_->mem()->num_entries();
429 430
      return true;
    case kNumEntriesInImmutableMemtable:
I
Igor Canadi 已提交
431
      // Current number of entries in the immutable memtables
432
      *value = cfd_->imm()->current()->GetTotalNumEntries();
433
      return true;
434 435 436 437 438 439 440 441
    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 已提交
442 443 444
    case kEstimatedNumKeys:
      // Estimate number of entries in the column family:
      // Use estimated entries in tables + total entries in memtables.
445 446 447 448 449
      *value = cfd_->mem()->num_entries() +
               cfd_->imm()->current()->GetTotalNumEntries() -
               (cfd_->mem()->num_deletes() +
                cfd_->imm()->current()->GetTotalNumDeletes()) *
                   2 +
S
sdong 已提交
450
               vstorage->GetEstimatedActiveKeys();
S
sdong 已提交
451
      return true;
452 453 454 455 456 457
    case kNumSnapshots:
      *value = db->snapshots().count();
      return true;
    case kOldestSnapshotTime:
      *value = static_cast<uint64_t>(db->snapshots().GetOldestSnapshotTime());
      return true;
458 459 460
    case kNumLiveVersions:
      *value = cfd_->GetNumLiveVersions();
      return true;
461 462 463
    case kIsFileDeletionEnabled:
      *value = db->IsFileDeletionsEnabled();
      return true;
464 465 466
    case kBaseLevel:
      *value = vstorage->base_level();
      return true;
467 468 469
    case kTotalSstFilesSize:
      *value = cfd_->GetTotalSstFilesSize();
      return true;
470 471 472
    case kEstimatePendingCompactionBytes:
      *value = vstorage->estimated_compaction_needed_bytes();
      return true;
473 474 475
    default:
      return false;
  }
I
Igor Canadi 已提交
476 477
}

478 479 480
void InternalStats::DumpDBStats(std::string* value) {
  char buf[1000];
  // DB-level stats, only available from default column family
481
  double seconds_up = (env_->NowMicros() - started_at_ + 1) / kMicrosInSec;
482 483 484 485 486 487
  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
488 489 490 491 492 493 494 495
  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);
496 497 498 499
  uint64_t compact_bytes_read = 0;
  uint64_t compact_bytes_write = 0;
  uint64_t compact_micros = 0;

500 501 502
  const int kHumanMicrosLen = 32;
  char human_micros[kHumanMicrosLen];

503
  // Data
S
sdong 已提交
504 505 506 507 508 509 510 511
  // 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.
512
  snprintf(buf, sizeof(buf),
513 514 515 516 517
           "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(),
518
           (write_other + write_self) / static_cast<double>(write_self + 1),
519
           user_bytes_written / kGB, user_bytes_written / kMB / seconds_up);
520 521 522
  value->append(buf);
  // WAL
  snprintf(buf, sizeof(buf),
523 524 525 526
           "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(),
527
           write_with_wal / static_cast<double>(wal_synced + 1),
528 529
           wal_bytes / kGB, wal_bytes / kMB / seconds_up);
  value->append(buf);
530 531
  // Compact
  for (int level = 0; level < number_levels_; level++) {
532 533
    compact_bytes_read += comp_stats_[level].bytes_read_output_level +
                          comp_stats_[level].bytes_read_non_output_levels;
534 535 536 537 538 539
    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",
540 541 542
           compact_bytes_write / kGB, compact_bytes_write / kMB / seconds_up,
           compact_bytes_read / kGB, compact_bytes_read / kMB / seconds_up,
           compact_micros / kMicrosInSec);
543
  value->append(buf);
544 545 546 547 548 549 550
  // 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));
551 552 553 554 555
  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 已提交
556 557
  uint64_t interval_num_keys_written =
      num_keys_written - db_stats_snapshot_.num_keys_written;
558
  snprintf(buf, sizeof(buf),
559 560 561 562 563 564
           "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(),
565 566
           static_cast<double>(interval_write_other + interval_write_self) /
               (interval_write_self + 1),
S
sdong 已提交
567
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB,
568 569
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB /
               std::max(interval_seconds_up, 0.001)),
570 571 572 573 574 575 576 577
  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),
578 579 580 581
           "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(),
582 583
           interval_write_with_wal /
              static_cast<double>(interval_wal_synced + 1),
584 585 586 587
           interval_wal_bytes / kGB,
           interval_wal_bytes / kMB / std::max(interval_seconds_up, 0.001));
  value->append(buf);

588 589 590 591 592 593 594 595
  // 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;

596 597 598 599 600 601 602 603 604
  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);
605 606
  value->append(buf);

607 608 609 610 611 612 613 614 615 616
  // 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));
617 618
  value->append(buf);

619 620 621 622 623 624 625 626 627 628
  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);
    }
  }

629 630 631 632
  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 已提交
633
  db_stats_snapshot_.num_keys_written = num_keys_written;
634 635 636
  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 已提交
637
  db_stats_snapshot_.write_stall_micros = write_stall_micros;
638 639 640
  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;
641 642
}

643
void InternalStats::DumpCFStats(std::string* value) {
S
sdong 已提交
644
  const VersionStorageInfo* vstorage = cfd_->current()->storage_info();
645 646

  int num_levels_to_check =
647
      (cfd_->ioptions()->compaction_style != kCompactionStyleFIFO)
648
          ? vstorage->num_levels() - 1
649
          : 1;
S
sdong 已提交
650

651 652 653 654
  // 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) {
655 656
    compaction_score[vstorage->CompactionScoreLevel(i)] =
        vstorage->CompactionScore(i);
657 658 659
  }
  // Count # of files being compacted for each level
  std::vector<int> files_being_compacted(number_levels_, 0);
660
  for (int level = 0; level < number_levels_; ++level) {
661
    for (auto* f : vstorage->LevelFiles(level)) {
662 663 664 665 666 667 668 669
      if (f->being_compacted) {
        ++files_being_compacted[level];
      }
    }
  }

  char buf[1000];
  // Per-ColumnFamily stats
670
  PrintLevelStatsHeader(buf, sizeof(buf), cfd_->GetName());
671 672 673 674 675 676 677
  value->append(buf);

  CompactionStats stats_sum(0);
  int total_files = 0;
  int total_files_being_compacted = 0;
  double total_file_size = 0;
  for (int level = 0; level < number_levels_; level++) {
S
sdong 已提交
678
    int files = vstorage->NumLevelFiles(level);
679 680 681 682
    total_files += files;
    total_files_being_compacted += files_being_compacted[level];
    if (comp_stats_[level].micros > 0 || files > 0) {
      stats_sum.Add(comp_stats_[level]);
S
sdong 已提交
683
      total_file_size += vstorage->NumLevelBytes(level);
684 685 686 687
      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;
688
      PrintLevelStats(buf, sizeof(buf), "L" + ToString(level), files,
S
sdong 已提交
689
                      files_being_compacted[level],
690 691
                      static_cast<double>(vstorage->NumLevelBytes(level)),
                      compaction_score[level],
692
                      w_amp, comp_stats_[level]);
693 694 695 696 697 698
      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);
699 700 701 702 703
  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] +
704
      cf_stats_count_[MEMTABLE_COMPACTION] + cf_stats_count_[MEMTABLE_SLOWDOWN];
705 706
  // Stats summary across levels
  PrintLevelStats(buf, sizeof(buf), "Sum", total_files,
707 708
                  total_files_being_compacted, total_file_size, 0, w_amp,
                  stats_sum);
709 710 711 712 713 714 715
  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);
716
  PrintLevelStats(buf, sizeof(buf), "Int", 0, 0, 0, 0, w_amp, interval_stats);
717 718 719
  value->append(buf);

  snprintf(buf, sizeof(buf),
720
           "Flush(GB): cumulative %.3f, interval %.3f\n",
721 722 723
           curr_ingest / kGB, interval_ingest / kGB);
  value->append(buf);

724 725 726 727 728 729 730 731 732
  snprintf(buf, sizeof(buf), "Stalls(count): %" PRIu64
                             " level0_slowdown, "
                             "%" PRIu64
                             " level0_slowdown_with_compaction, "
                             "%" PRIu64
                             " level0_numfiles, "
                             "%" PRIu64
                             " level0_numfiles_with_compaction, "
                             "%" PRIu64
733
                             " stop for pending_compaction_bytes, "
734
                             "%" PRIu64
735
                             " slowdown for pending_compaction_bytes, "
736
                             "%" PRIu64
737
                             " memtable_compaction, "
738 739
                             "%" PRIu64
                             " memtable_slowdown, "
740
                             "interval %" PRIu64 " total count\n",
741 742 743 744
           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],
745
           cf_stats_count_[HARD_PENDING_COMPACTION_BYTES_LIMIT],
746 747
           cf_stats_count_[SOFT_PENDING_COMPACTION_BYTES_LIMIT],
           cf_stats_count_[MEMTABLE_COMPACTION],
748
           cf_stats_count_[MEMTABLE_SLOWDOWN],
749
           total_stall_count - cf_stats_snapshot_.stall_count);
750 751 752 753 754 755 756
  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;
}

757 758 759 760 761 762 763 764 765 766

#else

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

#endif  // !ROCKSDB_LITE

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