blob: 4de7d478c5bc6f084582b4a2d4c3ea0f6da9d5c7 [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"
26
27#include <stdint.h>
28
ncteisen3be52f92018-05-11 12:34:02 -040029namespace grpc_core {
ncteisen3b42f832018-03-19 13:22:35 -070030
ncteisen3be52f92018-05-11 12:34:02 -040031// singleton registry object to track all objects that are needed to support
32// channelz bookkeeping. All objects share globally distributed uuids.
33class ChannelzRegistry {
34 public:
ncteisen3be52f92018-05-11 12:34:02 -040035 // To be called in grpc_init()
36 static void Init();
37
38 // To be callen in grpc_shutdown();
39 static void Shutdown();
40
ncteisen3a3bbaf2018-05-17 09:55:24 -070041 // 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
ncteisen3be52f92018-05-11 12:34:02 -040064 // Returned the singleton instance of ChannelzRegistry;
65 static ChannelzRegistry* Default();
66
ncteisen96bc3822018-05-11 13:05:16 -040067 // globally registers a channelz Object. Returns its unique uuid
68 template <typename Object>
ncteisen3a3bbaf2018-05-17 09:55:24 -070069 intptr_t InternalRegister(Object* object) {
ncteisen96bc3822018-05-11 13:05:16 -040070 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.
ncteisen3a3bbaf2018-05-17 09:55:24 -070078 void InternalUnregister(intptr_t uuid);
ncteisen96bc3822018-05-11 13:05:16 -040079
ncteisen3be52f92018-05-11 12:34:02 -040080 // if object with uuid has previously been registered, returns the
ncteisen96bc3822018-05-11 13:05:16 -040081 // Object associated with that uuid. Else returns nullptr.
82 template <typename Object>
ncteisen3a3bbaf2018-05-17 09:55:24 -070083 Object* InternalGet(intptr_t uuid) {
ncteisen96bc3822018-05-11 13:05:16 -040084 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 }
ncteisen3be52f92018-05-11 12:34:02 -040090
ncteisen3a3bbaf2018-05-17 09:55:24 -070091 // private members
ncteisen3be52f92018-05-11 12:34:02 -040092 gpr_mu mu_;
93 grpc_avl avl_;
94 gpr_atm uuid_;
ncteisen3be52f92018-05-11 12:34:02 -040095};
96
97} // namespace grpc_core
ncteisen3b42f832018-03-19 13:22:35 -070098
ncteisen7243c5f2018-05-11 16:41:31 -040099#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */