Debian Codegraph @

example/amd_smi_nodrm_example.cc

raw
1 /*
2 * Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #include <pwd.h>
24 #include <cinttypes>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include <cassert>
29 #include <cstdint>
30 #include <cstring>
31 #include <iostream>
32 #include <vector>
33
34 #include "amd_smi/amdsmi.h"
35
36 #define CHK_AMDSMI_RET(RET) \
37 { \
38 if (RET != AMDSMI_STATUS_SUCCESS) { \
39 const char *err_str; \
40 amdsmi_status_code_to_string(RET, &err_str); \
41 std::cout << "AMDSMI call returned " << RET << " at line " \
42 << __LINE__ << ": " << err_str << std::endl; \
43 if (RET != AMDSMI_STATUS_NOT_SUPPORTED && RET != AMDSMI_STATUS_INVAL) { \
44 return RET; \
45 } \
46 } \
47 }
48
49 int main() {
50 amdsmi_status_t ret;
51
52 // Init amdsmi for sockets and devices.
53 // Here we are only interested in AMD_GPUS.
54 ret = amdsmi_init(AMDSMI_INIT_AMD_GPUS);
55 CHK_AMDSMI_RET(ret)
56
57 // Get all sockets
58 uint32_t socket_count = 0;
59
60 // Get the socket count available for the system.
61 ret = amdsmi_get_socket_handles(&socket_count, nullptr);
62 CHK_AMDSMI_RET(ret)
63
64 // Allocate the memory for the sockets
65 std::vector<amdsmi_socket_handle> sockets(socket_count);
66 // Get the sockets of the system
67 ret = amdsmi_get_socket_handles(&socket_count, &sockets[0]);
68 CHK_AMDSMI_RET(ret)
69
70 std::cout << "Total Socket: " << socket_count << std::endl;
71
72 // For each socket, get identifier and devices
73 for (uint32_t i = 0; i < socket_count; i++) {
74 // Get Socket info
75 char socket_info[128];
76 ret = amdsmi_get_socket_info(sockets[i], 128, socket_info);
77 CHK_AMDSMI_RET(ret)
78 std::cout << "Socket " << socket_info << std::endl;
79
80 // Get the device count available for the socket.
81 uint32_t device_count = 0;
82 ret = amdsmi_get_processor_handles(sockets[i], &device_count, nullptr);
83 CHK_AMDSMI_RET(ret)
84
85 // Allocate the memory for the device handlers on the socket
86 std::vector<amdsmi_processor_handle> processor_handles(device_count);
87 // Get all devices of the socket
88 ret = amdsmi_get_processor_handles(sockets[i],
89 &device_count, &processor_handles[0]);
90 CHK_AMDSMI_RET(ret)
91
92 // For each device of the socket, get name and temperature.
93 for (uint32_t j = 0; j < device_count; j++) {
94 // Get device type. Since the amdsmi is initialized with
95 // AMD_SMI_INIT_AMD_GPUS, the processor_type must be AMDSMI_PROCESSOR_TYPE_AMD_GPU.
96 processor_type_t processor_type = {};
97 ret = amdsmi_get_processor_type(processor_handles[j], &processor_type);
98 CHK_AMDSMI_RET(ret)
99 if (processor_type != AMDSMI_PROCESSOR_TYPE_AMD_GPU) {
100 std::cout << "Expect AMDSMI_PROCESSOR_TYPE_AMD_GPU device type!\n";
101 return AMDSMI_STATUS_NOT_SUPPORTED;
102 }
103
104 amdsmi_ras_feature_t ras_feature;
105 ret = amdsmi_get_gpu_ras_feature_info(
106 processor_handles[j] ,&ras_feature);
107 CHK_AMDSMI_RET(ret)
108 if (ret != AMDSMI_STATUS_NOT_SUPPORTED) {
109 printf("\tras_feature: version: %x, schema: %x\n",
110 ras_feature.ras_eeprom_version, ras_feature.ecc_correction_schema_flag);
111 }
112
113
114 amdsmi_bdf_t bdf = {};
115 ret = amdsmi_get_gpu_device_bdf(processor_handles[j], &bdf);
116 CHK_AMDSMI_RET(ret)
117 printf(" Output of amdsmi_get_gpu_device_bdf:\n");
118 printf("\tDevice[%d] BDF %04" PRIx64 ":%02" PRIx32 ":%02" PRIx32 ".%" PRIu32 "\n\n", i,
119 static_cast<uint64_t>(bdf.domain_number),
120 static_cast<uint32_t>(bdf.bus_number),
121 static_cast<uint32_t>(bdf.device_number),
122 static_cast<uint32_t>(bdf.function_number));
123
124 amdsmi_asic_info_t asic_info = {};
125 ret = amdsmi_get_gpu_asic_info(processor_handles[j], &asic_info);
126 CHK_AMDSMI_RET(ret)
127 printf(" Output of amdsmi_get_gpu_asic_info:\n");
128 printf("\tMarket Name: %s\n", asic_info.market_name);
129 printf("\tDeviceID: 0x%lx\n", asic_info.device_id);
130 printf("\tVendorID: 0x%x\n", asic_info.vendor_id);
131 printf("\tRevisionID: 0x%x\n", asic_info.rev_id);
132 printf("\tSubSystemID: 0x%x\n", asic_info.subsystem_id);
133 printf("\tAsic serial: 0x%s\n", asic_info.asic_serial);
134 printf("\tOAM id: 0x%x\n", asic_info.oam_id);
135 printf("\tNum of Computes: %d\n\n", asic_info.num_of_compute_units);
136
137 // Get VBIOS info
138 amdsmi_vbios_info_t vbios_info = {};
139 ret = amdsmi_get_gpu_vbios_info(processor_handles[j], &vbios_info);
140 CHK_AMDSMI_RET(ret)
141 printf(" Output of amdsmi_get_gpu_vbios_info:\n");
142 printf("\tVBIOS/IFWI Name: %s\n", vbios_info.name);
143 printf("\tVBIOS/IFWI Build Date: %s\n", vbios_info.build_date);
144 printf("\tVBIOS/IFWI Part Number: %s\n", vbios_info.part_number);
145 printf("\tVBIOS/IFWI Version String: %s\n\n", vbios_info.version);
146 printf("\tVBIOS/IFWI Boot Firmware: %s\n\n", vbios_info.boot_firmware);
147
148 // Get engine usage info
149 amdsmi_engine_usage_t engine_usage = {};
150 ret = amdsmi_get_gpu_activity(processor_handles[j], &engine_usage);
151 CHK_AMDSMI_RET(ret)
152 printf(" Output of amdsmi_get_gpu_activity:\n");
153 printf("\tAverage GFX Activity: %d\n",
154 engine_usage.gfx_activity);
155 printf("\tAverage MM Activity: %d\n",
156 engine_usage.mm_activity);
157 printf("\tAverage UMC Activity: %d\n\n",
158 engine_usage.umc_activity);
159
160 // Get firmware info
161 amdsmi_fw_info_t fw_information = {};
162 ret = amdsmi_get_fw_info(processor_handles[j], &fw_information);
163 CHK_AMDSMI_RET(ret)
164 printf(" Output of amdsmi_get_fw_info:\n");
165 printf("\tFirmware version: %d\n", fw_information.num_fw_info);
166 printf("\tSMU: %ld\n",
167 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_SMU]
168 .fw_version);
169 printf("\tPM: %ld\n",
170 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_PM]
171 .fw_version);
172 printf("\tVCN: %ld\n",
173 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_VCN]
174 .fw_version);
175 printf("\tCP_ME: %ld\n",
176 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_CP_ME]
177 .fw_version);
178 printf("\tCP_PFP: %ld\n",
179 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_CP_PFP]
180 .fw_version);
181 printf("\tCP_CE: %ld\n",
182 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_CP_CE]
183 .fw_version);
184 printf("\tRLC: %ld\n",
185 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_RLC]
186 .fw_version);
187 printf("\tCP_MEC1: %ld\n",
188 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_CP_MEC1]
189 .fw_version);
190 printf("\tCP_MEC2: %ld\n",
191 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_CP_MEC2]
192 .fw_version);
193 printf("\tSDMA0: %ld\n",
194 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_SDMA0]
195 .fw_version);
196 printf("\tMC: %ld\n",
197 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_MC]
198 .fw_version);
199 printf("\tRLC RESTORE LIST CNTL: %ld\n",
200 fw_information
201 .fw_info_list
202 [amdsmi_fw_block_t::AMDSMI_FW_ID_RLC_RESTORE_LIST_CNTL]
203 .fw_version);
204 printf("\tRLC RESTORE LIST GPM MEM: %ld\n",
205 fw_information
206 .fw_info_list
207 [amdsmi_fw_block_t::AMDSMI_FW_ID_RLC_RESTORE_LIST_GPM_MEM]
208 .fw_version);
209 printf("\tRLC RESTORE LIST SRM MEM: %ld\n",
210 fw_information
211 .fw_info_list
212 [amdsmi_fw_block_t::AMDSMI_FW_ID_RLC_RESTORE_LIST_SRM_MEM]
213 .fw_version);
214 printf(
215 "\tPSP SOSDRV: %ld\n\n",
216 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_PSP_SOSDRV]
217 .fw_version);
218 printf(
219 "\tPLDM BUNDLE: %ld\n\n",
220 fw_information.fw_info_list[amdsmi_fw_block_t::AMDSMI_FW_ID_PLDM_BUNDLE]
221 .fw_version);
222
223 // Get temperature measurements
224 int64_t temp_measurements[AMDSMI_TEMPERATURE_TYPE__MAX + 1];
225 amdsmi_temperature_type_t temp_types[4] = {
226 AMDSMI_TEMPERATURE_TYPE_EDGE, AMDSMI_TEMPERATURE_TYPE_HOTSPOT,
227 AMDSMI_TEMPERATURE_TYPE_VRAM, AMDSMI_TEMPERATURE_TYPE_PLX};
228 for (const auto &temp_type : temp_types) {
229 ret = amdsmi_get_temp_metric(
230 processor_handles[j], temp_type,
231 AMDSMI_TEMP_CURRENT,
232 &temp_measurements[(int)(temp_type)]);
233 CHK_AMDSMI_RET(ret)
234 }
235 printf(" Output of amdsmi_get_temp_metric:\n");
236 printf("\tGPU Edge temp measurement: %ld\n",
237 temp_measurements[AMDSMI_TEMPERATURE_TYPE_EDGE]);
238 printf("\tGPU Hotspot temp measurement: %ld\n",
239 temp_measurements[AMDSMI_TEMPERATURE_TYPE_HOTSPOT]);
240 printf("\tGPU VRAM temp measurement: %ld\n",
241 temp_measurements[AMDSMI_TEMPERATURE_TYPE_VRAM]);
242 printf("\tGPU PLX temp measurement: %ld\n\n",
243 temp_measurements[AMDSMI_TEMPERATURE_TYPE_PLX]);
244
245 // Get bad pages
246 char bad_page_status_names[3][15] = {"RESERVED", "PENDING",
247 "UNRESERVABLE"};
248 uint32_t num_pages = 0;
249 std::vector<amdsmi_retired_page_record_t> bad_page_info(num_pages);
250 ret = amdsmi_get_gpu_bad_page_info(processor_handles[j], &num_pages,
251 bad_page_info.data());
252 std::cout << "num_pages = " << num_pages << "\n";
253 CHK_AMDSMI_RET(ret)
254 printf(" Output of amdsmi_get_gpu_bad_page_info:\n");
255 if (!num_pages) {
256 printf("\tNo bad pages found.\n");
257 } else {
258 std::vector<amdsmi_retired_page_record_t> bad_page_info(num_pages);
259 ret = amdsmi_get_gpu_bad_page_info(processor_handles[j], &num_pages,
260 bad_page_info.data());
261 CHK_AMDSMI_RET(ret)
262 for (uint32_t page_it = 0; page_it < num_pages; page_it += 1) {
263 printf(" Page[%d]\n", page_it);
264 printf("\tAddress: %lu\n",
265 bad_page_info[page_it].page_address);
266 printf("\tSize: %lu\n", bad_page_info[page_it].page_size);
267 printf(
268 "\tStatus: %s\n",
269 bad_page_status_names[bad_page_info[page_it].status]);
270 }
271 }
272 printf("\n");
273
274 // Get ECC error counts
275 amdsmi_error_count_t err_cnt_info = {};
276 ret = amdsmi_get_gpu_total_ecc_count(processor_handles[j], &err_cnt_info);
277 CHK_AMDSMI_RET(ret)
278 printf(" Output of amdsmi_get_gpu_total_ecc_count:\n");
279 printf("\tCorrectable errors: %lu\n", err_cnt_info.correctable_count);
280 printf("\tUncorrectable errors: %lu\n\n",
281 err_cnt_info.uncorrectable_count);
282
283 // Get device name
284 amdsmi_board_info_t board_info = {};
285 ret = amdsmi_get_gpu_board_info(processor_handles[j], &board_info);
286 CHK_AMDSMI_RET(ret)
287 printf(" Output of amdsmi_get_gpu_board_info:\n");
288 std::cout << "\tdevice [" << j
289 << "]\n\t\tProduct name: " << board_info.product_name
290 << "\n"
291 << "\t\tModel Number: " << board_info.model_number
292 << "\n"
293 << "\t\tBoard Serial: " << board_info.product_serial
294 << "\n"
295 << "\t\tManufacturer Name: " << board_info.manufacturer_name
296 << "\n\n";
297
298 // Get temperature
299 int64_t val_i64 = 0;
300 ret = amdsmi_get_temp_metric(processor_handles[j], AMDSMI_TEMPERATURE_TYPE_EDGE,
301 AMDSMI_TEMP_CURRENT, &val_i64);
302 CHK_AMDSMI_RET(ret)
303 printf(" Output of amdsmi_get_temp_metric:\n");
304 std::cout << "\t\tTemperature: " << val_i64 << "C"
305 << "\n\n";
306
307 // Get frame buffer
308 amdsmi_vram_usage_t vram_usage = {};
309 ret = amdsmi_get_gpu_vram_usage(processor_handles[j], &vram_usage);
310 CHK_AMDSMI_RET(ret)
311 printf(" Output of amdsmi_get_gpu_vram_usage:\n");
312 std::cout << "\t\tFrame buffer usage (MB): " << vram_usage.vram_used
313 << "/" << vram_usage.vram_total << "\n\n";
314
315 amdsmi_power_cap_info_t cap_info = {};
316 ret = amdsmi_get_power_cap_info(processor_handles[j], 0, &cap_info);
317 CHK_AMDSMI_RET(ret)
318 printf(" Output of amdsmi_get_power_cap_info:\n");
319 std::cout << "\t\t Power Cap: " << cap_info.power_cap / 1000000
320 << "W\n\n";
321
322 amdsmi_dpm_policy_t policy;
323 ret = amdsmi_get_soc_pstate(processor_handles[j], &policy);
324 if (ret != AMDSMI_STATUS_NOT_SUPPORTED) {
325 CHK_AMDSMI_RET(ret)
326 std::cout << "\t amdsmi_get_soc_pstate total:" << policy.num_supported
327 <<" current:" << policy.current << "\n";
328 for (uint32_t x=0; x < policy.num_supported; x++) {
329 std::cout << x <<": (" << policy.policies[x].policy_id
330 <<"," << policy.policies[x].policy_description << ")\n";
331 }
332 }
333
334 // Get nearest GPUs
335 const char *topology_link_type_str[] = {
336 "AMDSMI_LINK_TYPE_INTERNAL",
337 "AMDSMI_LINK_TYPE_PCIE",
338 "AMDSMI_LINK_TYPE_XGMI",
339 "AMDSMI_LINK_TYPE_NOT_APPLICABLE",
340 "AMDSMI_LINK_TYPE_UNKNOWN",
341 };
342 printf("\tOutput of amdsmi_get_link_topology_nearest:\n");
343 for (uint32_t topo_link_type = AMDSMI_LINK_TYPE_INTERNAL; topo_link_type <= AMDSMI_LINK_TYPE_UNKNOWN; topo_link_type++) {
344 auto topology_nearest_info = amdsmi_topology_nearest_t();
345 ret = amdsmi_get_link_topology_nearest(processor_handles[j],
346 static_cast<amdsmi_link_type_t>(topo_link_type),
347 nullptr);
348 CHK_AMDSMI_RET(ret);
349 ret = amdsmi_get_link_topology_nearest(processor_handles[j],
350 static_cast<amdsmi_link_type_t>(topo_link_type),
351 &topology_nearest_info);
352 CHK_AMDSMI_RET(ret);
353 printf("\tNearest GPUs found at %s\n", topology_link_type_str[topo_link_type]);
354 for (uint32_t k = 0; k < topology_nearest_info.count; k++) {
355 amdsmi_bdf_t bdf = {};
356 ret = amdsmi_get_gpu_device_bdf(topology_nearest_info.processor_list[k], &bdf);
357 CHK_AMDSMI_RET(ret)
358 printf("\tGPU BDF %04" PRIx64 ":%02" PRIx32 ":%02" PRIx32 ".%" PRIu32 "\n",
359 static_cast<uint64_t>(bdf.domain_number),
360 static_cast<uint32_t>(bdf.bus_number),
361 static_cast<uint32_t>(bdf.device_number),
362 static_cast<uint32_t>(bdf.function_number));
363 }
364 }
365 }
366 }
367
368 // Clean up resources allocated at amdsmi_init. It will invalidate sockets
369 // and devices pointers
370 ret = amdsmi_shut_down();
371 CHK_AMDSMI_RET(ret)
372
373 return 0;
374 }