1 //
2 //
3 // Copyright 2015 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 #include <grpc/grpc.h>
19 #include <grpc/support/port_platform.h>
20 #include <grpcpp/channel.h>
21 #include <grpcpp/client_context.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/ext/channelz_service_plugin.h>
24 #include <grpcpp/grpcpp.h>
25 #include <grpcpp/security/credentials.h>
26 #include <grpcpp/security/server_credentials.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 #include <grpcpp/server_context.h>
30 #include <unistd.h>
31
32 #include <cstdlib>
33 #include <fstream>
34 #include <iomanip>
35 #include <iostream>
36 #include <memory>
37 #include <ostream>
38 #include <queue>
39 #include <string>
40
41 #include "absl/flags/flag.h"
42 #include "absl/log/check.h"
43 #include "absl/log/log.h"
44 #include "absl/strings/str_format.h"
45 #include "absl/strings/str_join.h"
46 #include "google/protobuf/text_format.h"
47 #include "src/core/util/json/json.h"
48 #include "src/core/util/json/json_writer.h"
49 #include "src/cpp/server/channelz/channelz_service.h"
50 #include "src/proto/grpc/channelz/channelz.pb.h"
51 #include "test/core/test_util/test_config.h"
52 #include "test/cpp/util/test_config.h"
53 #include "test/cpp/util/test_credentials_provider.h"
54
55 ABSL_FLAG(std::string, server_address, "", "channelz server address");
56 ABSL_FLAG(std::string, custom_credentials_type, "", "custom credentials type");
57 ABSL_FLAG(int64_t, sampling_times, 1, "number of sampling");
58 // TODO(Capstan): Consider using absl::Duration
59 ABSL_FLAG(int64_t, sampling_interval_seconds, 0,
60 "sampling interval in seconds");
61 ABSL_FLAG(std::string, output_json, "", "output filename in json format");
62
63 namespace {
64 using grpc::ClientContext;
65 using grpc::Status;
66 using grpc::StatusCode;
67 using grpc::channelz::v1::GetChannelRequest;
68 using grpc::channelz::v1::GetChannelResponse;
69 using grpc::channelz::v1::GetServersRequest;
70 using grpc::channelz::v1::GetServersResponse;
71 using grpc::channelz::v1::GetSocketRequest;
72 using grpc::channelz::v1::GetSocketResponse;
73 using grpc::channelz::v1::GetSubchannelRequest;
74 using grpc::channelz::v1::GetSubchannelResponse;
75 using grpc::channelz::v1::GetTopChannelsRequest;
76 using grpc::channelz::v1::GetTopChannelsResponse;
77 } // namespace
78
79 class ChannelzSampler final {
80 public:
81 // Get server_id of a server
GetServerID(const grpc::channelz::v1::Server & server)82 int64_t GetServerID(const grpc::channelz::v1::Server& server) {
83 return server.ref().server_id();
84 }
85
86 // Get channel_id of a channel
GetChannelID(const grpc::channelz::v1::Channel & channel)87 inline int64_t GetChannelID(const grpc::channelz::v1::Channel& channel) {
88 return channel.ref().channel_id();
89 }
90
91 // Get subchannel_id of a subchannel
GetSubchannelID(const grpc::channelz::v1::Subchannel & subchannel)92 inline int64_t GetSubchannelID(
93 const grpc::channelz::v1::Subchannel& subchannel) {
94 return subchannel.ref().subchannel_id();
95 }
96
97 // Get socket_id of a socket
GetSocketID(const grpc::channelz::v1::Socket & socket)98 inline int64_t GetSocketID(const grpc::channelz::v1::Socket& socket) {
99 return socket.ref().socket_id();
100 }
101
102 // Get name of a server
GetServerName(const grpc::channelz::v1::Server & server)103 inline std::string GetServerName(const grpc::channelz::v1::Server& server) {
104 return server.ref().name();
105 }
106
107 // Get name of a channel
GetChannelName(const grpc::channelz::v1::Channel & channel)108 inline std::string GetChannelName(
109 const grpc::channelz::v1::Channel& channel) {
110 return channel.ref().name();
111 }
112
113 // Get name of a subchannel
GetSubchannelName(const grpc::channelz::v1::Subchannel & subchannel)114 inline std::string GetSubchannelName(
115 const grpc::channelz::v1::Subchannel& subchannel) {
116 return subchannel.ref().name();
117 }
118
119 // Get name of a socket
GetSocketName(const grpc::channelz::v1::Socket & socket)120 inline std::string GetSocketName(const grpc::channelz::v1::Socket& socket) {
121 return socket.ref().name();
122 }
123
124 // Get a channel based on channel_id
GetChannelRPC(int64_t channel_id)125 grpc::channelz::v1::Channel GetChannelRPC(int64_t channel_id) {
126 GetChannelRequest get_channel_request;
127 get_channel_request.set_channel_id(channel_id);
128 GetChannelResponse get_channel_response;
129 ClientContext get_channel_context;
130 get_channel_context.set_deadline(
131 grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
132 Status status = channelz_stub_->GetChannel(
133 &get_channel_context, get_channel_request, &get_channel_response);
134 if (!status.ok()) {
135 LOG(ERROR) << "GetChannelRPC failed: "
136 << get_channel_context.debug_error_string();
137 CHECK(0);
138 }
139 return get_channel_response.channel();
140 }
141
142 // Get a subchannel based on subchannel_id
GetSubchannelRPC(int64_t subchannel_id)143 grpc::channelz::v1::Subchannel GetSubchannelRPC(int64_t subchannel_id) {
144 GetSubchannelRequest get_subchannel_request;
145 get_subchannel_request.set_subchannel_id(subchannel_id);
146 GetSubchannelResponse get_subchannel_response;
147 ClientContext get_subchannel_context;
148 get_subchannel_context.set_deadline(
149 grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
150 Status status = channelz_stub_->GetSubchannel(&get_subchannel_context,
151 get_subchannel_request,
152 &get_subchannel_response);
153 if (!status.ok()) {
154 LOG(ERROR) << "GetSubchannelRPC failed: "
155 << get_subchannel_context.debug_error_string();
156 CHECK(0);
157 }
158 return get_subchannel_response.subchannel();
159 }
160
161 // get a socket based on socket_id
GetSocketRPC(int64_t socket_id)162 grpc::channelz::v1::Socket GetSocketRPC(int64_t socket_id) {
163 GetSocketRequest get_socket_request;
164 get_socket_request.set_socket_id(socket_id);
165 GetSocketResponse get_socket_response;
166 ClientContext get_socket_context;
167 get_socket_context.set_deadline(
168 grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
169 Status status = channelz_stub_->GetSocket(
170 &get_socket_context, get_socket_request, &get_socket_response);
171 if (!status.ok()) {
172 LOG(ERROR) << "GetSocketRPC failed: "
173 << get_socket_context.debug_error_string();
174 CHECK(0);
175 }
176 return get_socket_response.socket();
177 }
178
179 // get the descendant channels/subchannels/sockets of a channel
180 // push descendant channels/subchannels to queue for layer traverse
181 // store descendant channels/subchannels/sockets for dumping data
GetChannelDescedence(const grpc::channelz::v1::Channel & channel,std::queue<grpc::channelz::v1::Channel> & channel_queue,std::queue<grpc::channelz::v1::Subchannel> & subchannel_queue)182 void GetChannelDescedence(
183 const grpc::channelz::v1::Channel& channel,
184 std::queue<grpc::channelz::v1::Channel>& channel_queue,
185 std::queue<grpc::channelz::v1::Subchannel>& subchannel_queue) {
186 std::cout << " Channel ID" << GetChannelID(channel) << "_"
187 << GetChannelName(channel) << " descendence - ";
188 if (channel.channel_ref_size() > 0 || channel.subchannel_ref_size() > 0) {
189 if (channel.channel_ref_size() > 0) {
190 std::cout << "channel: ";
191 for (const auto& _channelref : channel.channel_ref()) {
192 int64_t ch_id = _channelref.channel_id();
193 std::cout << "ID" << ch_id << "_" << _channelref.name() << " ";
194 grpc::channelz::v1::Channel ch = GetChannelRPC(ch_id);
195 channel_queue.push(ch);
196 if (CheckID(ch_id)) {
197 all_channels_.push_back(ch);
198 StoreChannelInJson(ch);
199 }
200 }
201 if (channel.subchannel_ref_size() > 0) {
202 std::cout << ", ";
203 }
204 }
205 if (channel.subchannel_ref_size() > 0) {
206 std::cout << "subchannel: ";
207 for (const auto& _subchannelref : channel.subchannel_ref()) {
208 int64_t subch_id = _subchannelref.subchannel_id();
209 std::cout << "ID" << subch_id << "_" << _subchannelref.name() << " ";
210 grpc::channelz::v1::Subchannel subch = GetSubchannelRPC(subch_id);
211 subchannel_queue.push(subch);
212 if (CheckID(subch_id)) {
213 all_subchannels_.push_back(subch);
214 StoreSubchannelInJson(subch);
215 }
216 }
217 }
218 } else if (channel.socket_ref_size() > 0) {
219 std::cout << "socket: ";
220 for (const auto& _socketref : channel.socket_ref()) {
221 int64_t so_id = _socketref.socket_id();
222 std::cout << "ID" << so_id << "_" << _socketref.name() << " ";
223 grpc::channelz::v1::Socket so = GetSocketRPC(so_id);
224 if (CheckID(so_id)) {
225 all_sockets_.push_back(so);
226 StoreSocketInJson(so);
227 }
228 }
229 }
230 std::cout << std::endl;
231 }
232
233 // get the descendant channels/subchannels/sockets of a subchannel
234 // push descendant channels/subchannels to queue for layer traverse
235 // store descendant channels/subchannels/sockets for dumping data
GetSubchannelDescedence(grpc::channelz::v1::Subchannel & subchannel,std::queue<grpc::channelz::v1::Channel> & channel_queue,std::queue<grpc::channelz::v1::Subchannel> & subchannel_queue)236 void GetSubchannelDescedence(
237 grpc::channelz::v1::Subchannel& subchannel,
238 std::queue<grpc::channelz::v1::Channel>& channel_queue,
239 std::queue<grpc::channelz::v1::Subchannel>& subchannel_queue) {
240 std::cout << " Subchannel ID" << GetSubchannelID(subchannel) << "_"
241 << GetSubchannelName(subchannel) << " descendence - ";
242 if (subchannel.channel_ref_size() > 0 ||
243 subchannel.subchannel_ref_size() > 0) {
244 if (subchannel.channel_ref_size() > 0) {
245 std::cout << "channel: ";
246 for (const auto& _channelref : subchannel.channel_ref()) {
247 int64_t ch_id = _channelref.channel_id();
248 std::cout << "ID" << ch_id << "_" << _channelref.name() << " ";
249 grpc::channelz::v1::Channel ch = GetChannelRPC(ch_id);
250 channel_queue.push(ch);
251 if (CheckID(ch_id)) {
252 all_channels_.push_back(ch);
253 StoreChannelInJson(ch);
254 }
255 }
256 if (subchannel.subchannel_ref_size() > 0) {
257 std::cout << ", ";
258 }
259 }
260 if (subchannel.subchannel_ref_size() > 0) {
261 std::cout << "subchannel: ";
262 for (const auto& _subchannelref : subchannel.subchannel_ref()) {
263 int64_t subch_id = _subchannelref.subchannel_id();
264 std::cout << "ID" << subch_id << "_" << _subchannelref.name() << " ";
265 grpc::channelz::v1::Subchannel subch = GetSubchannelRPC(subch_id);
266 subchannel_queue.push(subch);
267 if (CheckID(subch_id)) {
268 all_subchannels_.push_back(subch);
269 StoreSubchannelInJson(subch);
270 }
271 }
272 }
273 } else if (subchannel.socket_ref_size() > 0) {
274 std::cout << "socket: ";
275 for (const auto& _socketref : subchannel.socket_ref()) {
276 int64_t so_id = _socketref.socket_id();
277 std::cout << "ID" << so_id << "_" << _socketref.name() << " ";
278 grpc::channelz::v1::Socket so = GetSocketRPC(so_id);
279 if (CheckID(so_id)) {
280 all_sockets_.push_back(so);
281 StoreSocketInJson(so);
282 }
283 }
284 }
285 std::cout << std::endl;
286 }
287
288 // Set up the channelz sampler client
289 // Initialize json as an array
Setup(const std::string & custom_credentials_type,const std::string & server_address)290 void Setup(const std::string& custom_credentials_type,
291 const std::string& server_address) {
292 json_ = grpc_core::Json::Array();
293 rpc_timeout_seconds_ = 20;
294 grpc::ChannelArguments channel_args;
295 std::shared_ptr<grpc::ChannelCredentials> channel_creds =
296 grpc::testing::GetCredentialsProvider()->GetChannelCredentials(
297 custom_credentials_type, &channel_args);
298 if (!channel_creds) {
299 LOG(ERROR) << "Wrong user credential type: " << custom_credentials_type
300 << ". Allowed credential types: INSECURE_CREDENTIALS, ssl, "
301 "alts, google_default_credentials.";
302 CHECK(0);
303 }
304 std::shared_ptr<grpc::Channel> channel =
305 CreateChannel(server_address, channel_creds);
306 channelz_stub_ = grpc::channelz::v1::Channelz::NewStub(channel);
307 }
308
309 // Get all servers, keep querying until getting all
310 // Store servers for dumping data
311 // Need to check id repeating for servers
GetServersRPC()312 void GetServersRPC() {
313 int64_t server_start_id = 0;
314 while (true) {
315 GetServersRequest get_servers_request;
316 GetServersResponse get_servers_response;
317 ClientContext get_servers_context;
318 get_servers_context.set_deadline(
319 grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
320 get_servers_request.set_start_server_id(server_start_id);
321 Status status = channelz_stub_->GetServers(
322 &get_servers_context, get_servers_request, &get_servers_response);
323 if (!status.ok()) {
324 if (status.error_code() == StatusCode::UNIMPLEMENTED) {
325 LOG(ERROR) << "Error status UNIMPLEMENTED. Please check and make "
326 "sure channelz has been registered on the server being "
327 "queried.";
328 } else {
329 LOG(ERROR) << "GetServers RPC with "
330 "GetServersRequest.server_start_id="
331 << server_start_id << ", failed: "
332 << get_servers_context.debug_error_string();
333 }
334 CHECK(0);
335 }
336 for (const auto& _server : get_servers_response.server()) {
337 all_servers_.push_back(_server);
338 StoreServerInJson(_server);
339 }
340 if (!get_servers_response.end()) {
341 server_start_id = GetServerID(all_servers_.back()) + 1;
342 } else {
343 break;
344 }
345 }
346 std::cout << "Number of servers = " << all_servers_.size() << std::endl;
347 }
348
349 // Get sockets that belongs to servers
350 // Store sockets for dumping data
GetSocketsOfServers()351 void GetSocketsOfServers() {
352 for (const auto& _server : all_servers_) {
353 std::cout << "Server ID" << GetServerID(_server) << "_"
354 << GetServerName(_server) << " listen_socket - ";
355 for (const auto& _socket : _server.listen_socket()) {
356 int64_t so_id = _socket.socket_id();
357 std::cout << "ID" << so_id << "_" << _socket.name() << " ";
358 if (CheckID(so_id)) {
359 grpc::channelz::v1::Socket so = GetSocketRPC(so_id);
360 all_sockets_.push_back(so);
361 StoreSocketInJson(so);
362 }
363 }
364 std::cout << std::endl;
365 }
366 }
367
368 // Get all top channels, keep querying until getting all
369 // Store channels for dumping data
370 // No need to check id repeating for top channels
GetTopChannelsRPC()371 void GetTopChannelsRPC() {
372 int64_t channel_start_id = 0;
373 while (true) {
374 GetTopChannelsRequest get_top_channels_request;
375 GetTopChannelsResponse get_top_channels_response;
376 ClientContext get_top_channels_context;
377 get_top_channels_context.set_deadline(
378 grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
379 get_top_channels_request.set_start_channel_id(channel_start_id);
380 Status status = channelz_stub_->GetTopChannels(
381 &get_top_channels_context, get_top_channels_request,
382 &get_top_channels_response);
383 if (!status.ok()) {
384 LOG(ERROR) << "GetTopChannels RPC with "
385 "GetTopChannelsRequest.channel_start_id="
386 << channel_start_id << " failed: "
387 << get_top_channels_context.debug_error_string();
388 CHECK(0);
389 }
390 for (const auto& _topchannel : get_top_channels_response.channel()) {
391 top_channels_.push_back(_topchannel);
392 all_channels_.push_back(_topchannel);
393 StoreChannelInJson(_topchannel);
394 }
395 if (!get_top_channels_response.end()) {
396 channel_start_id = GetChannelID(top_channels_.back()) + 1;
397 } else {
398 break;
399 }
400 }
401 std::cout << std::endl
402 << "Number of top channels = " << top_channels_.size()
403 << std::endl;
404 }
405
406 // layer traverse for each top channel
TraverseTopChannels()407 void TraverseTopChannels() {
408 for (const auto& _topchannel : top_channels_) {
409 int tree_depth = 0;
410 std::queue<grpc::channelz::v1::Channel> channel_queue;
411 std::queue<grpc::channelz::v1::Subchannel> subchannel_queue;
412 std::cout << "Tree depth = " << tree_depth << std::endl;
413 GetChannelDescedence(_topchannel, channel_queue, subchannel_queue);
414 while (!channel_queue.empty() || !subchannel_queue.empty()) {
415 ++tree_depth;
416 std::cout << "Tree depth = " << tree_depth << std::endl;
417 int ch_q_size = channel_queue.size();
418 int subch_q_size = subchannel_queue.size();
419 for (int i = 0; i < ch_q_size; ++i) {
420 grpc::channelz::v1::Channel ch = channel_queue.front();
421 channel_queue.pop();
422 GetChannelDescedence(ch, channel_queue, subchannel_queue);
423 }
424 for (int i = 0; i < subch_q_size; ++i) {
425 grpc::channelz::v1::Subchannel subch = subchannel_queue.front();
426 subchannel_queue.pop();
427 GetSubchannelDescedence(subch, channel_queue, subchannel_queue);
428 }
429 }
430 std::cout << std::endl;
431 }
432 }
433
434 // dump data of all entities to stdout
DumpStdout()435 void DumpStdout() {
436 std::string data_str;
437 for (const auto& _channel : all_channels_) {
438 std::cout << "channel ID" << GetChannelID(_channel) << "_"
439 << GetChannelName(_channel) << " data:" << std::endl;
440 // TODO(mohanli): TextFormat::PrintToString records time as seconds and
441 // nanos. Need a more human readable way.
442 ::google::protobuf::TextFormat::PrintToString(_channel.data(), &data_str);
443 printf("%s\n", data_str.c_str());
444 }
445 for (const auto& _subchannel : all_subchannels_) {
446 std::cout << "subchannel ID" << GetSubchannelID(_subchannel) << "_"
447 << GetSubchannelName(_subchannel) << " data:" << std::endl;
448 ::google::protobuf::TextFormat::PrintToString(_subchannel.data(),
449 &data_str);
450 printf("%s\n", data_str.c_str());
451 }
452 for (const auto& _server : all_servers_) {
453 std::cout << "server ID" << GetServerID(_server) << "_"
454 << GetServerName(_server) << " data:" << std::endl;
455 ::google::protobuf::TextFormat::PrintToString(_server.data(), &data_str);
456 printf("%s\n", data_str.c_str());
457 }
458 for (const auto& _socket : all_sockets_) {
459 std::cout << "socket ID" << GetSocketID(_socket) << "_"
460 << GetSocketName(_socket) << " data:" << std::endl;
461 ::google::protobuf::TextFormat::PrintToString(_socket.data(), &data_str);
462 printf("%s\n", data_str.c_str());
463 }
464 }
465
466 // Store a channel in Json
StoreChannelInJson(const grpc::channelz::v1::Channel & channel)467 void StoreChannelInJson(const grpc::channelz::v1::Channel& channel) {
468 std::string id = grpc::to_string(GetChannelID(channel));
469 std::string type = "Channel";
470 std::string description;
471 ::google::protobuf::TextFormat::PrintToString(channel.data(), &description);
472 grpc_core::Json description_json = grpc_core::Json::FromString(description);
473 StoreEntityInJson(id, type, description_json);
474 }
475
476 // Store a subchannel in Json
StoreSubchannelInJson(const grpc::channelz::v1::Subchannel & subchannel)477 void StoreSubchannelInJson(const grpc::channelz::v1::Subchannel& subchannel) {
478 std::string id = grpc::to_string(GetSubchannelID(subchannel));
479 std::string type = "Subchannel";
480 std::string description;
481 ::google::protobuf::TextFormat::PrintToString(subchannel.data(),
482 &description);
483 grpc_core::Json description_json = grpc_core::Json::FromString(description);
484 StoreEntityInJson(id, type, description_json);
485 }
486
487 // Store a server in Json
StoreServerInJson(const grpc::channelz::v1::Server & server)488 void StoreServerInJson(const grpc::channelz::v1::Server& server) {
489 std::string id = grpc::to_string(GetServerID(server));
490 std::string type = "Server";
491 std::string description;
492 ::google::protobuf::TextFormat::PrintToString(server.data(), &description);
493 grpc_core::Json description_json = grpc_core::Json::FromString(description);
494 StoreEntityInJson(id, type, description_json);
495 }
496
497 // Store a socket in Json
StoreSocketInJson(const grpc::channelz::v1::Socket & socket)498 void StoreSocketInJson(const grpc::channelz::v1::Socket& socket) {
499 std::string id = grpc::to_string(GetSocketID(socket));
500 std::string type = "Socket";
501 std::string description;
502 ::google::protobuf::TextFormat::PrintToString(socket.data(), &description);
503 grpc_core::Json description_json = grpc_core::Json::FromString(description);
504 StoreEntityInJson(id, type, description_json);
505 }
506
507 // Store an entity in Json
StoreEntityInJson(std::string & id,std::string & type,const grpc_core::Json & description)508 void StoreEntityInJson(std::string& id, std::string& type,
509 const grpc_core::Json& description) {
510 std::string start, finish;
511 gpr_timespec ago = gpr_time_sub(
512 now_,
513 gpr_time_from_seconds(absl::GetFlag(FLAGS_sampling_interval_seconds),
514 GPR_TIMESPAN));
515 std::stringstream ss;
516 const time_t time_now = now_.tv_sec;
517 ss << std::put_time(std::localtime(&time_now), "%F %T");
518 finish = ss.str(); // example: "2019-02-01 12:12:18"
519 ss.str("");
520 const time_t time_ago = ago.tv_sec;
521 ss << std::put_time(std::localtime(&time_ago), "%F %T");
522 start = ss.str();
523 grpc_core::Json obj = grpc_core::Json::FromObject(
524 {{"Task",
525 grpc_core::Json::FromString(absl::StrFormat("%s_ID%s", type, id))},
526 {"Start", grpc_core::Json::FromString(start)},
527 {"Finish", grpc_core::Json::FromString(finish)},
528 {"ID", grpc_core::Json::FromString(id)},
529 {"Type", grpc_core::Json::FromString(type)},
530 {"Description", description}});
531 json_.push_back(obj);
532 }
533
534 // Dump data in json
DumpJson()535 std::string DumpJson() {
536 return grpc_core::JsonDump(grpc_core::Json::FromArray(json_));
537 }
538
539 // Check if one entity has been recorded
CheckID(int64_t id)540 bool CheckID(int64_t id) {
541 if (id_set_.count(id) == 0) {
542 id_set_.insert(id);
543 return true;
544 } else {
545 return false;
546 }
547 }
548
549 // Record current time
RecordNow()550 void RecordNow() { now_ = gpr_now(GPR_CLOCK_REALTIME); }
551
552 private:
553 std::unique_ptr<grpc::channelz::v1::Channelz::Stub> channelz_stub_;
554 std::vector<grpc::channelz::v1::Channel> top_channels_;
555 std::vector<grpc::channelz::v1::Server> all_servers_;
556 std::vector<grpc::channelz::v1::Channel> all_channels_;
557 std::vector<grpc::channelz::v1::Subchannel> all_subchannels_;
558 std::vector<grpc::channelz::v1::Socket> all_sockets_;
559 std::unordered_set<int64_t> id_set_;
560 grpc_core::Json::Array json_;
561 int64_t rpc_timeout_seconds_;
562 gpr_timespec now_;
563 };
564
main(int argc,char ** argv)565 int main(int argc, char** argv) {
566 grpc::testing::TestEnvironment env(&argc, argv);
567 grpc::testing::InitTest(&argc, &argv, true);
568 std::ofstream output_file(absl::GetFlag(FLAGS_output_json));
569 for (int i = 0; i < absl::GetFlag(FLAGS_sampling_times); ++i) {
570 ChannelzSampler channelz_sampler;
571 channelz_sampler.Setup(absl::GetFlag(FLAGS_custom_credentials_type),
572 absl::GetFlag(FLAGS_server_address));
573 std::cout << "Wait for sampling interval "
574 << absl::GetFlag(FLAGS_sampling_interval_seconds) << "s..."
575 << std::endl;
576 const gpr_timespec kDelay = gpr_time_add(
577 gpr_now(GPR_CLOCK_MONOTONIC),
578 gpr_time_from_seconds(absl::GetFlag(FLAGS_sampling_interval_seconds),
579 GPR_TIMESPAN));
580 gpr_sleep_until(kDelay);
581 std::cout << "##### " << i << "th sampling #####" << std::endl;
582 channelz_sampler.RecordNow();
583 channelz_sampler.GetServersRPC();
584 channelz_sampler.GetSocketsOfServers();
585 channelz_sampler.GetTopChannelsRPC();
586 channelz_sampler.TraverseTopChannels();
587 channelz_sampler.DumpStdout();
588 if (!absl::GetFlag(FLAGS_output_json).empty()) {
589 output_file << channelz_sampler.DumpJson() << "\n" << std::flush;
590 }
591 }
592 output_file.close();
593 return 0;
594 }
595