cordz_functions.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 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/strings/internal/cordz_functions.h"
  15. #include <atomic>
  16. #include <cmath>
  17. #include <limits>
  18. #include <random>
  19. #include "absl/base/attributes.h"
  20. #include "absl/base/config.h"
  21. #include "absl/base/internal/raw_logging.h"
  22. #include "absl/profiling/internal/exponential_biased.h"
  23. namespace absl {
  24. ABSL_NAMESPACE_BEGIN
  25. namespace cord_internal {
  26. namespace {
  27. // The average interval until the next sample. A value of 0 disables profiling
  28. // while a value of 1 will profile all Cords.
  29. std::atomic<int> g_cordz_mean_interval(50000);
  30. } // namespace
  31. #ifdef ABSL_INTERNAL_CORDZ_ENABLED
  32. // Special negative 'not initialized' per thread value for cordz_next_sample.
  33. static constexpr int64_t kInitCordzNextSample = -1;
  34. ABSL_CONST_INIT thread_local int64_t cordz_next_sample = kInitCordzNextSample;
  35. // kIntervalIfDisabled is the number of profile-eligible events need to occur
  36. // before the code will confirm that cordz is still disabled.
  37. constexpr int64_t kIntervalIfDisabled = 1 << 16;
  38. ABSL_ATTRIBUTE_NOINLINE bool cordz_should_profile_slow() {
  39. thread_local absl::profiling_internal::ExponentialBiased
  40. exponential_biased_generator;
  41. int32_t mean_interval = get_cordz_mean_interval();
  42. // Check if we disabled profiling. If so, set the next sample to a "large"
  43. // number to minimize the overhead of the should_profile codepath.
  44. if (mean_interval <= 0) {
  45. cordz_next_sample = kIntervalIfDisabled;
  46. return false;
  47. }
  48. // Check if we're always sampling.
  49. if (mean_interval == 1) {
  50. cordz_next_sample = 1;
  51. return true;
  52. }
  53. if (cordz_next_sample <= 0) {
  54. // If first check on current thread, check cordz_should_profile()
  55. // again using the created (initial) stride in cordz_next_sample.
  56. const bool initialized = cordz_next_sample != kInitCordzNextSample;
  57. cordz_next_sample = exponential_biased_generator.GetStride(mean_interval);
  58. return initialized || cordz_should_profile();
  59. }
  60. --cordz_next_sample;
  61. return false;
  62. }
  63. void cordz_set_next_sample_for_testing(int64_t next_sample) {
  64. cordz_next_sample = next_sample;
  65. }
  66. #endif // ABSL_INTERNAL_CORDZ_ENABLED
  67. int32_t get_cordz_mean_interval() {
  68. return g_cordz_mean_interval.load(std::memory_order_acquire);
  69. }
  70. void set_cordz_mean_interval(int32_t mean_interval) {
  71. g_cordz_mean_interval.store(mean_interval, std::memory_order_release);
  72. }
  73. } // namespace cord_internal
  74. ABSL_NAMESPACE_END
  75. } // namespace absl