1 /* 2 * Copyright © 2021 Google, Inc. 3 * 4 * SPDX-License-Identifier: MIT 5 */ 6 7 #pragma once 8 9 #include "pps/pps_driver.h" 10 11 #include "common/freedreno_dev_info.h" 12 #include "drm/freedreno_drmif.h" 13 #include "drm/freedreno_ringbuffer.h" 14 #include "perfcntrs/freedreno_dt.h" 15 #include "perfcntrs/freedreno_perfcntr.h" 16 17 namespace pps 18 { 19 20 class FreedrenoDriver : public Driver 21 { 22 public: 23 bool is_dump_perfcnt_preemptible() const override; 24 uint64_t get_min_sampling_period_ns() override; 25 bool init_perfcnt() override; 26 void enable_counter(uint32_t counter_id) override; 27 void enable_all_counters() override; 28 void enable_perfcnt(uint64_t sampling_period_ns) override; 29 void disable_perfcnt() override; 30 bool dump_perfcnt() override; 31 uint64_t next() override; 32 uint32_t gpu_clock_id() const override; 33 uint64_t gpu_timestamp() const override; 34 bool cpu_gpu_timestamp(uint64_t &cpu_timestamp, 35 uint64_t &gpu_timestamp) const override; 36 37 private: 38 struct fd_device *dev; 39 struct fd_pipe *pipe; 40 const struct fd_dev_id *dev_id; 41 uint32_t max_freq; 42 uint32_t next_counter_id; 43 uint32_t next_countable_id; 44 uint64_t last_dump_ts = 0; 45 uint64_t last_capture_ts; 46 47 bool has_suspend_count; 48 uint32_t suspend_count; 49 50 const struct fd_dev_info *info; 51 52 /** 53 * The memory mapped i/o space for counter readback: 54 */ 55 void *io; 56 57 const struct fd_perfcntr_group *perfcntrs; 58 unsigned num_perfcntrs; 59 60 /** 61 * The number of counters assigned per perfcntr group, the index 62 * into this matches the index into perfcntrs 63 */ 64 std::vector<unsigned> assigned_counters; 65 66 /* 67 * Values that can be used by derived counters evaluation 68 */ 69 float time; /* time since last sample in fraction of second */ 70 // uint32_t cycles; /* the number of clock cycles since last sample */ 71 72 void setup_a6xx_counters(); 73 74 void configure_counters(bool reset, bool wait); 75 void collect_countables(); 76 77 /** 78 * Split out countable mutable state from the class so that copy- 79 * constructor does something sane when lambda derive function 80 * tries to get the countable value. 81 */ 82 struct CountableState { 83 uint64_t last_value, value; 84 const struct fd_perfcntr_countable *countable; 85 const struct fd_perfcntr_counter *counter; 86 }; 87 88 std::vector<struct CountableState> state; 89 90 /** 91 * Performance counters on adreno consist of sets of counters in various 92 * blocks of the GPU, where each counter can be can be muxed to collect 93 * one of a set of countables. 94 * 95 * But the countables tend to be too low level to be directly useful to 96 * visualize. Instead various combinations of countables are combined 97 * with various formulas to derive the high level "Counter" value exposed 98 * via gfx-pps. 99 * 100 * This class serves to decouple the logic of those formulas from the 101 * details of collecting countable values. 102 */ 103 class Countable { 104 public: 105 Countable(FreedrenoDriver *d, std::string name); 106 int64_t()107 operator int64_t() const { return get_value(); }; 108 109 void configure(struct fd_ringbuffer *ring, bool reset) const; 110 void collect() const; 111 void resolve() const; 112 113 private: 114 115 uint64_t get_value() const; 116 117 uint32_t id; 118 FreedrenoDriver *d; 119 std::string name; 120 }; 121 122 Countable countable(std::string name); 123 124 std::vector<Countable> countables; 125 126 /** 127 * A derived "Counter" (from pps's perspective) 128 */ 129 class DerivedCounter : public Counter { 130 public: 131 DerivedCounter(FreedrenoDriver *d, std::string name, Counter::Units units, 132 std::function<int64_t()> derive); 133 }; 134 135 DerivedCounter counter(std::string name, Counter::Units units, 136 std::function<int64_t()> derive); 137 }; 138 139 } // namespace pps 140