Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / src / core / lib / debug / trace.h
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 #include <grpc/support/port_platform.h>
23
24 #include <grpc/support/atm.h>
25 #include <stdbool.h>
26
27 #include "src/core/lib/gprpp/global_config.h"
28
29 GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_trace);
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;
47 class TraceFlagList {
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 {
58 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
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:
92   friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
93   friend class TraceFlagList;
94
95   void set_enabled(bool enabled) {
96 #ifdef GRPC_THREADSAFE_TRACER
97     gpr_atm_no_barrier_store(&value_, enabled);
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
115 typedef TraceFlag DebugOnlyTraceFlag;
116 #else
117 class DebugOnlyTraceFlag {
118  public:
119   constexpr DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
120   constexpr bool enabled() const { return false; }
121   constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
122
123  private:
124   void set_enabled(bool enabled) {}
125 };
126 #endif
127
128 }  // namespace grpc_core
129
130 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */