3.2 Scheme 2 -- ORDERED LIST/TIMER
QUEUES
Here [3] PER_TICK_BOOKKEEPING latency is re-
duced at the expense of START_TIMER. Timers are
stored ill an ordered list. Unlike Scheme 1, we will
store the absolute time at which the timer expires,
and not the interval before expiry.
The timer that is due to expire at the earliest time is
stored at the head of the list. Subsequent timers are
stored in increasing order as shown in Figure 2.
In Fig. 2 the lowest timer is due to expire at absolute
time 10 hours, 23 minutes, and 12 seconds.
Because the list is sorted, PER_TICK_PROCESSING
need only increment the current time of day, and com-
pare it with the head of the list. If they are equal,
or the time of day is greater, it deletes that list ele-
ment and calls EXPIRY_PROCESSING. It continues
to delete elements at the head of the list until the
expiry time of the head of the list is strictly less than
tile time of day.
START_TIMER searches the list to find the po-
sition to insert the new timer. In the example,
START_TIMER will insert a new timer due to expire
at 10:24:01 between the second and third elements.
The worst case latency to start a timer is
O(n).
The
average latency depends on the distribution of timer
intervals (from time started to time stopped), and the
distribution of the arrival process according to which
calls to START_TIMER are made.
Interestingly, this can be modeled (Figure 3) as a sin-
gle queue with infinite servers; this is valid because
every timer in the queue is essentially decremented
(or served) every timer tick. It is shown in I4] that we
can use Little's result to obtain the average number
in the queue; also the distribution of the remaining
time of elements in the timer queue seen by a new re-
quest is the residual life density of the timer interval
distribution.
If the arrival distribution is Poisson, the list is
searched from the head, and reads and writes both
cost one unit, then the average cost of insertion for
negative exponential and uniform timer interval dis-
tributions is shown in [4] to be:
2 + 2/3n --
negative exponential
2 + 1/2n --
uniform
Results for other timer interval distributions can be
computed using a result in [4]. For a negative expo-
nential distribution we can reduce the average cost to
2 + n/3
by searching the list from the rear. In fact,
if timers are always inserted at the rear of the list,
this search strategy yields an O(1) START_TIMER
latency. This happens, for instance, if all timers in-
tervals have the same value. However, for a get---1
distribution of the timer interval, we assume the av-
erage latency of insertion is
O(n).
STOP_TIMER need not search the list if the list is
doubly linked. When START_TIMER inserts a timer
into the ordered list it can store a pointer to the
element. STOP_TIMER can then use this pointer
to delete the element in O(1) time from the doubly
linked list. This can be used by any timer scheme.
If Scheme 2 is implemented by a host processor, the
interrupt overhead on every tick can be avoided if
there is hardware support to maintain a single timer.
The hardware timer is set to expire at the time at
which the the timer at the head of the list is due
to expire. The hardware intercepts all clock ticks
and interrupts the host only when a timer actually
expires. Unfortunately, some processor architectures
do not offer this capability.
Algorithms similar to Scheme 2 are used by both
VMS and UNIX in implementing their timer modules.
The performance of the two schemes is summarized
in Figure 4.
As for Space, Scheme 1 needs the minimum space
possible; Scheme 2 needs
O(n)
extra space for the
forward and back pointers between queue elements.
4
Timer Algorithms, Sorting Tech-
niques, and Time-Flow Mecha-
nisms in Discrete Event Simula-
tions
4.1 Sorting Algorithms and Priority Queues
Scheme 2 reduced PER_TICK_BOOKKEEPING la-
tency at the expense of START_TIMER by keeping
the timer list sorted. Consider the relationship be-
tween timer and sorting algorithms depicted in Figure
5.
However:
• In a typical sort all elements are input to the
27