hashtablez_sampler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: hashtablez_sampler.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines the API for a low level library to sample hashtables
  20. // and collect runtime statistics about them.
  21. //
  22. // `HashtablezSampler` controls the lifecycle of `HashtablezInfo` objects which
  23. // store information about a single sample.
  24. //
  25. // `Record*` methods store information into samples.
  26. // `Sample()` and `Unsample()` make use of a single global sampler with
  27. // properties controlled by the flags hashtablez_enabled,
  28. // hashtablez_sample_rate, and hashtablez_max_samples.
  29. //
  30. // WARNING
  31. //
  32. // Using this sampling API may cause sampled Swiss tables to use the global
  33. // allocator (operator `new`) in addition to any custom allocator. If you
  34. // are using a table in an unusual circumstance where allocation or calling a
  35. // linux syscall is unacceptable, this could interfere.
  36. //
  37. // This utility is internal-only. Use at your own risk.
  38. #ifndef ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
  39. #define ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
  40. #include <atomic>
  41. #include <cstddef>
  42. #include <cstdint>
  43. #include <functional>
  44. #include <memory>
  45. #include <vector>
  46. #include "absl/base/attributes.h"
  47. #include "absl/base/config.h"
  48. #include "absl/base/internal/per_thread_tls.h"
  49. #include "absl/base/optimization.h"
  50. #include "absl/base/thread_annotations.h"
  51. #include "absl/profiling/internal/sample_recorder.h"
  52. #include "absl/synchronization/mutex.h"
  53. #include "absl/time/time.h"
  54. #include "absl/utility/utility.h"
  55. namespace absl {
  56. ABSL_NAMESPACE_BEGIN
  57. namespace container_internal {
  58. // Stores information about a sampled hashtable. All mutations to this *must*
  59. // be made through `Record*` functions below. All reads from this *must* only
  60. // occur in the callback to `HashtablezSampler::Iterate`.
  61. struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> {
  62. // Constructs the object but does not fill in any fields.
  63. HashtablezInfo();
  64. ~HashtablezInfo();
  65. HashtablezInfo(const HashtablezInfo&) = delete;
  66. HashtablezInfo& operator=(const HashtablezInfo&) = delete;
  67. // Puts the object into a clean state, fills in the logically `const` members,
  68. // blocking for any readers that are currently sampling the object.
  69. void PrepareForSampling(int64_t stride, size_t inline_element_size_value,
  70. size_t key_size, size_t value_size,
  71. uint16_t soo_capacity_value)
  72. ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu);
  73. // These fields are mutated by the various Record* APIs and need to be
  74. // thread-safe.
  75. std::atomic<size_t> capacity;
  76. std::atomic<size_t> size;
  77. std::atomic<size_t> num_erases;
  78. std::atomic<size_t> num_rehashes;
  79. std::atomic<size_t> max_probe_length;
  80. std::atomic<size_t> total_probe_length;
  81. std::atomic<size_t> hashes_bitwise_or;
  82. std::atomic<size_t> hashes_bitwise_and;
  83. std::atomic<size_t> hashes_bitwise_xor;
  84. std::atomic<size_t> max_reserve;
  85. // All of the fields below are set by `PrepareForSampling`, they must not be
  86. // mutated in `Record*` functions. They are logically `const` in that sense.
  87. // These are guarded by init_mu, but that is not externalized to clients,
  88. // which can read them only during `SampleRecorder::Iterate` which will hold
  89. // the lock.
  90. static constexpr int kMaxStackDepth = 64;
  91. absl::Time create_time;
  92. int32_t depth;
  93. // The SOO capacity for this table in elements (not bytes). Note that sampled
  94. // tables are never SOO because we need to store the infoz handle on the heap.
  95. // Tables that would be SOO if not sampled should have: soo_capacity > 0 &&
  96. // size <= soo_capacity && max_reserve <= soo_capacity.
  97. uint16_t soo_capacity;
  98. void* stack[kMaxStackDepth];
  99. size_t inline_element_size; // How big is the slot in bytes?
  100. size_t key_size; // sizeof(key_type)
  101. size_t value_size; // sizeof(value_type)
  102. };
  103. void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length);
  104. void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity);
  105. void RecordClearedReservationSlow(HashtablezInfo* info);
  106. void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
  107. size_t capacity);
  108. void RecordInsertSlow(HashtablezInfo* info, size_t hash,
  109. size_t distance_from_desired);
  110. void RecordEraseSlow(HashtablezInfo* info);
  111. struct SamplingState {
  112. int64_t next_sample;
  113. // When we make a sampling decision, we record that distance so we can weight
  114. // each sample.
  115. int64_t sample_stride;
  116. };
  117. HashtablezInfo* SampleSlow(SamplingState& next_sample,
  118. size_t inline_element_size, size_t key_size,
  119. size_t value_size, uint16_t soo_capacity);
  120. void UnsampleSlow(HashtablezInfo* info);
  121. #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  122. #error ABSL_INTERNAL_HASHTABLEZ_SAMPLE cannot be directly set
  123. #endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  124. #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  125. class HashtablezInfoHandle {
  126. public:
  127. explicit HashtablezInfoHandle() : info_(nullptr) {}
  128. explicit HashtablezInfoHandle(HashtablezInfo* info) : info_(info) {}
  129. // We do not have a destructor. Caller is responsible for calling Unregister
  130. // before destroying the handle.
  131. void Unregister() {
  132. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  133. UnsampleSlow(info_);
  134. }
  135. inline bool IsSampled() const { return ABSL_PREDICT_FALSE(info_ != nullptr); }
  136. inline void RecordStorageChanged(size_t size, size_t capacity) {
  137. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  138. RecordStorageChangedSlow(info_, size, capacity);
  139. }
  140. inline void RecordRehash(size_t total_probe_length) {
  141. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  142. RecordRehashSlow(info_, total_probe_length);
  143. }
  144. inline void RecordReservation(size_t target_capacity) {
  145. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  146. RecordReservationSlow(info_, target_capacity);
  147. }
  148. inline void RecordClearedReservation() {
  149. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  150. RecordClearedReservationSlow(info_);
  151. }
  152. inline void RecordInsert(size_t hash, size_t distance_from_desired) {
  153. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  154. RecordInsertSlow(info_, hash, distance_from_desired);
  155. }
  156. inline void RecordErase() {
  157. if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
  158. RecordEraseSlow(info_);
  159. }
  160. friend inline void swap(HashtablezInfoHandle& lhs,
  161. HashtablezInfoHandle& rhs) {
  162. std::swap(lhs.info_, rhs.info_);
  163. }
  164. private:
  165. friend class HashtablezInfoHandlePeer;
  166. HashtablezInfo* info_;
  167. };
  168. #else
  169. // Ensure that when Hashtablez is turned off at compile time, HashtablezInfo can
  170. // be removed by the linker, in order to reduce the binary size.
  171. class HashtablezInfoHandle {
  172. public:
  173. explicit HashtablezInfoHandle() = default;
  174. explicit HashtablezInfoHandle(std::nullptr_t) {}
  175. inline void Unregister() {}
  176. inline bool IsSampled() const { return false; }
  177. inline void RecordStorageChanged(size_t /*size*/, size_t /*capacity*/) {}
  178. inline void RecordRehash(size_t /*total_probe_length*/) {}
  179. inline void RecordReservation(size_t /*target_capacity*/) {}
  180. inline void RecordClearedReservation() {}
  181. inline void RecordInsert(size_t /*hash*/, size_t /*distance_from_desired*/) {}
  182. inline void RecordErase() {}
  183. friend inline void swap(HashtablezInfoHandle& /*lhs*/,
  184. HashtablezInfoHandle& /*rhs*/) {}
  185. };
  186. #endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  187. #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  188. extern ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample;
  189. #endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  190. // Returns a sampling handle.
  191. inline HashtablezInfoHandle Sample(
  192. ABSL_ATTRIBUTE_UNUSED size_t inline_element_size,
  193. ABSL_ATTRIBUTE_UNUSED size_t key_size,
  194. ABSL_ATTRIBUTE_UNUSED size_t value_size,
  195. ABSL_ATTRIBUTE_UNUSED uint16_t soo_capacity) {
  196. #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  197. if (ABSL_PREDICT_TRUE(--global_next_sample.next_sample > 0)) {
  198. return HashtablezInfoHandle(nullptr);
  199. }
  200. return HashtablezInfoHandle(SampleSlow(global_next_sample,
  201. inline_element_size, key_size,
  202. value_size, soo_capacity));
  203. #else
  204. return HashtablezInfoHandle(nullptr);
  205. #endif // !ABSL_PER_THREAD_TLS
  206. }
  207. using HashtablezSampler =
  208. ::absl::profiling_internal::SampleRecorder<HashtablezInfo>;
  209. // Returns a global Sampler.
  210. HashtablezSampler& GlobalHashtablezSampler();
  211. using HashtablezConfigListener = void (*)();
  212. void SetHashtablezConfigListener(HashtablezConfigListener l);
  213. // Enables or disables sampling for Swiss tables.
  214. bool IsHashtablezEnabled();
  215. void SetHashtablezEnabled(bool enabled);
  216. void SetHashtablezEnabledInternal(bool enabled);
  217. // Sets the rate at which Swiss tables will be sampled.
  218. int32_t GetHashtablezSampleParameter();
  219. void SetHashtablezSampleParameter(int32_t rate);
  220. void SetHashtablezSampleParameterInternal(int32_t rate);
  221. // Sets a soft max for the number of samples that will be kept.
  222. size_t GetHashtablezMaxSamples();
  223. void SetHashtablezMaxSamples(size_t max);
  224. void SetHashtablezMaxSamplesInternal(size_t max);
  225. // Configuration override.
  226. // This allows process-wide sampling without depending on order of
  227. // initialization of static storage duration objects.
  228. // The definition of this constant is weak, which allows us to inject a
  229. // different value for it at link time.
  230. extern "C" bool ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)();
  231. } // namespace container_internal
  232. ABSL_NAMESPACE_END
  233. } // namespace absl
  234. #endif // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_