internal_stats.cc 16.6 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"
I
Igor Canadi 已提交
10 11
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
I
Igor Canadi 已提交
12
#include <vector>
13
#include "db/column_family.h"
I
Igor Canadi 已提交
14 15 16

namespace rocksdb {

L
Lei Jin 已提交
17 18 19 20
namespace {
const double kMB = 1048576.0;
const double kGB = kMB * 1024;

21
void PrintLevelStatsHeader(char* buf, size_t len, const std::string& cf_name) {
L
Lei Jin 已提交
22 23
  snprintf(
      buf, len,
24 25
      "\n** Compaction Stats [%s] **\n"
      "Level   Files   Size(MB) Score Read(GB)  Rn(GB) Rnp1(GB) "
I
Igor Canadi 已提交
26
      "Write(GB) Wnew(GB) RW-Amp W-Amp Rd(MB/s) Wr(MB/s)  Rn(cnt) "
L
Lei Jin 已提交
27 28 29 30
      "Rnp1(cnt) Wnp1(cnt) Wnew(cnt)  Comp(sec) Comp(cnt) Avg(sec) "
      "Stall(sec) Stall(cnt) Avg(ms)\n"
      "--------------------------------------------------------------------"
      "--------------------------------------------------------------------"
31 32
      "--------------------------------------------------------------------\n",
      cf_name.c_str());
L
Lei Jin 已提交
33 34 35
}

void PrintLevelStats(char* buf, size_t len, const std::string& name,
36 37
    int num_files, int being_compacted, double total_file_size, double score,
    double rw_amp, double w_amp, double stall_us, uint64_t stalls,
L
Lei Jin 已提交
38
    const InternalStats::CompactionStats& stats) {
39 40
  uint64_t bytes_read = stats.bytes_readn + stats.bytes_readnp1;
  uint64_t bytes_new = stats.bytes_written - stats.bytes_readnp1;
L
Lei Jin 已提交
41 42 43
  double elapsed = (stats.micros + 1) / 1000000.0;

  snprintf(buf, len,
44
    "%4s %5d/%-3d %8.0f %5.1f " /* Level, Files, Size(MB), Score */
L
Lei Jin 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    "%8.1f " /* Read(GB) */
    "%7.1f " /* Rn(GB) */
    "%8.1f " /* Rnp1(GB) */
    "%9.1f " /* Write(GB) */
    "%8.1f " /* Wnew(GB) */
    "%6.1f " /* RW-Amp */
    "%5.1f " /* W-Amp */
    "%8.1f " /* Rd(MB/s) */
    "%8.1f " /* Wr(MB/s) */
    "%8d " /* Rn(cnt) */
    "%9d " /* Rnp1(cnt) */
    "%9d " /* Wnp1(cnt) */
    "%9d " /* Wnew(cnt) */
    "%10.0f " /* Comp(sec) */
    "%9d " /* Comp(cnt) */
    "%8.3f " /* Avg(sec) */
61 62
    "%10.2f " /* Stall(sec) */
    "%10" PRIu64 " " /* Stall(cnt) */
L
Lei Jin 已提交
63
    "%7.2f\n" /* Avg(ms) */,
64
    name.c_str(), num_files, being_compacted, total_file_size / kMB, score,
L
Lei Jin 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    bytes_read / kGB,
    stats.bytes_readn / kGB,
    stats.bytes_readnp1 / kGB,
    stats.bytes_written / kGB,
    bytes_new / kGB,
    rw_amp,
    w_amp,
    bytes_read / kMB / elapsed,
    stats.bytes_written / kMB / elapsed,
    stats.files_in_leveln,
    stats.files_in_levelnp1,
    stats.files_out_levelnp1,
    stats.files_out_levelnp1 - stats.files_in_levelnp1,
    stats.micros / 1000000.0,
    stats.count,
    stats.count == 0 ? 0 : stats.micros / 1000000.0 / stats.count,
    stall_us / 1000000.0,
    stalls,
    stalls == 0 ? 0 : stall_us / 1000.0 / stalls);
}

86

L
Lei Jin 已提交
87 88
}

89
DBPropertyType GetPropertyType(const Slice& property) {
I
Igor Canadi 已提交
90 91
  Slice in = property;
  Slice prefix("rocksdb.");
92
  if (!in.starts_with(prefix)) return kUnknown;
I
Igor Canadi 已提交
93 94 95
  in.remove_prefix(prefix.size());

  if (in.starts_with("num-files-at-level")) {
96
    return kNumFilesAtLevel;
I
Igor Canadi 已提交
97
  } else if (in == "levelstats") {
98
    return kLevelStats;
I
Igor Canadi 已提交
99
  } else if (in == "stats") {
100
    return kStats;
101 102 103 104
  } else if (in == "cfstats") {
    return kCFStats;
  } else if (in == "dbstats") {
    return kDBStats;
105 106 107 108 109
  } else if (in == "sstables") {
    return kSsTables;
  } else if (in == "num-immutable-mem-table") {
    return kNumImmutableMemTable;
  } else if (in == "mem-table-flush-pending") {
110
    return kMemtableFlushPending;
111
  } else if (in == "compaction-pending") {
112 113 114
    return kCompactionPending;
  } else if (in == "background-errors") {
    return kBackgroundErrors;
115 116
  } else if (in == "cur-size-active-mem-table") {
    return kCurSizeActiveMemTable;
117 118 119 120
  } else if (in == "num-entries-active-mem-table") {
    return kNumEntriesInMutableMemtable;
  } else if (in == "num-entries-imm-mem-tables") {
    return kNumEntriesInImmutableMemtable;
S
sdong 已提交
121 122
  } else if (in == "estimate-num-keys") {
    return kEstimatedNumKeys;
123 124 125 126 127
  }
  return kUnknown;
}

bool InternalStats::GetProperty(DBPropertyType property_type,
128
                                const Slice& property, std::string* value) {
129 130 131 132 133 134 135 136 137
  if (property_type > kStartIntTypes) {
    uint64_t int_value;
    bool ret_value = GetIntProperty(property_type, property, &int_value);
    if (ret_value) {
      *value = std::to_string(int_value);
    }
    return ret_value;
  }

138
  Version* current = cfd_->current();
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  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",
                 current->NumLevelFiles(static_cast<int>(level)));
        *value = buf;
        return true;
      }
I
Igor Canadi 已提交
155
    }
156 157 158 159 160 161
    case kLevelStats: {
      char buf[1000];
      snprintf(buf, sizeof(buf),
               "Level Files Size(MB)\n"
               "--------------------\n");
      value->append(buf);
I
Igor Canadi 已提交
162

163 164 165
      for (int level = 0; level < number_levels_; level++) {
        snprintf(buf, sizeof(buf), "%3d %8d %8.0f\n", level,
                 current->NumLevelFiles(level),
L
Lei Jin 已提交
166
                 current->NumLevelBytes(level) / kMB);
I
Igor Canadi 已提交
167 168
        value->append(buf);
      }
169
      return true;
I
Igor Canadi 已提交
170
    }
171
    case kStats: {
172
      if (!GetProperty(kCFStats, "rocksdb.cfstats", value)) {
173
        return false;
174
      }
175
      if (!GetProperty(kDBStats, "rocksdb.dbstats", value)) {
176
        return false;
L
Lei Jin 已提交
177
      }
178 179 180
      return true;
    }
    case kCFStats: {
181
      DumpCFStats(value);
182 183 184 185
      return true;
    }
    case kDBStats: {
      DumpDBStats(value);
186 187 188 189 190
      return true;
    }
    case kSsTables:
      *value = current->DebugString();
      return true;
191 192 193 194 195 196 197 198 199 200 201
    default:
      return false;
  }
}

bool InternalStats::GetIntProperty(DBPropertyType property_type,
                                   const Slice& property,
                                   uint64_t* value) const {
  Version* current = cfd_->current();

  switch (property_type) {
202
    case kNumImmutableMemTable:
203
      *value = cfd_->imm()->size();
204
      return true;
205
    case kMemtableFlushPending:
206
      // Return number of mem tables that are ready to flush (made immutable)
207
      *value = (cfd_->imm()->IsFlushPending() ? 1 : 0);
208
      return true;
209
    case kCompactionPending:
210 211
      // 1 if the system already determines at least one compacdtion is needed.
      // 0 otherwise,
212
      *value = (current->NeedsCompaction() ? 1 : 0);
213
      return true;
214 215
    case kBackgroundErrors:
      // Accumulated number of  errors in background flushes or compactions.
216
      *value = GetBackgroundErrorCount();
217
      return true;
218 219
    case kCurSizeActiveMemTable:
      // Current size of the active memtable
220
      *value = cfd_->mem()->ApproximateMemoryUsage();
221
      return true;
222 223
    case kNumEntriesInMutableMemtable:
      // Current size of the active memtable
224
      *value = cfd_->mem()->GetNumEntries();
225 226 227
      return true;
    case kNumEntriesInImmutableMemtable:
      // Current size of the active memtable
228
      *value = cfd_->imm()->current()->GetTotalNumEntries();
229
      return true;
S
sdong 已提交
230 231 232
    case kEstimatedNumKeys:
      // Estimate number of entries in the column family:
      // Use estimated entries in tables + total entries in memtables.
233 234 235
      *value = cfd_->mem()->GetNumEntries() +
               cfd_->imm()->current()->GetTotalNumEntries() +
               current->GetEstimatedActiveKeys();
S
sdong 已提交
236
      return true;
237 238 239
    default:
      return false;
  }
I
Igor Canadi 已提交
240 241
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
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];
  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];
  // Data
  snprintf(buf, sizeof(buf),
           "Cumulative writes: %" PRIu64 " writes, %" PRIu64 " batches, "
           "%.1f writes per batch, %.2f GB user ingest\n",
           write_other + write_self, write_self,
           (write_other + write_self) / static_cast<double>(write_self + 1),
           user_bytes_written / kGB);
  value->append(buf);
  // WAL
  snprintf(buf, sizeof(buf),
           "Cumulative WAL: %" PRIu64 " writes, %" PRIu64 " syncs, "
           "%.2f writes per sync, %.2f GB written\n",
           write_with_wal, wal_synced,
           write_with_wal / static_cast<double>(wal_synced + 1),
           wal_bytes / kGB);
  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;
  snprintf(buf, sizeof(buf),
           "Interval writes: %" PRIu64 " writes, %" PRIu64 " batches, "
           "%.1f writes per batch, %.1f MB user ingest\n",
           interval_write_other + interval_write_self,
           interval_write_self,
           static_cast<double>(interval_write_other + interval_write_self) /
               (interval_write_self + 1),
           (user_bytes_written - db_stats_snapshot_.ingest_bytes) / kMB);
  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),
           "Interval WAL: %" PRIu64 " writes, %" PRIu64 " syncs, "
           "%.2f writes per sync, %.2f MB written\n",
           interval_write_with_wal,
           interval_wal_synced,
           interval_write_with_wal /
              static_cast<double>(interval_wal_synced + 1),
           interval_wal_bytes / kGB);
  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;
  db_stats_snapshot_.wal_bytes = wal_bytes;
  db_stats_snapshot_.wal_synced = wal_synced;
  db_stats_snapshot_.write_with_wal = write_with_wal;
}

312 313
void InternalStats::DumpCFStats(std::string* value) {
  Version* current = cfd_->current();
314 315

  int num_levels_to_check =
316 317
      (cfd_->options()->compaction_style != kCompactionStyleUniversal &&
       cfd_->options()->compaction_style != kCompactionStyleFIFO)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
          ? current->NumberLevels() - 1
          : 1;
  // 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) {
    compaction_score[current->compaction_level_[i]] =
      current->compaction_score_[i];
  }
  // Count # of files being compacted for each level
  std::vector<int> files_being_compacted(number_levels_, 0);
  for (int level = 0; level < num_levels_to_check; ++level) {
    for (auto* f : current->files_[level]) {
      if (f->being_compacted) {
        ++files_being_compacted[level];
      }
    }
  }

  char buf[1000];
  // Per-ColumnFamily stats
339
  PrintLevelStatsHeader(buf, sizeof(buf), cfd_->GetName());
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  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_soft = 0;
  uint64_t total_slowdown_count_soft = 0;
  uint64_t total_slowdown_hard = 0;
  uint64_t total_slowdown_count_hard = 0;
  uint64_t total_stall_count = 0;
  double total_stall_us = 0;
  for (int level = 0; level < number_levels_; level++) {
    int files = current->NumLevelFiles(level);
    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]);

      double stall_us = level == 0 ?
         (cf_stats_value_[LEVEL0_SLOWDOWN] +
          cf_stats_value_[LEVEL0_NUM_FILES] +
          cf_stats_value_[MEMTABLE_COMPACTION])
         : (stall_leveln_slowdown_soft_[level] +
            stall_leveln_slowdown_hard_[level]);

      stats_sum.Add(comp_stats_[level]);
      total_file_size += current->NumLevelBytes(level);
      total_stall_us += stall_us;
      total_stall_count += stalls;
      total_slowdown_soft += stall_leveln_slowdown_soft_[level];
      total_slowdown_count_soft += stall_leveln_slowdown_count_soft_[level];
      total_slowdown_hard += stall_leveln_slowdown_hard_[level];
      total_slowdown_count_hard += stall_leveln_slowdown_count_hard_[level];
      int64_t bytes_read = comp_stats_[level].bytes_readn +
                           comp_stats_[level].bytes_readnp1;
      double rw_amp = (comp_stats_[level].bytes_readn == 0) ? 0.0
          : (comp_stats_[level].bytes_written + bytes_read) /
            static_cast<double>(comp_stats_[level].bytes_readn);
      double w_amp = (comp_stats_[level].bytes_readn == 0) ? 0.0
          : comp_stats_[level].bytes_written /
            static_cast<double>(comp_stats_[level].bytes_readn);
      PrintLevelStats(buf, sizeof(buf), "L" + std::to_string(level),
          files, files_being_compacted[level], current->NumLevelBytes(level),
          compaction_score[level], rw_amp, w_amp, stall_us, stalls,
          comp_stats_[level]);
      value->append(buf);
    }
  }
  uint64_t curr_ingest = cf_stats_value_[BYTES_FLUSHED];
  // Cumulative summary
  double rw_amp = (stats_sum.bytes_written + stats_sum.bytes_readn +
      stats_sum.bytes_readnp1) / static_cast<double>(curr_ingest + 1);
  double w_amp = stats_sum.bytes_written / static_cast<double>(curr_ingest + 1);
  // Stats summary across levels
  PrintLevelStats(buf, sizeof(buf), "Sum", total_files,
      total_files_being_compacted, total_file_size, 0, rw_amp, w_amp,
      total_stall_us, total_stall_count, stats_sum);
  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);
  rw_amp = (interval_stats.bytes_written +
      interval_stats.bytes_readn + interval_stats.bytes_readnp1) /
      static_cast<double>(interval_ingest);
  w_amp = interval_stats.bytes_written / static_cast<double>(interval_ingest);
  PrintLevelStats(buf, sizeof(buf), "Int", 0, 0, 0, 0,
      rw_amp, w_amp, total_stall_us - cf_stats_snapshot_.stall_us,
      total_stall_count - cf_stats_snapshot_.stall_count, interval_stats);
  value->append(buf);

  snprintf(buf, sizeof(buf),
           "Flush(GB): accumulative %.3f, interval %.3f\n",
           curr_ingest / kGB, interval_ingest / kGB);
  value->append(buf);
  snprintf(buf, sizeof(buf),
           "Stalls(secs): %.3f level0_slowdown, %.3f level0_numfiles, "
           "%.3f memtable_compaction, %.3f leveln_slowdown_soft, "
           "%.3f leveln_slowdown_hard\n",
           cf_stats_value_[LEVEL0_SLOWDOWN] / 1000000.0,
           cf_stats_value_[LEVEL0_NUM_FILES] / 1000000.0,
           cf_stats_value_[MEMTABLE_COMPACTION] / 1000000.0,
           total_slowdown_soft / 1000000.0,
           total_slowdown_hard / 1000000.0);
  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_us = total_stall_us;
  cf_stats_snapshot_.stall_count = total_stall_count;
}

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