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