internal_stats.h 28.9 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  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 已提交
5 6 7 8 9 10 11
//
// 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.
//

#pragma once
12

13
#include <map>
14
#include <memory>
I
Igor Canadi 已提交
15
#include <string>
16 17
#include <vector>

18
#include "cache/cache_entry_roles.h"
19
#include "db/version_set.h"
20
#include "rocksdb/system_clock.h"
I
Igor Canadi 已提交
21

22 23
class ColumnFamilyData;

24
namespace ROCKSDB_NAMESPACE {
25

26 27
template <class Stats>
class CacheEntryStatsCollector;
28
class DBImpl;
S
Siying Dong 已提交
29
class MemTableList;
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// Config for retrieving a property's value.
struct DBPropertyInfo {
  bool need_out_of_mutex;

  // gcc had an internal error for initializing union of pointer-to-member-
  // functions. Workaround is to populate exactly one of the following function
  // pointers with a non-nullptr value.

  // @param value Value-result argument for storing the property's string value
  // @param suffix Argument portion of the property. For example, suffix would
  //      be "5" for the property "rocksdb.num-files-at-level5". So far, only
  //      certain string properties take an argument.
  bool (InternalStats::*handle_string)(std::string* value, Slice suffix);

  // @param value Value-result argument for storing the property's uint64 value
  // @param db Many of the int properties rely on DBImpl methods.
  // @param version Version is needed in case the property is retrieved without
  //      holding db mutex, which is only supported for int properties.
  bool (InternalStats::*handle_int)(uint64_t* value, DBImpl* db,
                                    Version* version);
51 52

  // @param props Map of general properties to populate
53 54 55
  // @param suffix Argument portion of the property. (see handle_string)
  bool (InternalStats::*handle_map)(std::map<std::string, std::string>* props,
                                    Slice suffix);
56 57 58 59

  // handle the string type properties rely on DBImpl methods
  // @param value Value-result argument for storing the property's string value
  bool (DBImpl::*handle_string_dbimpl)(std::string* value);
60 61
};

62
extern const DBPropertyInfo* GetPropertyInfo(const Slice& property);
63 64

#ifndef ROCKSDB_LITE
T
Tomas Kolda 已提交
65
#undef SCORE
66 67 68 69
enum class LevelStatType {
  INVALID = 0,
  NUM_FILES,
  COMPACTED_FILES,
70
  SIZE_BYTES,
71 72 73 74 75 76 77 78 79 80 81
  SCORE,
  READ_GB,
  RN_GB,
  RNP1_GB,
  WRITE_GB,
  W_NEW_GB,
  MOVED_GB,
  WRITE_AMP,
  READ_MBPS,
  WRITE_MBPS,
  COMP_SEC,
82
  COMP_CPU_SEC,
83 84 85 86
  COMP_COUNT,
  AVG_SEC,
  KEY_IN,
  KEY_DROP,
87
  R_BLOB_GB,
88
  W_BLOB_GB,
89 90 91 92 93 94 95 96 97 98
  TOTAL  // total number of types
};

struct LevelStat {
  // This what will be L?.property_name in the flat map returned to the user
  std::string property_name;
  // This will be what we will print in the header in the cli
  std::string header_name;
};

I
Igor Canadi 已提交
99 100
class InternalStats {
 public:
101 102
  static const std::map<LevelStatType, LevelStat> compaction_level_stats;

103
  enum InternalCFStatsType {
104 105 106 107 108 109 110 111
    L0_FILE_COUNT_LIMIT_SLOWDOWNS,
    LOCKED_L0_FILE_COUNT_LIMIT_SLOWDOWNS,
    MEMTABLE_LIMIT_STOPS,
    MEMTABLE_LIMIT_SLOWDOWNS,
    L0_FILE_COUNT_LIMIT_STOPS,
    LOCKED_L0_FILE_COUNT_LIMIT_STOPS,
    PENDING_COMPACTION_BYTES_LIMIT_SLOWDOWNS,
    PENDING_COMPACTION_BYTES_LIMIT_STOPS,
I
Igor Canadi 已提交
112
    WRITE_STALLS_ENUM_MAX,
113
    BYTES_FLUSHED,
114
    BYTES_INGESTED_ADD_FILE,
115 116 117
    INGESTED_NUM_FILES_TOTAL,
    INGESTED_LEVEL0_NUM_FILES_TOTAL,
    INGESTED_NUM_KEYS_TOTAL,
118
    INTERNAL_CF_STATS_ENUM_MAX,
I
Igor Canadi 已提交
119 120
  };

121
  enum InternalDBStatsType {
122 123 124 125 126 127 128 129 130
    kIntStatsWalFileBytes,
    kIntStatsWalFileSynced,
    kIntStatsBytesWritten,
    kIntStatsNumKeysWritten,
    kIntStatsWriteDoneByOther,
    kIntStatsWriteDoneBySelf,
    kIntStatsWriteWithWal,
    kIntStatsWriteStallMicros,
    kIntStatsNumMax,
131 132
  };

133
  InternalStats(int num_levels, SystemClock* clock, ColumnFamilyData* cfd);
I
Igor Canadi 已提交
134

135
  // Per level compaction stats.  comp_stats_[level] stores the stats for
I
Igor Canadi 已提交
136 137 138
  // compactions that produced data for the specified "level".
  struct CompactionStats {
    uint64_t micros;
139
    uint64_t cpu_micros;
I
Igor Canadi 已提交
140

141
    // The number of bytes read from all non-output levels (table files)
142
    uint64_t bytes_read_non_output_levels;
I
Igor Canadi 已提交
143

144
    // The number of bytes read from the compaction output level (table files)
145
    uint64_t bytes_read_output_level;
I
Igor Canadi 已提交
146

147 148 149
    // The number of bytes read from blob files
    uint64_t bytes_read_blob;

150
    // Total number of bytes written to table files during compaction
151
    uint64_t bytes_written;
I
Igor Canadi 已提交
152

153 154 155 156
    // Total number of bytes written to blob files during compaction
    uint64_t bytes_written_blob;

    // Total number of bytes moved to the output level (table files)
157 158
    uint64_t bytes_moved;

159 160
    // The number of compaction input files in all non-output levels (table
    // files)
161
    int num_input_files_in_non_output_levels;
I
Igor Canadi 已提交
162

163
    // The number of compaction input files in the output level (table files)
164
    int num_input_files_in_output_level;
I
Igor Canadi 已提交
165

166
    // The number of compaction output files (table files)
167
    int num_output_files;
I
Igor Canadi 已提交
168

169 170 171
    // The number of compaction output files (blob files)
    int num_output_files_blob;

172
    // Total incoming entries during compaction between levels N and N+1
S
sdong 已提交
173
    uint64_t num_input_records;
174 175

    // Accumulated diff number of entries
176
    // (num input entries - num output entries) for compaction levels N and N+1
S
sdong 已提交
177
    uint64_t num_dropped_records;
178

I
Igor Canadi 已提交
179 180 181
    // Number of compactions done
    int count;

182 183 184 185 186
    // Number of compactions done per CompactionReason
    int counts[static_cast<int>(CompactionReason::kNumOfReasons)];

    explicit CompactionStats()
        : micros(0),
187
          cpu_micros(0),
188 189
          bytes_read_non_output_levels(0),
          bytes_read_output_level(0),
190
          bytes_read_blob(0),
191
          bytes_written(0),
192
          bytes_written_blob(0),
193 194 195 196
          bytes_moved(0),
          num_input_files_in_non_output_levels(0),
          num_input_files_in_output_level(0),
          num_output_files(0),
197
          num_output_files_blob(0),
198 199 200 201 202 203 204 205 206 207
          num_input_records(0),
          num_dropped_records(0),
          count(0) {
      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i < num_of_reasons; i++) {
        counts[i] = 0;
      }
    }

    explicit CompactionStats(CompactionReason reason, int c)
I
Igor Canadi 已提交
208
        : micros(0),
209
          cpu_micros(0),
210 211
          bytes_read_non_output_levels(0),
          bytes_read_output_level(0),
212
          bytes_read_blob(0),
I
Igor Canadi 已提交
213
          bytes_written(0),
214
          bytes_written_blob(0),
215
          bytes_moved(0),
216 217 218
          num_input_files_in_non_output_levels(0),
          num_input_files_in_output_level(0),
          num_output_files(0),
219
          num_output_files_blob(0),
220 221
          num_input_records(0),
          num_dropped_records(0),
222 223 224 225 226 227 228 229 230 231 232 233
          count(c) {
      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i < num_of_reasons; i++) {
        counts[i] = 0;
      }
      int r = static_cast<int>(reason);
      if (r >= 0 && r < num_of_reasons) {
        counts[r] = c;
      } else {
        count = 0;
      }
    }
I
Igor Canadi 已提交
234

235 236
    explicit CompactionStats(const CompactionStats& c)
        : micros(c.micros),
237
          cpu_micros(c.cpu_micros),
238 239
          bytes_read_non_output_levels(c.bytes_read_non_output_levels),
          bytes_read_output_level(c.bytes_read_output_level),
240
          bytes_read_blob(c.bytes_read_blob),
241
          bytes_written(c.bytes_written),
242
          bytes_written_blob(c.bytes_written_blob),
243
          bytes_moved(c.bytes_moved),
244 245
          num_input_files_in_non_output_levels(
              c.num_input_files_in_non_output_levels),
246
          num_input_files_in_output_level(c.num_input_files_in_output_level),
247
          num_output_files(c.num_output_files),
248
          num_output_files_blob(c.num_output_files_blob),
249 250
          num_input_records(c.num_input_records),
          num_dropped_records(c.num_dropped_records),
251 252 253 254 255 256
          count(c.count) {
      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i < num_of_reasons; i++) {
        counts[i] = c.counts[i];
      }
    }
257

258 259 260 261 262
    CompactionStats& operator=(const CompactionStats& c) {
      micros = c.micros;
      cpu_micros = c.cpu_micros;
      bytes_read_non_output_levels = c.bytes_read_non_output_levels;
      bytes_read_output_level = c.bytes_read_output_level;
263
      bytes_read_blob = c.bytes_read_blob;
264
      bytes_written = c.bytes_written;
265
      bytes_written_blob = c.bytes_written_blob;
266 267 268 269 270
      bytes_moved = c.bytes_moved;
      num_input_files_in_non_output_levels =
          c.num_input_files_in_non_output_levels;
      num_input_files_in_output_level = c.num_input_files_in_output_level;
      num_output_files = c.num_output_files;
271
      num_output_files_blob = c.num_output_files_blob;
272 273 274 275 276 277 278 279 280 281 282
      num_input_records = c.num_input_records;
      num_dropped_records = c.num_dropped_records;
      count = c.count;

      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i < num_of_reasons; i++) {
        counts[i] = c.counts[i];
      }
      return *this;
    }

S
Siying Dong 已提交
283 284
    void Clear() {
      this->micros = 0;
285
      this->cpu_micros = 0;
S
Siying Dong 已提交
286 287
      this->bytes_read_non_output_levels = 0;
      this->bytes_read_output_level = 0;
288
      this->bytes_read_blob = 0;
S
Siying Dong 已提交
289
      this->bytes_written = 0;
290
      this->bytes_written_blob = 0;
S
Siying Dong 已提交
291 292 293 294
      this->bytes_moved = 0;
      this->num_input_files_in_non_output_levels = 0;
      this->num_input_files_in_output_level = 0;
      this->num_output_files = 0;
295
      this->num_output_files_blob = 0;
S
Siying Dong 已提交
296 297 298
      this->num_input_records = 0;
      this->num_dropped_records = 0;
      this->count = 0;
299 300 301 302
      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i < num_of_reasons; i++) {
        counts[i] = 0;
      }
S
Siying Dong 已提交
303 304
    }

I
Igor Canadi 已提交
305 306
    void Add(const CompactionStats& c) {
      this->micros += c.micros;
307
      this->cpu_micros += c.cpu_micros;
308 309
      this->bytes_read_non_output_levels += c.bytes_read_non_output_levels;
      this->bytes_read_output_level += c.bytes_read_output_level;
310
      this->bytes_read_blob += c.bytes_read_blob;
I
Igor Canadi 已提交
311
      this->bytes_written += c.bytes_written;
312
      this->bytes_written_blob += c.bytes_written_blob;
313
      this->bytes_moved += c.bytes_moved;
314 315 316 317 318
      this->num_input_files_in_non_output_levels +=
          c.num_input_files_in_non_output_levels;
      this->num_input_files_in_output_level +=
          c.num_input_files_in_output_level;
      this->num_output_files += c.num_output_files;
319
      this->num_output_files_blob += c.num_output_files_blob;
320 321
      this->num_input_records += c.num_input_records;
      this->num_dropped_records += c.num_dropped_records;
L
Lei Jin 已提交
322
      this->count += c.count;
323 324 325 326
      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i< num_of_reasons; i++) {
        counts[i] += c.counts[i];
      }
I
Igor Canadi 已提交
327
    }
328 329 330

    void Subtract(const CompactionStats& c) {
      this->micros -= c.micros;
331
      this->cpu_micros -= c.cpu_micros;
332 333
      this->bytes_read_non_output_levels -= c.bytes_read_non_output_levels;
      this->bytes_read_output_level -= c.bytes_read_output_level;
334
      this->bytes_read_blob -= c.bytes_read_blob;
335
      this->bytes_written -= c.bytes_written;
336
      this->bytes_written_blob -= c.bytes_written_blob;
337
      this->bytes_moved -= c.bytes_moved;
338 339 340 341 342
      this->num_input_files_in_non_output_levels -=
          c.num_input_files_in_non_output_levels;
      this->num_input_files_in_output_level -=
          c.num_input_files_in_output_level;
      this->num_output_files -= c.num_output_files;
343
      this->num_output_files_blob -= c.num_output_files_blob;
344 345
      this->num_input_records -= c.num_input_records;
      this->num_dropped_records -= c.num_dropped_records;
346
      this->count -= c.count;
347 348 349 350
      int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
      for (int i = 0; i < num_of_reasons; i++) {
        counts[i] -= c.counts[i];
      }
351
    }
I
Igor Canadi 已提交
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
  // For use with CacheEntryStatsCollector
  struct CacheEntryRoleStats {
    uint64_t cache_capacity = 0;
    std::string cache_id;
    std::array<uint64_t, kNumCacheEntryRoles> total_charges;
    std::array<size_t, kNumCacheEntryRoles> entry_counts;
    uint32_t collection_count = 0;
    uint32_t copies_of_last_collection = 0;
    uint64_t last_start_time_micros_ = 0;
    uint64_t last_end_time_micros_ = 0;

    void Clear() {
      // Wipe everything except collection_count
      uint32_t saved_collection_count = collection_count;
      *this = CacheEntryRoleStats();
      collection_count = saved_collection_count;
    }

    void BeginCollection(Cache*, SystemClock*, uint64_t start_time_micros);
    std::function<void(const Slice&, void*, size_t, Cache::DeleterFn)>
    GetEntryCallback();
    void EndCollection(Cache*, SystemClock*, uint64_t end_time_micros);
    void SkippedCollection();

    std::string ToString(SystemClock* clock) const;
    void ToMap(std::map<std::string, std::string>* values,
               SystemClock* clock) const;

   private:
    std::unordered_map<Cache::DeleterFn, CacheEntryRole> role_map_;
    uint64_t GetLastDurationMicros() const;
  };

S
Siying Dong 已提交
387
  void Clear() {
388
    for (int i = 0; i < kIntStatsNumMax; i++) {
S
Siying Dong 已提交
389 390 391 392 393 394 395 396 397 398 399 400
      db_stats_[i].store(0);
    }
    for (int i = 0; i < INTERNAL_CF_STATS_ENUM_MAX; i++) {
      cf_stats_count_[i] = 0;
      cf_stats_value_[i] = 0;
    }
    for (auto& comp_stat : comp_stats_) {
      comp_stat.Clear();
    }
    for (auto& h : file_read_latency_) {
      h.Clear();
    }
401
    blob_file_read_latency_.Clear();
S
Siying Dong 已提交
402 403 404
    cf_stats_snapshot_.Clear();
    db_stats_snapshot_.Clear();
    bg_error_count_ = 0;
405
    started_at_ = clock_->NowMicros();
S
Siying Dong 已提交
406 407
  }

408 409
  void AddCompactionStats(int level, Env::Priority thread_pri,
                          const CompactionStats& stats) {
410
    comp_stats_[level].Add(stats);
411
    comp_stats_by_pri_[thread_pri].Add(stats);
I
Igor Canadi 已提交
412 413
  }

414 415 416 417
  void IncBytesMoved(int level, uint64_t amount) {
    comp_stats_[level].bytes_moved += amount;
  }

418 419 420
  void AddCFStats(InternalCFStatsType type, uint64_t value) {
    cf_stats_value_[type] += value;
    ++cf_stats_count_[type];
I
Igor Canadi 已提交
421 422
  }

423 424
  void AddDBStats(InternalDBStatsType type, uint64_t value,
                  bool concurrent = false) {
425
    auto& v = db_stats_[type];
426 427 428 429 430 431
    if (concurrent) {
      v.fetch_add(value, std::memory_order_relaxed);
    } else {
      v.store(v.load(std::memory_order_relaxed) + value,
              std::memory_order_relaxed);
    }
432 433 434 435
  }

  uint64_t GetDBStats(InternalDBStatsType type) {
    return db_stats_[type].load(std::memory_order_relaxed);
I
Igor Canadi 已提交
436 437
  }

438 439 440 441
  HistogramImpl* GetFileReadHist(int level) {
    return &file_read_latency_[level];
  }

442 443
  HistogramImpl* GetBlobFileReadHist() { return &blob_file_read_latency_; }

444 445 446 447
  uint64_t GetBackgroundErrorCount() const { return bg_error_count_; }

  uint64_t BumpAndGetBackgroundErrorCount() { return ++bg_error_count_; }

448 449 450
  bool GetStringProperty(const DBPropertyInfo& property_info,
                         const Slice& property, std::string* value);

451 452
  bool GetMapProperty(const DBPropertyInfo& property_info,
                      const Slice& property,
453
                      std::map<std::string, std::string>* value);
454

455 456
  bool GetIntProperty(const DBPropertyInfo& property_info, uint64_t* value,
                      DBImpl* db);
I
Igor Canadi 已提交
457

458 459
  bool GetIntPropertyOutOfMutex(const DBPropertyInfo& property_info,
                                Version* version, uint64_t* value);
460

461 462 463 464 465 466 467
  // Unless there is a recent enough collection of the stats, collect and
  // saved new cache entry stats. If `foreground`, require data to be more
  // recent to skip re-collection.
  //
  // This should only be called while NOT holding the DB mutex.
  void CollectCacheEntryStats(bool foreground);

468 469
  const uint64_t* TEST_GetCFStatsValue() const { return cf_stats_value_; }

470 471 472 473
  const std::vector<CompactionStats>& TEST_GetCompactionStats() const {
    return comp_stats_;
  }

474
  void TEST_GetCacheEntryRoleStats(CacheEntryRoleStats* stats, bool foreground);
475

476 477 478
  // Store a mapping from the user-facing DB::Properties string to our
  // DBPropertyInfo struct used internally for retrieving properties.
  static const std::unordered_map<std::string, DBPropertyInfo> ppt_name_to_info;
479

I
Igor Canadi 已提交
480
 private:
481
  void DumpDBStats(std::string* value);
482
  void DumpCFMapStats(std::map<std::string, std::string>* cf_stats);
483
  void DumpCFMapStats(
484
      const VersionStorageInfo* vstorage,
485 486
      std::map<int, std::map<LevelStatType, double>>* level_stats,
      CompactionStats* compaction_stats_sum);
487 488
  void DumpCFMapStatsByPriority(
      std::map<int, std::map<LevelStatType, double>>* priorities_stats);
489
  void DumpCFMapStatsIOStalls(std::map<std::string, std::string>* cf_stats);
490
  void DumpCFStats(std::string* value);
491 492
  void DumpCFStatsNoFileHistogram(std::string* value);
  void DumpCFFileHistogram(std::string* value);
493

494
  bool GetBlockCacheForStats(Cache** block_cache);
495

496
  // Per-DB stats
497
  std::atomic<uint64_t> db_stats_[kIntStatsNumMax];
498
  // Per-ColumnFamily stats
499 500
  uint64_t cf_stats_value_[INTERNAL_CF_STATS_ENUM_MAX];
  uint64_t cf_stats_count_[INTERNAL_CF_STATS_ENUM_MAX];
501 502 503 504 505
  // Initialize/reference the collector in constructor so that we don't need
  // additional synchronization in InternalStats, relying on synchronization
  // in CacheEntryStatsCollector::GetStats. This collector is pinned in cache
  // (through a shared_ptr) so that it does not get immediately ejected from
  // a full cache, which would force a re-scan on the next GetStats.
506 507
  std::shared_ptr<CacheEntryStatsCollector<CacheEntryRoleStats>>
      cache_entry_stats_collector_;
508 509
  // Per-ColumnFamily/level compaction stats
  std::vector<CompactionStats> comp_stats_;
510
  std::vector<CompactionStats> comp_stats_by_pri_;
511
  std::vector<HistogramImpl> file_read_latency_;
512
  HistogramImpl blob_file_read_latency_;
I
Igor Canadi 已提交
513 514

  // Used to compute per-interval statistics
515 516 517
  struct CFStatsSnapshot {
    // ColumnFamily-level stats
    CompactionStats comp_stats;
518
    uint64_t ingest_bytes_flush;      // Bytes written to L0 (Flush)
519
    uint64_t stall_count;             // Stall count
520 521 522 523 524
    // Stats from compaction jobs - bytes written, bytes read, duration.
    uint64_t compact_bytes_write;
    uint64_t compact_bytes_read;
    uint64_t compact_micros;
    double seconds_up;
525

526 527 528 529 530 531
    // AddFile specific stats
    uint64_t ingest_bytes_addfile;     // Total Bytes ingested
    uint64_t ingest_files_addfile;     // Total number of files ingested
    uint64_t ingest_l0_files_addfile;  // Total number of files ingested to L0
    uint64_t ingest_keys_addfile;      // Total number of keys ingested

532
    CFStatsSnapshot()
533
        : ingest_bytes_flush(0),
534 535 536 537
          stall_count(0),
          compact_bytes_write(0),
          compact_bytes_read(0),
          compact_micros(0),
538 539 540 541 542
          seconds_up(0),
          ingest_bytes_addfile(0),
          ingest_files_addfile(0),
          ingest_l0_files_addfile(0),
          ingest_keys_addfile(0) {}
S
Siying Dong 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556

    void Clear() {
      comp_stats.Clear();
      ingest_bytes_flush = 0;
      stall_count = 0;
      compact_bytes_write = 0;
      compact_bytes_read = 0;
      compact_micros = 0;
      seconds_up = 0;
      ingest_bytes_addfile = 0;
      ingest_files_addfile = 0;
      ingest_l0_files_addfile = 0;
      ingest_keys_addfile = 0;
    }
557 558 559 560 561 562 563 564
  } cf_stats_snapshot_;

  struct DBStatsSnapshot {
    // DB-level stats
    uint64_t ingest_bytes;            // Bytes written by user
    uint64_t wal_bytes;               // Bytes written to WAL
    uint64_t wal_synced;              // Number of times WAL is synced
    uint64_t write_with_wal;          // Number of writes that request WAL
I
Igor Canadi 已提交
565 566
    // These count the number of writes processed by the calling thread or
    // another thread.
567 568
    uint64_t write_other;
    uint64_t write_self;
S
sdong 已提交
569 570 571 572 573
    // Total number of keys written. write_self and write_other measure number
    // of write requests written, Each of the write request can contain updates
    // to multiple keys. num_keys_written is total number of keys updated by all
    // those writes.
    uint64_t num_keys_written;
S
sdong 已提交
574 575
    // Total time writes delayed by stalls.
    uint64_t write_stall_micros;
576 577 578 579 580 581 582 583 584
    double seconds_up;

    DBStatsSnapshot()
        : ingest_bytes(0),
          wal_bytes(0),
          wal_synced(0),
          write_with_wal(0),
          write_other(0),
          write_self(0),
585
          num_keys_written(0),
S
sdong 已提交
586
          write_stall_micros(0),
587
          seconds_up(0) {}
S
Siying Dong 已提交
588 589 590 591 592 593 594 595 596 597 598 599

    void Clear() {
      ingest_bytes = 0;
      wal_bytes = 0;
      wal_synced = 0;
      write_with_wal = 0;
      write_other = 0;
      write_self = 0;
      num_keys_written = 0;
      write_stall_micros = 0;
      seconds_up = 0;
    }
600
  } db_stats_snapshot_;
I
Igor Canadi 已提交
601

602 603 604
  // Handler functions for getting property values. They use "value" as a value-
  // result argument, and return true upon successfully setting "value".
  bool HandleNumFilesAtLevel(std::string* value, Slice suffix);
605
  bool HandleCompressionRatioAtLevelPrefix(std::string* value, Slice suffix);
606 607
  bool HandleLevelStats(std::string* value, Slice suffix);
  bool HandleStats(std::string* value, Slice suffix);
608 609
  bool HandleCFMapStats(std::map<std::string, std::string>* compaction_stats,
                        Slice suffix);
610
  bool HandleCFStats(std::string* value, Slice suffix);
611 612
  bool HandleCFStatsNoFileHistogram(std::string* value, Slice suffix);
  bool HandleCFFileHistogram(std::string* value, Slice suffix);
613 614 615 616
  bool HandleDBStats(std::string* value, Slice suffix);
  bool HandleSsTables(std::string* value, Slice suffix);
  bool HandleAggregatedTableProperties(std::string* value, Slice suffix);
  bool HandleAggregatedTablePropertiesAtLevel(std::string* value, Slice suffix);
617 618 619 620
  bool HandleAggregatedTablePropertiesMap(
      std::map<std::string, std::string>* values, Slice suffix);
  bool HandleAggregatedTablePropertiesAtLevelMap(
      std::map<std::string, std::string>* values, Slice suffix);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  bool HandleNumImmutableMemTable(uint64_t* value, DBImpl* db,
                                  Version* version);
  bool HandleNumImmutableMemTableFlushed(uint64_t* value, DBImpl* db,
                                         Version* version);
  bool HandleMemTableFlushPending(uint64_t* value, DBImpl* db,
                                  Version* version);
  bool HandleNumRunningFlushes(uint64_t* value, DBImpl* db, Version* version);
  bool HandleCompactionPending(uint64_t* value, DBImpl* db, Version* version);
  bool HandleNumRunningCompactions(uint64_t* value, DBImpl* db,
                                   Version* version);
  bool HandleBackgroundErrors(uint64_t* value, DBImpl* db, Version* version);
  bool HandleCurSizeActiveMemTable(uint64_t* value, DBImpl* db,
                                   Version* version);
  bool HandleCurSizeAllMemTables(uint64_t* value, DBImpl* db, Version* version);
  bool HandleSizeAllMemTables(uint64_t* value, DBImpl* db, Version* version);
  bool HandleNumEntriesActiveMemTable(uint64_t* value, DBImpl* db,
                                      Version* version);
  bool HandleNumEntriesImmMemTables(uint64_t* value, DBImpl* db,
                                    Version* version);
  bool HandleNumDeletesActiveMemTable(uint64_t* value, DBImpl* db,
                                      Version* version);
  bool HandleNumDeletesImmMemTables(uint64_t* value, DBImpl* db,
                                    Version* version);
  bool HandleEstimateNumKeys(uint64_t* value, DBImpl* db, Version* version);
  bool HandleNumSnapshots(uint64_t* value, DBImpl* db, Version* version);
  bool HandleOldestSnapshotTime(uint64_t* value, DBImpl* db, Version* version);
647 648
  bool HandleOldestSnapshotSequence(uint64_t* value, DBImpl* db,
                                    Version* version);
649
  bool HandleNumLiveVersions(uint64_t* value, DBImpl* db, Version* version);
650 651
  bool HandleCurrentSuperVersionNumber(uint64_t* value, DBImpl* db,
                                       Version* version);
652 653 654 655
  bool HandleIsFileDeletionsEnabled(uint64_t* value, DBImpl* db,
                                    Version* version);
  bool HandleBaseLevel(uint64_t* value, DBImpl* db, Version* version);
  bool HandleTotalSstFilesSize(uint64_t* value, DBImpl* db, Version* version);
656
  bool HandleLiveSstFilesSize(uint64_t* value, DBImpl* db, Version* version);
657 658 659 660 661 662
  bool HandleEstimatePendingCompactionBytes(uint64_t* value, DBImpl* db,
                                            Version* version);
  bool HandleEstimateTableReadersMem(uint64_t* value, DBImpl* db,
                                     Version* version);
  bool HandleEstimateLiveDataSize(uint64_t* value, DBImpl* db,
                                  Version* version);
663
  bool HandleMinLogNumberToKeep(uint64_t* value, DBImpl* db, Version* version);
664 665
  bool HandleMinObsoleteSstNumberToKeep(uint64_t* value, DBImpl* db,
                                        Version* version);
666 667 668
  bool HandleActualDelayedWriteRate(uint64_t* value, DBImpl* db,
                                    Version* version);
  bool HandleIsWriteStopped(uint64_t* value, DBImpl* db, Version* version);
Y
Yi Wu 已提交
669 670
  bool HandleEstimateOldestKeyTime(uint64_t* value, DBImpl* db,
                                   Version* version);
Y
Yi Wu 已提交
671 672 673 674
  bool HandleBlockCacheCapacity(uint64_t* value, DBImpl* db, Version* version);
  bool HandleBlockCacheUsage(uint64_t* value, DBImpl* db, Version* version);
  bool HandleBlockCachePinnedUsage(uint64_t* value, DBImpl* db,
                                   Version* version);
675 676 677
  bool HandleBlockCacheEntryStats(std::string* value, Slice suffix);
  bool HandleBlockCacheEntryStatsMap(std::map<std::string, std::string>* values,
                                     Slice suffix);
678
  bool HandleLiveSstFilesSizeAtTemperature(std::string* value, Slice suffix);
679 680 681 682 683 684 685
  // Total number of background errors encountered. Every time a flush task
  // or compaction task fails, this counter is incremented. The failure can
  // be caused by any possible reason, including file system errors, out of
  // resources, or input file corruption. Failing when retrying the same flush
  // or compaction will cause the counter to increase too.
  uint64_t bg_error_count_;

686
  const int number_levels_;
687
  SystemClock* clock_;
688
  ColumnFamilyData* cfd_;
S
Siying Dong 已提交
689
  uint64_t started_at_;
I
Igor Canadi 已提交
690 691
};

692 693 694 695 696
#else

class InternalStats {
 public:
  enum InternalCFStatsType {
697 698 699 700 701 702 703 704
    L0_FILE_COUNT_LIMIT_SLOWDOWNS,
    LOCKED_L0_FILE_COUNT_LIMIT_SLOWDOWNS,
    MEMTABLE_LIMIT_STOPS,
    MEMTABLE_LIMIT_SLOWDOWNS,
    L0_FILE_COUNT_LIMIT_STOPS,
    LOCKED_L0_FILE_COUNT_LIMIT_STOPS,
    PENDING_COMPACTION_BYTES_LIMIT_SLOWDOWNS,
    PENDING_COMPACTION_BYTES_LIMIT_STOPS,
705 706
    WRITE_STALLS_ENUM_MAX,
    BYTES_FLUSHED,
707
    BYTES_INGESTED_ADD_FILE,
708 709 710
    INGESTED_NUM_FILES_TOTAL,
    INGESTED_LEVEL0_NUM_FILES_TOTAL,
    INGESTED_NUM_KEYS_TOTAL,
711 712 713 714
    INTERNAL_CF_STATS_ENUM_MAX,
  };

  enum InternalDBStatsType {
715 716 717 718 719 720 721 722 723
    kIntStatsWalFileBytes,
    kIntStatsWalFileSynced,
    kIntStatsBytesWritten,
    kIntStatsNumKeysWritten,
    kIntStatsWriteDoneByOther,
    kIntStatsWriteDoneBySelf,
    kIntStatsWriteWithWal,
    kIntStatsWriteStallMicros,
    kIntStatsNumMax,
724 725
  };

726
  InternalStats(int /*num_levels*/, SystemClock* /*clock*/,
727
                ColumnFamilyData* /*cfd*/) {}
728 729 730

  struct CompactionStats {
    uint64_t micros;
731
    uint64_t cpu_micros;
732 733
    uint64_t bytes_read_non_output_levels;
    uint64_t bytes_read_output_level;
734
    uint64_t bytes_read_blob;
735
    uint64_t bytes_written;
736
    uint64_t bytes_written_blob;
737
    uint64_t bytes_moved;
738 739 740
    int num_input_files_in_non_output_levels;
    int num_input_files_in_output_level;
    int num_output_files;
741
    int num_output_files_blob;
742 743 744 745
    uint64_t num_input_records;
    uint64_t num_dropped_records;
    int count;

746 747
    explicit CompactionStats() {}

748
    explicit CompactionStats(CompactionReason /*reason*/, int /*c*/) {}
749

750
    explicit CompactionStats(const CompactionStats& /*c*/) {}
751

752
    void Add(const CompactionStats& /*c*/) {}
753

754
    void Subtract(const CompactionStats& /*c*/) {}
755 756
  };

757 758
  void AddCompactionStats(int /*level*/, Env::Priority /*thread_pri*/,
                          const CompactionStats& /*stats*/) {}
759

760
  void IncBytesMoved(int /*level*/, uint64_t /*amount*/) {}
761

762
  void AddCFStats(InternalCFStatsType /*type*/, uint64_t /*value*/) {}
763

764 765
  void AddDBStats(InternalDBStatsType /*type*/, uint64_t /*value*/,
                  bool /*concurrent */ = false) {}
766

767
  HistogramImpl* GetFileReadHist(int /*level*/) { return nullptr; }
768

769 770
  HistogramImpl* GetBlobFileReadHist() { return nullptr; }

771 772 773 774
  uint64_t GetBackgroundErrorCount() const { return 0; }

  uint64_t BumpAndGetBackgroundErrorCount() { return 0; }

775 776
  bool GetStringProperty(const DBPropertyInfo& /*property_info*/,
                         const Slice& /*property*/, std::string* /*value*/) {
777 778
    return false;
  }
779

780 781 782
  bool GetMapProperty(const DBPropertyInfo& /*property_info*/,
                      const Slice& /*property*/,
                      std::map<std::string, std::string>* /*value*/) {
783 784 785
    return false;
  }

786 787
  bool GetIntProperty(const DBPropertyInfo& /*property_info*/, uint64_t* /*value*/,
                      DBImpl* /*db*/) const {
788 789
    return false;
  }
790

791 792
  bool GetIntPropertyOutOfMutex(const DBPropertyInfo& /*property_info*/,
                                Version* /*version*/, uint64_t* /*value*/) const {
793 794
    return false;
  }
795 796 797
};
#endif  // !ROCKSDB_LITE

798
}  // namespace ROCKSDB_NAMESPACE