1/*
2 * Copyright © 2020-2021 Collabora, Ltd.
3 * Author: Antonio Caggiano <antonio.caggiano@collabora.com>
4 *
5 * SPDX-License-Identifier: MIT
6 */
7
8#pragma once
9
10#include <pps/pps_driver.h>
11
12extern "C" {
13struct intel_perf_query_info;
14};
15
16namespace pps
17{
18
19class IntelPerf;
20
21/// @brief Variable length sequence of bytes generated by Intel Obstervation Architecture (OA)
22struct PerfRecord {
23   /// Timestamp in the GPU clock domain
24   uint64_t timestamp;
25
26   /// drm_i915_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.
36class 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
53   private:
54   /// @brief Requests the next perf sample
55   /// @return The sample GPU timestamp
56   uint64_t gpu_next();
57
58   /// @param data Buffer of bytes to parse
59   /// @param byte_count Number of bytes to parse
60   /// @return A list of perf records parsed from raw data passed as input
61   std::vector<PerfRecord> parse_perf_records(const std::vector<uint8_t> &data, size_t byte_count);
62
63   /// @brief Reads data from the GPU metric set
64   void read_data_from_metric_set();
65
66   /// Sampling period in nanoseconds requested by the datasource
67   uint64_t sampling_period_ns = 0;
68
69   /// Last upper 32bits of the GPU timestamp in the parsed reports
70   uint64_t gpu_timestamp_udw = 0;
71
72   /// Keep track of the timestamp of the last sample generated (upper & lower
73   /// 32bits)
74   uint64_t last_gpu_timestamp = 0;
75
76   /// Data buffer used to store data read from the metric set
77   std::vector<uint8_t> metric_buffer = std::vector<uint8_t>(1024, 0);
78   /// Number of bytes read so far still un-parsed.
79   /// Reset once bytes from the metric buffer are parsed to perf records
80   size_t total_bytes_read = 0;
81
82   /// List of OA perf records read so far
83   std::vector<PerfRecord> records;
84
85   std::unique_ptr<IntelPerf> perf;
86
87   // Gpu clock ID used to correlate GPU/CPU timestamps
88   uint32_t clock_id = 0;
89
90   // Selected query
91   intel_perf_query_info *selected_query = nullptr;
92};
93
94} // namespace pps
95