statistics_impl.h 5.0 KB

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