-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc_ids_key_ht.c
191 lines (166 loc) · 4.94 KB
/
ipc_ids_key_ht.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
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
#include "cacheutils.h"
#include "helper.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define KEY_MEASURE 0xbadc0fe
#define KEY_START 0xdeadbeef
#define KEY_WARMUP 0xbadbabe
#define REPEAT_MEASUREMENT 1024
#define AVERAGE (1<<3)
#define OFFSET 2
#define KEYS 8*1024
#define TRIES 8
#define WARMUP_KEYS 4096
size_t msgiqs[KEYS];
int compare(const void *a, const void *b)
{
return (*(size_t *)a - *(size_t *)b);
}
void cleanup(void)
{
printf("[*] cleanup\n");
for (size_t i = 0; i < KEYS; ++i) {
msgctl(msgiqs[i], IPC_RMID, NULL);
}
}
void print_usage(char *name)
{
printf("%s <struct_agnostic_amp=0> <core=7> <file_name=futex_hash_table_%%s.csv>\n", name);
exit(0);
}
char *name = 0;
size_t software_agnostic_amplification = 0;
size_t core = 7;
int main(int argc, char **argv)
{
static volatile char flush_cache[(1<<10)*(1<<7)];
if (argc > 4)
print_usage(argv[0]);
software_agnostic_amplification = (argc > 1) && strcmp(argv[1], "0");
if (argc > 2)
core = atoi(argv[2]);
if (argc > 3)
name = argv[3];
helper_init();
pin_to_core(core);
printf("[*] warmup\n");
for (volatile size_t i = 0; i < 0x400000000; ++i);
int ret;
size_t times[KEYS];
size_t times_std[KEYS];
size_t ground_truth[KEYS];
// size_t warmup_msgiqs[WARMUP_KEYS];
// printf("[*] allocate warmup keys\n");
// for (size_t i = 0; i < WARMUP_KEYS; ++i) {
// ret = msgget(KEY_WARMUP+i, IPC_CREAT);
// if (ret < 0) {
// printf("[!] i %ld\n", i);
// perror("msgget(KEY_WARMUP+i, IPC_CREAT)");
// exit(-1);
// }
// warmup_msgiqs[i] = ret;
// }
printf("[*] start measure\n");
for (size_t i = 0; i < KEYS; ++i) {
if (i % 16 == 0)
printf("[*] %ld/%d\n", i, KEYS);
ret = msgget(KEY_START+i, IPC_CREAT);
if (ret < 0) {
printf("[!] i %ld\n", i);
perror("msgget(KEY_START+i, IPC_CREAT)");
cleanup();
exit(-1);
}
msgiqs[i] = ret;
size_t tries = TRIES;
redo:
if (tries-- == 0) {
printf("[!] no more tries\n");
cleanup();
exit(-1);
}
size_t t0;
size_t t1;
size_t time = 0;
size_t measured_times[REPEAT_MEASUREMENT];
for (size_t j = 0; j < REPEAT_MEASUREMENT; ++j) {
sched_yield();
if (software_agnostic_amplification)
memset((char *)flush_cache, 1, sizeof(flush_cache));
t0 = rdtsc_begin();
ret = msgget(KEY_MEASURE+i, 0);
// ret = msgget(KEY_START+i, IPC_CREAT | IPC_EXCL);
t1 = rdtsc_end();
measured_times[j] = t1 - t0;
if (ret != -1) {
printf("[!] i %ld ret %d\n", i, ret);
perror("msgget(KEY_MEASURE+i, 0)");
// perror("msgget(KEY_START+i, IPC_CREAT | IPC_EXCL)");
cleanup();
exit(-1);
}
}
qsort(measured_times, REPEAT_MEASUREMENT, sizeof(size_t), compare);
for (size_t i = 0; i < AVERAGE; ++i)
time += measured_times[OFFSET+i];
time /= AVERAGE;
times[i] = time;
times_std[i] = measured_times[AVERAGE-1+OFFSET] - measured_times[OFFSET];
if (times_std[i] > 20/*2*/) {
printf("[*] %ld/%d\n", tries, TRIES);
goto redo;
}
ret = key_read_index((size_t)&ground_truth[i], KEY_MEASURE+i);
if (ret < 0) {
printf("[!] key_read_index error %d\n", ret);
cleanup();
exit(-1);
}
}
printf("[*] print results\n");
for (size_t i = 0; i < KEYS; ++i) {
if (i % 16 == 0)
printf("[%4zd] ", i);
// printf("%4zd(%3zd) [%zd]", times[i], times_std[i], ground_truth[i]);
printf("%4zd [%zd]", times[i], ground_truth[i]);
if (i % 16 == 15)
printf("\n");
}
cleanup();
if (name == 0) {
printf("[?] save to file? [y/N] ");
char in = getchar();
if (in != 'y')
exit(0);
}
time_t current_time;
struct tm *timeinfo;
char buffer[80];
time(¤t_time);
timeinfo = localtime(¤t_time);
strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S", timeinfo);
if (name == 0) {
static char file_name[0x1000];
snprintf(file_name, sizeof(file_name), "ipc_ids_key_ht_%s.csv", buffer);
name = file_name;
}
FILE *csv_file = fopen(name, "w");
if (csv_file == NULL) {
printf("[!] fopen\n");
return 1;
}
// Write headers
fprintf(csv_file, "ground truth; time\n");
for (size_t i = 0; i < KEYS; ++i) {
fprintf(csv_file, "%ld; %ld\n", ground_truth[i], times[i]);
}
fclose(csv_file);
printf("[+] done\n");
return 0;
}