exponential_biased.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/profiling/internal/exponential_biased.h"
  15. #include <stdint.h>
  16. #include <algorithm>
  17. #include <atomic>
  18. #include <cmath>
  19. #include <limits>
  20. #include "absl/base/attributes.h"
  21. #include "absl/base/optimization.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace profiling_internal {
  25. // The algorithm generates a random number between 0 and 1 and applies the
  26. // inverse cumulative distribution function for an exponential. Specifically:
  27. // Let m be the inverse of the sample period, then the probability
  28. // distribution function is m*exp(-mx) so the CDF is
  29. // p = 1 - exp(-mx), so
  30. // q = 1 - p = exp(-mx)
  31. // log_e(q) = -mx
  32. // -log_e(q)/m = x
  33. // log_2(q) * (-log_e(2) * 1/m) = x
  34. // In the code, q is actually in the range 1 to 2**26, hence the -26 below
  35. int64_t ExponentialBiased::GetSkipCount(int64_t mean) {
  36. if (ABSL_PREDICT_FALSE(!initialized_)) {
  37. Initialize();
  38. }
  39. uint64_t rng = NextRandom(rng_);
  40. rng_ = rng;
  41. // Take the top 26 bits as the random number
  42. // (This plus the 1<<58 sampling bound give a max possible step of
  43. // 5194297183973780480 bytes.)
  44. // The uint32_t cast is to prevent a (hard-to-reproduce) NAN
  45. // under piii debug for some binaries.
  46. double q = static_cast<uint32_t>(rng >> (kPrngNumBits - 26)) + 1.0;
  47. // Put the computed p-value through the CDF of a geometric.
  48. double interval = bias_ + (std::log2(q) - 26) * (-std::log(2.0) * mean);
  49. // Very large values of interval overflow int64_t. To avoid that, we will
  50. // cheat and clamp any huge values to (int64_t max)/2. This is a potential
  51. // source of bias, but the mean would need to be such a large value that it's
  52. // not likely to come up. For example, with a mean of 1e18, the probability of
  53. // hitting this condition is about 1/1000. For a mean of 1e17, standard
  54. // calculators claim that this event won't happen.
  55. if (interval > static_cast<double>(std::numeric_limits<int64_t>::max() / 2)) {
  56. // Assume huge values are bias neutral, retain bias for next call.
  57. return std::numeric_limits<int64_t>::max() / 2;
  58. }
  59. double value = std::rint(interval);
  60. bias_ = interval - value;
  61. return value;
  62. }
  63. int64_t ExponentialBiased::GetStride(int64_t mean) {
  64. return GetSkipCount(mean - 1) + 1;
  65. }
  66. void ExponentialBiased::Initialize() {
  67. // We don't get well distributed numbers from `this` so we call NextRandom() a
  68. // bunch to mush the bits around. We use a global_rand to handle the case
  69. // where the same thread (by memory address) gets created and destroyed
  70. // repeatedly.
  71. ABSL_CONST_INIT static std::atomic<uint32_t> global_rand(0);
  72. uint64_t r = reinterpret_cast<uint64_t>(this) +
  73. global_rand.fetch_add(1, std::memory_order_relaxed);
  74. for (int i = 0; i < 20; ++i) {
  75. r = NextRandom(r);
  76. }
  77. rng_ = r;
  78. initialized_ = true;
  79. }
  80. } // namespace profiling_internal
  81. ABSL_NAMESPACE_END
  82. } // namespace absl