• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #ifndef GRPC_SRC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
20 #define GRPC_SRC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <cstdint>
25 #include <map>
26 #include <string>
27 
28 #include "absl/base/thread_annotations.h"
29 
30 #include "src/core/lib/channel/channelz.h"
31 #include "src/core/lib/gprpp/ref_counted_ptr.h"
32 #include "src/core/lib/gprpp/sync.h"
33 
34 namespace grpc_core {
35 namespace channelz {
36 
37 // singleton registry object to track all objects that are needed to support
38 // channelz bookkeeping. All objects share globally distributed uuids.
39 class ChannelzRegistry final {
40  public:
Register(BaseNode * node)41   static void Register(BaseNode* node) {
42     return Default()->InternalRegister(node);
43   }
Unregister(intptr_t uuid)44   static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); }
Get(intptr_t uuid)45   static RefCountedPtr<BaseNode> Get(intptr_t uuid) {
46     return Default()->InternalGet(uuid);
47   }
48 
49   // Returns the allocated JSON string that represents the proto
50   // GetTopChannelsResponse as per channelz.proto.
GetTopChannels(intptr_t start_channel_id)51   static std::string GetTopChannels(intptr_t start_channel_id) {
52     return Default()->InternalGetTopChannels(start_channel_id);
53   }
54 
55   // Returns the allocated JSON string that represents the proto
56   // GetServersResponse as per channelz.proto.
GetServers(intptr_t start_server_id)57   static std::string GetServers(intptr_t start_server_id) {
58     return Default()->InternalGetServers(start_server_id);
59   }
60 
61   // Test only helper function to dump the JSON representation to std out.
62   // This can aid in debugging channelz code.
LogAllEntities()63   static void LogAllEntities() { Default()->InternalLogAllEntities(); }
64 
65   // Test only helper function to reset to initial state.
TestOnlyReset()66   static void TestOnlyReset() {
67     auto* p = Default();
68     MutexLock lock(&p->mu_);
69     p->node_map_.clear();
70     p->uuid_generator_ = 0;
71   }
72 
73  private:
74   // Returned the singleton instance of ChannelzRegistry;
75   static ChannelzRegistry* Default();
76 
77   // globally registers an Entry. Returns its unique uuid
78   void InternalRegister(BaseNode* node);
79 
80   // globally unregisters the object that is associated to uuid. Also does
81   // sanity check that an object doesn't try to unregister the wrong type.
82   void InternalUnregister(intptr_t uuid);
83 
84   // if object with uuid has previously been registered as the correct type,
85   // returns the void* associated with that uuid. Else returns nullptr.
86   RefCountedPtr<BaseNode> InternalGet(intptr_t uuid);
87 
88   std::string InternalGetTopChannels(intptr_t start_channel_id);
89   std::string InternalGetServers(intptr_t start_server_id);
90 
91   void InternalLogAllEntities();
92 
93   // protects members
94   Mutex mu_;
95   std::map<intptr_t, BaseNode*> node_map_ ABSL_GUARDED_BY(mu_);
96   intptr_t uuid_generator_ ABSL_GUARDED_BY(mu_) = 0;
97 };
98 
99 }  // namespace channelz
100 }  // namespace grpc_core
101 
102 #endif  // GRPC_SRC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
103