| 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 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/iomgr/iomgr.h" |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
#include <grpc/support/sync.h> |
| 31 |
|
| 32 |
#include "src/core/lib/gpr/string.h" |
| 33 |
#include "src/core/lib/gpr/useful.h" |
| 34 |
#include "src/core/lib/gprpp/global_config.h" |
| 35 |
#include "src/core/lib/gprpp/thd.h" |
| 36 |
#include "src/core/lib/iomgr/buffer_list.h" |
| 37 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 38 |
#include "src/core/lib/iomgr/executor.h" |
| 39 |
#include "src/core/lib/iomgr/internal_errqueue.h" |
| 40 |
#include "src/core/lib/iomgr/iomgr_internal.h" |
| 41 |
#include "src/core/lib/iomgr/timer.h" |
| 42 |
#include "src/core/lib/iomgr/timer_manager.h" |
| 43 |
|
| 44 |
GPR_GLOBAL_CONFIG_DEFINE_BOOL(grpc_abort_on_leaks, false, |
| 45 |
"A debugging aid to cause a call to abort() when " |
| 46 |
"gRPC objects are leaked past grpc_shutdown()"); |
| 47 |
|
| 48 |
static gpr_mu g_mu; |
| 49 |
static gpr_cv g_rcv; |
| 50 |
static int g_shutdown; |
| 51 |
static grpc_iomgr_object g_root_object; |
| 52 |
static bool g_grpc_abort_on_leaks; |
| 53 |
|
| 54 |
void grpc_iomgr_init() { |
| 55 |
grpc_core::ExecCtx exec_ctx; |
| 56 |
if (!grpc_have_determined_iomgr_platform()) { |
| 57 |
grpc_set_default_iomgr_platform(); |
| 58 |
} |
| 59 |
g_shutdown = 0; |
| 60 |
gpr_mu_init(&g_mu); |
| 61 |
gpr_cv_init(&g_rcv); |
| 62 |
grpc_core::Executor::InitAll(); |
| 63 |
g_root_object.next = g_root_object.prev = &g_root_object; |
| 64 |
g_root_object.name = const_cast<char*>("root"); |
| 65 |
grpc_iomgr_platform_init(); |
| 66 |
grpc_timer_list_init(); |
| 67 |
g_grpc_abort_on_leaks = GPR_GLOBAL_CONFIG_GET(grpc_abort_on_leaks); |
| 68 |
} |
| 69 |
|
| 70 |
void grpc_iomgr_start() { grpc_timer_manager_init(); } |
| 71 |
|
| 72 |
static size_t count_objects(void) { |
| 73 |
grpc_iomgr_object* obj; |
| 74 |
size_t n = 0; |
| 75 |
for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) { |
| 76 |
n++; |
| 77 |
} |
| 78 |
return n; |
| 79 |
} |
| 80 |
|
| 81 |
size_t grpc_iomgr_count_objects_for_testing(void) { |
| 82 |
gpr_mu_lock(&g_mu); |
| 83 |
size_t ret = count_objects(); |
| 84 |
gpr_mu_unlock(&g_mu); |
| 85 |
return ret; |
| 86 |
} |
| 87 |
|
| 88 |
static void dump_objects(const char* kind) { |
| 89 |
grpc_iomgr_object* obj; |
| 90 |
for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) { |
| 91 |
gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void grpc_iomgr_shutdown() { |
| 96 |
gpr_timespec shutdown_deadline = gpr_time_add( |
| 97 |
gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN)); |
| 98 |
gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME); |
| 99 |
|
| 100 |
{ |
| 101 |
grpc_timer_manager_shutdown(); |
| 102 |
grpc_iomgr_platform_flush(); |
| 103 |
|
| 104 |
gpr_mu_lock(&g_mu); |
| 105 |
g_shutdown = 1; |
| 106 |
while (g_root_object.next != &g_root_object) { |
| 107 |
if (gpr_time_cmp( |
| 108 |
gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time), |
| 109 |
gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) { |
| 110 |
if (g_root_object.next != &g_root_object) { |
| 111 |
gpr_log(GPR_DEBUG, |
| 112 |
"Waiting for %" PRIuPTR " iomgr objects to be destroyed", |
| 113 |
count_objects()); |
| 114 |
} |
| 115 |
last_warning_time = gpr_now(GPR_CLOCK_REALTIME); |
| 116 |
} |
| 117 |
grpc_core::ExecCtx::Get()->SetNowIomgrShutdown(); |
| 118 |
if (grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED) { |
| 119 |
gpr_mu_unlock(&g_mu); |
| 120 |
grpc_core::ExecCtx::Get()->Flush(); |
| 121 |
grpc_iomgr_platform_flush(); |
| 122 |
gpr_mu_lock(&g_mu); |
| 123 |
continue; |
| 124 |
} |
| 125 |
if (g_root_object.next != &g_root_object) { |
| 126 |
if (grpc_iomgr_abort_on_leaks()) { |
| 127 |
gpr_log(GPR_DEBUG, |
| 128 |
"Failed to free %" PRIuPTR |
| 129 |
" iomgr objects before shutdown deadline: " |
| 130 |
"memory leaks are likely", |
| 131 |
count_objects()); |
| 132 |
dump_objects("LEAKED"); |
| 133 |
abort(); |
| 134 |
} |
| 135 |
gpr_timespec short_deadline = |
| 136 |
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 137 |
gpr_time_from_millis(100, GPR_TIMESPAN)); |
| 138 |
if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) { |
| 139 |
if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > |
| 140 |
0) { |
| 141 |
if (g_root_object.next != &g_root_object) { |
| 142 |
gpr_log(GPR_DEBUG, |
| 143 |
"Failed to free %" PRIuPTR |
| 144 |
" iomgr objects before shutdown deadline: " |
| 145 |
"memory leaks are likely", |
| 146 |
count_objects()); |
| 147 |
dump_objects("LEAKED"); |
| 148 |
} |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
gpr_mu_unlock(&g_mu); |
| 155 |
grpc_timer_list_shutdown(); |
| 156 |
grpc_core::ExecCtx::Get()->Flush(); |
| 157 |
grpc_core::Executor::ShutdownAll(); |
| 158 |
} |
| 159 |
|
| 160 |
/* ensure all threads have left g_mu */ |
| 161 |
gpr_mu_lock(&g_mu); |
| 162 |
gpr_mu_unlock(&g_mu); |
| 163 |
|
| 164 |
grpc_iomgr_platform_shutdown(); |
| 165 |
gpr_mu_destroy(&g_mu); |
| 166 |
gpr_cv_destroy(&g_rcv); |
| 167 |
} |
| 168 |
|
| 169 |
void grpc_iomgr_shutdown_background_closure() { |
| 170 |
grpc_iomgr_platform_shutdown_background_closure(); |
| 171 |
} |
| 172 |
|
| 173 |
bool grpc_iomgr_is_any_background_poller_thread() { |
| 174 |
return grpc_iomgr_platform_is_any_background_poller_thread(); |
| 175 |
} |
| 176 |
|
| 177 |
bool grpc_iomgr_add_closure_to_background_poller(grpc_closure* closure, |
| 178 |
grpc_error_handle error) { |
| 179 |
return grpc_iomgr_platform_add_closure_to_background_poller(closure, error); |
| 180 |
} |
| 181 |
|
| 182 |
void grpc_iomgr_register_object(grpc_iomgr_object* obj, const char* name) { |
| 183 |
obj->name = gpr_strdup(name); |
| 184 |
gpr_mu_lock(&g_mu); |
| 185 |
obj->next = &g_root_object; |
| 186 |
obj->prev = g_root_object.prev; |
| 187 |
obj->next->prev = obj->prev->next = obj; |
| 188 |
gpr_mu_unlock(&g_mu); |
| 189 |
} |
| 190 |
|
| 191 |
void grpc_iomgr_unregister_object(grpc_iomgr_object* obj) { |
| 192 |
gpr_mu_lock(&g_mu); |
| 193 |
obj->next->prev = obj->prev; |
| 194 |
obj->prev->next = obj->next; |
| 195 |
gpr_cv_signal(&g_rcv); |
| 196 |
gpr_mu_unlock(&g_mu); |
| 197 |
gpr_free(obj->name); |
| 198 |
} |
| 199 |
|
| 200 |
bool grpc_iomgr_abort_on_leaks(void) { return g_grpc_abort_on_leaks; } |