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