| 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 |
#ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H |
| 20 |
#define GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stddef.h> |
| 25 |
#include <stdint.h> |
| 26 |
|
| 27 |
#include <grpc/impl/codegen/gpr_types.h> |
| 28 |
#include <grpc/slice.h> |
| 29 |
#include <grpc/support/sync.h> |
| 30 |
|
| 31 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 32 |
#include "src/core/lib/json/json.h" |
| 33 |
|
| 34 |
namespace grpc_core { |
| 35 |
namespace channelz { |
| 36 |
|
| 37 |
namespace testing { |
| 38 |
size_t GetSizeofTraceEvent(void); |
| 39 |
} |
| 40 |
|
| 41 |
class BaseNode; |
| 42 |
|
| 43 |
// Object used to hold live data for a channel. This data is exposed via the |
| 44 |
// channelz service: |
| 45 |
// https://github.com/grpc/proposal/blob/master/A14-channelz.md |
| 46 |
class ChannelTrace { |
| 47 |
public: |
| 48 |
explicit ChannelTrace(size_t max_event_memory); |
| 49 |
~ChannelTrace(); |
| 50 |
|
| 51 |
enum Severity { |
| 52 |
Unset = 0, // never to be used |
| 53 |
Info, // we start at 1 to avoid using proto default values |
| 54 |
Warning, |
| 55 |
Error |
| 56 |
}; |
| 57 |
|
| 58 |
// Adds a new trace event to the tracing object |
| 59 |
// |
| 60 |
// NOTE: each ChannelTrace tracks the memory used by its list of trace |
| 61 |
// events, so adding an event with a large amount of data could cause other |
| 62 |
// trace event to be evicted. If a single trace is larger than the limit, it |
| 63 |
// will cause all events to be evicted. The limit is set with the arg: |
| 64 |
// GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE. |
| 65 |
// |
| 66 |
// TODO(ncteisen): as this call is used more and more throughout the gRPC |
| 67 |
// stack, determine if it makes more sense to accept a char* instead of a |
| 68 |
// slice. |
| 69 |
void AddTraceEvent(Severity severity, const grpc_slice& data); |
| 70 |
|
| 71 |
// Adds a new trace event to the tracing object. This trace event refers to a |
| 72 |
// an event that concerns a different channelz entity. For example, if this |
| 73 |
// channel has created a new subchannel, then it would record that with |
| 74 |
// a TraceEvent referencing the new subchannel. |
| 75 |
// |
| 76 |
// NOTE: see the note in the method above. |
| 77 |
// |
| 78 |
// TODO(ncteisen): see the todo in the method above. |
| 79 |
void AddTraceEventWithReference(Severity severity, const grpc_slice& data, |
| 80 |
RefCountedPtr<BaseNode> referenced_entity); |
| 81 |
|
| 82 |
// Creates and returns the raw Json object, so a parent channelz |
| 83 |
// object may incorporate the json before rendering. |
| 84 |
Json RenderJson() const; |
| 85 |
|
| 86 |
private: |
| 87 |
friend size_t testing::GetSizeofTraceEvent(void); |
| 88 |
|
| 89 |
// Private class to encapsulate all the data and bookkeeping needed for a |
| 90 |
// a trace event. |
| 91 |
class TraceEvent { |
| 92 |
public: |
| 93 |
// Constructor for a TraceEvent that references a channel. |
| 94 |
TraceEvent(Severity severity, const grpc_slice& data, |
| 95 |
RefCountedPtr<BaseNode> referenced_entity_); |
| 96 |
|
| 97 |
// Constructor for a TraceEvent that does not reverence a different |
| 98 |
// channel. |
| 99 |
TraceEvent(Severity severity, const grpc_slice& data); |
| 100 |
|
| 101 |
~TraceEvent(); |
| 102 |
|
| 103 |
// Renders the data inside of this TraceEvent into a json object. This is |
| 104 |
// used by the ChannelTrace, when it is rendering itself. |
| 105 |
Json RenderTraceEvent() const; |
| 106 |
|
| 107 |
// set and get for the next_ pointer. |
| 108 |
TraceEvent* next() const { return next_; } |
| 109 |
void set_next(TraceEvent* next) { next_ = next; } |
| 110 |
|
| 111 |
size_t memory_usage() const { return memory_usage_; } |
| 112 |
|
| 113 |
private: |
| 114 |
Severity severity_; |
| 115 |
grpc_slice data_; |
| 116 |
gpr_timespec timestamp_; |
| 117 |
TraceEvent* next_; |
| 118 |
// the tracer object for the (sub)channel that this trace event refers to. |
| 119 |
RefCountedPtr<BaseNode> referenced_entity_; |
| 120 |
size_t memory_usage_; |
| 121 |
}; // TraceEvent |
| 122 |
|
| 123 |
// Internal helper to add and link in a trace event |
| 124 |
void AddTraceEventHelper(TraceEvent* new_trace_event); |
| 125 |
|
| 126 |
gpr_mu tracer_mu_; |
| 127 |
uint64_t num_events_logged_; |
| 128 |
size_t event_list_memory_usage_; |
| 129 |
size_t max_event_memory_; |
| 130 |
TraceEvent* head_trace_; |
| 131 |
TraceEvent* tail_trace_; |
| 132 |
gpr_timespec time_created_; |
| 133 |
}; |
| 134 |
|
| 135 |
} // namespace channelz |
| 136 |
} // namespace grpc_core |
| 137 |
|
| 138 |
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H */ |