diff options
author | Noah Eisen <ncteisen@gmail.com> | 2018-05-17 19:34:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-17 19:34:15 -0700 |
commit | 654ecedd0309740b16b53861ae3c24eecd55c55a (patch) | |
tree | ef3c7e7407d10de77d820301786bf09636decacd /src/core/lib/channel | |
parent | 39604e410d7d626777bec17e5d94b13798e08dbb (diff) | |
parent | 3a3bbaf11cfcaa39351cbdadcd2f0d5a47e951a5 (diff) |
Merge pull request #15341 from ncteisen/registry++
C++-ify the ChannelzRegistry, Adds Generic Object Support
Diffstat (limited to 'src/core/lib/channel')
-rw-r--r-- | src/core/lib/channel/channel_trace.cc | 6 | ||||
-rw-r--r-- | src/core/lib/channel/channel_trace_registry.cc | 80 | ||||
-rw-r--r-- | src/core/lib/channel/channel_trace_registry.h | 43 | ||||
-rw-r--r-- | src/core/lib/channel/channelz_registry.cc | 77 | ||||
-rw-r--r-- | src/core/lib/channel/channelz_registry.h | 99 |
5 files changed, 179 insertions, 126 deletions
diff --git a/src/core/lib/channel/channel_trace.cc b/src/core/lib/channel/channel_trace.cc index 654300cd32..eb7214b355 100644 --- a/src/core/lib/channel/channel_trace.cc +++ b/src/core/lib/channel/channel_trace.cc @@ -28,7 +28,7 @@ #include <stdlib.h> #include <string.h> -#include "src/core/lib/channel/channel_trace_registry.h" +#include "src/core/lib/channel/channelz_registry.h" #include "src/core/lib/channel/status_util.h" #include "src/core/lib/gpr/string.h" #include "src/core/lib/gpr/useful.h" @@ -70,7 +70,7 @@ ChannelTrace::ChannelTrace(size_t max_events) tail_trace_(nullptr) { if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0 gpr_mu_init(&tracer_mu_); - channel_uuid_ = grpc_channel_trace_registry_register_channel_trace(this); + channel_uuid_ = ChannelzRegistry::Register(this); time_created_ = grpc_millis_to_timespec(grpc_core::ExecCtx::Get()->Now(), GPR_CLOCK_REALTIME); } @@ -83,7 +83,7 @@ ChannelTrace::~ChannelTrace() { it = it->next(); Delete<TraceEvent>(to_free); } - grpc_channel_trace_registry_unregister_channel_trace(channel_uuid_); + ChannelzRegistry::Unregister(channel_uuid_); gpr_mu_destroy(&tracer_mu_); } diff --git a/src/core/lib/channel/channel_trace_registry.cc b/src/core/lib/channel/channel_trace_registry.cc deleted file mode 100644 index 6c82431467..0000000000 --- a/src/core/lib/channel/channel_trace_registry.cc +++ /dev/null @@ -1,80 +0,0 @@ -/* - * - * Copyright 2017 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 <grpc/impl/codegen/port_platform.h> - -#include "src/core/lib/avl/avl.h" -#include "src/core/lib/channel/channel_trace.h" -#include "src/core/lib/channel/channel_trace_registry.h" -#include "src/core/lib/gpr/useful.h" - -#include <grpc/support/alloc.h> -#include <grpc/support/log.h> - -// file global lock and avl. -static gpr_mu g_mu; -static grpc_avl g_avl; -static gpr_atm g_uuid = 0; - -// avl vtable for uuid (intptr_t) -> ChannelTrace -// this table is only looking, it does not own anything. -static void destroy_intptr(void* not_used, void* user_data) {} -static void* copy_intptr(void* key, void* user_data) { return key; } -static long compare_intptr(void* key1, void* key2, void* user_data) { - return GPR_ICMP(key1, key2); -} - -static void destroy_channel_trace(void* trace, void* user_data) {} -static void* copy_channel_trace(void* trace, void* user_data) { return trace; } -static const grpc_avl_vtable avl_vtable = { - destroy_intptr, copy_intptr, compare_intptr, destroy_channel_trace, - copy_channel_trace}; - -void grpc_channel_trace_registry_init() { - gpr_mu_init(&g_mu); - g_avl = grpc_avl_create(&avl_vtable); -} - -void grpc_channel_trace_registry_shutdown() { - grpc_avl_unref(g_avl, nullptr); - gpr_mu_destroy(&g_mu); -} - -intptr_t grpc_channel_trace_registry_register_channel_trace( - grpc_core::ChannelTrace* channel_trace) { - intptr_t prior = gpr_atm_no_barrier_fetch_add(&g_uuid, 1); - gpr_mu_lock(&g_mu); - g_avl = grpc_avl_add(g_avl, (void*)prior, channel_trace, nullptr); - gpr_mu_unlock(&g_mu); - return prior; -} - -void grpc_channel_trace_registry_unregister_channel_trace(intptr_t uuid) { - gpr_mu_lock(&g_mu); - g_avl = grpc_avl_remove(g_avl, (void*)uuid, nullptr); - gpr_mu_unlock(&g_mu); -} - -grpc_core::ChannelTrace* grpc_channel_trace_registry_get_channel_trace( - intptr_t uuid) { - gpr_mu_lock(&g_mu); - grpc_core::ChannelTrace* ret = static_cast<grpc_core::ChannelTrace*>( - grpc_avl_get(g_avl, (void*)uuid, nullptr)); - gpr_mu_unlock(&g_mu); - return ret; -} diff --git a/src/core/lib/channel/channel_trace_registry.h b/src/core/lib/channel/channel_trace_registry.h deleted file mode 100644 index 391ecba7de..0000000000 --- a/src/core/lib/channel/channel_trace_registry.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * Copyright 2017 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_CORE_LIB_CHANNEL_CHANNEL_TRACE_REGISTRY_H -#define GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_REGISTRY_H - -#include <grpc/impl/codegen/port_platform.h> - -#include "src/core/lib/channel/channel_trace.h" - -#include <stdint.h> - -// TODO(ncteisen): convert this file to C++ - -void grpc_channel_trace_registry_init(); -void grpc_channel_trace_registry_shutdown(); - -// globally registers a ChannelTrace. Returns its unique uuid -intptr_t grpc_channel_trace_registry_register_channel_trace( - grpc_core::ChannelTrace* channel_trace); -// globally unregisters the ChannelTrace that is associated to uuid. -void grpc_channel_trace_registry_unregister_channel_trace(intptr_t uuid); -// if object with uuid has previously been registered, returns the ChannelTrace -// associated with that uuid. Else returns nullptr. -grpc_core::ChannelTrace* grpc_channel_trace_registry_get_channel_trace( - intptr_t uuid); - -#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_REGISTRY_H */ diff --git a/src/core/lib/channel/channelz_registry.cc b/src/core/lib/channel/channelz_registry.cc new file mode 100644 index 0000000000..31d66e847a --- /dev/null +++ b/src/core/lib/channel/channelz_registry.cc @@ -0,0 +1,77 @@ +/* + * + * Copyright 2017 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 <grpc/impl/codegen/port_platform.h> + +#include "src/core/lib/channel/channel_trace.h" +#include "src/core/lib/channel/channelz_registry.h" +#include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/memory.h" + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> + +namespace grpc_core { +namespace { + +// singleton instance of the registry. +ChannelzRegistry* g_channelz_registry = nullptr; + +// avl vtable for uuid (intptr_t) -> channelz_obj (void*) +// this table is only looking, it does not own anything. +void destroy_intptr(void* not_used, void* user_data) {} +void* copy_intptr(void* key, void* user_data) { return key; } +long compare_intptr(void* key1, void* key2, void* user_data) { + return GPR_ICMP(key1, key2); +} + +void destroy_channelz_obj(void* channelz_obj, void* user_data) {} +void* copy_channelz_obj(void* channelz_obj, void* user_data) { + return channelz_obj; +} +const grpc_avl_vtable avl_vtable = {destroy_intptr, copy_intptr, compare_intptr, + destroy_channelz_obj, copy_channelz_obj}; + +} // anonymous namespace + +void ChannelzRegistry::Init() { g_channelz_registry = New<ChannelzRegistry>(); } + +void ChannelzRegistry::Shutdown() { Delete(g_channelz_registry); } + +ChannelzRegistry* ChannelzRegistry::Default() { + GPR_DEBUG_ASSERT(g_channelz_registry != nullptr); + return g_channelz_registry; +} + +ChannelzRegistry::ChannelzRegistry() : uuid_(1) { + gpr_mu_init(&mu_); + avl_ = grpc_avl_create(&avl_vtable); +} + +ChannelzRegistry::~ChannelzRegistry() { + grpc_avl_unref(avl_, nullptr); + gpr_mu_destroy(&mu_); +} + +void ChannelzRegistry::InternalUnregister(intptr_t uuid) { + gpr_mu_lock(&mu_); + avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr); + gpr_mu_unlock(&mu_); +} + +} // namespace grpc_core diff --git a/src/core/lib/channel/channelz_registry.h b/src/core/lib/channel/channelz_registry.h new file mode 100644 index 0000000000..4de7d478c5 --- /dev/null +++ b/src/core/lib/channel/channelz_registry.h @@ -0,0 +1,99 @@ +/* + * + * Copyright 2017 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_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H +#define GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H + +#include <grpc/impl/codegen/port_platform.h> + +#include "src/core/lib/avl/avl.h" +#include "src/core/lib/channel/channel_trace.h" + +#include <stdint.h> + +namespace grpc_core { + +// singleton registry object to track all objects that are needed to support +// channelz bookkeeping. All objects share globally distributed uuids. +class ChannelzRegistry { + public: + // To be called in grpc_init() + static void Init(); + + // To be callen in grpc_shutdown(); + static void Shutdown(); + + // globally registers a channelz Object. Returns its unique uuid + template <typename Object> + static intptr_t Register(Object* object) { + return Default()->InternalRegister(object); + } + + // globally unregisters the object that is associated to uuid. + static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); } + + // if object with uuid has previously been registered, returns the + // Object associated with that uuid. Else returns nullptr. + template <typename Object> + static Object* Get(intptr_t uuid) { + return Default()->InternalGet<Object>(uuid); + } + + private: + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE + + ChannelzRegistry(); + ~ChannelzRegistry(); + + // Returned the singleton instance of ChannelzRegistry; + static ChannelzRegistry* Default(); + + // globally registers a channelz Object. Returns its unique uuid + template <typename Object> + intptr_t InternalRegister(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 InternalUnregister(intptr_t uuid); + + // if object with uuid has previously been registered, returns the + // Object associated with that uuid. Else returns nullptr. + template <typename Object> + Object* InternalGet(intptr_t uuid) { + gpr_mu_lock(&mu_); + Object* ret = + static_cast<Object*>(grpc_avl_get(avl_, (void*)uuid, nullptr)); + gpr_mu_unlock(&mu_); + return ret; + } + + // private members + gpr_mu mu_; + grpc_avl avl_; + gpr_atm uuid_; +}; + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */ |