forked from google/autofdo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_map.cc
590 lines (537 loc) · 18.9 KB
/
symbol_map.cc
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Class to represent the symbol map.
#include <algorithm>
#include <map>
#include <set>
#include "gflags/gflags.h"
#include "base/common.h"
#include "addr2line.h"
#include "symbol_map.h"
#include "symbolize/elf_reader.h"
DEFINE_int32(dump_cutoff_percent, 2,
"functions that has total count lower than this percentage of "
"the max function count will not show in the dump");
namespace {
// Returns whether str ends with suffix.
inline bool HasSuffixString(const string &str,
const string &suffix) {
uint32 len = suffix.size();
uint32 str_len = str.size();
if (str_len <= len) {
return false;
}
return str.substr(str_len - len, len) == suffix;
}
string GetOriginalName(const char *name) {
const char *split = strchr(name, '.');
if (split) {
return string(name, split - name);
} else {
return string(name);
}
}
// Prints some blank space for identation.
void Identation(int ident) {
for (int i = 0; i < ident; i++) {
printf(" ");
}
}
void PrintSourceLocation(uint32 start_line, uint32 offset, int ident) {
Identation(ident);
if (offset & 0xffff) {
printf("%u.%u: ", (offset >> 16) + start_line, offset & 0xffff);
} else {
printf("%u: ", (offset >> 16) + start_line);
}
}
} // namespace
namespace autofdo {
ProfileInfo& ProfileInfo::operator+=(const ProfileInfo &s) {
count += s.count;
num_inst += s.num_inst;
for (const auto &target_count : s.target_map) {
target_map[target_count.first] += target_count.second;
}
return *this;
}
struct TargetCountCompare {
bool operator()(const TargetCountPair &t1, const TargetCountPair &t2) const {
if (t1.second != t2.second) {
return t1.second > t2.second;
} else {
return t1.first > t2.first;
}
}
};
void GetSortedTargetCountPairs(const CallTargetCountMap &call_target_count_map,
TargetCountPairs *target_counts) {
for (const auto &name_count : call_target_count_map) {
target_counts->push_back(name_count);
}
sort(target_counts->begin(), target_counts->end(), TargetCountCompare());
}
SymbolMap::~SymbolMap() {
// Different keys (function names) may map to a same symbol.
// In order to prevent double free, we first merge all symbols
// into a set, then remove every symbol from the set.
set<Symbol *> delete_set;
for (NameSymbolMap::iterator iter = map_.begin();
iter != map_.end(); ++iter) {
delete_set.insert(iter->second);
}
for (const auto &symbol : delete_set) {
delete symbol;
}
}
Symbol::~Symbol() {
for (auto &callsite_symbol : callsites) {
delete callsite_symbol.second;
}
}
void Symbol::Merge(const Symbol *other) {
total_count += other->total_count;
head_count += other->head_count;
if (info.file_name == NULL) {
info.file_name = other->info.file_name;
info.dir_name = other->info.dir_name;
}
for (const auto &pos_count : other->pos_counts)
pos_counts[pos_count.first] += pos_count.second;
// Traverses all callsite, recursively Merge the callee symbol.
for (const auto &callsite_symbol : other->callsites) {
pair<CallsiteMap::iterator, bool> ret = callsites.insert(
CallsiteMap::value_type(callsite_symbol.first, NULL));
// If the callsite does not exist in the current symbol, create a
// new callee symbol with the clone's function name.
if (ret.second) {
ret.first->second = new Symbol();
ret.first->second->info.func_name = ret.first->first.second;
}
ret.first->second->Merge(callsite_symbol.second);
}
}
void SymbolMap::Merge() {
for (auto &name_symbol : map_) {
string name = GetOriginalName(name_symbol.first.c_str());
pair<NameSymbolMap::iterator, bool> ret =
map_.insert(NameSymbolMap::value_type(name, NULL));
if (ret.second) {
ret.first->second = new Symbol();
ret.first->second->info.func_name = ret.first->first.c_str();
}
if (ret.first->second != name_symbol.second) {
ret.first->second->Merge(name_symbol.second);
name_symbol.second->total_count = 0;
name_symbol.second->head_count = 0;
}
}
}
void SymbolMap::AddSymbol(const string &name) {
pair<NameSymbolMap::iterator, bool> ret = map_.insert(
NameSymbolMap::value_type(name, NULL));
if (ret.second) {
ret.first->second = new Symbol(ret.first->first.c_str(), NULL, NULL, 0);
NameAliasMap::const_iterator alias_iter = name_alias_map_.find(name);
if (alias_iter != name_alias_map_.end()) {
for (const auto &name : alias_iter->second) {
map_[name] = ret.first->second;
}
}
}
}
uint64 SymbolMap::TotalCount() const {
uint64 total_count = 0;
for (const auto &name_symbol : map_) {
total_count += name_symbol.second->total_count;
}
return total_count;
}
const bool SymbolMap::GetSymbolInfoByAddr(
uint64 addr, const string **name,
uint64 *start_addr, uint64 *end_addr) const {
AddressSymbolMap::const_iterator ret = address_symbol_map_.upper_bound(addr);
if (ret == address_symbol_map_.begin()) {
return false;
}
ret--;
if (addr >= ret->first && addr < ret->first + ret->second.second) {
if (name) {
*name = &ret->second.first;
}
if (start_addr) {
*start_addr = ret->first;
}
if (end_addr) {
*end_addr = ret->first + ret->second.second;
}
return true;
} else {
return false;
}
}
const string *SymbolMap::GetSymbolNameByStartAddr(uint64 addr) const {
AddressSymbolMap::const_iterator ret = address_symbol_map_.find(addr);
if (ret == address_symbol_map_.end()) {
return NULL;
}
return &ret->second.first;
}
class SymbolReader : public ElfReader::SymbolSink {
public:
explicit SymbolReader(NameAliasMap *name_alias_map,
AddressSymbolMap *address_symbol_map)
: name_alias_map_(name_alias_map),
address_symbol_map_(address_symbol_map) { }
virtual void AddSymbol(const char *name, uint64 address, uint64 size) {
if (size == 0) {
return;
}
pair<AddressSymbolMap::iterator, bool> ret = address_symbol_map_->insert(
make_pair(address, make_pair(string(name), size)));
if (!ret.second) {
(*name_alias_map_)[ret.first->second.first].insert(name);
}
}
virtual ~SymbolReader() { }
private:
NameAliasMap *name_alias_map_;
AddressSymbolMap *address_symbol_map_;
DISALLOW_COPY_AND_ASSIGN(SymbolReader);
};
void SymbolMap::BuildSymbolMap() {
ElfReader elf_reader(binary_);
base_addr_ = elf_reader.VaddrOfFirstLoadSegment();
SymbolReader symbol_reader(&name_alias_map_, &address_symbol_map_);
elf_reader.VisitSymbols(&symbol_reader);
}
void SymbolMap::UpdateSymbolMap(const string &binary,
const Addr2line *addr2line) {
SymbolMap new_map(binary);
for (auto iter = map_.begin(); iter != map_.end(); ++iter) {
uint64 addr = new_map.GetSymbolStartAddr(iter->first);
if (addr == 0) {
continue;
}
SourceStack stack;
addr2line->GetInlineStack(addr, &stack);
if (stack.size() != 0) {
iter->second->info.file_name = stack[stack.size() - 1].file_name;
iter->second->info.dir_name = stack[stack.size() - 1].dir_name;
}
}
}
string Symbol::ModuleName() const {
// This is a special case in Google3, though tcmalloc.cc has a suffix of .cc,
// it's actually no a module, but included by tcmalloc_or_debug.cc, which is
// a pure wrapper. Thus when a function is found to belong to module
// tcmalloc.cc, it should be reattributed to the wrapper module.
if (info.RelativePath() == "./tcmalloc/tcmalloc.cc") {
return "tcmalloc/tcmalloc_or_debug.cc";
} else {
return info.RelativePath();
}
}
bool Symbol::IsFromHeader() const {
if (HasSuffixString(ModuleName(), ".c") ||
HasSuffixString(ModuleName(), ".cc") ||
HasSuffixString(ModuleName(), ".C") ||
HasSuffixString(ModuleName(), ".cpp")) {
return false;
} else if (HasSuffixString(ModuleName(), ".h")) {
return true;
} else {
LOG(WARNING) << ModuleName() << " has unknown suffix.";
// If suffix is unknown, we think it is from header so that the module
// will not be considered in module grouping.
return true;
}
}
void SymbolMap::AddSymbolEntryCount(const string &symbol_name, uint64 count) {
Symbol *symbol = map_.find(symbol_name)->second;
symbol->head_count = max(symbol->head_count, count);
}
Symbol *SymbolMap::TraverseInlineStack(const string &symbol_name,
const SourceStack &src,
uint64 count) {
Symbol *symbol = map_.find(symbol_name)->second;
symbol->total_count += count;
const SourceInfo &info = src[src.size() - 1];
if (symbol->info.file_name == NULL && info.file_name != NULL) {
symbol->info.file_name = info.file_name;
symbol->info.dir_name = info.dir_name;
}
for (int i = src.size() - 1; i > 0; i--) {
pair<CallsiteMap::iterator, bool> ret = symbol->callsites.insert(
CallsiteMap::value_type(Callsite(src[i].Offset(), src[i - 1].func_name),
NULL));
if (ret.second) {
ret.first->second = new Symbol(src[i - 1].func_name,
src[i - 1].dir_name,
src[i - 1].file_name,
src[i - 1].start_line);
}
symbol = ret.first->second;
symbol->total_count += count;
}
return symbol;
}
void SymbolMap::AddSourceCount(const string &symbol_name,
const SourceStack &src,
uint64 count, uint64 num_inst,
Operation op) {
if (src.size() == 0 || src[0].Malformed()) {
return;
}
Symbol *symbol = TraverseInlineStack(symbol_name, src, count);
if (op == MAX) {
if (count > symbol->pos_counts[src[0].Offset()].count) {
symbol->pos_counts[src[0].Offset()].count = count;
}
} else if (op == SUM) {
symbol->pos_counts[src[0].Offset()].count += count;
} else {
LOG(FATAL) << "op not supported.";
}
symbol->pos_counts[src[0].Offset()].num_inst += num_inst;
}
void SymbolMap::AddIndirectCallTarget(const string &symbol_name,
const SourceStack &src,
const string &target,
uint64 count) {
Symbol *symbol = TraverseInlineStack(symbol_name, src, 0);
symbol->pos_counts[src[0].Offset()].target_map[
GetOriginalName(target.c_str())] = count;
}
struct CallsiteLessThan {
bool operator()(const Callsite& c1, const Callsite& c2) const {
if (c1.first != c2.first)
return c1.first < c2.first;
if ((c1.second == NULL || c2.second == NULL))
return c1.second == NULL;
return strcmp(c1.second, c2.second) < 0;
}
};
void Symbol::Dump(int ident) const {
if (ident == 0) {
printf("%s total:%llu head:%llu\n", info.func_name,
total_count, head_count);
} else {
printf("%s total:%llu\n", info.func_name, total_count);
}
vector<uint32> positions;
for (const auto &pos_count : pos_counts)
positions.push_back(pos_count.first);
sort(positions.begin(), positions.end());
for (const auto &pos : positions) {
PositionCountMap::const_iterator ret = pos_counts.find(pos);
DCHECK(ret != pos_counts.end());
PrintSourceLocation(info.start_line, pos, ident + 2);
printf("%llu", ret->second.count);
TargetCountPairs target_count_pairs;
GetSortedTargetCountPairs(ret->second.target_map,
&target_count_pairs);
for (const auto &target_count : target_count_pairs) {
printf(" %s:%llu", target_count.first.c_str(), target_count.second);
}
printf("\n");
}
vector<Callsite> calls;
for (const auto &pos_symbol : callsites) {
calls.push_back(pos_symbol.first);
}
sort(calls.begin(), calls.end(), CallsiteLessThan());
for (const auto &callsite : calls) {
PrintSourceLocation(info.start_line, callsite.first, ident + 2);
callsites.find(callsite)->second->Dump(ident + 2);
}
}
void SymbolMap::Dump() const {
std::map<uint64, vector<string> > count_names_map;
for (const auto &name_symbol : map_) {
if (name_symbol.second->total_count > 0) {
count_names_map[~name_symbol.second->total_count].push_back(
name_symbol.first);
}
}
for (const auto &count_names : count_names_map) {
for (const auto &name : count_names.second) {
Symbol *symbol = map_.find(name)->second;
symbol->Dump(0);
}
}
}
float SymbolMap::Overlap(const SymbolMap &map) const {
std::map<string, pair<uint64, uint64> > overlap_map;
// Prepare for overlap_map
uint64 total_1 = 0;
uint64 total_2 = 0;
for (const auto &name_symbol : map_) {
total_1 += name_symbol.second->total_count;
overlap_map[name_symbol.first].first = name_symbol.second->total_count;
overlap_map[name_symbol.first].second = 0;
}
for (const auto &name_symbol : map.map()) {
if (overlap_map.find(name_symbol.first) == overlap_map.end()) {
overlap_map[name_symbol.first].first = 0;
}
total_2 += name_symbol.second->total_count;
overlap_map[name_symbol.first].second = name_symbol.second->total_count;
}
if (total_1 == 0 || total_2 == 0) {
return 0.0;
}
// Calculate the overlap
float overlap = 0.0;
for (const auto &name_counts : overlap_map) {
overlap += std::min(
static_cast<float>(name_counts.second.first) / total_1,
static_cast<float>(name_counts.second.second) / total_2);
}
return overlap;
}
void SymbolMap::DumpFuncLevelProfileCompare(const SymbolMap &map) const {
uint64 max_1 = 0;
uint64 max_2 = 0;
// Calculate the max of the two maps
for (const auto &name_symbol : map_) {
max_1 = std::max(name_symbol.second->total_count, max_1);
}
for (const auto &name_symbol : map.map()) {
max_2 = std::max(name_symbol.second->total_count, max_2);
}
// Sort map_1
std::map<uint64, vector<string> > count_names_map;
for (const auto &name_symbol : map_) {
if (name_symbol.second->total_count > 0) {
count_names_map[name_symbol.second->total_count].push_back(
name_symbol.first);
}
}
// Dump hot functions in map_1
for (auto count_names_iter = count_names_map.rbegin();
count_names_iter != count_names_map.rend(); ++count_names_iter) {
for (const auto &name : count_names_iter->second) {
Symbol *symbol = map_.find(name)->second;
if (symbol->total_count * 100 < max_1 * FLAGS_dump_cutoff_percent) {
break;
}
const auto &iter = map.map().find(name);
uint64 compare_count = 0;
if (iter != map.map().end()) {
compare_count = iter->second->total_count;
}
printf("%llu%% %llu%% %s\n", symbol->total_count * 100 / max_1,
compare_count * 100 / max_2, name.c_str());
}
}
// Sort map_2
count_names_map.clear();
for (const auto &name_symbol : map.map()) {
if (name_symbol.second->total_count > 0) {
count_names_map[name_symbol.second->total_count].push_back(
name_symbol.first);
}
}
// Dump hot functions in map_2 that was not caught.
for (auto count_names_iter = count_names_map.rbegin();
count_names_iter != count_names_map.rend(); ++count_names_iter) {
for (const auto &name : count_names_iter->second) {
Symbol *symbol = map.map().find(name)->second;
if (symbol->total_count * 100 < max_2 * FLAGS_dump_cutoff_percent) {
break;
}
const auto &iter = map_.find(name);
uint64 compare_count = 0;
if (iter != map.map().end()) {
compare_count = iter->second->total_count;
if (compare_count * 100 >= max_1 * FLAGS_dump_cutoff_percent) {
continue;
}
}
printf("%llu%% %llu%% %s\n", compare_count * 100 / max_1,
symbol->total_count * 100 / max_2, name.c_str());
}
}
}
typedef map<uint64, uint64> Histogram;
static uint64 AddSymbolProfileToHistogram(const Symbol *symbol,
Histogram *histogram) {
uint64 total_count = 0;
for (const auto &pos_count : symbol->pos_counts) {
pair<Histogram::iterator, bool> ret =
histogram->insert(Histogram::value_type(pos_count.second.count, 0));
ret.first->second += pos_count.second.num_inst;
total_count += pos_count.second.count * pos_count.second.num_inst;
}
for (const auto &callsite_symbol : symbol->callsites) {
total_count += AddSymbolProfileToHistogram(callsite_symbol.second,
histogram);
}
return total_count;
}
void SymbolMap::ComputeWorkingSets() {
Histogram histogram;
uint64 total_count = 0;
// Step 1. Compute histogram.
for (const auto &name_symbol : map_) {
total_count += AddSymbolProfileToHistogram(name_symbol.second, &histogram);
}
int bucket_num = 0;
uint64 accumulated_count = 0;
uint64 accumulated_inst = 0;
uint64 one_bucket_count = total_count / (NUM_GCOV_WORKING_SETS + 1);
// Step 2. Traverse the histogram to update the working set.
for (Histogram::const_reverse_iterator iter = histogram.rbegin();
iter != histogram.rend() && bucket_num < NUM_GCOV_WORKING_SETS; ++iter) {
uint64 count = iter->first;
uint64 num_inst = iter->second;
while (count * num_inst + accumulated_count
> one_bucket_count * (bucket_num + 1)
&& bucket_num < NUM_GCOV_WORKING_SETS) {
int64 offset =
(one_bucket_count * (bucket_num + 1) - accumulated_count) / count;
accumulated_inst += offset;
accumulated_count += offset * count;
num_inst -= offset;
working_set_[bucket_num].num_counters = accumulated_inst;
working_set_[bucket_num].min_counter = count;
bucket_num++;
}
accumulated_inst += num_inst;
accumulated_count += num_inst * count;
}
}
::map<uint64, uint64> SymbolMap::GetSampledSymbolStartAddressSizeMap(
const set<uint64> &sampled_addrs) const {
// We depend on the fact that sampled_addrs is an ordered set.
::map<uint64, uint64> ret;
uint64 next_start_addr = 0;
for (const auto &addr : sampled_addrs) {
uint64 adjusted_addr = addr + base_addr_;
if (adjusted_addr < next_start_addr) {
continue;
}
AddressSymbolMap::const_iterator iter =
address_symbol_map_.upper_bound(adjusted_addr);
if (iter == address_symbol_map_.begin()) {
continue;
}
iter--;
ret.insert(make_pair(iter->first, iter->second.second));
next_start_addr = iter->first + iter->second.second;
}
return ret;
}
} // namespace autofdo