forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cts_queue.h
339 lines (281 loc) · 10.3 KB
/
cts_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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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_CTS_QUEUE_H_
#define SCAL_DATASTRUCTURES_CTS_QUEUE_H_
#define __STDC_FORMAT_MACROS 1 // we want PRIu64 and friends
#define __STDC_LIMIT_MACROS
#include <stdint.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 DTS_DEBUG
template<typename T>
class CTSQueue : Queue<T>{
private:
typedef struct Item {
std::atomic<Item*> next;
std::atomic<T> data;
std::atomic<uint64_t> taken;
std::atomic<uint64_t> timestamp[2];
} Item;
typedef struct SPBuffer {
std::atomic<Item*> *insert;
std::atomic<Item*> *remove;
std::atomic<SPBuffer*> next;
int64_t index;
} SPBuffer;
uint64_t num_threads_;
std::atomic<uint64_t> unlink_lock;
// A map from thread IDs to spBuffers needed for insertion. For
// dynamic numbers of threads a hashmap could be used.
std::atomic<SPBuffer*> *spBuffers_;
std::atomic<SPBuffer*> entry_buffer_;
AtomicCounterTimestamp *timestamping_;
AtomicCounterTimestamp *dequeue_timestamping_;
std::atomic<Item*> **insert_;
std::atomic<Item*> **remove_;
#ifdef DTS_DEBUG
uint64_t* *counter1_;
uint64_t* *counter2_;
#endif
inline void unlink_SPBuffer(SPBuffer* buffer) {
if (buffer->index == -1 || spBuffers_[buffer->index] != NULL) {
return;
}
uint64_t tmp = 0;
if (!unlink_lock.compare_exchange_weak(tmp, 1)) {
return;
}
Item* item = get_youngest_item(buffer);
// Do not unlink a SP buffer which still contains an element.
if (item != NULL) {
unlink_lock.store(0);
return;
}
// Find the SP buffer which stores the buffer to be removed in its
// next pointer
SPBuffer *prev = entry_buffer_;
while (prev->next != entry_buffer_ && prev->next != buffer) {
prev = prev->next;
}
SPBuffer *next =prev->next;
if (next == entry_buffer_) {
// The buffer already got removed.
unlink_lock.store(0);
return;
}
// Unlink the buffer.
prev->next.compare_exchange_weak(next, buffer->next);
unlink_lock.store(0);
}
inline SPBuffer *register_thread(uint64_t thread_id) {
SPBuffer* buffer = scal::tlget_aligned<SPBuffer>(scal::kCachePrefetch);
spBuffers_[thread_id].store(buffer);
buffer->next.store(buffer);
buffer->insert = static_cast<std::atomic<Item*>*>(
scal::get<std::atomic<Item*>>(scal::kCachePrefetch * 4));
buffer->remove = 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);
new_item->taken.store(0);
buffer->insert->store(new_item);
buffer->remove->store(new_item);
buffer->index = thread_id;
SPBuffer* entry = entry_buffer_.load();
SPBuffer* next = entry->next.load();
while (true) {
buffer->next.store(next);
if (entry->next.compare_exchange_weak(next, buffer)) {
return buffer;
}
}
}
inline void unregister_thread(uint64_t thread_id) {
spBuffers_[thread_id].store(NULL);
}
// 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);
spBuffers_ = static_cast<std::atomic<SPBuffer*>*>(
scal::ThreadLocalAllocator::Get().CallocAligned(num_threads_, sizeof(std::atomic<SPBuffer*>),
scal::kCachePrefetch * 4));
for (uint64_t i = 0; i < num_threads_; i++) {
spBuffers_[i].store(NULL);
}
// Create the entry buffer.
SPBuffer* buffer = scal::tlget_aligned<SPBuffer>(scal::kCachePrefetch);
buffer->next.store(buffer);
buffer->insert = static_cast<std::atomic<Item*>*>(
scal::get<std::atomic<Item*>>(scal::kCachePrefetch * 4));
buffer->remove = 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);
new_item->taken.store(0);
buffer->insert->store(new_item);
buffer->remove->store(new_item);
buffer->index = -1;
entry_buffer_.store(buffer);
#ifdef DTS_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 DTS_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 DTS_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) {
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);
new_item->taken.store(0);
SPBuffer *buffer = spBuffers_[thread_id].load();
if (buffer == NULL) {
buffer = register_thread(thread_id);
}
buffer->insert->load()->next.store(new_item);
buffer->insert->store(new_item);
};
bool try_remove(SPBuffer* buffer, T *element, uint64_t *dequeue_timestamp) {
Item* tmp_remove = buffer->remove->load();
Item* tmp_insert = buffer->insert->load();
if (tmp_remove == tmp_insert) {
return false;
}
Item *prev = tmp_remove;
Item *prev_next = prev->next.load();
Item *current_item = prev_next;
uint64_t timestamp[2];
while (true) {
if (current_item == NULL) {
return false;
}
timestamping_->load_timestamp(timestamp, current_item->timestamp);
if (dequeue_timestamp[0] == timestamp[0]) {
Item* next = current_item->next;
if (next != NULL) {
prev->next.compare_exchange_weak(prev_next, next);
}
current_item->taken.store(1);
*element = current_item->data.load();
return true;
} else if (timestamping_->is_later(timestamp, dequeue_timestamp)) {
return false;
} else if (current_item->taken.load() == 0) {
prev = current_item;
prev_next = prev->next.load();
current_item = prev_next;
} else {
current_item = current_item->next.load();
}
}
}
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);
// We start iterating over the thread-local lists at a random index.
SPBuffer* current_buffer;
current_buffer = entry_buffer_.load()->next.load();
// We iterate over all thead-local buffers
while (true) {
if (try_remove(current_buffer, element, dequeue_timestamp)) {
return true;
} else {
current_buffer = current_buffer->next.load();
}
}
}
};
#endif // SCAL_DATASTRUCTURES_CTS_QUEUE_H_