-
Notifications
You must be signed in to change notification settings - Fork 17
/
qsim-prof.cpp
86 lines (70 loc) · 2.37 KB
/
qsim-prof.cpp
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
/*****************************************************************************\
* Qemu Simulation Framework (qsim) *
* Qsim is a modified version of the Qemu emulator (www.qemu.org), coupled *
* a C++ API, for the use of computer architecture researchers. *
* *
* This work is licensed under the terms of the GNU GPL, version 2. See the *
* COPYING file in the top-level directory. *
\*****************************************************************************/
#include <qsim-prof.h>
#include <fstream>
#include <vector>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <pthread.h>
using namespace Qsim;
using namespace std;
class QsimProf {
public:
QsimProf(OSDomain &osd, const char *filename, unsigned w, unsigned s):
tr(filename), osd(osd), w(w), s(s), t(osd.get_n())
{
pthread_mutex_init(&trLock, NULL);
icbH = osd.set_inst_cb(this, &QsimProf::inst_cb);
}
~QsimProf() {
//osd.unset_inst_cb(icbH);
tr.close();
}
void inst_cb(int c, uint64_t va, uint64_t pa, uint8_t len, const uint8_t *b,
enum inst_type type)
{
if (++t[c].windowCount == w) t[c].windowCount = 0;
if (t[c].windowCount == 0) {
t[c].samp.clear();
for (unsigned i = 0; i < s; ++i) t[c].samp.insert(rand()%w);
}
if (t[c].samp.find(t[c].windowCount) != t[c].samp.end()) {
pthread_mutex_lock(&trLock);
tr << std::dec << c << ", " << va << ", "
<< (osd.get_prot(c) == OSDomain::PROT_KERN) << ", ";
for (unsigned i = 0; i < len; ++i)
tr << std::hex << setw(2) << setfill('0') << (unsigned)b[i] << ' ';
tr << '\n';
pthread_mutex_unlock(&trLock);
}
}
private:
struct Thread {
Thread(): samp(), windowCount(0) {}
set<unsigned> samp;
unsigned windowCount;
unsigned char padding[64];
};
OSDomain::inst_cb_handle_t icbH;
pthread_mutex_t trLock;
ofstream tr;
OSDomain &osd;
unsigned w, s;
vector<Thread> t;
};
static QsimProf *prof(NULL);
void Qsim::start_prof(OSDomain &osd, const char *tracefile,
unsigned window, unsigned samplesPerWindow)
{
prof = new QsimProf(osd, tracefile, window, samplesPerWindow);
}
void Qsim::end_prof(OSDomain &osd) {
if (prof) delete(prof);
}