Added link speed info and ran eveyrthing through rustfmt

This commit is contained in:
Ilya Zlobintsev 2020-10-18 12:08:39 +03:00
parent 7709a92d73
commit 95167bbd73
9 changed files with 172 additions and 39 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
/target
Cargo.lock
*.glade\~

View File

@ -11,10 +11,10 @@ enum Opt {
fn main() {
let opt = Opt::from_args();
match opt {
Opt::Stats => {
Opt::Stats => {
let gpu_stats = daemon::get_gpu_stats();
println!("VRAM: {}/{}", gpu_stats.mem_used, gpu_stats.mem_total);
},
}
Opt::Info => {
let gpu_info = daemon::get_gpu_info();
println!("GPU Vendor: {}", gpu_info.gpu_vendor);

View File

@ -1,9 +1,9 @@
use std::os::unix::net::{UnixStream, UnixListener};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{Read, Write};
use std::os::unix::net::{UnixListener, UnixStream};
use std::process::Command;
use std::thread;
use std::fs;
use serde::{Serialize, Deserialize};
use crate::gpu_controller::GpuController;
use crate::SOCK_PATH;
@ -21,9 +21,7 @@ pub enum Action {
impl Daemon {
pub fn new(gpu_controller: GpuController) -> Daemon {
Daemon {
gpu_controller,
}
Daemon { gpu_controller }
}
pub fn run(self) {
@ -32,8 +30,12 @@ impl Daemon {
}
let listener = UnixListener::bind(SOCK_PATH).unwrap();
Command::new("chmod").arg("666").arg(SOCK_PATH).output().expect("Failed to chmod");
Command::new("chmod")
.arg("666")
.arg(SOCK_PATH)
.output()
.expect("Failed to chmod");
for stream in listener.incoming() {
let d = self.clone();
@ -54,13 +56,13 @@ impl Daemon {
stream.read_to_end(&mut buffer).unwrap();
println!("finished reading");
let action: Action = bincode::deserialize(&buffer).unwrap();
let response: Vec<u8> = match action {
Action::GetStats => bincode::serialize(&self.gpu_controller.get_stats()).unwrap(),
Action::GetInfo => bincode::serialize(&self.gpu_controller.gpu_info).unwrap(),
};
stream.write_all(&response).expect("Failed writing response");
stream
.write_all(&response)
.expect("Failed writing response");
}
}

View File

@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FanController {
@ -7,6 +7,8 @@ pub struct FanController {
impl FanController {
pub fn new(hwmon_path: &str) -> FanController {
FanController { hwmon_path: hwmon_path.to_string() }
FanController {
hwmon_path: hwmon_path.to_string(),
}
}
}
}

View File

@ -24,6 +24,9 @@ pub struct GpuInfo {
pub card_vendor: String,
pub driver: String,
pub vbios_version: String,
pub vram_size: u64, //in MiB
pub link_speed: String,
pub link_width: u8,
}
impl GpuController {
@ -39,14 +42,13 @@ impl GpuController {
}
fn get_info(hw_path: PathBuf) -> GpuInfo {
let uevent =
fs::read_to_string(hw_path.join("uevent")).expect("Failed to read uevent");
let uevent = fs::read_to_string(hw_path.join("uevent")).expect("Failed to read uevent");
//caps for raw values, lowercase for parsed
let mut driver = String::new();
let mut VENDOR_ID = String::new();
let mut MODEL_ID = String::new();
let mut CARD_VENDOR_ID= String::new();
let mut CARD_VENDOR_ID = String::new();
let mut CARD_MODEL_ID = String::new();
for line in uevent.split('\n') {
@ -57,12 +59,12 @@ impl GpuController {
let ids = split[1].split(':').collect::<Vec<&str>>();
VENDOR_ID = ids[0].to_string();
MODEL_ID = ids[1].to_string();
},
}
"PCI_SUBSYS_ID" => {
let ids = split[1].split(':').collect::<Vec<&str>>();
CARD_VENDOR_ID = ids[0].to_string();
CARD_MODEL_ID = ids[1].to_string();
},
}
_ => (),
}
}
@ -72,16 +74,23 @@ impl GpuController {
let mut card_vendor = String::new();
let mut card_model = String::new();
let full_hwid_list = fs::read_to_string("/usr/share/hwdata/pci.ids").expect("Could not read pci.ids. Perhaps the \"hwids\" package is not installed?");
let full_hwid_list = fs::read_to_string("/usr/share/hwdata/pci.ids")
.expect("Could not read pci.ids. Perhaps the \"hwids\" package is not installed?");
//some weird space character, don't touch
let pci_id_line = format!(" {}", MODEL_ID.to_lowercase());
let card_ids_line = format!(" {} {}", CARD_VENDOR_ID.to_lowercase(), CARD_MODEL_ID.to_lowercase());
let card_ids_line = format!(
" {} {}",
CARD_VENDOR_ID.to_lowercase(),
CARD_MODEL_ID.to_lowercase()
);
for line in full_hwid_list.split('\n') {
if line.len() > CARD_VENDOR_ID.len() {
if line[0..CARD_VENDOR_ID.len()] == CARD_VENDOR_ID.to_lowercase() {
card_vendor = line.splitn(2, ' ').collect::<Vec<&str>>()[1].trim_start().to_string();
card_vendor = line.splitn(2, ' ').collect::<Vec<&str>>()[1]
.trim_start()
.to_string();
}
}
if line.contains(&pci_id_line) {
@ -93,7 +102,28 @@ impl GpuController {
}
let vbios_version = fs::read_to_string(hw_path.join("vbios_version"))
.expect("Failed to read vbios_info").trim().to_string();
.expect("Failed to read vbios_info")
.trim()
.to_string();
let vram_size = fs::read_to_string(hw_path.join("mem_info_vram_total"))
.expect("Failed to read mem size")
.trim()
.parse::<u64>()
.unwrap()
/ 1024
/ 1024;
let link_speed = fs::read_to_string(hw_path.join("current_link_speed"))
.expect("Failed to read link speed")
.trim()
.to_string();
let link_width = fs::read_to_string(hw_path.join("current_link_width"))
.expect("Failed to read link width")
.trim()
.parse::<u8>()
.unwrap();
GpuInfo {
gpu_vendor: vendor,
@ -102,6 +132,9 @@ impl GpuController {
card_model,
driver,
vbios_version,
vram_size,
link_speed,
link_width,
}
}

View File

@ -1,5 +1,5 @@
use std::os::unix::net::UnixStream;
use std::io::{Read, Write};
use std::os::unix::net::UnixStream;
use gpu_controller::{GpuInfo, GpuStats};
@ -13,11 +13,15 @@ pub enum DaemonError {
ConnectionFailed,
}
pub fn get_gpu_stats() -> GpuStats{
pub fn get_gpu_stats() -> GpuStats {
let mut stream = UnixStream::connect(SOCK_PATH).expect("Failed to connect to daemon");
stream.write_all(&bincode::serialize(&daemon::Action::GetStats).unwrap()).unwrap();
stream.shutdown(std::net::Shutdown::Write).expect("Could not shut down");
stream
.write_all(&bincode::serialize(&daemon::Action::GetStats).unwrap())
.unwrap();
stream
.shutdown(std::net::Shutdown::Write)
.expect("Could not shut down");
let mut buffer = Vec::<u8>::new();
stream.read_to_end(&mut buffer).unwrap();
@ -27,15 +31,16 @@ pub fn get_gpu_stats() -> GpuStats{
pub fn get_gpu_info() -> Result<GpuInfo, DaemonError> {
match UnixStream::connect(SOCK_PATH) {
Ok(mut s) => {
s.write_all(&bincode::serialize(&daemon::Action::GetInfo).unwrap()).unwrap();
s.shutdown(std::net::Shutdown::Write).expect("Could not shut down");
s.write_all(&bincode::serialize(&daemon::Action::GetInfo).unwrap())
.unwrap();
s.shutdown(std::net::Shutdown::Write)
.expect("Could not shut down");
let mut buffer = Vec::<u8>::new();
s.read_to_end(&mut buffer).unwrap();
Ok(bincode::deserialize(&buffer).unwrap())
},
}
Err(_) => Err(DaemonError::ConnectionFailed),
}
}

View File

@ -6,4 +6,3 @@ fn main() {
let d = Daemon::new(gpu_controller);
d.run();
}

View File

@ -33,12 +33,22 @@ fn build_ui(application: &gtk::Application) {
.get_object("manufacturer_text_buffer")
.expect("Couldn't get textbuffer");
let vram_size_text_buffer: TextBuffer = builder
.get_object("vram_size_text_buffer")
.expect("Couldn't get textbuffer");
let link_speed_text_buffer: TextBuffer = builder
.get_object("link_speed_text_buffer")
.expect("Couldn't get textbuffer");
match daemon::get_gpu_info() {
Ok(gpu_info) => {
gpu_model_text_buffer.set_text(&gpu_info.card_model);
manufacturer_text_buffer.set_text(&gpu_info.card_vendor);
vbios_version_text_buffer.set_text(&gpu_info.vbios_version);
driver_text_buffer.set_text(&gpu_info.driver);
vram_size_text_buffer.set_text(&format!("{} MiB", &gpu_info.vram_size));
link_speed_text_buffer.set_text(&format!("{} x{}", &gpu_info.link_speed, &gpu_info.link_width));
}
Err(_) => {
MessageDialog::new(

View File

@ -8,12 +8,18 @@
<object class="GtkTextBuffer" id="gpu_model_text_buffer">
<property name="text">gpu_model</property>
</object>
<object class="GtkTextBuffer" id="link_speed_text_buffer">
<property name="text" translatable="yes">1.0 GT/s PCIe x1</property>
</object>
<object class="GtkTextBuffer" id="manufacturer_text_buffer">
<property name="text" translatable="yes">Manufacturer</property>
</object>
<object class="GtkTextBuffer" id="vbios_version_text_buffer">
<property name="text" translatable="yes">vbios_version</property>
</object>
<object class="GtkTextBuffer" id="vram_size_text_buffer">
<property name="text" translatable="yes">1024 MiB</property>
</object>
<object class="GtkWindow" id="main_window">
<property name="can-focus">False</property>
<property name="default-width">350</property>
@ -23,7 +29,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
<!-- n-columns=2 n-rows=4 -->
<!-- n-columns=2 n-rows=6 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
@ -171,6 +177,80 @@
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="margin-left">5</property>
<property name="margin-right">5</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="label" translatable="yes">Memory Size</property>
<property name="justify">fill</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<object class="GtkTextView">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-top">7</property>
<property name="hexpand">True</property>
<property name="editable">False</property>
<property name="justification">fill</property>
<property name="cursor-visible">False</property>
<property name="buffer">vram_size_text_buffer</property>
<property name="accepts-tab">False</property>
<property name="monospace">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="margin-left">5</property>
<property name="margin-right">5</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="label" translatable="yes">Link speed</property>
<property name="justify">fill</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
<object class="GtkTextView">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-top">7</property>
<property name="hexpand">True</property>
<property name="editable">False</property>
<property name="justification">fill</property>
<property name="cursor-visible">False</property>
<property name="buffer">link_speed_text_buffer</property>
<property name="accepts-tab">False</property>
<property name="monospace">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">5</property>
</packing>
</child>
</object>
</child>
<child type="tab">