blob: 673e13ce6e92b67d62e3b1ec66ff9d69aaac8ed4 [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 Tiller28086682017-07-18 14:22:19 -070031#include "src/core/lib/debug/stats.h"
Craig Tiller9533d042016-03-25 17:11:06 -070032#include "src/core/lib/debug/trace.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070033#include "src/core/lib/http/parser.h"
Mark D. Roth764cf042017-09-01 09:00:06 -070034#include "src/core/lib/iomgr/call_combiner.h"
Craig Tillerc3df7b42016-07-18 15:51:26 -070035#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070036#include "src/core/lib/iomgr/executor.h"
37#include "src/core/lib/iomgr/iomgr.h"
Craig Tiller20afa3d2016-10-17 14:52:14 -070038#include "src/core/lib/iomgr/resource_quota.h"
Juanli Shen6502ecc2017-09-13 13:10:54 -070039#include "src/core/lib/iomgr/timer_manager.h"
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080041#include "src/core/lib/slice/slice_internal.h"
Sree Kuchibhotlaa8cf05c2017-06-22 15:08:55 -070042#include "src/core/lib/surface/alarm_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/surface/api_trace.h"
44#include "src/core/lib/surface/call.h"
45#include "src/core/lib/surface/channel_init.h"
46#include "src/core/lib/surface/completion_queue.h"
47#include "src/core/lib/surface/init.h"
48#include "src/core/lib/surface/lame_client.h"
49#include "src/core/lib/surface/server.h"
Craig Tillerefbd7c22017-01-27 14:07:44 -080050#include "src/core/lib/transport/bdp_estimator.h"
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/transport/connectivity_state.h"
52#include "src/core/lib/transport/transport_impl.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Craig Tiller3113ef42016-03-29 09:03:14 -070054/* (generated) built in registry of plugins */
55extern void grpc_register_built_in_plugins(void);
56
Hongwei Wang85ad6852015-08-13 16:13:10 -070057#define MAX_PLUGINS 128
58
Craig Tiller8ace0bc2015-03-05 17:16:09 -080059static gpr_once g_basic_init = GPR_ONCE_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -080060static gpr_mu g_init_mu;
61static int g_initializations;
62
Craig Tiller8ace0bc2015-03-05 17:16:09 -080063static void do_basic_init(void) {
Yuchen Zeng95e4c482016-04-15 10:38:24 -070064 gpr_log_verbosity_init();
Craig Tiller35108f62015-02-17 11:24:15 -080065 gpr_mu_init(&g_init_mu);
Craig Tiller3113ef42016-03-29 09:03:14 -070066 grpc_register_built_in_plugins();
Ken Payson42bd87e2017-10-20 10:32:30 -070067 grpc_cq_global_init();
Craig Tiller35108f62015-02-17 11:24:15 -080068 g_initializations = 0;
69}
70
Craig Tillerbaa14a92017-11-03 09:09:36 -070071static bool append_filter(grpc_exec_ctx* exec_ctx,
72 grpc_channel_stack_builder* builder, void* arg) {
vjpaie7077b52016-03-21 20:58:44 -070073 return grpc_channel_stack_builder_append_filter(
Craig Tiller4782d922017-11-10 09:53:21 -080074 builder, (const grpc_channel_filter*)arg, nullptr, nullptr);
Craig Tiller178edfa2016-02-17 20:54:46 -080075}
76
Craig Tillerbaa14a92017-11-03 09:09:36 -070077static bool prepend_filter(grpc_exec_ctx* exec_ctx,
78 grpc_channel_stack_builder* builder, void* arg) {
vjpaie7077b52016-03-21 20:58:44 -070079 return grpc_channel_stack_builder_prepend_filter(
Craig Tiller4782d922017-11-10 09:53:21 -080080 builder, (const grpc_channel_filter*)arg, nullptr, nullptr);
Craig Tiller178edfa2016-02-17 20:54:46 -080081}
82
Craig Tiller178edfa2016-02-17 20:54:46 -080083static void register_builtin_channel_init() {
Craig Tillerf82ddc42016-04-05 17:15:07 -070084 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
85 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller4782d922017-11-10 09:53:21 -080086 grpc_add_connected_filter, nullptr);
Craig Tillerf82ddc42016-04-05 17:15:07 -070087 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
88 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller4782d922017-11-10 09:53:21 -080089 grpc_add_connected_filter, nullptr);
Craig Tillerf82ddc42016-04-05 17:15:07 -070090 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
91 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller4782d922017-11-10 09:53:21 -080092 grpc_add_connected_filter, nullptr);
Craig Tillerf82ddc42016-04-05 17:15:07 -070093 grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL,
94 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tillerbaa14a92017-11-03 09:09:36 -070095 append_filter, (void*)&grpc_lame_filter);
Craig Tiller81667322016-04-06 07:47:01 -070096 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 (void*)&grpc_server_top_filter);
Craig Tiller178edfa2016-02-17 20:54:46 -080098}
99
Hongwei Wanga3780a82015-07-17 15:27:18 -0700100typedef struct grpc_plugin {
101 void (*init)();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700102 void (*destroy)();
Hongwei Wanga3780a82015-07-17 15:27:18 -0700103} grpc_plugin;
104
Hongwei Wang85ad6852015-08-13 16:13:10 -0700105static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
106static int g_number_of_plugins = 0;
Hongwei Wanga3780a82015-07-17 15:27:18 -0700107
Hongwei Wang85ad6852015-08-13 16:13:10 -0700108void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
Jan Tattermusch1b34aeb2015-12-17 10:37:10 -0800109 GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700110 ((void*)(intptr_t)init, (void*)(intptr_t)destroy));
Hongwei Wang85ad6852015-08-13 16:13:10 -0700111 GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
112 g_all_of_the_plugins[g_number_of_plugins].init = init;
113 g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
114 g_number_of_plugins++;
Hongwei Wangff6097a2015-08-12 17:48:56 -0700115}
116
Craig Tiller32946d32015-01-15 11:37:30 -0800117void grpc_init(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700118 int i;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800119 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller35108f62015-02-17 11:24:15 -0800120
Craig Tiller5e56f002017-05-16 15:02:50 -0700121 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -0800122 gpr_mu_lock(&g_init_mu);
123 if (++g_initializations == 1) {
Craig Tillerf3756c12015-07-01 17:21:01 -0700124 gpr_time_init();
Craig Tiller28086682017-07-18 14:22:19 -0700125 grpc_stats_init();
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800126 grpc_slice_intern_init();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800127 grpc_mdctx_global_init();
Craig Tiller178edfa2016-02-17 20:54:46 -0800128 grpc_channel_init_init();
ncteisen06bce6e2017-07-10 07:58:49 -0700129 grpc_register_tracer(&grpc_api_trace);
130 grpc_register_tracer(&grpc_trace_channel);
131 grpc_register_tracer(&grpc_connectivity_state_trace);
ncteisen7712c7c2017-07-12 23:11:27 -0700132 grpc_register_tracer(&grpc_trace_channel_stack_builder);
ncteisen06bce6e2017-07-10 07:58:49 -0700133 grpc_register_tracer(&grpc_http1_trace);
134 grpc_register_tracer(&grpc_cq_pluck_trace); // default on
Mark D. Roth764cf042017-09-01 09:00:06 -0700135 grpc_register_tracer(&grpc_call_combiner_trace);
ncteisen06bce6e2017-07-10 07:58:49 -0700136 grpc_register_tracer(&grpc_combiner_trace);
137 grpc_register_tracer(&grpc_server_channel_trace);
138 grpc_register_tracer(&grpc_bdp_estimator_trace);
ncteisen7712c7c2017-07-12 23:11:27 -0700139 grpc_register_tracer(&grpc_cq_event_timeout_trace); // default on
ncteisen06bce6e2017-07-10 07:58:49 -0700140 grpc_register_tracer(&grpc_trace_operation_failures);
141 grpc_register_tracer(&grpc_resource_quota_trace);
142 grpc_register_tracer(&grpc_call_error_trace);
Craig Tiller4e41e362016-08-19 13:12:54 -0700143#ifndef NDEBUG
ncteisen06bce6e2017-07-10 07:58:49 -0700144 grpc_register_tracer(&grpc_trace_pending_tags);
Sree Kuchibhotla59beeff2017-07-25 14:08:33 -0700145 grpc_register_tracer(&grpc_trace_alarm_refcount);
ncteisen06bce6e2017-07-10 07:58:49 -0700146 grpc_register_tracer(&grpc_trace_cq_refcount);
147 grpc_register_tracer(&grpc_trace_closure);
148 grpc_register_tracer(&grpc_trace_error_refcount);
149 grpc_register_tracer(&grpc_trace_stream_refcount);
150 grpc_register_tracer(&grpc_trace_fd_refcount);
151 grpc_register_tracer(&grpc_trace_metadata);
Craig Tiller4e41e362016-08-19 13:12:54 -0700152#endif
Craig Tillerfaa84802015-03-01 21:56:38 -0800153 grpc_security_pre_init();
Craig Tiller5e56f002017-05-16 15:02:50 -0700154 grpc_iomgr_init(&exec_ctx);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700155 gpr_timers_global_init();
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800156 grpc_handshaker_factory_registry_init();
157 grpc_security_init();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700158 for (i = 0; i < g_number_of_plugins; i++) {
Craig Tiller4782d922017-11-10 09:53:21 -0800159 if (g_all_of_the_plugins[i].init != nullptr) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700160 g_all_of_the_plugins[i].init();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700161 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700162 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800163 /* register channel finalization AFTER all plugins, to ensure that it's run
164 * at the appropriate time */
165 grpc_register_security_filters();
166 register_builtin_channel_init();
David Garcia Quintaseb913102016-04-25 17:30:04 -0700167 grpc_tracer_init("GRPC_TRACE");
Craig Tiller178edfa2016-02-17 20:54:46 -0800168 /* no more changes to channel init pipelines */
169 grpc_channel_init_finalize();
Craig Tiller5e56f002017-05-16 15:02:50 -0700170 grpc_iomgr_start(&exec_ctx);
Craig Tiller35108f62015-02-17 11:24:15 -0800171 }
172 gpr_mu_unlock(&g_init_mu);
Craig Tiller5e56f002017-05-16 15:02:50 -0700173 grpc_exec_ctx_finish(&exec_ctx);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700174 GRPC_API_TRACE("grpc_init(void)", 0, ());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175}
176
Craig Tiller32946d32015-01-15 11:37:30 -0800177void grpc_shutdown(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700178 int i;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700179 GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
Craig Tillerb9b01ce2017-05-12 13:47:10 -0700180 grpc_exec_ctx exec_ctx =
Craig Tiller4782d922017-11-10 09:53:21 -0800181 GRPC_EXEC_CTX_INITIALIZER(0, grpc_never_ready_to_finish, nullptr);
Craig Tiller35108f62015-02-17 11:24:15 -0800182 gpr_mu_lock(&g_init_mu);
183 if (--g_initializations == 0) {
Juanli Shen6502ecc2017-09-13 13:10:54 -0700184 grpc_executor_shutdown(&exec_ctx);
185 grpc_timer_manager_set_threading(false); // shutdown timer_manager thread
Craig Tillerf82ddc42016-04-05 17:15:07 -0700186 for (i = g_number_of_plugins; i >= 0; i--) {
Craig Tiller4782d922017-11-10 09:53:21 -0800187 if (g_all_of_the_plugins[i].destroy != nullptr) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700188 g_all_of_the_plugins[i].destroy();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700189 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700190 }
Juanli Shen6502ecc2017-09-13 13:10:54 -0700191 grpc_iomgr_shutdown(&exec_ctx);
192 gpr_timers_global_destroy();
193 grpc_tracer_shutdown();
Craig Tillera59c16c2016-10-31 07:25:01 -0700194 grpc_mdctx_global_shutdown(&exec_ctx);
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800195 grpc_handshaker_factory_registry_shutdown(&exec_ctx);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800196 grpc_slice_intern_shutdown();
Craig Tiller28086682017-07-18 14:22:19 -0700197 grpc_stats_shutdown();
Craig Tiller35108f62015-02-17 11:24:15 -0800198 }
199 gpr_mu_unlock(&g_init_mu);
Craig Tillera59c16c2016-10-31 07:25:01 -0700200 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201}
Craig Tiller60fd3612015-03-05 16:24:22 -0800202
203int grpc_is_initialized(void) {
204 int r;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800205 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller60fd3612015-03-05 16:24:22 -0800206 gpr_mu_lock(&g_init_mu);
207 r = g_initializations > 0;
208 gpr_mu_unlock(&g_init_mu);
209 return r;
210}