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 | bba8840 | 2018-05-11 11:54:41 -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: |
| 35 | // These functions ensure singleton like behavior. We cannot use the normal |
| 36 | // pattern of a get functions with a static function variable due to build |
| 37 | // complications. |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 38 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 39 | // To be called in grpc_init() |
| 40 | static void Init(); |
| 41 | |
| 42 | // To be callen in grpc_shutdown(); |
| 43 | static void Shutdown(); |
| 44 | |
| 45 | // Returned the singleton instance of ChannelzRegistry; |
| 46 | static ChannelzRegistry* Default(); |
| 47 | |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame^] | 48 | // globally registers a channelz Object. Returns its unique uuid |
| 49 | template <typename Object> |
| 50 | intptr_t Register(Object* object) { |
| 51 | intptr_t prior = gpr_atm_no_barrier_fetch_add(&uuid_, 1); |
| 52 | gpr_mu_lock(&mu_); |
| 53 | avl_ = grpc_avl_add(avl_, (void*)prior, object, nullptr); |
| 54 | gpr_mu_unlock(&mu_); |
| 55 | return prior; |
| 56 | } |
| 57 | |
| 58 | // globally unregisters the object that is associated to uuid. |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 59 | void Unregister(intptr_t uuid); |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame^] | 60 | |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 61 | // if object with uuid has previously been registered, returns the |
ncteisen | 96bc382 | 2018-05-11 13:05:16 -0400 | [diff] [blame^] | 62 | // Object associated with that uuid. Else returns nullptr. |
| 63 | template <typename Object> |
| 64 | Object* Get(intptr_t uuid) { |
| 65 | gpr_mu_lock(&mu_); |
| 66 | Object* ret = |
| 67 | static_cast<Object*>(grpc_avl_get(avl_, (void*)uuid, nullptr)); |
| 68 | gpr_mu_unlock(&mu_); |
| 69 | return ret; |
| 70 | } |
ncteisen | 3be52f9 | 2018-05-11 12:34:02 -0400 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW |
| 74 | GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE |
| 75 | |
| 76 | gpr_mu mu_; |
| 77 | grpc_avl avl_; |
| 78 | gpr_atm uuid_; |
| 79 | |
| 80 | ChannelzRegistry(); |
| 81 | ~ChannelzRegistry(); |
| 82 | }; |
| 83 | |
| 84 | } // namespace grpc_core |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 85 | |
ncteisen | bba8840 | 2018-05-11 11:54:41 -0400 | [diff] [blame] | 86 | #endif /* GRPC_CORE_LIB_CHANNEL_CHANNELz_REGISTRY_H */ |