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