| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "epoll-internal.h" |
| 23 |
#include "core/util.h" |
| 24 |
#include <assert.h> |
| 25 |
#include <string.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <sys/timerfd.h> |
| 29 |
#include <sys/epoll.h> |
| 30 |
|
| 31 |
/* |
| 32 |
* Epoll proactor subsystem for timers. |
| 33 |
* |
| 34 |
* Two types of timers: (1) connection timers, one per connection, active if at least one of the peers has set a heartbeat, |
| 35 |
* timers move forward in time (but see replace_timer_deadline() note below), latency not critical; (2) a single proactor |
| 36 |
* timer, can move forwards or backwards, can be canceled. |
| 37 |
* |
| 38 |
* A single timerfd is shared by all the timers. Connection timers are tracked on a heap ordered list. The proactor timer is |
| 39 |
* tracked separately. The next timerfd_deadline is the earliest of all the timers, in this case the earliest of the first |
| 40 |
* connection timer and the poactor timer. |
| 41 |
* |
| 42 |
* If a connection timer is changed to a later time, it is not moved. It is kept in place but marked with the new deadline. On |
| 43 |
* expiry, depending on whether the deadline was extended, the decision is made to either generate a timeout or replace the |
| 44 |
* timer on the ordered list. |
| 45 |
* |
| 46 |
* When a timerfd read event is generated, the proactor invokes pni_timer_manager_process() to generate timeouts for each |
| 47 |
* expired timer and to do housekeeping on the rest. |
| 48 |
* |
| 49 |
* replace_timer_deadline(): a connection timer can go backwards in time at most once if: both peers have heartbeats and the |
| 50 |
* second AMQP open frame results in a shorter periodic transport timer than the first open frame. In this case, the |
| 51 |
* existing timer_deadline is immediately orphaned and a new one created for the rest of the connection's life. |
| 52 |
* |
| 53 |
* Lock ordering: tm->task_mutex --> tm->deletion_mutex. |
| 54 |
*/ |
| 55 |
|
| 56 |
static void timerfd_set(int fd, uint64_t t_millis) { |
| 57 |
// t_millis == 0 -> cancel |
| 58 |
struct itimerspec newt; |
| 59 |
memset(&newt, 0, sizeof(newt)); |
| 60 |
newt.it_value.tv_sec = t_millis / 1000; |
| 61 |
newt.it_value.tv_nsec = (t_millis % 1000) * 1000000; |
| 62 |
timerfd_settime(fd, 0, &newt, NULL); |
| 63 |
} |
| 64 |
|
| 65 |
static void timerfd_drain(int fd) { |
| 66 |
// Forget any old expired timers and only trigger an epoll read event for a subsequent expiry. |
| 67 |
uint64_t result = 0; |
| 68 |
ssize_t n = read(fd, &result, sizeof(result)); |
| 69 |
if (n != sizeof(result) && !(n < 0 && errno == EAGAIN)) { |
| 70 |
EPOLL_FATAL("timerfd read error", errno); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Struct to manage the ordering of timers on the heap ordered list and manage the lifecycle if |
| 75 |
// the parent timer is self-deleting. |
| 76 |
// This needs a Proton class definition to provide a custom compare() function. It otherwise |
| 77 |
// remains private to this file and opts out of the general object machinery. |
| 78 |
// This works because the pn_list class is already meant to work with non-Proton objects using a |
| 79 |
// different compare operator (pn_void_compare). |
| 80 |
typedef struct timer_deadline_t { |
| 81 |
uint64_t list_deadline; // Heap ordering deadline. Must not change while on list. |
| 82 |
pni_timer_t *timer; // Parent timer. NULL means orphaned and to be deleted. |
| 83 |
bool resequenced; // An out-of-order connection timeout caught and handled. |
| 84 |
} timer_deadline_t; |
| 85 |
|
| 86 |
static const pn_class_t *timer_deadline_reify(void *p); |
| 87 |
|
| 88 |
// The pn_list_t calls this to maintain its sorted heap. |
| 89 |
static intptr_t timer_deadline_compare(void *oa, void *ob) { |
| 90 |
timer_deadline_t *a = (timer_deadline_t *) oa; |
| 91 |
timer_deadline_t *b = (timer_deadline_t *) ob; |
| 92 |
return a->list_deadline - b->list_deadline; |
| 93 |
} |
| 94 |
|
| 95 |
#define CID_timer_deadline CID_pn_void |
| 96 |
#define timer_deadline_new NULL |
| 97 |
#define timer_deadline_initialize NULL |
| 98 |
#define timer_deadline_incref pn_void_incref |
| 99 |
#define timer_deadline_decref pn_void_decref |
| 100 |
#define timer_deadline_refcount pn_void_refcount |
| 101 |
#define timer_deadline_finalize NULL |
| 102 |
#define timer_deadline_free NULL |
| 103 |
#define timer_deadline_hashcode NULL |
| 104 |
#define timer_deadline_inspect NULL |
| 105 |
|
| 106 |
static const pn_class_t timer_deadline_clazz = PN_METACLASS(timer_deadline); |
| 107 |
static const pn_class_t *timer_deadline_reify(void *p) { return &timer_deadline_clazz; } |
| 108 |
|
| 109 |
static timer_deadline_t* timer_deadline_t_new(void) { |
| 110 |
// Just the struct. Not a Proton class based object. |
| 111 |
return (timer_deadline_t *) calloc(1, sizeof(timer_deadline_t)); |
| 112 |
} |
| 113 |
|
| 114 |
static void timer_deadline_t_free(timer_deadline_t* td) { |
| 115 |
assert(td->list_deadline == 0); |
| 116 |
free(td); |
| 117 |
} |
| 118 |
|
| 119 |
struct pni_timer_t { |
| 120 |
uint64_t deadline; |
| 121 |
timer_deadline_t *timer_deadline; |
| 122 |
pni_timer_manager_t *manager; |
| 123 |
pconnection_t *connection; |
| 124 |
}; |
| 125 |
|
| 126 |
pni_timer_t *pni_timer(pni_timer_manager_t *tm, pconnection_t *c) { |
| 127 |
timer_deadline_t *td = NULL; |
| 128 |
pni_timer_t *timer = NULL; |
| 129 |
assert(c || !tm->task.proactor->timer); // Proactor timer. Can only be one. |
| 130 |
timer = (pni_timer_t *) malloc(sizeof(pni_timer_t)); |
| 131 |
if (!timer) return NULL; |
| 132 |
if (c) { |
| 133 |
// Connections are tracked on the timer_heap. Allocate the tracking struct. |
| 134 |
td = timer_deadline_t_new(); |
| 135 |
if (!td) { |
| 136 |
free(timer); |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
lock(&tm->task.mutex); |
| 142 |
timer->connection = c; |
| 143 |
timer->manager = tm; |
| 144 |
timer->timer_deadline = td; |
| 145 |
timer->deadline = 0; |
| 146 |
if (c) |
| 147 |
td->timer = timer; |
| 148 |
unlock(&tm->task.mutex); |
| 149 |
return timer; |
| 150 |
} |
| 151 |
|
| 152 |
// Call with no locks. |
| 153 |
void pni_timer_free(pni_timer_t *timer) { |
| 154 |
timer_deadline_t *td = timer->timer_deadline; |
| 155 |
bool can_free_td = false; |
| 156 |
bool notify = false; |
| 157 |
if (td) |
| 158 |
notify = pni_timer_set(timer, 0); |
| 159 |
pni_timer_manager_t *tm = timer->manager; |
| 160 |
lock(&tm->task.mutex); |
| 161 |
lock(&tm->deletion_mutex); |
| 162 |
if (td) { |
| 163 |
if (td->list_deadline) |
| 164 |
td->timer = NULL; // Orphan. timer_manager does eventual timer_deadline_t_free() in process(). |
| 165 |
else |
| 166 |
can_free_td = true; |
| 167 |
} |
| 168 |
unlock(&tm->deletion_mutex); |
| 169 |
unlock(&tm->task.mutex); |
| 170 |
if (notify) |
| 171 |
notify_poller(tm->task.proactor); |
| 172 |
if (can_free_td) { |
| 173 |
timer_deadline_t_free(td); |
| 174 |
} |
| 175 |
free(timer); |
| 176 |
} |
| 177 |
|
| 178 |
static timer_deadline_t *replace_timer_deadline(pni_timer_manager_t *tm, pni_timer_t *timer); |
| 179 |
|
| 180 |
// Return true if initialization succeeds. Called once at proactor creation. |
| 181 |
bool pni_timer_manager_init(pni_timer_manager_t *tm) { |
| 182 |
tm->epoll_timer.fd = -1; |
| 183 |
tm->timerfd_deadline = 0; |
| 184 |
tm->timers_heap = NULL; |
| 185 |
tm->proactor_timer = NULL; |
| 186 |
pn_proactor_t *p = containerof(tm, pn_proactor_t, timer_manager); |
| 187 |
task_init(&tm->task, TIMER_MANAGER, p); |
| 188 |
pmutex_init(&tm->deletion_mutex); |
| 189 |
|
| 190 |
// Heap sorted pn_list_t using timer_deadline_compare() to determine ordering. |
| 191 |
tm->timers_heap = pn_list(&timer_deadline_clazz, 0); |
| 192 |
if (!tm->timers_heap) |
| 193 |
return false; |
| 194 |
tm->proactor_timer = pni_timer(tm, NULL); |
| 195 |
if (!tm->proactor_timer) |
| 196 |
return false; |
| 197 |
|
| 198 |
p->timer = tm->proactor_timer; |
| 199 |
epoll_extended_t *ee = &tm->epoll_timer; |
| 200 |
ee->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); |
| 201 |
ee->type = TIMER; |
| 202 |
ee->wanted = EPOLLIN; |
| 203 |
ee->polling = false; |
| 204 |
return (ee->fd >= 0); |
| 205 |
} |
| 206 |
|
| 207 |
// Only call from proactor's destructor, when it is single threaded and scheduling has stopped. |
| 208 |
void pni_timer_manager_finalize(pni_timer_manager_t *tm) { |
| 209 |
lock(&tm->task.mutex); |
| 210 |
unlock(&tm->task.mutex); // Memory barrier |
| 211 |
if (tm->epoll_timer.fd >= 0) close(tm->epoll_timer.fd); |
| 212 |
pni_timer_free(tm->proactor_timer); |
| 213 |
if (tm->timers_heap) { |
| 214 |
size_t sz = pn_list_size(tm->timers_heap); |
| 215 |
// On teardown there is no need to preserve the heap. Traverse the list ignoring minpop(). |
| 216 |
for (size_t idx = 0; idx < sz; idx++) { |
| 217 |
timer_deadline_t *td = (timer_deadline_t *) pn_list_get(tm->timers_heap, idx); |
| 218 |
td->list_deadline = 0; |
| 219 |
timer_deadline_t_free(td); |
| 220 |
} |
| 221 |
pn_free(tm->timers_heap); |
| 222 |
} |
| 223 |
pmutex_finalize(&tm->deletion_mutex); |
| 224 |
task_finalize(&tm->task); |
| 225 |
} |
| 226 |
|
| 227 |
// Call with timer_manager lock held. Return true if notify_poller required. |
| 228 |
static bool adjust_deadline(pni_timer_manager_t *tm) { |
| 229 |
// Make sure the timer_manager task will get a timeout in time for the earliest connection timeout. |
| 230 |
if (tm->task.working) |
| 231 |
return false; // timer_manager task will adjust the timer when it stops working |
| 232 |
bool notify = false; |
| 233 |
uint64_t new_deadline = tm->proactor_timer->deadline; |
| 234 |
if (pn_list_size(tm->timers_heap)) { |
| 235 |
// First element of timers_heap has earliest deadline on the heap. |
| 236 |
timer_deadline_t *heap0 = (timer_deadline_t *) pn_list_get(tm->timers_heap, 0); |
| 237 |
assert(heap0->list_deadline != 0); |
| 238 |
new_deadline = new_deadline ? pn_min(new_deadline, heap0->list_deadline) : heap0->list_deadline; |
| 239 |
} |
| 240 |
// Only change target deadline if new_deadline is in future but earlier than old timerfd_deadline. |
| 241 |
if (new_deadline) { |
| 242 |
if (tm->timerfd_deadline == 0 || new_deadline < tm->timerfd_deadline) { |
| 243 |
uint64_t now = pn_proactor_now_64(); |
| 244 |
if (new_deadline <= now) { |
| 245 |
// no need for a timer update. Wake the timer_manager. |
| 246 |
notify = schedule(&tm->task); |
| 247 |
} |
| 248 |
else { |
| 249 |
timerfd_set(tm->epoll_timer.fd, new_deadline - now); |
| 250 |
tm->timerfd_deadline = new_deadline; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
return notify; |
| 255 |
} |
| 256 |
|
| 257 |
// Call without task lock or timer_manager lock. |
| 258 |
// Calls for connection timers are generated in the proactor and serialized per connection. |
| 259 |
// Calls for the proactor timer can come from arbitrary user threads. |
| 260 |
// Caller must call notify_poller() if true returned. |
| 261 |
bool pni_timer_set(pni_timer_t *timer, uint64_t deadline) { |
| 262 |
pni_timer_manager_t *tm = timer->manager; |
| 263 |
bool notify = false; |
| 264 |
|
| 265 |
lock(&tm->task.mutex); |
| 266 |
if (deadline == timer->deadline) { |
| 267 |
unlock(&tm->task.mutex); |
| 268 |
return false; // No change. |
| 269 |
} |
| 270 |
|
| 271 |
if (timer == tm->proactor_timer) { |
| 272 |
assert(!timer->connection); |
| 273 |
timer->deadline = deadline; |
| 274 |
} else { |
| 275 |
// Connection |
| 276 |
timer_deadline_t *td = timer->timer_deadline; |
| 277 |
// A connection timer can go backwards at most once. Check here. |
| 278 |
if (deadline && td->list_deadline && deadline < td->list_deadline) { |
| 279 |
if (td->resequenced) |
| 280 |
EPOLL_FATAL("idle timeout sequencing error", 0); // |
| 281 |
else { |
| 282 |
// replace drops the lock for malloc. Safe because there can be no competing call to |
| 283 |
// the timer set function by the same pconnection from another thread. |
| 284 |
td = replace_timer_deadline(tm, timer); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
timer->deadline = deadline; |
| 289 |
// Put on list if not already there. |
| 290 |
if (deadline && !td->list_deadline) { |
| 291 |
td->list_deadline = deadline; |
| 292 |
pn_list_minpush(tm->timers_heap, td); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// Skip a cancelled timer (deadline == 0) since it doesn't change the timerfd deadline. |
| 297 |
if (deadline) |
| 298 |
notify = adjust_deadline(tm); |
| 299 |
unlock(&tm->task.mutex); |
| 300 |
|
| 301 |
return notify; |
| 302 |
} |
| 303 |
|
| 304 |
pn_event_batch_t *pni_timer_manager_process(pni_timer_manager_t *tm, bool timeout, bool sched_ready) { |
| 305 |
uint64_t now = pn_proactor_now_64(); |
| 306 |
lock(&tm->task.mutex); |
| 307 |
tm->task.working = true; |
| 308 |
if (timeout) |
| 309 |
tm->timerfd_deadline = 0; |
| 310 |
if (sched_ready) |
| 311 |
schedule_done(&tm->task); |
| 312 |
|
| 313 |
// First check for proactor timer expiry. |
| 314 |
uint64_t deadline = tm->proactor_timer->deadline; |
| 315 |
if (deadline && deadline <= now) { |
| 316 |
tm->proactor_timer->deadline = 0; |
| 317 |
unlock(&tm->task.mutex); |
| 318 |
pni_proactor_timeout(tm->task.proactor); |
| 319 |
lock(&tm->task.mutex); |
| 320 |
// If lower latency desired for the proactor timer, we could convert to the proactor task (if not working) and return |
| 321 |
// here with the event batch, and schedule the timer manager task to process the connection timers. |
| 322 |
} |
| 323 |
|
| 324 |
// Next, find all expired connection timers at front of the ordered heap. |
| 325 |
while (pn_list_size(tm->timers_heap)) { |
| 326 |
timer_deadline_t *td = (timer_deadline_t *) pn_list_get(tm->timers_heap, 0); |
| 327 |
if (td->list_deadline > now) |
| 328 |
break; |
| 329 |
|
| 330 |
// Expired. Remove from list. |
| 331 |
timer_deadline_t *min = (timer_deadline_t *) pn_list_minpop(tm->timers_heap); |
| 332 |
assert (min == td); |
| 333 |
min->list_deadline = 0; |
| 334 |
|
| 335 |
// Three possibilities to act on: |
| 336 |
// timer expired -> pni_connection_timeout() |
| 337 |
// timer deadline extended -> minpush back on list to new spot |
| 338 |
// timer freed -> free the associated timer_deadline popped off the list |
| 339 |
if (!td->timer) { |
| 340 |
unlock(&tm->task.mutex); |
| 341 |
timer_deadline_t_free(td); |
| 342 |
lock(&tm->task.mutex); |
| 343 |
} else { |
| 344 |
uint64_t deadline = td->timer->deadline; |
| 345 |
if (deadline) { |
| 346 |
if (deadline <= now) { |
| 347 |
td->timer->deadline = 0; |
| 348 |
pconnection_t *pc = td->timer->connection; |
| 349 |
lock(&tm->deletion_mutex); // Prevent connection from deleting itself when tm->task.mutex dropped. |
| 350 |
unlock(&tm->task.mutex); |
| 351 |
pni_pconnection_timeout(pc); |
| 352 |
unlock(&tm->deletion_mutex); |
| 353 |
lock(&tm->task.mutex); |
| 354 |
} else { |
| 355 |
td->list_deadline = deadline; |
| 356 |
pn_list_minpush(tm->timers_heap, td); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if (timeout) { |
| 363 |
// TODO: query whether perf gain by doing these system calls outside the lock, perhaps with additional set_reset_mutex. |
| 364 |
timerfd_drain(tm->epoll_timer.fd); |
| 365 |
rearm_polling(&tm->epoll_timer, tm->task.proactor->epollfd); |
| 366 |
} |
| 367 |
tm->task.working = false; // must be false for adjust_deadline to do adjustment |
| 368 |
bool notify = adjust_deadline(tm); |
| 369 |
unlock(&tm->task.mutex); |
| 370 |
|
| 371 |
if (notify) |
| 372 |
notify_poller(tm->task.proactor); |
| 373 |
// The timer_manager never has events to batch. |
| 374 |
return NULL; |
| 375 |
// TODO: perhaps become task of one of the timed out timers (if otherwise idle) and process() that task. |
| 376 |
} |
| 377 |
|
| 378 |
// Call with timer_manager lock held. |
| 379 |
// There can be no competing call to this and timer_set() from the same connection. |
| 380 |
static timer_deadline_t *replace_timer_deadline(pni_timer_manager_t *tm, pni_timer_t *timer) { |
| 381 |
assert(timer->connection); |
| 382 |
timer_deadline_t *old_td = timer->timer_deadline; |
| 383 |
assert(old_td); |
| 384 |
// Mark old struct for deletion. No parent timer. |
| 385 |
old_td->timer = NULL; |
| 386 |
|
| 387 |
unlock(&tm->task.mutex); |
| 388 |
// Create replacement timer for life of connection. |
| 389 |
timer_deadline_t *new_td = timer_deadline_t_new(); |
| 390 |
if (!new_td) |
| 391 |
EPOLL_FATAL("replacement timer deadline allocation", errno); |
| 392 |
lock(&tm->task.mutex); |
| 393 |
|
| 394 |
new_td->list_deadline = 0; |
| 395 |
new_td->timer = timer; |
| 396 |
new_td->resequenced = true; |
| 397 |
timer->timer_deadline = new_td; |
| 398 |
return new_td; |
| 399 |
} |