3
* various free function acting on a sample file header
5
* @remark Copyright 2004 OProfile authors
6
* @remark Read the file COPYING
9
* @author Philippe Elie
10
* @Modifications Daniel Hansel
21
#include <sys/types.h>
26
#include "op_config.h"
27
#include "op_exception.h"
29
#include "op_cpu_type.h"
31
#include "op_header.h"
32
#include "op_events.h"
33
#include "string_manip.h"
34
#include "format_output.h"
35
#include "xml_utils.h"
42
void op_check_header(opd_header const & h1, opd_header const & h2,
43
string const & filename)
45
if (h1.mtime != h2.mtime) {
47
os << "header timestamps are different ("
48
<< h1.mtime << ", " << h2.mtime << ") for "
50
throw op_fatal_error(os.str());
53
if (h1.is_kernel != h2.is_kernel) {
55
os << "header is_kernel flags are different for "
57
throw op_fatal_error(os.str());
60
// Note that in the generated ELF file for anonymous code the vma
61
// of the symbol is exaclty the same vma as the code had during sampling.
63
// Note that we don't check CPU speed since that can vary
64
// freely on the same machine
70
set<string> warned_files;
74
bool is_jit_sample(string const & filename)
76
// suffix for JIT sample files (see FIXME in check_mtime() below)
79
string::size_type pos;
80
pos = filename.rfind(suf);
81
// for JIT sample files do not output the warning to stderr.
82
if (pos != string::npos && pos == filename.size() - suf.size())
88
void check_mtime(string const & file, opd_header const & header)
90
u64 newmtime = op_get_mtime(file.c_str());
92
if (newmtime == header.mtime)
95
if (warned_files.find(file) != warned_files.end())
98
warned_files.insert(file);
100
// Files we couldn't get mtime of have zero mtime
102
// FIXME: header.mtime for JIT sample files is 0. The problem could be that
103
// in opd_mangling.c:opd_open_sample_file() the call of fill_header()
104
// think that the JIT sample file is not a binary file.
105
if (is_jit_sample(file)) {
106
cverb << vbfd << "warning: could not check that the binary file "
107
<< file << " has not been modified since "
108
"the profile was taken. Results may be inaccurate.\n";
110
cerr << "warning: could not check that the binary file "
111
<< file << " has not been modified since "
112
"the profile was taken. Results may be inaccurate.\n";
115
static bool warned_already = false;
117
cerr << "warning: the last modified time of the binary file "
118
"does not match that of the sample file for " << file
121
if (!warned_already) {
122
cerr << "Either this is the wrong binary or the binary "
123
"has been modified since the sample file was created.\n";
124
warned_already = true;
130
opd_header const read_header(string const & sample_filename)
132
int fd = open(sample_filename.c_str(), O_RDONLY);
134
throw op_fatal_error("Can't open sample file:" +
138
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
140
throw op_fatal_error("Can't read sample file header:" +
144
if (memcmp(header.magic, OPD_MAGIC, sizeof(header.magic))) {
146
throw op_fatal_error("Invalid sample file, "
147
"bad magic number: " +
159
string const op_print_event(op_cpu cpu_type, u32 type, u32 um, u32 count)
163
if (cpu_type == CPU_TIMER_INT) {
164
str += "Profiling through timer interrupt";
168
struct op_event * event = op_find_event(cpu_type, type, um);
171
event = op_find_event_any(cpu_type, type);
173
cerr << "Could not locate event " << int(type) << endl;
174
str = "Unknown event";
179
char const * um_desc = 0;
181
for (size_t i = 0; i < event->unit->num; ++i) {
182
if (event->unit->um[i].value == um)
183
um_desc = event->unit->um[i].desc;
186
str += string("Counted ") + event->name;
187
str += string(" events (") + event->desc + ")";
189
if (cpu_type != CPU_RTC) {
190
str += " with a unit mask of 0x";
193
ss << hex << setw(2) << setfill('0') << unsigned(um);
197
str += um_desc ? um_desc : "multiple flags";
201
str += " count " + op_lexical_cast<string>(count);
205
string const op_xml_print_event(op_cpu cpu_type, u32 type, u32 um, u32 count)
209
if (cpu_type == CPU_TIMER_INT || cpu_type == CPU_RTC)
210
return xml_utils::get_timer_setup((size_t)count);
212
struct op_event * event = op_find_event(cpu_type, type, um);
214
event = op_find_event_any(cpu_type, type);
216
cerr << "Could not locate event " << int(type) << endl;
221
if (cpu_type != CPU_RTC) {
222
ostringstream str_out;
224
unit_mask = str_out.str();
227
return xml_utils::get_event_setup(string(event->name),
228
(size_t)count, unit_mask);
233
string const describe_header(opd_header const & header)
235
op_cpu cpu = static_cast<op_cpu>(header.cpu_type);
238
return op_xml_print_event(cpu, header.ctr_event,
239
header.ctr_um, header.ctr_count);
241
return op_print_event(cpu, header.ctr_event,
242
header.ctr_um, header.ctr_count);
246
string const describe_cpu(opd_header const & header)
248
op_cpu cpu = static_cast<op_cpu>(header.cpu_type);
252
string cpu_name = op_get_cpu_name(cpu);
254
str = xml_utils::get_profile_header(cpu_name, header.cpu_speed);
256
str += string("CPU: ") + op_get_cpu_type_str(cpu);
257
if (header.cpu_speed > 0) {
261
ss << header.cpu_speed;
262
str += ss.str() + " MHz (estimated)";