cordz_functions.h 2.8 KB

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