-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathqueue.cpp
More file actions
335 lines (261 loc) · 8.72 KB
/
queue.cpp
File metadata and controls
335 lines (261 loc) · 8.72 KB
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
/* Unit tests for queue.
*
* Author: Steffen Vogel <[email protected]>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <criterion/criterion.h>
#include <criterion/parameterized.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <villas/log.hpp>
#include <villas/node/memory.hpp>
#include <villas/queue.h>
#include <villas/tsc.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
extern void init_memory();
#define SIZE (1 << 10)
static struct CQueue q;
#if defined(_POSIX_BARRIERS) && _POSIX_BARRIERS > 0
static pthread_barrier_t barrier;
#endif
struct param {
int iter_count;
int queue_size;
int thread_count;
bool many;
int batch_size;
struct memory::Type *mt;
volatile int start;
struct CQueue queue;
};
/* Get thread id as integer
* In contrast to pthread_t which is an opaque type */
#ifdef __linux__
#include <sys/syscall.h>
#endif
uint64_t thread_get_id() {
#ifdef __MACH__
uint64_t id;
pthread_threadid_np(pthread_self(), &id);
return id;
#elif defined(SYS_gettid)
return (int)syscall(SYS_gettid);
#endif
return -1;
}
// Sleep, do nothing (Architecture-Specific)
__attribute__((always_inline)) static inline void nop() {
#if defined(__aarch64__)
__asm__ volatile("yield"); // ARM equivalent of rep nop.
#else
__asm__ volatile("rep nop"); // x86 and most other architectures use rep nop.
#endif
}
static void *producer(void *ctx) {
int ret;
struct param *p = (struct param *)ctx;
srand((unsigned)time(0) + thread_get_id());
size_t nops = rand() % 1000;
// Wait for global start signal
while (p->start == 0)
sched_yield();
// Wait for a random time
for (size_t i = 0; i != nops; i += 1)
nop();
// Enqueue
for (intptr_t count = 0; count < p->iter_count; count++) {
do {
ret = queue_push(&p->queue, (void *)count);
sched_yield();
} while (ret != 1);
}
return nullptr;
}
static void *consumer(void *ctx) {
int ret;
struct param *p = (struct param *)ctx;
srand((unsigned)time(0) + thread_get_id());
size_t nops = rand() % 1000;
// Wait for global start signal
while (p->start == 0)
sched_yield();
// Wait for a random time
for (size_t i = 0; i != nops; i += 1)
nop();
// Dequeue
for (intptr_t count = 0; count < p->iter_count; count++) {
intptr_t ptr;
do {
ret = queue_pull(&p->queue, (void **)&ptr);
} while (ret != 1);
//logger->info("consumer: {}", count);
//cr_assert_eq((intptr_t) ptr, count);
}
return nullptr;
}
#if defined(_POSIX_BARRIERS) && _POSIX_BARRIERS > 0
void *producer_consumer(void *ctx) {
struct param *p = (struct param *)ctx;
srand((unsigned)time(0) + thread_get_id());
size_t nops = rand() % 1000;
// Wait for global start signal
while (p->start == 0)
sched_yield();
// Wait for a random time
for (size_t i = 0; i != nops; i += 1)
nop();
for (int iter = 0; iter < p->iter_count; ++iter) {
pthread_barrier_wait(&barrier);
for (intptr_t i = 0; i < p->batch_size; i++) {
void *ptr = (void *)(iter * p->batch_size + i);
while (!queue_push(&p->queue, ptr))
sched_yield(); // queue full, let other threads proceed
}
for (intptr_t i = 0; i < p->batch_size; i++) {
void *ptr;
while (!queue_pull(&p->queue, &ptr))
sched_yield(); // queue empty, let other threads proceed
}
}
return 0;
}
void *producer_consumer_many(void *ctx) {
struct param *p = (struct param *)ctx;
srand((unsigned)time(0) + thread_get_id());
size_t nops = rand() % 1000;
// Wait for global start signal
while (p->start == 0)
sched_yield();
// Wait for a random time
for (size_t i = 0; i != nops; i += 1)
nop();
void *ptrs[p->batch_size];
for (int iter = 0; iter < p->iter_count; ++iter) {
for (intptr_t i = 0; i < p->batch_size; i++)
ptrs[i] = (void *)(iter * p->batch_size + i);
pthread_barrier_wait(&barrier);
int pushed = 0;
do {
pushed +=
queue_push_many(&p->queue, &ptrs[pushed], p->batch_size - pushed);
if (pushed != p->batch_size)
sched_yield(); // queue full, let other threads proceed
} while (pushed < p->batch_size);
int pulled = 0;
do {
pulled +=
queue_pull_many(&p->queue, &ptrs[pulled], p->batch_size - pulled);
if (pulled != p->batch_size)
sched_yield(); // queue empty, let other threads proceed
} while (pulled < p->batch_size);
}
return 0;
}
#endif // _POSIX_BARRIERS
// cppcheck-suppress unknownMacro
Test(queue, single_threaded, .init = init_memory) {
int ret;
struct param p;
p.iter_count = 1 << 8;
p.queue_size = 1 << 10;
p.start = 1; // we start immeadiatly
ret = queue_init(&p.queue, p.queue_size, &memory::heap);
cr_assert_eq(ret, 0, "Failed to create queue");
producer(&p);
consumer(&p);
cr_assert_eq(queue_available(&q), 0);
ret = queue_destroy(&p.queue);
cr_assert_eq(ret, 0, "Failed to create queue");
}
#if defined(_POSIX_BARRIERS) && _POSIX_BARRIERS > 0
// cppcheck-suppress unknownMacro
ParameterizedTestParameters(queue, multi_threaded) {
static struct param params[] = {{.iter_count = 1 << 12,
.queue_size = 1 << 9,
.thread_count = 32,
.many = true,
.batch_size = 10,
.mt = &memory::heap},
{.iter_count = 1 << 8,
.queue_size = 1 << 9,
.thread_count = 4,
.many = true,
.batch_size = 100,
.mt = &memory::heap},
{.iter_count = 1 << 16,
.queue_size = 1 << 14,
.thread_count = 16,
.many = true,
.batch_size = 100,
.mt = &memory::heap},
{.iter_count = 1 << 8,
.queue_size = 1 << 9,
.thread_count = 4,
.many = true,
.batch_size = 10,
.mt = &memory::heap},
{.iter_count = 1 << 16,
.queue_size = 1 << 9,
.thread_count = 16,
.many = false,
.batch_size = 10,
.mt = &memory::mmap_hugetlb}};
return cr_make_param_array(struct param, params, ARRAY_LEN(params));
}
// cppcheck-suppress unknownMacro
ParameterizedTest(struct param *p, queue, multi_threaded, .timeout = 20,
.init = init_memory) {
int ret, cycpop;
struct Tsc tsc;
Logger logger = Log::get("test:queue:multi_threaded");
if (!utils::isPrivileged() && p->mt == &memory::mmap_hugetlb)
cr_skip_test("Skipping memory_mmap_hugetlb tests allocatpr because we are "
"running in an unprivileged environment.");
pthread_t threads[p->thread_count];
p->start = 0;
ret = queue_init(&p->queue, p->queue_size, p->mt);
cr_assert_eq(ret, 0, "Failed to create queue");
uint64_t start_tsc_time, end_tsc_time;
pthread_barrier_init(&barrier, nullptr, p->thread_count);
for (int i = 0; i < p->thread_count; ++i)
pthread_create(&threads[i], nullptr,
p->many ? producer_consumer_many : producer_consumer, p);
sleep(0.2);
ret = tsc_init(&tsc);
cr_assert(!ret);
start_tsc_time = tsc_now(&tsc);
p->start = 1;
for (int i = 0; i < p->thread_count; ++i)
pthread_join(threads[i], nullptr);
end_tsc_time = tsc_now(&tsc);
cycpop = (end_tsc_time - start_tsc_time) / p->iter_count;
if (cycpop < 400)
logger->debug("Cycles/op: {}", cycpop);
else
logger->warn(
"Cycles/op are very high ({}). Are you running on a hypervisor?",
cycpop);
ret = queue_available(&q);
cr_assert_eq(ret, 0);
ret = queue_destroy(&p->queue);
cr_assert_eq(ret, 0, "Failed to destroy queue");
ret = pthread_barrier_destroy(&barrier);
cr_assert_eq(ret, 0, "Failed to destroy barrier");
}
#endif // _POSIX_BARRIERS
// cppcheck-suppress unknownMacro
Test(queue, init_destroy, .init = init_memory) {
int ret;
struct CQueue q;
ret = queue_init(&q, 1024, &memory::heap);
cr_assert_eq(ret, 0); // Should succeed
ret = queue_destroy(&q);
cr_assert_eq(ret, 0); // Should succeed
}