debug/trace.h
Go to the documentation of this file.
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_CORE_LIB_DEBUG_TRACE_H
20 #define GRPC_CORE_LIB_DEBUG_TRACE_H
21 
22 #if defined(__has_feature)
23 #if __has_feature(thread_sanitizer)
24 #define GRPC_THREADSAFE_TRACER
25 #endif
26 #endif
27 
29 
30 #ifdef GRPC_THREADSAFE_TRACER
31 #include <atomic>
32 #endif
33 
36 
38 
39 // TODO(veblush): Remove this deprecated function once codes depending on this
40 // function are updated in the internal repo.
41 void grpc_tracer_init(const char* env_var_name);
42 
43 void grpc_tracer_init();
44 void grpc_tracer_shutdown(void);
45 
46 namespace grpc_core {
47 
48 class TraceFlag;
50  public:
51  static bool Set(const char* name, bool enabled);
52  static void Add(TraceFlag* flag);
53 
54  private:
55  static void LogAllTracers();
57 };
58 
59 namespace testing {
61 }
62 
63 class TraceFlag {
64  public:
65  TraceFlag(bool default_enabled, const char* name);
66  // TraceFlag needs to be trivially destructible since it is used as global
67  // variable.
68  ~TraceFlag() = default;
69 
70  const char* name() const { return name_; }
71 
72 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in
73 // opt builds (tracers are always on in dbg builds). The default in OSS is for
74 // tracers to be on since we support binary distributions of gRPC for the
75 // wrapped language (wr don't want to force recompilation to get tracing).
76 // Internally, however, for performance reasons, we compile them out by
77 // default, since internal build systems make recompiling trivial.
78 //
79 // Prefer GRPC_TRACE_FLAG_ENABLED() macro instead of using enabled() directly.
80 #define GRPC_USE_TRACERS // tracers on by default in OSS
81 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG)
82  bool enabled() {
83 #ifdef GRPC_THREADSAFE_TRACER
84  return value_.load(std::memory_order_relaxed);
85 #else
86  return value_;
87 #endif // GRPC_THREADSAFE_TRACER
88  }
89 #else
90  bool enabled() { return false; }
91 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */
92 
93  private:
95  friend class TraceFlagList;
96 
97  void set_enabled(bool enabled) {
98 #ifdef GRPC_THREADSAFE_TRACER
99  value_.store(enabled, std::memory_order_relaxed);
100 #else
101  value_ = enabled;
102 #endif
103  }
104 
106  const char* const name_;
107 #ifdef GRPC_THREADSAFE_TRACER
108  std::atomic<bool> value_;
109 #else
110  bool value_;
111 #endif
112 };
113 
114 #define GRPC_TRACE_FLAG_ENABLED(f) GPR_UNLIKELY((f).enabled())
115 
116 #ifndef NDEBUG
118 #else
119 class DebugOnlyTraceFlag {
120  public:
121  constexpr DebugOnlyTraceFlag(bool /*default_enabled*/, const char* /*name*/) {
122  }
123  constexpr bool enabled() const { return false; }
124  constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
125 
126  private:
127  void set_enabled(bool /*enabled*/) {}
128 };
129 #endif
130 
131 } // namespace grpc_core
132 
133 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */
flag
uint32_t flag
Definition: ssl_versions.cc:162
testing
Definition: aws_request_signer_test.cc:25
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::TraceFlag::name
const char * name() const
Definition: debug/trace.h:70
grpc_core::TraceFlag::TraceFlag
TraceFlag(bool default_enabled, const char *name)
Definition: debug/trace.cc:87
setup.name
name
Definition: setup.py:542
grpc_tracer_shutdown
void grpc_tracer_shutdown(void)
Definition: debug/trace.cc:151
grpc_core::TraceFlag::value_
bool value_
Definition: debug/trace.h:110
GPR_GLOBAL_CONFIG_DECLARE_STRING
GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_trace)
memory.h
grpc_core::TraceFlagList::LogAllTracers
static void LogAllTracers()
Definition: debug/trace.cc:78
grpc_core::TraceFlagList::root_tracer_
static TraceFlag * root_tracer_
Definition: debug/trace.h:56
grpc_core::TraceFlag::name_
const char *const name_
Definition: debug/trace.h:106
grpc_core::TraceFlagList::Set
static bool Set(const char *name, bool enabled)
Definition: debug/trace.cc:42
grpc_core::TraceFlag::~TraceFlag
~TraceFlag()=default
grpc_tracer_init
void grpc_tracer_init(const char *env_var_name)
Definition: debug/trace.cc:141
grpc_core::TraceFlag
Definition: debug/trace.h:63
grpc_core::TraceFlag::enabled
bool enabled()
Definition: debug/trace.h:82
grpc_core::TraceFlagList
Definition: debug/trace.h:49
grpc_core::testing::grpc_tracer_enable_flag
void grpc_tracer_enable_flag(TraceFlag *flag)
Definition: tracer_util.cc:25
global_config.h
grpc_core::TraceFlag::next_tracer_
TraceFlag * next_tracer_
Definition: debug/trace.h:105
grpc_core::TraceFlagList::Add
static void Add(TraceFlag *flag)
Definition: debug/trace.cc:73
grpc_core::TraceFlag::set_enabled
void set_enabled(bool enabled)
Definition: debug/trace.h:97
grpc_trace
char * grpc_trace
Definition: php_grpc.h:80
grpc_core::DebugOnlyTraceFlag
TraceFlag DebugOnlyTraceFlag
Definition: debug/trace.h:117
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40