iomgr.cc
Go to the documentation of this file.
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 
20 
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>
30 #include <grpc/support/sync.h>
31 
35 #include "src/core/lib/gprpp/thd.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 
49  grpc_experimental_enable_tcp_frame_size_tuning, false,
50  "If set, enables TCP to use RPC size estimation made by higher layers. TCP "
51  "would not indicate completion of a read operation until a specified "
52  "number of bytes have been read over the socket. Buffers are also "
53  "allocated according to estimated RPC sizes.");
54 
55 static gpr_mu g_mu;
56 static gpr_cv g_rcv;
57 static int g_shutdown;
60 
65  }
66  g_shutdown = 0;
67  gpr_mu_init(&g_mu);
71  g_root_object.name = const_cast<char*>("root");
74  g_grpc_abort_on_leaks = GPR_GLOBAL_CONFIG_GET(grpc_abort_on_leaks);
75 }
76 
78 
79 static size_t count_objects(void) {
81  size_t n = 0;
82  for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
83  n++;
84  }
85  return n;
86 }
87 
89 
90 static void dump_objects(const char* kind) {
92  for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
93  gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
94  }
95 }
96 
98  gpr_timespec shutdown_deadline = gpr_time_add(
100  gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
101 
102  {
105 
106  gpr_mu_lock(&g_mu);
107  g_shutdown = 1;
108  while (g_root_object.next != &g_root_object) {
109  if (gpr_time_cmp(
110  gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
112  if (g_root_object.next != &g_root_object) {
114  "Waiting for %" PRIuPTR " iomgr objects to be destroyed",
115  count_objects());
116  }
117  last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
118  }
120  if (grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED) {
124  gpr_mu_lock(&g_mu);
125  continue;
126  }
127  if (g_root_object.next != &g_root_object) {
130  "Failed to free %" PRIuPTR
131  " iomgr objects before shutdown deadline: "
132  "memory leaks are likely",
133  count_objects());
134  dump_objects("LEAKED");
135  abort();
136  }
137  gpr_timespec short_deadline =
140  if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
141  if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) >
142  0) {
143  if (g_root_object.next != &g_root_object) {
145  "Failed to free %" PRIuPTR
146  " iomgr objects before shutdown deadline: "
147  "memory leaks are likely",
148  count_objects());
149  dump_objects("LEAKED");
150  }
151  break;
152  }
153  }
154  }
155  }
160  }
161 
162  /* ensure all threads have left g_mu */
163  gpr_mu_lock(&g_mu);
165 
169 }
170 
173 }
174 
177 }
178 
182 }
183 
185  obj->name = gpr_strdup(name);
186  gpr_mu_lock(&g_mu);
187  obj->next = &g_root_object;
188  obj->prev = g_root_object.prev;
189  obj->next->prev = obj->prev->next = obj;
191 }
192 
194  gpr_mu_lock(&g_mu);
195  obj->next->prev = obj->prev;
196  obj->prev->next = obj->next;
199  gpr_free(obj->name);
200 }
201 
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
g_rcv
static gpr_cv g_rcv
Definition: iomgr.cc:56
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
iomgr.h
GPR_GLOBAL_CONFIG_DEFINE_BOOL
GPR_GLOBAL_CONFIG_DEFINE_BOOL(grpc_abort_on_leaks, false, "A debugging aid to cause a call to abort() when " "gRPC objects are leaked past grpc_shutdown()")
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
grpc_have_determined_iomgr_platform
bool grpc_have_determined_iomgr_platform()
Definition: iomgr_internal.cc:27
log.h
iomgr_internal.h
buffer_list.h
grpc_iomgr_is_any_background_poller_thread
bool grpc_iomgr_is_any_background_poller_thread()
Definition: iomgr.cc:175
GPR_GLOBAL_CONFIG_GET
#define GPR_GLOBAL_CONFIG_GET(name)
Definition: global_config_generic.h:24
timer_manager.h
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_iomgr_shutdown_background_closure
void grpc_iomgr_shutdown_background_closure()
Definition: iomgr.cc:171
grpc_iomgr_platform_shutdown_background_closure
void grpc_iomgr_platform_shutdown_background_closure()
Definition: iomgr_internal.cc:41
grpc_timer_manager_init
void grpc_timer_manager_init(void)
Definition: iomgr/timer_manager.cc:300
setup.name
name
Definition: setup.py:542
internal_errqueue.h
grpc_iomgr_platform_add_closure_to_background_poller
bool grpc_iomgr_platform_add_closure_to_background_poller(grpc_closure *closure, grpc_error_handle error)
Definition: iomgr_internal.cc:49
grpc_core::Executor::ShutdownAll
static void ShutdownAll()
Definition: executor.cc:404
grpc_timer_check
grpc_timer_check_result grpc_timer_check(grpc_core::Timestamp *next)
Definition: iomgr/timer.cc:38
grpc_iomgr_count_objects_for_testing
size_t grpc_iomgr_count_objects_for_testing(void)
Definition: iomgr.cc:88
grpc_set_default_iomgr_platform
void grpc_set_default_iomgr_platform()
Definition: memory_quota_test.cc:167
grpc_iomgr_platform_is_any_background_poller_thread
bool grpc_iomgr_platform_is_any_background_poller_thread()
Definition: iomgr_internal.cc:45
grpc_iomgr_start
void grpc_iomgr_start()
Definition: iomgr.cc:77
grpc_iomgr_unregister_object
void grpc_iomgr_unregister_object(grpc_iomgr_object *obj)
Definition: iomgr.cc:193
GRPC_TIMERS_FIRED
@ GRPC_TIMERS_FIRED
Definition: iomgr/timer.h:58
grpc_iomgr_object
Definition: iomgr_internal.h:29
grpc_iomgr_object::name
char * name
Definition: iomgr_internal.h:30
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
g_shutdown
static int g_shutdown
Definition: iomgr.cc:57
string_util.h
grpc_timer_list_init
void grpc_timer_list_init()
Definition: iomgr/timer.cc:42
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
g_root_object
static grpc_iomgr_object g_root_object
Definition: iomgr.cc:58
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:168
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc_iomgr_platform_init
void grpc_iomgr_platform_init()
Definition: iomgr_internal.cc:35
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
grpc_iomgr_shutdown
void grpc_iomgr_shutdown()
Definition: iomgr.cc:97
grpc_timer_manager_shutdown
void grpc_timer_manager_shutdown(void)
Definition: iomgr/timer_manager.cc:338
grpc_iomgr_add_closure_to_background_poller
bool grpc_iomgr_add_closure_to_background_poller(grpc_closure *closure, grpc_error_handle error)
Definition: iomgr.cc:179
g_grpc_abort_on_leaks
static bool g_grpc_abort_on_leaks
Definition: iomgr.cc:59
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_core::ExecCtx
Definition: exec_ctx.h:97
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
executor.h
dump_objects
static void dump_objects(const char *kind)
Definition: iomgr.cc:90
grpc_core::Executor::InitAll
static void InitAll()
Definition: executor.cc:377
grpc_iomgr_init
void grpc_iomgr_init()
Definition: iomgr.cc:61
g_mu
static gpr_mu g_mu
Definition: iomgr.cc:55
grpc_iomgr_register_object
void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name)
Definition: iomgr.cc:184
grpc_iomgr_abort_on_leaks
bool grpc_iomgr_abort_on_leaks(void)
Definition: iomgr.cc:202
global_config.h
grpc_iomgr_object::prev
struct grpc_iomgr_object * prev
Definition: iomgr_internal.h:32
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
grpc_core::ExecCtx::SetNowIomgrShutdown
void SetNowIomgrShutdown()
Definition: exec_ctx.h:191
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
thd.h
grpc_iomgr_platform_shutdown
void grpc_iomgr_platform_shutdown()
Definition: iomgr_internal.cc:39
exec_ctx.h
grpc_iomgr_object::next
struct grpc_iomgr_object * next
Definition: iomgr_internal.h:31
closure
Definition: proxy.cc:59
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
timer.h
grpc_timer_list_shutdown
void grpc_timer_list_shutdown()
Definition: iomgr/timer.cc:44
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_iomgr_platform_flush
void grpc_iomgr_platform_flush()
Definition: iomgr_internal.cc:37
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
gpr_timespec
Definition: gpr_types.h:50
grpc_error
Definition: error_internal.h:42
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
count_objects
static size_t count_objects(void)
Definition: iomgr.cc:79
sync.h
grpc_closure
Definition: closure.h:56
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:08