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