-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplay.cc
More file actions
297 lines (250 loc) · 7.37 KB
/
replay.cc
File metadata and controls
297 lines (250 loc) · 7.37 KB
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
#include <sys/mman.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <map>
using namespace std;
typedef unsigned long ul;
typedef uint64_t u64;
struct entry {
long key, addr, size;
};
#define ENTRIES_PER_BUCKET 8
struct bucket {
struct entry entries[ENTRIES_PER_BUCKET];
};
static struct bucket *table;
static ul nbuckets; // should be power of 2
static ul bucket_len; // should be power of 2
struct mem {
unsigned long vsize;
long rss;
};
static unsigned long cur_alloc = 0ul;
static char path[] = "/proc/self/stat";
#define FNV_OFFSET_BASIS_64 14695981039346656037ul
#define FNV_PRIME_64 1099511628211ul
static inline
u64 fnv(u64 val) {
u64 hash = FNV_OFFSET_BASIS_64;
uint8_t *p = (uint8_t*)&val;
for (ul i = 0; i < 8; i++)
hash = (hash ^ p[i]) * FNV_PRIME_64;
return hash;
}
static inline
ul make_hash(long value) {
return fnv(value);
}
// prevent compiler removing calls to memset
__attribute__((noinline))
static
void touch(void *where, size_t len) {
memset(where, 0xab, len);
}
// we subtract out the size of the table
struct mem getmem() {
static char *buf = NULL;
if (!buf)
assert((buf = (char*)calloc(1, 1024)));
FILE *fp = fopen(path, "r");
if (!fp) {
perror("fopen");
exit(EXIT_FAILURE);
}
fgets(buf, 1024, fp);
fclose(fp);
fp = NULL;
int field_idx = 22; // max 51
char *pos = buf;
while (field_idx-- > 0)
pos = &strchrnul(pos, ' ')[1];
struct mem mem;
mem.vsize = strtol(pos, NULL, 10);// - bucket_len;
// next field
pos = &strchrnul(pos, ' ')[1];
mem.rss = 4096*strtol(pos, NULL, 10);// - bucket_len;
return mem;
}
static const ul print_every = 100ul * 1000ul;
void print_mem() {
struct mem mem = getmem();
printf("%11lu (%6lu MiB) %11ld (%6lu MiB) | %11lu (%6lu MiB)"
" | %4.2lf %4.2lf\n",
mem.vsize, (ul)((float)mem.vsize/(1ul<<20)),
mem.rss, (ul)((float)mem.rss/(1ul<<20)),
cur_alloc, (ul)((float)cur_alloc/(1ul<<20)),
(float)mem.vsize/(float)cur_alloc,
(float)mem.rss/(float)cur_alloc);
}
void init_table(long n) {
assert(__builtin_popcount(n) == 1);
nbuckets = 2 * n / ENTRIES_PER_BUCKET;
assert(__builtin_popcount(nbuckets) == 1);
size_t len = nbuckets*ENTRIES_PER_BUCKET*sizeof(*table);
table = (struct bucket*)mmap(NULL, len,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
assert(table != MAP_FAILED);
memset(table, 0, len);
bucket_len = len;
printf(">> table buckets %lu len %lu\n",
nbuckets, len);
}
bool get(long key, struct entry **e) {
ul hashh = make_hash(key);
ul mask = nbuckets-1;
ul bidx = mask & hashh;
struct bucket *b = &table[bidx];
long i;
//printf("%s hash %16lx mask %16lx bidx %ld\n",
//__func__, hashh, mask, bidx);
for (i = 0; i < ENTRIES_PER_BUCKET; i++)
if (b->entries[i].key == key)
break;
bool ret = false;
if (i < ENTRIES_PER_BUCKET) {
*e = &b->entries[i];
ret = true;
}
return ret;
}
bool put(long key, long addr, long size) {
ul hashh = make_hash(key);
ul mask = nbuckets-1;
ul bidx = mask & hashh;
struct bucket *b = &table[bidx];
long i, inv = -1l;
//printf("%s hash %16lx mask %16lx bidx %ld\n",
//__func__, hashh, mask, bidx);
for (i = 0; i < ENTRIES_PER_BUCKET; i++) {
if (b->entries[i].key == key) {
break;
} else if (b->entries[i].key == 0l) {
if (inv < 0l)
inv = i;
}
}
bool ret = false;
// found existing, overwrite it
if (i < ENTRIES_PER_BUCKET) {
b->entries[i].key = key;
b->entries[i].addr = addr;
b->entries[i].size = size;
ret = true;
}
// not exist, but free slot found
else if (inv >= 0l) {
b->entries[inv].key = key;
b->entries[inv].addr = addr;
b->entries[inv].size = size;
ret = true;
}
return ret;
}
bool del(long key) {
ul hashh = make_hash(key);
ul mask = nbuckets-1;
ul bidx = mask & hashh;
struct bucket *b = &table[bidx];
long i;
//printf("%s hash %16lx mask %16lx bidx %ld\n",
//__func__, hashh, mask, bidx);
for (i = 0; i < ENTRIES_PER_BUCKET; i++)
if (b->entries[i].key == key)
break;
bool ret = false;
if (i < ENTRIES_PER_BUCKET) {
memset(&b->entries[i], 0, sizeof(b->entries[0]));
ret = true;
}
return ret;
}
void replay() {
long missing_frees = 0;
long itern = 0ul;
init_table(1ul<<16);
char buf[64];
while (fgets(buf, 64, stdin)) {
long tsc;
char op;
assert(2 == sscanf(buf, "%ld %c", &tsc, &op));
if ('m' == op) {
do_malloc:;
//printf("%s", buf);
long size, key;
assert(3 == sscanf(buf, "%ld m %ld %lx",
&tsc,&size,&key));
void *p = malloc(size);
assert(p);
touch(p, size);
cur_alloc += size;
put(key, (long)p, size);
}
else if ('c' == op) {
//printf("%s", buf);
long size, n, key;
assert(4 == sscanf(buf, "%ld c %ld %ld %lx",
&tsc,&n,&size,&key));
void *p = calloc(n,size);
assert(p);
touch(p, n*size);
cur_alloc += n*size;
assert(put(key, (long)p, n*size));
}
else if ('f' == op) {
//printf("%s", buf);
long key;
if (strstr(buf, "nil"))
continue;
assert(2 == sscanf(buf, "%ld f %lx",
&tsc,&key));
struct entry *e;
if (get(key, &e)) {
long p = e->addr;
free((void*)p);
cur_alloc -= e->size;
assert(del(key));
} else missing_frees++;
}
else if ('r' == op) {
//printf("%s", buf);
if (strstr(buf, "nil")) {
// shift out the (nil) and treat like malloc
char *pos = strchr(buf, '(');
assert(pos);
// +1 for NUL byte; 6 for '(nil) '
long n = 1+strlen((char*)((long)pos+6));
memmove(pos, (char*)((long)pos+6), n);
*strchr(buf, 'r') = 'm';
goto do_malloc;
} else {
long key, size, newkey;
assert(4 == sscanf(buf, "%ld r %lx %ld %lx",
&tsc,&key,&size,&newkey));
// handle as free then alloc
struct entry *e;
if (get(key, &e)) {
long p = e->addr;
free((void*)p);
cur_alloc -= e->size;
assert(del(key));
assert((p = (long)malloc(size)));
touch((void*)p, size);
cur_alloc += size;
assert(put(newkey, p, size));
} else missing_frees++;
}
}
else abort();
if (0 == (itern++ % print_every))
print_mem();
}
printf("missing frees: %ld\n", missing_frees);
}
int main() {
replay();
return EXIT_SUCCESS;
}