Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:00

0001 // Copyright 2019 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_INTERNAL_CORDZ_FUNCTIONS_H_
0016 #define ABSL_STRINGS_INTERNAL_CORDZ_FUNCTIONS_H_
0017 
0018 #include <stdint.h>
0019 
0020 #include "absl/base/attributes.h"
0021 #include "absl/base/config.h"
0022 #include "absl/base/optimization.h"
0023 
0024 namespace absl {
0025 ABSL_NAMESPACE_BEGIN
0026 namespace cord_internal {
0027 
0028 // Returns the current sample rate. This represents the average interval
0029 // between samples.
0030 int32_t get_cordz_mean_interval();
0031 
0032 // Sets the sample rate with the average interval between samples.
0033 void set_cordz_mean_interval(int32_t mean_interval);
0034 
0035 // Cordz is only enabled on Linux with thread_local support.
0036 #if defined(ABSL_INTERNAL_CORDZ_ENABLED)
0037 #error ABSL_INTERNAL_CORDZ_ENABLED cannot be set directly
0038 #elif defined(__linux__) && defined(ABSL_HAVE_THREAD_LOCAL)
0039 #define ABSL_INTERNAL_CORDZ_ENABLED 1
0040 #endif
0041 
0042 #ifdef ABSL_INTERNAL_CORDZ_ENABLED
0043 
0044 struct SamplingState {
0045   int64_t next_sample;
0046   int64_t sample_stride;
0047 };
0048 
0049 // cordz_next_sample is the number of events until the next sample event. If
0050 // the value is 1 or less, the code will check on the next event if cordz is
0051 // enabled, and if so, will sample the Cord. cordz is only enabled when we can
0052 // use thread locals.
0053 ABSL_CONST_INIT extern thread_local SamplingState cordz_next_sample;
0054 
0055 // Determines if the next sample should be profiled.
0056 // Returns:
0057 //   0: Do not sample
0058 //  >0: Sample with the stride of the last sampling period
0059 int64_t cordz_should_profile_slow(SamplingState& state);
0060 
0061 // Determines if the next sample should be profiled.
0062 // Returns:
0063 //   0: Do not sample
0064 //  >0: Sample with the stride of the last sampling period
0065 inline int64_t cordz_should_profile() {
0066   if (ABSL_PREDICT_TRUE(cordz_next_sample.next_sample > 1)) {
0067     cordz_next_sample.next_sample--;
0068     return 0;
0069   }
0070   return cordz_should_profile_slow(cordz_next_sample);
0071 }
0072 
0073 // Sets the interval until the next sample (for testing only)
0074 void cordz_set_next_sample_for_testing(int64_t next_sample);
0075 
0076 #else  // ABSL_INTERNAL_CORDZ_ENABLED
0077 
0078 inline int64_t cordz_should_profile() { return 0; }
0079 inline void cordz_set_next_sample_for_testing(int64_t) {}
0080 
0081 #endif  // ABSL_INTERNAL_CORDZ_ENABLED
0082 
0083 }  // namespace cord_internal
0084 ABSL_NAMESPACE_END
0085 }  // namespace absl
0086 
0087 #endif  // ABSL_STRINGS_INTERNAL_CORDZ_FUNCTIONS_H_