Repository navigation
Expand file tree
/
Copy pathstacktrace.cc
More file actions
384 lines (302 loc) · 9.85 KB
/
Copy pathstacktrace.cc
File metadata and controls
384 lines (302 loc) · 9.85 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright (c) 2022 LG Electronics Inc. */
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <limits.h>
#include <inttypes.h>
#include <unistd.h>
#include <dlfcn.h>
#include <cxxabi.h>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <mutex>
#include "heaptrace.h"
#include "stacktrace.h"
#include "compiler.h"
#include "utils.h"
#define SYMBOL_MAXLEN 128
std::map<stack_trace_t, stack_info_t> stackmap;
std::map<addr_t, object_info_t> addrmap;
std::recursive_mutex container_mutex;
// record_backtrace() is defined in stacktrace.h as an inline function.
void __record_backtrace(size_t size, void* addr,
stack_trace_t& stack_trace, int nptrs)
{
std::lock_guard<std::recursive_mutex> lock(container_mutex);
pr_dbg(" record_backtrace(%zd, %p)\n", size, addr);
if (stackmap.find(stack_trace) == stackmap.end()) {
// Record the creation time for the stack_trace
struct stack_info_t stack_info = { 0, };
stack_info.birth_time = std::chrono::steady_clock::now();
stackmap[stack_trace] = stack_info;
}
struct stack_info_t& stack_info = stackmap[stack_trace];
stack_info.stack_depth = nptrs;
stack_info.total_size += size;
stack_info.peak_total_size = std::max(stack_info.peak_total_size,
stack_info.total_size);
stack_info.count++;
stack_info.peak_count = std::max(stack_info.peak_count, stack_info.count);
struct object_info_t& object_info = addrmap[addr];
object_info.stack_trace = stack_trace;
object_info.size = size;
}
void release_backtrace(void* addr)
{
if (unlikely(!addr))
return;
std::lock_guard<std::recursive_mutex> lock(container_mutex);
pr_dbg(" release_backtrace(%p)\n", addr);
const auto& addrit = addrmap.find(addr);
if (addrit == addrmap.end())
return;
object_info_t& object_info = addrit->second;
stack_trace_t& stack_trace = object_info.stack_trace;
const auto& stackit = stackmap.find(stack_trace);
if (stackit == stackmap.end())
return;
stack_info_t& stack_info = stackit->second;
stack_info.total_size -= object_info.size;
stack_info.count--;
if (stack_info.count == 0) {
const auto& it = stackmap.find(stack_trace);
if (it != stackmap.end())
stackmap.erase(it);
}
}
static void print_backtrace_symbol(int count, void *addr)
{
Dl_info dlip;
char *symbol;
int offset;
int status;
// dladdr() translates address to symbolic info.
dladdr(addr, &dlip);
#if __SIZEOF_LONG__ == 4
pr_out("%2d [%#10lx] ", count, (unsigned long)addr);
#else
pr_out("%2d [%#14lx] ", count, (unsigned long)addr);
#endif
symbol = abi::__cxa_demangle(dlip.dli_sname, 0, 0, &status);
if (status == -2 && !symbol)
symbol = strdup(dlip.dli_sname);
if (symbol) {
int len = SYMBOL_MAXLEN;
if (strlen(symbol) > len) {
symbol[len - 3] = '.';
symbol[len - 2] = '.';
symbol[len - 1] = '.';
symbol[len] = '\0';
}
offset = static_cast<int>(static_cast<int*>(addr) -
static_cast<int*>(dlip.dli_saddr));
pr_out("%s +%#x\n", symbol, offset);
free(symbol);
}
else {
// TODO: It'd be better to print related address based on its
// load address. It needs to calculate the load base.
pr_out("%s (+%p)\n", dlip.dli_fname, addr);
}
}
static void print_backtrace_symbol_flamegraph(void *addr, const char *semicolon)
{
Dl_info dlip;
char *symbol;
int offset;
int status;
// dladdr() translates address to symbolic info.
dladdr(addr, &dlip);
symbol = abi::__cxa_demangle(dlip.dli_sname, 0, 0, &status);
if (status == -2 && !symbol)
symbol = strdup(dlip.dli_sname);
if (symbol) {
int len = SYMBOL_MAXLEN;
if (strlen(symbol) > len) {
symbol[len - 3] = '.';
symbol[len - 2] = '.';
symbol[len - 1] = '.';
symbol[len] = '\0';
}
offset = static_cast<int>(static_cast<int*>(addr) -
static_cast<int*>(dlip.dli_saddr));
pr_out("%s%s+%#x", semicolon, symbol, offset);
free(symbol);
}
else {
pr_out("%s%s+%p", semicolon, dlip.dli_fname, addr);
}
}
static std::string get_delta_time_unit(std::chrono::nanoseconds delta)
{
std::string str;
auto h = std::chrono::duration_cast<std::chrono::hours>(delta);
delta -= h;
auto mins = std::chrono::duration_cast<std::chrono::minutes>(delta);
delta -= mins;
auto secs = std::chrono::duration_cast<std::chrono::seconds>(delta);
delta -= secs;
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(delta);
delta -= millis;
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(delta);
delta -= micros;
auto nanos = delta;
if (h.count() > 0)
str = utils::asprintf("%" PRId64 " hours %" PRId64 " mins", h.count(), mins.count());
else if (mins.count() > 0)
str = utils::asprintf("%" PRId64 " mins %" PRId64 " secs", mins.count(), secs.count());
else if (secs.count() > 0)
str = utils::asprintf("%" PRId64 ".%" PRId64 " secs", secs.count(), millis.count());
else if (millis.count() > 0)
str = utils::asprintf("%" PRId64 ".%" PRId64 " ms", millis.count(), micros.count());
else if (micros.count() > 0)
str = utils::asprintf("%" PRId64 ".%" PRId64 " us", micros.count(), nanos.count());
else
str = utils::asprintf("%" PRId64 " ns", nanos.count());
return str;
}
static std::string get_byte_unit(uint64_t size)
{
std::string str;
int ret;
utils::bytes sz(size);
auto mb = std::chrono::duration_cast<utils::megabytes>(sz);
sz -= mb;
auto kb = std::chrono::duration_cast<utils::kilobytes>(sz);
sz -= kb;
auto b = sz;
if (mb.count() > 0)
str = utils::asprintf("%" PRId64 ".%" PRId64 " MB", mb.count(), kb.count());
else if (kb.count() > 0)
str = utils::asprintf("%" PRId64 ".%" PRId64 " KB", kb.count(), b.count());
else
str = utils::asprintf("%" PRId64 " bytes", b.count());
return str;
}
std::string read_statm() {
int vss, rss, shared;
int pagesize_kb = sysconf(_SC_PAGESIZE);
std::ifstream fs("/proc/self/statm");
fs >> vss >> rss >> shared;
vss *= pagesize_kb;
rss *= pagesize_kb;
shared *= pagesize_kb;
std::string str = get_byte_unit(vss) + " / "
+ get_byte_unit(rss) + " / "
+ get_byte_unit(shared);
return str;
}
static void print_dump_stackmap(const time_point_t& current, struct mallinfo& info,
std::vector<std::pair<stack_trace_t, stack_info_t>>& sorted_stack)
{
int cnt = 0;
uint64_t total_size = 0;
int tid = utils::gettid();
pr_out("=================================================================\n");
pr_out("[heaptrace] dump allocation status for /proc/%d/maps (%s)\n",
tid, utils::get_comm_name().c_str());
size_t stack_size = sorted_stack.size();
for (int i = 0; i < stack_size; i++) {
const stack_info_t& info = sorted_stack[i].second;
total_size += info.total_size;
if (i >= opts.top)
continue;
const stack_trace_t& stack_trace = sorted_stack[i].first;
std::string age = get_delta_time_unit(current - info.birth_time);
pr_out("=== backtrace #%d === [count/peak: %zd/%zd] "
"[size/peak: %s/%s] [age: %s]\n",
++cnt, info.count, info.peak_count,
get_byte_unit(info.total_size).c_str(),
get_byte_unit(info.peak_total_size).c_str(),
age.c_str());
for (int i = 0; i < info.stack_depth; i++)
print_backtrace_symbol(i, stack_trace[i]);
pr_out("\n");
}
pr_out("[heaptrace] heap traced num of backtrace : %zd\n",
stack_size);
pr_out("[heaptrace] heap traced allocation size : %s\n",
get_byte_unit(total_size).c_str());
pr_out("[heaptrace] allocator info (virtual) : %s\n",
get_byte_unit(info.arena + info.hblkhd).c_str());
pr_out("[heaptrace] allocator info (resident) : %s\n",
get_byte_unit(info.uordblks).c_str());
pr_out("[heaptrace] statm info (VSS/RSS/shared) : %s\n",
read_statm().c_str());
pr_out("=================================================================\n");
fflush(outfp);
}
static void print_dump_stackmap_flamegraph(std::vector<std::pair<stack_trace_t, stack_info_t>>& sorted_stack)
{
size_t stack_size = sorted_stack.size();
for (int i = 0; i < stack_size; i++) {
const stack_info_t& info = sorted_stack[i].second;
size_t count = info.count;
uint64_t size = info.total_size;
const char *semicolon = "";
if (i >= opts.top)
continue;
const stack_trace_t& stack_trace = sorted_stack[i].first;
for (int i = info.stack_depth - 1; i >= 0; i--) {
print_backtrace_symbol_flamegraph(stack_trace[i], semicolon);
semicolon = ";";
}
pr_out(" %" PRIu64 "\n", size);
}
fflush(outfp);
}
void dump_stackmap(enum alloc_sort_order order, bool flamegraph)
{
auto* tfs = &thread_flags;
time_point_t current;
if (stackmap.empty())
return;
tfs->hook_guard = true;
// get allocated size info from the allocator
struct mallinfo info = mallinfo();
// get current time
current = std::chrono::steady_clock::now();
// sort the stack trace based on the count and then total_size
std::vector<std::pair<stack_trace_t, stack_info_t>> sorted_stack;
{
// protect stackmap access
std::lock_guard<std::recursive_mutex> lock(container_mutex);
for (auto& p : stackmap)
sorted_stack.push_back(make_pair(p.first, p.second));
}
std::sort(sorted_stack.begin(), sorted_stack.end(),
[order](const std::pair<stack_trace_t, stack_info_t>& p1,
const std::pair<stack_trace_t, stack_info_t>& p2) {
if (order == ALLOC_COUNT) {
if (p1.second.count == p2.second.count)
return p1.second.total_size > p2.second.total_size;
return p1.second.count > p2.second.count;
}
else if (order == ALLOC_SIZE) {
if (p1.second.total_size == p2.second.total_size)
return p1.second.count > p2.second.count;
return p1.second.total_size > p2.second.total_size;
}
// not implemented yet
abort();
});
if (flamegraph)
print_dump_stackmap_flamegraph(sorted_stack);
else
print_dump_stackmap(current, info, sorted_stack);
tfs->hook_guard = false;
}
void clear_stackmap(void)
{
std::lock_guard<std::recursive_mutex> lock(container_mutex);
auto* tfs = &thread_flags;
tfs->hook_guard = true;
stackmap.clear();
addrmap.clear();
tfs->hook_guard = false;
}