• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 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 "src/core/lib/iomgr/port.h"
20 
21 #ifdef GRPC_POSIX_SOCKET
22 
23 #include <pthread.h>
24 
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27 
28 #include "src/core/lib/gpr/env.h"
29 #include "src/core/lib/gprpp/thd.h"
30 #include "src/core/lib/iomgr/ev_posix.h"
31 #include "src/core/lib/iomgr/iomgr_posix.h"
32 
33 typedef struct poll_args {
34   struct pollfd* fds;
35   nfds_t nfds;
36   int timeout;
37   int result;
38 } poll_args;
39 
40 gpr_cv poll_cv;
41 gpr_mu poll_mu;
42 static int socket_event = 0;
43 
44 // Trigger a "socket" POLLIN in mock_poll()
trigger_socket_event()45 void trigger_socket_event() {
46   gpr_mu_lock(&poll_mu);
47   socket_event = 1;
48   gpr_cv_broadcast(&poll_cv);
49   gpr_mu_unlock(&poll_mu);
50 }
51 
reset_socket_event()52 void reset_socket_event() {
53   gpr_mu_lock(&poll_mu);
54   socket_event = 0;
55   gpr_mu_unlock(&poll_mu);
56 }
57 
58 // Mocks posix poll() function
mock_poll(struct pollfd * fds,nfds_t nfds,int timeout)59 int mock_poll(struct pollfd* fds, nfds_t nfds, int timeout) {
60   int res = 0;
61   gpr_timespec poll_time;
62   gpr_mu_lock(&poll_mu);
63   GPR_ASSERT(nfds == 3);
64   GPR_ASSERT(fds[0].fd == 20);
65   GPR_ASSERT(fds[1].fd == 30);
66   GPR_ASSERT(fds[2].fd == 50);
67   GPR_ASSERT(fds[0].events == (POLLIN | POLLHUP));
68   GPR_ASSERT(fds[1].events == (POLLIN | POLLHUP));
69   GPR_ASSERT(fds[2].events == POLLIN);
70 
71   if (timeout < 0) {
72     poll_time = gpr_inf_future(GPR_CLOCK_REALTIME);
73   } else {
74     poll_time = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
75                              gpr_time_from_millis(timeout, GPR_TIMESPAN));
76   }
77 
78   if (socket_event || !gpr_cv_wait(&poll_cv, &poll_mu, poll_time)) {
79     fds[0].revents = POLLIN;
80     res = 1;
81   }
82   gpr_mu_unlock(&poll_mu);
83   return res;
84 }
85 
background_poll(void * args)86 void background_poll(void* args) {
87   poll_args* pargs = static_cast<poll_args*>(args);
88   pargs->result = grpc_poll_function(pargs->fds, pargs->nfds, pargs->timeout);
89 }
90 
test_many_fds(void)91 void test_many_fds(void) {
92   int i;
93   grpc_wakeup_fd fd[1000];
94   for (i = 0; i < 1000; i++) {
95     GPR_ASSERT(grpc_wakeup_fd_init(&fd[i]) == GRPC_ERROR_NONE);
96   }
97   for (i = 0; i < 1000; i++) {
98     grpc_wakeup_fd_destroy(&fd[i]);
99   }
100 }
101 
test_poll_cv_trigger(void)102 void test_poll_cv_trigger(void) {
103   grpc_wakeup_fd cvfd1, cvfd2, cvfd3;
104   struct pollfd pfds[6];
105   poll_args pargs;
106 
107   GPR_ASSERT(grpc_wakeup_fd_init(&cvfd1) == GRPC_ERROR_NONE);
108   GPR_ASSERT(grpc_wakeup_fd_init(&cvfd2) == GRPC_ERROR_NONE);
109   GPR_ASSERT(grpc_wakeup_fd_init(&cvfd3) == GRPC_ERROR_NONE);
110   GPR_ASSERT(cvfd1.read_fd < 0);
111   GPR_ASSERT(cvfd2.read_fd < 0);
112   GPR_ASSERT(cvfd3.read_fd < 0);
113   GPR_ASSERT(cvfd1.read_fd != cvfd2.read_fd);
114   GPR_ASSERT(cvfd2.read_fd != cvfd3.read_fd);
115   GPR_ASSERT(cvfd1.read_fd != cvfd3.read_fd);
116 
117   pfds[0].fd = cvfd1.read_fd;
118   pfds[1].fd = cvfd2.read_fd;
119   pfds[2].fd = 20;
120   pfds[3].fd = 30;
121   pfds[4].fd = cvfd3.read_fd;
122   pfds[5].fd = 50;
123 
124   pfds[0].events = 0;
125   pfds[1].events = POLLIN;
126   pfds[2].events = POLLIN | POLLHUP;
127   pfds[3].events = POLLIN | POLLHUP;
128   pfds[4].events = POLLIN;
129   pfds[5].events = POLLIN;
130 
131   pargs.fds = pfds;
132   pargs.nfds = 6;
133   pargs.timeout = 1000;
134   pargs.result = -2;
135 
136   {
137     grpc_core::Thread thd("grpc_background_poll", &background_poll, &pargs);
138     thd.Start();
139     // Wakeup wakeup_fd not listening for events
140     GPR_ASSERT(grpc_wakeup_fd_wakeup(&cvfd1) == GRPC_ERROR_NONE);
141     thd.Join();
142     GPR_ASSERT(pargs.result == 0);
143     GPR_ASSERT(pfds[0].revents == 0);
144     GPR_ASSERT(pfds[1].revents == 0);
145     GPR_ASSERT(pfds[2].revents == 0);
146     GPR_ASSERT(pfds[3].revents == 0);
147     GPR_ASSERT(pfds[4].revents == 0);
148     GPR_ASSERT(pfds[5].revents == 0);
149   }
150 
151   {
152     // Pollin on socket fd
153     pargs.timeout = -1;
154     pargs.result = -2;
155     grpc_core::Thread thd("grpc_background_poll", &background_poll, &pargs);
156     thd.Start();
157     trigger_socket_event();
158     thd.Join();
159     GPR_ASSERT(pargs.result == 1);
160     GPR_ASSERT(pfds[0].revents == 0);
161     GPR_ASSERT(pfds[1].revents == 0);
162     GPR_ASSERT(pfds[2].revents == POLLIN);
163     GPR_ASSERT(pfds[3].revents == 0);
164     GPR_ASSERT(pfds[4].revents == 0);
165     GPR_ASSERT(pfds[5].revents == 0);
166   }
167 
168   {
169     // Pollin on wakeup fd
170     reset_socket_event();
171     pargs.result = -2;
172     grpc_core::Thread thd("grpc_background_poll", &background_poll, &pargs);
173     thd.Start();
174     GPR_ASSERT(grpc_wakeup_fd_wakeup(&cvfd2) == GRPC_ERROR_NONE);
175     thd.Join();
176 
177     GPR_ASSERT(pargs.result == 1);
178     GPR_ASSERT(pfds[0].revents == 0);
179     GPR_ASSERT(pfds[1].revents == POLLIN);
180     GPR_ASSERT(pfds[2].revents == 0);
181     GPR_ASSERT(pfds[3].revents == 0);
182     GPR_ASSERT(pfds[4].revents == 0);
183     GPR_ASSERT(pfds[5].revents == 0);
184   }
185 
186   {
187     // Pollin on wakeupfd before poll()
188     pargs.result = -2;
189     grpc_core::Thread thd("grpc_background_poll", &background_poll, &pargs);
190     thd.Start();
191     thd.Join();
192 
193     GPR_ASSERT(pargs.result == 1);
194     GPR_ASSERT(pfds[0].revents == 0);
195     GPR_ASSERT(pfds[1].revents == POLLIN);
196     GPR_ASSERT(pfds[2].revents == 0);
197     GPR_ASSERT(pfds[3].revents == 0);
198     GPR_ASSERT(pfds[4].revents == 0);
199     GPR_ASSERT(pfds[5].revents == 0);
200   }
201 
202   {
203     // No Events
204     pargs.result = -2;
205     pargs.timeout = 1000;
206     reset_socket_event();
207     GPR_ASSERT(grpc_wakeup_fd_consume_wakeup(&cvfd1) == GRPC_ERROR_NONE);
208     GPR_ASSERT(grpc_wakeup_fd_consume_wakeup(&cvfd2) == GRPC_ERROR_NONE);
209     grpc_core::Thread thd("grpc_background_poll", &background_poll, &pargs);
210     thd.Start();
211     thd.Join();
212 
213     GPR_ASSERT(pargs.result == 0);
214     GPR_ASSERT(pfds[0].revents == 0);
215     GPR_ASSERT(pfds[1].revents == 0);
216     GPR_ASSERT(pfds[2].revents == 0);
217     GPR_ASSERT(pfds[3].revents == 0);
218     GPR_ASSERT(pfds[4].revents == 0);
219     GPR_ASSERT(pfds[5].revents == 0);
220   }
221 }
222 
main(int argc,char ** argv)223 int main(int argc, char** argv) {
224   gpr_setenv("GRPC_POLL_STRATEGY", "poll-cv");
225   grpc_poll_function = &mock_poll;
226   gpr_mu_init(&poll_mu);
227   gpr_cv_init(&poll_cv);
228   grpc_determine_iomgr_platform();
229   grpc_iomgr_platform_init();
230   test_many_fds();
231   grpc_iomgr_platform_shutdown();
232 
233   grpc_iomgr_platform_init();
234   test_poll_cv_trigger();
235   grpc_iomgr_platform_shutdown();
236   return 0;
237 }
238 
239 #else /* GRPC_POSIX_SOCKET */
240 
main(int argc,char ** argv)241 int main(int argc, char** argv) { return 1; }
242 
243 #endif /* GRPC_POSIX_SOCKET */
244