channel_trace.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <algorithm>
24 #include <map>
25 #include <string>
26 #include <utility>
27 
28 #include <grpc/support/alloc.h>
29 
36 
37 namespace grpc_core {
38 namespace channelz {
39 
41  RefCountedPtr<BaseNode> referenced_entity)
42  : severity_(severity),
43  data_(data),
44  timestamp_(ExecCtx::Get()->Now().as_timespec(GPR_CLOCK_REALTIME)),
45  next_(nullptr),
46  referenced_entity_(std::move(referenced_entity)),
48 
50  : severity_(severity),
51  data_(data),
52  timestamp_(ExecCtx::Get()->Now().as_timespec(GPR_CLOCK_REALTIME)),
53  next_(nullptr),
55 
57 
58 ChannelTrace::ChannelTrace(size_t max_event_memory)
59  : num_events_logged_(0),
61  max_event_memory_(max_event_memory),
62  head_trace_(nullptr),
63  tail_trace_(nullptr) {
64  if (max_event_memory_ == 0) {
65  return; // tracing is disabled if max_event_memory_ == 0
66  }
69 }
70 
72  if (max_event_memory_ == 0) {
73  return; // tracing is disabled if max_event_memory_ == 0
74  }
76  while (it != nullptr) {
77  TraceEvent* to_free = it;
78  it = it->next();
79  delete to_free;
80  }
82 }
83 
86  // first event case
87  if (head_trace_ == nullptr) {
88  head_trace_ = tail_trace_ = new_trace_event;
89  }
90  // regular event add case
91  else {
92  tail_trace_->set_next(new_trace_event);
94  }
95  event_list_memory_usage_ += new_trace_event->memory_usage();
96  // maybe garbage collect the tail until we are under the memory limit.
98  TraceEvent* to_free = head_trace_;
101  delete to_free;
102  }
103 }
104 
106  if (max_event_memory_ == 0) {
108  return; // tracing is disabled if max_event_memory_ == 0
109  }
111 }
112 
115  RefCountedPtr<BaseNode> referenced_entity) {
116  if (max_event_memory_ == 0) {
118  return; // tracing is disabled if max_event_memory_ == 0
119  }
120  // create and fill up the new event
122  new TraceEvent(severity, data, std::move(referenced_entity)));
123 }
124 
125 namespace {
126 
127 const char* severity_string(ChannelTrace::Severity severity) {
128  switch (severity) {
129  case ChannelTrace::Severity::Info:
130  return "CT_INFO";
131  case ChannelTrace::Severity::Warning:
132  return "CT_WARNING";
133  case ChannelTrace::Severity::Error:
134  return "CT_ERROR";
135  default:
136  GPR_UNREACHABLE_CODE(return "CT_UNKNOWN");
137  }
138 }
139 
140 } // anonymous namespace
141 
144  Json::Object object = {
145  {"description", description},
146  {"severity", severity_string(severity_)},
147  {"timestamp", gpr_format_timespec(timestamp_)},
148  };
150  if (referenced_entity_ != nullptr) {
151  const bool is_channel =
154  object[is_channel ? "channelRef" : "subchannelRef"] = Json::Object{
155  {(is_channel ? "channelId" : "subchannelId"),
157  };
158  }
159  return object;
160 }
161 
163  // Tracing is disabled if max_event_memory_ == 0.
164  if (max_event_memory_ == 0) {
165  return Json(); // JSON null
166  }
167  Json::Object object = {
168  {"creationTimestamp", gpr_format_timespec(time_created_)},
169  };
170  if (num_events_logged_ > 0) {
171  object["numEventsLogged"] = std::to_string(num_events_logged_);
172  }
173  // Only add in the event list if it is non-empty.
174  if (head_trace_ != nullptr) {
176  for (TraceEvent* it = head_trace_; it != nullptr; it = it->next()) {
177  array.emplace_back(it->RenderTraceEvent());
178  }
179  object["events"] = std::move(array);
180  }
181  return object;
182 }
183 
184 } // namespace channelz
185 } // namespace grpc_core
grpc_core::Json::Array
std::vector< Json > Array
Definition: src/core/lib/json/json.h:55
grpc_core::channelz::ChannelTrace::TraceEvent::memory_usage
size_t memory_usage() const
Definition: channel_trace.h:111
grpc_core::channelz::ChannelTrace::num_events_logged_
uint64_t num_events_logged_
Definition: channel_trace.h:127
cleanup.Json
Json
Definition: cleanup.py:49
regen-readme.it
it
Definition: regen-readme.py:15
grpc_core::channelz::ChannelTrace::tracer_mu_
gpr_mu tracer_mu_
Definition: channel_trace.h:126
bloat_diff.severity
def severity
Definition: bloat_diff.py:143
grpc_core::channelz::ChannelTrace::AddTraceEventWithReference
void AddTraceEventWithReference(Severity severity, const grpc_slice &data, RefCountedPtr< BaseNode > referenced_entity)
Definition: channel_trace.cc:113
grpc_core::channelz::ChannelTrace::TraceEvent::referenced_entity_
RefCountedPtr< BaseNode > referenced_entity_
Definition: channel_trace.h:119
grpc_core::channelz::ChannelTrace::TraceEvent::RenderTraceEvent
Json RenderTraceEvent() const
Definition: channel_trace.cc:142
grpc_core::channelz::ChannelTrace::RenderJson
Json RenderJson() const
Definition: channel_trace.cc:162
grpc_core
Definition: call_metric_recorder.h:31
setup.description
description
Definition: setup.py:544
string.h
array
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern array
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/array.c:111
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_core::channelz::ChannelTrace::TraceEvent::set_next
void set_next(TraceEvent *next)
Definition: channel_trace.h:109
memory_usage_
MemoryUsage memory_usage_
Definition: abseil-cpp/absl/strings/internal/cordz_info.cc:225
grpc_core::channelz::BaseNode::EntityType::kTopLevelChannel
@ kTopLevelChannel
channelz.h
grpc_core::channelz::ChannelTrace::time_created_
gpr_timespec time_created_
Definition: channel_trace.h:132
grpc_core::channelz::ChannelTrace::AddTraceEventHelper
void AddTraceEventHelper(TraceEvent *new_trace_event)
Definition: channel_trace.cc:84
grpc_core::channelz::ChannelTrace::max_event_memory_
size_t max_event_memory_
Definition: channel_trace.h:129
absl::synchronization_internal::Get
static GraphId Get(const IdMap &id, int num)
Definition: abseil-cpp/absl/synchronization/internal/graphcycles_test.cc:44
grpc_core::channelz::ChannelTrace::~ChannelTrace
~ChannelTrace()
Definition: channel_trace.cc:71
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
grpc_core::channelz::ChannelTrace::TraceEvent::timestamp_
gpr_timespec timestamp_
Definition: channel_trace.h:116
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
array
Definition: undname.c:101
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc_slice_memory_usage
size_t grpc_slice_memory_usage(grpc_slice s)
Definition: slice/slice.cc:76
grpc_core::channelz::ChannelTrace::event_list_memory_usage_
size_t event_list_memory_usage_
Definition: channel_trace.h:128
gpr_format_timespec
std::string gpr_format_timespec(gpr_timespec tm)
Definition: string.cc:55
time.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
grpc_core::channelz::ChannelTrace::Severity
Severity
Definition: channel_trace.h:51
grpc_core::channelz::ChannelTrace::head_trace_
TraceEvent * head_trace_
Definition: channel_trace.h:130
slice_internal.h
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: impl/codegen/port_platform.h:652
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::channelz::ChannelTrace::TraceEvent::TraceEvent
TraceEvent(Severity severity, const grpc_slice &data, RefCountedPtr< BaseNode > referenced_entity_)
Definition: channel_trace.cc:40
grpc_core::channelz::BaseNode::EntityType::kInternalChannel
@ kInternalChannel
grpc_core::channelz::ChannelTrace::TraceEvent::~TraceEvent
~TraceEvent()
Definition: channel_trace.cc:56
grpc_slice_to_c_string
GPRAPI char * grpc_slice_to_c_string(grpc_slice s)
Definition: slice/slice.cc:35
grpc_core::Json::Object
std::map< std::string, Json > Object
Definition: src/core/lib/json/json.h:54
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
alloc.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::channelz::ChannelTrace::TraceEvent::severity_
Severity severity_
Definition: channel_trace.h:114
exec_ctx.h
data_
std::string data_
Definition: cord_rep_btree_navigator_test.cc:84
grpc_core::channelz::ChannelTrace::tail_trace_
TraceEvent * tail_trace_
Definition: channel_trace.h:131
channelz
void channelz(grpc_end2end_test_config config)
Definition: test/core/end2end/tests/channelz.cc:318
slice_refcount.h
grpc_core::channelz::ChannelTrace::TraceEvent
Definition: channel_trace.h:91
grpc_core::channelz::ChannelTrace::TraceEvent::next
TraceEvent * next() const
Definition: channel_trace.h:108
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
channel_trace.h
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc_core::channelz::ChannelTrace::ChannelTrace
ChannelTrace(size_t max_event_memory)
Definition: channel_trace.cc:58
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
grpc_core::Timestamp::as_timespec
gpr_timespec as_timespec(gpr_clock_type type) const
Definition: src/core/lib/gprpp/time.cc:157
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
grpc_core::channelz::ChannelTrace::TraceEvent::data_
grpc_slice data_
Definition: channel_trace.h:115
port_platform.h
grpc_core::channelz::ChannelTrace::AddTraceEvent
void AddTraceEvent(Severity severity, const grpc_slice &data)
Definition: channel_trace.cc:105


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52