forked from rigtorp/ipc-bench
-
Notifications
You must be signed in to change notification settings - Fork 32
/
mempipe_thr.c
447 lines (386 loc) · 11.4 KB
/
mempipe_thr.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
Measure latency of IPC using tcp sockets
Copyright (c) 2011 Steven Smith <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/* Simple shared-memory IPC throughput test, with spin-waits rather
than blocking. The shared area is divided into packets, each of
which starts with a header. The header is the size of a cache
line, but the only really interesting field is the size of the
packet. To transmit, you just copy the data you want to transmit
it and then go back and write the header. Meanwhile, the receiver
will be sitting spinning waiting for the header to arrive, and will
read the packet out as soon as it does. Once it's done so, it'll
go back and set the finished flag in the header, so that the
transmitter knows when it's safe to reuse a bit of buffer for a new
message. The benchmark which we build on top of this just keeps
sending fixed-sized messages for soem number of times, waiting as
appropriate for the receiver to pick them up. */
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
#undef USE_MWAIT
#ifndef NO_FUTEX
#define USE_FUTEX
#endif
#include "test.h"
#include "xutil.h"
#ifdef Linux
#include <linux/futex.h>
#include "futex.h"
#endif
#define PAGE_SIZE 4096
#define CACHE_LINE_SIZE 64
static unsigned nr_shared_pages = 512;
#define ring_size (PAGE_SIZE * nr_shared_pages)
struct msg_header {
#define MH_FLAG_READY 1
#define MH_FLAG_STOP 2
#define MH_FLAG_WAITING 4
#define MH_FLAGS (MH_FLAG_READY|MH_FLAG_STOP|MH_FLAG_WAITING)
unsigned size_and_flags;
int pad[CACHE_LINE_SIZE / sizeof(int) - 1];
};
struct ring_state {
void* ringmem;
unsigned long next_tx_offset;
unsigned long first_unacked_msg;
unsigned long next_message_start;
struct iovec vecs[2];
};
static unsigned long
mask_ring_index(unsigned long idx)
{
return idx & (ring_size - 1);
}
static void
init_test(test_data *td)
{
struct ring_state* rs = (struct ring_state*)xmalloc(sizeof(struct ring_state));
rs->ringmem = establish_shm_segment(nr_shared_pages, td->numa_node);
td->data = rs;
}
#ifdef USE_MWAIT
static void
mwait(void)
{
asm volatile("mwait\n"
:
: "a" (0), "c" (0));
}
static void
monitor(const volatile void *what)
{
asm volatile("monitor\n"
:
: "a" (what),
"c" (0),
"d" (0)
);
}
#else
static void
mwait(void)
{
}
static void
monitor(const volatile void *what)
{
}
#endif
/* Deferred write state */
#ifdef USE_FUTEX
static struct {
volatile unsigned *ptr;
unsigned val;
unsigned cntr;
} deferred_write;
static void
_set_message_ready(volatile unsigned *ptr, unsigned val)
{
int sz;
sz = atomic_xchg(ptr, val);
if (sz & MH_FLAG_WAITING)
futex_wake(ptr);
}
#endif
static int
wait_for_message_ready(volatile struct msg_header *mh, int desired_state)
{
int sz;
#ifdef USE_FUTEX
int new_sz;
if (deferred_write.ptr) {
_set_message_ready(deferred_write.ptr, deferred_write.val);
deferred_write.ptr = NULL;
}
while (1) {
sz = mh->size_and_flags;
if ((sz & MH_FLAG_READY) == desired_state)
break;
new_sz = sz | MH_FLAG_WAITING;
if (new_sz == sz ||
atomic_cmpxchg(&mh->size_and_flags, sz, new_sz) == sz)
futex_wait_while_equal(&mh->size_and_flags, new_sz);
}
#else
sz = mh->size_and_flags;
if ((sz & MH_FLAG_READY) != desired_state) {
while (1) {
monitor(&mh->size_and_flags);
sz = mh->size_and_flags;
if ((sz & MH_FLAG_READY) == desired_state)
break;
mwait();
}
}
#endif
return sz;
}
static void
set_message_ready(volatile struct msg_header *mh, int size)
{
#ifdef USE_FUTEX
if (deferred_write.ptr) {
deferred_write.cntr--;
mh->size_and_flags = size;
if (!deferred_write.cntr) {
_set_message_ready(deferred_write.ptr, deferred_write.val);
deferred_write.ptr = NULL;
}
} else {
deferred_write.ptr = &mh->size_and_flags;
deferred_write.val = size;
deferred_write.cntr = 2;
}
#else
mh->size_and_flags = size;
#endif
}
static void
init_child(test_data *td)
{
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh = rs->ringmem;
/* Sync up with parent */
mh->size_and_flags = 0xf008;
while (mh->size_and_flags == 0xf008)
;
rs->next_message_start = 0;
/* Enter main message loop */
}
static struct iovec* get_read_buffer(test_data* td, int len, int* n_vecs) {
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh;
int sz;
assert(rs->next_message_start % CACHE_LINE_SIZE == 0);
mh = rs->ringmem + mask_ring_index(rs->next_message_start);
sz = wait_for_message_ready(mh, MH_FLAG_READY);
if (sz & MH_FLAG_STOP) { /* End of test */
*n_vecs = 0;
return 0;
}
sz &= ~MH_FLAGS;
if(sz != td->size) {
exit(1);
}
unsigned long offset = mask_ring_index(rs->next_message_start + sizeof(struct msg_header));
if (offset + td->size <= ring_size) {
rs->vecs[0].iov_base = rs->ringmem + offset;
rs->vecs[0].iov_len = td->size;
*n_vecs = 1;
}
else {
rs->vecs[0].iov_base = rs->ringmem + offset;
rs->vecs[0].iov_len = ring_size - offset;
rs->vecs[1].iov_base = rs->ringmem;
rs->vecs[1].iov_len = td->size - (ring_size - offset);
*n_vecs = 2;
}
return rs->vecs;
}
static void release_read_buffer(test_data* td, struct iovec* vecs, int n_vecs) {
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh = rs->ringmem + mask_ring_index(rs->next_message_start);
set_message_ready(mh, td->size);
rs->next_message_start += td->size + sizeof(struct msg_header);
}
static void child_finish(test_data* td) {
#ifdef USE_FUTEX
if(deferred_write.ptr)
_set_message_ready(deferred_write.ptr, deferred_write.val);
#endif
}
static void
init_parent(test_data *td)
{
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh = rs->ringmem;
assert(td->size < ring_size - sizeof(struct msg_header));
/* Wait for child to show up. */
while (mh->size_and_flags != 0xf008)
;
mh->size_and_flags = 0;
rs->next_tx_offset = 0;
rs->first_unacked_msg = 0;
/* Round up to multiple of cache line size, for sanity. */
td->size = td->size + CACHE_LINE_SIZE - 1;
td->size -= td->size % CACHE_LINE_SIZE;
}
struct iovec*
get_write_buffer(test_data* td, int len, int* n_vecs) {
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh;
assert(len == td->size);
/* Check for available ring space (eom = end of message) */
unsigned long eom = rs->next_tx_offset + td->size + sizeof(struct msg_header) * 2;
while (eom - rs->first_unacked_msg > ring_size) {
int size;
mh = rs->ringmem + mask_ring_index(rs->first_unacked_msg);
size = wait_for_message_ready(mh, 0);
size &= ~MH_FLAGS;
rs->first_unacked_msg += size + sizeof(struct msg_header);
}
unsigned long offset = mask_ring_index(rs->next_tx_offset + sizeof(struct msg_header));
if (offset + td->size <= ring_size) {
rs->vecs[0].iov_base = rs->ringmem + offset;
rs->vecs[0].iov_len = td->size;
*n_vecs = 1;
} else {
rs->vecs[0].iov_base = rs->ringmem + offset;
rs->vecs[0].iov_len = ring_size - offset;
rs->vecs[1].iov_base = rs->ringmem;
rs->vecs[1].iov_len = td->size - (ring_size - offset);
*n_vecs = 2;
}
return rs->vecs;
}
void release_write_buffer(test_data* td, struct iovec* vecs, int nvecs) {
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh;
volatile struct msg_header *mh2;
assert(vecs == rs->vecs);
/* Send message */
mh = rs->ringmem + mask_ring_index(rs->next_tx_offset);
/* Make sure that the size field in the *next* message is clear
before setting the size field in *this* message. That makes
sure that the receiver stops and spins in the right place,
rather than wandering off into la-la land if it picks up a
stale message. */
mh2 = rs->ringmem + mask_ring_index(rs->next_tx_offset + td->size + sizeof(struct msg_header));
mh2->size_and_flags = 0;
set_message_ready(mh, td->size | MH_FLAG_READY);
rs->next_tx_offset += td->size + sizeof(struct msg_header);
}
void parent_finish(test_data* td) {
struct ring_state* rs = (struct ring_state*)td->data;
volatile struct msg_header *mh;
mh = rs->ringmem + mask_ring_index(rs->next_tx_offset);
mh->size_and_flags = MH_FLAG_READY | MH_FLAG_STOP;
#ifdef USE_FUTEX
futex_wake(&mh->size_and_flags);
#endif
/* Wait for child to acknowledge receipt of all messages */
while (rs->first_unacked_msg != rs->next_tx_offset) {
int size;
mh = rs->ringmem + mask_ring_index(rs->first_unacked_msg);
size = wait_for_message_ready(mh, 0);
size &= ~MH_FLAGS;
rs->first_unacked_msg += size + sizeof(struct msg_header);
}
}
#ifdef USE_MWAIT
static void
cpuid(int leaf, unsigned long *a, unsigned long *b, unsigned long *c, unsigned long *d)
{
unsigned long _a, _b, _c, _d;
asm ("cpuid"
: "=a" (_a), "=b" (_b), "=c" (_c), "=d" (_d)
: "0" (leaf)
);
if (a)
*a = _a;
if (b)
*b = _b;
if (c)
*c = _c;
if (d)
*d = _d;
}
static void
check_monitor_line_size(void)
{
unsigned long a, b, c;
cpuid(5, &a, &b, NULL, NULL);
assert(a == b);
assert(a == CACHE_LINE_SIZE);
cpuid(1, NULL, NULL, &c, NULL);
printf("Available: %d\n", !!(c & (1 << 3)));
}
#else
static void
check_monitor_line_size(void)
{
}
#endif
int
main(int argc, char *argv[])
{
test_t t = {
.name = "mempipe_"
#ifdef NO_FUTEX
"spin_"
#endif
"thr",
.is_latency_test = 0,
.init_test = init_test,
.init_parent = init_parent,
.finish_parent = parent_finish,
.init_child = init_child,
.finish_child = child_finish,
.get_write_buffer = get_write_buffer,
.release_write_buffer = release_write_buffer,
.get_read_buffer = get_read_buffer,
.release_read_buffer = release_read_buffer
};
char *ring_order = getenv("MEMPIPE_RING_ORDER");
check_monitor_line_size();
if (ring_order) {
int as_int;
if (sscanf(ring_order, "%d", &as_int) != 1)
err(1, "MEMPIPE_RING_ORDER must be an integer");
if (as_int < 0 || as_int > 15)
errx(1, "MEMPIPE_RING_ORDER must be between 0 and 15");
nr_shared_pages = 1 << as_int;
}
run_test(argc, argv, &t);
return 0;
}