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

Download this file

300 lines (232 with data), 7.6 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
/**
* @file opgprof.cpp
* Implement opgprof utility
*
* @remark Copyright 2003 OProfile authors
* @remark Read the file COPYING
*
* @author John Levon
* @author Philippe Elie
*/
#include <iostream>
#include <cstdio>
#include "op_header.h"
#include "profile.h"
#include "op_libiberty.h"
#include "op_fileio.h"
#include "string_filter.h"
#include "profile_container.h"
#include "arrange_profiles.h"
#include "image_errors.h"
#include "opgprof_options.h"
#include "cverb.h"
#include "op_file.h"
using namespace std;
namespace {
#define GMON_VERSION 1
#define GMON_TAG_TIME_HIST 0
#define GMON_TAG_CG_ARC 1
struct gmon_hdr {
char cookie[4];
u32 version;
u32 spare[3];
};
void op_write_vma(FILE * fp, op_bfd const & abfd, bfd_vma vma)
{
// bfd vma write size is a per binary property not a bfd
// configuration property
switch (abfd.bfd_arch_bits_per_address()) {
case 32:
op_write_u32(fp, vma);
break;
case 64:
op_write_u64(fp, vma);
break;
default:
cerr << "oprofile: unknown vma size for this binary\n";
exit(EXIT_FAILURE);
}
}
void get_vma_range(bfd_vma & min, bfd_vma & max,
profile_container const & samples)
{
min = bfd_vma(-1);
max = 0;
sample_container::samples_iterator it = samples.begin();
sample_container::samples_iterator end = samples.end();
for (; it != end ; ++it) {
if (it->second.vma < min)
min = it->second.vma;
if (it->second.vma > max)
max = it->second.vma;
}
if (min == bfd_vma(-1))
min = 0;
// we must return a range [min, max) not a range [min, max]
if (max != 0)
max += 1;
}
/**
* @param abfd bfd object
* @param samples_files profile container to act on
* @param gap a power of 2
*
* return true if all sample in samples_files are at least aligned on gap. This
* function is used to get at runtime the right size of gprof bin size
* reducing gmon.out on arch with fixed size instruction length
*
*/
bool aligned_samples(profile_container const & samples, int gap)
{
sample_container::samples_iterator it = samples.begin();
sample_container::samples_iterator end = samples.end();
for (; it != end ; ++it) {
if (it->second.vma % gap)
return false;
}
return true;
}
void output_cg(FILE * fp, op_bfd const & abfd, profile_t const & cg_db)
{
bfd_vma offset = abfd.get_start_offset();
if (!cg_db.get_header().is_kernel)
offset = 0;
profile_t::iterator_pair p_it = cg_db.samples_range();
for (; p_it.first != p_it.second; ++p_it.first) {
bfd_vma from = p_it.first.vma() >> 32;
bfd_vma to = p_it.first.vma() & 0xffffffff;
op_write_u8(fp, GMON_TAG_CG_ARC);
op_write_vma(fp, abfd, abfd.offset_to_pc(from + offset));
op_write_vma(fp, abfd, abfd.offset_to_pc(to + offset));
op_write_u32(fp, p_it.first.count());
}
}
void output_gprof(op_bfd const & abfd, profile_container const & samples,
profile_t const & cg_db, string const & gmon_filename)
{
static gmon_hdr hdr = { { 'g', 'm', 'o', 'n' }, GMON_VERSION, {0,0,0,},};
bfd_vma low_pc;
bfd_vma high_pc;
/* FIXME worth to try more multiplier ? */
int multiplier = 2;
if (aligned_samples(samples, 4))
multiplier = 8;
cverb << "opgrof multiplier: " << multiplier << endl;
get_vma_range(low_pc, high_pc, samples);
cverb << "low_pc: " << hex << low_pc << " " << "high_pc: "
<< high_pc << dec << endl;
// round-down low_pc to ensure bin number is correct in the inner loop
low_pc = (low_pc / multiplier) * multiplier;
// round-up high_pc to ensure a correct histsize calculus
high_pc = ((high_pc + multiplier - 1) / multiplier) * multiplier;
cverb << "low_pc: " << hex << low_pc << " " << "high_pc: "
<< high_pc << dec << endl;
size_t histsize = (high_pc - low_pc) / multiplier;
// FIXME: must we skip the flat profile write if histsize == 0 ?
// (this can occur with callgraph w/o samples to the binary) but in
// this case user must gprof --no-flat-profile whiwh is a bit boring
// and result *seems* weirds.
FILE * fp = op_open_file(gmon_filename.c_str(), "w");
op_write_file(fp,&hdr, sizeof(gmon_hdr));
op_write_u8(fp, GMON_TAG_TIME_HIST);
op_write_vma(fp, abfd, low_pc);
op_write_vma(fp, abfd, high_pc);
/* size of histogram */
op_write_u32(fp, histsize);
/* profiling rate */
op_write_u32(fp, 1);
op_write_file(fp, "samples\0\0\0\0\0\0\0\0", 15);
/* abbreviation */
op_write_u8(fp, '1');
u16 * hist = (u16*)xcalloc(histsize, sizeof(u16));
profile_container::symbol_choice choice;
choice.threshold = options::threshold;
symbol_collection symbols = samples.select_symbols(choice);
symbol_collection::const_iterator sit = symbols.begin();
symbol_collection::const_iterator send = symbols.end();
for (; sit != send; ++sit) {
sample_container::samples_iterator it = samples.begin(*sit);
sample_container::samples_iterator end = samples.end(*sit);
for (; it != end ; ++it) {
u32 pos = (it->second.vma - low_pc) / multiplier;
u32 count = it->second.counts[0];
if (pos >= histsize) {
cerr << "Bogus histogram bin " << pos
<< ", larger than " << pos << " !\n";
continue;
}
if (hist[pos] + count > (u16)-1) {
hist[pos] = (u16)-1;
cerr << "Warning: capping sample count by "
<< hist[pos] + count - ((u16)-1) << endl;
} else {
hist[pos] += (u16)count;
}
}
}
op_write_file(fp, hist, histsize * sizeof(u16));
if (!cg_db.empty())
output_cg(fp, abfd, cg_db);
op_close_file(fp);
free(hist);
}
void
load_samples(op_bfd const & abfd, list<profile_sample_files> const & files,
string const & image, profile_container & samples)
{
list<profile_sample_files>::const_iterator it = files.begin();
list<profile_sample_files>::const_iterator const end = files.end();
for (; it != end; ++it) {
// we can get call graph w/o any samples to the binary
if (it->sample_filename.empty())
continue;
profile_t profile;
profile.add_sample_file(it->sample_filename,
abfd.get_start_offset());
check_mtime(abfd.get_filename(), profile.get_header());
samples.add(profile, abfd, image, 0);
}
}
void load_cg(profile_t & cg_db, list<profile_sample_files> const & files)
{
list<profile_sample_files>::const_iterator it = files.begin();
list<profile_sample_files>::const_iterator const end = files.end();
/* the list of non cg files is a super set of the list of cg file
* (module always log a samples to non-cg files before logging
* call stack) so by using the list of non-cg file we are sure to get
* all existing cg files.
*/
for (; it != end; ++it) {
list<string>::const_iterator cit;
list<string>::const_iterator const cend = it->cg_files.end();
for (cit = it->cg_files.begin(); cit != cend; ++cit) {
// FIXME: do we need filtering ?
cg_db.add_sample_file(*cit, 0);
}
}
}
int opgprof(vector<string> const & non_options)
{
handle_options(non_options);
profile_container samples(false, true);
bool ok = image_profile.error == image_ok;
// FIXME: symbol_filter would be allowed through option
op_bfd abfd(image_profile.image, string_filter(), ok);
if (!ok && image_profile.error == image_ok)
image_profile.error = image_format_failure;
if (image_profile.error != image_ok) {
report_image_error(image_profile, true);
exit(EXIT_FAILURE);
}
load_samples(abfd, image_profile.groups[0].begin()->files,
image_profile.image, samples);
profile_t cg_db;
load_cg(cg_db, image_profile.groups[0].begin()->files);
output_gprof(abfd, samples, cg_db, options::gmon_filename);
return 0;
}
} // anonymous namespace
int main(int argc, char const * argv[])
{
return run_pp_tool(argc, argv, opgprof);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks