forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer-set.hh
257 lines (224 loc) · 6.92 KB
/
timer-set.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
/*
* Imported from OSv:
*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#ifndef __TIMER_SET_HH
#define __TIMER_SET_HH
#include <chrono>
#include <limits>
#include <bitset>
#include <array>
#include <boost/intrusive/list.hpp>
#include "bitset-iter.hh"
namespace bi = boost::intrusive;
namespace seastar {
/**
* A data structure designed for holding and expiring timers. It's
* optimized for timer non-delivery by deferring sorting cost until
* expiry time. The optimization is based on the observation that in
* many workloads timers are cancelled or rescheduled before they
* expire. That's especially the case for TCP timers.
*
* The template type "Timer" should have a method named
* get_timeout() which returns Timer::time_point which denotes
* timer's expiration.
*/
template<typename Timer, bi::list_member_hook<> Timer::*link>
class timer_set {
public:
using time_point = typename Timer::time_point;
using timer_list_t = bi::list<Timer, bi::member_hook<Timer, bi::list_member_hook<>, link>>;
private:
using duration = typename Timer::duration;
using timestamp_t = typename Timer::duration::rep;
static constexpr timestamp_t max_timestamp = std::numeric_limits<timestamp_t>::max();
static constexpr int timestamp_bits = std::numeric_limits<timestamp_t>::digits;
// The last bucket is reserved for active timers with timeout <= _last.
static constexpr int n_buckets = timestamp_bits + 1;
std::array<timer_list_t, n_buckets> _buckets;
timestamp_t _last;
timestamp_t _next;
std::bitset<n_buckets> _non_empty_buckets;
private:
static timestamp_t get_timestamp(time_point _time_point)
{
return _time_point.time_since_epoch().count();
}
static timestamp_t get_timestamp(Timer& timer)
{
return get_timestamp(timer.get_timeout());
}
int get_index(timestamp_t timestamp) const
{
if (timestamp <= _last) {
return n_buckets - 1;
}
auto index = bitsets::count_leading_zeros(timestamp ^ _last);
assert(index < n_buckets - 1);
return index;
}
int get_index(Timer& timer) const
{
return get_index(get_timestamp(timer));
}
int get_last_non_empty_bucket() const
{
return bitsets::get_last_set(_non_empty_buckets);
}
public:
timer_set()
: _last(0)
, _next(max_timestamp)
, _non_empty_buckets(0)
{
}
~timer_set() {
for (auto&& list : _buckets) {
while (!list.empty()) {
auto& timer = *list.begin();
timer.cancel();
}
}
}
/**
* Adds timer to the active set.
*
* The value returned by timer.get_timeout() is used as timer's expiry. The result
* of timer.get_timeout() must not change while the timer is in the active set.
*
* Preconditions:
* - this timer must not be currently in the active set or in the expired set.
*
* Postconditions:
* - this timer will be added to the active set until it is expired
* by a call to expire() or removed by a call to remove().
*
* Returns true if and only if this timer's timeout is less than get_next_timeout().
* When this function returns true the caller should reschedule expire() to be
* called at timer.get_timeout() to ensure timers are expired in a timely manner.
*/
bool insert(Timer& timer)
{
auto timestamp = get_timestamp(timer);
auto index = get_index(timestamp);
_buckets[index].push_back(timer);
_non_empty_buckets[index] = true;
if (timestamp < _next) {
_next = timestamp;
return true;
}
return false;
}
/**
* Removes timer from the active set.
*
* Preconditions:
* - timer must be currently in the active set. Note: it must not be in
* the expired set.
*
* Postconditions:
* - timer is no longer in the active set.
* - this object will no longer hold any references to this timer.
*/
void remove(Timer& timer)
{
auto index = get_index(timer);
auto& list = _buckets[index];
list.erase(list.iterator_to(timer));
if (list.empty()) {
_non_empty_buckets[index] = false;
}
}
/**
* Expires active timers.
*
* The time points passed to this function must be monotonically increasing.
* Use get_next_timeout() to query for the next time point.
*
* Preconditions:
* - the time_point passed to this function must not be lesser than
* the previous one passed to this function.
*
* Postconditons:
* - all timers from the active set with Timer::get_timeout() <= now are moved
* to the expired set.
*/
timer_list_t expire(time_point now)
{
timer_list_t exp;
auto timestamp = get_timestamp(now);
if (timestamp < _last) {
abort();
}
auto index = get_index(timestamp);
for (int i : bitsets::for_each_set(_non_empty_buckets, index + 1)) {
exp.splice(exp.end(), _buckets[i]);
_non_empty_buckets[i] = false;
}
_last = timestamp;
_next = max_timestamp;
auto& list = _buckets[index];
while (!list.empty()) {
auto& timer = *list.begin();
list.pop_front();
if (timer.get_timeout() <= now) {
exp.push_back(timer);
} else {
insert(timer);
}
}
_non_empty_buckets[index] = !list.empty();
if (_next == max_timestamp && _non_empty_buckets.any()) {
for (auto& timer : _buckets[get_last_non_empty_bucket()]) {
_next = std::min(_next, get_timestamp(timer));
}
}
return exp;
}
/**
* Returns a time point at which expire() should be called
* in order to ensure timers are expired in a timely manner.
*
* Returned values are monotonically increasing.
*/
time_point get_next_timeout() const
{
return time_point(duration(std::max(_last, _next)));
}
/**
* Clears both active and expired timer sets.
*/
void clear()
{
for (int i : bitsets::for_each_set(_non_empty_buckets)) {
_buckets[i].clear();
}
}
size_t size() const
{
size_t res = 0;
for (int i : bitsets::for_each_set(_non_empty_buckets)) {
res += _buckets[i].size();
}
return res;
}
/**
* Returns true if and only if there are no timers in the active set.
*/
bool empty() const
{
return _non_empty_buckets.none();
}
time_point now() {
return Timer::clock::now();
}
};
};
#endif