| 1 |
/* |
| 2 |
* Copyright © 2019-2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "pan_pps_driver.h" |
| 7 |
|
| 8 |
#include <cstring> |
| 9 |
#include <xf86drm.h> |
| 10 |
|
| 11 |
#ifndef ANDROID_LIBPERFETTO |
| 12 |
#include <perfetto.h> |
| 13 |
#else |
| 14 |
#include <perfetto/tracing.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include <drm-uapi/panfrost_drm.h> |
| 18 |
#include <perf/pan_perf.h> |
| 19 |
#include <util/macros.h> |
| 20 |
|
| 21 |
#include <pps/pps.h> |
| 22 |
#include <pps/pps_algorithm.h> |
| 23 |
|
| 24 |
namespace pps { |
| 25 |
PanfrostDriver::PanfrostDriver() |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
PanfrostDriver::~PanfrostDriver() |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
uint64_t |
| 34 |
PanfrostDriver::get_min_sampling_period_ns() |
| 35 |
{ |
| 36 |
return 1000000; |
| 37 |
} |
| 38 |
|
| 39 |
std::pair<std::vector<CounterGroup>, std::vector<Counter>> |
| 40 |
PanfrostDriver::create_available_counters(const PanfrostPerf &perf) |
| 41 |
{ |
| 42 |
std::pair<std::vector<CounterGroup>, std::vector<Counter>> ret; |
| 43 |
auto &[groups, counters] = ret; |
| 44 |
|
| 45 |
size_t cid = 0; |
| 46 |
|
| 47 |
for (uint32_t gid = 0; gid < perf.perf->cfg->n_categories; ++gid) { |
| 48 |
const auto &category = perf.perf->cfg->categories[gid]; |
| 49 |
CounterGroup group = {}; |
| 50 |
group.id = gid; |
| 51 |
group.name = category.name; |
| 52 |
|
| 53 |
for (size_t id = 0; id < category.n_counters; ++id) { |
| 54 |
Counter counter = {}; |
| 55 |
counter.id = cid; |
| 56 |
counter.group = gid; |
| 57 |
|
| 58 |
counter.name = category.counters[id].name; |
| 59 |
|
| 60 |
counter.set_getter([=](const Counter &c, const Driver &d) { |
| 61 |
auto &pan_driver = PanfrostDriver::into(d); |
| 62 |
struct pan_perf *perf = pan_driver.perf->perf; |
| 63 |
const auto counter = &perf->cfg->categories[gid].counters[id]; |
| 64 |
return int64_t(pan_perf_counter_read(counter, perf)); |
| 65 |
}); |
| 66 |
|
| 67 |
group.counters.push_back(cid++); |
| 68 |
|
| 69 |
counters.emplace_back(counter); |
| 70 |
} |
| 71 |
|
| 72 |
groups.push_back(group); |
| 73 |
} |
| 74 |
|
| 75 |
return ret; |
| 76 |
} |
| 77 |
|
| 78 |
bool |
| 79 |
PanfrostDriver::init_perfcnt() |
| 80 |
{ |
| 81 |
if (!dev) { |
| 82 |
dev = std::make_unique<PanfrostDevice>(drm_device.fd); |
| 83 |
} |
| 84 |
if (!perf) { |
| 85 |
perf = std::make_unique<PanfrostPerf>(*dev); |
| 86 |
} |
| 87 |
if (groups.empty() && counters.empty()) { |
| 88 |
std::tie(groups, counters) = create_available_counters(*perf); |
| 89 |
} |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
PanfrostDriver::enable_counter(const uint32_t counter_id) |
| 95 |
{ |
| 96 |
enabled_counters.push_back(counters[counter_id]); |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
PanfrostDriver::enable_all_counters() |
| 101 |
{ |
| 102 |
enabled_counters.resize(counters.size()); |
| 103 |
for (size_t i = 0; i < counters.size(); ++i) { |
| 104 |
enabled_counters[i] = counters[i]; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
PanfrostDriver::enable_perfcnt(const uint64_t /* sampling_period_ns */) |
| 110 |
{ |
| 111 |
auto res = perf->enable(); |
| 112 |
if (!check(res, "Failed to enable performance counters")) { |
| 113 |
if (res == -ENOSYS) { |
| 114 |
PERFETTO_FATAL( |
| 115 |
"Please enable unstable ioctls with: modprobe panfrost unstable_ioctls=1"); |
| 116 |
} |
| 117 |
PERFETTO_FATAL("Please verify graphics card"); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
bool |
| 122 |
PanfrostDriver::dump_perfcnt() |
| 123 |
{ |
| 124 |
last_dump_ts = perfetto::base::GetBootTimeNs().count(); |
| 125 |
|
| 126 |
// Dump performance counters to buffer |
| 127 |
if (!check(perf->dump(), "Failed to dump performance counters")) { |
| 128 |
PERFETTO_ELOG("Skipping sample"); |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
uint64_t |
| 136 |
PanfrostDriver::next() |
| 137 |
{ |
| 138 |
auto ret = last_dump_ts; |
| 139 |
last_dump_ts = 0; |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
PanfrostDriver::disable_perfcnt() |
| 145 |
{ |
| 146 |
perf->disable(); |
| 147 |
perf.reset(); |
| 148 |
dev.reset(); |
| 149 |
groups.clear(); |
| 150 |
counters.clear(); |
| 151 |
enabled_counters.clear(); |
| 152 |
} |
| 153 |
|
| 154 |
uint32_t |
| 155 |
PanfrostDriver::gpu_clock_id() const |
| 156 |
{ |
| 157 |
return perfetto::protos::pbzero::BUILTIN_CLOCK_BOOTTIME; |
| 158 |
} |
| 159 |
|
| 160 |
uint64_t |
| 161 |
PanfrostDriver::gpu_timestamp() const |
| 162 |
{ |
| 163 |
return perfetto::base::GetBootTimeNs().count(); |
| 164 |
} |
| 165 |
|
| 166 |
bool |
| 167 |
PanfrostDriver::cpu_gpu_timestamp(uint64_t &, uint64_t &) const |
| 168 |
{ |
| 169 |
/* Not supported */ |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
} // namespace pps |