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/env.h"
33 #include "src/core/lib/gpr/string.h"
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/gprpp/thd.h"
36 #include "src/core/lib/iomgr/exec_ctx.h"
37 #include "src/core/lib/iomgr/executor.h"
38 #include "src/core/lib/iomgr/iomgr_internal.h"
39 #include "src/core/lib/iomgr/network_status_tracker.h"
40 #include "src/core/lib/iomgr/timer.h"
41 #include "src/core/lib/iomgr/timer_manager.h"
42
43 static gpr_mu g_mu;
44 static gpr_cv g_rcv;
45 static int g_shutdown;
46 static grpc_iomgr_object g_root_object;
47
grpc_iomgr_init()48 void grpc_iomgr_init() {
49 grpc_core::ExecCtx exec_ctx;
50 grpc_determine_iomgr_platform();
51 g_shutdown = 0;
52 gpr_mu_init(&g_mu);
53 gpr_cv_init(&g_rcv);
54 grpc_executor_init();
55 grpc_timer_list_init();
56 g_root_object.next = g_root_object.prev = &g_root_object;
57 g_root_object.name = (char*)"root";
58 grpc_network_status_init();
59 grpc_iomgr_platform_init();
60 }
61
grpc_iomgr_start()62 void grpc_iomgr_start() { grpc_timer_manager_init(); }
63
count_objects(void)64 static size_t count_objects(void) {
65 grpc_iomgr_object* obj;
66 size_t n = 0;
67 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
68 n++;
69 }
70 return n;
71 }
72
grpc_iomgr_count_objects_for_testing(void)73 size_t grpc_iomgr_count_objects_for_testing(void) { return count_objects(); }
74
dump_objects(const char * kind)75 static void dump_objects(const char* kind) {
76 grpc_iomgr_object* obj;
77 for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
78 gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
79 }
80 }
81
grpc_iomgr_shutdown()82 void grpc_iomgr_shutdown() {
83 gpr_timespec shutdown_deadline = gpr_time_add(
84 gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
85 gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
86
87 {
88 grpc_timer_manager_shutdown();
89 grpc_iomgr_platform_flush();
90 grpc_executor_shutdown();
91
92 gpr_mu_lock(&g_mu);
93 g_shutdown = 1;
94 while (g_root_object.next != &g_root_object) {
95 if (gpr_time_cmp(
96 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
97 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
98 if (g_root_object.next != &g_root_object) {
99 gpr_log(GPR_DEBUG,
100 "Waiting for %" PRIuPTR " iomgr objects to be destroyed",
101 count_objects());
102 }
103 last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
104 }
105 grpc_core::ExecCtx::Get()->SetNowIomgrShutdown();
106 if (grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED) {
107 gpr_mu_unlock(&g_mu);
108 grpc_core::ExecCtx::Get()->Flush();
109 grpc_iomgr_platform_flush();
110 gpr_mu_lock(&g_mu);
111 continue;
112 }
113 if (g_root_object.next != &g_root_object) {
114 if (grpc_iomgr_abort_on_leaks()) {
115 gpr_log(GPR_DEBUG,
116 "Failed to free %" PRIuPTR
117 " iomgr objects before shutdown deadline: "
118 "memory leaks are likely",
119 count_objects());
120 dump_objects("LEAKED");
121 abort();
122 }
123 gpr_timespec short_deadline =
124 gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
125 gpr_time_from_millis(100, GPR_TIMESPAN));
126 if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
127 if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) >
128 0) {
129 if (g_root_object.next != &g_root_object) {
130 gpr_log(GPR_DEBUG,
131 "Failed to free %" PRIuPTR
132 " iomgr objects before shutdown deadline: "
133 "memory leaks are likely",
134 count_objects());
135 dump_objects("LEAKED");
136 }
137 break;
138 }
139 }
140 }
141 }
142 gpr_mu_unlock(&g_mu);
143 grpc_timer_list_shutdown();
144 grpc_core::ExecCtx::Get()->Flush();
145 }
146
147 /* ensure all threads have left g_mu */
148 gpr_mu_lock(&g_mu);
149 gpr_mu_unlock(&g_mu);
150
151 grpc_iomgr_platform_shutdown();
152 grpc_network_status_shutdown();
153 gpr_mu_destroy(&g_mu);
154 gpr_cv_destroy(&g_rcv);
155 }
156
grpc_iomgr_register_object(grpc_iomgr_object * obj,const char * name)157 void grpc_iomgr_register_object(grpc_iomgr_object* obj, const char* name) {
158 obj->name = gpr_strdup(name);
159 gpr_mu_lock(&g_mu);
160 obj->next = &g_root_object;
161 obj->prev = g_root_object.prev;
162 obj->next->prev = obj->prev->next = obj;
163 gpr_mu_unlock(&g_mu);
164 }
165
grpc_iomgr_unregister_object(grpc_iomgr_object * obj)166 void grpc_iomgr_unregister_object(grpc_iomgr_object* obj) {
167 gpr_mu_lock(&g_mu);
168 obj->next->prev = obj->prev;
169 obj->prev->next = obj->next;
170 gpr_cv_signal(&g_rcv);
171 gpr_mu_unlock(&g_mu);
172 gpr_free(obj->name);
173 }
174
grpc_iomgr_abort_on_leaks(void)175 bool grpc_iomgr_abort_on_leaks(void) {
176 char* env = gpr_getenv("GRPC_ABORT_ON_LEAKS");
177 bool should_we = gpr_is_true(env);
178 gpr_free(env);
179 return should_we;
180 }
181