Switch to using RefCounted

reviewable/pr13883/r1
ncteisen 7 years ago
parent 4e8bcb2cee
commit 7042b519e8
  1. 3
      src/core/ext/filters/client_channel/subchannel.cc
  2. 33
      src/core/lib/channel/channel_tracer.cc
  3. 19
      src/core/lib/channel/channel_tracer.h
  4. 8
      src/core/lib/surface/channel.cc

@ -41,6 +41,7 @@
#include "src/core/lib/debug/stats.h" #include "src/core/lib/debug/stats.h"
#include "src/core/lib/gprpp/debug_location.h" #include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/gprpp/manual_constructor.h" #include "src/core/lib/gprpp/manual_constructor.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/iomgr/sockaddr_utils.h" #include "src/core/lib/iomgr/sockaddr_utils.h"
#include "src/core/lib/iomgr/timer.h" #include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/profiling/timers.h" #include "src/core/lib/profiling/timers.h"
@ -134,7 +135,7 @@ struct grpc_subchannel {
/** our alarm */ /** our alarm */
grpc_timer alarm; grpc_timer alarm;
grpc_core::ManualConstructor<grpc_core::ChannelTracer> tracer; grpc_core::RefCountedPtr<grpc_core::ChannelTracer> tracer;
}; };
struct grpc_subchannel_call { struct grpc_subchannel_call {

@ -40,12 +40,12 @@ class TraceEvent {
public: public:
TraceEvent(grpc_slice data, grpc_error* error, TraceEvent(grpc_slice data, grpc_error* error,
grpc_connectivity_state connectivity_state, grpc_connectivity_state connectivity_state,
ChannelTracer* referenced_tracer) RefCountedPtr<ChannelTracer> referenced_tracer)
: data_(data), : data_(data),
error_(error), error_(error),
connectivity_state_(connectivity_state), connectivity_state_(connectivity_state),
next_(nullptr) { next_(nullptr) {
referenced_tracer_ = referenced_tracer ? referenced_tracer->Ref() : nullptr; referenced_tracer_ = referenced_tracer;
time_created_ = gpr_now(GPR_CLOCK_REALTIME); time_created_ = gpr_now(GPR_CLOCK_REALTIME);
} }
@ -58,7 +58,7 @@ class TraceEvent {
grpc_connectivity_state connectivity_state_; grpc_connectivity_state connectivity_state_;
TraceEvent* next_; TraceEvent* next_;
// the tracer object for the (sub)channel that this trace node refers to. // the tracer object for the (sub)channel that this trace node refers to.
ChannelTracer* referenced_tracer_; RefCountedPtr<ChannelTracer> referenced_tracer_;
}; };
ChannelTracer::ChannelTracer(size_t max_nodes) ChannelTracer::ChannelTracer(size_t max_nodes)
@ -71,31 +71,20 @@ ChannelTracer::ChannelTracer(size_t max_nodes)
tail_trace_(nullptr) { tail_trace_(nullptr) {
if (!max_list_size_) return; // tracing is disabled if max_nodes == 0 if (!max_list_size_) return; // tracing is disabled if max_nodes == 0
gpr_mu_init(&tracer_mu_); gpr_mu_init(&tracer_mu_);
gpr_ref_init(&refs_, 1);
channel_uuid_ = grpc_object_registry_register_object( channel_uuid_ = grpc_object_registry_register_object(
this, GRPC_OBJECT_REGISTRY_CHANNEL_TRACER); this, GRPC_OBJECT_REGISTRY_CHANNEL_TRACER);
max_list_size_ = max_nodes; max_list_size_ = max_nodes;
time_created_ = gpr_now(GPR_CLOCK_REALTIME); time_created_ = gpr_now(GPR_CLOCK_REALTIME);
} }
ChannelTracer* ChannelTracer::Ref() {
if (!max_list_size_) return nullptr; // tracing is disabled if max_nodes == 0
gpr_ref(&refs_);
return this;
}
void ChannelTracer::FreeNode(TraceEvent* node) { void ChannelTracer::FreeNode(TraceEvent* node) {
GRPC_ERROR_UNREF(node->error_); GRPC_ERROR_UNREF(node->error_);
if (node->referenced_tracer_) {
node->referenced_tracer_->Unref();
}
grpc_slice_unref_internal(node->data_); grpc_slice_unref_internal(node->data_);
gpr_free(node); gpr_free(node);
} }
void ChannelTracer::Unref() { ChannelTracer::~ChannelTracer() {
if (!max_list_size_) return; // tracing is disabled if max_nodes == 0 if (!max_list_size_) return; // tracing is disabled if max_nodes == 0
if (gpr_unref(&refs_)) {
TraceEvent* it = head_trace_; TraceEvent* it = head_trace_;
while (it != nullptr) { while (it != nullptr) {
TraceEvent* to_free = it; TraceEvent* to_free = it;
@ -104,13 +93,12 @@ void ChannelTracer::Unref() {
} }
gpr_mu_destroy(&tracer_mu_); gpr_mu_destroy(&tracer_mu_);
} }
}
intptr_t ChannelTracer::GetUuid() { return channel_uuid_; } intptr_t ChannelTracer::GetUuid() { return channel_uuid_; }
void ChannelTracer::AddTrace(grpc_slice data, grpc_error* error, void ChannelTracer::AddTrace(grpc_slice data, grpc_error* error,
grpc_connectivity_state connectivity_state, grpc_connectivity_state connectivity_state,
ChannelTracer* referenced_tracer) { RefCountedPtr<ChannelTracer> referenced_tracer) {
if (!max_list_size_) return; // tracing is disabled if max_nodes == 0 if (!max_list_size_) return; // tracing is disabled if max_nodes == 0
++num_nodes_logged_; ++num_nodes_logged_;
if (referenced_tracer != nullptr) ++num_children_seen_; if (referenced_tracer != nullptr) ++num_children_seen_;
@ -136,6 +124,11 @@ void ChannelTracer::AddTrace(grpc_slice data, grpc_error* error,
} }
} }
void ChannelTracer::AddTrace(grpc_slice data, grpc_error* error,
grpc_connectivity_state connectivity_state) {
AddTrace(data, error, connectivity_state, RefCountedPtr<ChannelTracer>());
}
// returns an allocated string that represents tm according to RFC-3339. // returns an allocated string that represents tm according to RFC-3339.
static char* fmt_time(gpr_timespec tm) { static char* fmt_time(gpr_timespec tm) {
char buffer[35]; char buffer[35];
@ -297,12 +290,12 @@ class ChannelTracerRenderer {
// If we are recursively populating everything, and this node // If we are recursively populating everything, and this node
// references a tracer we haven't seen yet, we render that tracer // references a tracer we haven't seen yet, we render that tracer
// in full, adding it to the parent JSON's "children" field. // in full, adding it to the parent JSON's "children" field.
if (children && !TracerAlreadySeen(node->referenced_tracer_)) { if (children && !TracerAlreadySeen(node->referenced_tracer_.get())) {
grpc_json* referenced_tracer = grpc_json_create_child( grpc_json* referenced_tracer = grpc_json_create_child(
nullptr, children, nullptr, nullptr, GRPC_JSON_OBJECT, false); nullptr, children, nullptr, nullptr, GRPC_JSON_OBJECT, false);
AddSeenTracer(node->referenced_tracer_); AddSeenTracer(node->referenced_tracer_.get());
ChannelTracer* saved = current_tracer_; ChannelTracer* saved = current_tracer_;
current_tracer_ = node->referenced_tracer_; current_tracer_ = node->referenced_tracer_.get();
RecursivelyPopulateJson(referenced_tracer); RecursivelyPopulateJson(referenced_tracer);
current_tracer_ = saved; current_tracer_ = saved;
} }

@ -20,6 +20,8 @@
#define GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACER_H #define GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACER_H
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include "src/core/lib/gprpp/ref_counted.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/error.h"
#include "src/core/lib/json/json.h" #include "src/core/lib/json/json.h"
@ -27,22 +29,24 @@ namespace grpc_core {
class TraceEvent; class TraceEvent;
class ChannelTracer { class ChannelTracer : public RefCounted {
public: public:
ChannelTracer(size_t max_nodes); ChannelTracer(size_t max_nodes);
~ChannelTracer() {} ~ChannelTracer();
// TODO(ncteisen): incorporate RefCounted class
ChannelTracer* Ref();
void Unref();
/* returns the tracers uuid */ /* returns the tracers uuid */
intptr_t GetUuid(); intptr_t GetUuid();
/* Adds a new trace node to the tracing object */ /* Adds a new trace node to the tracing object */
void AddTrace(grpc_slice data, grpc_error* error,
grpc_connectivity_state connectivity_state);
/* Adds a new trace node to the tracing object. This trace node refers to a
an event on a child of the channel. For example this could log when a
particular subchannel becomes connected */
void AddTrace(grpc_slice data, grpc_error* error, void AddTrace(grpc_slice data, grpc_error* error,
grpc_connectivity_state connectivity_state, grpc_connectivity_state connectivity_state,
ChannelTracer* subchannel); RefCountedPtr<ChannelTracer> referenced_tracer);
/* Returns the tracing data rendered as a grpc json string. /* Returns the tracing data rendered as a grpc json string.
The string is owned by the caller and must be freed. If recursive The string is owned by the caller and must be freed. If recursive
@ -59,7 +63,6 @@ class ChannelTracer {
void FreeNode(TraceEvent* node); void FreeNode(TraceEvent* node);
friend class ChannelTracerRenderer; friend class ChannelTracerRenderer;
gpr_refcount refs_;
gpr_mu tracer_mu_; gpr_mu tracer_mu_;
intptr_t channel_uuid_; intptr_t channel_uuid_;
uint64_t num_nodes_logged_; uint64_t num_nodes_logged_;

@ -34,6 +34,7 @@
#include "src/core/lib/gpr/string.h" #include "src/core/lib/gpr/string.h"
#include "src/core/lib/gprpp/manual_constructor.h" #include "src/core/lib/gprpp/manual_constructor.h"
#include "src/core/lib/gprpp/memory.h" #include "src/core/lib/gprpp/memory.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/iomgr/iomgr.h" #include "src/core/lib/iomgr/iomgr.h"
#include "src/core/lib/slice/slice_internal.h" #include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/surface/api_trace.h" #include "src/core/lib/surface/api_trace.h"
@ -64,7 +65,7 @@ struct grpc_channel {
gpr_mu registered_call_mu; gpr_mu registered_call_mu;
registered_call* registered_calls; registered_call* registered_calls;
grpc_core::ManualConstructor<grpc_core::ChannelTracer> tracer; grpc_core::RefCountedPtr<grpc_core::ChannelTracer> tracer;
char* target; char* target;
}; };
@ -202,13 +203,14 @@ grpc_channel* grpc_channel_create_with_builder(
const grpc_integer_options options = {10, 0, 100}; const grpc_integer_options options = {10, 0, 100};
size_t max_nodes = size_t max_nodes =
(size_t)grpc_channel_arg_get_integer(&args->args[i], options); (size_t)grpc_channel_arg_get_integer(&args->args[i], options);
channel->tracer.Init(max_nodes); channel->tracer =
grpc_core::MakeRefCounted<grpc_core::ChannelTracer>(max_nodes);
} }
} }
grpc_channel_args_destroy(args); grpc_channel_args_destroy(args);
channel->tracer->AddTrace(grpc_slice_from_static_string("Channel created"), channel->tracer->AddTrace(grpc_slice_from_static_string("Channel created"),
GRPC_ERROR_NONE, GRPC_CHANNEL_IDLE, nullptr); GRPC_ERROR_NONE, GRPC_CHANNEL_IDLE);
return channel; return channel;
} }

Loading…
Cancel
Save