Skip to content

Commit 03a2b52

Browse files
author
Deren Vural
committed
Updated MainWindow class
- Finished refresh_cards() function, now reads GPU name (if using SMI, SMI/SETTINGS or OPTIMUS) by calling the get_gpu_data() function on the provider object Updated Provider class - Added get_gpu_data() function Updated Processor class - Removed unused functions - Added some cases to the process() function, allowing gpu data queries - Added typing to argv arrays Signed-off-by: Deren Vural <[email protected]>
1 parent 7f8accd commit 03a2b52

File tree

3 files changed

+219
-40
lines changed

3 files changed

+219
-40
lines changed

src/mainwindow/imp.rs

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -496,19 +496,36 @@ impl MainWindow {
496496
// Construct a row for each GPU
497497
for uuid in gpu_uuids {
498498
// Get GPU data
499-
let gpu_name = "GPU NAME XXQ";//existing_provider.get_gpu_data(uuid, "name");
500-
501-
// Create new ActionRow object
502-
let current_row: ActionRow =
503-
ActionRow::builder()
504-
.title(gpu_name)
505-
.subtitle(&uuid)
506-
.activatable(true)
507-
.selectable(true)
508-
.build();
509-
510-
// Append new ActionRow object to GtkListBox
511-
self.cards_list.append(&current_row);
499+
match existing_provider.get_gpu_data(&uuid, "name") {
500+
Ok(gpu_name) => {
501+
// Create new ActionRow object
502+
let current_row: ActionRow =
503+
ActionRow::builder()
504+
.title(&gpu_name)
505+
.subtitle(&uuid)
506+
.activatable(true)
507+
.selectable(true)
508+
.build();
509+
510+
// Append new ActionRow object to GtkListBox
511+
self.cards_list.append(&current_row);
512+
},
513+
Err(err) => {
514+
println!("..Attempt to read GPU name failed, returning: {}", err);
515+
516+
// Create new ActionRow object
517+
let current_row: ActionRow =
518+
ActionRow::builder()
519+
.title(&uuid)
520+
.subtitle(&uuid)
521+
.activatable(true)
522+
.selectable(true)
523+
.build();
524+
525+
// Append new ActionRow object to GtkListBox
526+
self.cards_list.append(&current_row);
527+
}
528+
}
512529
}
513530
}
514531
Err(err) => println!("..Attempt to read GPU data failed, returning: {}", err),
@@ -538,19 +555,36 @@ impl MainWindow {
538555
// Construct a row for each GPU
539556
for uuid in gpu_uuids {
540557
// Get GPU data
541-
let gpu_name = "GPU NAME QXX";//existing_provider.get_gpu_data(uuid, "name");
542-
543-
// Create new ActionRow object
544-
let current_row: ActionRow =
545-
ActionRow::builder()
546-
.title(gpu_name)
547-
.subtitle(&uuid)
548-
.activatable(true)
549-
.selectable(true)
550-
.build();
551-
552-
// Append new ActionRow object to GtkListBox
553-
self.cards_list.append(&current_row);
558+
match new_provider.get_gpu_data(&uuid, "name") {
559+
Ok(gpu_name) => {
560+
// Create new ActionRow object
561+
let current_row: ActionRow =
562+
ActionRow::builder()
563+
.title(&gpu_name)
564+
.subtitle(&uuid)
565+
.activatable(true)
566+
.selectable(true)
567+
.build();
568+
569+
// Append new ActionRow object to GtkListBox
570+
self.cards_list.append(&current_row);
571+
},
572+
Err(err) => {
573+
println!("..Attempt to read GPU name failed, returning: {}", err);
574+
575+
// Create new ActionRow object
576+
let current_row: ActionRow =
577+
ActionRow::builder()
578+
.title(&uuid)
579+
.subtitle(&uuid)
580+
.activatable(true)
581+
.selectable(true)
582+
.build();
583+
584+
// Append new ActionRow object to GtkListBox
585+
self.cards_list.append(&current_row);
586+
}
587+
}
554588
}
555589
}
556590
Err(err) => println!("..Attempt to read GPU data failed, returning: {}", err),

src/processor/mod.rs

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,87 @@ impl Processor {
138138

139139
// Build OsStr array from vector (if matching a specific size)
140140
match call_stack_items.len() {
141+
6 => {
142+
//optirun nvidia-smi --query-gpu=gpu_name --format=csv,noheader -i uuid
143+
// Build array
144+
let argv: [&OsStr; 6] = [
145+
call_stack_items[0],
146+
call_stack_items[1],
147+
call_stack_items[2],
148+
call_stack_items[3],
149+
call_stack_items[4],
150+
call_stack_items[5],
151+
];
152+
153+
// Run process, get output
154+
match subprocess::exec_communicate(&argv, None::<&gio::Cancellable>) {
155+
Ok(return_val) => match return_val {
156+
// ACTUAL
157+
(None, None) => return Ok(None),
158+
159+
(None, Some(stderr_buffer)) => {
160+
println!(
161+
"Process failed with error: {}",
162+
String::from_utf8_lossy(&stderr_buffer)
163+
);
164+
}
165+
166+
(Some(stdout_buffer), None) => return Ok(Some(self.parse(&String::from_utf8_lossy(&stdout_buffer)))),
167+
168+
(Some(stdout_buffer), Some(stderr_buffer)) => {
169+
println!(
170+
"Process succeeded, but with error: {}",
171+
String::from_utf8_lossy(&stderr_buffer)
172+
);
173+
174+
return Ok(Some(self.parse(&String::from_utf8_lossy(&stdout_buffer))));
175+
}
176+
},
177+
Err(err) => return Err(err),
178+
};
179+
}
180+
5 => {
181+
//nvidia-smi --query-gpu=gpu_name --format=csv,noheader -i uuid
182+
// Build array
183+
let argv: [&OsStr; 5] = [
184+
call_stack_items[0],
185+
call_stack_items[1],
186+
call_stack_items[2],
187+
call_stack_items[3],
188+
call_stack_items[4],
189+
];
190+
191+
// Run process, get output
192+
match subprocess::exec_communicate(&argv, None::<&gio::Cancellable>) {
193+
Ok(return_val) => match return_val {
194+
// ACTUAL
195+
(None, None) => return Ok(None),
196+
197+
(None, Some(stderr_buffer)) => {
198+
println!(
199+
"Process failed with error: {}",
200+
String::from_utf8_lossy(&stderr_buffer)
201+
);
202+
}
203+
204+
(Some(stdout_buffer), None) => return Ok(Some(self.parse(&String::from_utf8_lossy(&stdout_buffer)))),
205+
206+
(Some(stdout_buffer), Some(stderr_buffer)) => {
207+
println!(
208+
"Process failed with error: {}",
209+
String::from_utf8_lossy(&stderr_buffer)
210+
);
211+
212+
return Ok(Some(self.parse(&String::from_utf8_lossy(&stdout_buffer))));
213+
}
214+
},
215+
Err(err) => return Err(err),
216+
};
217+
}
141218
4 => {
219+
//nvidia-settings -q GpuUUID -t
142220
// Build array
143-
let argv = [
221+
let argv: [&OsStr; 4] = [
144222
call_stack_items[0],
145223
call_stack_items[1],
146224
call_stack_items[2],
@@ -175,8 +253,9 @@ impl Processor {
175253
};
176254
}
177255
2 => {
256+
//??
178257
// Build array
179-
let argv = [call_stack_items[0], call_stack_items[1]];
258+
let argv: [&OsStr; 2] = [call_stack_items[0], call_stack_items[1]];
180259

181260
// Run process, get output
182261
match subprocess::exec_communicate(&argv, None::<&gio::Cancellable>) {
@@ -211,18 +290,6 @@ impl Processor {
211290
Ok(None)
212291
}
213292

214-
/*
215-
fn add_property(self, call_extension: &str) {
216-
todo!()
217-
//self.call.push(call_extension);
218-
}
219-
220-
fn get_name(self) -> () {//&str {
221-
todo!()
222-
//self.name.
223-
}
224-
*/
225-
226293
/*
227294
* Name:
228295
* parse

src/provider/mod.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,84 @@ impl Provider {
178178
}
179179
}
180180

181+
/*
182+
* Name:
183+
* get_gpu_data
184+
*
185+
* Description:
186+
* Grab gpu data from provider program given a GPU uuid and property name
187+
*
188+
* Made:
189+
* 30/10/2022
190+
*
191+
* Made by:
192+
* Deren Vural
193+
*
194+
* Notes:
195+
* Designed to be expanded on later when more data needed..
196+
*/
197+
pub fn get_gpu_data(&self, uuid: &str, property: &str) -> Result<String, String> {
198+
// Check provider type
199+
let processor_args: [String; 2];
200+
match self.property::<i32>("provider-type") {
201+
// Nvidia Settings+SMI / Nvidia SMI
202+
0 | 2 => {
203+
match property {
204+
"name" => {
205+
processor_args = [
206+
String::from("nvidia-smi"),
207+
String::from("--query-gpu=gpu_name --format=csv,noheader -i ") + uuid
208+
];
209+
}
210+
_ => {
211+
// Return error..
212+
return Err(String::from("Invalid property name, check provider preferences.."))
213+
}
214+
}
215+
}
216+
// Nvidia Optimus
217+
3 => {
218+
match property {
219+
"name" => {
220+
processor_args = [
221+
String::from("optirun"),
222+
String::from("nvidia-smi --query-gpu=gpu_name --format=csv,noheader -i ") + uuid
223+
];
224+
}
225+
_ => {
226+
// Return error..
227+
return Err(String::from("Invalid property name, check provider preferences.."))
228+
}
229+
}
230+
}
231+
_ => {
232+
// Return error..
233+
return Err(String::from("Invalid provider, check preferences.."))
234+
}
235+
}
236+
237+
// Create a processor object with appropriate args
238+
let processor: Processor = Processor::new(
239+
&processor_args[0],
240+
&processor_args[1],
241+
);
242+
243+
// Validate output
244+
match processor.process() {
245+
Ok(output) => match output {
246+
Some(valid_output) => {
247+
// If a valid output given, check if correct length (1)
248+
match valid_output.len() {
249+
1 => return Ok(String::from(valid_output[0].as_str())),
250+
_ => return Err(String::from("Process encountered an unknown error.."))
251+
}
252+
}
253+
None => return Err(String::from("Process encountered an unknown error.."))
254+
},
255+
Err(err) => return Err(String::from(err.message()))
256+
}
257+
}
258+
181259
/*
182260
* Name:
183261
* open_settings

0 commit comments

Comments
 (0)