Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-19 09:23:05

0001 // Copyright 2019 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_PROFILING_INTERNAL_EXPONENTIAL_BIASED_H_
0016 #define ABSL_PROFILING_INTERNAL_EXPONENTIAL_BIASED_H_
0017 
0018 #include <stdint.h>
0019 
0020 #include "absl/base/config.h"
0021 #include "absl/base/macros.h"
0022 
0023 namespace absl {
0024 ABSL_NAMESPACE_BEGIN
0025 namespace profiling_internal {
0026 
0027 // ExponentialBiased provides a small and fast random number generator for a
0028 // rounded exponential distribution. This generator manages very little state,
0029 // and imposes no synchronization overhead. This makes it useful in specialized
0030 // scenarios requiring minimum overhead, such as stride based periodic sampling.
0031 //
0032 // ExponentialBiased provides two closely related functions, GetSkipCount() and
0033 // GetStride(), both returning a rounded integer defining a number of events
0034 // required before some event with a given mean probability occurs.
0035 //
0036 // The distribution is useful to generate a random wait time or some periodic
0037 // event with a given mean probability. For example, if an action is supposed to
0038 // happen on average once every 'N' events, then we can get a random 'stride'
0039 // counting down how long before the event to happen. For example, if we'd want
0040 // to sample one in every 1000 'Frobber' calls, our code could look like this:
0041 //
0042 //   Frobber::Frobber() {
0043 //     stride_ = exponential_biased_.GetStride(1000);
0044 //   }
0045 //
0046 //   void Frobber::Frob(int arg) {
0047 //     if (--stride == 0) {
0048 //       SampleFrob(arg);
0049 //       stride_ = exponential_biased_.GetStride(1000);
0050 //     }
0051 //     ...
0052 //   }
0053 //
0054 // The rounding of the return value creates a bias, especially for smaller means
0055 // where the distribution of the fraction is not evenly distributed. We correct
0056 // this bias by tracking the fraction we rounded up or down on each iteration,
0057 // effectively tracking the distance between the cumulative value, and the
0058 // rounded cumulative value. For example, given a mean of 2:
0059 //
0060 //   raw = 1.63076, cumulative = 1.63076, rounded = 2, bias = -0.36923
0061 //   raw = 0.14624, cumulative = 1.77701, rounded = 2, bias =  0.14624
0062 //   raw = 4.93194, cumulative = 6.70895, rounded = 7, bias = -0.06805
0063 //   raw = 0.24206, cumulative = 6.95101, rounded = 7, bias =  0.24206
0064 //   etc...
0065 //
0066 // Adjusting with rounding bias is relatively trivial:
0067 //
0068 //    double value = bias_ + exponential_distribution(mean)();
0069 //    double rounded_value = std::rint(value);
0070 //    bias_ = value - rounded_value;
0071 //    return rounded_value;
0072 //
0073 // This class is thread-compatible.
0074 class ExponentialBiased {
0075  public:
0076   // The number of bits set by NextRandom.
0077   static constexpr int kPrngNumBits = 48;
0078 
0079   // `GetSkipCount()` returns the number of events to skip before some chosen
0080   // event happens. For example, randomly tossing a coin, we will on average
0081   // throw heads once before we get tails. We can simulate random coin tosses
0082   // using GetSkipCount() as:
0083   //
0084   //   ExponentialBiased eb;
0085   //   for (...) {
0086   //     int number_of_heads_before_tail = eb.GetSkipCount(1);
0087   //     for (int flips = 0; flips < number_of_heads_before_tail; ++flips) {
0088   //       printf("head...");
0089   //     }
0090   //     printf("tail\n");
0091   //   }
0092   //
0093   int64_t GetSkipCount(int64_t mean);
0094 
0095   // GetStride() returns the number of events required for a specific event to
0096   // happen. See the class comments for a usage example. `GetStride()` is
0097   // equivalent to `GetSkipCount(mean - 1) + 1`. When to use `GetStride()` or
0098   // `GetSkipCount()` depends mostly on what best fits the use case.
0099   int64_t GetStride(int64_t mean);
0100 
0101   // Computes a random number in the range [0, 1<<(kPrngNumBits+1) - 1]
0102   //
0103   // This is public to enable testing.
0104   static uint64_t NextRandom(uint64_t rnd);
0105 
0106  private:
0107   void Initialize();
0108 
0109   uint64_t rng_{0};
0110   double bias_{0};
0111   bool initialized_{false};
0112 };
0113 
0114 // Returns the next prng value.
0115 // pRNG is: aX+b mod c with a = 0x5DEECE66D, b =  0xB, c = 1<<48
0116 // This is the lrand64 generator.
0117 inline uint64_t ExponentialBiased::NextRandom(uint64_t rnd) {
0118   const uint64_t prng_mult = uint64_t{0x5DEECE66D};
0119   const uint64_t prng_add = 0xB;
0120   const uint64_t prng_mod_power = 48;
0121   const uint64_t prng_mod_mask =
0122       ~((~static_cast<uint64_t>(0)) << prng_mod_power);
0123   return (prng_mult * rnd + prng_add) & prng_mod_mask;
0124 }
0125 
0126 }  // namespace profiling_internal
0127 ABSL_NAMESPACE_END
0128 }  // namespace absl
0129 
0130 #endif  // ABSL_PROFILING_INTERNAL_EXPONENTIAL_BIASED_H_