[ecfbcc]: libpp / callgraph_container.cpp Maximize Restore History

Download this file

callgraph_container.cpp    607 lines (469 with data), 15.9 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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/**
* @file callgraph_container.cpp
* Container associating symbols and caller/caller symbols
*
* @remark Copyright 2004 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#include <cstdlib>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
#include <numeric>
#include "callgraph_container.h"
#include "cverb.h"
#include "parse_filename.h"
#include "profile_container.h"
#include "arrange_profiles.h"
#include "populate.h"
#include "string_filter.h"
#include "op_bfd.h"
#include "op_sample_file.h"
#include "locate_images.h"
using namespace std;
namespace {
bool operator==(cg_symbol const & lhs, cg_symbol const & rhs)
{
less_symbol cmp_symb;
return !cmp_symb(lhs, rhs) && !cmp_symb(rhs, lhs);
}
// we store {caller,callee} inside a single u64
odb_key_t caller_to_key(u32 value)
{
return odb_key_t(value) << 32;
}
u32 key_to_callee(odb_key_t key)
{
return key & 0xffffffff;
}
bool compare_by_callee_vma(pair<odb_key_t, count_type> const & lhs,
pair<odb_key_t, count_type> const & rhs)
{
return (key_to_callee(lhs.first)) < (key_to_callee(rhs.first));
}
/*
* We need 2 comparators for callgraph to get the desired output:
*
* caller_with_few_samples
* caller_with_many_samples
* function_with_many_samples
* callee_with_many_samples
* callee_with_few_samples
*/
bool
compare_arc_count(symbol_entry const & lhs, symbol_entry const & rhs)
{
return lhs.sample.counts[0] < rhs.sample.counts[0];
}
bool
compare_arc_count_reverse(symbol_entry const & lhs, symbol_entry const & rhs)
{
return rhs.sample.counts[0] < lhs.sample.counts[0];
}
// find the nearest bfd symbol for the given file offset and check it's
// in range
op_bfd_symbol const *
get_symbol_by_filepos(op_bfd const & bfd, u32 bfd_offset,
vma_t offset, symbol_index_t & i)
{
offset += bfd_offset;
op_bfd_symbol tmpsym(offset, 0, string());
// sorted by filepos so this will find the nearest
vector<op_bfd_symbol>::const_iterator it =
upper_bound(bfd.syms.begin(), bfd.syms.end(), tmpsym);
if (it != bfd.syms.begin())
--it;
if (it == bfd.syms.end()) {
cerr << "get_symbol_by_filepos: no symbols at all?" << endl;
abort();
}
// if the offset is past the end of the symbol, we didn't find one
u32 const end_offset = it->size() + it->filepos();
if (offset >= end_offset) {
// let's be verbose for now
cerr << "warning: dropping hyperspace sample at offset "
<< hex << offset << " >= " << end_offset
<< " for binary " << bfd.get_filename() << dec << endl;
return NULL;
}
i = distance(bfd.syms.begin(), it);
return &(*it);
}
/// temporary caller and callee data held during processing
class call_data {
public:
call_data(profile_container const & p, profile_t const & pr,
op_bfd const & bfd, u32 boff, image_name_id iid,
image_name_id aid, bool debug_info)
: pc(p), profile(pr), b(bfd), boffset(boff), image(iid),
app(aid), debug(debug_info) {}
/// point to a caller symbol
void caller_sym(symbol_index_t i) {
sym = symbol_entry();
unsigned long long start;
unsigned long long end;
b.get_symbol_range(i, start, end);
samples.clear();
// see profile_t::samples_range() for why we need this check
if (start > boffset) {
profile_t::iterator_pair p_it = profile.samples_range(
caller_to_key(start - boffset),
caller_to_key(end - boffset));
// Our odb_key_t contain (from_eip << 32 | to_eip),
// the range of keys we selected above contains one
// caller but different callees, and due to the
// ordering callee offsets are not consecutive: so
// we must sort them first.
for (; p_it.first != p_it.second; ++p_it.first) {
samples.push_back(make_pair(p_it.first.vma(),
p_it.first.count()));
}
sort(samples.begin(), samples.end(),
compare_by_callee_vma);
}
sym.size = end - start;
sym.name = symbol_names.create(b.syms[i].name());
sym.sample.vma = b.syms[i].vma();
finish_sym(i, start);
if (cverb << vdebug) {
cverb << vdebug << hex << "Caller sym: "
<< b.syms[i].name() << " filepos " << start
<< "-" << end << dec << endl;
}
}
/// point to a callee symbol
bool callee_sym(u32 off) {
sym = symbol_entry();
symbol_index_t i = 0;
op_bfd_symbol const * bfdsym =
get_symbol_by_filepos(b, boffset, off, i);
if (!bfdsym)
return false;
callee_end = bfdsym->size() + bfdsym->filepos() - boffset;
sym.size = bfdsym->size();
sym.name = symbol_names.create(bfdsym->name());
sym.sample.vma = bfdsym->vma();
finish_sym(i, bfdsym->filepos());
if (cverb << vdebug) {
cverb << vdebug << hex << "Callee sym: "
<< bfdsym->name() << " filepos "
<< bfdsym->filepos() << "-"
<< (bfdsym->filepos() + bfdsym->size())
<< dec << endl;
}
return true;
}
void verbose_bfd(string const & prefix) const {
cverb << vdebug << prefix << " " << b.get_filename()
<< " offset " << boffset << " app "
<< image_names.name(app) << endl;
}
typedef vector<pair<odb_key_t, count_type> > samples_t;
typedef samples_t::const_iterator const_iterator;
samples_t samples;
symbol_entry sym;
u32 callee_end;
private:
/// fill in the rest of the sym
void finish_sym(symbol_index_t i, unsigned long start) {
sym.image_name = image;
sym.app_name = app;
symbol_entry const * self = pc.find(sym);
if (self)
sym.sample.counts = self->sample.counts;
if (debug) {
string filename;
file_location & loc = sym.sample.file_loc;
if (b.get_linenr(i, start, filename, loc.linenr))
loc.filename = debug_names.create(filename);
}
}
profile_container const & pc;
profile_t const & profile;
op_bfd const & b;
u32 boffset;
image_name_id image;
image_name_id app;
bool debug;
};
/// accumulate all samples for a given caller/callee pair
count_type
accumulate_callee(call_data::const_iterator & it, call_data::const_iterator end,
u32 callee_end)
{
count_type count = 0;
call_data::const_iterator const start = it;
while (it != end) {
u32 offset = key_to_callee(it->first);
if (cverb << (vdebug & vlevel1)) {
cverb << (vdebug & vlevel1) << hex << "offset: "
<< offset << dec << endl;
}
// stop if we pass the end of the callee
if (offset >= callee_end)
break;
count += it->second;
++it;
}
// If we haven't advanced at all, then we'll get
// an infinite loop, so we must abort.
if (it == start) {
cerr << "failure to advance iterator\n";
abort();
}
return count;
}
} // anonymous namespace
void arc_recorder::
add(symbol_entry const & caller, symbol_entry const * callee,
count_array_t const & arc_count)
{
cg_data & data = sym_map[caller];
// If we have a callee, add it to the caller's list, then
// add the caller to the callee's list.
if (callee) {
data.callees[*callee] += arc_count;
cg_data & callee_data = sym_map[*callee];
callee_data.callers[caller] += arc_count;
}
}
void arc_recorder::process_children(cg_symbol & sym, double threshold)
{
// generate the synthetic self entry for the symbol
symbol_entry self = sym;
self.name = symbol_names.create(symbol_names.demangle(self.name)
+ " [self]");
sym.total_callee_count += self.sample.counts;
sym.callees.push_back(self);
sort(sym.callers.begin(), sym.callers.end(), compare_arc_count);
sort(sym.callees.begin(), sym.callees.end(), compare_arc_count_reverse);
// FIXME: this relies on sort always being sample count
cg_symbol::children::iterator cit = sym.callers.begin();
cg_symbol::children::iterator cend = sym.callers.end();
while (cit != cend && op_ratio(cit->sample.counts[0],
sym.total_caller_count[0]) < threshold)
++cit;
if (cit != cend)
sym.callers.erase(sym.callers.begin(), cit);
cit = sym.callees.begin();
cend = sym.callees.end();
while (cit != cend && op_ratio(cit->sample.counts[0],
sym.total_callee_count[0]) >= threshold)
++cit;
if (cit != cend)
sym.callees.erase(cit, sym.callees.end());
}
void arc_recorder::
process(count_array_t total, double threshold,
string_filter const & sym_filter)
{
map_t::const_iterator it;
map_t::const_iterator end = sym_map.end();
for (it = sym_map.begin(); it != end; ++it) {
cg_symbol sym((*it).first);
cg_data const & data = (*it).second;
// threshold out the main symbol if needed
if (op_ratio(sym.sample.counts[0], total[0]) < threshold)
continue;
// FIXME: slow?
if (!sym_filter.match(symbol_names.demangle(sym.name)))
continue;
cg_data::children::const_iterator cit;
cg_data::children::const_iterator cend = data.callers.end();
for (cit = data.callers.begin(); cit != cend; ++cit) {
symbol_entry csym = cit->first;
csym.sample.counts = cit->second;
sym.callers.push_back(csym);
sym.total_caller_count += cit->second;
}
cend = data.callees.end();
for (cit = data.callees.begin(); cit != cend; ++cit) {
symbol_entry csym = cit->first;
csym.sample.counts = cit->second;
sym.callees.push_back(csym);
sym.total_callee_count += cit->second;
}
process_children(sym, threshold);
// insert sym into cg_syms_objs
// then store pointer to sym in cg_syms
cg_syms.push_back(&(*cg_syms_objs.insert(cg_syms_objs.end(), sym)));
}
}
symbol_collection const & arc_recorder::get_symbols() const
{
return cg_syms;
}
void callgraph_container::populate(list<inverted_profile> const & iprofiles,
extra_images const & extra, bool debug_info, double threshold,
bool merge_lib, string_filter const & sym_filter)
{
this->extra_found_images = extra;
// non callgraph samples container, we record sample at symbol level
// not at vma level.
profile_container pc(debug_info, false, extra_found_images);
list<inverted_profile>::const_iterator it;
list<inverted_profile>::const_iterator const end = iprofiles.end();
for (it = iprofiles.begin(); it != end; ++it) {
// populate_caller_image take care about empty sample filename
populate_for_image(pc, *it, sym_filter, 0);
}
add_symbols(pc);
total_count = pc.samples_count();
for (it = iprofiles.begin(); it != end; ++it) {
for (size_t i = 0; i < it->groups.size(); ++i) {
populate(it->groups[i], it->image,
i, pc, debug_info, merge_lib);
}
}
recorder.process(total_count, threshold / 100.0, sym_filter);
}
void callgraph_container::populate(list<image_set> const & lset,
string const & app_image, size_t pclass,
profile_container const & pc, bool debug_info, bool merge_lib)
{
list<image_set>::const_iterator lit;
list<image_set>::const_iterator const lend = lset.end();
for (lit = lset.begin(); lit != lend; ++lit) {
list<profile_sample_files>::const_iterator pit;
list<profile_sample_files>::const_iterator pend
= lit->files.end();
for (pit = lit->files.begin(); pit != pend; ++pit) {
populate(pit->cg_files, app_image,
pclass, pc, debug_info, merge_lib);
}
}
}
void callgraph_container::populate(list<string> const & cg_files,
string const & app_image, size_t pclass,
profile_container const & pc, bool debug_info, bool merge_lib)
{
list<string>::const_iterator it;
list<string>::const_iterator const end = cg_files.end();
for (it = cg_files.begin(); it != end; ++it) {
cverb << vdebug << "samples file : " << *it << endl;
parsed_filename caller_file =
parse_filename(*it, extra_found_images);
string const app_name = caller_file.image;
image_error error;
extra_found_images.find_image_path(caller_file.lib_image,
error, false);
if (error != image_ok)
report_image_error(caller_file.lib_image,
error, false, extra_found_images);
bool caller_bfd_ok = true;
op_bfd caller_bfd(caller_file.lib_image,
string_filter(), extra_found_images, caller_bfd_ok);
if (!caller_bfd_ok)
report_image_error(caller_file.lib_image,
image_format_failure, false,
extra_found_images);
parsed_filename callee_file =
parse_filename(*it, extra_found_images);
extra_found_images.find_image_path(callee_file.cg_image,
error, false);
if (error != image_ok)
report_image_error(callee_file.cg_image,
error, false, extra_found_images);
bool callee_bfd_ok = true;
op_bfd callee_bfd(callee_file.cg_image,
string_filter(), extra_found_images, callee_bfd_ok);
if (!callee_bfd_ok)
report_image_error(callee_file.cg_image,
image_format_failure, false,
extra_found_images);
profile_t profile;
// We can't use start_offset support in profile_t, give
// it a zero offset and we will fix that in add()
profile.add_sample_file(*it);
add(profile, caller_bfd, caller_bfd_ok, callee_bfd,
merge_lib ? app_image : app_name, pc,
debug_info, pclass);
}
}
void callgraph_container::
add(profile_t const & profile, op_bfd const & caller_bfd, bool caller_bfd_ok,
op_bfd const & callee_bfd, string const & app_name,
profile_container const & pc, bool debug_info, size_t pclass)
{
string const image_name = caller_bfd.get_filename();
opd_header const & header = profile.get_header();
// We can't use kernel sample file w/o the binary else we will
// use it with a zero offset, the code below will abort because
// we will get incorrect callee sub-range and out of range
// callee vma. FIXME
if (header.is_kernel && !caller_bfd_ok)
return;
// We must handle start_offset, this offset can be different for the
// caller and the callee: kernel sample traversing the syscall barrier.
u32 caller_offset;
if (header.is_kernel)
caller_offset = caller_bfd.get_start_offset(0);
else
caller_offset = header.anon_start;
u32 callee_offset;
if (header.cg_to_is_kernel)
callee_offset = callee_bfd.get_start_offset(0);
else
callee_offset = header.cg_to_anon_start;
image_name_id image_id = image_names.create(image_name);
image_name_id callee_image_id = image_names.create(callee_bfd.get_filename());
image_name_id app_id = image_names.create(app_name);
call_data caller(pc, profile, caller_bfd, caller_offset, image_id,
app_id, debug_info);
call_data callee(pc, profile, callee_bfd, callee_offset,
callee_image_id, app_id, debug_info);
if (cverb << vdebug) {
caller.verbose_bfd("Caller:");
callee.verbose_bfd("Callee:");
}
// For each symbol in the caller bfd, process all arcs to
// callee bfd symbols
for (symbol_index_t i = 0; i < caller_bfd.syms.size(); ++i) {
caller.caller_sym(i);
call_data::const_iterator dit = caller.samples.begin();
call_data::const_iterator dend = caller.samples.end();
while (dit != dend) {
// if we can't find the callee, skip an arc
if (!callee.callee_sym(key_to_callee(dit->first))) {
++dit;
continue;
}
count_array_t arc_count;
arc_count[pclass] =
accumulate_callee(dit, dend, callee.callee_end);
recorder.add(caller.sym, &callee.sym, arc_count);
}
}
}
void callgraph_container::add_symbols(profile_container const & pc)
{
symbol_container::symbols_t::iterator it;
symbol_container::symbols_t::iterator const end = pc.end_symbol();
for (it = pc.begin_symbol(); it != end; ++it)
recorder.add(*it, 0, count_array_t());
}
column_flags callgraph_container::output_hint() const
{
column_flags output_hints = cf_none;
// FIXME: costly: must we access directly recorder map ?
symbol_collection syms = recorder.get_symbols();
symbol_collection::iterator it;
symbol_collection::iterator const end = syms.end();
for (it = syms.begin(); it != end; ++it)
output_hints = (*it)->output_hint(output_hints);
return output_hints;
}
count_array_t callgraph_container::samples_count() const
{
return total_count;
}
symbol_collection const & callgraph_container::get_symbols() const
{
return recorder.get_symbols();
}