1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  * Author: Antonio Caggiano <antonio.caggiano@collabora.com>
4  * Author: Robert Beckett <bob.beckett@collabora.com>
5  * Author: Corentin Noël <corentin.noel@collabora.com>
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 The minimum sampling period for the current device
49    virtual uint64_t get_min_sampling_period_ns() = 0;
50 
51    /// @brief Enable a counter by its ID
52    virtual void enable_counter(uint32_t counter_id) = 0;
53 
54    virtual void enable_all_counters() = 0;
55 
56    /// @brief Initialize performance counters data such as groups and counters
57    /// @return Whether it was successful or not
58    virtual bool init_perfcnt() = 0;
59 
60    /// @brief Enables performance counters, meaning that from now on they can be sampled
61    virtual void enable_perfcnt(uint64_t sampling_period_ns) = 0;
62 
63    /// @brief Disables performance counters on the device
64    virtual void disable_perfcnt() = 0;
65 
66    /// @brief Asking the GPU to dump performance counters could have different meanings
67    /// depending on the concrete driver. Some could just ask the GPU to dump counters to a
68    /// user space buffer, while some others will need to read data from a stream which was
69    /// written asynchronously.
70    /// @return Whether it was able to dump, false otherwise
71    virtual bool dump_perfcnt() = 0;
72 
73    /// @brief After dumping performance counters, with this function you can iterate
74    /// through the samples collected.
75    /// @return The CPU timestamp associated to current sample, or 0 if there are no more samples
76    virtual uint64_t next() = 0;
77 
78    DrmDevice drm_device;
79 
80    /// List of counter groups
81    std::vector<CounterGroup> groups;
82 
83    /// List of counters exposed by the GPU
84    std::vector<Counter> counters;
85 
86    /// List of counters that are actually enabled
87    std::vector<Counter> enabled_counters;
88 
89    protected:
90    // Prevent object slicing by allowing move only from subclasses
91    Driver(Driver &&) = default;
92    Driver &operator=(Driver &&) = default;
93 };
94 
95 } // namespace pps
96