From a135485bb8ecbbea027727525c0828e4dce5d3da Mon Sep 17 00:00:00 2001
From: ncteisen <ncteisen@gmail.com>
Date: Thu, 8 Jun 2017 16:25:53 -0700
Subject: [PATCH] Add workqueue tracer

---
 .../ext/filters/client_channel/lb_policy.c    |  3 +++
 .../ext/filters/client_channel/lb_policy.h    |  2 ++
 .../ext/filters/client_channel/resolver.c     |  2 ++
 .../ext/filters/client_channel/resolver.h     |  2 ++
 src/core/lib/iomgr/closure.c                  |  2 ++
 src/core/lib/iomgr/closure.h                  |  2 ++
 src/core/lib/iomgr/combiner.c                 |  8 +++---
 src/core/lib/iomgr/combiner.h                 |  3 +--
 src/core/lib/iomgr/error.c                    |  2 ++
 src/core/lib/iomgr/error.h                    |  2 ++
 src/core/lib/iomgr/ev_epollsig_linux.c        | 27 ++++++++++---------
 src/core/lib/iomgr/ev_poll_posix.c            |  1 +
 src/core/lib/iomgr/ev_posix.c                 |  1 +
 src/core/lib/transport/transport.c            |  3 +++
 src/core/lib/transport/transport.h            |  2 ++
 15 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/src/core/ext/filters/client_channel/lb_policy.c b/src/core/ext/filters/client_channel/lb_policy.c
index a2fd1b6c47..6fc9b4bc15 100644
--- a/src/core/ext/filters/client_channel/lb_policy.c
+++ b/src/core/ext/filters/client_channel/lb_policy.c
@@ -21,7 +21,10 @@
 
 #define WEAK_REF_BITS 16
 
+#ifndef NDEBUG
 grpc_tracer_flag grpc_trace_lb_policy_refcount = GRPC_TRACER_INITIALIZER(false);
+#endif
+
 
 void grpc_lb_policy_init(grpc_lb_policy *policy,
                          const grpc_lb_policy_vtable *vtable,
diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h
index 5783c81bec..645d51e138 100644
--- a/src/core/ext/filters/client_channel/lb_policy.h
+++ b/src/core/ext/filters/client_channel/lb_policy.h
@@ -29,7 +29,9 @@ 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;
 
+#ifndef NDEBUG
 extern grpc_tracer_flag grpc_trace_lb_policy_refcount;
+#endif
 
 struct grpc_lb_policy {
   const grpc_lb_policy_vtable *vtable;
diff --git a/src/core/ext/filters/client_channel/resolver.c b/src/core/ext/filters/client_channel/resolver.c
index bf1ddcc666..6a3e82eff6 100644
--- a/src/core/ext/filters/client_channel/resolver.c
+++ b/src/core/ext/filters/client_channel/resolver.c
@@ -19,7 +19,9 @@
 #include "src/core/ext/filters/client_channel/resolver.h"
 #include "src/core/lib/iomgr/combiner.h"
 
+#ifndef NDEBUG
 grpc_tracer_flag grpc_trace_resolver_refcount = GRPC_TRACER_INITIALIZER(false);
+#endif
 
 void grpc_resolver_init(grpc_resolver *resolver,
                         const grpc_resolver_vtable *vtable,
diff --git a/src/core/ext/filters/client_channel/resolver.h b/src/core/ext/filters/client_channel/resolver.h
index 8f9e8e83e9..ae9c8f66fe 100644
--- a/src/core/ext/filters/client_channel/resolver.h
+++ b/src/core/ext/filters/client_channel/resolver.h
@@ -25,7 +25,9 @@
 typedef struct grpc_resolver grpc_resolver;
 typedef struct grpc_resolver_vtable grpc_resolver_vtable;
 
+#ifndef NDEBUG
 extern grpc_tracer_flag grpc_trace_resolver_refcount;
+#endif
 
 /** \a grpc_resolver provides \a grpc_channel_args objects to its caller */
 struct grpc_resolver {
diff --git a/src/core/lib/iomgr/closure.c b/src/core/lib/iomgr/closure.c
index c1535fbabe..e028e72ed6 100644
--- a/src/core/lib/iomgr/closure.c
+++ b/src/core/lib/iomgr/closure.c
@@ -24,7 +24,9 @@
 
 #include "src/core/lib/profiling/timers.h"
 
+#ifndef NDEBUG
 grpc_tracer_flag grpc_trace_closure = GRPC_TRACER_INITIALIZER(false);
+#endif
 
 #ifndef NDEBUG
 grpc_closure *grpc_closure_init(const char *file, int line,
diff --git a/src/core/lib/iomgr/closure.h b/src/core/lib/iomgr/closure.h
index 874d08d29e..2ec6f77e76 100644
--- a/src/core/lib/iomgr/closure.h
+++ b/src/core/lib/iomgr/closure.h
@@ -29,7 +29,9 @@
 struct grpc_closure;
 typedef struct grpc_closure grpc_closure;
 
+#ifndef NDEBUG
 extern grpc_tracer_flag grpc_trace_closure;
+#endif
 
 typedef struct grpc_closure_list {
   grpc_closure *head;
diff --git a/src/core/lib/iomgr/combiner.c b/src/core/lib/iomgr/combiner.c
index d1377a942a..06e638d4cf 100644
--- a/src/core/lib/iomgr/combiner.c
+++ b/src/core/lib/iomgr/combiner.c
@@ -102,12 +102,12 @@ static void start_destroy(grpc_exec_ctx *exec_ctx, grpc_combiner *lock) {
   }
 }
 
-#ifdef GRPC_COMBINER_REFCOUNT_DEBUG
+#ifndef NDEBUG
 #define GRPC_COMBINER_DEBUG_SPAM(op, delta)                               \
-  gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,                             \
-          "combiner[%p] %s %" PRIdPTR " --> %" PRIdPTR " %s", lock, (op), \
+  if (GRPC_TRACER_ON(grpc_combiner_trace)) { gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,                             \
+          "C:%p %s %" PRIdPTR " --> %" PRIdPTR " %s", lock, (op), \
           gpr_atm_no_barrier_load(&lock->refs.count),                     \
-          gpr_atm_no_barrier_load(&lock->refs.count) + (delta), reason);
+          gpr_atm_no_barrier_load(&lock->refs.count) + (delta), reason); }
 #else
 #define GRPC_COMBINER_DEBUG_SPAM(op, delta)
 #endif
diff --git a/src/core/lib/iomgr/combiner.h b/src/core/lib/iomgr/combiner.h
index a616113ca0..8e0434369d 100644
--- a/src/core/lib/iomgr/combiner.h
+++ b/src/core/lib/iomgr/combiner.h
@@ -35,8 +35,7 @@
 // necessary
 grpc_combiner *grpc_combiner_create(void);
 
-//#define GRPC_COMBINER_REFCOUNT_DEBUG
-#ifdef GRPC_COMBINER_REFCOUNT_DEBUG
+#ifndef NDEBUG
 #define GRPC_COMBINER_DEBUG_ARGS \
   , const char *file, int line, const char *reason
 #define GRPC_COMBINER_REF(combiner, reason) \
diff --git a/src/core/lib/iomgr/error.c b/src/core/lib/iomgr/error.c
index 30a6a0fd2c..6fb0f4d624 100644
--- a/src/core/lib/iomgr/error.c
+++ b/src/core/lib/iomgr/error.c
@@ -34,7 +34,9 @@
 #include "src/core/lib/profiling/timers.h"
 #include "src/core/lib/slice/slice_internal.h"
 
+#ifndef NDEBUG
 grpc_tracer_flag grpc_trace_error_refcount = GRPC_TRACER_INITIALIZER(false);
+#endif
 
 static const char *error_int_name(grpc_error_ints key) {
   switch (key) {
diff --git a/src/core/lib/iomgr/error.h b/src/core/lib/iomgr/error.h
index 729f4eb70c..fb8e19f643 100644
--- a/src/core/lib/iomgr/error.h
+++ b/src/core/lib/iomgr/error.h
@@ -38,7 +38,9 @@ extern "C" {
 
 typedef struct grpc_error grpc_error;
 
+#ifndef NDEBUG
 extern grpc_tracer_flag grpc_trace_error_refcount;
+#endif
 
 typedef enum {
   /// 'errno' from the operating system
diff --git a/src/core/lib/iomgr/ev_epollsig_linux.c b/src/core/lib/iomgr/ev_epollsig_linux.c
index fa4b4e8d0a..90f730e66a 100644
--- a/src/core/lib/iomgr/ev_epollsig_linux.c
+++ b/src/core/lib/iomgr/ev_epollsig_linux.c
@@ -161,20 +161,18 @@ static void fd_global_shutdown(void);
  * Polling island Declarations
  */
 
-//#define PI_REFCOUNT_DEBUG
-
-#ifdef PI_REFCOUNT_DEBUG
+#ifndef NDEBUG
 
 #define PI_ADD_REF(p, r) pi_add_ref_dbg((p), (r), __FILE__, __LINE__)
 #define PI_UNREF(exec_ctx, p, r) \
   pi_unref_dbg((exec_ctx), (p), (r), __FILE__, __LINE__)
 
-#else /* defined(GRPC_WORKQUEUE_REFCOUNT_DEBUG) */
+#else 
 
 #define PI_ADD_REF(p, r) pi_add_ref((p))
 #define PI_UNREF(exec_ctx, p, r) pi_unref((exec_ctx), (p))
 
-#endif /* !defined(GRPC_PI_REF_COUNT_DEBUG) */
+#endif
 
 /* This is also used as grpc_workqueue (by directly casing it) */
 typedef struct polling_island {
@@ -287,21 +285,26 @@ gpr_atm g_epoll_sync;
 static void pi_add_ref(polling_island *pi);
 static void pi_unref(grpc_exec_ctx *exec_ctx, polling_island *pi);
 
-#ifdef PI_REFCOUNT_DEBUG
+#ifndef NDEBUG
+grpc_tracer_flag grpc_trace_workqueue_refcount = GRPC_TRACER_INITIALIZER(false);
 static void pi_add_ref_dbg(polling_island *pi, const char *reason,
                            const char *file, int line) {
-  long old_cnt = gpr_atm_acq_load(&pi->ref_count);
-  pi_add_ref(pi);
-  gpr_log(GPR_DEBUG, "Add ref pi: %p, old: %ld -> new:%ld (%s) - (%s, %d)",
+  if (GRPC_TRACER_ON(grpc_trace_workqueue_refcount)) {
+    long old_cnt = gpr_atm_acq_load(&pi->ref_count);
+    gpr_log(GPR_DEBUG, "Add ref pi: %p, old: %ld -> new:%ld (%s) - (%s, %d)",
           (void *)pi, old_cnt, old_cnt + 1, reason, file, line);
+  }
+  pi_add_ref(pi);
 }
 
 static void pi_unref_dbg(grpc_exec_ctx *exec_ctx, polling_island *pi,
                          const char *reason, const char *file, int line) {
-  long old_cnt = gpr_atm_acq_load(&pi->ref_count);
-  pi_unref(exec_ctx, pi);
-  gpr_log(GPR_DEBUG, "Unref pi: %p, old:%ld -> new:%ld (%s) - (%s, %d)",
+  if (GRPC_TRACER_ON(grpc_trace_workqueue_refcount)) {
+    long old_cnt = gpr_atm_acq_load(&pi->ref_count);
+    gpr_log(GPR_DEBUG, "Unref pi: %p, old:%ld -> new:%ld (%s) - (%s, %d)",
           (void *)pi, old_cnt, (old_cnt - 1), reason, file, line);
+  }
+  pi_unref(exec_ctx, pi);
 }
 #endif
 
diff --git a/src/core/lib/iomgr/ev_poll_posix.c b/src/core/lib/iomgr/ev_poll_posix.c
index d1a27b8228..0023cdba6f 100644
--- a/src/core/lib/iomgr/ev_poll_posix.c
+++ b/src/core/lib/iomgr/ev_poll_posix.c
@@ -1272,6 +1272,7 @@ static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
 }
 
 /*******************************************************************************
+
  * Condition Variable polling extensions
  */
 
diff --git a/src/core/lib/iomgr/ev_posix.c b/src/core/lib/iomgr/ev_posix.c
index 54960d1ecc..a43763e92a 100644
--- a/src/core/lib/iomgr/ev_posix.c
+++ b/src/core/lib/iomgr/ev_posix.c
@@ -38,6 +38,7 @@
 #include "src/core/lib/iomgr/ev_poll_posix.h"
 #include "src/core/lib/support/env.h"
 
+
 grpc_tracer_flag grpc_polling_trace =
     GRPC_TRACER_INITIALIZER(false); /* Disabled by default */
 
diff --git a/src/core/lib/transport/transport.c b/src/core/lib/transport/transport.c
index 2bedbcd290..4d7b942da9 100644
--- a/src/core/lib/transport/transport.c
+++ b/src/core/lib/transport/transport.c
@@ -31,7 +31,10 @@
 #include "src/core/lib/support/string.h"
 #include "src/core/lib/transport/transport_impl.h"
 
+
+#ifndef NDEBUG
 grpc_tracer_flag grpc_trace_stream_refcount = GRPC_TRACER_INITIALIZER(false);
+#endif
 
 #ifndef NDEBUG
 void grpc_stream_ref(grpc_stream_refcount *refcount, const char *reason) {
diff --git a/src/core/lib/transport/transport.h b/src/core/lib/transport/transport.h
index 57c18d1e30..811610ffbf 100644
--- a/src/core/lib/transport/transport.h
+++ b/src/core/lib/transport/transport.h
@@ -42,7 +42,9 @@ typedef struct grpc_transport grpc_transport;
    for a stream. */
 typedef struct grpc_stream grpc_stream;
 
+#ifndef NDEBUG
 extern grpc_tracer_flag grpc_trace_stream_refcount;
+#endif
 
 typedef struct grpc_stream_refcount {
   gpr_refcount refs;
-- 
GitLab