blob: 0ad82fed99ee40e48de18f7f5d23f5a73c234f45 [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 "Pixel" Noble18a68372017-11-20 23:33:22 +010024#include <grpc/fork.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025#include <grpc/grpc.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070026#include <grpc/support/alloc.h>
Yuchen Zeng95e4c482016-04-15 10:38:24 -070027#include <grpc/support/log.h>
Craig Tillerf3756c12015-07-01 17:21:01 -070028#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070029#include "src/core/lib/channel/channel_stack.h"
ncteisenbba88402018-05-11 11:54:41 -040030#include "src/core/lib/channel/channelz_registry.h"
Craig Tiller9533d042016-03-25 17:11:06 -070031#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080032#include "src/core/lib/channel/handshaker_registry.h"
Craig Tiller28086682017-07-18 14:22:19 -070033#include "src/core/lib/debug/stats.h"
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/debug/trace.h"
kpayson64701e70d2018-04-30 17:40:00 -070035#include "src/core/lib/gprpp/fork.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070036#include "src/core/lib/http/parser.h"
Mark D. Roth764cf042017-09-01 09:00:06 -070037#include "src/core/lib/iomgr/call_combiner.h"
Craig Tillerc3df7b42016-07-18 15:51:26 -070038#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/iomgr/executor.h"
40#include "src/core/lib/iomgr/iomgr.h"
Craig Tiller20afa3d2016-10-17 14:52:14 -070041#include "src/core/lib/iomgr/resource_quota.h"
Juanli Shen6502ecc2017-09-13 13:10:54 -070042#include "src/core/lib/iomgr/timer_manager.h"
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080044#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/surface/api_trace.h"
46#include "src/core/lib/surface/call.h"
47#include "src/core/lib/surface/channel_init.h"
48#include "src/core/lib/surface/completion_queue.h"
49#include "src/core/lib/surface/init.h"
50#include "src/core/lib/surface/lame_client.h"
51#include "src/core/lib/surface/server.h"
Craig Tillerefbd7c22017-01-27 14:07:44 -080052#include "src/core/lib/transport/bdp_estimator.h"
Craig Tiller9533d042016-03-25 17:11:06 -070053#include "src/core/lib/transport/connectivity_state.h"
54#include "src/core/lib/transport/transport_impl.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055
Craig Tiller3113ef42016-03-29 09:03:14 -070056/* (generated) built in registry of plugins */
57extern void grpc_register_built_in_plugins(void);
58
Hongwei Wang85ad6852015-08-13 16:13:10 -070059#define MAX_PLUGINS 128
60
Craig Tiller8ace0bc2015-03-05 17:16:09 -080061static gpr_once g_basic_init = GPR_ONCE_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -080062static gpr_mu g_init_mu;
63static int g_initializations;
64
Craig Tiller8ace0bc2015-03-05 17:16:09 -080065static void do_basic_init(void) {
Yuchen Zeng95e4c482016-04-15 10:38:24 -070066 gpr_log_verbosity_init();
Craig Tiller35108f62015-02-17 11:24:15 -080067 gpr_mu_init(&g_init_mu);
Craig Tiller3113ef42016-03-29 09:03:14 -070068 grpc_register_built_in_plugins();
Ken Payson42bd87e2017-10-20 10:32:30 -070069 grpc_cq_global_init();
Craig Tiller35108f62015-02-17 11:24:15 -080070 g_initializations = 0;
71}
72
Yash Tibrewal8cf14702017-12-06 09:47:54 -080073static bool append_filter(grpc_channel_stack_builder* builder, void* arg) {
vjpaie7077b52016-03-21 20:58:44 -070074 return grpc_channel_stack_builder_append_filter(
Noah Eisenbe82e642018-02-09 09:16:55 -080075 builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
Craig Tiller178edfa2016-02-17 20:54:46 -080076}
77
Yash Tibrewal8cf14702017-12-06 09:47:54 -080078static bool prepend_filter(grpc_channel_stack_builder* builder, void* arg) {
vjpaie7077b52016-03-21 20:58:44 -070079 return grpc_channel_stack_builder_prepend_filter(
Noah Eisenbe82e642018-02-09 09:16:55 -080080 builder, static_cast<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
121 gpr_mu_lock(&g_init_mu);
122 if (++g_initializations == 1) {
kpayson64701e70d2018-04-30 17:40:00 -0700123 grpc_core::Fork::GlobalInit();
kpayson6438ab21e2018-03-08 15:47:40 -0800124 grpc_fork_handlers_auto_register();
Craig Tillerf3756c12015-07-01 17:21:01 -0700125 gpr_time_init();
Craig Tiller28086682017-07-18 14:22:19 -0700126 grpc_stats_init();
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800127 grpc_slice_intern_init();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800128 grpc_mdctx_global_init();
Craig Tiller178edfa2016-02-17 20:54:46 -0800129 grpc_channel_init_init();
ncteisen9a6c7222018-07-13 12:09:55 -0700130 grpc_core::channelz::ChannelzRegistry::Init();
Craig Tillerfaa84802015-03-01 21:56:38 -0800131 grpc_security_pre_init();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800132 grpc_core::ExecCtx::GlobalInit();
133 grpc_iomgr_init();
Craig Tiller0ba432d2015-10-09 16:57:11 -0700134 gpr_timers_global_init();
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800135 grpc_handshaker_factory_registry_init();
136 grpc_security_init();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700137 for (i = 0; i < g_number_of_plugins; i++) {
Craig Tiller4782d922017-11-10 09:53:21 -0800138 if (g_all_of_the_plugins[i].init != nullptr) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700139 g_all_of_the_plugins[i].init();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700140 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700141 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800142 /* register channel finalization AFTER all plugins, to ensure that it's run
143 * at the appropriate time */
144 grpc_register_security_filters();
145 register_builtin_channel_init();
David Garcia Quintaseb913102016-04-25 17:30:04 -0700146 grpc_tracer_init("GRPC_TRACE");
Craig Tiller178edfa2016-02-17 20:54:46 -0800147 /* no more changes to channel init pipelines */
148 grpc_channel_init_finalize();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800149 grpc_iomgr_start();
Craig Tiller35108f62015-02-17 11:24:15 -0800150 }
151 gpr_mu_unlock(&g_init_mu);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800152
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700153 GRPC_API_TRACE("grpc_init(void)", 0, ());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154}
155
Craig Tiller32946d32015-01-15 11:37:30 -0800156void grpc_shutdown(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700157 int i;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700158 GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
Craig Tiller35108f62015-02-17 11:24:15 -0800159 gpr_mu_lock(&g_init_mu);
160 if (--g_initializations == 0) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800161 {
162 grpc_core::ExecCtx exec_ctx(0);
163 {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800164 grpc_timer_manager_set_threading(
165 false); // shutdown timer_manager thread
yang-g8f659062018-01-31 15:16:41 -0800166 grpc_executor_shutdown();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800167 for (i = g_number_of_plugins; i >= 0; i--) {
168 if (g_all_of_the_plugins[i].destroy != nullptr) {
169 g_all_of_the_plugins[i].destroy();
170 }
171 }
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700172 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800173 grpc_iomgr_shutdown();
174 gpr_timers_global_destroy();
175 grpc_tracer_shutdown();
176 grpc_mdctx_global_shutdown();
177 grpc_handshaker_factory_registry_shutdown();
178 grpc_slice_intern_shutdown();
ncteisen9a6c7222018-07-13 12:09:55 -0700179 grpc_core::channelz::ChannelzRegistry::Shutdown();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800180 grpc_stats_shutdown();
kpayson64701e70d2018-04-30 17:40:00 -0700181 grpc_core::Fork::GlobalShutdown();
Hongwei Wanga3780a82015-07-17 15:27:18 -0700182 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800183 grpc_core::ExecCtx::GlobalShutdown();
Craig Tiller35108f62015-02-17 11:24:15 -0800184 }
185 gpr_mu_unlock(&g_init_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186}
Craig Tiller60fd3612015-03-05 16:24:22 -0800187
188int grpc_is_initialized(void) {
189 int r;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800190 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller60fd3612015-03-05 16:24:22 -0800191 gpr_mu_lock(&g_init_mu);
192 r = g_initializations > 0;
193 gpr_mu_unlock(&g_init_mu);
194 return r;
195}