[0d0e60]: pp / opreport.cpp Maximize Restore History

Download this file

opreport.cpp    442 lines (338 with data), 10.0 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
/**
* @file opreport.cpp
* Implement opreport utility
*
* @remark Copyright 2003 OProfile authors
* @remark Read the file COPYING
*
* @author John Levon
* @author Philippe Elie
*/
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <sstream>
#include <numeric>
#include "op_exception.h"
#include "stream_util.h"
#include "string_manip.h"
#include "file_manip.h"
#include "opreport_options.h"
#include "op_header.h"
#include "profile.h"
#include "populate.h"
#include "arrange_profiles.h"
#include "profile_container.h"
#include "symbol_sort.h"
#include "format_output.h"
#include "image_errors.h"
using namespace std;
namespace {
static size_t nr_classes;
/// storage for a merged file summary
struct summary {
count_array_t counts;
string lib_image;
bool operator<(summary const & rhs) const {
return options::reverse_sort
? counts[0] < rhs.counts[0] : rhs.counts[0] < counts[0];
}
/// add a set of files to a summary
size_t add_files(list<profile_sample_files> const & files,
size_t pclass);
};
size_t summary::
add_files(list<profile_sample_files> const & files, size_t pclass)
{
size_t subtotal = 0;
list<profile_sample_files>::const_iterator it = files.begin();
list<profile_sample_files>::const_iterator const end = files.end();
for (; it != end; ++it) {
size_t count = profile_t::sample_count(it->sample_filename);
counts[pclass] += count;
subtotal += count;
// FIXME: is it worth (for now yes I expect bugs ...)
if (!it->cg_files.empty()) {
throw "opreport.cpp::add_files(): unxpected non empty "
"cg file set\n";
}
}
return subtotal;
}
/**
* Summary of an application: a set of image summaries
* for one application, i.e. an application image and all
* dependent images such as libraries.
*/
struct app_summary {
/// total count of us and all dependents
count_array_t counts;
/// the main image
string image;
/// our dependent images
vector<summary> deps;
/// construct and fill in the data
size_t add_profile(profile_set const & profile, size_t pclass);
bool operator<(app_summary const & rhs) const {
return options::reverse_sort
? counts[0] < rhs.counts[0] : rhs.counts[0] < counts[0];
}
private:
/// find a matching summary (including main app summary)
summary & find_summary(string const & image);
};
summary & app_summary::find_summary(string const & image)
{
vector<summary>::iterator sit = deps.begin();
vector<summary>::iterator const send = deps.end();
for (; sit != send; ++sit) {
if (sit->lib_image == image)
return *sit;
}
summary summ;
summ.lib_image = image;
deps.push_back(summ);
return deps.back();
}
size_t app_summary::add_profile(profile_set const & profile,
size_t pclass)
{
size_t group_total = 0;
// first the main image
summary & summ = find_summary(profile.image);
size_t app_count = summ.add_files(profile.files, pclass);
counts[pclass] += app_count;
group_total += app_count;
// now all dependent images if any
list<profile_dep_set>::const_iterator it = profile.deps.begin();
list<profile_dep_set>::const_iterator const end = profile.deps.end();
for (; it != end; ++it) {
summary & summ = find_summary(it->lib_image);
size_t lib_count = summ.add_files(it->files, pclass);
counts[pclass] += lib_count;
group_total += lib_count;
}
return group_total;
}
/// all the summaries
struct summary_container {
summary_container(vector<profile_class> const & pclasses);
/// all map summaries
vector<app_summary> apps;
/// total count of samples for all summaries
count_array_t total_counts;
};
summary_container::
summary_container(vector<profile_class> const & pclasses)
{
typedef map<string, app_summary> app_map_t;
app_map_t app_map;
for (size_t i = 0; i < pclasses.size(); ++i) {
list<profile_set>::const_iterator it
= pclasses[i].profiles.begin();
list<profile_set>::const_iterator const end
= pclasses[i].profiles.end();
for (; it != end; ++it) {
app_map_t::iterator ait = app_map.find(it->image);
if (ait == app_map.end()) {
app_summary app;
app.image = it->image;
total_counts[i] += app.add_profile(*it, i);
app_map[app.image] = app;
} else {
total_counts[i]
+= ait->second.add_profile(*it, i);
}
}
}
app_map_t::const_iterator it = app_map.begin();
app_map_t::const_iterator const end = app_map.end();
for (; it != end; ++it) {
apps.push_back(it->second);
}
// sort by count
stable_sort(apps.begin(), apps.end());
vector<app_summary>::iterator ait = apps.begin();
vector<app_summary>::iterator const aend = apps.end();
for (; ait != aend; ++ait) {
stable_sort(ait->deps.begin(), ait->deps.end());
}
}
void output_header()
{
if (!options::show_header)
return;
cout << classes.cpuinfo << endl;
if (!classes.event.empty())
cout << classes.event << endl;
for (vector<profile_class>::size_type i = 0;
i < classes.v.size(); ++i) {
cout << classes.v[i].longname << endl;
}
}
string get_filename(string const & filename)
{
return options::long_filenames ? filename : op_basename(filename);
}
/// Output a count and a percentage
void output_count(double total_count, size_t count)
{
cout << setw(9) << count << ' ';
double ratio = op_ratio(count, total_count);
cout << format_percent(ratio * 100, percent_int_width,
percent_fract_width) << ' ';
}
void output_col_headers(bool indent)
{
if (!options::show_header)
return;
if (indent)
cout << '\t';
size_t colwidth = 9 + 1 + percent_width;
for (size_t i = 0; i < classes.v.size(); ++i) {
string name = classes.v[i].name;
if (name.length() > colwidth)
name = name.substr(0, colwidth - 3)
+ "...";
io_state state(cout);
// gcc 2.95 doesn't know right io manipulator
cout.setf(ios::right, ios::adjustfield);
// gcc 2.95 doesn't honor setw() for std::string
cout << setw(colwidth) << name.c_str();
cout << '|';
}
cout << '\n';
if (indent)
cout << '\t';
for (size_t i = 0; i < classes.v.size(); ++i) {
cout << " samples| ";
io_state state(cout);
// gcc 2.95 doesn't know right io manipulator
cout.setf(ios::right, ios::adjustfield);
cout << setw(percent_width) << "%|";
}
cout << '\n';
if (indent)
cout << '\t';
for (size_t i = 0; i < classes.v.size(); ++i) {
cout << "-----------";
string str(percent_width, '-');
cout << str;
}
cout << '\n';
}
void
output_deps(summary_container const & summaries,
app_summary const & app)
{
// the app summary itself is *always* present
// (perhaps with zero counts) so this test
// is correct
if (app.deps.size() == 1)
return;
output_col_headers(true);
for (size_t j = 0 ; j < app.deps.size(); ++j) {
summary const & summ = app.deps[j];
if (summ.counts.zero())
continue;
cout << '\t';
for (size_t i = 0; i < nr_classes; ++i) {
double tot_count = options::global_percent
? summaries.total_counts[i] : app.counts[i];
output_count(tot_count, summ.counts[i]);
}
cout << get_filename(summ.lib_image);
cout << '\n';
}
}
/**
* Display all the given summary information
*/
void output_summaries(summary_container const & summaries)
{
output_col_headers(false);
for (size_t i = 0; i < summaries.apps.size(); ++i) {
app_summary const & app = summaries.apps[i];
if ((app.counts[0] * 100.0) / summaries.total_counts[0]
< options::threshold) {
continue;
}
for (size_t j = 0; j < nr_classes; ++j) {
output_count(summaries.total_counts[j],
app.counts[j]);
}
cout << get_filename(app.image) << '\n';
output_deps(summaries, app);
}
}
format_flags const get_format_flags(column_flags const & cf)
{
format_flags flags(ff_none);
flags = format_flags(flags | ff_nr_samples);
flags = format_flags(flags | ff_percent | ff_symb_name);
if (options::show_address)
flags = format_flags(flags | ff_vma);
if (options::debug_info)
flags = format_flags(flags | ff_linenr_info);
if (options::accumulated) {
flags = format_flags(flags | ff_nr_samples_cumulated);
flags = format_flags(flags | ff_percent_cumulated);
}
if (cf & cf_image_name)
flags = format_flags(flags | ff_image_name);
return flags;
}
void output_symbols(profile_container const & samples, bool multiple_apps)
{
profile_container::symbol_choice choice;
choice.threshold = options::threshold;
symbol_collection symbols = samples.select_symbols(choice);
options::sort_by.sort(symbols, options::reverse_sort,
options::long_filenames);
format_output::formatter out(samples);
out.set_nr_classes(nr_classes);
out.show_details(options::details);
out.show_long_filenames(options::long_filenames);
out.show_header(options::show_header);
out.vma_format_64bit(choice.hints & cf_64bit_vma);
out.show_global_percent(options::global_percent);
format_flags flags = get_format_flags(choice.hints);
if (multiple_apps)
flags = format_flags(flags | ff_app_name);
out.add_format(flags);
out.output(cout, symbols);
}
int opreport(vector<string> const & non_options)
{
handle_options(non_options);
nr_classes = classes.v.size();
if (!options::symbols) {
summary_container summaries(classes.v);
output_header();
output_summaries(summaries);
return 0;
}
profile_container samples(options::debug_info, options::details);
bool multiple_apps = false;
for (size_t i = 0; i < classes.v.size(); ++i) {
if (classes.v[i].profiles.size() > 1)
multiple_apps = true;
}
list<inverted_profile> iprofiles
= invert_profiles(classes, options::extra_found_images);
report_image_errors(iprofiles);
list<inverted_profile>::iterator it = iprofiles.begin();
list<inverted_profile>::iterator const end = iprofiles.end();
for (; it != end; ++it)
populate_for_image(samples, *it, options::symbol_filter);
output_header();
output_symbols(samples, multiple_apps);
return 0;
}
} // anonymous namespace
int main(int argc, char const * argv[])
{
cout.tie(0);
return run_pp_tool(argc, argv, opreport);
}