diff --git a/src/core/lib/debug/stats.h b/src/core/lib/debug/stats.h index 749665262aa..9e88ad7000f 100644 --- a/src/core/lib/debug/stats.h +++ b/src/core/lib/debug/stats.h @@ -35,6 +35,9 @@ extern grpc_stats_data* grpc_stats_per_cpu_storage; #define GRPC_THREAD_STATS_DATA() \ (&grpc_stats_per_cpu_storage[grpc_core::ExecCtx::Get()->starting_cpu()]) +/* Only collect stats if GRPC_COLLECT_STATS is defined or it is a debug build. + */ +#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) #define GRPC_STATS_INC_COUNTER(ctr) \ (gpr_atm_no_barrier_fetch_add(&GRPC_THREAD_STATS_DATA()->counters[(ctr)], 1)) @@ -42,6 +45,10 @@ extern grpc_stats_data* grpc_stats_per_cpu_storage; (gpr_atm_no_barrier_fetch_add( \ &GRPC_THREAD_STATS_DATA()->histograms[histogram##_FIRST_SLOT + (index)], \ 1)) +#else /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ +#define GRPC_STATS_INC_COUNTER(ctr) +#define GRPC_STATS_INC_HISTOGRAM(histogram, index) +#endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ void grpc_stats_init(void); void grpc_stats_shutdown(void); diff --git a/src/core/lib/debug/stats_data.h b/src/core/lib/debug/stats_data.h index da1266ad736..37c548095f5 100644 --- a/src/core/lib/debug/stats_data.h +++ b/src/core/lib/debug/stats_data.h @@ -174,6 +174,7 @@ typedef enum { GRPC_STATS_HISTOGRAM_SERVER_CQS_CHECKED_BUCKETS = 8, GRPC_STATS_HISTOGRAM_BUCKETS = 840 } grpc_stats_histogram_constants; +#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) #define GRPC_STATS_INC_CLIENT_CALLS_CREATED() \ GRPC_STATS_INC_COUNTER(GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED) #define GRPC_STATS_INC_SERVER_CALLS_CREATED() \ @@ -427,6 +428,117 @@ void grpc_stats_inc_http2_send_flowctl_per_write(int x); #define GRPC_STATS_INC_SERVER_CQS_CHECKED(value) \ grpc_stats_inc_server_cqs_checked((int)(value)) void grpc_stats_inc_server_cqs_checked(int x); +#else +#define GRPC_STATS_INC_CLIENT_CALLS_CREATED() +#define GRPC_STATS_INC_SERVER_CALLS_CREATED() +#define GRPC_STATS_INC_CQS_CREATED() +#define GRPC_STATS_INC_CLIENT_CHANNELS_CREATED() +#define GRPC_STATS_INC_CLIENT_SUBCHANNELS_CREATED() +#define GRPC_STATS_INC_SERVER_CHANNELS_CREATED() +#define GRPC_STATS_INC_SYSCALL_POLL() +#define GRPC_STATS_INC_SYSCALL_WAIT() +#define GRPC_STATS_INC_POLLSET_KICK() +#define GRPC_STATS_INC_POLLSET_KICKED_WITHOUT_POLLER() +#define GRPC_STATS_INC_POLLSET_KICKED_AGAIN() +#define GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD() +#define GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV() +#define GRPC_STATS_INC_POLLSET_KICK_OWN_THREAD() +#define GRPC_STATS_INC_HISTOGRAM_SLOW_LOOKUPS() +#define GRPC_STATS_INC_SYSCALL_WRITE() +#define GRPC_STATS_INC_SYSCALL_READ() +#define GRPC_STATS_INC_TCP_BACKUP_POLLERS_CREATED() +#define GRPC_STATS_INC_TCP_BACKUP_POLLER_POLLS() +#define GRPC_STATS_INC_HTTP2_OP_BATCHES() +#define GRPC_STATS_INC_HTTP2_OP_CANCEL() +#define GRPC_STATS_INC_HTTP2_OP_SEND_INITIAL_METADATA() +#define GRPC_STATS_INC_HTTP2_OP_SEND_MESSAGE() +#define GRPC_STATS_INC_HTTP2_OP_SEND_TRAILING_METADATA() +#define GRPC_STATS_INC_HTTP2_OP_RECV_INITIAL_METADATA() +#define GRPC_STATS_INC_HTTP2_OP_RECV_MESSAGE() +#define GRPC_STATS_INC_HTTP2_OP_RECV_TRAILING_METADATA() +#define GRPC_STATS_INC_HTTP2_SETTINGS_WRITES() +#define GRPC_STATS_INC_HTTP2_PINGS_SENT() +#define GRPC_STATS_INC_HTTP2_WRITES_BEGUN() +#define GRPC_STATS_INC_HTTP2_WRITES_OFFLOADED() +#define GRPC_STATS_INC_HTTP2_WRITES_CONTINUED() +#define GRPC_STATS_INC_HTTP2_PARTIAL_WRITES() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_INITIAL_WRITE() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_START_NEW_STREAM() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_SEND_MESSAGE() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_SEND_INITIAL_METADATA() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_SEND_TRAILING_METADATA() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_RETRY_SEND_PING() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_CONTINUE_PINGS() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_GOAWAY_SENT() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_RST_STREAM() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_CLOSE_FROM_API() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_STREAM_FLOW_CONTROL() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_TRANSPORT_FLOW_CONTROL() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_SEND_SETTINGS() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_BDP_ESTIMATOR_PING() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_FLOW_CONTROL_UNSTALLED_BY_SETTING() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_FLOW_CONTROL_UNSTALLED_BY_UPDATE() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_APPLICATION_PING() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_KEEPALIVE_PING() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_TRANSPORT_FLOW_CONTROL_UNSTALLED() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_PING_RESPONSE() +#define GRPC_STATS_INC_HTTP2_INITIATE_WRITE_DUE_TO_FORCE_RST_STREAM() +#define GRPC_STATS_INC_HTTP2_SPURIOUS_WRITES_BEGUN() +#define GRPC_STATS_INC_HPACK_RECV_INDEXED() +#define GRPC_STATS_INC_HPACK_RECV_LITHDR_INCIDX() +#define GRPC_STATS_INC_HPACK_RECV_LITHDR_INCIDX_V() +#define GRPC_STATS_INC_HPACK_RECV_LITHDR_NOTIDX() +#define GRPC_STATS_INC_HPACK_RECV_LITHDR_NOTIDX_V() +#define GRPC_STATS_INC_HPACK_RECV_LITHDR_NVRIDX() +#define GRPC_STATS_INC_HPACK_RECV_LITHDR_NVRIDX_V() +#define GRPC_STATS_INC_HPACK_RECV_UNCOMPRESSED() +#define GRPC_STATS_INC_HPACK_RECV_HUFFMAN() +#define GRPC_STATS_INC_HPACK_RECV_BINARY() +#define GRPC_STATS_INC_HPACK_RECV_BINARY_BASE64() +#define GRPC_STATS_INC_HPACK_SEND_INDEXED() +#define GRPC_STATS_INC_HPACK_SEND_LITHDR_INCIDX() +#define GRPC_STATS_INC_HPACK_SEND_LITHDR_INCIDX_V() +#define GRPC_STATS_INC_HPACK_SEND_LITHDR_NOTIDX() +#define GRPC_STATS_INC_HPACK_SEND_LITHDR_NOTIDX_V() +#define GRPC_STATS_INC_HPACK_SEND_LITHDR_NVRIDX() +#define GRPC_STATS_INC_HPACK_SEND_LITHDR_NVRIDX_V() +#define GRPC_STATS_INC_HPACK_SEND_UNCOMPRESSED() +#define GRPC_STATS_INC_HPACK_SEND_HUFFMAN() +#define GRPC_STATS_INC_HPACK_SEND_BINARY() +#define GRPC_STATS_INC_HPACK_SEND_BINARY_BASE64() +#define GRPC_STATS_INC_COMBINER_LOCKS_INITIATED() +#define GRPC_STATS_INC_COMBINER_LOCKS_SCHEDULED_ITEMS() +#define GRPC_STATS_INC_COMBINER_LOCKS_SCHEDULED_FINAL_ITEMS() +#define GRPC_STATS_INC_COMBINER_LOCKS_OFFLOADED() +#define GRPC_STATS_INC_CALL_COMBINER_LOCKS_INITIATED() +#define GRPC_STATS_INC_CALL_COMBINER_LOCKS_SCHEDULED_ITEMS() +#define GRPC_STATS_INC_CALL_COMBINER_SET_NOTIFY_ON_CANCEL() +#define GRPC_STATS_INC_CALL_COMBINER_CANCELLED() +#define GRPC_STATS_INC_EXECUTOR_SCHEDULED_SHORT_ITEMS() +#define GRPC_STATS_INC_EXECUTOR_SCHEDULED_LONG_ITEMS() +#define GRPC_STATS_INC_EXECUTOR_SCHEDULED_TO_SELF() +#define GRPC_STATS_INC_EXECUTOR_WAKEUP_INITIATED() +#define GRPC_STATS_INC_EXECUTOR_QUEUE_DRAINED() +#define GRPC_STATS_INC_EXECUTOR_PUSH_RETRIES() +#define GRPC_STATS_INC_SERVER_REQUESTED_CALLS() +#define GRPC_STATS_INC_SERVER_SLOWPATH_REQUESTS_QUEUED() +#define GRPC_STATS_INC_CQ_EV_QUEUE_TRYLOCK_FAILURES() +#define GRPC_STATS_INC_CQ_EV_QUEUE_TRYLOCK_SUCCESSES() +#define GRPC_STATS_INC_CQ_EV_QUEUE_TRANSIENT_POP_FAILURES() +#define GRPC_STATS_INC_CALL_INITIAL_SIZE(value) +#define GRPC_STATS_INC_POLL_EVENTS_RETURNED(value) +#define GRPC_STATS_INC_TCP_WRITE_SIZE(value) +#define GRPC_STATS_INC_TCP_WRITE_IOV_SIZE(value) +#define GRPC_STATS_INC_TCP_READ_SIZE(value) +#define GRPC_STATS_INC_TCP_READ_OFFER(value) +#define GRPC_STATS_INC_TCP_READ_OFFER_IOV_SIZE(value) +#define GRPC_STATS_INC_HTTP2_SEND_MESSAGE_SIZE(value) +#define GRPC_STATS_INC_HTTP2_SEND_INITIAL_METADATA_PER_WRITE(value) +#define GRPC_STATS_INC_HTTP2_SEND_MESSAGE_PER_WRITE(value) +#define GRPC_STATS_INC_HTTP2_SEND_TRAILING_METADATA_PER_WRITE(value) +#define GRPC_STATS_INC_HTTP2_SEND_FLOWCTL_PER_WRITE(value) +#define GRPC_STATS_INC_SERVER_CQS_CHECKED(value) +#endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ extern const int grpc_stats_histo_buckets[13]; extern const int grpc_stats_histo_start[13]; extern const int* const grpc_stats_histo_bucket_boundaries[13]; diff --git a/src/core/lib/iomgr/exec_ctx.h b/src/core/lib/iomgr/exec_ctx.h index 72d0ae58c10..a8c709daa35 100644 --- a/src/core/lib/iomgr/exec_ctx.h +++ b/src/core/lib/iomgr/exec_ctx.h @@ -79,7 +79,7 @@ namespace grpc_core { * - Exactly one instance of ExecCtx must be created per thread. Instances must * always be called exec_ctx. * - Do not pass exec_ctx as a parameter to a function. Always access it using - * grpc_core::ExecCtx::Get() + * grpc_core::ExecCtx::Get(). */ class ExecCtx { public: @@ -101,8 +101,12 @@ class ExecCtx { ExecCtx(const ExecCtx&) = delete; ExecCtx& operator=(const ExecCtx&) = delete; - /** Return starting_cpu */ + /** Return starting_cpu. This is only required for stats collection and is + * hence only defined if GRPC_COLLECT_STATS is enabled. + */ +#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) unsigned starting_cpu() const { return starting_cpu_; } +#endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ struct CombinerData { /* currently active combiner: updated only via combiner.c */ @@ -128,12 +132,14 @@ class ExecCtx { /** Flush any work that has been enqueued onto this grpc_exec_ctx. * Caller must guarantee that no interfering locks are held. - * Returns true if work was performed, false otherwise. */ + * Returns true if work was performed, false otherwise. + */ bool Flush(); /** Returns true if we'd like to leave this execution context as soon as -possible: useful for deciding whether to do something more or not depending -on outside context */ + * possible: useful for deciding whether to do something more or not + * depending on outside context. + */ bool IsReadyToFinish() { if ((flags_ & GRPC_EXEC_CTX_FLAG_IS_FINISHED) == 0) { if (CheckReadyToFinish()) { @@ -147,12 +153,14 @@ on outside context */ } /** Returns the stored current time relative to start if valid, - * otherwise refreshes the stored time, sets it valid and returns the new - * value */ + * otherwise refreshes the stored time, sets it valid and returns the new + * value. + */ grpc_millis Now(); /** Invalidates the stored time value. A new time value will be set on calling - * Now() */ + * Now(). + */ void InvalidateNow() { now_is_valid_ = false; } /** To be used only by shutdown code in iomgr */ @@ -162,20 +170,20 @@ on outside context */ } /** To be used only for testing. - * Sets the now value + * Sets the now value. */ void TestOnlySetNow(grpc_millis new_val) { now_ = new_val; now_is_valid_ = true; } - /** Global initialization for ExecCtx. Called by iomgr */ + /** Global initialization for ExecCtx. Called by iomgr. */ static void GlobalInit(void); - /** Global shutdown for ExecCtx. Called by iomgr */ + /** Global shutdown for ExecCtx. Called by iomgr. */ static void GlobalShutdown(void) { gpr_tls_destroy(&exec_ctx_); } - /** Gets pointer to current exec_ctx */ + /** Gets pointer to current exec_ctx. */ static ExecCtx* Get() { return reinterpret_cast(gpr_tls_get(&exec_ctx_)); } @@ -185,19 +193,22 @@ on outside context */ } protected: - /** Check if ready to finish */ + /** Check if ready to finish. */ virtual bool CheckReadyToFinish() { return false; } - /** Disallow delete on ExecCtx */ + /** Disallow delete on ExecCtx. */ static void operator delete(void* p) { abort(); } private: - /** Set exec_ctx_ to exec_ctx */ + /** Set exec_ctx_ to exec_ctx. */ grpc_closure_list closure_list_ = GRPC_CLOSURE_LIST_INIT; CombinerData combiner_data_ = {nullptr, nullptr}; uintptr_t flags_; + +#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) unsigned starting_cpu_ = gpr_cpu_current_cpu(); +#endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ bool now_is_valid_ = false; grpc_millis now_ = 0; diff --git a/test/core/debug/stats_test.cc b/test/core/debug/stats_test.cc index 949a88f8f07..aeb13655bfd 100644 --- a/test/core/debug/stats_test.cc +++ b/test/core/debug/stats_test.cc @@ -142,9 +142,14 @@ INSTANTIATE_TEST_CASE_P(HistogramTestCases, HistogramTest, } // namespace grpc int main(int argc, char** argv) { +/* Only run this test if GRPC_COLLECT_STATS is defined or if it is a debug + * build. + */ +#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) ::testing::InitGoogleTest(&argc, argv); grpc_init(); int ret = RUN_ALL_TESTS(); grpc_shutdown(); return ret; +#endif } diff --git a/test/core/end2end/tests/simple_request.cc b/test/core/end2end/tests/simple_request.cc index 2d6ccad0ec4..941d9ae3198 100644 --- a/test/core/end2end/tests/simple_request.cc +++ b/test/core/end2end/tests/simple_request.cc @@ -235,12 +235,14 @@ static void simple_request_body(grpc_end2end_test_config config, if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) { expected_calls *= 2; } +#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) GPR_ASSERT(after->counters[GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED] - before->counters[GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED] == expected_calls); GPR_ASSERT(after->counters[GRPC_STATS_COUNTER_SERVER_CALLS_CREATED] - before->counters[GRPC_STATS_COUNTER_SERVER_CALLS_CREATED] == expected_calls); +#endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ gpr_free(before); gpr_free(after); } diff --git a/tools/codegen/core/gen_stats_data.py b/tools/codegen/core/gen_stats_data.py index 3ebd00d049f..33faeafc16c 100755 --- a/tools/codegen/core/gen_stats_data.py +++ b/tools/codegen/core/gen_stats_data.py @@ -265,6 +265,7 @@ with open('src/core/lib/debug/stats_data.h', 'w') as H: print >> H, " GRPC_STATS_HISTOGRAM_BUCKETS = %d" % first_slot print >> H, "} grpc_stats_histogram_constants;" + print >> H, "#if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)" for ctr in inst_map['Counter']: print >> H, ("#define GRPC_STATS_INC_%s() " + "GRPC_STATS_INC_COUNTER(GRPC_STATS_COUNTER_%s)") % ( @@ -274,6 +275,14 @@ with open('src/core/lib/debug/stats_data.h', 'w') as H: histogram.name.upper(), histogram.name.lower()) print >> H, "void grpc_stats_inc_%s(int x);" % histogram.name.lower() + print >> H, "#else" + for ctr in inst_map['Counter']: + print >> H, ("#define GRPC_STATS_INC_%s() ") % (ctr.name.upper()) + for histogram in inst_map['Histogram']: + print >> H, "#define GRPC_STATS_INC_%s(value)" % ( + histogram.name.upper()) + print >> H, "#endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */" + for i, tbl in enumerate(static_tables): print >> H, "extern const %s grpc_stats_table_%d[%d];" % (tbl[0], i, len(tbl[1]))