-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample_file_util.cpp
More file actions
112 lines (90 loc) · 2.75 KB
/
sample_file_util.cpp
File metadata and controls
112 lines (90 loc) · 2.75 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
#include <iostream>
#include <iomanip>
#include <ios>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include "protobuf_util.h"
#include "memory_samples.pb.h"
#include "sample_file_util.h"
using namespace std;
sample_file::Sample make_sample(unsigned window_id, const sample_file::MemoryAccess& begin, const sample_file::MemoryAccess& end)
{
sample_file::Sample sample;
sample.set_window_id(window_id);
sample.mutable_begin()->CopyFrom(begin);
sample.mutable_end()->CopyFrom(end);
return sample;
}
sample_file::Sample make_dangling_sample(unsigned window_id, const sample_file::MemoryAccess& begin)
{
sample_file::Sample sample;
sample.set_window_id(window_id);
sample.mutable_begin()->CopyFrom(begin);
return sample;
}
void print_memoryaccess(const sample_file::MemoryAccess& access)
{
cout << "access counter: " << access.access_counter() << endl;
cout << std::hex << "memory address: " << access.memory_address() << endl;
cout << "program counter: " << access.program_counter() << endl;
std::setbase(10);
}
void print_sample(const sample_file::Sample& sample)
{
const bool has_end = sample.has_end();
if (has_end)
cout << "==Sample==" << endl;
else
cout << "==Dangling Sample==" << endl;
cout << "Begin:" << endl;
print_memoryaccess(sample.begin());
if (has_end)
{
cout << "End:" << endl;
print_memoryaccess(sample.end());
}
}
SampleWriter::SampleWriter(const string& filename) : filename(filename), finalized(false), ofs(filename, ios::binary)
{
if (ofs.fail())
throw ios_base::failure("Could not open the sample file " + filename);
oos = new google::protobuf::io::OstreamOutputStream(&ofs);
}
void SampleWriter::write_sample(const sample_file::Sample& sample)
{
bool ret = writeDelimitedTo_custom(sample, oos);
if (!ret)
throw ios_base::failure("Output error on sample file " + filename);
}
void SampleWriter::finalize(void)
{
if (!finalized)
{
delete oos;
ofs.close();
finalized = true;
}
}
SampleWriter::~SampleWriter(void)
{
if (!finalized)
finalize();
}
SampleReader::SampleReader(const string& filename) : filename(filename), finalized(false), ifs(filename, ios::binary)
{
if (ifs.fail())
throw ios_base::failure("Could not open the sample file " + filename);
iis = new google::protobuf::io::IstreamInputStream(&ifs);
}
bool SampleReader::read_sample(sample_file::Sample& sample)
{
bool clean_eof;
const bool ret = readDelimitedFrom_custom(iis, &sample, &clean_eof);
if (!ret && !clean_eof)
throw ios_base::failure("Input error on sample file " + filename);
return ret;
}
SampleReader::~SampleReader(void)
{
delete iis;
ifs.close();
}