| 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 <inttypes.h> |
| 22 |
|
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "absl/strings/str_cat.h" |
| 26 |
|
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/cpu.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
#include <grpc/support/sync.h> |
| 31 |
|
| 32 |
#include "src/core/lib/debug/trace.h" |
| 33 |
#include "src/core/lib/gpr/spinlock.h" |
| 34 |
#include "src/core/lib/gpr/useful.h" |
| 35 |
#include "src/core/lib/gprpp/manual_constructor.h" |
| 36 |
#include "src/core/lib/gprpp/time.h" |
| 37 |
#include "src/core/lib/gprpp/time_averaged_stats.h" |
| 38 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 39 |
#include "src/core/lib/iomgr/port.h" |
| 40 |
#include "src/core/lib/iomgr/timer.h" |
| 41 |
#include "src/core/lib/iomgr/timer_heap.h" |
| 42 |
|
| 43 |
#define INVALID_HEAP_INDEX 0xffffffffu |
| 44 |
|
| 45 |
#define ADD_DEADLINE_SCALE 0.33 |
| 46 |
#define MIN_QUEUE_WINDOW_DURATION 0.01 |
| 47 |
#define MAX_QUEUE_WINDOW_DURATION 1.0 |
| 48 |
|
| 49 |
grpc_core::TraceFlag grpc_timer_trace(false, "timer"); |
| 50 |
grpc_core::TraceFlag grpc_timer_check_trace(false, "timer_check"); |
| 51 |
|
| 52 |
/* A "timer shard". Contains a 'heap' and a 'list' of timers. All timers with |
| 53 |
* deadlines earlier than 'queue_deadline_cap' are maintained in the heap and |
| 54 |
* others are maintained in the list (unordered). This helps to keep the number |
| 55 |
* of elements in the heap low. |
| 56 |
* |
| 57 |
* The 'queue_deadline_cap' gets recomputed periodically based on the timer |
| 58 |
* stats maintained in 'stats' and the relevant timers are then moved from the |
| 59 |
* 'list' to 'heap'. |
| 60 |
*/ |
| 61 |
struct timer_shard { |
| 62 |
gpr_mu mu; |
| 63 |
grpc_core::ManualConstructor<grpc_core::TimeAveragedStats> stats; |
| 64 |
/* All and only timers with deadlines < this will be in the heap. */ |
| 65 |
grpc_core::Timestamp queue_deadline_cap; |
| 66 |
/* The deadline of the next timer due in this shard. */ |
| 67 |
grpc_core::Timestamp min_deadline; |
| 68 |
/* Index of this timer_shard in the g_shard_queue. */ |
| 69 |
uint32_t shard_queue_index; |
| 70 |
/* This holds all timers with deadlines < queue_deadline_cap. Timers in this |
| 71 |
list have the top bit of their deadline set to 0. */ |
| 72 |
grpc_timer_heap heap; |
| 73 |
/* This holds timers whose deadline is >= queue_deadline_cap. */ |
| 74 |
grpc_timer list; |
| 75 |
}; |
| 76 |
static size_t g_num_shards; |
| 77 |
|
| 78 |
/* Array of timer shards. Whenever a timer (grpc_timer *) is added, its address |
| 79 |
* is hashed to select the timer shard to add the timer to */ |
| 80 |
static timer_shard* g_shards; |
| 81 |
|
| 82 |
/* Maintains a sorted list of timer shards (sorted by their min_deadline, i.e |
| 83 |
* the deadline of the next timer in each shard). |
| 84 |
* Access to this is protected by g_shared_mutables.mu */ |
| 85 |
static timer_shard** g_shard_queue; |
| 86 |
|
| 87 |
#ifndef NDEBUG |
| 88 |
|
| 89 |
/* == DEBUG ONLY: hash table for duplicate timer detection == */ |
| 90 |
|
| 91 |
#define NUM_HASH_BUCKETS 1009 /* Prime number close to 1000 */ |
| 92 |
|
| 93 |
static gpr_mu g_hash_mu[NUM_HASH_BUCKETS]; /* One mutex per bucket */ |
| 94 |
static grpc_timer* g_timer_ht[NUM_HASH_BUCKETS] = {nullptr}; |
| 95 |
|
| 96 |
static void init_timer_ht() { |
| 97 |
for (int i = 0; i < NUM_HASH_BUCKETS; i++) { |
| 98 |
gpr_mu_init(&g_hash_mu[i]); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static void destroy_timer_ht() { |
| 103 |
for (int i = 0; i < NUM_HASH_BUCKETS; i++) { |
| 104 |
gpr_mu_destroy(&g_hash_mu[i]); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static bool is_in_ht(grpc_timer* t) { |
| 109 |
size_t i = grpc_core::HashPointer(t, NUM_HASH_BUCKETS); |
| 110 |
|
| 111 |
gpr_mu_lock(&g_hash_mu[i]); |
| 112 |
grpc_timer* p = g_timer_ht[i]; |
| 113 |
while (p != nullptr && p != t) { |
| 114 |
p = p->hash_table_next; |
| 115 |
} |
| 116 |
gpr_mu_unlock(&g_hash_mu[i]); |
| 117 |
|
| 118 |
return (p == t); |
| 119 |
} |
| 120 |
|
| 121 |
static void add_to_ht(grpc_timer* t) { |
| 122 |
GPR_ASSERT(!t->hash_table_next); |
| 123 |
size_t i = grpc_core::HashPointer(t, NUM_HASH_BUCKETS); |
| 124 |
|
| 125 |
gpr_mu_lock(&g_hash_mu[i]); |
| 126 |
grpc_timer* p = g_timer_ht[i]; |
| 127 |
while (p != nullptr && p != t) { |
| 128 |
p = p->hash_table_next; |
| 129 |
} |
| 130 |
|
| 131 |
if (p == t) { |
| 132 |
grpc_closure* c = t->closure; |
| 133 |
gpr_log(GPR_ERROR, |
| 134 |
"** Duplicate timer (%p) being added. Closure: (%p), created at: " |
| 135 |
"(%s:%d), scheduled at: (%s:%d) **", |
| 136 |
t, c, c->file_created, c->line_created, c->file_initiated, |
| 137 |
c->line_initiated); |
| 138 |
abort(); |
| 139 |
} |
| 140 |
|
| 141 |
/* Timer not present in the bucket. Insert at head of the list */ |
| 142 |
t->hash_table_next = g_timer_ht[i]; |
| 143 |
g_timer_ht[i] = t; |
| 144 |
gpr_mu_unlock(&g_hash_mu[i]); |
| 145 |
} |
| 146 |
|
| 147 |
static void remove_from_ht(grpc_timer* t) { |
| 148 |
size_t i = grpc_core::HashPointer(t, NUM_HASH_BUCKETS); |
| 149 |
bool removed = false; |
| 150 |
|
| 151 |
gpr_mu_lock(&g_hash_mu[i]); |
| 152 |
if (g_timer_ht[i] == t) { |
| 153 |
g_timer_ht[i] = g_timer_ht[i]->hash_table_next; |
| 154 |
removed = true; |
| 155 |
} else if (g_timer_ht[i] != nullptr) { |
| 156 |
grpc_timer* p = g_timer_ht[i]; |
| 157 |
while (p->hash_table_next != nullptr && p->hash_table_next != t) { |
| 158 |
p = p->hash_table_next; |
| 159 |
} |
| 160 |
|
| 161 |
if (p->hash_table_next == t) { |
| 162 |
p->hash_table_next = t->hash_table_next; |
| 163 |
removed = true; |
| 164 |
} |
| 165 |
} |
| 166 |
gpr_mu_unlock(&g_hash_mu[i]); |
| 167 |
|
| 168 |
if (!removed) { |
| 169 |
grpc_closure* c = t->closure; |
| 170 |
gpr_log(GPR_ERROR, |
| 171 |
"** Removing timer (%p) that is not added to hash table. Closure " |
| 172 |
"(%p), created at: (%s:%d), scheduled at: (%s:%d) **", |
| 173 |
t, c, c->file_created, c->line_created, c->file_initiated, |
| 174 |
c->line_initiated); |
| 175 |
abort(); |
| 176 |
} |
| 177 |
|
| 178 |
t->hash_table_next = nullptr; |
| 179 |
} |
| 180 |
|
| 181 |
/* If a timer is added to a timer shard (either heap or a list), it must |
| 182 |
* be pending. A timer is added to hash table only-if it is added to the |
| 183 |
* timer shard. |
| 184 |
* Therefore, if timer->pending is false, it cannot be in hash table */ |
| 185 |
static void validate_non_pending_timer(grpc_timer* t) { |
| 186 |
if (!t->pending && is_in_ht(t)) { |
| 187 |
grpc_closure* c = t->closure; |
| 188 |
gpr_log(GPR_ERROR, |
| 189 |
"** gpr_timer_cancel() called on a non-pending timer (%p) which " |
| 190 |
"is in the hash table. Closure: (%p), created at: (%s:%d), " |
| 191 |
"scheduled at: (%s:%d) **", |
| 192 |
t, c, c->file_created, c->line_created, c->file_initiated, |
| 193 |
c->line_initiated); |
| 194 |
abort(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
#define INIT_TIMER_HASH_TABLE() init_timer_ht() |
| 199 |
#define DESTROY_TIMER_HASH_TABLE() destroy_timer_ht() |
| 200 |
#define ADD_TO_HASH_TABLE(t) add_to_ht((t)) |
| 201 |
#define REMOVE_FROM_HASH_TABLE(t) remove_from_ht((t)) |
| 202 |
#define VALIDATE_NON_PENDING_TIMER(t) validate_non_pending_timer((t)) |
| 203 |
|
| 204 |
#else |
| 205 |
|
| 206 |
#define INIT_TIMER_HASH_TABLE() |
| 207 |
#define DESTROY_TIMER_HASH_TABLE() |
| 208 |
#define ADD_TO_HASH_TABLE(t) |
| 209 |
#define REMOVE_FROM_HASH_TABLE(t) |
| 210 |
#define VALIDATE_NON_PENDING_TIMER(t) |
| 211 |
|
| 212 |
#endif |
| 213 |
|
| 214 |
/* Thread local variable that stores the deadline of the next timer the thread |
| 215 |
* has last-seen. This is an optimization to prevent the thread from checking |
| 216 |
* shared_mutables.min_timer (which requires acquiring shared_mutables.mu lock, |
| 217 |
* an expensive operation) */ |
| 218 |
static thread_local int64_t g_last_seen_min_timer; |
| 219 |
|
| 220 |
struct shared_mutables { |
| 221 |
/* The deadline of the next timer due across all timer shards */ |
| 222 |
grpc_core::Timestamp min_timer; |
| 223 |
/* Allow only one run_some_expired_timers at once */ |
| 224 |
gpr_spinlock checker_mu; |
| 225 |
bool initialized; |
| 226 |
/* Protects g_shard_queue (and the shared_mutables struct itself) */ |
| 227 |
gpr_mu mu; |
| 228 |
} GPR_ALIGN_STRUCT(GPR_CACHELINE_SIZE); |
| 229 |
|
| 230 |
static struct shared_mutables g_shared_mutables; |
| 231 |
|
| 232 |
static grpc_timer_check_result run_some_expired_timers( |
| 233 |
grpc_core::Timestamp now, grpc_core::Timestamp* next, |
| 234 |
grpc_error_handle error); |
| 235 |
|
| 236 |
static grpc_core::Timestamp compute_min_deadline(timer_shard* shard) { |
| 237 |
return grpc_timer_heap_is_empty(&shard->heap) |
| 238 |
? shard->queue_deadline_cap + grpc_core::Duration::Epsilon() |
| 239 |
: grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 240 |
grpc_timer_heap_top(&shard->heap)->deadline); |
| 241 |
} |
| 242 |
|
| 243 |
static void timer_list_init() { |
| 244 |
uint32_t i; |
| 245 |
|
| 246 |
g_num_shards = grpc_core::Clamp(2 * gpr_cpu_num_cores(), 1u, 32u); |
| 247 |
g_shards = |
| 248 |
static_cast<timer_shard*>(gpr_zalloc(g_num_shards * sizeof(*g_shards))); |
| 249 |
g_shard_queue = static_cast<timer_shard**>( |
| 250 |
gpr_zalloc(g_num_shards * sizeof(*g_shard_queue))); |
| 251 |
|
| 252 |
g_shared_mutables.initialized = true; |
| 253 |
g_shared_mutables.checker_mu = GPR_SPINLOCK_INITIALIZER; |
| 254 |
gpr_mu_init(&g_shared_mutables.mu); |
| 255 |
g_shared_mutables.min_timer = grpc_core::Timestamp::Now(); |
| 256 |
|
| 257 |
g_last_seen_min_timer = 0; |
| 258 |
|
| 259 |
for (i = 0; i < g_num_shards; i++) { |
| 260 |
timer_shard* shard = &g_shards[i]; |
| 261 |
gpr_mu_init(&shard->mu); |
| 262 |
shard->stats.Init(1.0 / ADD_DEADLINE_SCALE, 0.1, 0.5); |
| 263 |
shard->queue_deadline_cap = g_shared_mutables.min_timer; |
| 264 |
shard->shard_queue_index = i; |
| 265 |
grpc_timer_heap_init(&shard->heap); |
| 266 |
shard->list.next = shard->list.prev = &shard->list; |
| 267 |
shard->min_deadline = compute_min_deadline(shard); |
| 268 |
g_shard_queue[i] = shard; |
| 269 |
} |
| 270 |
|
| 271 |
INIT_TIMER_HASH_TABLE(); |
| 272 |
} |
| 273 |
|
| 274 |
static void timer_list_shutdown() { |
| 275 |
size_t i; |
| 276 |
run_some_expired_timers(grpc_core::Timestamp::InfFuture(), nullptr, |
| 277 |
GRPC_ERROR_CREATE("Timer list shutdown")); |
| 278 |
for (i = 0; i < g_num_shards; i++) { |
| 279 |
timer_shard* shard = &g_shards[i]; |
| 280 |
gpr_mu_destroy(&shard->mu); |
| 281 |
grpc_timer_heap_destroy(&shard->heap); |
| 282 |
} |
| 283 |
gpr_mu_destroy(&g_shared_mutables.mu); |
| 284 |
gpr_free(g_shards); |
| 285 |
gpr_free(g_shard_queue); |
| 286 |
g_shared_mutables.initialized = false; |
| 287 |
|
| 288 |
DESTROY_TIMER_HASH_TABLE(); |
| 289 |
} |
| 290 |
|
| 291 |
/* returns true if the first element in the list */ |
| 292 |
static void list_join(grpc_timer* head, grpc_timer* timer) { |
| 293 |
timer->next = head; |
| 294 |
timer->prev = head->prev; |
| 295 |
timer->next->prev = timer->prev->next = timer; |
| 296 |
} |
| 297 |
|
| 298 |
static void list_remove(grpc_timer* timer) { |
| 299 |
timer->next->prev = timer->prev; |
| 300 |
timer->prev->next = timer->next; |
| 301 |
} |
| 302 |
|
| 303 |
static void swap_adjacent_shards_in_queue(uint32_t first_shard_queue_index) { |
| 304 |
timer_shard* temp; |
| 305 |
temp = g_shard_queue[first_shard_queue_index]; |
| 306 |
g_shard_queue[first_shard_queue_index] = |
| 307 |
g_shard_queue[first_shard_queue_index + 1]; |
| 308 |
g_shard_queue[first_shard_queue_index + 1] = temp; |
| 309 |
g_shard_queue[first_shard_queue_index]->shard_queue_index = |
| 310 |
first_shard_queue_index; |
| 311 |
g_shard_queue[first_shard_queue_index + 1]->shard_queue_index = |
| 312 |
first_shard_queue_index + 1; |
| 313 |
} |
| 314 |
|
| 315 |
static void note_deadline_change(timer_shard* shard) { |
| 316 |
while (shard->shard_queue_index > 0 && |
| 317 |
shard->min_deadline < |
| 318 |
g_shard_queue[shard->shard_queue_index - 1]->min_deadline) { |
| 319 |
swap_adjacent_shards_in_queue(shard->shard_queue_index - 1); |
| 320 |
} |
| 321 |
while (shard->shard_queue_index < g_num_shards - 1 && |
| 322 |
shard->min_deadline > |
| 323 |
g_shard_queue[shard->shard_queue_index + 1]->min_deadline) { |
| 324 |
swap_adjacent_shards_in_queue(shard->shard_queue_index); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
void grpc_timer_init_unset(grpc_timer* timer) { timer->pending = false; } |
| 329 |
|
| 330 |
static void timer_init(grpc_timer* timer, grpc_core::Timestamp deadline, |
| 331 |
grpc_closure* closure) { |
| 332 |
int is_first_timer = 0; |
| 333 |
timer_shard* shard = &g_shards[grpc_core::HashPointer(timer, g_num_shards)]; |
| 334 |
timer->closure = closure; |
| 335 |
timer->deadline = deadline.milliseconds_after_process_epoch(); |
| 336 |
|
| 337 |
#ifndef NDEBUG |
| 338 |
timer->hash_table_next = nullptr; |
| 339 |
#endif |
| 340 |
|
| 341 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_trace)) { |
| 342 |
gpr_log(GPR_INFO, "TIMER %p: SET %" PRId64 " now %" PRId64 " call %p[%p]", |
| 343 |
timer, deadline.milliseconds_after_process_epoch(), |
| 344 |
grpc_core::Timestamp::Now().milliseconds_after_process_epoch(), |
| 345 |
closure, closure->cb); |
| 346 |
} |
| 347 |
|
| 348 |
if (!g_shared_mutables.initialized) { |
| 349 |
timer->pending = false; |
| 350 |
grpc_core::ExecCtx::Run( |
| 351 |
DEBUG_LOCATION, timer->closure, |
| 352 |
GRPC_ERROR_CREATE("Attempt to create timer before initialization")); |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
gpr_mu_lock(&shard->mu); |
| 357 |
timer->pending = true; |
| 358 |
grpc_core::Timestamp now = grpc_core::Timestamp::Now(); |
| 359 |
if (deadline <= now) { |
| 360 |
timer->pending = false; |
| 361 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, timer->closure, absl::OkStatus()); |
| 362 |
gpr_mu_unlock(&shard->mu); |
| 363 |
/* early out */ |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
shard->stats->AddSample((deadline - now).millis() / 1000.0); |
| 368 |
|
| 369 |
ADD_TO_HASH_TABLE(timer); |
| 370 |
|
| 371 |
if (deadline < shard->queue_deadline_cap) { |
| 372 |
is_first_timer = grpc_timer_heap_add(&shard->heap, timer); |
| 373 |
} else { |
| 374 |
timer->heap_index = INVALID_HEAP_INDEX; |
| 375 |
list_join(&shard->list, timer); |
| 376 |
} |
| 377 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_trace)) { |
| 378 |
gpr_log(GPR_INFO, |
| 379 |
" .. add to shard %d with queue_deadline_cap=%" PRId64 |
| 380 |
" => is_first_timer=%s", |
| 381 |
static_cast<int>(shard - g_shards), |
| 382 |
shard->queue_deadline_cap.milliseconds_after_process_epoch(), |
| 383 |
is_first_timer ? "true" : "false"); |
| 384 |
} |
| 385 |
gpr_mu_unlock(&shard->mu); |
| 386 |
|
| 387 |
/* Deadline may have decreased, we need to adjust the main queue. Note |
| 388 |
that there is a potential racy unlocked region here. There could be a |
| 389 |
reordering of multiple grpc_timer_init calls, at this point, but the < test |
| 390 |
below should ensure that we err on the side of caution. There could |
| 391 |
also be a race with grpc_timer_check, which might beat us to the lock. In |
| 392 |
that case, it is possible that the timer that we added will have already |
| 393 |
run by the time we hold the lock, but that too is a safe error. |
| 394 |
Finally, it's possible that the grpc_timer_check that intervened failed to |
| 395 |
trigger the new timer because the min_deadline hadn't yet been reduced. |
| 396 |
In that case, the timer will simply have to wait for the next |
| 397 |
grpc_timer_check. */ |
| 398 |
if (is_first_timer) { |
| 399 |
gpr_mu_lock(&g_shared_mutables.mu); |
| 400 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_trace)) { |
| 401 |
gpr_log(GPR_INFO, " .. old shard min_deadline=%" PRId64, |
| 402 |
shard->min_deadline.milliseconds_after_process_epoch()); |
| 403 |
} |
| 404 |
if (deadline < shard->min_deadline) { |
| 405 |
grpc_core::Timestamp old_min_deadline = g_shard_queue[0]->min_deadline; |
| 406 |
shard->min_deadline = deadline; |
| 407 |
note_deadline_change(shard); |
| 408 |
if (shard->shard_queue_index == 0 && deadline < old_min_deadline) { |
| 409 |
#if GPR_ARCH_64 |
| 410 |
// TODO(sreek): Using c-style cast here. static_cast<> gives an error |
| 411 |
// (on mac platforms complaining that gpr_atm* is (long *) while |
| 412 |
// (&g_shared_mutables.min_timer) is a (long long *). The cast should be |
| 413 |
// safe since we know that both are pointer types and 64-bit wide. |
| 414 |
gpr_atm_no_barrier_store((gpr_atm*)(&g_shared_mutables.min_timer), |
| 415 |
deadline.milliseconds_after_process_epoch()); |
| 416 |
#else |
| 417 |
// On 32-bit systems, gpr_atm_no_barrier_store does not work on 64-bit |
| 418 |
// types (like grpc_core::Timestamp). So all reads and writes to |
| 419 |
// g_shared_mutables.min_timer varialbe under g_shared_mutables.mu |
| 420 |
g_shared_mutables.min_timer = deadline; |
| 421 |
#endif |
| 422 |
grpc_kick_poller(); |
| 423 |
} |
| 424 |
} |
| 425 |
gpr_mu_unlock(&g_shared_mutables.mu); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
static void timer_consume_kick(void) { |
| 430 |
/* Force re-evaluation of last seen min */ |
| 431 |
g_last_seen_min_timer = 0; |
| 432 |
} |
| 433 |
|
| 434 |
static void timer_cancel(grpc_timer* timer) { |
| 435 |
if (!g_shared_mutables.initialized) { |
| 436 |
/* must have already been cancelled, also the shard mutex is invalid */ |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
timer_shard* shard = &g_shards[grpc_core::HashPointer(timer, g_num_shards)]; |
| 441 |
gpr_mu_lock(&shard->mu); |
| 442 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_trace)) { |
| 443 |
gpr_log(GPR_INFO, "TIMER %p: CANCEL pending=%s", timer, |
| 444 |
timer->pending ? "true" : "false"); |
| 445 |
} |
| 446 |
|
| 447 |
if (timer->pending) { |
| 448 |
REMOVE_FROM_HASH_TABLE(timer); |
| 449 |
|
| 450 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, timer->closure, |
| 451 |
absl::CancelledError()); |
| 452 |
timer->pending = false; |
| 453 |
if (timer->heap_index == INVALID_HEAP_INDEX) { |
| 454 |
list_remove(timer); |
| 455 |
} else { |
| 456 |
grpc_timer_heap_remove(&shard->heap, timer); |
| 457 |
} |
| 458 |
} else { |
| 459 |
VALIDATE_NON_PENDING_TIMER(timer); |
| 460 |
} |
| 461 |
gpr_mu_unlock(&shard->mu); |
| 462 |
} |
| 463 |
|
| 464 |
/* Rebalances the timer shard by computing a new 'queue_deadline_cap' and moving |
| 465 |
all relevant timers in shard->list (i.e timers with deadlines earlier than |
| 466 |
'queue_deadline_cap') into into shard->heap. |
| 467 |
Returns 'true' if shard->heap has at least ONE element |
| 468 |
REQUIRES: shard->mu locked */ |
| 469 |
static bool refill_heap(timer_shard* shard, grpc_core::Timestamp now) { |
| 470 |
/* Compute the new queue window width and bound by the limits: */ |
| 471 |
double computed_deadline_delta = |
| 472 |
shard->stats->UpdateAverage() * ADD_DEADLINE_SCALE; |
| 473 |
double deadline_delta = |
| 474 |
grpc_core::Clamp(computed_deadline_delta, MIN_QUEUE_WINDOW_DURATION, |
| 475 |
MAX_QUEUE_WINDOW_DURATION); |
| 476 |
grpc_timer *timer, *next; |
| 477 |
|
| 478 |
/* Compute the new cap and put all timers under it into the queue: */ |
| 479 |
shard->queue_deadline_cap = |
| 480 |
std::max(now, shard->queue_deadline_cap) + |
| 481 |
grpc_core::Duration::FromSecondsAsDouble(deadline_delta); |
| 482 |
|
| 483 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 484 |
gpr_log(GPR_INFO, " .. shard[%d]->queue_deadline_cap --> %" PRId64, |
| 485 |
static_cast<int>(shard - g_shards), |
| 486 |
shard->queue_deadline_cap.milliseconds_after_process_epoch()); |
| 487 |
} |
| 488 |
for (timer = shard->list.next; timer != &shard->list; timer = next) { |
| 489 |
next = timer->next; |
| 490 |
auto timer_deadline = |
| 491 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 492 |
timer->deadline); |
| 493 |
|
| 494 |
if (timer_deadline < shard->queue_deadline_cap) { |
| 495 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 496 |
gpr_log(GPR_INFO, " .. add timer with deadline %" PRId64 " to heap", |
| 497 |
timer_deadline.milliseconds_after_process_epoch()); |
| 498 |
} |
| 499 |
list_remove(timer); |
| 500 |
grpc_timer_heap_add(&shard->heap, timer); |
| 501 |
} |
| 502 |
} |
| 503 |
return !grpc_timer_heap_is_empty(&shard->heap); |
| 504 |
} |
| 505 |
|
| 506 |
/* This pops the next non-cancelled timer with deadline <= now from the |
| 507 |
queue, or returns NULL if there isn't one. |
| 508 |
REQUIRES: shard->mu locked */ |
| 509 |
static grpc_timer* pop_one(timer_shard* shard, grpc_core::Timestamp now) { |
| 510 |
grpc_timer* timer; |
| 511 |
for (;;) { |
| 512 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 513 |
gpr_log(GPR_INFO, " .. shard[%d]: heap_empty=%s", |
| 514 |
static_cast<int>(shard - g_shards), |
| 515 |
grpc_timer_heap_is_empty(&shard->heap) ? "true" : "false"); |
| 516 |
} |
| 517 |
if (grpc_timer_heap_is_empty(&shard->heap)) { |
| 518 |
if (now < shard->queue_deadline_cap) return nullptr; |
| 519 |
if (!refill_heap(shard, now)) return nullptr; |
| 520 |
} |
| 521 |
timer = grpc_timer_heap_top(&shard->heap); |
| 522 |
auto timer_deadline = |
| 523 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 524 |
timer->deadline); |
| 525 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 526 |
gpr_log(GPR_INFO, |
| 527 |
" .. check top timer deadline=%" PRId64 " now=%" PRId64, |
| 528 |
timer_deadline.milliseconds_after_process_epoch(), |
| 529 |
now.milliseconds_after_process_epoch()); |
| 530 |
} |
| 531 |
if (timer_deadline > now) return nullptr; |
| 532 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_trace)) { |
| 533 |
gpr_log(GPR_INFO, "TIMER %p: FIRE %" PRId64 "ms late", timer, |
| 534 |
(now - timer_deadline).millis()); |
| 535 |
} |
| 536 |
timer->pending = false; |
| 537 |
grpc_timer_heap_pop(&shard->heap); |
| 538 |
return timer; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/* REQUIRES: shard->mu unlocked */ |
| 543 |
static size_t pop_timers(timer_shard* shard, grpc_core::Timestamp now, |
| 544 |
grpc_core::Timestamp* new_min_deadline, |
| 545 |
grpc_error_handle error) { |
| 546 |
size_t n = 0; |
| 547 |
grpc_timer* timer; |
| 548 |
gpr_mu_lock(&shard->mu); |
| 549 |
while ((timer = pop_one(shard, now))) { |
| 550 |
REMOVE_FROM_HASH_TABLE(timer); |
| 551 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, timer->closure, error); |
| 552 |
n++; |
| 553 |
} |
| 554 |
*new_min_deadline = compute_min_deadline(shard); |
| 555 |
gpr_mu_unlock(&shard->mu); |
| 556 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 557 |
gpr_log(GPR_INFO, " .. shard[%d] popped %" PRIdPTR, |
| 558 |
static_cast<int>(shard - g_shards), n); |
| 559 |
} |
| 560 |
return n; |
| 561 |
} |
| 562 |
|
| 563 |
static grpc_timer_check_result run_some_expired_timers( |
| 564 |
grpc_core::Timestamp now, grpc_core::Timestamp* next, |
| 565 |
grpc_error_handle error) { |
| 566 |
grpc_timer_check_result result = GRPC_TIMERS_NOT_CHECKED; |
| 567 |
|
| 568 |
#if GPR_ARCH_64 |
| 569 |
// TODO(sreek): Using c-style cast here. static_cast<> gives an error (on |
| 570 |
// mac platforms complaining that gpr_atm* is (long *) while |
| 571 |
// (&g_shared_mutables.min_timer) is a (long long *). The cast should be |
| 572 |
// safe since we know that both are pointer types and 64-bit wide |
| 573 |
grpc_core::Timestamp min_timer = |
| 574 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 575 |
gpr_atm_no_barrier_load((gpr_atm*)(&g_shared_mutables.min_timer))); |
| 576 |
#else |
| 577 |
// On 32-bit systems, gpr_atm_no_barrier_load does not work on 64-bit types |
| 578 |
// (like grpc_core::Timestamp). So all reads and writes to |
| 579 |
// g_shared_mutables.min_timer are done under g_shared_mutables.mu |
| 580 |
gpr_mu_lock(&g_shared_mutables.mu); |
| 581 |
grpc_core::Timestamp min_timer = g_shared_mutables.min_timer; |
| 582 |
gpr_mu_unlock(&g_shared_mutables.mu); |
| 583 |
#endif |
| 584 |
g_last_seen_min_timer = min_timer.milliseconds_after_process_epoch(); |
| 585 |
|
| 586 |
if (now < min_timer) { |
| 587 |
if (next != nullptr) *next = std::min(*next, min_timer); |
| 588 |
return GRPC_TIMERS_CHECKED_AND_EMPTY; |
| 589 |
} |
| 590 |
|
| 591 |
if (gpr_spinlock_trylock(&g_shared_mutables.checker_mu)) { |
| 592 |
gpr_mu_lock(&g_shared_mutables.mu); |
| 593 |
result = GRPC_TIMERS_CHECKED_AND_EMPTY; |
| 594 |
|
| 595 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 596 |
gpr_log( |
| 597 |
GPR_INFO, " .. shard[%d]->min_deadline = %" PRId64, |
| 598 |
static_cast<int>(g_shard_queue[0] - g_shards), |
| 599 |
g_shard_queue[0]->min_deadline.milliseconds_after_process_epoch()); |
| 600 |
} |
| 601 |
|
| 602 |
while (g_shard_queue[0]->min_deadline < now || |
| 603 |
(now != grpc_core::Timestamp::InfFuture() && |
| 604 |
g_shard_queue[0]->min_deadline == now)) { |
| 605 |
grpc_core::Timestamp new_min_deadline; |
| 606 |
|
| 607 |
/* For efficiency, we pop as many available timers as we can from the |
| 608 |
shard. This may violate perfect timer deadline ordering, but that |
| 609 |
shouldn't be a big deal because we don't make ordering guarantees. */ |
| 610 |
if (pop_timers(g_shard_queue[0], now, &new_min_deadline, error) > 0) { |
| 611 |
result = GRPC_TIMERS_FIRED; |
| 612 |
} |
| 613 |
|
| 614 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 615 |
gpr_log( |
| 616 |
GPR_INFO, |
| 617 |
" .. result --> %d" |
| 618 |
", shard[%d]->min_deadline %" PRId64 " --> %" PRId64 |
| 619 |
", now=%" PRId64, |
| 620 |
result, static_cast<int>(g_shard_queue[0] - g_shards), |
| 621 |
g_shard_queue[0]->min_deadline.milliseconds_after_process_epoch(), |
| 622 |
new_min_deadline.milliseconds_after_process_epoch(), |
| 623 |
now.milliseconds_after_process_epoch()); |
| 624 |
} |
| 625 |
|
| 626 |
/* An grpc_timer_init() on the shard could intervene here, adding a new |
| 627 |
timer that is earlier than new_min_deadline. However, |
| 628 |
grpc_timer_init() will block on the mutex before it can call |
| 629 |
set_min_deadline, so this one will complete first and then the Addtimer |
| 630 |
will reduce the min_deadline (perhaps unnecessarily). */ |
| 631 |
g_shard_queue[0]->min_deadline = new_min_deadline; |
| 632 |
note_deadline_change(g_shard_queue[0]); |
| 633 |
} |
| 634 |
|
| 635 |
if (next) { |
| 636 |
*next = std::min(*next, g_shard_queue[0]->min_deadline); |
| 637 |
} |
| 638 |
|
| 639 |
#if GPR_ARCH_64 |
| 640 |
// TODO(sreek): Using c-style cast here. static_cast<> gives an error (on |
| 641 |
// mac platforms complaining that gpr_atm* is (long *) while |
| 642 |
// (&g_shared_mutables.min_timer) is a (long long *). The cast should be |
| 643 |
// safe since we know that both are pointer types and 64-bit wide |
| 644 |
gpr_atm_no_barrier_store( |
| 645 |
(gpr_atm*)(&g_shared_mutables.min_timer), |
| 646 |
g_shard_queue[0]->min_deadline.milliseconds_after_process_epoch()); |
| 647 |
#else |
| 648 |
// On 32-bit systems, gpr_atm_no_barrier_store does not work on 64-bit |
| 649 |
// types (like grpc_core::Timestamp). So all reads and writes to |
| 650 |
// g_shared_mutables.min_timer are done under g_shared_mutables.mu |
| 651 |
g_shared_mutables.min_timer = g_shard_queue[0]->min_deadline; |
| 652 |
#endif |
| 653 |
gpr_mu_unlock(&g_shared_mutables.mu); |
| 654 |
gpr_spinlock_unlock(&g_shared_mutables.checker_mu); |
| 655 |
} |
| 656 |
|
| 657 |
return result; |
| 658 |
} |
| 659 |
|
| 660 |
static grpc_timer_check_result timer_check(grpc_core::Timestamp* next) { |
| 661 |
// prelude |
| 662 |
grpc_core::Timestamp now = grpc_core::Timestamp::Now(); |
| 663 |
|
| 664 |
/* fetch from a thread-local first: this avoids contention on a globally |
| 665 |
mutable cacheline in the common case */ |
| 666 |
grpc_core::Timestamp min_timer = |
| 667 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 668 |
g_last_seen_min_timer); |
| 669 |
|
| 670 |
if (now < min_timer) { |
| 671 |
if (next != nullptr) { |
| 672 |
*next = std::min(*next, min_timer); |
| 673 |
} |
| 674 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 675 |
gpr_log(GPR_INFO, "TIMER CHECK SKIP: now=%" PRId64 " min_timer=%" PRId64, |
| 676 |
now.milliseconds_after_process_epoch(), |
| 677 |
min_timer.milliseconds_after_process_epoch()); |
| 678 |
} |
| 679 |
return GRPC_TIMERS_CHECKED_AND_EMPTY; |
| 680 |
} |
| 681 |
|
| 682 |
grpc_error_handle shutdown_error = |
| 683 |
now != grpc_core::Timestamp::InfFuture() |
| 684 |
? absl::OkStatus() |
| 685 |
: GRPC_ERROR_CREATE("Shutting down timer system"); |
| 686 |
|
| 687 |
// tracing |
| 688 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 689 |
std::string next_str; |
| 690 |
if (next == nullptr) { |
| 691 |
next_str = "NULL"; |
| 692 |
} else { |
| 693 |
next_str = absl::StrCat(next->milliseconds_after_process_epoch()); |
| 694 |
} |
| 695 |
#if GPR_ARCH_64 |
| 696 |
gpr_log( |
| 697 |
GPR_INFO, |
| 698 |
"TIMER CHECK BEGIN: now=%" PRId64 " next=%s tls_min=%" PRId64 |
| 699 |
" glob_min=%" PRId64, |
| 700 |
now.milliseconds_after_process_epoch(), next_str.c_str(), |
| 701 |
min_timer.milliseconds_after_process_epoch(), |
| 702 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 703 |
gpr_atm_no_barrier_load((gpr_atm*)(&g_shared_mutables.min_timer))) |
| 704 |
.milliseconds_after_process_epoch()); |
| 705 |
#else |
| 706 |
gpr_log(GPR_INFO, "TIMER CHECK BEGIN: now=%" PRId64 " next=%s min=%" PRId64, |
| 707 |
now.milliseconds_after_process_epoch(), next_str.c_str(), |
| 708 |
min_timer.milliseconds_after_process_epoch()); |
| 709 |
#endif |
| 710 |
} |
| 711 |
// actual code |
| 712 |
grpc_timer_check_result r = |
| 713 |
run_some_expired_timers(now, next, shutdown_error); |
| 714 |
// tracing |
| 715 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_timer_check_trace)) { |
| 716 |
std::string next_str; |
| 717 |
if (next == nullptr) { |
| 718 |
next_str = "NULL"; |
| 719 |
} else { |
| 720 |
next_str = absl::StrCat(next->milliseconds_after_process_epoch()); |
| 721 |
} |
| 722 |
gpr_log(GPR_INFO, "TIMER CHECK END: r=%d; next=%s", r, next_str.c_str()); |
| 723 |
} |
| 724 |
return r; |
| 725 |
} |
| 726 |
|
| 727 |
grpc_timer_vtable grpc_generic_timer_vtable = { |
| 728 |
timer_init, timer_cancel, timer_check, |
| 729 |
timer_list_init, timer_list_shutdown, timer_consume_kick}; |