aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar ncteisen <ncteisen@gmail.com>2018-07-13 12:09:55 -0700
committerGravatar ncteisen <ncteisen@gmail.com>2018-07-15 22:25:17 -0700
commit9a6c722e301be70715fa4f16cea22d172d605615 (patch)
tree48cf910926b9b8786cbb8d2f04e94bae165adb41 /src/core
parent4f840eafecfed75aeee50ff850eaa79094cde248 (diff)
Rewrite registry to know type
Diffstat (limited to 'src/core')
-rw-r--r--src/core/lib/channel/channelz.cc4
-rw-r--r--src/core/lib/channel/channelz_registry.cc30
-rw-r--r--src/core/lib/channel/channelz_registry.h72
-rw-r--r--src/core/lib/surface/init.cc4
4 files changed, 62 insertions, 48 deletions
diff --git a/src/core/lib/channel/channelz.cc b/src/core/lib/channel/channelz.cc
index 2074cb0cc5..923b8043cd 100644
--- a/src/core/lib/channel/channelz.cc
+++ b/src/core/lib/channel/channelz.cc
@@ -92,14 +92,14 @@ ChannelNode::ChannelNode(grpc_channel* channel, size_t channel_tracer_max_nodes)
: channel_(channel), target_(nullptr), channel_uuid_(-1) {
trace_.Init(channel_tracer_max_nodes);
target_ = UniquePtr<char>(grpc_channel_get_target(channel_));
- channel_uuid_ = ChannelzRegistry::Register(this);
+ channel_uuid_ = ChannelzRegistry::RegisterChannelNode(this);
gpr_atm_no_barrier_store(&last_call_started_millis_,
(gpr_atm)ExecCtx::Get()->Now());
}
ChannelNode::~ChannelNode() {
trace_.Destroy();
- ChannelzRegistry::Unregister(channel_uuid_);
+ ChannelzRegistry::UnregisterChannelNode(channel_uuid_);
}
void ChannelNode::RecordCallStarted() {
diff --git a/src/core/lib/channel/channelz_registry.cc b/src/core/lib/channel/channelz_registry.cc
index 023ede552a..43cace1975 100644
--- a/src/core/lib/channel/channelz_registry.cc
+++ b/src/core/lib/channel/channelz_registry.cc
@@ -25,10 +25,12 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
+#include <grpc/support/sync.h>
#include <cstring>
namespace grpc_core {
+namespace channelz {
namespace {
// singleton instance of the registry.
@@ -49,12 +51,32 @@ ChannelzRegistry::ChannelzRegistry() { gpr_mu_init(&mu_); }
ChannelzRegistry::~ChannelzRegistry() { gpr_mu_destroy(&mu_); }
-void ChannelzRegistry::InternalUnregister(intptr_t uuid) {
+intptr_t ChannelzRegistry::InternalRegisterEntry(const RegistryEntry& entry) {
+ mu_guard guard(&mu_);
+ entities_.push_back(entry);
+ intptr_t uuid = entities_.size();
+ return uuid;
+}
+
+void ChannelzRegistry::InternalUnregisterEntry(intptr_t uuid, EntityType type) {
GPR_ASSERT(uuid >= 1);
- gpr_mu_lock(&mu_);
+ mu_guard guard(&mu_);
GPR_ASSERT(static_cast<size_t>(uuid) <= entities_.size());
- entities_[uuid - 1] = nullptr;
- gpr_mu_unlock(&mu_);
+ GPR_ASSERT(entities_[uuid - 1].type == type);
+ entities_[uuid - 1].object = nullptr;
+}
+
+void* ChannelzRegistry::InternalGetEntry(intptr_t uuid, EntityType type) {
+ mu_guard guard(&mu_);
+ if (uuid < 1 || uuid > static_cast<intptr_t>(entities_.size())) {
+ return nullptr;
+ }
+ if (entities_[uuid - 1].type == type) {
+ return entities_[uuid - 1].object;
+ } else {
+ return nullptr;
+ }
}
+} // namespace channelz
} // namespace grpc_core
diff --git a/src/core/lib/channel/channelz_registry.h b/src/core/lib/channel/channelz_registry.h
index a5a187a054..cc069d9145 100644
--- a/src/core/lib/channel/channelz_registry.h
+++ b/src/core/lib/channel/channelz_registry.h
@@ -27,6 +27,7 @@
#include <stdint.h>
namespace grpc_core {
+namespace channelz {
// singleton registry object to track all objects that are needed to support
// channelz bookkeeping. All objects share globally distributed uuids.
@@ -38,23 +39,31 @@ class ChannelzRegistry {
// 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);
+ static intptr_t RegisterChannelNode(ChannelNode* channel_node) {
+ RegistryEntry entry(channel_node, EntityType::kChannelNode);
+ return Default()->InternalRegisterEntry(entry);
}
-
- // 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);
+ static void UnregisterChannelNode(intptr_t uuid) {
+ Default()->InternalUnregisterEntry(uuid, EntityType::kChannelNode);
+ }
+ static ChannelNode* GetChannelNode(intptr_t uuid) {
+ void* gotten = Default()->InternalGetEntry(uuid, EntityType::kChannelNode);
+ return gotten == nullptr ? nullptr : static_cast<ChannelNode*>(gotten);
}
private:
+ enum class EntityType {
+ kChannelNode,
+ kUnset,
+ };
+
+ struct RegistryEntry {
+ RegistryEntry(void* object_in, EntityType type_in)
+ : object(object_in), type(type_in) {}
+ void* object;
+ EntityType type;
+ };
+
GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
@@ -64,40 +73,23 @@ class 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) {
- gpr_mu_lock(&mu_);
- entities_.push_back(static_cast<void*>(object));
- intptr_t uuid = entities_.size();
- gpr_mu_unlock(&mu_);
- return uuid;
- }
+ // globally registers an Entry. Returns its unique uuid
+ intptr_t InternalRegisterEntry(const RegistryEntry& entry);
- // 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_);
- if (uuid < 1 || uuid > static_cast<intptr_t>(entities_.size())) {
- gpr_mu_unlock(&mu_);
- return nullptr;
- }
- Object* ret = static_cast<Object*>(entities_[uuid - 1]);
- gpr_mu_unlock(&mu_);
- return ret;
- }
+ // globally unregisters the object that is associated to uuid. Also does
+ // sanity check that an object doesn't try to unregister the wrong type.
+ void InternalUnregisterEntry(intptr_t uuid, EntityType type);
- // private members
+ // if object with uuid has previously been registered as the correct type,
+ // returns the void* associated with that uuid. Else returns nullptr.
+ void* InternalGetEntry(intptr_t uuid, EntityType type);
// protects entities_ and uuid_
gpr_mu mu_;
- InlinedVector<void*, 20> entities_;
+ InlinedVector<RegistryEntry, 20> entities_;
};
+} // namespace channelz
} // namespace grpc_core
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */
diff --git a/src/core/lib/surface/init.cc b/src/core/lib/surface/init.cc
index 16be81e9c2..0ad82fed99 100644
--- a/src/core/lib/surface/init.cc
+++ b/src/core/lib/surface/init.cc
@@ -127,7 +127,7 @@ void grpc_init(void) {
grpc_slice_intern_init();
grpc_mdctx_global_init();
grpc_channel_init_init();
- grpc_core::ChannelzRegistry::Init();
+ grpc_core::channelz::ChannelzRegistry::Init();
grpc_security_pre_init();
grpc_core::ExecCtx::GlobalInit();
grpc_iomgr_init();
@@ -176,7 +176,7 @@ void grpc_shutdown(void) {
grpc_mdctx_global_shutdown();
grpc_handshaker_factory_registry_shutdown();
grpc_slice_intern_shutdown();
- grpc_core::ChannelzRegistry::Shutdown();
+ grpc_core::channelz::ChannelzRegistry::Shutdown();
grpc_stats_shutdown();
grpc_core::Fork::GlobalShutdown();
}