• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/core/lib/iomgr/port.h"
23 
24 #include <grpc/support/log.h>
25 
26 #ifdef GRPC_LINUX_ERRQUEUE
27 #include <time.h>
28 
29 #include "src/core/lib/gprpp/memory.h"
30 
31 namespace grpc_core {
AddNewEntry(TracedBuffer ** head,uint32_t seq_no,void * arg)32 void TracedBuffer::AddNewEntry(TracedBuffer** head, uint32_t seq_no,
33                                void* arg) {
34   GPR_DEBUG_ASSERT(head != nullptr);
35   TracedBuffer* new_elem = New<TracedBuffer>(seq_no, arg);
36   /* Store the current time as the sendmsg time. */
37   new_elem->ts_.sendmsg_time = gpr_now(GPR_CLOCK_REALTIME);
38   if (*head == nullptr) {
39     *head = new_elem;
40     return;
41   }
42   /* Append at the end. */
43   TracedBuffer* ptr = *head;
44   while (ptr->next_ != nullptr) {
45     ptr = ptr->next_;
46   }
47   ptr->next_ = new_elem;
48 }
49 
50 namespace {
51 /** Fills gpr_timespec gts based on values from timespec ts */
fill_gpr_from_timestamp(gpr_timespec * gts,const struct timespec * ts)52 void fill_gpr_from_timestamp(gpr_timespec* gts, const struct timespec* ts) {
53   gts->tv_sec = ts->tv_sec;
54   gts->tv_nsec = static_cast<int32_t>(ts->tv_nsec);
55   gts->clock_type = GPR_CLOCK_REALTIME;
56 }
57 
58 /** The saved callback function that will be invoked when we get all the
59  * timestamps that we are going to get for a TracedBuffer. */
60 void (*timestamps_callback)(void*, grpc_core::Timestamps*,
61                             grpc_error* shutdown_err);
62 } /* namespace */
63 
ProcessTimestamp(TracedBuffer ** head,struct sock_extended_err * serr,struct scm_timestamping * tss)64 void TracedBuffer::ProcessTimestamp(TracedBuffer** head,
65                                     struct sock_extended_err* serr,
66                                     struct scm_timestamping* tss) {
67   GPR_DEBUG_ASSERT(head != nullptr);
68   TracedBuffer* elem = *head;
69   TracedBuffer* next = nullptr;
70   while (elem != nullptr) {
71     /* The byte number refers to the sequence number of the last byte which this
72      * timestamp relates to. */
73     if (serr->ee_data >= elem->seq_no_) {
74       switch (serr->ee_info) {
75         case SCM_TSTAMP_SCHED:
76           fill_gpr_from_timestamp(&(elem->ts_.scheduled_time), &(tss->ts[0]));
77           elem = elem->next_;
78           break;
79         case SCM_TSTAMP_SND:
80           fill_gpr_from_timestamp(&(elem->ts_.sent_time), &(tss->ts[0]));
81           elem = elem->next_;
82           break;
83         case SCM_TSTAMP_ACK:
84           fill_gpr_from_timestamp(&(elem->ts_.acked_time), &(tss->ts[0]));
85           /* Got all timestamps. Do the callback and free this TracedBuffer.
86            * The thing below can be passed by value if we don't want the
87            * restriction on the lifetime. */
88           timestamps_callback(elem->arg_, &(elem->ts_), GRPC_ERROR_NONE);
89           next = elem->next_;
90           Delete<TracedBuffer>(elem);
91           *head = elem = next;
92           break;
93         default:
94           abort();
95       }
96     } else {
97       break;
98     }
99   }
100 }
101 
Shutdown(TracedBuffer ** head,grpc_error * shutdown_err)102 void TracedBuffer::Shutdown(TracedBuffer** head, grpc_error* shutdown_err) {
103   GPR_DEBUG_ASSERT(head != nullptr);
104   TracedBuffer* elem = *head;
105   while (elem != nullptr) {
106     if (timestamps_callback) {
107       timestamps_callback(elem->arg_, &(elem->ts_), shutdown_err);
108     }
109     auto* next = elem->next_;
110     Delete<TracedBuffer>(elem);
111     elem = next;
112   }
113   *head = nullptr;
114   GRPC_ERROR_UNREF(shutdown_err);
115 }
116 
grpc_tcp_set_write_timestamps_callback(void (* fn)(void *,grpc_core::Timestamps *,grpc_error * error))117 void grpc_tcp_set_write_timestamps_callback(void (*fn)(void*,
118                                                        grpc_core::Timestamps*,
119                                                        grpc_error* error)) {
120   timestamps_callback = fn;
121 }
122 } /* namespace grpc_core */
123 
124 #else /* GRPC_LINUX_ERRQUEUE */
125 
126 namespace grpc_core {
grpc_tcp_set_write_timestamps_callback(void (* fn)(void *,grpc_core::Timestamps *,grpc_error * error))127 void grpc_tcp_set_write_timestamps_callback(void (*fn)(void*,
128                                                        grpc_core::Timestamps*,
129                                                        grpc_error* error)) {
130   gpr_log(GPR_DEBUG, "Timestamps callback is not enabled for this platform");
131 }
132 } /* namespace grpc_core */
133 
134 #endif /* GRPC_LINUX_ERRQUEUE */
135