forked from shimonran/RDMA_Hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
635 lines (541 loc) · 17 KB
/
main.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/* vim: set noet: */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
#include <endian.h>
#include <getopt.h>
#include <sys/time.h>
#include <errno.h>
#include <sched.h>
#include <emmintrin.h>
#include <infiniband/verbs.h>
#include "get_clock.h"
#include "sockets.h"
#include "resources.h"
#include "print.h"
/* poll CQ timeout in millisec (2 seconds) */
#define MAX_POLL_CQ_TIMEOUT 2000
#define CACHE_SIZE 64
#define CACHE_LINES (8192 * 1024 / CACHE_SIZE)
#define BM_BITS_PER_WORD (sizeof(uint64_t) * CHAR_BIT)
#define BM_WORDS (CACHE_LINES / BM_BITS_PER_WORD)
uint64_t bm[BM_WORDS] = {0};
#define WORD_OFFSET(b) ((b) / BM_BITS_PER_WORD)
#define BIT_OFFSET(b) ((b) % BM_BITS_PER_WORD)
void bm_set(unsigned int addr) {
bm[WORD_OFFSET(addr)] |= 1ull << BIT_OFFSET(addr);
}
void bm_clear(unsigned int addr) {
bm[WORD_OFFSET(addr)] &= ~(1ull << BIT_OFFSET(addr));
}
bool bm_read(unsigned int addr) {
return (bm[WORD_OFFSET(addr)] & (1ull << BIT_OFFSET(addr))) != 0;
}
unsigned int rand_line() {
unsigned int r;
while (bm_read(r = rand() % CACHE_LINES));
bm_set(r);
return r;
}
/* default config */
struct config_t config = {
NULL, /* dev_name */
NULL, /* server_name */
19875, /* tcp_port */
1, /* ib_port */
-1, /* gid_idx */
1000, /* iters */
0, /* mode */
64, /* msg_size, size of a cache line */
4, /* column count, resulting size of row is four cache lines
pray the prefetcher fetches no more than 2 cache lines
ahead. */
524288 /* row count, each pass is ~33MB so everything should be
evicted from Intel's 20 MB LLC on the next pass. */
};
/* poll_completion */
/******************************************************************************
* * Function: poll_completion
* *
* * Input
* * res pointer to resources structure
* *
* * Output
* * none
* *
* * Returns
* * 0 on success, 1 on failure
* *
* * Description
* * Poll the completion queue for a single event. This function will continue to
* * poll the queue until MAX_POLL_CQ_TIMEOUT milliseconds have passed.
* *
* ******************************************************************************/
static int poll_completion(struct resources *res)
{
struct ibv_wc wc;
unsigned long start_time_msec;
unsigned long cur_time_msec;
struct timeval cur_time;
int poll_result;
int rc = 0;
/* poll the completion for a while before giving up of doing it .. */
gettimeofday(&cur_time, NULL);
start_time_msec = (cur_time.tv_sec * 1000) + (cur_time.tv_usec / 1000);
do {
poll_result = ibv_poll_cq(res->cq, 1, &wc);
gettimeofday(&cur_time, NULL);
cur_time_msec = (cur_time.tv_sec * 1000) + (cur_time.tv_usec / 1000);
} while ((poll_result == 0) && ((cur_time_msec - start_time_msec) < MAX_POLL_CQ_TIMEOUT));
if (poll_result < 0) {
/* poll CQ failed */
fprintf(stderr, "poll CQ failed retval = %d, errno: %s\n", poll_result, strerror(errno));
rc = 1;
} else if (poll_result == 0) {
/* the CQ is empty */
fprintf(stderr, "completion wasn't found in the CQ after timeout. errno: %s\n", strerror(errno));
rc = 1;
} else {
/* CQE found */
debug_print("completion was found in CQ with status 0x%x\n", wc.status);
/* check the completion status (here we don't care about the completion opcode */
if (wc.status != IBV_WC_SUCCESS) {
fprintf(stderr, "got bad completion with status: 0x%x, vendor syndrome: 0x%x\n", wc.status, wc.vendor_err);
rc = 1;
}
}
return rc;
}
/******************************************************************************
* * Function: post_send
* *
* * Input
* * res pointer to resources structure
* * opcode IBV_WR_SEND, IBV_WR_RDMA_READ or IBV_WR_RDMA_WRITE
* *
* * Output
* * none
* *
* * Returns
* * 0 on success, error code on failure
* *
* * Description
* * This function will create and post a send work request
* ******************************************************************************/
static int post_send(struct resources *res, int opcode)
{
struct ibv_send_wr sr;
struct ibv_sge sge;
struct ibv_send_wr *bad_wr = NULL;
int rc;
/* prepare the scatter/gather entry */
memset(&sge, 0, sizeof(sge));
sge.addr = (uintptr_t)res->buf;
sge.length = config.msg_size;
sge.lkey = res->mr->lkey;
/* prepare the send work request */
memset(&sr, 0, sizeof(sr));
sr.next = NULL;
sr.wr_id = 0;
sr.sg_list = &sge;
sr.num_sge = 1;
sr.opcode = opcode;
sr.send_flags = IBV_SEND_SIGNALED;
if(opcode != IBV_WR_SEND) {
sr.wr.rdma.remote_addr = res->remote_props.addr;
sr.wr.rdma.rkey = res->remote_props.rkey;
}
/* there is a Receive Request in the responder side, so we won't get any into RNR flow */
rc = ibv_post_send(res->qp, &sr, &bad_wr);
if (rc)
fprintf(stderr, "failed to post SR\n");
return rc;
}
/* Time the difference between an post_send and a poll_cq */
static int post_send_poll_complete(struct resources *res, int opcode, uint64_t* cycle_count)
{
// From post_send
struct ibv_send_wr sr;
struct ibv_sge sge;
struct ibv_send_wr *bad_wr = NULL;
int rc;
// From poll_complete
struct ibv_wc wc;
int poll_result;
// Timing variables
uint64_t start_cycle_count;
uint64_t end_cycle_count;
/* prepare the scatter/gather entry */
memset(&sge, 0, sizeof(sge));
sge.addr = (uintptr_t)res->buf;
sge.length = config.msg_size;
sge.lkey = res->mr->lkey;
/* prepare the send work request */
memset(&sr, 0, sizeof(sr));
sr.next = NULL;
sr.wr_id = 0;
sr.sg_list = &sge;
sr.num_sge = 1;
sr.opcode = opcode;
sr.send_flags = IBV_SEND_SIGNALED;
if(opcode != IBV_WR_SEND) {
sr.wr.rdma.remote_addr = res->remote_props.addr;
sr.wr.rdma.rkey = res->remote_props.rkey;
}
/* there is a Receive Request in the responder side, so we won't get any into RNR flow */
start_cycle_count = start_tsc();
rc = ibv_post_send(res->qp, &sr, &bad_wr);
if (rc)
fprintf(stderr, "failed to post SR\n");
do {
poll_result = ibv_poll_cq(res->cq, 1, &wc);
} while (poll_result == 0);
end_cycle_count = stop_tsc();
if (poll_result < 0) {
/* poll CQ failed */
fprintf(stderr, "poll CQ failed retval = %d, errno: %s\n", poll_result, strerror(errno));
rc = 1;
} else if (poll_result == 0) {
/* the CQ is empty */
fprintf(stderr, "completion wasn't found in the CQ after timeout. errno: %s\n", strerror(errno));
rc = 1;
} else {
/* CQE found */
*cycle_count = end_cycle_count - start_cycle_count;
/* check the completion status (here we don't care about the completion opcode */
if (wc.status != IBV_WC_SUCCESS) {
fprintf(stderr, "got bad completion with status: 0x%x, vendor syndrome: 0x%x\n", wc.status, wc.vendor_err);
rc = 1;
}
}
return rc;
}
/******************************************************************************
* * Function: print_config
* *
* * Description
* * Print out config information
* ******************************************************************************/
static void print_config(void)
{
debug_print(" ------------------------------------------------\n");
debug_print(" Device name : \"%s\"\n", config.dev_name);
debug_print(" IB port : %u\n", config.ib_port);
if (config.server_name)
debug_print("[client only] IP : %s\n", config.server_name);
debug_print(" TCP port : %u\n", config.tcp_port);
if (config.gid_idx >= 0)
debug_print(" GID index : %u\n", config.gid_idx);
debug_print(" ------------------------------------------------\n\n");
}
/******************************************************************************
* * Function: usage
* *
* * Input
* * argv0 command line arguments
* *
* * Output
* * none
* *
* * Returns
* * none
* *
* * Description
* * print a description of command line syntax
* ******************************************************************************/
static void usage(const char *argv0)
{
fprintf(stdout, "Usage:\n");
fprintf(stdout, " %s start a server and wait for connection\n", argv0);
fprintf(stdout, " %s <host> connect to server at <host>\n", argv0);
fprintf(stdout, "\n");
fprintf(stdout, "Options:\n");
fprintf(stdout, " -p, --port <port> listen on/connect to port <port> (default 18515)\n");
fprintf(stdout, " -d, --ib-dev <dev> use IB device <dev> (default first device found)\n");
fprintf(stdout, " -i, --ib-port <port> use port <port> of IB device (default 1)\n");
fprintf(stdout, " -g, --gid_idx <gid index> gid index to be used in GRH (default not used)\n");
fprintf(stdout, " -n, --iterations <iterations> "
"Number of iterations to perform in the test "
"(default 1000)\n");
fprintf(stdout, " -m, --mode <mode> set to 0 for seq or 1 for rand or 2 for clflush (default 0)\n");
fprintf(stdout, " -s, --msg-size <bytes> size of client buffer (default 64)\n");
fprintf(stdout, " -c, --column-count <num> number of columns (default 128)\n");
fprintf(stdout, " -r, --row-count <num> number of rows (default 8192)\n");
}
static int read_write_read(struct resources *res, uint64_t target_addr, double cycles_to_usec) {
uint64_t write_cyclces, orig_addr, read1_cycles, read2_cycles;
int64_t delta;
/* Store the original addr so we can change back to it after we're done. */
orig_addr = res->remote_props.addr;
res->remote_props.addr = target_addr;
/* First read the contents of the server's buffer.
* This should be a cache miss. */
if (post_send_poll_complete(res, IBV_WR_RDMA_READ, &read1_cycles)) {
fprintf(stderr, "failed to post SR 2\n");
return 1;
}
debug_print("[READ] Contents of server's buffer: '%hhu', it took %lu cycles\n", res->buf[0], read1_cycles);
/* Now we replace what's in the client's buffer to write to the server's buffer.
* This should pull this target_addr memory into cache. */
res->buf[0] = res->buf[0] + 2;
debug_print("[WRITE] Now replacing it with: '%hhu',", res->buf[0]);
if (post_send_poll_complete(res, IBV_WR_RDMA_WRITE, &write_cyclces)) {
fprintf(stderr, "failed to post SR 3\n");
return 1;
}
debug_print("it took %lu cycles\n", write_cyclces);
/* Then we read contents of server's buffer again.
* This should be a cache hit. */
if (post_send_poll_complete(res, IBV_WR_RDMA_READ, &read2_cycles)) {
fprintf(stderr, "failed to post SR 2\n");
return 1;
}
delta = read1_cycles - read2_cycles;
data_print("%lu,%lu,%f,%f\n", read1_cycles, read2_cycles, (read1_cycles * 1000) / cycles_to_usec, (read2_cycles * 1000) / cycles_to_usec);
debug_print("[READ] Contents of server's buffer: '%hhu', it took %lu cycles\n", res->buf[0], read2_cycles);
debug_print("[DIFF] %5ld cycles = %06.1f nsec\n", delta, delta / cycles_to_usec);
/* Restore the original addr */
res->remote_props.addr = orig_addr;
return 0;
}
/******************************************************************************
* * Function: main
* *
* * Input
* * argc number of items in argv
* * argv command line parameters
* *
* * Output
* * none
* *
* * Returns
* * 0 on success, 1 on failure
* *
* * Description
* * Main program code
* ******************************************************************************/
int main(int argc, char *argv[])
{
struct resources res;
int rc = 1;
char temp_char;
int i, j;
uint64_t start_addr, target_addr;
/* parse the command line parameters */
while (1) {
int c;
static struct option long_options[] = {
{.name = "port", .has_arg = 1, .val = 'p' },
{.name = "ib-dev", .has_arg = 1, .val = 'd' },
{.name = "ib-port", .has_arg = 1, .val = 'i' },
{.name = "gid-idx", .has_arg = 1, .val = 'g' },
{.name = "iterations", .has_arg = 1, .val = 'n' },
{.name = "mode", .has_arg = 1, .val = 'm'},
{.name = "msg-size", .has_arg = 1, .val = 's'},
{.name = "column-count", .has_arg = 1, .val = 'c'},
{.name = "row-count", .has_arg = 1, .val = 'r'},
{.name = NULL, .has_arg = 0, .val = '\0'}
};
c = getopt_long(argc, argv, "p:d:i:g:n:m:s:c:r:", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'p':
config.tcp_port = strtoul(optarg, NULL, 0);
break;
case 'd':
config.dev_name = strdup(optarg);
break;
case 'i':
config.ib_port = strtoul(optarg, NULL, 0);
if (config.ib_port < 0) {
usage(argv[0]);
return 1;
}
break;
case 'g':
config.gid_idx = strtoul(optarg, NULL, 0);
if (config.gid_idx < 0) {
usage(argv[0]);
return 1;
}
break;
case 'n':
config.iters = strtoul(optarg, NULL, 0);
if (config.iters < 0) {
usage(argv[0]);
return 1;
}
break;
case 'm':
config.mode = strtoul(optarg, NULL, 0);
if (config.mode < 0 || config.mode > 2) {
usage(argv[0]);
return 1;
}
break;
case 's':
config.msg_size = strtoul(optarg, NULL, 0);
if (config.msg_size < 0) {
usage(argv[0]);
return 1;
}
break;
case 'c':
config.column_count = strtoul(optarg, NULL, 0);
if (config.column_count < 0) {
usage(argv[0]);
return 1;
}
break;
case 'r':
config.row_count = strtoul(optarg, NULL, 0);
if (config.row_count < 0) {
usage(argv[0]);
return 1;
}
break;
default:
usage(argv[0]);
return 1;
}
}
/* parse the last parameter (if exists) as the server name */
if (optind == argc - 1)
config.server_name = argv[optind];
else if (optind < argc) {
usage(argv[0]);
return 1;
}
/* set cpu affinity for client */
if (config.server_name) {
cpu_set_t s;
CPU_ZERO(&s);
CPU_SET(sched_getcpu(), &s);
sched_setaffinity(0, sizeof(cpu_set_t), &s);
}
/* print the used parameters for info */
print_config();
/* init all of the resources, so cleanup will be easy */
resources_init(&res);
/* create resources before using them */
if (resources_create(&res)) {
fprintf(stderr, "failed to create resources\n");
goto main_exit;
}
/* connect the QPs */
if (connect_qp(&res)) {
fprintf(stderr, "failed to connect QPs\n");
goto main_exit;
}
/* let the server post the sr */
if (!config.server_name)
if (post_send(&res, IBV_WR_SEND)) {
fprintf(stderr, "failed to post sr\n");
goto main_exit;
}
/* in both sides we expect to get a completion */
if (poll_completion(&res)) {
fprintf(stderr, "poll completion failed\n");
goto main_exit;
}
/* after polling the completion we have the message in the client buffer too */
if (config.server_name)
debug_print("[Client only] Message is: '%hhu'\n", res.buf[0]);
/* Sync so we are sure server side has data ready before client tries to read it */
if (sock_sync_data(res.sock, 1, "R", &temp_char)) { /* just send a dummy char back and forth */
fprintf(stderr, "sync error before RDMA ops\n");
rc = 1;
goto main_exit;
}
if (config.server_name)
debug_print("Beginning tests...\n----------------------------\n\n");
double cycles_to_usec = get_cpu_mhz(false);
/* Now the client performs an RDMA read and then write on server.
* Note that the server has no idea these events have occured */
if (config.server_name) {
start_addr = res.remote_props.addr;
switch (config.mode) {
case 0: /* seq */
for (i = 0; i < config.column_count; ++i) {
for (j = 0; j < config.row_count; ++j) {
/* index into the row we want */
target_addr = start_addr + j * (config.column_count * config.msg_size);
/* index into the column we want */
target_addr += i * config.msg_size;
if (read_write_read(&res, target_addr, cycles_to_usec)) {
rc = 1;
goto main_exit;
}
}
}
break;
case 1: /* rand */
for (i = 0; i < config.iters; ++i) {
if (read_write_read(&res, start_addr + rand_line(), cycles_to_usec)) {
rc = 1;
goto main_exit;
}
if (i == CACHE_LINES) {
memset(bm, 0, sizeof(bm));
}
}
break;
case 2: /* single byte */
for (i = 0; i < config.iters; ++i) {
if (read_write_read(&res, start_addr, cycles_to_usec)) {
rc = 1;
goto main_exit;
}
if (sock_sync_data(res.sock, 1, "A", &temp_char)) { /* just send a dummy char back and forth */
fprintf(stderr, "sync error after RDMA ops\n");
rc = 1;
goto main_exit;
}
if (sock_sync_data(res.sock, 1, "B", &temp_char)) { /* just send a dummy char back and forth */
fprintf(stderr, "sync error after RDMA ops\n");
rc = 1;
goto main_exit;
}
}
break;
}
}
else if (config.mode == 2) {
for (i = 0; i < config.iters; ++i) {
if (sock_sync_data(res.sock, 1, "A", &temp_char)) { /* just send a dummy char back and forth */
fprintf(stderr, "sync error after RDMA ops\n");
rc = 1;
goto main_exit;
}
_mm_clflush(res.buf);
_mm_mfence();
if (sock_sync_data(res.sock, 1, "B", &temp_char)) { /* just send a dummy char back and forth */
fprintf(stderr, "sync error after RDMA ops\n");
rc = 1;
goto main_exit;
}
}
}
/* Sync so server will know that client is done mucking with its memory */
if (sock_sync_data(res.sock, 1, "W", &temp_char)) { /* just send a dummy char back and forth */
fprintf(stderr, "sync error after RDMA ops\n");
rc = 1;
goto main_exit;
}
rc = 0;
main_exit:
if (resources_destroy(&res)) {
fprintf(stderr, "failed to destroy resources\n");
rc = 1;
}
if(config.dev_name)
free((char *) config.dev_name);
debug_print("\ntest result is %d\n", rc);
return rc;
}