blob: 20bf1ad57c69167688482a4542022b5cf6cfac0b [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.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * 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.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// 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
Austin Schuh745610d2015-09-06 18:19:50 -070060int Sampler::GetSamplePeriod() {
61 return FLAGS_tcmalloc_sample_parameter;
62}
63
64// Run this before using your sampler
Brian Silverman20350ac2021-11-17 18:19:55 -080065void Sampler::Init(uint64_t seed) {
66 ASSERT(seed != 0);
67
Austin Schuh745610d2015-09-06 18:19:50 -070068 // Initialize PRNG
Brian Silverman20350ac2021-11-17 18:19:55 -080069 rnd_ = seed;
Austin Schuh745610d2015-09-06 18:19:50 -070070 // Step it forward 20 times for good measure
71 for (int i = 0; i < 20; i++) {
72 rnd_ = NextRandom(rnd_);
73 }
74 // Initialize counter
75 bytes_until_sample_ = PickNextSamplingPoint();
76}
77
Brian Silverman20350ac2021-11-17 18:19:55 -080078#define MAX_SSIZE (static_cast<ssize_t>(static_cast<size_t>(static_cast<ssize_t>(-1)) >> 1))
Austin Schuh745610d2015-09-06 18:19:50 -070079
80// Generates a geometric variable with the specified mean (512K by default).
81// This is done by generating a random number between 0 and 1 and applying
82// the inverse cumulative distribution function for an exponential.
83// Specifically: Let m be the inverse of the sample period, then
84// the probability distribution function is m*exp(-mx) so the CDF is
85// p = 1 - exp(-mx), so
86// q = 1 - p = exp(-mx)
87// log_e(q) = -mx
88// -log_e(q)/m = x
89// log_2(q) * (-log_e(2) * 1/m) = x
90// In the code, q is actually in the range 1 to 2**26, hence the -26 below
Brian Silverman20350ac2021-11-17 18:19:55 -080091ssize_t Sampler::PickNextSamplingPoint() {
92 if (FLAGS_tcmalloc_sample_parameter <= 0) {
93 // In this case, we don't want to sample ever, and the larger a
94 // value we put here, the longer until we hit the slow path
95 // again. However, we have to support the flag changing at
96 // runtime, so pick something reasonably large (to keep overhead
97 // low) but small enough that we'll eventually start to sample
98 // again.
99 return 16 << 20;
100 }
101
Austin Schuh745610d2015-09-06 18:19:50 -0700102 rnd_ = NextRandom(rnd_);
103 // Take the top 26 bits as the random number
104 // (This plus the 1<<58 sampling bound give a max possible step of
105 // 5194297183973780480 bytes.)
106 const uint64_t prng_mod_power = 48; // Number of bits in prng
107 // The uint32_t cast is to prevent a (hard-to-reproduce) NAN
108 // under piii debug for some binaries.
109 double q = static_cast<uint32_t>(rnd_ >> (prng_mod_power - 26)) + 1.0;
110 // Put the computed p-value through the CDF of a geometric.
Brian Silverman20350ac2021-11-17 18:19:55 -0800111 double interval =
112 (log2(q) - 26) * (-log(2.0) * FLAGS_tcmalloc_sample_parameter);
113
114 // Very large values of interval overflow ssize_t. If we happen to
115 // hit such improbable condition, we simply cheat and clamp interval
116 // to largest supported value.
117 return static_cast<ssize_t>(
118 std::min<double>(interval, static_cast<double>(MAX_SSIZE)));
119}
120
121bool Sampler::RecordAllocationSlow(size_t k) {
122 if (!initialized_) {
123 initialized_ = true;
124 Init(reinterpret_cast<uintptr_t>(this));
125 if (static_cast<size_t>(bytes_until_sample_) >= k) {
126 bytes_until_sample_ -= k;
127 return true;
128 }
129 }
130 bytes_until_sample_ = PickNextSamplingPoint();
131 return FLAGS_tcmalloc_sample_parameter <= 0;
Austin Schuh745610d2015-09-06 18:19:50 -0700132}
133
134} // namespace tcmalloc