init.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. *
  3. * Copyright 2015 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. #include <grpc/support/port_platform.h>
  19. #include <limits.h>
  20. #include <memory.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/time.h>
  25. #include "src/core/lib/channel/channel_stack.h"
  26. #include "src/core/lib/channel/connected_channel.h"
  27. #include "src/core/lib/channel/handshaker_registry.h"
  28. #include "src/core/lib/debug/stats.h"
  29. #include "src/core/lib/debug/trace.h"
  30. #include "src/core/lib/http/parser.h"
  31. #include "src/core/lib/iomgr/call_combiner.h"
  32. #include "src/core/lib/iomgr/combiner.h"
  33. #include "src/core/lib/iomgr/executor.h"
  34. #include "src/core/lib/iomgr/iomgr.h"
  35. #include "src/core/lib/iomgr/resource_quota.h"
  36. #include "src/core/lib/iomgr/timer_manager.h"
  37. #include "src/core/lib/profiling/timers.h"
  38. #include "src/core/lib/slice/slice_internal.h"
  39. #include "src/core/lib/surface/alarm_internal.h"
  40. #include "src/core/lib/surface/api_trace.h"
  41. #include "src/core/lib/surface/call.h"
  42. #include "src/core/lib/surface/channel_init.h"
  43. #include "src/core/lib/surface/completion_queue.h"
  44. #include "src/core/lib/surface/init.h"
  45. #include "src/core/lib/surface/lame_client.h"
  46. #include "src/core/lib/surface/server.h"
  47. #include "src/core/lib/transport/bdp_estimator.h"
  48. #include "src/core/lib/transport/connectivity_state.h"
  49. #include "src/core/lib/transport/transport_impl.h"
  50. /* (generated) built in registry of plugins */
  51. extern void grpc_register_built_in_plugins(void);
  52. #define MAX_PLUGINS 128
  53. static gpr_once g_basic_init = GPR_ONCE_INIT;
  54. static gpr_mu g_init_mu;
  55. static int g_initializations;
  56. static void do_basic_init(void) {
  57. gpr_log_verbosity_init();
  58. gpr_mu_init(&g_init_mu);
  59. grpc_register_built_in_plugins();
  60. grpc_cq_global_init();
  61. g_initializations = 0;
  62. }
  63. static bool append_filter(grpc_exec_ctx *exec_ctx,
  64. grpc_channel_stack_builder *builder, void *arg) {
  65. return grpc_channel_stack_builder_append_filter(
  66. builder, (const grpc_channel_filter *)arg, NULL, NULL);
  67. }
  68. static bool prepend_filter(grpc_exec_ctx *exec_ctx,
  69. grpc_channel_stack_builder *builder, void *arg) {
  70. return grpc_channel_stack_builder_prepend_filter(
  71. builder, (const grpc_channel_filter *)arg, NULL, NULL);
  72. }
  73. static void register_builtin_channel_init() {
  74. grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
  75. GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
  76. grpc_add_connected_filter, NULL);
  77. grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
  78. GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
  79. grpc_add_connected_filter, NULL);
  80. grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
  81. GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
  82. grpc_add_connected_filter, NULL);
  83. grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL,
  84. GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
  85. append_filter, (void *)&grpc_lame_filter);
  86. grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
  87. (void *)&grpc_server_top_filter);
  88. }
  89. typedef struct grpc_plugin {
  90. void (*init)();
  91. void (*destroy)();
  92. } grpc_plugin;
  93. static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
  94. static int g_number_of_plugins = 0;
  95. void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
  96. GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
  97. ((void *)(intptr_t)init, (void *)(intptr_t)destroy));
  98. GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
  99. g_all_of_the_plugins[g_number_of_plugins].init = init;
  100. g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
  101. g_number_of_plugins++;
  102. }
  103. void grpc_init(void) {
  104. int i;
  105. gpr_once_init(&g_basic_init, do_basic_init);
  106. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  107. gpr_mu_lock(&g_init_mu);
  108. if (++g_initializations == 1) {
  109. gpr_time_init();
  110. grpc_stats_init();
  111. grpc_slice_intern_init();
  112. grpc_mdctx_global_init();
  113. grpc_channel_init_init();
  114. grpc_register_tracer(&grpc_api_trace);
  115. grpc_register_tracer(&grpc_trace_channel);
  116. grpc_register_tracer(&grpc_connectivity_state_trace);
  117. grpc_register_tracer(&grpc_trace_channel_stack_builder);
  118. grpc_register_tracer(&grpc_http1_trace);
  119. grpc_register_tracer(&grpc_cq_pluck_trace); // default on
  120. grpc_register_tracer(&grpc_call_combiner_trace);
  121. grpc_register_tracer(&grpc_combiner_trace);
  122. grpc_register_tracer(&grpc_server_channel_trace);
  123. grpc_register_tracer(&grpc_bdp_estimator_trace);
  124. grpc_register_tracer(&grpc_cq_event_timeout_trace); // default on
  125. grpc_register_tracer(&grpc_trace_operation_failures);
  126. grpc_register_tracer(&grpc_resource_quota_trace);
  127. grpc_register_tracer(&grpc_call_error_trace);
  128. #ifndef NDEBUG
  129. grpc_register_tracer(&grpc_trace_pending_tags);
  130. grpc_register_tracer(&grpc_trace_alarm_refcount);
  131. grpc_register_tracer(&grpc_trace_cq_refcount);
  132. grpc_register_tracer(&grpc_trace_closure);
  133. grpc_register_tracer(&grpc_trace_error_refcount);
  134. grpc_register_tracer(&grpc_trace_stream_refcount);
  135. grpc_register_tracer(&grpc_trace_fd_refcount);
  136. grpc_register_tracer(&grpc_trace_metadata);
  137. #endif
  138. grpc_security_pre_init();
  139. grpc_iomgr_init(&exec_ctx);
  140. gpr_timers_global_init();
  141. grpc_handshaker_factory_registry_init();
  142. grpc_security_init();
  143. for (i = 0; i < g_number_of_plugins; i++) {
  144. if (g_all_of_the_plugins[i].init != NULL) {
  145. g_all_of_the_plugins[i].init();
  146. }
  147. }
  148. /* register channel finalization AFTER all plugins, to ensure that it's run
  149. * at the appropriate time */
  150. grpc_register_security_filters();
  151. register_builtin_channel_init();
  152. grpc_tracer_init("GRPC_TRACE");
  153. /* no more changes to channel init pipelines */
  154. grpc_channel_init_finalize();
  155. grpc_iomgr_start(&exec_ctx);
  156. }
  157. gpr_mu_unlock(&g_init_mu);
  158. grpc_exec_ctx_finish(&exec_ctx);
  159. GRPC_API_TRACE("grpc_init(void)", 0, ());
  160. }
  161. void grpc_shutdown(void) {
  162. int i;
  163. GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
  164. grpc_exec_ctx exec_ctx =
  165. GRPC_EXEC_CTX_INITIALIZER(0, grpc_never_ready_to_finish, NULL);
  166. gpr_mu_lock(&g_init_mu);
  167. if (--g_initializations == 0) {
  168. grpc_executor_shutdown(&exec_ctx);
  169. grpc_timer_manager_set_threading(false); // shutdown timer_manager thread
  170. for (i = g_number_of_plugins; i >= 0; i--) {
  171. if (g_all_of_the_plugins[i].destroy != NULL) {
  172. g_all_of_the_plugins[i].destroy();
  173. }
  174. }
  175. grpc_iomgr_shutdown(&exec_ctx);
  176. gpr_timers_global_destroy();
  177. grpc_tracer_shutdown();
  178. grpc_mdctx_global_shutdown(&exec_ctx);
  179. grpc_handshaker_factory_registry_shutdown(&exec_ctx);
  180. grpc_slice_intern_shutdown();
  181. grpc_stats_shutdown();
  182. }
  183. gpr_mu_unlock(&g_init_mu);
  184. grpc_exec_ctx_finish(&exec_ctx);
  185. }
  186. int grpc_is_initialized(void) {
  187. int r;
  188. gpr_once_init(&g_basic_init, do_basic_init);
  189. gpr_mu_lock(&g_init_mu);
  190. r = g_initializations > 0;
  191. gpr_mu_unlock(&g_init_mu);
  192. return r;
  193. }