statistics.h 3.8 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef STORAGE_LEVELDB_INCLUDE_STATISTICS_H_
#define STORAGE_LEVELDB_INCLUDE_STATISTICS_H_

A
Abhishek Kona 已提交
8
#include <atomic>
9 10 11 12 13
#include <cstddef>
#include <cstdint>
#include <string>
#include <memory>

14 15
namespace leveldb {

A
Abhishek Kona 已提交
16 17 18 19 20 21 22 23 24
/**
 * Keep adding ticker's here.
 * Any ticker should have a value less than TICKER_ENUM_MAX.
 * Add a new ticker by assigning it the current value of TICKER_ENUM_MAX
 * And incrementing TICKER_ENUM_MAX.
 */
enum Tickers {
  BLOCK_CACHE_MISS = 0,
  BLOCK_CACHE_HIT = 1,
25 26 27 28 29 30 31 32
  BLOOM_FILTER_USEFUL = 2, // no. of times bloom filter has avoided file reads.
  /**
   * COMPACTION_KEY_DROP_* count the reasons for key drop during compaction
   * There are 3 reasons currently.
   */
  COMPACTION_KEY_DROP_NEWER_ENTRY = 3, // key was written with a newer value.
  COMPACTION_KEY_DROP_OBSOLETE = 4, // The key is obsolete.
  COMPACTION_KEY_DROP_USER = 5, // user compaction function has dropped the key.
33 34 35 36
  // Number of keys written to the database via the Put and Write call's
  NUMBER_KEYS_WRITTEN = 6,
  // Number of Keys read,
  NUMBER_KEYS_READ = 7,
37 38 39
  // Bytes written / read
  BYTES_WRITTEN = 8,
  BYTES_READ = 9,
40 41 42
  NO_FILE_CLOSES = 10,
  NO_FILE_OPENS = 11,
  NO_FILE_ERRORS = 12,
43 44 45 46 47 48 49 50
  // Time system had to wait to do LO-L1 compactions
  STALL_L0_SLOWDOWN_MICROS = 13,
  // Time system had to wait to move memtable to L1.
  STALL_MEMTABLE_COMPACTION_MICROS = 14,
  // write throttle because of too many files in L0
  STALL_L0_NUM_FILES_MICROS = 15,
  RATE_LIMIT_DELAY_MILLIS = 16,
  TICKER_ENUM_MAX = 17
A
Abhishek Kona 已提交
51 52
};

53

54 55 56 57 58 59 60 61 62
/**
 * Keep adding histogram's here.
 * Any histogram whould have value less than HISTOGRAM_ENUM_MAX
 * Add a new Histogram by assigning it the current value of HISTOGRAM_ENUM_MAX
 * And increment HISTOGRAM_ENUM_MAX
 */
enum Histograms {
  DB_GET = 0,
  DB_WRITE = 1,
A
Abhishek Kona 已提交
63 64
  COMPACTION_TIME = 2,
  HISTOGRAM_ENUM_MAX = 3,
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
};

struct HistogramData {
  double median;
  double percentile95;
  double percentile99;
  double average;
  double standard_deviation;
};


class Histogram {
 public:
  // clear's the histogram
  virtual void Clear() = 0;
  virtual ~Histogram();
  // Add a value to be recorded in the histogram.
  virtual void Add(uint64_t value) = 0;
  virtual void Add(double value) = 0;

  virtual std::string ToString() const = 0;

  // Get statistics
  virtual double Median() const = 0;
  virtual double Percentile(double p) const = 0;
  virtual double Average() const = 0;
  virtual double StandardDeviation() const = 0;
  virtual void Data(HistogramData * const data) const = 0;

};
A
Abhishek Kona 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107

/**
 * A dumb ticker which keeps incrementing through its life time.
 * Not thread safe. Locking is currently managed by external leveldb lock
 */
class Ticker {
 public:
  Ticker() : count_(0) { }

  inline void recordTick() {
    count_++;
  }

108 109 110
  inline void recordTick(int count) {
    count_ += count;
  }
111

A
Abhishek Kona 已提交
112 113 114 115 116
  inline uint64_t getCount() {
    return count_;
  }

 private:
A
Abhishek Kona 已提交
117
  std::atomic_uint_fast64_t count_;
A
Abhishek Kona 已提交
118 119
};

120 121
// Analyze the performance of a db
class Statistics {
A
Abhishek Kona 已提交
122
 public:
123

A
Abhishek Kona 已提交
124
  virtual long getTickerCount(Tickers tickerType) = 0;
125
  virtual void recordTick(Tickers tickerType, uint64_t count = 0) = 0;
126 127 128 129
  virtual void measureTime(Histograms histogramType, uint64_t count) = 0;
  virtual void measureTime(Histograms histogramType, double count) = 0;

  virtual void histogramData(Histograms type, HistogramData * const data) = 0;
A
Abhishek Kona 已提交
130

131 132
};

A
Abhishek Kona 已提交
133
// Ease of Use functions
A
Abhishek Kona 已提交
134
inline void RecordTick(std::shared_ptr<Statistics> statistics,
135 136
                       Tickers ticker,
                       uint64_t count = 1) {
A
Abhishek Kona 已提交
137
  if (statistics) {
138
    statistics->recordTick(ticker, count);
A
Abhishek Kona 已提交
139
  }
140
}
141 142 143
}  // namespace leveldb

#endif  // STORAGE_LEVELDB_INCLUDE_STATISTICS_H_