xref: /aosp_15_r20/external/mesa3d/src/tool/pps/pps_driver.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  * Author: Antonio Caggiano <[email protected]>
4  * Author: Robert Beckett <[email protected]>
5  * Author: Corentin Noël <[email protected]>
6  *
7  * SPDX-License-Identifier: MIT
8  */
9 
10 #pragma once
11 
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16 
17 #include "pps_counter.h"
18 #include "pps_device.h"
19 
20 namespace pps
21 {
22 /// @brief Abstract Driver class
23 class Driver
24 {
25    public:
26    /// @return A map of supported DRM device names and their relative pps driver
27    static const std::unordered_map<std::string, std::unique_ptr<Driver>> &get_supported_drivers();
28 
29    /// @return A list of supported DRM device names
30    static const std::vector<std::string> supported_device_names();
31 
32    /// @return A driver supporting a specific DRM device
33    static Driver *get_driver(DrmDevice &&drm_device);
34 
35    /// @return The name of a default selected PPS driver
36    static std::string default_driver_name();
37 
38    /// @return The name of a driver based on the request, otherwise the default driver name
39    static std::string find_driver_name(const char *requested_name);
40 
41    Driver() = default;
42    virtual ~Driver() = default;
43 
44    // Forbid copy
45    Driver(const Driver &) = delete;
46    Driver &operator=(const Driver &) = delete;
47 
48    /// @return Whether dump_perfcnt is preemptible
is_dump_perfcnt_preemptible()49    virtual bool is_dump_perfcnt_preemptible() const { return true; }
50 
51    /// @return The minimum sampling period for the current device
52    virtual uint64_t get_min_sampling_period_ns() = 0;
53 
54    /// @brief Enable a counter by its ID
55    virtual void enable_counter(uint32_t counter_id) = 0;
56 
57    virtual void enable_all_counters() = 0;
58 
59    /// @brief Initialize performance counters data such as groups and counters
60    /// @return Whether it was successful or not
61    virtual bool init_perfcnt() = 0;
62 
63    /// @brief Enables performance counters, meaning that from now on they can be sampled
64    virtual void enable_perfcnt(uint64_t sampling_period_ns) = 0;
65 
66    /// @brief Disables performance counters on the device
67    virtual void disable_perfcnt() = 0;
68 
69    /// @brief Asking the GPU to dump performance counters could have different meanings
70    /// depending on the concrete driver. Some could just ask the GPU to dump counters to a
71    /// user space buffer, while some others will need to read data from a stream which was
72    /// written asynchronously.
73    /// @return Whether it was able to dump, false otherwise
74    virtual bool dump_perfcnt() = 0;
75 
76    /// @brief After dumping performance counters, with this function you can iterate
77    /// through the samples collected.
78    /// @return The GPU timestamp associated to current sample, or 0 if there are no more samples
79    virtual uint64_t next() = 0;
80 
81    /// Clock ID in which the values returned by gpu_timestamp() belong
82    virtual uint32_t gpu_clock_id() const = 0;
83 
84    /// Sample a timestamp from the GPU
85    virtual uint64_t gpu_timestamp() const = 0;
86 
87    /// Sample a timestamp from both the CPU & the GPU
88    ///
89    /// This is useful when the driver can do a better timestamp correlation
90    /// than sampling separately CPU & GPU timestamps.
91    virtual bool cpu_gpu_timestamp(uint64_t &cpu_timestamp, uint64_t &gpu_timestamp) const = 0;
92 
93    DrmDevice drm_device;
94 
95    /// List of counter groups
96    std::vector<CounterGroup> groups;
97 
98    /// List of counters exposed by the GPU
99    std::vector<Counter> counters;
100 
101    /// List of counters that are actually enabled
102    std::vector<Counter> enabled_counters;
103 
104    protected:
105    // Prevent object slicing by allowing move only from subclasses
106    Driver(Driver &&) = default;
107    Driver &operator=(Driver &&) = default;
108 };
109 
110 } // namespace pps
111