xref: /aosp_15_r20/external/abseil-cpp/absl/profiling/internal/exponential_biased.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2019 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include "absl/profiling/internal/exponential_biased.h"
16*9356374aSAndroid Build Coastguard Worker 
17*9356374aSAndroid Build Coastguard Worker #include <stdint.h>
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include <algorithm>
20*9356374aSAndroid Build Coastguard Worker #include <atomic>
21*9356374aSAndroid Build Coastguard Worker #include <cmath>
22*9356374aSAndroid Build Coastguard Worker #include <limits>
23*9356374aSAndroid Build Coastguard Worker 
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/base/optimization.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker namespace absl {
28*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
29*9356374aSAndroid Build Coastguard Worker namespace profiling_internal {
30*9356374aSAndroid Build Coastguard Worker 
31*9356374aSAndroid Build Coastguard Worker // The algorithm generates a random number between 0 and 1 and applies the
32*9356374aSAndroid Build Coastguard Worker // inverse cumulative distribution function for an exponential. Specifically:
33*9356374aSAndroid Build Coastguard Worker // Let m be the inverse of the sample period, then the probability
34*9356374aSAndroid Build Coastguard Worker // distribution function is m*exp(-mx) so the CDF is
35*9356374aSAndroid Build Coastguard Worker // p = 1 - exp(-mx), so
36*9356374aSAndroid Build Coastguard Worker // q = 1 - p = exp(-mx)
37*9356374aSAndroid Build Coastguard Worker // log_e(q) = -mx
38*9356374aSAndroid Build Coastguard Worker // -log_e(q)/m = x
39*9356374aSAndroid Build Coastguard Worker // log_2(q) * (-log_e(2) * 1/m) = x
40*9356374aSAndroid Build Coastguard Worker // In the code, q is actually in the range 1 to 2**26, hence the -26 below
GetSkipCount(int64_t mean)41*9356374aSAndroid Build Coastguard Worker int64_t ExponentialBiased::GetSkipCount(int64_t mean) {
42*9356374aSAndroid Build Coastguard Worker   if (ABSL_PREDICT_FALSE(!initialized_)) {
43*9356374aSAndroid Build Coastguard Worker     Initialize();
44*9356374aSAndroid Build Coastguard Worker   }
45*9356374aSAndroid Build Coastguard Worker 
46*9356374aSAndroid Build Coastguard Worker   uint64_t rng = NextRandom(rng_);
47*9356374aSAndroid Build Coastguard Worker   rng_ = rng;
48*9356374aSAndroid Build Coastguard Worker 
49*9356374aSAndroid Build Coastguard Worker   // Take the top 26 bits as the random number
50*9356374aSAndroid Build Coastguard Worker   // (This plus the 1<<58 sampling bound give a max possible step of
51*9356374aSAndroid Build Coastguard Worker   // 5194297183973780480 bytes.)
52*9356374aSAndroid Build Coastguard Worker   // The uint32_t cast is to prevent a (hard-to-reproduce) NAN
53*9356374aSAndroid Build Coastguard Worker   // under piii debug for some binaries.
54*9356374aSAndroid Build Coastguard Worker   double q = static_cast<uint32_t>(rng >> (kPrngNumBits - 26)) + 1.0;
55*9356374aSAndroid Build Coastguard Worker   // Put the computed p-value through the CDF of a geometric.
56*9356374aSAndroid Build Coastguard Worker   double interval = bias_ + (std::log2(q) - 26) * (-std::log(2.0) * mean);
57*9356374aSAndroid Build Coastguard Worker   // Very large values of interval overflow int64_t. To avoid that, we will
58*9356374aSAndroid Build Coastguard Worker   // cheat and clamp any huge values to (int64_t max)/2. This is a potential
59*9356374aSAndroid Build Coastguard Worker   // source of bias, but the mean would need to be such a large value that it's
60*9356374aSAndroid Build Coastguard Worker   // not likely to come up. For example, with a mean of 1e18, the probability of
61*9356374aSAndroid Build Coastguard Worker   // hitting this condition is about 1/1000. For a mean of 1e17, standard
62*9356374aSAndroid Build Coastguard Worker   // calculators claim that this event won't happen.
63*9356374aSAndroid Build Coastguard Worker   if (interval > static_cast<double>(std::numeric_limits<int64_t>::max() / 2)) {
64*9356374aSAndroid Build Coastguard Worker     // Assume huge values are bias neutral, retain bias for next call.
65*9356374aSAndroid Build Coastguard Worker     return std::numeric_limits<int64_t>::max() / 2;
66*9356374aSAndroid Build Coastguard Worker   }
67*9356374aSAndroid Build Coastguard Worker   double value = std::rint(interval);
68*9356374aSAndroid Build Coastguard Worker   bias_ = interval - value;
69*9356374aSAndroid Build Coastguard Worker   return value;
70*9356374aSAndroid Build Coastguard Worker }
71*9356374aSAndroid Build Coastguard Worker 
GetStride(int64_t mean)72*9356374aSAndroid Build Coastguard Worker int64_t ExponentialBiased::GetStride(int64_t mean) {
73*9356374aSAndroid Build Coastguard Worker   return GetSkipCount(mean - 1) + 1;
74*9356374aSAndroid Build Coastguard Worker }
75*9356374aSAndroid Build Coastguard Worker 
Initialize()76*9356374aSAndroid Build Coastguard Worker void ExponentialBiased::Initialize() {
77*9356374aSAndroid Build Coastguard Worker   // We don't get well distributed numbers from `this` so we call NextRandom() a
78*9356374aSAndroid Build Coastguard Worker   // bunch to mush the bits around. We use a global_rand to handle the case
79*9356374aSAndroid Build Coastguard Worker   // where the same thread (by memory address) gets created and destroyed
80*9356374aSAndroid Build Coastguard Worker   // repeatedly.
81*9356374aSAndroid Build Coastguard Worker   ABSL_CONST_INIT static std::atomic<uint32_t> global_rand(0);
82*9356374aSAndroid Build Coastguard Worker   uint64_t r = reinterpret_cast<uint64_t>(this) +
83*9356374aSAndroid Build Coastguard Worker                global_rand.fetch_add(1, std::memory_order_relaxed);
84*9356374aSAndroid Build Coastguard Worker   for (int i = 0; i < 20; ++i) {
85*9356374aSAndroid Build Coastguard Worker     r = NextRandom(r);
86*9356374aSAndroid Build Coastguard Worker   }
87*9356374aSAndroid Build Coastguard Worker   rng_ = r;
88*9356374aSAndroid Build Coastguard Worker   initialized_ = true;
89*9356374aSAndroid Build Coastguard Worker }
90*9356374aSAndroid Build Coastguard Worker 
91*9356374aSAndroid Build Coastguard Worker }  // namespace profiling_internal
92*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
93*9356374aSAndroid Build Coastguard Worker }  // namespace absl
94