blob: 57fffa8b5a556617ef26fb22b3134f1bfa696b88 [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
ncteisenbba88402018-05-11 11:54:41 -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:
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.
ncteisen3b42f832018-03-19 13:22:35 -070038
ncteisen3be52f92018-05-11 12:34:02 -040039 // 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
ncteisen96bc3822018-05-11 13:05:16 -040048 // 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.
ncteisen3be52f92018-05-11 12:34:02 -040059 void Unregister(intptr_t uuid);
ncteisen96bc3822018-05-11 13:05:16 -040060
ncteisen3be52f92018-05-11 12:34:02 -040061 // if object with uuid has previously been registered, returns the
ncteisen96bc3822018-05-11 13:05:16 -040062 // 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 }
ncteisen3be52f92018-05-11 12:34:02 -040071
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
ncteisen3b42f832018-03-19 13:22:35 -070085
ncteisenbba88402018-05-11 11:54:41 -040086#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELz_REGISTRY_H */