diff --git a/src/core/lib/channel/channelz_registry.cc b/src/core/lib/channel/channelz_registry.cc index 1680f03522a..c02322924ec 100644 --- a/src/core/lib/channel/channelz_registry.cc +++ b/src/core/lib/channel/channelz_registry.cc @@ -68,26 +68,10 @@ ChannelzRegistry::~ChannelzRegistry() { gpr_mu_destroy(&mu_); } -intptr_t ChannelzRegistry::Register(grpc_core::ChannelTrace* channel_trace) { - intptr_t prior = gpr_atm_no_barrier_fetch_add(&uuid_, 1); - gpr_mu_lock(&mu_); - avl_ = grpc_avl_add(avl_, (void*)prior, channel_trace, nullptr); - gpr_mu_unlock(&mu_); - return prior; -} - void ChannelzRegistry::Unregister(intptr_t uuid) { gpr_mu_lock(&mu_); avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr); gpr_mu_unlock(&mu_); } -grpc_core::ChannelTrace* ChannelzRegistry::Get(intptr_t uuid) { - gpr_mu_lock(&mu_); - grpc_core::ChannelTrace* ret = static_cast( - grpc_avl_get(avl_, (void*)uuid, nullptr)); - gpr_mu_unlock(&mu_); - return ret; -} - } // namespace grpc_core diff --git a/src/core/lib/channel/channelz_registry.h b/src/core/lib/channel/channelz_registry.h index 1e4ee6edd9c..57fffa8b5a5 100644 --- a/src/core/lib/channel/channelz_registry.h +++ b/src/core/lib/channel/channelz_registry.h @@ -45,13 +45,29 @@ class ChannelzRegistry { // Returned the singleton instance of ChannelzRegistry; static ChannelzRegistry* Default(); - // globally registers a ChannelTrace. Returns its unique uuid - intptr_t Register(grpc_core::ChannelTrace* channel_trace); - // globally unregisters the ChannelTrace that is associated to uuid. + // globally registers a channelz Object. Returns its unique uuid + template + intptr_t Register(Object* object) { + intptr_t prior = gpr_atm_no_barrier_fetch_add(&uuid_, 1); + gpr_mu_lock(&mu_); + avl_ = grpc_avl_add(avl_, (void*)prior, object, nullptr); + gpr_mu_unlock(&mu_); + return prior; + } + + // globally unregisters the object that is associated to uuid. void Unregister(intptr_t uuid); + // if object with uuid has previously been registered, returns the - // ChannelTrace associated with that uuid. Else returns nullptr. - grpc_core::ChannelTrace* Get(intptr_t uuid); + // Object associated with that uuid. Else returns nullptr. + template + Object* Get(intptr_t uuid) { + gpr_mu_lock(&mu_); + Object* ret = + static_cast(grpc_avl_get(avl_, (void*)uuid, nullptr)); + gpr_mu_unlock(&mu_); + return ret; + } private: GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW diff --git a/test/core/channel/channel_trace_test.cc b/test/core/channel/channel_trace_test.cc index b91956a6635..78964b2d723 100644 --- a/test/core/channel/channel_trace_test.cc +++ b/test/core/channel/channel_trace_test.cc @@ -99,7 +99,8 @@ void ValidateTraceDataMatchedUuidLookup(RefCountedPtr tracer) { intptr_t uuid = tracer->GetUuid(); if (uuid == -1) return; // Doesn't make sense to lookup if tracing disabled char* tracer_json_str = tracer->RenderTrace(); - ChannelTrace* uuid_lookup = ChannelzRegistry::Default()->Get(uuid); + ChannelTrace* uuid_lookup = + ChannelzRegistry::Default()->Get(uuid); char* uuid_lookup_json_str = uuid_lookup->RenderTrace(); EXPECT_EQ(strcmp(tracer_json_str, uuid_lookup_json_str), 0); gpr_free(tracer_json_str);