| 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 |
#ifndef GRPC_CORE_LIB_CHANNEL_CHANNELZ_H |
| 20 |
#define GRPC_CORE_LIB_CHANNEL_CHANNELZ_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stddef.h> |
| 25 |
|
| 26 |
#include <atomic> |
| 27 |
#include <cstdint> |
| 28 |
#include <map> |
| 29 |
#include <set> |
| 30 |
#include <string> |
| 31 |
#include <utility> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include "absl/strings/string_view.h" |
| 35 |
#include "absl/types/optional.h" |
| 36 |
|
| 37 |
#include <grpc/impl/codegen/connectivity_state.h> |
| 38 |
#include <grpc/impl/codegen/grpc_types.h> |
| 39 |
#include <grpc/slice.h> |
| 40 |
|
| 41 |
#include "src/core/lib/channel/channel_trace.h" |
| 42 |
#include "src/core/lib/gpr/time_precise.h" |
| 43 |
#include "src/core/lib/gpr/useful.h" |
| 44 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 45 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 46 |
#include "src/core/lib/gprpp/sync.h" |
| 47 |
#include "src/core/lib/json/json.h" |
| 48 |
|
| 49 |
// Channel arg key for channelz node. |
| 50 |
#define GRPC_ARG_CHANNELZ_CHANNEL_NODE "grpc.internal.channelz_channel_node" |
| 51 |
|
| 52 |
// Channel arg key for indicating an internal channel. |
| 53 |
#define GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL \ |
| 54 |
"grpc.channelz_is_internal_channel" |
| 55 |
|
| 56 |
/** This is the default value for whether or not to enable channelz. If |
| 57 |
* GRPC_ARG_ENABLE_CHANNELZ is set, it will override this default value. */ |
| 58 |
#define GRPC_ENABLE_CHANNELZ_DEFAULT true |
| 59 |
|
| 60 |
/** This is the default value for the maximum amount of memory used by trace |
| 61 |
* events per channel trace node. If |
| 62 |
* GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE is set, it will override |
| 63 |
* this default value. */ |
| 64 |
#define GRPC_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE_DEFAULT (1024 * 4) |
| 65 |
|
| 66 |
namespace grpc_core { |
| 67 |
|
| 68 |
namespace channelz { |
| 69 |
|
| 70 |
class SocketNode; |
| 71 |
class ListenSocketNode; |
| 72 |
|
| 73 |
namespace testing { |
| 74 |
class CallCountingHelperPeer; |
| 75 |
class ChannelNodePeer; |
| 76 |
} // namespace testing |
| 77 |
|
| 78 |
// base class for all channelz entities |
| 79 |
class BaseNode : public RefCounted<BaseNode> { |
| 80 |
public: |
| 81 |
// There are only four high level channelz entities. However, to support |
| 82 |
// GetTopChannelsRequest, we split the Channel entity into two different |
| 83 |
// types. All children of BaseNode must be one of these types. |
| 84 |
enum class EntityType { |
| 85 |
kTopLevelChannel, |
| 86 |
kInternalChannel, |
| 87 |
kSubchannel, |
| 88 |
kServer, |
| 89 |
kSocket, |
| 90 |
}; |
| 91 |
|
| 92 |
protected: |
| 93 |
BaseNode(EntityType type, std::string name); |
| 94 |
|
| 95 |
public: |
| 96 |
~BaseNode() override; |
| 97 |
|
| 98 |
// All children must implement this function. |
| 99 |
virtual Json RenderJson() = 0; |
| 100 |
|
| 101 |
// Renders the json and returns allocated string that must be freed by the |
| 102 |
// caller. |
| 103 |
std::string RenderJsonString(); |
| 104 |
|
| 105 |
EntityType type() const { return type_; } |
| 106 |
intptr_t uuid() const { return uuid_; } |
| 107 |
const std::string& name() const { return name_; } |
| 108 |
|
| 109 |
private: |
| 110 |
// to allow the ChannelzRegistry to set uuid_ under its lock. |
| 111 |
friend class ChannelzRegistry; |
| 112 |
const EntityType type_; |
| 113 |
intptr_t uuid_; |
| 114 |
std::string name_; |
| 115 |
}; |
| 116 |
|
| 117 |
// This class is a helper class for channelz entities that deal with Channels, |
| 118 |
// Subchannels, and Servers, since those have similar proto definitions. |
| 119 |
// This class has the ability to: |
| 120 |
// - track calls_{started,succeeded,failed} |
| 121 |
// - track last_call_started_timestamp |
| 122 |
// - perform rendering of the above items |
| 123 |
class CallCountingHelper { |
| 124 |
public: |
| 125 |
CallCountingHelper(); |
| 126 |
|
| 127 |
void RecordCallStarted(); |
| 128 |
void RecordCallFailed(); |
| 129 |
void RecordCallSucceeded(); |
| 130 |
|
| 131 |
// Common rendering of the call count data and last_call_started_timestamp. |
| 132 |
void PopulateCallCounts(Json::Object* json); |
| 133 |
|
| 134 |
private: |
| 135 |
// testing peer friend. |
| 136 |
friend class testing::CallCountingHelperPeer; |
| 137 |
|
| 138 |
// TODO(soheil): add a proper PerCPU helper and use it here. |
| 139 |
struct AtomicCounterData { |
| 140 |
// Define the ctors so that we can use this structure in InlinedVector. |
| 141 |
AtomicCounterData() = default; |
| 142 |
AtomicCounterData(const AtomicCounterData& that) |
| 143 |
: calls_started(that.calls_started.load(std::memory_order_relaxed)), |
| 144 |
calls_succeeded(that.calls_succeeded.load(std::memory_order_relaxed)), |
| 145 |
calls_failed(that.calls_failed.load(std::memory_order_relaxed)), |
| 146 |
last_call_started_cycle( |
| 147 |
that.last_call_started_cycle.load(std::memory_order_relaxed)) {} |
| 148 |
|
| 149 |
std::atomic<int64_t> calls_started{0}; |
| 150 |
std::atomic<int64_t> calls_succeeded{0}; |
| 151 |
std::atomic<int64_t> calls_failed{0}; |
| 152 |
std::atomic<gpr_cycle_counter> last_call_started_cycle{0}; |
| 153 |
// Make sure the size is exactly one cache line. |
| 154 |
uint8_t padding[GPR_CACHELINE_SIZE - 3 * sizeof(std::atomic<intptr_t>) - |
| 155 |
sizeof(std::atomic<gpr_cycle_counter>)]; |
| 156 |
}; |
| 157 |
// TODO(soheilhy,veblush): Revist this after abseil integration. |
| 158 |
// This has a problem when using abseil inlined_vector because it |
| 159 |
// carries an alignment attribute properly but our allocator doesn't |
| 160 |
// respect this. To avoid UBSAN errors, this should be removed with |
| 161 |
// abseil inlined_vector. |
| 162 |
// GPR_ALIGN_STRUCT(GPR_CACHELINE_SIZE); |
| 163 |
|
| 164 |
struct CounterData { |
| 165 |
int64_t calls_started = 0; |
| 166 |
int64_t calls_succeeded = 0; |
| 167 |
int64_t calls_failed = 0; |
| 168 |
gpr_cycle_counter last_call_started_cycle = 0; |
| 169 |
}; |
| 170 |
|
| 171 |
// collects the sharded data into one CounterData struct. |
| 172 |
void CollectData(CounterData* out); |
| 173 |
|
| 174 |
std::vector<AtomicCounterData> per_cpu_counter_data_storage_; |
| 175 |
size_t num_cores_ = 0; |
| 176 |
}; |
| 177 |
|
| 178 |
// Handles channelz bookkeeping for channels |
| 179 |
class ChannelNode : public BaseNode { |
| 180 |
public: |
| 181 |
ChannelNode(std::string target, size_t channel_tracer_max_nodes, |
| 182 |
bool is_internal_channel); |
| 183 |
|
| 184 |
static absl::string_view ChannelArgName() { |
| 185 |
return GRPC_ARG_CHANNELZ_CHANNEL_NODE; |
| 186 |
} |
| 187 |
|
| 188 |
// Returns the string description of the given connectivity state. |
| 189 |
static const char* GetChannelConnectivityStateChangeString( |
| 190 |
grpc_connectivity_state state); |
| 191 |
|
| 192 |
Json RenderJson() override; |
| 193 |
|
| 194 |
// proxy methods to composed classes. |
| 195 |
void AddTraceEvent(ChannelTrace::Severity severity, const grpc_slice& data) { |
| 196 |
trace_.AddTraceEvent(severity, data); |
| 197 |
} |
| 198 |
void AddTraceEventWithReference(ChannelTrace::Severity severity, |
| 199 |
const grpc_slice& data, |
| 200 |
RefCountedPtr<BaseNode> referenced_channel) { |
| 201 |
trace_.AddTraceEventWithReference(severity, data, |
| 202 |
std::move(referenced_channel)); |
| 203 |
} |
| 204 |
void RecordCallStarted() { call_counter_.RecordCallStarted(); } |
| 205 |
void RecordCallFailed() { call_counter_.RecordCallFailed(); } |
| 206 |
void RecordCallSucceeded() { call_counter_.RecordCallSucceeded(); } |
| 207 |
|
| 208 |
void SetConnectivityState(grpc_connectivity_state state); |
| 209 |
|
| 210 |
// TODO(roth): take in a RefCountedPtr to the child channel so we can retrieve |
| 211 |
// the human-readable name. |
| 212 |
void AddChildChannel(intptr_t child_uuid); |
| 213 |
void RemoveChildChannel(intptr_t child_uuid); |
| 214 |
|
| 215 |
// TODO(roth): take in a RefCountedPtr to the child subchannel so we can |
| 216 |
// retrieve the human-readable name. |
| 217 |
void AddChildSubchannel(intptr_t child_uuid); |
| 218 |
void RemoveChildSubchannel(intptr_t child_uuid); |
| 219 |
|
| 220 |
private: |
| 221 |
// Allows the channel trace test to access trace_. |
| 222 |
friend class testing::ChannelNodePeer; |
| 223 |
|
| 224 |
void PopulateChildRefs(Json::Object* json); |
| 225 |
|
| 226 |
std::string target_; |
| 227 |
CallCountingHelper call_counter_; |
| 228 |
ChannelTrace trace_; |
| 229 |
|
| 230 |
// Least significant bit indicates whether the value is set. Remaining |
| 231 |
// bits are a grpc_connectivity_state value. |
| 232 |
std::atomic<int> connectivity_state_{0}; |
| 233 |
|
| 234 |
Mutex child_mu_; // Guards sets below. |
| 235 |
std::set<intptr_t> child_channels_; |
| 236 |
std::set<intptr_t> child_subchannels_; |
| 237 |
}; |
| 238 |
|
| 239 |
// Handles channelz bookkeeping for servers |
| 240 |
class ServerNode : public BaseNode { |
| 241 |
public: |
| 242 |
explicit ServerNode(size_t channel_tracer_max_nodes); |
| 243 |
|
| 244 |
~ServerNode() override; |
| 245 |
|
| 246 |
Json RenderJson() override; |
| 247 |
|
| 248 |
std::string RenderServerSockets(intptr_t start_socket_id, |
| 249 |
intptr_t max_results); |
| 250 |
|
| 251 |
void AddChildSocket(RefCountedPtr<SocketNode> node); |
| 252 |
|
| 253 |
void RemoveChildSocket(intptr_t child_uuid); |
| 254 |
|
| 255 |
void AddChildListenSocket(RefCountedPtr<ListenSocketNode> node); |
| 256 |
|
| 257 |
void RemoveChildListenSocket(intptr_t child_uuid); |
| 258 |
|
| 259 |
// proxy methods to composed classes. |
| 260 |
void AddTraceEvent(ChannelTrace::Severity severity, const grpc_slice& data) { |
| 261 |
trace_.AddTraceEvent(severity, data); |
| 262 |
} |
| 263 |
void AddTraceEventWithReference(ChannelTrace::Severity severity, |
| 264 |
const grpc_slice& data, |
| 265 |
RefCountedPtr<BaseNode> referenced_channel) { |
| 266 |
trace_.AddTraceEventWithReference(severity, data, |
| 267 |
std::move(referenced_channel)); |
| 268 |
} |
| 269 |
void RecordCallStarted() { call_counter_.RecordCallStarted(); } |
| 270 |
void RecordCallFailed() { call_counter_.RecordCallFailed(); } |
| 271 |
void RecordCallSucceeded() { call_counter_.RecordCallSucceeded(); } |
| 272 |
|
| 273 |
private: |
| 274 |
CallCountingHelper call_counter_; |
| 275 |
ChannelTrace trace_; |
| 276 |
Mutex child_mu_; // Guards child maps below. |
| 277 |
std::map<intptr_t, RefCountedPtr<SocketNode>> child_sockets_; |
| 278 |
std::map<intptr_t, RefCountedPtr<ListenSocketNode>> child_listen_sockets_; |
| 279 |
}; |
| 280 |
|
| 281 |
#define GRPC_ARG_CHANNELZ_SECURITY "grpc.internal.channelz_security" |
| 282 |
|
| 283 |
// Handles channelz bookkeeping for sockets |
| 284 |
class SocketNode : public BaseNode { |
| 285 |
public: |
| 286 |
struct Security : public RefCounted<Security> { |
| 287 |
struct Tls { |
| 288 |
// This is a workaround for https://bugs.llvm.org/show_bug.cgi?id=50346 |
| 289 |
Tls() {} |
| 290 |
|
| 291 |
enum class NameType { kUnset = 0, kStandardName = 1, kOtherName = 2 }; |
| 292 |
NameType type = NameType::kUnset; |
| 293 |
// Holds the value of standard_name or other_names if type is not kUnset. |
| 294 |
std::string name; |
| 295 |
std::string local_certificate; |
| 296 |
std::string remote_certificate; |
| 297 |
|
| 298 |
Json RenderJson(); |
| 299 |
}; |
| 300 |
enum class ModelType { kUnset = 0, kTls = 1, kOther = 2 }; |
| 301 |
ModelType type = ModelType::kUnset; |
| 302 |
absl::optional<Tls> tls; |
| 303 |
absl::optional<Json> other; |
| 304 |
|
| 305 |
Json RenderJson(); |
| 306 |
|
| 307 |
static absl::string_view ChannelArgName() { |
| 308 |
return GRPC_ARG_CHANNELZ_SECURITY; |
| 309 |
} |
| 310 |
|
| 311 |
static int ChannelArgsCompare(const Security* a, const Security* b) { |
| 312 |
return QsortCompare(a, b); |
| 313 |
} |
| 314 |
|
| 315 |
grpc_arg MakeChannelArg() const; |
| 316 |
|
| 317 |
static RefCountedPtr<Security> GetFromChannelArgs( |
| 318 |
const grpc_channel_args* args); |
| 319 |
}; |
| 320 |
|
| 321 |
SocketNode(std::string local, std::string remote, std::string name, |
| 322 |
RefCountedPtr<Security> security); |
| 323 |
~SocketNode() override {} |
| 324 |
|
| 325 |
Json RenderJson() override; |
| 326 |
|
| 327 |
void RecordStreamStartedFromLocal(); |
| 328 |
void RecordStreamStartedFromRemote(); |
| 329 |
void RecordStreamSucceeded() { |
| 330 |
streams_succeeded_.fetch_add(1, std::memory_order_relaxed); |
| 331 |
} |
| 332 |
void RecordStreamFailed() { |
| 333 |
streams_failed_.fetch_add(1, std::memory_order_relaxed); |
| 334 |
} |
| 335 |
void RecordMessagesSent(uint32_t num_sent); |
| 336 |
void RecordMessageReceived(); |
| 337 |
void RecordKeepaliveSent() { |
| 338 |
keepalives_sent_.fetch_add(1, std::memory_order_relaxed); |
| 339 |
} |
| 340 |
|
| 341 |
const std::string& remote() { return remote_; } |
| 342 |
|
| 343 |
private: |
| 344 |
std::atomic<int64_t> streams_started_{0}; |
| 345 |
std::atomic<int64_t> streams_succeeded_{0}; |
| 346 |
std::atomic<int64_t> streams_failed_{0}; |
| 347 |
std::atomic<int64_t> messages_sent_{0}; |
| 348 |
std::atomic<int64_t> messages_received_{0}; |
| 349 |
std::atomic<int64_t> keepalives_sent_{0}; |
| 350 |
std::atomic<gpr_cycle_counter> last_local_stream_created_cycle_{0}; |
| 351 |
std::atomic<gpr_cycle_counter> last_remote_stream_created_cycle_{0}; |
| 352 |
std::atomic<gpr_cycle_counter> last_message_sent_cycle_{0}; |
| 353 |
std::atomic<gpr_cycle_counter> last_message_received_cycle_{0}; |
| 354 |
std::string local_; |
| 355 |
std::string remote_; |
| 356 |
RefCountedPtr<Security> const security_; |
| 357 |
}; |
| 358 |
|
| 359 |
// Handles channelz bookkeeping for listen sockets |
| 360 |
class ListenSocketNode : public BaseNode { |
| 361 |
public: |
| 362 |
ListenSocketNode(std::string local_addr, std::string name); |
| 363 |
~ListenSocketNode() override {} |
| 364 |
|
| 365 |
Json RenderJson() override; |
| 366 |
|
| 367 |
private: |
| 368 |
std::string local_addr_; |
| 369 |
}; |
| 370 |
|
| 371 |
} // namespace channelz |
| 372 |
} // namespace grpc_core |
| 373 |
|
| 374 |
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_H */ |