sampler.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // -*- 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. // All Rights Reserved.
  32. //
  33. // Author: Daniel Ford
  34. #include "sampler.h"
  35. #include <algorithm> // For min()
  36. #include <math.h>
  37. #include "base/commandlineflags.h"
  38. using std::min;
  39. // The approximate gap in bytes between sampling actions.
  40. // I.e., we take one sample approximately once every
  41. // tcmalloc_sample_parameter bytes of allocation
  42. // i.e. about once every 512KB if value is 1<<19.
  43. #ifdef NO_TCMALLOC_SAMPLES
  44. DEFINE_int64(tcmalloc_sample_parameter, 0,
  45. "Unused: code is compiled with NO_TCMALLOC_SAMPLES");
  46. #else
  47. DEFINE_int64(tcmalloc_sample_parameter,
  48. EnvToInt64("TCMALLOC_SAMPLE_PARAMETER", 0),
  49. "The approximate gap in bytes between sampling actions. "
  50. "This must be between 1 and 2^58.");
  51. #endif
  52. namespace tcmalloc {
  53. // Statics for Sampler
  54. double Sampler::log_table_[1<<kFastlogNumBits];
  55. // Populate the lookup table for FastLog2.
  56. // This approximates the log2 curve with a step function.
  57. // Steps have height equal to log2 of the mid-point of the step.
  58. void Sampler::PopulateFastLog2Table() {
  59. for (int i = 0; i < (1<<kFastlogNumBits); i++) {
  60. log_table_[i] = (log(1.0 + static_cast<double>(i+0.5)/(1<<kFastlogNumBits))
  61. / log(2.0));
  62. }
  63. }
  64. int Sampler::GetSamplePeriod() {
  65. return FLAGS_tcmalloc_sample_parameter;
  66. }
  67. // Run this before using your sampler
  68. void Sampler::Init(uint32_t seed) {
  69. // Initialize PRNG
  70. if (seed != 0) {
  71. rnd_ = seed;
  72. } else {
  73. rnd_ = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this));
  74. if (rnd_ == 0) {
  75. rnd_ = 1;
  76. }
  77. }
  78. // Step it forward 20 times for good measure
  79. for (int i = 0; i < 20; i++) {
  80. rnd_ = NextRandom(rnd_);
  81. }
  82. // Initialize counter
  83. bytes_until_sample_ = PickNextSamplingPoint();
  84. }
  85. // Initialize the Statics for the Sampler class
  86. void Sampler::InitStatics() {
  87. PopulateFastLog2Table();
  88. }
  89. // Generates a geometric variable with the specified mean (512K by default).
  90. // This is done by generating a random number between 0 and 1 and applying
  91. // the inverse cumulative distribution function for an exponential.
  92. // Specifically: Let m be the inverse of the sample period, then
  93. // the probability distribution function is m*exp(-mx) so the CDF is
  94. // p = 1 - exp(-mx), so
  95. // q = 1 - p = exp(-mx)
  96. // log_e(q) = -mx
  97. // -log_e(q)/m = x
  98. // log_2(q) * (-log_e(2) * 1/m) = x
  99. // In the code, q is actually in the range 1 to 2**26, hence the -26 below
  100. size_t Sampler::PickNextSamplingPoint() {
  101. rnd_ = NextRandom(rnd_);
  102. // Take the top 26 bits as the random number
  103. // (This plus the 1<<58 sampling bound give a max possible step of
  104. // 5194297183973780480 bytes.)
  105. const uint64_t prng_mod_power = 48; // Number of bits in prng
  106. // The uint32_t cast is to prevent a (hard-to-reproduce) NAN
  107. // under piii debug for some binaries.
  108. double q = static_cast<uint32_t>(rnd_ >> (prng_mod_power - 26)) + 1.0;
  109. // Put the computed p-value through the CDF of a geometric.
  110. // For faster performance (save ~1/20th exec time), replace
  111. // min(0.0, FastLog2(q) - 26) by (Fastlog2(q) - 26.000705)
  112. // The value 26.000705 is used rather than 26 to compensate
  113. // for inaccuracies in FastLog2 which otherwise result in a
  114. // negative answer.
  115. return static_cast<size_t>(min(0.0, (FastLog2(q) - 26)) * (-log(2.0)
  116. * FLAGS_tcmalloc_sample_parameter) + 1);
  117. }
  118. } // namespace tcmalloc