-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmccontext.cpp
More file actions
180 lines (142 loc) · 3.38 KB
/
pmccontext.cpp
File metadata and controls
180 lines (142 loc) · 3.38 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
#include "pmccontext.h"
#include <strings.h>
#include <exception>
#include <errno.h>
std::string
PmcContext::getPmcInitErrorMessage(int error)
{
std::string message("error in pmc_init");
switch (error) {
case ENOENT:
message += ": hwpmc module not loaded?";
break;
case EPROGMISMATCH:
message +=
": libpmc version number does not match hwpmc version number";
break;
case ENXIO:
message += ": pmc hardware not supported";
break;
}
message += "(";
message += strerror(errno);
message += ")";
return (message);
}
std::string
PmcContext::getPmcErrorMessage(const std::string &event, const char *function,
int error)
{
std::string message("error in ");
message += function;
message += " of ";
message += event;
message += ": ";
message += strerror(errno);
return (message);
}
PmcContext::PmcContext()
: m_cpuMask(~0)
{
int error = pmc_init();
if (error)
throw PmcException(getPmcInitErrorMessage(errno));
}
void
PmcContext::loadPmc(const std::string &name)
{
PmcMap::iterator it;
pmc_id_t pmcId;
int error;
int cpuMask;
int cpu;
it = m_pmcs.lower_bound(name);
if (it == m_pmcs.end() || (m_pmcs.key_comp()(name, it->first) != 0)) {
cpuMask = m_cpuMask;
it = m_pmcs.insert(it, PmcMap::value_type(name, PmcCpuMap()));
while (cpuMask) {
cpu = ffs(cpuMask) - 1;
error = pmc_allocate(name.c_str(), PMC_MODE_SC, 0, cpu,
&pmcId);
if (error) {
clearCpuMap(it->second);
m_pmcs.erase(it);
throw PmcException(getPmcErrorMessage(name,
"pmc_allocate", errno));
}
error = pmc_start(pmcId);
if (error) {
clearCpuMap(it->second);
m_pmcs.erase(it);
throw PmcException(getPmcErrorMessage(name,
"pmc_start", errno));
}
it->second.insert(PmcCpuMap::value_type(cpu, pmcId));
cpuMask &= ~(1 << cpu);
}
/*
* Some pmcs start with a non-zero value(e.g. the tsc). Most
* will start at zero. Read the value now so we get a consistent
* delta when we go to get the pmc value for the first time.
*/
readPmc(it);
}
}
void
PmcContext::clearCpuMap(PmcCpuMap &map)
{
PmcCpuMap::iterator it;
for (it = map.begin(); it != map.end(); ++it)
pmc_release(it->second.m_id);
map.clear();
}
void
PmcContext::clearPmcs()
{
PmcMap::iterator it;
for (it = m_pmcs.begin(); it != m_pmcs.end(); ++it)
clearCpuMap(it->second);
m_pmcs.clear();
}
void
PmcContext::readPmc(const PmcMap::iterator &it)
{
PmcCpuMap::iterator jt;
for (jt = it->second.begin(); jt != it->second.end(); ++jt) {
pmc_value_t v;
pmc_read(jt->second.m_id, &v);
jt->second.m_value = v - jt->second.m_lastAbsolute;
jt->second.m_lastAbsolute = v;
}
}
void
PmcContext::readPmcs()
{
PmcMap::iterator it;
for (it = m_pmcs.begin(); it != m_pmcs.end(); ++it)
readPmc(it);
}
pmc_value_t
PmcContext::getPmc(const std::string &name) throw (PmcNotLoaded)
{
pmc_value_t total;
PmcMap::iterator it = m_pmcs.find(name);
if(it == m_pmcs.end())
throw PmcNotLoaded();
total = 0;
PmcCpuMap::iterator jt;
for(jt = it->second.begin(); jt != it->second.end(); ++jt)
total += jt->second.m_value;
return (total);
}
pmc_value_t
PmcContext::getPmcCpu(const std::string &name, int cpu) throw (PmcNotLoaded)
{
PmcMap::iterator it = m_pmcs.find(name);
if(it == m_pmcs.end())
throw PmcNotLoaded();
PmcCpuMap::iterator jt = it->second.find(cpu);
if(jt == it->second.end())
throw std::runtime_error("unknown cpu");
return (jt->second.m_value);
}