Debian Codegraph @

src/core/lib/iomgr/buffer_list.h

raw
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_IOMGR_BUFFER_LIST_H
20 #define GRPC_CORE_LIB_IOMGR_BUFFER_LIST_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include "absl/types/optional.h"
25
26 #include <grpc/support/time.h>
27
28 #include "src/core/lib/gprpp/sync.h"
29 #include "src/core/lib/iomgr/error.h"
30 #include "src/core/lib/iomgr/internal_errqueue.h"
31 #include "src/core/lib/iomgr/port.h"
32
33 namespace grpc_core {
34
35 struct ConnectionMetrics {
36 /* Delivery rate in Bytes/s. */
37 absl::optional<uint64_t> delivery_rate;
38 /* If the delivery rate is limited by the application, this is set to true. */
39 absl::optional<bool> is_delivery_rate_app_limited;
40 /* Total packets retransmitted. */
41 absl::optional<uint32_t> packet_retx;
42 /* Total packets retransmitted spuriously. This metric is smaller than or
43 equal to packet_retx. */
44 absl::optional<uint32_t> packet_spurious_retx;
45 /* Total packets sent. */
46 absl::optional<uint32_t> packet_sent;
47 /* Total packets delivered. */
48 absl::optional<uint32_t> packet_delivered;
49 /* Total packets delivered with ECE marked. This metric is smaller than or
50 equal to packet_delivered. */
51 absl::optional<uint32_t> packet_delivered_ce;
52 /* Total bytes lost so far. */
53 absl::optional<uint64_t> data_retx;
54 /* Total bytes sent so far. */
55 absl::optional<uint64_t> data_sent;
56 /* Total bytes in write queue but not sent. */
57 absl::optional<uint64_t> data_notsent;
58 /* Pacing rate of the connection in Bps */
59 absl::optional<uint64_t> pacing_rate;
60 /* Minimum RTT observed in usec. */
61 absl::optional<uint32_t> min_rtt;
62 /* Smoothed RTT in usec */
63 absl::optional<uint32_t> srtt;
64 /* Send congestion window. */
65 absl::optional<uint32_t> congestion_window;
66 /* Slow start threshold in packets. */
67 absl::optional<uint32_t> snd_ssthresh;
68 /* Maximum degree of reordering (i.e., maximum number of packets reodered)
69 on the connection. */
70 absl::optional<uint32_t> reordering;
71 /* Represents the number of recurring retransmissions of the first sequence
72 that is not acknowledged yet. */
73 absl::optional<uint8_t> recurring_retrans;
74 /* The cumulative time (in usec) that the transport protocol was busy
75 sending data. */
76 absl::optional<uint64_t> busy_usec;
77 /* The cumulative time (in usec) that the transport protocol was limited by
78 the receive window size. */
79 absl::optional<uint64_t> rwnd_limited_usec;
80 /* The cumulative time (in usec) that the transport protocol was limited by
81 the send buffer size. */
82 absl::optional<uint64_t> sndbuf_limited_usec;
83 };
84
85 struct BufferTimestamp {
86 gpr_timespec time;
87 ConnectionMetrics metrics; /* Metrics collected with this timestamp */
88 };
89
90 struct Timestamps {
91 BufferTimestamp sendmsg_time;
92 BufferTimestamp scheduled_time;
93 BufferTimestamp sent_time;
94 BufferTimestamp acked_time;
95
96 uint32_t byte_offset; /* byte offset relative to the start of the RPC */
97
98 #ifdef GRPC_LINUX_ERRQUEUE
99 tcp_info info; /* tcp_info collected on sendmsg */
100 #endif /* GRPC_LINUX_ERRQUEUE */
101 };
102
103 // TracedBuffer is a class to keep track of timestamps for a specific buffer in
104 // the TCP layer. We are only tracking timestamps for Linux kernels and hence
105 // this class would only be used by Linux platforms. For all other platforms,
106 // TracedBuffer would be an empty class.
107 // The timestamps collected are according to Timestamps declared above A
108 // TracedBuffer list is kept track of using the head element of the list. If
109 // *the head element of the list is nullptr, then the list is empty.
110 #ifdef GRPC_LINUX_ERRQUEUE
111
112 class TracedBufferList {
113 public:
114 // Add a new entry in the TracedBuffer list pointed to by head. Also saves
115 // sendmsg_time with the current timestamp.
116 void AddNewEntry(int32_t seq_no, int fd, void* arg);
117 // Processes a received timestamp based on sock_extended_err and
118 // scm_timestamping structures. It will invoke the timestamps callback if the
119 // timestamp type is SCM_TSTAMP_ACK.
120 void ProcessTimestamp(struct sock_extended_err* serr,
121 struct cmsghdr* opt_stats,
122 struct scm_timestamping* tss);
123 // The Size() operation is slow and is used only in tests.
124 int Size() {
125 MutexLock lock(&mu_);
126 int size = 0;
127 TracedBuffer* curr = head_;
128 while (curr) {
129 ++size;
130 curr = curr->next_;
131 }
132 return size;
133 }
134 // Cleans the list by calling the callback for each traced buffer in the list
135 // with timestamps that it has.
136 void Shutdown(void* /*remaining*/, absl::Status /*shutdown_err*/);
137
138 private:
139 class TracedBuffer {
140 public:
141 TracedBuffer(uint32_t seq_no, void* arg) : seq_no_(seq_no), arg_(arg) {}
142
143 private:
144 friend class TracedBufferList;
145 TracedBuffer* next_ = nullptr;
146 uint32_t seq_no_; /* The sequence number for the last byte in the buffer */
147 void* arg_; /* The arg to pass to timestamps_callback */
148 Timestamps ts_; /* The timestamps corresponding to this buffer */
149 };
150 Mutex mu_;
151 // TracedBuffers are ordered by sequence number and would need to be processed
152 // in a FIFO order starting with the smallest sequence number. To enable this,
153 // they are stored in a singly linked with head and tail pointers which allows
154 // easy appends and forward iteration operations.
155 TracedBuffer* head_ = nullptr;
156 TracedBuffer* tail_ = nullptr;
157 };
158
159 #else /* GRPC_LINUX_ERRQUEUE */
160 // TracedBufferList implementation is a no-op for this platform.
161 class TracedBufferList {
162 public:
163 void AddNewEntry(int32_t /*seq_no*/, int /*fd*/, void* /*arg*/) {}
164 void ProcessTimestamp(struct sock_extended_err* /*serr*/,
165 struct cmsghdr* /*opt_stats*/,
166 struct scm_timestamping* /*tss*/) {}
167 int Size() { return 0; }
168 void Shutdown(void* /*remaining*/, absl::Status /*shutdown_err*/) {}
169 };
170 #endif /* GRPC_LINUX_ERRQUEUE */
171
172 /** Sets the callback function to call when timestamps for a write are
173 * collected. The callback does not own a reference to error. */
174 void grpc_tcp_set_write_timestamps_callback(
175 void (*fn)(void*, Timestamps*, grpc_error_handle error));
176
177 } /* namespace grpc_core */
178
179 #endif /* GRPC_CORE_LIB_IOMGR_BUFFER_LIST_H */