• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/impl/codegen/port_platform.h>
23 
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"
29 
30 namespace grpc_core {
31 namespace channelz {
32 
33 namespace testing {
34 size_t GetSizeofTraceEvent(void);
35 }
36 
37 class BaseNode;
38 
39 // Object used to hold live data for a channel. This data is exposed via the
40 // channelz service:
41 // https://github.com/grpc/proposal/blob/master/A14-channelz.md
42 class ChannelTrace {
43  public:
44   explicit ChannelTrace(size_t max_event_memory);
45   ~ChannelTrace();
46 
47   enum Severity {
48     Unset = 0,  // never to be used
49     Info,       // we start at 1 to avoid using proto default values
50     Warning,
51     Error
52   };
53 
54   // Adds a new trace event to the tracing object
55   //
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.
61   //
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
64   // slice.
65   void AddTraceEvent(Severity severity, const grpc_slice& data);
66 
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.
71   //
72   // NOTE: see the note in the method above.
73   //
74   // TODO(ncteisen): see the todo in the method above.
75   void AddTraceEventWithReference(Severity severity, const grpc_slice& data,
76                                   RefCountedPtr<BaseNode> referenced_entity);
77 
78   // Creates and returns the raw Json object, so a parent channelz
79   // object may incorporate the json before rendering.
80   Json RenderJson() const;
81 
82  private:
83   friend size_t testing::GetSizeofTraceEvent(void);
84 
85   // Private class to encapsulate all the data and bookkeeping needed for a
86   // a trace event.
87   class TraceEvent {
88    public:
89     // Constructor for a TraceEvent that references a channel.
90     TraceEvent(Severity severity, const grpc_slice& data,
91                RefCountedPtr<BaseNode> referenced_entity_);
92 
93     // Constructor for a TraceEvent that does not reverence a different
94     // channel.
95     TraceEvent(Severity severity, const grpc_slice& data);
96 
97     ~TraceEvent();
98 
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     Json RenderTraceEvent() const;
102 
103     // set and get for the next_ pointer.
next()104     TraceEvent* next() const { return next_; }
set_next(TraceEvent * next)105     void set_next(TraceEvent* next) { next_ = next; }
106 
memory_usage()107     size_t memory_usage() const { return memory_usage_; }
108 
109    private:
110     Severity severity_;
111     grpc_slice data_;
112     gpr_timespec timestamp_;
113     TraceEvent* next_;
114     // the tracer object for the (sub)channel that this trace event refers to.
115     RefCountedPtr<BaseNode> referenced_entity_;
116     size_t memory_usage_;
117   };  // TraceEvent
118 
119   // Internal helper to add and link in a trace event
120   void AddTraceEventHelper(TraceEvent* new_trace_event);
121 
122   gpr_mu tracer_mu_;
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_;
129 };
130 
131 }  // namespace channelz
132 }  // namespace grpc_core
133 
134 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_TRACE_H */
135