hashtablez_sampler.cc 9.5 KB

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