1 // 2 // 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_LIB_DEBUG_TRACE_H 20 #define GRPC_SRC_CORE_LIB_DEBUG_TRACE_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <atomic> 25 #include <map> 26 #include <string> 27 28 #include "absl/strings/string_view.h" 29 30 void grpc_tracer_init(); 31 void grpc_tracer_shutdown(void); 32 33 namespace grpc_core { 34 35 class TraceFlag; 36 class TraceFlagList { 37 public: 38 static bool Set(absl::string_view name, bool enabled); 39 static void Add(TraceFlag* flag); 40 static void SaveTo(std::map<std::string, bool>& values); 41 42 private: 43 static void LogAllTracers(); 44 static TraceFlag* root_tracer_; 45 }; 46 47 namespace testing { 48 void grpc_tracer_enable_flag(TraceFlag* flag); 49 } 50 51 class TraceFlag { 52 public: 53 TraceFlag(bool default_enabled, const char* name); 54 // TraceFlag needs to be trivially destructible since it is used as global 55 // variable. 56 ~TraceFlag() = default; 57 name()58 const char* name() const { return name_; } 59 60 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in 61 // opt builds (tracers are always on in dbg builds). The default in OSS is for 62 // tracers to be on since we support binary distributions of gRPC for the 63 // wrapped language (wr don't want to force recompilation to get tracing). 64 // Internally, however, for performance reasons, we compile them out by 65 // default, since internal build systems make recompiling trivial. 66 // 67 // Prefer GRPC_TRACE_FLAG_ENABLED() macro instead of using enabled() directly. 68 #define GRPC_USE_TRACERS // tracers on by default in OSS 69 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG) enabled()70 bool enabled() { return value_.load(std::memory_order_relaxed); } 71 #else enabled()72 bool enabled() { return false; } 73 #endif // defined(GRPC_USE_TRACERS) || !defined(NDEBUG) 74 75 private: 76 friend void testing::grpc_tracer_enable_flag(TraceFlag* flag); 77 friend class TraceFlagList; 78 set_enabled(bool enabled)79 void set_enabled(bool enabled) { 80 value_.store(enabled, std::memory_order_relaxed); 81 } 82 83 TraceFlag* next_tracer_; 84 const char* const name_; 85 std::atomic<bool> value_; 86 }; 87 88 #define GRPC_TRACE_FLAG_ENABLED(f) GPR_UNLIKELY((f).enabled()) 89 90 #ifndef NDEBUG 91 typedef TraceFlag DebugOnlyTraceFlag; 92 #else 93 class DebugOnlyTraceFlag { 94 public: DebugOnlyTraceFlag(bool,const char *)95 constexpr DebugOnlyTraceFlag(bool /*default_enabled*/, const char* /*name*/) { 96 } enabled()97 constexpr bool enabled() const { return false; } name()98 constexpr const char* name() const { return "DebugOnlyTraceFlag"; } 99 100 private: set_enabled(bool)101 void set_enabled(bool /*enabled*/) {} 102 }; 103 #endif 104 105 class SavedTraceFlags { 106 public: 107 SavedTraceFlags(); 108 void Restore(); 109 110 private: 111 std::map<std::string, bool> values_; 112 }; 113 114 } // namespace grpc_core 115 116 #endif // GRPC_SRC_CORE_LIB_DEBUG_TRACE_H 117