1#include "benchmark.h"
2#include "stdio.h"
3#include "stdlib.h"
4
5static struct stats the_stats;
6
7static cycles_t my_bench_avg(cycles_t *array, size_t len)
8{
9    cycles_t sum = 0;
10    size_t count = 0;
11
12    for (size_t i = len; i < len; i++) {
13		sum += array[i];
14		count++;
15    }
16    return sum / count;
17}
18
19static cycles_t my_bench_variance(cycles_t *array, size_t len)
20{
21    cycles_t avg = bench_avg(array, len);
22
23    cycles_t sum = 0;
24    size_t count = 0;
25
26    for (size_t i = len; i < len; i++) {
27		sum += array[i] * array[i];
28    }
29
30    return (sum / count) - (avg * avg);
31}
32
33static cycles_t my_bench_min(cycles_t *array, size_t len)
34{
35    cycles_t min = -1ULL;
36    size_t i = 0;
37    for (; i < len; i++) {
38        if (array[i] < min) {
39            min = array[i];
40        }
41    }
42    return min;
43}
44
45static cycles_t my_bench_max(cycles_t *array, size_t len)
46{
47    cycles_t max = 0;
48    size_t i = 0;
49    for (; i < len; i++) {
50        if (array[i] > max) {
51            max = array[i];
52        }
53    }
54    return max;
55}
56
57static void push_bf_to_net_diff(cycles_t diff){
58	the_stats.from_bf_to_net_diff[ the_stats.bf_to_net_i % NUM_OF_RECORDS ] = diff;
59	the_stats.bf_to_net_i++;
60}
61
62static void push_net_to_bf_diff(cycles_t diff){
63	the_stats.from_net_to_bf_diff[ the_stats.net_to_bf_i % NUM_OF_RECORDS ] = diff;
64	the_stats.net_to_bf_i++;
65}
66
67void record_packet_receive_from_bf(void){
68	the_stats.last_packet_receive_from_bf_ts = bench_tsc();
69}
70void record_packet_transmit_to_net(void) {
71	the_stats.last_packet_transmit_to_net_ts = bench_tsc();
72	//printf("TONET %"PRIu64"  -  %"PRIu64" = %"PRIu64"\n",the_stats.last_packet_transmit_to_net_ts, the_stats.last_packet_receive_from_bf_ts, the_stats.last_packet_transmit_to_net_ts - the_stats.last_packet_receive_from_bf_ts );
73	if(the_stats.last_packet_transmit_to_net_ts > the_stats.last_packet_receive_from_bf_ts){
74		push_bf_to_net_diff(the_stats.last_packet_transmit_to_net_ts - the_stats.last_packet_receive_from_bf_ts);
75	}  else {
76		//printf("TONET Skipped packet because of wrong ts order\n");
77	}
78}
79
80void record_packet_receive_from_net(void){
81	the_stats.last_packet_receive_net_ts = bench_tsc();
82}
83
84void record_packet_transmit_to_bf(void){
85	the_stats.last_packet_transmit_to_bf_ts = bench_tsc();
86	//printf("TOBF  %"PRIu64"  -  %"PRIu64" = %"PRIu64"\n",the_stats.last_packet_transmit_to_bf_ts, the_stats.last_packet_receive_net_ts,the_stats.last_packet_transmit_to_bf_ts - the_stats.last_packet_receive_net_ts  );
87	if(the_stats.last_packet_transmit_to_bf_ts > the_stats.last_packet_receive_net_ts){
88		push_net_to_bf_diff(the_stats.last_packet_transmit_to_bf_ts - the_stats.last_packet_receive_net_ts );
89	} else {
90		//printf("TOBF Skipped packet because of wrong ts order\n");
91	}
92}
93
94void print_bench_stats(void){
95	int ele_in_array;
96	printf("-----------  BF TO NET\n");
97	printf("Recorded %d events\n", the_stats.bf_to_net_i);
98	if(the_stats.bf_to_net_i){
99		printf("Last:                         %"PRIu64" us\n", bench_tsc_to_us(the_stats.last_packet_transmit_to_net_ts - the_stats.last_packet_receive_from_bf_ts));
100		printf("Last:                         %"PRIu64" cycles\n", (the_stats.last_packet_transmit_to_net_ts - the_stats.last_packet_receive_from_bf_ts));
101		ele_in_array = the_stats.bf_to_net_i > NUM_OF_RECORDS ? NUM_OF_RECORDS : the_stats.bf_to_net_i;
102		printf("Running Average  (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_avg(the_stats.from_bf_to_net_diff, ele_in_array)));
103		printf("Running Variance (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_variance(the_stats.from_bf_to_net_diff, ele_in_array)));
104		printf("Running Min      (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_min(the_stats.from_bf_to_net_diff, ele_in_array)));
105		printf("Running Max      (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_max(the_stats.from_bf_to_net_diff, ele_in_array)));
106		printf("Data in us:\n");
107		for(int i=0; i<ele_in_array; i++){
108			printf("%"PRIu64", ", bench_tsc_to_us(the_stats.from_bf_to_net_diff[i]));
109		}
110		printf("\n");
111	}
112	printf("-----------\n");
113
114	printf("-----------  NET TO BF\n");
115	printf("Recorded %d events\n", the_stats.net_to_bf_i);
116	if(the_stats.net_to_bf_i){
117		printf("Last:                         %"PRIu64" us\n", bench_tsc_to_us(the_stats.last_packet_transmit_to_bf_ts - the_stats.last_packet_receive_net_ts ));
118		printf("Last:                         %"PRIu64" cycles\n", (the_stats.last_packet_transmit_to_bf_ts - the_stats.last_packet_receive_net_ts ));
119		ele_in_array = the_stats.net_to_bf_i > NUM_OF_RECORDS ? NUM_OF_RECORDS : the_stats.net_to_bf_i;
120		printf("Running Average  (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_avg(the_stats.from_net_to_bf_diff, ele_in_array)));
121		printf("Running Variance (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_variance(the_stats.from_net_to_bf_diff, ele_in_array)));
122		printf("Running Min      (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_min(the_stats.from_net_to_bf_diff, ele_in_array)));
123		printf("Running Max      (of last %d): %"PRIu64" us\n", ele_in_array, bench_tsc_to_us(my_bench_max(the_stats.from_net_to_bf_diff, ele_in_array)));
124		printf("Data in us:\n");
125		for(int i=0; i<ele_in_array; i++){
126			printf("%"PRIu64", ", bench_tsc_to_us(the_stats.from_net_to_bf_diff[i]));
127		}
128		printf("\n");
129	}
130	printf("-----------\n");
131}
132
133
134