forked from linux-rdma/perftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperftest_resources.c
executable file
·5125 lines (4404 loc) · 154 KB
/
perftest_resources.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if !defined(__FreeBSD__)
#include <malloc.h>
#endif
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
#if defined(__FreeBSD__)
#include <sys/stat.h>
#endif
#include "perftest_resources.h"
#include "raw_ethernet_resources.h"
#include "config.h"
#ifdef HAVE_VERBS_EXP
static enum ibv_exp_wr_opcode exp_opcode_verbs_array[] = {IBV_EXP_WR_SEND,IBV_EXP_WR_RDMA_WRITE,IBV_EXP_WR_RDMA_READ};
static enum ibv_exp_wr_opcode exp_opcode_atomic_array[] = {IBV_EXP_WR_ATOMIC_CMP_AND_SWP,IBV_EXP_WR_ATOMIC_FETCH_AND_ADD};
#endif
static enum ibv_wr_opcode opcode_verbs_array[] = {IBV_WR_SEND,IBV_WR_RDMA_WRITE,IBV_WR_RDMA_READ};
static enum ibv_wr_opcode opcode_atomic_array[] = {IBV_WR_ATOMIC_CMP_AND_SWP,IBV_WR_ATOMIC_FETCH_AND_ADD};
#define CPU_UTILITY "/proc/stat"
struct perftest_parameters* duration_param;
struct check_alive_data check_alive_data;
/******************************************************************************
* Beginning
******************************************************************************/
#ifdef HAVE_CUDA
#define ASSERT(x) \
do { \
if (!(x)) { \
fprintf(stdout, "Assertion \"%s\" failed at %s:%d\n", #x, __FILE__, __LINE__); \
} \
} while (0)
#define CUCHECK(stmt) \
do { \
CUresult result = (stmt); \
ASSERT(CUDA_SUCCESS == result); \
} while (0)
/*----------------------------------------------------------------------------*/
static CUdevice cuDevice;
static CUcontext cuContext;
static int pp_init_gpu(struct pingpong_context *ctx, size_t _size)
{
const size_t gpu_page_size = 64*1024;
size_t size = (_size + gpu_page_size - 1) & ~(gpu_page_size - 1);
printf("initializing CUDA\n");
CUresult error = cuInit(0);
if (error != CUDA_SUCCESS) {
printf("cuInit(0) returned %d\n", error);
exit(1);
}
int deviceCount = 0;
error = cuDeviceGetCount(&deviceCount);
if (error != CUDA_SUCCESS) {
printf("cuDeviceGetCount() returned %d\n", error);
exit(1);
}
/* This function call returns 0 if there are no CUDA capable devices. */
if (deviceCount == 0) {
printf("There are no available device(s) that support CUDA\n");
return 1;
} else if (deviceCount == 1)
printf("There is 1 device supporting CUDA\n");
else
printf("There are %d devices supporting CUDA, picking first...\n", deviceCount);
int devID = 0;
/* pick up device with zero ordinal (default, or devID) */
CUCHECK(cuDeviceGet(&cuDevice, devID));
char name[128];
CUCHECK(cuDeviceGetName(name, sizeof(name), devID));
printf("[pid = %d, dev = %d] device name = [%s]\n", getpid(), cuDevice, name);
printf("creating CUDA Ctx\n");
/* Create context */
error = cuCtxCreate(&cuContext, CU_CTX_MAP_HOST, cuDevice);
if (error != CUDA_SUCCESS) {
printf("cuCtxCreate() error=%d\n", error);
return 1;
}
printf("making it the current CUDA Ctx\n");
error = cuCtxSetCurrent(cuContext);
if (error != CUDA_SUCCESS) {
printf("cuCtxSetCurrent() error=%d\n", error);
return 1;
}
printf("cuMemAlloc() of a %zd bytes GPU buffer\n", size);
CUdeviceptr d_A;
error = cuMemAlloc(&d_A, size);
if (error != CUDA_SUCCESS) {
printf("cuMemAlloc error=%d\n", error);
return 1;
}
printf("allocated GPU buffer address at %016llx pointer=%p\n", d_A,
(void *) d_A);
ctx->buf[0] = (void*)d_A;
return 0;
}
static int pp_free_gpu(struct pingpong_context *ctx)
{
int ret = 0;
CUdeviceptr d_A = (CUdeviceptr) ctx->buf[0];
printf("deallocating RX GPU buffer\n");
cuMemFree(d_A);
d_A = 0;
printf("destroying current CUDA Ctx\n");
CUCHECK(cuCtxDestroy(cuContext));
return ret;
}
#endif
static int pp_init_mmap(struct pingpong_context *ctx, size_t size,
const char *fname, unsigned long offset)
{
int fd = open(fname, O_RDWR);
if (fd < 0) {
printf("Unable to open '%s': %s\n", fname, strerror(errno));
return 1;
}
ctx->buf[0] = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, fd,
offset);
close(fd);
if (ctx->buf[0] == MAP_FAILED) {
printf("Unable to mmap '%s': %s\n", fname, strerror(errno));
return 1;
}
printf("allocated mmap buffer of size %zd at %p\n", size, ctx->buf[0]);
return 0;
}
static int pp_free_mmap(struct pingpong_context *ctx)
{
munmap(ctx->buf[0], ctx->buff_size);
return 0;
}
#ifdef HAVE_VERBS_EXP
static void get_verbs_pointers(struct pingpong_context *ctx)
{
ctx->exp_post_send_func_pointer = ibv_exp_get_provider_func(ctx->context,IBV_EXP_POST_SEND_FUNC);
if (!ctx->exp_post_send_func_pointer) {
fprintf(stderr, "Couldn't get ibv_exp_post_send pointer\n");
ctx->exp_post_send_func_pointer = &ibv_exp_post_send;
}
ctx->post_send_func_pointer = ibv_exp_get_provider_func(ctx->context,IBV_POST_SEND_FUNC);
if (!ctx->post_send_func_pointer) {
fprintf(stderr, "Couldn't get ibv_post_send pointer\n");
ctx->post_send_func_pointer = &ibv_post_send;
}
ctx->poll_cq_func_pointer = ibv_exp_get_provider_func(ctx->context,IBV_POLL_CQ_FUNC);
if (!ctx->poll_cq_func_pointer) {
fprintf(stderr, "Couldn't get ibv_poll_cq pointer\n");
}
}
#endif
static int next_word_string(char* input, char* output, int from_index)
{
int i = from_index;
int j = 0;
while (input[i] != ' ') {
output[j] = input[i];
j++; i++;
}
output[j]=0;
return i+1;
}
static int get_n_word_string(char *input, char *output,int from_index, int iters)
{
for (;iters > 0; iters--) {
from_index = next_word_string(input,output,from_index);
}
return from_index;
}
static void compress_spaces(char *str, char *dst)
{
for (; *str; ++str) {
*dst++ = *str;
if (isspace(*str)) {
do ++str;
while (isspace(*str));
--str;
}
}
*dst = 0;
}
static void get_cpu_stats(struct perftest_parameters *duration_param,int stat_index)
{
char* file_name = CPU_UTILITY;
FILE *fp;
char line[100];
char tmp[100];
int index=0;
fp = fopen(file_name, "r");
if (fp != NULL) {
if (fgets(line,100,fp) != NULL) {
compress_spaces(line,line);
index=get_n_word_string(line,tmp,index,2); /* skip first word */
duration_param->cpu_util_data.ustat[stat_index-1] = atoll(tmp);
index=get_n_word_string(line,tmp,index,3); /* skip 2 stats */
duration_param->cpu_util_data.idle[stat_index-1] = atoll(tmp);
fclose(fp);
}
}
}
#ifdef HAVE_VERBS_EXP
static int check_for_contig_pages_support(struct ibv_context *context)
{
int answer;
struct ibv_exp_device_attr attr;
memset(&attr,0,sizeof attr);
if (ibv_exp_query_device(context,&attr)) {
fprintf(stderr, "Couldn't get device attributes\n");
return FAILURE;
}
answer = ( attr.exp_device_cap_flags &= IBV_EXP_DEVICE_MR_ALLOCATE) ? SUCCESS : FAILURE;
return answer;
}
#endif
#ifdef HAVE_XRCD
/******************************************************************************
*
******************************************************************************/
static int ctx_xrcd_create(struct pingpong_context *ctx,struct perftest_parameters *user_param)
{
char *tmp_file_name;
struct ibv_xrcd_init_attr xrcd_init_attr;
memset(&xrcd_init_attr , 0 , sizeof xrcd_init_attr);
tmp_file_name = (user_param->machine == SERVER) ? SERVER_FD : CLIENT_FD;
ctx->fd = open(tmp_file_name, O_RDONLY | O_CREAT, S_IRUSR | S_IRGRP);
if (ctx->fd < 0) {
fprintf(stderr,"Error opening file %s errno: %s", tmp_file_name,strerror(errno));
return FAILURE;
}
xrcd_init_attr.comp_mask = IBV_XRCD_INIT_ATTR_FD | IBV_XRCD_INIT_ATTR_OFLAGS;
xrcd_init_attr.fd = ctx->fd;
xrcd_init_attr.oflags = O_CREAT ;
ctx->xrc_domain = ibv_open_xrcd(ctx->context,&xrcd_init_attr);
if (ctx->xrc_domain == NULL) {
fprintf(stderr,"Error opening XRC domain\n");
return FAILURE;
}
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int ctx_xrc_srq_create(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
struct ibv_srq_init_attr_ex srq_init_attr;
memset(&srq_init_attr, 0, sizeof(srq_init_attr));
srq_init_attr.attr.max_wr = user_param->rx_depth;
srq_init_attr.attr.max_sge = 1;
srq_init_attr.comp_mask = IBV_SRQ_INIT_ATTR_TYPE | IBV_SRQ_INIT_ATTR_XRCD | IBV_SRQ_INIT_ATTR_CQ | IBV_SRQ_INIT_ATTR_PD;
srq_init_attr.srq_type = IBV_SRQT_XRC;
srq_init_attr.xrcd = ctx->xrc_domain;
if(user_param->verb == SEND)
srq_init_attr.cq = ctx->recv_cq;
else
srq_init_attr.cq = ctx->send_cq;
srq_init_attr.pd = ctx->pd;
ctx->srq = ibv_create_srq_ex(ctx->context, &srq_init_attr);
if (ctx->srq == NULL) {
fprintf(stderr, "Couldn't open XRC SRQ\n");
return FAILURE;
}
return 0;
}
/******************************************************************************
*
******************************************************************************/
static struct ibv_qp *ctx_xrc_qp_create(struct pingpong_context *ctx,
struct perftest_parameters *user_param,
int qp_index)
{
struct ibv_qp* qp = NULL;
int num_of_qps = user_param->num_of_qps / 2;
#ifdef HAVE_VERBS_EXP
struct ibv_exp_qp_init_attr qp_init_attr;
#else
struct ibv_qp_init_attr_ex qp_init_attr;
#endif
memset(&qp_init_attr, 0, sizeof(qp_init_attr));
if ( (!(user_param->duplex || user_param->tst == LAT) && (user_param->machine == SERVER) )
|| ((user_param->duplex || user_param->tst == LAT) && (qp_index >= num_of_qps))) {
qp_init_attr.qp_type = IBV_QPT_XRC_RECV;
qp_init_attr.comp_mask = IBV_QP_INIT_ATTR_XRCD;
qp_init_attr.xrcd = ctx->xrc_domain;
qp_init_attr.cap.max_recv_wr = user_param->rx_depth;
qp_init_attr.cap.max_recv_sge = 1;
qp_init_attr.cap.max_inline_data = user_param->inline_size;
} else {
qp_init_attr.qp_type = IBV_QPT_XRC_SEND;
qp_init_attr.send_cq = ctx->send_cq;
qp_init_attr.cap.max_send_wr = user_param->tx_depth;
qp_init_attr.cap.max_send_sge = 1;
qp_init_attr.comp_mask = IBV_QP_INIT_ATTR_PD;
qp_init_attr.pd = ctx->pd;
qp_init_attr.cap.max_inline_data = user_param->inline_size;
}
#ifdef HAVE_ACCL_VERBS
if (user_param->use_res_domain) {
qp_init_attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_RES_DOMAIN;
qp_init_attr.res_domain = ctx->res_domain;
}
#endif
#ifdef HAVE_VERBS_EXP
qp = ibv_exp_create_qp(ctx->context, &qp_init_attr);
#else
qp = ibv_create_qp_ex(ctx->context, &qp_init_attr);
#endif
return qp;
}
#endif
#ifdef HAVE_DC
/******************************************************************************
*
******************************************************************************/
static int ctx_dc_tgt_create(struct pingpong_context *ctx,struct perftest_parameters *user_param,int dct_index)
{
struct ibv_exp_device_attr dattr;
int err;
int num_of_qps = user_param->num_of_qps;
int num_of_qps_per_port = user_param->num_of_qps / 2;
int port_num;
memset(&dattr,0,sizeof(struct ibv_exp_device_attr));
/* in dc with bidirectional,
* there are send qps and recv qps. the actual number of send/recv qps
* is num_of_qps / 2.
*/
if (user_param->duplex || user_param->tst == LAT) {
num_of_qps /= 2;
num_of_qps_per_port = num_of_qps / 2;
}
/* first half of qps are for ib_port and second half are for ib_port2
* in dc with bidirectional, the first half of qps are dc_ini qps and
* the second half are dc_tgts . the first half of the send/recv qps
* are for ib_port1 and the second half are for ib_port2
*/
if (user_param->dualport == ON && (dct_index % num_of_qps >= num_of_qps_per_port))
port_num = user_param->ib_port2;
else
port_num = user_param->ib_port;
dattr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
IBV_EXP_DEVICE_DC_RD_REQ |
IBV_EXP_DEVICE_DC_RD_RES;
err = ibv_exp_query_device(ctx->context, &dattr);
if (err) {
printf("couldn't query device extended attributes\n");
return -1;
} else {
if (!(dattr.comp_mask & IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS)) {
printf("no extended capability flgas\n");
return -1;
}
if (!(dattr.exp_device_cap_flags & IBV_EXP_DEVICE_DC_TRANSPORT)) {
printf("DC transport not enabled\n");
return -1;
}
if (!(dattr.comp_mask & IBV_EXP_DEVICE_DC_RD_REQ)) {
printf("no report on max requestor rdma/atomic resources\n");
return -1;
}
if (!(dattr.comp_mask & IBV_EXP_DEVICE_DC_RD_RES)) {
printf("no report on max responder rdma/atomic resources\n");
return -1;
}
}
struct ibv_exp_dct_init_attr dctattr = {
.pd = ctx->pd,
.cq = (user_param->verb == SEND && (user_param->duplex || user_param->tst == LAT)) ? ctx->recv_cq : ctx->send_cq,
.srq = ctx->srq,
.dc_key = user_param->dct_key,
.port = port_num,
.access_flags = IBV_ACCESS_REMOTE_WRITE,
.min_rnr_timer = 2,
.tclass = 0,
.flow_label = 0,
.mtu = user_param->curr_mtu,
.pkey_index = user_param->pkey_index,
.gid_index = user_param->gid_index,
.hop_limit = 1,
.inline_size = user_param->inline_size,
};
ctx->dct[dct_index] = ibv_exp_create_dct(ctx->context, &dctattr);
if (!ctx->dct[dct_index]) {
printf("create dct failed\n");
return FAILURE;
}
struct ibv_exp_dct_attr dcqattr;
memset(&dcqattr,0,sizeof(struct ibv_exp_dct_attr));
err = ibv_exp_query_dct(ctx->dct[dct_index], &dcqattr);
if (err) {
printf("query dct failed\n");
return FAILURE;
} else if (dcqattr.dc_key != user_param->dct_key) {
printf("queried dckry (0x%llx) is different then provided at create (0x%llx)\n",
(unsigned long long)dcqattr.dc_key,
(unsigned long long)user_param->dct_key);
return FAILURE;
} else if (dcqattr.state != IBV_EXP_DCT_STATE_ACTIVE) {
printf("state is not active %d\n", dcqattr.state);
return FAILURE;
}
return 0;
}
#endif
#ifdef HAVE_RSS_EXP
static struct ibv_qp *ctx_rss_eth_qp_create(struct pingpong_context *ctx,struct perftest_parameters *user_param,int qp_index)
{
struct ibv_exp_qp_init_attr attr;
struct ibv_qp* qp = NULL;
memset(&attr, 0, sizeof(struct ibv_exp_qp_init_attr));
attr.send_cq = ctx->send_cq;
attr.recv_cq = ctx->recv_cq;
attr.cap.max_send_wr = user_param->tx_depth;
attr.cap.max_send_sge = MAX_SEND_SGE;
attr.cap.max_inline_data = user_param->inline_size;
attr.cap.max_recv_wr = user_param->rx_depth;
attr.cap.max_recv_sge = MAX_RECV_SGE;
attr.qp_type = IBV_QPT_RAW_PACKET;
attr.comp_mask = IBV_EXP_QP_INIT_ATTR_PD | IBV_EXP_QP_INIT_ATTR_QPG;
attr.pd = ctx->pd;
if (qp_index == 0) { /* rss parent */
#ifdef HAVE_VERBS_EXP
attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
#else
attr.qpg.qpg_type = IBV_QPG_PARENT;
#endif
attr.qpg.qpg_parent = NULL;
attr.qpg.parent_attrib.tss_child_count = 0;
attr.qpg.parent_attrib.rss_child_count = user_param->num_of_qps - 1;
} else { /* rss childs */
#ifdef HAVE_VERBS_EXP
attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
#else
attr.qpg.qpg_type = IBV_QPG_CHILD_RX;
#endif
attr.qpg.qpg_parent = ctx->qp[0];
}
qp = ibv_exp_create_qp(ctx->context,&attr);
return qp;
}
#endif
/******************************************************************************
*
******************************************************************************/
int check_add_port(char **service,int port,
const char *servername,
struct addrinfo *hints,
struct addrinfo **res)
{
int number;
if (asprintf(service,"%d", port) < 0) {
return FAILURE;
}
number = getaddrinfo(servername,*service,hints,res);
if (number < 0) {
fprintf(stderr, "%s for %s:%d\n", gai_strerror(number), servername, port);
return FAILURE;
}
return SUCCESS;
}
/******************************************************************************
*
******************************************************************************/
int create_rdma_resources(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
int is_udp_ps = user_param->connection_type == UD || user_param->connection_type == RawEth;
enum rdma_port_space port_space = (is_udp_ps) ? RDMA_PS_UDP : RDMA_PS_TCP;
struct rdma_cm_id **cm_id = (user_param->machine == CLIENT) ? &ctx->cm_id : &ctx->cm_id_control;
ctx->cm_channel = rdma_create_event_channel();
if (ctx->cm_channel == NULL) {
fprintf(stderr, " rdma_create_event_channel failed\n");
return FAILURE;
}
if (rdma_create_id(ctx->cm_channel,cm_id,NULL,port_space)) {
fprintf(stderr,"rdma_create_id failed\n");
return FAILURE;
}
return SUCCESS;
}
/******************************************************************************
*
******************************************************************************/
int destroy_rdma_resources(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
int ret;
if (user_param->machine == CLIENT) {
ret = rdma_destroy_id(ctx->cm_id);
} else {
ret = rdma_destroy_id(ctx->cm_id_control);
}
rdma_destroy_event_channel(ctx->cm_channel);
return ret;
}
/******************************************************************************
+ *
+ ******************************************************************************/
struct ibv_device* ctx_find_dev(const char *ib_devname)
{
int num_of_device;
struct ibv_device **dev_list;
struct ibv_device *ib_dev = NULL;
dev_list = ibv_get_device_list(&num_of_device);
if (num_of_device <= 0) {
fprintf(stderr," Did not detect devices \n");
fprintf(stderr," If device exists, check if driver is up\n");
return NULL;
}
if (!ib_devname) {
ib_dev = dev_list[0];
if (!ib_dev) {
fprintf(stderr, "No IB devices found\n");
exit (1);
}
} else {
for (; (ib_dev = *dev_list); ++dev_list)
if (!strcmp(ibv_get_device_name(ib_dev), ib_devname))
break;
if (!ib_dev)
fprintf(stderr, "IB device %s not found\n", ib_devname);
}
return ib_dev;
}
/******************************************************************************
*
******************************************************************************/
void alloc_ctx(struct pingpong_context *ctx,struct perftest_parameters *user_param)
{
uint64_t tarr_size;
int num_of_qps_factor;
ctx->cycle_buffer = user_param->cycle_buffer;
ctx->cache_line_size = user_param->cache_line_size;
ALLOCATE(user_param->port_by_qp, uint64_t, user_param->num_of_qps);
tarr_size = (user_param->noPeak) ? 1 : user_param->iters*user_param->num_of_qps;
ALLOCATE(user_param->tposted, cycles_t, tarr_size);
memset(user_param->tposted, 0, sizeof(cycles_t)*tarr_size);
if ((user_param->tst == LAT || user_param->tst == FS_RATE) && user_param->test_type == DURATION)
ALLOCATE(user_param->tcompleted, cycles_t, 1);
ALLOCATE(ctx->qp, struct ibv_qp*, user_param->num_of_qps);
ALLOCATE(ctx->mr, struct ibv_mr*, user_param->num_of_qps);
ALLOCATE(ctx->buf, void* , user_param->num_of_qps);
#ifdef HAVE_ACCL_VERBS
ALLOCATE(ctx->qp_burst_family, struct ibv_exp_qp_burst_family*, user_param->num_of_qps);
#endif
#ifdef HAVE_DC
if (user_param->connection_type == DC) {
#ifdef HAVE_VERBS_EXP
ALLOCATE(ctx->dct, struct ibv_exp_dct*, user_param->num_of_qps);
#else
ALLOCATE(ctx->dct, struct ibv_dct*, user_param->num_of_qps);
#endif
}
#endif
if ((user_param->tst == BW || user_param->tst == LAT_BY_BW) && (user_param->machine == CLIENT || user_param->duplex)) {
ALLOCATE(user_param->tcompleted,cycles_t,tarr_size);
memset(user_param->tcompleted, 0, sizeof(cycles_t)*tarr_size);
ALLOCATE(ctx->my_addr,uint64_t,user_param->num_of_qps);
ALLOCATE(ctx->rem_addr,uint64_t,user_param->num_of_qps);
ALLOCATE(ctx->scnt,uint64_t,user_param->num_of_qps);
ALLOCATE(ctx->ccnt,uint64_t,user_param->num_of_qps);
memset(ctx->scnt, 0, user_param->num_of_qps * sizeof (uint64_t));
memset(ctx->ccnt, 0, user_param->num_of_qps * sizeof (uint64_t));
} else if ((user_param->tst == BW || user_param->tst == LAT_BY_BW)
&& user_param->verb == SEND && user_param->machine == SERVER) {
ALLOCATE(ctx->my_addr, uint64_t, user_param->num_of_qps);
ALLOCATE(user_param->tcompleted, cycles_t, 1);
} else if (user_param->tst == FS_RATE && user_param->test_type == ITERATIONS) {
ALLOCATE(user_param->tcompleted, cycles_t, tarr_size);
memset(user_param->tcompleted, 0, sizeof(cycles_t) * tarr_size);
}
if (user_param->machine == CLIENT || user_param->tst == LAT || user_param->duplex) {
ALLOCATE(ctx->sge_list, struct ibv_sge,user_param->num_of_qps * user_param->post_list);
#ifdef HAVE_VERBS_EXP
ALLOCATE(ctx->exp_wr, struct ibv_exp_send_wr, user_param->num_of_qps * user_param->post_list);
#endif
ALLOCATE(ctx->wr, struct ibv_send_wr, user_param->num_of_qps * user_param->post_list);
if ((user_param->verb == SEND && user_param->connection_type == UD ) || user_param->connection_type == DC) {
ALLOCATE(ctx->ah, struct ibv_ah*, user_param->num_of_qps);
}
}
if (user_param->verb == SEND && (user_param->tst == LAT || user_param->machine == SERVER || user_param->duplex)) {
ALLOCATE(ctx->recv_sge_list,struct ibv_sge,user_param->num_of_qps);
ALLOCATE(ctx->rwr,struct ibv_recv_wr,user_param->num_of_qps);
ALLOCATE(ctx->rx_buffer_addr,uint64_t,user_param->num_of_qps);
}
if (user_param->mac_fwd == ON )
ctx->cycle_buffer = user_param->size * user_param->rx_depth;
ctx->size = user_param->size;
num_of_qps_factor = (user_param->mr_per_qp) ? 1 : user_param->num_of_qps;
/* holds the size of maximum between msg size and cycle buffer,
* aligned to cache line,
* it is multiply by 2 for send and receive
* with reference to number of flows and number of QPs */
ctx->buff_size = INC(BUFF_SIZE(ctx->size, ctx->cycle_buffer),
ctx->cache_line_size) * 2 * num_of_qps_factor * user_param->flows;
ctx->send_qp_buff_size = ctx->buff_size / num_of_qps_factor / 2;
ctx->flow_buff_size = ctx->send_qp_buff_size / user_param->flows;
user_param->buff_size = ctx->buff_size;
if (user_param->connection_type == UD)
ctx->buff_size += ctx->cache_line_size;
}
/******************************************************************************
*
******************************************************************************/
int destroy_ctx(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
int i, first, dereg_counter;
int test_result = 0;
int num_of_qps = user_param->num_of_qps;
if (user_param->wait_destroy) {
printf(" Waiting %u seconds before releasing resources...\n",
user_param->wait_destroy);
sleep(user_param->wait_destroy);
}
dereg_counter = (user_param->mr_per_qp) ? user_param->num_of_qps : 1;
if (user_param->work_rdma_cm == ON)
rdma_disconnect(ctx->cm_id);
/* in dc with bidirectional,
* there are send qps and recv qps. the actual number of send/recv qps
* is num_of_qps / 2.
*/
if (user_param->duplex || user_param->tst == LAT) {
num_of_qps /= 2;
}
/* RSS parent should be last */
if (user_param->use_rss)
first = 1;
else
first = 0;
for (i = first; i < user_param->num_of_qps; i++) {
if (( (user_param->connection_type == DC && !((!(user_param->duplex || user_param->tst == LAT) && (user_param->machine == SERVER) )
|| ((user_param->duplex || user_param->tst == LAT) && (i >= num_of_qps)))) ||
user_param->connection_type == UD) && (user_param->tst == LAT || user_param->machine == CLIENT || user_param->duplex)) {
if (ibv_destroy_ah(ctx->ah[i])) {
fprintf(stderr, "Failed to destroy AH\n");
test_result = 1;
}
}
#ifdef HAVE_DC
if (user_param->connection_type == DC && ((!(user_param->duplex || user_param->tst == LAT)
&& (user_param->machine == SERVER)) || ((user_param->duplex || user_param->tst == LAT) && (i >= num_of_qps)))) {
if (ibv_exp_destroy_dct(ctx->dct[i])) {
fprintf(stderr, "Failed to destroy dct\n");
test_result = 1;
}
if ( i == user_param->num_of_qps -1 )
return test_result;
} else
#endif
if (ibv_destroy_qp(ctx->qp[i])) {
fprintf(stderr, "Couldn't destroy QP - %s\n", strerror(errno));
test_result = 1;
}
}
if (user_param->use_rss) {
if (user_param->connection_type == UD && (user_param->tst == LAT || user_param->machine == CLIENT || user_param->duplex)) {
if (ibv_destroy_ah(ctx->ah[0])) {
fprintf(stderr, "Failed to destroy AH\n");
test_result = 1;
}
}
if (ibv_destroy_qp(ctx->qp[0])) {
fprintf(stderr, "Couldn't destroy QP - %s\n", strerror(errno));
test_result = 1;
}
}
if (user_param->srq_exists) {
if (ibv_destroy_srq(ctx->srq)) {
fprintf(stderr, "Couldn't destroy SRQ\n");
test_result = 1;
}
}
#ifdef HAVE_XRCD
if (user_param->use_xrc) {
if (ibv_close_xrcd(ctx->xrc_domain)) {
fprintf(stderr, "Couldn't destroy XRC domain\n");
test_result = 1;
}
if (ctx->fd >= 0 && close(ctx->fd)) {
fprintf(stderr, "Couldn't close the file for the XRC Domain\n");
test_result = 1;
}
}
#endif
if (ibv_destroy_cq(ctx->send_cq)) {
fprintf(stderr, "Failed to destroy CQ - %s\n", strerror(errno));
test_result = 1;
}
if (user_param->verb == SEND && (user_param->tst == LAT || user_param->machine == SERVER || user_param->duplex || (ctx->channel)) ) {
if (!(user_param->connection_type == DC && user_param->machine == SERVER)) {
if (ibv_destroy_cq(ctx->recv_cq)) {
fprintf(stderr, "Failed to destroy CQ - %s\n", strerror(errno));
test_result = 1;
}
}
}
for (i = 0; i < dereg_counter; i++) {
if (ibv_dereg_mr(ctx->mr[i])) {
fprintf(stderr, "Failed to deregister MR #%d\n", i+1);
test_result = 1;
}
}
if (user_param->verb == SEND && user_param->work_rdma_cm == ON && ctx->send_rcredit) {
if (ibv_dereg_mr(ctx->credit_mr)) {
fprintf(stderr, "Failed to deregister send credit MR\n");
test_result = 1;
}
free(ctx->ctrl_buf);
free(ctx->ctrl_sge_list);
free(ctx->ctrl_wr);
}
if (ibv_dealloc_pd(ctx->pd)) {
fprintf(stderr, "Failed to deallocate PD - %s\n", strerror(errno));
test_result = 1;
}
if (ctx->channel) {
if (ibv_destroy_comp_channel(ctx->channel)) {
fprintf(stderr, "Failed to close event channel\n");
test_result = 1;
}
}
if (user_param->use_rdma_cm == OFF) {
if (ibv_close_device(ctx->context)) {
fprintf(stderr, "Failed to close device context\n");
test_result = 1;
}
}
#ifdef HAVE_CUDA
if (user_param->use_cuda) {
pp_free_gpu(ctx);
}
else
#endif
if (user_param->mmap_file != NULL) {
pp_free_mmap(ctx);
} else if (ctx->is_contig_supported == FAILURE) {
for (i = 0; i < dereg_counter; i++) {
if (user_param->use_hugepages) {
shmdt(ctx->buf[i]);
} else {
free(ctx->buf[i]);
}
}
}
free(ctx->qp);
if ((user_param->tst == BW || user_param->tst == LAT_BY_BW ) && (user_param->machine == CLIENT || user_param->duplex)) {
free(user_param->tposted);
free(user_param->tcompleted);
free(ctx->my_addr);
free(ctx->rem_addr);
free(ctx->scnt);
free(ctx->ccnt);
}
else if ((user_param->tst == BW || user_param->tst == LAT_BY_BW ) && user_param->verb == SEND && user_param->machine == SERVER) {
free(user_param->tposted);
free(user_param->tcompleted);
free(ctx->my_addr);
}
if (user_param->machine == CLIENT || user_param->tst == LAT || user_param->duplex) {
free(ctx->sge_list);
free(ctx->wr);
#ifdef HAVE_VERBS_EXP
free(ctx->exp_wr);
#endif
}
if (user_param->verb == SEND && (user_param->tst == LAT || user_param->machine == SERVER || user_param->duplex)) {
free(ctx->rx_buffer_addr);
free(ctx->recv_sge_list);
free(ctx->rwr);
}
return test_result;
}
/******************************************************************************
*
******************************************************************************/
#ifdef HAVE_VERBS_EXP
static int check_inline_recv_support(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
struct ibv_exp_device_attr dattr;
int ret = 0;
memset(&dattr, 0, sizeof(dattr));
dattr.comp_mask |= IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
ret = ibv_exp_query_device(ctx->context, &dattr);
if (ret) {
printf(" Couldn't query device for inline-receive capabilities.\n");
} else if (!(dattr.comp_mask & IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ)) {
printf(" Inline-receive not supported by driver.\n");
ret = 1;
} else if (dattr.inline_recv_sz < user_param->inline_recv_size) {
printf(" Max inline-receive(%d) < Requested inline-receive(%d).\n",
dattr.inline_recv_sz, user_param->inline_recv_size);
}
return ret;
}
#endif
/******************************************************************************
*
******************************************************************************/
#if defined HAVE_EX_ODP || defined HAVE_EXP_ODP
static int check_odp_support(struct pingpong_context *ctx)
{
#ifdef HAVE_EX_ODP
struct ibv_device_attr_ex dattr;
int odp_support_send = IBV_ODP_SUPPORT_SEND;
int odp_support_recv = IBV_ODP_SUPPORT_RECV;
int ret = ibv_query_device_ex(ctx->context, NULL, &dattr);
#elif defined HAVE_EXP_ODP
struct ibv_exp_device_attr dattr;
int ret = ibv_exp_query_device(ctx->context, &dattr);
int odp_support_send = IBV_EXP_ODP_SUPPORT_SEND;
int odp_support_recv = IBV_EXP_ODP_SUPPORT_RECV;
#endif
if (ret) {
fprintf(stderr, " Couldn't query device for on-demand paging capabilities.\n");
return 0;
} else if (!(dattr.odp_caps.per_transport_caps.rc_odp_caps & odp_support_send)) {
fprintf(stderr, " Send is not supported for RC transport.\n");
return 0;
} else if (!(dattr.odp_caps.per_transport_caps.rc_odp_caps & odp_support_recv)) {
fprintf(stderr, " Receive is not supported for RC transport.\n");
return 0;
}
return 1;
}
#endif
/******************************************************************************
*
******************************************************************************/
int create_reg_cqs(struct pingpong_context *ctx,
struct perftest_parameters *user_param,
int tx_buffer_depth, int need_recv_cq)
{
ctx->send_cq = ibv_create_cq(ctx->context,tx_buffer_depth *
user_param->num_of_qps, NULL, ctx->channel, user_param->eq_num);
if (!ctx->send_cq) {
fprintf(stderr, "Couldn't create CQ\n");
return FAILURE;
}
if (need_recv_cq) {
ctx->recv_cq = ibv_create_cq(ctx->context,user_param->rx_depth *
user_param->num_of_qps, NULL, ctx->channel, user_param->eq_num);
if (!ctx->recv_cq) {
fprintf(stderr, "Couldn't create a receiver CQ\n");
return FAILURE;