ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | f315bc1 | 2016-02-18 08:25:15 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/iomgr/iomgr.h" |
| 35 | |
| 36 | #include <stdlib.h> |
Craig Tiller | 131f6ed | 2015-09-15 08:20:20 -0700 | [diff] [blame] | 37 | #include <string.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 38 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 41 | #include <grpc/support/string_util.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 42 | #include <grpc/support/sync.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 43 | #include <grpc/support/thd.h> |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 44 | #include <grpc/support/useful.h> |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 45 | |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 46 | #include "src/core/iomgr/exec_ctx.h" |
Craig Tiller | 131f6ed | 2015-09-15 08:20:20 -0700 | [diff] [blame] | 47 | #include "src/core/iomgr/iomgr_internal.h" |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 48 | #include "src/core/iomgr/timer.h" |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 49 | #include "src/core/support/env.h" |
Craig Tiller | 131f6ed | 2015-09-15 08:20:20 -0700 | [diff] [blame] | 50 | #include "src/core/support/string.h" |
| 51 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 52 | static gpr_mu g_mu; |
Nicolas "Pixel" Noble | ae7b45a | 2015-02-04 03:28:34 +0100 | [diff] [blame] | 53 | static gpr_cv g_rcv; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 54 | static int g_shutdown; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 55 | static grpc_iomgr_object g_root_object; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 56 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | void grpc_iomgr_init(void) { |
Craig Tiller | 994c262 | 2015-07-23 14:00:58 -0700 | [diff] [blame] | 58 | g_shutdown = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | gpr_mu_init(&g_mu); |
| 60 | gpr_cv_init(&g_rcv); |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 61 | grpc_exec_ctx_global_init(); |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 62 | grpc_timer_list_init(gpr_now(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 63 | g_root_object.next = g_root_object.prev = &g_root_object; |
| 64 | g_root_object.name = "root"; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | grpc_iomgr_platform_init(); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 66 | grpc_pollset_global_init(); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 69 | static size_t count_objects(void) { |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 70 | grpc_iomgr_object *obj; |
| 71 | size_t n = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) { |
| 73 | n++; |
| 74 | } |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 75 | return n; |
| 76 | } |
| 77 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | static void dump_objects(const char *kind) { |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 79 | grpc_iomgr_object *obj; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) { |
| 81 | gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj); |
| 82 | } |
Craig Tiller | 49d03c8 | 2015-09-03 14:34:30 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | void grpc_iomgr_shutdown(void) { |
| 86 | gpr_timespec shutdown_deadline = gpr_time_add( |
| 87 | gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN)); |
| 88 | gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME); |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 89 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 90 | |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 91 | grpc_iomgr_platform_flush(); |
| 92 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | gpr_mu_lock(&g_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 94 | g_shutdown = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | while (g_root_object.next != &g_root_object) { |
| 96 | if (gpr_time_cmp( |
| 97 | gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time), |
| 98 | gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) { |
| 99 | if (g_root_object.next != &g_root_object) { |
| 100 | gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed", |
| 101 | count_objects()); |
| 102 | } |
| 103 | last_warning_time = gpr_now(GPR_CLOCK_REALTIME); |
Craig Tiller | 29f741f | 2015-05-29 12:29:43 -0700 | [diff] [blame] | 104 | } |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 105 | if (grpc_timer_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC), |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | NULL)) { |
| 107 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 108 | grpc_exec_ctx_flush(&exec_ctx); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | gpr_mu_lock(&g_mu); |
| 110 | continue; |
| 111 | } |
| 112 | if (g_root_object.next != &g_root_object) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | gpr_timespec short_deadline = gpr_time_add( |
| 114 | gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100, GPR_TIMESPAN)); |
| 115 | if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) { |
| 116 | if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) { |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 117 | if (g_root_object.next != &g_root_object) { |
| 118 | gpr_log(GPR_DEBUG, |
| 119 | "Failed to free %d iomgr objects before shutdown deadline: " |
| 120 | "memory leaks are likely", |
| 121 | count_objects()); |
| 122 | dump_objects("LEAKED"); |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 123 | if (grpc_iomgr_abort_on_leaks()) { |
| 124 | abort(); |
| 125 | } |
Craig Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 126 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | gpr_mu_unlock(&g_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 133 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 134 | grpc_timer_list_shutdown(&exec_ctx); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 8e0b08a | 2015-06-01 17:04:17 -0700 | [diff] [blame] | 136 | |
Craig Tiller | 62aef62 | 2015-09-21 21:27:54 -0700 | [diff] [blame] | 137 | /* ensure all threads have left g_mu */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | gpr_mu_lock(&g_mu); |
| 139 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 62aef62 | 2015-09-21 21:27:54 -0700 | [diff] [blame] | 140 | |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 141 | grpc_pollset_global_shutdown(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 142 | grpc_iomgr_platform_shutdown(); |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 143 | grpc_exec_ctx_global_shutdown(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | gpr_mu_destroy(&g_mu); |
| 145 | gpr_cv_destroy(&g_rcv); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) { |
| 149 | obj->name = gpr_strdup(name); |
| 150 | gpr_mu_lock(&g_mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 151 | obj->next = &g_root_object; |
Craig Tiller | 49d03c8 | 2015-09-03 14:34:30 -0700 | [diff] [blame] | 152 | obj->prev = g_root_object.prev; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 153 | obj->next->prev = obj->prev->next = obj; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | gpr_mu_unlock(&g_mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) { |
| 158 | gpr_mu_lock(&g_mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 159 | obj->next->prev = obj->prev; |
| 160 | obj->prev->next = obj->next; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | gpr_cv_signal(&g_rcv); |
| 162 | gpr_mu_unlock(&g_mu); |
| 163 | gpr_free(obj->name); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 164 | } |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 165 | |
| 166 | bool grpc_iomgr_abort_on_leaks(void) { |
| 167 | char *env = gpr_getenv("GRPC_ABORT_ON_LEAKS"); |
| 168 | if (env == NULL) return false; |
| 169 | static const char *truthy[] = {"yes", "Yes", "YES", "true", |
| 170 | "True", "TRUE", "1"}; |
| 171 | for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) { |
| 172 | if (0 == strcmp(env, truthy[i])) return true; |
| 173 | } |
| 174 | return false; |
| 175 | } |