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