| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* timer.c: Management of timers. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdarg.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <time.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <sys/socket.h> |
| 20 |
#include <sys/time.h> |
| 21 |
#include <netinet/in.h> |
| 22 |
#include <arpa/inet.h> |
| 23 |
#include <netdb.h> |
| 24 |
#include <pthread.h> |
| 25 |
#include <assert.h> |
| 26 |
|
| 27 |
#include "utils.h" |
| 28 |
#include "mempool.h" |
| 29 |
#include "hash.h" |
| 30 |
#include "timer.h" |
| 31 |
|
| 32 |
/* Lock and unlock access to global structures */ |
| 33 |
#define TIMER_LOCK() pthread_mutex_lock(&timer_mutex) |
| 34 |
#define TIMER_UNLOCK() pthread_mutex_unlock(&timer_mutex) |
| 35 |
|
| 36 |
/* Pool of Timer Queues */ |
| 37 |
static timer_queue_t *timer_queue_pool = NULL; |
| 38 |
|
| 39 |
/* Hash table to map Timer ID to timer entries */ |
| 40 |
static hash_table_t *timer_id_hash = NULL; |
| 41 |
|
| 42 |
/* Last ID used. */ |
| 43 |
static timer_id timer_next_id = 1; |
| 44 |
|
| 45 |
/* Mutex to access to global structures (Hash Tables, Pool of queues, ...) */ |
| 46 |
static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 47 |
|
| 48 |
|
| 49 |
/* Find a timer by its ID */ |
| 50 |
static inline timer_entry_t *timer_find_by_id(timer_id id) |
| 51 |
{ |
| 52 |
return(hash_table_lookup(timer_id_hash,&id)); |
| 53 |
} |
| 54 |
|
| 55 |
/* Allocate a new ID. Disgusting method but it should work. */ |
| 56 |
static inline timer_id timer_alloc_id(void) |
| 57 |
{ |
| 58 |
while(hash_table_lookup(timer_id_hash,&timer_next_id)) |
| 59 |
timer_next_id++; |
| 60 |
|
| 61 |
return(timer_next_id); |
| 62 |
} |
| 63 |
|
| 64 |
/* Free an ID */ |
| 65 |
static inline void timer_free_id(timer_id id) |
| 66 |
{ |
| 67 |
hash_table_remove(timer_id_hash,&id); |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
* Select the queue of the pool that has the lowest criticity level. This |
| 72 |
* is a stupid method. |
| 73 |
*/ |
| 74 |
timer_queue_t *timer_select_queue_from_pool(void) |
| 75 |
{ |
| 76 |
timer_queue_t *s_queue,*queue; |
| 77 |
int level; |
| 78 |
|
| 79 |
/* to begin, select the first queue of the pool */ |
| 80 |
s_queue = timer_queue_pool; |
| 81 |
level = s_queue->level; |
| 82 |
|
| 83 |
/* walk through timer queues */ |
| 84 |
for(queue=timer_queue_pool->next;queue;queue=queue->next) { |
| 85 |
if (queue->level < level) { |
| 86 |
level = queue->level; |
| 87 |
s_queue = queue; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* returns selected queue */ |
| 92 |
return s_queue; |
| 93 |
} |
| 94 |
|
| 95 |
/* Add a timer in a queue */ |
| 96 |
static inline void timer_add_to_queue(timer_queue_t *queue, |
| 97 |
timer_entry_t *timer) |
| 98 |
{ |
| 99 |
timer_entry_t *t,*prev = NULL; |
| 100 |
|
| 101 |
/* Insert after the last timer with the same or earlier time */ |
| 102 |
for(t=queue->list;t;t=t->next) { |
| 103 |
if (t->expire > timer->expire) break; |
| 104 |
prev = t; |
| 105 |
} |
| 106 |
|
| 107 |
/* Add it in linked list */ |
| 108 |
timer->next = t; |
| 109 |
timer->prev = prev; |
| 110 |
timer->queue = queue; |
| 111 |
|
| 112 |
if (timer->next) |
| 113 |
timer->next->prev = timer; |
| 114 |
|
| 115 |
if (timer->prev) |
| 116 |
timer->prev->next = timer; |
| 117 |
else |
| 118 |
queue->list = timer; |
| 119 |
|
| 120 |
/* Increment number of timers in queue */ |
| 121 |
queue->timer_count++; |
| 122 |
|
| 123 |
/* Increment criticity level */ |
| 124 |
queue->level += timer->level; |
| 125 |
} |
| 126 |
|
| 127 |
/* Add a timer in a queue atomically */ |
| 128 |
_Unused static inline void timer_add_to_queue_atomic(timer_queue_t *queue, |
| 129 |
timer_entry_t *timer) |
| 130 |
{ |
| 131 |
TIMERQ_LOCK(queue); |
| 132 |
timer_add_to_queue(queue,timer); |
| 133 |
TIMERQ_UNLOCK(queue); |
| 134 |
} |
| 135 |
|
| 136 |
/* Remove a timer from queue */ |
| 137 |
static inline void timer_remove_from_queue(timer_queue_t *queue, |
| 138 |
timer_entry_t *timer) |
| 139 |
{ |
| 140 |
if (timer->prev) |
| 141 |
timer->prev->next = timer->next; |
| 142 |
else |
| 143 |
queue->list = timer->next; |
| 144 |
|
| 145 |
if (timer->next) |
| 146 |
timer->next->prev = timer->prev; |
| 147 |
|
| 148 |
timer->next = timer->prev = NULL; |
| 149 |
|
| 150 |
/* Decrement number of timers in queue */ |
| 151 |
queue->timer_count--; |
| 152 |
|
| 153 |
/* Decrement criticity level */ |
| 154 |
queue->level -= timer->level; |
| 155 |
} |
| 156 |
|
| 157 |
/* Remove a timer from a queue atomically */ |
| 158 |
static inline void |
| 159 |
timer_remove_from_queue_atomic(timer_queue_t *queue,timer_entry_t *timer) |
| 160 |
{ |
| 161 |
TIMERQ_LOCK(queue); |
| 162 |
timer_remove_from_queue(queue,timer); |
| 163 |
TIMERQ_UNLOCK(queue); |
| 164 |
} |
| 165 |
|
| 166 |
/* Free ressources used by a timer */ |
| 167 |
static inline void timer_free(timer_entry_t *timer,int take_lock) |
| 168 |
{ |
| 169 |
if (take_lock) TIMER_LOCK(); |
| 170 |
|
| 171 |
/* Remove ID from hash table */ |
| 172 |
hash_table_remove(timer_id_hash,&timer->id); |
| 173 |
|
| 174 |
if (take_lock) TIMER_UNLOCK(); |
| 175 |
|
| 176 |
/* Free memory used by timer */ |
| 177 |
free(timer); |
| 178 |
} |
| 179 |
|
| 180 |
/* Run timer action */ |
| 181 |
static inline int timer_exec(timer_entry_t *timer) |
| 182 |
{ |
| 183 |
return(timer->callback(timer->user_arg,timer)); |
| 184 |
} |
| 185 |
|
| 186 |
/* Schedule a timer in a queue */ |
| 187 |
static inline void timer_schedule_in_queue(timer_queue_t *queue, |
| 188 |
timer_entry_t *timer) |
| 189 |
{ |
| 190 |
m_tmcnt_t current,current_adj; |
| 191 |
|
| 192 |
/* Set new expiration date and clear "run" flag */ |
| 193 |
if (timer->flags & TIMER_BOUNDARY) { |
| 194 |
current_adj = m_gettime_adj(); |
| 195 |
current = m_gettime(); |
| 196 |
|
| 197 |
timer->expire = current + timer->offset + |
| 198 |
(timer->interval - (current_adj % timer->interval)); |
| 199 |
} else |
| 200 |
timer->expire += timer->interval; |
| 201 |
|
| 202 |
timer->flags &= ~TIMER_RUNNING; |
| 203 |
timer_add_to_queue(queue,timer); |
| 204 |
} |
| 205 |
|
| 206 |
/* Schedule a timer */ |
| 207 |
static int timer_schedule(timer_entry_t *timer) |
| 208 |
{ |
| 209 |
timer_queue_t *queue; |
| 210 |
|
| 211 |
/* Select the least used queue of the pool */ |
| 212 |
if (!(queue = timer_select_queue_from_pool())) { |
| 213 |
fprintf(stderr, |
| 214 |
"timer_schedule: no pool available for timer with ID %llu", |
| 215 |
timer->id); |
| 216 |
return(-1); |
| 217 |
} |
| 218 |
|
| 219 |
/* Reschedule it in queue */ |
| 220 |
TIMERQ_LOCK(queue); |
| 221 |
timer_schedule_in_queue(queue,timer); |
| 222 |
TIMERQ_UNLOCK(queue); |
| 223 |
return(0); |
| 224 |
} |
| 225 |
|
| 226 |
/* Timer loop */ |
| 227 |
static void *timer_loop(timer_queue_t *queue) |
| 228 |
{ |
| 229 |
struct timespec t_spc; |
| 230 |
timer_entry_t *timer; |
| 231 |
m_tmcnt_t c_time; |
| 232 |
|
| 233 |
/* Set signal properties */ |
| 234 |
m_signal_block(SIGINT); |
| 235 |
m_signal_block(SIGQUIT); |
| 236 |
m_signal_block(SIGTERM); |
| 237 |
|
| 238 |
for(;;) |
| 239 |
{ |
| 240 |
/* Prevent asynchronous access problems */ |
| 241 |
TIMERQ_LOCK(queue); |
| 242 |
|
| 243 |
/* We need to check "running" flags to know if we must stop */ |
| 244 |
if (!queue->running) { |
| 245 |
TIMERQ_UNLOCK(queue); |
| 246 |
break; |
| 247 |
} |
| 248 |
|
| 249 |
/* Get first event */ |
| 250 |
timer = queue->list; |
| 251 |
|
| 252 |
/* |
| 253 |
* If we have timers in queue, we setup a timer to wait for first one. |
| 254 |
* In all cases, thread is woken up when a reschedule occurs. |
| 255 |
*/ |
| 256 |
if (timer) { |
| 257 |
t_spc.tv_sec = timer->expire / 1000; |
| 258 |
t_spc.tv_nsec = (timer->expire % 1000) * 1000000; |
| 259 |
pthread_cond_timedwait(&queue->schedule,&queue->lock,&t_spc); |
| 260 |
} |
| 261 |
else { |
| 262 |
/* We just wait for reschedule since we don't have any timer */ |
| 263 |
pthread_cond_wait(&queue->schedule,&queue->lock); |
| 264 |
} |
| 265 |
|
| 266 |
/* We need to check "running" flags to know if we must stop */ |
| 267 |
if (!queue->running) { |
| 268 |
TIMERQ_UNLOCK(queue); |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* Now, we need to find why we were woken up. So, we compare current |
| 274 |
* time with first timer to see if we must execute action associated |
| 275 |
* with it. |
| 276 |
*/ |
| 277 |
c_time = m_gettime(); |
| 278 |
|
| 279 |
/* Get first event */ |
| 280 |
timer = queue->list; |
| 281 |
|
| 282 |
/* If there is nothing to do for now, wait again */ |
| 283 |
if ((timer == NULL) || (timer->expire > c_time)) { |
| 284 |
TIMERQ_UNLOCK(queue); |
| 285 |
continue; |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
* We have a timer to manage. Remove it from queue and mark it as |
| 290 |
* running. |
| 291 |
*/ |
| 292 |
timer_remove_from_queue(queue,timer); |
| 293 |
timer->flags |= TIMER_RUNNING; |
| 294 |
|
| 295 |
/* Execute user function and reschedule timer if required */ |
| 296 |
if (timer_exec(timer)) |
| 297 |
timer_schedule_in_queue(queue,timer); |
| 298 |
|
| 299 |
TIMERQ_UNLOCK(queue); |
| 300 |
} |
| 301 |
|
| 302 |
return NULL; |
| 303 |
} |
| 304 |
|
| 305 |
/* Remove a timer */ |
| 306 |
int timer_remove(timer_id id) |
| 307 |
{ |
| 308 |
timer_queue_t *queue = NULL; |
| 309 |
timer_entry_t *timer; |
| 310 |
|
| 311 |
TIMER_LOCK(); |
| 312 |
|
| 313 |
/* Find timer */ |
| 314 |
if (!(timer = timer_find_by_id(id))) { |
| 315 |
TIMER_UNLOCK(); |
| 316 |
return(-1); |
| 317 |
} |
| 318 |
|
| 319 |
/* If we have a queue, remove timer from it atomically */ |
| 320 |
if (timer->queue) { |
| 321 |
queue = timer->queue; |
| 322 |
timer_remove_from_queue_atomic(queue,timer); |
| 323 |
} |
| 324 |
|
| 325 |
/* Release timer ID */ |
| 326 |
timer_free_id(id); |
| 327 |
|
| 328 |
/* Free memory used by timer */ |
| 329 |
free(timer); |
| 330 |
TIMER_UNLOCK(); |
| 331 |
|
| 332 |
/* Signal to this queue that it has been modified */ |
| 333 |
if (queue) |
| 334 |
pthread_cond_signal(&queue->schedule); |
| 335 |
return(0); |
| 336 |
} |
| 337 |
|
| 338 |
/* Enable a timer */ |
| 339 |
static timer_id timer_enable(timer_entry_t *timer) |
| 340 |
{ |
| 341 |
/* Allocate a new ID */ |
| 342 |
TIMER_LOCK(); |
| 343 |
timer->id = timer_alloc_id(); |
| 344 |
|
| 345 |
/* Insert ID in hash table */ |
| 346 |
if (hash_table_insert(timer_id_hash,&timer->id,timer) == -1) { |
| 347 |
TIMER_UNLOCK(); |
| 348 |
free(timer); |
| 349 |
return(0); |
| 350 |
} |
| 351 |
|
| 352 |
/* Schedule event */ |
| 353 |
if (timer_schedule(timer) == -1) { |
| 354 |
timer_free(timer,FALSE); |
| 355 |
timer = NULL; |
| 356 |
TIMER_UNLOCK(); |
| 357 |
return(0); |
| 358 |
} |
| 359 |
|
| 360 |
/* Returns timer ID */ |
| 361 |
TIMER_UNLOCK(); |
| 362 |
pthread_cond_signal(&timer->queue->schedule); |
| 363 |
return(timer->id); |
| 364 |
} |
| 365 |
|
| 366 |
/* Create a new timer */ |
| 367 |
timer_id timer_create_entry(m_tmcnt_t interval,int boundary,int level, |
| 368 |
timer_proc callback,void *user_arg) |
| 369 |
{ |
| 370 |
timer_entry_t *timer; |
| 371 |
|
| 372 |
/* Allocate memory for new timer entry */ |
| 373 |
if (!(timer = malloc(sizeof(*timer)))) |
| 374 |
return(0); |
| 375 |
|
| 376 |
timer->interval = interval; |
| 377 |
timer->offset = 0; |
| 378 |
timer->callback = callback; |
| 379 |
timer->user_arg = user_arg; |
| 380 |
timer->flags = 0; |
| 381 |
timer->level = level; |
| 382 |
|
| 383 |
/* Set expiration delay */ |
| 384 |
if (boundary) { |
| 385 |
timer->flags |= TIMER_BOUNDARY; |
| 386 |
} else |
| 387 |
timer->expire = m_gettime(); |
| 388 |
|
| 389 |
return(timer_enable(timer)); |
| 390 |
} |
| 391 |
|
| 392 |
/* Create a timer on boundary, with an offset */ |
| 393 |
timer_id timer_create_with_offset(m_tmcnt_t interval,m_tmcnt_t offset, |
| 394 |
int level,timer_proc callback,void *user_arg) |
| 395 |
{ |
| 396 |
timer_entry_t *timer; |
| 397 |
|
| 398 |
/* Allocate memory for new timer entry */ |
| 399 |
if (!(timer = malloc(sizeof(*timer)))) |
| 400 |
return(0); |
| 401 |
|
| 402 |
timer->interval = interval; |
| 403 |
timer->offset = 0; |
| 404 |
timer->callback = callback; |
| 405 |
timer->user_arg = user_arg; |
| 406 |
timer->flags = 0; |
| 407 |
timer->level = level; |
| 408 |
timer->flags |= TIMER_BOUNDARY; |
| 409 |
|
| 410 |
return(timer_enable(timer)); |
| 411 |
} |
| 412 |
|
| 413 |
/* Set a new interval for a timer */ |
| 414 |
int timer_set_interval(timer_id id,long interval) |
| 415 |
{ |
| 416 |
timer_queue_t *queue; |
| 417 |
timer_entry_t *timer; |
| 418 |
|
| 419 |
TIMER_LOCK(); |
| 420 |
|
| 421 |
/* Locate timer */ |
| 422 |
if (!(timer = timer_find_by_id(id))) { |
| 423 |
TIMER_UNLOCK(); |
| 424 |
return(-1); |
| 425 |
} |
| 426 |
|
| 427 |
queue = timer->queue; |
| 428 |
|
| 429 |
TIMERQ_LOCK(queue); |
| 430 |
|
| 431 |
/* Compute new expiration date */ |
| 432 |
timer->interval = interval; |
| 433 |
timer->expire = m_gettime() + (m_tmcnt_t)interval; |
| 434 |
|
| 435 |
timer_remove_from_queue(queue,timer); |
| 436 |
timer_schedule_in_queue(queue,timer); |
| 437 |
|
| 438 |
TIMERQ_UNLOCK(queue); |
| 439 |
TIMER_UNLOCK(); |
| 440 |
|
| 441 |
/* Reschedule */ |
| 442 |
pthread_cond_signal(&queue->schedule); |
| 443 |
return(0); |
| 444 |
} |
| 445 |
|
| 446 |
/* Create a new timer queue */ |
| 447 |
timer_queue_t *timer_create_queue(void) |
| 448 |
{ |
| 449 |
timer_queue_t *queue; |
| 450 |
|
| 451 |
/* Create new queue structure */ |
| 452 |
if (!(queue = malloc(sizeof(*queue)))) |
| 453 |
return NULL; |
| 454 |
|
| 455 |
queue->running = TRUE; |
| 456 |
queue->list = NULL; |
| 457 |
queue->level = 0; |
| 458 |
|
| 459 |
/* Create mutex */ |
| 460 |
if (pthread_mutex_init(&queue->lock,NULL)) |
| 461 |
goto err_mutex; |
| 462 |
|
| 463 |
/* Create condition */ |
| 464 |
if (pthread_cond_init(&queue->schedule,NULL)) |
| 465 |
goto err_cond; |
| 466 |
|
| 467 |
/* Create thread */ |
| 468 |
if (pthread_create(&queue->thread,NULL,(void *(*)(void *))timer_loop,queue)) |
| 469 |
goto err_create; |
| 470 |
|
| 471 |
return queue; |
| 472 |
|
| 473 |
err_create: |
| 474 |
pthread_cond_destroy(&queue->schedule); |
| 475 |
err_cond: |
| 476 |
pthread_mutex_destroy(&queue->lock); |
| 477 |
err_mutex: |
| 478 |
free(queue); |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
|
| 482 |
/* Flush queues */ |
| 483 |
void timer_flush_queues(void) |
| 484 |
{ |
| 485 |
timer_entry_t *timer,*next_timer; |
| 486 |
timer_queue_t *queue,*next_queue; |
| 487 |
pthread_t thread; |
| 488 |
|
| 489 |
TIMER_LOCK(); |
| 490 |
|
| 491 |
for(queue=timer_queue_pool;queue;queue=next_queue) |
| 492 |
{ |
| 493 |
TIMERQ_LOCK(queue); |
| 494 |
next_queue = queue->next; |
| 495 |
thread = queue->thread; |
| 496 |
|
| 497 |
/* mark queue as not running */ |
| 498 |
queue->running = FALSE; |
| 499 |
|
| 500 |
/* suppress all timers */ |
| 501 |
for(timer=queue->list;timer;timer=next_timer) { |
| 502 |
next_timer = timer->next; |
| 503 |
timer_free_id(timer->id); |
| 504 |
free(timer); |
| 505 |
} |
| 506 |
|
| 507 |
/* signal changes to the queue thread */ |
| 508 |
pthread_cond_signal(&queue->schedule); |
| 509 |
|
| 510 |
TIMERQ_UNLOCK(queue); |
| 511 |
|
| 512 |
/* wait for thread to terminate */ |
| 513 |
pthread_join(thread,NULL); |
| 514 |
|
| 515 |
pthread_cond_destroy(&queue->schedule); |
| 516 |
pthread_mutex_destroy(&queue->lock); |
| 517 |
free(queue); |
| 518 |
} |
| 519 |
timer_queue_pool = NULL; |
| 520 |
|
| 521 |
TIMER_UNLOCK(); |
| 522 |
} |
| 523 |
|
| 524 |
/* Add a specified number of queues to the pool */ |
| 525 |
int timer_pool_add_queues(int nr_queues) |
| 526 |
{ |
| 527 |
timer_queue_t *queue; |
| 528 |
int i; |
| 529 |
|
| 530 |
for(i=0;i<nr_queues;i++) |
| 531 |
{ |
| 532 |
if (!(queue = timer_create_queue())) |
| 533 |
return(-1); |
| 534 |
|
| 535 |
TIMER_LOCK(); |
| 536 |
queue->next = timer_queue_pool; |
| 537 |
timer_queue_pool = queue; |
| 538 |
TIMER_UNLOCK(); |
| 539 |
} |
| 540 |
|
| 541 |
return(0); |
| 542 |
} |
| 543 |
|
| 544 |
/* Terminate timer sub-sytem */ |
| 545 |
static void timer_terminate(void) |
| 546 |
{ |
| 547 |
timer_flush_queues(); |
| 548 |
|
| 549 |
assert(timer_id_hash); |
| 550 |
hash_table_delete(timer_id_hash); |
| 551 |
timer_id_hash = NULL; |
| 552 |
} |
| 553 |
|
| 554 |
/* Initialize timer sub-system */ |
| 555 |
int timer_init(void) |
| 556 |
{ |
| 557 |
/* Initialize hash table which maps ID to timer entries */ |
| 558 |
assert(!timer_id_hash); |
| 559 |
if (!(timer_id_hash = hash_u64_create(TIMER_HASH_SIZE))) { |
| 560 |
fprintf(stderr,"timer_init: unable to create hash table."); |
| 561 |
return(-1); |
| 562 |
} |
| 563 |
|
| 564 |
/* Initialize default queues. If this fails, try to continue. */ |
| 565 |
if (timer_pool_add_queues(TIMERQ_NUMBER) == -1) { |
| 566 |
fprintf(stderr, |
| 567 |
"timer_init: unable to initialize at least one timer queue."); |
| 568 |
} |
| 569 |
|
| 570 |
atexit(timer_terminate); |
| 571 |
|
| 572 |
return(0); |
| 573 |
} |