blob: 0f66dcc8727cc551cfdaa032e221fb7cf5b68f77 [file] [log] [blame]
Yash Tibrewalf0397932018-05-31 19:39:52 -07001/*
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 "src/core/lib/iomgr/port.h"
25
26#include <grpc/support/time.h>
27
28#include "src/core/lib/gprpp/memory.h"
29#include "src/core/lib/iomgr/error.h"
30#include "src/core/lib/iomgr/internal_errqueue.h"
31
32namespace grpc_core {
33struct Timestamps {
34 gpr_timespec sendmsg_time;
35 gpr_timespec scheduled_time;
36 gpr_timespec sent_time;
37 gpr_timespec acked_time;
38};
39
Yash Tibrewal0d757a62018-07-16 16:21:43 -070040/** TracedBuffer is a class to keep track of timestamps for a specific buffer in
41 * the TCP layer. We are only tracking timestamps for Linux kernels and hence
42 * this class would only be used by Linux platforms. For all other platforms,
43 * TracedBuffer would be an empty class.
44 *
45 * The timestamps collected are according to grpc_core::Timestamps declared
46 * above.
47 *
48 * A TracedBuffer list is kept track of using the head element of the list. If
49 * the head element of the list is nullptr, then the list is empty.
50 */
Yash Tibrewalf0397932018-05-31 19:39:52 -070051#ifdef GRPC_LINUX_ERRQUEUE
52class TracedBuffer {
53 public:
Yash Tibrewal0d757a62018-07-16 16:21:43 -070054 /** Add a new entry in the TracedBuffer list pointed to by head. Also saves
55 * sendmsg_time with the current timestamp. */
Yash Tibrewalf0397932018-05-31 19:39:52 -070056 static void AddNewEntry(grpc_core::TracedBuffer** head, uint32_t seq_no,
57 void* arg);
58
Yash Tibrewal0d757a62018-07-16 16:21:43 -070059 /** Processes a received timestamp based on sock_extended_err and
60 * scm_timestamping structures. It will invoke the timestamps callback if the
61 * timestamp type is SCM_TSTAMP_ACK. */
Yash Tibrewalf0397932018-05-31 19:39:52 -070062 static void ProcessTimestamp(grpc_core::TracedBuffer** head,
63 struct sock_extended_err* serr,
64 struct scm_timestamping* tss);
65
Yash Tibrewal0d757a62018-07-16 16:21:43 -070066 /** Cleans the list by calling the callback for each traced buffer in the list
67 * with timestamps that it has. */
Yash Tibrewalf0397932018-05-31 19:39:52 -070068 static void Shutdown(grpc_core::TracedBuffer** head,
69 grpc_error* shutdown_err);
70
71 private:
72 GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
73
74 TracedBuffer(int seq_no, void* arg)
Yash Tibrewal0d757a62018-07-16 16:21:43 -070075 : seq_no_(seq_no), arg_(arg), next_(nullptr) {}
Yash Tibrewalf0397932018-05-31 19:39:52 -070076
77 uint32_t seq_no_; /* The sequence number for the last byte in the buffer */
78 void* arg_; /* The arg to pass to timestamps_callback */
Yash Tibrewal0d757a62018-07-16 16:21:43 -070079 grpc_core::Timestamps ts_; /* The timestamps corresponding to this buffer */
80 grpc_core::TracedBuffer* next_; /* The next TracedBuffer in the list */
Yash Tibrewalf0397932018-05-31 19:39:52 -070081};
82#else /* GRPC_LINUX_ERRQUEUE */
83class TracedBuffer {};
84#endif /* GRPC_LINUX_ERRQUEUE */
85
Yash Tibrewal0d757a62018-07-16 16:21:43 -070086/** Sets the callback function to call when timestamps for a write are
87 * collected. The callback does not own a reference to error. */
Yash Tibrewalf0397932018-05-31 19:39:52 -070088void grpc_tcp_set_write_timestamps_callback(void (*fn)(void*,
89 grpc_core::Timestamps*,
90 grpc_error* error));
91
Yash Tibrewal0d757a62018-07-16 16:21:43 -070092}; /* namespace grpc_core */
Yash Tibrewalf0397932018-05-31 19:39:52 -070093
94#endif /* GRPC_CORE_LIB_IOMGR_BUFFER_LIST_H */