• 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/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 
grpc_iomgr_init()54 void grpc_iomgr_init() {
55   grpc_core::ExecCtx exec_ctx;
56   grpc_determine_iomgr_platform();
57   g_shutdown = 0;
58   gpr_mu_init(&g_mu);
59   gpr_cv_init(&g_rcv);
60   grpc_core::Executor::InitAll();
61   g_root_object.next = g_root_object.prev = &g_root_object;
62   g_root_object.name = const_cast<char*>("root");
63   grpc_iomgr_platform_init();
64   grpc_timer_list_init();
65   grpc_core::grpc_errqueue_init();
66   g_grpc_abort_on_leaks = GPR_GLOBAL_CONFIG_GET(grpc_abort_on_leaks);
67 }
68 
grpc_iomgr_start()69 void grpc_iomgr_start() { grpc_timer_manager_init(); }
70 
count_objects(void)71 static size_t count_objects(void) {
72   grpc_iomgr_object* obj;
73   size_t n = 0;
74   for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
75     n++;
76   }
77   return n;
78 }
79 
grpc_iomgr_count_objects_for_testing(void)80 size_t grpc_iomgr_count_objects_for_testing(void) { return count_objects(); }
81 
dump_objects(const char * kind)82 static void dump_objects(const char* kind) {
83   grpc_iomgr_object* obj;
84   for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
85     gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
86   }
87 }
88 
grpc_iomgr_shutdown()89 void grpc_iomgr_shutdown() {
90   gpr_timespec shutdown_deadline = gpr_time_add(
91       gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
92   gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
93 
94   {
95     grpc_timer_manager_shutdown();
96     grpc_iomgr_platform_flush();
97     grpc_core::Executor::ShutdownAll();
98 
99     gpr_mu_lock(&g_mu);
100     g_shutdown = 1;
101     while (g_root_object.next != &g_root_object) {
102       if (gpr_time_cmp(
103               gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
104               gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
105         if (g_root_object.next != &g_root_object) {
106           gpr_log(GPR_DEBUG,
107                   "Waiting for %" PRIuPTR " iomgr objects to be destroyed",
108                   count_objects());
109         }
110         last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
111       }
112       grpc_core::ExecCtx::Get()->SetNowIomgrShutdown();
113       if (grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED) {
114         gpr_mu_unlock(&g_mu);
115         grpc_core::ExecCtx::Get()->Flush();
116         grpc_iomgr_platform_flush();
117         gpr_mu_lock(&g_mu);
118         continue;
119       }
120       if (g_root_object.next != &g_root_object) {
121         if (grpc_iomgr_abort_on_leaks()) {
122           gpr_log(GPR_DEBUG,
123                   "Failed to free %" PRIuPTR
124                   " iomgr objects before shutdown deadline: "
125                   "memory leaks are likely",
126                   count_objects());
127           dump_objects("LEAKED");
128           abort();
129         }
130         gpr_timespec short_deadline =
131             gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
132                          gpr_time_from_millis(100, GPR_TIMESPAN));
133         if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
134           if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) >
135               0) {
136             if (g_root_object.next != &g_root_object) {
137               gpr_log(GPR_DEBUG,
138                       "Failed to free %" PRIuPTR
139                       " iomgr objects before shutdown deadline: "
140                       "memory leaks are likely",
141                       count_objects());
142               dump_objects("LEAKED");
143             }
144             break;
145           }
146         }
147       }
148     }
149     gpr_mu_unlock(&g_mu);
150     grpc_timer_list_shutdown();
151     grpc_core::ExecCtx::Get()->Flush();
152   }
153 
154   /* ensure all threads have left g_mu */
155   gpr_mu_lock(&g_mu);
156   gpr_mu_unlock(&g_mu);
157 
158   grpc_iomgr_platform_shutdown();
159   gpr_mu_destroy(&g_mu);
160   gpr_cv_destroy(&g_rcv);
161 }
162 
grpc_iomgr_shutdown_background_closure()163 void grpc_iomgr_shutdown_background_closure() {
164   grpc_iomgr_platform_shutdown_background_closure();
165 }
166 
grpc_iomgr_is_any_background_poller_thread()167 bool grpc_iomgr_is_any_background_poller_thread() {
168   return grpc_iomgr_platform_is_any_background_poller_thread();
169 }
170 
grpc_iomgr_add_closure_to_background_poller(grpc_closure * closure,grpc_error * error)171 bool grpc_iomgr_add_closure_to_background_poller(grpc_closure* closure,
172                                                  grpc_error* error) {
173   return grpc_iomgr_platform_add_closure_to_background_poller(closure, error);
174 }
175 
grpc_iomgr_register_object(grpc_iomgr_object * obj,const char * name)176 void grpc_iomgr_register_object(grpc_iomgr_object* obj, const char* name) {
177   obj->name = gpr_strdup(name);
178   gpr_mu_lock(&g_mu);
179   obj->next = &g_root_object;
180   obj->prev = g_root_object.prev;
181   obj->next->prev = obj->prev->next = obj;
182   gpr_mu_unlock(&g_mu);
183 }
184 
grpc_iomgr_unregister_object(grpc_iomgr_object * obj)185 void grpc_iomgr_unregister_object(grpc_iomgr_object* obj) {
186   gpr_mu_lock(&g_mu);
187   obj->next->prev = obj->prev;
188   obj->prev->next = obj->next;
189   gpr_cv_signal(&g_rcv);
190   gpr_mu_unlock(&g_mu);
191   gpr_free(obj->name);
192 }
193 
grpc_iomgr_abort_on_leaks(void)194 bool grpc_iomgr_abort_on_leaks(void) { return g_grpc_abort_on_leaks; }
195