Skip to content

Commit

Permalink
Remove unnecessary list and free amdgpu resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Syllo committed Aug 21, 2022
1 parent c0031ee commit 75c749d
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/extract_gpuinfo_amdgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ struct amdgpu_process_info_cache {

struct gpu_info_amdgpu {
struct gpu_info base;
struct list_head allocate_list;

drmVersionPtr drmVersion;
int fd;
Expand All @@ -112,7 +111,8 @@ struct gpu_info_amdgpu {
unsigned maxFanValue;
};

static LIST_HEAD(allocations);
unsigned amdgpu_count;
static struct gpu_info_amdgpu *gpu_infos;

static bool gpuinfo_amdgpu_init(void);
static void gpuinfo_amdgpu_shutdown(void);
Expand Down Expand Up @@ -213,6 +213,29 @@ static bool gpuinfo_amdgpu_init(void) {
}

static void gpuinfo_amdgpu_shutdown(void) {
for (unsigned i = 0; i < amdgpu_count; ++i) {
struct gpu_info_amdgpu *gpu_info = &gpu_infos[i];
if (gpu_info->fanSpeedFILE)
fclose(gpu_info->fanSpeedFILE);
if (gpu_info->PCIeDPM)
fclose(gpu_info->PCIeDPM);
if (gpu_info->PCIeBW)
fclose(gpu_info->PCIeBW);
if (gpu_info->powerCap)
fclose(gpu_info->powerCap);
_drmFreeVersion(gpu_info->drmVersion);
_amdgpu_device_deinitialize(gpu_info->amdgpu_device);
// Clean the process cache
struct amdgpu_process_info_cache *cache_entry, *cache_tmp;
HASH_ITER(hh, gpu_info->last_update_process_cache, cache_entry, cache_tmp) {
HASH_DEL(gpu_info->last_update_process_cache, cache_entry);
free(cache_entry);
}
}
free(gpu_infos);
gpu_infos = NULL;
amdgpu_count = 0;

if (libdrm_handle) {
dlclose(libdrm_handle);
libdrm_handle = NULL;
Expand All @@ -223,13 +246,6 @@ static void gpuinfo_amdgpu_shutdown(void) {
dlclose(libdrm_amdgpu_handle);
libdrm_amdgpu_handle = NULL;
}

struct gpu_info_amdgpu *allocated, *tmp;

list_for_each_entry_safe(allocated, tmp, &allocations, allocate_list) {
list_del(&allocated->allocate_list);
free(allocated);
}
}

static const char *gpuinfo_amdgpu_last_error_string(void) {
Expand Down Expand Up @@ -328,14 +344,12 @@ static bool gpuinfo_amdgpu_get_device_handles(
return false;

unsigned int libdrm_count = last_libdrm_return_status;
struct gpu_info_amdgpu *gpu_infos = calloc(libdrm_count, sizeof(*gpu_infos));
gpu_infos = calloc(libdrm_count, sizeof(*gpu_infos));
if (!gpu_infos) {
local_error_string = strerror(errno);
return false;
}

list_add(&gpu_infos[0].allocate_list, &allocations);

for (unsigned int i = 0; i < libdrm_count; i++) {
if (devs[i]->bustype != DRM_BUS_PCI ||
devs[i]->deviceinfo.pci->vendor_id != VENDOR_AMD)
Expand Down Expand Up @@ -391,25 +405,25 @@ static bool gpuinfo_amdgpu_get_device_handles(

uint32_t drm_major, drm_minor;
last_libdrm_return_status =
_amdgpu_device_initialize(fd, &drm_major, &drm_minor, &gpu_infos[*count].amdgpu_device);
_amdgpu_device_initialize(fd, &drm_major, &drm_minor, &gpu_infos[amdgpu_count].amdgpu_device);
} else {
// TODO: radeon suppport here
assert(false);
}

if (!last_libdrm_return_status) {
gpu_infos[*count].drmVersion = ver;
gpu_infos[*count].fd = fd;
gpu_infos[*count].base.vendor = &gpu_vendor_amdgpu;
gpu_infos[amdgpu_count].drmVersion = ver;
gpu_infos[amdgpu_count].fd = fd;
gpu_infos[amdgpu_count].base.vendor = &gpu_vendor_amdgpu;

snprintf(gpu_infos[*count].pdev, PDEV_LEN - 1, "%04x:%02x:%02x.%d",
devs[i]->businfo.pci->domain,
devs[i]->businfo.pci->bus,
devs[i]->businfo.pci->dev,
devs[i]->businfo.pci->func);
initDeviceSysfsPaths(&gpu_infos[*count]);
list_add_tail(&gpu_infos[*count].base.list, devices);
*count += 1;
initDeviceSysfsPaths(&gpu_infos[amdgpu_count]);
list_add_tail(&gpu_infos[amdgpu_count].base.list, devices);
amdgpu_count++;
} else {
_drmFreeVersion(ver);
close(fd);
Expand All @@ -418,6 +432,7 @@ static bool gpuinfo_amdgpu_get_device_handles(
}

_drmFreeDevices(devs, libdrm_count);
*count = amdgpu_count;

return true;
}
Expand Down

0 comments on commit 75c749d

Please sign in to comment.