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 "src/core/lib/channel/channel_trace.h" 27 #include "src/core/lib/channel/channelz.h" 28 #include "src/core/lib/gprpp/map.h" 29 #include "src/core/lib/gprpp/sync.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. 36 class ChannelzRegistry { 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 Register(BaseNode * node)44 static void Register(BaseNode* node) { 45 return Default()->InternalRegister(node); 46 } Unregister(intptr_t uuid)47 static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); } Get(intptr_t 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. GetTopChannels(intptr_t start_channel_id)54 static std::string 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. GetServers(intptr_t start_server_id)60 static std::string 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. LogAllEntities()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 std::string InternalGetTopChannels(intptr_t start_channel_id); 84 std::string 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 */ 98