Skip to content

Commit

Permalink
🧹 chwd: refactor printing funcs into binary-scope only
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Oct 27, 2024
1 parent 0d63560 commit cb831fc
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 168 deletions.
4 changes: 2 additions & 2 deletions src/console_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn handle_arguments_listing(data: &Data, args: &crate::args::Args) {
if args.list_available {
let pci_devices = &data.pci_devices;
if args.detail {
crate::device::print_available_profiles_in_detail(pci_devices);
crate::device_misc::print_available_profiles_in_detail(pci_devices);
} else {
for pci_device in pci_devices.iter() {
let available_profiles = &pci_device.get_available_profiles();
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn print_installed_profiles(installed_profiles: &[Profile]) {
}

for profile in installed_profiles.iter() {
crate::profile::print_profile_details(profile);
crate::profile_misc::print_profile_details(profile);
}
println!();
}
Expand Down
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub fn get_all_devices_of_profile(devices: &ListOfDevicesT, profile: &Profile) -
}

if let Some(gc_versions) = &profile.gc_versions {
if let Some(hwd_gc_versions) = crate::misc::get_gc_versions() {
if let Some(hwd_gc_versions) = crate::hwd_misc::get_gc_versions() {
return get_all_devices_from_gc_versions(devices, &hwd_gc_versions, gc_versions);
}
return vec![];
Expand Down
43 changes: 0 additions & 43 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::profile::Profile;
use crate::{console_writer, fl, profile};

use std::sync::Arc;

Expand Down Expand Up @@ -55,45 +54,3 @@ pub fn get_unique_devices(devices: &[Device]) -> Vec<Device> {

uniq_devices
}

pub fn print_available_profiles_in_detail(devices: &[Device]) {
let mut config_found = false;
for device in devices.iter() {
let available_profiles = &device.available_profiles;
let installed_profiles = &device.installed_profiles;
if available_profiles.is_empty() && installed_profiles.is_empty() {
continue;
}
config_found = true;

log::info!(
"{} {}: {} ({}:{}:{})",
"PCI",
fl!("device"),
device.sysfs_id,
device.class_id,
device.vendor_id,
device.device_id
);
println!(" {} {} {}", device.class_name, device.vendor_name, device.device_name);
println!();
if !installed_profiles.is_empty() {
println!(" > {}:\n", fl!("installed"));
for installed_profile in installed_profiles.iter() {
profile::print_profile_details(installed_profile);
}
println!("\n");
}
if !available_profiles.is_empty() {
println!(" > {}:\n", fl!("available"));
for available_profile in available_profiles.iter() {
profile::print_profile_details(available_profile);
}
println!("\n");
}
}

if !config_found {
console_writer::print_warn_msg!("no-profile-device");
}
}
60 changes: 60 additions & 0 deletions src/device_misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 2023-2024 Vladislav Nepogodin
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::device::Device;
use crate::{console_writer, fl, profile_misc};

pub fn print_available_profiles_in_detail(devices: &[Device]) {
let mut config_found = false;
for device in devices.iter() {
let available_profiles = &device.available_profiles;
let installed_profiles = &device.installed_profiles;
if available_profiles.is_empty() && installed_profiles.is_empty() {
continue;
}
config_found = true;

log::info!(
"{} {}: {} ({}:{}:{})",
"PCI",
fl!("device"),
device.sysfs_id,
device.class_id,
device.vendor_id,
device.device_id
);
println!(" {} {} {}", device.class_name, device.vendor_name, device.device_name);
println!();
if !installed_profiles.is_empty() {
println!(" > {}:\n", fl!("installed"));
for installed_profile in installed_profiles.iter() {
profile_misc::print_profile_details(installed_profile);
}
println!("\n");
}
if !available_profiles.is_empty() {
println!(" > {}:\n", fl!("available"));
for available_profile in available_profiles.iter() {
profile_misc::print_profile_details(available_profile);
}
println!("\n");
}
}

if !config_found {
console_writer::print_warn_msg!("no-profile-device");
}
}
113 changes: 113 additions & 0 deletions src/hwd_misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (C) 2023-2024 Vladislav Nepogodin
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

pub fn get_sysfs_busid_from_amdgpu_path(amdgpu_path: &str) -> &str {
amdgpu_path.split('/')
// Extract the 7th element (amdgpu id)
.nth(6)
.unwrap_or_default()
}

// returns Vec of ( sysfs busid, formatted GC version )
pub fn get_gc_versions() -> Option<Vec<(String, String)>> {
use std::fs;

let ip_match_paths = glob::glob("/sys/bus/pci/drivers/amdgpu/*/ip_discovery/die/*/GC/*/")
.expect("Failed to read glob pattern");

let gc_versions = ip_match_paths
.filter_map(Result::ok)
.filter_map(|path| path.to_str().map(|s| s.to_owned()))
.filter_map(|ip_match_path| {
let sysfs_busid = get_sysfs_busid_from_amdgpu_path(&ip_match_path).to_owned();

let major =
fs::read_to_string(format!("{ip_match_path}/major")).ok()?.trim().to_owned();
let minor =
fs::read_to_string(format!("{ip_match_path}/minor")).ok()?.trim().to_owned();
let revision =
fs::read_to_string(format!("{ip_match_path}/revision")).ok()?.trim().to_owned();

Some((sysfs_busid, format!("{major}.{minor}.{revision}")))
})
.collect::<Vec<_>>();

// Correctly check for empty Vec:
if gc_versions.is_empty() {
None
} else {
Some(gc_versions)
}
}

#[cfg(test)]
mod tests {
use crate::hwd_misc;

#[test]
fn gpu_from_amdgpu_path() {
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:c2:00.0/ip_discovery/die/0/GC/0/"
),
"0000:c2:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:c2:00.0/ip_discovery/die//"
),
"0000:c2:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/0000:c2:00.0/"),
"0000:c2:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:30:00.0/ip_discovery/die/0/GC/0"
),
"0000:30:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:30:00.0/ip_discovery/die//"
),
"0000:30:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/0000:30:00.0/"),
"0000:30:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:04:00.0/ip_discovery/die/0/GC/0"
),
"0000:04:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:04:00.0/ip_discovery/die//"
),
"0000:04:00.0"
);
assert_eq!(
hwd_misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/0000:04:00.0/"),
"0000:04:00.0"
);

assert_eq!(hwd_misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/"), "");
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
pub mod consts;
pub mod data;
pub mod device;
pub mod hwd_misc;
pub mod localization;
pub mod profile;
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

pub mod args;
pub mod console_writer;
pub mod device_misc;
pub mod logger;
pub mod misc;
pub mod profile_misc;

use chwd::profile::Profile;
use chwd::*;
Expand Down
93 changes: 0 additions & 93 deletions src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,6 @@ pub fn check_environment() -> Vec<String> {
missing_dirs
}

pub fn get_sysfs_busid_from_amdgpu_path(amdgpu_path: &str) -> &str {
amdgpu_path.split('/')
// Extract the 7th element (amdgpu id)
.nth(6)
.unwrap_or_default()
}

// returns Vec of ( sysfs busid, formatted GC version )
pub fn get_gc_versions() -> Option<Vec<(String, String)>> {
use std::fs;

let ip_match_paths = glob::glob("/sys/bus/pci/drivers/amdgpu/*/ip_discovery/die/*/GC/*/")
.expect("Failed to read glob pattern");

let gc_versions = ip_match_paths
.filter_map(Result::ok)
.filter_map(|path| path.to_str().map(|s| s.to_owned()))
.filter_map(|ip_match_path| {
let sysfs_busid = get_sysfs_busid_from_amdgpu_path(&ip_match_path).to_owned();

let major =
fs::read_to_string(format!("{ip_match_path}/major")).ok()?.trim().to_owned();
let minor =
fs::read_to_string(format!("{ip_match_path}/minor")).ok()?.trim().to_owned();
let revision =
fs::read_to_string(format!("{ip_match_path}/revision")).ok()?.trim().to_owned();

Some((sysfs_busid, format!("{major}.{minor}.{revision}")))
})
.collect::<Vec<_>>();

// Correctly check for empty Vec:
if gc_versions.is_empty() {
None
} else {
Some(gc_versions)
}
}

#[cfg(test)]
mod tests {
use crate::{misc, profile};
Expand All @@ -134,58 +95,4 @@ mod tests {
assert!(misc::find_profile("nvidia-dkm", &profiles).is_none());
assert!(misc::find_profile("nvidia-dkms.40xxcards", &profiles).is_some());
}

#[test]
fn gpu_from_amdgpu_path() {
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:c2:00.0/ip_discovery/die/0/GC/0/"
),
"0000:c2:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:c2:00.0/ip_discovery/die//"
),
"0000:c2:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/0000:c2:00.0/"),
"0000:c2:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:30:00.0/ip_discovery/die/0/GC/0"
),
"0000:30:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:30:00.0/ip_discovery/die//"
),
"0000:30:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/0000:30:00.0/"),
"0000:30:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:04:00.0/ip_discovery/die/0/GC/0"
),
"0000:04:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path(
"/sys/bus/pci/drivers/amdgpu/0000:04:00.0/ip_discovery/die//"
),
"0000:04:00.0"
);
assert_eq!(
misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/0000:04:00.0/"),
"0000:04:00.0"
);

assert_eq!(misc::get_sysfs_busid_from_amdgpu_path("/sys/bus/pci/drivers/amdgpu/"), "");
}
}
Loading

0 comments on commit cb831fc

Please sign in to comment.