blob: 6182e41b080d66f5ecf11c31d5ac0e2d0f629f43 [file] [log] [blame]
ncteisen3b42f832018-03-19 13:22:35 -07001/*
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
ncteisen7243c5f2018-05-11 16:41:31 -040019#ifndef GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
20#define GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
ncteisen3b42f832018-03-19 13:22:35 -070021
22#include <grpc/impl/codegen/port_platform.h>
23
ncteisen3be52f92018-05-11 12:34:02 -040024#include "src/core/lib/avl/avl.h"
ncteisen3b42f832018-03-19 13:22:35 -070025#include "src/core/lib/channel/channel_trace.h"
ncteisenaca50432018-07-10 18:50:45 -070026#include "src/core/lib/gprpp/inlined_vector.h"
ncteisen3b42f832018-03-19 13:22:35 -070027
28#include <stdint.h>
29
ncteisen3be52f92018-05-11 12:34:02 -040030namespace grpc_core {
ncteisen3b42f832018-03-19 13:22:35 -070031
ncteisen3be52f92018-05-11 12:34:02 -040032// singleton registry object to track all objects that are needed to support
33// channelz bookkeeping. All objects share globally distributed uuids.
34class ChannelzRegistry {
35 public:
ncteisen3be52f92018-05-11 12:34:02 -040036 // To be called in grpc_init()
37 static void Init();
38
39 // To be callen in grpc_shutdown();
40 static void Shutdown();
41
ncteisen3a3bbaf2018-05-17 09:55:24 -070042 // 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
ncteisen3be52f92018-05-11 12:34:02 -040065 // Returned the singleton instance of ChannelzRegistry;
66 static ChannelzRegistry* Default();
67
ncteisen96bc3822018-05-11 13:05:16 -040068 // globally registers a channelz Object. Returns its unique uuid
69 template <typename Object>
ncteisen3a3bbaf2018-05-17 09:55:24 -070070 intptr_t InternalRegister(Object* object) {
ncteisen96bc3822018-05-11 13:05:16 -040071 gpr_mu_lock(&mu_);
ncteisenaca50432018-07-10 18:50:45 -070072 entities_.push_back(static_cast<void*>(object));
73 intptr_t uuid = entities_.size();
ncteisen96bc3822018-05-11 13:05:16 -040074 gpr_mu_unlock(&mu_);
ncteisenaca50432018-07-10 18:50:45 -070075 return uuid;
ncteisen96bc3822018-05-11 13:05:16 -040076 }
77
78 // globally unregisters the object that is associated to uuid.
ncteisen3a3bbaf2018-05-17 09:55:24 -070079 void InternalUnregister(intptr_t uuid);
ncteisen96bc3822018-05-11 13:05:16 -040080
ncteisen3be52f92018-05-11 12:34:02 -040081 // if object with uuid has previously been registered, returns the
ncteisen96bc3822018-05-11 13:05:16 -040082 // Object associated with that uuid. Else returns nullptr.
83 template <typename Object>
ncteisen3a3bbaf2018-05-17 09:55:24 -070084 Object* InternalGet(intptr_t uuid) {
ncteisen96bc3822018-05-11 13:05:16 -040085 gpr_mu_lock(&mu_);
ncteisenaca50432018-07-10 18:50:45 -070086 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]);
ncteisen96bc3822018-05-11 13:05:16 -040091 gpr_mu_unlock(&mu_);
92 return ret;
93 }
ncteisen3be52f92018-05-11 12:34:02 -040094
ncteisen3a3bbaf2018-05-17 09:55:24 -070095 // private members
ncteisenaca50432018-07-10 18:50:45 -070096
97 // protects entities_ and uuid_
ncteisen3be52f92018-05-11 12:34:02 -040098 gpr_mu mu_;
ncteisenaca50432018-07-10 18:50:45 -070099 InlinedVector<void*, 20> entities_;
ncteisen3be52f92018-05-11 12:34:02 -0400100};
101
102} // namespace grpc_core
ncteisen3b42f832018-03-19 13:22:35 -0700103
ncteisen7243c5f2018-05-11 16:41:31 -0400104#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */