-
Notifications
You must be signed in to change notification settings - Fork 26
/
mc-mapping-pagemap.c
309 lines (259 loc) · 8.09 KB
/
mc-mapping-pagemap.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* DRAM controller address mapping detector
*
* Copyright (C) 2013 Heechul Yun <[email protected]>
* Copyright (C) 2018 Heechul Yun <[email protected]>
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
/**************************************************************************
* Conditional Compilation Options
**************************************************************************/
/**************************************************************************
* Included Files
**************************************************************************/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sched.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <assert.h>
#include <sys/sysinfo.h>
#include <inttypes.h>
#include <pthread.h>
/**************************************************************************
* Public Definitions
**************************************************************************/
#define MAX_BIT (24) // [27:23] bits are used for iterations
#define MAX(a,b) ((a>b)?(a):(b))
#define MIN(a,b) ((a>b)?(b):(a))
#define CEIL(val,unit) (((val + unit - 1)/unit)*unit)
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
/**************************************************************************
* Public Types
**************************************************************************/
/**************************************************************************
* Global Variables
**************************************************************************/
long g_mem_size;
double g_fraction_of_physical_memory = 0.2;
int g_cache_num_ways = 16;
void *g_mapping;
ulong *g_frame_phys;
int g_cpuid = 0;
int g_pagemap_fd;
/**************************************************************************
* Public Function Prototypes
**************************************************************************/
uint64_t get_elapsed(struct timespec *start, struct timespec *end)
{
uint64_t dur;
if (start->tv_nsec > end->tv_nsec)
dur = (uint64_t)(end->tv_sec - 1 - start->tv_sec) * 1000000000 +
(1000000000 + end->tv_nsec - start->tv_nsec);
else
dur = (uint64_t)(end->tv_sec - start->tv_sec) * 1000000000 +
(end->tv_nsec - start->tv_nsec);
return dur;
}
// ----------------------------------------------
size_t getPhysicalMemorySize() {
struct sysinfo info;
sysinfo(&info);
return (size_t) info.totalram * (size_t) info.mem_unit;
}
// ----------------------------------------------
size_t frameNumberFromPagemap(size_t value) {
return value & ((1ULL << 54) - 1);
}
// ----------------------------------------------
ulong getPhysicalAddr(ulong virtual_addr)
{
u_int64_t value;
off_t offset = (virtual_addr / 4096) * sizeof(value);
int got = pread(g_pagemap_fd, &value, 8, offset);
//printf("vaddr=%lu, value=0x%llx, got=%d\n", virtual_addr, value, got);
assert(got == 8);
// Check the "page present" flag.
assert(value & (1ULL << 63));
ulong frame_num = frameNumberFromPagemap(value);
return (frame_num * 4096) | (virtual_addr & (4095));
}
// ----------------------------------------------
void setupMapping() {
g_mem_size =
(long)(g_fraction_of_physical_memory * getPhysicalMemorySize());
printf("mem_size (MB): %d\n", (int)(g_mem_size / 1024 / 1024));
/* map */
g_mapping = mmap(NULL, g_mem_size, PROT_READ | PROT_WRITE,
MAP_POPULATE | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assert(g_mapping != (void *) -1);
/* page virt -> phys translation table */
g_frame_phys = (ulong *)malloc(sizeof(long) * (g_mem_size / 0x1000));
/* initialize */
for (long i = 0; i < g_mem_size; i += 0x1000) {
ulong vaddr, paddr;
vaddr = (ulong)(g_mapping + i);
*((ulong *)vaddr) = 0;
paddr = getPhysicalAddr(vaddr);
g_frame_phys[i/0x1000] = paddr;
// printf("vaddr-paddr: %p-%p\n", (void *)vaddr, (void *)paddr);
}
printf("allocation complete.\n");
}
// ----------------------------------------------
void initPagemap()
{
g_pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
assert(g_pagemap_fd >= 0);
}
// ----------------------------------------------
long utime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
}
uint64_t nstime()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000000000 + ts.tv_nsec;
}
/**************************************************************************
* Implementation
**************************************************************************/
long *create_list(ulong match_mask, int max_shift, int min_count)
{
ulong vaddr, paddr;
int count = 0;
long *list_curr = NULL;
long *list_head = NULL;
// printf("mask: 0x%lx, shift: %d\n", match_mask, max_shift);
for (long i = 0; i < g_mem_size; i += 0x1000) {
vaddr = (ulong)(g_mapping + i) + (match_mask & 0xFFF);
paddr = g_frame_phys[i/0x1000] + (match_mask & 0xFFF);
if (!((paddr & ((1<<max_shift) - 1)) ^ match_mask)) {
if (*(ulong *)vaddr > 0)
continue;
/* found a match */
// printf("vaddr-paddr: %p-%p\n", (void *)vaddr, (void *)paddr);
count ++;
if (count == 1) {
list_head = list_curr = (long *)vaddr;
}
*list_curr = vaddr;
list_curr = (long *)vaddr;
if (count == min_count) {
*list_curr = (ulong) list_head;
// printf("#of entries in the list: %d\n", count);
return list_head;
}
}
}
printf("failed: found (%d) / requested (%d) pages\n", count, min_count);
return NULL;
}
int run(long *list, int count)
{
long i = 0;
while (list && i++ < count) {
list = (long *)*list;
}
return i;
}
void worker(void *param)
{
long *list = (long *)param;
printf("worker thread begins\n");
while (list) {
list = (long *)*list;
}
}
int main(int argc, char* argv[])
{
cpu_set_t cmask;
int num_processors, n_corun = 1;
int opt;
int repeat = 1000000;
pthread_t tid[16]; /* thread identifier */
pthread_attr_t attr;
pthread_attr_init(&attr);
num_processors = sysconf(_SC_NPROCESSORS_CONF);
/*
* get command line options
*/
while ((opt = getopt(argc, argv, "w:p:c:n:i:h")) != -1) {
switch (opt) {
case 'w': /* cache num ways */
g_cache_num_ways = strtol(optarg, NULL, 0);
break;
case 'p': /* set memory fraction */
g_fraction_of_physical_memory = strtof(optarg, NULL);
break;
case 'c': /* set CPU affinity */
g_cpuid = strtol(optarg, NULL, 0);
break;
case 'n': /* #of co-runners */
n_corun = strtol(optarg, NULL, 0);
break;
case 'i': /* iterations */
repeat = strtol(optarg, NULL, 0);
printf("repeat=%d\n", repeat);
break;
}
}
initPagemap();
setupMapping();
#if 0
struct sched_param param;
/* try to use a real-time scheduler*/
param.sched_priority = 1;
if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
perror("sched_setscheduler failed");
}
#endif
/* launch corun worker threads */
tid[0]= pthread_self();
long *corun_list[16];
/* thread affinity set */
for (int i = 0; i < MIN(1+n_corun, num_processors); i++) {
if (i != 0) {
corun_list[i] = create_list(0x0, MAX_BIT, g_cache_num_ways*2);
pthread_create(&tid[i], &attr, (void *)worker, corun_list[i]);
}
CPU_ZERO(&cmask);
CPU_SET((g_cpuid + i) % num_processors, &cmask);
if (pthread_setaffinity_np(tid[i], sizeof(cpu_set_t), &cmask) < 0)
perror("error");
}
sleep(2);
for (int bit = 6; bit < MAX_BIT; bit++){
/* initialize data */
ulong bank_mask = (1<<bit);
long *subject_list =
create_list(bank_mask, MAX_BIT, g_cache_num_ways*2);
/* subject measurement */
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
int naccess = run(subject_list, repeat);
clock_gettime(CLOCK_REALTIME, &end);
int64_t nsdiff = get_elapsed(&start, &end);
printf("Bit%d: %.2f MB/s, %.2f ns\n", bit,
(double)64*1000*naccess/nsdiff,
(double)nsdiff/naccess);
}
return 0;
}