forked from eclipse-cyclonedds/cyclonedds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddsperf.c
2779 lines (2543 loc) · 97 KB
/
ddsperf.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) 2019 to 2022 ZettaScale Technology and others
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
// v. 1.0 which is available at
// http://www.eclipse.org/org/documents/edl-v10.php.
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#define _ISOC99_SOURCE
#define _POSIX_PTHREAD_SEMANTICS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <signal.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <getopt.h>
#include "dds/dds.h"
#include "dds/ddsc/dds_statistics.h"
#include "ddsperf_types.h"
#include "async_listener.h"
#include "dds/ddsrt/process.h"
#include "dds/ddsrt/string.h"
#include "dds/ddsrt/sync.h"
#include "dds/ddsrt/sockets.h"
#include "dds/ddsrt/threads.h"
#include "dds/ddsrt/random.h"
#include "dds/ddsrt/avl.h"
#include "dds/ddsrt/fibheap.h"
#include "dds/ddsrt/atomics.h"
#include "cputime.h"
#include "netload.h"
#if !defined(_WIN32) && !defined(LWIP_SOCKET)
#include <errno.h>
#endif
#define UDATA_MAGIC "DDSPerf:"
#define UDATA_MAGIC_SIZE (sizeof (UDATA_MAGIC) - 1)
#define PINGPONG_RAWSIZE 20000
enum topicsel {
KS, /* KeyedSeq type: seq#, key, sequence-of-octet */
K32, /* Keyed32 type: seq#, key, array-of-24-octet (sizeof = 32) */
K256, /* Keyed256 type: seq#, key, array-of-248-octet (sizeof = 256) */
OU, /* OneULong type: seq# */
UK16, /* Unkeyed16, type: seq#, array-of-12-octet (sizeof = 16) */
UK1024,/* Unkeyed1024, type: seq#, array-of-1020-octet (sizeof = 1024) */
S16, /* Keyed, 16 octets, int64 junk, seq#, key */
S256, /* Keyed, 16 * S16, int64 junk, seq#, key */
S4k, /* Keyed, 16 * S256, int64 junk, seq#, key */
S32k /* Keyed, 4 * S4k, int64 junk, seq#, key */
};
enum submode {
SM_NONE, /* no subscriber at all */
SM_WAITSET, /* subscriber using a waitset */
SM_POLLING, /* ... using polling, sleeping for 1ms if no data */
SM_LISTENER /* ... using a DATA_AVAILABLE listener */
};
static const char *argv0;
static ddsrt_atomic_uint32_t termflag = DDSRT_ATOMIC_UINT32_INIT (0);
/* Domain participant, guard condition for termination, domain id */
static dds_entity_t dp;
static dds_instance_handle_t dp_handle;
static dds_entity_t termcond;
static dds_domainid_t did = DDS_DOMAIN_DEFAULT;
/* Readers for built-in topics to get discovery information */
static dds_entity_t rd_participants, rd_subscriptions, rd_publications;
/* Topics, readers, writers (except for pong writers: there are
many of those) */
static dds_entity_t tp_data, tp_ping, tp_pong, tp_stat;
static char tpname_data[32], tpname_ping[32], tpname_pong[32];
static dds_entity_t sub, pub, wr_data, wr_ping, wr_stat, rd_data, rd_ping, rd_pong, rd_stat;
/* Number of different key values to use (must be 1 for OU type) */
static unsigned nkeyvals = 1;
/* Topic type to use */
static enum topicsel topicsel = KS;
/* Data and ping/pong subscriber triggering modes */
static enum submode submode = SM_LISTENER;
static enum submode pingpongmode = SM_LISTENER;
/* Whether to show "sub" stats every second even when nothing happens */
static bool substat_every_second = false;
/* Whether to show extended statistics (currently just rexmit info) */
static bool extended_stats = false;
/* Size of the sequence in KeyedSeq type in bytes */
static uint32_t baggagesize = 0;
/* Whether or not to register instances prior to writing */
static bool register_instances = true;
/* Maximum run time in seconds */
static double dur = HUGE_VAL;
/* Minimum number of peers (if not met, exit status is 1) */
static uint32_t minmatch = 0;
/* Wait this long for MINMATCH peers before starting */
static double initmaxwait = 0;
/* Maximum time it may take to discover all MINMATCH peers */
static double maxwait = HUGE_VAL;
/* Number of participants for which all expected endpoints
have been matched (this includes the local participant
if ignorelocal is DDS_IGNORELOCAL_NONE) [protected by
disc_lock] */
static uint32_t matchcount = 0;
/* An error is always signalled if not all endpoints of a
participant have been discovered within a set amount of
time (5s, currently) [protected by disc_lock] */
static uint32_t matchtimeout = 0;
/* Data is published in bursts of this many samples */
static uint32_t burstsize = 1;
/* Whether to use reliable or best-effort readers/writers */
static bool reliable = true;
/* History depth for throughput data reader and writer; 0 is
KEEP_ALL, otherwise it is KEEP_LAST histdepth. Ping/pong
always uses KEEP_LAST 1. */
static int32_t histdepth = 0;
/* Publishing rate in Hz, HUGE_VAL means as fast as possible,
0 means no throughput data is published at all */
static double pub_rate;
/* Fraction of throughput data samples that double as a ping
message */
static uint32_t ping_frac = 0;
/* Setting for "ignore local" reader/writer QoS: whether or
not to ignore readers and writers in the same particiapnt
that would otherwise match */
static dds_ignorelocal_kind_t ignorelocal = DDS_IGNORELOCAL_PARTICIPANT;
/* Pinging interval for roundtrip testing, 0 means as fast as
possible, DDS_INFINITY means never */
static dds_duration_t ping_intv;
/* Number of times a new ping was sent before all expected
pongs had been received */
static uint32_t ping_timeouts = 0;
/* Maximum allowed increase in RSS between 2nd RSS sample and
final RSS sample: final one must be <=
init * (1 + rss_factor/100) + rss_term */
static bool rss_check = false;
static double rss_factor = 1;
static double rss_term = 0;
/* Minimum number of samples, minimum number of roundtrips to
declare the run a success */
static uint64_t min_received = 0;
static uint64_t min_roundtrips = 0;
/* Whether to gather/show latency information in "sub" mode */
static bool sublatency = false;
/* Event queue for processing discovery events (data available on
DCPSParticipant, subscription & publication matched)
asynchronously to avoid deadlocking on creating a reader from
within a subscription_matched event. */
struct async_listener *async_listener;
static ddsrt_mutex_t disc_lock;
/* Publisher statistics and lock protecting it */
struct hist {
unsigned nbins;
uint64_t binwidth;
uint64_t bin0; /* bins are [bin0,bin0+binwidth),[bin0+binwidth,bin0+2*binwidth) */
uint64_t binN; /* bin0 + nbins*binwidth */
uint64_t min, max; /* min and max observed since last reset */
uint64_t under, over; /* < bin0, >= binN */
uint64_t bins[];
};
static ddsrt_mutex_t pubstat_lock;
static struct hist *pubstat_hist;
struct latencystat {
int64_t min, max;
int64_t sum;
uint32_t cnt;
uint64_t totcnt;
int64_t *raw;
};
/* Subscriber statistics for tracking number of samples received
and lost per source */
struct eseq_stat {
/* totals */
uint64_t nrecv;
uint64_t nlost;
uint64_t nrecv_bytes;
uint32_t last_size;
/* stats printer state */
struct {
uint64_t nrecv;
uint64_t nlost;
uint64_t nrecv_bytes;
} ref[10];
unsigned refidx;
/* for gathering latency information in case the clocks are
sufficiently well synchronised (e.g., single machine or PTP) */
struct latencystat info;
};
struct eseq_admin {
ddsrt_mutex_t lock;
unsigned nkeys;
unsigned nph;
dds_instance_handle_t *ph;
dds_instance_handle_t *pph;
struct eseq_stat *stats;
uint32_t **eseq;
};
static struct eseq_admin eseq_admin;
/* Entry for mapping ping/data publication handle to pong writer */
struct subthread_arg_pongwr {
dds_instance_handle_t pubhandle;
dds_instance_handle_t pphandle;
dds_entity_t wr_pong;
};
/* Entry for mapping pong publication handle to latency statistics */
struct subthread_arg_pongstat {
dds_instance_handle_t pubhandle;
dds_instance_handle_t pphandle;
struct latencystat info;
};
/* Pong statistics is stored in n array of npongstat entries
[protected by pongstat_lock] */
static ddsrt_mutex_t pongstat_lock;
static uint32_t npongstat;
static struct subthread_arg_pongstat *pongstat;
/* All topics have a sequence number, this is the one of the
latest ping sent and the number of pongs received for that
sequence number. Also the time at which it was sent for
generating new ping messages in the case of loss of topology
changes, and a timestamp after which a warning is printed
when a new ping is published. [All protected by
pongwr_lock] */
static dds_time_t cur_ping_time;
static dds_time_t twarn_ping_timeout;
static uint32_t cur_ping_seq;
static uint32_t n_pong_seen;
/* Number of pongs expected for each ping [protected by
pongwr_lock] */
static uint32_t n_pong_expected;
/* Table mapping data and ping publication handles to writers
of pongs (one per participant in a unique partition so that
a participant only receives responses to its own pings) is
a simply array of npongwr entries [protected by pongwr_lock] */
static ddsrt_mutex_t pongwr_lock;
static uint32_t npongwr;
static struct subthread_arg_pongwr *pongwr;
/* Each subscriber thread gets its own not-quite-pre-allocated
set of samples (it does use a loan, but that loan gets reused) */
struct subthread_arg {
dds_entity_t rd;
uint32_t max_samples;
dds_sample_info_t *iseq;
void **mseq;
};
/* Type used for converting GUIDs to strings, used for generating
the per-participant partition names */
struct guidstr {
char str[36];
};
/* Endpoints that can be matched; all endpoints except for a data
subscriber always exist; the data subscriber is only created if
requested */
#define MM_RD_DATA 1u
#define MM_RD_PING 2u
#define MM_RD_PONG 4u
#define MM_WR_DATA 8u
#define MM_WR_PING 16u
#define MM_WR_PONG 32u
#define MM_ALL (2 * MM_WR_PONG - 1)
struct ppant {
ddsrt_avl_node_t avlnode; /* embedded AVL node for handle index */
ddsrt_fibheap_node_t fhnode; /* prio queue for timeout handling */
dds_instance_handle_t handle; /* participant instance handle */
dds_guid_t guid; /* participant GUID */
char *hostname; /* hostname is taken from user_data QoS */
uint32_t pid; /* pid is also taken from user_data QoS */
dds_time_t tdisc; /* time at which it was discovered */
dds_time_t tdeadline; /* by what time must unmatched be 0 */
uint32_t unmatched; /* expected but not yet detected endpoints */
};
static int cmp_instance_handle (const void *va, const void *vb)
{
const dds_instance_handle_t *a = va;
const dds_instance_handle_t *b = vb;
return (*a == *b) ? 0 : (*a < *b) ? -1 : 1;
}
/* AVL tree of ppant structures indexed on handle using cmp_instance_handle */
static ddsrt_avl_treedef_t ppants_td = DDSRT_AVL_TREEDEF_INITIALIZER (offsetof (struct ppant, avlnode), offsetof (struct ppant, handle), cmp_instance_handle, 0);
static ddsrt_avl_tree_t ppants;
/* Priority queue (Fibonacci heap) of ppant structures with tdeadline as key */
static int cmp_ppant_tdeadline (const void *va, const void *vb)
{
const struct ppant *a = va;
const struct ppant *b = vb;
return (a->tdeadline == b->tdeadline) ? 0 : (a->tdeadline < b->tdeadline) ? -1 : 1;
}
static ddsrt_fibheap_def_t ppants_to_match_fhd = DDSRT_FIBHEAPDEF_INITIALIZER (offsetof (struct ppant, fhnode), cmp_ppant_tdeadline);
static ddsrt_fibheap_t ppants_to_match;
/* Printing error messages: error2 is for DDS errors, error3 is for usage errors */
static void verrorx (int exitcode, const char *fmt, va_list ap) ddsrt_attribute_noreturn;
static void error2 (const char *fmt, ...) ddsrt_attribute_format_printf(1, 2) ddsrt_attribute_noreturn;
static void error3 (const char *fmt, ...) ddsrt_attribute_format_printf(1, 2) ddsrt_attribute_noreturn;
static void publication_matched_listener (dds_entity_t wr, const dds_publication_matched_status_t status, void *arg);
struct seq_keyval {
uint32_t seq;
int32_t keyval;
};
union data {
uint32_t seq;
struct seq_keyval seq_keyval;
KeyedSeq ks;
Keyed32 k32;
Keyed256 k256;
OneULong ou;
Unkeyed16 uk16;
Unkeyed1024 uk1024;
Struct16 s16;
Struct256 s256;
Struct4k s4k;
Struct32k s32k;
};
static void verrorx (int exitcode, const char *fmt, va_list ap)
{
vprintf (fmt, ap);
fflush (stdout);
exit (exitcode);
}
static void error2 (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
verrorx (2, fmt, ap);
}
static void error3 (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
verrorx (3, fmt, ap);
}
static char *make_guidstr (struct guidstr *buf, const dds_guid_t *guid)
{
snprintf (buf->str, sizeof (buf->str), "%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x",
guid->v[0], guid->v[1], guid->v[2], guid->v[3],
guid->v[4], guid->v[5], guid->v[6], guid->v[7],
guid->v[8], guid->v[9], guid->v[10], guid->v[11],
guid->v[12], guid->v[13], guid->v[14], guid->v[15]);
return buf->str;
}
static void hist_reset_minmax (struct hist *h)
{
h->min = UINT64_MAX;
h->max = 0;
}
static void hist_reset (struct hist *h)
{
hist_reset_minmax (h);
h->under = 0;
h->over = 0;
memset (h->bins, 0, h->nbins * sizeof (*h->bins));
}
static struct hist *hist_new (unsigned nbins, uint64_t binwidth, uint64_t bin0)
{
struct hist *h = malloc (sizeof (*h) + nbins * sizeof (*h->bins));
assert(h);
h->nbins = nbins;
h->binwidth = binwidth;
h->bin0 = bin0;
h->binN = h->bin0 + h->nbins * h->binwidth;
hist_reset (h);
return h;
}
static void hist_free (struct hist *h)
{
free (h);
}
static void hist_record (struct hist *h, uint64_t x, unsigned weight)
{
if (x < h->min)
h->min = x;
if (x > h->max)
h->max = x;
if (x < h->bin0)
h->under += weight;
else if (x >= h->binN)
h->over += weight;
else
h->bins[(x - h->bin0) / h->binwidth] += weight;
}
static void xsnprintf(char *buf, size_t bufsz, size_t *p, const char *fmt, ...) ddsrt_attribute_format_printf(4, 5);
static void xsnprintf(char *buf, size_t bufsz, size_t *p, const char *fmt, ...)
{
if (*p < bufsz)
{
int n;
va_list ap;
va_start (ap, fmt);
n = vsnprintf (buf + *p, bufsz - *p, fmt, ap);
va_end (ap);
*p += (size_t) n;
}
}
static void hist_print (const char *prefix, struct hist *h, dds_time_t dt, int reset)
{
const size_t l_size = sizeof(char) * h->nbins + 200 + strlen (prefix);
const size_t hist_size = sizeof(char) * h->nbins + 1;
char *l = (char *) malloc(l_size);
char *hist = (char *) malloc(hist_size);
double dt_s = (double)dt / 1e9, avg;
uint64_t peak = 0, cnt = h->under + h->over;
size_t p = 0;
assert(l);
xsnprintf (l, l_size, &p, "%s", prefix);
assert(hist);
hist[h->nbins] = 0;
for (unsigned i = 0; i < h->nbins; i++)
{
cnt += h->bins[i];
if (h->bins[i] > peak)
peak = h->bins[i];
}
const uint64_t p1 = peak / 100;
const uint64_t p10 = peak / 10;
const uint64_t p20 = 1 * peak / 5;
const uint64_t p40 = 2 * peak / 5;
const uint64_t p60 = 3 * peak / 5;
const uint64_t p80 = 4 * peak / 5;
for (unsigned i = 0; i < h->nbins; i++)
{
if (h->bins[i] == 0) hist[i] = ' ';
else if (h->bins[i] <= p1) hist[i] = '.';
else if (h->bins[i] <= p10) hist[i] = '_';
else if (h->bins[i] <= p20) hist[i] = '-';
else if (h->bins[i] <= p40) hist[i] = '=';
else if (h->bins[i] <= p60) hist[i] = 'x';
else if (h->bins[i] <= p80) hist[i] = 'X';
else hist[i] = '@';
}
avg = (double) cnt / dt_s;
if (avg < 999.5)
xsnprintf (l, l_size, &p, "%5.3g", avg);
else if (avg < 1e6)
xsnprintf (l, l_size, &p, "%4.3gk", avg / 1e3);
else
xsnprintf (l, l_size, &p, "%4.3gM", avg / 1e6);
xsnprintf (l, l_size, &p, "/s ");
if (h->min == UINT64_MAX)
xsnprintf (l, l_size, &p, " inf ");
else if (h->min < 1000)
xsnprintf (l, l_size, &p, "%3"PRIu64"n ", h->min);
else if (h->min + 500 < 1000000)
xsnprintf (l, l_size, &p, "%3"PRIu64"u ", (h->min + 500) / 1000);
else if (h->min + 500000 < 1000000000)
xsnprintf (l, l_size, &p, "%3"PRIu64"m ", (h->min + 500000) / 1000000);
else
xsnprintf (l, l_size, &p, "%3"PRIu64"s ", (h->min + 500000000) / 1000000000);
if (h->bin0 > 0) {
int pct = (cnt == 0) ? 0 : 100 * (int) ((h->under + cnt/2) / cnt);
xsnprintf (l, l_size, &p, "%3d%% ", pct);
}
{
int pct = (cnt == 0) ? 0 : 100 * (int) ((h->over + cnt/2) / cnt);
xsnprintf (l, l_size, &p, "|%s| %3d%%", hist, pct);
}
if (h->max < 1000)
xsnprintf (l, l_size, &p, " %3"PRIu64"n", h->max);
else if (h->max + 500 < 1000000)
xsnprintf (l, l_size, &p, " %3"PRIu64"u", (h->max + 500) / 1000);
else if (h->max + 500000 < 1000000000)
xsnprintf (l, l_size, &p, " %3"PRIu64"m", (h->max + 500000) / 1000000);
else
xsnprintf (l, l_size, &p, " %3"PRIu64"s", (h->max + 500000000) / 1000000000);
(void) p;
puts (l);
fflush (stdout);
free (l);
free (hist);
if (reset)
hist_reset (h);
}
static void *make_baggage (dds_sequence_octet *b, uint32_t cnt)
{
b->_maximum = b->_length = cnt;
if (cnt == 0)
b->_buffer = NULL;
else
{
b->_buffer = malloc (b->_maximum);
assert(b->_buffer);
memset(b->_buffer, 0xee, b->_maximum);
}
return b->_buffer;
}
static uint32_t *getseqptr (union data *data)
{
switch (topicsel)
{
case KS: return &data->ks.seq;
case K32: return &data->k32.seq;
case K256: return &data->k256.seq;
case OU: return &data->ou.seq;
case UK16: return &data->uk16.seq;
case UK1024: return &data->uk1024.seq;
case S16: return &data->s16.seq;
case S256: return &data->s256.seq;
case S4k: return &data->s4k.seq;
case S32k: return &data->s32k.seq;
}
return 0;
}
static uint32_t *getkeyvalptr (union data *data)
{
switch (topicsel)
{
case KS: return &data->ks.keyval;
case K32: return &data->k32.keyval;
case K256: return &data->k256.keyval;
case OU: return NULL;
case UK16: return NULL;
case UK1024: return NULL;
case S16: return &data->s16.keyval;
case S256: return &data->s256.keyval;
case S4k: return &data->s4k.keyval;
case S32k: return &data->s32k.keyval;
}
return 0;
}
static void *init_sample (union data *data, uint32_t seq)
{
void *baggage = NULL;
memset (data, 0xee, sizeof (*data));
if (topicsel == KS)
baggage = make_baggage (&data->ks.baggage, baggagesize);
uint32_t * const seqptr = getseqptr (data);
uint32_t * const keyvalptr = getkeyvalptr (data);
*seqptr = seq;
if (keyvalptr)
*keyvalptr = 0;
return baggage;
}
static uint32_t pubthread (void *varg)
{
int result;
dds_instance_handle_t *ihs;
dds_time_t ntot = 0, tfirst;
union data data;
void *baggage = NULL;
(void) varg;
memset (&data, 0, sizeof (data));
assert (nkeyvals > 0);
assert (topicsel != OU || nkeyvals == 1);
baggage = init_sample (&data, 0);
uint32_t * const seqptr = getseqptr (&data);
uint32_t * const keyvalptr = getkeyvalptr (&data);
ihs = malloc (nkeyvals * sizeof (dds_instance_handle_t));
assert(ihs);
if (!register_instances)
{
for (unsigned k = 0; k < nkeyvals; k++)
ihs[k] = 0;
}
else
{
for (unsigned k = 0; k < nkeyvals; k++)
{
if (keyvalptr)
*keyvalptr = k;
if ((result = dds_register_instance (wr_data, &ihs[k], &data)) != DDS_RETCODE_OK)
{
printf ("dds_register_instance failed: %d\n", result);
fflush (stdout);
exit (2);
}
}
}
uint32_t time_interval = 1; // call dds_time() once for this many samples
uint32_t time_counter = time_interval; // how many more samples on current time stamp
uint32_t batch_counter = 0; // number of samples in current batch
if (keyvalptr)
*keyvalptr = 0;
tfirst = dds_time();
dds_time_t t_write = tfirst;
while (!ddsrt_atomic_ld32 (&termflag))
{
/* lsb of timestamp is abused to signal whether the sample is a ping requiring a response or not */
bool reqresp = (ping_frac == 0) ? 0 : (ping_frac == UINT32_MAX) ? 1 : (ddsrt_random () <= ping_frac);
if ((result = dds_write_ts (wr_data, &data, (t_write & ~1) | reqresp)) != DDS_RETCODE_OK)
{
printf ("write error: %d\n", result);
fflush (stdout);
if (result != DDS_RETCODE_TIMEOUT)
exit (2);
/* retry with original timestamp, it really is just a way of reporting
blocking for an exceedingly long time, but do force a fresh time stamp
for the next sample */
time_counter = time_interval = 1;
continue;
}
if (reqresp)
{
dds_write_flush (wr_data);
}
const dds_time_t t_post_write = (time_counter == 1) ? dds_time () : t_write;
const dds_duration_t dt = t_post_write - t_write;
if (--time_counter == 0)
{
// try to read out the clock only every now and then
// - keeping it between once per 1us and once per 5us
// - also at least every 10th sample (not likely to go over 5MHz anyway)
// - if the interval is longish, reset it immediately to read the
// clock every time (low-rate, scheduling mishaps)
// that should work to keep the cost of reading the clock inside ddsperf
// to a reasonably low fraction of the time without significantly distorting
// the results
if (dt < DDS_USECS (5) && time_interval < 10)
time_interval++;
else if (dt > DDS_USECS (1) && time_interval > 1)
time_interval = (dt > DDS_USECS (10)) ? 1 : time_interval - 1;
time_counter = time_interval;
}
ddsrt_mutex_lock (&pubstat_lock);
hist_record (pubstat_hist, (uint64_t) dt, 1);
ntot++;
ddsrt_mutex_unlock (&pubstat_lock);
(*seqptr)++;
if (keyvalptr)
*keyvalptr = (*keyvalptr + 1) % nkeyvals;
t_write = t_post_write;
if (pub_rate < HUGE_VAL)
{
if (++batch_counter == burstsize)
{
/* FIXME: should average rate over a short-ish period, rather than over the entire run */
while (((double) (ntot / burstsize) / ((double) (t_write - tfirst) / 1e9 + 5e-3)) > pub_rate && !ddsrt_atomic_ld32 (&termflag))
{
/* FIXME: flushing manually because batching is not yet implemented properly */
dds_write_flush (wr_data);
dds_sleepfor (DDS_MSECS (1));
t_write = dds_time ();
time_counter = time_interval = 1;
}
batch_counter = 0;
}
}
}
if (baggage)
free (baggage);
free (ihs);
return 0;
}
static uint32_t topic_payload_size (enum topicsel tp, uint32_t bgsize)
{
uint32_t size = 0;
switch (tp)
{
case KS: size = 12 + bgsize; break;
case K32: size = 32; break;
case K256: size = 256; break;
case OU: size = 4; break;
case UK16: size = 16; break;
case UK1024: size = 1024; break;
case S16: size = (uint32_t) sizeof (Struct16); break;
case S256: size = (uint32_t) sizeof (Struct256); break;
case S4k: size = (uint32_t) sizeof (Struct4k); break;
case S32k: size = (uint32_t) sizeof (Struct32k); break;
}
return size;
}
static void latencystat_init (struct latencystat *x)
{
x->min = INT64_MAX;
x->max = INT64_MIN;
x->sum = x->cnt = 0;
x->raw = malloc (PINGPONG_RAWSIZE * sizeof (*x->raw));
assert(x->raw);
}
static void latencystat_fini (struct latencystat *x)
{
free (x->raw);
}
static void latencystat_reset (struct latencystat *x, int64_t *newraw)
{
x->raw = newraw;
x->min = INT64_MAX;
x->max = INT64_MIN;
x->sum = x->cnt = 0;
}
static int cmp_int64 (const void *va, const void *vb)
{
const int64_t *a = va;
const int64_t *b = vb;
return (*a == *b) ? 0 : (*a < *b) ? -1 : 1;
}
static int64_t *latencystat_print (struct latencystat *y, const char *prefix, const char *subprefix, dds_instance_handle_t pubhandle, dds_instance_handle_t pphandle, uint32_t size)
{
if (y->cnt > 0)
{
const uint32_t rawcnt = (y->cnt > PINGPONG_RAWSIZE) ? PINGPONG_RAWSIZE : y->cnt;
char ppinfo[128];
struct ppant *pp;
ddsrt_mutex_lock (&disc_lock);
if ((pp = ddsrt_avl_lookup (&ppants_td, &ppants, &pphandle)) == NULL)
snprintf (ppinfo, sizeof (ppinfo), "%"PRIx64, pubhandle);
else
snprintf (ppinfo, sizeof (ppinfo), "%s:%"PRIu32, pp->hostname, pp->pid);
ddsrt_mutex_unlock (&disc_lock);
qsort (y->raw, rawcnt, sizeof (*y->raw), cmp_int64);
printf ("%s%s %s size %"PRIu32" mean %.3fus min %.3fus 50%% %.3fus 90%% %.3fus 99%% %.3fus max %.3fus cnt %"PRIu32"\n",
prefix, subprefix, ppinfo, size,
(double) y->sum / (double) y->cnt / 1e3,
(double) y->min / 1e3,
(double) y->raw[rawcnt - (rawcnt + 1) / 2] / 1e3,
(double) y->raw[rawcnt - (rawcnt + 9) / 10] / 1e3,
(double) y->raw[rawcnt - (rawcnt + 99) / 100] / 1e3,
(double) y->max / 1e3,
y->cnt);
}
return y->raw;
}
static void latencystat_update (struct latencystat *x, int64_t tdelta)
{
if (tdelta < x->min) x->min = tdelta;
if (tdelta > x->max) x->max = tdelta;
x->sum += tdelta;
if (x->cnt < PINGPONG_RAWSIZE)
x->raw[x->cnt] = tdelta;
x->cnt++;
x->totcnt++;
}
static void init_eseq_admin (struct eseq_admin *ea, unsigned nkeys)
{
ddsrt_mutex_init (&ea->lock);
ea->nkeys = nkeys;
ea->nph = 0;
ea->ph = NULL;
ea->pph = NULL;
ea->stats = NULL;
ea->eseq = NULL;
}
static void fini_eseq_admin (struct eseq_admin *ea)
{
free (ea->ph);
free (ea->pph);
if (sublatency)
{
for (unsigned i = 0; i < ea->nph; i++)
latencystat_fini (&ea->stats[i].info);
}
free (ea->stats);
for (unsigned i = 0; i < ea->nph; i++)
free (ea->eseq[i]);
ddsrt_mutex_destroy (&ea->lock);
free (ea->eseq);
}
static dds_instance_handle_t get_pphandle_for_pubhandle (dds_instance_handle_t pubhandle)
{
/* FIXME: implement the get_matched_... interfaces so there's no need for keeping a reader
(and having to GC it, which I'm skipping here ...) */
int32_t n;
void *msg = NULL;
dds_sample_info_t info;
if ((n = dds_read_instance (rd_publications, &msg, &info, 1, 1, pubhandle)) < 0)
error2 ("dds_read_instance(rd_publications, %"PRIx64") failed: %d\n", pubhandle, (int) n);
if (n == 0 || !info.valid_data)
{
printf ("get_pong_writer: publication handle %"PRIx64" not found\n", pubhandle);
fflush (stdout);
return 0;
}
else
{
const dds_builtintopic_endpoint_t *sample = msg;
dds_instance_handle_t pphandle = sample->participant_instance_handle;
dds_return_loan (rd_publications, &msg, n);
return pphandle;
}
}
DDSRT_WARNING_MSVC_OFF(6308)
static int check_eseq (struct eseq_admin *ea, uint32_t seq, uint32_t keyval, uint32_t size, const dds_instance_handle_t pubhandle, int64_t tdelta)
{
uint32_t *eseq;
if (keyval >= ea->nkeys)
{
printf ("received key %"PRIu32" >= nkeys %u\n", keyval, ea->nkeys);
exit (3);
}
ddsrt_mutex_lock (&ea->lock);
for (uint32_t i = 0; i < ea->nph; i++)
if (pubhandle == ea->ph[i])
{
uint32_t e = ea->eseq[i][keyval];
ea->eseq[i][keyval] = seq + ea->nkeys;
ea->stats[i].nrecv++;
ea->stats[i].nrecv_bytes += size;
ea->stats[i].nlost += seq - e;
ea->stats[i].last_size = size;
if (sublatency)
latencystat_update (&ea->stats[i].info, tdelta);
ddsrt_mutex_unlock (&ea->lock);
return seq == e;
}
ea->ph = realloc (ea->ph, (ea->nph + 1) * sizeof (*ea->ph));
assert(ea->ph);
ea->ph[ea->nph] = pubhandle;
ea->pph = realloc (ea->pph, (ea->nph + 1) * sizeof (*ea->pph));
assert(ea->pph);
ea->pph[ea->nph] = get_pphandle_for_pubhandle (pubhandle);
ea->eseq = realloc (ea->eseq, (ea->nph + 1) * sizeof (*ea->eseq));
assert(ea->eseq);
ea->eseq[ea->nph] = malloc (ea->nkeys * sizeof (*ea->eseq[ea->nph]));
assert(ea->eseq[ea->nph]);
eseq = ea->eseq[ea->nph];
DDSRT_WARNING_MSVC_OFF(6386)
for (unsigned i = 0; i < ea->nkeys; i++)
eseq[i] = seq + (i - keyval) + (i <= keyval ? ea->nkeys : 0);
DDSRT_WARNING_MSVC_ON(6386)
ea->stats = realloc (ea->stats, (ea->nph + 1) * sizeof (*ea->stats));
assert(ea->stats);
memset (&ea->stats[ea->nph], 0, sizeof (ea->stats[ea->nph]));
ea->stats[ea->nph].nrecv = 1;
ea->stats[ea->nph].nrecv_bytes = size;
ea->stats[ea->nph].last_size = size;
if (sublatency)
{
latencystat_init (&ea->stats[ea->nph].info);
latencystat_update (&ea->stats[ea->nph].info, tdelta);
}
ea->nph++;
ddsrt_mutex_unlock (&ea->lock);
return 1;
}
DDSRT_WARNING_MSVC_ON(6308)
static bool update_roundtrip (dds_instance_handle_t pubhandle, int64_t tdelta, bool isping, uint32_t seq)
{
bool allseen;
ddsrt_mutex_lock (&pongstat_lock);
if (isping && seq == cur_ping_seq)
{
ddsrt_mutex_lock (&pongwr_lock);
allseen = (++n_pong_seen == n_pong_expected);
ddsrt_mutex_unlock (&pongwr_lock);
}
else
{
allseen = false;
}
for (uint32_t i = 0; i < npongstat; i++)
if (pongstat[i].pubhandle == pubhandle)
{
latencystat_update (&pongstat[i].info, tdelta);
ddsrt_mutex_unlock (&pongstat_lock);
return allseen;
}
DDSRT_WARNING_MSVC_OFF(6308)
pongstat = realloc (pongstat, (npongstat + 1) * sizeof (*pongstat));
assert(pongstat);
DDSRT_WARNING_MSVC_ON(6308)
struct subthread_arg_pongstat * const x = &pongstat[npongstat];
x->pubhandle = pubhandle;
x->pphandle = get_pphandle_for_pubhandle (pubhandle);
latencystat_init (&x->info);
latencystat_update (&x->info, tdelta);
npongstat++;
ddsrt_mutex_unlock (&pongstat_lock);
return allseen;
}
static dds_entity_t get_pong_writer_locked (dds_instance_handle_t pubhandle)
{
dds_instance_handle_t pphandle;
for (uint32_t j = 0; j < npongwr; j++)
if (pongwr[j].pubhandle == pubhandle)
return pongwr[j].wr_pong;
/* FIXME: implement the get_matched_... interfaces so there's no need for keeping a reader
(and having to GC it, which I'm skipping here ...) */
pphandle = get_pphandle_for_pubhandle (pubhandle);
/* This gets called when no writer is associated yet with pubhandle, but it may be that a writer
is associated already with pphandle (because there is the data writer and the ping writer) */
for (uint32_t i = 0; i < npongwr; i++)
{
if (pongwr[i].pphandle == pphandle)
{
dds_entity_t wr_pong = pongwr[i].wr_pong;
if (pongwr[i].pubhandle == 0)
{
pongwr[i].pubhandle = pubhandle;
return wr_pong;
}
else
{
DDSRT_WARNING_MSVC_OFF(6308)
pongwr = realloc (pongwr, (npongwr + 1) * sizeof (*pongwr));
assert(pongwr);
DDSRT_WARNING_MSVC_ON(6308)
pongwr[npongwr].pubhandle = pubhandle;
pongwr[npongwr].pphandle = pphandle;
pongwr[npongwr].wr_pong = wr_pong;
npongwr++;
return wr_pong;