| 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 |
|
| 28 |
#include <grpc/support/port_platform.h> |
| 29 |
|
| 30 |
#ifdef GRPC_THREADSAFE_TRACER |
| 31 |
#include <atomic> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "src/core/lib/gprpp/global_config.h" |
| 35 |
#include "src/core/lib/gprpp/memory.h" |
| 36 |
|
| 37 |
GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_trace); |
| 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; |
| 49 |
class TraceFlagList { |
| 50 |
public: |
| 51 |
static bool Set(const char* name, bool enabled); |
| 52 |
static void Add(TraceFlag* flag); |
| 53 |
|
| 54 |
private: |
| 55 |
static void LogAllTracers(); |
| 56 |
static TraceFlag* root_tracer_; |
| 57 |
}; |
| 58 |
|
| 59 |
namespace testing { |
| 60 |
void grpc_tracer_enable_flag(TraceFlag* flag); |
| 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: |
| 94 |
friend void testing::grpc_tracer_enable_flag(TraceFlag* flag); |
| 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 |
|
| 105 |
TraceFlag* next_tracer_; |
| 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 |
| 117 |
typedef TraceFlag DebugOnlyTraceFlag; |
| 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 */ |