diff options
author | Chris Kennelly <ckennelly@google.com> | 2024-03-18 11:47:55 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-03-18 11:48:40 -0700 |
commit | 50a88673355a93cf35308f3a05cc48ee02bbb613 (patch) | |
tree | d2d742664ba80b3cc8ad898c56c40f8bc63fe10b /absl/strings/internal/cordz_functions.cc | |
parent | 5e61a28e48e324b5cac13a6a1e3b6ba423ec2a6f (diff) | |
download | abseil-50a88673355a93cf35308f3a05cc48ee02bbb613.tar.gz abseil-50a88673355a93cf35308f3a05cc48ee02bbb613.tar.bz2 abseil-50a88673355a93cf35308f3a05cc48ee02bbb613.zip |
Record sampling stride in cord profiling to facilitate unsampling.
The sampling rate may change over time, so this allows us to weight samples by
the value observed when we made the sampling decision.
PiperOrigin-RevId: 616900100
Change-Id: I9b1affdba93f5f48367cb7503916296b2d84709a
Diffstat (limited to 'absl/strings/internal/cordz_functions.cc')
-rw-r--r-- | absl/strings/internal/cordz_functions.cc | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/absl/strings/internal/cordz_functions.cc b/absl/strings/internal/cordz_functions.cc index 20d314f0..6033d046 100644 --- a/absl/strings/internal/cordz_functions.cc +++ b/absl/strings/internal/cordz_functions.cc @@ -40,13 +40,15 @@ std::atomic<int> g_cordz_mean_interval(50000); // Special negative 'not initialized' per thread value for cordz_next_sample. static constexpr int64_t kInitCordzNextSample = -1; -ABSL_CONST_INIT thread_local int64_t cordz_next_sample = kInitCordzNextSample; +ABSL_CONST_INIT thread_local SamplingState cordz_next_sample = { + kInitCordzNextSample, 1}; // kIntervalIfDisabled is the number of profile-eligible events need to occur // before the code will confirm that cordz is still disabled. constexpr int64_t kIntervalIfDisabled = 1 << 16; -ABSL_ATTRIBUTE_NOINLINE bool cordz_should_profile_slow() { +ABSL_ATTRIBUTE_NOINLINE int64_t +cordz_should_profile_slow(SamplingState& state) { thread_local absl::profiling_internal::ExponentialBiased exponential_biased_generator; @@ -55,30 +57,34 @@ ABSL_ATTRIBUTE_NOINLINE bool cordz_should_profile_slow() { // Check if we disabled profiling. If so, set the next sample to a "large" // number to minimize the overhead of the should_profile codepath. if (mean_interval <= 0) { - cordz_next_sample = kIntervalIfDisabled; - return false; + state = {kIntervalIfDisabled, kIntervalIfDisabled}; + return 0; } // Check if we're always sampling. if (mean_interval == 1) { - cordz_next_sample = 1; - return true; + state = {1, 1}; + return 1; } - if (cordz_next_sample <= 0) { + if (cordz_next_sample.next_sample <= 0) { // If first check on current thread, check cordz_should_profile() // again using the created (initial) stride in cordz_next_sample. - const bool initialized = cordz_next_sample != kInitCordzNextSample; - cordz_next_sample = exponential_biased_generator.GetStride(mean_interval); - return initialized || cordz_should_profile(); + const bool initialized = + cordz_next_sample.next_sample != kInitCordzNextSample; + auto old_stride = state.sample_stride; + auto stride = exponential_biased_generator.GetStride(mean_interval); + state = {stride, stride}; + bool should_sample = initialized || cordz_should_profile() > 0; + return should_sample ? old_stride : 0; } - --cordz_next_sample; - return false; + --state.next_sample; + return 0; } void cordz_set_next_sample_for_testing(int64_t next_sample) { - cordz_next_sample = next_sample; + cordz_next_sample = {next_sample, next_sample}; } #endif // ABSL_INTERNAL_CORDZ_ENABLED |