scheduler.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2009-2015 Tobias Brunner
  3. * Copyright (C) 2005-2007 Martin Willi
  4. * Copyright (C) 2005 Jan Hutter
  5. * HSR Hochschule fuer Technik Rapperswil
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. */
  17. /**
  18. * @defgroup scheduler scheduler
  19. * @{ @ingroup processing
  20. */
  21. #ifndef SCHEDULER_H_
  22. #define SCHEDULER_H_
  23. typedef struct scheduler_t scheduler_t;
  24. #include <library.h>
  25. #include <processing/jobs/job.h>
  26. /**
  27. * The scheduler queues timed events which are then passed to the processor.
  28. *
  29. * The scheduler is implemented as a heap. A heap is a special kind of tree-
  30. * based data structure that satisfies the following property: if B is a child
  31. * node of A, then key(A) >= (or <=) key(B). So either the element with the
  32. * greatest (max-heap) or the smallest (min-heap) key is the root of the heap.
  33. * We use a min-heap with the key being the absolute unix time at which an
  34. * event is scheduled. So the root is always the event that will fire next.
  35. *
  36. * An earlier implementation of the scheduler used a sorted linked list to store
  37. * the events. That had the advantage that removing the next event was extremely
  38. * fast, also, adding an event scheduled before or after all other events was
  39. * equally fast (all in O(1)). The problem was, though, that adding an event
  40. * in-between got slower, as the number of events grew larger (O(n)).
  41. * For each connection there could be several events: IKE-rekey, NAT-keepalive,
  42. * retransmissions, expire (half-open), and others. So a gateway that probably
  43. * has to handle thousands of concurrent connections has to be able to queue a
  44. * large number of events as fast as possible. Locking makes this even worse, to
  45. * provide thread-safety, no events can be processed, while an event is queued,
  46. * so making the insertion fast is even more important.
  47. *
  48. * That's the advantage of the heap. Adding an element to the heap can be
  49. * achieved in O(log n) - on the other hand, removing the root node also
  50. * requires O(log n) operations. Consider 10000 queued events. Inserting a new
  51. * event in the list implementation required up to 10000 comparisons. In the
  52. * heap implementation, the worst case is about 13.3 comparisons. That's a
  53. * drastic improvement.
  54. *
  55. * The implementation itself uses a binary tree mapped to a one-based array to
  56. * store the elements. This reduces storage overhead and simplifies navigation:
  57. * the children of the node at position n are at position 2n and 2n+1 (likewise
  58. * the parent node of the node at position n is at position [n/2]). Thus,
  59. * navigating up and down the tree is reduced to simple index computations.
  60. *
  61. * Adding an element to the heap works as follows: The heap is always filled
  62. * from left to right, until a row is full, then the next row is filled. Mapped
  63. * to an array this gets as simple as putting the new element to the first free
  64. * position. In a one-based array that position equals the number of elements
  65. * currently stored in the heap. Then the heap property has to be restored, i.e.
  66. * the new element has to be "bubbled up" the tree until the parent node's key
  67. * is smaller or the element got the new root of the tree.
  68. *
  69. * Removing the next event from the heap works similarly. The event itself is
  70. * the root node and stored at position 1 of the array. After removing it, the
  71. * root has to be replaced and the heap property has to be restored. This is
  72. * done by moving the bottom element (last row, rightmost element) to the root
  73. * and then "seep it down" by swapping it with child nodes until none of the
  74. * children has a smaller key or it is again a leaf node.
  75. */
  76. struct scheduler_t {
  77. /**
  78. * Adds a event to the queue, using a relative time offset in s.
  79. *
  80. * @param job job to schedule
  81. * @param time relative time to schedule job, in s
  82. */
  83. void (*schedule_job) (scheduler_t *this, job_t *job, uint32_t s);
  84. /**
  85. * Adds a event to the queue, using a relative time offset in ms.
  86. *
  87. * @param job job to schedule
  88. * @param time relative time to schedule job, in ms
  89. */
  90. void (*schedule_job_ms) (scheduler_t *this, job_t *job, uint32_t ms);
  91. /**
  92. * Adds a event to the queue, using an absolute time.
  93. *
  94. * The passed timeval should be calculated based on the time_monotonic()
  95. * function.
  96. *
  97. * @param job job to schedule
  98. * @param time absolute time to schedule job
  99. */
  100. void (*schedule_job_tv) (scheduler_t *this, job_t *job, timeval_t tv);
  101. /**
  102. * Returns number of jobs scheduled.
  103. *
  104. * @return number of scheduled jobs
  105. */
  106. u_int (*get_job_load) (scheduler_t *this);
  107. /**
  108. * Remove all scheduled jobs.
  109. */
  110. void (*flush)(scheduler_t *this);
  111. /**
  112. * Destroys a scheduler object.
  113. */
  114. void (*destroy) (scheduler_t *this);
  115. };
  116. /**
  117. * Create a scheduler.
  118. *
  119. * @return scheduler_t object
  120. */
  121. scheduler_t *scheduler_create(void);
  122. #endif /** SCHEDULER_H_ @}*/