forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rts_queue.h
298 lines (254 loc) · 10 KB
/
rts_queue.h
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2012-2013, the Scal Project Authors. All rights reserved.
// Please see the AUTHORS file for details. Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
#ifndef SCAL_DATASTRUCTURES_RTS_QUEUE_H_
#define SCAL_DATASTRUCTURES_RTS_QUEUE_H_
#define __STDC_FORMAT_MACROS 1 // we want PRIu64 and friends
#define __STDC_LIMIT_MACROS
#include <inttypes.h>
#include <assert.h>
#include <atomic>
#include <stdio.h>
#include "datastructures/ts_timestamp.h"
#include "datastructures/queue.h"
#include "util/threadlocals.h"
#include "util/malloc.h"
#include "util/platform.h"
#include "util/random.h"
#define RTS_DEBUG
template<typename T>
class RTSQueue : Queue<T>{
private:
typedef struct Item {
std::atomic<Item*> next;
std::atomic<T> data;
std::atomic<uint64_t> timestamp[2];
} Item;
uint64_t num_threads_;
AtomicCounterTimestamp *timestamping_;
AtomicCounterTimestamp *dequeue_timestamping_;
std::atomic<Item*> **insert_;
std::atomic<Item*> **remove_;
#ifdef RTS_DEBUG
uint64_t* *counter1_;
uint64_t* *counter2_;
#endif
// Helper function to remove the ABA counter from a pointer.
inline void *get_aba_free_pointer(void *pointer) {
uint64_t result = (uint64_t)pointer;
result &= 0xfffffffffffffff8;
return (void*)result;
}
// Helper function which retrieves the ABA counter of the pointer old
// and sets this ABA counter + increment to the pointer pointer.
inline void *add_next_aba(void *pointer, void *old, uint64_t increment) {
uint64_t aba = (uint64_t)old;
aba += increment;
aba &= 0x7;
uint64_t result = (uint64_t)pointer;
result = (result & 0xfffffffffffffff8) | aba;
return (void*)((result & 0xffffffffffffff8) | aba);
}
public:
void initialize(uint64_t num_threads) {
num_threads_ = num_threads;
timestamping_ = static_cast<AtomicCounterTimestamp*>(
scal::get<AtomicCounterTimestamp>(scal::kCachePrefetch * 4));
timestamping_->initialize(0, num_threads);
dequeue_timestamping_ = static_cast<AtomicCounterTimestamp*>(
scal::get<AtomicCounterTimestamp>(scal::kCachePrefetch * 4));
dequeue_timestamping_->initialize(0, num_threads);
insert_ = static_cast<std::atomic<Item*>**>(
scal::ThreadLocalAllocator::Get().CallocAligned(num_threads_, sizeof(std::atomic<Item*>*),
scal::kCachePrefetch * 4));
remove_ = static_cast<std::atomic<Item*>**>(
scal::ThreadLocalAllocator::Get().CallocAligned(num_threads_, sizeof(std::atomic<Item*>*),
scal::kCachePrefetch * 4));
for (uint64_t i = 0; i < num_threads_; i++) {
insert_[i] = static_cast<std::atomic<Item*>*>(
scal::get<std::atomic<Item*>>(scal::kCachePrefetch * 4));
remove_[i] = static_cast<std::atomic<Item*>*>(
scal::get<std::atomic<Item*>>(scal::kCachePrefetch * 4));
// Add a sentinal node.
Item *new_item = scal::get<Item>(scal::kCachePrefetch * 4);
timestamping_->init_sentinel_atomic(new_item->timestamp);
new_item->data.store(0);
new_item->next.store(NULL);
insert_[i]->store(new_item);
remove_[i]->store(new_item);
}
#ifdef RTS_DEBUG
counter1_ = static_cast<uint64_t**>(
scal::ThreadLocalAllocator::Get().CallocAligned(num_threads, sizeof(uint64_t*),
scal::kCachePrefetch * 4));
counter2_ = static_cast<uint64_t**>(
scal::ThreadLocalAllocator::Get().CallocAligned(num_threads, sizeof(uint64_t*),
scal::kCachePrefetch * 4));
for (uint64_t i = 0; i < num_threads; i++) {
counter1_[i] = scal::get<uint64_t>(scal::kCachePrefetch * 4);
*(counter1_[i]) = 0;
counter2_[i] = scal::get<uint64_t>(scal::kCachePrefetch * 4);
*(counter2_[i]) = 0;
}
#endif
}
#ifdef RTS_DEBUG
inline void inc_counter1(uint64_t value) {
uint64_t thread_id = scal::ThreadContext::get().thread_id();
(*counter1_[thread_id]) += value;
}
inline void inc_counter2(uint64_t value) {
uint64_t thread_id = scal::ThreadContext::get().thread_id();
(*counter2_[thread_id]) += value;
}
#endif
char* ds_get_stats(void) {
#ifdef RTS_DEBUG
uint64_t sum1 = 0;
uint64_t sum2 = 1;
for (uint64_t i = 0; i < num_threads_; i++) {
sum1 += *counter1_[i];
sum2 += *counter2_[i];
}
if (sum1 == 0) {
// Avoid division by zero.
sum1 = 1;
}
double avg1 = sum1;
avg1 = (double)0;
double avg2 = sum2;
avg2 /= (double)sum1;
char buffer[255] = { 0 };
uint32_t n = snprintf(buffer,
sizeof(buffer),
" ,\"c1\": %.2f ,\"c2\": %.2f",
avg1, avg2);
if (n != strlen(buffer)) {
fprintf(stderr, "%s: error creating stats string\n", __func__);
abort();
}
char *newbuf = static_cast<char*>(calloc(
strlen(buffer) + 1, sizeof(*newbuf)));
return strncpy(newbuf, buffer, strlen(buffer));
#else
return NULL;
#endif
}
inline void insert_element(T element) {
#ifdef RTS_DEBUG
inc_counter1(1);
#endif
uint64_t thread_id = scal::ThreadContext::get().thread_id();
// Create a new item.
Item *new_item = scal::tlget_aligned<Item>(scal::kCachePrefetch);
timestamping_->set_timestamp(new_item->timestamp);
new_item->data.store(element);
new_item->next.store(NULL);
// Add the item to the thread-local list.
Item* old_insert = insert_[thread_id]->load();
old_insert->next.store(new_item);
insert_[thread_id]->store(new_item);
};
bool try_remove_oldest(T *element, uint64_t *dequeue_timestamp) {
// Initialize the result pointer to NULL, which means that no
// element has been removed.
Item *result = NULL;
// Indicates the index of the buffer which contains the oldest item.
uint64_t buffer_index = -1;
// Memory on the stack frame where timestamps of items can be stored
// temporarily.
uint64_t tmp_timestamp[2][2];
// Index in the tmp_timestamp array where no timestamp is stored at the
// moment.
uint64_t tmp_index = 1;
timestamping_->init_top(tmp_timestamp[0]);
// Pointer to the earliest timestamp found until now.
uint64_t *timestamp = tmp_timestamp[0];
// Stores the value of the remove pointer of a thead-local buffer
// before the buffer is actually accessed.
Item* old_remove = NULL;
// We start iterating of the thread-local lists at a random index.
uint64_t start = scal::hwrand();
// We iterate over all thead-local buffers
uint64_t num_buffers = num_threads_;
for (uint64_t i = 0; i < num_buffers; i++) {
#ifdef RTS_DEBUG
inc_counter2(1);
#endif
uint64_t tmp_buffer_index = (start + i) % (num_buffers);
// We get the remove/insert pointer of the current thread-local
// buffer.
Item* tmp_remove = remove_[tmp_buffer_index]->load();
Item* tmp_insert = insert_[tmp_buffer_index]->load();
Item* item =
((Item*)get_aba_free_pointer(tmp_remove))->next.load();
// We get the oldest element from that thread-local buffer.
// If we found an element, we compare it to the oldest element
// we have found until now.
if (get_aba_free_pointer(tmp_remove) != tmp_insert) {
uint64_t *item_timestamp;
timestamping_->load_timestamp(tmp_timestamp[tmp_index], item->timestamp);
item_timestamp = tmp_timestamp[tmp_index];
// Check if we can remove the element immediately.
if (!timestamping_->is_later(item_timestamp, dequeue_timestamp)) {
if ((remove_[tmp_buffer_index]->load() == tmp_remove) &&
remove_[tmp_buffer_index]->compare_exchange_weak(
tmp_remove, (Item*)add_next_aba(item, tmp_remove, 1))) {
// The item has been removed.
*element = item->data.load(std::memory_order_acquire);
return true;
} else {
// Elimination failed, we have to load a new element of that
// buffer.
tmp_remove = remove_[tmp_buffer_index]->load();
tmp_insert = insert_[tmp_buffer_index]->load();
item =((Item*)get_aba_free_pointer(tmp_remove))->next.load();
if (get_aba_free_pointer(tmp_remove) != tmp_insert) {
timestamping_->load_timestamp(tmp_timestamp[tmp_index], item->timestamp);
item_timestamp = tmp_timestamp[tmp_index];
}
}
}
if (get_aba_free_pointer(tmp_remove) != tmp_insert) {
if (timestamping_->is_later(timestamp, item_timestamp)) {
// We found a new oldest element, so we remember it.
result = item;
buffer_index = tmp_buffer_index;
timestamp = item_timestamp;
tmp_index ^=1;
old_remove = tmp_remove;
}
}
}
}
if (result != NULL) {
if (remove_[buffer_index]->load() == old_remove) {
if (remove_[buffer_index]->compare_exchange_weak(
old_remove, (Item*)add_next_aba(result, old_remove, 1))) {
*element = result->data.load();
return true;
}
}
}
*element = (T)NULL;
return true;
}
inline bool enqueue(T element) {
insert_element(element);
return true;
}
inline bool dequeue(T *element) {
uint64_t dequeue_timestamp[2];
dequeue_timestamping_->set_timestamp_local(dequeue_timestamp);
while (try_remove_oldest(element, dequeue_timestamp)) {
if (*element != (T)NULL) {
return true;
}
}
// This is unreachable code, because this queue blocks when no
// element can be found, i.e. there does not exist an emptiness
// check.
return false;
}
};
#endif // SCAL_DATASTRUCTURES_TS_QUEUE_BUFFER_H_