Skip to content

Commit

Permalink
fixed amd gpu name detection
Browse files Browse the repository at this point in the history
  • Loading branch information
husseinhareb committed Aug 27, 2024
1 parent 796701e commit f75e263
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 12 deletions.
20 changes: 15 additions & 5 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ battery = "0.7.8"
libmedium = "0.8.1"
dirs = "5.0.1"
nvml-wrapper = "0.10.0"
ash = "0.38.0"
ash = "0.37.3"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
77 changes: 71 additions & 6 deletions src-tauri/src/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use ash::vk;
use ash::Entry;
use serde::{Deserialize, Serialize};
use nvml_wrapper::Nvml;
use nvml_wrapper::error::NvmlError;
use std::error::Error;
use std::ffi::CStr;
use std::fs::{read_dir, read_to_string};
use std::path::PathBuf;
use ash::vk;
use ash::Entry;
use std::ffi::CStr;
use std::ptr;

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -114,10 +114,10 @@ fn read_hwmon_info(

if let Ok(power_input) = read_to_string(hwmon_path.join("power1_average")) {
let power_mw = power_input.trim().parse::<u64>().unwrap_or(0);
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000.0)); // Divide by 1000 to convert mW to W
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000000.0));
} else if let Ok(power_input) = read_to_string(hwmon_path.join("power1_input")) {
let power_mw = power_input.trim().parse::<u64>().unwrap_or(0);
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000.0)); // Divide by 1000 to convert mW to W
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000000.0));
}
}
}
Expand All @@ -126,6 +126,71 @@ fn read_hwmon_info(
(fan_speed, temperature, clock_speed, wattage)
}

fn get_amd_gpu_name() -> String {
// Initialize Vulkan entry point
let entry = unsafe { Entry::load().expect("Failed to create Vulkan entry") };

// Create a Vulkan instance
let app_name = CStr::from_bytes_with_nul(b"AMD GPU Detector\0").unwrap();
let engine_name = CStr::from_bytes_with_nul(b"No Engine\0").unwrap();

let app_info = vk::ApplicationInfo {
s_type: vk::StructureType::APPLICATION_INFO,
p_next: ptr::null(),
p_application_name: app_name.as_ptr(),
p_engine_name: engine_name.as_ptr(),
application_version: vk::make_api_version(0, 1, 0, 0),
engine_version: vk::make_api_version(0, 1, 0, 0),
api_version: vk::make_api_version(0, 1, 0, 0),
};

let create_info = vk::InstanceCreateInfo {
s_type: vk::StructureType::INSTANCE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::InstanceCreateFlags::empty(),
p_application_info: &app_info,
enabled_layer_count: 0,
pp_enabled_layer_names: ptr::null(),
enabled_extension_count: 0,
pp_enabled_extension_names: ptr::null(),
};

let instance = unsafe {
entry
.create_instance(&create_info, None)
.expect("Failed to create Vulkan instance")
};

// Enumerate physical devices (GPUs)
let physical_devices = unsafe {
instance
.enumerate_physical_devices()
.expect("Failed to enumerate physical devices")
};

// Get the name of the first GPU
let device_name = if let Some(&device) = physical_devices.first() {
// Get device properties
let properties = unsafe { instance.get_physical_device_properties(device) };

// Convert the device name to a Rust String
unsafe {
CStr::from_ptr(properties.device_name.as_ptr())
.to_string_lossy()
.into_owned()
}
} else {
String::from("No GPU found")
};

// Clean up Vulkan instance
unsafe {
instance.destroy_instance(None);
}

device_name
}

fn get_amd_gpu_info() -> Result<GpuInformations, Box<dyn Error>> {
let drm_path = PathBuf::from("/sys/class/drm/");
let mut amd_gpu_path = None;
Expand Down Expand Up @@ -184,7 +249,7 @@ fn get_amd_gpu_info() -> Result<GpuInformations, Box<dyn Error>> {
let utilization = read_gpu_busy_percent(&device_path);

Ok(GpuInformations {
name: Some("AMD GPU".to_string()),
name: Some(get_amd_gpu_name()),
driver_version: None,
memory_total,
memory_used,
Expand Down

0 comments on commit f75e263

Please sign in to comment.