grpc 1.6.7 → 1.7.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +579 -77
  3. data/include/grpc/byte_buffer.h +1 -63
  4. data/include/grpc/compression.h +27 -5
  5. data/include/grpc/fork.h +24 -0
  6. data/include/grpc/grpc.h +12 -6
  7. data/include/grpc/grpc_security.h +28 -7
  8. data/include/grpc/impl/codegen/atm.h +1 -0
  9. data/include/grpc/impl/codegen/byte_buffer.h +86 -0
  10. data/include/grpc/impl/codegen/compression_types.h +63 -5
  11. data/include/grpc/impl/codegen/fork.h +48 -0
  12. data/include/grpc/impl/codegen/grpc_types.h +26 -9
  13. data/include/grpc/impl/codegen/port_platform.h +11 -4
  14. data/include/grpc/impl/codegen/slice.h +6 -1
  15. data/include/grpc/impl/codegen/sync.h +3 -1
  16. data/include/grpc/impl/codegen/sync_custom.h +36 -0
  17. data/include/grpc/module.modulemap +75 -3
  18. data/include/grpc/slice.h +1 -5
  19. data/include/grpc/support/sync_custom.h +24 -0
  20. data/src/core/ext/census/base_resources.c +14 -14
  21. data/src/core/ext/census/context.c +7 -5
  22. data/src/core/ext/census/grpc_filter.c +12 -14
  23. data/src/core/ext/census/mlog.c +2 -1
  24. data/src/core/ext/census/resource.c +13 -9
  25. data/src/core/ext/filters/client_channel/channel_connectivity.c +15 -8
  26. data/src/core/ext/filters/client_channel/client_channel.c +418 -439
  27. data/src/core/ext/filters/client_channel/client_channel_factory.c +4 -5
  28. data/src/core/ext/filters/client_channel/client_channel_plugin.c +2 -2
  29. data/src/core/ext/filters/client_channel/http_connect_handshaker.c +7 -5
  30. data/src/core/ext/filters/client_channel/http_proxy.c +17 -21
  31. data/src/core/ext/filters/client_channel/lb_policy.c +1 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +7 -7
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +371 -257
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +7 -5
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +25 -14
  36. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +16 -16
  37. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +33 -28
  38. data/src/core/ext/filters/client_channel/lb_policy_factory.c +10 -8
  39. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  40. data/src/core/ext/filters/client_channel/proxy_mapper_registry.c +1 -1
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.c +7 -6
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c +62 -28
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c +29 -23
  44. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.c +25 -14
  45. data/src/core/ext/filters/client_channel/retry_throttle.c +9 -6
  46. data/src/core/ext/filters/client_channel/subchannel.c +30 -30
  47. data/src/core/ext/filters/client_channel/subchannel.h +1 -4
  48. data/src/core/ext/filters/client_channel/subchannel_index.c +31 -15
  49. data/src/core/ext/filters/client_channel/subchannel_index.h +7 -0
  50. data/src/core/ext/filters/client_channel/uri_parser.c +4 -3
  51. data/src/core/ext/filters/deadline/deadline_filter.c +78 -39
  52. data/src/core/ext/filters/deadline/deadline_filter.h +7 -1
  53. data/src/core/ext/filters/http/client/http_client_filter.c +14 -14
  54. data/src/core/ext/filters/http/http_filters_plugin.c +1 -1
  55. data/src/core/ext/filters/http/message_compress/message_compress_filter.c +240 -175
  56. data/src/core/ext/filters/http/server/http_server_filter.c +48 -36
  57. data/src/core/ext/filters/load_reporting/{load_reporting_filter.c → server_load_reporting_filter.c} +11 -12
  58. data/src/core/ext/filters/load_reporting/{load_reporting_filter.h → server_load_reporting_filter.h} +6 -5
  59. data/src/core/ext/filters/load_reporting/{load_reporting.c → server_load_reporting_plugin.c} +19 -13
  60. data/src/core/ext/filters/load_reporting/{load_reporting.h → server_load_reporting_plugin.h} +4 -3
  61. data/src/core/ext/filters/max_age/max_age_filter.c +2 -3
  62. data/src/core/ext/filters/message_size/message_size_filter.c +4 -2
  63. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c +0 -1
  64. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +5 -5
  65. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  66. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +1 -1
  67. data/src/core/ext/transport/chttp2/server/chttp2_server.c +20 -18
  68. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +1 -0
  69. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +493 -210
  70. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  71. data/src/core/ext/transport/chttp2/transport/flow_control.c +9 -8
  72. data/src/core/ext/transport/chttp2/transport/frame_data.c +2 -2
  73. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +2 -2
  74. data/src/core/ext/transport/chttp2/transport/frame_ping.c +5 -4
  75. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  76. data/src/core/ext/transport/chttp2/transport/frame_settings.c +10 -9
  77. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +9 -5
  78. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +62 -41
  79. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +52 -8
  80. data/src/core/ext/transport/chttp2/transport/hpack_table.c +2 -2
  81. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +3 -2
  82. data/src/core/ext/transport/chttp2/transport/internal.h +60 -30
  83. data/src/core/ext/transport/chttp2/transport/parsing.c +16 -5
  84. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -16
  85. data/src/core/ext/transport/chttp2/transport/stream_map.c +6 -4
  86. data/src/core/ext/transport/chttp2/transport/writing.c +133 -105
  87. data/src/core/ext/transport/inproc/inproc_transport.c +61 -65
  88. data/src/core/lib/channel/channel_args.c +112 -12
  89. data/src/core/lib/channel/channel_args.h +31 -0
  90. data/src/core/lib/channel/channel_stack.c +1 -15
  91. data/src/core/lib/channel/channel_stack.h +3 -10
  92. data/src/core/lib/channel/channel_stack_builder.c +41 -10
  93. data/src/core/lib/channel/channel_stack_builder.h +10 -0
  94. data/src/core/lib/channel/connected_channel.c +94 -23
  95. data/src/core/lib/channel/handshaker.c +8 -6
  96. data/src/core/lib/channel/handshaker_registry.c +1 -1
  97. data/src/core/lib/compression/algorithm_metadata.h +14 -0
  98. data/src/core/lib/compression/compression.c +101 -1
  99. data/src/core/lib/compression/stream_compression.c +32 -146
  100. data/src/core/lib/compression/stream_compression.h +28 -4
  101. data/src/core/lib/compression/stream_compression_gzip.c +228 -0
  102. data/src/core/lib/{iomgr/ev_epoll_thread_pool_linux.h → compression/stream_compression_gzip.h} +5 -7
  103. data/src/core/lib/compression/stream_compression_identity.c +94 -0
  104. data/src/core/lib/{iomgr/ev_epoll_limited_pollers_linux.h → compression/stream_compression_identity.h} +7 -8
  105. data/src/core/lib/debug/stats.c +174 -0
  106. data/src/core/lib/debug/stats.h +61 -0
  107. data/src/core/lib/debug/stats_data.c +687 -0
  108. data/src/core/lib/debug/stats_data.h +470 -0
  109. data/src/core/lib/debug/trace.c +3 -3
  110. data/src/core/lib/debug/trace.h +1 -1
  111. data/src/core/lib/http/format_request.c +1 -1
  112. data/src/core/lib/http/httpcli.c +8 -7
  113. data/src/core/lib/http/httpcli_security_connector.c +2 -1
  114. data/src/core/lib/http/parser.c +4 -3
  115. data/src/core/lib/iomgr/call_combiner.c +202 -0
  116. data/src/core/lib/iomgr/call_combiner.h +121 -0
  117. data/src/core/lib/iomgr/closure.c +18 -4
  118. data/src/core/lib/iomgr/combiner.c +11 -4
  119. data/src/core/lib/iomgr/error.c +26 -24
  120. data/src/core/lib/iomgr/ev_epoll1_linux.c +395 -212
  121. data/src/core/lib/iomgr/ev_epollex_linux.c +141 -128
  122. data/src/core/lib/iomgr/ev_epollsig_linux.c +44 -41
  123. data/src/core/lib/iomgr/ev_poll_posix.c +99 -75
  124. data/src/core/lib/iomgr/ev_posix.c +5 -9
  125. data/src/core/lib/iomgr/ev_posix.h +1 -1
  126. data/src/core/lib/iomgr/exec_ctx.h +6 -1
  127. data/src/core/lib/iomgr/executor.c +142 -36
  128. data/src/core/lib/iomgr/executor.h +6 -1
  129. data/src/core/lib/iomgr/fork_posix.c +88 -0
  130. data/src/core/lib/iomgr/fork_windows.c +39 -0
  131. data/src/core/lib/iomgr/iocp_windows.c +2 -0
  132. data/src/core/lib/iomgr/iomgr.c +2 -8
  133. data/src/core/lib/iomgr/is_epollexclusive_available.c +6 -6
  134. data/src/core/lib/iomgr/load_file.c +2 -1
  135. data/src/core/lib/iomgr/polling_entity.c +9 -9
  136. data/src/core/lib/iomgr/polling_entity.h +7 -1
  137. data/src/core/lib/iomgr/pollset.h +1 -1
  138. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  139. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  140. data/src/core/lib/iomgr/port.h +4 -0
  141. data/src/core/lib/iomgr/resolve_address_posix.c +8 -7
  142. data/src/core/lib/iomgr/resolve_address_windows.c +1 -1
  143. data/src/core/lib/iomgr/resource_quota.c +24 -19
  144. data/src/core/lib/iomgr/socket_factory_posix.c +4 -4
  145. data/src/core/lib/iomgr/socket_mutator.c +4 -4
  146. data/src/core/lib/iomgr/socket_utils_windows.c +0 -4
  147. data/src/core/lib/iomgr/tcp_client_posix.c +5 -4
  148. data/src/core/lib/iomgr/tcp_posix.c +181 -20
  149. data/src/core/lib/iomgr/tcp_server_posix.c +8 -7
  150. data/src/core/lib/iomgr/tcp_server_utils_posix_common.c +1 -1
  151. data/src/core/lib/iomgr/timer.h +4 -0
  152. data/src/core/lib/iomgr/timer_generic.c +138 -3
  153. data/src/core/lib/iomgr/timer_generic.h +3 -0
  154. data/src/core/lib/iomgr/timer_heap.c +4 -4
  155. data/src/core/lib/iomgr/timer_manager.c +2 -2
  156. data/src/core/lib/iomgr/timer_uv.c +2 -0
  157. data/src/core/lib/iomgr/udp_server.c +10 -8
  158. data/src/core/lib/iomgr/unix_sockets_posix.c +4 -2
  159. data/src/core/lib/iomgr/wakeup_fd_cv.c +9 -8
  160. data/src/core/lib/iomgr/wakeup_fd_cv.h +2 -2
  161. data/src/core/lib/json/json.c +1 -1
  162. data/src/core/lib/json/json_string.c +13 -13
  163. data/src/core/lib/profiling/timers.h +18 -8
  164. data/src/core/lib/security/credentials/composite/composite_credentials.c +4 -10
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +2 -1
  166. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +11 -6
  167. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +4 -4
  168. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +132 -50
  169. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  170. data/src/core/lib/security/transport/client_auth_filter.c +68 -135
  171. data/src/core/lib/security/transport/secure_endpoint.c +110 -90
  172. data/src/core/lib/security/transport/secure_endpoint.h +8 -3
  173. data/src/core/lib/security/transport/security_connector.c +10 -12
  174. data/src/core/lib/security/transport/security_handshaker.c +45 -24
  175. data/src/core/lib/security/transport/server_auth_filter.c +71 -20
  176. data/src/core/lib/slice/b64.c +2 -2
  177. data/src/core/lib/slice/slice.c +16 -14
  178. data/src/core/lib/slice/slice_buffer.c +5 -4
  179. data/src/core/lib/slice/slice_hash_table.c +3 -2
  180. data/src/core/lib/slice/slice_intern.c +8 -5
  181. data/src/core/lib/support/block_annotate.h +22 -0
  182. data/src/core/lib/support/fork.c +62 -0
  183. data/src/core/lib/support/fork.h +35 -0
  184. data/src/core/lib/support/log_linux.c +1 -1
  185. data/src/core/lib/support/string.c +15 -1
  186. data/src/core/lib/support/string.h +3 -0
  187. data/src/core/lib/support/thd_internal.h +6 -0
  188. data/src/core/lib/support/thd_posix.c +56 -0
  189. data/src/core/lib/support/thd_windows.c +2 -0
  190. data/src/core/lib/surface/alarm.c +22 -15
  191. data/src/core/lib/surface/byte_buffer.c +4 -2
  192. data/src/core/lib/surface/call.c +442 -141
  193. data/src/core/lib/surface/call.h +6 -6
  194. data/src/core/lib/surface/call_log_batch.c +1 -1
  195. data/src/core/lib/surface/call_test_only.h +12 -0
  196. data/src/core/lib/surface/channel.c +39 -4
  197. data/src/core/lib/surface/channel_init.c +6 -6
  198. data/src/core/lib/surface/channel_ping.c +2 -2
  199. data/src/core/lib/surface/completion_queue.c +56 -57
  200. data/src/core/lib/surface/init.c +17 -3
  201. data/src/core/lib/surface/init_secure.c +5 -1
  202. data/src/core/lib/surface/lame_client.cc +9 -10
  203. data/src/core/lib/surface/server.c +81 -72
  204. data/src/core/lib/surface/version.c +2 -2
  205. data/src/core/lib/transport/byte_stream.c +1 -0
  206. data/src/core/lib/transport/byte_stream.h +3 -1
  207. data/src/core/lib/transport/connectivity_state.c +2 -1
  208. data/src/core/lib/transport/metadata.c +7 -4
  209. data/src/core/lib/transport/metadata_batch.c +18 -16
  210. data/src/core/lib/transport/metadata_batch.h +1 -0
  211. data/src/core/lib/transport/service_config.c +5 -3
  212. data/src/core/lib/transport/static_metadata.c +395 -614
  213. data/src/core/lib/transport/static_metadata.h +165 -133
  214. data/src/core/lib/transport/status_conversion.c +1 -1
  215. data/src/core/lib/transport/transport.c +20 -20
  216. data/src/core/lib/transport/transport.h +8 -5
  217. data/src/core/lib/transport/transport_impl.h +0 -3
  218. data/src/core/lib/transport/transport_op_string.c +8 -1
  219. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -4
  220. data/src/core/tsi/fake_transport_security.c +133 -2
  221. data/src/core/tsi/fake_transport_security.h +5 -0
  222. data/src/core/tsi/ssl_transport_security.c +105 -8
  223. data/src/core/tsi/ssl_transport_security.h +30 -7
  224. data/src/core/tsi/transport_security.h +8 -2
  225. data/src/core/tsi/transport_security_grpc.c +20 -13
  226. data/src/core/tsi/transport_security_grpc.h +13 -9
  227. data/src/ruby/ext/grpc/rb_call_credentials.c +6 -2
  228. data/src/ruby/ext/grpc/rb_grpc.c +1 -1
  229. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +30 -20
  230. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +50 -35
  231. data/src/ruby/lib/grpc.rb +1 -0
  232. data/src/ruby/lib/grpc/generic/active_call.rb +34 -9
  233. data/src/ruby/lib/grpc/generic/bidi_call.rb +19 -10
  234. data/src/ruby/lib/grpc/generic/client_stub.rb +95 -38
  235. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  236. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  237. data/src/ruby/lib/grpc/generic/rpc_desc.rb +66 -20
  238. data/src/ruby/lib/grpc/generic/rpc_server.rb +15 -3
  239. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +1 -2
  240. data/src/ruby/lib/grpc/version.rb +1 -1
  241. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +1 -0
  242. data/src/ruby/spec/channel_connection_spec.rb +1 -34
  243. data/src/ruby/spec/client_server_spec.rb +188 -82
  244. data/src/ruby/spec/generic/active_call_spec.rb +65 -11
  245. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  246. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  247. data/src/ruby/spec/generic/rpc_desc_spec.rb +38 -0
  248. data/src/ruby/spec/generic/rpc_server_spec.rb +1 -34
  249. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  250. data/src/ruby/spec/spec_helper.rb +4 -0
  251. data/src/ruby/spec/support/helpers.rb +73 -0
  252. data/src/ruby/spec/support/services.rb +147 -0
  253. data/third_party/cares/ares_build.h +21 -62
  254. data/third_party/cares/cares/ares.h +23 -1
  255. data/third_party/cares/cares/ares__close_sockets.c +2 -2
  256. data/third_party/cares/cares/ares_create_query.c +3 -3
  257. data/third_party/cares/cares/ares_expand_name.c +6 -2
  258. data/third_party/cares/cares/ares_expand_string.c +1 -1
  259. data/third_party/cares/cares/ares_getnameinfo.c +27 -7
  260. data/third_party/cares/cares/ares_init.c +407 -39
  261. data/third_party/cares/cares/ares_library_init.c +10 -0
  262. data/third_party/cares/cares/ares_library_init.h +2 -1
  263. data/third_party/cares/cares/ares_nowarn.c +6 -6
  264. data/third_party/cares/cares/ares_nowarn.h +2 -2
  265. data/third_party/cares/cares/ares_parse_naptr_reply.c +6 -1
  266. data/third_party/cares/cares/ares_private.h +11 -0
  267. data/third_party/cares/cares/ares_process.c +126 -37
  268. data/third_party/cares/cares/ares_version.h +2 -2
  269. data/third_party/cares/cares/ares_writev.c +2 -2
  270. data/third_party/cares/cares/config-win32.h +8 -34
  271. data/third_party/cares/cares/inet_net_pton.c +2 -2
  272. data/third_party/cares/cares/setup_once.h +5 -5
  273. data/third_party/cares/config_darwin/ares_config.h +98 -196
  274. data/third_party/cares/config_linux/ares_config.h +103 -203
  275. metadata +47 -20
  276. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +0 -1957
  277. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +0 -1182
@@ -1,1182 +0,0 @@
1
- /*
2
- *
3
- * Copyright 2017 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
- /* This polling engine is only relevant on linux kernels supporting epoll() */
22
- #ifdef GRPC_LINUX_EPOLL
23
-
24
- #include "src/core/lib/iomgr/ev_epoll_thread_pool_linux.h"
25
-
26
- #include <assert.h>
27
- #include <errno.h>
28
- #include <limits.h>
29
- #include <poll.h>
30
- #include <pthread.h>
31
- #include <string.h>
32
- #include <sys/epoll.h>
33
- #include <sys/socket.h>
34
- #include <unistd.h>
35
-
36
- #include <grpc/support/alloc.h>
37
- #include <grpc/support/cpu.h>
38
- #include <grpc/support/log.h>
39
- #include <grpc/support/string_util.h>
40
- #include <grpc/support/thd.h>
41
- #include <grpc/support/tls.h>
42
- #include <grpc/support/useful.h>
43
-
44
- #include "src/core/lib/iomgr/ev_posix.h"
45
- #include "src/core/lib/iomgr/iomgr_internal.h"
46
- #include "src/core/lib/iomgr/lockfree_event.h"
47
- #include "src/core/lib/iomgr/timer.h"
48
- #include "src/core/lib/iomgr/wakeup_fd_posix.h"
49
- #include "src/core/lib/profiling/timers.h"
50
- #include "src/core/lib/support/block_annotate.h"
51
-
52
- /* TODO: sreek - Move this to init.c and initialize this like other tracers. */
53
- #define GRPC_POLLING_TRACE(fmt, ...) \
54
- if (GRPC_TRACER_ON(grpc_polling_trace)) { \
55
- gpr_log(GPR_INFO, (fmt), __VA_ARGS__); \
56
- }
57
-
58
- /* The alarm system needs to be able to wakeup 'some poller' sometimes
59
- * (specifically when a new alarm needs to be triggered earlier than the next
60
- * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a
61
- * case occurs. */
62
-
63
- struct epoll_set;
64
-
65
- #define GRPC_POLLSET_KICK_BROADCAST ((grpc_pollset_worker *)1)
66
-
67
- /*******************************************************************************
68
- * Fd Declarations
69
- */
70
- struct grpc_fd {
71
- gpr_mu mu;
72
- struct epoll_set *eps;
73
-
74
- int fd;
75
-
76
- /* The fd is either closed or we relinquished control of it. In either cases,
77
- this indicates that the 'fd' on this structure is no longer valid */
78
- bool orphaned;
79
-
80
- gpr_atm read_closure;
81
- gpr_atm write_closure;
82
-
83
- struct grpc_fd *freelist_next;
84
- grpc_closure *on_done_closure;
85
-
86
- grpc_iomgr_object iomgr_object;
87
- };
88
-
89
- static void fd_global_init(void);
90
- static void fd_global_shutdown(void);
91
-
92
- /*******************************************************************************
93
- * epoll set Declarations
94
- */
95
-
96
- #ifndef NDEBUG
97
-
98
- #define EPS_ADD_REF(p, r) eps_add_ref_dbg((p), (r), __FILE__, __LINE__)
99
- #define EPS_UNREF(exec_ctx, p, r) \
100
- eps_unref_dbg((exec_ctx), (p), (r), __FILE__, __LINE__)
101
-
102
- #else
103
-
104
- #define EPS_ADD_REF(p, r) eps_add_ref((p))
105
- #define EPS_UNREF(exec_ctx, p, r) eps_unref((exec_ctx), (p))
106
-
107
- #endif
108
-
109
- typedef struct epoll_set {
110
- /* Mutex poller should acquire to poll this. This enforces that only one
111
- * poller can be polling on epoll_set at any time */
112
- gpr_mu mu;
113
-
114
- /* Ref count. Use EPS_ADD_REF() and EPS_UNREF() macros to increment/decrement
115
- the refcount. Once the ref count becomes zero, this structure is destroyed
116
- which means we should ensure that there is never a scenario where a
117
- EPS_ADD_REF() is racing with a EPS_UNREF() that just made the ref_count
118
- zero. */
119
- gpr_atm ref_count;
120
-
121
- /* Number of threads currently polling on this epoll set*/
122
- gpr_atm poller_count;
123
-
124
- /* Is the epoll set shutdown */
125
- gpr_atm is_shutdown;
126
-
127
- /* The fd of the underlying epoll set */
128
- int epoll_fd;
129
- } epoll_set;
130
-
131
- /*******************************************************************************
132
- * Pollset Declarations
133
- */
134
- struct grpc_pollset_worker {
135
- gpr_cv kick_cv;
136
-
137
- struct grpc_pollset_worker *next;
138
- struct grpc_pollset_worker *prev;
139
- };
140
-
141
- struct grpc_pollset {
142
- gpr_mu mu;
143
- struct epoll_set *eps;
144
-
145
- grpc_pollset_worker root_worker;
146
- bool kicked_without_pollers;
147
-
148
- bool shutting_down; /* Is the pollset shutting down ? */
149
- bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */
150
- grpc_closure *shutdown_done; /* Called after after shutdown is complete */
151
- };
152
-
153
- /*******************************************************************************
154
- * Pollset-set Declarations
155
- */
156
- struct grpc_pollset_set {
157
- char unused;
158
- };
159
-
160
- /*****************************************************************************
161
- * Dedicated polling threads and pollsets - Declarations
162
- */
163
-
164
- size_t g_num_eps = 1;
165
- struct epoll_set **g_epoll_sets = NULL;
166
- gpr_atm g_next_eps;
167
- size_t g_num_threads_per_eps = 1;
168
- gpr_thd_id *g_poller_threads = NULL;
169
-
170
- /* Used as read-notifier pollsets for fds. We won't be using read notifier
171
- * pollsets with this polling engine. So it does not matter what pollset we
172
- * return */
173
- grpc_pollset g_read_notifier;
174
-
175
- static void add_fd_to_eps(grpc_fd *fd);
176
- static bool init_epoll_sets();
177
- static void shutdown_epoll_sets();
178
- static void poller_thread_loop(void *arg);
179
- static void start_poller_threads();
180
- static void shutdown_poller_threads();
181
-
182
- /*******************************************************************************
183
- * Common helpers
184
- */
185
-
186
- static bool append_error(grpc_error **composite, grpc_error *error,
187
- const char *desc) {
188
- if (error == GRPC_ERROR_NONE) return true;
189
- if (*composite == GRPC_ERROR_NONE) {
190
- *composite = GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc);
191
- }
192
- *composite = grpc_error_add_child(*composite, error);
193
- return false;
194
- }
195
-
196
- /*******************************************************************************
197
- * epoll set Definitions
198
- */
199
-
200
- /* The wakeup fd that is used to wake up all threads in an epoll_set informing
201
- that the epoll set is shutdown. This wakeup fd initialized to be readable
202
- and MUST NOT be consumed i.e the threads that woke up MUST NOT call
203
- grpc_wakeup_fd_consume_wakeup() */
204
- static grpc_wakeup_fd epoll_set_wakeup_fd;
205
-
206
- /* The epoll set being polled right now.
207
- See comments in workqueue_maybe_wakeup for why this is tracked. */
208
- static __thread epoll_set *g_current_thread_epoll_set;
209
-
210
- /* Forward declaration */
211
- static void epoll_set_delete(epoll_set *eps);
212
-
213
- #ifdef GRPC_TSAN
214
- /* Currently TSAN may incorrectly flag data races between epoll_ctl and
215
- epoll_wait for any grpc_fd structs that are added to the epoll set via
216
- epoll_ctl and are returned (within a very short window) via epoll_wait().
217
-
218
- To work-around this race, we establish a happens-before relation between
219
- the code just-before epoll_ctl() and the code after epoll_wait() by using
220
- this atomic */
221
- gpr_atm g_epoll_sync;
222
- #endif /* defined(GRPC_TSAN) */
223
-
224
- static void eps_add_ref(epoll_set *eps);
225
- static void eps_unref(grpc_exec_ctx *exec_ctx, epoll_set *eps);
226
-
227
- #ifndef NDEBUG
228
- static void eps_add_ref_dbg(epoll_set *eps, const char *reason,
229
- const char *file, int line) {
230
- if (GRPC_TRACER_ON(grpc_polling_trace)) {
231
- gpr_atm old_cnt = gpr_atm_acq_load(&eps->ref_count);
232
- gpr_log(GPR_DEBUG, "Add ref eps: %p, old:%" PRIdPTR " -> new:%" PRIdPTR
233
- " (%s) - (%s, %d)",
234
- eps, old_cnt, old_cnt + 1, reason, file, line);
235
- }
236
- eps_add_ref(eps);
237
- }
238
-
239
- static void eps_unref_dbg(grpc_exec_ctx *exec_ctx, epoll_set *eps,
240
- const char *reason, const char *file, int line) {
241
- if (GRPC_TRACER_ON(grpc_polling_trace)) {
242
- gpr_atm old_cnt = gpr_atm_acq_load(&eps->ref_count);
243
- gpr_log(GPR_DEBUG, "Unref eps: %p, old:%" PRIdPTR " -> new:%" PRIdPTR
244
- " (%s) - (%s, %d)",
245
- eps, old_cnt, (old_cnt - 1), reason, file, line);
246
- }
247
- eps_unref(exec_ctx, eps);
248
- }
249
- #endif
250
-
251
- static void eps_add_ref(epoll_set *eps) {
252
- gpr_atm_no_barrier_fetch_add(&eps->ref_count, 1);
253
- }
254
-
255
- static void eps_unref(grpc_exec_ctx *exec_ctx, epoll_set *eps) {
256
- /* If ref count went to zero, delete the epoll set. This deletion is
257
- not done under a lock since once the ref count goes to zero, we are
258
- guaranteed that no one else holds a reference to the epoll set (and
259
- that there is no racing eps_add_ref() call either).*/
260
- if (1 == gpr_atm_full_fetch_add(&eps->ref_count, -1)) {
261
- epoll_set_delete(eps);
262
- }
263
- }
264
-
265
- static void epoll_set_add_fd_locked(epoll_set *eps, grpc_fd *fd,
266
- grpc_error **error) {
267
- int err;
268
- struct epoll_event ev;
269
- char *err_msg;
270
- const char *err_desc = "epoll_set_add_fd_locked";
271
-
272
- #ifdef GRPC_TSAN
273
- /* See the definition of g_epoll_sync for more context */
274
- gpr_atm_rel_store(&g_epoll_sync, (gpr_atm)0);
275
- #endif /* defined(GRPC_TSAN) */
276
-
277
- ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
278
- ev.data.ptr = fd;
279
- err = epoll_ctl(eps->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev);
280
- if (err < 0 && errno != EEXIST) {
281
- gpr_asprintf(
282
- &err_msg,
283
- "epoll_ctl (epoll_fd: %d) add fd: %d failed with error: %d (%s)",
284
- eps->epoll_fd, fd->fd, errno, strerror(errno));
285
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
286
- gpr_free(err_msg);
287
- }
288
- }
289
-
290
- static void epoll_set_add_wakeup_fd_locked(epoll_set *eps,
291
- grpc_wakeup_fd *wakeup_fd,
292
- grpc_error **error) {
293
- struct epoll_event ev;
294
- int err;
295
- char *err_msg;
296
- const char *err_desc = "epoll_set_add_wakeup_fd";
297
-
298
- ev.events = (uint32_t)(EPOLLIN | EPOLLET);
299
- ev.data.ptr = wakeup_fd;
300
- err = epoll_ctl(eps->epoll_fd, EPOLL_CTL_ADD,
301
- GRPC_WAKEUP_FD_GET_READ_FD(wakeup_fd), &ev);
302
- if (err < 0 && errno != EEXIST) {
303
- gpr_asprintf(&err_msg,
304
- "epoll_ctl (epoll_fd: %d) add wakeup fd: %d failed with "
305
- "error: %d (%s)",
306
- eps->epoll_fd, GRPC_WAKEUP_FD_GET_READ_FD(wakeup_fd), errno,
307
- strerror(errno));
308
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
309
- gpr_free(err_msg);
310
- }
311
- }
312
-
313
- static void epoll_set_remove_fd(epoll_set *eps, grpc_fd *fd, bool is_fd_closed,
314
- grpc_error **error) {
315
- int err;
316
- char *err_msg;
317
- const char *err_desc = "epoll_set_remove_fd";
318
-
319
- /* If fd is already closed, then it would have been automatically been removed
320
- from the epoll set */
321
- if (!is_fd_closed) {
322
- err = epoll_ctl(eps->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
323
- if (err < 0 && errno != ENOENT) {
324
- gpr_asprintf(
325
- &err_msg,
326
- "epoll_ctl (epoll_fd: %d) del fd: %d failed with error: %d (%s)",
327
- eps->epoll_fd, fd->fd, errno, strerror(errno));
328
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
329
- gpr_free(err_msg);
330
- }
331
- }
332
- }
333
-
334
- /* Might return NULL in case of an error */
335
- static epoll_set *epoll_set_create(grpc_error **error) {
336
- epoll_set *eps = NULL;
337
- const char *err_desc = "epoll_set_create";
338
-
339
- *error = GRPC_ERROR_NONE;
340
-
341
- eps = gpr_malloc(sizeof(*eps));
342
- eps->epoll_fd = -1;
343
-
344
- gpr_mu_init(&eps->mu);
345
-
346
- gpr_atm_rel_store(&eps->ref_count, 0);
347
- gpr_atm_rel_store(&eps->poller_count, 0);
348
-
349
- gpr_atm_rel_store(&eps->is_shutdown, false);
350
-
351
- eps->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
352
-
353
- if (eps->epoll_fd < 0) {
354
- append_error(error, GRPC_OS_ERROR(errno, "epoll_create1"), err_desc);
355
- goto done;
356
- }
357
-
358
- done:
359
- if (*error != GRPC_ERROR_NONE) {
360
- epoll_set_delete(eps);
361
- eps = NULL;
362
- }
363
- return eps;
364
- }
365
-
366
- static void epoll_set_delete(epoll_set *eps) {
367
- if (eps->epoll_fd >= 0) {
368
- close(eps->epoll_fd);
369
- }
370
-
371
- gpr_mu_destroy(&eps->mu);
372
-
373
- gpr_free(eps);
374
- }
375
-
376
- static grpc_error *epoll_set_global_init() {
377
- grpc_error *error = GRPC_ERROR_NONE;
378
-
379
- error = grpc_wakeup_fd_init(&epoll_set_wakeup_fd);
380
- if (error == GRPC_ERROR_NONE) {
381
- error = grpc_wakeup_fd_wakeup(&epoll_set_wakeup_fd);
382
- }
383
-
384
- return error;
385
- }
386
-
387
- static void epoll_set_global_shutdown() {
388
- grpc_wakeup_fd_destroy(&epoll_set_wakeup_fd);
389
- }
390
-
391
- /*******************************************************************************
392
- * Fd Definitions
393
- */
394
-
395
- /* We need to keep a freelist not because of any concerns of malloc performance
396
- * but instead so that implementations with multiple threads in (for example)
397
- * epoll_wait deal with the race between pollset removal and incoming poll
398
- * notifications.
399
- *
400
- * The problem is that the poller ultimately holds a reference to this
401
- * object, so it is very difficult to know when is safe to free it, at least
402
- * without some expensive synchronization.
403
- *
404
- * If we keep the object freelisted, in the worst case losing this race just
405
- * becomes a spurious read notification on a reused fd.
406
- */
407
-
408
- static grpc_fd *fd_freelist = NULL;
409
- static gpr_mu fd_freelist_mu;
410
-
411
- static grpc_fd *get_fd_from_freelist() {
412
- grpc_fd *new_fd = NULL;
413
-
414
- gpr_mu_lock(&fd_freelist_mu);
415
- if (fd_freelist != NULL) {
416
- new_fd = fd_freelist;
417
- fd_freelist = fd_freelist->freelist_next;
418
- }
419
- gpr_mu_unlock(&fd_freelist_mu);
420
- return new_fd;
421
- }
422
-
423
- static void add_fd_to_freelist(grpc_fd *fd) {
424
- gpr_mu_lock(&fd_freelist_mu);
425
- fd->freelist_next = fd_freelist;
426
- fd_freelist = fd;
427
- grpc_iomgr_unregister_object(&fd->iomgr_object);
428
-
429
- grpc_lfev_destroy(&fd->read_closure);
430
- grpc_lfev_destroy(&fd->write_closure);
431
-
432
- gpr_mu_unlock(&fd_freelist_mu);
433
- }
434
-
435
- static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
436
-
437
- static void fd_global_shutdown(void) {
438
- gpr_mu_lock(&fd_freelist_mu);
439
- gpr_mu_unlock(&fd_freelist_mu);
440
- while (fd_freelist != NULL) {
441
- grpc_fd *fd = fd_freelist;
442
- fd_freelist = fd_freelist->freelist_next;
443
- gpr_mu_destroy(&fd->mu);
444
- gpr_free(fd);
445
- }
446
- gpr_mu_destroy(&fd_freelist_mu);
447
- }
448
-
449
- static grpc_fd *fd_create(int fd, const char *name) {
450
- grpc_fd *new_fd = get_fd_from_freelist();
451
- if (new_fd == NULL) {
452
- new_fd = gpr_malloc(sizeof(grpc_fd));
453
- gpr_mu_init(&new_fd->mu);
454
- }
455
-
456
- /* Note: It is not really needed to get the new_fd->mu lock here. If this
457
- * is a newly created fd (or an fd we got from the freelist), no one else
458
- * would be holding a lock to it anyway. */
459
- gpr_mu_lock(&new_fd->mu);
460
- new_fd->eps = NULL;
461
-
462
- new_fd->fd = fd;
463
- new_fd->orphaned = false;
464
- grpc_lfev_init(&new_fd->read_closure);
465
- grpc_lfev_init(&new_fd->write_closure);
466
-
467
- new_fd->freelist_next = NULL;
468
- new_fd->on_done_closure = NULL;
469
-
470
- gpr_mu_unlock(&new_fd->mu);
471
-
472
- char *fd_name;
473
- gpr_asprintf(&fd_name, "%s fd=%d", name, fd);
474
- grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name);
475
- gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, (void *)new_fd, fd_name);
476
- gpr_free(fd_name);
477
-
478
- /* Associate the fd with one of the eps */
479
- add_fd_to_eps(new_fd);
480
- return new_fd;
481
- }
482
-
483
- static int fd_wrapped_fd(grpc_fd *fd) {
484
- int ret_fd = -1;
485
- gpr_mu_lock(&fd->mu);
486
- if (!fd->orphaned) {
487
- ret_fd = fd->fd;
488
- }
489
- gpr_mu_unlock(&fd->mu);
490
-
491
- return ret_fd;
492
- }
493
-
494
- static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
495
- grpc_closure *on_done, int *release_fd,
496
- bool already_closed, const char *reason) {
497
- bool is_fd_closed = already_closed;
498
- grpc_error *error = GRPC_ERROR_NONE;
499
- epoll_set *unref_eps = NULL;
500
-
501
- gpr_mu_lock(&fd->mu);
502
- fd->on_done_closure = on_done;
503
-
504
- /* If release_fd is not NULL, we should be relinquishing control of the file
505
- descriptor fd->fd (but we still own the grpc_fd structure). */
506
- if (release_fd != NULL) {
507
- *release_fd = fd->fd;
508
- } else if (!is_fd_closed) {
509
- close(fd->fd);
510
- is_fd_closed = true;
511
- }
512
-
513
- fd->orphaned = true;
514
-
515
- /* Remove the fd from the epoll set */
516
- if (fd->eps != NULL) {
517
- epoll_set_remove_fd(fd->eps, fd, is_fd_closed, &error);
518
- unref_eps = fd->eps;
519
- fd->eps = NULL;
520
- }
521
-
522
- GRPC_CLOSURE_SCHED(exec_ctx, fd->on_done_closure, GRPC_ERROR_REF(error));
523
-
524
- gpr_mu_unlock(&fd->mu);
525
-
526
- /* We are done with this fd. Release it (i.e add back to freelist) */
527
- add_fd_to_freelist(fd);
528
-
529
- if (unref_eps != NULL) {
530
- /* Unref stale epoll set here, outside the fd lock above.
531
- The epoll set owns a workqueue which owns an fd, and unreffing
532
- inside the lock can cause an eventual lock loop that makes TSAN very
533
- unhappy. */
534
- EPS_UNREF(exec_ctx, unref_eps, "fd_orphan");
535
- }
536
- GRPC_LOG_IF_ERROR("fd_orphan", GRPC_ERROR_REF(error));
537
- GRPC_ERROR_UNREF(error);
538
- }
539
-
540
- /* This polling engine doesn't really need the read notifier functionality. So
541
- * it just returns a dummy read notifier pollset */
542
- static grpc_pollset *fd_get_read_notifier_pollset(grpc_exec_ctx *exec_ctx,
543
- grpc_fd *fd) {
544
- return &g_read_notifier;
545
- }
546
-
547
- static bool fd_is_shutdown(grpc_fd *fd) {
548
- return grpc_lfev_is_shutdown(&fd->read_closure);
549
- }
550
-
551
- /* Might be called multiple times */
552
- static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_error *why) {
553
- if (grpc_lfev_set_shutdown(exec_ctx, &fd->read_closure,
554
- GRPC_ERROR_REF(why))) {
555
- shutdown(fd->fd, SHUT_RDWR);
556
- grpc_lfev_set_shutdown(exec_ctx, &fd->write_closure, GRPC_ERROR_REF(why));
557
- }
558
- GRPC_ERROR_UNREF(why);
559
- }
560
-
561
- static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
562
- grpc_closure *closure) {
563
- grpc_lfev_notify_on(exec_ctx, &fd->read_closure, closure, "read");
564
- }
565
-
566
- static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
567
- grpc_closure *closure) {
568
- grpc_lfev_notify_on(exec_ctx, &fd->write_closure, closure, "write");
569
- }
570
-
571
- /*******************************************************************************
572
- * Pollset Definitions
573
- */
574
- /* TODO: sreek - Not needed anymore */
575
- GPR_TLS_DECL(g_current_thread_pollset);
576
- GPR_TLS_DECL(g_current_thread_worker);
577
-
578
- static void pollset_worker_init(grpc_pollset_worker *worker) {
579
- worker->next = worker->prev = NULL;
580
- gpr_cv_init(&worker->kick_cv);
581
- }
582
-
583
- /* Global state management */
584
- static grpc_error *pollset_global_init(void) {
585
- gpr_tls_init(&g_current_thread_pollset);
586
- gpr_tls_init(&g_current_thread_worker);
587
- return GRPC_ERROR_NONE;
588
- }
589
-
590
- static void pollset_global_shutdown(void) {
591
- gpr_tls_destroy(&g_current_thread_pollset);
592
- gpr_tls_destroy(&g_current_thread_worker);
593
- }
594
-
595
- static grpc_error *pollset_worker_kick(grpc_pollset_worker *worker) {
596
- gpr_cv_signal(&worker->kick_cv);
597
- return GRPC_ERROR_NONE;
598
- }
599
-
600
- /* Return 1 if the pollset has active threads in pollset_work (pollset must
601
- * be locked) */
602
- static int pollset_has_workers(grpc_pollset *p) {
603
- return p->root_worker.next != &p->root_worker;
604
- }
605
-
606
- static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
607
- worker->prev->next = worker->next;
608
- worker->next->prev = worker->prev;
609
- }
610
-
611
- static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
612
- if (pollset_has_workers(p)) {
613
- grpc_pollset_worker *w = p->root_worker.next;
614
- remove_worker(p, w);
615
- return w;
616
- } else {
617
- return NULL;
618
- }
619
- }
620
-
621
- static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
622
- worker->next = &p->root_worker;
623
- worker->prev = worker->next->prev;
624
- worker->prev->next = worker->next->prev = worker;
625
- }
626
-
627
- static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
628
- worker->prev = &p->root_worker;
629
- worker->next = worker->prev->next;
630
- worker->prev->next = worker->next->prev = worker;
631
- }
632
-
633
- /* p->mu must be held before calling this function */
634
- static grpc_error *pollset_kick(grpc_pollset *p,
635
- grpc_pollset_worker *specific_worker) {
636
- GPR_TIMER_BEGIN("pollset_kick", 0);
637
- grpc_error *error = GRPC_ERROR_NONE;
638
- const char *err_desc = "Kick Failure";
639
- grpc_pollset_worker *worker = specific_worker;
640
- if (worker != NULL) {
641
- if (worker == GRPC_POLLSET_KICK_BROADCAST) {
642
- if (pollset_has_workers(p)) {
643
- GPR_TIMER_BEGIN("pollset_kick.broadcast", 0);
644
- for (worker = p->root_worker.next; worker != &p->root_worker;
645
- worker = worker->next) {
646
- if (gpr_tls_get(&g_current_thread_worker) != (intptr_t)worker) {
647
- append_error(&error, pollset_worker_kick(worker), err_desc);
648
- }
649
- }
650
- GPR_TIMER_END("pollset_kick.broadcast", 0);
651
- } else {
652
- p->kicked_without_pollers = true;
653
- }
654
- } else {
655
- GPR_TIMER_MARK("kicked_specifically", 0);
656
- if (gpr_tls_get(&g_current_thread_worker) != (intptr_t)worker) {
657
- append_error(&error, pollset_worker_kick(worker), err_desc);
658
- }
659
- }
660
- } else if (gpr_tls_get(&g_current_thread_pollset) != (intptr_t)p) {
661
- /* Since worker == NULL, it means that we can kick "any" worker on this
662
- pollset 'p'. If 'p' happens to be the same pollset this thread is
663
- currently polling (i.e in pollset_work() function), then there is no need
664
- to kick any other worker since the current thread can just absorb the
665
- kick. This is the reason why we enter this case only when
666
- g_current_thread_pollset is != p */
667
-
668
- GPR_TIMER_MARK("kick_anonymous", 0);
669
- worker = pop_front_worker(p);
670
- if (worker != NULL) {
671
- GPR_TIMER_MARK("finally_kick", 0);
672
- push_back_worker(p, worker);
673
- append_error(&error, pollset_worker_kick(worker), err_desc);
674
- } else {
675
- GPR_TIMER_MARK("kicked_no_pollers", 0);
676
- p->kicked_without_pollers = true;
677
- }
678
- }
679
-
680
- GPR_TIMER_END("pollset_kick", 0);
681
- GRPC_LOG_IF_ERROR("pollset_kick", GRPC_ERROR_REF(error));
682
- return error;
683
- }
684
-
685
- static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
686
- gpr_mu_init(&pollset->mu);
687
- *mu = &pollset->mu;
688
- pollset->eps = NULL;
689
-
690
- pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
691
- pollset->kicked_without_pollers = false;
692
-
693
- pollset->shutting_down = false;
694
- pollset->finish_shutdown_called = false;
695
- pollset->shutdown_done = NULL;
696
- }
697
-
698
- static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
699
- grpc_lfev_set_ready(exec_ctx, &fd->read_closure, "read");
700
- }
701
-
702
- static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
703
- grpc_lfev_set_ready(exec_ctx, &fd->write_closure, "write");
704
- }
705
-
706
- static void pollset_release_epoll_set(grpc_exec_ctx *exec_ctx, grpc_pollset *ps,
707
- char *reason) {
708
- if (ps->eps != NULL) {
709
- EPS_UNREF(exec_ctx, ps->eps, reason);
710
- }
711
- ps->eps = NULL;
712
- }
713
-
714
- static void finish_shutdown_locked(grpc_exec_ctx *exec_ctx,
715
- grpc_pollset *pollset) {
716
- /* The pollset cannot have any workers if we are at this stage */
717
- GPR_ASSERT(!pollset_has_workers(pollset));
718
-
719
- pollset->finish_shutdown_called = true;
720
-
721
- /* Release the ref and set pollset->eps to NULL */
722
- pollset_release_epoll_set(exec_ctx, pollset, "ps_shutdown");
723
- GRPC_CLOSURE_SCHED(exec_ctx, pollset->shutdown_done, GRPC_ERROR_NONE);
724
- }
725
-
726
- /* pollset->mu lock must be held by the caller before calling this */
727
- static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
728
- grpc_closure *closure) {
729
- GPR_TIMER_BEGIN("pollset_shutdown", 0);
730
- GPR_ASSERT(!pollset->shutting_down);
731
- pollset->shutting_down = true;
732
- pollset->shutdown_done = closure;
733
- pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
734
-
735
- /* If the pollset has any workers, we cannot call finish_shutdown_locked()
736
- because it would release the underlying epoll set. In such a case, we
737
- let the last worker call finish_shutdown_locked() from pollset_work() */
738
- if (!pollset_has_workers(pollset)) {
739
- GPR_ASSERT(!pollset->finish_shutdown_called);
740
- GPR_TIMER_MARK("pollset_shutdown.finish_shutdown_locked", 0);
741
- finish_shutdown_locked(exec_ctx, pollset);
742
- }
743
- GPR_TIMER_END("pollset_shutdown", 0);
744
- }
745
-
746
- /* pollset_shutdown is guaranteed to be called before pollset_destroy. So other
747
- * than destroying the mutexes, there is nothing special that needs to be done
748
- * here */
749
- static void pollset_destroy(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) {
750
- GPR_ASSERT(!pollset_has_workers(pollset));
751
- gpr_mu_destroy(&pollset->mu);
752
- }
753
-
754
- /* Blocking call */
755
- static void acquire_epoll_lease(epoll_set *eps) {
756
- if (g_num_threads_per_eps > 1) {
757
- gpr_mu_lock(&eps->mu);
758
- }
759
- }
760
-
761
- static void release_epoll_lease(epoll_set *eps) {
762
- if (g_num_threads_per_eps > 1) {
763
- gpr_mu_unlock(&eps->mu);
764
- }
765
- }
766
-
767
- #define GRPC_EPOLL_MAX_EVENTS 100
768
- static void do_epoll_wait(grpc_exec_ctx *exec_ctx, int epoll_fd, epoll_set *eps,
769
- grpc_error **error) {
770
- struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
771
- int ep_rv;
772
- char *err_msg;
773
- const char *err_desc = "do_epoll_wait";
774
-
775
- int timeout_ms = -1;
776
-
777
- GRPC_SCHEDULING_START_BLOCKING_REGION;
778
- acquire_epoll_lease(eps);
779
- ep_rv = epoll_wait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms);
780
- release_epoll_lease(eps);
781
- GRPC_SCHEDULING_END_BLOCKING_REGION;
782
-
783
- if (ep_rv < 0) {
784
- gpr_asprintf(&err_msg,
785
- "epoll_wait() epoll fd: %d failed with error: %d (%s)",
786
- epoll_fd, errno, strerror(errno));
787
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
788
- }
789
-
790
- #ifdef GRPC_TSAN
791
- /* See the definition of g_poll_sync for more details */
792
- gpr_atm_acq_load(&g_epoll_sync);
793
- #endif /* defined(GRPC_TSAN) */
794
-
795
- for (int i = 0; i < ep_rv; ++i) {
796
- void *data_ptr = ep_ev[i].data.ptr;
797
- if (data_ptr == &epoll_set_wakeup_fd) {
798
- gpr_atm_rel_store(&eps->is_shutdown, 1);
799
- gpr_log(GPR_INFO, "pollset poller: shutdown set");
800
- } else {
801
- grpc_fd *fd = data_ptr;
802
- int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
803
- int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
804
- int write_ev = ep_ev[i].events & EPOLLOUT;
805
- if (read_ev || cancel) {
806
- fd_become_readable(exec_ctx, fd);
807
- }
808
- if (write_ev || cancel) {
809
- fd_become_writable(exec_ctx, fd);
810
- }
811
- }
812
- }
813
- }
814
-
815
- static void epoll_set_work(grpc_exec_ctx *exec_ctx, epoll_set *eps,
816
- grpc_error **error) {
817
- int epoll_fd = -1;
818
- GPR_TIMER_BEGIN("epoll_set_work", 0);
819
-
820
- /* Since epoll_fd is immutable, it is safe to read it without a lock on the
821
- epoll set. */
822
- epoll_fd = eps->epoll_fd;
823
-
824
- gpr_atm_no_barrier_fetch_add(&eps->poller_count, 1);
825
- g_current_thread_epoll_set = eps;
826
-
827
- do_epoll_wait(exec_ctx, epoll_fd, eps, error);
828
-
829
- g_current_thread_epoll_set = NULL;
830
- gpr_atm_no_barrier_fetch_add(&eps->poller_count, -1);
831
-
832
- GPR_TIMER_END("epoll_set_work", 0);
833
- }
834
-
835
- /* pollset->mu lock must be held by the caller before calling this.
836
- The function pollset_work() may temporarily release the lock (pollset->mu)
837
- during the course of its execution but it will always re-acquire the lock and
838
- ensure that it is held by the time the function returns */
839
- static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
840
- grpc_pollset_worker **worker_hdl,
841
- gpr_timespec now, gpr_timespec deadline) {
842
- GPR_TIMER_BEGIN("pollset_work", 0);
843
- grpc_error *error = GRPC_ERROR_NONE;
844
-
845
- grpc_pollset_worker worker;
846
- pollset_worker_init(&worker);
847
-
848
- if (worker_hdl) *worker_hdl = &worker;
849
-
850
- gpr_tls_set(&g_current_thread_pollset, (intptr_t)pollset);
851
- gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker);
852
-
853
- if (pollset->kicked_without_pollers) {
854
- /* If the pollset was kicked without pollers, pretend that the current
855
- worker got the kick and skip polling. A kick indicates that there is some
856
- work that needs attention like an event on the completion queue or an
857
- alarm */
858
- GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
859
- pollset->kicked_without_pollers = 0;
860
- } else if (!pollset->shutting_down) {
861
- push_front_worker(pollset, &worker);
862
-
863
- gpr_cv_wait(&worker.kick_cv, &pollset->mu,
864
- gpr_convert_clock_type(deadline, GPR_CLOCK_REALTIME));
865
- /* pollset->mu locked here */
866
-
867
- remove_worker(pollset, &worker);
868
- }
869
-
870
- /* If we are the last worker on the pollset (i.e pollset_has_workers() is
871
- false at this point) and the pollset is shutting down, we may have to
872
- finish the shutdown process by calling finish_shutdown_locked().
873
- See pollset_shutdown() for more details.
874
-
875
- Note: Continuing to access pollset here is safe; it is the caller's
876
- responsibility to not destroy a pollset when it has outstanding calls to
877
- pollset_work() */
878
- if (pollset->shutting_down && !pollset_has_workers(pollset) &&
879
- !pollset->finish_shutdown_called) {
880
- GPR_TIMER_MARK("pollset_work.finish_shutdown_locked", 0);
881
- finish_shutdown_locked(exec_ctx, pollset);
882
-
883
- gpr_mu_unlock(&pollset->mu);
884
- grpc_exec_ctx_flush(exec_ctx);
885
- gpr_mu_lock(&pollset->mu);
886
- }
887
-
888
- if (worker_hdl) *worker_hdl = NULL;
889
-
890
- gpr_tls_set(&g_current_thread_pollset, (intptr_t)0);
891
- gpr_tls_set(&g_current_thread_worker, (intptr_t)0);
892
-
893
- GPR_TIMER_END("pollset_work", 0);
894
-
895
- GRPC_LOG_IF_ERROR("pollset_work", GRPC_ERROR_REF(error));
896
- return error;
897
- }
898
-
899
- static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
900
- grpc_fd *fd) {
901
- /* Nothing to do */
902
- }
903
-
904
- /*******************************************************************************
905
- * Pollset-set Definitions
906
- */
907
- grpc_pollset_set g_dummy_pollset_set;
908
- static grpc_pollset_set *pollset_set_create(void) {
909
- return &g_dummy_pollset_set;
910
- }
911
-
912
- static void pollset_set_destroy(grpc_exec_ctx *exec_ctx,
913
- grpc_pollset_set *pss) {
914
- /* Nothing to do */
915
- }
916
-
917
- static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss,
918
- grpc_fd *fd) {
919
- /* Nothing to do */
920
- }
921
-
922
- static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss,
923
- grpc_fd *fd) {
924
- /* Nothing to do */
925
- }
926
-
927
- static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
928
- grpc_pollset_set *pss, grpc_pollset *ps) {
929
- /* Nothing to do */
930
- }
931
-
932
- static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
933
- grpc_pollset_set *pss, grpc_pollset *ps) {
934
- /* Nothing to do */
935
- }
936
-
937
- static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
938
- grpc_pollset_set *bag,
939
- grpc_pollset_set *item) {
940
- /* Nothing to do */
941
- }
942
-
943
- static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
944
- grpc_pollset_set *bag,
945
- grpc_pollset_set *item) {
946
- /* Nothing to do */
947
- }
948
-
949
- /*******************************************************************************
950
- * Event engine binding
951
- */
952
-
953
- static void shutdown_engine(void) {
954
- shutdown_poller_threads();
955
- shutdown_epoll_sets();
956
- fd_global_shutdown();
957
- pollset_global_shutdown();
958
- epoll_set_global_shutdown();
959
- gpr_log(GPR_INFO, "ev-epoll-threadpool engine shutdown complete");
960
- }
961
-
962
- static const grpc_event_engine_vtable vtable = {
963
- .pollset_size = sizeof(grpc_pollset),
964
-
965
- .fd_create = fd_create,
966
- .fd_wrapped_fd = fd_wrapped_fd,
967
- .fd_orphan = fd_orphan,
968
- .fd_shutdown = fd_shutdown,
969
- .fd_is_shutdown = fd_is_shutdown,
970
- .fd_notify_on_read = fd_notify_on_read,
971
- .fd_notify_on_write = fd_notify_on_write,
972
- .fd_get_read_notifier_pollset = fd_get_read_notifier_pollset,
973
-
974
- .pollset_init = pollset_init,
975
- .pollset_shutdown = pollset_shutdown,
976
- .pollset_destroy = pollset_destroy,
977
- .pollset_work = pollset_work,
978
- .pollset_kick = pollset_kick,
979
- .pollset_add_fd = pollset_add_fd,
980
-
981
- .pollset_set_create = pollset_set_create,
982
- .pollset_set_destroy = pollset_set_destroy,
983
- .pollset_set_add_pollset = pollset_set_add_pollset,
984
- .pollset_set_del_pollset = pollset_set_del_pollset,
985
- .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
986
- .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
987
- .pollset_set_add_fd = pollset_set_add_fd,
988
- .pollset_set_del_fd = pollset_set_del_fd,
989
-
990
- .shutdown_engine = shutdown_engine,
991
- };
992
-
993
- /*****************************************************************************
994
- * Dedicated polling threads and pollsets - Definitions
995
- */
996
- static void add_fd_to_eps(grpc_fd *fd) {
997
- GPR_ASSERT(fd->eps == NULL);
998
- GPR_TIMER_BEGIN("add_fd_to_eps", 0);
999
-
1000
- grpc_error *error = GRPC_ERROR_NONE;
1001
- size_t idx = (size_t)gpr_atm_no_barrier_fetch_add(&g_next_eps, 1) % g_num_eps;
1002
- epoll_set *eps = g_epoll_sets[idx];
1003
-
1004
- gpr_mu_lock(&fd->mu);
1005
-
1006
- if (fd->orphaned) {
1007
- gpr_mu_unlock(&fd->mu);
1008
- return; /* Early out */
1009
- }
1010
-
1011
- epoll_set_add_fd_locked(eps, fd, &error);
1012
- EPS_ADD_REF(eps, "fd");
1013
- fd->eps = eps;
1014
-
1015
- GRPC_POLLING_TRACE("add_fd_to_eps (fd: %d, eps idx = %" PRIdPTR ")", fd->fd,
1016
- idx);
1017
- gpr_mu_unlock(&fd->mu);
1018
-
1019
- GRPC_LOG_IF_ERROR("add_fd_to_eps", error);
1020
- GPR_TIMER_END("add_fd_to_eps", 0);
1021
- }
1022
-
1023
- static bool init_epoll_sets() {
1024
- grpc_error *error = GRPC_ERROR_NONE;
1025
- bool is_success = true;
1026
-
1027
- g_epoll_sets = (epoll_set **)malloc(g_num_eps * sizeof(epoll_set *));
1028
-
1029
- for (size_t i = 0; i < g_num_eps; i++) {
1030
- g_epoll_sets[i] = epoll_set_create(&error);
1031
- if (g_epoll_sets[i] == NULL) {
1032
- gpr_log(GPR_ERROR, "Error in creating a epoll set");
1033
- g_num_eps = i; /* Helps cleanup */
1034
- shutdown_epoll_sets();
1035
- is_success = false;
1036
- goto done;
1037
- }
1038
-
1039
- EPS_ADD_REF(g_epoll_sets[i], "init_epoll_sets");
1040
- }
1041
-
1042
- gpr_atm_no_barrier_store(&g_next_eps, 0);
1043
- gpr_mu *mu;
1044
- pollset_init(&g_read_notifier, &mu);
1045
-
1046
- done:
1047
- GRPC_LOG_IF_ERROR("init_epoll_sets", error);
1048
- return is_success;
1049
- }
1050
-
1051
- static void shutdown_epoll_sets() {
1052
- if (!g_epoll_sets) {
1053
- return;
1054
- }
1055
-
1056
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
1057
- for (size_t i = 0; i < g_num_eps; i++) {
1058
- EPS_UNREF(&exec_ctx, g_epoll_sets[i], "shutdown_epoll_sets");
1059
- }
1060
- grpc_exec_ctx_flush(&exec_ctx);
1061
-
1062
- gpr_free(g_epoll_sets);
1063
- g_epoll_sets = NULL;
1064
- pollset_destroy(&exec_ctx, &g_read_notifier);
1065
- grpc_exec_ctx_finish(&exec_ctx);
1066
- }
1067
-
1068
- static void poller_thread_loop(void *arg) {
1069
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
1070
- grpc_error *error = GRPC_ERROR_NONE;
1071
- epoll_set *eps = (epoll_set *)arg;
1072
-
1073
- while (!gpr_atm_acq_load(&eps->is_shutdown)) {
1074
- epoll_set_work(&exec_ctx, eps, &error);
1075
- grpc_exec_ctx_flush(&exec_ctx);
1076
- }
1077
-
1078
- grpc_exec_ctx_finish(&exec_ctx);
1079
- GRPC_LOG_IF_ERROR("poller_thread_loop", error);
1080
- }
1081
-
1082
- /* g_epoll_sets MUST be initialized before calling this */
1083
- static void start_poller_threads() {
1084
- GPR_ASSERT(g_epoll_sets);
1085
-
1086
- gpr_log(GPR_INFO, "Starting poller threads");
1087
-
1088
- size_t num_threads = g_num_eps * g_num_threads_per_eps;
1089
- g_poller_threads = (gpr_thd_id *)malloc(num_threads * sizeof(gpr_thd_id));
1090
- gpr_thd_options options = gpr_thd_options_default();
1091
- gpr_thd_options_set_joinable(&options);
1092
-
1093
- for (size_t i = 0; i < num_threads; i++) {
1094
- gpr_thd_new(&g_poller_threads[i], poller_thread_loop,
1095
- (void *)g_epoll_sets[i % g_num_eps], &options);
1096
- }
1097
- }
1098
-
1099
- static void shutdown_poller_threads() {
1100
- GPR_ASSERT(g_poller_threads);
1101
- GPR_ASSERT(g_epoll_sets);
1102
- grpc_error *error = GRPC_ERROR_NONE;
1103
-
1104
- gpr_log(GPR_INFO, "Shutting down pollers");
1105
-
1106
- epoll_set *eps = NULL;
1107
- size_t num_threads = g_num_eps * g_num_threads_per_eps;
1108
- for (size_t i = 0; i < num_threads; i++) {
1109
- eps = g_epoll_sets[i];
1110
- epoll_set_add_wakeup_fd_locked(eps, &epoll_set_wakeup_fd, &error);
1111
- }
1112
-
1113
- for (size_t i = 0; i < g_num_eps; i++) {
1114
- gpr_thd_join(g_poller_threads[i]);
1115
- }
1116
-
1117
- GRPC_LOG_IF_ERROR("shutdown_poller_threads", error);
1118
- gpr_free(g_poller_threads);
1119
- g_poller_threads = NULL;
1120
- }
1121
-
1122
- /****************************************************************************/
1123
-
1124
- /* It is possible that GLIBC has epoll but the underlying kernel doesn't.
1125
- * Create a dummy epoll_fd to make sure epoll support is available */
1126
- static bool is_epoll_available() {
1127
- int fd = epoll_create1(EPOLL_CLOEXEC);
1128
- if (fd < 0) {
1129
- gpr_log(
1130
- GPR_ERROR,
1131
- "epoll_create1 failed with error: %d. Not using epoll polling engine",
1132
- fd);
1133
- return false;
1134
- }
1135
- close(fd);
1136
- return true;
1137
- }
1138
-
1139
- const grpc_event_engine_vtable *grpc_init_epoll_thread_pool_linux(
1140
- bool requested_explicitly) {
1141
- if (!requested_explicitly) return NULL;
1142
-
1143
- if (!grpc_has_wakeup_fd()) {
1144
- return NULL;
1145
- }
1146
-
1147
- if (!is_epoll_available()) {
1148
- return NULL;
1149
- }
1150
-
1151
- fd_global_init();
1152
-
1153
- if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) {
1154
- return NULL;
1155
- }
1156
-
1157
- if (!GRPC_LOG_IF_ERROR("epoll_set_global_init", epoll_set_global_init())) {
1158
- return NULL;
1159
- }
1160
-
1161
- if (!init_epoll_sets()) {
1162
- return NULL;
1163
- }
1164
-
1165
- /* TODO (sreek): Maynot be a good idea to start threads here (especially if
1166
- * this engine doesn't get picked. Consider introducing an engine_init
1167
- * function in the vtable */
1168
- start_poller_threads();
1169
- return &vtable;
1170
- }
1171
-
1172
- #else /* defined(GRPC_LINUX_EPOLL) */
1173
- #if defined(GRPC_POSIX_SOCKET)
1174
- #include "src/core/lib/iomgr/ev_posix.h"
1175
- /* If GRPC_LINUX_EPOLL is not defined, it means epoll is not available. Return
1176
- * NULL */
1177
- const grpc_event_engine_vtable *grpc_init_epoll_thread_pool_linux(
1178
- bool requested_explicitly) {
1179
- return NULL;
1180
- }
1181
- #endif /* defined(GRPC_POSIX_SOCKET) */
1182
- #endif /* !defined(GRPC_LINUX_EPOLL) */