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 | |
ncteisen | 7243c5f | 2018-05-11 16:41:31 -0400 | [diff] [blame] | 19 | #ifndef GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H |
| 20 | #define GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 21 | |
| 22 | #include <grpc/impl/codegen/port_platform.h> |
| 23 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 24 | #include "src/core/lib/avl/avl.h" |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 25 | #include "src/core/lib/channel/channel_trace.h" |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 29 | namespace grpc_core { |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 30 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 31 | // singleton registry object to track all objects that are needed to support |
| 32 | // channelz bookkeeping. All objects share globally distributed uuids. |
| 33 | class ChannelzRegistry { |
| 34 | public: |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 35 | // To be called in grpc_init() |
| 36 | static void Init(); |
| 37 | |
| 38 | // To be callen in grpc_shutdown(); |
| 39 | static void Shutdown(); |
| 40 | |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 41 | // globally registers a channelz Object. Returns its unique uuid |
| 42 | template <typename Object> |
| 43 | static intptr_t Register(Object* object) { |
| 44 | return Default()->InternalRegister(object); |
| 45 | } |
| 46 | |
| 47 | // globally unregisters the object that is associated to uuid. |
| 48 | static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); } |
| 49 | |
| 50 | // if object with uuid has previously been registered, returns the |
| 51 | // Object associated with that uuid. Else returns nullptr. |
| 52 | template <typename Object> |
| 53 | static Object* Get(intptr_t uuid) { |
| 54 | return Default()->InternalGet<Object>(uuid); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW |
| 59 | GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE |
| 60 | |
| 61 | ChannelzRegistry(); |
| 62 | ~ChannelzRegistry(); |
| 63 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 64 | // Returned the singleton instance of ChannelzRegistry; |
| 65 | static ChannelzRegistry* Default(); |
| 66 | |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 67 | // globally registers a channelz Object. Returns its unique uuid |
| 68 | template <typename Object> |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 69 | intptr_t InternalRegister(Object* object) { |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 70 | intptr_t prior = gpr_atm_no_barrier_fetch_add(&uuid_, 1); |
| 71 | gpr_mu_lock(&mu_); |
| 72 | avl_ = grpc_avl_add(avl_, (void*)prior, object, nullptr); |
| 73 | gpr_mu_unlock(&mu_); |
| 74 | return prior; |
| 75 | } |
| 76 | |
| 77 | // globally unregisters the object that is associated to uuid. |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 78 | void InternalUnregister(intptr_t uuid); |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 79 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 80 | // if object with uuid has previously been registered, returns the |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 81 | // Object associated with that uuid. Else returns nullptr. |
| 82 | template <typename Object> |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 83 | Object* InternalGet(intptr_t uuid) { |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 84 | gpr_mu_lock(&mu_); |
| 85 | Object* ret = |
| 86 | static_cast<Object*>(grpc_avl_get(avl_, (void*)uuid, nullptr)); |
| 87 | gpr_mu_unlock(&mu_); |
| 88 | return ret; |
| 89 | } |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 90 | |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 91 | // private members |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 92 | gpr_mu mu_; |
| 93 | grpc_avl avl_; |
| 94 | gpr_atm uuid_; |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace grpc_core |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 98 | |
ncteisen | 7243c5f | 2018-05-11 16:41:31 -0400 | [diff] [blame] | 99 | #endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */ |