Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 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 "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 | |
| 32 | namespace grpc_core { |
| 33 | struct Timestamps { |
| 34 | gpr_timespec sendmsg_time; |
| 35 | gpr_timespec scheduled_time; |
| 36 | gpr_timespec sent_time; |
| 37 | gpr_timespec acked_time; |
| 38 | }; |
| 39 | |
Yash Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 40 | /** 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 Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 51 | #ifdef GRPC_LINUX_ERRQUEUE |
| 52 | class TracedBuffer { |
| 53 | public: |
Yash Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 54 | /** Add a new entry in the TracedBuffer list pointed to by head. Also saves |
| 55 | * sendmsg_time with the current timestamp. */ |
Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 56 | static void AddNewEntry(grpc_core::TracedBuffer** head, uint32_t seq_no, |
| 57 | void* arg); |
| 58 | |
Yash Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 59 | /** 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 Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 62 | static void ProcessTimestamp(grpc_core::TracedBuffer** head, |
| 63 | struct sock_extended_err* serr, |
| 64 | struct scm_timestamping* tss); |
| 65 | |
Yash Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 66 | /** Cleans the list by calling the callback for each traced buffer in the list |
| 67 | * with timestamps that it has. */ |
Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 68 | 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 Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 75 | : seq_no_(seq_no), arg_(arg), next_(nullptr) {} |
Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 76 | |
| 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 Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 79 | grpc_core::Timestamps ts_; /* The timestamps corresponding to this buffer */ |
| 80 | grpc_core::TracedBuffer* next_; /* The next TracedBuffer in the list */ |
Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 81 | }; |
| 82 | #else /* GRPC_LINUX_ERRQUEUE */ |
| 83 | class TracedBuffer {}; |
| 84 | #endif /* GRPC_LINUX_ERRQUEUE */ |
| 85 | |
Yash Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 86 | /** Sets the callback function to call when timestamps for a write are |
| 87 | * collected. The callback does not own a reference to error. */ |
Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 88 | void grpc_tcp_set_write_timestamps_callback(void (*fn)(void*, |
| 89 | grpc_core::Timestamps*, |
| 90 | grpc_error* error)); |
| 91 | |
Yash Tibrewal | 0d757a6 | 2018-07-16 16:21:43 -0700 | [diff] [blame^] | 92 | }; /* namespace grpc_core */ |
Yash Tibrewal | f039793 | 2018-05-31 19:39:52 -0700 | [diff] [blame] | 93 | |
| 94 | #endif /* GRPC_CORE_LIB_IOMGR_BUFFER_LIST_H */ |