• 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 #ifndef ABSL_STRINGS_CORDZ_FUNCTIONS_H_
16 #define ABSL_STRINGS_CORDZ_FUNCTIONS_H_
17 
18 #include <stdint.h>
19 
20 #include "absl/base/attributes.h"
21 #include "absl/base/config.h"
22 #include "absl/base/optimization.h"
23 
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 namespace cord_internal {
27 
28 // Returns the current sample rate. This represents the average interval
29 // between samples.
30 int32_t get_cordz_mean_interval();
31 
32 // Sets the sample rate with the average interval between samples.
33 void set_cordz_mean_interval(int32_t mean_interval);
34 
35 // Enable cordz unless any of the following applies:
36 // - no thread local support
37 // - MSVC build
38 // - Android build
39 // - Apple build
40 // - DLL build
41 // Hashtablez is turned off completely in opensource builds.
42 // MSVC's static atomics are dynamically initialized in debug mode, which breaks
43 // sampling.
44 #if defined(ABSL_HAVE_THREAD_LOCAL) && !defined(_MSC_VER)  && \
45     !defined(ABSL_BUILD_DLL) && !defined(ABSL_CONSUME_DLL) && \
46     !defined(__ANDROID__) && !defined(__APPLE__)
47 #define ABSL_INTERNAL_CORDZ_ENABLED 1
48 #endif
49 
50 #ifdef ABSL_INTERNAL_CORDZ_ENABLED
51 
52 // cordz_next_sample is the number of events until the next sample event. If
53 // the value is 1 or less, the code will check on the next event if cordz is
54 // enabled, and if so, will sample the Cord. cordz is only enabled when we can
55 // use thread locals.
56 ABSL_CONST_INIT extern thread_local int64_t cordz_next_sample;
57 
58 // Determines if the next sample should be profiled. If it is, the value pointed
59 // at by next_sample will be set with the interval until the next sample.
60 bool cordz_should_profile_slow();
61 
62 // Returns true if the next cord should be sampled.
cordz_should_profile()63 inline bool cordz_should_profile() {
64   if (ABSL_PREDICT_TRUE(cordz_next_sample > 1)) {
65     cordz_next_sample--;
66     return false;
67   }
68   return cordz_should_profile_slow();
69 }
70 
71 // Sets the interval until the next sample (for testing only)
72 void cordz_set_next_sample_for_testing(int64_t next_sample);
73 
74 #else  // ABSL_INTERNAL_CORDZ_ENABLED
75 
cordz_should_profile()76 inline bool cordz_should_profile() { return false; }
cordz_set_next_sample_for_testing(int64_t)77 inline void cordz_set_next_sample_for_testing(int64_t) {}
78 
79 #endif  // ABSL_INTERNAL_CORDZ_ENABLED
80 
81 }  // namespace cord_internal
82 ABSL_NAMESPACE_END
83 }  // namespace absl
84 
85 #endif  // ABSL_STRINGS_CORDZ_FUNCTIONS_H_
86