-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbench2.c
128 lines (103 loc) · 3.04 KB
/
bench2.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
#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 "util.h"
#include "rdtscp.h"
#define RING_SIZE 1024
#define ITERATIONS 200000
struct payload {
ck_spinlock_fas_t spinlock;
__u8 val;
} __attribute__((aligned(64)));
struct shm_mem {
// Even entries are used to send, odds are used to reply
struct payload payloads[RING_SIZE * 2];
} __attribute__((aligned(64)));
#define SHM_SIZE sizeof(struct shm_mem)
int setup_fd(const char *name) {
int fd;
fd = shm_open(name, O_RDWR|O_CREAT, 0777);
if (fd == -1) {
perror("shm_open");
return -1;
}
return fd;
}
uint64_t times[RING_SIZE * ITERATIONS];
void bench2(struct shm_mem *ptr) {
struct rusage usage_start, usage_end;
struct payload *snd_slot, *rcv_slot;
uint64_t start, end;
int numbers[RING_SIZE];
long long sum = 0, retsum = 0;
long long total_cycles = 0;
long long i, x;
/* Zero-out all the memory to avoid page faults */
memset(ptr, 0, sizeof(struct shm_mem));
memset(times, 0, sizeof(times));
for (i = 0; i < RING_SIZE * 2; i++) {
ck_spinlock_fas_init(&ptr->payloads[i].spinlock);
ck_spinlock_fas_lock(&ptr->payloads[i].spinlock);
}
for (i = 0; i < RING_SIZE; i++) {
x = rand() % 100;
sum = sum + x;
numbers[i] = x;
}
sum = sum * ITERATIONS;
assert(!getrusage(RUSAGE_SELF, &usage_start));
start = rdtscp();
for (i = 0; i < (ITERATIONS * RING_SIZE); i++) {
snd_slot = &ptr->payloads[(i % RING_SIZE) * 2];
rcv_slot = &ptr->payloads[((i % RING_SIZE) * 2) + 1];
snd_slot->val = numbers[i % RING_SIZE];
ck_spinlock_fas_unlock(&snd_slot->spinlock);
/* Receive Side */
ck_spinlock_fas_lock(&snd_slot->spinlock);
rcv_slot->val = snd_slot->val * 2;
ck_spinlock_fas_unlock(&rcv_slot->spinlock);
/* Receive Side */
ck_spinlock_fas_lock(&rcv_slot->spinlock);
retsum = retsum + rcv_slot->val;
/* Avoid calling rdtscp twice, so just move the value over */
end = rdtscp();
times[i] = end - start;
start = end;
}
assert(!getrusage(RUSAGE_SELF, &usage_end));
sort_uint64_t_array(times, ARRAY_SIZE(times));
for (int i = 0; i < ARRAY_SIZE(times); i++)
total_cycles = total_cycles + times[i];
printf("Average cycles: %llu\n", total_cycles/ARRAY_SIZE(times));
printf("Median Iteration Cycles: %lu\n", times[ARRAY_SIZE(times)/2]);
printf("Min Cycles: %lu\n", times[0]);
printf("95th Percentile Cycles: %lu\n", times[P(95, times)]);
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);
if (retsum != (sum * 2)) {
printf("Something broke\n");
printf("Sum: %llu\n", sum);
printf("RetSum: %llu\n", retsum);
}
}
int main(int argc, char *argv[]) {
struct shm_mem *ptr;
ptr = (struct shm_mem*) malloc(SHM_SIZE);
if (!ptr) {
perror("malloc");
return 1;
}
memset(times, 0, sizeof(times));
bench2(ptr);
return 0;
}