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