-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.c
291 lines (243 loc) · 7.89 KB
/
util.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "util.h"
/* Measure the time it takes to access a block with virtual address addr. */
extern inline __attribute__((always_inline))
uint64_t measure_one_block_access_time(ADDR_PTR addr) {
uint64_t cycles;
asm volatile("mov %1, %%r8\n\t"
"lfence\n\t"
"rdtsc\n\t"
"lfence\n\t"
"mov %%eax, %%edi\n\t"
"mov (%%r8), %%r8\n\t"
"lfence\n\t"
"rdtsc\n\t"
"lfence\n\t"
"sub %%edi, %%eax\n\t"
: "=a"(cycles) /*output*/
: "r"(addr)
: "r8", "edi");
return cycles;
}
/*
* CLFlushes the given address.
*/
extern inline __attribute__((always_inline))
void clflush(ADDR_PTR addr) {
asm volatile ("clflush (%0)"::"r"(addr));
}
extern inline __attribute__((always_inline))
uint64_t rdtsc() {
uint64_t a, d;
asm volatile ("lfence");
asm volatile ("rdtsc" : "=a" (a), "=d" (d));
asm volatile ("lfence");
return (d << 32) | a;
}
extern inline __attribute__((always_inline))
CYCLES rdtscp(void) {
CYCLES cycles;
asm volatile ("rdtscp"
: /* outputs */ "=a" (cycles));
return cycles;
}
inline uint64_t get_time() {
// can be a choice of channel?
return rdtsc();
}
extern inline __attribute__((always_inline))
uint64_t cc_sync() {
while((get_time() & CHANNEL_SYNC_TIMEMASK) > CHANNEL_SYNC_JITTER) {}
return get_time();
}
/*
* Computes base to the exp.
*/
int ipow(int base, int exp)
{
int result = 1;
while (exp) {
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
/*
* Returns the 11 bits used index a LLC set in a slice of a given address.
*/
uint64_t get_cache_slice_set_index(ADDR_PTR virt_addr) {
// return (virt_addr >> LOG_CACHE_LINESIZE) & CACHE_SETS_L1_MASK;
return (virt_addr >> LOG_CACHE_LINESIZE) & (2048-1);
}
uint64_t get_L3_cache_set_index(ADDR_PTR virt_addr) {
return (virt_addr >> LOG_CACHE_LINESIZE) & CACHE_SETS_L3_MASK;
// return (virt_addr >> LOG_CACHE_LINESIZE) & (2048-1);
}
/*
* Returns the 15 physical bits of a given virtual address in a hugepage.
*/
// uint64_t get_hugepage_cache_set_index(ADDR_PTR virt_addr)
// {
// return (virt_addr & HUGEPAGE_MASK) >> LOG_CACHE_LINESIZE;
// }
/*
* Allocate a buffer of the size as passed-in
* returns the pointer to the buffer
*/
void *allocate_buffer(uint64_t size) {
void *buffer = MAP_FAILED;
#ifdef HUGEPAGES
buffer = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|HUGEPAGES, -1, 0);
#endif
if (buffer == MAP_FAILED) {
fprintf(stderr, "WARNING: allocating non-hugepages\n");
buffer = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
}
if (buffer == MAP_FAILED) {
fprintf(stderr, "Failed to allocate buffer!\n");
exit(-1);
}
return buffer;
}
/*
* Convert a given ASCII string to a binary string.
* From:
* https://stackoverflow.com/questions/41384262/convert-string-to-binary-in-c
*/
char *string_to_binary(char *s)
{
if (s == NULL) return 0; /* no input string */
size_t len = strlen(s) - 1;
// Each char is one byte (8 bits) and + 1 at the end for null terminator
char *binary = malloc(len * 8 + 1);
binary[0] = '\0';
for (size_t i = 0; i < len; ++i) {
char ch = s[i];
for (int j = 7; j >= 0; --j) {
if (ch & (1 << j)) {
strcat(binary, "1");
} else {
strcat(binary, "0");
}
}
}
return binary;
}
/*
* Convert 8 bit data stream into character and return
*/
char *conv_msg(char *data, int size, char *msg) {
for (int i = 0; i < size; i++) {
char _tmp = 0;
for (int j = i * 8; j < ((i + 1) * 8); j++) {
_tmp = (_tmp << 1) + data[j] - '0';
}
msg[i] = _tmp;
}
msg[size] = '\0';
return msg;
}
/*
* Appends the given string to the linked list which is pointed to by the given head
*/
void append_string_to_linked_list(struct Node **head, ADDR_PTR addr)
{
struct Node *current = *head;
// Create the new node to append to the linked list
struct Node *new_node = malloc(sizeof(*new_node));
new_node->addr = addr;
new_node->next = NULL;
// If the linked list is empty, just make the head to be this new node
if (current == NULL)
*head = new_node;
// Otherwise, go till the last node and append the new node after it
else {
while (current->next != NULL)
current = current->next;
current->next = new_node;
}
}
uint64_t print_pid() {
uint64_t pid = getpid();
printf("Process ID: %lu\n", pid);
return pid;
}
void print_help() {
printf("======================= H E L P ===============================\n");
printf("-c: (uint 0 to 2) to select a channel\n");
printf("-i: (uint) to specify a interval for each bit transmission\n");
printf("-p: (uint) to specify a time period for prime (for llc-pp)\n");
printf("-a: (uint) to specify a time period for access (for llc-pp)\n");
printf("-r: (uint) to specify a LLC cache set to contend on\n");
printf("-b: to start benchmark mode (default is chat mode)\n");
printf("-h: to print this message\n");
printf("===============================================================\n");
}
void init_default(struct config *config, int argc, char **argv) {
config->buffer = NULL;
config->addr_set = NULL;
// Cache region specifies the targeted set
config->cache_region = CHANNEL_DEFAULT_REGION;
// Interval specifies the time used to send a single bit
config->interval = CHANNEL_DEFAULT_INTERVAL;
// Prime+Probe specific paramters:
config->access_period = CHANNEL_DEFAULT_PERIOD;
config->prime_period = CHANNEL_DEFAULT_PERIOD;
// Flush+Reload specific paramters:
config->shared_filename = "shared.txt";
config->benchmark_mode = false;
config->channel = PrimeProbe;
int option;
while ((option = getopt(argc, argv, "c:i:p:a:r:bh")) != -1) {
switch (option) {
case 'c':
// value 0,1,2 to select channel
config->channel = atoi(optarg);
break;
case 'i':
config->interval = atoi(optarg);
break;
case 'p':
config->prime_period = atoi(optarg);
break;
case 'a':
config->access_period = atoi(optarg);
break;
case 'r':
config->cache_region = atoi(optarg);
break;
case 'b':
config->benchmark_mode = true;
break;
case '?':
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
case 'h':
default:
print_help();
exit(1);
}
}
if (config->channel == PrimeProbe || config->channel == L1DPrimeProbe) {
config->miss_threshold = config->channel == PrimeProbe?
CHANNEL_L3_MISS_THRESHOLD:
CHANNEL_L1_MISS_THRESHOLD;
if (config->interval < config->prime_period + config->access_period) {
fprintf(stderr, "ERROR: P+P channel bit interval too short!\n");
exit(-1);
}
else {
config->probe_period = config->interval - config->prime_period - config->access_period;
}
}
// debug("prime %u access %u probe %u\n", config->prime_period, config->access_period, config->probe_period);
if (config->channel == FlushReload) {
config->access_period = CHANNEL_FR_DEFAULT_INTERVAL;
config->access_period = CHANNEL_FR_DEFAULT_PERIOD;
config->miss_threshold = CHANNEL_L1_MISS_THRESHOLD;
if (config->cache_region > 63) {
fprintf(stderr, "ERROR: F+R channel region should be within a 4K page (64lines)!\n");
exit(-1);
}
}
}