diff --git a/src/core/ext/filters/client_channel/lb_policy.c b/src/core/ext/filters/client_channel/lb_policy.c index 50f8faef8e5..a2fd1b6c474 100644 --- a/src/core/ext/filters/client_channel/lb_policy.c +++ b/src/core/ext/filters/client_channel/lb_policy.c @@ -21,6 +21,8 @@ #define WEAK_REF_BITS 16 +grpc_tracer_flag grpc_trace_lb_policy_refcount = GRPC_TRACER_INITIALIZER(false); + void grpc_lb_policy_init(grpc_lb_policy *policy, const grpc_lb_policy_vtable *vtable, grpc_combiner *combiner) { @@ -30,7 +32,7 @@ void grpc_lb_policy_init(grpc_lb_policy *policy, policy->combiner = GRPC_COMBINER_REF(combiner, "lb_policy"); } -#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG +#ifndef NDEBUG #define REF_FUNC_EXTRA_ARGS , const char *file, int line, const char *reason #define REF_MUTATE_EXTRA_ARGS REF_FUNC_EXTRA_ARGS, const char *purpose #define REF_FUNC_PASS_ARGS(new_reason) , file, line, new_reason @@ -46,11 +48,13 @@ static gpr_atm ref_mutate(grpc_lb_policy *c, gpr_atm delta, int barrier REF_MUTATE_EXTRA_ARGS) { gpr_atm old_val = barrier ? gpr_atm_full_fetch_add(&c->ref_pair, delta) : gpr_atm_no_barrier_fetch_add(&c->ref_pair, delta); -#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG - gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, - "LB_POLICY: 0x%" PRIxPTR " %12s 0x%" PRIxPTR " -> 0x%" PRIxPTR - " [%s]", - (intptr_t)c, purpose, old_val, old_val + delta, reason); +#ifndef NDEBUG + if (GRPC_TRACER_ON(grpc_trace_lb_policy_refcount)) { + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, + "LB_POLICY: 0x%" PRIxPTR " %12s 0x%" PRIxPTR " -> 0x%" PRIxPTR + " [%s]", + (intptr_t)c, purpose, old_val, old_val + delta, reason); + } #endif return old_val; } diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h index 42503c37ca2..5783c81bec4 100644 --- a/src/core/ext/filters/client_channel/lb_policy.h +++ b/src/core/ext/filters/client_channel/lb_policy.h @@ -29,6 +29,8 @@ typedef struct grpc_lb_policy grpc_lb_policy; typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable; typedef struct grpc_lb_policy_args grpc_lb_policy_args; +extern grpc_tracer_flag grpc_trace_lb_policy_refcount; + struct grpc_lb_policy { const grpc_lb_policy_vtable *vtable; gpr_atm ref_pair; @@ -96,8 +98,7 @@ struct grpc_lb_policy_vtable { const grpc_lb_policy_args *args); }; -//#define GRPC_LB_POLICY_REFCOUNT_DEBUG -#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG +#ifndef NDEBUG /* Strong references: the policy will shutdown when they reach zero */ #define GRPC_LB_POLICY_REF(p, r) \ diff --git a/src/core/ext/filters/client_channel/resolver.c b/src/core/ext/filters/client_channel/resolver.c index 69b1c31e592..bf1ddcc6666 100644 --- a/src/core/ext/filters/client_channel/resolver.c +++ b/src/core/ext/filters/client_channel/resolver.c @@ -19,6 +19,8 @@ #include "src/core/ext/filters/client_channel/resolver.h" #include "src/core/lib/iomgr/combiner.h" +grpc_tracer_flag grpc_trace_resolver_refcount = GRPC_TRACER_INITIALIZER(false); + void grpc_resolver_init(grpc_resolver *resolver, const grpc_resolver_vtable *vtable, grpc_combiner *combiner) { @@ -27,25 +29,30 @@ void grpc_resolver_init(grpc_resolver *resolver, gpr_ref_init(&resolver->refs, 1); } -#ifdef GRPC_RESOLVER_REFCOUNT_DEBUG -void grpc_resolver_ref(grpc_resolver *resolver, grpc_closure_list *closure_list, +#ifndef NDEBUG +void grpc_resolver_ref(grpc_resolver *resolver, const char *file, int line, const char *reason) { - gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p ref %d -> %d %s", - resolver, (int)resolver->refs.count, (int)resolver->refs.count + 1, - reason); + if (GRPC_TRACER_ON(grpc_trace_resolver_refcount)) { + long old_refs = gpr_atm_no_barrier_load(&resolver->refs.count); + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p ref %" PRIdPTR " -> %" PRIdPTR " %s", + resolver, old_refs, old_refs + 1, + reason); + } #else void grpc_resolver_ref(grpc_resolver *resolver) { #endif gpr_ref(&resolver->refs); } -#ifdef GRPC_RESOLVER_REFCOUNT_DEBUG -void grpc_resolver_unref(grpc_resolver *resolver, - grpc_closure_list *closure_list, const char *file, +#ifndef NDEBUG +void grpc_resolver_unref(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, const char *file, int line, const char *reason) { - gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p unref %d -> %d %s", - resolver, (int)resolver->refs.count, (int)resolver->refs.count - 1, - reason); + if (GRPC_TRACER_ON(grpc_trace_resolver_refcount)) { + long old_refs = gpr_atm_no_barrier_load(&resolver->refs.count); + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p unref %" PRIdPTR " -> %" PRIdPTR " %s", + resolver, old_refs, old_refs - 1, + reason); + } #else void grpc_resolver_unref(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) { #endif diff --git a/src/core/ext/filters/client_channel/resolver.h b/src/core/ext/filters/client_channel/resolver.h index c78bb316cbc..8f9e8e83e98 100644 --- a/src/core/ext/filters/client_channel/resolver.h +++ b/src/core/ext/filters/client_channel/resolver.h @@ -25,6 +25,8 @@ typedef struct grpc_resolver grpc_resolver; typedef struct grpc_resolver_vtable grpc_resolver_vtable; +extern grpc_tracer_flag grpc_trace_resolver_refcount; + /** \a grpc_resolver provides \a grpc_channel_args objects to its caller */ struct grpc_resolver { const grpc_resolver_vtable *vtable; @@ -41,17 +43,17 @@ struct grpc_resolver_vtable { grpc_channel_args **result, grpc_closure *on_complete); }; -#ifdef GRPC_RESOLVER_REFCOUNT_DEBUG +#ifndef NDEBUG #define GRPC_RESOLVER_REF(p, r) grpc_resolver_ref((p), __FILE__, __LINE__, (r)) -#define GRPC_RESOLVER_UNREF(cl, p, r) \ - grpc_resolver_unref((cl), (p), __FILE__, __LINE__, (r)) +#define GRPC_RESOLVER_UNREF(e, p, r) \ + grpc_resolver_unref((e), (p), __FILE__, __LINE__, (r)) void grpc_resolver_ref(grpc_resolver *policy, const char *file, int line, const char *reason); -void grpc_resolver_unref(grpc_resolver *policy, grpc_closure_list *closure_list, +void grpc_resolver_unref(grpc_exec_ctx *exec_ctx, grpc_resolver *policy, const char *file, int line, const char *reason); #else #define GRPC_RESOLVER_REF(p, r) grpc_resolver_ref((p)) -#define GRPC_RESOLVER_UNREF(cl, p, r) grpc_resolver_unref((cl), (p)) +#define GRPC_RESOLVER_UNREF(e, p, r) grpc_resolver_unref((e), (p)) void grpc_resolver_ref(grpc_resolver *policy); void grpc_resolver_unref(grpc_exec_ctx *exec_ctx, grpc_resolver *policy); #endif diff --git a/src/core/ext/filters/client_channel/subchannel.c b/src/core/ext/filters/client_channel/subchannel.c index 37dd96726ee..c85b31002e5 100644 --- a/src/core/ext/filters/client_channel/subchannel.c +++ b/src/core/ext/filters/client_channel/subchannel.c @@ -140,25 +140,13 @@ struct grpc_subchannel_call { static void subchannel_connected(grpc_exec_ctx *exec_ctx, void *subchannel, grpc_error *error); -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG #define REF_REASON reason -#define REF_LOG(name, p) \ - gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "%s: %p ref %d -> %d %s", \ - (name), (p), (p)->refs.count, (p)->refs.count + 1, reason) -#define UNREF_LOG(name, p) \ - gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "%s: %p unref %d -> %d %s", \ - (name), (p), (p)->refs.count, (p)->refs.count - 1, reason) #define REF_MUTATE_EXTRA_ARGS \ GRPC_SUBCHANNEL_REF_EXTRA_ARGS, const char *purpose #define REF_MUTATE_PURPOSE(x) , file, line, reason, x #else #define REF_REASON "" -#define REF_LOG(name, p) \ - do { \ - } while (0) -#define UNREF_LOG(name, p) \ - do { \ - } while (0) #define REF_MUTATE_EXTRA_ARGS #define REF_MUTATE_PURPOSE(x) #endif @@ -207,10 +195,12 @@ static gpr_atm ref_mutate(grpc_subchannel *c, gpr_atm delta, int barrier REF_MUTATE_EXTRA_ARGS) { gpr_atm old_val = barrier ? gpr_atm_full_fetch_add(&c->ref_pair, delta) : gpr_atm_no_barrier_fetch_add(&c->ref_pair, delta); -#ifdef GRPC_STREAM_REFCOUNT_DEBUG - gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, - "SUBCHANNEL: %p %s 0x%08" PRIxPTR " -> 0x%08" PRIxPTR " [%s]", c, - purpose, old_val, old_val + delta, reason); +#ifndef NDEBUG + if (GRPC_TRACER_ON(grpc_trace_stream_refcount)) { + gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, + "SUBCHANNEL: %p %s 0x%08" PRIxPTR " -> 0x%08" PRIxPTR " [%s]", c, + purpose, old_val, old_val + delta, reason); + } #endif return old_val; } diff --git a/src/core/ext/filters/client_channel/subchannel.h b/src/core/ext/filters/client_channel/subchannel.h index f38bf42803c..6d2abb04df5 100644 --- a/src/core/ext/filters/client_channel/subchannel.h +++ b/src/core/ext/filters/client_channel/subchannel.h @@ -37,7 +37,7 @@ typedef struct grpc_subchannel_call grpc_subchannel_call; typedef struct grpc_subchannel_args grpc_subchannel_args; typedef struct grpc_subchannel_key grpc_subchannel_key; -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG #define GRPC_SUBCHANNEL_REF(p, r) \ grpc_subchannel_ref((p), __FILE__, __LINE__, (r)) #define GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(p, r) \ diff --git a/src/core/ext/transport/chttp2/transport/chttp2_transport.c b/src/core/ext/transport/chttp2/transport/chttp2_transport.c index 0ad63d1af2f..aeee1b47897 100644 --- a/src/core/ext/transport/chttp2/transport/chttp2_transport.c +++ b/src/core/ext/transport/chttp2/transport/chttp2_transport.c @@ -620,7 +620,7 @@ static void close_transport_locked(grpc_exec_ctx *exec_ctx, GRPC_ERROR_UNREF(error); } -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG void grpc_chttp2_stream_ref(grpc_chttp2_stream *s, const char *reason) { grpc_stream_ref(s->refcount, reason); } diff --git a/src/core/ext/transport/chttp2/transport/internal.h b/src/core/ext/transport/chttp2/transport/internal.h index 4041b29fecb..f07456332e8 100644 --- a/src/core/ext/transport/chttp2/transport/internal.h +++ b/src/core/ext/transport/chttp2/transport/internal.h @@ -748,7 +748,7 @@ void grpc_chttp2_mark_stream_closed(grpc_exec_ctx *exec_ctx, void grpc_chttp2_start_writing(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t); -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG #define GRPC_CHTTP2_STREAM_REF(stream, reason) \ grpc_chttp2_stream_ref(stream, reason) #define GRPC_CHTTP2_STREAM_UNREF(exec_ctx, stream, reason) \ diff --git a/src/core/lib/channel/channel_stack.h b/src/core/lib/channel/channel_stack.h index b0559ad745c..a80f8aa8268 100644 --- a/src/core/lib/channel/channel_stack.h +++ b/src/core/lib/channel/channel_stack.h @@ -234,7 +234,7 @@ void grpc_call_stack_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, grpc_call_stack *call_stack, grpc_polling_entity *pollent); -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG #define GRPC_CALL_STACK_REF(call_stack, reason) \ grpc_stream_ref(&(call_stack)->refcount, reason) #define GRPC_CALL_STACK_UNREF(exec_ctx, call_stack, reason) \ diff --git a/src/core/lib/surface/call.c b/src/core/lib/surface/call.c index b499219e170..bea75bc2d68 100644 --- a/src/core/lib/surface/call.c +++ b/src/core/lib/surface/call.c @@ -457,7 +457,7 @@ void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call, exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent); } -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG #define REF_REASON reason #define REF_ARG , const char *reason #else diff --git a/src/core/lib/surface/call.h b/src/core/lib/surface/call.h index 60b661cf8cd..185bfccb77d 100644 --- a/src/core/lib/surface/call.h +++ b/src/core/lib/surface/call.h @@ -62,7 +62,7 @@ grpc_error *grpc_call_create(grpc_exec_ctx *exec_ctx, void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call, grpc_completion_queue *cq); -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG void grpc_call_internal_ref(grpc_call *call, const char *reason); void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *call, const char *reason); diff --git a/src/core/lib/surface/channel.c b/src/core/lib/surface/channel.c index 5647dff28b1..5780a18ce8e 100644 --- a/src/core/lib/surface/channel.c +++ b/src/core/lib/surface/channel.c @@ -345,7 +345,7 @@ grpc_call *grpc_channel_create_registered_call( return call; } -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG #define REF_REASON reason #define REF_ARG , const char *reason #else diff --git a/src/core/lib/surface/channel.h b/src/core/lib/surface/channel.h index 848debc7c58..528bb868e2c 100644 --- a/src/core/lib/surface/channel.h +++ b/src/core/lib/surface/channel.h @@ -59,7 +59,7 @@ grpc_mdelem grpc_channel_get_reffed_status_elem(grpc_exec_ctx *exec_ctx, size_t grpc_channel_get_call_size_estimate(grpc_channel *channel); void grpc_channel_update_call_size_estimate(grpc_channel *channel, size_t size); -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG void grpc_channel_internal_ref(grpc_channel *channel, const char *reason); void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx, grpc_channel *channel, const char *reason); diff --git a/src/core/lib/surface/init.c b/src/core/lib/surface/init.c index 9b8077bc6cb..449022c781d 100644 --- a/src/core/lib/surface/init.c +++ b/src/core/lib/surface/init.c @@ -47,6 +47,11 @@ #include "src/core/lib/transport/connectivity_state.h" #include "src/core/lib/transport/transport_impl.h" +#ifndef NDEBUG +#include "src/core/ext/filters/client_channel/lb_policy.h" +#include "src/core/ext/filters/client_channel/resolver.h" +#endif + /* (generated) built in registry of plugins */ extern void grpc_register_built_in_plugins(void); @@ -140,6 +145,9 @@ void grpc_init(void) { grpc_register_tracer("pending_tags", &grpc_trace_pending_tags); grpc_register_tracer("closure", &grpc_trace_closure); grpc_register_tracer("error_refcount", &grpc_trace_error_refcount); + grpc_register_tracer("lb_policy_refcount", &grpc_trace_lb_policy_refcount); + grpc_register_tracer("resolver_refcount", &grpc_trace_resolver_refcount); + grpc_register_tracer("stream_refcount", &grpc_trace_stream_refcount); #endif grpc_security_pre_init(); grpc_iomgr_init(&exec_ctx); diff --git a/src/core/lib/transport/transport.c b/src/core/lib/transport/transport.c index c2afedec585..2bedbcd2901 100644 --- a/src/core/lib/transport/transport.c +++ b/src/core/lib/transport/transport.c @@ -31,25 +31,31 @@ #include "src/core/lib/support/string.h" #include "src/core/lib/transport/transport_impl.h" -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +grpc_tracer_flag grpc_trace_stream_refcount = GRPC_TRACER_INITIALIZER(false); + +#ifndef NDEBUG void grpc_stream_ref(grpc_stream_refcount *refcount, const char *reason) { - gpr_atm val = gpr_atm_no_barrier_load(&refcount->refs.count); - gpr_log(GPR_DEBUG, "%s %p:%p REF %" PRIdPTR "->%" PRIdPTR " %s", - refcount->object_type, refcount, refcount->destroy.cb_arg, val, - val + 1, reason); + if (GRPC_TRACER_ON(grpc_trace_stream_refcount)) { + gpr_atm val = gpr_atm_no_barrier_load(&refcount->refs.count); + gpr_log(GPR_DEBUG, "%s %p:%p REF %" PRIdPTR "->%" PRIdPTR " %s", + refcount->object_type, refcount, refcount->destroy.cb_arg, val, + val + 1, reason); + } #else void grpc_stream_ref(grpc_stream_refcount *refcount) { #endif gpr_ref_non_zero(&refcount->refs); } -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount, const char *reason) { - gpr_atm val = gpr_atm_no_barrier_load(&refcount->refs.count); - gpr_log(GPR_DEBUG, "%s %p:%p UNREF %" PRIdPTR "->%" PRIdPTR " %s", - refcount->object_type, refcount, refcount->destroy.cb_arg, val, - val - 1, reason); + if (GRPC_TRACER_ON(grpc_trace_stream_refcount)) { + gpr_atm val = gpr_atm_no_barrier_load(&refcount->refs.count); + gpr_log(GPR_DEBUG, "%s %p:%p UNREF %" PRIdPTR "->%" PRIdPTR " %s", + refcount->object_type, refcount, refcount->destroy.cb_arg, val, + val - 1, reason); + } #else void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount) { @@ -74,7 +80,7 @@ void grpc_stream_unref(grpc_exec_ctx *exec_ctx, offsetof(grpc_stream_refcount, slice_refcount))) static void slice_stream_ref(void *p) { -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG grpc_stream_ref(STREAM_REF_FROM_SLICE_REF(p), "slice"); #else grpc_stream_ref(STREAM_REF_FROM_SLICE_REF(p)); @@ -82,7 +88,7 @@ static void slice_stream_ref(void *p) { } static void slice_stream_unref(grpc_exec_ctx *exec_ctx, void *p) { -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG grpc_stream_unref(exec_ctx, STREAM_REF_FROM_SLICE_REF(p), "slice"); #else grpc_stream_unref(exec_ctx, STREAM_REF_FROM_SLICE_REF(p)); @@ -102,7 +108,7 @@ static const grpc_slice_refcount_vtable stream_ref_slice_vtable = { .eq = grpc_slice_default_eq_impl, .hash = grpc_slice_default_hash_impl}; -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs, grpc_iomgr_cb_func cb, void *cb_arg, const char *object_type) { diff --git a/src/core/lib/transport/transport.h b/src/core/lib/transport/transport.h index d231157c874..57c18d1e300 100644 --- a/src/core/lib/transport/transport.h +++ b/src/core/lib/transport/transport.h @@ -42,18 +42,18 @@ typedef struct grpc_transport grpc_transport; for a stream. */ typedef struct grpc_stream grpc_stream; -//#define GRPC_STREAM_REFCOUNT_DEBUG +extern grpc_tracer_flag grpc_trace_stream_refcount; typedef struct grpc_stream_refcount { gpr_refcount refs; grpc_closure destroy; -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG const char *object_type; #endif grpc_slice_refcount slice_refcount; } grpc_stream_refcount; -#ifdef GRPC_STREAM_REFCOUNT_DEBUG +#ifndef NDEBUG void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs, grpc_iomgr_cb_func cb, void *cb_arg, const char *object_type);