internal_stats.h 14.8 KB
Newer Older
I
Igor Canadi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  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.
//

#pragma once
#include "db/version_set.h"

#include <vector>
#include <string>

17 18
class ColumnFamilyData;

I
Igor Canadi 已提交
19
namespace rocksdb {
20 21

class MemTableList;
22
class DBImpl;
23

24 25
// IMPORTANT: If you add a new property here, also add it to the list in
//            include/rocksdb/db.h
26 27
enum DBPropertyType : uint32_t {
  kUnknown,
28 29
  kNumFilesAtLevel,  // Number of files at a specific level
  kLevelStats,       // Return number of files and total sizes of each level
30 31 32
  kCFStats,          // Return general statitistics of CF
  kDBStats,          // Return general statitistics of DB
  kStats,            // Return general statitistics of both DB and CF
33
  kSsTables,         // Return a human readable string of current SST files
34
  kStartIntTypes,    // ---- Dummy value to indicate the start of integer values
35 36 37 38 39 40
  kNumImmutableMemTable,         // Return number of immutable mem tables that
                                 // have not been flushed.
  kNumImmutableMemTableFlushed,  // Return number of immutable mem tables
                                 // in memory that have already been flushed
  kMemtableFlushPending,         // Return 1 if mem table flushing is pending,
                                 // otherwise 0.
41
  kNumRunningFlushes,      // Return the number of currently running flushes.
42
  kCompactionPending,      // Return 1 if a compaction is pending. Otherwise 0.
43 44
  kNumRunningCompactions,  // Return the number of currently running
                           // compactions.
45
  kBackgroundErrors,       // Return accumulated background errors encountered.
46
  kCurSizeActiveMemTable,  // Return current size of the active memtable
47 48 49 50
  kCurSizeAllMemTables,    // Return current size of unflushed
                           // (active + immutable) memtables
  kSizeAllMemTables,       // Return current size of all (active + immutable
                           // + pinned) memtables
51
  kNumEntriesInMutableMemtable,    // Return number of deletes in the mutable
52 53 54
                                   // memtable.
  kNumEntriesInImmutableMemtable,  // Return sum of number of entries in all
                                   // the immutable mem tables.
55 56 57 58
  kNumDeletesInMutableMemtable,    // Return number of deletion entries in the
                                   // mutable memtable.
  kNumDeletesInImmutableMemtable,  // Return the total number of deletion
                                   // entries in all the immutable mem tables.
S
sdong 已提交
59
  kEstimatedNumKeys,  // Estimated total number of keys in the database.
60
  kEstimatedUsageByTableReaders,  // Estimated memory by table readers.
61 62
  kIsFileDeletionEnabled,         // Equals disable_delete_obsolete_files_,
                                  // 0 means file deletions enabled
63 64
  kNumSnapshots,                  // Number of snapshots in the system
  kOldestSnapshotTime,            // Unix timestamp of the first snapshot
65
  kNumLiveVersions,
66 67 68 69
  kEstimateLiveDataSize,            // Estimated amount of live data in bytes
  kTotalSstFilesSize,               // Total size of all sst files.
  kBaseLevel,                       // The level that L0 data is compacted to
  kEstimatePendingCompactionBytes,  // Estimated bytes to compaction
70 71 72 73 74
  kAggregatedTableProperties,  // Return a string that contains the aggregated
                               // table properties.
  kAggregatedTablePropertiesAtLevel,  // Return a string that contains the
                                      // aggregated
  // table properties at the specified level.
75 76
};

77 78 79
extern DBPropertyType GetPropertyType(const Slice& property,
                                      bool* is_int_property,
                                      bool* need_out_of_mutex);
80

81 82

#ifndef ROCKSDB_LITE
I
Igor Canadi 已提交
83 84
class InternalStats {
 public:
85
  enum InternalCFStatsType {
86 87
    LEVEL0_SLOWDOWN_TOTAL,
    LEVEL0_SLOWDOWN_WITH_COMPACTION,
I
Igor Canadi 已提交
88
    MEMTABLE_COMPACTION,
89
    MEMTABLE_SLOWDOWN,
90 91
    LEVEL0_NUM_FILES_TOTAL,
    LEVEL0_NUM_FILES_WITH_COMPACTION,
92
    SOFT_PENDING_COMPACTION_BYTES_LIMIT,
93
    HARD_PENDING_COMPACTION_BYTES_LIMIT,
I
Igor Canadi 已提交
94
    WRITE_STALLS_ENUM_MAX,
95 96
    BYTES_FLUSHED,
    INTERNAL_CF_STATS_ENUM_MAX,
I
Igor Canadi 已提交
97 98
  };

99 100 101 102
  enum InternalDBStatsType {
    WAL_FILE_BYTES,
    WAL_FILE_SYNCED,
    BYTES_WRITTEN,
S
sdong 已提交
103
    NUMBER_KEYS_WRITTEN,
104 105 106
    WRITE_DONE_BY_OTHER,
    WRITE_DONE_BY_SELF,
    WRITE_WITH_WAL,
S
sdong 已提交
107
    WRITE_STALL_MICROS,
108 109 110
    INTERNAL_DB_STATS_ENUM_MAX,
  };

111
  InternalStats(int num_levels, Env* env, ColumnFamilyData* cfd)
112 113 114 115
      : db_stats_(INTERNAL_DB_STATS_ENUM_MAX),
        cf_stats_value_(INTERNAL_CF_STATS_ENUM_MAX),
        cf_stats_count_(INTERNAL_CF_STATS_ENUM_MAX),
        comp_stats_(num_levels),
116
        file_read_latency_(num_levels),
117
        bg_error_count_(0),
I
Igor Canadi 已提交
118 119
        number_levels_(num_levels),
        env_(env),
120
        cfd_(cfd),
121 122 123 124 125 126 127 128 129
        started_at_(env->NowMicros()) {
    for (int i = 0; i< INTERNAL_DB_STATS_ENUM_MAX; ++i) {
      db_stats_[i] = 0;
    }
    for (int i = 0; i< INTERNAL_CF_STATS_ENUM_MAX; ++i) {
      cf_stats_value_[i] = 0;
      cf_stats_count_[i] = 0;
    }
  }
I
Igor Canadi 已提交
130

131
  // Per level compaction stats.  comp_stats_[level] stores the stats for
I
Igor Canadi 已提交
132 133 134 135
  // compactions that produced data for the specified "level".
  struct CompactionStats {
    uint64_t micros;

136 137
    // The number of bytes read from all non-output levels
    uint64_t bytes_read_non_output_levels;
I
Igor Canadi 已提交
138

139 140
    // The number of bytes read from the compaction output level.
    uint64_t bytes_read_output_level;
I
Igor Canadi 已提交
141

142
    // Total number of bytes written during compaction
143
    uint64_t bytes_written;
I
Igor Canadi 已提交
144

145
    // Total number of bytes moved to the output level
146 147
    uint64_t bytes_moved;

148 149
    // The number of compaction input files in all non-output levels.
    int num_input_files_in_non_output_levels;
I
Igor Canadi 已提交
150

151 152
    // The number of compaction input files in the output level.
    int num_input_files_in_output_level;
I
Igor Canadi 已提交
153

154 155
    // The number of compaction output files.
    int num_output_files;
I
Igor Canadi 已提交
156

157
    // Total incoming entries during compaction between levels N and N+1
S
sdong 已提交
158
    uint64_t num_input_records;
159 160 161

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

I
Igor Canadi 已提交
164 165 166
    // Number of compactions done
    int count;

I
Igor Canadi 已提交
167
    explicit CompactionStats(int _count = 0)
I
Igor Canadi 已提交
168
        : micros(0),
169 170
          bytes_read_non_output_levels(0),
          bytes_read_output_level(0),
I
Igor Canadi 已提交
171
          bytes_written(0),
172
          bytes_moved(0),
173 174 175
          num_input_files_in_non_output_levels(0),
          num_input_files_in_output_level(0),
          num_output_files(0),
176 177
          num_input_records(0),
          num_dropped_records(0),
I
Igor Canadi 已提交
178
          count(_count) {}
I
Igor Canadi 已提交
179

180 181
    explicit CompactionStats(const CompactionStats& c)
        : micros(c.micros),
182 183
          bytes_read_non_output_levels(c.bytes_read_non_output_levels),
          bytes_read_output_level(c.bytes_read_output_level),
184
          bytes_written(c.bytes_written),
185
          bytes_moved(c.bytes_moved),
186 187 188 189 190
          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),
191 192
          num_input_records(c.num_input_records),
          num_dropped_records(c.num_dropped_records),
193 194
          count(c.count) {}

I
Igor Canadi 已提交
195 196
    void Add(const CompactionStats& c) {
      this->micros += c.micros;
197 198
      this->bytes_read_non_output_levels += c.bytes_read_non_output_levels;
      this->bytes_read_output_level += c.bytes_read_output_level;
I
Igor Canadi 已提交
199
      this->bytes_written += c.bytes_written;
200
      this->bytes_moved += c.bytes_moved;
201 202 203 204 205
      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;
206 207
      this->num_input_records += c.num_input_records;
      this->num_dropped_records += c.num_dropped_records;
L
Lei Jin 已提交
208
      this->count += c.count;
I
Igor Canadi 已提交
209
    }
210 211 212

    void Subtract(const CompactionStats& c) {
      this->micros -= c.micros;
213 214
      this->bytes_read_non_output_levels -= c.bytes_read_non_output_levels;
      this->bytes_read_output_level -= c.bytes_read_output_level;
215
      this->bytes_written -= c.bytes_written;
216
      this->bytes_moved -= c.bytes_moved;
217 218 219 220 221
      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;
222 223
      this->num_input_records -= c.num_input_records;
      this->num_dropped_records -= c.num_dropped_records;
224 225
      this->count -= c.count;
    }
I
Igor Canadi 已提交
226 227 228
  };

  void AddCompactionStats(int level, const CompactionStats& stats) {
229
    comp_stats_[level].Add(stats);
I
Igor Canadi 已提交
230 231
  }

232 233 234 235
  void IncBytesMoved(int level, uint64_t amount) {
    comp_stats_[level].bytes_moved += amount;
  }

236 237 238
  void AddCFStats(InternalCFStatsType type, uint64_t value) {
    cf_stats_value_[type] += value;
    ++cf_stats_count_[type];
I
Igor Canadi 已提交
239 240
  }

241 242
  void AddDBStats(InternalDBStatsType type, uint64_t value) {
    db_stats_[type] += value;
I
Igor Canadi 已提交
243 244
  }

245 246 247 248
  HistogramImpl* GetFileReadHist(int level) {
    return &file_read_latency_[level];
  }

249 250 251 252
  uint64_t GetBackgroundErrorCount() const { return bg_error_count_; }

  uint64_t BumpAndGetBackgroundErrorCount() { return ++bg_error_count_; }

253 254
  bool GetStringProperty(DBPropertyType property_type, const Slice& property,
                         std::string* value);
I
Igor Canadi 已提交
255

256 257
  bool GetIntProperty(DBPropertyType property_type, uint64_t* value,
                      DBImpl* db) const;
258 259 260

  bool GetIntPropertyOutOfMutex(DBPropertyType property_type, Version* version,
                                uint64_t* value) const;
261

I
Igor Canadi 已提交
262
 private:
263
  void DumpDBStats(std::string* value);
264
  void DumpCFStats(std::string* value);
265 266 267 268 269 270 271 272

  // Per-DB stats
  std::vector<uint64_t> db_stats_;
  // Per-ColumnFamily stats
  std::vector<uint64_t> cf_stats_value_;
  std::vector<uint64_t> cf_stats_count_;
  // Per-ColumnFamily/level compaction stats
  std::vector<CompactionStats> comp_stats_;
273
  std::vector<HistogramImpl> file_read_latency_;
I
Igor Canadi 已提交
274 275

  // Used to compute per-interval statistics
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  struct CFStatsSnapshot {
    // ColumnFamily-level stats
    CompactionStats comp_stats;
    uint64_t ingest_bytes;            // Bytes written to L0
    uint64_t stall_count;             // Stall count

    CFStatsSnapshot()
        : comp_stats(0),
          ingest_bytes(0),
          stall_count(0) {}
  } 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 已提交
294 295
    // These count the number of writes processed by the calling thread or
    // another thread.
296 297
    uint64_t write_other;
    uint64_t write_self;
298 299 300 301
    // Stats from compaction jobs - bytes written, bytes read, duration.
    uint64_t compact_bytes_write;
    uint64_t compact_bytes_read;
    uint64_t compact_micros;
S
sdong 已提交
302 303 304 305 306
    // 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 已提交
307 308
    // Total time writes delayed by stalls.
    uint64_t write_stall_micros;
309 310 311 312 313 314 315 316 317
    double seconds_up;

    DBStatsSnapshot()
        : ingest_bytes(0),
          wal_bytes(0),
          wal_synced(0),
          write_with_wal(0),
          write_other(0),
          write_self(0),
318 319 320
          compact_bytes_write(0),
          compact_bytes_read(0),
          compact_micros(0),
321
          num_keys_written(0),
S
sdong 已提交
322
          write_stall_micros(0),
323 324
          seconds_up(0) {}
  } db_stats_snapshot_;
I
Igor Canadi 已提交
325

326 327 328 329 330 331 332
  // 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_;

333
  const int number_levels_;
I
Igor Canadi 已提交
334
  Env* env_;
335
  ColumnFamilyData* cfd_;
336
  const uint64_t started_at_;
I
Igor Canadi 已提交
337 338
};

339 340 341 342 343
#else

class InternalStats {
 public:
  enum InternalCFStatsType {
344 345
    LEVEL0_SLOWDOWN_TOTAL,
    LEVEL0_SLOWDOWN_WITH_COMPACTION,
346
    MEMTABLE_COMPACTION,
347
    MEMTABLE_SLOWDOWN,
348 349
    LEVEL0_NUM_FILES_TOTAL,
    LEVEL0_NUM_FILES_WITH_COMPACTION,
350
    SOFT_PENDING_COMPACTION_BYTES_LIMIT,
351
    HARD_PENDING_COMPACTION_BYTES_LIMIT,
352 353 354 355 356 357 358 359 360 361 362 363 364
    WRITE_STALLS_ENUM_MAX,
    BYTES_FLUSHED,
    INTERNAL_CF_STATS_ENUM_MAX,
  };

  enum InternalDBStatsType {
    WAL_FILE_BYTES,
    WAL_FILE_SYNCED,
    BYTES_WRITTEN,
    NUMBER_KEYS_WRITTEN,
    WRITE_DONE_BY_OTHER,
    WRITE_DONE_BY_SELF,
    WRITE_WITH_WAL,
S
sdong 已提交
365
    WRITE_STALL_MICROS,
366 367 368 369 370 371 372
    INTERNAL_DB_STATS_ENUM_MAX,
  };

  InternalStats(int num_levels, Env* env, ColumnFamilyData* cfd) {}

  struct CompactionStats {
    uint64_t micros;
373 374
    uint64_t bytes_read_non_output_levels;
    uint64_t bytes_read_output_level;
375
    uint64_t bytes_written;
376
    uint64_t bytes_moved;
377 378 379
    int num_input_files_in_non_output_levels;
    int num_input_files_in_output_level;
    int num_output_files;
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    uint64_t num_input_records;
    uint64_t num_dropped_records;
    int count;

    explicit CompactionStats(int _count = 0) {}

    explicit CompactionStats(const CompactionStats& c) {}

    void Add(const CompactionStats& c) {}

    void Subtract(const CompactionStats& c) {}
  };

  void AddCompactionStats(int level, const CompactionStats& stats) {}

395 396
  void IncBytesMoved(int level, uint64_t amount) {}

397 398 399 400
  void AddCFStats(InternalCFStatsType type, uint64_t value) {}

  void AddDBStats(InternalDBStatsType type, uint64_t value) {}

401 402
  HistogramImpl* GetFileReadHist(int level) { return nullptr; }

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  uint64_t GetBackgroundErrorCount() const { return 0; }

  uint64_t BumpAndGetBackgroundErrorCount() { return 0; }

  bool GetStringProperty(DBPropertyType property_type, const Slice& property,
                         std::string* value) { return false; }

  bool GetIntProperty(DBPropertyType property_type, uint64_t* value,
                      DBImpl* db) const { return false; }

  bool GetIntPropertyOutOfMutex(DBPropertyType property_type, Version* version,
                                uint64_t* value) const { return false; }
};
#endif  // !ROCKSDB_LITE

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