internal_stats.h 12.7 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
enum DBPropertyType : uint32_t {
  kUnknown,
26 27
  kNumFilesAtLevel,  // Number of files at a specific level
  kLevelStats,       // Return number of files and total sizes of each level
28 29 30
  kCFStats,          // Return general statitistics of CF
  kDBStats,          // Return general statitistics of DB
  kStats,            // Return general statitistics of both DB and CF
31
  kSsTables,         // Return a human readable string of current SST files
32
  kStartIntTypes,    // ---- Dummy value to indicate the start of integer values
33 34 35 36 37
  kNumImmutableMemTable,   // Return number of immutable mem tables
  kMemtableFlushPending,   // Return 1 if mem table flushing is pending,
                           // otherwise 0.
  kCompactionPending,      // Return 1 if a compaction is pending. Otherwise 0.
  kBackgroundErrors,       // Return accumulated background errors encountered.
38
  kCurSizeActiveMemTable,  // Return current size of the active memtable
I
Igor Canadi 已提交
39 40
  kCurSizeAllMemTables,    // Return current size of all (active + immutable)
                           // memtables
41 42 43 44
  kNumEntriesInMutableMemtable,    // Return number of entries in the mutable
                                   // memtable.
  kNumEntriesInImmutableMemtable,  // Return sum of number of entries in all
                                   // the immutable mem tables.
S
sdong 已提交
45
  kEstimatedNumKeys,  // Estimated total number of keys in the database.
46
  kEstimatedUsageByTableReaders,  // Estimated memory by table readers.
47 48
  kIsFileDeletionEnabled,         // Equals disable_delete_obsolete_files_,
                                  // 0 means file deletions enabled
49 50
};

51 52 53
extern DBPropertyType GetPropertyType(const Slice& property,
                                      bool* is_int_property,
                                      bool* need_out_of_mutex);
54

55 56

#ifndef ROCKSDB_LITE
I
Igor Canadi 已提交
57 58
class InternalStats {
 public:
59
  enum InternalCFStatsType {
I
Igor Canadi 已提交
60 61 62 63
    LEVEL0_SLOWDOWN,
    MEMTABLE_COMPACTION,
    LEVEL0_NUM_FILES,
    WRITE_STALLS_ENUM_MAX,
64 65
    BYTES_FLUSHED,
    INTERNAL_CF_STATS_ENUM_MAX,
I
Igor Canadi 已提交
66 67
  };

68 69 70 71
  enum InternalDBStatsType {
    WAL_FILE_BYTES,
    WAL_FILE_SYNCED,
    BYTES_WRITTEN,
S
sdong 已提交
72
    NUMBER_KEYS_WRITTEN,
73 74 75 76 77 78
    WRITE_DONE_BY_OTHER,
    WRITE_DONE_BY_SELF,
    WRITE_WITH_WAL,
    INTERNAL_DB_STATS_ENUM_MAX,
  };

79
  InternalStats(int num_levels, Env* env, ColumnFamilyData* cfd)
80 81 82 83 84 85 86 87
      : 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),
        stall_leveln_slowdown_hard_(num_levels),
        stall_leveln_slowdown_count_hard_(num_levels),
        stall_leveln_slowdown_soft_(num_levels),
        stall_leveln_slowdown_count_soft_(num_levels),
88
        bg_error_count_(0),
I
Igor Canadi 已提交
89 90
        number_levels_(num_levels),
        env_(env),
91
        cfd_(cfd),
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        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;
    }
    for (int i = 0; i < num_levels; ++i) {
      stall_leveln_slowdown_hard_[i] = 0;
      stall_leveln_slowdown_count_hard_[i] = 0;
      stall_leveln_slowdown_soft_[i] = 0;
      stall_leveln_slowdown_count_soft_[i] = 0;
    }
  }
I
Igor Canadi 已提交
107

108
  // Per level compaction stats.  comp_stats_[level] stores the stats for
I
Igor Canadi 已提交
109 110 111 112 113
  // compactions that produced data for the specified "level".
  struct CompactionStats {
    uint64_t micros;

    // Bytes read from level N during compaction between levels N and N+1
114
    uint64_t bytes_readn;
I
Igor Canadi 已提交
115 116

    // Bytes read from level N+1 during compaction between levels N and N+1
117
    uint64_t bytes_readnp1;
I
Igor Canadi 已提交
118 119

    // Total bytes written during compaction between levels N and N+1
120
    uint64_t bytes_written;
I
Igor Canadi 已提交
121

122 123 124
    // Total bytes moved to this level
    uint64_t bytes_moved;

I
Igor Canadi 已提交
125 126 127 128 129 130 131 132 133
    // Files read from level N during compaction between levels N and N+1
    int files_in_leveln;

    // Files read from level N+1 during compaction between levels N and N+1
    int files_in_levelnp1;

    // Files written during compaction between levels N and N+1
    int files_out_levelnp1;

134
    // Total incoming entries during compaction between levels N and N+1
S
sdong 已提交
135
    uint64_t num_input_records;
136 137 138

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

I
Igor Canadi 已提交
141 142 143
    // Number of compactions done
    int count;

I
Igor Canadi 已提交
144
    explicit CompactionStats(int _count = 0)
I
Igor Canadi 已提交
145 146 147 148
        : micros(0),
          bytes_readn(0),
          bytes_readnp1(0),
          bytes_written(0),
149
          bytes_moved(0),
I
Igor Canadi 已提交
150 151 152
          files_in_leveln(0),
          files_in_levelnp1(0),
          files_out_levelnp1(0),
153 154
          num_input_records(0),
          num_dropped_records(0),
I
Igor Canadi 已提交
155
          count(_count) {}
I
Igor Canadi 已提交
156

157 158 159 160 161
    explicit CompactionStats(const CompactionStats& c)
        : micros(c.micros),
          bytes_readn(c.bytes_readn),
          bytes_readnp1(c.bytes_readnp1),
          bytes_written(c.bytes_written),
162
          bytes_moved(c.bytes_moved),
163 164 165
          files_in_leveln(c.files_in_leveln),
          files_in_levelnp1(c.files_in_levelnp1),
          files_out_levelnp1(c.files_out_levelnp1),
166 167
          num_input_records(c.num_input_records),
          num_dropped_records(c.num_dropped_records),
168 169
          count(c.count) {}

I
Igor Canadi 已提交
170 171 172 173 174
    void Add(const CompactionStats& c) {
      this->micros += c.micros;
      this->bytes_readn += c.bytes_readn;
      this->bytes_readnp1 += c.bytes_readnp1;
      this->bytes_written += c.bytes_written;
175
      this->bytes_moved += c.bytes_moved;
I
Igor Canadi 已提交
176 177 178
      this->files_in_leveln += c.files_in_leveln;
      this->files_in_levelnp1 += c.files_in_levelnp1;
      this->files_out_levelnp1 += c.files_out_levelnp1;
179 180
      this->num_input_records += c.num_input_records;
      this->num_dropped_records += c.num_dropped_records;
L
Lei Jin 已提交
181
      this->count += c.count;
I
Igor Canadi 已提交
182
    }
183 184 185 186 187 188

    void Subtract(const CompactionStats& c) {
      this->micros -= c.micros;
      this->bytes_readn -= c.bytes_readn;
      this->bytes_readnp1 -= c.bytes_readnp1;
      this->bytes_written -= c.bytes_written;
189
      this->bytes_moved -= c.bytes_moved;
190 191 192
      this->files_in_leveln -= c.files_in_leveln;
      this->files_in_levelnp1 -= c.files_in_levelnp1;
      this->files_out_levelnp1 -= c.files_out_levelnp1;
193 194
      this->num_input_records -= c.num_input_records;
      this->num_dropped_records -= c.num_dropped_records;
195 196
      this->count -= c.count;
    }
I
Igor Canadi 已提交
197 198 199
  };

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

203 204 205 206
  void IncBytesMoved(int level, uint64_t amount) {
    comp_stats_[level].bytes_moved += amount;
  }

207 208 209 210 211 212 213 214 215 216 217 218 219
  void RecordLevelNSlowdown(int level, uint64_t micros, bool soft) {
    if (soft) {
      stall_leveln_slowdown_soft_[level] += micros;
      ++stall_leveln_slowdown_count_soft_[level];
    } else {
      stall_leveln_slowdown_hard_[level] += micros;
      ++stall_leveln_slowdown_count_hard_[level];
    }
  }

  void AddCFStats(InternalCFStatsType type, uint64_t value) {
    cf_stats_value_[type] += value;
    ++cf_stats_count_[type];
I
Igor Canadi 已提交
220 221
  }

222 223
  void AddDBStats(InternalDBStatsType type, uint64_t value) {
    db_stats_[type] += value;
I
Igor Canadi 已提交
224 225
  }

226 227 228 229
  uint64_t GetBackgroundErrorCount() const { return bg_error_count_; }

  uint64_t BumpAndGetBackgroundErrorCount() { return ++bg_error_count_; }

230 231
  bool GetStringProperty(DBPropertyType property_type, const Slice& property,
                         std::string* value);
I
Igor Canadi 已提交
232

233 234
  bool GetIntProperty(DBPropertyType property_type, uint64_t* value,
                      DBImpl* db) const;
235 236 237

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

I
Igor Canadi 已提交
239
 private:
240
  void DumpDBStats(std::string* value);
241
  void DumpCFStats(std::string* value);
242 243 244 245 246 247 248 249 250 251 252 253 254

  // 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_;
  // These count the number of microseconds for which MakeRoomForWrite stalls.
  std::vector<uint64_t> stall_leveln_slowdown_hard_;
  std::vector<uint64_t> stall_leveln_slowdown_count_hard_;
  std::vector<uint64_t> stall_leveln_slowdown_soft_;
  std::vector<uint64_t> stall_leveln_slowdown_count_soft_;
I
Igor Canadi 已提交
255 256

  // Used to compute per-interval statistics
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  struct CFStatsSnapshot {
    // ColumnFamily-level stats
    CompactionStats comp_stats;
    uint64_t ingest_bytes;            // Bytes written to L0
    uint64_t stall_us;                // Stall time in micro-seconds
    uint64_t stall_count;             // Stall count

    CFStatsSnapshot()
        : comp_stats(0),
          ingest_bytes(0),
          stall_us(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 已提交
277 278
    // These count the number of writes processed by the calling thread or
    // another thread.
279 280
    uint64_t write_other;
    uint64_t write_self;
S
sdong 已提交
281 282 283 284 285
    // 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;
286 287 288 289 290 291 292 293 294
    double seconds_up;

    DBStatsSnapshot()
        : ingest_bytes(0),
          wal_bytes(0),
          wal_synced(0),
          write_with_wal(0),
          write_other(0),
          write_self(0),
295
          num_keys_written(0),
296 297
          seconds_up(0) {}
  } db_stats_snapshot_;
I
Igor Canadi 已提交
298

299 300 301 302 303 304 305
  // 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_;

306
  const int number_levels_;
I
Igor Canadi 已提交
307
  Env* env_;
308
  ColumnFamilyData* cfd_;
309
  const uint64_t started_at_;
I
Igor Canadi 已提交
310 311
};

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
#else

class InternalStats {
 public:
  enum InternalCFStatsType {
    LEVEL0_SLOWDOWN,
    MEMTABLE_COMPACTION,
    LEVEL0_NUM_FILES,
    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,
    INTERNAL_DB_STATS_ENUM_MAX,
  };

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

  struct CompactionStats {
    uint64_t micros;
    uint64_t bytes_readn;
    uint64_t bytes_readnp1;
    uint64_t bytes_written;
343
    uint64_t bytes_moved;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    int files_in_leveln;
    int files_in_levelnp1;
    int files_out_levelnp1;
    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) {}

362 363
  void IncBytesMoved(int level, uint64_t amount) {}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  void RecordLevelNSlowdown(int level, uint64_t micros, bool soft) {}

  void AddCFStats(InternalCFStatsType type, uint64_t value) {}

  void AddDBStats(InternalDBStatsType type, uint64_t value) {}

  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 已提交
385
}  // namespace rocksdb