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