xref: /aosp_15_r20/external/mesa3d/src/intel/ds/intel_pps_driver.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020-2021 Collabora, Ltd.
3  * Author: Antonio Caggiano <[email protected]>
4  *
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #pragma once
9 
10 #include <pps/pps_driver.h>
11 
12 extern "C" {
13 struct intel_perf_query_info;
14 };
15 
16 namespace pps
17 {
18 
19 class IntelPerf;
20 
21 /// @brief Variable length sequence of bytes generated by Intel Obstervation Architecture (OA)
22 struct PerfRecord {
23    /// Timestamp in the GPU clock domain
24    uint64_t timestamp;
25 
26    /// intel_perf_record_header + report data
27    std::vector<uint8_t> data;
28 };
29 
30 /// @brief PPS Driver implementation for Intel graphics devices.
31 /// When sampling it may collect multiple perf-records at once. Each perf-record holds multiple
32 /// counter values. Those values are continuously incremented by the GPU. In order to get a delta,
33 /// the driver computes an _accumulation_ (`last_perf_record - previous_perf_record`).
34 /// For optimization purposes, it might ignore some perf-records, considering only those
35 /// perf-records close to the boundary of the sampling period range.
36 class IntelDriver : public Driver
37 {
38    public:
39    IntelDriver();
40    ~IntelDriver();
41 
42    uint64_t get_min_sampling_period_ns() override;
43    bool init_perfcnt() override;
44    void enable_counter(uint32_t counter_id) override;
45    void enable_all_counters() override;
46    void enable_perfcnt(uint64_t sampling_period_ns) override;
47    void disable_perfcnt() override;
48    bool dump_perfcnt() override;
49    uint64_t next() override;
50    uint32_t gpu_clock_id() const override;
51    uint64_t gpu_timestamp() const override;
52    bool cpu_gpu_timestamp(uint64_t &cpu_timestamp,
53                           uint64_t &gpu_timestamp) const override;
54 
55    private:
56    /// @brief Requests the next perf sample
57    /// @return The sample GPU timestamp
58    uint64_t gpu_next();
59 
60    /// @param data Buffer of bytes to parse
61    /// @param byte_count Number of bytes to parse
62    /// @return A list of perf records parsed from raw data passed as input
63    std::vector<PerfRecord> parse_perf_records(const std::vector<uint8_t> &data, size_t byte_count);
64 
65    /// @brief Reads data from the GPU metric set
66    void read_data_from_metric_set();
67 
68    /// Sampling period in nanoseconds requested by the datasource
69    uint64_t sampling_period_ns = 0;
70 
71    /// Last upper 32bits of the GPU timestamp in the parsed reports
72    uint64_t gpu_timestamp_udw = 0;
73 
74    /// Keep track of the timestamp of the last sample generated (upper & lower
75    /// 32bits)
76    uint64_t last_gpu_timestamp = 0;
77 
78    /// Data buffer used to store data read from the metric set
79    std::vector<uint8_t> metric_buffer = std::vector<uint8_t>(1024, 0);
80    /// Number of bytes read so far still un-parsed.
81    /// Reset once bytes from the metric buffer are parsed to perf records
82    size_t total_bytes_read = 0;
83 
84    /// List of OA perf records read so far
85    std::vector<PerfRecord> records;
86 
87    std::unique_ptr<IntelPerf> perf;
88 
89    // Gpu clock ID used to correlate GPU/CPU timestamps
90    uint32_t clock_id = 0;
91 
92    // Selected query
93    intel_perf_query_info *selected_query = nullptr;
94 };
95 
96 } // namespace pps
97