3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H
20 #define GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H
22 #include <grpc/impl/codegen/port_platform.h>
24 #include <grpc/grpc.h>
25 #include "src/core/lib/gprpp/ref_counted.h"
26 #include "src/core/lib/gprpp/ref_counted_ptr.h"
27 #include "src/core/lib/iomgr/error.h"
28 #include "src/core/lib/json/json.h"
34 size_t GetSizeofTraceEvent(void);
39 // Object used to hold live data for a channel. This data is exposed via the
41 // https://github.com/grpc/proposal/blob/master/A14-channelz.md
44 ChannelTrace(size_t max_event_memory);
48 Unset = 0, // never to be used
49 Info, // we start at 1 to avoid using proto default values
54 // Adds a new trace event to the tracing object
56 // NOTE: each ChannelTrace tracks the memory used by its list of trace
57 // events, so adding an event with a large amount of data could cause other
58 // trace event to be evicted. If a single trace is larger than the limit, it
59 // will cause all events to be evicted. The limit is set with the arg:
60 // GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE.
62 // TODO(ncteisen): as this call is used more and more throughout the gRPC
63 // stack, determine if it makes more sense to accept a char* instead of a
65 void AddTraceEvent(Severity severity, const grpc_slice& data);
67 // Adds a new trace event to the tracing object. This trace event refers to a
68 // an event that concerns a different channelz entity. For example, if this
69 // channel has created a new subchannel, then it would record that with
70 // a TraceEvent referencing the new subchannel.
72 // NOTE: see the note in the method above.
74 // TODO(ncteisen): see the todo in the method above.
75 void AddTraceEventWithReference(Severity severity, const grpc_slice& data,
76 RefCountedPtr<BaseNode> referenced_entity);
78 // Creates and returns the raw grpc_json object, so a parent channelz
79 // object may incorporate the json before rendering.
80 grpc_json* RenderJson() const;
83 friend size_t testing::GetSizeofTraceEvent(void);
85 // Private class to encapsulate all the data and bookkeeping needed for a
89 // Constructor for a TraceEvent that references a channel.
90 TraceEvent(Severity severity, const grpc_slice& data,
91 RefCountedPtr<BaseNode> referenced_entity_);
93 // Constructor for a TraceEvent that does not reverence a different
95 TraceEvent(Severity severity, const grpc_slice& data);
99 // Renders the data inside of this TraceEvent into a json object. This is
100 // used by the ChannelTrace, when it is rendering itself.
101 void RenderTraceEvent(grpc_json* json) const;
103 // set and get for the next_ pointer.
104 TraceEvent* next() const { return next_; }
105 void set_next(TraceEvent* next) { next_ = next; }
107 size_t memory_usage() const { return memory_usage_; }
112 gpr_timespec timestamp_;
114 // the tracer object for the (sub)channel that this trace event refers to.
115 RefCountedPtr<BaseNode> referenced_entity_;
116 size_t memory_usage_;
119 // Internal helper to add and link in a trace event
120 void AddTraceEventHelper(TraceEvent* new_trace_event);
123 uint64_t num_events_logged_;
124 size_t event_list_memory_usage_;
125 size_t max_event_memory_;
126 TraceEvent* head_trace_;
127 TraceEvent* tail_trace_;
128 gpr_timespec time_created_;
131 } // namespace channelz
132 } // namespace grpc_core
134 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H */