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
|
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <iostream>
#include "retrace.hpp"
#include "metric_backend.hpp"
#include "metric_writer.hpp"
#include "metric_backend_amd_perfmon.hpp"
#include "metric_backend_intel_perfquery.hpp"
#include "metric_backend_opengl.hpp"
#include "mmap_allocator.hpp"
namespace glretrace {
bool metricBackendsSetup = false;
bool profilingContextAcquired = false;
bool profilingBoundaries[QUERY_BOUNDARY_LIST_END] = {false};
unsigned profilingBoundariesIndex[QUERY_BOUNDARY_LIST_END] = {0};
std::vector<MetricBackend*> metricBackends; // to be populated in initContext()
MetricBackend* curMetricBackend = nullptr; // backend active in the current pass
MetricWriter profiler(metricBackends, MmapAllocator<char>());
MetricBackend* getBackend(std::string backendName) {
// allocator for metric storage
MmapAllocator<char> alloc;
// to be populated with backends
Context *currentContext = getCurrentContext();
if (backendName == "GL_AMD_performance_monitor") return &MetricBackend_AMD_perfmon::getInstance(currentContext, alloc);
else if (backendName == "GL_INTEL_performance_query") return &MetricBackend_INTEL_perfquery::getInstance(currentContext, alloc);
else if (backendName == "opengl") return &MetricBackend_opengl::getInstance(currentContext, alloc);
else return nullptr;
}
bool
isLastPass() {
return ( retrace::curPass + 1 >= retrace::numPasses );
}
/* Callbacks for listing metrics with --list-metrics */
void listMetrics_metricCallback(Metric* c, int error, void* userData) {
static const std::string metricType[] = {"CNT_TYPE_GENERIC", "CNT_TYPE_NUM",
"CNT_TYPE_DURATION", "CNT_TYPE_PERCENT",
"CNT_TYPE_TIMESTAMP", "CNT_TYPE_OTHER"};
static const std::string metricNumType[] = {"CNT_NUM_UINT", "CNT_NUM_FLOAT",
"CNT_NUM_UINT64", "CNT_NUM_DOUBLE",
"CNT_NUM_BOOL", "CNT_NUM_INT64"};
std::cout << " Metric #" << c->id() << ": " << c->name()
<< " (type: " << metricType[c->type()] << ", num. type: "
<< metricNumType[c->numType()] << ").\n";
std::cout << " Description: " << c->description() << "\n";
}
void listMetrics_groupCallback(unsigned g, int error, void* userData) {
MetricBackend* b = reinterpret_cast<MetricBackend*>(userData);
std::cout << "\n Group #" << g << ": " << b->getGroupName(g) << ".\n";
b->enumMetrics(g, listMetrics_metricCallback, userData);
}
void listMetricsCLI() {
// backends is to be populated with backend names
std::string backends[] = {"GL_AMD_performance_monitor",
"GL_INTEL_performance_query",
"opengl"};
std::cout << "Available metrics: \n";
for (auto s : backends) {
auto b = getBackend(s);
if (!b->isSupported()) {
continue;
}
std::cout << "\nBackend " << s << ":\n";
b->enumGroups(listMetrics_groupCallback, b);
std::cout << std::endl;
}
}
void parseMetricsBlock(QueryBoundary pollingRule, const char* str,
std::size_t limit, MetricBackend* backend)
{
const char* end;
bool lastItem = false;
while (((end = reinterpret_cast<const char*>(std::memchr(str, ',', limit))) != nullptr)
|| !lastItem)
{
std::unique_ptr<Metric> p;
std::string metricName;
if (!end) {
lastItem = true;
end = str + limit;
}
std::size_t span = std::strspn(str, " ");
limit -= span;
str += span;
// parse [group, id]
if (*str == '[') {
std::string groupStr = std::string(str, 1, end-str-1);
limit -= end + 1 - str;
str = end + 1;
end = reinterpret_cast<const char*>(std::memchr(str, ']', limit));
std::string idStr = std::string(str, 0, end-str);
limit -= end + 1 - str;
str = end + 1;
const char* next = reinterpret_cast<const char*>(std::memchr(str, ',', limit));
if (next) {
end = next;
limit -= end + 1 - str;
str = end + 1;
}
#if defined(ANDROID)
// http://stackoverflow.com/questions/17950814/how-to-use-stdstoul-and-stdstoull-in-android
unsigned groupId = strtoul(groupStr.c_str(), nullptr, 10);
unsigned metricId = strtoul(idStr.c_str(), nullptr, 10);
#else
unsigned groupId = std::stoul(groupStr);
unsigned metricId = std::stoul(idStr);
#endif
p = backend->getMetricById(groupId, metricId);
metricName = "[" + groupStr + ", " + idStr + "]";
// parse metricName
} else {
if (end - str) {
metricName = std::string(str, 0, end-str);
p = backend->getMetricByName(metricName);
}
limit -= end + (lastItem ? 0 : 1) - str;
str = end + (lastItem ? 0 : 1);
if (metricName.empty()) continue;
}
if (!p) {
std::cerr << "Warning: No metric \"" << metricName
<< "\"." << std::endl;
continue;
}
int error = backend->enableMetric(p.get(), pollingRule);
if (error) {
std::cerr << "Warning: Metric " << metricName << " not enabled"
" (error " << error << ")." << std::endl;
} else {
profilingBoundaries[pollingRule] = true;
}
}
}
void parseBackendBlock(QueryBoundary pollingRule, const char* str,
std::size_t limit, std::set<MetricBackend*> &backendsHash)
{
const char* delim = reinterpret_cast<const char*>(std::memchr(str, ':', limit));
if (delim) {
std::size_t span = std::strspn(str, " ");
std::string backendName = std::string(str, span, delim-str-span);
MetricBackend* backend = getBackend(backendName);
if (!backend) {
std::cerr << "Warning: No backend \"" << backendName << "\"."
<< std::endl;
return;
}
if (!backend->isSupported()) {
std::cerr << "Warning: Backend \"" << backendName
<< "\" is not supported." << std::endl;
return;
}
/**
* order in metricBackends is important for output
* also there should be no duplicates
*/
if (backendsHash.find(backend) == backendsHash.end()) {
metricBackends.push_back(backend);
backendsHash.insert(backend);
}
limit -= (delim-str) + 1;
parseMetricsBlock(pollingRule, delim + 1, limit, backend);
}
}
void enableMetricsFromCLI(const char* metrics, QueryBoundary pollingRule) {
static std::set<MetricBackend*> backendsHash; // for not allowing duplicates
const char* end;
while ((end = std::strchr(metrics, ';')) != nullptr) {
parseBackendBlock(pollingRule, metrics, end-metrics, backendsHash);
metrics = end + 1;
}
parseBackendBlock(pollingRule, metrics, std::strlen(metrics), backendsHash);
}
} /* namespace glretrace */
|