SWDEV-353742 - Port smilib function to amdsmi
Change-Id: I99df249755a5c665a8dd1777fa82d046e139bd77 Signed-off-by: Dalibor Stanisavljevic <Dalibor.Stanisavljevic@amd.com>
このコミットが含まれているのは:
+514
-76
@@ -50,12 +50,14 @@
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <xf86drm.h>
|
||||
#include "amd_smi/amd_smi.h"
|
||||
#include "amd_smi/impl/fdinfo.h"
|
||||
#include "amd_smi/impl/amd_smi_common.h"
|
||||
#include "amd_smi/impl/amd_smi_system.h"
|
||||
#include "amd_smi/impl/amd_smi_socket.h"
|
||||
@@ -63,6 +65,7 @@
|
||||
#include "rocm_smi/rocm_smi.h"
|
||||
#include "rocm_smi/rocm_smi_common.h"
|
||||
#include "amd_smi/impl/amdgpu_drm.h"
|
||||
#include "amd_smi/impl/amd_smi_utils.h"
|
||||
|
||||
// TODO(bliu): One to one map to all status code
|
||||
static amdsmi_status_t rsmi_to_amdsmi_status(rsmi_status_t status) {
|
||||
@@ -178,7 +181,9 @@ amdsmi_status_t amdsmi_get_device_handles(amdsmi_socket_handle socket_handle,
|
||||
.handle_to_socket(socket_handle, &socket);
|
||||
if (r != AMDSMI_STATUS_SUCCESS) return r;
|
||||
|
||||
*device_count = static_cast<uint32_t>(socket->get_devices().size());
|
||||
r = socket->get_device_count(device_count);
|
||||
if (r != AMDSMI_STATUS_SUCCESS) return r;
|
||||
|
||||
*device_handles = reinterpret_cast<amdsmi_device_handle*>(
|
||||
socket->get_devices().data());
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
@@ -198,54 +203,44 @@ amdsmi_status_t amdsmi_get_device_type(amdsmi_device_handle device_handle ,
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t amdsmi_get_board_info(amdsmi_device_handle device_handle,
|
||||
amdsmi_board_info_t *info) {
|
||||
if (info == NULL) {
|
||||
amdsmi_status_t
|
||||
amdsmi_get_device_bdf(amdsmi_device_handle device_handle, amdsmi_bdf_t *bdf) {
|
||||
|
||||
if (bdf == NULL) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
memset(info, 0, sizeof(amdsmi_board_info_t));
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
|
||||
|
||||
// ignore errors so that if the function is not supported,
|
||||
// it will continue to add other info.
|
||||
auto r = rsmi_wrapper(rsmi_dev_name_get, device_handle,
|
||||
info->product_name, AMDSMI_PRODUCT_NAME_LENGTH);
|
||||
|
||||
r = rsmi_wrapper(rsmi_dev_serial_number_get, device_handle,
|
||||
info->product_serial, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
if (gpu_device->check_if_drm_is_supported()) {
|
||||
*bdf = gpu_device->get_bdf();
|
||||
}
|
||||
else {
|
||||
//TODO
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// TODO(bliu) : add other asic info
|
||||
amdsmi_status amdsmi_get_asic_info(amdsmi_device_handle device_handle,
|
||||
amdsmi_asic_info_t *info) {
|
||||
if (info == nullptr)
|
||||
amdsmi_status_t amdsmi_get_board_info(amdsmi_device_handle device_handle, amdsmi_board_info_t *board_info) {
|
||||
if (board_info == NULL) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
memset(info, 0, sizeof(amdsmi_asic_info_t));
|
||||
}
|
||||
|
||||
auto r = rsmi_wrapper(rsmi_dev_serial_number_get, device_handle,
|
||||
info->asic_serial, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
amdsmi_status_t status;
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
|
||||
|
||||
r = rsmi_wrapper(rsmi_dev_brand_get, device_handle,
|
||||
info->market_name, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
if (gpu_device->check_if_drm_is_supported()) {
|
||||
status = smi_amdgpu_get_board_info(gpu_device, board_info);
|
||||
}
|
||||
else {
|
||||
status = rsmi_wrapper(rsmi_dev_name_get, device_handle, board_info->product_name, AMDSMI_PRODUCT_NAME_LENGTH);
|
||||
status = rsmi_wrapper(rsmi_dev_serial_number_get, device_handle, board_info->product_serial, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
}
|
||||
|
||||
uint16_t vendor_id = 0;
|
||||
r = rsmi_wrapper(rsmi_dev_vendor_id_get, device_handle,
|
||||
&vendor_id);
|
||||
if ( r == AMDSMI_STATUS_SUCCESS)
|
||||
info->vendor_id = static_cast<uint32_t>(vendor_id);
|
||||
|
||||
r = rsmi_wrapper(rsmi_dev_unique_id_get, device_handle,
|
||||
&(info->device_id));
|
||||
|
||||
vendor_id = 0;
|
||||
r = rsmi_wrapper(rsmi_dev_subsystem_vendor_id_get, device_handle,
|
||||
&vendor_id);
|
||||
if ( r == AMDSMI_STATUS_SUCCESS)
|
||||
info->subvendor_id = static_cast<uint32_t>(vendor_id);
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
return status;
|
||||
}
|
||||
|
||||
amdsmi_status_t amdsmi_dev_temp_metric_get(amdsmi_device_handle device_handle,
|
||||
@@ -333,7 +328,7 @@ amdsmi_status_t amdsmi_get_caps_info(amdsmi_device_handle device_handle,
|
||||
sizeof(struct drm_amdgpu_info_device), &device);
|
||||
if (r != AMDSMI_STATUS_SUCCESS) return r;
|
||||
|
||||
info->gfx.gfxip_cu_count = device.cu_active_number;
|
||||
info->gfx.gfxip_cu_count = (uint16_t)device.cu_active_number;
|
||||
|
||||
r = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_INFO,
|
||||
AMDGPU_HW_IP_GFX, sizeof(ip), &ip);
|
||||
@@ -392,16 +387,6 @@ amdsmi_status_t amdsmi_get_caps_info(amdsmi_device_handle device_handle,
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// TODO(bliu): add more vbios info
|
||||
amdsmi_status amdsmi_get_vbios_info(amdsmi_device_handle device_handle,
|
||||
amdsmi_vbios_info_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
return rsmi_wrapper(rsmi_dev_vbios_version_get, device_handle,
|
||||
info->vbios_version_string, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
}
|
||||
|
||||
amdsmi_status_t amdsmi_dev_fan_rpms_get(amdsmi_device_handle device_handle,
|
||||
uint32_t sensor_ind, int64_t *speed) {
|
||||
return rsmi_wrapper(rsmi_dev_fan_rpms_get, device_handle, sensor_ind,
|
||||
@@ -437,7 +422,7 @@ amdsmi_status_t amdsmi_dev_id_get(amdsmi_device_handle device_handle,
|
||||
}
|
||||
|
||||
// TODO(bliu) : add fw info from libdrm
|
||||
amdsmi_status amdsmi_get_fw_info(amdsmi_device_handle dev,
|
||||
amdsmi_status_t amdsmi_get_fw_info(amdsmi_device_handle dev,
|
||||
amdsmi_fw_info_t *info) {
|
||||
const std::map<amdsmi_fw_block, rsmi_fw_block_t> fw_in_rsmi = {
|
||||
{ FW_ID_ASD, RSMI_FW_BLOCK_ASD},
|
||||
@@ -477,6 +462,66 @@ amdsmi_status amdsmi_get_fw_info(amdsmi_device_handle dev,
|
||||
info->num_fw_info++;
|
||||
}
|
||||
}
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// TODO(bliu) : add other asic info
|
||||
amdsmi_status_t
|
||||
amdsmi_get_asic_info(amdsmi_device_handle device_handle, amdsmi_asic_info_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
struct drm_amdgpu_info_device dev_info = {};
|
||||
struct drm_amdgpu_info_vbios vbios = {};
|
||||
char* name;
|
||||
char *tmp;
|
||||
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
|
||||
amdsmi_status_t status;
|
||||
if (gpu_device->check_if_drm_is_supported()){
|
||||
status = gpu_device->amdgpu_query_info(AMDGPU_INFO_DEV_INFO, sizeof(struct drm_amdgpu_info_device), &dev_info);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) return status;
|
||||
status = gpu_device->amdgpu_query_vbios(&vbios);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) return status;
|
||||
|
||||
SMIGPUDEVICE_MUTEX(gpu_device->get_mutex())
|
||||
|
||||
std::string path = "/sys/class/drm/" + gpu_device->get_gpu_path() + "/device/unique_id";
|
||||
FILE *fp = fopen(path.c_str(), "r");
|
||||
if (fp) {
|
||||
fscanf(fp, "%s", &info->asic_serial);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
name = strtok_r((char *) vbios.name, " ", &tmp);
|
||||
if (name)
|
||||
strncpy(info->market_name, name, AMDSMI_MAX_STRING_LENGTH);
|
||||
|
||||
info->device_id = dev_info.device_id;
|
||||
info->family = dev_info.family;
|
||||
info->rev_id = dev_info.pci_rev;
|
||||
}
|
||||
else {
|
||||
uint16_t vendor_id = 0;
|
||||
|
||||
amdsmi_status_t status = rsmi_wrapper(rsmi_dev_serial_number_get, device_handle,
|
||||
info->asic_serial, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
|
||||
status = rsmi_wrapper(rsmi_dev_brand_get, device_handle,
|
||||
info->market_name, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
|
||||
status = rsmi_wrapper(rsmi_dev_vendor_id_get, device_handle,
|
||||
&vendor_id);
|
||||
if (status == AMDSMI_STATUS_SUCCESS) info->vendor_id = vendor_id;
|
||||
vendor_id = 0;
|
||||
|
||||
status = rsmi_wrapper(rsmi_dev_subsystem_vendor_id_get, device_handle,
|
||||
&vendor_id);
|
||||
if (status == AMDSMI_STATUS_SUCCESS) info->subvendor_id = vendor_id;
|
||||
return status;
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
@@ -664,7 +709,7 @@ amdsmi_is_P2P_accessible(amdsmi_device_handle device_handle_src,
|
||||
}
|
||||
|
||||
// TODO(bliu) : other xgmi related information
|
||||
amdsmi_status
|
||||
amdsmi_status_t
|
||||
amdsmi_get_xgmi_info(amdsmi_device_handle device_handle, amdsmi_xgmi_info_t *info) {
|
||||
if (info == nullptr)
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
@@ -882,29 +927,48 @@ amdsmi_status_t amdsmi_dev_gpu_metrics_info_get(
|
||||
reinterpret_cast<rsmi_gpu_metrics_t*>(pgpu_metrics));
|
||||
}
|
||||
|
||||
// TODO(bliu): read from libdrm
|
||||
amdsmi_status
|
||||
amdsmi_status_t
|
||||
amdsmi_get_power_cap_info(amdsmi_device_handle device_handle,
|
||||
uint32_t sensor_ind,
|
||||
amdsmi_power_cap_info_t *info) {
|
||||
if (info == nullptr)
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
|
||||
amd::smi::AMDSmiGPUDevice* gpudevice = nullptr;
|
||||
amdsmi_status_t r = get_gpu_device_from_handle(device_handle, &gpudevice);
|
||||
if (r != AMDSMI_STATUS_SUCCESS)
|
||||
return r;
|
||||
amd::smi::AMDSmiGPUDevice* gpudevice =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
|
||||
amdsmi_status_t status;
|
||||
if (gpudevice->check_if_drm_is_supported()){
|
||||
int power_cap = 0;
|
||||
int dpm = 0;
|
||||
|
||||
// Ignore errors to get as much as possible info.
|
||||
memset(info, 0, sizeof(amdsmi_power_cap_info_t));
|
||||
auto rsmi_status = rsmi_dev_power_cap_default_get(gpudevice->get_gpu_id(),
|
||||
&(info->default_power_cap));
|
||||
rsmi_status = rsmi_dev_power_cap_range_get(gpudevice->get_gpu_id(),
|
||||
sensor_ind, &(info->max_power_cap), &(info->min_power_cap));
|
||||
rsmi_status = rsmi_dev_power_cap_get(gpudevice->get_gpu_id(),
|
||||
sensor_ind, &(info->power_cap));
|
||||
status = smi_amdgpu_get_power_cap(gpudevice, &power_cap);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
info->power_cap = power_cap;
|
||||
|
||||
status = smi_amdgpu_get_ranges(gpudevice, CLOCK_TYPE_GFX,
|
||||
NULL, NULL, &dpm);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
info->dpm_cap = dpm;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
else {
|
||||
// Ignore errors to get as much as possible info.
|
||||
memset(info, 0, sizeof(amdsmi_power_cap_info_t));
|
||||
auto rsmi_status = rsmi_dev_power_cap_default_get(gpudevice->get_gpu_id(),
|
||||
&(info->default_power_cap));
|
||||
rsmi_status = rsmi_dev_power_cap_range_get(gpudevice->get_gpu_id(),
|
||||
sensor_ind, &(info->max_power_cap), &(info->min_power_cap));
|
||||
rsmi_status = rsmi_dev_power_cap_get(gpudevice->get_gpu_id(),
|
||||
sensor_ind, &(info->power_cap));
|
||||
|
||||
// TODO(bliu) : dpm_cap
|
||||
}
|
||||
|
||||
// TODO(bliu) : dpm_cap
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1192,19 +1256,393 @@ amdsmi_status_t amdsmi_version_str_get(amdsmi_sw_component_t component,
|
||||
return rsmi_to_amdsmi_status(status);
|
||||
}
|
||||
|
||||
amdsmi_status amdsmi_get_gpu_activity(amdsmi_device_handle dev,
|
||||
amdsmi_engine_usage_t *info) {
|
||||
if (info == nullptr)
|
||||
amdsmi_status_t
|
||||
amdsmi_get_vbios_info(amdsmi_device_handle dev, amdsmi_vbios_info_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
struct drm_amdgpu_info_vbios vbios = {};
|
||||
|
||||
// Get gpu activity from the gpu_metrics table
|
||||
amdsmi_gpu_metrics_t gpu_metrics_info;
|
||||
auto r = amdsmi_dev_gpu_metrics_info_get(dev, &gpu_metrics_info);
|
||||
if ( r == AMDSMI_STATUS_SUCCESS ) {
|
||||
info->average_gfx_activity = gpu_metrics_info.average_gfx_activity;
|
||||
info->average_umc_activity = gpu_metrics_info.average_umc_activity;
|
||||
info->average_mm_activity[0] = gpu_metrics_info.average_mm_activity;
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
if (gpu_device->check_if_drm_is_supported()){
|
||||
status = gpu_device->amdgpu_query_vbios(&vbios);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
strncpy(info->name, (char *) vbios.name, AMDSMI_MAX_STRING_LENGTH);
|
||||
strncpy(info->build_date, (char *) vbios.date, AMDSMI_MAX_DATE_LENGTH);
|
||||
strncpy(info->part_number, (char *) vbios.vbios_pn, AMDSMI_MAX_STRING_LENGTH);
|
||||
strncpy(info->vbios_version_string, (char *) vbios.vbios_ver_str, AMDSMI_NORMAL_STRING_LENGTH);
|
||||
info->vbios_version = vbios.version;
|
||||
}
|
||||
else {
|
||||
// rocm
|
||||
}
|
||||
|
||||
return r;
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_gpu_activity(amdsmi_device_handle dev, amdsmi_engine_usage_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amdsmi_gpu_metrics_t metrics = {};
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
status = amdsmi_dev_gpu_metrics_info_get(dev, &metrics);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
info->average_gfx_activity = metrics.average_gfx_activity;
|
||||
info->average_mm_activity[0] = metrics.average_mm_activity;
|
||||
info->average_umc_activity = metrics.average_umc_activity;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_power_limit(amdsmi_device_handle dev, amdsmi_power_limit_t *limit) {
|
||||
if (limit == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
int power_limit;
|
||||
status = smi_amdgpu_get_power_cap(gpu_device, &power_limit);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
limit->limit = (uint16_t)(power_limit);
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_clock_measure(amdsmi_device_handle dev, amdsmi_clk_type_t clk_type, amdsmi_clock_measure_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
if (clk_type >= CLOCK_TYPE__MAX) {
|
||||
printf("Domain value greater or equals CLOCK_TYPE__MAX value. Return code: %d", AMDSMI_STATUS_INVAL);
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amdsmi_gpu_metrics_t metrics = {};
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
|
||||
status = amdsmi_dev_gpu_metrics_info_get(dev, &metrics);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
int max_freq;
|
||||
status = smi_amdgpu_get_ranges(gpu_device, clk_type,
|
||||
&max_freq, NULL, NULL);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
info->max_clk = max_freq;
|
||||
|
||||
switch (clk_type) {
|
||||
case CLOCK_TYPE_GFX:
|
||||
info->avg_clk = metrics.average_gfxclk_frequency;
|
||||
info->cur_clk = metrics.current_gfxclk;
|
||||
break;
|
||||
case CLOCK_TYPE_MEM:
|
||||
info->avg_clk = metrics.average_uclk_frequency;
|
||||
info->cur_clk = metrics.current_uclk;
|
||||
break;
|
||||
case CLOCK_TYPE_VCLK0:
|
||||
info->avg_clk = metrics.average_vclk0_frequency;
|
||||
info->cur_clk = metrics.current_vclk0;
|
||||
break;
|
||||
case CLOCK_TYPE_VCLK1:
|
||||
info->avg_clk = metrics.average_vclk1_frequency;
|
||||
info->cur_clk = metrics.current_vclk1;
|
||||
break;
|
||||
default:
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_temperature_limit(amdsmi_device_handle dev, amdsmi_temperature_type_t temp_type, amdsmi_temperature_limit_t *limit) {
|
||||
if (limit == nullptr || temp_type >= TEMPERATURE_TYPE__MAX) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
std::string name;
|
||||
std::string path;
|
||||
switch (temp_type) {
|
||||
case TEMPERATURE_TYPE_EDGE:
|
||||
name = "edge";
|
||||
break;
|
||||
case TEMPERATURE_TYPE_JUNCTION:
|
||||
name = "junction";
|
||||
break;
|
||||
case TEMPERATURE_TYPE_VRAM:
|
||||
name = "mem";
|
||||
break;
|
||||
default:
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
status = smi_amdgpu_find_hwmon_dir(gpu_device, &path);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(gpu_device->get_mutex())
|
||||
|
||||
for (int count = 1; ; count++) {
|
||||
std::string local_path = path + "/temp" +
|
||||
std::to_string(count);
|
||||
std::string temp = local_path + "_label";
|
||||
char f_name[10];
|
||||
std::ifstream file(temp.c_str(), std::ifstream::in);
|
||||
|
||||
if (!file.is_open()) {
|
||||
printf("Failed to open file: %s \n", temp.c_str());
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
file.getline(f_name, 10);
|
||||
|
||||
if (!strstr(name.c_str(), f_name)) {
|
||||
int readTemp = 0;
|
||||
temp = local_path + "_crit";
|
||||
std::ifstream file2(temp.c_str(), std::ifstream::in);
|
||||
|
||||
if (!file2.is_open()) {
|
||||
printf("Failed to open file: %s \n", temp.c_str());
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
file2.getline(f_name, 10);
|
||||
if (!sscanf(f_name, "%d", &readTemp)) {
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
limit->limit = (uint16_t)(readTemp / 1000);
|
||||
break;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
amdsmi_status_t
|
||||
amdsmi_get_temperature_measure(amdsmi_device_handle dev, amdsmi_temperature_type_t temp_type, amdsmi_temperature_t *info) {
|
||||
if (info == nullptr || temp_type > TEMPERATURE_TYPE__MAX) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amdsmi_gpu_metrics_t metrics;
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
status = amdsmi_dev_gpu_metrics_info_get(dev, &metrics);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
switch (temp_type) {
|
||||
case TEMPERATURE_TYPE_EDGE:
|
||||
info->cur_temp = metrics.temperature_edge;
|
||||
break;
|
||||
case TEMPERATURE_TYPE_JUNCTION:
|
||||
info->cur_temp = metrics.temperature_hotspot;
|
||||
break;
|
||||
case TEMPERATURE_TYPE_VRAM:
|
||||
info->cur_temp = metrics.temperature_mem;
|
||||
break;
|
||||
case TEMPERATURE_TYPE_PLX:
|
||||
info->cur_temp = metrics.temperature_vrsoc;
|
||||
break;
|
||||
default:
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_ras_features_enabled(amdsmi_device_handle device_handle, amdsmi_gpu_block block, amdsmi_ras_err_state_t *state) {
|
||||
if (state == nullptr || block > AMDSMI_GPU_BLOCK_LAST) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
uint64_t features_mask = 0;
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
|
||||
amdsmi_status_t status;
|
||||
status = smi_amdgpu_get_enabled_blocks(gpu_device, &features_mask);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
*state = (features_mask & block) ? AMDSMI_RAS_ERR_STATE_ENABLED : AMDSMI_RAS_ERR_STATE_DISABLED;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_bad_page_info(amdsmi_device_handle device_handle, uint32_t *num_pages, amdsmi_retired_page_record_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
|
||||
amdsmi_status_t status;
|
||||
if (gpu_device->check_if_drm_is_supported()){
|
||||
status = smi_amdgpu_get_bad_page_info(gpu_device, num_pages, info);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// rocm
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_ecc_error_count(amdsmi_device_handle dev, amdsmi_error_count_t *ec) {
|
||||
if (ec == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
if (gpu_device->check_if_drm_is_supported()){
|
||||
status = smi_amdgpu_get_ecc_error_count(gpu_device, ec);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// rocm
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_process_list(amdsmi_device_handle dev, amdsmi_process_handle *list, uint32_t *max_processes) {
|
||||
if (max_processes == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
std::vector<long int> pids;
|
||||
uint32_t i = 0;
|
||||
uint64_t size = 0;
|
||||
amdsmi_status_t status;
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
if (gpu_device->check_if_drm_is_supported()){
|
||||
amdsmi_bdf_t bdf = gpu_device->get_bdf();
|
||||
status = gpuvsmi_get_pids(bdf, pids, &size);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
if (*max_processes == 0 || (pids.size() == 0)) {
|
||||
*max_processes = (uint32_t)pids.size();
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
if (!list) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
if (*max_processes < pids.size()) {
|
||||
return AMDSMI_STATUS_OUT_OF_RESOURCES;
|
||||
}
|
||||
for (auto &pid : pids) {
|
||||
if (i >= *max_processes) {
|
||||
break;
|
||||
}
|
||||
list[i++] = (uint32_t)pid;
|
||||
}
|
||||
*max_processes = (uint32_t)pids.size();
|
||||
}
|
||||
else {
|
||||
// rocm
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_process_info(amdsmi_device_handle dev, amdsmi_process_handle process, amdsmi_proc_info_t *info) {
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
if (gpu_device->check_if_drm_is_supported()) {
|
||||
status = gpuvsmi_get_pid_info(gpu_device->get_bdf(), process, *info);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) return status;
|
||||
}
|
||||
else {
|
||||
// rocm
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t
|
||||
amdsmi_get_target_frequency_range(amdsmi_device_handle dev, amdsmi_clk_type_t clk_type, amdsmi_frequency_range_t *range) {
|
||||
if (range == nullptr || clk_type > CLOCK_TYPE__MAX) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
amdsmi_gpu_metrics_t metrics = {};
|
||||
amd::smi::AMDSmiGPUDevice* gpu_device =
|
||||
static_cast<amd::smi::AMDSmiGPUDevice*>(dev);
|
||||
amdsmi_status_t status;
|
||||
|
||||
int min = 0, max = 0;
|
||||
status = amdsmi_dev_gpu_metrics_info_get(dev, &metrics);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
status = smi_amdgpu_get_ranges(gpu_device, clk_type, &max, &min, nullptr);
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
range->supported_freq_range.lower_bound = (long)min;
|
||||
range->current_freq_range.lower_bound = (long)min;
|
||||
range->supported_freq_range.upper_bound = (long)max;
|
||||
max = 0;
|
||||
switch (clk_type) {
|
||||
case CLOCK_TYPE_GFX:
|
||||
max = metrics.current_gfxclk;
|
||||
break;
|
||||
case CLOCK_TYPE_MEM:
|
||||
max = metrics.current_uclk;
|
||||
break;
|
||||
case CLOCK_TYPE_VCLK0:
|
||||
max = metrics.current_vclk0;
|
||||
break;
|
||||
case CLOCK_TYPE_VCLK1:
|
||||
max = metrics.current_vclk1;
|
||||
break;
|
||||
default:
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
range->current_freq_range.upper_bound = (long)max;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -55,27 +55,40 @@ namespace smi {
|
||||
|
||||
amdsmi_status_t AMDSmiDrm::init() {
|
||||
// A few RAII handler
|
||||
|
||||
using dir_ptr = std::unique_ptr<DIR, decltype(&closedir)>;
|
||||
using drm_version_ptr = std::unique_ptr<drmVersion,
|
||||
decltype(&drmFreeVersion)>;
|
||||
// using drm_device_ptr = std::unique_ptr(drmDevicePtr,
|
||||
// decltype(&drmFreeDevice));
|
||||
|
||||
struct dirent *dir = nullptr;
|
||||
int fd = -1;
|
||||
|
||||
|
||||
amdsmi_status_t status = lib_loader_.load("libdrm.so");
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
// load symbol from libdrm
|
||||
drm_cmd_write_ = nullptr;
|
||||
status = lib_loader_.load_symbol(&drm_cmd_write_, "drmCommandWrite");
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
using drmGetVersionType = drmVersionPtr (*)(int); // drmGetVersion
|
||||
using drmFreeVersionType = void (*)(drmVersionPtr); // drmFreeVersion
|
||||
|
||||
using drmGetVersionType = drmVersionPtr (*)(int); // drmGetVersion
|
||||
using drmFreeVersionType = void (*)(drmVersionPtr); // drmFreeVersion
|
||||
using drmGetDeviceType = int(*)(int, drmDevicePtr*); // drmGetDevice
|
||||
using drmFreeDeviceType = void(*)(drmDevicePtr*); // drmFreeDevice
|
||||
|
||||
drmGetVersionType drm_get_version = nullptr;
|
||||
drmFreeVersionType drm_free_version = nullptr;
|
||||
|
||||
drmGetDeviceType drm_get_device = nullptr;
|
||||
drmFreeDeviceType drm_free_device = nullptr;
|
||||
|
||||
status = lib_loader_.load_symbol(&drm_get_version, "drmGetVersion");
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
@@ -85,10 +98,20 @@ amdsmi_status_t AMDSmiDrm::init() {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = lib_loader_.load_symbol(&drm_get_device, "drmGetDevice");
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
status = lib_loader_.load_symbol(&drm_free_device, "drmFreeDevice");
|
||||
if (status != AMDSMI_STATUS_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
auto d = dir_ptr(opendir("/dev/dri/"), &closedir);
|
||||
if (d == nullptr) return AMDSMI_STATUS_NOT_INIT;
|
||||
|
||||
drmDevicePtr device;
|
||||
|
||||
while ((dir = readdir(d.get())) != NULL) {
|
||||
char* name_cstr = new char[sizeof(dir->d_name) + 10];
|
||||
auto name = std::unique_ptr<char[]>(name_cstr);
|
||||
@@ -105,7 +128,22 @@ amdsmi_status_t AMDSmiDrm::init() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (drm_get_device(fd, &device) != 0) {
|
||||
drm_free_device(&device);
|
||||
return AMDSMI_STATUS_DRM_ERROR;
|
||||
}
|
||||
|
||||
drm_fds_.push_back(fd);
|
||||
drm_paths_.push_back(dir->d_name);
|
||||
|
||||
amdsmi_bdf_t bdf;
|
||||
bdf.function_number = device->businfo.pci->func;
|
||||
bdf.device_number = device->businfo.pci->dev;
|
||||
bdf.bus_number = device->businfo.pci->bus;
|
||||
bdf.domain_number = device->businfo.pci->domain;
|
||||
|
||||
drm_bdfs_.push_back(bdf);
|
||||
drm_free_device(&device);
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
@@ -115,7 +153,10 @@ amdsmi_status_t AMDSmiDrm::cleanup() {
|
||||
for (unsigned int i=0; i < drm_fds_.size(); i++) {
|
||||
close(drm_fds_[i]);
|
||||
}
|
||||
|
||||
drm_fds_.clear();
|
||||
drm_paths_.clear();
|
||||
drm_bdfs_.clear();
|
||||
lib_loader_.unload();
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
@@ -190,9 +231,34 @@ amdsmi_status_t AMDSmiDrm::amdgpu_query_vbios(int fd, void *info) {
|
||||
}
|
||||
|
||||
|
||||
int AMDSmiDrm::get_drm_fd_by_index(uint32_t gpu_index) const {
|
||||
if (gpu_index + 1 > drm_fds_.size()) return -1;
|
||||
return drm_fds_[gpu_index];
|
||||
amdsmi_status_t AMDSmiDrm::get_drm_fd_by_index(uint32_t gpu_index, uint32_t *fd_info) const {
|
||||
if (gpu_index + 1 > drm_fds_.size()) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
*fd_info = drm_fds_[gpu_index];
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiDrm::get_bdf_by_index(uint32_t gpu_index, amdsmi_bdf_t *bdf_info) const {
|
||||
if (gpu_index + 1 > drm_bdfs_.size()) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
*bdf_info = drm_bdfs_[gpu_index];
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiDrm::get_drm_path_by_index(uint32_t gpu_index, std::string *drm_path) const {
|
||||
if (gpu_index + 1 > drm_paths_.size()) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
*drm_path = drm_paths_[gpu_index];
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
std::vector<std::string>& AMDSmiDrm::get_drm_paths() {
|
||||
return drm_paths_;
|
||||
}
|
||||
|
||||
bool AMDSmiDrm::check_if_drm_is_supported() {
|
||||
return drm_cmd_write_ != NULL ? true : false;
|
||||
}
|
||||
|
||||
std::vector<amdsmi_bdf_t> AMDSmiDrm::get_bdfs() {
|
||||
return drm_bdfs_;
|
||||
}
|
||||
|
||||
} // namespace smi
|
||||
|
||||
@@ -52,33 +52,78 @@ uint32_t AMDSmiGPUDevice::get_gpu_id() const {
|
||||
return gpu_id_;
|
||||
}
|
||||
|
||||
uint32_t AMDSmiGPUDevice::get_gpu_fd() const {
|
||||
return fd_;
|
||||
}
|
||||
|
||||
std::string& AMDSmiGPUDevice::get_gpu_path() {
|
||||
return path_;
|
||||
}
|
||||
|
||||
amdsmi_bdf_t AMDSmiGPUDevice::get_bdf() {
|
||||
return bdf_;
|
||||
}
|
||||
amdsmi_status_t AMDSmiGPUDevice::get_drm_data() {
|
||||
amdsmi_status_t ret;
|
||||
uint32_t fd = 0;
|
||||
std::string path;
|
||||
amdsmi_bdf_t bdf;
|
||||
ret = drm_.get_drm_fd_by_index(gpu_id_, &fd);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
ret = drm_.get_drm_path_by_index(gpu_id_, &path);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
ret = drm_.get_bdf_by_index(gpu_id_, &bdf);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
|
||||
mutex_ = shared_mutex_init(path.c_str(), 0777);
|
||||
if (mutex_.ptr == nullptr) {
|
||||
printf("Failed to create shared mem. mutex.");
|
||||
return AMDSMI_STATUS_INIT_ERROR;
|
||||
}
|
||||
bdf_ = bdf, path_ = path, fd_ = fd;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
pthread_mutex_t* AMDSmiGPUDevice::get_mutex() {
|
||||
return mutex_.ptr;
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiGPUDevice::amdgpu_query_info(unsigned info_id,
|
||||
unsigned size, void *value) const {
|
||||
int fd = drm_.get_drm_fd_by_index(gpu_id_);
|
||||
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
amdsmi_status_t ret;
|
||||
uint32_t fd = 0;
|
||||
ret = drm_.get_drm_fd_by_index(gpu_id_, &fd);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
|
||||
return drm_.amdgpu_query_info(fd, info_id, size, value);
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiGPUDevice::amdgpu_query_hw_ip(unsigned info_id,
|
||||
unsigned hw_ip_type, unsigned size, void *value) const {
|
||||
int fd = drm_.get_drm_fd_by_index(gpu_id_);
|
||||
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
amdsmi_status_t ret;
|
||||
uint32_t fd = 0;
|
||||
ret = drm_.get_drm_fd_by_index(gpu_id_, &fd);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
|
||||
return drm_.amdgpu_query_hw_ip(fd, info_id, hw_ip_type, size, value);
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiGPUDevice::amdgpu_query_fw(unsigned info_id,
|
||||
unsigned fw_type, unsigned size, void *value) const {
|
||||
int fd = drm_.get_drm_fd_by_index(gpu_id_);
|
||||
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
amdsmi_status_t ret;
|
||||
uint32_t fd = 0;
|
||||
ret = drm_.get_drm_fd_by_index(gpu_id_, &fd);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
|
||||
return drm_.amdgpu_query_fw(fd, info_id, fw_type, size, value);
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiGPUDevice::amdgpu_query_vbios(void *info) const {
|
||||
int fd = drm_.get_drm_fd_by_index(gpu_id_);
|
||||
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
amdsmi_status_t ret;
|
||||
uint32_t fd = 0;
|
||||
ret = drm_.get_drm_fd_by_index(gpu_id_, &fd);
|
||||
if (ret != AMDSMI_STATUS_SUCCESS) return AMDSMI_STATUS_NOT_SUPPORTED;;
|
||||
|
||||
return drm_.amdgpu_query_vbios(fd, info);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,11 @@ AMDSmiSocket::~AMDSmiSocket() {
|
||||
devices_.clear();
|
||||
}
|
||||
|
||||
amdsmi_status_t AMDSmiSocket::get_device_count(uint32_t* device_count) const {
|
||||
*device_count = static_cast<uint32_t>(devices_.size());
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
} // namespace smi
|
||||
} // namespace amd
|
||||
|
||||
|
||||
+71
-34
@@ -45,6 +45,7 @@
|
||||
#include "amd_smi/impl/amd_smi_system.h"
|
||||
#include "amd_smi/impl/amd_smi_gpu_device.h"
|
||||
#include "rocm_smi/rocm_smi.h"
|
||||
#include "rocm_smi/rocm_smi_main.h"
|
||||
|
||||
|
||||
namespace amd {
|
||||
@@ -54,55 +55,91 @@ namespace smi {
|
||||
|
||||
amdsmi_status_t AMDSmiSystem::init(uint64_t flags) {
|
||||
init_flag_ = flags;
|
||||
amdsmi_status_t amd_smi_status;
|
||||
// populate sockets and devices
|
||||
if (flags & AMDSMI_INIT_AMD_GPUS) {
|
||||
drm_.init();
|
||||
amd_smi_status = drm_.init();
|
||||
// init rsmi
|
||||
rsmi_status_t ret = rsmi_init(flags);
|
||||
if (ret != RSMI_STATUS_SUCCESS) {
|
||||
return static_cast<amdsmi_status_t>(ret);
|
||||
}
|
||||
|
||||
uint32_t device_count = 0;
|
||||
ret = rsmi_num_monitor_devices(&device_count);
|
||||
if (ret != RSMI_STATUS_SUCCESS) {
|
||||
return static_cast<amdsmi_status_t>(ret);
|
||||
}
|
||||
// libdrm is supported
|
||||
if (amd_smi_status == AMDSMI_STATUS_SUCCESS) {
|
||||
amd::smi::RocmSMI::getInstance().DiscoverAmdgpuDevices();
|
||||
uint32_t device_count = amd::smi::RocmSMI::getInstance().devices().size();
|
||||
for (uint32_t i=0; i < device_count; i++) {
|
||||
std::stringstream ss;
|
||||
//values for socked id are harcoded
|
||||
ss << std::setfill('0') << std::uppercase << std::hex
|
||||
<< std::setw(4) << drm_.get_bdfs()[i].domain_number << ":"
|
||||
<< std::setw(2) << drm_.get_bdfs()[i].bus_number << ":"
|
||||
<< std::setw(2) << drm_.get_bdfs()[i].device_number << "."
|
||||
<< std::setw(2) << drm_.get_bdfs()[i].function_number;
|
||||
|
||||
for (uint32_t i=0; i < device_count; i++) {
|
||||
uint64_t bdfid = 0;
|
||||
ret = rsmi_dev_pci_id_get(i, &bdfid);
|
||||
// Multiple devices may share the same socket
|
||||
auto socket_id = ss.str();
|
||||
AMDSmiSocket* socket = nullptr;
|
||||
for (unsigned int j=0; j < sockets_.size(); j++) {
|
||||
if (sockets_[j]->get_socket_id() == socket_id) {
|
||||
socket = sockets_[j];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (socket == nullptr) {
|
||||
socket = new AMDSmiSocket(ss.str());
|
||||
sockets_.push_back(socket);
|
||||
}
|
||||
|
||||
AMDSmiDevice* device = new AMDSmiGPUDevice(i, drm_);
|
||||
socket->add_device(device);
|
||||
devices_.insert(device);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
uint32_t device_count = 0;
|
||||
ret = rsmi_num_monitor_devices(&device_count);
|
||||
if (ret != RSMI_STATUS_SUCCESS) {
|
||||
return static_cast<amdsmi_status_t>(ret);
|
||||
}
|
||||
|
||||
uint64_t domain = (bdfid >> 32) & 0xffffffff;
|
||||
uint64_t bus = (bdfid >> 8) & 0xff;
|
||||
uint64_t device_id = (bdfid >> 3) & 0x1f;
|
||||
uint64_t function = bdfid & 0x7;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::setfill('0') << std::uppercase << std::hex
|
||||
<< std::setw(4) << domain << ":" << std::setw(2) << bus << ":"
|
||||
<< std::setw(2) << device_id << "." << std::setw(2) << function;
|
||||
|
||||
// Multiple devices may share the same socket
|
||||
auto socket_id = ss.str();
|
||||
AMDSmiSocket* socket = nullptr;
|
||||
for (unsigned int j=0; j < sockets_.size(); j++) {
|
||||
if (sockets_[j]->get_socket_id() == socket_id) {
|
||||
socket = sockets_[j];
|
||||
break;
|
||||
for (uint32_t i=0; i < device_count; i++) {
|
||||
uint64_t bdfid = 0;
|
||||
ret = rsmi_dev_pci_id_get(i, &bdfid);
|
||||
if (ret != RSMI_STATUS_SUCCESS) {
|
||||
return static_cast<amdsmi_status_t>(ret);
|
||||
}
|
||||
}
|
||||
if (socket == nullptr) {
|
||||
socket = new AMDSmiSocket(ss.str());
|
||||
sockets_.push_back(socket);
|
||||
}
|
||||
|
||||
AMDSmiDevice* device = new AMDSmiGPUDevice(i, drm_);
|
||||
socket->add_device(device);
|
||||
devices_.insert(device);
|
||||
uint64_t domain = (bdfid >> 32) & 0xffffffff;
|
||||
uint64_t bus = (bdfid >> 8) & 0xff;
|
||||
uint64_t device_id = (bdfid >> 3) & 0x1f;
|
||||
uint64_t function = bdfid & 0x7;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::setfill('0') << std::uppercase << std::hex
|
||||
<< std::setw(4) << domain << ":" << std::setw(2) << bus << ":"
|
||||
<< std::setw(2) << device_id << "." << std::setw(2) << function;
|
||||
|
||||
// Multiple devices may share the same socket
|
||||
auto socket_id = ss.str();
|
||||
AMDSmiSocket* socket = nullptr;
|
||||
for (unsigned int j=0; j < sockets_.size(); j++) {
|
||||
if (sockets_[j]->get_socket_id() == socket_id) {
|
||||
socket = sockets_[j];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (socket == nullptr) {
|
||||
socket = new AMDSmiSocket(ss.str());
|
||||
sockets_.push_back(socket);
|
||||
}
|
||||
|
||||
AMDSmiDevice* device = new AMDSmiGPUDevice(i, drm_);
|
||||
socket->add_device(device);
|
||||
devices_.insert(device);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
/* * Copyright (C) 2022 Advanced Micro Devices. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <xf86drm.h>
|
||||
#include <xf86drmMode.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "amd_smi/impl/amd_smi_utils.h"
|
||||
#include "shared_mutex.h" // NOLINT
|
||||
|
||||
static const uint32_t kAmdGpuId = 0x1002;
|
||||
|
||||
static bool isAMDGPU(std::string dev_path) {
|
||||
std::string vend_path = dev_path + "/device/vendor";
|
||||
std::string vbios_v_path = dev_path + "/device/vbios_version";
|
||||
if (!amd::smi::FileExists(vend_path.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!amd::smi::FileExists(vbios_v_path.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream fs;
|
||||
fs.open(vend_path);
|
||||
|
||||
if (!fs.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t vendor_id;
|
||||
|
||||
fs >> std::hex >> vendor_id;
|
||||
|
||||
fs.close();
|
||||
|
||||
if (vendor_id == kAmdGpuId) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
amdsmi_status_t smi_amdgpu_find_hwmon_dir(amd::smi::AMDSmiGPUDevice *device, std::string* full_path)
|
||||
{
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
if (full_path == nullptr) {
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
|
||||
DIR *dh;
|
||||
struct dirent * contents;
|
||||
std::string device_path = "/sys/class/drm/" + device->get_gpu_path();
|
||||
std::string directory_path = device_path + "/device/hwmon/";
|
||||
|
||||
if (!isAMDGPU(device_path)) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
dh = opendir(directory_path.c_str());
|
||||
if (!dh) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
/*
|
||||
First directory is '.', second directory is '..' and third directory is
|
||||
valid directory for reading sysfs node
|
||||
*/
|
||||
while ((contents = readdir(dh)) != NULL) {
|
||||
std::string name = contents->d_name;
|
||||
if (name.find("hwmon", 0) != std::string::npos)
|
||||
*full_path = directory_path + name;
|
||||
}
|
||||
|
||||
closedir(dh);
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
amdsmi_status_t smi_amdgpu_get_board_info(amd::smi::AMDSmiGPUDevice* device, amdsmi_board_info_t *info) {
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
std::string product_name_path = "/sys/class/drm/" + device->get_gpu_path() + std::string("/device/product_name");
|
||||
std::string product_number_path = "/sys/class/drm/" + device->get_gpu_path() + std::string("/device/product_number");
|
||||
std::string serial_number_path = "/sys/class/drm/" + device->get_gpu_path() + std::string("/device/serial_number");
|
||||
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen(product_name_path.c_str(), "rb");
|
||||
if (!fp) {
|
||||
fgets(info->product_name, sizeof(info->product_name), fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
fp = fopen(product_number_path.c_str(), "rb");
|
||||
if (!fp) {
|
||||
fgets(info->model_number, sizeof(info->model_number), fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
fp = fopen(serial_number_path.c_str(), "rb");
|
||||
if (!fp) {
|
||||
fscanf(fp, "%lx", &info->serial_number);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t smi_amdgpu_get_power_cap(amd::smi::AMDSmiGPUDevice* device, int *cap)
|
||||
{
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
constexpr int DATA_SIZE = 10;
|
||||
char val[DATA_SIZE];
|
||||
std::string fullpath;
|
||||
amdsmi_status_t ret = AMDSMI_STATUS_SUCCESS;
|
||||
|
||||
ret = smi_amdgpu_find_hwmon_dir(device, &fullpath);
|
||||
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
fullpath += "/power1_cap_max";
|
||||
std::ifstream file(fullpath.c_str(), std::ifstream::in);
|
||||
if (!file.is_open()) {
|
||||
printf("Failed to open file: %s \n", fullpath.c_str());
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
file.getline(val, DATA_SIZE);
|
||||
|
||||
if (sscanf(val, "%d", cap) < 0) {
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
// Dividing by 1000000 to get measurement in Watts
|
||||
*cap /= 1000000;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t smi_amdgpu_get_ranges(amd::smi::AMDSmiGPUDevice* device, amdsmi_clk_type_t domain,
|
||||
int *max_freq, int *min_freq, int *num_dpm)
|
||||
{
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
std::string fullpath = "/sys/class/drm/" + device->get_gpu_path() + "/device";
|
||||
char str[10];
|
||||
unsigned int max, min, dpm;
|
||||
|
||||
switch (domain) {
|
||||
case CLOCK_TYPE_GFX:
|
||||
fullpath += "/pp_dpm_sclk";
|
||||
break;
|
||||
case CLOCK_TYPE_MEM:
|
||||
fullpath += "/pp_dpm_mclk";
|
||||
break;
|
||||
case CLOCK_TYPE_VCLK0:
|
||||
fullpath += "/pp_dpm_vclk";
|
||||
break;
|
||||
case CLOCK_TYPE_VCLK1:
|
||||
fullpath += "/pp_dpm_vclk1";
|
||||
break;
|
||||
default:
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
std::ifstream ranges(fullpath.c_str());
|
||||
|
||||
if (ranges.fail()) {
|
||||
printf("Failed to open file: %s \n", fullpath.c_str());
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
max = 0;
|
||||
min = -1;
|
||||
dpm = 0;
|
||||
for (std::string line; getline(ranges, line);) {
|
||||
unsigned int d, freq;
|
||||
|
||||
if (sscanf(line.c_str(), "%u: %d%s", &d, &freq, str) <= 2){
|
||||
ranges.close();
|
||||
return AMDSMI_STATUS_IO;
|
||||
}
|
||||
|
||||
max = freq > max ? freq : max;
|
||||
min = freq < min ? freq: min;
|
||||
dpm = d > dpm ? d : dpm;
|
||||
}
|
||||
|
||||
if (num_dpm)
|
||||
*num_dpm = dpm;
|
||||
if (max_freq)
|
||||
*max_freq = max;
|
||||
if (min_freq)
|
||||
*min_freq = min;
|
||||
|
||||
ranges.close();
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t smi_amdgpu_get_enabled_blocks(amd::smi::AMDSmiGPUDevice* device, uint64_t *enabled_blocks) {
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
std::string fullpath = "/sys/class/drm/" + device->get_gpu_path() + "/device/ras/features";
|
||||
std::ifstream f(fullpath.c_str());
|
||||
std::string tmp_str;
|
||||
|
||||
if (f.fail()) {
|
||||
printf("Failed to open file: %s \n", fullpath.c_str());
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
getline(f, line);
|
||||
|
||||
std::istringstream f1(line);
|
||||
|
||||
f1 >> tmp_str; // ignore
|
||||
f1 >> tmp_str; // ignore
|
||||
f1 >> tmp_str;
|
||||
|
||||
*enabled_blocks = strtoul(tmp_str.c_str(), nullptr, 16);
|
||||
f.close();
|
||||
|
||||
if (*enabled_blocks == 0 || *enabled_blocks == ULONG_MAX) {
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t smi_amdgpu_get_bad_page_info(amd::smi::AMDSmiGPUDevice* device, uint32_t *num_pages, amdsmi_retired_page_record_t *info) {
|
||||
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
std::string line;
|
||||
std::vector<std::string> badPagesVec;
|
||||
|
||||
std::string fullpath = "/sys/class/drm/" + device->get_gpu_path() + std::string("/device/ras/gpu_vram_bad_pages");
|
||||
std::ifstream fs(fullpath.c_str());
|
||||
|
||||
if (fs.fail()) {
|
||||
printf("Failed to open file: %s \n", fullpath.c_str());
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
while (std::getline(fs, line)) {
|
||||
badPagesVec.push_back(line);
|
||||
}
|
||||
|
||||
if (badPagesVec.size() == 0) {
|
||||
num_pages = 0;
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
// Remove any *trailing* empty (whitespace) lines
|
||||
while (badPagesVec.size() != 0 &&
|
||||
badPagesVec.back().find_first_not_of(" \t\n\v\f\r") == std::string::npos) {
|
||||
badPagesVec.pop_back();
|
||||
}
|
||||
|
||||
*num_pages = static_cast<uint32_t>(badPagesVec.size());
|
||||
|
||||
if (info == nullptr) {
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
char status_code;
|
||||
amdsmi_memory_page_status_t tmp_stat;
|
||||
std::string junk;
|
||||
|
||||
for (uint32_t i = 0; i < *num_pages; ++i) {
|
||||
std::istringstream fs1(badPagesVec[i]);
|
||||
|
||||
fs1 >> std::hex >> info[i].page_address;
|
||||
fs1 >> junk;
|
||||
fs1 >> std::hex >> info[i].page_size;
|
||||
fs1 >> junk;
|
||||
fs1 >> status_code;
|
||||
|
||||
switch (status_code) {
|
||||
case 'P':
|
||||
tmp_stat = AMDSMI_MEM_PAGE_STATUS_PENDING;
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
tmp_stat = AMDSMI_MEM_PAGE_STATUS_UNRESERVABLE;
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
tmp_stat = AMDSMI_MEM_PAGE_STATUS_RESERVED;
|
||||
break;
|
||||
default:
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
}
|
||||
info[i].status = tmp_stat;
|
||||
}
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t smi_amdgpu_get_ecc_error_count(amd::smi::AMDSmiGPUDevice* device, amdsmi_error_count_t *err_cnt) {
|
||||
|
||||
if (!device->check_if_drm_is_supported()) {
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
SMIGPUDEVICE_MUTEX(device->get_mutex())
|
||||
char str[10];
|
||||
|
||||
std::string fullpath = "/sys/class/drm/" + device->get_gpu_path() + std::string("/device/ras/umc_err_count");
|
||||
std::ifstream f(fullpath.c_str());
|
||||
|
||||
if (f.fail()) {
|
||||
printf("Failed to open file: %s \n", fullpath.c_str());
|
||||
return AMDSMI_STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
getline(f, line);
|
||||
sscanf(line.c_str(), "%s%ld", str, &(err_cnt->uncorrectable_count));
|
||||
|
||||
getline(f, line);
|
||||
sscanf(line.c_str(), "%s%ld", str, &(err_cnt->correctable_count));
|
||||
|
||||
f.close();
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
/* * Copyright (C) 2022 Advanced Micro Devices. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
|
||||
#include "amd_smi/amd_smi.h"
|
||||
#include "amd_smi/impl/amd_smi_utils.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
amdsmi_status_t gpuvsmi_pid_is_gpu(const std::string &path, const char *bdf)
|
||||
{
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
|
||||
d = opendir(path.c_str());
|
||||
if (!d)
|
||||
return AMDSMI_STATUS_NO_PERM;
|
||||
|
||||
/* iterate through all the fds, try to find
|
||||
* a match for the GPU bdf
|
||||
*/
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
std::string file = path + dir->d_name;
|
||||
std::ifstream fdinfo(file.c_str());
|
||||
for (std::string line; std::getline(fdinfo, line);) {
|
||||
if (line.find(bdf) != std::string::npos) {
|
||||
closedir(d);
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(d);
|
||||
|
||||
return AMDSMI_STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
amdsmi_status_t gpuvsmi_get_pids(const amdsmi_bdf_t &bdf, std::vector<long int> &pids, uint64_t *size)
|
||||
{
|
||||
char bdf_str[13];
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
|
||||
/* 0000:00:00.0 */
|
||||
snprintf(bdf_str, 13, "%04x:%02x:%02x.%d", bdf.domain_number & 0xffff,
|
||||
bdf.bus_number & 0xff,
|
||||
bdf.device_number & 0x1f,
|
||||
bdf.function_number & 0x7);
|
||||
|
||||
d = opendir("/proc");
|
||||
if (!d)
|
||||
return AMDSMI_STATUS_NO_PERM;
|
||||
|
||||
pids.clear();
|
||||
/* Find the pid folders in /proc/ that we have access to */
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
if (dir->d_type == DT_DIR) {
|
||||
/* Try to cast the name of the folder to a
|
||||
* number, if it fails, it is not */
|
||||
char *p;
|
||||
long int pid;
|
||||
|
||||
pid = strtol(dir->d_name, &p, 10);
|
||||
if (*p != 0)
|
||||
continue;
|
||||
|
||||
/* Check if fdinfo is accesible */
|
||||
std::string path = "/proc/" + std::string(dir->d_name) + "/fdinfo/";
|
||||
|
||||
if (access(path.c_str(), R_OK))
|
||||
continue;
|
||||
|
||||
/* check if GPU is present */
|
||||
if (gpuvsmi_pid_is_gpu(path, bdf_str))
|
||||
continue;
|
||||
pids.push_back(pid);
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
|
||||
*size = pids.size();
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
amdsmi_status_t gpuvsmi_get_pid_info(const amdsmi_bdf_t &bdf, long int pid,
|
||||
amdsmi_proc_info_t &info)
|
||||
{
|
||||
char bdf_str[13];
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
|
||||
/* 0000:00:00.0 */
|
||||
snprintf(bdf_str, 13, "%04x:%02x:%02x.%d", bdf.domain_number & 0xffff,
|
||||
bdf.bus_number & 0xff,
|
||||
bdf.device_number & 0x1f,
|
||||
bdf.function_number & 0x7);
|
||||
|
||||
|
||||
std::string path = "/proc/" + std::to_string(pid) + "/fdinfo/";
|
||||
std::string name_path = "/proc/" + std::to_string(pid) + "/comm";
|
||||
std::string cgroup_path = "/proc/" + std::to_string(pid) + "/cgroup";
|
||||
|
||||
if (gpuvsmi_pid_is_gpu(path.c_str(), bdf_str)) {
|
||||
return AMDSMI_STATUS_INVAL;
|
||||
}
|
||||
|
||||
d = opendir(path.c_str());
|
||||
if (!d)
|
||||
return AMDSMI_STATUS_NO_PERM;
|
||||
|
||||
/* Vectors to check if repated fd pasid */
|
||||
std::vector<int> pasids;
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
/* Iterate through all fdinfos */
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
|
||||
std::string file = path + dir->d_name;
|
||||
std::ifstream fdinfo(file.c_str());
|
||||
|
||||
for (std::string line; getline(fdinfo, line);) {
|
||||
if (line.find("pasid:") != std::string::npos) {
|
||||
int pasid;
|
||||
|
||||
if (sscanf(line.c_str(), "pasid: %d", &pasid) != 1)
|
||||
continue;
|
||||
|
||||
auto it = std::find(pasids.begin(), pasids.end(), pasid);
|
||||
|
||||
if (it == pasids.end())
|
||||
pasids.push_back(pasid);
|
||||
} else if (line.find("gtt mem:") != std::string::npos) {
|
||||
unsigned long mem;
|
||||
|
||||
if (sscanf(line.c_str(), "gtt mem: %lu", &mem) != 1)
|
||||
continue;
|
||||
|
||||
info.mem += mem * 1024;
|
||||
info.memory_usage.gtt_mem += mem * 1024;
|
||||
} else if (line.find("cpu mem:") != std::string::npos) {
|
||||
unsigned long mem;
|
||||
|
||||
if (sscanf(line.c_str(), "cpu mem: %lu", &mem) != 1)
|
||||
continue;
|
||||
|
||||
info.mem += mem * 1024;
|
||||
info.memory_usage.cpu_mem += mem * 1024;
|
||||
} else if (line.find("vram mem:") != std::string::npos) {
|
||||
unsigned long mem;
|
||||
|
||||
if (sscanf(line.c_str(), "vram mem: %lu", &mem) != 1)
|
||||
continue;
|
||||
|
||||
info.mem += mem * 1024;
|
||||
info.memory_usage.vram_mem += mem * 1024;
|
||||
} else if (line.find("gfx") != std::string::npos) {
|
||||
float usage;
|
||||
int ring;
|
||||
|
||||
if (sscanf(line.c_str(), "gfx%d: %f%%", &ring, &usage) != 2)
|
||||
continue;
|
||||
|
||||
if (ring >= AMDSMI_MAX_MM_IP_COUNT)
|
||||
continue;
|
||||
|
||||
info.engine_usage.gfx[ring] += (uint16_t)(usage * 100);
|
||||
} else if (line.find("compute") != std::string::npos) {
|
||||
float usage;
|
||||
int ring;
|
||||
|
||||
if (sscanf(line.c_str(), "compute%d: %f%%", &ring, &usage) != 2)
|
||||
continue;
|
||||
|
||||
if (ring >= AMDSMI_MAX_MM_IP_COUNT)
|
||||
continue;
|
||||
|
||||
info.engine_usage.compute[ring] += (uint16_t)(usage * 100);
|
||||
} else if (line.find("dma") != std::string::npos) {
|
||||
float usage;
|
||||
int ring;
|
||||
|
||||
if (sscanf(line.c_str(), "dma%d: %f%%", &ring, &usage) != 2)
|
||||
continue;
|
||||
|
||||
if (ring >= AMDSMI_MAX_MM_IP_COUNT)
|
||||
continue;
|
||||
|
||||
info.engine_usage.sdma[ring] += (uint16_t)(usage * 100);
|
||||
} else if (line.find("enc") != std::string::npos) {
|
||||
float usage;
|
||||
int ring;
|
||||
|
||||
if (sscanf(line.c_str(), "enc%d: %f%%", &ring, &usage) != 2)
|
||||
continue;
|
||||
|
||||
if (ring >= AMDSMI_MAX_MM_IP_COUNT)
|
||||
continue;
|
||||
|
||||
info.engine_usage.enc[ring] += (uint16_t)(usage * 100);
|
||||
} else if (line.find("dec") != std::string::npos) {
|
||||
float usage;
|
||||
int ring;
|
||||
|
||||
if (sscanf(line.c_str(), "dec%d: %f%%", &ring, &usage) != 2)
|
||||
continue;
|
||||
|
||||
if (ring >= AMDSMI_MAX_MM_IP_COUNT)
|
||||
continue;
|
||||
|
||||
info.engine_usage.dec[ring] += (uint16_t)(usage * 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
closedir(d);
|
||||
|
||||
if (!pasids.size())
|
||||
return AMDSMI_STATUS_NOT_FOUND;
|
||||
|
||||
std::ifstream filename(name_path.c_str());
|
||||
std::string name;
|
||||
|
||||
getline(filename, name);
|
||||
|
||||
if (name.empty())
|
||||
return AMDSMI_STATUS_API_FAILED;
|
||||
|
||||
strncpy(info.name, name.c_str(), std::min(
|
||||
(unsigned long) AMDSMI_NORMAL_STRING_LENGTH,
|
||||
name.length()));
|
||||
|
||||
info.pid = (uint32_t)pid;
|
||||
|
||||
return AMDSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
新しいイシューから参照
ユーザーをブロックする