hashtablez_sampler.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include "absl/container/internal/hashtablez_sampler.h"
  15. #include <algorithm>
  16. #include <atomic>
  17. #include <cassert>
  18. #include <cmath>
  19. #include <cstddef>
  20. #include <cstdint>
  21. #include <functional>
  22. #include <limits>
  23. #include "absl/base/attributes.h"
  24. #include "absl/base/config.h"
  25. #include "absl/base/internal/per_thread_tls.h"
  26. #include "absl/base/internal/raw_logging.h"
  27. #include "absl/base/macros.h"
  28. #include "absl/base/no_destructor.h"
  29. #include "absl/base/optimization.h"
  30. #include "absl/debugging/stacktrace.h"
  31. #include "absl/memory/memory.h"
  32. #include "absl/profiling/internal/exponential_biased.h"
  33. #include "absl/profiling/internal/sample_recorder.h"
  34. #include "absl/synchronization/mutex.h"
  35. #include "absl/time/clock.h"
  36. #include "absl/utility/utility.h"
  37. namespace absl {
  38. ABSL_NAMESPACE_BEGIN
  39. namespace container_internal {
  40. #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  41. constexpr int HashtablezInfo::kMaxStackDepth;
  42. #endif
  43. namespace {
  44. ABSL_CONST_INIT std::atomic<bool> g_hashtablez_enabled{
  45. false
  46. };
  47. ABSL_CONST_INIT std::atomic<int32_t> g_hashtablez_sample_parameter{1 << 10};
  48. std::atomic<HashtablezConfigListener> g_hashtablez_config_listener{nullptr};
  49. #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  50. ABSL_PER_THREAD_TLS_KEYWORD absl::profiling_internal::ExponentialBiased
  51. g_exponential_biased_generator;
  52. #endif
  53. void TriggerHashtablezConfigListener() {
  54. auto* listener = g_hashtablez_config_listener.load(std::memory_order_acquire);
  55. if (listener != nullptr) listener();
  56. }
  57. } // namespace
  58. #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  59. ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample = {0, 0};
  60. #endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  61. HashtablezSampler& GlobalHashtablezSampler() {
  62. static absl::NoDestructor<HashtablezSampler> sampler;
  63. return *sampler;
  64. }
  65. HashtablezInfo::HashtablezInfo() = default;
  66. HashtablezInfo::~HashtablezInfo() = default;
  67. void HashtablezInfo::PrepareForSampling(int64_t stride,
  68. size_t inline_element_size_value,
  69. size_t key_size_value,
  70. size_t value_size_value,
  71. uint16_t soo_capacity_value) {
  72. capacity.store(0, std::memory_order_relaxed);
  73. size.store(0, std::memory_order_relaxed);
  74. num_erases.store(0, std::memory_order_relaxed);
  75. num_rehashes.store(0, std::memory_order_relaxed);
  76. max_probe_length.store(0, std::memory_order_relaxed);
  77. total_probe_length.store(0, std::memory_order_relaxed);
  78. hashes_bitwise_or.store(0, std::memory_order_relaxed);
  79. hashes_bitwise_and.store(~size_t{}, std::memory_order_relaxed);
  80. hashes_bitwise_xor.store(0, std::memory_order_relaxed);
  81. max_reserve.store(0, std::memory_order_relaxed);
  82. create_time = absl::Now();
  83. weight = stride;
  84. // The inliner makes hardcoded skip_count difficult (especially when combined
  85. // with LTO). We use the ability to exclude stacks by regex when encoding
  86. // instead.
  87. depth = absl::GetStackTrace(stack, HashtablezInfo::kMaxStackDepth,
  88. /* skip_count= */ 0);
  89. inline_element_size = inline_element_size_value;
  90. key_size = key_size_value;
  91. value_size = value_size_value;
  92. soo_capacity = soo_capacity_value;
  93. }
  94. static bool ShouldForceSampling() {
  95. enum ForceState {
  96. kDontForce,
  97. kForce,
  98. kUninitialized
  99. };
  100. ABSL_CONST_INIT static std::atomic<ForceState> global_state{
  101. kUninitialized};
  102. ForceState state = global_state.load(std::memory_order_relaxed);
  103. if (ABSL_PREDICT_TRUE(state == kDontForce)) return false;
  104. if (state == kUninitialized) {
  105. state = ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)()
  106. ? kForce
  107. : kDontForce;
  108. global_state.store(state, std::memory_order_relaxed);
  109. }
  110. return state == kForce;
  111. }
  112. HashtablezInfo* SampleSlow(SamplingState& next_sample,
  113. size_t inline_element_size, size_t key_size,
  114. size_t value_size, uint16_t soo_capacity) {
  115. if (ABSL_PREDICT_FALSE(ShouldForceSampling())) {
  116. next_sample.next_sample = 1;
  117. const int64_t old_stride = exchange(next_sample.sample_stride, 1);
  118. HashtablezInfo* result = GlobalHashtablezSampler().Register(
  119. old_stride, inline_element_size, key_size, value_size, soo_capacity);
  120. return result;
  121. }
  122. #if !defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
  123. next_sample = {
  124. std::numeric_limits<int64_t>::max(),
  125. std::numeric_limits<int64_t>::max(),
  126. };
  127. return nullptr;
  128. #else
  129. bool first = next_sample.next_sample < 0;
  130. const int64_t next_stride = g_exponential_biased_generator.GetStride(
  131. g_hashtablez_sample_parameter.load(std::memory_order_relaxed));
  132. next_sample.next_sample = next_stride;
  133. const int64_t old_stride = exchange(next_sample.sample_stride, next_stride);
  134. // Small values of interval are equivalent to just sampling next time.
  135. ABSL_ASSERT(next_stride >= 1);
  136. // g_hashtablez_enabled can be dynamically flipped, we need to set a threshold
  137. // low enough that we will start sampling in a reasonable time, so we just use
  138. // the default sampling rate.
  139. if (!g_hashtablez_enabled.load(std::memory_order_relaxed)) return nullptr;
  140. // We will only be negative on our first count, so we should just retry in
  141. // that case.
  142. if (first) {
  143. if (ABSL_PREDICT_TRUE(--next_sample.next_sample > 0)) return nullptr;
  144. return SampleSlow(next_sample, inline_element_size, key_size, value_size,
  145. soo_capacity);
  146. }
  147. return GlobalHashtablezSampler().Register(old_stride, inline_element_size,
  148. key_size, value_size, soo_capacity);
  149. #endif
  150. }
  151. void UnsampleSlow(HashtablezInfo* info) {
  152. GlobalHashtablezSampler().Unregister(info);
  153. }
  154. void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) {
  155. #ifdef ABSL_INTERNAL_HAVE_SSE2
  156. total_probe_length /= 16;
  157. #else
  158. total_probe_length /= 8;
  159. #endif
  160. info->total_probe_length.store(total_probe_length, std::memory_order_relaxed);
  161. info->num_erases.store(0, std::memory_order_relaxed);
  162. // There is only one concurrent writer, so `load` then `store` is sufficient
  163. // instead of using `fetch_add`.
  164. info->num_rehashes.store(
  165. 1 + info->num_rehashes.load(std::memory_order_relaxed),
  166. std::memory_order_relaxed);
  167. }
  168. void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity) {
  169. info->max_reserve.store(
  170. (std::max)(info->max_reserve.load(std::memory_order_relaxed),
  171. target_capacity),
  172. std::memory_order_relaxed);
  173. }
  174. void RecordClearedReservationSlow(HashtablezInfo* info) {
  175. info->max_reserve.store(0, std::memory_order_relaxed);
  176. }
  177. void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
  178. size_t capacity) {
  179. info->size.store(size, std::memory_order_relaxed);
  180. info->capacity.store(capacity, std::memory_order_relaxed);
  181. if (size == 0) {
  182. // This is a clear, reset the total/num_erases too.
  183. info->total_probe_length.store(0, std::memory_order_relaxed);
  184. info->num_erases.store(0, std::memory_order_relaxed);
  185. }
  186. }
  187. void RecordInsertSlow(HashtablezInfo* info, size_t hash,
  188. size_t distance_from_desired) {
  189. // SwissTables probe in groups of 16, so scale this to count items probes and
  190. // not offset from desired.
  191. size_t probe_length = distance_from_desired;
  192. #ifdef ABSL_INTERNAL_HAVE_SSE2
  193. probe_length /= 16;
  194. #else
  195. probe_length /= 8;
  196. #endif
  197. info->hashes_bitwise_and.fetch_and(hash, std::memory_order_relaxed);
  198. info->hashes_bitwise_or.fetch_or(hash, std::memory_order_relaxed);
  199. info->hashes_bitwise_xor.fetch_xor(hash, std::memory_order_relaxed);
  200. info->max_probe_length.store(
  201. std::max(info->max_probe_length.load(std::memory_order_relaxed),
  202. probe_length),
  203. std::memory_order_relaxed);
  204. info->total_probe_length.fetch_add(probe_length, std::memory_order_relaxed);
  205. info->size.fetch_add(1, std::memory_order_relaxed);
  206. }
  207. void RecordEraseSlow(HashtablezInfo* info) {
  208. info->size.fetch_sub(1, std::memory_order_relaxed);
  209. // There is only one concurrent writer, so `load` then `store` is sufficient
  210. // instead of using `fetch_add`.
  211. info->num_erases.store(1 + info->num_erases.load(std::memory_order_relaxed),
  212. std::memory_order_relaxed);
  213. }
  214. void SetHashtablezConfigListener(HashtablezConfigListener l) {
  215. g_hashtablez_config_listener.store(l, std::memory_order_release);
  216. }
  217. bool IsHashtablezEnabled() {
  218. return g_hashtablez_enabled.load(std::memory_order_acquire);
  219. }
  220. void SetHashtablezEnabled(bool enabled) {
  221. SetHashtablezEnabledInternal(enabled);
  222. TriggerHashtablezConfigListener();
  223. }
  224. void SetHashtablezEnabledInternal(bool enabled) {
  225. g_hashtablez_enabled.store(enabled, std::memory_order_release);
  226. }
  227. int32_t GetHashtablezSampleParameter() {
  228. return g_hashtablez_sample_parameter.load(std::memory_order_acquire);
  229. }
  230. void SetHashtablezSampleParameter(int32_t rate) {
  231. SetHashtablezSampleParameterInternal(rate);
  232. TriggerHashtablezConfigListener();
  233. }
  234. void SetHashtablezSampleParameterInternal(int32_t rate) {
  235. if (rate > 0) {
  236. g_hashtablez_sample_parameter.store(rate, std::memory_order_release);
  237. } else {
  238. ABSL_RAW_LOG(ERROR, "Invalid hashtablez sample rate: %lld",
  239. static_cast<long long>(rate)); // NOLINT(runtime/int)
  240. }
  241. }
  242. size_t GetHashtablezMaxSamples() {
  243. return GlobalHashtablezSampler().GetMaxSamples();
  244. }
  245. void SetHashtablezMaxSamples(size_t max) {
  246. SetHashtablezMaxSamplesInternal(max);
  247. TriggerHashtablezConfigListener();
  248. }
  249. void SetHashtablezMaxSamplesInternal(size_t max) {
  250. if (max > 0) {
  251. GlobalHashtablezSampler().SetMaxSamples(max);
  252. } else {
  253. ABSL_RAW_LOG(ERROR, "Invalid hashtablez max samples: 0");
  254. }
  255. }
  256. } // namespace container_internal
  257. ABSL_NAMESPACE_END
  258. } // namespace absl