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