GRPC Core  9.0.0
channelz_registry.h
Go to the documentation of this file.
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 
23 
24 #include <stdint.h>
25 
28 #include "src/core/lib/gprpp/map.h"
30 
31 namespace grpc_core {
32 namespace channelz {
33 
34 // singleton registry object to track all objects that are needed to support
35 // channelz bookkeeping. All objects share globally distributed uuids.
37  public:
38  // To be called in grpc_init()
39  static void Init();
40 
41  // To be called in grpc_shutdown();
42  static void Shutdown();
43 
44  static void Register(BaseNode* node) {
45  return Default()->InternalRegister(node);
46  }
47  static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); }
48  static RefCountedPtr<BaseNode> Get(intptr_t uuid) {
49  return Default()->InternalGet(uuid);
50  }
51 
52  // Returns the allocated JSON string that represents the proto
53  // GetTopChannelsResponse as per channelz.proto.
54  static char* GetTopChannels(intptr_t start_channel_id) {
55  return Default()->InternalGetTopChannels(start_channel_id);
56  }
57 
58  // Returns the allocated JSON string that represents the proto
59  // GetServersResponse as per channelz.proto.
60  static char* GetServers(intptr_t start_server_id) {
61  return Default()->InternalGetServers(start_server_id);
62  }
63 
64  // Test only helper function to dump the JSON representation to std out.
65  // This can aid in debugging channelz code.
66  static void LogAllEntities() { Default()->InternalLogAllEntities(); }
67 
68  private:
69  // Returned the singleton instance of ChannelzRegistry;
70  static ChannelzRegistry* Default();
71 
72  // globally registers an Entry. Returns its unique uuid
73  void InternalRegister(BaseNode* node);
74 
75  // globally unregisters the object that is associated to uuid. Also does
76  // sanity check that an object doesn't try to unregister the wrong type.
77  void InternalUnregister(intptr_t uuid);
78 
79  // if object with uuid has previously been registered as the correct type,
80  // returns the void* associated with that uuid. Else returns nullptr.
81  RefCountedPtr<BaseNode> InternalGet(intptr_t uuid);
82 
83  char* InternalGetTopChannels(intptr_t start_channel_id);
84  char* InternalGetServers(intptr_t start_server_id);
85 
86  void InternalLogAllEntities();
87 
88  // protects members
89  Mutex mu_;
90  std::map<intptr_t, BaseNode*> node_map_;
91  intptr_t uuid_generator_ = 0;
92 };
93 
94 } // namespace channelz
95 } // namespace grpc_core
96 
97 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_REGISTRY_H */
static void Unregister(intptr_t uuid)
Definition: channelz_registry.h:47
static void Shutdown()
Definition: channelz_registry.cc:48
Round Robin Policy.
Definition: backend_metric.cc:24
Definition: channelz.h:74
Definition: channelz_registry.h:36
static void Init()
Definition: channelz_registry.cc:46
Definition: ref_counted_ptr.h:35
static char * GetServers(intptr_t start_server_id)
Definition: channelz_registry.h:60
static char * GetTopChannels(intptr_t start_channel_id)
Definition: channelz_registry.h:54
static RefCountedPtr< BaseNode > Get(intptr_t uuid)
Definition: channelz_registry.h:48
static void Register(BaseNode *node)
Definition: channelz_registry.h:44
Definition: sync.h:40
static void LogAllEntities()
Definition: channelz_registry.h:66