blob: 31d66e847a7e555ab5b6b502369a9cf25980ddb9 [file] [log] [blame]
ncteisen3b42f832018-03-19 13:22:35 -07001/*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19#include <grpc/impl/codegen/port_platform.h>
20
ncteisen3b42f832018-03-19 13:22:35 -070021#include "src/core/lib/channel/channel_trace.h"
ncteisenbba88402018-05-11 11:54:41 -040022#include "src/core/lib/channel/channelz_registry.h"
ncteisen3b42f832018-03-19 13:22:35 -070023#include "src/core/lib/gpr/useful.h"
ncteisen3be52f92018-05-11 12:34:02 -040024#include "src/core/lib/gprpp/memory.h"
ncteisen3b42f832018-03-19 13:22:35 -070025
26#include <grpc/support/alloc.h>
27#include <grpc/support/log.h>
28
ncteisen3be52f92018-05-11 12:34:02 -040029namespace grpc_core {
30namespace {
ncteisen3b42f832018-03-19 13:22:35 -070031
ncteisen3be52f92018-05-11 12:34:02 -040032// singleton instance of the registry.
33ChannelzRegistry* g_channelz_registry = nullptr;
34
35// avl vtable for uuid (intptr_t) -> channelz_obj (void*)
ncteisen3b42f832018-03-19 13:22:35 -070036// this table is only looking, it does not own anything.
ncteisen3be52f92018-05-11 12:34:02 -040037void destroy_intptr(void* not_used, void* user_data) {}
38void* copy_intptr(void* key, void* user_data) { return key; }
39long compare_intptr(void* key1, void* key2, void* user_data) {
ncteisen3b42f832018-03-19 13:22:35 -070040 return GPR_ICMP(key1, key2);
41}
42
ncteisen3be52f92018-05-11 12:34:02 -040043void destroy_channelz_obj(void* channelz_obj, void* user_data) {}
44void* copy_channelz_obj(void* channelz_obj, void* user_data) {
45 return channelz_obj;
46}
47const grpc_avl_vtable avl_vtable = {destroy_intptr, copy_intptr, compare_intptr,
48 destroy_channelz_obj, copy_channelz_obj};
ncteisen3b42f832018-03-19 13:22:35 -070049
ncteisen3be52f92018-05-11 12:34:02 -040050} // anonymous namespace
51
52void ChannelzRegistry::Init() { g_channelz_registry = New<ChannelzRegistry>(); }
53
54void ChannelzRegistry::Shutdown() { Delete(g_channelz_registry); }
55
56ChannelzRegistry* ChannelzRegistry::Default() {
57 GPR_DEBUG_ASSERT(g_channelz_registry != nullptr);
58 return g_channelz_registry;
ncteisen3b42f832018-03-19 13:22:35 -070059}
60
ncteisen3be52f92018-05-11 12:34:02 -040061ChannelzRegistry::ChannelzRegistry() : uuid_(1) {
62 gpr_mu_init(&mu_);
63 avl_ = grpc_avl_create(&avl_vtable);
ncteisen3b42f832018-03-19 13:22:35 -070064}
65
ncteisen3be52f92018-05-11 12:34:02 -040066ChannelzRegistry::~ChannelzRegistry() {
67 grpc_avl_unref(avl_, nullptr);
68 gpr_mu_destroy(&mu_);
69}
70
ncteisen3a3bbaf2018-05-17 09:55:24 -070071void ChannelzRegistry::InternalUnregister(intptr_t uuid) {
ncteisen3be52f92018-05-11 12:34:02 -040072 gpr_mu_lock(&mu_);
73 avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr);
74 gpr_mu_unlock(&mu_);
ncteisen3b42f832018-03-19 13:22:35 -070075}
76
ncteisen3be52f92018-05-11 12:34:02 -040077} // namespace grpc_core