David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Alexander Polcyn | db3e898 | 2018-02-21 16:59:24 -0800 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 21 | #include "src/core/lib/iomgr/executor.h" |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 22 | |
| 23 | #include <string.h> |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 24 | #include <functional> |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 25 | |
| 26 | #include <grpc/support/alloc.h> |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 27 | #include <grpc/support/cpu.h> |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 28 | #include <grpc/support/log.h> |
| 29 | #include <grpc/support/sync.h> |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 30 | |
Craig Tiller | 57bb9a9 | 2017-08-31 16:44:15 -0700 | [diff] [blame] | 31 | #include "src/core/lib/debug/stats.h" |
Vijay Pai | b6cf123 | 2018-01-25 21:02:26 -0800 | [diff] [blame] | 32 | #include "src/core/lib/gpr/tls.h" |
Vijay Pai | d4d0a30 | 2018-01-25 13:24:03 -0800 | [diff] [blame] | 33 | #include "src/core/lib/gpr/useful.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/exec_ctx.h" |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 35 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 36 | #define MAX_DEPTH 2 |
| 37 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 38 | #define EXECUTOR_TRACE(format, ...) \ |
| 39 | if (executor_trace.enabled()) { \ |
| 40 | gpr_log(GPR_INFO, format, __VA_ARGS__); \ |
| 41 | } |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 42 | |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 43 | grpc_core::TraceFlag executor_trace(false, "executor"); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 44 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 45 | GPR_TLS_DECL(g_this_thread_state); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 46 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 47 | GrpcExecutor::GrpcExecutor(const char* executor_name) : name(executor_name) { |
| 48 | adding_thread_lock = GPR_SPINLOCK_STATIC_INITIALIZER; |
| 49 | gpr_atm_no_barrier_store(&num_threads, 0); |
| 50 | } |
| 51 | |
| 52 | void GrpcExecutor::Init() { SetThreading(true); } |
| 53 | |
| 54 | size_t GrpcExecutor::RunClosures(grpc_closure_list list) { |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 55 | size_t n = 0; |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 56 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 57 | grpc_closure* c = list.head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 58 | while (c != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 59 | grpc_closure* next = c->next_data.next; |
| 60 | grpc_error* error = c->error_data.error; |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 61 | #ifndef NDEBUG |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 62 | EXECUTOR_TRACE("EXECUTOR: run %p [created by %s:%d]", c, c->file_created, |
| 63 | c->line_created); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 64 | c->scheduled = false; |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 65 | #else |
| 66 | EXECUTOR_TRACE("EXECUTOR: run %p", c); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 67 | #endif |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 68 | c->cb(c->cb_arg, error); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 69 | GRPC_ERROR_UNREF(error); |
| 70 | c = next; |
| 71 | n++; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 72 | grpc_core::ExecCtx::Get()->Flush(); |
Craig Tiller | 061ef74 | 2016-12-29 10:54:09 -0800 | [diff] [blame] | 73 | } |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 74 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 75 | return n; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 78 | bool GrpcExecutor::IsThreaded() { |
| 79 | return gpr_atm_no_barrier_load(&num_threads) > 0; |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 82 | void GrpcExecutor::SetThreading(bool threading) { |
| 83 | gpr_atm curr_num_threads = gpr_atm_no_barrier_load(&num_threads); |
| 84 | |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 85 | if (threading) { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 86 | if (curr_num_threads > 0) return; |
| 87 | |
| 88 | // TODO (sreek): max_threads initialization can be moved into the |
| 89 | // constructor |
| 90 | max_threads = GPR_MAX(1, 2 * gpr_cpu_num_cores()); |
| 91 | gpr_atm_no_barrier_store(&num_threads, 1); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 92 | gpr_tls_init(&g_this_thread_state); |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 93 | thd_state = static_cast<thread_state*>( |
| 94 | gpr_zalloc(sizeof(thread_state) * max_threads)); |
| 95 | |
| 96 | for (size_t i = 0; i < max_threads; i++) { |
| 97 | gpr_mu_init(&thd_state[i].mu); |
| 98 | gpr_cv_init(&thd_state[i].cv); |
| 99 | thd_state[i].id = i; |
| 100 | thd_state[i].thd = grpc_core::Thread(); |
| 101 | thd_state[i].elems = GRPC_CLOSURE_LIST_INIT; |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 104 | thd_state[0].thd = |
| 105 | grpc_core::Thread(name, &GrpcExecutor::ThreadMain, &thd_state[0]); |
| 106 | thd_state[0].thd.Start(); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 107 | } else { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 108 | if (curr_num_threads == 0) return; |
| 109 | |
| 110 | for (size_t i = 0; i < max_threads; i++) { |
| 111 | gpr_mu_lock(&thd_state[i].mu); |
| 112 | thd_state[i].shutdown = true; |
| 113 | gpr_cv_signal(&thd_state[i].cv); |
| 114 | gpr_mu_unlock(&thd_state[i].mu); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 115 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 116 | |
| 117 | /* Ensure no thread is adding a new thread. Once this is past, then no |
| 118 | * thread will try to add a new one either (since shutdown is true) */ |
| 119 | gpr_spinlock_lock(&adding_thread_lock); |
| 120 | gpr_spinlock_unlock(&adding_thread_lock); |
| 121 | |
| 122 | for (gpr_atm i = 0; i < num_threads; i++) { |
| 123 | thd_state[i].thd.Join(); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 124 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 125 | |
| 126 | gpr_atm_no_barrier_store(&num_threads, 0); |
| 127 | for (size_t i = 0; i < max_threads; i++) { |
| 128 | gpr_mu_destroy(&thd_state[i].mu); |
| 129 | gpr_cv_destroy(&thd_state[i].cv); |
| 130 | RunClosures(thd_state[i].elems); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 131 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 132 | |
| 133 | gpr_free(thd_state); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 134 | gpr_tls_destroy(&g_this_thread_state); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 135 | } |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 138 | void GrpcExecutor::Shutdown() { SetThreading(false); } |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 139 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 140 | void GrpcExecutor::ThreadMain(void* arg) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 141 | thread_state* ts = static_cast<thread_state*>(arg); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 142 | gpr_tls_set(&g_this_thread_state, (intptr_t)ts); |
| 143 | |
kpayson64 | 46a6059 | 2018-06-20 15:18:38 -0700 | [diff] [blame] | 144 | grpc_core::ExecCtx exec_ctx(GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD); |
Craig Tiller | 8996208 | 2017-05-12 14:30:42 -0700 | [diff] [blame] | 145 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 146 | size_t subtract_depth = 0; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 147 | for (;;) { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 148 | EXECUTOR_TRACE("EXECUTOR[%ld]: step (sub_depth=%" PRIdPTR ")", ts->id, |
| 149 | subtract_depth); |
| 150 | |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 151 | gpr_mu_lock(&ts->mu); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 152 | ts->depth -= subtract_depth; |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 153 | // Wait for closures to be enqueued or for the executor to be shutdown |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 154 | while (grpc_closure_list_empty(ts->elems) && !ts->shutdown) { |
Craig Tiller | 1ab56d8 | 2017-07-19 09:55:57 -0700 | [diff] [blame] | 155 | ts->queued_long_job = false; |
Sree Kuchibhotla | 54961bb | 2017-12-04 12:50:27 -0800 | [diff] [blame] | 156 | gpr_cv_wait(&ts->cv, &ts->mu, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 157 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 158 | |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 159 | if (ts->shutdown) { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 160 | EXECUTOR_TRACE("EXECUTOR[%ld]: shutdown", ts->id); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 161 | gpr_mu_unlock(&ts->mu); |
| 162 | break; |
| 163 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 164 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 165 | GRPC_STATS_INC_EXECUTOR_QUEUE_DRAINED(); |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 166 | grpc_closure_list closures = ts->elems; |
Yash Tibrewal | 37fdb73 | 2017-09-25 16:45:02 -0700 | [diff] [blame] | 167 | ts->elems = GRPC_CLOSURE_LIST_INIT; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 168 | gpr_mu_unlock(&ts->mu); |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 169 | |
| 170 | EXECUTOR_TRACE("EXECUTOR[%ld]: execute", ts->id); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 171 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 172 | grpc_core::ExecCtx::Get()->InvalidateNow(); |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 173 | subtract_depth = RunClosures(closures); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 177 | void GrpcExecutor::Enqueue(grpc_closure* closure, grpc_error* error, |
| 178 | bool is_short) { |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 179 | bool retry_push; |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 180 | if (is_short) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 181 | GRPC_STATS_INC_EXECUTOR_SCHEDULED_SHORT_ITEMS(); |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 182 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 183 | GRPC_STATS_INC_EXECUTOR_SCHEDULED_LONG_ITEMS(); |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 184 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 185 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 186 | do { |
| 187 | retry_push = false; |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 188 | size_t cur_thread_count = |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 189 | static_cast<size_t>(gpr_atm_no_barrier_load(&num_threads)); |
| 190 | |
| 191 | // If the number of threads is zero(i.e either the executor is not threaded |
| 192 | // or already shutdown), then queue the closure on the exec context itself |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 193 | if (cur_thread_count == 0) { |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 194 | #ifndef NDEBUG |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 195 | EXECUTOR_TRACE("EXECUTOR: schedule %p (created %s:%d) inline", closure, |
| 196 | closure->file_created, closure->line_created); |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 197 | #else |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 198 | EXECUTOR_TRACE("EXECUTOR: schedule %p inline", closure); |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 199 | #endif |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 200 | grpc_closure_list_append(grpc_core::ExecCtx::Get()->closure_list(), |
| 201 | closure, error); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 202 | return; |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 203 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 204 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 205 | thread_state* ts = (thread_state*)gpr_tls_get(&g_this_thread_state); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 206 | if (ts == nullptr) { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 207 | ts = &thd_state[GPR_HASH_POINTER(grpc_core::ExecCtx::Get(), |
| 208 | cur_thread_count)]; |
Craig Tiller | 022ad3a | 2017-09-07 13:01:56 -0700 | [diff] [blame] | 209 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 210 | GRPC_STATS_INC_EXECUTOR_SCHEDULED_TO_SELF(); |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 211 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 212 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 213 | thread_state* orig_ts = ts; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 214 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 215 | bool try_new_thread; |
| 216 | for (;;) { |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 217 | #ifndef NDEBUG |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 218 | EXECUTOR_TRACE( |
| 219 | "EXECUTOR: try to schedule %p (%s) (created %s:%d) to thread %ld", |
| 220 | closure, is_short ? "short" : "long", closure->file_created, |
| 221 | closure->line_created, ts->id); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 222 | #else |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 223 | EXECUTOR_TRACE("EXECUTOR: try to schedule %p (%s) to thread %ld", closure, |
| 224 | is_short ? "short" : "long", ts->id); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 225 | #endif |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 226 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 227 | gpr_mu_lock(&ts->mu); |
| 228 | if (ts->queued_long_job) { |
Craig Tiller | b0ce25e | 2017-09-08 14:42:26 -0700 | [diff] [blame] | 229 | // if there's a long job queued, we never queue anything else to this |
| 230 | // queue (since long jobs can take 'infinite' time and we need to |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 231 | // guarantee no starvation). Spin through queues and try again |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 232 | gpr_mu_unlock(&ts->mu); |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 233 | size_t idx = ts->id; |
| 234 | ts = &thd_state[(idx + 1) % cur_thread_count]; |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 235 | if (ts == orig_ts) { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 236 | // We cycled through all the threads. Retry enqueue again (by creating |
| 237 | // a new thread) |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 238 | retry_push = true; |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 239 | // TODO (sreek): What if the executor is shutdown OR if |
| 240 | // cur_thread_count is already equal to max_threads ? (currently - as |
| 241 | // of July 2018, we do not run in to this issue because there is only |
| 242 | // one instance of long job in gRPC. This has to be fixed soon) |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 243 | try_new_thread = true; |
| 244 | break; |
| 245 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 246 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 247 | continue; |
| 248 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 249 | |
| 250 | // == Found the thread state (i.e thread) to enqueue this closure! == |
| 251 | |
| 252 | // Also, if this thread has been waiting for closures, wake it up. |
| 253 | // - If grpc_closure_list_empty() is true and the Executor is not |
| 254 | // shutdown, it means that the thread must be waiting in ThreadMain() |
| 255 | // - Note that gpr_cv_signal() won't immediately wakeup the thread. That |
| 256 | // happens after we release the mutex &ts->mu a few lines below |
ncteisen | c0b00c3 | 2017-12-14 11:30:38 -0800 | [diff] [blame] | 257 | if (grpc_closure_list_empty(ts->elems) && !ts->shutdown) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 258 | GRPC_STATS_INC_EXECUTOR_WAKEUP_INITIATED(); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 259 | gpr_cv_signal(&ts->cv); |
| 260 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 261 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 262 | grpc_closure_list_append(&ts->elems, closure, error); |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 263 | |
| 264 | // If we already queued more than MAX_DEPTH number of closures on this |
| 265 | // thread, use this as a hint to create more threads |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame] | 266 | ts->depth++; |
| 267 | try_new_thread = ts->depth > MAX_DEPTH && |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 268 | cur_thread_count < max_threads && !ts->shutdown; |
| 269 | |
| 270 | ts->queued_long_job = !is_short; |
| 271 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 272 | gpr_mu_unlock(&ts->mu); |
| 273 | break; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 274 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 275 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 276 | if (try_new_thread && gpr_spinlock_trylock(&adding_thread_lock)) { |
| 277 | cur_thread_count = |
| 278 | static_cast<size_t>(gpr_atm_no_barrier_load(&num_threads)); |
| 279 | if (cur_thread_count < max_threads) { |
| 280 | // Increment num_threads (Safe to do a no_barrier_store instead of a |
| 281 | // cas because we always increment num_threads under the |
| 282 | // 'adding_thread_lock') |
| 283 | gpr_atm_no_barrier_store(&num_threads, cur_thread_count + 1); |
| 284 | |
| 285 | thd_state[cur_thread_count].thd = grpc_core::Thread( |
| 286 | name, &GrpcExecutor::ThreadMain, &thd_state[cur_thread_count]); |
| 287 | thd_state[cur_thread_count].thd.Start(); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 288 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 289 | gpr_spinlock_unlock(&adding_thread_lock); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 290 | } |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 291 | |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 292 | if (retry_push) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 293 | GRPC_STATS_INC_EXECUTOR_PUSH_RETRIES(); |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 294 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 295 | } while (retry_push); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 296 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 297 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 298 | static GrpcExecutor g_global_executor("grpc-executor"); |
| 299 | |
| 300 | void enqueue_long(grpc_closure* closure, grpc_error* error) { |
| 301 | g_global_executor.Enqueue(closure, error, false); |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 304 | void enqueue_short(grpc_closure* closure, grpc_error* error) { |
| 305 | g_global_executor.Enqueue(closure, error, true); |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 308 | // Short-Job executor scheduler |
| 309 | static const grpc_closure_scheduler_vtable global_executor_vtable_short = { |
| 310 | enqueue_short, enqueue_short, "executor-short"}; |
| 311 | static grpc_closure_scheduler global_scheduler_short = { |
| 312 | &global_executor_vtable_short}; |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 313 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 314 | // Long-job executor scheduler |
| 315 | static const grpc_closure_scheduler_vtable global_executor_vtable_long = { |
| 316 | enqueue_long, enqueue_long, "executor-long"}; |
| 317 | static grpc_closure_scheduler global_scheduler_long = { |
| 318 | &global_executor_vtable_long}; |
| 319 | |
| 320 | void grpc_executor_init() { g_global_executor.Init(); } |
| 321 | |
| 322 | void grpc_executor_shutdown() { g_global_executor.Shutdown(); } |
| 323 | |
| 324 | bool grpc_executor_is_threaded() { return g_global_executor.IsThreaded(); } |
| 325 | |
| 326 | void grpc_executor_set_threading(bool enable) { |
| 327 | g_global_executor.SetThreading(enable); |
| 328 | } |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 329 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 330 | grpc_closure_scheduler* grpc_executor_scheduler( |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 331 | grpc_executor_job_length length) { |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 332 | return length == GRPC_EXECUTOR_SHORT ? &global_scheduler_short |
| 333 | : &global_scheduler_long; |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 334 | } |