vlog_config.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2022 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. // -----------------------------------------------------------------------------
  16. // vlog_config.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines `VLogSite`, a public primitive that represents
  20. // a callsite for the `VLOG` family of macros and related libraries.
  21. // It also declares and defines multiple internal utilities used to implement
  22. // `VLOG`, such as `VLogSiteManager`.
  23. #ifndef ABSL_LOG_INTERNAL_VLOG_CONFIG_H_
  24. #define ABSL_LOG_INTERNAL_VLOG_CONFIG_H_
  25. // IWYU pragma: private, include "absl/log/log.h"
  26. #include <atomic>
  27. #include <cstdint>
  28. #include <functional>
  29. #include <limits>
  30. #include <type_traits>
  31. #include "absl/base/attributes.h"
  32. #include "absl/base/config.h"
  33. #include "absl/base/optimization.h"
  34. #include "absl/base/thread_annotations.h"
  35. #include "absl/strings/string_view.h"
  36. namespace absl {
  37. ABSL_NAMESPACE_BEGIN
  38. namespace log_internal {
  39. class SyntheticBinary;
  40. class VLogSite;
  41. int RegisterAndInitialize(VLogSite* v);
  42. void UpdateVLogSites();
  43. constexpr int kUseFlag = (std::numeric_limits<int16_t>::min)();
  44. // Represents a unique callsite for a `VLOG()` or `VLOG_IS_ON()` call.
  45. //
  46. // Libraries that provide `VLOG`-like functionality should use this to
  47. // efficiently handle --vmodule.
  48. //
  49. // VLogSite objects must not be destroyed until the program exits. Doing so will
  50. // probably yield nasty segfaults in VLogSiteManager::UpdateLogSites(). The
  51. // recommendation is to make all such objects function-local statics.
  52. class VLogSite final {
  53. public:
  54. // `f` must not be destroyed until the program exits.
  55. explicit constexpr VLogSite(const char* f)
  56. : file_(f), v_(kUninitialized), next_(nullptr) {}
  57. VLogSite(const VLogSite&) = delete;
  58. VLogSite& operator=(const VLogSite&) = delete;
  59. // Inlining the function yields a ~3x performance improvement at the cost of a
  60. // 1.5x code size increase at the call site.
  61. // Takes locks but does not allocate memory.
  62. ABSL_ATTRIBUTE_ALWAYS_INLINE
  63. bool IsEnabled(int level) {
  64. int stale_v = v_.load(std::memory_order_relaxed);
  65. if (ABSL_PREDICT_TRUE(level > stale_v)) {
  66. return false;
  67. }
  68. // We put everything other than the fast path, i.e. vlogging is initialized
  69. // but not on, behind an out-of-line function to reduce code size.
  70. // "level" is almost always a call-site constant, so we can save a bit
  71. // of code space by special-casing for a few common levels.
  72. #if ABSL_HAVE_BUILTIN(__builtin_constant_p) || defined(__GNUC__)
  73. if (__builtin_constant_p(level)) {
  74. if (level == 0) return SlowIsEnabled0(stale_v);
  75. if (level == 1) return SlowIsEnabled1(stale_v);
  76. if (level == 2) return SlowIsEnabled2(stale_v);
  77. if (level == 3) return SlowIsEnabled3(stale_v);
  78. if (level == 4) return SlowIsEnabled4(stale_v);
  79. if (level == 5) return SlowIsEnabled5(stale_v);
  80. }
  81. #endif
  82. return SlowIsEnabled(stale_v, level);
  83. }
  84. private:
  85. friend int log_internal::RegisterAndInitialize(VLogSite* v);
  86. friend void log_internal::UpdateVLogSites();
  87. friend class log_internal::SyntheticBinary;
  88. static constexpr int kUninitialized = (std::numeric_limits<int>::max)();
  89. // SlowIsEnabled performs slower checks to determine whether a log site is
  90. // enabled. Because it is expected to be called somewhat rarely
  91. // (comparatively), it is not inlined to save on code size.
  92. //
  93. // Prerequisites to calling SlowIsEnabled:
  94. // 1) stale_v is uninitialized OR
  95. // 2) stale_v is initialized and >= level (meaning we must log).
  96. // Takes locks but does not allocate memory.
  97. ABSL_ATTRIBUTE_NOINLINE
  98. bool SlowIsEnabled(int stale_v, int level);
  99. ABSL_ATTRIBUTE_NOINLINE bool SlowIsEnabled0(int stale_v);
  100. ABSL_ATTRIBUTE_NOINLINE bool SlowIsEnabled1(int stale_v);
  101. ABSL_ATTRIBUTE_NOINLINE bool SlowIsEnabled2(int stale_v);
  102. ABSL_ATTRIBUTE_NOINLINE bool SlowIsEnabled3(int stale_v);
  103. ABSL_ATTRIBUTE_NOINLINE bool SlowIsEnabled4(int stale_v);
  104. ABSL_ATTRIBUTE_NOINLINE bool SlowIsEnabled5(int stale_v);
  105. // This object is too size-sensitive to use absl::string_view.
  106. const char* const file_;
  107. std::atomic<int> v_;
  108. std::atomic<VLogSite*> next_;
  109. };
  110. static_assert(std::is_trivially_destructible<VLogSite>::value,
  111. "VLogSite must be trivially destructible");
  112. // Returns the current verbose log level of `file`.
  113. // Does not allocate memory.
  114. int VLogLevel(absl::string_view file);
  115. // Registers a site `v` to get updated as `vmodule` and `v` change. Also
  116. // initializes the site based on their current values, and returns that result.
  117. // Does not allocate memory.
  118. int RegisterAndInitialize(VLogSite* v);
  119. // Allocates memory.
  120. void UpdateVLogSites();
  121. // Completely overwrites the saved value of `vmodule`.
  122. // Allocates memory.
  123. void UpdateVModule(absl::string_view vmodule);
  124. // Updates the global verbosity level to `v` and returns the prior value.
  125. // Allocates memory.
  126. int UpdateGlobalVLogLevel(int v);
  127. // Atomically prepends `module_pattern=log_level` to the start of vmodule.
  128. // Returns the prior value for `module_pattern` if there was an exact match and
  129. // `global_v` otherwise.
  130. // Allocates memory.
  131. int PrependVModule(absl::string_view module_pattern, int log_level);
  132. // Registers `on_update` to be called whenever `v` or `vmodule` change.
  133. // Allocates memory.
  134. void OnVLogVerbosityUpdate(std::function<void()> cb);
  135. // Does not allocate memory.
  136. VLogSite* SetVModuleListHeadForTestOnly(VLogSite* v);
  137. } // namespace log_internal
  138. ABSL_NAMESPACE_END
  139. } // namespace absl
  140. #endif // ABSL_LOG_INTERNAL_VLOG_CONFIG_H_