1// Copyright (c) 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31// All Rights Reserved.
32//
33// Author: Daniel Ford
34
35#include "sampler.h"
36
37#include <algorithm>  // For min()
38#include <math.h>
39#include "base/commandlineflags.h"
40
41using std::min;
42
43// The approximate gap in bytes between sampling actions.
44// I.e., we take one sample approximately once every
45// tcmalloc_sample_parameter bytes of allocation
46// i.e. about once every 512KB if value is 1<<19.
47#ifdef NO_TCMALLOC_SAMPLES
48DEFINE_int64(tcmalloc_sample_parameter, 0,
49             "Unused: code is compiled with NO_TCMALLOC_SAMPLES");
50#else
51DEFINE_int64(tcmalloc_sample_parameter,
52             EnvToInt64("TCMALLOC_SAMPLE_PARAMETER", 0),
53             "The approximate gap in bytes between sampling actions. "
54             "This must be between 1 and 2^58.");
55#endif
56
57namespace tcmalloc {
58
59// Statics for Sampler
60double Sampler::log_table_[1<<kFastlogNumBits];
61
62// Populate the lookup table for FastLog2.
63// This approximates the log2 curve with a step function.
64// Steps have height equal to log2 of the mid-point of the step.
65void Sampler::PopulateFastLog2Table() {
66  for (int i = 0; i < (1<<kFastlogNumBits); i++) {
67    log_table_[i] = (log(1.0 + static_cast<double>(i+0.5)/(1<<kFastlogNumBits))
68                     / log(2.0));
69  }
70}
71
72int Sampler::GetSamplePeriod() {
73  return FLAGS_tcmalloc_sample_parameter;
74}
75
76// Run this before using your sampler
77void Sampler::Init(uint32_t seed) {
78  // Initialize PRNG
79  if (seed != 0) {
80    rnd_ = seed;
81  } else {
82    rnd_ = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this));
83    if (rnd_ == 0) {
84      rnd_ = 1;
85    }
86  }
87  // Step it forward 20 times for good measure
88  for (int i = 0; i < 20; i++) {
89    rnd_ = NextRandom(rnd_);
90  }
91  // Initialize counter
92  bytes_until_sample_ = PickNextSamplingPoint();
93}
94
95// Initialize the Statics for the Sampler class
96void Sampler::InitStatics() {
97  PopulateFastLog2Table();
98}
99
100// Generates a geometric variable with the specified mean (512K by default).
101// This is done by generating a random number between 0 and 1 and applying
102// the inverse cumulative distribution function for an exponential.
103// Specifically: Let m be the inverse of the sample period, then
104// the probability distribution function is m*exp(-mx) so the CDF is
105// p = 1 - exp(-mx), so
106// q = 1 - p = exp(-mx)
107// log_e(q) = -mx
108// -log_e(q)/m = x
109// log_2(q) * (-log_e(2) * 1/m) = x
110// In the code, q is actually in the range 1 to 2**26, hence the -26 below
111size_t Sampler::PickNextSamplingPoint() {
112  rnd_ = NextRandom(rnd_);
113  // Take the top 26 bits as the random number
114  // (This plus the 1<<58 sampling bound give a max possible step of
115  // 5194297183973780480 bytes.)
116  const uint64_t prng_mod_power = 48;  // Number of bits in prng
117  // The uint32_t cast is to prevent a (hard-to-reproduce) NAN
118  // under piii debug for some binaries.
119  double q = static_cast<uint32_t>(rnd_ >> (prng_mod_power - 26)) + 1.0;
120  // Put the computed p-value through the CDF of a geometric.
121  // For faster performance (save ~1/20th exec time), replace
122  // min(0.0, FastLog2(q) - 26)  by  (Fastlog2(q) - 26.000705)
123  // The value 26.000705 is used rather than 26 to compensate
124  // for inaccuracies in FastLog2 which otherwise result in a
125  // negative answer.
126  return static_cast<size_t>(min(0.0, (FastLog2(q) - 26)) * (-log(2.0)
127                             * FLAGS_tcmalloc_sample_parameter) + 1);
128}
129
130}  // namespace tcmalloc
131