Debian Codegraph @

src/core/lib/iomgr/buffer_list.cc

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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/buffer_list.h"
22
23 #include <grpc/support/log.h>
24
25 #include "src/core/lib/gprpp/sync.h"
26 #include "src/core/lib/iomgr/port.h"
27
28 #ifdef GRPC_LINUX_ERRQUEUE
29 #include <netinet/in.h>
30 #include <string.h>
31 #include <time.h>
32
33 namespace grpc_core {
34 namespace {
35 // Fills gpr_timespec gts based on values from timespec ts.
36 void FillGprFromTimestamp(gpr_timespec* gts, const struct timespec* ts) {
37 gts->tv_sec = ts->tv_sec;
38 gts->tv_nsec = static_cast<int32_t>(ts->tv_nsec);
39 gts->clock_type = GPR_CLOCK_REALTIME;
40 }
41
42 void DefaultTimestampsCallback(void* /*arg*/, Timestamps* /*ts*/,
43 absl::Status /*shudown_err*/) {
44 gpr_log(GPR_DEBUG, "Timestamps callback has not been registered");
45 }
46
47 // The saved callback function that will be invoked when we get all the
48 // timestamps that we are going to get for a TracedBuffer.
49 void (*g_timestamps_callback)(void*, Timestamps*,
50 grpc_error_handle shutdown_err) =
51 DefaultTimestampsCallback;
52
53 // Used to extract individual opt stats from cmsg, so as to avoid troubles with
54 // unaligned reads.
55 template <typename T>
56 T ReadUnaligned(const void* ptr) {
57 T val;
58 memcpy(&val, ptr, sizeof(val));
59 return val;
60 }
61
62 // Extracts opt stats from the tcp_info struct \a info to \a metrics
63 void ExtractOptStatsFromTcpInfo(ConnectionMetrics* metrics,
64 const tcp_info* info) {
65 if (info == nullptr) {
66 return;
67 }
68 if (info->length > offsetof(tcp_info, tcpi_sndbuf_limited)) {
69 metrics->recurring_retrans = info->tcpi_retransmits;
70 metrics->is_delivery_rate_app_limited =
71 info->tcpi_delivery_rate_app_limited;
72 metrics->congestion_window = info->tcpi_snd_cwnd;
73 metrics->reordering = info->tcpi_reordering;
74 metrics->packet_retx = info->tcpi_total_retrans;
75 metrics->pacing_rate = info->tcpi_pacing_rate;
76 metrics->data_notsent = info->tcpi_notsent_bytes;
77 if (info->tcpi_min_rtt != UINT32_MAX) {
78 metrics->min_rtt = info->tcpi_min_rtt;
79 }
80 metrics->packet_sent = info->tcpi_data_segs_out;
81 metrics->delivery_rate = info->tcpi_delivery_rate;
82 metrics->busy_usec = info->tcpi_busy_time;
83 metrics->rwnd_limited_usec = info->tcpi_rwnd_limited;
84 metrics->sndbuf_limited_usec = info->tcpi_sndbuf_limited;
85 }
86 if (info->length > offsetof(tcp_info, tcpi_dsack_dups)) {
87 metrics->data_sent = info->tcpi_bytes_sent;
88 metrics->data_retx = info->tcpi_bytes_retrans;
89 metrics->packet_spurious_retx = info->tcpi_dsack_dups;
90 }
91 }
92
93 // Extracts opt stats from the given control message \a opt_stats to the
94 // connection metrics \a metrics.
95 void ExtractOptStatsFromCmsg(ConnectionMetrics* metrics,
96 const cmsghdr* opt_stats) {
97 if (opt_stats == nullptr) {
98 return;
99 }
100 const auto* data = CMSG_DATA(opt_stats);
101 constexpr int64_t cmsg_hdr_len = CMSG_ALIGN(sizeof(struct cmsghdr));
102 const int64_t len = opt_stats->cmsg_len - cmsg_hdr_len;
103 int64_t offset = 0;
104
105 while (offset < len) {
106 const auto* attr = reinterpret_cast<const nlattr*>(data + offset);
107 const void* val = data + offset + NLA_HDRLEN;
108 switch (attr->nla_type) {
109 case TCP_NLA_BUSY: {
110 metrics->busy_usec = ReadUnaligned<uint64_t>(val);
111 break;
112 }
113 case TCP_NLA_RWND_LIMITED: {
114 metrics->rwnd_limited_usec = ReadUnaligned<uint64_t>(val);
115 break;
116 }
117 case TCP_NLA_SNDBUF_LIMITED: {
118 metrics->sndbuf_limited_usec = ReadUnaligned<uint64_t>(val);
119 break;
120 }
121 case TCP_NLA_PACING_RATE: {
122 metrics->pacing_rate = ReadUnaligned<uint64_t>(val);
123 break;
124 }
125 case TCP_NLA_DELIVERY_RATE: {
126 metrics->delivery_rate = ReadUnaligned<uint64_t>(val);
127 break;
128 }
129 case TCP_NLA_DELIVERY_RATE_APP_LMT: {
130 metrics->is_delivery_rate_app_limited = ReadUnaligned<uint8_t>(val);
131 break;
132 }
133 case TCP_NLA_SND_CWND: {
134 metrics->congestion_window = ReadUnaligned<uint32_t>(val);
135 break;
136 }
137 case TCP_NLA_MIN_RTT: {
138 metrics->min_rtt = ReadUnaligned<uint32_t>(val);
139 break;
140 }
141 case TCP_NLA_SRTT: {
142 metrics->srtt = ReadUnaligned<uint32_t>(val);
143 break;
144 }
145 case TCP_NLA_RECUR_RETRANS: {
146 metrics->recurring_retrans = ReadUnaligned<uint8_t>(val);
147 break;
148 }
149 case TCP_NLA_BYTES_SENT: {
150 metrics->data_sent = ReadUnaligned<uint64_t>(val);
151 break;
152 }
153 case TCP_NLA_DATA_SEGS_OUT: {
154 metrics->packet_sent = ReadUnaligned<uint64_t>(val);
155 break;
156 }
157 case TCP_NLA_TOTAL_RETRANS: {
158 metrics->packet_retx = ReadUnaligned<uint64_t>(val);
159 break;
160 }
161 case TCP_NLA_DELIVERED: {
162 metrics->packet_delivered = ReadUnaligned<uint32_t>(val);
163 break;
164 }
165 case TCP_NLA_DELIVERED_CE: {
166 metrics->packet_delivered_ce = ReadUnaligned<uint32_t>(val);
167 break;
168 }
169 case TCP_NLA_BYTES_RETRANS: {
170 metrics->data_retx = ReadUnaligned<uint64_t>(val);
171 break;
172 }
173 case TCP_NLA_DSACK_DUPS: {
174 metrics->packet_spurious_retx = ReadUnaligned<uint32_t>(val);
175 break;
176 }
177 case TCP_NLA_REORDERING: {
178 metrics->reordering = ReadUnaligned<uint32_t>(val);
179 break;
180 }
181 case TCP_NLA_SND_SSTHRESH: {
182 metrics->snd_ssthresh = ReadUnaligned<uint32_t>(val);
183 break;
184 }
185 }
186 offset += NLA_ALIGN(attr->nla_len);
187 }
188 }
189
190 int GetSocketTcpInfo(struct tcp_info* info, int fd) {
191 memset(info, 0, sizeof(*info));
192 info->length = offsetof(tcp_info, length);
193 return getsockopt(fd, IPPROTO_TCP, TCP_INFO, info, &(info->length));
194 }
195
196 } // namespace.
197
198 void TracedBufferList::AddNewEntry(int32_t seq_no, int fd, void* arg) {
199 TracedBuffer* new_elem = new TracedBuffer(seq_no, arg);
200 // Store the current time as the sendmsg time.
201 new_elem->ts_.sendmsg_time.time = gpr_now(GPR_CLOCK_REALTIME);
202 new_elem->ts_.scheduled_time.time = gpr_inf_past(GPR_CLOCK_REALTIME);
203 new_elem->ts_.sent_time.time = gpr_inf_past(GPR_CLOCK_REALTIME);
204 new_elem->ts_.acked_time.time = gpr_inf_past(GPR_CLOCK_REALTIME);
205 if (GetSocketTcpInfo(&(new_elem->ts_.info), fd) == 0) {
206 ExtractOptStatsFromTcpInfo(&(new_elem->ts_.sendmsg_time.metrics),
207 &(new_elem->ts_.info));
208 }
209 MutexLock lock(&mu_);
210 if (!head_) {
211 head_ = tail_ = new_elem;
212 } else {
213 tail_->next_ = new_elem;
214 tail_ = new_elem;
215 }
216 }
217
218 void TracedBufferList::ProcessTimestamp(struct sock_extended_err* serr,
219 struct cmsghdr* opt_stats,
220 struct scm_timestamping* tss) {
221 MutexLock lock(&mu_);
222 TracedBuffer* elem = head_;
223 while (elem != nullptr) {
224 // The byte number refers to the sequence number of the last byte which this
225 // timestamp relates to.
226 if (serr->ee_data >= elem->seq_no_) {
227 switch (serr->ee_info) {
228 case SCM_TSTAMP_SCHED:
229 FillGprFromTimestamp(&(elem->ts_.scheduled_time.time), &(tss->ts[0]));
230 ExtractOptStatsFromCmsg(&(elem->ts_.scheduled_time.metrics),
231 opt_stats);
232 elem = elem->next_;
233 break;
234 case SCM_TSTAMP_SND:
235 FillGprFromTimestamp(&(elem->ts_.sent_time.time), &(tss->ts[0]));
236 ExtractOptStatsFromCmsg(&(elem->ts_.sent_time.metrics), opt_stats);
237 elem = elem->next_;
238 break;
239 case SCM_TSTAMP_ACK:
240 FillGprFromTimestamp(&(elem->ts_.acked_time.time), &(tss->ts[0]));
241 ExtractOptStatsFromCmsg(&(elem->ts_.acked_time.metrics), opt_stats);
242 // Got all timestamps. Do the callback and free this TracedBuffer. The
243 // thing below can be passed by value if we don't want the restriction
244 // on the lifetime.
245 g_timestamps_callback(elem->arg_, &(elem->ts_), absl::OkStatus());
246 // Safe to update head_ to elem->next_ because the list is ordered by
247 // seq_no. Thus if elem is to be deleted, it has to be the first
248 // element in the list.
249 head_ = elem->next_;
250 delete elem;
251 elem = head_;
252 break;
253 default:
254 abort();
255 }
256 } else {
257 break;
258 }
259 }
260 tail_ = !head_ ? head_ : tail_;
261 }
262
263 void TracedBufferList::Shutdown(void* remaining, absl::Status shutdown_err) {
264 MutexLock lock(&mu_);
265 while (head_) {
266 TracedBuffer* elem = head_;
267 g_timestamps_callback(elem->arg_, &(elem->ts_), shutdown_err);
268 head_ = head_->next_;
269 delete elem;
270 }
271 if (remaining != nullptr) {
272 g_timestamps_callback(remaining, nullptr, shutdown_err);
273 }
274 tail_ = head_;
275 }
276
277 void grpc_tcp_set_write_timestamps_callback(
278 void (*fn)(void*, Timestamps*, grpc_error_handle error)) {
279 g_timestamps_callback = fn;
280 }
281 } /* namespace grpc_core */
282
283 #else /* GRPC_LINUX_ERRQUEUE */
284
285 namespace grpc_core {
286 void grpc_tcp_set_write_timestamps_callback(
287 void (*fn)(void*, Timestamps*, grpc_error_handle error)) {
288 // Cast value of fn to void to avoid unused parameter warning.
289 // Can't comment out the name because some compilers and formatters don't
290 // like the sequence */* , which would arise from */*fn*/.
291 (void)fn;
292 gpr_log(GPR_DEBUG, "Timestamps callback is not enabled for this platform");
293 }
294 } // namespace grpc_core
295
296 #endif /* GRPC_LINUX_ERRQUEUE */