-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmem_profile.c
202 lines (165 loc) · 5.66 KB
/
mem_profile.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
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
typedef struct {
uint64_t in_use_space, in_use_objects, alloc_space, alloc_objects;
uint64_t *call_stack;
uint64_t call_stack_len;
} mem_record_t;
typedef struct mem_profile mem_profile_t;
typedef struct {
uint8_t *start;
uint8_t *end;
mem_profile_t *profile;
} arena_t;
struct mem_profile {
mem_record_t *records;
uint64_t records_len;
uint64_t records_cap;
uint64_t in_use_space, in_use_objects, alloc_space, alloc_objects;
arena_t arena;
};
static void *arena_alloc(arena_t *a, size_t size, size_t align, size_t count);
static uint8_t record_call_stack(uint64_t *dst, uint64_t cap) {
uintptr_t *rbp = __builtin_frame_address(0);
uint64_t len = 0;
while (rbp != 0 && ((uint64_t)rbp & 7) == 0 && *rbp != 0) {
const uintptr_t rip = *(rbp + 1);
rbp = (uintptr_t *)*rbp;
// `rip` points to the return instruction in the caller, once this call is
// done. But: We want the location of the call i.e. the `call xxx`
// instruction, so we subtract one byte to point inside it, which is not
// quite 'at' it, but good enough.
dst[len++] = rip - 1;
if (len >= cap)
return len;
}
return len;
}
static void mem_profile_record_alloc(mem_profile_t *profile,
uint64_t objects_count,
uint64_t bytes_count) {
// Record the call stack by stack walking.
uint64_t call_stack[64] = {0};
uint64_t call_stack_len =
record_call_stack(call_stack, sizeof(call_stack) / sizeof(call_stack[0]));
// Update the sums.
profile->alloc_objects += objects_count;
profile->alloc_space += bytes_count;
profile->in_use_objects += objects_count;
profile->in_use_space += bytes_count;
// Upsert the record.
for (uint64_t i = 0; i < profile->records_len; i++) {
mem_record_t *r = &profile->records[i];
if (r->call_stack_len == call_stack_len &&
memcmp(r->call_stack, call_stack, call_stack_len * sizeof(uint64_t)) ==
0) {
// Found an existing record, update it.
r->alloc_objects += objects_count;
r->alloc_space += bytes_count;
r->in_use_objects += objects_count;
r->in_use_space += bytes_count;
return;
}
}
// Not found, insert a new record.
mem_record_t record = {
.alloc_objects = objects_count,
.alloc_space = bytes_count,
.in_use_objects = objects_count,
.in_use_space = bytes_count,
};
record.call_stack = arena_alloc(&profile->arena, sizeof(uint64_t),
_Alignof(uint64_t), call_stack_len);
memcpy(record.call_stack, call_stack, call_stack_len * sizeof(uint64_t));
record.call_stack_len = call_stack_len;
if (profile->records_len >= profile->records_cap) {
uint64_t new_cap = profile->records_cap * 2;
// Grow the array.
mem_record_t *new_records = arena_alloc(
&profile->arena, sizeof(mem_record_t), _Alignof(mem_record_t), new_cap);
memcpy(new_records, profile->records,
profile->records_len * sizeof(mem_record_t));
profile->records_cap = new_cap;
profile->records = new_records;
}
profile->records[profile->records_len++] = record;
}
static void mem_profile_write(mem_profile_t *profile, FILE *out) {
fprintf(out, "heap profile: %lu: %lu [ %lu: %lu] @ heapprofile\n",
profile->in_use_objects, profile->in_use_space,
profile->alloc_objects, profile->alloc_space);
for (uint64_t i = 0; i < profile->records_len; i++) {
mem_record_t r = profile->records[i];
fprintf(out, "%lu: %lu [%lu: %lu] @ ", r.in_use_objects, r.in_use_space,
r.alloc_objects, r.alloc_space);
for (uint64_t j = 0; j < r.call_stack_len; j++) {
fprintf(out, "%#lx ", r.call_stack[j]);
}
fputc('\n', out);
}
fputs("\nMAPPED_LIBRARIES:\n", out);
static uint8_t mem[4096] = {0};
int fd = open("/proc/self/maps", O_RDONLY);
assert(fd != -1);
ssize_t read_bytes = read(fd, mem, sizeof(mem));
assert(read_bytes != -1);
close(fd);
fwrite(mem, 1, read_bytes, out);
fflush(out);
}
static void *arena_alloc(arena_t *a, size_t size, size_t align, size_t count) {
size_t available = a->end - a->start;
size_t padding = -(size_t)a->start & (align - 1);
size_t offset = padding + size * count;
if (available < offset) {
fprintf(stderr,
"Out of memory: available=%lu "
"allocation_size=%lu\n",
available, offset);
abort();
}
uint8_t *res = a->start + padding;
a->start += offset;
if (a->profile) {
mem_profile_record_alloc(a->profile, count, offset);
}
return (void *)res;
}
static arena_t arena_new(uint64_t cap, mem_profile_t *profile) {
uint8_t *mem = mmap(NULL, cap, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
arena_t arena = {
.profile = profile,
.start = mem,
.end = mem + cap,
};
return arena;
}
void b(int n, arena_t *arena) {
arena_alloc(arena, sizeof(int), _Alignof(int), n);
}
void a(int n, arena_t *arena) {
arena_alloc(arena, sizeof(int), _Alignof(int), n);
b(n, arena);
}
int main() {
arena_t mem_profile_arena = arena_new(1 << 16, NULL);
mem_profile_t mem_profile = {
.arena = mem_profile_arena,
.records = arena_alloc(&mem_profile_arena, sizeof(mem_record_t),
_Alignof(mem_record_t), 16),
.records_cap = 16,
};
arena_t arena = arena_new(1 << 28, &mem_profile);
for (int i = 0; i < 2; i++)
a(2 * 1024 * 1024, &arena);
b(3 * 1024 * 1024, &arena);
mem_profile_write(&mem_profile, stderr);
}