|
| 1 | +// <copyright file="JavaPoissonSampler.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +namespace AllocSimulator |
| 7 | +{ |
| 8 | + // from https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/threadHeapSampler.cpp |
| 9 | + public class JavaPoissonSampler : ISampler |
| 10 | + { |
| 11 | + private const ulong MeanSamplingSize = 512 * 1024; // 512 KB is the mean of the distribution |
| 12 | + private const double MinusLog2 = -0.6931471805599453; // = - ln(2) |
| 13 | + |
| 14 | + // for fast randomizer |
| 15 | + private const ulong PrngMult = 0x5DEECE66D; |
| 16 | + private const ulong PrngAdd = 0xB; |
| 17 | + private const ulong PrngModMask = (1 << 48) - 1; |
| 18 | + |
| 19 | + private ulong _totalAllocatedAmount; |
| 20 | + private ulong _threshold; // number of bytes until the next sample |
| 21 | + private ulong _random; |
| 22 | + |
| 23 | + public JavaPoissonSampler() |
| 24 | + { |
| 25 | + _random = (ulong)Random.Shared.NextInt64(1, 281474976710656); // from 1 to 2^48 |
| 26 | + _threshold = GetNextThreshold(); |
| 27 | + } |
| 28 | + |
| 29 | + public bool ShouldSample(long size) |
| 30 | + { |
| 31 | + _totalAllocatedAmount += (ulong)size; |
| 32 | + var shouldSample = _totalAllocatedAmount > _threshold; |
| 33 | + |
| 34 | + if (shouldSample) |
| 35 | + { |
| 36 | + _totalAllocatedAmount = 0; |
| 37 | + _threshold = GetNextThreshold(); |
| 38 | + } |
| 39 | + |
| 40 | + return shouldSample; |
| 41 | + } |
| 42 | + |
| 43 | + // Generates a geometric variable with the specified mean (512K by default). |
| 44 | + // This is done by generating a random number between 0 and 1 and applying |
| 45 | + // the inverse cumulative distribution function for an exponential. |
| 46 | + // Specifically: Let m be the inverse of the sample interval, then |
| 47 | + // the probability distribution function is m*exp(-mx) so the CDF is |
| 48 | + // p = 1 - exp(-mx), so |
| 49 | + // q = 1 - p = exp(-mx) |
| 50 | + // log_e(q) = -mx |
| 51 | + // -log_e(q)/m = x |
| 52 | + // log_2(q) * (-log_e(2) * 1/m) = x |
| 53 | + // In the code, q is actually in the range 1 to 2**26, hence the -26 below |
| 54 | + private ulong GetNextThreshold() |
| 55 | + { |
| 56 | + _random = GetNextRandom(_random); |
| 57 | + |
| 58 | + // Take the top 26 bits as the random number |
| 59 | + // (This plus a 1<<58 sampling bound gives a max possible step of |
| 60 | + // 5194297183973780480 bytes. In this case, |
| 61 | + // for sample_parameter = 1<<19, max possible step is |
| 62 | + // 9448372 bytes (24 bits). |
| 63 | + |
| 64 | + // 48 is the number of bits in prng |
| 65 | + // The uint32_t cast is to prevent a (hard-to-reproduce) NAN |
| 66 | + // under piii debug for some binaries. |
| 67 | + double q = (uint)(_random >> (48 - 26)) + 1.0; |
| 68 | + // Put the computed p-value through the CDF of a geometric. |
| 69 | + // For faster performance (save ~1/20th exec time), replace |
| 70 | + // min(0.0, FastLog2(q) - 26) by (Fastlog2(q) - 26.000705) |
| 71 | + // The value 26.000705 is used rather than 26 to compensate |
| 72 | + // for inaccuracies in FastLog2 which otherwise result in a |
| 73 | + // negative answer. |
| 74 | + double log_val = (Log2(q) - 26); |
| 75 | + if (log_val > 0.0) |
| 76 | + { |
| 77 | + log_val = 0.0; |
| 78 | + } |
| 79 | + |
| 80 | + double result = (log_val * (MinusLog2 * (MeanSamplingSize))) + 1; |
| 81 | + ulong interval = (ulong)result; |
| 82 | + _threshold = interval; |
| 83 | + return _threshold; |
| 84 | + } |
| 85 | + |
| 86 | + // Returns the next prng value. |
| 87 | + // pRNG is: aX+b mod c with a = 0x5DEECE66D, b = 0xB, c = 1<<48 |
| 88 | + // This is the lrand64 generator. |
| 89 | + private ulong GetNextRandom(ulong random) |
| 90 | + { |
| 91 | + return ((PrngMult * random) + PrngAdd) & PrngModMask; |
| 92 | + } |
| 93 | + |
| 94 | + private double Log2(double d) |
| 95 | + { |
| 96 | + return Math.Log2(d); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments