timer_generic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  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. #include "src/core/lib/iomgr/port.h"
  34. #ifdef GRPC_TIMER_USE_GENERIC
  35. #include "src/core/lib/iomgr/timer.h"
  36. #include <grpc/support/alloc.h>
  37. #include <grpc/support/log.h>
  38. #include <grpc/support/string_util.h>
  39. #include <grpc/support/sync.h>
  40. #include <grpc/support/tls.h>
  41. #include <grpc/support/useful.h>
  42. #include "src/core/lib/debug/trace.h"
  43. #include "src/core/lib/iomgr/time_averaged_stats.h"
  44. #include "src/core/lib/iomgr/timer_heap.h"
  45. #include "src/core/lib/support/spinlock.h"
  46. #define INVALID_HEAP_INDEX 0xffffffffu
  47. #define LOG2_NUM_SHARDS 5
  48. #define NUM_SHARDS (1 << LOG2_NUM_SHARDS)
  49. #define ADD_DEADLINE_SCALE 0.33
  50. #define MIN_QUEUE_WINDOW_DURATION 0.01
  51. #define MAX_QUEUE_WINDOW_DURATION 1
  52. grpc_tracer_flag grpc_timer_trace = GRPC_TRACER_INITIALIZER(false);
  53. grpc_tracer_flag grpc_timer_check_trace = GRPC_TRACER_INITIALIZER(false);
  54. typedef struct {
  55. gpr_mu mu;
  56. grpc_time_averaged_stats stats;
  57. /* All and only timers with deadlines <= this will be in the heap. */
  58. gpr_atm queue_deadline_cap;
  59. gpr_atm min_deadline;
  60. /* Index in the g_shard_queue */
  61. uint32_t shard_queue_index;
  62. /* This holds all timers with deadlines < queue_deadline_cap. Timers in this
  63. list have the top bit of their deadline set to 0. */
  64. grpc_timer_heap heap;
  65. /* This holds timers whose deadline is >= queue_deadline_cap. */
  66. grpc_timer list;
  67. } shard_type;
  68. struct shared_mutables {
  69. gpr_atm min_timer;
  70. /* Allow only one run_some_expired_timers at once */
  71. gpr_spinlock checker_mu;
  72. bool initialized;
  73. /* Protects g_shard_queue */
  74. gpr_mu mu;
  75. } GPR_ALIGN_STRUCT(GPR_CACHELINE_SIZE);
  76. static struct shared_mutables g_shared_mutables = {
  77. .checker_mu = GPR_SPINLOCK_STATIC_INITIALIZER, .initialized = false,
  78. };
  79. static gpr_clock_type g_clock_type;
  80. static shard_type g_shards[NUM_SHARDS];
  81. /* Protected by g_shared_mutables.mu */
  82. static shard_type *g_shard_queue[NUM_SHARDS];
  83. static gpr_timespec g_start_time;
  84. GPR_TLS_DECL(g_last_seen_min_timer);
  85. static gpr_atm saturating_add(gpr_atm a, gpr_atm b) {
  86. if (a > GPR_ATM_MAX - b) {
  87. return GPR_ATM_MAX;
  88. }
  89. return a + b;
  90. }
  91. static int run_some_expired_timers(grpc_exec_ctx *exec_ctx, gpr_atm now,
  92. gpr_atm *next, grpc_error *error);
  93. static gpr_timespec dbl_to_ts(double d) {
  94. gpr_timespec ts;
  95. ts.tv_sec = (int64_t)d;
  96. ts.tv_nsec = (int32_t)(1e9 * (d - (double)ts.tv_sec));
  97. ts.clock_type = GPR_TIMESPAN;
  98. return ts;
  99. }
  100. static gpr_atm timespec_to_atm_round_up(gpr_timespec ts) {
  101. ts = gpr_time_sub(ts, g_start_time);
  102. double x = GPR_MS_PER_SEC * (double)ts.tv_sec +
  103. (double)ts.tv_nsec / GPR_NS_PER_MS +
  104. (double)(GPR_NS_PER_SEC - 1) / (double)GPR_NS_PER_SEC;
  105. if (x < 0) return 0;
  106. if (x > GPR_ATM_MAX) return GPR_ATM_MAX;
  107. return (gpr_atm)x;
  108. }
  109. static gpr_atm timespec_to_atm_round_down(gpr_timespec ts) {
  110. ts = gpr_time_sub(ts, g_start_time);
  111. double x =
  112. GPR_MS_PER_SEC * (double)ts.tv_sec + (double)ts.tv_nsec / GPR_NS_PER_MS;
  113. if (x < 0) return 0;
  114. if (x > GPR_ATM_MAX) return GPR_ATM_MAX;
  115. return (gpr_atm)x;
  116. }
  117. static gpr_timespec atm_to_timespec(gpr_atm x) {
  118. return gpr_time_add(g_start_time, dbl_to_ts((double)x / 1000.0));
  119. }
  120. static gpr_atm compute_min_deadline(shard_type *shard) {
  121. return grpc_timer_heap_is_empty(&shard->heap)
  122. ? saturating_add(shard->queue_deadline_cap, 1)
  123. : grpc_timer_heap_top(&shard->heap)->deadline;
  124. }
  125. void grpc_timer_list_init(gpr_timespec now) {
  126. uint32_t i;
  127. g_shared_mutables.initialized = true;
  128. gpr_mu_init(&g_shared_mutables.mu);
  129. g_clock_type = now.clock_type;
  130. g_start_time = now;
  131. g_shared_mutables.min_timer = timespec_to_atm_round_down(now);
  132. gpr_tls_init(&g_last_seen_min_timer);
  133. gpr_tls_set(&g_last_seen_min_timer, 0);
  134. grpc_register_tracer("timer", &grpc_timer_trace);
  135. grpc_register_tracer("timer_check", &grpc_timer_check_trace);
  136. for (i = 0; i < NUM_SHARDS; i++) {
  137. shard_type *shard = &g_shards[i];
  138. gpr_mu_init(&shard->mu);
  139. grpc_time_averaged_stats_init(&shard->stats, 1.0 / ADD_DEADLINE_SCALE, 0.1,
  140. 0.5);
  141. shard->queue_deadline_cap = g_shared_mutables.min_timer;
  142. shard->shard_queue_index = i;
  143. grpc_timer_heap_init(&shard->heap);
  144. shard->list.next = shard->list.prev = &shard->list;
  145. shard->min_deadline = compute_min_deadline(shard);
  146. g_shard_queue[i] = shard;
  147. }
  148. }
  149. void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx) {
  150. int i;
  151. run_some_expired_timers(
  152. exec_ctx, GPR_ATM_MAX, NULL,
  153. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Timer list shutdown"));
  154. for (i = 0; i < NUM_SHARDS; i++) {
  155. shard_type *shard = &g_shards[i];
  156. gpr_mu_destroy(&shard->mu);
  157. grpc_timer_heap_destroy(&shard->heap);
  158. }
  159. gpr_mu_destroy(&g_shared_mutables.mu);
  160. gpr_tls_destroy(&g_last_seen_min_timer);
  161. g_shared_mutables.initialized = false;
  162. }
  163. static double ts_to_dbl(gpr_timespec ts) {
  164. return (double)ts.tv_sec + 1e-9 * ts.tv_nsec;
  165. }
  166. /* returns true if the first element in the list */
  167. static void list_join(grpc_timer *head, grpc_timer *timer) {
  168. timer->next = head;
  169. timer->prev = head->prev;
  170. timer->next->prev = timer->prev->next = timer;
  171. }
  172. static void list_remove(grpc_timer *timer) {
  173. timer->next->prev = timer->prev;
  174. timer->prev->next = timer->next;
  175. }
  176. static void swap_adjacent_shards_in_queue(uint32_t first_shard_queue_index) {
  177. shard_type *temp;
  178. temp = g_shard_queue[first_shard_queue_index];
  179. g_shard_queue[first_shard_queue_index] =
  180. g_shard_queue[first_shard_queue_index + 1];
  181. g_shard_queue[first_shard_queue_index + 1] = temp;
  182. g_shard_queue[first_shard_queue_index]->shard_queue_index =
  183. first_shard_queue_index;
  184. g_shard_queue[first_shard_queue_index + 1]->shard_queue_index =
  185. first_shard_queue_index + 1;
  186. }
  187. static void note_deadline_change(shard_type *shard) {
  188. while (shard->shard_queue_index > 0 &&
  189. shard->min_deadline <
  190. g_shard_queue[shard->shard_queue_index - 1]->min_deadline) {
  191. swap_adjacent_shards_in_queue(shard->shard_queue_index - 1);
  192. }
  193. while (shard->shard_queue_index < NUM_SHARDS - 1 &&
  194. shard->min_deadline >
  195. g_shard_queue[shard->shard_queue_index + 1]->min_deadline) {
  196. swap_adjacent_shards_in_queue(shard->shard_queue_index);
  197. }
  198. }
  199. void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer,
  200. gpr_timespec deadline, grpc_closure *closure,
  201. gpr_timespec now) {
  202. int is_first_timer = 0;
  203. shard_type *shard = &g_shards[GPR_HASH_POINTER(timer, NUM_SHARDS)];
  204. GPR_ASSERT(deadline.clock_type == g_clock_type);
  205. GPR_ASSERT(now.clock_type == g_clock_type);
  206. timer->closure = closure;
  207. gpr_atm deadline_atm = timer->deadline = timespec_to_atm_round_up(deadline);
  208. if (GRPC_TRACER_ON(grpc_timer_trace)) {
  209. gpr_log(GPR_DEBUG, "TIMER %p: SET %" PRId64 ".%09d [%" PRIdPTR
  210. "] now %" PRId64 ".%09d [%" PRIdPTR "] call %p[%p]",
  211. timer, deadline.tv_sec, deadline.tv_nsec, deadline_atm, now.tv_sec,
  212. now.tv_nsec, timespec_to_atm_round_down(now), closure, closure->cb);
  213. }
  214. if (!g_shared_mutables.initialized) {
  215. timer->pending = false;
  216. grpc_closure_sched(exec_ctx, timer->closure,
  217. GRPC_ERROR_CREATE_FROM_STATIC_STRING(
  218. "Attempt to create timer before initialization"));
  219. return;
  220. }
  221. gpr_mu_lock(&shard->mu);
  222. timer->pending = true;
  223. if (gpr_time_cmp(deadline, now) <= 0) {
  224. timer->pending = false;
  225. grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_NONE);
  226. gpr_mu_unlock(&shard->mu);
  227. /* early out */
  228. return;
  229. }
  230. grpc_time_averaged_stats_add_sample(&shard->stats,
  231. ts_to_dbl(gpr_time_sub(deadline, now)));
  232. if (deadline_atm < shard->queue_deadline_cap) {
  233. is_first_timer = grpc_timer_heap_add(&shard->heap, timer);
  234. } else {
  235. timer->heap_index = INVALID_HEAP_INDEX;
  236. list_join(&shard->list, timer);
  237. }
  238. if (GRPC_TRACER_ON(grpc_timer_trace)) {
  239. gpr_log(GPR_DEBUG, " .. add to shard %d with queue_deadline_cap=%" PRIdPTR
  240. " => is_first_timer=%s",
  241. (int)(shard - g_shards), shard->queue_deadline_cap,
  242. is_first_timer ? "true" : "false");
  243. }
  244. gpr_mu_unlock(&shard->mu);
  245. /* Deadline may have decreased, we need to adjust the master queue. Note
  246. that there is a potential racy unlocked region here. There could be a
  247. reordering of multiple grpc_timer_init calls, at this point, but the < test
  248. below should ensure that we err on the side of caution. There could
  249. also be a race with grpc_timer_check, which might beat us to the lock. In
  250. that case, it is possible that the timer that we added will have already
  251. run by the time we hold the lock, but that too is a safe error.
  252. Finally, it's possible that the grpc_timer_check that intervened failed to
  253. trigger the new timer because the min_deadline hadn't yet been reduced.
  254. In that case, the timer will simply have to wait for the next
  255. grpc_timer_check. */
  256. if (is_first_timer) {
  257. gpr_mu_lock(&g_shared_mutables.mu);
  258. if (GRPC_TRACER_ON(grpc_timer_trace)) {
  259. gpr_log(GPR_DEBUG, " .. old shard min_deadline=%" PRIdPTR,
  260. shard->min_deadline);
  261. }
  262. if (deadline_atm < shard->min_deadline) {
  263. gpr_atm old_min_deadline = g_shard_queue[0]->min_deadline;
  264. shard->min_deadline = deadline_atm;
  265. note_deadline_change(shard);
  266. if (shard->shard_queue_index == 0 && deadline_atm < old_min_deadline) {
  267. gpr_atm_no_barrier_store(&g_shared_mutables.min_timer, deadline_atm);
  268. grpc_kick_poller();
  269. }
  270. }
  271. gpr_mu_unlock(&g_shared_mutables.mu);
  272. }
  273. }
  274. void grpc_timer_consume_kick(void) {
  275. /* force re-evaluation of last seeen min */
  276. gpr_tls_set(&g_last_seen_min_timer, 0);
  277. }
  278. void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer) {
  279. if (!g_shared_mutables.initialized) {
  280. /* must have already been cancelled, also the shard mutex is invalid */
  281. return;
  282. }
  283. shard_type *shard = &g_shards[GPR_HASH_POINTER(timer, NUM_SHARDS)];
  284. gpr_mu_lock(&shard->mu);
  285. if (GRPC_TRACER_ON(grpc_timer_trace)) {
  286. gpr_log(GPR_DEBUG, "TIMER %p: CANCEL pending=%s", timer,
  287. timer->pending ? "true" : "false");
  288. }
  289. if (timer->pending) {
  290. grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_CANCELLED);
  291. timer->pending = false;
  292. if (timer->heap_index == INVALID_HEAP_INDEX) {
  293. list_remove(timer);
  294. } else {
  295. grpc_timer_heap_remove(&shard->heap, timer);
  296. }
  297. }
  298. gpr_mu_unlock(&shard->mu);
  299. }
  300. /* This is called when the queue is empty and "now" has reached the
  301. queue_deadline_cap. We compute a new queue deadline and then scan the map
  302. for timers that fall at or under it. Returns true if the queue is no
  303. longer empty.
  304. REQUIRES: shard->mu locked */
  305. static int refill_queue(shard_type *shard, gpr_atm now) {
  306. /* Compute the new queue window width and bound by the limits: */
  307. double computed_deadline_delta =
  308. grpc_time_averaged_stats_update_average(&shard->stats) *
  309. ADD_DEADLINE_SCALE;
  310. double deadline_delta =
  311. GPR_CLAMP(computed_deadline_delta, MIN_QUEUE_WINDOW_DURATION,
  312. MAX_QUEUE_WINDOW_DURATION);
  313. grpc_timer *timer, *next;
  314. /* Compute the new cap and put all timers under it into the queue: */
  315. shard->queue_deadline_cap =
  316. saturating_add(GPR_MAX(now, shard->queue_deadline_cap),
  317. (gpr_atm)(deadline_delta * 1000.0));
  318. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  319. gpr_log(GPR_DEBUG, " .. shard[%d]->queue_deadline_cap --> %" PRIdPTR,
  320. (int)(shard - g_shards), shard->queue_deadline_cap);
  321. }
  322. for (timer = shard->list.next; timer != &shard->list; timer = next) {
  323. next = timer->next;
  324. if (timer->deadline < shard->queue_deadline_cap) {
  325. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  326. gpr_log(GPR_DEBUG, " .. add timer with deadline %" PRIdPTR " to heap",
  327. timer->deadline);
  328. }
  329. list_remove(timer);
  330. grpc_timer_heap_add(&shard->heap, timer);
  331. }
  332. }
  333. return !grpc_timer_heap_is_empty(&shard->heap);
  334. }
  335. /* This pops the next non-cancelled timer with deadline <= now from the
  336. queue, or returns NULL if there isn't one.
  337. REQUIRES: shard->mu locked */
  338. static grpc_timer *pop_one(shard_type *shard, gpr_atm now) {
  339. grpc_timer *timer;
  340. for (;;) {
  341. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  342. gpr_log(GPR_DEBUG, " .. shard[%d]: heap_empty=%s",
  343. (int)(shard - g_shards),
  344. grpc_timer_heap_is_empty(&shard->heap) ? "true" : "false");
  345. }
  346. if (grpc_timer_heap_is_empty(&shard->heap)) {
  347. if (now < shard->queue_deadline_cap) return NULL;
  348. if (!refill_queue(shard, now)) return NULL;
  349. }
  350. timer = grpc_timer_heap_top(&shard->heap);
  351. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  352. gpr_log(GPR_DEBUG,
  353. " .. check top timer deadline=%" PRIdPTR " now=%" PRIdPTR,
  354. timer->deadline, now);
  355. }
  356. if (timer->deadline > now) return NULL;
  357. if (GRPC_TRACER_ON(grpc_timer_trace)) {
  358. gpr_log(GPR_DEBUG, "TIMER %p: FIRE %" PRIdPTR "ms late", timer,
  359. now - timer->deadline);
  360. }
  361. timer->pending = false;
  362. grpc_timer_heap_pop(&shard->heap);
  363. return timer;
  364. }
  365. }
  366. /* REQUIRES: shard->mu unlocked */
  367. static size_t pop_timers(grpc_exec_ctx *exec_ctx, shard_type *shard,
  368. gpr_atm now, gpr_atm *new_min_deadline,
  369. grpc_error *error) {
  370. size_t n = 0;
  371. grpc_timer *timer;
  372. gpr_mu_lock(&shard->mu);
  373. while ((timer = pop_one(shard, now))) {
  374. grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_REF(error));
  375. n++;
  376. }
  377. *new_min_deadline = compute_min_deadline(shard);
  378. gpr_mu_unlock(&shard->mu);
  379. return n;
  380. }
  381. static int run_some_expired_timers(grpc_exec_ctx *exec_ctx, gpr_atm now,
  382. gpr_atm *next, grpc_error *error) {
  383. size_t n = 0;
  384. gpr_atm min_timer = gpr_atm_no_barrier_load(&g_shared_mutables.min_timer);
  385. gpr_tls_set(&g_last_seen_min_timer, min_timer);
  386. if (now < min_timer) {
  387. if (next != NULL) *next = GPR_MIN(*next, min_timer);
  388. return 0;
  389. }
  390. if (gpr_spinlock_trylock(&g_shared_mutables.checker_mu)) {
  391. gpr_mu_lock(&g_shared_mutables.mu);
  392. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  393. gpr_log(GPR_DEBUG, " .. shard[%d]->min_deadline = %" PRIdPTR,
  394. (int)(g_shard_queue[0] - g_shards),
  395. g_shard_queue[0]->min_deadline);
  396. }
  397. while (g_shard_queue[0]->min_deadline < now ||
  398. (now != GPR_ATM_MAX && g_shard_queue[0]->min_deadline == now)) {
  399. gpr_atm new_min_deadline;
  400. /* For efficiency, we pop as many available timers as we can from the
  401. shard. This may violate perfect timer deadline ordering, but that
  402. shouldn't be a big deal because we don't make ordering guarantees. */
  403. n +=
  404. pop_timers(exec_ctx, g_shard_queue[0], now, &new_min_deadline, error);
  405. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  406. gpr_log(GPR_DEBUG, " .. popped --> %" PRIdPTR
  407. ", shard[%d]->min_deadline %" PRIdPTR
  408. " --> %" PRIdPTR ", now=%" PRIdPTR,
  409. n, (int)(g_shard_queue[0] - g_shards),
  410. g_shard_queue[0]->min_deadline, new_min_deadline, now);
  411. }
  412. /* An grpc_timer_init() on the shard could intervene here, adding a new
  413. timer that is earlier than new_min_deadline. However,
  414. grpc_timer_init() will block on the master_lock before it can call
  415. set_min_deadline, so this one will complete first and then the Addtimer
  416. will reduce the min_deadline (perhaps unnecessarily). */
  417. g_shard_queue[0]->min_deadline = new_min_deadline;
  418. note_deadline_change(g_shard_queue[0]);
  419. }
  420. if (next) {
  421. *next = GPR_MIN(*next, g_shard_queue[0]->min_deadline);
  422. }
  423. gpr_atm_no_barrier_store(&g_shared_mutables.min_timer,
  424. g_shard_queue[0]->min_deadline);
  425. gpr_mu_unlock(&g_shared_mutables.mu);
  426. gpr_spinlock_unlock(&g_shared_mutables.checker_mu);
  427. } else if (next != NULL) {
  428. /* TODO(ctiller): this forces calling code to do an short poll, and
  429. then retry the timer check (because this time through the timer list was
  430. contended).
  431. We could reduce the cost here dramatically by keeping a count of how
  432. many currently active pollers got through the uncontended case above
  433. successfully, and waking up other pollers IFF that count drops to zero.
  434. Once that count is in place, this entire else branch could disappear. */
  435. *next = GPR_MIN(*next, now + 1);
  436. }
  437. GRPC_ERROR_UNREF(error);
  438. return (int)n;
  439. }
  440. bool grpc_timer_check(grpc_exec_ctx *exec_ctx, gpr_timespec now,
  441. gpr_timespec *next) {
  442. // prelude
  443. GPR_ASSERT(now.clock_type == g_clock_type);
  444. gpr_atm now_atm = timespec_to_atm_round_down(now);
  445. /* fetch from a thread-local first: this avoids contention on a globally
  446. mutable cacheline in the common case */
  447. gpr_atm min_timer = gpr_tls_get(&g_last_seen_min_timer);
  448. if (now_atm < min_timer) {
  449. if (next != NULL) {
  450. *next =
  451. atm_to_timespec(GPR_MIN(timespec_to_atm_round_up(*next), min_timer));
  452. }
  453. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  454. gpr_log(GPR_DEBUG,
  455. "TIMER CHECK SKIP: now_atm=%" PRIdPTR " min_timer=%" PRIdPTR,
  456. now_atm, min_timer);
  457. }
  458. return 0;
  459. }
  460. grpc_error *shutdown_error =
  461. gpr_time_cmp(now, gpr_inf_future(now.clock_type)) != 0
  462. ? GRPC_ERROR_NONE
  463. : GRPC_ERROR_CREATE_FROM_STATIC_STRING("Shutting down timer system");
  464. // tracing
  465. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  466. char *next_str;
  467. if (next == NULL) {
  468. next_str = gpr_strdup("NULL");
  469. } else {
  470. gpr_asprintf(&next_str, "%" PRId64 ".%09d [%" PRIdPTR "]", next->tv_sec,
  471. next->tv_nsec, timespec_to_atm_round_down(*next));
  472. }
  473. gpr_log(GPR_DEBUG, "TIMER CHECK BEGIN: now=%" PRId64 ".%09d [%" PRIdPTR
  474. "] next=%s tls_min=%" PRIdPTR " glob_min=%" PRIdPTR,
  475. now.tv_sec, now.tv_nsec, now_atm, next_str,
  476. gpr_tls_get(&g_last_seen_min_timer),
  477. gpr_atm_no_barrier_load(&g_shared_mutables.min_timer));
  478. gpr_free(next_str);
  479. }
  480. // actual code
  481. bool r;
  482. gpr_atm next_atm;
  483. if (next == NULL) {
  484. r = run_some_expired_timers(exec_ctx, now_atm, NULL, shutdown_error);
  485. } else {
  486. next_atm = timespec_to_atm_round_down(*next);
  487. r = run_some_expired_timers(exec_ctx, now_atm, &next_atm, shutdown_error);
  488. *next = atm_to_timespec(next_atm);
  489. }
  490. // tracing
  491. if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
  492. char *next_str;
  493. if (next == NULL) {
  494. next_str = gpr_strdup("NULL");
  495. } else {
  496. gpr_asprintf(&next_str, "%" PRId64 ".%09d [%" PRIdPTR "]", next->tv_sec,
  497. next->tv_nsec, next_atm);
  498. }
  499. gpr_log(GPR_DEBUG, "TIMER CHECK END: %d timers triggered; next=%s", r,
  500. next_str);
  501. gpr_free(next_str);
  502. }
  503. return r > 0;
  504. }
  505. #endif /* GRPC_TIMER_USE_GENERIC */