| 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 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/channel/channelz_registry.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <cstdint> |
| 25 |
#include <cstring> |
| 26 |
#include <utility> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include <grpc/grpc.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
#include <grpc/support/string_util.h> |
| 32 |
|
| 33 |
#include "src/core/lib/channel/channelz.h" |
| 34 |
#include "src/core/lib/gprpp/sync.h" |
| 35 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 36 |
#include "src/core/lib/json/json.h" |
| 37 |
|
| 38 |
namespace grpc_core { |
| 39 |
namespace channelz { |
| 40 |
namespace { |
| 41 |
|
| 42 |
const int kPaginationLimit = 100; |
| 43 |
|
| 44 |
} // anonymous namespace |
| 45 |
|
| 46 |
ChannelzRegistry* ChannelzRegistry::Default() { |
| 47 |
static ChannelzRegistry* singleton = new ChannelzRegistry(); |
| 48 |
return singleton; |
| 49 |
} |
| 50 |
|
| 51 |
void ChannelzRegistry::InternalRegister(BaseNode* node) { |
| 52 |
MutexLock lock(&mu_); |
| 53 |
node->uuid_ = ++uuid_generator_; |
| 54 |
node_map_[node->uuid_] = node; |
| 55 |
} |
| 56 |
|
| 57 |
void ChannelzRegistry::InternalUnregister(intptr_t uuid) { |
| 58 |
GPR_ASSERT(uuid >= 1); |
| 59 |
MutexLock lock(&mu_); |
| 60 |
GPR_ASSERT(uuid <= uuid_generator_); |
| 61 |
node_map_.erase(uuid); |
| 62 |
} |
| 63 |
|
| 64 |
RefCountedPtr<BaseNode> ChannelzRegistry::InternalGet(intptr_t uuid) { |
| 65 |
MutexLock lock(&mu_); |
| 66 |
if (uuid < 1 || uuid > uuid_generator_) { |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
auto it = node_map_.find(uuid); |
| 70 |
if (it == node_map_.end()) return nullptr; |
| 71 |
// Found node. Return only if its refcount is not zero (i.e., when we |
| 72 |
// know that there is no other thread about to destroy it). |
| 73 |
BaseNode* node = it->second; |
| 74 |
return node->RefIfNonZero(); |
| 75 |
} |
| 76 |
|
| 77 |
std::string ChannelzRegistry::InternalGetTopChannels( |
| 78 |
intptr_t start_channel_id) { |
| 79 |
std::vector<RefCountedPtr<BaseNode>> top_level_channels; |
| 80 |
RefCountedPtr<BaseNode> node_after_pagination_limit; |
| 81 |
{ |
| 82 |
MutexLock lock(&mu_); |
| 83 |
for (auto it = node_map_.lower_bound(start_channel_id); |
| 84 |
it != node_map_.end(); ++it) { |
| 85 |
BaseNode* node = it->second; |
| 86 |
RefCountedPtr<BaseNode> node_ref; |
| 87 |
if (node->type() == BaseNode::EntityType::kTopLevelChannel && |
| 88 |
(node_ref = node->RefIfNonZero()) != nullptr) { |
| 89 |
// Check if we are over pagination limit to determine if we need to set |
| 90 |
// the "end" element. If we don't go through this block, we know that |
| 91 |
// when the loop terminates, we have <= to kPaginationLimit. |
| 92 |
// Note that because we have already increased this node's |
| 93 |
// refcount, we need to decrease it, but we can't unref while |
| 94 |
// holding the lock, because this may lead to a deadlock. |
| 95 |
if (top_level_channels.size() == kPaginationLimit) { |
| 96 |
node_after_pagination_limit = std::move(node_ref); |
| 97 |
break; |
| 98 |
} |
| 99 |
top_level_channels.emplace_back(std::move(node_ref)); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
Json::Object object; |
| 104 |
if (!top_level_channels.empty()) { |
| 105 |
// Create list of channels. |
| 106 |
Json::Array array; |
| 107 |
for (size_t i = 0; i < top_level_channels.size(); ++i) { |
| 108 |
array.emplace_back(top_level_channels[i]->RenderJson()); |
| 109 |
} |
| 110 |
object["channel"] = std::move(array); |
| 111 |
} |
| 112 |
if (node_after_pagination_limit == nullptr) object["end"] = true; |
| 113 |
Json json(std::move(object)); |
| 114 |
return json.Dump(); |
| 115 |
} |
| 116 |
|
| 117 |
std::string ChannelzRegistry::InternalGetServers(intptr_t start_server_id) { |
| 118 |
std::vector<RefCountedPtr<BaseNode>> servers; |
| 119 |
RefCountedPtr<BaseNode> node_after_pagination_limit; |
| 120 |
{ |
| 121 |
MutexLock lock(&mu_); |
| 122 |
for (auto it = node_map_.lower_bound(start_server_id); |
| 123 |
it != node_map_.end(); ++it) { |
| 124 |
BaseNode* node = it->second; |
| 125 |
RefCountedPtr<BaseNode> node_ref; |
| 126 |
if (node->type() == BaseNode::EntityType::kServer && |
| 127 |
(node_ref = node->RefIfNonZero()) != nullptr) { |
| 128 |
// Check if we are over pagination limit to determine if we need to set |
| 129 |
// the "end" element. If we don't go through this block, we know that |
| 130 |
// when the loop terminates, we have <= to kPaginationLimit. |
| 131 |
// Note that because we have already increased this node's |
| 132 |
// refcount, we need to decrease it, but we can't unref while |
| 133 |
// holding the lock, because this may lead to a deadlock. |
| 134 |
if (servers.size() == kPaginationLimit) { |
| 135 |
node_after_pagination_limit = std::move(node_ref); |
| 136 |
break; |
| 137 |
} |
| 138 |
servers.emplace_back(std::move(node_ref)); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
Json::Object object; |
| 143 |
if (!servers.empty()) { |
| 144 |
// Create list of servers. |
| 145 |
Json::Array array; |
| 146 |
for (size_t i = 0; i < servers.size(); ++i) { |
| 147 |
array.emplace_back(servers[i]->RenderJson()); |
| 148 |
} |
| 149 |
object["server"] = std::move(array); |
| 150 |
} |
| 151 |
if (node_after_pagination_limit == nullptr) object["end"] = true; |
| 152 |
Json json(std::move(object)); |
| 153 |
return json.Dump(); |
| 154 |
} |
| 155 |
|
| 156 |
void ChannelzRegistry::InternalLogAllEntities() { |
| 157 |
std::vector<RefCountedPtr<BaseNode>> nodes; |
| 158 |
{ |
| 159 |
MutexLock lock(&mu_); |
| 160 |
for (auto& p : node_map_) { |
| 161 |
RefCountedPtr<BaseNode> node = p.second->RefIfNonZero(); |
| 162 |
if (node != nullptr) { |
| 163 |
nodes.emplace_back(std::move(node)); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
for (size_t i = 0; i < nodes.size(); ++i) { |
| 168 |
std::string json = nodes[i]->RenderJsonString(); |
| 169 |
gpr_log(GPR_INFO, "%s", json.c_str()); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
} // namespace channelz |
| 174 |
} // namespace grpc_core |
| 175 |
|
| 176 |
char* grpc_channelz_get_top_channels(intptr_t start_channel_id) { |
| 177 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 178 |
grpc_core::ExecCtx exec_ctx; |
| 179 |
return gpr_strdup( |
| 180 |
grpc_core::channelz::ChannelzRegistry::GetTopChannels(start_channel_id) |
| 181 |
.c_str()); |
| 182 |
} |
| 183 |
|
| 184 |
char* grpc_channelz_get_servers(intptr_t start_server_id) { |
| 185 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 186 |
grpc_core::ExecCtx exec_ctx; |
| 187 |
return gpr_strdup( |
| 188 |
grpc_core::channelz::ChannelzRegistry::GetServers(start_server_id) |
| 189 |
.c_str()); |
| 190 |
} |
| 191 |
|
| 192 |
char* grpc_channelz_get_server(intptr_t server_id) { |
| 193 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 194 |
grpc_core::ExecCtx exec_ctx; |
| 195 |
grpc_core::RefCountedPtr<grpc_core::channelz::BaseNode> server_node = |
| 196 |
grpc_core::channelz::ChannelzRegistry::Get(server_id); |
| 197 |
if (server_node == nullptr || |
| 198 |
server_node->type() != |
| 199 |
grpc_core::channelz::BaseNode::EntityType::kServer) { |
| 200 |
return nullptr; |
| 201 |
} |
| 202 |
grpc_core::Json json = grpc_core::Json::Object{ |
| 203 |
{"server", server_node->RenderJson()}, |
| 204 |
}; |
| 205 |
return gpr_strdup(json.Dump().c_str()); |
| 206 |
} |
| 207 |
|
| 208 |
char* grpc_channelz_get_server_sockets(intptr_t server_id, |
| 209 |
intptr_t start_socket_id, |
| 210 |
intptr_t max_results) { |
| 211 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 212 |
grpc_core::ExecCtx exec_ctx; |
| 213 |
// Validate inputs before handing them of to the renderer. |
| 214 |
grpc_core::RefCountedPtr<grpc_core::channelz::BaseNode> base_node = |
| 215 |
grpc_core::channelz::ChannelzRegistry::Get(server_id); |
| 216 |
if (base_node == nullptr || |
| 217 |
base_node->type() != grpc_core::channelz::BaseNode::EntityType::kServer || |
| 218 |
start_socket_id < 0 || max_results < 0) { |
| 219 |
return nullptr; |
| 220 |
} |
| 221 |
// This cast is ok since we have just checked to make sure base_node is |
| 222 |
// actually a server node. |
| 223 |
grpc_core::channelz::ServerNode* server_node = |
| 224 |
static_cast<grpc_core::channelz::ServerNode*>(base_node.get()); |
| 225 |
return gpr_strdup( |
| 226 |
server_node->RenderServerSockets(start_socket_id, max_results).c_str()); |
| 227 |
} |
| 228 |
|
| 229 |
char* grpc_channelz_get_channel(intptr_t channel_id) { |
| 230 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 231 |
grpc_core::ExecCtx exec_ctx; |
| 232 |
grpc_core::RefCountedPtr<grpc_core::channelz::BaseNode> channel_node = |
| 233 |
grpc_core::channelz::ChannelzRegistry::Get(channel_id); |
| 234 |
if (channel_node == nullptr || |
| 235 |
(channel_node->type() != |
| 236 |
grpc_core::channelz::BaseNode::EntityType::kTopLevelChannel && |
| 237 |
channel_node->type() != |
| 238 |
grpc_core::channelz::BaseNode::EntityType::kInternalChannel)) { |
| 239 |
return nullptr; |
| 240 |
} |
| 241 |
grpc_core::Json json = grpc_core::Json::Object{ |
| 242 |
{"channel", channel_node->RenderJson()}, |
| 243 |
}; |
| 244 |
return gpr_strdup(json.Dump().c_str()); |
| 245 |
} |
| 246 |
|
| 247 |
char* grpc_channelz_get_subchannel(intptr_t subchannel_id) { |
| 248 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 249 |
grpc_core::ExecCtx exec_ctx; |
| 250 |
grpc_core::RefCountedPtr<grpc_core::channelz::BaseNode> subchannel_node = |
| 251 |
grpc_core::channelz::ChannelzRegistry::Get(subchannel_id); |
| 252 |
if (subchannel_node == nullptr || |
| 253 |
subchannel_node->type() != |
| 254 |
grpc_core::channelz::BaseNode::EntityType::kSubchannel) { |
| 255 |
return nullptr; |
| 256 |
} |
| 257 |
grpc_core::Json json = grpc_core::Json::Object{ |
| 258 |
{"subchannel", subchannel_node->RenderJson()}, |
| 259 |
}; |
| 260 |
return gpr_strdup(json.Dump().c_str()); |
| 261 |
} |
| 262 |
|
| 263 |
char* grpc_channelz_get_socket(intptr_t socket_id) { |
| 264 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 265 |
grpc_core::ExecCtx exec_ctx; |
| 266 |
grpc_core::RefCountedPtr<grpc_core::channelz::BaseNode> socket_node = |
| 267 |
grpc_core::channelz::ChannelzRegistry::Get(socket_id); |
| 268 |
if (socket_node == nullptr || |
| 269 |
socket_node->type() != |
| 270 |
grpc_core::channelz::BaseNode::EntityType::kSocket) { |
| 271 |
return nullptr; |
| 272 |
} |
| 273 |
grpc_core::Json json = grpc_core::Json::Object{ |
| 274 |
{"socket", socket_node->RenderJson()}, |
| 275 |
}; |
| 276 |
return gpr_strdup(json.Dump().c_str()); |
| 277 |
} |