ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 21 | #include "src/core/lib/channel/channel_trace.h" |
ncteisen | bba8840 | 2018-05-11 11:54:41 -0400 | [diff] [blame] | 22 | #include "src/core/lib/channel/channelz_registry.h" |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 23 | #include "src/core/lib/gpr/useful.h" |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 24 | #include "src/core/lib/gprpp/memory.h" |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 25 | |
| 26 | #include <grpc/support/alloc.h> |
| 27 | #include <grpc/support/log.h> |
| 28 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 29 | namespace grpc_core { |
| 30 | namespace { |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 31 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 32 | // singleton instance of the registry. |
| 33 | ChannelzRegistry* g_channelz_registry = nullptr; |
| 34 | |
| 35 | // avl vtable for uuid (intptr_t) -> channelz_obj (void*) |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 36 | // this table is only looking, it does not own anything. |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 37 | void destroy_intptr(void* not_used, void* user_data) {} |
| 38 | void* copy_intptr(void* key, void* user_data) { return key; } |
| 39 | long compare_intptr(void* key1, void* key2, void* user_data) { |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 40 | return GPR_ICMP(key1, key2); |
| 41 | } |
| 42 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 43 | void destroy_channelz_obj(void* channelz_obj, void* user_data) {} |
| 44 | void* copy_channelz_obj(void* channelz_obj, void* user_data) { |
| 45 | return channelz_obj; |
| 46 | } |
| 47 | const grpc_avl_vtable avl_vtable = {destroy_intptr, copy_intptr, compare_intptr, |
| 48 | destroy_channelz_obj, copy_channelz_obj}; |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 49 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 50 | } // anonymous namespace |
| 51 | |
| 52 | void ChannelzRegistry::Init() { g_channelz_registry = New<ChannelzRegistry>(); } |
| 53 | |
| 54 | void ChannelzRegistry::Shutdown() { Delete(g_channelz_registry); } |
| 55 | |
| 56 | ChannelzRegistry* ChannelzRegistry::Default() { |
| 57 | GPR_DEBUG_ASSERT(g_channelz_registry != nullptr); |
| 58 | return g_channelz_registry; |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 61 | ChannelzRegistry::ChannelzRegistry() : uuid_(1) { |
| 62 | gpr_mu_init(&mu_); |
| 63 | avl_ = grpc_avl_create(&avl_vtable); |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 64 | } |
| 65 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 66 | ChannelzRegistry::~ChannelzRegistry() { |
| 67 | grpc_avl_unref(avl_, nullptr); |
| 68 | gpr_mu_destroy(&mu_); |
| 69 | } |
| 70 | |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 71 | void ChannelzRegistry::InternalUnregister(intptr_t uuid) { |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 72 | gpr_mu_lock(&mu_); |
| 73 | avl_ = grpc_avl_remove(avl_, (void*)uuid, nullptr); |
| 74 | gpr_mu_unlock(&mu_); |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 75 | } |
| 76 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 77 | } // namespace grpc_core |