-
Notifications
You must be signed in to change notification settings - Fork 368
/
pingpong.c
2244 lines (1851 loc) · 53.2 KB
/
pingpong.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
/*
* Copyright (c) 2013-2015 Intel Corporation. All rights reserved.
* Copyright (c) 2014-2016, Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015 Los Alamos Nat. Security, LLC. All rights reserved.
* Copyright (c) 2016 Cray Inc. All rights reserved.
*
* This software is available to you under the BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 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.
*/
#include <config.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <assert.h>
#include <getopt.h>
#include <inttypes.h>
#include <netdb.h>
#include <poll.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <ofi_osd.h>
#include <rdma/fabric.h>
#include <rdma/fi_cm.h>
#include <rdma/fi_domain.h>
#include <rdma/fi_endpoint.h>
#include <rdma/fi_eq.h>
#include <rdma/fi_errno.h>
#include <rdma/fi_tagged.h>
#ifndef OFI_MR_BASIC_MAP
#define OFI_MR_BASIC_MAP (FI_MR_ALLOCATED | FI_MR_PROV_KEY | FI_MR_VIRT_ADDR)
#endif
#ifndef PP_FIVERSION
#define PP_FIVERSION FI_VERSION(1, 5)
#endif
static const uint64_t TAG = 1234;
enum precision {
NANO = 1,
MICRO = 1000,
MILLI = 1000000,
};
enum {
PP_OPT_ACTIVE = 1 << 0,
PP_OPT_ITER = 1 << 1,
PP_OPT_SIZE = 1 << 2,
PP_OPT_VERIFY_DATA = 1 << 3,
};
struct pp_opts {
uint16_t src_port;
uint16_t dst_port;
char *dst_addr;
int iterations;
int transfer_size;
int sizes_enabled;
int options;
};
#define PP_SIZE_MAX_POWER_TWO 22
#define PP_MAX_DATA_MSG \
((1 << PP_SIZE_MAX_POWER_TWO) + (1 << (PP_SIZE_MAX_POWER_TWO - 1)))
#define PP_STR_LEN 32
#define PP_MAX_CTRL_MSG 64
#define PP_CTRL_BUF_LEN 64
#define PP_MR_KEY 0xC0DE
#define INTEG_SEED 7
#define PP_ENABLE_ALL (~0)
#define PP_DEFAULT_SIZE (1 << 0)
#define PP_MSG_CHECK_PORT_OK "port ok"
#define PP_MSG_LEN_PORT 5
#define PP_MSG_CHECK_CNT_OK "cnt ok"
#define PP_MSG_LEN_CNT 10
#define PP_MSG_SYNC_Q "q"
#define PP_MSG_SYNC_A "a"
#define PP_PRINTERR(call, retv) \
fprintf(stderr, "%s(): %s:%-4d, ret=%d (%s)\n", call, __FILE__, \
__LINE__, (int)retv, fi_strerror((int) -retv))
#define PP_ERR(fmt, ...) \
fprintf(stderr, "[%s] %s:%-4d: " fmt "\n", "error", __FILE__, \
__LINE__, ##__VA_ARGS__)
int pp_debug;
#define PP_DEBUG(fmt, ...) \
do { \
if (pp_debug) { \
fprintf(stderr, "[%s] %s:%-4d: " fmt, "debug", \
__FILE__, __LINE__, ##__VA_ARGS__); \
} \
} while (0)
#define PP_CLOSE_FID(fd) \
do { \
int ret; \
if ((fd)) { \
ret = fi_close(&(fd)->fid); \
if (ret) \
PP_ERR("fi_close (%d) fid %d", ret, \
(int)(fd)->fid.fclass); \
fd = NULL; \
} \
} while (0)
#ifndef MAX
#define MAX(a, b) \
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a > _b ? _a : _b; \
})
#endif
#ifndef MIN
#define MIN(a, b) \
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a < _b ? _a : _b; \
})
#endif
struct ct_pingpong {
struct fi_info *fi_pep, *fi, *hints;
struct fid_fabric *fabric;
struct fid_domain *domain;
struct fid_pep *pep;
struct fid_ep *ep;
struct fid_cq *txcq, *rxcq;
struct fid_mr *mr;
struct fid_av *av;
struct fid_eq *eq;
struct fid_mr no_mr;
void *tx_ctx_ptr, *rx_ctx_ptr;
struct fi_context tx_ctx[2], rx_ctx[2];
uint64_t remote_cq_data;
uint64_t tx_seq, rx_seq, tx_cq_cntr, rx_cq_cntr;
fi_addr_t remote_fi_addr;
void *buf, *tx_buf, *rx_buf;
size_t buf_size, tx_size, rx_size, msg_prefix_size;
int timeout_sec;
uint64_t start, end;
struct fi_av_attr av_attr;
struct fi_eq_attr eq_attr;
struct fi_cq_attr cq_attr;
struct pp_opts opts;
long cnt_ack_msg;
SOCKET ctrl_connfd;
char ctrl_buf[PP_CTRL_BUF_LEN + 1];
void *rem_name;
};
static const char integ_alphabet[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* Size does not include trailing new line */
static const int integ_alphabet_length =
(sizeof(integ_alphabet) / sizeof(*integ_alphabet)) - 1;
/*******************************************************************************
* Utils
******************************************************************************/
static uint64_t pp_gettime_us(void)
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec * 1000000 + now.tv_usec;
}
static long parse_ulong(char *str, long max)
{
long ret;
char *end;
errno = 0;
ret = strtol(str, &end, 10);
if (*end != '\0' || errno != 0) {
if (errno == 0)
ret = -EINVAL;
else
ret = -errno;
fprintf(stderr, "Error parsing \"%s\": %s\n", str,
strerror(-ret));
return ret;
}
if ((ret < 0) || (max > 0 && ret > max)) {
ret = -ERANGE;
fprintf(stderr, "Error parsing \"%s\": %s\n", str,
strerror(-ret));
return ret;
}
return ret;
}
static void pp_banner_fabric_info(struct ct_pingpong *ct)
{
PP_DEBUG(
"Running pingpong test with the %s provider and %s endpoint type\n",
ct->fi->fabric_attr->prov_name,
fi_tostr(&ct->fi->ep_attr->type, FI_TYPE_EP_TYPE));
PP_DEBUG("%s", fi_tostr(ct->fi->fabric_attr, FI_TYPE_FABRIC_ATTR));
PP_DEBUG("%s", fi_tostr(ct->fi->domain_attr, FI_TYPE_DOMAIN_ATTR));
PP_DEBUG("%s", fi_tostr(ct->fi->ep_attr, FI_TYPE_EP_ATTR));
}
static void pp_banner_options(struct ct_pingpong *ct)
{
char size_msg[50];
char iter_msg[50];
struct pp_opts opts = ct->opts;
if ((opts.dst_addr == NULL) || (opts.dst_addr[0] == '\0'))
opts.dst_addr = "None";
if (opts.sizes_enabled == PP_ENABLE_ALL)
snprintf(size_msg, 50, "%s", "All sizes");
else if (opts.options & PP_OPT_SIZE)
snprintf(size_msg, 50, "selected size = %d",
opts.transfer_size);
else
snprintf(size_msg, 50, "default size = %d",
opts.transfer_size);
if (opts.options & PP_OPT_ITER)
snprintf(iter_msg, 50, "selected iterations: %d",
opts.iterations);
else {
snprintf(iter_msg, 50, "default iterations: %d",
opts.iterations);
}
PP_DEBUG(" * PingPong options:\n");
PP_DEBUG(" - %-20s: [%" PRIu16 "]\n", "src_port", opts.src_port);
PP_DEBUG(" - %-20s: [%s]\n", "dst_addr", opts.dst_addr);
PP_DEBUG(" - %-20s: [%" PRIu16 "]\n", "dst_port", opts.dst_port);
PP_DEBUG(" - %-20s: %s\n", "sizes_enabled", size_msg);
PP_DEBUG(" - %-20s: %s\n", "iterations", iter_msg);
if (ct->hints->fabric_attr->prov_name)
PP_DEBUG(" - %-20s: %s\n", "provider",
ct->hints->fabric_attr->prov_name);
if (ct->hints->domain_attr->name)
PP_DEBUG(" - %-20s: %s\n", "domain",
ct->hints->domain_attr->name);
}
/*******************************************************************************
* Control Messaging
******************************************************************************/
static int pp_getaddrinfo(char *name, uint16_t port, struct addrinfo **results)
{
int ret;
const char *err_msg;
char port_s[6];
struct addrinfo hints = {
.ai_family = AF_INET, /* IPv4 */
.ai_socktype = SOCK_STREAM, /* TCP socket */
.ai_protocol = IPPROTO_TCP, /* Any protocol */
.ai_flags = AI_NUMERICSERV /* numeric port is used */
};
snprintf(port_s, 6, "%" PRIu16, port);
ret = getaddrinfo(name, port_s, &hints, results);
if (ret != 0) {
err_msg = gai_strerror(ret);
PP_ERR("getaddrinfo : %s", err_msg);
ret = -EXIT_FAILURE;
goto out;
}
ret = EXIT_SUCCESS;
out:
return ret;
}
static int pp_ctrl_init_client(struct ct_pingpong *ct)
{
struct sockaddr_in in_addr = {0};
struct addrinfo *results;
struct addrinfo *rp;
int errno_save;
int ret;
ret = pp_getaddrinfo(ct->opts.dst_addr, ct->opts.dst_port, &results);
if (ret)
return ret;
if (!results) {
PP_ERR("getaddrinfo returned NULL list");
return -EXIT_FAILURE;
}
for (rp = results; rp; rp = rp->ai_next) {
ct->ctrl_connfd = ofi_socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (ct->ctrl_connfd == INVALID_SOCKET) {
errno_save = ofi_sockerr();
continue;
}
if (ct->opts.src_port != 0) {
in_addr.sin_family = AF_INET;
in_addr.sin_port = htons(ct->opts.src_port);
in_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret =
bind(ct->ctrl_connfd, (struct sockaddr *)&in_addr,
sizeof(in_addr));
if (ret == -1) {
errno_save = ofi_sockerr();
ofi_close_socket(ct->ctrl_connfd);
continue;
}
}
ret = connect(ct->ctrl_connfd, rp->ai_addr, rp->ai_addrlen);
if (ret != -1)
break;
errno_save = ofi_sockerr();
ofi_close_socket(ct->ctrl_connfd);
}
if (!rp || ret == -1) {
ret = -errno_save;
ct->ctrl_connfd = -1;
PP_ERR("failed to connect: %s", strerror(errno_save));
} else {
PP_DEBUG("CLIENT: connected\n");
}
freeaddrinfo(results);
return ret;
}
static int pp_ctrl_init_server(struct ct_pingpong *ct)
{
struct sockaddr_in ctrl_addr = {0};
int optval = 1;
SOCKET listenfd;
int ret;
listenfd = ofi_socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == INVALID_SOCKET) {
ret = -ofi_sockerr();
PP_PRINTERR("socket", ret);
return ret;
}
ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const char *)&optval, sizeof(optval));
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("setsockopt(SO_REUSEADDR)", ret);
goto fail_close_socket;
}
ctrl_addr.sin_family = AF_INET;
ctrl_addr.sin_port = htons(ct->opts.src_port);
ctrl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(listenfd, (struct sockaddr *)&ctrl_addr,
sizeof(ctrl_addr));
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("bind", ret);
goto fail_close_socket;
}
ret = listen(listenfd, 10);
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("listen", ret);
goto fail_close_socket;
}
PP_DEBUG("SERVER: waiting for connection\n");
ct->ctrl_connfd = accept(listenfd, NULL, NULL);
if (ct->ctrl_connfd == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("accept", ret);
goto fail_close_socket;
}
ofi_close_socket(listenfd);
PP_DEBUG("SERVER: connected\n");
return ret;
fail_close_socket:
if (ct->ctrl_connfd != -1) {
ofi_close_socket(ct->ctrl_connfd);
ct->ctrl_connfd = -1;
}
if (listenfd != -1)
ofi_close_socket(listenfd);
return ret;
}
static int pp_ctrl_init(struct ct_pingpong *ct)
{
const uint32_t default_ctrl = 47592;
struct timeval tv = {
.tv_sec = 5
};
int ret;
PP_DEBUG("Initializing control messages\n");
if (ct->opts.dst_addr) {
if (ct->opts.dst_port == 0)
ct->opts.dst_port = default_ctrl;
ret = pp_ctrl_init_client(ct);
} else {
if (ct->opts.src_port == 0)
ct->opts.src_port = default_ctrl;
ret = pp_ctrl_init_server(ct);
}
if (ret)
return ret;
ret = setsockopt(ct->ctrl_connfd, SOL_SOCKET, SO_RCVTIMEO,
(const char *)&tv, sizeof(struct timeval));
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("setsockopt(SO_RCVTIMEO)", ret);
return ret;
}
PP_DEBUG("Control messages initialized\n");
return ret;
}
static int pp_ctrl_send(struct ct_pingpong *ct, char *buf, size_t size)
{
int ret, err;
ret = ofi_send_socket(ct->ctrl_connfd, buf, size, 0);
if (ret < 0) {
err = -ofi_sockerr();
PP_PRINTERR("ctrl/send", err);
return err;
}
if (ret == 0) {
err = -ECONNABORTED;
PP_ERR("ctrl/read: no data or remote connection closed");
return err;
}
return ret;
}
static int pp_ctrl_recv(struct ct_pingpong *ct, char *buf, size_t size)
{
int ret, err;
do {
PP_DEBUG("receiving\n");
ret = ofi_read_socket(ct->ctrl_connfd, buf, size);
} while (ret == -1 && OFI_SOCK_TRY_SND_RCV_AGAIN(ofi_sockerr()));
if (ret < 0) {
err = -ofi_sockerr();
PP_PRINTERR("ctrl/read", err);
return err;
}
if (ret == 0) {
err = -ECONNABORTED;
PP_ERR("ctrl/read: no data or remote connection closed");
return err;
}
return ret;
}
static int pp_send_name(struct ct_pingpong *ct, struct fid *endpoint)
{
void *local_name = NULL;
size_t addrlen = 0;
uint32_t len;
int ret;
PP_DEBUG("Fetching local address\n");
ret = fi_getname(endpoint, local_name, &addrlen);
if ((ret != -FI_ETOOSMALL) || (addrlen <= 0)) {
PP_ERR("fi_getname didn't return length\n");
return -EMSGSIZE;
}
local_name = calloc(1, addrlen);
if (!local_name) {
PP_ERR("Failed to allocate memory for the address\n");
return -ENOMEM;
}
ret = fi_getname(endpoint, local_name, &addrlen);
if (ret) {
PP_PRINTERR("fi_getname", ret);
goto fn;
}
PP_DEBUG("Sending name length\n");
len = htonl(addrlen);
ret = pp_ctrl_send(ct, (char *) &len, sizeof(len));
if (ret < 0)
goto fn;
PP_DEBUG("Sending address format\n");
if (ct->fi) {
ret = pp_ctrl_send(ct, (char *) &ct->fi->addr_format,
sizeof(ct->fi->addr_format));
} else {
ret = pp_ctrl_send(ct, (char *) &ct->fi_pep->addr_format,
sizeof(ct->fi_pep->addr_format));
}
if (ret < 0)
goto fn;
PP_DEBUG("Sending name\n");
ret = pp_ctrl_send(ct, local_name, addrlen);
PP_DEBUG("Sent name\n");
fn:
free(local_name);
return ret;
}
static int pp_recv_name(struct ct_pingpong *ct)
{
uint32_t len;
int ret;
PP_DEBUG("Receiving name length\n");
ret = pp_ctrl_recv(ct, (char *) &len, sizeof(len));
if (ret < 0)
return ret;
len = ntohl(len);
ct->rem_name = calloc(1, len);
if (!ct->rem_name) {
PP_ERR("Failed to allocate memory for the address\n");
return -ENOMEM;
}
PP_DEBUG("Receiving address format\n");
ret = pp_ctrl_recv(ct, (char *) &ct->hints->addr_format,
sizeof(ct->hints->addr_format));
if (ret < 0)
return ret;
PP_DEBUG("Receiving name\n");
ret = pp_ctrl_recv(ct, ct->rem_name, len);
if (ret < 0)
return ret;
PP_DEBUG("Received name\n");
ct->hints->dest_addr = calloc(1, len);
if (!ct->hints->dest_addr) {
PP_DEBUG("Failed to allocate memory for destination address\n");
return -ENOMEM;
}
/* fi_freeinfo will free the dest_addr field. */
memcpy(ct->hints->dest_addr, ct->rem_name, len);
ct->hints->dest_addrlen = len;
return 0;
}
static int pp_ctrl_finish(struct ct_pingpong *ct)
{
if (ct->ctrl_connfd != -1) {
ofi_close_socket(ct->ctrl_connfd);
ct->ctrl_connfd = -1;
}
return 0;
}
static int pp_ctrl_sync(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Syncing nodes\n");
if (ct->opts.dst_addr) {
snprintf(ct->ctrl_buf, sizeof(PP_MSG_SYNC_Q), "%s",
PP_MSG_SYNC_Q);
PP_DEBUG("CLIENT: syncing\n");
ret = pp_ctrl_send(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_Q));
PP_DEBUG("CLIENT: after send / ret=%d\n", ret);
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_SYNC_Q)) {
PP_ERR("CLIENT: bad length of sent data (len=%d/%zu)",
ret, sizeof(PP_MSG_SYNC_Q));
return -EBADMSG;
}
PP_DEBUG("CLIENT: syncing now\n");
ret = pp_ctrl_recv(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_A));
PP_DEBUG("CLIENT: after recv / ret=%d\n", ret);
if (ret < 0)
return ret;
if (strcmp(ct->ctrl_buf, PP_MSG_SYNC_A)) {
ct->ctrl_buf[PP_CTRL_BUF_LEN] = '\0';
PP_DEBUG("CLIENT: sync error while acking A: <%s> "
"(len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
return -EBADMSG;
}
PP_DEBUG("CLIENT: synced\n");
} else {
PP_DEBUG("SERVER: syncing\n");
ret = pp_ctrl_recv(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_Q));
PP_DEBUG("SERVER: after recv / ret=%d\n", ret);
if (ret < 0)
return ret;
if (strcmp(ct->ctrl_buf, PP_MSG_SYNC_Q)) {
ct->ctrl_buf[PP_CTRL_BUF_LEN] = '\0';
PP_DEBUG("SERVER: sync error while acking Q: <%s> "
"(len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
return -EBADMSG;
}
PP_DEBUG("SERVER: syncing now\n");
snprintf(ct->ctrl_buf, sizeof(PP_MSG_SYNC_A), "%s",
PP_MSG_SYNC_A);
ret = pp_ctrl_send(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_A));
PP_DEBUG("SERVER: after send / ret=%d\n", ret);
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_SYNC_A)) {
PP_ERR("SERVER: bad length of sent data (len=%d/%zu)",
ret, sizeof(PP_MSG_SYNC_A));
return -EBADMSG;
}
PP_DEBUG("SERVER: synced\n");
}
PP_DEBUG("Nodes synced\n");
return 0;
}
static int pp_ctrl_txrx_msg_count(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Exchanging ack count\n");
if (ct->opts.dst_addr) {
memset(&ct->ctrl_buf, '\0', PP_MSG_LEN_CNT + 1);
snprintf(ct->ctrl_buf, PP_MSG_LEN_CNT + 1, "%ld",
ct->cnt_ack_msg);
PP_DEBUG("CLIENT: sending count = <%s> (len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
ret = pp_ctrl_send(ct, ct->ctrl_buf, PP_MSG_LEN_CNT);
if (ret < 0)
return ret;
if (ret < PP_MSG_LEN_CNT) {
PP_ERR("CLIENT: bad length of sent data (len=%d/%d)",
ret, PP_MSG_LEN_CNT);
return -EBADMSG;
}
PP_DEBUG("CLIENT: sent count\n");
ret =
pp_ctrl_recv(ct, ct->ctrl_buf, sizeof(PP_MSG_CHECK_CNT_OK));
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_CHECK_CNT_OK)) {
PP_ERR(
"CLIENT: bad length of received data (len=%d/%zu)",
ret, sizeof(PP_MSG_CHECK_CNT_OK));
return -EBADMSG;
}
if (strcmp(ct->ctrl_buf, PP_MSG_CHECK_CNT_OK)) {
PP_DEBUG("CLIENT: error while server acking the count: "
"<%s> (len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
return ret;
}
PP_DEBUG("CLIENT: count acked by server\n");
} else {
memset(&ct->ctrl_buf, '\0', PP_MSG_LEN_CNT + 1);
PP_DEBUG("SERVER: receiving count\n");
ret = pp_ctrl_recv(ct, ct->ctrl_buf, PP_MSG_LEN_CNT);
if (ret < 0)
return ret;
if (ret < PP_MSG_LEN_CNT) {
PP_ERR(
"SERVER: bad length of received data (len=%d/%d)",
ret, PP_MSG_LEN_CNT);
return -EBADMSG;
}
ct->cnt_ack_msg = parse_ulong(ct->ctrl_buf, -1);
if (ct->cnt_ack_msg < 0)
return ret;
PP_DEBUG("SERVER: received count = <%ld> (len=%zu)\n",
ct->cnt_ack_msg, strlen(ct->ctrl_buf));
snprintf(ct->ctrl_buf, sizeof(PP_MSG_CHECK_CNT_OK), "%s",
PP_MSG_CHECK_CNT_OK);
ret =
pp_ctrl_send(ct, ct->ctrl_buf, sizeof(PP_MSG_CHECK_CNT_OK));
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_CHECK_CNT_OK)) {
PP_ERR(
"CLIENT: bad length of received data (len=%d/%zu)",
ret, sizeof(PP_MSG_CHECK_CNT_OK));
return -EBADMSG;
}
PP_DEBUG("SERVER: acked count to client\n");
}
PP_DEBUG("Ack count exchanged\n");
return 0;
}
/*******************************************************************************
* Options
******************************************************************************/
static inline void pp_start(struct ct_pingpong *ct)
{
PP_DEBUG("Starting test chrono\n");
ct->opts.options |= PP_OPT_ACTIVE;
ct->start = pp_gettime_us();
}
static inline void pp_stop(struct ct_pingpong *ct)
{
ct->end = pp_gettime_us();
ct->opts.options &= ~PP_OPT_ACTIVE;
PP_DEBUG("Stopped test chrono\n");
}
static inline int pp_check_opts(struct ct_pingpong *ct, uint64_t flags)
{
return (ct->opts.options & flags) == flags;
}
/*******************************************************************************
* Data Verification
******************************************************************************/
static void pp_fill_buf(void *buf, int size)
{
char *msg_buf;
int msg_index;
static unsigned int iter;
int i;
msg_index = ((iter++) * INTEG_SEED) % integ_alphabet_length;
msg_buf = (char *)buf;
for (i = 0; i < size; i++) {
PP_DEBUG("index=%d msg_index=%d\n", i, msg_index);
msg_buf[i] = integ_alphabet[msg_index++];
if (msg_index >= integ_alphabet_length)
msg_index = 0;
}
}
static int pp_check_buf(void *buf, int size)
{
char *recv_data;
char c;
static unsigned int iter;
int msg_index;
int i;
PP_DEBUG("Verifying buffer content\n");
msg_index = ((iter++) * INTEG_SEED) % integ_alphabet_length;
recv_data = (char *)buf;
for (i = 0; i < size; i++) {
c = integ_alphabet[msg_index++];
if (msg_index >= integ_alphabet_length)
msg_index = 0;
if (c != recv_data[i]) {
PP_DEBUG("index=%d msg_index=%d expected=%d got=%d\n",
i, msg_index, c, recv_data[i]);
break;
}
}
if (i != size) {
PP_DEBUG("Finished veryfing buffer: content is corrupted\n");
printf("Error at iteration=%d size=%d byte=%d\n", iter, size,
i);
return 1;
}
PP_DEBUG("Buffer verified\n");
return 0;
}
/*******************************************************************************
* Error handling
******************************************************************************/
static void eq_readerr(struct fid_eq *eq)
{
struct fi_eq_err_entry eq_err;
int rd;
rd = fi_eq_readerr(eq, &eq_err, 0);
if (rd != sizeof(eq_err)) {
PP_PRINTERR("fi_eq_readerr", rd);
} else {
PP_ERR("eq_readerr: %s",
fi_eq_strerror(eq, eq_err.prov_errno, eq_err.err_data,
NULL, 0));
}
}
static void pp_process_eq_err(ssize_t rd, struct fid_eq *eq, const char *fn)
{
if (rd == -FI_EAVAIL)
eq_readerr(eq);
else
PP_PRINTERR(fn, rd);
}
/*******************************************************************************
* Test sizes
******************************************************************************/
static int generate_test_sizes(struct pp_opts *opts, size_t tx_size, int **sizes_)
{
int defaults[] = {64, 256, 1024, 4096, 65536, 1048576};
int power_of_two;
int half_up;
int n = 0;
int i;
int *sizes = NULL;
PP_DEBUG("Generating test sizes\n");
sizes = calloc(64, sizeof(*sizes));
if (sizes == NULL)
return 0;
*sizes_ = sizes;
if (opts->options & PP_OPT_SIZE) {
if (opts->transfer_size > tx_size)
return 0;
sizes[0] = opts->transfer_size;
n = 1;
} else if (opts->sizes_enabled != PP_ENABLE_ALL) {
for (i = 0; i < (sizeof(defaults) / sizeof(defaults[0])); i++) {
if (defaults[i] > tx_size)
break;
sizes[i] = defaults[i];
n++;
}
} else {
for (i = 0;; i++) {
power_of_two = (i == 0) ? 0 : (1 << i);
half_up =
(i == 0) ? 1 : power_of_two + (power_of_two / 2);
if (power_of_two > tx_size)
break;
sizes[i * 2] = power_of_two;
n++;
if (half_up > tx_size)
break;
sizes[(i * 2) + 1] = half_up;
n++;
}
}
PP_DEBUG("Generated %d test sizes\n", n);
return n;
}
/*******************************************************************************
* Performance output
******************************************************************************/
/* str must be an allocated buffer of PP_STR_LEN bytes */
static char *size_str(char *str, uint64_t size)
{
uint64_t base, fraction = 0;
char mag;
memset(str, '\0', PP_STR_LEN);
if (size >= (1 << 30)) {
base = 1 << 30;
mag = 'g';
} else if (size >= (1 << 20)) {
base = 1 << 20;
mag = 'm';
} else if (size >= (1 << 10)) {
base = 1 << 10;
mag = 'k';
} else {
base = 1;
mag = '\0';
}
if (size / base < 10)
fraction = (size % base) * 10 / base;
if (fraction)
snprintf(str, PP_STR_LEN, "%" PRIu64 ".%" PRIu64 "%c",
size / base, fraction, mag);
else
snprintf(str, PP_STR_LEN, "%" PRIu64 "%c", size / base, mag);
return str;
}
/* str must be an allocated buffer of PP_STR_LEN bytes */
static char *cnt_str(char *str, size_t size, uint64_t cnt)
{
if (cnt >= 1000000000)
snprintf(str, size, "%" PRIu64 "b", cnt / 1000000000);
else if (cnt >= 1000000)