summaryrefslogtreecommitdiff
path: root/retrace/metric_helper.cpp
blob: cafcf80f2bdadbec23881cc0aefd446fedd19ed5 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**************************************************************************
 *
 * Copyright 2015 Alexander Trukhin
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 **************************************************************************/

#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() {
    static MetricWriter writer(metricBackends, MmapAllocator<char>());
    return writer;
}

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);

    profiler(); // initialize MetricWriter
}

} /* namespace glretrace */