blob: 14a86bfa0a8517d43d36f57f3ee18bcf59c007f7 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Craig Tilleracf0f072015-06-29 08:24:16 -070019#include <grpc/support/port_platform.h>
20
Craig Tiller178edfa2016-02-17 20:54:46 -080021#include <limits.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070022#include <memory.h>
23
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080024#include <grpc/grpc.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070025#include <grpc/support/alloc.h>
Yuchen Zeng95e4c482016-04-15 10:38:24 -070026#include <grpc/support/log.h>
Craig Tillerf3756c12015-07-01 17:21:01 -070027#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070028#include "src/core/lib/channel/channel_stack.h"
Craig Tiller9533d042016-03-25 17:11:06 -070029#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080030#include "src/core/lib/channel/handshaker_registry.h"
Craig Tiller9533d042016-03-25 17:11:06 -070031#include "src/core/lib/debug/trace.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070032#include "src/core/lib/http/parser.h"
Craig Tillerc3df7b42016-07-18 15:51:26 -070033#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/iomgr/executor.h"
35#include "src/core/lib/iomgr/iomgr.h"
Craig Tiller20afa3d2016-10-17 14:52:14 -070036#include "src/core/lib/iomgr/resource_quota.h"
Craig Tiller9533d042016-03-25 17:11:06 -070037#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080038#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/surface/api_trace.h"
40#include "src/core/lib/surface/call.h"
41#include "src/core/lib/surface/channel_init.h"
42#include "src/core/lib/surface/completion_queue.h"
43#include "src/core/lib/surface/init.h"
44#include "src/core/lib/surface/lame_client.h"
45#include "src/core/lib/surface/server.h"
Craig Tillerefbd7c22017-01-27 14:07:44 -080046#include "src/core/lib/transport/bdp_estimator.h"
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/transport/connectivity_state.h"
48#include "src/core/lib/transport/transport_impl.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
Craig Tiller3113ef42016-03-29 09:03:14 -070050/* (generated) built in registry of plugins */
51extern void grpc_register_built_in_plugins(void);
52
Hongwei Wang85ad6852015-08-13 16:13:10 -070053#define MAX_PLUGINS 128
54
Craig Tiller8ace0bc2015-03-05 17:16:09 -080055static gpr_once g_basic_init = GPR_ONCE_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -080056static gpr_mu g_init_mu;
57static int g_initializations;
58
Craig Tiller8ace0bc2015-03-05 17:16:09 -080059static void do_basic_init(void) {
Yuchen Zeng95e4c482016-04-15 10:38:24 -070060 gpr_log_verbosity_init();
Craig Tiller35108f62015-02-17 11:24:15 -080061 gpr_mu_init(&g_init_mu);
Craig Tiller3113ef42016-03-29 09:03:14 -070062 grpc_register_built_in_plugins();
Craig Tiller35108f62015-02-17 11:24:15 -080063 g_initializations = 0;
64}
65
Craig Tiller87a7e1f2016-11-09 09:42:19 -080066static bool append_filter(grpc_exec_ctx *exec_ctx,
67 grpc_channel_stack_builder *builder, void *arg) {
vjpaie7077b52016-03-21 20:58:44 -070068 return grpc_channel_stack_builder_append_filter(
69 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080070}
71
Craig Tiller87a7e1f2016-11-09 09:42:19 -080072static bool prepend_filter(grpc_exec_ctx *exec_ctx,
73 grpc_channel_stack_builder *builder, void *arg) {
vjpaie7077b52016-03-21 20:58:44 -070074 return grpc_channel_stack_builder_prepend_filter(
75 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080076}
77
Craig Tiller178edfa2016-02-17 20:54:46 -080078static void register_builtin_channel_init() {
Craig Tillerf82ddc42016-04-05 17:15:07 -070079 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
80 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller178edfa2016-02-17 20:54:46 -080081 grpc_add_connected_filter, NULL);
Craig Tillerf82ddc42016-04-05 17:15:07 -070082 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
83 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tillerde676262016-02-19 12:28:34 -080084 grpc_add_connected_filter, NULL);
Craig Tillerf82ddc42016-04-05 17:15:07 -070085 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
86 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller178edfa2016-02-17 20:54:46 -080087 grpc_add_connected_filter, NULL);
Craig Tillerf82ddc42016-04-05 17:15:07 -070088 grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL,
89 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller178edfa2016-02-17 20:54:46 -080090 append_filter, (void *)&grpc_lame_filter);
Craig Tiller81667322016-04-06 07:47:01 -070091 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
92 (void *)&grpc_server_top_filter);
Craig Tiller178edfa2016-02-17 20:54:46 -080093}
94
Hongwei Wanga3780a82015-07-17 15:27:18 -070095typedef struct grpc_plugin {
96 void (*init)();
Hongwei Wang85ad6852015-08-13 16:13:10 -070097 void (*destroy)();
Hongwei Wanga3780a82015-07-17 15:27:18 -070098} grpc_plugin;
99
Hongwei Wang85ad6852015-08-13 16:13:10 -0700100static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
101static int g_number_of_plugins = 0;
Hongwei Wanga3780a82015-07-17 15:27:18 -0700102
Hongwei Wang85ad6852015-08-13 16:13:10 -0700103void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
Jan Tattermusch1b34aeb2015-12-17 10:37:10 -0800104 GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
Craig Tiller178edfa2016-02-17 20:54:46 -0800105 ((void *)(intptr_t)init, (void *)(intptr_t)destroy));
Hongwei Wang85ad6852015-08-13 16:13:10 -0700106 GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
107 g_all_of_the_plugins[g_number_of_plugins].init = init;
108 g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
109 g_number_of_plugins++;
Hongwei Wangff6097a2015-08-12 17:48:56 -0700110}
111
Craig Tiller32946d32015-01-15 11:37:30 -0800112void grpc_init(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700113 int i;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800114 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller35108f62015-02-17 11:24:15 -0800115
Craig Tiller5e56f002017-05-16 15:02:50 -0700116 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -0800117 gpr_mu_lock(&g_init_mu);
118 if (++g_initializations == 1) {
Craig Tillerf3756c12015-07-01 17:21:01 -0700119 gpr_time_init();
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800120 grpc_slice_intern_init();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800121 grpc_mdctx_global_init();
Craig Tiller178edfa2016-02-17 20:54:46 -0800122 grpc_channel_init_init();
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700123 grpc_register_tracer("api", &grpc_api_trace);
Craig Tillerfaa84802015-03-01 21:56:38 -0800124 grpc_register_tracer("channel", &grpc_trace_channel);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700125 grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace);
Craig Tillerd78d1642016-02-21 22:18:51 -0800126 grpc_register_tracer("channel_stack_builder",
127 &grpc_trace_channel_stack_builder);
Craig Tiller3ab2fe02016-04-11 20:11:18 -0700128 grpc_register_tracer("http1", &grpc_http1_trace);
ncteisen3bc25582017-06-09 10:35:35 -0700129 grpc_register_tracer("queue_pluck", &grpc_cq_pluck_trace); // default on
Craig Tillerc3df7b42016-07-18 15:51:26 -0700130 grpc_register_tracer("combiner", &grpc_combiner_trace);
Craig Tiller687a0e62016-09-21 10:48:59 -0700131 grpc_register_tracer("server_channel", &grpc_server_channel_trace);
Craig Tillerefbd7c22017-01-27 14:07:44 -0800132 grpc_register_tracer("bdp_estimator", &grpc_bdp_estimator_trace);
ncteisen3bc25582017-06-09 10:35:35 -0700133 grpc_register_tracer("queue_timeout",
134 &grpc_cq_event_timeout_trace); // default on
Craig Tillera286b042016-06-13 15:20:39 +0000135 grpc_register_tracer("op_failure", &grpc_trace_operation_failures);
Craig Tiller20afa3d2016-10-17 14:52:14 -0700136 grpc_register_tracer("resource_quota", &grpc_resource_quota_trace);
Craig Tiller58b30cd2017-01-31 17:07:36 -0800137 grpc_register_tracer("call_error", &grpc_call_error_trace);
Craig Tiller4e41e362016-08-19 13:12:54 -0700138#ifndef NDEBUG
139 grpc_register_tracer("pending_tags", &grpc_trace_pending_tags);
ncteisend39010e2017-06-08 17:08:07 -0700140 grpc_register_tracer("queue_refcount", &grpc_trace_cq_refcount);
ncteisenf0c46e32017-06-08 15:57:09 -0700141 grpc_register_tracer("closure", &grpc_trace_closure);
142 grpc_register_tracer("error_refcount", &grpc_trace_error_refcount);
ncteisen9c43fc02017-06-08 16:06:23 -0700143 grpc_register_tracer("stream_refcount", &grpc_trace_stream_refcount);
ncteisend39010e2017-06-08 17:08:07 -0700144 grpc_register_tracer("fd_refcount", &grpc_trace_fd_refcount);
ncteisen4b584052017-06-08 16:44:38 -0700145 grpc_register_tracer("metadata", &grpc_trace_metadata);
Craig Tiller4e41e362016-08-19 13:12:54 -0700146#endif
Craig Tillerfaa84802015-03-01 21:56:38 -0800147 grpc_security_pre_init();
Craig Tiller5e56f002017-05-16 15:02:50 -0700148 grpc_iomgr_init(&exec_ctx);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700149 gpr_timers_global_init();
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800150 grpc_handshaker_factory_registry_init();
151 grpc_security_init();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700152 for (i = 0; i < g_number_of_plugins; i++) {
153 if (g_all_of_the_plugins[i].init != NULL) {
154 g_all_of_the_plugins[i].init();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700155 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700156 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800157 /* register channel finalization AFTER all plugins, to ensure that it's run
158 * at the appropriate time */
159 grpc_register_security_filters();
160 register_builtin_channel_init();
David Garcia Quintaseb913102016-04-25 17:30:04 -0700161 grpc_tracer_init("GRPC_TRACE");
Craig Tiller178edfa2016-02-17 20:54:46 -0800162 /* no more changes to channel init pipelines */
163 grpc_channel_init_finalize();
Craig Tiller5e56f002017-05-16 15:02:50 -0700164 grpc_iomgr_start(&exec_ctx);
Craig Tiller35108f62015-02-17 11:24:15 -0800165 }
166 gpr_mu_unlock(&g_init_mu);
Craig Tiller5e56f002017-05-16 15:02:50 -0700167 grpc_exec_ctx_finish(&exec_ctx);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700168 GRPC_API_TRACE("grpc_init(void)", 0, ());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169}
170
Craig Tiller32946d32015-01-15 11:37:30 -0800171void grpc_shutdown(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700172 int i;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700173 GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
Craig Tillerb9b01ce2017-05-12 13:47:10 -0700174 grpc_exec_ctx exec_ctx =
175 GRPC_EXEC_CTX_INITIALIZER(0, grpc_never_ready_to_finish, NULL);
Craig Tiller35108f62015-02-17 11:24:15 -0800176 gpr_mu_lock(&g_init_mu);
177 if (--g_initializations == 0) {
Craig Tiller3cf79222016-11-14 08:02:45 -0800178 grpc_iomgr_shutdown(&exec_ctx);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700179 gpr_timers_global_destroy();
Craig Tiller2f300e22015-06-04 08:28:43 -0700180 grpc_tracer_shutdown();
Craig Tillerf82ddc42016-04-05 17:15:07 -0700181 for (i = g_number_of_plugins; i >= 0; i--) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700182 if (g_all_of_the_plugins[i].destroy != NULL) {
183 g_all_of_the_plugins[i].destroy();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700184 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700185 }
Craig Tillera59c16c2016-10-31 07:25:01 -0700186 grpc_mdctx_global_shutdown(&exec_ctx);
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800187 grpc_handshaker_factory_registry_shutdown(&exec_ctx);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800188 grpc_slice_intern_shutdown();
Craig Tiller35108f62015-02-17 11:24:15 -0800189 }
190 gpr_mu_unlock(&g_init_mu);
Craig Tillera59c16c2016-10-31 07:25:01 -0700191 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192}
Craig Tiller60fd3612015-03-05 16:24:22 -0800193
194int grpc_is_initialized(void) {
195 int r;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800196 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller60fd3612015-03-05 16:24:22 -0800197 gpr_mu_lock(&g_init_mu);
198 r = g_initializations > 0;
199 gpr_mu_unlock(&g_init_mu);
200 return r;
201}