libdrm_amdgpu_sys/amdgpu/gpu_metrics/metrics_table/mod.rs
1/// ref: drivers/gpu/drm/amd/include/kgd_pp_interface.h
2
3mod v1;
4mod v1_4_5;
5mod v2_v3;
6
7use std::fs::File;
8use std::io::{self, Read};
9use std::path::PathBuf;
10pub use crate::bindings::{
11 metrics_table_header,
12 gpu_metrics_v1_0,
13 gpu_metrics_v1_1,
14 gpu_metrics_v1_2,
15 gpu_metrics_v1_3,
16 gpu_metrics_v1_4,
17 gpu_metrics_v1_5,
18 gpu_metrics_v2_0,
19 gpu_metrics_v2_1,
20 gpu_metrics_v2_2,
21 gpu_metrics_v2_3,
22 gpu_metrics_v2_4,
23 gpu_metrics_v3_0,
24 NUM_HBM_INSTANCES,
25 NUM_VCN,
26 NUM_JPEG_ENG,
27 NUM_XGMI_LINKS,
28 MAX_CLKS,
29 MAX_GFX_CLKS,
30};
31use crate::AMDGPU::ThrottleStatus;
32
33impl metrics_table_header {
34 pub(crate) fn from_bytes(buf: &[u8]) -> Self {
35 let [structer_size_0, structer_size_1, format_revision, content_revision] = {
36 if let Some(tmp) = buf.get(0..4).and_then(|v| v.try_into().ok()) {
37 tmp
38 } else {
39 return Self { structure_size: 0, format_revision: 0, content_revision: 0 };
40 }
41 };
42
43 Self {
44 structure_size: u16::from_le_bytes([structer_size_0, structer_size_1]),
45 format_revision,
46 content_revision,
47 }
48 }
49
50 pub fn from_buf(buf: [u8; 4]) -> Self {
51 Self::from_bytes(&buf)
52 }
53
54 pub fn from_sysfs_path<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
55 let mut f = File::open(path.into())?;
56 let mut buf = [0u8; 4];
57
58 f.read_exact(&mut buf)?;
59
60 Ok(Self::from_buf(buf))
61 }
62}
63
64/// The actual unsupported value will be 0xFFFF.
65pub trait MetricsInfo {
66 fn get_header(&self) -> Option<metrics_table_header>;
67 /// Celsius
68 fn get_temperature_edge(&self) -> Option<u16>;
69 /// Celsius
70 fn get_temperature_hotspot(&self) -> Option<u16>;
71 /// Celsius
72 fn get_temperature_mem(&self) -> Option<u16>;
73 /// Celsius
74 fn get_temperature_vrgfx(&self) -> Option<u16>;
75 /// Celsius
76 fn get_temperature_vrsoc(&self) -> Option<u16>;
77 /// Celsius
78 fn get_temperature_vrmem(&self) -> Option<u16>;
79 /// millidegrees Celsius
80 fn get_temperature_gfx(&self) -> Option<u16>;
81 /// millidegrees Celsius
82 fn get_temperature_soc(&self) -> Option<u16>;
83 /// millidegrees Celsius,
84 /// For VanGogh APU, only the first half is a valid value.
85 /// ref: `drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c`
86 fn get_temperature_core(&self) -> Option<Vec<u16>>;
87 /// millidegrees Celsius,
88 /// For VanGogh APU, only the first half is a valid value.
89 /// ref: `drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c`
90 fn get_temperature_l3(&self) -> Option<Vec<u16>>;
91 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
92 fn get_temperature_skin(&self) -> Option<u16>;
93
94 fn get_average_gfx_activity(&self) -> Option<u16>;
95 fn get_average_umc_activity(&self) -> Option<u16>;
96 fn get_average_mm_activity(&self) -> Option<u16>;
97 /// time filtered IPU per-column busy % [0-100],
98 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
99 fn get_average_ipu_activity(&self) -> Option<Vec<u16>>;
100 /// time filtered per-core C0 residency % [0-100],
101 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
102 fn get_average_core_c0_activity(&self) -> Option<Vec<u16>>;
103
104 /// time filtered DRAM read bandwidth [MB/sec],
105 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
106 fn get_average_dram_reads(&self) -> Option<u16>;
107 /// time filtered DRAM write bandwidth [MB/sec],
108 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
109 fn get_average_dram_writes(&self) -> Option<u16>;
110 /// time filtered IPU read bandwidth [MB/sec],
111 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
112 fn get_average_ipu_reads(&self) -> Option<u16>;
113 /// time filtered IPU write bandwidth [MB/sec],
114 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
115 fn get_average_ipu_writes(&self) -> Option<u16>;
116
117 fn get_system_clock_counter(&self) -> Option<u64>;
118 /// Watts
119 fn get_average_socket_power(&self) -> Option<u32>;
120 /// Watts
121 fn get_average_cpu_power(&self) -> Option<u16>;
122 /// Watts
123 fn get_average_soc_power(&self) -> Option<u16>;
124 /// Watts
125 fn get_average_gfx_power(&self) -> Option<u16>;
126 /// Watts
127 fn get_average_gfx_power_u32(&self) -> Option<u32>;
128 /// Watts,
129 /// For VanGogh APU, only the first half is a valid value.
130 /// ref: `drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c`
131 fn get_average_core_power(&self) -> Option<Vec<u16>>;
132
133 /// time filtered IPU power \[mW\],
134 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
135 fn get_average_ipu_power(&self) -> Option<u16>;
136 /// time filtered APU power \[mW\],
137 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
138 fn get_average_apu_power(&self) -> Option<u32>;
139 /// time filtered dGPU power \[mW\],
140 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
141 fn get_average_dgpu_power(&self) -> Option<u32>;
142 /// time filtered sum of core power across all cores in the socket \[mW\],
143 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
144 fn get_average_all_core_power(&self) -> Option<u32>;
145 /// time filtered total system power \[mW\],
146 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
147 fn get_average_sys_power(&self) -> Option<u16>;
148 /// maximum IRM defined STAPM power limit \[mW\],
149 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
150 fn get_stapm_power_limit(&self) -> Option<u16>;
151 /// time filtered STAPM power limit \[mW\],
152 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
153 fn get_current_stapm_power_limit(&self) -> Option<u16>;
154
155 /// MHz
156 fn get_average_gfxclk_frequency(&self) -> Option<u16>;
157 /// MHz
158 fn get_average_socclk_frequency(&self) -> Option<u16>;
159 /// UMC Clock, MHz
160 fn get_average_uclk_frequency(&self) -> Option<u16>;
161 /// Fabric Clock, MHz
162 fn get_average_fclk_frequency(&self) -> Option<u16>;
163 /// Video Clock, MHz
164 fn get_average_vclk_frequency(&self) -> Option<u16>;
165 /// Display Clock, MHz
166 fn get_average_dclk_frequency(&self) -> Option<u16>;
167 /// Video Clock, MHz
168 fn get_average_vclk1_frequency(&self) -> Option<u16>;
169 /// Display Clock, MHz
170 fn get_average_dclk1_frequency(&self) -> Option<u16>;
171
172 /// time filtered clocks \[MHz\],
173 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
174 fn get_average_vpeclk_frequency(&self) -> Option<u16>;
175 /// time filtered clocks \[MHz\],
176 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
177 fn get_average_ipuclk_frequency(&self) -> Option<u16>;
178 /// time filtered clocks \[MHz\],
179 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
180 fn get_average_mpipu_frequency(&self) -> Option<u16>;
181
182 /// MHz
183 fn get_current_gfxclk(&self) -> Option<u16>;
184 /// MHz
185 fn get_current_socclk(&self) -> Option<u16>;
186 /// UMC Clock, MHz
187 fn get_current_uclk(&self) -> Option<u16>;
188 /// Fablic Clock, MHz
189 fn get_current_fclk(&self) -> Option<u16>;
190 /// Video Clock, MHz
191 fn get_current_vclk(&self) -> Option<u16>;
192 /// Display Clock, MHz
193 fn get_current_dclk(&self) -> Option<u16>;
194 /// Video Clock, MHz
195 fn get_current_vclk1(&self) -> Option<u16>;
196 /// Display Clock, MHz
197 fn get_current_dclk1(&self) -> Option<u16>;
198 /// MHz,
199 /// For VanGogh APU, only the first half is a valid value.
200 /// ref: `drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c`
201 fn get_current_coreclk(&self) -> Option<Vec<u16>>;
202 /// MHz,
203 /// For VanGogh APU, only the first half is a valid value.
204 /// ref: `drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c`
205 fn get_current_l3clk(&self) -> Option<Vec<u16>>;
206
207 /// CCLK frequency limit enforced on classic cores \[MHz\],
208 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
209 fn get_current_core_maxfreq(&self) -> Option<u16>;
210 /// GFXCLK frequency limit enforced on GFX \[MHz\],
211 /// SMU v14.0.0 with [gpu_metrics_v3_0] supports it.
212 fn get_current_gfx_maxfreq(&self) -> Option<u16>;
213
214 fn get_throttle_status(&self) -> Option<u32>;
215 /// This method returns `None` if `indep_throttle_status` is `u64::MAX`,
216 /// and implements a workaround for RDNA 3 dGPUs and RDNA 4 dGPUs.
217 /// ref: https://gitlab.freedesktop.org/drm/amd/-/issues/3251
218 fn get_indep_throttle_status(&self) -> Option<u64>;
219 fn get_current_fan_speed(&self) -> Option<u16>;
220 fn get_fan_pwm(&self) -> Option<u16>;
221
222 /// Clock Lock Status. Each bit corresponds to clock instance
223 fn get_pcie_link_width(&self) -> Option<u16>;
224 /// Clock Lock Status. Each bit corresponds to clock instance
225 fn get_pcie_link_speed(&self) -> Option<u16>;
226 /// PCIE accumulated bandwidth (GB/sec),
227 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
228 fn get_pcie_bandwidth_acc(&self) -> Option<u64>;
229 /// PCIE instantaneous bandwidth (GB/sec)
230 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
231 fn get_pcie_bandwidth_inst(&self) -> Option<u64>;
232
233 /// XGMI bus width and bitrate (in Gbps)
234 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
235 fn get_xgmi_link_width(&self) -> Option<u16>;
236 /// XGMI bus width and bitrate (in Gbps)
237 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
238 fn get_xgmi_link_speed(&self) -> Option<u16>;
239 /// XGMI accumulated data transfer size(KiloBytes),
240 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
241 fn get_xgmi_read_data_acc(&self) -> Option<[u64; NUM_XGMI_LINKS as usize]>;
242 /// XGMI accumulated data transfer size(KiloBytes),
243 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
244 fn get_xgmi_write_data_acc(&self) -> Option<[u64; NUM_XGMI_LINKS as usize]>;
245
246 fn get_gfx_activity_acc(&self) -> Option<u32>;
247 fn get_mem_activity_acc(&self) -> Option<u32>;
248
249 /// Only Aldebaran (MI200) supports it.
250 fn get_temperature_hbm(&self) -> Option<[u16; NUM_HBM_INSTANCES as usize]>;
251
252 /// mV
253 fn get_voltage_soc(&self) -> Option<u16>;
254 /// mV
255 fn get_voltage_gfx(&self) -> Option<u16>;
256 /// mV
257 fn get_voltage_mem(&self) -> Option<u16>;
258
259 /// Average Temperature (unit: centi-Celsius)
260 fn get_average_temperature_gfx(&self) -> Option<u16>;
261 /// Average Temperature (unit: centi-Celsius)
262 fn get_average_temperature_soc(&self) -> Option<u16>;
263 /// Average Temperature (unit: centi-Celsius)
264 fn get_average_temperature_core(&self) -> Option<Vec<u16>>;
265 /// Average Temperature (unit: centi-Celsius)
266 fn get_average_temperature_l3(&self) -> Option<Vec<u16>>;
267
268 /// Power/Voltage (unit: mV)
269 /// only Vangogh with [gpu_metrics_v2_4] supports it.
270 fn get_average_cpu_voltage(&self) -> Option<u16>;
271 /// Power/Voltage (unit: mV)
272 /// only Vangogh with [gpu_metrics_v2_4] supports it.
273 fn get_average_soc_voltage(&self) -> Option<u16>;
274 /// Power/Voltage (unit: mV)
275 /// only Vangogh with [gpu_metrics_v2_4] supports it.
276 fn get_average_gfx_voltage(&self) -> Option<u16>;
277
278 /// Power/Current (unit: mA),
279 /// only Vangogh with [gpu_metrics_v2_4] supports it.
280 fn get_average_cpu_current(&self) -> Option<u16>;
281 /// Power/Voltage (unit: mV)
282 /// only Vangogh with [gpu_metrics_v2_4] supports it.
283 fn get_average_soc_current(&self) -> Option<u16>;
284 /// Power/Voltage (unit: mV)
285 /// only Vangogh with [gpu_metrics_v2_4] supports it.
286 fn get_average_gfx_current(&self) -> Option<u16>;
287
288 /// Clock Lock Status. Each bit corresponds to clock instance,
289 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
290 fn get_gfxclk_lock_status(&self) -> Option<u32>;
291 /// Only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
292 fn get_current_socket_power(&self) -> Option<u16>;
293
294 /// All instances (XCC) current gfx clock,
295 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
296 fn get_all_instances_current_gfxclk(&self) -> Option<[u16; MAX_GFX_CLKS as usize]>;
297 /// All instances current soc clock,
298 /// only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
299 fn get_all_instances_current_socclk(&self) -> Option<[u16; MAX_CLKS as usize]>;
300 /// Only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
301 fn get_all_instances_current_vclk0(&self) -> Option<[u16; MAX_CLKS as usize]>;
302 /// Only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
303 fn get_all_instances_current_dclk0(&self) -> Option<[u16; MAX_CLKS as usize]>;
304
305 /// Utilization (%), only MI300 with [gpu_metrics_v1_4] or [gpu_metrics_v1_5] supports it.
306 fn get_all_vcn_activity(&self) -> Option<[u16; NUM_VCN as usize]>;
307 /// Utilization (%), only MI300 with [gpu_metrics_v1_5] supports it.
308 fn get_all_jpeg_activity(&self) -> Option<[u16; NUM_JPEG_ENG as usize]>;
309
310 /// Strix Point, Krackan Point and Strix Halo supports it.
311 fn get_throttle_residency_prochot(&self) -> Option<u32>;
312 /// Strix Point, Krackan Point and Strix Halo supports it.
313 fn get_throttle_residency_spl(&self) -> Option<u32>;
314 /// Strix Point, Krackan Point and Strix Halo supports it.
315 fn get_throttle_residency_fppt(&self) -> Option<u32>;
316 /// Strix Point, Krackan Point and Strix Halo supports it.
317 fn get_throttle_residency_sppt(&self) -> Option<u32>;
318 /// Strix Point, Krackan Point and Strix Halo supports it.
319 fn get_throttle_residency_thm_core(&self) -> Option<u32>;
320 /// Strix Point, Krackan Point and Strix Halo supports it.
321 fn get_throttle_residency_thm_gfx(&self) -> Option<u32>;
322 /// Strix Point, Krackan Point and Strix Halo supports it.
323 fn get_throttle_residency_thm_soc(&self) -> Option<u32>;
324
325 fn get_throttle_status_info(&self) -> Option<ThrottleStatus> {
326 self.get_indep_throttle_status().map(ThrottleStatus::new)
327 }
328
329 /// This method returns `indep_throttle_status` without any checks or workarounds.
330 fn get_indep_throttle_status_without_check(&self) -> Option<u64>;
331}