-
Notifications
You must be signed in to change notification settings - Fork 7
/
bench4.c
157 lines (135 loc) · 3.02 KB
/
bench4.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
#include <sched.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "rdtscp.h"
#include "util.h"
#define ITERATIONS 500000
#define RING_SIZE 8
struct payload {
__u64 val;
};
uint64_t times[ITERATIONS * RING_SIZE];
int do_send(int fd) {
uint64_t start, end;
int numbers[RING_SIZE];
long long sum = 0, retsum = 0;
struct payload p;
long long i, x;
// Zero-out all the memory
memset(times, 0, sizeof(times));
for (i = 0; i < RING_SIZE; i++) {
x = rand() % 100;
sum = sum + x;
numbers[i] = x;
}
sum = sum * ITERATIONS;
start = rdtscp();
for (i = 0; i < (ITERATIONS * RING_SIZE); i++) {
p.val = numbers[i % RING_SIZE];
send(fd, &p, sizeof(p), 0);
recv(fd, &p, sizeof(p), 0);
retsum = retsum + p.val;
end = rdtscp();
times[i] = end - start;
start = end;
}
/* Ignore the first sample */
uint64_t *times2 = ×[1];
sort_uint64_t_array(times2, ARRAY_SIZE(times) - 1);
printf("Median Iteration Time: %lu\n", times[(ARRAY_SIZE(times) - 1)/2]);
printf("Min Time: %lu\n", times2[0]);
printf("95th percentile Time: %lu\n", times2[P(95, times)]);
if (retsum != (sum * 2)) {
printf("Something broke\n");
printf("Sum: %llu\n", sum);
printf("RetSum: %llu\n", retsum);
return 1;
}
return 0;
}
int do_recv(int fd) {
struct payload p;
int i;
for (i = 0; i < (ITERATIONS * RING_SIZE); i++) {
recv(fd, &p, sizeof(p), 0);
p.val = p.val * 2;
send(fd, &p, sizeof(p), 0);
}
return 0;
}
#define MODE_SEND 0
#define MODE_RECV 1
int main(int argc, char *argv[]) {
struct sockaddr_un sockaddr = {
.sun_family = AF_UNIX,
.sun_path = "ipc",
};
struct rusage usage_start, usage_end;
int mode;
int ret;
int fd, fd2;
memset(times, 0, sizeof(times));
if (argc != 2) {
printf("Mode?\n");
return 1;
}
switch (argv[1][0]) {
case 's':
mode = MODE_SEND;
break;
case 'r':
mode = MODE_RECV;
break;
default:
printf("Wat?\n");
return 1;
}
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd <= 0) {
perror("socket");
return 1;
}
getrusage(RUSAGE_SELF, &usage_start);
switch (mode) {
case MODE_SEND:
unlink(sockaddr.sun_path);
if (bind(fd, (const struct sockaddr *)&sockaddr, sizeof(struct sockaddr))) {
perror("bind");
goto send_fail;
}
if (listen(fd, 1)) {
perror("listen");
goto send_fail;
}
fd2 = accept(fd, NULL, NULL);
ret = do_send(fd2);
break;
case MODE_RECV:
if (connect(fd, (const struct sockaddr *)&sockaddr, sizeof(struct sockaddr))) {
perror("connect");
return 1;
}
ret = do_recv(fd);
break;
}
getrusage(RUSAGE_SELF, &usage_end);
printf("Invol Ctx Switches: %ld\nVoluntary Ctx Switches: %ld\n", usage_end.ru_nivcsw - usage_start.ru_nivcsw, usage_end.ru_nvcsw - usage_start.ru_nvcsw);
return ret;
send_fail:
unlink(sockaddr.sun_path);
return 1;
}