statistics.h 4.9 KB

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