Add support for getting the PCI ID database online

This commit is contained in:
Ilya Zlobintsev
2021-03-01 13:50:08 +02:00
parent 1d963646a5
commit ce3f26865a
4 changed files with 55 additions and 28 deletions

View File

@@ -15,4 +15,5 @@ log = "0.4"
env_logger = "0.8"
rand = "0.8"
signal-hook = "0.3"
pciid-parser = { git = "https://github.com/ilyazzz/pci-id-parser.git" }
pciid-parser = { git = "https://github.com/ilyazzz/pci-id-parser.git" }
reqwest = { version = "0.11", features = ["blocking", "json"] }

View File

@@ -75,6 +75,7 @@ impl GpuConfig {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Config {
pub gpu_configs: HashMap<u32, (GpuIdentifier, GpuConfig)>,
pub allow_online_update: Option<bool>,
pub config_path: PathBuf,
}
@@ -84,6 +85,7 @@ impl Config {
Config {
gpu_configs,
allow_online_update: None,
config_path: config_path.clone(),
}
}

View File

@@ -143,7 +143,7 @@ pub struct GpuController {
}
impl GpuController {
pub fn new(hw_path: PathBuf, config: GpuConfig) -> Self {
pub fn new(hw_path: PathBuf, config: GpuConfig, pci_db: &Option<PciDatabase>) -> Self {
let mut controller = GpuController {
hw_path: hw_path.clone(),
hw_mon: None,
@@ -151,7 +151,7 @@ impl GpuController {
gpu_info: GpuInfo::default(),
};
controller.gpu_info = controller.get_info_initial();
controller.gpu_info = controller.get_info_initial(pci_db);
controller.load_config(&config);
@@ -215,7 +215,7 @@ impl GpuController {
info
}
fn get_info_initial(&self) -> GpuInfo {
fn get_info_initial(&self, pci_db: &Option<PciDatabase>) -> GpuInfo {
let uevent =
fs::read_to_string(self.hw_path.join("uevent")).expect("Failed to read uevent");
@@ -277,17 +277,25 @@ impl GpuController {
let vulkan_info = GpuController::get_vulkan_info(&model_id);
let vendor_data = match PciDatabase::read() {
Ok(pci_db) => {
match pci_db.get_by_ids(&vendor_id, &model_id, &card_vendor_id, &card_model_id) {
let vendor_data = match pci_db {
Some(db) => {
match db.get_by_ids(&vendor_id, &model_id, &card_vendor_id, &card_model_id) {
Ok(data) => data,
Err(_) => VendorData::default(),
}
}
Err(_) => {
println!("pci.ids not found! Make sure you have 'hwdata' installed");
VendorData::default()
}
None => match PciDatabase::read() {
Ok(db) => {
match db.get_by_ids(&vendor_id, &model_id, &card_vendor_id, &card_model_id) {
Ok(data) => data,
Err(_) => VendorData::default(),
}
}
Err(err) => {
println!("{:?} pci.ids not found! Make sure you have 'hwdata' installed", err);
VendorData::default()
}
},
};
log::info!("Vendor data: {:?}", vendor_data);

View File

@@ -5,6 +5,7 @@ pub mod hw_mon;
use config::{Config, GpuConfig};
use gpu_controller::PowerProfile;
use pciid_parser::PciDatabase;
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use std::os::unix::net::{UnixListener, UnixStream};
@@ -91,15 +92,32 @@ impl Daemon {
log::trace!("Using config {:?}", config);
let mut gpu_controllers: HashMap<u32, GpuController> = HashMap::new();
let gpu_controllers = Self::load_gpu_controllers(&mut config);
/*for (gpu_identifier, gpu_config) in &config.gpu_configs {
let mut controller = GpuController::new(gpu_identifier.path.clone(), GpuConfig::new());
if controller.gpu_info.pci_slot == gpu_identifier.pci_id && controller.gpu_info.card_model == gpu_identifier.card_model && controller.gpu_info.gpu_model == gpu_identifier.gpu_model {
controller.load_config(gpu_config.clone());
gpu_controllers.insert(gpu_identifier.id, controller);
}
}*/
if !unprivileged {
config.save().unwrap();
}
Daemon {
listener,
gpu_controllers,
config,
}
}
fn load_gpu_controllers(config: &mut Config) -> HashMap<u32, GpuController> {
let pci_db = match config.allow_online_update {
Some(true) => match Self::get_pci_db_online() {
Ok(db) => Some(db),
Err(e) => {
log::info!("Error updating PCI db: {:?}", e);
None
}
},
Some(false) | None => None,
};
let mut gpu_controllers: HashMap<u32, GpuController> = HashMap::new();
'entries: for entry in
fs::read_dir("/sys/class/drm").expect("Could not open /sys/class/drm")
@@ -110,7 +128,7 @@ impl Daemon {
log::info!("Initializing {:?}", entry.path());
let mut controller =
GpuController::new(entry.path().join("device"), GpuConfig::new());
GpuController::new(entry.path().join("device"), GpuConfig::new(), &pci_db);
let gpu_info = &controller.get_info();
for (id, (gpu_identifier, gpu_config)) in &config.gpu_configs {
@@ -136,15 +154,13 @@ impl Daemon {
}
}
}
if !unprivileged {
config.save().unwrap();
}
Daemon {
listener,
gpu_controllers,
config,
}
gpu_controllers
}
fn get_pci_db_online() -> Result<PciDatabase, reqwest::Error> {
let vendors = reqwest::blocking::get("https://pci.endpoint.ml/devices.json")?.json()?;
Ok(PciDatabase { vendors })
}
pub fn listen(mut self) {