1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 #pragma once
7 #include "rocksdb/statistics.h"
8
9 #include <atomic>
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "monitoring/histogram.h"
15 #include "port/likely.h"
16 #include "port/port.h"
17 #include "util/core_local.h"
18 #include "util/mutexlock.h"
19
20 #ifdef __clang__
21 #define ROCKSDB_FIELD_UNUSED __attribute__((__unused__))
22 #else
23 #define ROCKSDB_FIELD_UNUSED
24 #endif // __clang__
25
26 #ifndef STRINGIFY
27 #define STRINGIFY(x) #x
28 #define TOSTRING(x) STRINGIFY(x)
29 #endif
30
31 namespace ROCKSDB_NAMESPACE {
32
33 enum TickersInternal : uint32_t {
34 INTERNAL_TICKER_ENUM_START = TICKER_ENUM_MAX,
35 INTERNAL_TICKER_ENUM_MAX
36 };
37
38 enum HistogramsInternal : uint32_t {
39 INTERNAL_HISTOGRAM_START = HISTOGRAM_ENUM_MAX,
40 INTERNAL_HISTOGRAM_ENUM_MAX
41 };
42
43 class StatisticsImpl : public Statistics {
44 public:
45 StatisticsImpl(std::shared_ptr<Statistics> stats);
46 virtual ~StatisticsImpl();
47
48 virtual uint64_t getTickerCount(uint32_t ticker_type) const override;
49 virtual void histogramData(uint32_t histogram_type,
50 HistogramData* const data) const override;
51 std::string getHistogramString(uint32_t histogram_type) const override;
52
53 virtual void setTickerCount(uint32_t ticker_type, uint64_t count) override;
54 virtual uint64_t getAndResetTickerCount(uint32_t ticker_type) override;
55 virtual void recordTick(uint32_t ticker_type, uint64_t count) override;
56 // The function is implemented for now for backward compatibility reason.
57 // In case a user explictly calls it, for example, they may have a wrapped
58 // Statistics object, passing the call to recordTick() into here, nothing
59 // will break.
measureTime(uint32_t histogramType,uint64_t time)60 void measureTime(uint32_t histogramType, uint64_t time) override {
61 recordInHistogram(histogramType, time);
62 }
63 virtual void recordInHistogram(uint32_t histogram_type,
64 uint64_t value) override;
65
66 virtual Status Reset() override;
67 virtual std::string ToString() const override;
68 virtual bool getTickerMap(std::map<std::string, uint64_t>*) const override;
69 virtual bool HistEnabledForType(uint32_t type) const override;
70
71 private:
72 // If non-nullptr, forwards updates to the object pointed to by `stats_`.
73 std::shared_ptr<Statistics> stats_;
74 // Synchronizes anything that operates across other cores' local data,
75 // such that operations like Reset() can be performed atomically.
76 mutable port::Mutex aggregate_lock_;
77
78 // The ticker/histogram data are stored in this structure, which we will store
79 // per-core. It is cache-aligned, so tickers/histograms belonging to different
80 // cores can never share the same cache line.
81 //
82 // Alignment attributes expand to nothing depending on the platform
ALIGN_AS(CACHE_LINE_SIZE)83 struct ALIGN_AS(CACHE_LINE_SIZE) StatisticsData {
84 std::atomic_uint_fast64_t tickers_[INTERNAL_TICKER_ENUM_MAX] = {{0}};
85 HistogramImpl histograms_[INTERNAL_HISTOGRAM_ENUM_MAX];
86 #ifndef HAVE_ALIGNED_NEW
87 char
88 padding[(CACHE_LINE_SIZE -
89 (INTERNAL_TICKER_ENUM_MAX * sizeof(std::atomic_uint_fast64_t) +
90 INTERNAL_HISTOGRAM_ENUM_MAX * sizeof(HistogramImpl)) %
91 CACHE_LINE_SIZE)] ROCKSDB_FIELD_UNUSED;
92 #endif
93 void *operator new(size_t s) { return port::cacheline_aligned_alloc(s); }
94 void *operator new[](size_t s) { return port::cacheline_aligned_alloc(s); }
95 void operator delete(void *p) { port::cacheline_aligned_free(p); }
96 void operator delete[](void *p) { port::cacheline_aligned_free(p); }
97 };
98
99 static_assert(sizeof(StatisticsData) % CACHE_LINE_SIZE == 0, "Expected " TOSTRING(CACHE_LINE_SIZE) "-byte aligned");
100
101 CoreLocalArray<StatisticsData> per_core_stats_;
102
103 uint64_t getTickerCountLocked(uint32_t ticker_type) const;
104 std::unique_ptr<HistogramImpl> getHistogramImplLocked(
105 uint32_t histogram_type) const;
106 void setTickerCountLocked(uint32_t ticker_type, uint64_t count);
107 };
108
109 // Utility functions
RecordInHistogram(Statistics * statistics,uint32_t histogram_type,uint64_t value)110 inline void RecordInHistogram(Statistics* statistics, uint32_t histogram_type,
111 uint64_t value) {
112 if (statistics) {
113 statistics->recordInHistogram(histogram_type, value);
114 }
115 }
116
RecordTimeToHistogram(Statistics * statistics,uint32_t histogram_type,uint64_t value)117 inline void RecordTimeToHistogram(Statistics* statistics,
118 uint32_t histogram_type, uint64_t value) {
119 if (statistics) {
120 statistics->reportTimeToHistogram(histogram_type, value);
121 }
122 }
123
124 inline void RecordTick(Statistics* statistics, uint32_t ticker_type,
125 uint64_t count = 1) {
126 if (statistics) {
127 statistics->recordTick(ticker_type, count);
128 }
129 }
130
SetTickerCount(Statistics * statistics,uint32_t ticker_type,uint64_t count)131 inline void SetTickerCount(Statistics* statistics, uint32_t ticker_type,
132 uint64_t count) {
133 if (statistics) {
134 statistics->setTickerCount(ticker_type, count);
135 }
136 }
137
138 } // namespace ROCKSDB_NAMESPACE
139