summaryrefslogtreecommitdiffstats
path: root/src/Reports.c
blob: 008f7f43fa3f9ab7e105c6c80ee40820f06bcf25 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
/*---------------------------------------------------------------
 * Copyright (c) 1999,2000,2001,2002,2003
 * The Board of Trustees of the University of Illinois
 * All Rights Reserved.
 *---------------------------------------------------------------
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software (Iperf) and associated
 * documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 *
 * Redistributions of source code must retain the above
 * copyright notice, this list of conditions and
 * the following disclaimers.
 *
 *
 * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimers in the documentation and/or other materials
 * provided with the distribution.
 *
 *
 * Neither the names of the University of Illinois, NCSA,
 * nor the names of its contributors may be used to endorse
 * or promote products derived from this Software without
 * specific prior written permission.
 *
 * 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 CONTRIBUTORS 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.
 * ________________________________________________________________
 * National Laboratory for Applied Network Research
 * National Center for Supercomputing Applications
 * University of Illinois at Urbana-Champaign
 * http://www.ncsa.uiuc.edu
 * ________________________________________________________________
 *
 * Reporter.c
 * rewritten by Robert McMahon
 * -------------------------------------------------------------------
 * Handle instantiation and deletion of reports, including sum reports,
 * in a thread safe way
 * ------------------------------------------------------------------- */

#include "headers.h"
#include <math.h>
#include "Settings.hpp"
#include "PerfSocket.hpp"
#include "Reporter.h"
#include "Locale.h"
#include "active_hosts.h"
#include "payloads.h"
static int transferid_counter = 0;

static inline int my_str_copy(char **dst, char *src) {
    int cnt = 0;
    if (src) {
        *dst = (char *) calloc(strlen(src) + 1, sizeof(char));
	if (*dst == NULL) {
	    fprintf(stderr, "Out of Memory!!\n");
	    exit(1);
	}
	cnt = strlen(src) + 1;
        strcpy((*dst), src);
    } else {
	*dst = NULL;
    }
    return cnt;
}

// These are the thread settings that are shared among report types
// Make a copy vs referencing the thread setting object. This will
// better encpasulate report handling.
static void common_copy (struct ReportCommon **common, struct thread_Settings *inSettings) {
    // Do deep copies from settings
    *common = (struct ReportCommon *) calloc(1, sizeof(struct ReportCommon));
    my_str_copy(&(*common)->Host, inSettings->mHost);
    my_str_copy(&(*common)->HideHost, inSettings->mHideHost);
    my_str_copy(&(*common)->Localhost, inSettings->mLocalhost);
    my_str_copy(&(*common)->Ifrname, inSettings->mIfrname);
    my_str_copy(&(*common)->Ifrnametx, inSettings->mIfrnametx);
    my_str_copy(&(*common)->SSMMulticastStr, inSettings->mSSMMulticastStr);
    my_str_copy(&(*common)->Congestion, inSettings->mCongestion);
    my_str_copy(&(*common)->transferIDStr, inSettings->mTransferIDStr);
    my_str_copy(&(*common)->PermitKey, inSettings->mPermitKey);

    // copy some relevant settings
    (*common)->flags = inSettings->flags;
    (*common)->flags_extend = inSettings->flags_extend;
    (*common)->flags_extend2 = inSettings->flags_extend2;
    (*common)->ThreadMode = inSettings->mThreadMode;
    (*common)->ReportMode = inSettings->mReportMode;
    (*common)->KeyCheck = inSettings->mKeyCheck;
    (*common)->Format = inSettings->mFormat;
    (*common)->TTL = inSettings->mTTL;
    // copy some traffic related settings
    (*common)->BufLen = inSettings->mBufLen;
    (*common)->MSS = inSettings->mMSS;
    (*common)->TCPWin = inSettings->mTCPWin;
    (*common)->FQPacingRate = inSettings->mFQPacingRate;
    (*common)->Port = inSettings->mPort;
    (*common)->PortLast = inSettings->mPortLast;
    (*common)->BindPort = inSettings->mBindPort;
    (*common)->ListenPort = inSettings->mListenPort;
    (*common)->AppRate = inSettings->mAppRate;
    (*common)->BurstSize = inSettings->mBurstSize;
    (*common)->AppRateUnits = inSettings->mAppRateUnits;
    (*common)->socket = inSettings->mSock;
    (*common)->transferID = inSettings->mTransferID;
    (*common)->threads = inSettings->mThreads;
    (*common)->winsize_requested = inSettings->mTCPWin;
#if defined(HAVE_LINUX_FILTER_H) && defined(HAVE_AF_PACKET)
    (*common)->socketdrop = inSettings->mSockDrop;
#endif
    (*common)->peer = inSettings->peer;
    (*common)->size_peer = inSettings->size_peer;
    (*common)->jitter_binwidth = inSettings->jitter_binwidth;
    (*common)->local = inSettings->local;
    (*common)->size_local = inSettings->size_local;
    (*common)->HistBins =inSettings->mHistBins;
    (*common)->HistBinsize =inSettings->mHistBinsize;
    (*common)->HistUnits =inSettings->mHistUnits;
    (*common)->pktIPG =inSettings->mBurstIPG;
    (*common)->rtt_weight = inSettings->rtt_nearcongest_weight_factor;
    (*common)->ListenerTimeout =inSettings->mListenerTimeout;
    (*common)->FPS = inSettings->mFPS;
    (*common)->TOS = inSettings->mTOS;
    (*common)->RTOS = inSettings->mRTOS;
    (*common)->bbsize = inSettings->mBounceBackBytes;
    (*common)->bbhold = inSettings->mBounceBackHold;
    (*common)->bbcount = inSettings->mBounceBackBurst;
#if HAVE_DECL_TCP_WINDOW_CLAMP
    (*common)->ClampSize = inSettings->mClampSize;
#endif
#if HAVE_DECL_TCP_NOTSENT_LOWAT
    (*common)->WritePrefetch = inSettings->mWritePrefetch;
#endif
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Alloc common rpt/com/size %p/%p/%d", (void *) common, (void *)(*common), sizeof(struct ReportCommon));
#endif
}

static void free_common_copy (struct ReportCommon *common) {
    assert(common != NULL);
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Free common=%p", (void *)common);
#endif
    // Free deep copies
    if (common->Host)
	free(common->Host);
    if (common->HideHost)
	free(common->HideHost);
    if (common->Localhost)
	free(common->Localhost);
    if (common->Ifrname)
	free(common->Ifrname);
    if (common->Ifrnametx)
	free(common->Ifrnametx);
    if (common->SSMMulticastStr)
	free(common->SSMMulticastStr);
    if (common->Congestion)
	free(common->Congestion);
    if (common->transferIDStr)
	free(common->transferIDStr);
    if (common->PermitKey)
	free(common->PermitKey);
    free(common);
}

// This will set the transfer id and id string
// on the setting object. If the current id is zero
// this will get the next one. Otherwise it will use
// the value.
void setTransferID (struct thread_Settings *inSettings, int role_reversal) {
    if (!inSettings->mTransferIDStr) {
	if (!inSettings->mTransferID) {
	    Mutex_Lock(&transferid_mutex);
	    inSettings->mTransferID = ++transferid_counter;
	    Mutex_Unlock(&transferid_mutex);
	}
	int len = 0;
	if (role_reversal)  {
#ifdef HAVE_ROLE_REVERSAL_ID
	    if (isPermitKey(inSettings) && (inSettings->mPermitKey[0] != '\0')) {
		len = snprintf(NULL, 0, "[%s(*%d)] ", \
			       inSettings->mPermitKey, inSettings->mTransferID);
		inSettings->mTransferIDStr = (char *) calloc(len + 1, sizeof(char));
		len = sprintf(inSettings->mTransferIDStr, "[%s(*%d)] ", \
			       inSettings->mPermitKey, inSettings->mTransferID);
	    } else if (inSettings->mTransferID < 10) {
		len = snprintf(NULL, 0, "[ *%d] ", inSettings->mTransferID);
		inSettings->mTransferIDStr = (char *) calloc(len + 1, sizeof(char));
		len = sprintf(inSettings->mTransferIDStr, "[ *%d] ", inSettings->mTransferID);
	    } else {
		len = snprintf(NULL, 0, "[*%d] ", inSettings->mTransferID);
		inSettings->mTransferIDStr = (char *) calloc(len + 1, sizeof(char));
		len = sprintf(inSettings->mTransferIDStr, "[*%d] ", inSettings->mTransferID);
	    }
#endif
	} else if (isPermitKey(inSettings) && (inSettings->mPermitKey[0] != '\0')) {
	    len = snprintf(NULL, 0, "[%s(%d)] ", \
			   inSettings->mPermitKey, inSettings->mTransferID);
	    inSettings->mTransferIDStr = (char *) calloc(len + 1, sizeof(char));
	    len = sprintf(inSettings->mTransferIDStr, "[%s(%d)] ", \
			   inSettings->mPermitKey, inSettings->mTransferID);
	} else  {
	    len = snprintf(NULL, 0, "[%3d] ", inSettings->mTransferID);
	    inSettings->mTransferIDStr = (char *) calloc(len+1, sizeof(char));
	    len = sprintf(inSettings->mTransferIDStr, "[%3d] ", inSettings->mTransferID);
	}
    }
}

void SetFullDuplexHandlers (struct thread_Settings *inSettings, struct SumReport* sumreport) {
    if (isUDP(inSettings)) {
	sumreport->transfer_protocol_sum_handler = reporter_transfer_protocol_fullduplex_udp;
	sumreport->info.output_handler = ((inSettings->mReportMode == kReport_CSV) ? NULL : \
					  (isSumOnly(inSettings) ? NULL : \
					   (isEnhanced(inSettings) ? udp_output_fullduplex_enhanced : udp_output_fullduplex)));
    } else {
	sumreport->transfer_protocol_sum_handler = reporter_transfer_protocol_fullduplex_tcp;
	sumreport->info.output_handler = ((inSettings->mReportMode == kReport_CSV) ? NULL : \
					      (isSumOnly(inSettings) ? NULL : \
					       (isEnhanced(inSettings) ? tcp_output_fullduplex_enhanced : tcp_output_fullduplex)));
    }
}

void SetSumHandlers (struct thread_Settings *inSettings, struct SumReport* sumreport) {
    switch (inSettings->mThreadMode) {
    case kMode_Server :
	if (isUDP(inSettings)) {
	    sumreport->transfer_protocol_sum_handler = reporter_transfer_protocol_sum_server_udp;
	    if (inSettings->mReportMode == kReport_CSV) {
		if (isEnhanced(inSettings))
		    sumreport->info.output_handler = udp_output_enhanced_csv;
		else
		    sumreport->info.output_handler = udp_output_basic_csv;
	    } else {
		if (isTripTime(inSettings)) {
		    sumreport->info.output_handler = udp_output_sumcnt_read_triptime;
		} else if (isSumOnly(inSettings)) {
		    sumreport->info.output_handler = udp_output_sumcnt_enhanced;
		} else if (isFullDuplex(inSettings)) {
		    sumreport->info.output_handler = udp_output_fullduplex_sum;
		} else {
		    sumreport->info.output_handler =  (isEnhanced(inSettings) ? udp_output_sum_read_enhanced : udp_output_sum_read);
		}
	    }
	} else {
	    sumreport->transfer_protocol_sum_handler = reporter_transfer_protocol_sum_server_tcp;
	    if (inSettings->mReportMode == kReport_CSV) {
		if (isEnhanced(inSettings))
		    sumreport->info.output_handler = tcp_output_read_enhanced_csv;
		else
		    sumreport->info.output_handler = tcp_output_basic_csv;
	    } else {
		if (isTripTime(inSettings)) {
		    sumreport->info.output_handler = tcp_output_sumcnt_read_triptime;
		} else if (isSumOnly(inSettings)) {
		    sumreport->info.output_handler = (isEnhanced(inSettings) ? tcp_output_sumcnt_read_enhanced : tcp_output_sumcnt_read);
		} else if (isFullDuplex(inSettings)) {
		    sumreport->info.output_handler = tcp_output_sum_read;
		} else {
		    sumreport->info.output_handler =  (isEnhanced(inSettings) ? tcp_output_sum_read_enhanced : tcp_output_sum_read);
		}
	    }
	}
	break;
    case kMode_Client :
	if (isUDP(inSettings)) {
	    sumreport->transfer_protocol_sum_handler = reporter_transfer_protocol_sum_client_udp;
	    if (inSettings->mReportMode == kReport_CSV) {
		if (isEnhanced(inSettings))
		    sumreport->info.output_handler = udp_output_enhanced_csv;
		else
		    sumreport->info.output_handler = udp_output_basic_csv;
	    } else {
		if (isSumOnly(inSettings)) {
		    sumreport->info.output_handler = ((isEnhanced(inSettings) && !isFullDuplex(inSettings)) ? \
						      udp_output_sumcnt_write_enhanced : udp_output_sumcnt);
		} else if (isFullDuplex(inSettings)) {
		    sumreport->info.output_handler = udp_output_fullduplex_sum;
		} else {
		    sumreport->info.output_handler = (isEnhanced(inSettings) ? udp_output_sum_write_enhanced : udp_output_sum_write);
		}
	    }
	} else {
	    sumreport->transfer_protocol_sum_handler = reporter_transfer_protocol_sum_client_tcp;
	    if (inSettings->mReportMode == kReport_CSV) {
		if (isEnhanced(inSettings))
		    sumreport->info.output_handler = tcp_output_write_enhanced_csv;
		else
		    sumreport->info.output_handler = tcp_output_basic_csv;
	    } else {
		if (isSumOnly(inSettings)) {
		    sumreport->info.output_handler = (isEnhanced(inSettings) ? tcp_output_sumcnt_write_enhanced : tcp_output_sumcnt_write);
		} else if (isFullDuplex(inSettings)) {
		    sumreport->info.output_handler = tcp_output_fullduplex_sum;
		} else {
		    sumreport->info.output_handler = (isEnhanced(inSettings) ? tcp_output_sum_write_enhanced : tcp_output_sum_write);
		}
	    }
	}
	break;
    default:
	FAIL(1, "SetSumReport", inSettings);
    }
}

struct SumReport* InitSumReport(struct thread_Settings *inSettings, int inID, int fullduplex_report) {
    struct SumReport *sumreport = (struct SumReport *) calloc(1, sizeof(struct SumReport));
    if (sumreport == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    sumreport->reference.count = 0;
    sumreport->reference.maxcount = 0;
    Mutex_Initialize(&sumreport->reference.lock);
    sumreport->threads = 0;
    common_copy(&sumreport->info.common, inSettings);
    // sumreport->info.common->transferID = inID; // this is now set in the active code
    sumreport->info.threadcnt = 0;
    sumreport->info.isMaskOutput = false;
    if (inSettings->mReportMode == kReport_CSV) {
        format_ips_port_string(&sumreport->info, 1);
    }

    // Only initialize the interval time here
    // The startTime and nextTime for summing reports will be set by
    // the reporter thread in realtime
    if ((inSettings->mInterval) && (inSettings->mIntervalMode == kInterval_Time)) {
	sumreport->info.ts.intervalTime.tv_sec = (long) (inSettings->mInterval / rMillion);
	sumreport->info.ts.intervalTime.tv_usec = (long) (inSettings->mInterval % rMillion);
	sumreport->info.ts.significant_partial = ((double) inSettings->mInterval * PARTIALPERCENT / rMillion) ;
    }
    // Note that for UDP the client flag settings have not been read (and set) so only use server side flags in tests
    if (isEnhanced(inSettings) && (inSettings->mThreadMode == kMode_Server) && !fullduplex_report) {
	if (isHistogram(inSettings)) {
	    if (isUDP(inSettings)) {
		char name[] = "SUMT8";
		sumreport->info.latency_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0,\
								    pow(10,inSettings->mHistUnits), \
								    inSettings->mHistci_lower, inSettings->mHistci_upper, sumreport->info.common->transferID, name);
	    } else {
		char name[] = "SUMF8";
		sumreport->info.framelatency_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0, \
									 pow(10,inSettings->mHistUnits), inSettings->mHistci_lower, \
									 inSettings->mHistci_upper, sumreport->info.common->transferID, name);
	    }
	}
	if (isJitterHistogram(inSettings) && isUDP(inSettings)) {
	    char name[] = "SUMJ8";
	    sumreport->info.jitter_histogram = histogram_init(JITTER_BINCNT,inSettings->jitter_binwidth,0,JITTER_UNITS, \
							      JITTER_LCI, JITTER_UCI, sumreport->info.common->transferID, name);
	}
    }
    if (fullduplex_report) {
	SetFullDuplexHandlers(inSettings, sumreport);
	if (!isServerReverse(inSettings)) {
	    sumreport->fullduplex_barrier.count = 0;
	    Condition_Initialize(&sumreport->fullduplex_barrier.await);
	    sumreport->fullduplex_barrier.timeout = ((isModeTime(inSettings) && isUDP(inSettings)) ? \
						     ((int)(inSettings->mAmount / 100) + 1) : MINBARRIERTIMEOUT);
	    if (sumreport->fullduplex_barrier.timeout < MINBARRIERTIMEOUT)
		sumreport->fullduplex_barrier.timeout = MINBARRIERTIMEOUT;
	} else {
	    sumreport->info.ts.startTime = inSettings->accept_time;
	    sumreport->info.ts.nextTime = sumreport->info.ts.startTime;
	    TimeAdd(sumreport->info.ts.nextTime, sumreport->info.ts.intervalTime);
	}
    } else {
	SetSumHandlers(inSettings, sumreport);
    }
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Init sum report %p id=%d", (void *)sumreport, inID);
#endif
    return sumreport;
}

struct ConnectionInfo * InitConnectOnlyReport (struct thread_Settings *thread) {
    assert(thread != NULL);
    // this connection report used only by report for accumulate stats
    struct ConnectionInfo *creport = (struct ConnectionInfo *) calloc(1, sizeof(struct ConnectionInfo));
    if (!creport) {
	FAIL(1, "Out of Memory!!\n", thread);
    }
    common_copy(&creport->common, thread);
    creport->connect_times.min = FLT_MAX;
    creport->connect_times.max = FLT_MIN;
    creport->connect_times.vd = 0;
    creport->connect_times.m2 = 0;
    creport->connect_times.mean = 0;
    creport->txholdbacktime = thread->txholdback_timer;
    return creport;
}

void FreeSumReport (struct SumReport *sumreport) {
    assert(sumreport);
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Free sum report hdr=%p", (void *)sumreport);
#endif
    Condition_Destroy_Reference(&sumreport->reference);
    if (sumreport->info.latency_histogram) {
	histogram_delete(sumreport->info.latency_histogram);
    }
    if (sumreport->info.framelatency_histogram) {
	histogram_delete(sumreport->info.framelatency_histogram);
    }
    if (sumreport->info.bbrtt_histogram) {
	histogram_delete(sumreport->info.bbrtt_histogram);
    }
    if (sumreport->info.jitter_histogram) {
	histogram_delete(sumreport->info.jitter_histogram);
    }
    free_common_copy(sumreport->info.common);
    free(sumreport);
}


static void Free_iReport (struct ReporterData *ireport) {
    assert(ireport != NULL);

#ifdef HAVE_THREAD_DEBUG
    thread_debug("Free report hdr=%p reporter thread suspend count=%d packetring=%p histo=%p frame histo=%p", \
		 (void *)ireport, ireport->reporter_thread_suspends, (void *) ireport->packetring, \
		 (void *)ireport->info.latency_histogram, (void *) ireport->info.framelatency_histogram);
#endif
    if (ireport->packetring && ireport->info.total.Bytes.current && !(isSingleUDP(ireport->info.common)) && \
	!TimeZero(ireport->info.ts.intervalTime) && (ireport->reporter_thread_suspends < 3)) {
	fprintf(stdout, "WARN: this test may have been CPU bound (%d) (or may not be detecting the underlying network devices)\n", \
		ireport->reporter_thread_suspends);
    }
    if (ireport->packetring) {
	packetring_free(ireport->packetring);
    }
    if (ireport->info.latency_histogram) {
	histogram_delete(ireport->info.latency_histogram);
    }
    if (ireport->info.jitter_histogram) {
	histogram_delete(ireport->info.jitter_histogram);
    }
    if (ireport->info.framelatency_histogram) {
	histogram_delete(ireport->info.framelatency_histogram);
    }
    if (ireport->info.bbrtt_histogram) {
	histogram_delete(ireport->info.bbrtt_histogram);
    }
    free_common_copy(ireport->info.common);
    free(ireport);
}

void FreeConnectionReport (struct ConnectionInfo *report) {
    free_common_copy(report->common);
    free(report);
}

static void Free_sReport (struct ReportSettings *report) {
    free_common_copy(report->common);
    free(report);
}

static void Free_srReport (struct TransferInfo *report) {
    free_common_copy(report->common);
    free(report);
}

void FreeReport (struct ReportHeader *reporthdr) {
    assert(reporthdr != NULL);
#ifdef HAVE_THREAD_DEBUG
    char rs[REPORTTXTMAX];
    reporttype_text(reporthdr, &rs[0]);
    thread_debug("Jobq *FREE* report hdr/rpt %p/%p (%s)", (void *) reporthdr, (void *) reporthdr->this_report, &rs[0]);
#endif
    switch (reporthdr->type) {
    case DATA_REPORT:
	Free_iReport((struct ReporterData *)reporthdr->this_report);
	break;
    case CONNECTION_REPORT:
	FreeConnectionReport((struct ConnectionInfo *)reporthdr->this_report);
	break;
    case SETTINGS_REPORT:
	Free_sReport((struct ReportSettings *)reporthdr->this_report);
	break;
    case SERVER_RELAY_REPORT:
	Free_srReport((struct TransferInfo *)reporthdr->this_report);
	break;
    default:
	fprintf(stderr, "Invalid report type in free (%x)\n", reporthdr->type);
	assert(0);
	break;
    }
    free(reporthdr);
}

/*
 * InitReport is called by a transfer agent (client or
 * server) to setup the needed structures to communicate
 * traffic and connection information.  Also initialize
 * the report start time and next interval report time
 * Finally, in the case of parallel clients, have them all
 * synchronize on compeleting their connect()
 */

void IncrSumReportRefCounter (struct SumReport *sumreport) {
    assert(sumreport);
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Sum multiheader %p ref=%d->%d", (void *)sumreport, sumreport->reference.count, (sumreport->reference.count + 1));
#endif
    Mutex_Lock(&sumreport->reference.lock);
    sumreport->reference.count++;
    if (sumreport->reference.count > sumreport->reference.maxcount)
	sumreport->reference.maxcount = sumreport->reference.count;
    Mutex_Unlock(&sumreport->reference.lock);
}

int DecrSumReportRefCounter (struct SumReport *sumreport) {
    assert(sumreport);
//    thread_debug("before lock hdr=%p", (void *)sumreport);
    Mutex_Lock(&sumreport->reference.lock);
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Sum multiheader %p ref=%d->%d", (void *)sumreport, sumreport->reference.count, (sumreport->reference.count - 1));
#endif
//    thread_debug("in lock hdr=%p", (void *)sumreport);
    int refcnt = --sumreport->reference.count;
    Mutex_Unlock(&sumreport->reference.lock);
//    thread_debug("unlock hdr=%p", (void *)sumreport);
    return refcnt;
}


// Note, this report structure needs to remain self contained and not coupled
// to any settings structure pointers. This allows the thread settings to
// be freed without impacting the reporter.  It's not recommended that
// this be done, i.e. free the settings before the report, but be defensive
// here to allow it
struct ReportHeader* InitIndividualReport (struct thread_Settings *inSettings) {
    /*
     * Create the report header and an ireport (if needed)
     */
    assert(inSettings!=NULL);
    struct ReportHeader *reporthdr = (struct ReportHeader *) calloc(1, sizeof(struct ReportHeader));
    if (reporthdr == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->this_report = calloc(1, sizeof(struct ReporterData));
    if (reporthdr->this_report == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->type = DATA_REPORT;
    reporthdr->ReportMode = inSettings->mReportMode;

    struct ReporterData *ireport = (struct ReporterData *)(reporthdr->this_report);
    if (inSettings->mSumReport) {
	ireport->GroupSumReport = inSettings->mSumReport;
    }
    if (isFullDuplex(inSettings)) {
	assert(inSettings->mFullDuplexReport != NULL);
	IncrSumReportRefCounter(inSettings->mFullDuplexReport);
	ireport->FullDuplexReport = inSettings->mFullDuplexReport;
    }
    // Copy common settings into the transfer report section
    common_copy(&ireport->info.common, inSettings);
    ireport->info.final = false;
    ireport->info.burstid_transition = false;
    ireport->info.isEnableTcpInfo = false;

    // Create a new packet ring which is used to communicate
    // packet stats from the traffic thread to the reporter
    // thread.  The reporter thread does all packet accounting
    ireport->packetring = packetring_init((inSettings->numreportstructs ? inSettings->numreportstructs : (isSingleUDP(inSettings) ? 40 : NUM_REPORT_STRUCTS)), \
					  &ReportCond, (isSingleUDP(inSettings) ? NULL : &inSettings->awake_me));
#ifdef HAVE_THREAD_DEBUG
    char rs[REPORTTXTMAX];
    reporttype_text(reporthdr, &rs[0]);
    thread_debug("Init %s report hdr/rpt/com=%p/%p/%p multireport/fullduplex=%p/%p pring(bytes)/cond=%p(%d)/%p (socket=%d)", &rs[0], \
		 (void *) reporthdr, (void *) ireport, (void *) ireport->info.common, \
		 (void *) inSettings->mSumReport, (void *) inSettings->mFullDuplexReport, \
		 (void *) ireport->packetring, ireport->packetring->bytes, (void *) ireport->packetring->awake_producer, inSettings->mSock);
#endif
    if (inSettings->numreportstructs)
	fprintf (stdout, "%sNUM_REPORT_STRUCTS override from %d to %d\n", inSettings->mTransferIDStr, NUM_REPORT_STRUCTS, inSettings->numreportstructs);

    // Set up the function vectors, there are three
    // 1) packet_handler: does packet accounting per the test and protocol
    // 2) transfer_protocol_handler: performs output, e.g. interval reports, per the test and protocol

    if (inSettings->mIntervalMode == kInterval_Time) {
	ireport->info.ts.intervalTime.tv_sec = (long) (inSettings->mInterval / rMillion);
	ireport->info.ts.intervalTime.tv_usec = (long) (inSettings->mInterval % rMillion);
	ireport->transfer_interval_handler = reporter_condprint_time_interval_report;
	ireport->info.ts.significant_partial = (double) inSettings->mInterval * PARTIALPERCENT / rMillion ;
    } else {
	ireport->transfer_interval_handler = NULL;
    }
    ireport->packet_handler_pre_report = NULL;
    ireport->packet_handler_post_report = NULL;
    switch (inSettings->mThreadMode) {
    case kMode_Server :
	if (isUDP(inSettings)) {
	    ireport->packet_handler_post_report = reporter_handle_packet_server_udp;
	    ireport->transfer_protocol_handler = reporter_transfer_protocol_server_udp;
	    if ((inSettings->mIntervalMode == kInterval_Frames) && isIsochronous(inSettings)) {
		ireport->transfer_interval_handler = reporter_condprint_frame_interval_report_server_udp;
		ireport->transfer_protocol_handler = reporter_transfer_protocol_server_udp;
	    } else {
		ireport->transfer_protocol_handler = reporter_transfer_protocol_server_udp;
		if (isSumOnly(inSettings)) {
		    ireport->info.output_handler = NULL;
		} else if ((inSettings->mReportMode == kReport_CSV) && !isSumOnly(inSettings)) {
		    if (isEnhanced(inSettings))
			ireport->info.output_handler = udp_output_enhanced_csv;
		    else
			ireport->info.output_handler = udp_output_basic_csv;
		} else if (isTripTime(inSettings)) {
		    if (isIsochronous(inSettings))
			ireport->info.output_handler = udp_output_read_triptime_isoch;
		    else
			ireport->info.output_handler = udp_output_read_triptime;
		} else if (isEnhanced(inSettings)) {
		    ireport->info.output_handler = udp_output_read_enhanced;
		} else if (isFullDuplex(inSettings)) {
		    ireport->info.output_handler = udp_output_read;
		} else {
		    ireport->info.output_handler = udp_output_read;
		}
	    }
	} else {  // TCP case
	    ireport->packet_handler_post_report = reporter_handle_packet_server_tcp;
	    ireport->transfer_protocol_handler = reporter_transfer_protocol_server_tcp;
	    if (isPeriodicBurst(inSettings)) {
		ireport->transfer_interval_handler = reporter_condprint_burst_interval_report_server_tcp;
		ireport->info.output_handler = tcp_output_burst_read;
		ireport->packet_handler_pre_report = reporter_handle_packet_server_tcp;
		ireport->packet_handler_post_report = NULL;
	    } else if ((inSettings->mIntervalMode == kInterval_Frames) && isIsochronous(inSettings)) {
		ireport->transfer_interval_handler = reporter_condprint_frame_interval_report_server_tcp;
		ireport->info.output_handler = tcp_output_frame_read_triptime;
		ireport->packet_handler_pre_report = reporter_handle_packet_server_tcp;
		ireport->packet_handler_post_report = NULL;
	    } else if (isSumOnly(inSettings)) {
		ireport->info.output_handler = NULL;
	    } else if ((inSettings->mReportMode == kReport_CSV) && !isSumOnly(inSettings)) {
		if (isEnhanced(inSettings))
		    ireport->info.output_handler = tcp_output_read_enhanced_csv;
		else
		    ireport->info.output_handler = tcp_output_basic_csv;
	    } else if (isBounceBack(inSettings)) {
		ireport->packet_handler_post_report = reporter_handle_packet_bb_server;
		ireport->transfer_protocol_handler = reporter_transfer_protocol_server_bb_tcp;
		ireport->info.output_handler = tcp_output_write;
	    } else if (isTripTime(inSettings) && isIsochronous(inSettings)) {
		ireport->info.output_handler = tcp_output_read_enhanced_isoch;
	    } else if (isTripTime(inSettings)) {
		ireport->info.output_handler = tcp_output_read_triptime;
	    } else if (isEnhanced(inSettings)) {
		ireport->info.output_handler = tcp_output_read_enhanced;
	    } else if (!isFullDuplex(inSettings)) {
		ireport->info.output_handler = tcp_output_read;
	    } else {
		ireport->info.output_handler = tcp_output_read;
	    }
	}
	break;
    case kMode_Client :
	ireport->packet_handler_post_report = reporter_handle_packet_client;
	if (isUDP(inSettings)) {
	    ireport->transfer_protocol_handler = reporter_transfer_protocol_client_udp;
            if (isSumOnly(inSettings)) {
		ireport->info.output_handler = NULL;
	    } else if ((inSettings->mReportMode == kReport_CSV) && !isSumOnly(inSettings)) {
		if (isEnhanced(inSettings))
		    ireport->info.output_handler = udp_output_enhanced_csv;
		else
		    ireport->info.output_handler = udp_output_basic_csv;
	    } else if (isIsochronous(inSettings)) {
		ireport->info.output_handler = udp_output_write_enhanced_isoch;
	    } else if (isEnhanced(inSettings)) {
		ireport->info.output_handler = udp_output_write_enhanced;
	    } else if (isFullDuplex(inSettings)) {
		ireport->info.output_handler = udp_output_write;
	    } else {
		ireport->info.output_handler = udp_output_write;
	    }
	} else {
	    ireport->transfer_protocol_handler = reporter_transfer_protocol_client_tcp;
	    if (isSumOnly(inSettings)) {
		ireport->info.output_handler = NULL;
	    } else if ((inSettings->mReportMode == kReport_CSV) && !isSumOnly(inSettings)) {
		if (isEnhanced(inSettings))
		    ireport->info.output_handler = tcp_output_write_enhanced_csv;
		else
		    ireport->info.output_handler = tcp_output_basic_csv;
	    } else if (isBounceBack(inSettings)) {
		ireport->packet_handler_post_report = reporter_handle_packet_bb_client;
		ireport->transfer_protocol_handler = reporter_transfer_protocol_client_bb_tcp;
		ireport->info.output_handler = tcp_output_write_bb;
	    } else if (isIsochronous(inSettings)) {
		ireport->info.output_handler = tcp_output_write_enhanced_isoch;
	    } else if (isTcpWriteTimes(inSettings)) {
		ireport->info.output_handler = tcp_output_write_enhanced_write;
	    } else if (isEnhanced(inSettings)) {
		ireport->info.output_handler = tcp_output_write_enhanced;
	    } else if (isFullDuplex(inSettings)) {
		ireport->info.output_handler = tcp_output_write;
	    } else {
		ireport->info.output_handler = tcp_output_write;
	    }
	}
	break;
    case kMode_Unknown :
    case kMode_Reporter :
    case kMode_ReporterClient :
    case kMode_Listener:
    default:
	FAIL(1, "InitIndividualReport\n", inSettings);
    }

    if (inSettings->mThreadMode == kMode_Server) {
	ireport->info.sock_callstats.read.binsize = inSettings->mBufLen / 8;
	if (isUDP(inSettings)) {
	    if (isJitterHistogram(inSettings)) {
		char name[] = "J8";
		ireport->info.jitter_histogram = histogram_init(JITTER_BINCNT,inSettings->jitter_binwidth,0,JITTER_UNITS, \
							      JITTER_LCI, JITTER_UCI, ireport->info.common->transferID, name);
	    }
	    if (isTripTime(inSettings) && isHistogram(inSettings)) {
		char name[] = "T8";
		ireport->info.latency_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0,\
								  pow(10,inSettings->mHistUnits), \
								  inSettings->mHistci_lower, inSettings->mHistci_upper, ireport->info.common->transferID, name);
	    }
	}
	if (isHistogram(inSettings) && (isIsochronous(inSettings) || (!isUDP(inSettings) && isTripTime(inSettings)))) {
	    char name[] = "F8";
	    // make sure frame bin size min is 100 microsecond
	    ireport->info.framelatency_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0, \
								   pow(10,inSettings->mHistUnits), inSettings->mHistci_lower, \
								   inSettings->mHistci_upper, ireport->info.common->transferID, name);
	}
    }
    if ((inSettings->mThreadMode == kMode_Client) && !isUDP(inSettings) && isHistogram(inSettings)) {
	if (isTcpWriteTimes(inSettings)) {
	    char name[] = "W8";
	    ireport->info.write_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0,\
							    pow(10,inSettings->mHistUnits), \
							    inSettings->mHistci_lower, inSettings->mHistci_upper, ireport->info.common->transferID, name);
	} else if (isWritePrefetch(inSettings)) {
	    char name[] = "S8";
	    ireport->info.latency_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0,\
							      pow(10,inSettings->mHistUnits), \
							      inSettings->mHistci_lower, inSettings->mHistci_upper, ireport->info.common->transferID, name);
	}
    }
    if ((inSettings->mThreadMode == kMode_Client) && isBounceBack(inSettings)) {
	char name[] = "BB8";
	if (!isHistogram(inSettings)) {
	    inSettings->mHistBins = 100000; // 10 seconds wide
	    inSettings->mHistBinsize = 100; // 100 usec bins
	    inSettings->mHistUnits = 6;  // usecs 10 pow(x)
	    inSettings->mHistci_lower = 5;
	    inSettings->mHistci_upper = 95;
	}
	ireport->info.bbrtt_histogram = histogram_init(inSettings->mHistBins,inSettings->mHistBinsize,0,	\
							pow(10,inSettings->mHistUnits), \
							inSettings->mHistci_lower, inSettings->mHistci_upper, ireport->info.common->transferID, name);
    }
    return reporthdr;
}


/*
 * This init/update and print/finish (in the ReportDefault.c)
 * is poor.  It has to be done this way to preserve the
 * interface to older versions where the reporter settings
 * were delayed until a Transfer report came through.
 * This transfer report has all the reports bound to it.
 *
 * The better implmementation is to treat all reports
 * as independent objects that can be updated, processed,
 * and output independlty per the Reporter threads job queue
 * without shared state or copied state variables between these
 * reports.  The shared state, is really reporter state, that
 * should be maintained in and by the reporter object/thread.
 *
 * For now, just fix it good enough.  Later, write a c++
 * reporter object and use standard c++ design techniques
 * to achieve this.  Such code will be easier to maintain
 * and to extend.
 */
struct ReportHeader* InitConnectionReport (struct thread_Settings *inSettings) {
    assert(inSettings != NULL);
    struct ReportHeader *reporthdr = (struct ReportHeader *) calloc(1, sizeof(struct ReportHeader));
    if (reporthdr == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->this_report = calloc(1, sizeof(struct ConnectionInfo));
    if (reporthdr->this_report == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->type = CONNECTION_REPORT;
    reporthdr->ReportMode = inSettings->mReportMode;

    struct ConnectionInfo * creport = (struct ConnectionInfo *)(reporthdr->this_report);
    common_copy(&creport->common, inSettings);
    tcpstats_copy(&creport->tcpinitstats, &inSettings->tcpinitstats);
    // Fill out known fields for the connection report
    reporter_peerversion(creport, inSettings->peer_version_u, inSettings->peer_version_l);
    if (isEnhanced(inSettings) && isTxStartTime(inSettings)) {
	creport->epochStartTime.tv_sec = inSettings->txstart_epoch.tv_sec;
	creport->epochStartTime.tv_usec = inSettings->txstart_epoch.tv_usec;
    } else if (isTripTime(inSettings)) {
	creport->epochStartTime.tv_sec = inSettings->accept_time.tv_sec;
	creport->epochStartTime.tv_usec = inSettings->accept_time.tv_usec;
    }
    //  Copy state from the settings object into the connection report
    creport->connect_times.min = FLT_MAX;
    creport->connect_times.max = FLT_MIN;
    creport->connect_times.vd = 0;
    creport->connect_times.m2 = 0;
    creport->connect_times.mean = 0;
    if (inSettings->mSock > 0) {
	creport->winsize = getsock_tcp_windowsize(inSettings->mSock,	\
                  (inSettings->mThreadMode != kMode_Client ? 0 : 1) );
#if HAVE_DECL_TCP_WINDOW_CLAMP
	if (isRxClamp(inSettings)) {
	    getsock_tcp_windowclamp(inSettings->mSock);
	}
#endif
    }
    creport->common->winsize_requested = inSettings->mTCPWin;
    creport->txholdbacktime = inSettings->txholdback_timer;
    if (isPeriodicBurst(inSettings)) {
	creport->common->FPS = inSettings->mFPS;
    }
#ifdef HAVE_THREAD_DEBUG
    char rs[REPORTTXTMAX];
    reporttype_text(reporthdr, &rs[0]);
    thread_debug("Init %s report hdr/rpt/com %p/%p/%p", &rs[0],		\
		 (void *) reporthdr, (void *) reporthdr->this_report, (void *) creport->common);
#endif
    return reporthdr;
}

/*
 * ReportSettings will generate a summary report for
 * settings being used with Listeners or Clients
 */
struct ReportHeader *InitSettingsReport (struct thread_Settings *inSettings) {
    assert(inSettings != NULL);
    struct ReportHeader *reporthdr = (struct ReportHeader *) calloc(1, sizeof(struct ReportHeader));
    if (reporthdr == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->this_report = calloc(1, sizeof(struct ReportSettings));
    if (reporthdr->this_report == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->type = SETTINGS_REPORT;
    reporthdr->ReportMode = inSettings->mReportMode;

    struct ReportSettings *sreport = (struct ReportSettings *)reporthdr->this_report;
    common_copy(&sreport->common, inSettings);
    sreport->peer = inSettings->peer;
    sreport->size_peer = inSettings->size_peer;
    sreport->local = inSettings->local;
    sreport->size_local = inSettings->size_local;
    sreport->isochstats.mFPS = inSettings->mFPS;
    sreport->isochstats.mMean = inSettings->mMean/8;
    sreport->isochstats.mVariance = inSettings->mVariance/8;
    sreport->isochstats.mBurstIPG = (unsigned int) (inSettings->mBurstIPG*1000.0);
    sreport->isochstats.mBurstInterval = (unsigned int) (1 / inSettings->mFPS * 1000000);
    if (!isUDP(inSettings)) {
	if (inSettings->mMSS > 0) {
	    sreport->sockmaxseg = inSettings->mMSS;
	} else if (isPrintMSS(inSettings) && !(inSettings->mMSS > 0)) {
	    sreport->sockmaxseg = getsock_tcp_mss(inSettings->mSock);
	}
    }
#ifdef HAVE_THREAD_DEBUG
    char rs[REPORTTXTMAX];
    reporttype_text(reporthdr, &rs[0]);
    thread_debug("Init %s report hdr/rpt/com %p/%p/%p", &rs[0], \
		 (void *) reporthdr, (void *) reporthdr->this_report, (void *) sreport->common);
#endif
    return reporthdr;
}

/*
 * This will generate a report of the UDP
 * statistics as reported by the server on the client
 * side.
 */
struct ReportHeader* InitServerRelayUDPReport(struct thread_Settings *inSettings, struct server_hdr *server) {
    /*
     * Create the report header and an ireport (if needed)
     */
    struct ReportHeader *reporthdr = (struct ReportHeader *) calloc(1, sizeof(struct ReportHeader));
    if (reporthdr == NULL) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
    reporthdr->this_report = calloc(1, sizeof(struct ServerRelay));
    if (!reporthdr->this_report) {
	FAIL(1, "Out of Memory!!\n", inSettings);
    }
#ifdef HAVE_THREAD_DEBUG
    thread_debug("Init server relay report %p size %ld", (void *)reporthdr, sizeof(struct ReportHeader) + sizeof(struct ServerRelay));
#endif
    reporthdr->type = SERVER_RELAY_REPORT;
    reporthdr->ReportMode = inSettings->mReportMode;
    struct ServerRelay *sr_report = (struct ServerRelay *)reporthdr->this_report;
    common_copy(&sr_report->info.common, inSettings);
    struct TransferInfo *stats = &sr_report->info;
    stats->common->transferID = inSettings->mTransferID;

    stats->jitter = ntohl(server->base.jitter1);
    stats->jitter += ntohl(server->base.jitter2) / (double)rMillion;
#ifdef HAVE_INT64_T
    stats->cntBytes = (((intmax_t) ntohl(server->base.total_len1)) << 32) + \
	ntohl(server->base.total_len2);
#else
    stats->cntBytes = (intmax_t) ntohl(server->base.total_len2);
#endif
    stats->ts.iStart = 0;
    stats->ts.iEnd = ntohl(server->base.stop_sec);
    stats->ts.iEnd += ntohl(server->base.stop_usec) / (double)rMillion;
    uint32_t flags = ntohl(server->base.flags);
    if ((flags & HEADER_SEQNO64B)) {
	stats->cntError = (((intmax_t) ntohl(server->extend2.error_cnt2)) << 32) + \
	    ntohl(server->base.error_cnt);
	stats->cntOutofOrder = (((intmax_t) ntohl(server->extend2.outorder_cnt2)) << 32) + \
	    ntohl(server->base.outorder_cnt);
	stats->cntDatagrams = (((intmax_t) ntohl(server->extend2.datagrams2)) << 32) + \
	    ntohl(server->base.datagrams);
    } else {
	stats->cntError  = ntohl(server->base.error_cnt);
	stats->cntOutofOrder = ntohl(server->base.outorder_cnt);
	stats->cntDatagrams = ntohl(server->base.datagrams);
    }
    if ((flags & SERVER_HEADER_EXTEND) != 0) {
	setEnhanced(stats->common);
	stats->transit.current.min = ntohl(server->extend.minTransit1);
	stats->transit.current.min += ntohl(server->extend.minTransit2) / (double)rMillion;
	stats->transit.current.max = ntohl(server->extend.maxTransit1);
	stats->transit.current.max += ntohl(server->extend.maxTransit2) / (double)rMillion;
	stats->transit.current.sum = ntohl(server->extend.sumTransit1);
	stats->transit.current.sum += ntohl(server->extend.sumTransit2) / (double)rMillion;
	stats->transit.current.mean = ntohl(server->extend.meanTransit1);
	stats->transit.current.mean += ntohl(server->extend.meanTransit2) / (double)rMillion;
	stats->transit.current.m2 = ntohl(server->extend.m2Transit1);
	stats->transit.current.m2 += ntohl(server->extend.m2Transit2) / (double)rMillion;
	stats->transit.current.m2 *= 1e-12;
	stats->transit.current.vd = ntohl(server->extend.vdTransit1);
	stats->transit.current.vd += ntohl(server->extend.vdTransit2) / (double)rMillion;
	stats->transit.current.cnt = ntohl(server->extend.cntTransit);
	stats->cntIPG = ntohl(server->extend.cntIPG);
	stats->IPGsum = ntohl(server->extend.IPGsum);
    } else {
	unsetEnhanced(stats->common);
    }
    sr_report->peer = inSettings->local;
    sr_report->size_peer = inSettings->size_local;
    sr_report->local = inSettings->peer;
    sr_report->size_local = inSettings->size_peer;
    return reporthdr;
}

/* -------------------------------------------------------------------
 * Send an AckFIN (a datagram acknowledging a FIN) on the socket,
 * then select on the socket for some time to check for silence.
 * If additional datagrams come in (not silent), probably our AckFIN
 * was lost so the client has re-transmitted
 * termination datagrams, so re-transmit our AckFIN.
 * Sent by server to client
 * ------------------------------------------------------------------- */
void write_UDP_AckFIN (struct TransferInfo *stats, int len) {
    assert(stats!= NULL);
    int ackpacket_length = (int) (sizeof(struct UDP_datagram) + sizeof(struct server_hdr));
    int readlen = ((ackpacket_length * 2) > len * 2) ? (ackpacket_length * 2) : (len * 2);
    char *ackPacket = (char *) calloc(1, readlen);
    int success = 0;
    assert(ackPacket);
    fd_set readSet;
    int rc = 1;
    struct timeval timeout;

    if (ackPacket) {
	struct UDP_datagram *UDP_Hdr = (struct UDP_datagram *)ackPacket;
	struct server_hdr *hdr = (struct server_hdr *)(UDP_Hdr+1);

	UDP_Hdr = (struct UDP_datagram*) ackPacket;
	int flags = HEADER_VERSION1;
	if (isEnhanced(stats->common) || isTripTime(stats->common))
	    flags |= SERVER_HEADER_EXTEND;
#ifdef HAVE_INT64_T
	flags |=  HEADER_SEQNO64B;
#endif
	hdr->base.flags        = htonl((long) flags);
#ifdef HAVE_INT64_T
	hdr->base.total_len1   = htonl((long) (stats->cntBytes >> 32));
#else
	hdr->base.total_len1   = htonl(0x0);
#endif
	hdr->base.total_len2   = htonl((long) (stats->cntBytes & 0xFFFFFFFF));
        hdr->base.stop_sec     = htonl( (long) stats->ts.iEnd);
        hdr->base.stop_usec    = htonl( (long)((stats->ts.iEnd - (long)stats->ts.iEnd) * rMillion));
	hdr->base.error_cnt    = htonl((long) (stats->cntError & 0xFFFFFFFF));
	hdr->base.outorder_cnt = htonl((long) (stats->cntOutofOrder  & 0xFFFFFFFF));
	hdr->base.datagrams    = htonl((long) (stats->cntDatagrams & 0xFFFFFFFF));
	if (flags & HEADER_SEQNO64B) {
	    hdr->extend2.error_cnt2    = htonl((long) (stats->cntError >> 32));
	    hdr->extend2.outorder_cnt2 = htonl((long) (stats->cntOutofOrder >> 32) );
	    hdr->extend2.datagrams2    = htonl((long) (stats->cntDatagrams >> 32));
	}
	//	printf("****** Server final estimator %f calculated average %f\n", stats->jitter, (stats->inline_jitter.total.sum / stats->inline_jitter.total.cnt));
	if (stats->inline_jitter.total.cnt > 0)
	    stats->jitter = (stats->inline_jitter.total.sum / stats->inline_jitter.total.cnt); // overide the final estimator with an average
	hdr->base.jitter1      = htonl((long) stats->jitter);
	hdr->base.jitter2      = htonl((long) ((stats->jitter - (long)stats->jitter) * rMillion));

	hdr->extend.minTransit1  = htonl((long) stats->transit.total.min);
	hdr->extend.minTransit2  = htonl((long) ((stats->transit.total.min - (long)stats->transit.total.min) * rMillion));
	hdr->extend.maxTransit1  = htonl((long) stats->transit.total.max);
	hdr->extend.maxTransit2  = htonl((long) ((stats->transit.total.max - (long)stats->transit.total.max) * rMillion));
	hdr->extend.sumTransit1  = htonl((long) stats->transit.total.sum);
	hdr->extend.sumTransit2  = htonl((long) ((stats->transit.total.sum - (long)stats->transit.total.sum) * rMillion));
	hdr->extend.meanTransit1  = htonl((long) stats->transit.total.mean);
	hdr->extend.meanTransit2  = htonl((long) ((stats->transit.total.mean - (long)stats->transit.total.mean) * rMillion));
	stats->transit.total.m2 *= 1e12;
	hdr->extend.m2Transit1  = htonl((long) stats->transit.total.m2);
	hdr->extend.m2Transit2  = htonl((long) ((stats->transit.total.m2 - (long)stats->transit.total.m2) * rMillion));
	hdr->extend.vdTransit1  = htonl((long) stats->transit.total.vd);
	hdr->extend.vdTransit2  = htonl((long) ((stats->transit.total.vd - (long)stats->transit.total.vd) * rMillion));
	hdr->extend.cntTransit   = htonl(stats->transit.total.cnt);
	hdr->extend.cntIPG = htonl((long) (stats->cntDatagrams / (stats->ts.iEnd - stats->ts.iStart)));
	hdr->extend.IPGsum = htonl(1);

#define TRYCOUNT 10
	int count = TRYCOUNT;
	while (--count) {
	    // write data
#if defined(HAVE_LINUX_FILTER_H) && defined(HAVE_AF_PACKET)
	    // If in l2mode, use the AF_INET socket to write this packet
	    //
#ifdef HAVE_THREAD_DEBUG
	    thread_debug("UDP server send done-ack w/server-stats to client (sock=%d)", stats->common->socket);
#endif
	    rc = write(((stats->common->socketdrop > 0) ? stats->common->socketdrop : stats->common->socket), ackPacket, ackpacket_length);
#else
	    rc = write(stats->common->socket, ackPacket, ackpacket_length);
#endif
	    WARN_errno(rc < 0, "write-ackfin");
	    // wait here is for silence, no more packets from the client

	    FD_ZERO(&readSet);
	    FD_SET(stats->common->socket, &readSet);
	    timeout.tv_sec  = 0;
	    timeout.tv_usec = 250000;
	    rc = select(stats->common->socket+1, &readSet, NULL, NULL, &timeout);
	    if (rc == 0) {
#ifdef HAVE_THREAD_DEBUG
		thread_debug("UDP server detected silence - server stats assumed received by client");
#endif
		success = 1;
		break;
	    }
	    rc = read(stats->common->socket, ackPacket, readlen);
	    // WARN_errno(rc < 0, "ack await silence");
	    if ((rc < 0) && FATALUDPREADERR(errno)) {
		break;
	    }
#ifdef HAVE_THREAD_DEBUG
	    if (rc > 0) {
		thread_debug("UDP server thinks server stats packet maybe lost, will retransmit and try again", rc);
	    }
#endif
	}
	free(ackPacket);
    }
    if (!success && (stats->common->ReportMode != kReport_CSV)) {
	fprintf(stderr, warn_ack_failed, stats->common->socket);
    }
}
// end write_UDP_AckFIN