-
Notifications
You must be signed in to change notification settings - Fork 9
/
access_profiler.cpp
265 lines (211 loc) · 5.73 KB
/
access_profiler.cpp
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
/*
access_profiler instruments types and collects usage counts for class fields
Copyright (C) 2013 Arvid Norberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <signal.h>
#include <stdlib.h> // for malloc/free
#include <sys/mman.h> // for mprotect
#include <cxxabi.h>
#include <unordered_map>
#include <cstdint>
#include <cinttypes> // for PRId64
#include <typeinfo>
#include <vector>
#include <mutex>
struct type_t
{
// the type this page belongs to
std::uint16_t type_idx;
void* base_ptr;
size_t size;
};
std::mutex pagemutex;
// maps page addresses to the type that page belongs to
std::unordered_map<uintptr_t, type_t> pagemap;
std::mutex typemutex;
std::unordered_map<std::type_info const*, std::uint8_t> typemap;
int next_typeidx = 0;
struct access_t
{
int size;
std::atomic<std::uint64_t>* counter;
};
enum { max_types = 300 };
std::vector<access_t> access;
// when the sigsegv handler unprotects a page and enters
// single step mode, it saves the page that was unprotected
// here, and protects it again in the single step handler
// (i.e. one instruction later)
pthread_key_t last_page = 0;
void segv_handler(int, siginfo_t *info, void* uap)
{
ucontext_t* uc = (ucontext_t*)uap;
uintptr_t page_addr = uintptr_t(info->si_addr);
page_addr &= ~4095;
void* base_ptr;
int type_size;
int type_idx;
{
std::lock_guard<std::mutex> l(pagemutex);
auto i = pagemap.find(page_addr);
if (i == pagemap.end()) return;
type_t& t = i->second;
base_ptr = t.base_ptr;
type_size = t.size;
type_idx = t.type_idx;
}
// the allocator itself apparently touches some memory
// of unrelated pages when freeing
// we must still allow the access, but don't record it
if ((char*)info->si_addr < (char*)base_ptr + type_size)
{
// record the access
int offset = uintptr_t(info->si_addr) - uintptr_t(base_ptr);
++access[type_idx].counter[offset];
}
// unprotect the page
mprotect((void*)page_addr, 4096, PROT_READ | PROT_WRITE);
// save the page address so the single step handler
// know which page to protect again
pthread_setspecific(last_page, (void*)page_addr);
// turn on single step
#ifdef __LP64__
uc->uc_mcontext->__ss.__rflags |= 0x100;
#else
uc->uc_mcontext->__ss.__eflags |= 0x100;
#endif
}
void single_step_handler(int signo, siginfo_t* info, void* uap)
{
ucontext_t* uc = (ucontext_t*)uap;
// turn off single step
#ifdef __LP64__
uc->uc_mcontext->__ss.__rflags &= ~0x100;
#else
uc->uc_mcontext->__ss.__eflags &= ~0x100;
#endif
void* buf = pthread_getspecific(last_page);
// and protect the page again
mprotect(buf, 4096, PROT_NONE);
}
namespace access_profiler
{
namespace detail
{
int type_idx(std::type_info const* ti, int size)
{
std::lock_guard<std::mutex> l(typemutex);
auto i = typemap.find(ti);
if (i != typemap.end()) return i->second;
if (next_typeidx >= max_types) return -1;
access[next_typeidx].counter = new std::atomic<std::uint64_t>[size];
for (int i = 0; i < size; ++i) access[next_typeidx].counter[i] = 0;
access[next_typeidx].size = size;
typemap[ti] = next_typeidx++;
return next_typeidx-1;
}
void* allocate_instrumented_type(int size, int type)
{
if (type == -1) return malloc(size);
// round up to even page
int page_size = (size + 4095) & ~4095;
void* buf = valloc(page_size);
if (buf == nullptr) return buf;
{
std::lock_guard<std::mutex> l(pagemutex);
for (uintptr_t ptr = (uintptr_t)buf; ptr < uintptr_t(buf) + page_size;
ptr += 4096)
{
type_t& t = pagemap[ptr];
t.base_ptr = buf;
t.size = size;
t.type_idx = type;
}
}
mprotect(buf, page_size, PROT_NONE);
return buf;
}
void free_instrumented_type(void* buf, int size, int type)
{
if (type == -1)
{
free(buf);
return;
}
// round up to even page
size = (size + 4095) & ~4095;
mprotect(buf, size, PROT_READ | PROT_WRITE);
{
std::lock_guard<std::mutex> l(pagemutex);
for (uintptr_t ptr = (uintptr_t)buf; ptr < uintptr_t(buf) + size;
ptr += 4096)
{
pagemap.erase(ptr);
}
}
free(buf);
}
} // detail
void init_instrumentation()
{
pthread_key_create(&last_page, NULL);
struct sigaction sa;
sa.sa_sigaction = &segv_handler;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
sa.sa_sigaction = &single_step_handler;
sigaction(SIGTRAP, &sa, NULL);
access.resize(max_types);
}
void print_report()
{
FILE* out = fopen("access_profile.out", "w+");
if (out == nullptr)
{
fprintf(stderr, "failed to open \"access_profile.out\" "
"for writing: (%d) %s\n"
, errno, strerror(errno));
return;
}
std::lock_guard<std::mutex> l(typemutex);
for (auto i : typemap)
{
int dummy;
fprintf(out, "\n%s\n", abi::__cxa_demangle(i.first->name(), NULL, NULL, &dummy));
int type_idx = i.second;
access_t& a = access[type_idx];
// don't show fields with less than 1% of max
// hit rate
for (int i = 0; i < a.size; ++i)
{
std::uint64_t cnt = a.counter[i].load();
if (cnt == 0) continue;
fprintf(out, " %4d: %" PRId64 "\n", i, cnt);
}
}
fclose(out);
}
static struct static_init_t
{
static_init_t()
{
init_instrumentation();
}
~static_init_t()
{
print_report();
}
} initializer;
} // access_profiler