• 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_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
20 #define GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H
21 
22 #include <grpc/impl/codegen/port_platform.h>
23 
24 #include "src/core/lib/channel/channel_trace.h"
25 #include "src/core/lib/channel/channelz.h"
26 #include "src/core/lib/gprpp/inlined_vector.h"
27 
28 #include <stdint.h>
29 
30 namespace grpc_core {
31 namespace channelz {
32 
33 // singleton registry object to track all objects that are needed to support
34 // channelz bookkeeping. All objects share globally distributed uuids.
35 class ChannelzRegistry {
36  public:
37   // To be called in grpc_init()
38   static void Init();
39 
40   // To be called in grpc_shutdown();
41   static void Shutdown();
42 
Register(BaseNode * node)43   static intptr_t Register(BaseNode* node) {
44     return Default()->InternalRegister(node);
45   }
Unregister(intptr_t uuid)46   static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); }
Get(intptr_t uuid)47   static BaseNode* Get(intptr_t uuid) { return Default()->InternalGet(uuid); }
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 char* 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 char* GetServers(intptr_t start_server_id) {
58     return Default()->InternalGetServers(start_server_id);
59   }
60 
61  private:
62   GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
63   GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
64 
65   ChannelzRegistry();
66   ~ChannelzRegistry();
67 
68   // Returned the singleton instance of ChannelzRegistry;
69   static ChannelzRegistry* Default();
70 
71   // globally registers an Entry. Returns its unique uuid
72   intptr_t InternalRegister(BaseNode* node);
73 
74   // globally unregisters the object that is associated to uuid. Also does
75   // sanity check that an object doesn't try to unregister the wrong type.
76   void InternalUnregister(intptr_t uuid);
77 
78   // if object with uuid has previously been registered as the correct type,
79   // returns the void* associated with that uuid. Else returns nullptr.
80   BaseNode* InternalGet(intptr_t uuid);
81 
82   char* InternalGetTopChannels(intptr_t start_channel_id);
83   char* InternalGetServers(intptr_t start_server_id);
84 
85   // protects entities_ and uuid_
86   gpr_mu mu_;
87   InlinedVector<BaseNode*, 20> entities_;
88 };
89 
90 }  // namespace channelz
91 }  // namespace grpc_core
92 
93 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */
94