Imported Upstream version 1.20.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 void grpc_tracer_init(const char* env_var_name);
28 void grpc_tracer_shutdown(void);
29
30 #if defined(__has_feature)
31 #if __has_feature(thread_sanitizer)
32 #define GRPC_THREADSAFE_TRACER
33 #endif
34 #endif
35
36 namespace grpc_core {
37
38 class TraceFlag;
39 class TraceFlagList {
40  public:
41   static bool Set(const char* name, bool enabled);
42   static void Add(TraceFlag* flag);
43
44  private:
45   static void LogAllTracers();
46   static TraceFlag* root_tracer_;
47 };
48
49 namespace testing {
50 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
51 }
52
53 class TraceFlag {
54  public:
55   TraceFlag(bool default_enabled, const char* name);
56   // TraceFlag needs to be trivially destructible since it is used as global
57   // variable.
58   ~TraceFlag() = default;
59
60   const char* name() const { return name_; }
61
62 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in
63 // opt builds (tracers are always on in dbg builds). The default in OSS is for
64 // tracers to be on since we support binary distributions of gRPC for the
65 // wrapped language (wr don't want to force recompilation to get tracing).
66 // Internally, however, for performance reasons, we compile them out by
67 // default, since internal build systems make recompiling trivial.
68 #define GRPC_USE_TRACERS  // tracers on by default in OSS
69 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG)
70   bool enabled() {
71 #ifdef GRPC_THREADSAFE_TRACER
72     return gpr_atm_no_barrier_load(&value_) != 0;
73 #else
74     return value_;
75 #endif  // GRPC_THREADSAFE_TRACER
76   }
77 #else
78   bool enabled() { return false; }
79 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */
80
81  private:
82   friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
83   friend class TraceFlagList;
84
85   void set_enabled(bool enabled) {
86 #ifdef GRPC_THREADSAFE_TRACER
87     gpr_atm_no_barrier_store(&value_, enabled);
88 #else
89     value_ = enabled;
90 #endif
91   }
92
93   TraceFlag* next_tracer_;
94   const char* const name_;
95 #ifdef GRPC_THREADSAFE_TRACER
96   gpr_atm value_;
97 #else
98   bool value_;
99 #endif
100 };
101
102 #ifndef NDEBUG
103 typedef TraceFlag DebugOnlyTraceFlag;
104 #else
105 class DebugOnlyTraceFlag {
106  public:
107   constexpr DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
108   constexpr bool enabled() const { return false; }
109   constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
110
111  private:
112   void set_enabled(bool enabled) {}
113 };
114 #endif
115
116 }  // namespace grpc_core
117
118 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */