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 | |
| 24 | #include "src/core/lib/channel/channel_trace.h" |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 25 | #include "src/core/lib/gprpp/inlined_vector.h" |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 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 | gpr_mu_lock(&mu_); |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 71 | entities_.push_back(static_cast<void*>(object)); |
| 72 | intptr_t uuid = entities_.size(); |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 73 | gpr_mu_unlock(&mu_); |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 74 | return uuid; |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 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_); |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 85 | if (uuid < 1 || uuid > static_cast<intptr_t>(entities_.size())) { |
| 86 | gpr_mu_unlock(&mu_); |
| 87 | return nullptr; |
| 88 | } |
| 89 | Object* ret = static_cast<Object*>(entities_[uuid - 1]); |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame] | 90 | gpr_mu_unlock(&mu_); |
| 91 | return ret; |
| 92 | } |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 93 | |
ncteisen | 3a3bbaf | 2018-05-17 09:55:24 -0700 | [diff] [blame] | 94 | // private members |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 95 | |
| 96 | // protects entities_ and uuid_ |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 97 | gpr_mu mu_; |
ncteisen | aca5043 | 2018-07-10 18:50:45 -0700 | [diff] [blame] | 98 | InlinedVector<void*, 20> entities_; |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace grpc_core |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 102 | |
ncteisen | 7243c5f | 2018-05-11 16:41:31 -0400 | [diff] [blame] | 103 | #endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */ |