• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/profiling/internal/periodic_sampler.h"
16 
17 #include <atomic>
18 
19 #include "absl/profiling/internal/exponential_biased.h"
20 
21 namespace absl {
22 ABSL_NAMESPACE_BEGIN
23 namespace profiling_internal {
24 
GetExponentialBiased(int period)25 int64_t PeriodicSamplerBase::GetExponentialBiased(int period) noexcept {
26   return rng_.GetStride(period);
27 }
28 
SubtleConfirmSample()29 bool PeriodicSamplerBase::SubtleConfirmSample() noexcept {
30   int current_period = period();
31 
32   // Deal with period case 0 (always off) and 1 (always on)
33   if (ABSL_PREDICT_FALSE(current_period < 2)) {
34     stride_ = 0;
35     return current_period == 1;
36   }
37 
38   // Check if this is the first call to Sample()
39   if (ABSL_PREDICT_FALSE(stride_ == 1)) {
40     stride_ = static_cast<uint64_t>(-GetExponentialBiased(current_period));
41     if (static_cast<int64_t>(stride_) < -1) {
42       ++stride_;
43       return false;
44     }
45   }
46 
47   stride_ = static_cast<uint64_t>(-GetExponentialBiased(current_period));
48   return true;
49 }
50 
51 }  // namespace profiling_internal
52 ABSL_NAMESPACE_END
53 }  // namespace absl
54