sampler.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef TCMALLOC_SAMPLER_H_
  35. #define TCMALLOC_SAMPLER_H_
  36. #include "config.h"
  37. #include <stddef.h> // for size_t
  38. #ifdef HAVE_STDINT_H
  39. #include <stdint.h> // for uint64_t, uint32_t, int32_t
  40. #endif
  41. #include <string.h> // for memcpy
  42. #include "base/basictypes.h" // for ASSERT
  43. #include "internal_logging.h" // for ASSERT
  44. namespace tcmalloc {
  45. //-------------------------------------------------------------------
  46. // Sampler to decide when to create a sample trace for an allocation
  47. // Not thread safe: Each thread should have it's own sampler object.
  48. // Caller must use external synchronization if used
  49. // from multiple threads.
  50. //
  51. // With 512K average sample step (the default):
  52. // the probability of sampling a 4K allocation is about 0.00778
  53. // the probability of sampling a 1MB allocation is about 0.865
  54. // the probability of sampling a 1GB allocation is about 1.00000
  55. // In general, the probablity of sampling is an allocation of size X
  56. // given a flag value of Y (default 1M) is:
  57. // 1 - e^(-X/Y)
  58. //
  59. // With 128K average sample step:
  60. // the probability of sampling a 1MB allocation is about 0.99966
  61. // the probability of sampling a 1GB allocation is about 1.0
  62. // (about 1 - 2**(-26))
  63. // With 1M average sample step:
  64. // the probability of sampling a 4K allocation is about 0.00390
  65. // the probability of sampling a 1MB allocation is about 0.632
  66. // the probability of sampling a 1GB allocation is about 1.0
  67. //
  68. // The sampler works by representing memory as a long stream from
  69. // which allocations are taken. Some of the bytes in this stream are
  70. // marked and if an allocation includes a marked byte then it is
  71. // sampled. Bytes are marked according to a Poisson point process
  72. // with each byte being marked independently with probability
  73. // p = 1/tcmalloc_sample_parameter. This makes the probability
  74. // of sampling an allocation of X bytes equal to the CDF of
  75. // a geometric with mean tcmalloc_sample_parameter. (ie. the
  76. // probability that at least one byte in the range is marked). This
  77. // is accurately given by the CDF of the corresponding exponential
  78. // distribution : 1 - e^(X/tcmalloc_sample_parameter_)
  79. // Independence of the byte marking ensures independence of
  80. // the sampling of each allocation.
  81. //
  82. // This scheme is implemented by noting that, starting from any
  83. // fixed place, the number of bytes until the next marked byte
  84. // is geometrically distributed. This number is recorded as
  85. // bytes_until_sample_. Every allocation subtracts from this
  86. // number until it is less than 0. When this happens the current
  87. // allocation is sampled.
  88. //
  89. // When an allocation occurs, bytes_until_sample_ is reset to
  90. // a new independtly sampled geometric number of bytes. The
  91. // memoryless property of the point process means that this may
  92. // be taken as the number of bytes after the end of the current
  93. // allocation until the next marked byte. This ensures that
  94. // very large allocations which would intersect many marked bytes
  95. // only result in a single call to PickNextSamplingPoint.
  96. //-------------------------------------------------------------------
  97. class PERFTOOLS_DLL_DECL Sampler {
  98. public:
  99. // Initialize this sampler.
  100. // Passing a seed of 0 gives a non-deterministic
  101. // seed value given by casting the object ("this")
  102. void Init(uint32_t seed);
  103. void Cleanup();
  104. // Record allocation of "k" bytes. Return true iff allocation
  105. // should be sampled
  106. bool SampleAllocation(size_t k);
  107. // Generate a geometric with mean 512K (or FLAG_tcmalloc_sample_parameter)
  108. size_t PickNextSamplingPoint();
  109. // Initialize the statics for the Sampler class
  110. static void InitStatics();
  111. // Returns the current sample period
  112. int GetSamplePeriod();
  113. // The following are public for the purposes of testing
  114. static uint64_t NextRandom(uint64_t rnd_); // Returns the next prng value
  115. static double FastLog2(const double & d); // Computes Log2(x) quickly
  116. static void PopulateFastLog2Table(); // Populate the lookup table
  117. private:
  118. size_t bytes_until_sample_; // Bytes until we sample next
  119. uint64_t rnd_; // Cheap random number generator
  120. // Statics for the fast log
  121. // Note that this code may not depend on anything in //util
  122. // hence the duplication of functionality here
  123. static const int kFastlogNumBits = 10;
  124. static const int kFastlogMask = (1 << kFastlogNumBits) - 1;
  125. static double log_table_[1<<kFastlogNumBits]; // Constant
  126. };
  127. inline bool Sampler::SampleAllocation(size_t k) {
  128. if (bytes_until_sample_ < k) {
  129. bytes_until_sample_ = PickNextSamplingPoint();
  130. return true;
  131. } else {
  132. bytes_until_sample_ -= k;
  133. return false;
  134. }
  135. }
  136. // Inline functions which are public for testing purposes
  137. // Returns the next prng value.
  138. // pRNG is: aX+b mod c with a = 0x5DEECE66D, b = 0xB, c = 1<<48
  139. // This is the lrand64 generator.
  140. inline uint64_t Sampler::NextRandom(uint64_t rnd) {
  141. const uint64_t prng_mult = 0x5DEECE66DLL;
  142. const uint64_t prng_add = 0xB;
  143. const uint64_t prng_mod_power = 48;
  144. const uint64_t prng_mod_mask =
  145. ~((~static_cast<uint64_t>(0)) << prng_mod_power);
  146. return (prng_mult * rnd + prng_add) & prng_mod_mask;
  147. }
  148. // Adapted from //util/math/fastmath.[h|cc] by Noam Shazeer
  149. // This mimics the VeryFastLog2 code in those files
  150. inline double Sampler::FastLog2(const double & d) {
  151. ASSERT(d>0);
  152. COMPILE_ASSERT(sizeof(d) == sizeof(uint64_t), DoubleMustBe64Bits);
  153. uint64_t x;
  154. memcpy(&x, &d, sizeof(x)); // we depend on the compiler inlining this
  155. const uint32_t x_high = x >> 32;
  156. const uint32_t y = x_high >> (20 - kFastlogNumBits) & kFastlogMask;
  157. const int32_t exponent = ((x_high >> 20) & 0x7FF) - 1023;
  158. return exponent + log_table_[y];
  159. }
  160. } // namespace tcmalloc
  161. #endif // TCMALLOC_SAMPLER_H_