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