blob: f63f19015517f7bd41d22674fc137d85b571ef6f [file] [log] [blame]
ctiller58393c22015-01-07 14:03:30 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
ctiller58393c22015-01-07 14:03:30 -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
ctiller58393c22015-01-07 14:03:30 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
ctiller58393c22015-01-07 14:03:30 -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.
ctiller58393c22015-01-07 14:03:30 -080016 *
17 */
18
Craig Tiller9533d042016-03-25 17:11:06 -070019#include "src/core/lib/iomgr/iomgr.h"
ctiller58393c22015-01-07 14:03:30 -080020
21#include <stdlib.h>
Craig Tiller131f6ed2015-09-15 08:20:20 -070022#include <string.h>
ctiller58393c22015-01-07 14:03:30 -080023
ctiller58393c22015-01-07 14:03:30 -080024#include <grpc/support/alloc.h>
25#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070026#include <grpc/support/string_util.h>
ctiller58393c22015-01-07 14:03:30 -080027#include <grpc/support/sync.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070028#include <grpc/support/thd.h>
Craig Tiller0cb803d2016-03-02 22:17:24 -080029#include <grpc/support/useful.h>
ctiller58393c22015-01-07 14:03:30 -080030
Craig Tiller9533d042016-03-25 17:11:06 -070031#include "src/core/lib/iomgr/exec_ctx.h"
Craig Tillerb9b01ce2017-05-12 13:47:10 -070032#include "src/core/lib/iomgr/executor.h"
Craig Tiller9533d042016-03-25 17:11:06 -070033#include "src/core/lib/iomgr/iomgr_internal.h"
Craig Tillerf11b3332016-07-14 16:48:23 -070034#include "src/core/lib/iomgr/network_status_tracker.h"
Craig Tiller9533d042016-03-25 17:11:06 -070035#include "src/core/lib/iomgr/timer.h"
Craig Tillerc3571792017-05-02 12:33:38 -070036#include "src/core/lib/iomgr/timer_manager.h"
Craig Tiller9533d042016-03-25 17:11:06 -070037#include "src/core/lib/support/env.h"
38#include "src/core/lib/support/string.h"
Craig Tiller131f6ed2015-09-15 08:20:20 -070039
ctiller58393c22015-01-07 14:03:30 -080040static gpr_mu g_mu;
Nicolas "Pixel" Nobleae7b45a2015-02-04 03:28:34 +010041static gpr_cv g_rcv;
ctiller58393c22015-01-07 14:03:30 -080042static int g_shutdown;
Craig Tillerfa275a92015-06-01 13:55:54 -070043static grpc_iomgr_object g_root_object;
ctiller58393c22015-01-07 14:03:30 -080044
Craig Tiller5e56f002017-05-16 15:02:50 -070045void grpc_iomgr_init(grpc_exec_ctx *exec_ctx) {
Craig Tiller994c2622015-07-23 14:00:58 -070046 g_shutdown = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070047 gpr_mu_init(&g_mu);
48 gpr_cv_init(&g_rcv);
Craig Tilleref1bf872016-02-28 17:37:33 -080049 grpc_exec_ctx_global_init();
Craig Tiller5e56f002017-05-16 15:02:50 -070050 grpc_executor_init(exec_ctx);
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070051 grpc_timer_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tillerfa275a92015-06-01 13:55:54 -070052 g_root_object.next = g_root_object.prev = &g_root_object;
Yash Tibrewal533d1182017-09-18 10:48:22 -070053 g_root_object.name = (char *)"root";
Craig Tillerf11b3332016-07-14 16:48:23 -070054 grpc_network_status_init();
Craig Tillera82950e2015-09-22 12:33:20 -070055 grpc_iomgr_platform_init();
Craig Tiller84f75d42017-05-03 13:06:35 -070056}
57
Craig Tiller5e56f002017-05-16 15:02:50 -070058void grpc_iomgr_start(grpc_exec_ctx *exec_ctx) { grpc_timer_manager_init(); }
ctiller58393c22015-01-07 14:03:30 -080059
Craig Tillera82950e2015-09-22 12:33:20 -070060static size_t count_objects(void) {
Craig Tillerfa275a92015-06-01 13:55:54 -070061 grpc_iomgr_object *obj;
62 size_t n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070063 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
64 n++;
65 }
Craig Tillerfa275a92015-06-01 13:55:54 -070066 return n;
67}
68
Craig Tillera82950e2015-09-22 12:33:20 -070069static void dump_objects(const char *kind) {
Craig Tillerfa275a92015-06-01 13:55:54 -070070 grpc_iomgr_object *obj;
Craig Tillera82950e2015-09-22 12:33:20 -070071 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
72 gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
73 }
Craig Tiller49d03c82015-09-03 14:34:30 -070074}
75
Craig Tiller3cf79222016-11-14 08:02:45 -080076void grpc_iomgr_shutdown(grpc_exec_ctx *exec_ctx) {
Craig Tillera82950e2015-09-22 12:33:20 -070077 gpr_timespec shutdown_deadline = gpr_time_add(
78 gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
79 gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
ctiller58393c22015-01-07 14:03:30 -080080
Craig Tillerc3571792017-05-02 12:33:38 -070081 grpc_timer_manager_shutdown();
Craig Tiller01be53d2015-09-30 08:36:03 -070082 grpc_iomgr_platform_flush();
Craig Tiller5e56f002017-05-16 15:02:50 -070083 grpc_executor_shutdown(exec_ctx);
Craig Tiller01be53d2015-09-30 08:36:03 -070084
Craig Tillera82950e2015-09-22 12:33:20 -070085 gpr_mu_lock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -080086 g_shutdown = 1;
Craig Tillera82950e2015-09-22 12:33:20 -070087 while (g_root_object.next != &g_root_object) {
88 if (gpr_time_cmp(
89 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
90 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
91 if (g_root_object.next != &g_root_object) {
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -070092 gpr_log(GPR_DEBUG,
93 "Waiting for %" PRIuPTR " iomgr objects to be destroyed",
Craig Tillera82950e2015-09-22 12:33:20 -070094 count_objects());
95 }
96 last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tiller29f741f2015-05-29 12:29:43 -070097 }
Craig Tillerb2ef1cf2017-05-30 09:25:48 -070098 if (grpc_timer_check(exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL) ==
99 GRPC_TIMERS_FIRED) {
Craig Tillera82950e2015-09-22 12:33:20 -0700100 gpr_mu_unlock(&g_mu);
Craig Tiller3cf79222016-11-14 08:02:45 -0800101 grpc_exec_ctx_flush(exec_ctx);
Craig Tillerda81d1a2016-11-28 15:00:53 -0800102 grpc_iomgr_platform_flush();
Craig Tillera82950e2015-09-22 12:33:20 -0700103 gpr_mu_lock(&g_mu);
104 continue;
105 }
106 if (g_root_object.next != &g_root_object) {
Craig Tillerfc2636d2016-09-12 09:57:07 -0700107 if (grpc_iomgr_abort_on_leaks()) {
108 gpr_log(GPR_DEBUG, "Failed to free %" PRIuPTR
109 " iomgr objects before shutdown deadline: "
110 "memory leaks are likely",
111 count_objects());
112 dump_objects("LEAKED");
113 abort();
114 }
Craig Tillera82950e2015-09-22 12:33:20 -0700115 gpr_timespec short_deadline = gpr_time_add(
116 gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100, GPR_TIMESPAN));
117 if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
118 if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) {
Craig Tiller63010382015-09-24 15:00:58 -0700119 if (g_root_object.next != &g_root_object) {
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -0700120 gpr_log(GPR_DEBUG, "Failed to free %" PRIuPTR
121 " iomgr objects before shutdown deadline: "
122 "memory leaks are likely",
Craig Tiller63010382015-09-24 15:00:58 -0700123 count_objects());
124 dump_objects("LEAKED");
125 }
Craig Tillera82950e2015-09-22 12:33:20 -0700126 break;
127 }
128 }
Craig Tillera82950e2015-09-22 12:33:20 -0700129 }
130 }
131 gpr_mu_unlock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -0800132
Craig Tiller3cf79222016-11-14 08:02:45 -0800133 grpc_timer_list_shutdown(exec_ctx);
134 grpc_exec_ctx_flush(exec_ctx);
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700135
Craig Tiller62aef622015-09-21 21:27:54 -0700136 /* ensure all threads have left g_mu */
Craig Tillera82950e2015-09-22 12:33:20 -0700137 gpr_mu_lock(&g_mu);
138 gpr_mu_unlock(&g_mu);
Craig Tiller62aef622015-09-21 21:27:54 -0700139
Craig Tillera82950e2015-09-22 12:33:20 -0700140 grpc_iomgr_platform_shutdown();
Craig Tilleref1bf872016-02-28 17:37:33 -0800141 grpc_exec_ctx_global_shutdown();
Craig Tillerf11b3332016-07-14 16:48:23 -0700142 grpc_network_status_shutdown();
Craig Tillera82950e2015-09-22 12:33:20 -0700143 gpr_mu_destroy(&g_mu);
144 gpr_cv_destroy(&g_rcv);
ctiller58393c22015-01-07 14:03:30 -0800145}
146
Craig Tillera82950e2015-09-22 12:33:20 -0700147void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
148 obj->name = gpr_strdup(name);
149 gpr_mu_lock(&g_mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700150 obj->next = &g_root_object;
Craig Tiller49d03c82015-09-03 14:34:30 -0700151 obj->prev = g_root_object.prev;
Craig Tillerfa275a92015-06-01 13:55:54 -0700152 obj->next->prev = obj->prev->next = obj;
Craig Tillera82950e2015-09-22 12:33:20 -0700153 gpr_mu_unlock(&g_mu);
ctiller58393c22015-01-07 14:03:30 -0800154}
155
Craig Tillera82950e2015-09-22 12:33:20 -0700156void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
157 gpr_mu_lock(&g_mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700158 obj->next->prev = obj->prev;
159 obj->prev->next = obj->next;
Craig Tillera82950e2015-09-22 12:33:20 -0700160 gpr_cv_signal(&g_rcv);
161 gpr_mu_unlock(&g_mu);
162 gpr_free(obj->name);
ctiller58393c22015-01-07 14:03:30 -0800163}
Craig Tiller0cb803d2016-03-02 22:17:24 -0800164
165bool grpc_iomgr_abort_on_leaks(void) {
166 char *env = gpr_getenv("GRPC_ABORT_ON_LEAKS");
Yuchen Zeng6514a0d2017-08-25 14:09:47 -0700167 bool should_we = gpr_is_true(env);
Yuchen Zeng4a11ecc2017-08-25 14:10:30 -0700168 gpr_free(env);
Craig Tillera3b54cd2016-03-25 15:59:55 -0700169 return should_we;
Craig Tiller0cb803d2016-03-02 22:17:24 -0800170}