xref: /aosp_15_r20/external/mesa3d/src/tool/pps/pps_counter.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  * Author: Antonio Caggiano <[email protected]>
4  * Author: Rohan Garg <[email protected]>
5  * Author: Robert Beckett <[email protected]>
6  *
7  * SPDX-License-Identifier: MIT
8  */
9 
10 #pragma once
11 
12 #include <cstdint>
13 #include <functional>
14 #include <string>
15 #include <variant>
16 #include <vector>
17 
18 namespace pps
19 {
20 struct CounterGroup {
21    std::string name;
22 
23    uint32_t id;
24 
25    /// List of counters ID belonging to this group
26    std::vector<int32_t> counters;
27 
28    std::vector<CounterGroup> subgroups;
29 };
30 
31 class Driver;
32 
33 class Counter
34 {
35    public:
36    /// @brief A counter value can be of different types depending on what it represents:
37    /// cycles, cycles-per-instruction, percentages, bytes, and so on.
38    enum class Units {
39       Percent,
40       Byte,
41       Hertz,
42       None,
43    };
44 
45    using Value = std::variant<int64_t, double>;
46 
47    /// @param c Counter which we want to retrieve a value
48    /// @param d Driver used to sample performance counters
49    /// @return The value of the counter
50    using Getter = Value(const Counter &c, const Driver &d);
51 
52    Counter() = default;
53    virtual ~Counter() = default;
54 
55    /// @param id ID of the counter
56    /// @param name Name of the counter
57    /// @param group Group ID this counter belongs to
58    Counter(int32_t id, const std::string &name, int32_t group);
59 
60    bool operator==(const Counter &c) const;
61 
62    /// @param get New getter function for this counter
63    void set_getter(const std::function<Getter> &get);
64 
65    /// @brief d Driver used to sample performance counters
66    /// @return Last sampled value for this counter
67    Value get_value(const Driver &d) const;
68 
69    /// Id of the counter
70    int32_t id = -1;
71 
72    /// Name of the counter
73    std::string name = "";
74 
75    /// ID of the group this counter belongs to
76    int32_t group = -1;
77 
78    /// Offset of this counter within GPU counter list
79    /// For derived counters it is negative and remains unused
80    int32_t offset = -1;
81 
82    /// Whether it is a derived counter or not
83    bool derived = false;
84 
85    /// Returns the value of this counter
86    std::function<Getter> getter;
87 
88    /// The unit of the counter
89    Units units;
90 };
91 
92 /// @param get New getter function for this counter
set_getter(const std::function<Getter> & get)93 inline void Counter::set_getter(const std::function<Getter> &get)
94 {
95    getter = get;
96 }
97 
98 /// @brief d Driver used to sample performance counters
99 /// @return Last sampled value for this counter
get_value(const Driver & d)100 inline Counter::Value Counter::get_value(const Driver &d) const
101 {
102    return getter(*this, d);
103 }
104 
105 /// @return The underlying u32 value
to_u32(T && elem)106 template<typename T> constexpr uint32_t to_u32(T &&elem)
107 {
108    return static_cast<uint32_t>(elem);
109 }
110 
111 } // namespace pps
112