-
Notifications
You must be signed in to change notification settings - Fork 7
/
bench3.c
151 lines (131 loc) · 3.1 KB
/
bench3.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
#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 <ck_spinlock.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/syscall.h>
#include "rdtscp.h"
#include "util.h"
#define ITERATIONS 500000
#define RING_SIZE 8
uint64_t times[ITERATIONS * RING_SIZE];
int do_send(int in, int out) {
uint64_t start, end;
long numbers[RING_SIZE];
long long sum = 0, retsum = 0;
long long i, x, p;
// 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++) {
syscall(SYS_msgsnd, out, &numbers[i % RING_SIZE], 0 /* API automatically adds sizeof(long) */, 0);
syscall(SYS_msgrcv, in, &p, 0 /* API automatically adds sizeof(long) */, 0, 0);
retsum = retsum + p;
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 in, int out) {
long p;
int i;
for (i = 0; i < (ITERATIONS * RING_SIZE); i++) {
syscall(SYS_msgrcv, in, &p, sizeof(p) - sizeof(long), 0, 0);
p = p * 2;
syscall(SYS_msgsnd, out, &p, sizeof(p) - sizeof(long), 0);
}
return 0;
}
#define MODE_SEND 0
#define MODE_RECV 1
int main(int argc, char *argv[]) {
struct rusage usage_start, usage_end;
key_t ingress, egress;
int in, out;
int mode;
int ret;
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;
}
ingress = ftok(".", 'i');
egress = ftok(".", 'o');
if (mode == MODE_RECV)
goto work;
ret = msgctl(ingress, IPC_RMID, 0);
if (ret && errno != EINVAL) {
perror("unlink ingress");
return ret;
}
ret = msgctl(egress, IPC_RMID, 0);
if (ret && errno != EINVAL) {
perror("unlink egress");
return ret;
}
work:
in = msgget(ingress, IPC_CREAT | 0777);
if (in <= 0) {
perror("msgget in");
return 1;
}
out = msgget(egress, IPC_CREAT | 0777);
if (out <= 0) {
perror("msgget in");
return 1;
}
getrusage(RUSAGE_SELF, &usage_start);
switch (mode) {
case MODE_SEND:
ret = do_send(in, out);
break;
case MODE_RECV:
ret = do_recv(out, in);
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;
}