grpc 1.17.1 → 1.18.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 (166) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1228 -988
  3. data/etc/roots.pem +242 -30
  4. data/include/grpc/grpc.h +2 -1
  5. data/include/grpc/grpc_security_constants.h +3 -3
  6. data/include/grpc/impl/codegen/atm_gcc_sync.h +2 -0
  7. data/include/grpc/impl/codegen/atm_windows.h +2 -0
  8. data/include/grpc/impl/codegen/compression_types.h +2 -1
  9. data/include/grpc/impl/codegen/grpc_types.h +1 -1
  10. data/include/grpc/impl/codegen/port_platform.h +9 -0
  11. data/src/core/ext/filters/client_channel/client_channel.cc +163 -882
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +2 -4
  13. data/src/core/ext/filters/client_channel/health/health_check_client.h +2 -3
  14. data/src/core/ext/filters/client_channel/lb_policy.cc +1 -1
  15. data/src/core/ext/filters/client_channel/lb_policy.h +8 -17
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +176 -216
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h +1 -1
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +20 -23
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +1 -1
  20. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +49 -52
  21. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +13 -35
  22. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +31 -30
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +69 -225
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h +1 -1
  25. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_secure.cc +20 -23
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +1 -1
  27. data/src/core/ext/filters/client_channel/lb_policy_factory.h +2 -84
  28. data/src/core/ext/filters/client_channel/request_routing.cc +936 -0
  29. data/src/core/ext/filters/client_channel/request_routing.h +177 -0
  30. data/src/core/ext/filters/client_channel/resolver.cc +1 -1
  31. data/src/core/ext/filters/client_channel/resolver.h +1 -1
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +37 -26
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +30 -18
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +119 -100
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +8 -5
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +5 -4
  37. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_posix.cc +2 -1
  38. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +12 -14
  39. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +5 -9
  40. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +2 -1
  41. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +1 -2
  42. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +17 -17
  43. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +45 -52
  44. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +13 -17
  45. data/src/core/ext/filters/client_channel/server_address.cc +103 -0
  46. data/src/core/ext/filters/client_channel/server_address.h +108 -0
  47. data/src/core/ext/filters/client_channel/subchannel.cc +10 -8
  48. data/src/core/ext/filters/client_channel/subchannel.h +9 -6
  49. data/src/core/ext/filters/client_channel/subchannel_index.cc +20 -27
  50. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +3 -2
  51. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +8 -9
  52. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  53. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +1 -1
  54. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.cc +8 -11
  55. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +24 -54
  56. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -1
  57. data/src/core/ext/transport/chttp2/transport/context_list.cc +67 -0
  58. data/src/core/ext/transport/chttp2/transport/context_list.h +53 -0
  59. data/src/core/ext/transport/chttp2/transport/internal.h +38 -11
  60. data/src/core/ext/transport/chttp2/transport/writing.cc +5 -0
  61. data/src/core/ext/transport/inproc/inproc_transport.cc +1 -1
  62. data/src/core/lib/channel/channelz.cc +19 -18
  63. data/src/core/lib/channel/channelz.h +7 -1
  64. data/src/core/lib/channel/channelz_registry.cc +3 -2
  65. data/src/core/lib/debug/trace.cc +3 -0
  66. data/src/core/lib/debug/trace.h +5 -3
  67. data/src/core/lib/gpr/sync_posix.cc +96 -4
  68. data/src/core/lib/gprpp/inlined_vector.h +25 -19
  69. data/src/core/lib/gprpp/memory.h +2 -11
  70. data/src/core/lib/gprpp/orphanable.h +18 -82
  71. data/src/core/lib/gprpp/ref_counted.h +75 -84
  72. data/src/core/lib/gprpp/ref_counted_ptr.h +22 -17
  73. data/src/core/lib/http/httpcli_security_connector.cc +101 -94
  74. data/src/core/lib/http/parser.h +5 -5
  75. data/src/core/lib/iomgr/buffer_list.cc +16 -5
  76. data/src/core/lib/iomgr/buffer_list.h +10 -3
  77. data/src/core/lib/iomgr/call_combiner.cc +50 -2
  78. data/src/core/lib/iomgr/call_combiner.h +29 -2
  79. data/src/core/lib/iomgr/dynamic_annotations.h +67 -0
  80. data/src/core/lib/iomgr/endpoint.cc +4 -0
  81. data/src/core/lib/iomgr/endpoint.h +3 -0
  82. data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
  83. data/src/core/lib/iomgr/ev_epoll1_linux.cc +4 -0
  84. data/src/core/lib/iomgr/ev_epollex_linux.cc +4 -0
  85. data/src/core/lib/iomgr/ev_poll_posix.cc +4 -0
  86. data/src/core/lib/iomgr/ev_posix.cc +15 -7
  87. data/src/core/lib/iomgr/ev_posix.h +10 -0
  88. data/src/core/lib/iomgr/exec_ctx.cc +13 -0
  89. data/src/core/lib/iomgr/fork_posix.cc +1 -1
  90. data/src/core/lib/iomgr/internal_errqueue.cc +36 -3
  91. data/src/core/lib/iomgr/internal_errqueue.h +7 -1
  92. data/src/core/lib/iomgr/iomgr.cc +7 -0
  93. data/src/core/lib/iomgr/iomgr.h +4 -0
  94. data/src/core/lib/iomgr/iomgr_custom.cc +3 -1
  95. data/src/core/lib/iomgr/iomgr_internal.cc +4 -0
  96. data/src/core/lib/iomgr/iomgr_internal.h +4 -0
  97. data/src/core/lib/iomgr/iomgr_posix.cc +6 -1
  98. data/src/core/lib/iomgr/iomgr_windows.cc +4 -1
  99. data/src/core/lib/iomgr/port.h +1 -2
  100. data/src/core/lib/iomgr/resource_quota.cc +1 -0
  101. data/src/core/lib/iomgr/sockaddr_utils.cc +1 -0
  102. data/src/core/lib/iomgr/tcp_custom.cc +4 -1
  103. data/src/core/lib/iomgr/tcp_posix.cc +95 -35
  104. data/src/core/lib/iomgr/tcp_windows.cc +4 -1
  105. data/src/core/lib/iomgr/timer_manager.cc +6 -0
  106. data/src/core/lib/security/context/security_context.cc +75 -108
  107. data/src/core/lib/security/context/security_context.h +59 -35
  108. data/src/core/lib/security/credentials/alts/alts_credentials.cc +36 -48
  109. data/src/core/lib/security/credentials/alts/alts_credentials.h +37 -10
  110. data/src/core/lib/security/credentials/composite/composite_credentials.cc +97 -157
  111. data/src/core/lib/security/credentials/composite/composite_credentials.h +60 -24
  112. data/src/core/lib/security/credentials/credentials.cc +18 -142
  113. data/src/core/lib/security/credentials/credentials.h +119 -95
  114. data/src/core/lib/security/credentials/fake/fake_credentials.cc +46 -71
  115. data/src/core/lib/security/credentials/fake/fake_credentials.h +23 -5
  116. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +144 -51
  117. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +28 -5
  118. data/src/core/lib/security/credentials/iam/iam_credentials.cc +27 -35
  119. data/src/core/lib/security/credentials/iam/iam_credentials.h +18 -4
  120. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +60 -69
  121. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +29 -10
  122. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +2 -0
  123. data/src/core/lib/security/credentials/local/local_credentials.cc +19 -32
  124. data/src/core/lib/security/credentials/local/local_credentials.h +32 -11
  125. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +130 -149
  126. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +74 -29
  127. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +59 -77
  128. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +40 -17
  129. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +66 -83
  130. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +58 -15
  131. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +152 -177
  132. data/src/core/lib/security/security_connector/alts/alts_security_connector.h +12 -10
  133. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +210 -215
  134. data/src/core/lib/security/security_connector/fake/fake_security_connector.h +9 -6
  135. data/src/core/lib/security/security_connector/local/local_security_connector.cc +176 -169
  136. data/src/core/lib/security/security_connector/local/local_security_connector.h +10 -9
  137. data/src/core/lib/security/security_connector/security_connector.cc +41 -124
  138. data/src/core/lib/security/security_connector/security_connector.h +102 -105
  139. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +348 -370
  140. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.h +14 -12
  141. data/src/core/lib/security/security_connector/ssl_utils.cc +13 -9
  142. data/src/core/lib/security/security_connector/ssl_utils.h +3 -1
  143. data/src/core/lib/security/transport/client_auth_filter.cc +50 -50
  144. data/src/core/lib/security/transport/secure_endpoint.cc +7 -1
  145. data/src/core/lib/security/transport/security_handshaker.cc +82 -66
  146. data/src/core/lib/security/transport/server_auth_filter.cc +15 -13
  147. data/src/core/lib/surface/init.cc +1 -0
  148. data/src/core/lib/surface/server.cc +13 -11
  149. data/src/core/lib/surface/server.h +6 -6
  150. data/src/core/lib/surface/version.cc +2 -2
  151. data/src/core/lib/transport/metadata.cc +1 -0
  152. data/src/core/lib/transport/static_metadata.cc +228 -221
  153. data/src/core/lib/transport/static_metadata.h +75 -71
  154. data/src/core/lib/transport/transport.cc +2 -1
  155. data/src/core/lib/transport/transport.h +5 -1
  156. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +9 -2
  157. data/src/core/tsi/ssl_transport_security.cc +35 -24
  158. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +1 -1
  159. data/src/ruby/lib/grpc/generic/rpc_server.rb +61 -0
  160. data/src/ruby/lib/grpc/generic/service.rb +1 -1
  161. data/src/ruby/lib/grpc/version.rb +1 -1
  162. data/src/ruby/pb/grpc/health/checker.rb +2 -3
  163. data/src/ruby/spec/generic/rpc_server_spec.rb +22 -0
  164. data/src/ruby/spec/support/services.rb +1 -0
  165. metadata +37 -32
  166. data/src/core/ext/filters/client_channel/lb_policy_factory.cc +0 -163
@@ -18,6 +18,7 @@
18
18
 
19
19
  #include <grpc/support/port_platform.h>
20
20
 
21
+ #include "src/core/ext/transport/chttp2/transport/context_list.h"
21
22
  #include "src/core/ext/transport/chttp2/transport/internal.h"
22
23
 
23
24
  #include <limits.h>
@@ -362,6 +363,7 @@ class DataSendContext {
362
363
  grpc_chttp2_encode_data(s_->id, &s_->compressed_data_buffer, send_bytes,
363
364
  is_last_frame_, &s_->stats.outgoing, &t_->outbuf);
364
365
  s_->flow_control->SentData(send_bytes);
366
+ s_->byte_counter += send_bytes;
365
367
  if (s_->compressed_data_buffer.length == 0) {
366
368
  s_->sending_bytes += s_->uncompressed_data_size;
367
369
  }
@@ -496,6 +498,9 @@ class StreamWriteContext {
496
498
  data_send_context.CompressMoreBytes();
497
499
  }
498
500
  }
501
+ if (s_->traced && grpc_endpoint_can_track_err(t_->ep)) {
502
+ grpc_core::ContextList::Append(&t_->cl, s_);
503
+ }
499
504
  write_context_->ResetPingClock();
500
505
  if (data_send_context.is_last_frame()) {
501
506
  SentLastFrame();
@@ -1236,7 +1236,7 @@ grpc_channel* grpc_inproc_channel_create(grpc_server* server,
1236
1236
 
1237
1237
  // TODO(ncteisen): design and support channelz GetSocket for inproc.
1238
1238
  grpc_server_setup_transport(server, server_transport, nullptr, server_args,
1239
- 0);
1239
+ nullptr);
1240
1240
  grpc_channel* channel = grpc_channel_create(
1241
1241
  "inproc", client_args, GRPC_CLIENT_DIRECT_CHANNEL, client_transport);
1242
1242
 
@@ -203,33 +203,34 @@ ServerNode::ServerNode(grpc_server* server, size_t channel_tracer_max_nodes)
203
203
 
204
204
  ServerNode::~ServerNode() {}
205
205
 
206
- char* ServerNode::RenderServerSockets(intptr_t start_socket_id) {
206
+ char* ServerNode::RenderServerSockets(intptr_t start_socket_id,
207
+ intptr_t max_results) {
208
+ // if user does not set max_results, we choose 500.
209
+ size_t pagination_limit = max_results == 0 ? 500 : max_results;
207
210
  grpc_json* top_level_json = grpc_json_create(GRPC_JSON_OBJECT);
208
211
  grpc_json* json = top_level_json;
209
212
  grpc_json* json_iterator = nullptr;
210
- ChildRefsList socket_refs;
211
- // uuids index into entities one-off (idx 0 is really uuid 1, since 0 is
212
- // reserved). However, we want to support requests coming in with
213
- // start_server_id=0, which signifies "give me everything."
214
- size_t start_idx = start_socket_id == 0 ? 0 : start_socket_id - 1;
215
- grpc_server_populate_server_sockets(server_, &socket_refs, start_idx);
213
+ ChildSocketsList socket_refs;
214
+ grpc_server_populate_server_sockets(server_, &socket_refs, start_socket_id);
215
+ // declared early so it can be used outside of the loop.
216
+ size_t i = 0;
216
217
  if (!socket_refs.empty()) {
217
218
  // create list of socket refs
218
219
  grpc_json* array_parent = grpc_json_create_child(
219
220
  nullptr, json, "socketRef", nullptr, GRPC_JSON_ARRAY, false);
220
- for (size_t i = 0; i < socket_refs.size(); ++i) {
221
- json_iterator =
222
- grpc_json_create_child(json_iterator, array_parent, nullptr, nullptr,
223
- GRPC_JSON_OBJECT, false);
224
- grpc_json_add_number_string_child(json_iterator, nullptr, "socketId",
225
- socket_refs[i]);
221
+ for (i = 0; i < GPR_MIN(socket_refs.size(), pagination_limit); ++i) {
222
+ grpc_json* socket_ref_json = grpc_json_create_child(
223
+ nullptr, array_parent, nullptr, nullptr, GRPC_JSON_OBJECT, false);
224
+ json_iterator = grpc_json_add_number_string_child(
225
+ socket_ref_json, nullptr, "socketId", socket_refs[i]->uuid());
226
+ grpc_json_create_child(json_iterator, socket_ref_json, "name",
227
+ socket_refs[i]->remote(), GRPC_JSON_STRING, false);
226
228
  }
227
229
  }
228
- // For now we do not have any pagination rules. In the future we could
229
- // pick a constant for max_channels_sent for a GetServers request.
230
- // Tracking: https://github.com/grpc/grpc/issues/16019.
231
- json_iterator = grpc_json_create_child(nullptr, json, "end", nullptr,
232
- GRPC_JSON_TRUE, false);
230
+ if (i == socket_refs.size()) {
231
+ json_iterator = grpc_json_create_child(nullptr, json, "end", nullptr,
232
+ GRPC_JSON_TRUE, false);
233
+ }
233
234
  char* json_str = grpc_json_dump_to_string(top_level_json, 0);
234
235
  grpc_json_destroy(top_level_json);
235
236
  return json_str;
@@ -59,6 +59,9 @@ namespace channelz {
59
59
  // add human readable names as in the channelz.proto
60
60
  typedef InlinedVector<intptr_t, 10> ChildRefsList;
61
61
 
62
+ class SocketNode;
63
+ typedef InlinedVector<SocketNode*, 10> ChildSocketsList;
64
+
62
65
  namespace testing {
63
66
  class CallCountingHelperPeer;
64
67
  class ChannelNodePeer;
@@ -207,7 +210,8 @@ class ServerNode : public BaseNode {
207
210
 
208
211
  grpc_json* RenderJson() override;
209
212
 
210
- char* RenderServerSockets(intptr_t start_socket_id);
213
+ char* RenderServerSockets(intptr_t start_socket_id,
214
+ intptr_t pagination_limit);
211
215
 
212
216
  // proxy methods to composed classes.
213
217
  void AddTraceEvent(ChannelTrace::Severity severity, grpc_slice data) {
@@ -251,6 +255,8 @@ class SocketNode : public BaseNode {
251
255
  gpr_atm_no_barrier_fetch_add(&keepalives_sent_, static_cast<gpr_atm>(1));
252
256
  }
253
257
 
258
+ const char* remote() { return remote_.get(); }
259
+
254
260
  private:
255
261
  gpr_atm streams_started_ = 0;
256
262
  gpr_atm streams_succeeded_ = 0;
@@ -252,7 +252,8 @@ char* grpc_channelz_get_server(intptr_t server_id) {
252
252
  }
253
253
 
254
254
  char* grpc_channelz_get_server_sockets(intptr_t server_id,
255
- intptr_t start_socket_id) {
255
+ intptr_t start_socket_id,
256
+ intptr_t max_results) {
256
257
  grpc_core::channelz::BaseNode* base_node =
257
258
  grpc_core::channelz::ChannelzRegistry::Get(server_id);
258
259
  if (base_node == nullptr ||
@@ -263,7 +264,7 @@ char* grpc_channelz_get_server_sockets(intptr_t server_id,
263
264
  // actually a server node
264
265
  grpc_core::channelz::ServerNode* server_node =
265
266
  static_cast<grpc_core::channelz::ServerNode*>(base_node);
266
- return server_node->RenderServerSockets(start_socket_id);
267
+ return server_node->RenderServerSockets(start_socket_id, max_results);
267
268
  }
268
269
 
269
270
  char* grpc_channelz_get_channel(intptr_t channel_id) {
@@ -21,6 +21,7 @@
21
21
  #include "src/core/lib/debug/trace.h"
22
22
 
23
23
  #include <string.h>
24
+ #include <type_traits>
24
25
 
25
26
  #include <grpc/grpc.h>
26
27
  #include <grpc/support/alloc.h>
@@ -79,6 +80,8 @@ void TraceFlagList::LogAllTracers() {
79
80
 
80
81
  // Flags register themselves on the list during construction
81
82
  TraceFlag::TraceFlag(bool default_enabled, const char* name) : name_(name) {
83
+ static_assert(std::is_trivially_destructible<TraceFlag>::value,
84
+ "TraceFlag needs to be trivially destructible.");
82
85
  set_enabled(default_enabled);
83
86
  TraceFlagList::Add(this);
84
87
  }
@@ -53,7 +53,8 @@ void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
53
53
  class TraceFlag {
54
54
  public:
55
55
  TraceFlag(bool default_enabled, const char* name);
56
- ~TraceFlag() {}
56
+ // This needs to be trivially destructible as it is used as global variable.
57
+ ~TraceFlag() = default;
57
58
 
58
59
  const char* name() const { return name_; }
59
60
 
@@ -102,8 +103,9 @@ typedef TraceFlag DebugOnlyTraceFlag;
102
103
  #else
103
104
  class DebugOnlyTraceFlag {
104
105
  public:
105
- DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
106
- bool enabled() { return false; }
106
+ constexpr DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
107
+ constexpr bool enabled() const { return false; }
108
+ constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
107
109
 
108
110
  private:
109
111
  void set_enabled(bool enabled) {}
@@ -30,11 +30,18 @@
30
30
  // For debug of the timer manager crash only.
31
31
  // TODO (mxyan): remove after bug is fixed.
32
32
  #ifdef GRPC_DEBUG_TIMER_MANAGER
33
+ #include <string.h>
33
34
  void (*g_grpc_debug_timer_manager_stats)(
34
35
  int64_t timer_manager_init_count, int64_t timer_manager_shutdown_count,
35
36
  int64_t fork_count, int64_t timer_wait_err, int64_t timer_cv_value,
36
37
  int64_t timer_mu_value, int64_t abstime_sec_value,
37
- int64_t abstime_nsec_value) = nullptr;
38
+ int64_t abstime_nsec_value, int64_t abs_deadline_sec_value,
39
+ int64_t abs_deadline_nsec_value, int64_t now1_sec_value,
40
+ int64_t now1_nsec_value, int64_t now2_sec_value, int64_t now2_nsec_value,
41
+ int64_t add_result_sec_value, int64_t add_result_nsec_value,
42
+ int64_t sub_result_sec_value, int64_t sub_result_nsec_value,
43
+ int64_t next_value, int64_t start_time_sec,
44
+ int64_t start_time_nsec) = nullptr;
38
45
  int64_t g_timer_manager_init_count = 0;
39
46
  int64_t g_timer_manager_shutdown_count = 0;
40
47
  int64_t g_fork_count = 0;
@@ -43,6 +50,19 @@ int64_t g_timer_cv_value = 0;
43
50
  int64_t g_timer_mu_value = 0;
44
51
  int64_t g_abstime_sec_value = -1;
45
52
  int64_t g_abstime_nsec_value = -1;
53
+ int64_t g_abs_deadline_sec_value = -1;
54
+ int64_t g_abs_deadline_nsec_value = -1;
55
+ int64_t g_now1_sec_value = -1;
56
+ int64_t g_now1_nsec_value = -1;
57
+ int64_t g_now2_sec_value = -1;
58
+ int64_t g_now2_nsec_value = -1;
59
+ int64_t g_add_result_sec_value = -1;
60
+ int64_t g_add_result_nsec_value = -1;
61
+ int64_t g_sub_result_sec_value = -1;
62
+ int64_t g_sub_result_nsec_value = -1;
63
+ int64_t g_next_value = -1;
64
+ int64_t g_start_time_sec = -1;
65
+ int64_t g_start_time_nsec = -1;
46
66
  #endif // GRPC_DEBUG_TIMER_MANAGER
47
67
 
48
68
  #ifdef GPR_LOW_LEVEL_COUNTERS
@@ -90,17 +110,74 @@ void gpr_cv_init(gpr_cv* cv) {
90
110
 
91
111
  void gpr_cv_destroy(gpr_cv* cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); }
92
112
 
113
+ // For debug of the timer manager crash only.
114
+ // TODO (mxyan): remove after bug is fixed.
115
+ #ifdef GRPC_DEBUG_TIMER_MANAGER
116
+ static gpr_timespec gpr_convert_clock_type_debug_timespec(
117
+ gpr_timespec t, gpr_clock_type clock_type, gpr_timespec& now1,
118
+ gpr_timespec& now2, gpr_timespec& add_result, gpr_timespec& sub_result) {
119
+ if (t.clock_type == clock_type) {
120
+ return t;
121
+ }
122
+
123
+ if (t.tv_sec == INT64_MAX || t.tv_sec == INT64_MIN) {
124
+ t.clock_type = clock_type;
125
+ return t;
126
+ }
127
+
128
+ if (clock_type == GPR_TIMESPAN) {
129
+ return gpr_time_sub(t, gpr_now(t.clock_type));
130
+ }
131
+
132
+ if (t.clock_type == GPR_TIMESPAN) {
133
+ return gpr_time_add(gpr_now(clock_type), t);
134
+ }
135
+
136
+ now1 = gpr_now(t.clock_type);
137
+ sub_result = gpr_time_sub(t, now1);
138
+ now2 = gpr_now(clock_type);
139
+ add_result = gpr_time_add(now2, sub_result);
140
+ return add_result;
141
+ }
142
+
143
+ #define gpr_convert_clock_type_debug(t, clock_type, now1, now2, add_result, \
144
+ sub_result) \
145
+ gpr_convert_clock_type_debug_timespec((t), (clock_type), (now1), (now2), \
146
+ (add_result), (sub_result))
147
+ #else
148
+ #define gpr_convert_clock_type_debug(t, clock_type, now1, now2, add_result, \
149
+ sub_result) \
150
+ gpr_convert_clock_type((t), (clock_type))
151
+ #endif
152
+
93
153
  int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
94
154
  int err = 0;
155
+ #ifdef GRPC_DEBUG_TIMER_MANAGER
156
+ // For debug of the timer manager crash only.
157
+ // TODO (mxyan): remove after bug is fixed.
158
+ gpr_timespec abs_deadline_copy;
159
+ abs_deadline_copy.tv_sec = abs_deadline.tv_sec;
160
+ abs_deadline_copy.tv_nsec = abs_deadline.tv_nsec;
161
+ gpr_timespec now1;
162
+ gpr_timespec now2;
163
+ gpr_timespec add_result;
164
+ gpr_timespec sub_result;
165
+ memset(&now1, 0, sizeof(now1));
166
+ memset(&now2, 0, sizeof(now2));
167
+ memset(&add_result, 0, sizeof(add_result));
168
+ memset(&sub_result, 0, sizeof(sub_result));
169
+ #endif
95
170
  if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
96
171
  0) {
97
172
  err = pthread_cond_wait(cv, mu);
98
173
  } else {
99
174
  struct timespec abs_deadline_ts;
100
175
  #if GPR_LINUX
101
- abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_MONOTONIC);
176
+ abs_deadline = gpr_convert_clock_type_debug(
177
+ abs_deadline, GPR_CLOCK_MONOTONIC, now1, now2, add_result, sub_result);
102
178
  #else
103
- abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
179
+ abs_deadline = gpr_convert_clock_type_debug(
180
+ abs_deadline, GPR_CLOCK_REALTIME, now1, now2, add_result, sub_result);
104
181
  #endif // GPR_LINUX
105
182
  abs_deadline_ts.tv_sec = static_cast<time_t>(abs_deadline.tv_sec);
106
183
  abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
@@ -123,10 +200,25 @@ int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
123
200
  g_timer_wait_err = err;
124
201
  g_timer_cv_value = (int64_t)cv;
125
202
  g_timer_mu_value = (int64_t)mu;
203
+ g_abs_deadline_sec_value = abs_deadline_copy.tv_sec;
204
+ g_abs_deadline_nsec_value = abs_deadline_copy.tv_nsec;
205
+ g_now1_sec_value = now1.tv_sec;
206
+ g_now1_nsec_value = now1.tv_nsec;
207
+ g_now2_sec_value = now2.tv_sec;
208
+ g_now2_nsec_value = now2.tv_nsec;
209
+ g_add_result_sec_value = add_result.tv_sec;
210
+ g_add_result_nsec_value = add_result.tv_nsec;
211
+ g_sub_result_sec_value = sub_result.tv_sec;
212
+ g_sub_result_nsec_value = sub_result.tv_nsec;
126
213
  g_grpc_debug_timer_manager_stats(
127
214
  g_timer_manager_init_count, g_timer_manager_shutdown_count,
128
215
  g_fork_count, g_timer_wait_err, g_timer_cv_value, g_timer_mu_value,
129
- g_abstime_sec_value, g_abstime_nsec_value);
216
+ g_abstime_sec_value, g_abstime_nsec_value, g_abs_deadline_sec_value,
217
+ g_abs_deadline_nsec_value, g_now1_sec_value, g_now1_nsec_value,
218
+ g_now2_sec_value, g_now2_nsec_value, g_add_result_sec_value,
219
+ g_add_result_nsec_value, g_sub_result_sec_value,
220
+ g_sub_result_nsec_value, g_next_value, g_start_time_sec,
221
+ g_start_time_nsec);
130
222
  }
131
223
  }
132
224
  #endif
@@ -100,10 +100,7 @@ class InlinedVector {
100
100
  void reserve(size_t capacity) {
101
101
  if (capacity > capacity_) {
102
102
  T* new_dynamic = static_cast<T*>(gpr_malloc(sizeof(T) * capacity));
103
- for (size_t i = 0; i < size_; ++i) {
104
- new (&new_dynamic[i]) T(std::move(data()[i]));
105
- data()[i].~T();
106
- }
103
+ move_elements(data(), new_dynamic, size_);
107
104
  gpr_free(dynamic_);
108
105
  dynamic_ = new_dynamic;
109
106
  capacity_ = capacity;
@@ -131,13 +128,25 @@ class InlinedVector {
131
128
  size_--;
132
129
  }
133
130
 
131
+ size_t size() const { return size_; }
132
+ bool empty() const { return size_ == 0; }
133
+
134
+ size_t capacity() const { return capacity_; }
135
+
136
+ void clear() {
137
+ destroy_elements();
138
+ init_data();
139
+ }
140
+
141
+ private:
134
142
  void copy_from(const InlinedVector& v) {
135
- // if v is allocated, copy over the buffer.
143
+ // if v is allocated, make sure we have enough capacity.
136
144
  if (v.dynamic_ != nullptr) {
137
145
  reserve(v.capacity_);
138
- memcpy(dynamic_, v.dynamic_, v.size_ * sizeof(T));
139
- } else {
140
- memcpy(inline_, v.inline_, v.size_ * sizeof(T));
146
+ }
147
+ // copy over elements
148
+ for (size_t i = 0; i < v.size_; ++i) {
149
+ new (&(data()[i])) T(v[i]);
141
150
  }
142
151
  // copy over metadata
143
152
  size_ = v.size_;
@@ -145,11 +154,12 @@ class InlinedVector {
145
154
  }
146
155
 
147
156
  void move_from(InlinedVector& v) {
148
- // if v is allocated, then we steal its buffer, else we copy it.
157
+ // if v is allocated, then we steal its dynamic array; otherwise, we
158
+ // move the elements individually.
149
159
  if (v.dynamic_ != nullptr) {
150
160
  dynamic_ = v.dynamic_;
151
161
  } else {
152
- memcpy(inline_, v.inline_, v.size_ * sizeof(T));
162
+ move_elements(v.data(), data(), v.size_);
153
163
  }
154
164
  // copy over metadata
155
165
  size_ = v.size_;
@@ -158,17 +168,13 @@ class InlinedVector {
158
168
  v.init_data();
159
169
  }
160
170
 
161
- size_t size() const { return size_; }
162
- bool empty() const { return size_ == 0; }
163
-
164
- size_t capacity() const { return capacity_; }
165
-
166
- void clear() {
167
- destroy_elements();
168
- init_data();
171
+ static void move_elements(T* src, T* dst, size_t num_elements) {
172
+ for (size_t i = 0; i < num_elements; ++i) {
173
+ new (&dst[i]) T(std::move(src[i]));
174
+ src[i].~T();
175
+ }
169
176
  }
170
177
 
171
- private:
172
178
  void init_data() {
173
179
  dynamic_ = nullptr;
174
180
  size_ = 0;
@@ -40,15 +40,10 @@
40
40
 
41
41
  namespace grpc_core {
42
42
 
43
- // The alignment of memory returned by gpr_malloc().
44
- constexpr size_t kAlignmentForDefaultAllocationInBytes = 8;
45
-
46
43
  // Alternative to new, since we cannot use it (for fear of libstdc++)
47
44
  template <typename T, typename... Args>
48
45
  inline T* New(Args&&... args) {
49
- void* p = alignof(T) > kAlignmentForDefaultAllocationInBytes
50
- ? gpr_malloc_aligned(sizeof(T), alignof(T))
51
- : gpr_malloc(sizeof(T));
46
+ void* p = gpr_malloc(sizeof(T));
52
47
  return new (p) T(std::forward<Args>(args)...);
53
48
  }
54
49
 
@@ -57,11 +52,7 @@ template <typename T>
57
52
  inline void Delete(T* p) {
58
53
  if (p == nullptr) return;
59
54
  p->~T();
60
- if (alignof(T) > kAlignmentForDefaultAllocationInBytes) {
61
- gpr_free_aligned(p);
62
- } else {
63
- gpr_free(p);
64
- }
55
+ gpr_free(p);
65
56
  }
66
57
 
67
58
  template <typename T>
@@ -31,6 +31,7 @@
31
31
  #include "src/core/lib/gprpp/abstract.h"
32
32
  #include "src/core/lib/gprpp/debug_location.h"
33
33
  #include "src/core/lib/gprpp/memory.h"
34
+ #include "src/core/lib/gprpp/ref_counted.h"
34
35
  #include "src/core/lib/gprpp/ref_counted_ptr.h"
35
36
 
36
37
  namespace grpc_core {
@@ -89,107 +90,42 @@ class InternallyRefCounted : public Orphanable {
89
90
  template <typename T>
90
91
  friend class RefCountedPtr;
91
92
 
92
- InternallyRefCounted() { gpr_ref_init(&refs_, 1); }
93
- virtual ~InternallyRefCounted() {}
93
+ // TraceFlagT is defined to accept both DebugOnlyTraceFlag and TraceFlag.
94
+ // Note: RefCount tracing is only enabled on debug builds, even when a
95
+ // TraceFlag is used.
96
+ template <typename TraceFlagT = TraceFlag>
97
+ explicit InternallyRefCounted(TraceFlagT* trace_flag = nullptr)
98
+ : refs_(1, trace_flag) {}
99
+ virtual ~InternallyRefCounted() = default;
94
100
 
95
101
  RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
96
102
  IncrementRefCount();
97
103
  return RefCountedPtr<Child>(static_cast<Child*>(this));
98
104
  }
99
-
100
- void Unref() {
101
- if (gpr_unref(&refs_)) {
102
- Delete(static_cast<Child*>(this));
103
- }
104
- }
105
-
106
- private:
107
- void IncrementRefCount() { gpr_ref(&refs_); }
108
-
109
- gpr_refcount refs_;
110
- };
111
-
112
- // An alternative version of the InternallyRefCounted base class that
113
- // supports tracing. This is intended to be used in cases where the
114
- // object will be handled both by idiomatic C++ code using smart
115
- // pointers and legacy code that is manually calling Ref() and Unref().
116
- // Once all of our code is converted to idiomatic C++, we may be able to
117
- // eliminate this class.
118
- template <typename Child>
119
- class InternallyRefCountedWithTracing : public Orphanable {
120
- public:
121
- // Not copyable nor movable.
122
- InternallyRefCountedWithTracing(const InternallyRefCountedWithTracing&) =
123
- delete;
124
- InternallyRefCountedWithTracing& operator=(
125
- const InternallyRefCountedWithTracing&) = delete;
126
-
127
- GRPC_ABSTRACT_BASE_CLASS
128
-
129
- protected:
130
- GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
131
-
132
- // Allow RefCountedPtr<> to access Unref() and IncrementRefCount().
133
- template <typename T>
134
- friend class RefCountedPtr;
135
-
136
- InternallyRefCountedWithTracing()
137
- : InternallyRefCountedWithTracing(static_cast<TraceFlag*>(nullptr)) {}
138
-
139
- explicit InternallyRefCountedWithTracing(TraceFlag* trace_flag)
140
- : trace_flag_(trace_flag) {
141
- gpr_ref_init(&refs_, 1);
142
- }
143
-
144
- #ifdef NDEBUG
145
- explicit InternallyRefCountedWithTracing(DebugOnlyTraceFlag* trace_flag)
146
- : InternallyRefCountedWithTracing() {}
147
- #endif
148
-
149
- virtual ~InternallyRefCountedWithTracing() {}
150
-
151
- RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
152
- IncrementRefCount();
153
- return RefCountedPtr<Child>(static_cast<Child*>(this));
154
- }
155
-
156
105
  RefCountedPtr<Child> Ref(const DebugLocation& location,
157
106
  const char* reason) GRPC_MUST_USE_RESULT {
158
- if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) {
159
- gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count);
160
- gpr_log(GPR_INFO, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s",
161
- trace_flag_->name(), this, location.file(), location.line(),
162
- old_refs, old_refs + 1, reason);
163
- }
164
- return Ref();
107
+ IncrementRefCount(location, reason);
108
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
165
109
  }
166
110
 
167
- // TODO(roth): Once all of our code is converted to C++ and can use
168
- // RefCountedPtr<> instead of manual ref-counting, make the Unref() methods
169
- // private, since they will only be used by RefCountedPtr<>, which is a
170
- // friend of this class.
171
-
172
111
  void Unref() {
173
- if (gpr_unref(&refs_)) {
112
+ if (refs_.Unref()) {
174
113
  Delete(static_cast<Child*>(this));
175
114
  }
176
115
  }
177
-
178
116
  void Unref(const DebugLocation& location, const char* reason) {
179
- if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) {
180
- gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count);
181
- gpr_log(GPR_INFO, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s",
182
- trace_flag_->name(), this, location.file(), location.line(),
183
- old_refs, old_refs - 1, reason);
117
+ if (refs_.Unref(location, reason)) {
118
+ Delete(static_cast<Child*>(this));
184
119
  }
185
- Unref();
186
120
  }
187
121
 
188
122
  private:
189
- void IncrementRefCount() { gpr_ref(&refs_); }
123
+ void IncrementRefCount() { refs_.Ref(); }
124
+ void IncrementRefCount(const DebugLocation& location, const char* reason) {
125
+ refs_.Ref(location, reason);
126
+ }
190
127
 
191
- TraceFlag* trace_flag_ = nullptr;
192
- gpr_refcount refs_;
128
+ grpc_core::RefCount refs_;
193
129
  };
194
130
 
195
131
  } // namespace grpc_core