[export] Update latent_see API to be extensible to multiple logging sinks. ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/653824016](http://cl/653824016) PiperOrigin-RevId: 653824016
diff --git a/CMakeLists.txt b/CMakeLists.txt index df39130..28b7f14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -2653,6 +2653,7 @@ src/core/util/json/json_util.cc src/core/util/json/json_writer.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc src/core/xds/grpc/certificate_provider_store.cc src/core/xds/grpc/file_watcher_certificate_provider_factory.cc src/core/xds/grpc/xds_audit_logger_registry.cc @@ -3376,6 +3377,7 @@ src/core/util/json/json_reader.cc src/core/util/json/json_writer.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc ${gRPC_ADDITIONAL_DLL_SRC} ) @@ -5544,6 +5546,7 @@ src/core/util/json/json_reader.cc src/core/util/json/json_writer.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc ) target_compile_features(grpc_authorization_provider PUBLIC cxx_std_14) @@ -6511,6 +6514,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/activity_test.cc ) if(WIN32 AND MSVC) @@ -6545,6 +6549,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -9107,6 +9112,7 @@ src/core/lib/transport/status_conversion.cc src/core/lib/transport/timeout_encoding.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/transport/call_filters_test.cc ) if(WIN32 AND MSVC) @@ -9325,6 +9331,7 @@ src/core/lib/promise/activity.cc src/core/lib/transport/call_state.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/transport/call_state_test.cc ) if(WIN32 AND MSVC) @@ -9359,6 +9366,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -9648,6 +9656,7 @@ src/core/tsi/alts/handshaker/transport_security_common_api.cc src/core/util/json/json_writer.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/call/call_utils_test.cc ) if(WIN32 AND MSVC) @@ -10095,6 +10104,7 @@ src/core/lib/slice/slice.cc src/core/lib/slice/slice_string_helpers.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/cancel_callback_test.cc ) if(WIN32 AND MSVC) @@ -10131,6 +10141,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::function_ref absl::hash @@ -11195,6 +11206,7 @@ src/core/lib/slice/slice.cc src/core/lib/slice/slice_string_helpers.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/gprpp/chunked_vector_test.cc ) if(WIN32 AND MSVC) @@ -11231,6 +11243,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::function_ref absl::hash @@ -14491,6 +14504,7 @@ src/core/lib/slice/slice.cc src/core/lib/slice/slice_string_helpers.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/exec_ctx_wakeup_scheduler_test.cc ) if(WIN32 AND MSVC) @@ -14527,6 +14541,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -15291,6 +15306,7 @@ src/core/lib/slice/slice_string_helpers.cc src/core/lib/transport/bdp_estimator.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/transport/chttp2/flow_control_test.cc ) if(WIN32 AND MSVC) @@ -15327,6 +15343,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::function_ref absl::hash @@ -15369,6 +15386,7 @@ src/core/lib/slice/slice.cc src/core/lib/slice/slice_string_helpers.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/for_each_test.cc ) if(WIN32 AND MSVC) @@ -15405,6 +15423,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::function_ref absl::hash @@ -18792,6 +18811,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/inter_activity_pipe_test.cc ) if(WIN32 AND MSVC) @@ -18826,6 +18846,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -18908,6 +18929,7 @@ src/core/lib/slice/slice.cc src/core/lib/slice/slice_string_helpers.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/interceptor_list_test.cc ) if(WIN32 AND MSVC) @@ -18944,6 +18966,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::function_ref absl::hash @@ -19676,6 +19699,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/latch_test.cc ) if(WIN32 AND MSVC) @@ -19710,6 +19734,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -20156,6 +20181,7 @@ src/core/lib/slice/slice.cc src/core/lib/slice/slice_string_helpers.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/map_pipe_test.cc ) if(WIN32 AND MSVC) @@ -20192,6 +20218,7 @@ upb_mini_descriptor_lib upb_wire_lib absl::config + absl::no_destructor absl::flat_hash_map absl::function_ref absl::hash @@ -21118,6 +21145,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/mpsc_test.cc ) if(WIN32 AND MSVC) @@ -21152,6 +21180,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -21621,6 +21650,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/observable_test.cc ) if(WIN32 AND MSVC) @@ -21655,6 +21685,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -23496,6 +23527,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/promise_mutex_test.cc ) if(WIN32 AND MSVC) @@ -23530,6 +23562,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits @@ -33269,6 +33302,7 @@ src/core/lib/gprpp/per_cpu.cc src/core/lib/promise/activity.cc src/core/util/latent_see.cc + src/core/util/latent_see_impl.cc test/core/promise/wait_for_callback_test.cc ) if(WIN32 AND MSVC) @@ -33303,6 +33337,7 @@ ${_gRPC_ALLTARGETS_LIBRARIES} gtest absl::config + absl::no_destructor absl::flat_hash_map absl::hash absl::type_traits
diff --git a/Makefile b/Makefile index 8db5503..e9726d4 100644 --- a/Makefile +++ b/Makefile
@@ -1462,6 +1462,7 @@ src/core/util/json/json_util.cc \ src/core/util/json/json_writer.cc \ src/core/util/latent_see.cc \ + src/core/util/latent_see_impl.cc \ src/core/util/linux/cpu.cc \ src/core/util/linux/log.cc \ src/core/util/log.cc \
diff --git a/Package.swift b/Package.swift index bba9497..9bdae08 100644 --- a/Package.swift +++ b/Package.swift
@@ -1932,6 +1932,8 @@ "src/core/util/json/json_writer.h", "src/core/util/latent_see.cc", "src/core/util/latent_see.h", + "src/core/util/latent_see_impl.cc", + "src/core/util/latent_see_impl.h", "src/core/util/linux/cpu.cc", "src/core/util/linux/log.cc", "src/core/util/log.cc",
diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 3ec6313..c69618d 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml
@@ -1218,6 +1218,7 @@ - src/core/util/json/json_util.h - src/core/util/json/json_writer.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - src/core/util/upb_utils.h - src/core/xds/grpc/certificate_provider_store.h @@ -2032,6 +2033,7 @@ - src/core/util/json/json_util.cc - src/core/util/json/json_writer.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - src/core/xds/grpc/certificate_provider_store.cc - src/core/xds/grpc/file_watcher_certificate_provider_factory.cc - src/core/xds/grpc/xds_audit_logger_registry.cc @@ -2705,6 +2707,7 @@ - src/core/util/json/json_reader.h - src/core/util/json/json_writer.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - src/core/util/upb_utils.h - third_party/upb/upb/generated_code_support.h @@ -3103,6 +3106,7 @@ - src/core/util/json/json_reader.cc - src/core/util/json/json_writer.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc deps: - upb_mini_descriptor_lib - upb_wire_lib @@ -4755,6 +4759,7 @@ - src/core/util/json/json_reader.h - src/core/util/json/json_writer.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - third_party/upb/upb/generated_code_support.h src: @@ -5023,6 +5028,7 @@ - src/core/util/json/json_reader.cc - src/core/util/json/json_writer.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc deps: - upb_mini_descriptor_lib - upb_wire_lib @@ -5447,6 +5453,7 @@ - src/core/lib/promise/seq.h - src/core/lib/promise/wait_set.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/test_wakeup_schedulers.h src: - src/core/lib/debug/trace.cc @@ -5456,10 +5463,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/activity_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -6677,6 +6686,7 @@ - src/core/lib/transport/status_conversion.h - src/core/lib/transport/timeout_encoding.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - test/core/promise/poll_matcher.h - third_party/upb/upb/generated_code_support.h @@ -6725,6 +6735,7 @@ - src/core/lib/transport/status_conversion.cc - src/core/lib/transport/timeout_encoding.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/transport/call_filters_test.cc deps: - gtest @@ -6864,6 +6875,7 @@ - src/core/lib/promise/status_flag.h - src/core/lib/transport/call_state.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/poll_matcher.h src: - src/core/lib/debug/trace.cc @@ -6874,10 +6886,12 @@ - src/core/lib/promise/activity.cc - src/core/lib/transport/call_state.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/transport/call_state_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -7208,6 +7222,7 @@ - src/core/util/json/json_args.h - src/core/util/json/json_writer.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - third_party/upb/upb/generated_code_support.h src: @@ -7445,6 +7460,7 @@ - src/core/tsi/alts/handshaker/transport_security_common_api.cc - src/core/util/json/json_writer.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/call/call_utils_test.cc deps: - gtest @@ -7861,6 +7877,7 @@ - src/core/lib/slice/slice_refcount.h - src/core/lib/slice/slice_string_helpers.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - third_party/upb/upb/generated_code_support.h src: @@ -7892,12 +7909,14 @@ - src/core/lib/slice/slice.cc - src/core/lib/slice/slice_string_helpers.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/cancel_callback_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/functional:function_ref - absl/hash:hash @@ -8656,6 +8675,7 @@ - src/core/lib/slice/slice_refcount.h - src/core/lib/slice/slice_string_helpers.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - third_party/upb/upb/generated_code_support.h src: @@ -8687,12 +8707,14 @@ - src/core/lib/slice/slice.cc - src/core/lib/slice/slice_string_helpers.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/gprpp/chunked_vector_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/functional:function_ref - absl/hash:hash @@ -10204,6 +10226,7 @@ - src/core/lib/slice/slice_refcount.h - src/core/lib/slice/slice_string_helpers.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - third_party/upb/upb/generated_code_support.h src: @@ -10229,12 +10252,14 @@ - src/core/lib/slice/slice.cc - src/core/lib/slice/slice_string_helpers.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/exec_ctx_wakeup_scheduler_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -10754,6 +10779,7 @@ - src/core/lib/transport/bdp_estimator.h - src/core/lib/transport/http2_errors.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - third_party/upb/upb/generated_code_support.h src: @@ -10789,12 +10815,14 @@ - src/core/lib/slice/slice_string_helpers.cc - src/core/lib/transport/bdp_estimator.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/transport/chttp2/flow_control_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/functional:function_ref - absl/hash:hash @@ -10870,6 +10898,7 @@ - src/core/lib/slice/slice_refcount.h - src/core/lib/slice/slice_string_helpers.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - test/core/promise/test_wakeup_schedulers.h - third_party/upb/upb/generated_code_support.h @@ -10902,12 +10931,14 @@ - src/core/lib/slice/slice.cc - src/core/lib/slice/slice_string_helpers.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/for_each_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/functional:function_ref - absl/hash:hash @@ -12454,6 +12485,7 @@ - src/core/lib/promise/poll.h - src/core/lib/promise/seq.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/test_wakeup_schedulers.h src: - src/core/lib/debug/trace.cc @@ -12463,10 +12495,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/inter_activity_pipe_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -12544,6 +12578,7 @@ - src/core/lib/slice/slice_refcount.h - src/core/lib/slice/slice_string_helpers.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - test/core/promise/test_context.h - third_party/upb/upb/generated_code_support.h @@ -12576,12 +12611,14 @@ - src/core/lib/slice/slice.cc - src/core/lib/slice/slice_string_helpers.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/interceptor_list_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/functional:function_ref - absl/hash:hash @@ -13016,6 +13053,7 @@ - src/core/lib/promise/poll.h - src/core/lib/promise/seq.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/test_wakeup_schedulers.h src: - src/core/lib/debug/trace.cc @@ -13025,10 +13063,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/latch_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -13249,6 +13289,7 @@ - src/core/lib/slice/slice_refcount.h - src/core/lib/slice/slice_string_helpers.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - src/core/util/spinlock.h - test/core/promise/test_wakeup_schedulers.h - third_party/upb/upb/generated_code_support.h @@ -13281,12 +13322,14 @@ - src/core/lib/slice/slice.cc - src/core/lib/slice/slice_string_helpers.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/map_pipe_test.cc deps: - gtest - upb_mini_descriptor_lib - upb_wire_lib - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/functional:function_ref - absl/hash:hash @@ -13803,6 +13846,7 @@ - src/core/lib/promise/promise.h - src/core/lib/promise/wait_set.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/poll_matcher.h src: - src/core/lib/debug/trace.cc @@ -13812,10 +13856,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/mpsc_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -14116,6 +14162,7 @@ - src/core/lib/promise/observable.h - src/core/lib/promise/poll.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/poll_matcher.h src: - src/core/lib/debug/trace.cc @@ -14125,10 +14172,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/observable_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -15031,6 +15080,7 @@ - src/core/lib/promise/promise_mutex.h - src/core/lib/promise/seq.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/test_wakeup_schedulers.h src: - src/core/lib/debug/trace.cc @@ -15040,10 +15090,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/promise_mutex_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits @@ -20820,6 +20872,7 @@ - src/core/lib/promise/poll.h - src/core/lib/promise/wait_for_callback.h - src/core/util/latent_see.h + - src/core/util/latent_see_impl.h - test/core/promise/test_wakeup_schedulers.h src: - src/core/lib/debug/trace.cc @@ -20829,10 +20882,12 @@ - src/core/lib/gprpp/per_cpu.cc - src/core/lib/promise/activity.cc - src/core/util/latent_see.cc + - src/core/util/latent_see_impl.cc - test/core/promise/wait_for_callback_test.cc deps: - gtest - absl/base:config + - absl/base:no_destructor - absl/container:flat_hash_map - absl/hash:hash - absl/meta:type_traits
diff --git a/config.m4 b/config.m4 index 619d976..615d952 100644 --- a/config.m4 +++ b/config.m4
@@ -837,6 +837,7 @@ src/core/util/json/json_util.cc \ src/core/util/json/json_writer.cc \ src/core/util/latent_see.cc \ + src/core/util/latent_see_impl.cc \ src/core/util/linux/cpu.cc \ src/core/util/linux/log.cc \ src/core/util/log.cc \
diff --git a/config.w32 b/config.w32 index 03840640..711e4a8 100644 --- a/config.w32 +++ b/config.w32
@@ -802,6 +802,7 @@ "src\\core\\util\\json\\json_util.cc " + "src\\core\\util\\json\\json_writer.cc " + "src\\core\\util\\latent_see.cc " + + "src\\core\\util\\latent_see_impl.cc " + "src\\core\\util\\linux\\cpu.cc " + "src\\core\\util\\linux\\log.cc " + "src\\core\\util\\log.cc " +
diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index f77ce80..42686fd 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec
@@ -1322,6 +1322,7 @@ 'src/core/util/json/json_util.h', 'src/core/util/json/json_writer.h', 'src/core/util/latent_see.h', + 'src/core/util/latent_see_impl.h', 'src/core/util/spinlock.h', 'src/core/util/string.h', 'src/core/util/time_precise.h', @@ -2605,6 +2606,7 @@ 'src/core/util/json/json_util.h', 'src/core/util/json/json_writer.h', 'src/core/util/latent_see.h', + 'src/core/util/latent_see_impl.h', 'src/core/util/spinlock.h', 'src/core/util/string.h', 'src/core/util/time_precise.h',
diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 3655e9e..065c795 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec
@@ -2048,6 +2048,8 @@ 'src/core/util/json/json_writer.h', 'src/core/util/latent_see.cc', 'src/core/util/latent_see.h', + 'src/core/util/latent_see_impl.cc', + 'src/core/util/latent_see_impl.h', 'src/core/util/linux/cpu.cc', 'src/core/util/linux/log.cc', 'src/core/util/log.cc', @@ -3386,6 +3388,7 @@ 'src/core/util/json/json_util.h', 'src/core/util/json/json_writer.h', 'src/core/util/latent_see.h', + 'src/core/util/latent_see_impl.h', 'src/core/util/spinlock.h', 'src/core/util/string.h', 'src/core/util/time_precise.h',
diff --git a/grpc.gemspec b/grpc.gemspec index 56e70fe..2bab663 100644 --- a/grpc.gemspec +++ b/grpc.gemspec
@@ -1934,6 +1934,8 @@ s.files += %w( src/core/util/json/json_writer.h ) s.files += %w( src/core/util/latent_see.cc ) s.files += %w( src/core/util/latent_see.h ) + s.files += %w( src/core/util/latent_see_impl.cc ) + s.files += %w( src/core/util/latent_see_impl.h ) s.files += %w( src/core/util/linux/cpu.cc ) s.files += %w( src/core/util/linux/log.cc ) s.files += %w( src/core/util/log.cc )
diff --git a/package.xml b/package.xml index 3855a5f..fab94fc 100644 --- a/package.xml +++ b/package.xml
@@ -1916,6 +1916,8 @@ <file baseinstalldir="/" name="src/core/util/json/json_writer.h" role="src" /> <file baseinstalldir="/" name="src/core/util/latent_see.cc" role="src" /> <file baseinstalldir="/" name="src/core/util/latent_see.h" role="src" /> + <file baseinstalldir="/" name="src/core/util/latent_see_impl.cc" role="src" /> + <file baseinstalldir="/" name="src/core/util/latent_see_impl.h" role="src" /> <file baseinstalldir="/" name="src/core/util/linux/cpu.cc" role="src" /> <file baseinstalldir="/" name="src/core/util/linux/log.cc" role="src" /> <file baseinstalldir="/" name="src/core/util/log.cc" role="src" />
diff --git a/src/core/BUILD b/src/core/BUILD index 1acce19..e7a1044 100644 --- a/src/core/BUILD +++ b/src/core/BUILD
@@ -137,11 +137,14 @@ name = "latent_see", srcs = [ "util/latent_see.cc", + "util/latent_see_impl.cc", ], hdrs = [ "util/latent_see.h", + "util/latent_see_impl.h", ], external_deps = [ + "absl/base:no_destructor", "absl/log", "absl/strings", "absl/types:optional",
diff --git a/src/core/lib/gprpp/work_serializer.cc b/src/core/lib/gprpp/work_serializer.cc index 105aac2..910f082 100644 --- a/src/core/lib/gprpp/work_serializer.cc +++ b/src/core/lib/gprpp/work_serializer.cc
@@ -43,6 +43,7 @@ #include "src/core/telemetry/stats.h" #include "src/core/telemetry/stats_data.h" #include "src/core/util/latent_see.h" +#include "src/core/util/latent_see_impl.h" namespace grpc_core {
diff --git a/src/core/lib/promise/activity.h b/src/core/lib/promise/activity.h index 715a28c..b628114 100644 --- a/src/core/lib/promise/activity.h +++ b/src/core/lib/promise/activity.h
@@ -43,6 +43,7 @@ #include "src/core/lib/promise/detail/status.h" #include "src/core/lib/promise/poll.h" #include "src/core/util/latent_see.h" +#include "src/core/util/latent_see_impl.h" namespace grpc_core {
diff --git a/src/core/util/latent_see.cc b/src/core/util/latent_see.cc index cdfaf51..6a15f4b 100644 --- a/src/core/util/latent_see.cc +++ b/src/core/util/latent_see.cc
@@ -15,97 +15,87 @@ #include "src/core/util/latent_see.h" #ifdef GRPC_ENABLE_LATENT_SEE -#include <chrono> -#include <cstdint> - -#include "absl/strings/str_cat.h" -#include "absl/types/optional.h" +#include "src/core/util/latent_see_impl.h" +#include "absl/strings/string_view.h" namespace grpc_core { namespace latent_see { -thread_local std::vector<Log::Event> Log::thread_events_; -thread_local uint64_t Log::thread_id_ = Log::Get().next_thread_id_.fetch_add(1); -std::atomic<uint64_t> Flow::next_flow_id_{1}; +namespace { +LogFn g_logger = nullptr; +} // namespace -std::string Log::GenerateJson() { - std::vector<RecordedEvent> events; - for (auto& fragment : fragments_) { - MutexLock lock(&fragment.mu); - events.insert(events.end(), fragment.events.begin(), fragment.events.end()); - } - absl::optional<std::chrono::steady_clock::time_point> start_time; - for (auto& event : events) { - if (!start_time.has_value() || *start_time > event.event.timestamp) { - start_time = event.event.timestamp; - } - } - if (!start_time.has_value()) return "[]"; - std::string json = "[\n"; - bool first = true; - for (const auto& event : events) { - absl::string_view phase; - bool has_id; - switch (event.event.type) { - case EventType::kBegin: - phase = "B"; - has_id = false; - break; - case EventType::kEnd: - phase = "E"; - has_id = false; - break; - case EventType::kFlowStart: - phase = "s"; - has_id = true; - break; - case EventType::kFlowEnd: - phase = "f"; - has_id = true; - break; - case EventType::kMark: - phase = "i"; - has_id = false; - break; - } - if (!first) absl::StrAppend(&json, ",\n"); - first = false; - absl::StrAppend(&json, "{\"name\": ", event.event.metadata->name, - ", \"ph\": \"", phase, "\", \"ts\": ", - std::chrono::duration<double, std::micro>( - event.event.timestamp - *start_time) - .count(), - ", \"pid\": 0, \"tid\": ", event.thread_id); - if (has_id) { - absl::StrAppend(&json, ", \"id\": ", event.event.id); - } - if (event.event.type == EventType::kFlowEnd) { - absl::StrAppend(&json, ", \"bp\": \"e\""); - } - absl::StrAppend(&json, ", \"args\": {\"file\": \"", - event.event.metadata->file, - "\", \"line\": ", event.event.metadata->line, - ", \"batch\": ", event.batch_id, "}}"); - } - absl::StrAppend(&json, "\n]"); - return json; +MarkerStorage::Ops MarkerStorage::ops_ = { + .construct = + [](MarkerStorage* marker, const char* name, MarkerType /*type*/, + const char* file, int line, const char* /*function*/) { + static_assert(sizeof(MarkerStorage) >= sizeof(Metadata)); + new (marker->GetStorageAs<Metadata>()) Metadata(file, line, name); + }, + .destruct = [](MarkerStorage* /*marker*/) {}}; + +template <> +ParentScopeTimerStorage::Ops ParentScopeTimerStorage::ops_ = { + .construct = + [](ParentScopeTimerStorage* timer, MarkerStorage* marker) { + static_assert(sizeof(ParentScopeTimerStorage) >= sizeof(ParentScope)); + new (timer->GetStorageAs<ParentScope>()) + ParentScope(marker->GetStorageAs<Metadata>()); + }, + .destruct = + [](ParentScopeTimerStorage* timer) { + timer->GetStorageAs<ParentScope>()->~ParentScope(); + }}; + +template <> +InnerScopeTimerStorage::Ops ScopedTimerStorage<false>::ops_ = { + .construct = + [](InnerScopeTimerStorage* timer, MarkerStorage* marker) { + static_assert(sizeof(InnerScopeTimerStorage) >= sizeof(InnerScope)); + new (timer->GetStorageAs<InnerScope>()) + InnerScope(marker->GetStorageAs<Metadata>()); + }, + .destruct = + [](InnerScopeTimerStorage* timer) { + timer->GetStorageAs<InnerScope>()->~InnerScope(); + }}; + +GroupThreadScopeStorage::Ops GroupThreadScopeStorage::ops_ = { + .construct = + [](GroupThreadScopeStorage* timer, MarkerStorage* marker, + absl::string_view name) { + static_assert(sizeof(GroupThreadScopeStorage) >= sizeof(Flow)); + new (timer->GetStorageAs<Flow>()) + Flow(marker->GetStorageAs<Metadata>()); + }, + .destruct = + [](GroupThreadScopeStorage* timer) { + timer->GetStorageAs<Flow>()->~Flow(); + }}; + +void MarkerStorage::OverrideOps(MarkerStorage::Ops ops) { ops_ = ops; } + +void GroupThreadScopeStorage::OverrideOps(GroupThreadScopeStorage::Ops ops) { + ops_ = ops; } -void Log::FlushThreadLog() { - auto& thread_events = thread_events_; - if (thread_events.empty()) return; - auto& log = Get(); - const auto batch_id = - log.next_batch_id_.fetch_add(1, std::memory_order_relaxed); - auto& fragment = log.fragments_.this_cpu(); - const auto thread_id = thread_id_; - { - MutexLock lock(&fragment.mu); - for (auto event : thread_events) { - fragment.events.push_back(RecordedEvent{thread_id, batch_id, event}); - } +template <> +void ParentScopeTimerStorage::OverrideOps(ParentScopeTimerStorage::Ops ops) { + ops_ = ops; +} + +template <> +void InnerScopeTimerStorage::OverrideOps(InnerScopeTimerStorage::Ops ops) { + ops_ = ops; +} + +void SetEventLogger(LogFn logger) { g_logger = logger; } + +void LogEvent(MarkerStorage* marker) { + if (g_logger != nullptr) { + return g_logger(marker); } - thread_events.clear(); + Mark(marker->GetStorageAs<Metadata>()); } } // namespace latent_see
diff --git a/src/core/util/latent_see.h b/src/core/util/latent_see.h index c8e6686..0e22aef 100644 --- a/src/core/util/latent_see.h +++ b/src/core/util/latent_see.h
@@ -17,189 +17,175 @@ #include <grpc/support/port_platform.h> +#include <cstring> + #ifdef GRPC_ENABLE_LATENT_SEE -#include <chrono> #include <cstdint> -#include <utility> -#include <vector> -#include "absl/log/log.h" +#include "absl/base/no_destructor.h" +#include "absl/strings/string_view.h" -#include "src/core/lib/gprpp/per_cpu.h" -#include "src/core/lib/gprpp/sync.h" +#define GRPC_LATENT_SEE_INTERNAL_PASTE2(str, line) str##line +#define GRPC_LATENT_SEE_INTERNAL_PASTE1(str, line) \ + GRPC_LATENT_SEE_INTERNAL_PASTE2(str, line) +#define GRPC_LATENT_SEE_INTERNAL_UNIQUE(str) \ + GRPC_LATENT_SEE_INTERNAL_PASTE1(str, __LINE__) namespace grpc_core { namespace latent_see { -struct Metadata { - const char* file; - int line; - const char* name; -}; +constexpr int kInvalid = -1; +constexpr int StorageSize = 128; -enum class EventType : uint8_t { kBegin, kEnd, kFlowStart, kFlowEnd, kMark }; +class Marker; -class Log { +class MarkerStorage { public: - static void FlushThreadLog(); - - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static void Append( - const Metadata* metadata, EventType type, uint64_t id) { - thread_events_.push_back( - Event{metadata, std::chrono::steady_clock::now(), id, type}); + enum MarkerType : uint8_t { + INVALID = 0, + INNER_SCOPE = 1, + PARENT_SCOPE = 2, + EVENT = 3, + ERROR = 4, + }; + struct Ops { + void (*construct)(MarkerStorage*, const char* name, MarkerType type, + const char* file, int line, const char* function); + void (*destruct)(MarkerStorage*); + }; + MarkerStorage(const char* name, MarkerType type, const char* file, int line, + const char* function) { + ops_.construct(this, name, type, file, line, function); } + ~MarkerStorage() { ops_.destruct(this); } + // This type is neither copyable nor movable. + MarkerStorage(const MarkerStorage&) = delete; + MarkerStorage& operator=(const MarkerStorage&) = delete; + + template <typename T> + T* GetStorageAs() { + return reinterpret_cast<T*>(&storage_); + } + + static void OverrideOps(Ops ops); private: - Log() = default; - - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static Log& Get() { - static Log* log = []() { - atexit([] { - LOG(INFO) << "Writing latent_see.json in " << get_current_dir_name(); - FILE* f = fopen("latent_see.json", "w"); - if (f == nullptr) return; - fprintf(f, "%s", log->GenerateJson().c_str()); - fclose(f); - }); - return new Log(); - }(); - return *log; - } - - std::string GenerateJson(); - - struct Event { - const Metadata* metadata; - std::chrono::steady_clock::time_point timestamp; - uint64_t id; - EventType type; - }; - struct RecordedEvent { - uint64_t thread_id; - uint64_t batch_id; - Event event; - }; - std::atomic<uint64_t> next_thread_id_{1}; - std::atomic<uint64_t> next_batch_id_{1}; - static thread_local std::vector<Event> thread_events_; - static thread_local uint64_t thread_id_; - struct Fragment { - Mutex mu; - std::vector<RecordedEvent> events ABSL_GUARDED_BY(mu); - }; - PerCpu<Fragment> fragments_{PerCpuOptions()}; + static Ops ops_; + alignas(StorageSize) char storage_[StorageSize]; }; -template <bool kFlush> -class Scope { +template <bool is_parent> +class ScopedTimerStorage { public: - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION explicit Scope(const Metadata* metadata) - : metadata_(metadata) { - Log::Append(metadata_, EventType::kBegin, 0); - } - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION ~Scope() { - Log::Append(metadata_, EventType::kEnd, 0); - if (kFlush) Log::FlushThreadLog(); + struct Ops { + void (*construct)(ScopedTimerStorage<is_parent>*, MarkerStorage*); + void (*destruct)(ScopedTimerStorage<is_parent>*); + }; + ScopedTimerStorage() = default; + void Init(MarkerStorage* marker) { ops_.construct(this, marker); } + ~ScopedTimerStorage() { ops_.destruct(this); } + // This type is neither copyable nor movable. + ScopedTimerStorage(const ScopedTimerStorage<is_parent>&) = delete; + ScopedTimerStorage& operator=(const ScopedTimerStorage<is_parent>&) = delete; + + template <typename T> + T* GetStorageAs() { + return reinterpret_cast<T*>(&storage_); } - Scope(const Scope&) = delete; - Scope& operator=(const Scope&) = delete; + static void OverrideOps(Ops ops); private: - const Metadata* const metadata_; + static Ops ops_; + alignas(StorageSize) char storage_[StorageSize]; }; -using ParentScope = Scope<true>; -using InnerScope = Scope<false>; - -class Flow { +class GroupThreadScopeStorage { public: - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION Flow() : metadata_(nullptr) {} - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION explicit Flow(const Metadata* metadata) - : metadata_(metadata), - id_(next_flow_id_.fetch_add(1, std::memory_order_relaxed)) { - Log::Append(metadata_, EventType::kFlowStart, id_); + struct Ops { + void (*construct)(GroupThreadScopeStorage*, MarkerStorage*, + absl::string_view name); + void (*destruct)(GroupThreadScopeStorage*); + }; + GroupThreadScopeStorage(MarkerStorage* marker, absl::string_view name) { + ops_.construct(this, marker, name); } - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION ~Flow() { - if (metadata_ != nullptr) { - Log::Append(metadata_, EventType::kFlowEnd, id_); - } + ~GroupThreadScopeStorage() { ops_.destruct(this); } + // This type is neither copyable nor movable. + GroupThreadScopeStorage(const GroupThreadScopeStorage&) = delete; + GroupThreadScopeStorage& operator=(const GroupThreadScopeStorage&) = delete; + + template <typename T> + T* GetStorageAs() { + return reinterpret_cast<T*>(&storage_); } - Flow(const Flow&) = delete; - Flow& operator=(const Flow&) = delete; - Flow(Flow&& other) noexcept - : metadata_(std::exchange(other.metadata_, nullptr)), id_(other.id_) {} - Flow& operator=(Flow&& other) noexcept { - if (metadata_ != nullptr) Log::Append(metadata_, EventType::kFlowEnd, id_); - metadata_ = std::exchange(other.metadata_, nullptr); - id_ = other.id_; - return *this; - } - - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION bool is_active() const { - return metadata_ != nullptr; - } - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void End() { - if (metadata_ == nullptr) return; - Log::Append(metadata_, EventType::kFlowEnd, id_); - metadata_ = nullptr; - } - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void Begin(const Metadata* metadata) { - if (metadata_ != nullptr) Log::Append(metadata_, EventType::kFlowEnd, id_); - metadata_ = metadata; - if (metadata_ == nullptr) return; - id_ = next_flow_id_.fetch_add(1, std::memory_order_relaxed); - Log::Append(metadata_, EventType::kFlowStart, id_); - } + static void OverrideOps(Ops ops); private: - const Metadata* metadata_; - uint64_t id_; - static std::atomic<uint64_t> next_flow_id_; + static Ops ops_; + alignas(StorageSize) char storage_[StorageSize]; }; -GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline void Mark(const Metadata* md) { - Log::Append(md, EventType::kMark, 0); -} +using ParentScopeTimerStorage = ScopedTimerStorage<true>; +using InnerScopeTimerStorage = ScopedTimerStorage<false>; + +typedef void (*LogFn)(MarkerStorage*); + +void SetEventLogger(LogFn logger); + +void LogEvent(MarkerStorage* marker); } // namespace latent_see } // namespace grpc_core -#define GRPC_LATENT_SEE_METADATA(name) \ - []() { \ - static grpc_core::latent_see::Metadata metadata = {__FILE__, __LINE__, \ - #name}; \ - return &metadata; \ - }() + // Parent scope: logs a begin and end event, and flushes the thread log on scope // exit. Because the flush takes some time it's better to place one parent scope // at the top of the stack, and use lighter weight scopes within it. -#define GRPC_LATENT_SEE_PARENT_SCOPE(name) \ - grpc_core::latent_see::ParentScope latent_see_scope##__LINE__( \ - GRPC_LATENT_SEE_METADATA(name)) +#define GRPC_LATENT_SEE_PARENT_SCOPE(name) \ + static absl::NoDestructor<grpc_core::latent_see::MarkerStorage> \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker)( \ + #name, grpc_core::latent_see::MarkerStorage::PARENT_SCOPE, __FILE__, \ + __LINE__, __PRETTY_FUNCTION__); \ + grpc_core::latent_see::ParentScopeTimerStorage \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(timer); \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(timer).Init( \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker).get()) + // Inner scope: logs a begin and end event. Lighter weight than parent scope, // but does not flush the thread state - so should only be enclosed by a parent // scope. -#define GRPC_LATENT_SEE_INNER_SCOPE(name) \ - grpc_core::latent_see::InnerScope latent_see_scope##__LINE__( \ - GRPC_LATENT_SEE_METADATA(name)) +#define GRPC_LATENT_SEE_INNER_SCOPE(name) \ + static absl::NoDestructor<grpc_core::latent_see::MarkerStorage> \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker)( \ + #name, grpc_core::latent_see::MarkerStorage::INNER_SCOPE, __FILE__, \ + __LINE__, __PRETTY_FUNCTION__); \ + grpc_core::latent_see::InnerScopeTimerStorage \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(timer); \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(timer).Init( \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker).get()) + // Mark: logs a single event. // This is not flushed automatically, and so should only be used within a parent // scope. -#define GRPC_LATENT_SEE_MARK(name) \ - grpc_core::latent_see::Mark(GRPC_LATENT_SEE_METADATA(name)) +#define GRPC_LATENT_SEE_MARK(name) \ + static absl::NoDestructor<grpc_core::latent_see::MarkerStorage> \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker)( \ + #name, grpc_core::latent_see::MarkerStorage::EVENT, __FILE__, \ + __LINE__, __PRETTY_FUNCTION__); \ + grpc_core::latent_see::LogEvent(GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker).get()) + +// Denotes that all work done in the current thread will be grouped together +// by name until we leave the current scope. +#define GRPC_LATENT_SEE_GROUP(name) \ + static absl::NoDestructor<grpc_core::latent_see::MarkerStorage> \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker)( \ + "<group>", grpc_core::latent_see::MarkerStorage::SCOPE, __FILE__, \ + __LINE__, __PRETTY_FUNCTION__); \ + grpc_core::latent_see::GroupThreadScopeStorage \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(timer)( \ + GRPC_LATENT_SEE_INTERNAL_UNIQUE(marker).get(), #name) #else // !def(GRPC_ENABLE_LATENT_SEE) -namespace grpc_core { -namespace latent_see { -struct Metadata {}; -struct Flow { - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION bool is_active() const { return false; } - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void End() {} - GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void Begin(Metadata*) {} -}; -} // namespace latent_see -} // namespace grpc_core -#define GRPC_LATENT_SEE_METADATA(name) nullptr #define GRPC_LATENT_SEE_PARENT_SCOPE(name) \ do { \ } while (0) @@ -209,6 +195,9 @@ #define GRPC_LATENT_SEE_MARK(name) \ do { \ } while (0) +#define GRPC_LATENT_SEE_GROUP(name) \ + do { \ + } while (0) #endif // GRPC_ENABLE_LATENT_SEE #endif // GRPC_SRC_CORE_UTIL_LATENT_SEE_H
diff --git a/src/core/util/latent_see_impl.cc b/src/core/util/latent_see_impl.cc new file mode 100644 index 0000000..fe05c9a --- /dev/null +++ b/src/core/util/latent_see_impl.cc
@@ -0,0 +1,113 @@ +// Copyright 2024 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/core/util/latent_see_impl.h" + +#ifdef GRPC_ENABLE_LATENT_SEE +#include <chrono> +#include <cstdint> + +#include "absl/strings/str_cat.h" +#include "absl/types/optional.h" + +namespace grpc_core { +namespace latent_see { + +thread_local std::vector<Log::Event> Log::thread_events_; +thread_local uint64_t Log::thread_id_ = Log::Get().next_thread_id_.fetch_add(1); +std::atomic<uint64_t> Flow::next_flow_id_{1}; + +std::string Log::GenerateJson() { + std::vector<RecordedEvent> events; + for (auto& fragment : fragments_) { + MutexLock lock(&fragment.mu); + events.insert(events.end(), fragment.events.begin(), fragment.events.end()); + } + absl::optional<std::chrono::steady_clock::time_point> start_time; + for (auto& event : events) { + if (!start_time.has_value() || *start_time > event.event.timestamp) { + start_time = event.event.timestamp; + } + } + if (!start_time.has_value()) return "[]"; + std::string json = "[\n"; + bool first = true; + for (const auto& event : events) { + absl::string_view phase; + bool has_id; + switch (event.event.type) { + case EventType::kBegin: + phase = "B"; + has_id = false; + break; + case EventType::kEnd: + phase = "E"; + has_id = false; + break; + case EventType::kFlowStart: + phase = "s"; + has_id = true; + break; + case EventType::kFlowEnd: + phase = "f"; + has_id = true; + break; + case EventType::kMark: + phase = "i"; + has_id = false; + break; + } + if (!first) absl::StrAppend(&json, ",\n"); + first = false; + absl::StrAppend(&json, "{\"name\": ", event.event.metadata->name, + ", \"ph\": \"", phase, "\", \"ts\": ", + std::chrono::duration<double, std::micro>( + event.event.timestamp - *start_time) + .count(), + ", \"pid\": 0, \"tid\": ", event.thread_id); + if (has_id) { + absl::StrAppend(&json, ", \"id\": ", event.event.id); + } + if (event.event.type == EventType::kFlowEnd) { + absl::StrAppend(&json, ", \"bp\": \"e\""); + } + absl::StrAppend(&json, ", \"args\": {\"file\": \"", + event.event.metadata->file, + "\", \"line\": ", event.event.metadata->line, + ", \"batch\": ", event.batch_id, "}}"); + } + absl::StrAppend(&json, "\n]"); + return json; +} + +void Log::FlushThreadLog() { + auto& thread_events = thread_events_; + if (thread_events.empty()) return; + auto& log = Get(); + const auto batch_id = + log.next_batch_id_.fetch_add(1, std::memory_order_relaxed); + auto& fragment = log.fragments_.this_cpu(); + const auto thread_id = thread_id_; + { + MutexLock lock(&fragment.mu); + for (auto event : thread_events) { + fragment.events.push_back(RecordedEvent{thread_id, batch_id, event}); + } + } + thread_events.clear(); +} + +} // namespace latent_see +} // namespace grpc_core +#endif
diff --git a/src/core/util/latent_see_impl.h b/src/core/util/latent_see_impl.h new file mode 100644 index 0000000..202dbce --- /dev/null +++ b/src/core/util/latent_see_impl.h
@@ -0,0 +1,189 @@ +// Copyright 2024 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRPC_SRC_CORE_UTIL_LATENT_SEE_IMPL_H_ +#define GRPC_SRC_CORE_UTIL_LATENT_SEE_IMPL_H_ + +#include <grpc/support/port_platform.h> + +#ifdef GRPC_ENABLE_LATENT_SEE +#include <chrono> +#include <cstdint> +#include <utility> +#include <vector> + +#include "src/core/lib/gprpp/per_cpu.h" +#include "src/core/lib/gprpp/sync.h" +#include "absl/log/log.h" + +namespace grpc_core { +namespace latent_see { + +struct Metadata { + Metadata(const char* file_name, int line_no, const char* md_name) + : file(file_name), line(line_no), name(md_name) {} + const char* file; + int line; + const char* name; +}; + +enum class EventType : uint8_t { kBegin, kEnd, kFlowStart, kFlowEnd, kMark }; + +class Log { + public: + static void FlushThreadLog(); + + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static void Append( + const Metadata* metadata, EventType type, uint64_t id) { + thread_events_.push_back( + Event{metadata, std::chrono::steady_clock::now(), id, type}); + } + + private: + Log() = default; + + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION static Log& Get() { + static Log* log = []() { + atexit([] { + LOG(ERROR) << "Writing latent_see.json in " << get_current_dir_name(); + FILE* f = fopen("/tmp/latent_see.json", "w"); + if (f == nullptr) return; + fprintf(f, "%s", log->GenerateJson().c_str()); + fclose(f); + }); + return new Log(); + }(); + return *log; + } + + std::string GenerateJson(); + + struct Event { + const Metadata* metadata; + std::chrono::steady_clock::time_point timestamp; + uint64_t id; + EventType type; + }; + struct RecordedEvent { + uint64_t thread_id; + uint64_t batch_id; + Event event; + }; + std::atomic<uint64_t> next_thread_id_{1}; + std::atomic<uint64_t> next_batch_id_{1}; + static thread_local std::vector<Event> thread_events_; + static thread_local uint64_t thread_id_; + struct Fragment { + Mutex mu; + std::vector<RecordedEvent> events ABSL_GUARDED_BY(mu); + }; + PerCpu<Fragment> fragments_{PerCpuOptions()}; +}; + +template <bool kFlush> +class Scope { + public: + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION explicit Scope(const Metadata* metadata) + : metadata_(metadata) { + Log::Append(metadata_, EventType::kBegin, 0); + } + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION ~Scope() { + Log::Append(metadata_, EventType::kEnd, 0); + if (kFlush) Log::FlushThreadLog(); + } + + Scope(const Scope&) = delete; + Scope& operator=(const Scope&) = delete; + + private: + const Metadata* const metadata_; +}; + +using ParentScope = Scope<true>; +using InnerScope = Scope<false>; + +class Flow { + public: + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION Flow() : metadata_(nullptr) {} + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION explicit Flow(const Metadata* metadata) + : metadata_(metadata), + id_(next_flow_id_.fetch_add(1, std::memory_order_relaxed)) { + Log::Append(metadata_, EventType::kFlowStart, id_); + } + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION ~Flow() { + if (metadata_ != nullptr) { + Log::Append(metadata_, EventType::kFlowEnd, id_); + } + } + + Flow(const Flow&) = delete; + Flow& operator=(const Flow&) = delete; + Flow(Flow&& other) noexcept + : metadata_(std::exchange(other.metadata_, nullptr)), id_(other.id_) {} + Flow& operator=(Flow&& other) noexcept { + if (metadata_ != nullptr) Log::Append(metadata_, EventType::kFlowEnd, id_); + metadata_ = std::exchange(other.metadata_, nullptr); + id_ = other.id_; + return *this; + } + + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION bool is_active() const { + return metadata_ != nullptr; + } + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void End() { + if (metadata_ == nullptr) return; + Log::Append(metadata_, EventType::kFlowEnd, id_); + metadata_ = nullptr; + } + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void Begin(const Metadata* metadata) { + if (metadata_ != nullptr) Log::Append(metadata_, EventType::kFlowEnd, id_); + metadata_ = metadata; + if (metadata_ == nullptr) return; + id_ = next_flow_id_.fetch_add(1, std::memory_order_relaxed); + Log::Append(metadata_, EventType::kFlowStart, id_); + } + + private: + const Metadata* metadata_; + uint64_t id_; + static std::atomic<uint64_t> next_flow_id_; +}; + +GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline void Mark(const Metadata* md) { + Log::Append(md, EventType::kMark, 0); +} + +} // namespace latent_see +} // namespace grpc_core +#define GRPC_LATENT_SEE_METADATA(name) \ + []() { \ + static grpc_core::latent_see::Metadata metadata = {__FILE__, __LINE__, \ + #name}; \ + return &metadata; \ + }() +#else // !def(GRPC_ENABLE_LATENT_SEE) +#define GRPC_LATENT_SEE_METADATA(name) nullptr +namespace grpc_core { +namespace latent_see { +struct Metadata {}; +struct Flow { + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION bool is_active() const { return false; } + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void End() {} + GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void Begin(Metadata*) {} +}; +} // namespace latent_see +} // namespace grpc_core +#endif // GRPC_ENABLE_LATENT_SEE + +#endif // GRPC_SRC_CORE_UTIL_LATENT_SEE_IMPL_H_
diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 75a2199..aad4e96 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py
@@ -811,6 +811,7 @@ 'src/core/util/json/json_util.cc', 'src/core/util/json/json_writer.cc', 'src/core/util/latent_see.cc', + 'src/core/util/latent_see_impl.cc', 'src/core/util/linux/cpu.cc', 'src/core/util/linux/log.cc', 'src/core/util/log.cc',
diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index e48f25e..5d63704 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal
@@ -2938,6 +2938,8 @@ src/core/util/json/json_writer.h \ src/core/util/latent_see.cc \ src/core/util/latent_see.h \ +src/core/util/latent_see_impl.cc \ +src/core/util/latent_see_impl.h \ src/core/util/linux/cpu.cc \ src/core/util/linux/log.cc \ src/core/util/log.cc \
diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index e345629..f2e4516 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal
@@ -2718,6 +2718,8 @@ src/core/util/json/json_writer.h \ src/core/util/latent_see.cc \ src/core/util/latent_see.h \ +src/core/util/latent_see_impl.cc \ +src/core/util/latent_see_impl.h \ src/core/util/linux/cpu.cc \ src/core/util/linux/log.cc \ src/core/util/log.cc \