|
6 | 6 |
|
7 | 7 | #pragma once |
8 | 8 |
|
| 9 | +#include "hwcpipe/hwcpipe_counter.h" |
9 | 10 | #include "hwcpipe/types.hpp" |
10 | 11 |
|
11 | 12 | #include <device/handle.hpp> |
|
14 | 15 | #include <device/hwcnt/sampler/manual.hpp> |
15 | 16 | #include <device/instance.hpp> |
16 | 17 |
|
| 18 | +#include <cstdint> |
| 19 | +#include <initializer_list> |
| 20 | +#include <system_error> |
17 | 21 | namespace hwcpipe { |
18 | 22 | namespace detail { |
19 | 23 |
|
| 24 | +namespace expression { |
20 | 25 | /** |
21 | | - * Structure representing the block type/offset address of a counter within |
22 | | - * a particular GPU's PMU data. |
| 26 | + * The expression evaluation context provides an abstraction over some block of |
| 27 | + * hardware counter storage. It breaks an otherwise cyclic dependency between |
| 28 | + * the sampler class & the expression evaluator function type. |
| 29 | + */ |
| 30 | +class context { |
| 31 | + public: |
| 32 | + /** |
| 33 | + * @brief Returns the value a hardware counter from the sampler to be used |
| 34 | + * in the expression evaluator |
| 35 | + * |
| 36 | + * @param counter Hardware counter to get the value for. |
| 37 | + * @return the counter value |
| 38 | + */ |
| 39 | + HWCP_NODISCARD virtual double get_counter_value(hwcpipe_counter counter) const = 0; |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief Returns the AXI bus width in bytes |
| 43 | + */ |
| 44 | + HWCP_NODISCARD virtual double get_mali_config_ext_bus_byte_size() const = 0; |
| 45 | + |
| 46 | + /** |
| 47 | + * @brief Returns the number of shader core (constant) to be used |
| 48 | + * in evaluator function |
| 49 | + */ |
| 50 | + HWCP_NODISCARD virtual double get_mali_config_shader_core_count() const = 0; |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief |
| 54 | + */ |
| 55 | + HWCP_NODISCARD virtual double get_mali_config_l2_cache_count() const = 0; |
| 56 | +}; |
| 57 | + |
| 58 | +// Signature for generated evaluation functions. |
| 59 | +using evaluator = double (*)(const context &); |
| 60 | +/** |
| 61 | + * Holds information about the expression that the sampler will need when |
| 62 | + * registering the counters and evaluating. |
| 63 | + */ |
| 64 | +struct expression_definition { |
| 65 | + /** |
| 66 | + * Pointer to the function that will evaluate the expression and return the |
| 67 | + * calculated result. |
| 68 | + */ |
| 69 | + evaluator eval; |
| 70 | + /** |
| 71 | + * The list of counters that this expression depends on. These will need to |
| 72 | + * be implicitly registered with the sampler so that they can be collected |
| 73 | + * when it is polled. |
| 74 | + */ |
| 75 | + const std::initializer_list<hwcpipe_counter> dependencies; |
| 76 | +}; |
| 77 | + |
| 78 | +} // namespace expression |
| 79 | +/** |
| 80 | + * Structure representing the block type/offset address and shift |
| 81 | + * scaling of a counter within a particular GPU's PMU data. |
23 | 82 | */ |
24 | 83 | struct block_offset { |
25 | 84 | uint32_t offset; |
| 85 | + uint32_t shift; |
26 | 86 | hwcpipe::device::hwcnt::block_type block_type; |
27 | 87 | }; |
28 | 88 |
|
| 89 | +/** |
| 90 | + * Counters can either be raw hardware counters, or they are expressions based |
| 91 | + * on some combination of hardware counters, constants or other expressions. |
| 92 | + * |
| 93 | + * For hardware counters, we need to know their block/offset addresses so that |
| 94 | + * we can read them as we iterate over the block types. For expression counters |
| 95 | + * we need to call the function that will evaluate the formula. |
| 96 | + */ |
| 97 | +struct counter_definition { |
| 98 | + enum class type { invalid, hardware, expression }; |
| 99 | + union u { |
| 100 | + block_offset address{}; |
| 101 | + expression::expression_definition expression; |
| 102 | + explicit u(expression::expression_definition expression) |
| 103 | + : expression(expression) {} |
| 104 | + explicit u(block_offset address) |
| 105 | + : address(address) {} |
| 106 | + } u; |
| 107 | + type tag; |
| 108 | + |
| 109 | + counter_definition() |
| 110 | + : tag(type::invalid) |
| 111 | + , u(block_offset{0, 0, device::hwcnt::block_type::fe}) {} |
| 112 | + explicit counter_definition(expression::expression_definition expression) |
| 113 | + : tag(type::expression) |
| 114 | + , u(expression) {} |
| 115 | + explicit counter_definition(block_offset address) |
| 116 | + : tag(type::hardware) |
| 117 | + , u(address) {} |
| 118 | +}; |
| 119 | + |
29 | 120 | struct hwcpipe_backend_policy { |
30 | 121 | using handle_type = device::handle; |
31 | 122 | using instance_type = device::instance; |
32 | 123 | using sampler_type = device::hwcnt::sampler::manual; |
33 | 124 | using sample_type = device::hwcnt::sample; |
34 | 125 | }; |
35 | 126 |
|
| 127 | +/** |
| 128 | + * This class should **only** be used in the derived expressions (derived_functions.cpp). |
| 129 | + * |
| 130 | + * Used to override the result of 0.0 / 0.0 to return 0.0. Operators for other types included |
| 131 | + * to resolve ambiguity. |
| 132 | + */ |
| 133 | +class hwcpipe_double { |
| 134 | + public: |
| 135 | + double value; |
| 136 | + |
| 137 | + hwcpipe_double(double value) |
| 138 | + : value(value) {} |
| 139 | + |
| 140 | + hwcpipe_double operator+(const hwcpipe_double &other) const { return hwcpipe_double(value + other.value); } |
| 141 | + |
| 142 | + hwcpipe_double operator-(const hwcpipe_double &other) const { return hwcpipe_double(value - other.value); } |
| 143 | + |
| 144 | + hwcpipe_double operator*(const hwcpipe_double &other) const { return hwcpipe_double(value * other.value); } |
| 145 | + |
| 146 | + hwcpipe_double operator/(const hwcpipe_double &other) const { |
| 147 | + if (value == 0.0 && other.value == 0.0) { |
| 148 | + return hwcpipe_double(0.0); |
| 149 | + } |
| 150 | + return hwcpipe_double(value / other.value); |
| 151 | + } |
| 152 | + |
| 153 | + hwcpipe_double operator+(const int &other) const { return hwcpipe_double(value + other); } |
| 154 | + |
| 155 | + hwcpipe_double operator-(const int &other) const { return hwcpipe_double(value - other); } |
| 156 | + |
| 157 | + hwcpipe_double operator*(const int &other) const { return hwcpipe_double(value * other); } |
| 158 | + |
| 159 | + hwcpipe_double operator/(const int &other) const { |
| 160 | + if (value == 0.0 && other == 0) { |
| 161 | + return hwcpipe_double(0.0); |
| 162 | + } |
| 163 | + return hwcpipe_double(value / other); |
| 164 | + } |
| 165 | + |
| 166 | + hwcpipe_double operator+(const double &other) const { return hwcpipe_double(value + other); } |
| 167 | + |
| 168 | + hwcpipe_double operator-(const double &other) const { return hwcpipe_double(value - other); } |
| 169 | + |
| 170 | + hwcpipe_double operator*(const double &other) const { return hwcpipe_double(value * other); } |
| 171 | + |
| 172 | + hwcpipe_double operator/(const double &other) const { |
| 173 | + if (value == 0.0 && other == 0.0) { |
| 174 | + return hwcpipe_double(0.0); |
| 175 | + } |
| 176 | + return hwcpipe_double(value / other); |
| 177 | + } |
| 178 | + |
| 179 | + bool operator<(const hwcpipe_double &other) const { return value < other.value; } |
| 180 | + |
| 181 | + bool operator<=(const hwcpipe_double &other) const { return value <= other.value; } |
| 182 | + |
| 183 | + bool operator>(const hwcpipe_double &other) const { return value > other.value; } |
| 184 | + |
| 185 | + bool operator>=(const hwcpipe_double &other) const { return value >= other.value; } |
| 186 | + |
| 187 | + bool operator==(const hwcpipe_double &other) const { return value == other.value; } |
| 188 | + |
| 189 | + operator double() const { return value; } |
| 190 | +}; |
| 191 | + |
36 | 192 | } // namespace detail |
37 | 193 | } // namespace hwcpipe |
0 commit comments