Skip to content

Commit

Permalink
remove some usage of static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bjia56 committed Jan 18, 2025
1 parent ef8ccae commit ccef700
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
51 changes: 28 additions & 23 deletions src/linux/cosmotop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,41 +1654,44 @@ namespace Gpu {

namespace Intel {
// Attempts to initialize with Intel PMU, returning the device name if successful
static char *init_pmu() {
static string init_pmu() {
char *gpu_path = find_intel_gpu_dir();
if (!gpu_path) {
Logger::debug("Failed to find Intel GPU sysfs path, Intel GPUs will not be detected");
return nullptr;
return "";
}

char *gpu_device_id = get_intel_device_id(gpu_path);
free(gpu_path);
if (!gpu_device_id) {
Logger::debug("Failed to find Intel GPU device ID, Intel GPUs will not be detected");
return nullptr;
return "";
}

char *gpu_device_name = get_intel_device_name(gpu_device_id);
if (!gpu_device_name) {
char *dev_name = get_intel_device_name(gpu_device_id);
free(gpu_device_id);

string gpu_device_name;
if (dev_name) {
gpu_device_name = string(dev_name);
free(dev_name);
} else {
Logger::warning("Failed to find Intel GPU device name in internal database");
gpu_device_name = strdup("Intel GPU");
gpu_device_name = "Intel GPU"s;
}

free(gpu_device_id);

engines = discover_engines(device);
if (!engines) {
Logger::debug("Failed to find Intel GPU engines, Intel GPUs will not be detected");
free(gpu_device_name);
return nullptr;
return "";
}

int ret = pmu_init(engines);
if (ret) {
Logger::warning("Intel GPU: Failed to initialize PMU");
free(gpu_device_name);
free_engines(engines);
engines = nullptr;
return nullptr;
return "";
}

return gpu_device_name;
Expand Down Expand Up @@ -1720,11 +1723,11 @@ namespace Gpu {
}

// Attempts to initialize with intel_gpu_exporter REST api
static char *init_rest() {
static string init_rest() {
const auto rest_endpoint = Config::getS("intel_gpu_exporter");
if (rest_endpoint.empty()) {
Logger::debug("Fallback Intel GPU exporter not configured, Intel GPUs will not be detected");
return nullptr;
return "";
}

try {
Expand All @@ -1737,7 +1740,7 @@ namespace Gpu {
Logger::warning("Failed to get Intel GPU device ID from exporter");
delete exporter;
exporter = nullptr;
return nullptr;
return "";
}

// We get the value as a float, so need to convert it to hex string
Expand All @@ -1748,25 +1751,28 @@ namespace Gpu {
char *gpu_device_name = get_intel_device_name(ss.str().c_str());
if (!gpu_device_name) {
Logger::warning("Failed to find Intel GPU device name in internal database");
gpu_device_name = strdup("Intel GPU");
return "Intel GPU"s;
}

return gpu_device_name;
string result = string(gpu_device_name);
free(gpu_device_name);

return result;
} catch (const std::exception &e) {
Logger::warning("Failed to connect to Intel GPU exporter: "s + e.what());
if (exporter) delete exporter;
exporter = nullptr;
return nullptr;
return "";
}
}

bool init() {
if (initialized) return false;

char *gpu_device_name = init_pmu();
if (!gpu_device_name) {
string gpu_device_name = init_pmu();
if (gpu_device_name.empty()) {
gpu_device_name = init_rest();
if (!gpu_device_name) return false;
if (gpu_device_name.empty()) return false;
}

if (engines) {
Expand All @@ -1778,8 +1784,7 @@ namespace Gpu {
gpus.resize(gpus.size() + device_count);
gpu_names.resize(gpus.size() + device_count);

gpu_names[Nvml::device_count + Rsmi::device_count] = string(gpu_device_name);
free(gpu_device_name);
gpu_names[Nvml::device_count + Rsmi::device_count] = gpu_device_name;

initialized = true;
Intel::collect<1>(gpus.data() + Nvml::device_count + Rsmi::device_count);
Expand Down
9 changes: 6 additions & 3 deletions src/linux/intel_gpu_top/intel_name_lookup_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
#define VENDOR_FILE "vendor"
#define DEVICE_FILE "device"

// Caller must free the returned pointer
char* find_intel_gpu_dir() {
DIR *dir;
struct dirent *entry;
static char path[256];
char path[256];
char vendor_path[256];
char vendor_id[16];

Expand All @@ -57,7 +58,7 @@ char* find_intel_gpu_dir() {
closedir(dir);
// Return the parent directory (i.e., /sys/class/drm/card*)
snprintf(path, sizeof(path), "%s/%s", SYSFS_PATH, entry->d_name);
return path;
return strdup(path);
}
}
fclose(file);
Expand All @@ -69,8 +70,9 @@ char* find_intel_gpu_dir() {
return NULL; // Intel GPU not found
}

// Caller must free the returned pointer
char* get_intel_device_id(const char* gpu_dir) {
static char device_path[256];
char device_path[256];
char device_id[16];

// Construct the path to the device file
Expand All @@ -93,6 +95,7 @@ char* get_intel_device_id(const char* gpu_dir) {
return NULL;
}

// Caller must free the returned pointer
char *get_intel_device_name(const char *device_id) {
uint16_t devid = strtol(device_id, NULL, 16);
char dev_name[256];
Expand Down

0 comments on commit ccef700

Please sign in to comment.