Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / ext / filters / client_channel / client_channel_channelz.cc
1 /*
2  *
3  * Copyright 2018 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/ext/filters/client_channel/client_channel.h"
22 #include "src/core/ext/filters/client_channel/client_channel_channelz.h"
23 #include "src/core/lib/channel/channelz_registry.h"
24 #include "src/core/lib/gpr/useful.h"
25 #include "src/core/lib/surface/channel.h"
26 #include "src/core/lib/transport/connectivity_state.h"
27
28 #include <grpc/support/string_util.h>
29
30 namespace grpc_core {
31 namespace channelz {
32
33 SubchannelNode::SubchannelNode(const char* target_address,
34                                size_t channel_tracer_max_nodes)
35     : BaseNode(EntityType::kSubchannel,
36                UniquePtr<char>(gpr_strdup(target_address))),
37       target_(UniquePtr<char>(gpr_strdup(target_address))),
38       trace_(channel_tracer_max_nodes) {}
39
40 SubchannelNode::~SubchannelNode() {}
41
42 void SubchannelNode::UpdateConnectivityState(grpc_connectivity_state state) {
43   connectivity_state_.Store(state, MemoryOrder::RELAXED);
44 }
45
46 void SubchannelNode::SetChildSocket(RefCountedPtr<SocketNode> socket) {
47   MutexLock lock(&socket_mu_);
48   child_socket_ = std::move(socket);
49 }
50
51 void SubchannelNode::PopulateConnectivityState(grpc_json* json) {
52   grpc_connectivity_state state =
53       connectivity_state_.Load(MemoryOrder::RELAXED);
54   json = grpc_json_create_child(nullptr, json, "state", nullptr,
55                                 GRPC_JSON_OBJECT, false);
56   grpc_json_create_child(nullptr, json, "state",
57                          grpc_connectivity_state_name(state), GRPC_JSON_STRING,
58                          false);
59 }
60
61 grpc_json* SubchannelNode::RenderJson() {
62   grpc_json* top_level_json = grpc_json_create(GRPC_JSON_OBJECT);
63   grpc_json* json = top_level_json;
64   grpc_json* json_iterator = nullptr;
65   json_iterator = grpc_json_create_child(json_iterator, json, "ref", nullptr,
66                                          GRPC_JSON_OBJECT, false);
67   json = json_iterator;
68   json_iterator = nullptr;
69   json_iterator = grpc_json_add_number_string_child(json, json_iterator,
70                                                     "subchannelId", uuid());
71   // reset json iterators to top level object
72   json = top_level_json;
73   json_iterator = nullptr;
74   // create and fill the data child.
75   grpc_json* data = grpc_json_create_child(json_iterator, json, "data", nullptr,
76                                            GRPC_JSON_OBJECT, false);
77   json = data;
78   json_iterator = nullptr;
79   PopulateConnectivityState(json);
80   GPR_ASSERT(target_.get() != nullptr);
81   grpc_json_create_child(nullptr, json, "target", target_.get(),
82                          GRPC_JSON_STRING, false);
83   // fill in the channel trace if applicable
84   grpc_json* trace_json = trace_.RenderJson();
85   if (trace_json != nullptr) {
86     trace_json->key = "trace";  // this object is named trace in channelz.proto
87     grpc_json_link_child(json, trace_json, nullptr);
88   }
89   // ask CallCountingHelper to populate trace and call count data.
90   call_counter_.PopulateCallCounts(json);
91   json = top_level_json;
92   // populate the child socket.
93   RefCountedPtr<SocketNode> child_socket;
94   {
95     MutexLock lock(&socket_mu_);
96     child_socket = child_socket_;
97   }
98   if (child_socket != nullptr && child_socket->uuid() != 0) {
99     grpc_json* array_parent = grpc_json_create_child(
100         nullptr, json, "socketRef", nullptr, GRPC_JSON_ARRAY, false);
101     json_iterator = grpc_json_create_child(json_iterator, array_parent, nullptr,
102                                            nullptr, GRPC_JSON_OBJECT, false);
103     grpc_json* sibling_iterator = grpc_json_add_number_string_child(
104         json_iterator, nullptr, "socketId", child_socket->uuid());
105     grpc_json_create_child(sibling_iterator, json_iterator, "name",
106                            child_socket->name(), GRPC_JSON_STRING, false);
107   }
108   return top_level_json;
109 }
110
111 }  // namespace channelz
112 }  // namespace grpc_core